Skip to content

Commit 216c763

Browse files
schwabecron2
authored andcommitted
Increase default size of internal hash maps to 4 * --max-clients
The default of 256 seems quite low as with (at least) 1024 possible entries (the --max-clients default setting) we have a guaranteed collisions. Using 4 times the number of possible entries for real addresses should reduce collisions quite a bit while also leaving some headroom for the virtual addresses hash where a client might have more than one address. A reason to keep the limit so low are the memory requirements. Each bucket has the size of one linked-list pointer (4 byte or 32 bit and 8 byte for 64 bit). So 256 buckets use 1 or 2 kB while 4096 will use 16 kB or 32 kB. When the current limit was set 20 years ago this might have been a meaningful memory saving but today the collision probability is more important. Change-Id: Ia699b0dfa407ac377970bb130434298eaaec592b Signed-off-by: Arne Schwabe <arne@rfc2549.org> Acked-by: Antonio Quartulli <antonio@mandelbit.com> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1563 Message-Id: <20260325124526.124049-1-frank@lichtenheld.com> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg36268.html Signed-off-by: Gert Doering <gert@greenie.muc.de> (cherry picked from commit 7b5ebf7)
1 parent 82f6765 commit 216c763

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

doc/man-sections/advanced-options.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ used when debugging or testing out special usage scenarios.
3636

3737
hash-size r v
3838

39-
By default, both tables are sized at 256 buckets.
39+
By default, both tables are sized at 4 times ``--max-clients`` buckets.
40+
With the default of 1024 of ``--max-clients`` this gives 4096 buckets.
4041

4142
--bcast-buffers n
4243
Allocate ``n`` buffers for broadcast datagrams (default :code:`256`).

doc/man-sections/server-options.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ fast hardware. SSL/TLS authentication must be used in this mode.
414414
iroute-ipv6 ipv6addr/bits
415415

416416
--max-clients n
417-
Limit server to a maximum of ``n`` concurrent clients.
417+
Limit server to a maximum of ``n`` concurrent clients. Defaults to 1024.
418418

419419
--max-routes-per-client n
420420
Allow a maximum of ``n`` internal routes per client (default

src/openvpn/options.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,6 @@ init_options(struct options *o)
854854
#endif
855855
o->vlan_accept = VLAN_ALL;
856856
o->vlan_pvid = 1;
857-
o->real_hash_size = 256;
858-
o->virtual_hash_size = 256;
859857
o->n_bcast_buf = 256;
860858
o->tcp_queue_limit = 64;
861859
o->max_clients = 1024;
@@ -3737,6 +3735,22 @@ dhcp_options_postprocess_dns(struct options *o, struct env_set *es)
37373735
gc_free(&gc);
37383736
}
37393737
#endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
3738+
/**
3739+
* Sets the internal hash maps sizes according to the max_clients
3740+
*
3741+
*/
3742+
static void
3743+
helper_hashmap_sizes(struct options *o)
3744+
{
3745+
if (!o->real_hash_size)
3746+
{
3747+
o->real_hash_size = 4 * o->max_clients;
3748+
}
3749+
if (!o->virtual_hash_size)
3750+
{
3751+
o->virtual_hash_size = 4 * o->max_clients;
3752+
}
3753+
}
37403754

37413755
static void
37423756
options_postprocess_mutate(struct options *o, struct env_set *es)
@@ -3752,6 +3766,11 @@ options_postprocess_mutate(struct options *o, struct env_set *es)
37523766
helper_keepalive(o);
37533767
helper_tcp_nodelay(o);
37543768

3769+
if (o->mode == MODE_SERVER)
3770+
{
3771+
helper_hashmap_sizes(o);
3772+
}
3773+
37553774
options_postprocess_setdefault_ncpciphers(o);
37563775
options_set_backwards_compatible_options(o);
37573776
options_process_mutate_prf(o);

0 commit comments

Comments
 (0)