Skip to contentSkip to conversation

dco-freebsd: use m->instances[] instead of m->hash

PATCH In review Antonio Quartulli 4 participants 6 messages Mar 22, 2023 – Mar 23, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 dco-freebsd: use m->instances[] instead of m->hash

When retrieving the multi_instance of a specific peer, there is no need to peform a linear search across the whole m->hash list. We can directly access the needed object via m->instances[peer-id] in constant time (and just one line of code).

Adapt the dco-freebsd code to do so.

Cc Kristof Provost <[email protected]...>
Change-Id I8d8af6f872146604a9710edf443db65df48ac3cb

---
NOTE: not tested because I have no FreeBSD environment

Changes from v1:
* added boundary check on peer-id

Changes from v2:
* use one check only instead of two

---
 src/openvpn/dco_freebsd.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
src/openvpn/dco_freebsd.c +7 -16
index 225b3cf8..54ec16ff 100644
@@ -674,27 +674,18 @@ dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
 static void
 dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *nvl)
 {
-    struct hash_element *he;
-    struct hash_iterator hi;
 
-    hash_iterator_init(m->hash, &hi);
-
-    while ((he = hash_iterator_next(&hi)))
+    if (peer_id >= m->max_clients || !m->instances[peer_id])
     {
-        struct multi_instance *mi = (struct multi_instance *) he->value;
-
-        if (mi->context.c2.tls_multi->peer_id != peerid)
-        {
-            continue;
-        }
-
-        mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
-        mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
-
+        msg(M_WARN, "Cannot store DCO stats for peer %u",
+            peerid);
         return;
     }
 
-    msg(M_INFO, "Peer %d returned by kernel, but not found locally", peerid);
+    struct multi_instance *mi = m->instances[peer_id];
+
+    mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
+    mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
 }
 
 int
Signature · 1 line
2.39.2
#1 lore raw
Quoted 28 lines · Antonio Quartulli

When retrieving the multi_instance of a specific peer, there is no need to peform a linear search across the whole m->hash list. We can directly access the needed object via m->instances[peer-id] in constant time (and just one line of code).

Adapt the dco-freebsd code to do so.

Cc Kristof Provost <[email protected]...>
Change-Id I8d8af6f872146604a9710edf443db65df48ac3cb

---
NOTE: not tested because I have no FreeBSD environment

Changes from v1:
* added boundary check on peer-id

Changes from v2:
* use one check only instead of two

---
 src/openvpn/dco_freebsd.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
src/openvpn/dco_freebsd.c +0 -0
index 225b3cf8..54ec16ff 100644

src/openvpn/dco_freebsd.c dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) · line 675

Quoted 2 lines
src/openvpn/dco_freebsd.c +0 -0
@@ -674,27 +674,18 @@ dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
 static void
 dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *nvl)

‘peerid’ here

src/openvpn/dco_freebsd.c dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) · line 678

Quoted 8 lines
src/openvpn/dco_freebsd.c +1 -5
 {
-    struct hash_element *he;
-    struct hash_iterator hi;
 
-    hash_iterator_init(m->hash, &hi);
-
-    while ((he = hash_iterator_next(&hi)))
+    if (peer_id >= m->max_clients || !m->instances[peer_id])

But ‘peer_id’ everywhere else.

So that needs to be fixed, because this version doesn’t build.

Other than that I’m happy with this change.

Best regards,
Kristof

#2 lore raw

PATCH v4 1/1 dco-freebsd: use m->instances[] instead of m->hash

In reply to Antonio Quartulli

From: Antonio Quartulli <[email protected]...>

When retrieving the multi_instance of a specific peer, there is no need to peform a linear search across the whole m->hash list. We can directly access the needed object via m->instances[peer-id] in constant time (and just one line of code).

Adapt the dco-freebsd code to do so.

v4: use "peerid" everywhere as that's what FreeBSD does, change message text

Cc Kristof Provost <[email protected]...>
Change-Id I8d8af6f872146604a9710edf443db65df48ac3cb
---
 src/openvpn/dco_freebsd.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
src/openvpn/dco_freebsd.c +6 -16
index 225b3cf8..a334d5d2 100644
@@ -674,27 +674,17 @@ dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
 static void
 dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *nvl)
 {
-    struct hash_element *he;
-    struct hash_iterator hi;
 
-    hash_iterator_init(m->hash, &hi);
-
-    while ((he = hash_iterator_next(&hi)))
+    if (peerid >= m->max_clients || !m->instances[peerid])
     {
-        struct multi_instance *mi = (struct multi_instance *) he->value;
-
-        if (mi->context.c2.tls_multi->peer_id != peerid)
-        {
-            continue;
-        }
-
-        mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
-        mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
-
+        msg(M_WARN, "dco_update_peer_stat: invalid peer ID %d returned by kernel", peerid);
         return;
     }
 
-    msg(M_INFO, "Peer %d returned by kernel, but not found locally", peerid);
+    struct multi_instance *mi = m->instances[peerid];
+
+    mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
+    mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
 }
 
 int
Signature · 1 line
2.39.2
#3 lore raw

Hi,

Quoted 36 lines · Gert Doering

From: Antonio Quartulli <[email protected]...>

