From: "plaisthos (Code Review)" Date: Thu, 5 Mar 2026 14:04:16 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <91703e34791b7cba34e05058119235ea316aaab8-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="o8OAHMazmlg="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Remove multi_context->iter List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openvpn-devel --o8OAHMazmlg= 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/+/1556?usp=3Demail ) Change subject: Remove multi_context->iter ...................................................................... Remove multi_context->iter The multi_context->iter is basically a hash with only one bucket. This makes m->iter a linear list. Instead of maintaining this extra list use m->instances instead. This is a fixed sized continuous array, so iterating over it should be very quick. When the number of connected clients approaches max_clients, iterating over a static array should be faster than a linked list, especially when considering cache locality. Of the several places where m->iter is used only one is potentially on a critical path: the usage of m->iter in multi_bcast. However this performance difference would be only visible with a lightly loaded server with very few clients. And even in this scenario I could not manage to measure a difference. Change-Id: Ibf8865e451866e1fffc8dbc8ad5ecf6bc5577ce4 --- M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/push_util.c 3 files changed, 25 insertions(+), 83 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/56/1556/1 diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 1625fd0..9d4ea49 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -300,14 +300,6 @@ m->vhash =3D hash_init(t->options.virtual_hash_size, (uint32_t)get_ran= dom(), mroute_addr_hash_function, mroute_addr_compare_fu= nction); - /* - * This hash table is a clone of m->hash but with a - * bucket size of one so that it can be used - * for fast iteration through the list. - */ - m->iter =3D hash_init(1, (uint32_t)get_random(), mroute_addr_hash_func= tion, - mroute_addr_compare_function); - #ifdef ENABLE_MANAGEMENT m->cid_hash =3D hash_init(t->options.real_hash_size, 0, cid_hash_funct= ion, cid_compare_function); #endif @@ -591,10 +583,6 @@ { ASSERT(hash_remove(m->hash, &mi->real)); } - if (mi->did_iter) - { - ASSERT(hash_remove(m->iter, &mi->real)); - } #ifdef ENABLE_MANAGEMENT if (mi->did_cid_hash) { @@ -664,23 +652,19 @@ { if (m->hash) { - struct hash_iterator hi; - struct hash_element *he; - - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *mi =3D (struct multi_instance *)he->val= ue; - mi->did_iter =3D false; - multi_close_instance(m, mi, true); + struct multi_instance *mi =3D m->instances[i]; + if (mi) + { + multi_close_instance(m, mi, true); + } } - hash_iterator_free(&hi); multi_reap_all(m); hash_free(m->hash); hash_free(m->vhash); - hash_free(m->iter); #ifdef ENABLE_MANAGEMENT hash_free(m->cid_hash); #endif @@ -760,14 +744,6 @@ generate_prefix(mi); } - if (!hash_add(m->iter, &mi->real, mi, false)) - { - msg(D_MULTI_LOW, "MULTI: unable to add real address [%s] to iterat= or hash table", - mroute_addr_print(&mi->real, &gc)); - goto err; - } - mi->did_iter =3D true; - #ifdef ENABLE_MANAGEMENT do { @@ -1348,27 +1324,21 @@ const char *new_cn =3D tls_common_name(new_mi->context.c2.tls_mult= i, true); if (new_cn) { - struct hash_iterator hi; - struct hash_element *he; int count =3D 0; - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *mi =3D (struct multi_instance *)he-= >value; - if (mi !=3D new_mi && !mi->halt) + struct multi_instance *mi =3D m->instances[i]; + if (mi && mi !=3D new_mi && !mi->halt) { const char *cn =3D tls_common_name(mi->context.c2.tls_= multi, true); if (cn && !strcmp(cn, new_cn)) { - mi->did_iter =3D false; multi_close_instance(m, mi, false); - hash_iterator_delete_element(&hi); ++count; } } } - hash_iterator_free(&hi); if (count) { @@ -2906,9 +2876,6 @@ multi_bcast(struct multi_context *m, const struct buffer *buf, const struct multi_instance *sender_instance, uint16_t vid) { - struct hash_iterator hi; - struct hash_element *he; - struct multi_instance *mi; struct mbuf_buffer *mb; if (BLEN(buf) > 0) @@ -2917,12 +2884,12 @@ printf("BCAST len=3D%d\n", BLEN(buf)); #endif mb =3D mbuf_alloc_buf(buf); - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - mi =3D (struct multi_instance *)he->value; - if (mi !=3D sender_instance && !mi->halt) + struct multi_instance *mi =3D m->instances[i]; + + if (mi && mi !=3D sender_instance && !mi->halt) { if (vid !=3D 0 && vid !=3D mi->context.options.vlan_pvid) { @@ -2931,8 +2898,6 @@ multi_add_mbuf(m, mi, mb); } } - - hash_iterator_free(&hi); mbuf_free_buf(mb); } } @@ -3182,7 +3147,6 @@ /* remove old address from hash table before changing address */ ASSERT(hash_remove(m->hash, &mi->real)); - ASSERT(hash_remove(m->iter, &mi->real)); =20 /* change external network address of the remote peer */ mi->real =3D real; @@ -3198,7 +3162,6 @@ tls_update_remote_addr(mi->context.c2.tls_multi, &mi->context.c2.from); ASSERT(hash_add(m->hash, &mi->real, mi, false)); - ASSERT(hash_add(m->iter, &mi->real, mi, false)); #ifdef ENABLE_MANAGEMENT ASSERT(hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, true= )); @@ -3830,22 +3793,17 @@ static void multi_push_restart_schedule_exit(struct multi_context *m, bool next_server) { - struct hash_iterator hi; - struct hash_element *he; - /* tell all clients to restart */ - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *mi =3D (struct multi_instance *)he->value; - if (!mi->halt && proto_is_dgram(mi->context.c2.link_sockets[0]->in= fo.proto)) + struct multi_instance *mi =3D m->instances[i]; + if (mi && !mi->halt && proto_is_dgram(mi->context.c2.link_sockets[= 0]->info.proto)) { send_control_channel_string(&mi->context, next_server ? "RESTA= RT,[N]" : "RESTART", D_PUSH); multi_schedule_context_wakeup(m, mi); } } - hash_iterator_free(&hi); /* reschedule signal */ ASSERT(!openvpn_gettimeofday(&m->deferred_shutdown_signal.wakeup, NULL= )); @@ -3916,15 +3874,12 @@ management_callback_kill_by_cn(void *arg, const char *del_cn) { struct multi_context *m =3D (struct multi_context *)arg; - struct hash_iterator hi; - struct hash_element *he; int count =3D 0; - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *mi =3D (struct multi_instance *)he->value; - if (!mi->halt) + struct multi_instance *mi =3D m->instances[i]; + if (mi && !mi->halt) { const char *cn =3D tls_common_name(mi->context.c2.tls_multi, f= alse); if (cn && !strcmp(cn, del_cn)) @@ -3934,7 +3889,6 @@ } } } - hash_iterator_free(&hi); return count; } @@ -3942,8 +3896,6 @@ management_callback_kill_by_addr(void *arg, const in_addr_t addr, const ui= nt16_t port, const uint8_t proto) { struct multi_context *m =3D (struct multi_context *)arg; - struct hash_iterator hi; - struct hash_element *he; struct openvpn_sockaddr saddr; struct mroute_addr maddr; int count =3D 0; @@ -3955,17 +3907,15 @@ maddr.proto =3D proto; if (mroute_extract_openvpn_sockaddr(&maddr, &saddr, true)) { - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *mi =3D (struct multi_instance *)he->val= ue; - if (!mi->halt && mroute_addr_equal(&maddr, &mi->real)) + struct multi_instance *mi =3D m->instances[i]; + if (mi && !mi->halt && mroute_addr_equal(&maddr, &mi->real)) { multi_signal_instance(m, mi, SIGTERM); ++count; } } - hash_iterator_free(&hi); } return count; } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index c686e47..498409d 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -132,7 +132,6 @@ struct in6_addr reporting_addr_ipv6; /* IPv6 address in status listing= */ bool did_real_hash; - bool did_iter; #ifdef ENABLE_MANAGEMENT bool did_cid_hash; struct buffer_list *cc_config; @@ -168,9 +167,6 @@ * address of the remote peer. */ struct hash *vhash; /**< VPN tunnel instances indexed by * virtual address of remote host= s. */ - struct hash *iter; /**< VPN tunnel instances indexed b= y real - * address of the remote peer, op= timized - * for iteration. */ struct schedule *schedule; struct mbuf_set *mbuf; /**< Set of buffers for passing data * channel packets between VPN tu= nnel diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c index 51c7b5f..6456554 100644 --- a/src/openvpn/push_util.c +++ b/src/openvpn/push_util.c @@ -316,15 +316,12 @@ } int count =3D 0; - struct hash_iterator hi; - const struct hash_element *he; - hash_iterator_init(m->iter, &hi); - while ((he =3D hash_iterator_next(&hi))) + for (int i =3D 0; i < m->max_clients; i++) { - struct multi_instance *curr_mi =3D he->value; + struct multi_instance *curr_mi =3D m->instances[i]; - if (curr_mi->halt || !support_push_update(curr_mi)) + if (!curr_mi || curr_mi->halt || !support_push_update(curr_mi)) { continue; } @@ -338,7 +335,6 @@ count++; } - hash_iterator_free(&hi); buffer_list_free(msgs); gc_free(&gc); return count; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1556?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: Ibf8865e451866e1fffc8dbc8ad5ecf6bc5577ce4 Gerrit-Change-Number: 1556 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos Gerrit-CC: openvpn-devel --o8OAHMazmlg= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

