Attention is currently required from: Arne Schwabe.
Hello Arne Schwabe,
I'd like you to do a code review.
Please visit
https://gerrit.openvpn.net/c/openvpn/+/2211?usp=email
to review the following change.
Change subject: multi: raise the open files limit for --max-clients
......................................................................
multi: raise the open files limit for --max-clients
Every TCP client takes a file descriptor and accept() happens before the
--max-clients check, so with the default limits (soft limit 1024,
--max-clients 1024) the server runs out of descriptors before it can
refuse a client.
When the server has a TCP listener, raise the soft limit to
--max-clients plus some room for the other descriptors, up to the hard
limit, and warn if that is not enough. Processes started by OpenVPN
inherit the new limit.
Not on macOS, where the event loop uses select() and cannot watch
descriptors >= FD_SETSIZE.
GitHub: closes OpenVPN/openvpn#1148
Change-Id: I17fafbe65fd3ff510a72ed809c85b0d19343fad2
Signed-off-by: Gianmarco De Gregori <[email protected]>
---
M doc/man-sections/server-options.rst
M src/openvpn/multi.c
2 files changed, 62 insertions(+), 0 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/11/2211/1
diff --git a/doc/man-sections/server-options.rst
b/doc/man-sections/server-options.rst
index 7a09809..5c1b129 100644
--- a/doc/man-sections/server-options.rst
+++ b/doc/man-sections/server-options.rst
@@ -418,6 +418,9 @@
--max-clients n
Limit server to a maximum of ``n`` concurrent clients. Defaults to 1024.
+ With a TCP listener, the soft limit on open files is raised up to the hard
+ limit if needed to accept ``n`` clients (not on Windows and macOS).
+
--max-routes-per-client n
Allow a maximum of ``n`` internal routes per client (default
:code:`256`). This is designed to help contain DoS attacks where an
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index ad9f9f4..dacda86 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -270,6 +270,64 @@
#endif
/*
+ * Every TCP client takes a file descriptor, and accept() happens before the
+ * --max-clients check: raise the soft limit so that --max-clients is reached
+ * first, or warn if the hard limit does not allow it.
+ */
+static void
+multi_check_nofile_limit(const struct context *t)
+{
+#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) &&
!defined(TARGET_DARWIN)
+ bool tcp = false;
+ for (int i = 0; i < t->c1.link_sockets_num; i++)
+ {
+ if (!proto_is_dgram(t->c2.link_sockets[i]->info.proto))
+ {
+ tcp = true;
+ break;
+ }
+ }
+ if (!tcp)
+ {
+ return;
+ }
+
+ /* room for the tun device, the listeners, log, management, scripts... */
+ const rlim_t needed = (rlim_t)t->options.max_clients + 64;
+
+ struct rlimit rl;
+ if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
+ {
+ msg(M_WARN | M_ERRNO, "WARNING: getrlimit(RLIMIT_NOFILE) failed");
+ return;
+ }
+ if (rl.rlim_cur >= needed)
+ {
+ return;
+ }
+
+ const rlim_t old = rl.rlim_cur;
+ rl.rlim_cur = rl.rlim_max < needed ? rl.rlim_max : needed;
+ if (rl.rlim_cur > old && setrlimit(RLIMIT_NOFILE, &rl) == 0)
+ {
+ msg(M_INFO, "Raised the open files limit from %lu to %lu", (unsigned
long)old,
+ (unsigned long)rl.rlim_cur);
+ }
+ else
+ {
+ rl.rlim_cur = old;
+ }
+ if (rl.rlim_cur < needed)
+ {
+ msg(M_WARN,
+ "WARNING: the open files limit (%lu) is too low for --max-clients
%" PRIu32
+ ", TCP clients may be refused before --max-clients is reached",
+ (unsigned long)rl.rlim_cur, t->options.max_clients);
+ }
+#endif /* if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) &&
!defined(TARGET_DARWIN) */
+}
+
+/*
* Main initialization function, init multi_context object.
*/
static void
@@ -398,6 +456,7 @@
* Per-client limits
*/
m->max_clients = t->options.max_clients;
+ multi_check_nofile_limit(t);
m->instances = calloc(m->max_clients, sizeof(struct multi_instance *));
check_malloc_return(m->instances);
--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2211?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I17fafbe65fd3ff510a72ed809c85b0d19343fad2
Gerrit-Change-Number: 2211
Gerrit-PatchSet: 1
Gerrit-Owner: Gianmarco De Gregori <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel