|
19 | 19 | #include <getopt.h>
|
20 | 20 | #include <stdbool.h>
|
21 | 21 | #include <regex.h>
|
| 22 | +#include <glib.h> |
22 | 23 | #include "slirp4netns.h"
|
23 | 24 |
|
24 | 25 | #define DEFAULT_MTU (1500)
|
@@ -120,6 +121,54 @@ static int sendfd(int sock, int fd)
|
120 | 121 | return rc;
|
121 | 122 | }
|
122 | 123 |
|
| 124 | + |
| 125 | +/* |
| 126 | + kernel 4.20 bumped up the default value of /proc/sys/net/ipv4/tcp_rmem from |
| 127 | + 87380 to 131072. This is known to slow down slirp4netns port forwarding. See |
| 128 | + https://github.com/rootless-containers/slirp4netns/issues/128 See |
| 129 | + https://github.com/torvalds/linux/commit/a337531b942bd8a03e7052444d7e36972aac2d92 |
| 130 | + */ |
| 131 | +static void try_configure_kernel420_net_ipv4_tcp_rmem() |
| 132 | +{ |
| 133 | + gchar *contents = NULL; |
| 134 | + gsize length = 0; |
| 135 | + gchar **tokenv = NULL; |
| 136 | + int nfd = -1; |
| 137 | + if (!g_file_get_contents("/proc/sys/net/ipv4/tcp_rmem", &contents, &length, |
| 138 | + NULL)) { |
| 139 | + goto ret; |
| 140 | + } |
| 141 | + tokenv = g_strsplit_set(contents, " \t\r\n", 4); |
| 142 | + if (g_strv_length(tokenv) != 4) { |
| 143 | + goto ret; |
| 144 | + } |
| 145 | + /* when running with kernel < 4.20, tokenv is like {"4096", "87380", |
| 146 | + * "6291456", ""} (preferred) */ |
| 147 | + /* when running with kernel >= 4.20, tokenv is like {"4096", "131072", |
| 148 | + * "6291456", ""} (not preferred) */ |
| 149 | + if (g_strcmp0(tokenv[1], "87380") == 0) { |
| 150 | + /* no need to adjust */ |
| 151 | + goto ret; |
| 152 | + } |
| 153 | + /* g_file_set_contents() can't be used as it attempts to create |
| 154 | + * /proc/sys/net/ipv4/tcp_rmem.RANDOMSTR */ |
| 155 | + nfd = open("/proc/sys/net/ipv4/tcp_rmem", O_WRONLY); |
| 156 | + if (nfd < 0) { |
| 157 | + goto ret; |
| 158 | + } |
| 159 | + if (dprintf(nfd, "%s\t%s\t%s%s\n", tokenv[0], "87380", |
| 160 | + g_strcmp0(tokenv[2], "131072") == 0 ? "87380" : tokenv[2], |
| 161 | + tokenv[3]) > 0) { |
| 162 | + printf("configure: Adjusted the value of /proc/sys/net/ipv4/tcp_rmem " |
| 163 | + "inside the namespace (131072 -> 87380)\n"); |
| 164 | + } |
| 165 | +ret: |
| 166 | + if (nfd >= 0) |
| 167 | + close(nfd); |
| 168 | + g_strfreev(tokenv); |
| 169 | + g_free(contents); |
| 170 | +} |
| 171 | + |
123 | 172 | static int configure_network(const char *tapname,
|
124 | 173 | struct slirp4netns_config *cfg)
|
125 | 174 | {
|
@@ -191,6 +240,8 @@ static int configure_network(const char *tapname,
|
191 | 240 | perror("set route");
|
192 | 241 | return -1;
|
193 | 242 | }
|
| 243 | + |
| 244 | + try_configure_kernel420_net_ipv4_tcp_rmem(); |
194 | 245 | return 0;
|
195 | 246 | }
|
196 | 247 |
|
|
0 commit comments