From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, dgibson@redhat.com,
david@gibson.dropbear.id.au, jmaloy@redhat.com,
passt-dev@passt.top
Subject: [PATCH v5 03/10] fwd: Add entries of ARP/NDP cache table to a FIFO/LRU queue
Date: Fri, 5 Sep 2025 22:11:47 -0400 [thread overview]
Message-ID: <20250906021154.2760611-4-jmaloy@redhat.com> (raw)
In-Reply-To: <20250906021154.2760611-1-jmaloy@redhat.com>
Because we are going to do ARP/NDP lookup of many non-local
addresses the table might eventually fill up with placeholder
entries which will never be completed. We therefore need an
aging mechanism based on access frequency, so that we can
evict the least used entries when a new one is created when
the table is full.
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v5: - New
---
fwd.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 92 insertions(+)
diff --git a/fwd.c b/fwd.c
index 236e58e..ab49dba 100644
--- a/fwd.c
+++ b/fwd.c
@@ -46,6 +46,8 @@ struct mac_cache_entry {
unsigned char mac[ETH_ALEN];
struct timespec expiry;
uint32_t count;
+ struct mac_cache_entry *qprev;
+ struct mac_cache_entry *qnext;
/* Hash bucket chain */
struct mac_cache_entry *next;
@@ -55,6 +57,10 @@ struct mac_cache_table {
struct mac_cache_entry **buckets;
size_t nbuckets;
size_t size;
+
+ /* FIFO/LRU queue */
+ struct mac_cache_entry *qhead;
+ struct mac_cache_entry *qtail;
};
static struct mac_cache_table mac_cache;
@@ -81,6 +87,82 @@ static inline size_t fwd_mac_cache_bucket_idx(const struct ctx *c,
return ((size_t)h) & (nbuckets - 1);
}
+/**
+ * fwd_mac_cacheq_append_tail() - Unlink entry from LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_append_tail(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ e->qnext = NULL;
+ e->qprev = t->qtail;
+ if (t->qtail)
+ t->qtail->qnext = e;
+ else
+ t->qhead = e;
+ t->qtail = e;
+}
+
+/**
+ * fwd_mac_cacheq_unlink() - Unlink entry from LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_unlink(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ if (e->qprev)
+ e->qprev->qnext = e->qnext;
+ else
+ t->qhead = e->qnext;
+ if (e->qnext)
+ e->qnext->qprev = e->qprev;
+ else
+ t->qtail = e->qprev;
+ e->qprev = e->qnext = NULL;
+}
+
+/**
+ * fwd_mac_cacheq_move_to_tail() - Move entry to tail of LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_move_to_tail(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ if (t->qtail == e)
+ return;
+
+ fwd_mac_cacheq_unlink(e);
+ fwd_mac_cacheq_append_tail(e);
+}
+
+/**
+ * fwd_mac_cache_evict_one() - Remove entry from head of LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cache_evict_one(const struct ctx *c)
+{
+ struct mac_cache_entry **pp, *e, *cursor;
+ struct mac_cache_table *t = &mac_cache;
+ size_t idx;
+
+ e = t->qhead;
+ if (!e)
+ return;
+
+ idx = fwd_mac_cache_bucket_idx(c, &e->key, t->nbuckets);
+ for (pp = &t->buckets[idx]; ((cursor = *pp)); pp = &cursor->next) {
+ if (cursor != e)
+ continue;
+ *pp = cursor->next;
+ break;
+ }
+ free(e);
+ t->size--;
+}
+
/**
* timespec_before() - Check the relation between two pints in time
* @a: Point in time to be tested
@@ -179,6 +261,11 @@ static struct mac_cache_entry *fwd_mac_cache_add(const struct ctx *c,
e->next = t->buckets[idx];
t->buckets[idx] = e;
+ /* If needed, delete least recently used entry before adding new one */
+ if (t->size == MAC_CACHE_BUCKETS)
+ fwd_mac_cache_evict_one(c);
+ fwd_mac_cacheq_append_tail(e);
+
return e;
}
@@ -213,6 +300,9 @@ bool fwd_neigh_mac_get(const struct ctx *c, const union inany_addr *addr,
mac_entry_set_expiry(e, MAC_CACHE_RENEWAL);
}
+ /* Actively used entries must not be evicted from cache */
+ fwd_mac_cacheq_move_to_tail(e);
+
if (ret) {
memcpy(mac, e->mac, ETH_ALEN);
return true;
@@ -235,7 +325,9 @@ int fwd_mac_cache_init(void)
t->nbuckets = MAC_CACHE_BUCKETS;
t->buckets = calloc(t->nbuckets, sizeof(*t->buckets));
+ t->qhead = t->qtail = NULL;
t->size = 0;
+
return t->buckets ? 0 : -1;
}
--
@@ -46,6 +46,8 @@ struct mac_cache_entry {
unsigned char mac[ETH_ALEN];
struct timespec expiry;
uint32_t count;
+ struct mac_cache_entry *qprev;
+ struct mac_cache_entry *qnext;
/* Hash bucket chain */
struct mac_cache_entry *next;
@@ -55,6 +57,10 @@ struct mac_cache_table {
struct mac_cache_entry **buckets;
size_t nbuckets;
size_t size;
+
+ /* FIFO/LRU queue */
+ struct mac_cache_entry *qhead;
+ struct mac_cache_entry *qtail;
};
static struct mac_cache_table mac_cache;
@@ -81,6 +87,82 @@ static inline size_t fwd_mac_cache_bucket_idx(const struct ctx *c,
return ((size_t)h) & (nbuckets - 1);
}
+/**
+ * fwd_mac_cacheq_append_tail() - Unlink entry from LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_append_tail(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ e->qnext = NULL;
+ e->qprev = t->qtail;
+ if (t->qtail)
+ t->qtail->qnext = e;
+ else
+ t->qhead = e;
+ t->qtail = e;
+}
+
+/**
+ * fwd_mac_cacheq_unlink() - Unlink entry from LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_unlink(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ if (e->qprev)
+ e->qprev->qnext = e->qnext;
+ else
+ t->qhead = e->qnext;
+ if (e->qnext)
+ e->qnext->qprev = e->qprev;
+ else
+ t->qtail = e->qprev;
+ e->qprev = e->qnext = NULL;
+}
+
+/**
+ * fwd_mac_cacheq_move_to_tail() - Move entry to tail of LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cacheq_move_to_tail(struct mac_cache_entry *e)
+{
+ struct mac_cache_table *t = &mac_cache;
+
+ if (t->qtail == e)
+ return;
+
+ fwd_mac_cacheq_unlink(e);
+ fwd_mac_cacheq_append_tail(e);
+}
+
+/**
+ * fwd_mac_cache_evict_one() - Remove entry from head of LRU queue
+ * @c: Execution context
+ */
+static void fwd_mac_cache_evict_one(const struct ctx *c)
+{
+ struct mac_cache_entry **pp, *e, *cursor;
+ struct mac_cache_table *t = &mac_cache;
+ size_t idx;
+
+ e = t->qhead;
+ if (!e)
+ return;
+
+ idx = fwd_mac_cache_bucket_idx(c, &e->key, t->nbuckets);
+ for (pp = &t->buckets[idx]; ((cursor = *pp)); pp = &cursor->next) {
+ if (cursor != e)
+ continue;
+ *pp = cursor->next;
+ break;
+ }
+ free(e);
+ t->size--;
+}
+
/**
* timespec_before() - Check the relation between two pints in time
* @a: Point in time to be tested
@@ -179,6 +261,11 @@ static struct mac_cache_entry *fwd_mac_cache_add(const struct ctx *c,
e->next = t->buckets[idx];
t->buckets[idx] = e;
+ /* If needed, delete least recently used entry before adding new one */
+ if (t->size == MAC_CACHE_BUCKETS)
+ fwd_mac_cache_evict_one(c);
+ fwd_mac_cacheq_append_tail(e);
+
return e;
}
@@ -213,6 +300,9 @@ bool fwd_neigh_mac_get(const struct ctx *c, const union inany_addr *addr,
mac_entry_set_expiry(e, MAC_CACHE_RENEWAL);
}
+ /* Actively used entries must not be evicted from cache */
+ fwd_mac_cacheq_move_to_tail(e);
+
if (ret) {
memcpy(mac, e->mac, ETH_ALEN);
return true;
@@ -235,7 +325,9 @@ int fwd_mac_cache_init(void)
t->nbuckets = MAC_CACHE_BUCKETS;
t->buckets = calloc(t->nbuckets, sizeof(*t->buckets));
+ t->qhead = t->qtail = NULL;
t->size = 0;
+
return t->buckets ? 0 : -1;
}
--
2.50.1
next prev parent reply other threads:[~2025-09-06 2:12 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-06 2:11 [PATCH v5 00/10] Use true MAC address of LAN local remote hosts Jon Maloy
2025-09-06 2:11 ` [PATCH v5 01/10] netlink: add function to extract MAC addresses from NDP/ARP table Jon Maloy
2025-09-08 2:12 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 02/10] fwd: Added cache table for ARP/NDP contents Jon Maloy
2025-09-08 2:42 ` David Gibson
2025-09-09 15:02 ` Jon Maloy
2025-09-10 1:49 ` David Gibson
2025-09-08 9:57 ` David Gibson
2025-09-06 2:11 ` Jon Maloy [this message]
2025-09-08 2:51 ` [PATCH v5 03/10] fwd: Add entries of ARP/NDP cache table to a FIFO/LRU queue David Gibson
2025-09-06 2:11 ` [PATCH v5 04/10] arp/ndp: respond with true MAC address of LAN local remote hosts Jon Maloy
2025-09-08 3:04 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 05/10] flow: add MAC address of LAN local remote hosts to flow Jon Maloy
2025-09-08 3:07 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 06/10] udp: forward external source MAC address through tap interface Jon Maloy
2025-09-08 3:13 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 07/10] tcp: " Jon Maloy
2025-09-08 3:18 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 08/10] tap: change signature of function tap_push_l2h() Jon Maloy
2025-09-08 3:21 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 09/10] tcp: make tcp_rst_no_conn() respond with correct MAC address Jon Maloy
2025-09-08 3:29 ` David Gibson
2025-09-06 2:11 ` [PATCH v5 10/10] icmp: let icmp use mac address from flowside structure Jon Maloy
2025-09-08 3:35 ` David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250906021154.2760611-4-jmaloy@redhat.com \
--to=jmaloy@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=dgibson@redhat.com \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).