Skip to content

Commit 45caa74

Browse files
authored
Merge pull request #319 from libos-nuse/feature-conf-sysctl
lkl: add LKL_HIJACK_SYSCTL to configure sysctl values
2 parents e7b3474 + 40badbc commit 45caa74

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

Documentation/lkl.txt

+8
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ are the list of those variable for your environment.
238238
```
239239
$ LKL_HIJACK_OFFLOAD=0x8002 lkl-hijack.sh ./netserver -D -f
240240
```
241+
* LKL_HIJACK_SYSCTL
242+
243+
Configure sysctl values of the booted kernel via the hijack library. Multiple
244+
entries can be specified.
245+
```
246+
$ LKL_HIJACK_SYSCTL="net.ipv4.tcp_wmem=4096 87380 2147483647"
247+
./bin/lkl-hijack.sh ip address show
248+
```
241249

242250
FAQ
243251
===

tools/lkl/include/lkl.h

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ extern "C" {
1515
#undef class
1616
#endif
1717

18+
#if defined(__MINGW32__)
19+
#define strtok_r strtok_s
20+
#endif
21+
1822
#if __LKL__BITS_PER_LONG == 64
1923
#define lkl_sys_stat lkl_sys_newstat
2024
#define lkl_sys_lstat lkl_sys_newlstat
@@ -479,6 +483,21 @@ int lkl_qdisc_add(int ifindex, char *root, char *type);
479483
*/
480484
void lkl_qdisc_parse_add(int ifindex, char *entries);
481485

486+
/**
487+
* lkl_sysctl - write a sysctl value
488+
*
489+
* @path - the path to an sysctl entry (e.g., "net.ipv4.tcp_wmem");
490+
* @value - the value of the sysctl (e.g., "4096 87380 2147483647")
491+
*/
492+
int lkl_sysctl(const char *path, const char *value);
493+
494+
/**
495+
* lkl_sysctl_parse_write - Configure sysctl parameters with strings
496+
*
497+
* @sysctls - Configure sysctl parameters as the form of "key=value;..."
498+
*/
499+
void lkl_sysctl_parse_write(const char *sysctls);
500+
482501
#ifdef __cplusplus
483502
}
484503
#endif

tools/lkl/lib/hijack/init.c

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ hijack_init(void)
220220
char *offload1 = getenv("LKL_HIJACK_OFFLOAD");
221221
int offload = 0;
222222
char boot_cmdline[256] = "\0";
223+
char *sysctls = getenv("LKL_HIJACK_SYSCTL");
223224

224225
memset(&nd_args, 0, sizeof(struct lkl_netdev_args));
225226
if (!debug) {
@@ -436,6 +437,8 @@ hijack_init(void)
436437
if (nd_ifindex >= 0 && qdisc_entries)
437438
lkl_qdisc_parse_add(nd_ifindex, qdisc_entries);
438439

440+
if (sysctls)
441+
lkl_sysctl_parse_write(sysctls);
439442
}
440443

441444
void __attribute__((destructor))

tools/lkl/lib/utils.c

+56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdarg.h>
22
#include <stdio.h>
3+
#include <string.h>
34
#include <lkl_host.h>
45

56
static const char * const lkl_err_strings[] = {
@@ -205,3 +206,58 @@ void lkl_bug(const char *fmt, ...)
205206

206207
lkl_host_ops.panic();
207208
}
209+
210+
int lkl_sysctl(const char *path, const char *value)
211+
{
212+
int ret;
213+
int fd;
214+
char *delim, *p;
215+
char full_path[256];
216+
217+
lkl_mount_fs("proc");
218+
219+
snprintf(full_path, sizeof(full_path), "/proc/sys/%s", path);
220+
p = full_path;
221+
while ((delim = strstr(p, "."))) {
222+
*delim = '/';
223+
p = delim + 1;
224+
}
225+
226+
fd = lkl_sys_open(full_path, LKL_O_WRONLY | LKL_O_CREAT, 0);
227+
if (fd < 0) {
228+
lkl_printf("lkl_sys_open %s: %s\n",
229+
full_path, lkl_strerror(fd));
230+
return -1;
231+
}
232+
ret = lkl_sys_write(fd, value, strlen(value));
233+
if (ret < 0) {
234+
lkl_printf("lkl_sys_write %s: %s\n",
235+
full_path, lkl_strerror(fd));
236+
}
237+
238+
lkl_sys_close(fd);
239+
240+
return 0;
241+
}
242+
243+
/* Configure sysctl parameters as the form of "key=value;key=value;..." */
244+
void lkl_sysctl_parse_write(const char *sysctls)
245+
{
246+
char *saveptr = NULL, *token = NULL;
247+
char *key = NULL, *value = NULL;
248+
char strings[256];
249+
int ret = 0;
250+
251+
strcpy(strings, sysctls);
252+
for (token = strtok_r(strings, ";", &saveptr); token;
253+
token = strtok_r(NULL, ";", &saveptr)) {
254+
key = strtok(token, "=");
255+
value = strtok(NULL, "=");
256+
ret = lkl_sysctl(key, value);
257+
if (ret) {
258+
lkl_printf("Failed to configure sysctl entries: %s\n",
259+
lkl_strerror(ret));
260+
return;
261+
}
262+
}
263+
}

0 commit comments

Comments
 (0)