plaisthos has upl= oaded this change for review.

View Change

Remove multi_context->iter

The multi_context->iter is basi= cally a hash with only one bucket. This makes
m->iter a linear list. = Instead of maintaining this extra list use
m->instances instead. This= is a fixed sized continuous array, so iterating
over it should be very = quick. When the number of connected clients
approaches max_clients, iter= ating over a static array should be faster than
a linked list, especiall= y when considering cache locality.

Of the several places where m->= ;iter is used only one is potentially on a
critical path: the usage of m= ->iter in multi_bcast.

However this performance difference would = be only visible with a lightly
loaded server with very few clients. And = even in this scenario I could
not manage to measure a difference.
Change-Id: Ibf8865e451866e1fffc8dbc8ad5ecf6bc5577ce4
---
M src/openv= pn/multi.c
M src/openvpn/multi.h
M src/openvpn/push_util.c
3 files= changed, 25 insertions(+), 83 deletions(-)

git =
pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/56/1556/1
= diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
inde= x 1625fd0..9d4ea49 100644
--- a/src/openvpn/multi.c<= br>+++ b/src/openvpn/multi.c
@@ -300,14 +300,6 @@
m->vhash =3D hash_init(t->options.virtual_hash_size= , (uint32_t)get_random(),
mroute_= addr_hash_function, mroute_addr_compare_function);
=
- /*
- * This hash table is a clone of m->= hash but with a
- *= bucket size of one so that it can be used
- * for fast iteration through the list.
= - */
- m->iter =3D hash_init(1, (uint32_t)get_= random(), mroute_addr_hash_function,
- mroute_addr_compare_function);
-
#ifdef EN= ABLE_MANAGEMENT
m->cid_hash =3D hash_init(t->opt= ions.real_hash_size, 0, cid_hash_function, cid_compare_function); #endif
@@ -591,10 +583,6 @@
= {
ASSERT(hash_remove(m->hash, &mi->= ;real));
}
- if (mi->did_iter)
- {
- ASSERT(hash_remove(m->iter, &mi->real));
- }
#ifdef ENABLE_MANAGEMENT

