From: "cron2 (Code Review)" Date: Sun, 12 Apr 2026 14:44:05 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="lxzPNz4Y4ZA="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Optimise iterating over all clients by remembering highest peer id List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --lxzPNz4Y4ZA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#8) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1557?usp=3Demail ) Change subject: Optimise iterating over all clients by remembering highest = peer id ...................................................................... Optimise iterating over all clients by remembering highest peer id This keeps track of the highest peer id that is currently allocated to avoid iterating over the empty tail of the m->instances array. Change-Id: If797f3fe178fba3f43fb12898e5484bfb38f05c3 Signed-off-by: Arne Schwabe Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1557 Message-Id: <20260412125356.32261-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36= 577.html Signed-off-by: Gert Doering --- M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/push_util.c 3 files changed, 26 insertions(+), 10 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/57/1557/8 diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 0b03ff1..c7a91ab 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -601,6 +601,13 @@ if (mi->context.c2.tls_multi->peer_id !=3D MAX_PEER_ID) { m->instances[mi->context.c2.tls_multi->peer_id] =3D NULL; + + /* Adjust the max_peerid as this might have been the highest + * peer id instance */ + while (m->max_peerid > 0 && m->instances[m->max_peerid] =3D=3D= NULL) + { + m->max_peerid--; + } } schedule_remove_entry(m->schedule, (struct schedule_entry *)mi); @@ -652,7 +659,7 @@ { if (m->hash) { - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; if (mi) @@ -1326,7 +1333,7 @@ { int count =3D 0; - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; if (mi && mi !=3D new_mi && !mi->halt) @@ -2885,7 +2892,7 @@ #endif mb =3D mbuf_alloc_buf(buf); - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; @@ -3794,7 +3801,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server) { /* tell all clients to restart */ - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; if (mi && !mi->halt && proto_is_dgram(mi->context.c2.link_sockets[= 0]->info.proto)) @@ -3876,7 +3883,7 @@ struct multi_context *m =3D (struct multi_context *)arg; int count =3D 0; - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; if (mi && !mi->halt) @@ -3907,7 +3914,7 @@ maddr.proto =3D proto; if (mroute_extract_openvpn_sockaddr(&maddr, &saddr, true)) { - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *mi =3D m->instances[i]; if (mi && !mi->halt && mroute_addr_equal(&maddr, &mi->real)) @@ -4100,8 +4107,14 @@ } /* should not really end up here, since multi_create_instance returns = null - * if amount of clients exceeds max_clients */ + * if amount of clients exceeds max_clients and this method would then + * also not have been called */ ASSERT(mi->context.c2.tls_multi->peer_id < m->max_clients); + + if (mi->context.c2.tls_multi->peer_id > m->max_peerid) + { + m->max_peerid =3D mi->context.c2.tls_multi->peer_id; + } } #if defined(__GNUC__) || defined(__clang__) diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 3f4eefe..935bda1 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -160,9 +160,12 @@ */ struct multi_context { - struct multi_instance **instances; /**< Array of multi_instances. An i= nstance can be + struct multi_instance **instances; /**< Array of multi_instances with = the size of + * max_clients. An instance can be * accessed using peer-id as an ind= ex. */ - + uint32_t max_peerid; /**< highest currently allocated pe= er-id + * and maximum allocated/valid inde= x in + * instances */ struct hash *hash; /**< VPN tunnel instances indexed b= y real * address of the remote peer. */ struct hash *vhash; /**< VPN tunnel instances indexed by diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c index fe48091..f57f54d 100644 --- a/src/openvpn/push_util.c +++ b/src/openvpn/push_util.c @@ -317,7 +317,7 @@ int count =3D 0; - for (int i =3D 0; i < m->max_clients; i++) + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++) { struct multi_instance *curr_mi =3D m->instances[i]; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1557?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: If797f3fe178fba3f43fb12898e5484bfb38f05c3 Gerrit-Change-Number: 1557 Gerrit-PatchSet: 8 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel --lxzPNz4Y4ZA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #8 to the change originally created by plaisthos.<= /p>

Vi= ew Change

Optimise iterating over all clients by remem=
bering highest peer id

This keeps track of the highest peer id that = is currently allocated to avoid
iterating over the empty tail of the m-&= gt;instances array.

Change-Id: If797f3fe178fba3f43fb12898e5484bfb38f= 05c3
Signed-off-by: Arne Schwabe <arne@...1227...>
Gerrit URL: = https://gerrit.openvpn.net/c/openvpn/+/1557
Message-Id: <202604121253= 56.32261-1-gert@...1296...>
URL: https://www.mail-archive.com/openvpn= -devel@lists.sourceforge.net/msg36577.html
Signed-off-by: Gert Doering &= lt;gert@...1296...>
---
M src/openvpn/multi.c
M src/openvpn/mul= ti.h
M src/openvpn/push_util.c
3 files changed, 26 insertions(+), 10 = deletions(-)

