From 1248d8856a9f20caa601134b424c71b2c98e76b5 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Tue, 5 Jan 2016 21:42:39 +0200 Subject: flowtop: Use single function to update flow entry There is no need to have 2 separate handlers for the flow updating, so use the one which was used for flow refreshing. Significant change is that a new flow entry will be not added during update (i.e. on NFCT_T_UPDATE events) if it was not found in the list. But this case shoud never happen as there will always be an NFCT_T_NEW event before an NFCT_T_UPDATE event. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- flowtop.c | 84 +++++++++++++++++++++++++-------------------------------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/flowtop.c b/flowtop.c index 1d438ba..cc5ffa2 100644 --- a/flowtop.c +++ b/flowtop.c @@ -421,20 +421,6 @@ static struct flow_entry *flow_list_find_prev_id(const struct flow_list *fl, return NULL; } -static void flow_list_update_entry(struct flow_list *fl, - const struct nf_conntrack *ct) -{ - struct flow_entry *n; - - n = flow_list_find_id(fl, nfct_get_attr_u32(ct, ATTR_ID)); - if (n == NULL) { - flow_list_new_entry(fl, ct); - return; - } - - flow_entry_from_ct(n, ct); -} - static void flow_list_destroy_entry(struct flow_list *fl, const struct nf_conntrack *ct) { @@ -1373,34 +1359,6 @@ static void presenter(void) lookup_cleanup(LT_PORTS_TCP); } -static int flow_event_cb(enum nf_conntrack_msg_type type, - struct nf_conntrack *ct, void *data __maybe_unused) -{ - if (sigint) - return NFCT_CB_STOP; - - synchronize_rcu(); - spinlock_lock(&flow_list.lock); - - switch (type) { - case NFCT_T_NEW: - flow_list_new_entry(&flow_list, ct); - break; - case NFCT_T_UPDATE: - flow_list_update_entry(&flow_list, ct); - break; - case NFCT_T_DESTROY: - flow_list_destroy_entry(&flow_list, ct); - break; - default: - break; - } - - spinlock_unlock(&flow_list.lock); - - return NFCT_CB_CONTINUE; -} - static void restore_sysctl(void *obj) { struct sysctl_params_ctx *sysctl_ctx = obj; @@ -1463,17 +1421,10 @@ static void flow_entry_filter(struct flow_entry *n) n->is_visible = true; } -static int flow_update_cb(enum nf_conntrack_msg_type type, - struct nf_conntrack *ct, void *data __maybe_unused) +static int flow_list_update_entry(struct flow_list *fl, struct nf_conntrack *ct) { struct flow_entry *n; - if (type != NFCT_T_UPDATE) - return NFCT_CB_CONTINUE; - - if (sigint) - return NFCT_CB_STOP; - n = flow_list_find_id(&flow_list, nfct_get_attr_u32(ct, ATTR_ID)); if (!n) return NFCT_CB_CONTINUE; @@ -1486,6 +1437,37 @@ static int flow_update_cb(enum nf_conntrack_msg_type type, return NFCT_CB_CONTINUE; } +static int flow_event_cb(enum nf_conntrack_msg_type type, + struct nf_conntrack *ct, void *data __maybe_unused) +{ + if (sigint) + return NFCT_CB_STOP; + + synchronize_rcu(); + spinlock_lock(&flow_list.lock); + + switch (type) { + case NFCT_T_NEW: + flow_list_new_entry(&flow_list, ct); + break; + case NFCT_T_UPDATE: + flow_list_update_entry(&flow_list, ct); + break; + case NFCT_T_DESTROY: + flow_list_destroy_entry(&flow_list, ct); + break; + default: + break; + } + + spinlock_unlock(&flow_list.lock); + + if (sigint) + return NFCT_CB_STOP; + + return NFCT_CB_CONTINUE; +} + static void collector_refresh_flows(struct nfct_handle *handle) { struct flow_entry *n; @@ -1653,7 +1635,7 @@ static void *collector(void *null __maybe_unused) if (!ct_update) panic("Cannot create a nfct handle: %s\n", strerror(errno)); - nfct_callback_register(ct_update, NFCT_T_ALL, flow_update_cb, NULL); + nfct_callback_register(ct_update, NFCT_T_ALL, flow_event_cb, NULL); poll_fd[0].fd = nfct_fd(ct_event); poll_fd[0].events = POLLIN; -- cgit v1.2.3-54-g00ecf