From: "plaisthos (Code Review)" Date: Thu, 5 Mar 2026 14:36:27 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <4e81dd5e46482d3e60775d6cfaf18e815fb53a64-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="B+ePSEPsrMY="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Optimise performance of iterating over all client by remembering high... List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openvpn-devel --B+ePSEPsrMY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable plaisthos has uploaded this change for review. ( http://gerrit.openvpn.net/= c/openvpn/+/1557?usp=3Demail ) Change subject: Optimise performance of iterating over all client by rememb= ering highest peer id ...................................................................... Optimise performance of iterating over all client by remembering highest pe= er 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 --- M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/push_util.c 3 files changed, 24 insertions(+), 9 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/57/1557/1 diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 9d4ea49..5a7bc82 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]; =20 @@ -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)) @@ -4099,6 +4106,11 @@ } } + if (mi->context.c2.tls_multi->peer_id > m->max_peerid) + { + m->max_peerid =3D mi->context.c2.tls_multi->peer_id; + } + /* should not really end up here, since multi_create_instance returns = null * if amount of clients exceeds max_clients */ ASSERT(mi->context.c2.tls_multi->peer_id < m->max_clients); diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 498409d..17d850b 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; /**< currently highest allocated pe= erid and + * maximum allocated/valid index 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 6456554..529cc39 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: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: If797f3fe178fba3f43fb12898e5484bfb38f05c3 Gerrit-Change-Number: 1557 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos Gerrit-CC: openvpn-devel --B+ePSEPsrMY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

plaisthos has upl= oaded this change for review.

View Change

Optimise performance of iterating over all client by remembering highes=
t 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
---M src/openvpn/multi.c
M src/openvpn/multi.h
M src/openvpn/push_util= .c
3 files changed, 24 insertions(+), 9 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/57/155=
7/1
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.cindex 9d4ea49..5a7bc82 100644
--- a/src/openvpn/mul= ti.c
+++ b/src/openvpn/multi.c
@@ -601,6 +6= 01,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_pee= rid; i++)
{
struct mu= lti_instance *mi =3D m->instances[i];
if (m= i)
@@ -1326,7 +1333,7 @@
{<= br> int count =3D 0;

- for (int i =3D 0; i < m-&= gt;max_clients; i++)
= + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++)=
{
struct multi_inst= ance *mi =3D m->instances[i];
if (mi &a= mp;& mi !=3D new_mi && !mi->halt)
@@ -2885,7= +2892,7 @@
#endif
mb =3D mbuf_al= loc_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-&= gt;max_clients; i++)
= + for (uint32_t i =3D 0; i <=3D m->max_peerid; i++)
{
struct multi_instance *mi =3D m->insta= nces[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_pee= rid; i++)
{
struct multi_inst= ance *mi =3D m->instances[i];
if (mi &&= !mi->halt)
@@ -3907,7 +3914,7 @@
m= addr.proto =3D proto;
if (mroute_extract_openvpn_socka= ddr(&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->ha= lt && mroute_addr_equal(&maddr, &mi->real))
@@ -4099,6 +4106,11 @@
}
= }

+= if (mi->context.c2.tls_multi->peer_id > m->max_peerid)
+ {
+ m->max_peerid =3D mi->co= ntext.c2.tls_multi->peer_id;
+ }
+
/* should not really end up here, since multi_create_in= stance returns null
* if amount of clients exceeds ma= x_clients */
ASSERT(mi->context.c2.tls_multi->pe= er_id < m->max_clients);
diff --git a/src/openvpn/mul= ti.h b/src/openvpn/multi.h
index 498409d..17d850b 100644

--- a/src/openvpn/multi.h
+++ b/src/openvpn/m= ulti.h
@@ -160,9 +160,12 @@
*/
= struct multi_context
{
- struct multi_instance **instances; /**< Arr= ay of multi_instances. An instance 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 index. */
-
+ = uint32_t max_peerid; /**< currently highest allocated p= eerid and
+ = * maximum allocated/valid index in
= + = * instances */
struct hash *hash; = /**< VPN tunnel instances indexed by real
= * address of the remote peer. */<= br> struct hash *vhash; /**< VPN tunnel instanc= es indexed by
diff --git a/src/openvpn/push_util.c b/src/op= envpn/push_util.c
index 6456554..529cc39 100644
<= span>--- a/src/openvpn/push_util.c
+++ b/src/openvpn/push_u= til.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 change 1557. To unsu= bscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If797f3fe178fba3f43fb12898e5= 484bfb38f05c3
Gerrit-Change-Number: 1557
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --B+ePSEPsrMY=--