Skip to content

Commit 1e115a5

Browse files
ThinkerYzu1Martin KaFai Lau
authored andcommitted
selftests/bpf: netns_new() and netns_free() helpers.
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 <[email protected]> Signed-off-by: Kui-Feng Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent f5281aa commit 1e115a5

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
@@ -446,6 +446,52 @@ char *ping_command(int family)
446446
return "ping";
447447
}
448448

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

tools/testing/selftests/bpf/network_helpers.h

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

9799
static __u16 csum_fold(__u32 csum)
98100
{

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
@@ -642,6 +644,92 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
642644
return err;
643645
}
644646

647+
struct netns_obj {
648+
char *nsname;
649+
struct tmonitor_ctx *tmon;
650+
struct nstoken *nstoken;
651+
};
652+
653+
/* Create a new network namespace with the given name.
654+
*
655+
* Create a new network namespace and set the network namespace of the
656+
* current process to the new network namespace if the argument "open" is
657+
* true. This function should be paired with netns_free() to release the
658+
* resource and delete the network namespace.
659+
*
660+
* It also implements the functionality of the option "-m" by starting
661+
* traffic monitor on the background to capture the packets in this network
662+
* namespace if the current test or subtest matching the pattern.
663+
*
664+
* nsname: the name of the network namespace to create.
665+
* open: open the network namespace if true.
666+
*
667+
* Return: the network namespace object on success, NULL on failure.
668+
*/
669+
struct netns_obj *netns_new(const char *nsname, bool open)
670+
{
671+
struct netns_obj *netns_obj = malloc(sizeof(*netns_obj));
672+
const char *test_name, *subtest_name;
673+
int r;
674+
675+
if (!netns_obj)
676+
return NULL;
677+
memset(netns_obj, 0, sizeof(*netns_obj));
678+
679+
netns_obj->nsname = strdup(nsname);
680+
if (!netns_obj->nsname)
681+
goto fail;
682+
683+
/* Create the network namespace */
684+
r = make_netns(nsname);
685+
if (r)
686+
goto fail;
687+
688+
/* Start traffic monitor */
689+
if (env.test->should_tmon ||
690+
(env.subtest_state && env.subtest_state->should_tmon)) {
691+
test_name = env.test->test_name;
692+
subtest_name = env.subtest_state ? env.subtest_state->name : NULL;
693+
netns_obj->tmon = traffic_monitor_start(nsname, test_name, subtest_name);
694+
if (!netns_obj->tmon) {
695+
fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
696+
goto fail;
697+
}
698+
} else {
699+
netns_obj->tmon = NULL;
700+
}
701+
702+
if (open) {
703+
netns_obj->nstoken = open_netns(nsname);
704+
if (!netns_obj->nstoken)
705+
goto fail;
706+
}
707+
708+
return netns_obj;
709+
fail:
710+
traffic_monitor_stop(netns_obj->tmon);
711+
remove_netns(nsname);
712+
free(netns_obj->nsname);
713+
free(netns_obj);
714+
return NULL;
715+
}
716+
717+
/* Delete the network namespace.
718+
*
719+
* This function should be paired with netns_new() to delete the namespace
720+
* created by netns_new().
721+
*/
722+
void netns_free(struct netns_obj *netns_obj)
723+
{
724+
if (!netns_obj)
725+
return;
726+
traffic_monitor_stop(netns_obj->tmon);
727+
close_netns(netns_obj->nstoken);
728+
remove_netns(netns_obj->nsname);
729+
free(netns_obj->nsname);
730+
free(netns_obj);
731+
}
732+
645733
/* extern declarations for test funcs */
646734
#define DEFINE_TEST(name) \
647735
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
@@ -430,6 +430,10 @@ int write_sysctl(const char *sysctl, const char *value);
430430
int get_bpf_max_tramp_links_from(struct btf *btf);
431431
int get_bpf_max_tramp_links(void);
432432

433+
struct netns_obj;
434+
struct netns_obj *netns_new(const char *name, bool open);
435+
void netns_free(struct netns_obj *netns);
436+
433437
#ifdef __x86_64__
434438
#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
435439
#elif defined(__s390x__)

0 commit comments

Comments
 (0)