if (mi->did_cid_hash= )
{
@@ -664,23 +652,19 @@ {
if (m->hash)
{
- struct hash_iterat= or hi;
- struct = hash_element *he;
-
- hash_iterator_ini= t(m->iter, &hi);
- while ((he =3D hash_iterator_next(&hi)))
+ for (int i =3D 0; i < m->m= ax_clients; i++)
{
- struct multi_instance *mi =3D (struct mul= ti_instance *)he->value;
- mi->did_iter =3D false;
- multi_close_instance(m, mi, true);
+ struct multi_= instance *mi =3D m->instances[i];
+ if (mi)
+ {
+ multi_close_instance(m, mi, true);
+ }

= }
- hash_i= terator_free(&hi);

multi_rea= p_all(m);

hash_free(m->hash);=
hash_free(m->vhash);
- hash_free(m->iter);
= #ifdef ENABLE_MANAGEMENT
hash_free(m->cid_hash= );
#endif
@@ -760,14 +744,6 @@
<= span> generate_prefix(mi);

}
=
- if (!hash_add(m-&= gt;iter, &mi->real, mi, false))
- {
= - msg(D_MULTI_LOW, "MULTI: unable to add real address [%s] to i= terator hash table",
- mroute_addr_print(&mi->real, &gc));
- goto err;

- }
- mi->did_iter =3D true;
-
#ifdef ENABLE_MANAGEMENT
do
{
@@ -1348,27 +13= 24,21 @@
const char *new_cn =3D tls_common_name(ne= w_mi->context.c2.tls_multi, true);
if (new_cn)<= /span>
{
- struct hash_iterator hi;
- struct hash_element *he;
= int count =3D 0;

- hash_iterator_init(m->iter, &hi)= ;
- while ((= he =3D hash_iterator_next(&hi)))
+ for (int i =3D 0; i < m->max_clients; i+= +)
{
- struct multi_instance *mi =3D (struct multi_ins= tance *)he->value;
-= if (mi !=3D new_mi && !mi->halt)
+ struct multi_instan= ce *mi =3D m->instances[i];

+ if (mi && mi !=3D new_mi && !mi-= >halt)
{
= const char *cn =3D tls_common_name(mi->context.c2.tls_multi, true= );
if (cn && !strcmp(cn, new_c= n))
{
- mi->did_iter =3D false;
multi_close_instance(m, mi, false);
- = hash_iterator_delete_element(&hi);
= ++count;
}
= }
}
- hash_iterator_free(&hi);
<= span>

if (count)
= {
@@ -2906,9 +2876,6 @@
multi_bcast(struc= t multi_context *m, const struct buffer *buf,
= const struct multi_instance *sender_instance, uint16_t vid)
{

- struct hash_it= erator hi;
- struct = hash_element *he;
- = struct multi_instance *mi;
struct mbuf_buffer *mb;

if (BLEN(buf) > 0)
@= @ -2917,12 +2884,12 @@
printf("BCAST len=3D%d= \n", BLEN(buf));
#endif
mb = =3D mbuf_alloc_buf(buf);
- while ((he =3D hash= _iterator_next(&hi)))
+ for (int i =3D 0; i < m->max_clients; i++)
{

- = mi =3D (struct multi_instance *)he->value;
- if (mi !=3D sender_instance &&= amp; !mi->halt)
+ = struct multi_instance *mi =3D m->instances[i];
+

+ if (mi && mi !=3D sender_instance= && !mi->halt)
{
= if (vid !=3D 0 && vid !=3D mi->context.options.vl= an_pvid)
{
@@ -2931,8 +289= 8,6 @@
multi_add_mbuf(m, mi, mb); }
}
-
- hash_iterator_free(&hi);
mbuf_f= ree_buf(mb);
}
}
@@ -= 3182,7 +3147,6 @@

/* remove old addr= ess from hash table before changing address */
ASSERT(= hash_remove(m->hash, &mi->real));
- ASSERT(hash_remove(m->iter, &mi->real))= ;

/* change external network address= of the remote peer */
mi->real =3D real;@@ -3198,7 +3162,6 @@
tls_update_remote_addr(mi= ->context.c2.tls_multi, &mi->context.c2.from);
<= /span>
ASSERT(hash_add(m->hash, &mi->real, mi, fals= e));
- ASSERT(hash_a= dd(m->iter, &mi->real, mi, false));

<= span> #ifdef ENABLE_MANAGEMENT

ASSERT(hash_add(m->c= id_hash, &mi->context.c2.mda_context.cid, mi, true));
@@ -3830,22 +3793,17 @@

static void
mul= ti_push_restart_schedule_exit(struct multi_context *m, bool next_server)
{
- s= truct hash_iterator hi;
- struct hash_element *he;
-
/* tell all clients to restart */
= - hash_iterator_init(m->ite= r, &hi);
- while= ((he =3D hash_iterator_next(&hi)))
+ for (int i =3D 0; i < m->max_clients; i++)
{
- = struct multi_instance *mi =3D (struct multi_instance *)he->value;=
- if (!mi->h= alt && proto_is_dgram(mi->context.c2.link_sockets[0]->info.pr= oto))
+ struct= multi_instance *mi =3D m->instances[i];
+ if (mi && !mi->halt && p= roto_is_dgram(mi->context.c2.link_sockets[0]->info.proto))
= {
send_control_channel_string(&= amp;mi->context, next_server ? "RESTART,[N]" : "RESTART&q= uot;,
D_PUSH);
multi_schedule_context_wakeup(m, mi);
<= span> }

}
- hash_iterator_free(&hi);
<= br> /* reschedule signal */
ASSERT(!openvpn_= gettimeofday(&m->deferred_shutdown_signal.wakeup, NULL));
= @@ -3916,15 +3874,12 @@
management_callback_kill_by_= cn(void *arg, const char *del_cn)
{
s= truct multi_context *m =3D (struct multi_context *)arg;
- struct hash_iterator hi;
- struct hash_element *he;
int count =3D 0;

- hash_iterator_init(m->iter, &hi);
- while ((he =3D hash= _iterator_next(&hi)))
+ for (int i =3D 0; i < m->max_clients; i++)
= {
- struct = multi_instance *mi =3D (struct multi_instance *)he->value;
- if (!mi->halt)
+ struct multi_instance= *mi =3D m->instances[i];
+ if (mi && !mi->halt)
{=
const char *cn =3D tls_common_name(mi->con= text.c2.tls_multi, false);
if (cn && != strcmp(cn, del_cn))
@@ -3934,7 +3889,6 @@
= }
}
}
<= span style=3D"color: hsl(0, 100%, 40%);">- hash_iterator_free(&hi);<= /span>
return count;
}

@@ -3942,8 +3896,6 @@
management_callback_kil= l_by_addr(void *arg, const in_addr_t addr, const uint16_t port, const uint8= _t proto)
{
struct multi_context *m = =3D (struct multi_context *)arg;
- struct hash_iterator hi;
- struct hash_element *he;
struct = openvpn_sockaddr saddr;
struct mroute_addr maddr;
int count =3D 0;
@@ -3955,17 +3907,15 @@
maddr.proto =3D proto;
if (mroute_= extract_openvpn_sockaddr(&maddr, &saddr, true))
= {
- hash_itera= tor_init(m->iter, &hi);
- while ((he =3D hash_iterator_next(&hi)))
+ for (int i =3D 0; i < = m->max_clients; i++)

{
- struct multi_instance *mi =3D (str= uct multi_instance *)he->value;
- if (!mi->halt && mroute_addr_equal(&= ;maddr, &mi->real))
+ struct multi_instance *mi =3D m->instances[i];=
+ if (mi &&a= mp; !mi->halt && mroute_addr_equal(&maddr, &mi->real)= )
{
multi_sig= nal_instance(m, mi, SIGTERM);
++count;
}
}
- hash_iterator_free(&hi);
}
return count;
}
diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h<= br>index c686e47..498409d 100644
--- a/src/openvpn/mu= lti.h
+++ b/src/openvpn/multi.h
@@ -132,7 += 132,6 @@
struct in6_addr reporting_addr_ipv6; /* IPv6 = address in status listing */

bool di= d_real_hash;
- bool = did_iter;
#ifdef ENABLE_MANAGEMENT
bo= ol did_cid_hash;
struct buffer_list *cc_config;=
@@ -168,9 +167,6 @@
= * address of the remote peer. */
struct = hash *vhash; /**< VPN tunnel instances indexed by<= br> * virtual address of re= mote hosts. */
- str= uct hash *iter; /**< VPN tunnel instances indexed by rea= l
- = * address of the remote peer, optimized
- = * for iteration. */

struct schedule *schedule;<= /span>
struct mbuf_set *mbuf; /**< Set of buff= ers for passing data
= * channel packets between VPN tunnel
diff --git a/src/= openvpn/push_util.c b/src/openvpn/push_util.c
index 51c7b5f= ..6456554 100644
--- a/src/openvpn/push_util.c
+++ b/src/openvpn/push_util.c

@@ -316,15 +316,12 @@
}

int count =3D 0;=
- struct hash_itera= tor hi;
- const stru= ct hash_element *he;

- hash_iterator_init(m->iter, &hi);
- while ((he =3D hash_iterator_ne= xt(&hi)))

+ fo= r (int i =3D 0; i < m->max_clients; i++)
{
- struct multi_instan= ce *curr_mi =3D he->value;
+ struct multi_instance *curr_mi =3D m->instances[i];


- = if (curr_mi->halt || !support_push_update(curr_mi))
+ if (!curr_mi || curr_mi->ha= lt || !support_push_update(curr_mi))
{
<= span> continue;

}
@@ -= 338,7 +335,6 @@
count++;
}

- ha= sh_iterator_free(&hi);
buffer_list_free(msgs);
gc_free(&gc);
return count;

To view, visit change 1556. To unsubscribe, or = for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibf8865e451866e1fffc8dbc8ad5= ecf6bc5577ce4
Gerrit-Change-Number: 1556
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --o8OAHMazmlg=--