When retrieving the multi_instance of a specific peer, there is no need to peform a linear search across the whole m->hash list. We can directly access the needed object via m->instances[peer-id] in constant time (and just one line of code).

Adapt the dco-freebsd code to do so.

v4: use "peerid" everywhere as that's what FreeBSD does, change message text

Cc Kristof Provost <[email protected]...>
Change-Id I8d8af6f872146604a9710edf443db65df48ac3cb
---
  src/openvpn/dco_freebsd.c | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
src/openvpn/dco_freebsd.c +1 -6
index 225b3cf8..a334d5d2 100644
@@ -674,27 +674,17 @@ dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
  static void
  dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *nvl)
  {
-    struct hash_element *he;
-    struct hash_iterator hi;
 
-    hash_iterator_init(m->hash, &hi);
-
-    while ((he = hash_iterator_next(&hi)))
+    if (peerid >= m->max_clients || !m->instances[peerid])
      {
-        struct multi_instance *mi = (struct multi_instance *) he->value;

src/openvpn/dco_freebsd.c dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) · line 680

Quoted 10 lines
src/openvpn/dco_freebsd.c +1 -9
-
-        if (mi->context.c2.tls_multi->peer_id != peerid)
-        {
-            continue;
-        }
-
-        mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
-        mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
-
+        msg(M_WARN, "dco_update_peer_stat: invalid peer ID %d returned by kernel", peerid);

I'd prefer to use __func__ here, but it is noted that dco_freebsd.c seldomly uses this approach. For this reason I think hardcoding the function name is fine for now.

Later on we can do a full cleanup.

src/openvpn/dco_freebsd.c dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) · line 690

Quoted 11 lines
src/openvpn/dco_freebsd.c +4 -1
          return;
      }
 
-    msg(M_INFO, "Peer %d returned by kernel, but not found locally", peerid);
+    struct multi_instance *mi = m->instances[peerid];
+
+    mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
+    mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
  }
 
  int

This patch is basically my v3 with the peerid fixed and the message changed.

I can't ACK since this patch bears my own signature, but I am fine with the applied modification.

Cheers,

Signature · 1 line
Antonio Quartulli
Acked-by on v4 1/1
#4 lore raw
In reply to Gert Doering

Am 23.03.23 um 09:03 schrieb Gert Doering:

Quoted 47 lines

From: Antonio Quartulli <[email protected]...>

When retrieving the multi_instance of a specific peer, there is no need to peform a linear search across the whole m->hash list. We can directly access the needed object via m->instances[peer-id] in constant time (and just one line of code).

Adapt the dco-freebsd code to do so.

v4: use "peerid" everywhere as that's what FreeBSD does, change message text

Cc Kristof Provost <[email protected]...>
Change-Id I8d8af6f872146604a9710edf443db65df48ac3cb
---
  src/openvpn/dco_freebsd.c | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
src/openvpn/dco_freebsd.c +2 -15
index 225b3cf8..a334d5d2 100644
@@ -674,27 +674,17 @@ dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
  static void
  dco_update_peer_stat(struct multi_context *m, uint32_t peerid, const nvlist_t *nvl)
  {
-    struct hash_element *he;
-    struct hash_iterator hi;
 
-    hash_iterator_init(m->hash, &hi);
-
-    while ((he = hash_iterator_next(&hi)))
+    if (peerid >= m->max_clients || !m->instances[peerid])
      {
-        struct multi_instance *mi = (struct multi_instance *) he->value;
-
-        if (mi->context.c2.tls_multi->peer_id != peerid)
-        {
-            continue;
-        }
-
-        mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
-        mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
-
+        msg(M_WARN, "dco_update_peer_stat: invalid peer ID %d returned by kernel", peerid);
          return;

src/openvpn/dco_freebsd.c dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) · line 690

Quoted 10 lines
src/openvpn/dco_freebsd.c +4 -1
      }
 
-    msg(M_INFO, "Peer %d returned by kernel, but not found locally", peerid);
+    struct multi_instance *mi = m->instances[peerid];
+
+    mi->context.c2.dco_read_bytes = nvlist_get_number(nvl, "in");
+    mi->context.c2.dco_write_bytes = nvlist_get_number(nvl, "out");
  }
 
  int
#5 lore raw
In reply to Gert Doering

As this patch has a bit of mixed history "who wrote it, who ACKed it, what happened afterwards" I decided to record the ACK from Arne and Kristof.

v4 has been tested on FreeBSD with DCO enabled, p2mp udp server, one client being connected all the time and the other client reconnecting (moving between peer-id 1 and 2), packets going back and forth, looking at the resulting counters. Everything looked as expected.

(This is basically an optimization that was discussed in November already but nobody followed up on it - and with the upcoming Linux DCO counter patches, the topic "why use iterator when you have an array?" resurfaced - thanks, Antonio, for covering FreeBSD too)

Your patch has been applied to the master and release/2.6 branch.

commit 03145f223236df90b35d1db444319fd3f785792b (master) commit 5acefd944c6a8a4338b5105ffe6b9ffce6bad330 (release/2.6)

Author: Antonio Quartulli
Date:   Thu Mar 23 09:03:41 2023 +0100
     dco-freebsd: use m->instances[] instead of m->hash

     Signed-off-by: Antonio Quartulli <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Acked-by: Kristof Provost <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering