From cc3ff62fae755a03935efe559f0130495e857aad Mon Sep 17 00:00:00 2001
From: Anjan Chanda <anjan.chanda@iopsys.eu>
Date: Wed, 31 May 2023 13:26:45 +0200
Subject: [PATCH] neigh_history_enqueue() - pass 'struct neigh_entry' instead
 of arg list

---
 src/neigh.c    | 72 ++++++++++++++++++++++----------------------------
 src/neigh.h    |  2 +-
 src/netlink.c  | 12 +++------
 src/topology.h |  4 +--
 src/ubus.c     |  7 ++---
 5 files changed, 40 insertions(+), 57 deletions(-)

diff --git a/src/neigh.c b/src/neigh.c
index 6f7beeb..4018b25 100644
--- a/src/neigh.c
+++ b/src/neigh.c
@@ -163,58 +163,53 @@ int neigh_history_entry_update_lastseen(void *priv, uint8_t *macaddr)
 }
 #endif
 
-int neigh_history_enqueue(void *priv, uint8_t *macaddr, const char *hostname,
-			  enum neigh_type type, struct neigh_wanstats *ws,
-			  uint32_t timeout)
+int neigh_history_enqueue(void *priv, struct neigh_entry *n, uint32_t timeout)
 {
 	struct topologyd_private *p = (struct topologyd_private *)priv;
-	struct neigh_history_entry *e = NULL;
+	struct neigh_history_entry *he = NULL;
 
 
-	e = neigh_history_lookup(&p->neigh_q, macaddr);
-	if (e) {
+	he = neigh_history_lookup(&p->neigh_q, n->macaddr);
+	if (he) {
 		/* stop ageing history as neigh is live */
-		e->timeout = timeout;
-		timer_del(&e->delete_timer);
-		e->alive = true;
+		he->timeout = timeout;
+		timer_del(&he->delete_timer);
+		he->alive = true;
 
 		/* set neigh type iff wifi type determined */
-		if (type == NEIGH_TYPE_WIFI)
-			e->type = type;
+		if (n->type == NEIGH_TYPE_WIFI)
+			he->type = n->type;
 
-		if (hostname && hostname[0] != '\0')
-			strncpy(e->hostname, hostname, 255);
+		if (n->hostname && n->hostname[0] != '\0')
+			strncpy(he->hostname, n->hostname, 255);
 
-		if (ws) {
-			dbg("Update history stats: +ul-pkts = %llu, +dl-pkts = %llu\n",
-			    ws->ul_packets, ws->dl_packets);
+		dbg("Update history stats: +ul-pkts = %llu, +dl-pkts = %llu\n",
+		    n->ws.ul_packets, n->ws.dl_packets);
 
-			e->ws.ul_packets += ws->ul_packets;
-			e->ws.ul_bytes += ws->ul_bytes;
-			e->ws.dl_packets += ws->dl_packets;
-			e->ws.dl_bytes += ws->dl_bytes;
-		}
+		he->ws.ul_packets += n->ws.ul_packets;
+		he->ws.ul_bytes += n->ws.ul_bytes;
+		he->ws.dl_packets += n->ws.dl_packets;
+		he->ws.dl_bytes += n->ws.dl_bytes;
 		return 0;
 	}
 
-	dbg("No history for " MACFMT " found! Add new..\n", MAC2STR(macaddr));
+	dbg("No history cache for " MACFMT " found! Add new..\n", MAC2STR(n->macaddr));
 
-	e = neigh_history_entry_create(priv, macaddr, hostname, type, 0, 0, timeout);
-	if (e) {
-		int idx = neigh_hash(macaddr);
+	he = neigh_history_entry_create(priv, n->macaddr, n->hostname, n->type,
+					0, 0, timeout);
+	if (he) {
+		int idx = neigh_hash(n->macaddr);
 
 		/* stop ageing history as neigh is live */
-		e->timeout = timeout;
-		timer_del(&e->delete_timer);
-		e->alive = true;
-		if (ws) {
-			e->ws.ul_packets = ws->ul_packets;
-			e->ws.ul_bytes = ws->ul_bytes;
-			e->ws.dl_packets = ws->dl_packets;
-			e->ws.dl_bytes = ws->dl_bytes;
-		}
-
-		hlist_add_head(&e->hlist, &p->neigh_q.history[idx]);
+		he->timeout = timeout;
+		timer_del(&he->delete_timer);
+		he->alive = true;
+		he->ws.ul_packets = n->ws.ul_packets;
+		he->ws.ul_bytes = n->ws.ul_bytes;
+		he->ws.dl_packets = n->ws.dl_packets;
+		he->ws.dl_bytes = n->ws.dl_bytes;
+
+		hlist_add_head(&he->hlist, &p->neigh_q.history[idx]);
 		p->neigh_q.num_history++;
 		return 1;
 	}
@@ -869,11 +864,8 @@ struct neigh_entry *neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state,
 
 #if 0
 		/* add/update history entry for this neigh */
-		neigh_history_enqueue(priv, e->macaddr, e->hostname, e->type,
-				      &e->ws,
-				      NEIGH_HISTORY_AGEOUT_DEFAULT);
+		neigh_history_enqueue(priv, e, NEIGH_HISTORY_AGEOUT_DEFAULT);
 #endif
-
 		return NULL;
 	}
 
diff --git a/src/neigh.h b/src/neigh.h
index c6e901b..2fa923b 100644
--- a/src/neigh.h
+++ b/src/neigh.h
@@ -72,7 +72,7 @@ struct neigh_entry {
 	int unreachable;		/* mark entry nonreachable */
 	int delete_pending;
 	atimer_t delete_timer;
-	uint32_t ageing_time;    /* in msecs */
+	uint32_t ageing_time;           /* in msecs */
 	struct timeval ageing_tmo;
 	int probe_cnt;
 
diff --git a/src/netlink.c b/src/netlink.c
index 22faf09..67551c1 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -210,10 +210,8 @@ static int topologyd_handle_neigh_tbl_change(struct topologyd_private *priv, boo
 		/* new neigh added */
 		topology_get_neigh_hostname(&priv->neigh_q, macaddr);
 
-		/* add/update history entry for this neigh */
-		neigh_history_enqueue(priv, new->macaddr, new->hostname,
-				      new->type, &new->ws,
-				      NEIGH_HISTORY_AGEOUT_DEFAULT);
+		/* add/update history cache for this neigh */
+		neigh_history_enqueue(priv, new, NEIGH_HISTORY_AGEOUT_DEFAULT);
 	}
 
 	/* update bridge port_nos on which the hosts are last seen */
@@ -634,10 +632,8 @@ int topologyd_get_known_neighbors(struct topologyd_private *priv, char *ifname)
 				/* new neigh added */
 				ret = topology_get_neigh_hostname(&priv->neigh_q, hwaddr);  //TODO: cond
 
-				/* add/update history entry for this neigh */
-				neigh_history_enqueue(priv, new->macaddr, new->hostname,
-						      new->type, &new->ws,
-						      NEIGH_HISTORY_AGEOUT_DEFAULT);
+				/* add/update history cache for this neigh */
+				neigh_history_enqueue(priv, new, NEIGH_HISTORY_AGEOUT_DEFAULT);
 			}
 
 			nl_object_put((struct nl_object *)neigh);
diff --git a/src/topology.h b/src/topology.h
index 812cae2..cd51c81 100644
--- a/src/topology.h
+++ b/src/topology.h
@@ -313,9 +313,7 @@ int nfct_get_entries_nolo(struct topologyd_private *priv);
 
 
 int neigh_history_entry_add(void *priv, struct neigh_history_entry *he);
-int neigh_history_enqueue(void *priv, uint8_t *macaddr, const char *hostname,
-			  enum neigh_type type, struct neigh_wanstats *ws,
-			  uint32_t timeout);
+int neigh_history_enqueue(void *priv, struct neigh_entry *e, uint32_t timeout);
 
 int neigh_history_load_from_json_file(void *priv, const char *file);
 int neigh_history_store_to_json_file(void *priv, const char *file);
diff --git a/src/ubus.c b/src/ubus.c
index 336b080..f909fd2 100644
--- a/src/ubus.c
+++ b/src/ubus.c
@@ -770,11 +770,8 @@ static void topologyd_wifi_sta_event_handler(struct topologyd_private *p,
 				/* new wifi neigh added */
 				topology_get_neigh_hostname(&p->neigh_q, macaddr);
 
-				/* add/update history entry for this neigh */
-				neigh_history_enqueue(p, new->macaddr,
-						      new->hostname,
-						      new->type,
-						      &new->ws,
+				/* add/update history cache for this neigh */
+				neigh_history_enqueue(p, new,
 						      NEIGH_HISTORY_AGEOUT_DEFAULT);
 			}
 		} else if (del) {
-- 
GitLab