Skip to content

Commit db451bd

Browse files
ThinkerYzu1gregkh
authored andcommitted
selftests/bpf: netns_new() and netns_free() helpers.
[ Upstream commit 1e115a5 ] netns_new()/netns_free() create/delete network namespaces. They support the option '-m' of test_progs to start/stop traffic monitor for the network namespace being created for matched tests. Acked-by: Stanislav Fomichev <sdf@fomichev.me> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com> Link: https://lore.kernel.org/r/20240815053254.470944-4-thinker.li@gmail.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Stable-dep-of: 5bf1557 ("selftests/bpf: Fix backtrace printing for selftests crashes") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8027586 commit db451bd

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,52 @@ char *ping_command(int family)
448448
return "ping";
449449
}
450450

451+
int remove_netns(const char *name)
452+
{
453+
char *cmd;
454+
int r;
455+
456+
r = asprintf(&cmd, "ip netns del %s >/dev/null 2>&1", name);
457+
if (r < 0) {
458+
log_err("Failed to malloc cmd");
459+
return -1;
460+
}
461+
462+
r = system(cmd);
463+
free(cmd);
464+
return r;
465+
}
466+
467+
int make_netns(const char *name)
468+
{
469+
char *cmd;
470+
int r;
471+
472+
r = asprintf(&cmd, "ip netns add %s", name);
473+
if (r < 0) {
474+
log_err("Failed to malloc cmd");
475+
return -1;
476+
}
477+
478+
r = system(cmd);
479+
free(cmd);
480+
481+
if (r)
482+
return r;
483+
484+
r = asprintf(&cmd, "ip -n %s link set lo up", name);
485+
if (r < 0) {
486+
log_err("Failed to malloc cmd for setting up lo");
487+
remove_netns(name);
488+
return -1;
489+
}
490+
491+
r = system(cmd);
492+
free(cmd);
493+
494+
return r;
495+
}
496+
451497
struct nstoken {
452498
int orig_netns_fd;
453499
};

tools/testing/selftests/bpf/network_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ struct nstoken;
9292
struct nstoken *open_netns(const char *name);
9393
void close_netns(struct nstoken *token);
9494
int send_recv_data(int lfd, int fd, uint32_t total_bytes);
95+
int make_netns(const char *name);
96+
int remove_netns(const char *name);
9597

9698
static __u16 csum_fold(__u32 csum)
9799
{

tools/testing/selftests/bpf/test_progs.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <bpf/btf.h>
1919
#include "json_writer.h"
2020

21+
#include "network_helpers.h"
22+
2123
#ifdef __GLIBC__
2224
#include <execinfo.h> /* backtrace */
2325
#endif
@@ -624,6 +626,92 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
624626
return err;
625627
}
626628

629+
struct netns_obj {
630+
char *nsname;
631+
struct tmonitor_ctx *tmon;
632+
struct nstoken *nstoken;
633+
};
634+
635+
/* Create a new network namespace with the given name.
636+
*
637+
* Create a new network namespace and set the network namespace of the
638+
* current process to the new network namespace if the argument "open" is
639+
* true. This function should be paired with netns_free() to release the
640+
* resource and delete the network namespace.
641+
*
642+
* It also implements the functionality of the option "-m" by starting
643+
* traffic monitor on the background to capture the packets in this network
644+
* namespace if the current test or subtest matching the pattern.
645+
*
646+
* nsname: the name of the network namespace to create.
647+
* open: open the network namespace if true.
648+
*
649+
* Return: the network namespace object on success, NULL on failure.
650+
*/
651+
struct netns_obj *netns_new(const char *nsname, bool open)
652+
{
653+
struct netns_obj *netns_obj = malloc(sizeof(*netns_obj));
654+
const char *test_name, *subtest_name;
655+
int r;
656+
657+
if (!netns_obj)
658+
return NULL;
659+
memset(netns_obj, 0, sizeof(*netns_obj));
660+
661+
netns_obj->nsname = strdup(nsname);
662+
if (!netns_obj->nsname)
663+
goto fail;
664+
665+
/* Create the network namespace */
666+
r = make_netns(nsname);
667+
if (r)
668+
goto fail;
669+
670+
/* Start traffic monitor */
671+
if (env.test->should_tmon ||
672+
(env.subtest_state && env.subtest_state->should_tmon)) {
673+
test_name = env.test->test_name;
674+
subtest_name = env.subtest_state ? env.subtest_state->name : NULL;
675+
netns_obj->tmon = traffic_monitor_start(nsname, test_name, subtest_name);
676+
if (!netns_obj->tmon) {
677+
fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
678+
goto fail;
679+
}
680+
} else {
681+
netns_obj->tmon = NULL;
682+
}
683+
684+
if (open) {
685+
netns_obj->nstoken = open_netns(nsname);
686+
if (!netns_obj->nstoken)
687+
goto fail;
688+
}
689+
690+
return netns_obj;
691+
fail:
692+
traffic_monitor_stop(netns_obj->tmon);
693+
remove_netns(nsname);
694+
free(netns_obj->nsname);
695+
free(netns_obj);
696+
return NULL;
697+
}
698+
699+
/* Delete the network namespace.
700+
*
701+
* This function should be paired with netns_new() to delete the namespace
702+
* created by netns_new().
703+
*/
704+
void netns_free(struct netns_obj *netns_obj)
705+
{
706+
if (!netns_obj)
707+
return;
708+
traffic_monitor_stop(netns_obj->tmon);
709+
close_netns(netns_obj->nstoken);
710+
remove_netns(netns_obj->nsname);
711+
free(netns_obj->nsname);
712+
free(netns_obj);
713+
}
714+
627715
/* extern declarations for test funcs */
628716
#define DEFINE_TEST(name) \
629717
extern void test_##name(void) __weak; \

tools/testing/selftests/bpf/test_progs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ int write_sysctl(const char *sysctl, const char *value);
428428
int get_bpf_max_tramp_links_from(struct btf *btf);
429429
int get_bpf_max_tramp_links(void);
430430

431+
struct netns_obj;
432+
struct netns_obj *netns_new(const char *name, bool open);
433+
void netns_free(struct netns_obj *netns);
434+
431435
#ifdef __x86_64__
432436
#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
433437
#elif defined(__s390x__)

0 commit comments

Comments
 (0)