git pull ssh://gerrit.openvpn.net:2=
9418/openvpn refs/changes/57/1557/8
diff --git a/src/openvpn/multi.=
c b/src/openvpn/multi.c
index 0b03ff1..c7a91ab 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/mult= i.c
@@ -601,6 +601,13 @@
if (mi-&g= t;context.c2.tls_multi->peer_id !=3D MAX_PEER_ID)
= {
m->instances[mi->context.c2.tls_mul= ti->peer_id] =3D NULL;
+
+ /= * Adjust the max_peerid as this might have been the highest
+ * peer id instance */<= /span>
+ while (m= ->max_peerid > 0 && m->instances[m->max_peerid] =3D=3D = NULL)
+ {<= /span>
+ m-&g= t;max_peerid--;
+ = }
}

= schedule_remove_entry(m->schedule, (struct schedule_entry *)mi);
@@ -652,7 +659,7 @@
{
i= f (m->hash)
{
- for (int i =3D 0; i < m->max_clients; i++)
+ for (uint32_t = i =3D 0; i <=3D m->max_peerid; i++)
{=
struct multi_instance *mi =3D m->instances[i];
if (mi)
@@ -1326,7 +1333,7 @@
{
int count =3D 0;

- = for (int i =3D 0; i < m->max_clients; i++)
+ for (uint32_t i =3D 0; i <= =3D m->max_peerid; i++)
{
= struct multi_instance *mi =3D m->instances[i]; if (mi && mi !=3D new_mi && !mi->= ;halt)
@@ -2885,7 +2892,7 @@
#endif=
mb =3D mbuf_alloc_buf(buf);

= - for (int i =3D 0; i <= m->max_clients; i++)
+ for (uint32_t i =3D 0; i <=3D m->max_peerid; i++)=
{
struct multi_instance *mi= =3D m->instances[i];

@@ -3794,7 +3801= ,7 @@
multi_push_restart_schedule_exit(struct multi_contex= t *m, bool next_server)
{
/* tell all= clients to restart */
= - for (int i =3D 0; i < m->max_clients; i++)
+ for (uint32_t i =3D 0; i <=3D m-&g= t;max_peerid; i++)
{
struct m= ulti_instance *mi =3D m->instances[i];
if (mi &= amp;& !mi->halt && proto_is_dgram(mi->context.c2.link_soc= kets[0]->info.proto))
@@ -3876,7 +3883,7 @@
struct multi_context *m =3D (struct multi_context *)arg;
int count =3D 0;

- for (int i =3D 0; i < m->max_clients; i+= +)
+ for (uint32_t= i =3D 0; i <=3D m->max_peerid; i++)
{ struct multi_instance *mi =3D m->instances[i]; if (mi && !mi->halt)
@@ -3907,7 = +3914,7 @@
maddr.proto =3D proto;
= if (mroute_extract_openvpn_sockaddr(&maddr, &saddr, true))<= br> {
- = for (int i =3D 0; i < m->max_clients; i++)
+ for (uint32_t i =3D 0; i <=3D m-&= gt;max_peerid; i++)
{
= struct multi_instance *mi =3D m->instances[i];
= if (mi && !mi->halt && mroute_addr_equal(&maddr= , &mi->real))
@@ -4100,8 +4107,14 @@
}


/* should not really end up = here, since multi_create_instance returns null
- * if amount of clients exceeds max_clients */=
+ * if amount of= clients exceeds max_clients and this method would then
+ * also not have been called */
ASSERT(mi->context.c2.tls_multi->peer_id < m->= ;max_clients);
+
+ if (mi->context.c= 2.tls_multi->peer_id > m->max_peerid)
+ {
+ m->max_peerid =3D mi->context.c2.tls_multi->p= eer_id;
+ }=
}

#if defined(__GNUC__) || de= fined(__clang__)
diff --git a/src/openvpn/multi.h b/src/ope= nvpn/multi.h
index 3f4eefe..935bda1 100644
= --- a/src/openvpn/multi.h
+++ b/src/openvpn/multi.h<= br>@@ -160,9 +160,12 @@
*/
struct = multi_context
{
- struct multi_instance **instances; /**< Array of multi_in= stances. An instance can be
+ struct multi_instance **instances; /**< Array of multi_insta= nces with the size of
+ * max_clients. An instance can = be
* accessed usin= g peer-id as an index. */
-
+ uint32_t ma= x_peerid; /**< highest currently allocated peer-id<= br>+ = * and maximum allocated/valid index in
+ *= instances */
struct hash *hash; /**&l= t; VPN tunnel instances indexed by real
= * address of the remote peer. */
= struct hash *vhash; /**< VPN tunnel instances indexed = by
diff --git a/src/openvpn/push_util.c b/src/openvpn/push_= util.c
index fe48091..f57f54d 100644
--- a/= src/openvpn/push_util.c
+++ b/src/openvpn/push_util.c

@@ -317,7 +317,7 @@

int c= ount =3D 0;

- for (int i =3D 0; i < m->max_clients; i++)
+ for (uint32_t i =3D 0; i <= =3D m->max_peerid; i++)

{
= struct multi_instance *curr_mi =3D m->instances[i];


To view, visit change 1557. To unsubscribe, o= r for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If797f3fe178fba3f43fb12898e5= 484bfb38f05c3
Gerrit-Change-Number: 1557
Gerrit-PatchSet: 8
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --lxzPNz4Y4ZA=--