monitor network traffic for flaky test cases
Subsystems (3)
v6 cover monitor network traffic for flaky test cases
Capture packets in the background for flaky test cases related to network features.
We have some flaky test cases that are difficult to debug without knowing what the traffic looks like. Capturing packets, the CI log and packet files may help developers to fix these flaky test cases.
This patch set monitors a few test cases. Recently, they have been showing flaky behavior.
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 68, ifname lo (In), SYN
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 60, ifname lo (In), SYN, ACK
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 60, ifname lo (In), ACK
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), ACK
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 52, ifname lo (In), FIN, ACK
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), RST, ACK
TCP packet: 127.0.0.1:33695 -> 127.0.0.1:40467, len 52, ifname lo, RST, ACK
Packet file: packets-2173-86-select_reuseport:sockhash_IPv4_TCP_LOOPBACK_test_detach_bpf-test.log
#280/87 select_reuseport/sockhash IPv4/TCP LOOPBACK test_detach_bpf:OKThe above block is the log of a test case. It shows every packet of a connection. The captured packets are stored in the file called packets-2173-86-select_reuseport:sockhash_IPv4_TCP_LOOPBACK_test_detach_bpf-test.log.
We have a set of high-level helpers and a test_progs option to simplify the process of enabling the traffic monitor. netns_new() and netns_free() are helpers used to create and delete namespaces while also enabling the traffic monitor for the namespace based on the patterns provided by the "-m" option of test_progs. The value of the "-m" option is a list of patterns used to enable the traffic monitor for a group of tests or a file containing patterns. CI can utilize this option to enable monitoring.
traffic_monitor_start() and traffic_monitor_stop() are low-level functions to start monitoring explicitly. You can have more controls, however high-level helpers are preferred.
The following block is an example that monitors the network traffic of a test case in a network namespace.
struct netns_obj *netns;
...
netns = netns_new("test", true);
if (!ASSERT_TRUE(netns, "netns_new"))
goto err;
... test ...
netns_free(netns);netns_new() will create a network namespace named "test" and bring up "lo" in the namespace. By passing "true" as the 2nd argument, it will set the network namespace of the current process to "test".netns_free() will destroy the namespace, and the process will leave the "test" namespace if the struct netns_obj returned by netns_new() is created with "true" as the 2nd argument. If the name of the test matches the patterns given by the "-m" option, the traffic monitor will be enabled for the "test" namespace as well.
The packet files are located in the directory "/tmp/tmon_pcap/". The directory is intended to be compressed as a file so that developers can download it from the CI.
This feature is enabled only if libpcap is available when building selftests.
---
Changes from v5:
- Remove "-m" completely if traffic monitor is not enabled.
Changes from v4:
- Use pkg-config to detect libpcap, and enable traffic monitor if there is libpcap.
- Move traffic monitor functions back to network_helper.c, and pass extra parameters to traffic_monitor_start().
- Use flockfile() & funlockfile() to avoid log interleaving.
- Show "In", "Out", "M" ... for captured packets.
- Print a warning message if the user pass a "-m" when libpcap is not available.
- Bring up dev lo in netns_new().
Changes from v3:
- Rebase to the latest tip of bpf-next/for-next
- Change verb back to C string.
Changes from v2:
- Include pcap header files conditionally.
- Move the implementation of traffic monitor to test_progs.c.
- Include test name and namespace as a part of names of packet files.
- Parse and print ICMP(v4|v6) packets.
- Add netns_new() and netns_free() to create and delete network namespaces.
- Make tc_redirect, sockmap_listen and select_reuseport test in a network namespace.
- Add the "-m" option to test_progs to enable traffic monitor for the tests matching the pattern. CI may use this option to enable monitoring for a given set of tests.
Changes from v1:
- Move to calling libpcap directly to capture packets in a background thread.
- Print parsed packet information for TCP and UDP packets.
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
Kui-Feng Lee (6):
selftests/bpf: Add traffic monitor functions. selftests/bpf: Add the traffic monitor option to test_progs. selftests/bpf: netns_new() and netns_free() helpers. selftests/bpf: Monitor traffic for tc_redirect. selftests/bpf: Monitor traffic for sockmap_listen. selftests/bpf: Monitor traffic for select_reuseport.
tools/testing/selftests/bpf/Makefile | 4 + tools/testing/selftests/bpf/network_helpers.c | 494 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 20 + .../bpf/prog_tests/select_reuseport.c | 37 +- .../selftests/bpf/prog_tests/sockmap_listen.c | 8 + .../selftests/bpf/prog_tests/tc_redirect.c | 33 +- tools/testing/selftests/bpf/test_progs.c | 177 ++++++- tools/testing/selftests/bpf/test_progs.h | 6 + 8 files changed, 724 insertions(+), 55 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 1/6 selftests/bpf: Add traffic monitor functions.
Add functions that capture packets and print log in the background. They are supposed to be used for debugging flaky network test cases. A monitored test case should call traffic_monitor_start() to start a thread to capture packets in the background for a given namespace and call traffic_monitor_stop() to stop capturing. (Or, option '-m' implemented by the later patches.)
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 68, ifname lo (In), SYN
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 60, ifname lo (In), SYN, ACK
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 60, ifname lo (In), ACK
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), ACK
IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 52, ifname lo (In), FIN, ACK
IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), RST, ACK
Packet file: packets-2173-86-select_reuseport:sockhash_IPv4_TCP_LOOPBACK_test_detach_bpf-test.log
#280/87 select_reuseport/sockhash IPv4/TCP LOOPBACK test_detach_bpf:OKThe above is the output of an example. It shows the packets of a connection and the name of the file that contains captured packets in the directory /tmp/tmon_pcap. The file can be loaded by tcpdump or wireshark.
This feature only works if libpcap is available. (Could be found by pkg-config)
--- tools/testing/selftests/bpf/Makefile | 4 + tools/testing/selftests/bpf/network_helpers.c | 454 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 18 + 3 files changed, 476 insertions(+)
tools/testing/selftests/bpf/Makefile +4 -0
@@ -41,6 +41,10 @@ CFLAGS += -g $(OPT_FLAGS) -rdynamic \ LDFLAGS += $(SAN_LDFLAGS) LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread +LDLIBS += $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null) +CFLAGS += $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null) +CFLAGS += $(shell $(PKG_CONFIG) --exists libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1") + # The following tests perform type punning and they may break strict # aliasing rules, which are exploited by both GCC and clang by default # while optimizing. This can lead to broken programs.
tools/testing/selftests/bpf/network_helpers.c +454 -0
473 lines not on this page · raw message
tools/testing/selftests/bpf/network_helpers.h +18 -0
@@ -136,4 +136,22 @@ static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, return csum_fold((__u32)s); } +struct tmonitor_ctx ; + +#ifdef TRAFFIC_MONITOR +struct tmonitor_ctx *traffic_monitor_start (const char *netns,const char *test_name, +const char *subtest_name); +void traffic_monitor_stop (struct tmonitor_ctx *ctx); +#else +static inline struct tmonitor_ctx *traffic_monitor_start (const char *netns,const char *test_name, +const char *subtest_name) +{ +return NULL ; +} + +static inline void traffic_monitor_stop (struct tmonitor_ctx *ctx) +{ +} +#endif +#endif
Signature · 1 line
2.34.1
PATCH v6 2/6 selftests/bpf: Add the traffic monitor option to test_progs.
In reply to Kui-Feng LeeAdd option '-m' to test_progs to accept names and patterns of test cases. This option will be used later to enable traffic monitor that capture network packets generated by test cases.
--- tools/testing/selftests/bpf/test_progs.c | 87 +++++++++++++++++------- tools/testing/selftests/bpf/test_progs.h | 2 + 2 files changed, 65 insertions(+), 24 deletions(-)
tools/testing/selftests/bpf/test_progs.c +63 -24
@@ -155,6 +155,7 @@ struct prog_test_def {void (*run_serial_test)(void );bool should_run;bool need_cgroup_cleanup; +bool should_tmon; };/* Override C runtime library's usleep() implementation to ensure nanosleep()
@@ -192,46 +193,59 @@ static bool should_run(struct test_selector *sel, int num, const char *name)return num < sel->num_set_len && sel->num_set[num]; } -static bool should_run_subtest (struct test_selector *sel, -struct test_selector *subtest_sel, -int subtest_num, -const char *test_name, -const char *subtest_name) +static bool match_subtest (struct test_filter_set *filter, +const char *test_name, +const char *subtest_name) {int i, j; -for (i =0 ; i < sel->blacklist.cnt; i++) { -if (glob_match (test_name, sel->blacklist.tests[i].name)) { -if (!sel->blacklist.tests[i].subtest_cnt) -return false; - -for (j =0 ; j < sel->blacklist.tests[i].subtest_cnt; j++) { -if (glob_match (subtest_name, - sel->blacklist.tests[i].subtests[j])) -return false; - } - } - } - -for (i =0 ; i < sel->whitelist.cnt; i++) { -if (glob_match (test_name, sel->whitelist.tests[i].name)) { -if (!sel->whitelist.tests[i].subtest_cnt) +for (i =0 ; i < filter->cnt; i++) { +if (glob_match (test_name, filter->tests[i].name)) { +if (!filter->tests[i].subtest_cnt)return true; -for (j =0 ; j < sel->whitelist.tests[i].subtest_cnt; j++) { +for (j =0 ; j < filter->tests[i].subtest_cnt; j++) {if (glob_match (subtest_name, - sel->whitelist.tests[i].subtests[j])) + filter->tests[i].subtests[j]))return true; } } } +return false; +} + +static bool should_run_subtest (struct test_selector *sel, +struct test_selector *subtest_sel, +int subtest_num, +const char *test_name, +const char *subtest_name) +{ +if (match_subtest (&sel->blacklist, test_name, subtest_name)) +return false; + +if (match_subtest (&sel->whitelist, test_name, subtest_name)) +return true; +if (!sel->whitelist.cnt && !subtest_sel->num_set)return true;return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num]; } +static bool should_tmon (struct test_selector *sel,int num,const char *name) +{ +int i; + +for (i =0 ; i < sel->whitelist.cnt; i++) { +if (glob_match (name, sel->whitelist.tests[i].name) && + !sel->whitelist.tests[i].subtest_cnt) +return true; + } + +return false; +} +static char *test_result (bool failed,bool skipped) {return failed ?"FAIL" : (skipped ?"SKIP" :"OK" );
@@ -488,6 +502,10 @@ bool test__start_subtest(const char *subtest_name)return false; } + subtest_state->should_tmon =match_subtest (&env.tmon_selector.whitelist, + test->test_name, + subtest_name); + env.subtest_state = subtest_state;stdio_hijack_init (&subtest_state->log_buf, &subtest_state->log_cnt);
@@ -667,7 +685,8 @@ enum ARG_KEYS {ARG_TEST_NAME_GLOB_DENYLIST ='d' ,ARG_NUM_WORKERS ='j' ,ARG_DEBUG =-1 , -ARG_JSON_SUMMARY ='J' +ARG_JSON_SUMMARY ='J' , + ARG_TRAFFIC_MONITOR = 'm', }; static const struct argp_option opts[] = {
@@ -694,6 +713,10 @@ static const struct argp_option opts[] = { {"debug" ,ARG_DEBUG ,NULL ,0 , "print extra debug information for test_progs." }, { "json-summary", ARG_JSON_SUMMARY, "FILE", 0, "Write report in json format to this file."}, +#ifdef TRAFFIC_MONITOR + { "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0, + "Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." }, +#endif {}, };
@@ -905,6 +928,18 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)break ;case ARGP_KEY_END :break ; +#ifdef TRAFFIC_MONITOR +case ARG_TRAFFIC_MONITOR : +if (arg[0 ] =='@' ) + err =parse_test_list_file (arg +1 , + &env->tmon_selector.whitelist, + true); +else + err =parse_test_list (arg, + &env->tmon_selector.whitelist, + true); +break ; +#endif default :return ARGP_ERR_UNKNOWN ; }
@@ -1736,6 +1771,9 @@ int main(int argc, char **argv) test->test_num, test->test_name, test->test_name, test->test_name);exit (EXIT_ERR_SETUP_INFRA ); } +if (test->should_run) + test->should_tmon =should_tmon (&env.tmon_selector, + test->test_num, test->test_name); }/* ignore workers if we are just listing */
@@ -1820,6 +1858,7 @@ int main(int argc, char **argv)free_test_selector (&env.test_selector);free_test_selector (&env.subtest_selector); +free_test_selector (&env.tmon_selector);free_test_states ();if (env.succ_cnt + env.fail_cnt + env.skip_cnt ==0 )
tools/testing/selftests/bpf/test_progs.h +2 -0
@@ -74,6 +74,7 @@ struct subtest_state {int error_cnt;bool skipped;bool filtered; +bool should_tmon;FILE *stdout_saved; };
@@ -98,6 +99,7 @@ struct test_state {struct test_env {struct test_selector test_selector;struct test_selector subtest_selector; +struct test_selector tmon_selector;bool verifier_stats;bool debug;enum verbosity verbosity;
Signature · 1 line
2.34.1
PATCH v6 3/6 selftests/bpf: netns_new() and netns_free() helpers.
In reply to Kui-Feng Leenetns_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.
--- tools/testing/selftests/bpf/network_helpers.c | 40 +++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 + tools/testing/selftests/bpf/test_progs.c | 90 +++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 4 + 4 files changed, 136 insertions(+)
tools/testing/selftests/bpf/network_helpers.c +40 -0
@@ -445,6 +445,46 @@ char *ping_command(int family)return "ping" ; } +int make_netns (const char *name) +{ +char *cmd; +int r; + + r =asprintf (&cmd,"ip netns add %s" , name); +if (r <0 ) { +log_err ("Failed to malloc cmd" ); +return -1 ; + } + + r =system (cmd); +if (r >0 ) +/* exit code */ + r = -r; + +free (cmd); +return r; +} + +int remove_netns (const char *name) +{ +char *cmd; +int r; + + r =asprintf (&cmd,"ip netns del %s >/dev/null 2>&1" , name); +if (r <0 ) { +log_err ("Failed to malloc cmd" ); +return -1 ; + } + + r =system (cmd); +if (r >0 ) +/* exit code */ + r = -r; + +free (cmd); +return r; +} +struct nstoken {int orig_netns_fd; };
tools/testing/selftests/bpf/network_helpers.h +2 -0
@@ -93,6 +93,8 @@ struct nstoken;struct nstoken *open_netns (const char *name);void close_netns (struct nstoken *token);int send_recv_data (int lfd,int fd,uint32_t total_bytes); +int make_netns (const char *name); +int remove_netns (const char *name);static __u16 csum_fold (__u32 csum) {
tools/testing/selftests/bpf/test_progs.c +90 -0
102 lines not on this page · raw message
tools/testing/selftests/bpf/test_progs.h +4 -0
@@ -430,6 +430,10 @@ int write_sysctl(const char *sysctl, const char *value);int get_bpf_max_tramp_links_from (struct btf *btf);int get_bpf_max_tramp_links (void ); +struct netns_obj ; +struct netns_obj *netns_new (const char *name,bool open); +void netns_free (struct netns_obj *netns); +#ifdef __x86_64__#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"#elif defined(__s390x__)
Signature · 1 line
2.34.1
PATCH v6 4/6 selftests/bpf: Monitor traffic for tc_redirect.
In reply to Kui-Feng LeeEnable traffic monitoring for the test case tc_redirect.
--- .../selftests/bpf/prog_tests/tc_redirect.c | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/tc_redirect.c +26 -7
@@ -68,6 +68,7 @@ __FILE__, __LINE__, strerror(errno), ##__VA_ARGS__)static const char *const namespaces[] = {NS_SRC ,NS_FWD ,NS_DST ,NULL }; +static struct netns_obj *netns_objs[3 ];static int write_file (const char *path,const char *newval) {
@@ -87,27 +88,45 @@ static int write_file(const char *path, const char *newval)static int netns_setup_namespaces (const char *verb) { +struct netns_obj **ns_obj = netns_objs;const char *const *ns = namespaces; -char cmd[128 ];while (*ns) { -snprintf (cmd,sizeof (cmd),"ip netns %s %s" , verb, *ns); -if (!ASSERT_OK (system (cmd), cmd)) -return -1 ; +if (strcmp (verb,"add" ) ==0 ) { + *ns_obj =netns_new (*ns, false); +if (!*ns_obj) { +log_err ("netns_new failed" ); +return -1 ; + } + }else { +if (!*ns_obj) { +log_err ("netns_obj is NULL" ); +return -1 ; + } +netns_free (*ns_obj); + *ns_obj =NULL ; + } ns++; + ns_obj++; }return 0 ; }static void netns_setup_namespaces_nofail (const char *verb) { +struct netns_obj **ns_obj = netns_objs;const char *const *ns = namespaces; -char cmd[128 ];while (*ns) { -snprintf (cmd,sizeof (cmd),"ip netns %s %s > /dev/null 2>&1" , verb, *ns); -system (cmd); +if (strcmp (verb,"add" ) ==0 ) { + *ns_obj =netns_new (*ns, false); + }else { +if (*ns_obj) +netns_free (*ns_obj); + *ns_obj =NULL ; + } ns++; + ns_obj++; } }
Signature · 1 line
2.34.1
PATCH v6 5/6 selftests/bpf: Monitor traffic for sockmap_listen.
In reply to Kui-Feng LeeEnable traffic monitor for each subtest of sockmap_listen.
--- tools/testing/selftests/bpf/prog_tests/sockmap_listen.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +8 -0
@@ -1925,6 +1925,7 @@ static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_map int family) {const char *family_name, *map_name; +struct netns_obj *netns;char s[MAX_TEST_NAME ]; family_name =family_str (family);
@@ -1932,8 +1933,15 @@ static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_mapsnprintf (s,sizeof (s),"%s %s %s" , map_name, family_name, __func__);if (!test__start_subtest (s))return ; + + netns =netns_new ("test" , true); +if (!ASSERT_OK_PTR (netns,"netns_new" )) +return ; +inet_unix_skb_redir_to_connected (skel, map, family);unix_inet_skb_redir_to_connected (skel, map, family); + +netns_free (netns); } static void run_tests(struct test_sockmap_listen *skel, struct bpf_map *map,
Signature · 1 line
2.34.1
PATCH v6 6/6 selftests/bpf: Monitor traffic for select_reuseport.
In reply to Kui-Feng LeeEnable traffic monitoring for the subtests of select_reuseport.
--- .../bpf/prog_tests/select_reuseport.c | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-)
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +13 -24
@@ -37,9 +37,7 @@ static int sk_fds[REUSEPORT_ARRAY_SIZE];static int reuseport_array =-1 , outer_map =-1 ;static enum bpf_map_type inner_map_type;static int select_by_skb_data_prog; -static int saved_tcp_syncookie =-1 ;static struct bpf_object *obj; -static int saved_tcp_fo =-1 ;static __u32 index_zero;static int epfd;
@@ -193,14 +191,6 @@ static int write_int_sysctl(const char *sysctl, int v)return 0 ; } -static void restore_sysctls (void ) -{ -if (saved_tcp_fo !=-1 ) -write_int_sysctl (TCP_FO_SYSCTL , saved_tcp_fo); -if (saved_tcp_syncookie !=-1 ) -write_int_sysctl (TCP_SYNCOOKIE_SYSCTL , saved_tcp_syncookie); -} -static int enable_fastopen (void ) {int fo;
@@ -793,6 +783,7 @@ static void test_config(int sotype, sa_family_t family, bool inany)TEST_INIT (test_pass_on_err), TEST_INIT(test_detach_bpf), }; +struct netns_obj *netns;char s[MAX_TEST_NAME ];const struct test *t;
@@ -808,9 +799,21 @@ static void test_config(int sotype, sa_family_t family, bool inany)if (!test__start_subtest (s))continue ; + netns =netns_new ("test" , true); +if (!ASSERT_OK_PTR (netns,"netns_new" )) +continue ; + +if (CHECK_FAIL (enable_fastopen ())) + goto out; +if (CHECK_FAIL (disable_syncookie ())) + goto out; +setup_per_test (sotype, family, inany, t->no_inner_map); t->fn (sotype, family);cleanup_per_test (t->no_inner_map); + +out: +netns_free (netns); } }
@@ -850,21 +853,7 @@ void test_map_type(enum bpf_map_type mt)void serial_test_select_reuseport (void ) { - saved_tcp_fo =read_int_sysctl (TCP_FO_SYSCTL ); -if (saved_tcp_fo <0 ) - goto out; - saved_tcp_syncookie =read_int_sysctl (TCP_SYNCOOKIE_SYSCTL ); -if (saved_tcp_syncookie <0 ) - goto out; - -if (enable_fastopen ()) - goto out; -if (disable_syncookie ()) - goto out; -test_map_type (BPF_MAP_TYPE_REUSEPORT_SOCKARRAY );test_map_type (BPF_MAP_TYPE_SOCKMAP );test_map_type (BPF_MAP_TYPE_SOCKHASH ); -out: -restore_sysctls (); }
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v6 2/6] selftests/bpf: Add the traffic monitor option to test_progs.
In reply to Kui-Feng LeeQuoted 1 line · Kui-Feng Lee
+static bool
should_tmon(struct test_selector *sel, int num, const char *name)
"int num" is not used. -m is name only, so not needed?
Quoted 12 lines
tools/testing/selftests/bpf/test_progs.c +12
-0+{ +int i; + +for (i =0 ; i < sel->whitelist.cnt; i++) { +if (glob_match (name, sel->whitelist.tests[i].name) && + !sel->whitelist.tests[i].subtest_cnt) +return true; + } + +return false; +} +
Re: [PATCH bpf-next v6 2/6] selftests/bpf: Add the traffic monitor option to test_progs.
Quoted 5 lines · Martin KaFai Lau
Quoted 2 lines · Kui-Feng Lee
+static bool
should_tmon(struct test_selector *sel, int num, const char *name)"int num" is not used. -m is name only, so not needed?
Right, it is not needed anymore.
Quoted 14 lines
Quoted 13 lines
tools/testing/selftests/bpf/test_progs.c +12
-0+{ + int i; + + for (i = 0; i < sel->whitelist.cnt; i++) { + if (glob_match(name, sel->whitelist.tests[i].name) && + !sel->whitelist.tests[i].subtest_cnt) + return true; + } + + return false; +} +
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers.
In reply to Kui-Feng LeeQuoted 22 lines · Kui-Feng Lee
tools/testing/selftests/bpf/test_progs.c +22
-0+struct netns_obj *netns_new (const char *nsname,bool open) +{ +struct netns_obj *netns_obj =malloc (sizeof (*netns_obj)); +const char *test_name, *subtest_name; +int r; + +if (!netns_obj) +return NULL ; +memset (netns_obj,0 ,sizeof (*netns_obj)); + + netns_obj->nsname =strdup (nsname); +if (!netns_obj->nsname) + goto fail; + +/* Create the network namespace */ + r =make_netns (nsname); +if (r) + goto fail; + +/* Set the network namespace of the current process */ +if (open) { + netns_obj->nstoken =open_netns (nsname);
tools/testing/selftests/bpf/test_progs.c int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) · line 700
Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +10
-0+if (!netns_obj->nstoken) + goto fail; + } + +/* Start traffic monitor */ +if (env.test->should_tmon || + (env.subtest_state && env.subtest_state->should_tmon)) { + test_name = env.test->test_name; + subtest_name = env.subtest_state ? env.subtest_state->name :NULL ; + netns_obj->tmon =traffic_monitor_start (nsname, test_name, subtest_name);
The traffic_monitor_start() does open/close_netns(). close_netns() will restore to the previous netns. Is it better to do traffic_monitor_start() before the above open_netns() such that we don't have to worry about the stacking open_netns and which netns the close_netns will restore?
tools/testing/selftests/bpf/test_progs.c int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) · line 707
Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +7
-0+if (!netns_obj->tmon) +fprintf (stderr,"Failed to start traffic monitor for %s\n" , nsname); + }else { + netns_obj->tmon =NULL ; + } + +system ("ip link set lo up" );
The "bool open" could be false here. This command could be acted on the init_netns and the intention is to set lo up at the newly created netns.
Quoted 45 lines
tools/testing/selftests/bpf/test_progs.c +27
-0+ +return netns_obj; +fail: +close_netns (netns_obj->nstoken); +remove_netns (nsname); +free (netns_obj->nsname); +free (netns_obj); +return NULL ; +} + +/* Delete the network namespace. +* +* This function should be paired with netns_new() to delete the namespace +* created by netns_new(). +*/ +void netns_free (struct netns_obj *netns_obj) +{ +if (!netns_obj) +return ; +if (netns_obj->tmon) +traffic_monitor_stop (netns_obj->tmon); +close_netns (netns_obj->nstoken); +remove_netns (netns_obj->nsname); +free (netns_obj->nsname); +free (netns_obj); +} +/* extern declarations for test funcs */ #define DEFINE_TEST (name) \ extern void test_##name(void) __weak; \tools/testing/selftests/bpf/test_progs.h +4
-0@@ -430,6 +430,10 @@ int write_sysctl(const char *sysctl, const char *value);int get_bpf_max_tramp_links_from (struct btf *btf);int get_bpf_max_tramp_links (void ); +struct netns_obj ; +struct netns_obj *netns_new (const char *name,bool open); +void netns_free (struct netns_obj *netns); +#ifdef __x86_64__#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"#elif defined(__s390x__)
Re: [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers.
Quoted 29 lines · Martin KaFai Lau
Quoted 28 lines · Kui-Feng Lee
tools/testing/selftests/bpf/test_progs.c +28
-0+struct netns_obj *netns_new (const char *nsname,bool open) +{ + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj)); + const char *test_name, *subtest_name; + int r; + + if (!netns_obj) + return NULL; + memset(netns_obj, 0, sizeof(*netns_obj)); + + netns_obj->nsname = strdup(nsname); + if (!netns_obj->nsname) + goto fail; + + /* Create the network namespace */ + r = make_netns(nsname); + if (r) + goto fail; + + /* Set the network namespace of the current process */ + if (open) { + netns_obj->nstoken = open_netns(nsname); + if (!netns_obj->nstoken) + goto fail; + } + + /* Start traffic monitor */ + if (env.test->should_tmon ||
Quoted 8 lines
Quoted 6 lines
+ (env.subtest_state &&
env.subtest_state->should_tmon)) { +test_name=env.test->test_name;
+subtest_name= env.subtest_state ?env.subtest_state->name:
NULL;
+netns_obj->tmon=traffic_monitor_start(nsname, test_name, subtest_name);The
traffic_monitor_start()does open/close_netns().close_netns()will restore to the previous netns. Is it better to dotraffic_monitor_start()before the aboveopen_netns()such that we don't have to worry about the stackingopen_netnsand which netns theclose_netnswill restore?
Do you mean to open_netns() in another thread at the same time and interleave with the open_netns()/close_netns() pairs in the current thread?
Quoted 11 lines
Quoted 10 lines
+ if (!netns_obj->tmon)
+fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
+ } else {
+netns_obj->tmon= NULL;
+ }
+
+system("ip link set lo up");
Quoted 2 lines
The "bool open" could be false here. This command could be acted on the >
init_netnsand the intention is to set lo up at the newly created netns.
You are right! I should enclose this call in-between a pair of open_netns() & close_netns().
Quoted 47 lines
Quoted 46 lines
tools/testing/selftests/bpf/test_progs.c +13
-0+ + return netns_obj; +fail: + close_netns(netns_obj->nstoken); + remove_netns(nsname); + free(netns_obj->nsname); + free(netns_obj); + return NULL; +} + +/* Delete the network namespace. +* +* This function should be paired with netns_new() to delete the namespace
+ * created bynetns_new().
+ */tools/testing/selftests/bpf/test_progs.c +12
-0+void netns_free (struct netns_obj *netns_obj) +{ + if (!netns_obj) + return; + if (netns_obj->tmon) + traffic_monitor_stop(netns_obj->tmon); + close_netns(netns_obj->nstoken); + remove_netns(netns_obj->nsname); + free(netns_obj->nsname); + free(netns_obj); +} +/* extern declarations for test funcs */ #define DEFINE_TEST(name) \
extern void test_##name(void) __weak; \tools/testing/selftests/bpf/test_progs.h +4
-0@@ -430,6 +430,10 @@ int write_sysctl(const char *sysctl, const char *value); int get_bpf_max_tramp_links_from(struct btf *btf); int get_bpf_max_tramp_links(void); +struct netns_obj ; +struct netns_obj *netns_new (const char *name,bool open); +void netns_free (struct netns_obj *netns); + #ifdef __x86_64__ #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" #elif defined(__s390x__)
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v6 1/6] selftests/bpf: Add traffic monitor functions.
In reply to Kui-Feng LeeQuoted 54 lines · Kui-Feng Lee
tools/testing/selftests/bpf/network_helpers.c +54
-0+static bool is_ethernet (const u_char *packet) +{ +u16 arphdr_type; + +memcpy (&arphdr_type, packet +8 ,2 ); + arphdr_type =ntohs (arphdr_type); + +/* Except the following cases, the protocol type contains the +* Ethernet protocol type for the packet. +* +* https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html +*/ +switch (arphdr_type) { +case 770 :/* ARPHRD_FRAD */ +case 778 :/* ARPHDR_IPGRE */ +case 803 :/* ARPHRD_IEEE80211_RADIOTAP */ +return false; + } +return true; +} + +static const char *const pkt_types[] = { +"In" , +"B" ,/* Broadcast */ +"M" ,/* Multicast */ +"C" ,/* Captured with the promiscuous mode */ +"Out" , +}; + +static const char *pkt_type_str (u16 pkt_type) +{ +if (pkt_type <ARRAY_SIZE (pkt_types)) +return pkt_types[pkt_type]; +return "Unknown" ; +} + +/* Show the information of the transport layer in the packet */ +static void show_transport (const u_char *packet,u16 len,u32 ifindex, +const char *src_addr,const char *dst_addr, +u16 proto,bool ipv6,u8 pkt_type) +{ +char *ifname, _ifname[IF_NAMESIZE ]; +const char *transport_str; +u16 src_port, dst_port; +struct udphdr *udp; +struct tcphdr *tcp; + + ifname =if_indextoname (ifindex, _ifname); +if (!ifname) { +snprintf (_ifname,sizeof (_ifname),"unknown(%d)" , ifindex); + ifname = _ifname; + } + +if (proto ==IPPROTO_UDP ) {
tools/testing/selftests/bpf/network_helpers.c int send_recv_data(int lfd, int fd, uint32_t total_bytes) · line 754
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10
-0+ udp = (struct udphdr *)packet; + src_port =ntohs (udp->source); + dst_port =ntohs (udp->dest); + transport_str ="UDP" ; + }else if (proto ==IPPROTO_TCP ) { + tcp = (struct tcphdr *)packet; + src_port =ntohs (tcp->source); + dst_port =ntohs (tcp->dest); + transport_str ="TCP" +;
nit. ";" spilled over to a newline.
tools/testing/selftests/bpf/network_helpers.c int send_recv_data(int lfd, int fd, uint32_t total_bytes) · line 758
Quoted 4 lines
tools/testing/selftests/bpf/network_helpers.c +4
-0+ }else if (proto ==IPPROTO_ICMP ) { +printf ("IPv4 ICMP packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n" , + src_addr, dst_addr, len, packet[0 ], packet[1 ], ifname, +pkt_type_str (pkt_type));
nit. Move pkt_type_str(pkt_type) to the front. That will resemble the tcpdump output to make the output familiar to most people. Same for the other proto below.
Quoted 41 lines
tools/testing/selftests/bpf/network_helpers.c +41
-0+return ; + }else if (proto ==IPPROTO_ICMPV6 ) { +printf ("IPv6 ICMPv6 packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n" , + src_addr, dst_addr, len, packet[0 ], packet[1 ], ifname, +pkt_type_str (pkt_type)); +return ; + }else { +printf ("%s (proto %d): %s -> %s, ifname %s (%s)\n" , + ipv6 ?"IPv6" :"IPv4" , proto, src_addr, dst_addr, + ifname,pkt_type_str (pkt_type)); +return ; + } + +/* TCP */ + +flockfile (stdout); +if (ipv6) +printf ("IPv6 %s packet: [%s]:%d -> [%s]:%d, len %d, ifname %s (%s)" , + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname,pkt_type_str (pkt_type)); +else +printf ("IPv4 %s packet: %s:%d -> %s:%d, len %d, ifname %s (%s)" , + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname,pkt_type_str (pkt_type)); + +if (proto ==IPPROTO_TCP ) { +if (tcp->fin) +printf (", FIN" ); +if (tcp->syn) +printf (", SYN" ); +if (tcp->rst) +printf (", RST" ); +if (tcp->ack) +printf (", ACK" ); + } + +printf ("\n" ); +funlockfile (stdout); +} + +static void show_ipv6_packet (const u_char *packet,u32 ifindex,u8 pkt_type)
tools/testing/selftests/bpf/network_helpers.c int send_recv_data(int lfd, int fd, uint32_t total_bytes) · line 809
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10
-0+{ +char src_str[INET6_ADDRSTRLEN ], dst_str[INET6_ADDRSTRLEN ]; +struct ipv6hdr *pkt = (struct ipv6hdr *)packet; +struct in6_addr src; +struct in6_addr dst; +u_char proto; + +memcpy (&src, &pkt->saddr,sizeof (src)); +memcpy (&dst, &pkt->daddr,sizeof (dst)); +inet_ntop (AF_INET6 , &src, src_str,sizeof (src_str));
nit. In v2, I think Stan has mentioned a similar point that &pkt->saddr can be directly used instead of memcpy. I don't see this mentioned in the changelog also. Re-mentioning here just in case it is an overlook.
Does it need to check inet_ntop error or it will never fail for whatever address a bpf prog may have written to a packet?
Does the src/dst_str need to be initialized if there was a inet_ntop error?
Quoted 43 lines
tools/testing/selftests/bpf/network_helpers.c +43
-0+inet_ntop (AF_INET6 , &dst, dst_str,sizeof (dst_str)); + proto = pkt->nexthdr; +show_transport (packet +sizeof (struct ipv6hdr ), +ntohs (pkt->payload_len), + ifindex, src_str, dst_str, proto, true, pkt_type); +} + +static void show_ipv4_packet (const u_char *packet,u32 ifindex,u8 pkt_type) +{ +char src_str[INET_ADDRSTRLEN ], dst_str[INET_ADDRSTRLEN ]; +struct iphdr *pkt = (struct iphdr *)packet; +struct in_addr src; +struct in_addr dst; +u_char proto; + +memcpy (&src, &pkt->saddr,sizeof (src)); +memcpy (&dst, &pkt->daddr,sizeof (dst)); +inet_ntop (AF_INET , &src, src_str,sizeof (src_str)); +inet_ntop (AF_INET , &dst, dst_str,sizeof (dst_str)); + proto = pkt->protocol; +show_transport (packet +sizeof (struct iphdr ), +ntohs (pkt->tot_len), + ifindex, src_str, dst_str, proto, false, pkt_type); +} + +static void *traffic_monitor_thread (void *arg) +{ +char *ifname, _ifname[IF_NAMESIZE ]; +const u_char *packet, *payload; +struct tmonitor_ctx *ctx = arg; +pcap_dumper_t *dumper = ctx->dumper; +int fd = ctx->pcap_fd, nfds, r; +int wake_fd = ctx->wake_fd_r; +struct pcap_pkthdr header; +pcap_t *pcap = ctx->pcap; +u32 ifindex; +fd_set fds; +u16 proto; +u8 ptype; + + nfds = (fd > wake_fd ? fd : wake_fd) +1 ; +FD_ZERO (&fds); +
tools/testing/selftests/bpf/network_helpers.c int send_recv_data(int lfd, int fd, uint32_t total_bytes) · line 862
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10
-0+while (!ctx->done) { +FD_SET (fd, &fds); +FD_SET (wake_fd, &fds); + r =select (nfds, &fds,NULL ,NULL ,NULL ); +if (!r) +continue ; +if (r <0 ) { +if (errno ==EINTR ) +continue ; +log_err ("Fail to select on pcap fd and wake fd: %s" ,strerror (errno));
nit. log_err already has the strerror(errno). There is at least another case in this patch. Please check.
Quoted 8 lines
tools/testing/selftests/bpf/network_helpers.c +8
-0+break ; + } + + packet =pcap_next (pcap, &header); +if (!packet) +continue ; + +/* According to the man page of pcap_dump(), first argument
tools/testing/selftests/bpf/network_helpers.c int send_recv_data(int lfd, int fd, uint32_t total_bytes) · line 880
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10
-0+* is the pcap_dumper_t pointer even it's argument type is +* u_char *. +*/ +pcap_dump ((u_char *)dumper, &header, packet); + +/* Not sure what other types of packets look like. Here, we +* parse only Ethernet and compatible packets. +*/ +if (!is_ethernet (packet)) { +printf ("Packet captured\n" );
nit. print the value of the arphdr_type.
Quoted 36 lines
tools/testing/selftests/bpf/network_helpers.c +36
-0+continue ; + } + +/* Skip SLL2 header +* https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html +* +* Although the document doesn't mention that, the payload +* doesn't include the Ethernet header. The payload starts +* from the first byte of the network layer header. +*/ + payload = packet +20 ; + +memcpy (&proto, packet,2 ); + proto =ntohs (proto); +memcpy (&ifindex, packet +4 ,4 ); + ifindex =ntohl (ifindex); + ptype = packet[10 ]; + +if (proto ==ETH_P_IPV6 ) { +show_ipv6_packet (payload, ifindex, ptype); + }else if (proto ==ETH_P_IP ) { +show_ipv4_packet (payload, ifindex, ptype); + }else { + ifname =if_indextoname (ifindex, _ifname); +if (!ifname) { +snprintf (_ifname,sizeof (_ifname),"unknown(%d)" , ifindex); + ifname = _ifname; + } + +printf ("Unknown network protocol type %x, ifname %s (%s)\n" , + proto, ifname,pkt_type_str (ptype)); + } + } + +return NULL ; +}
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers.
In reply to Kui-Feng LeeQuoted 40 lines · Kui-Feng Lee
Quoted 28 lines · Martin KaFai Lau
Quoted 27 lines · Kui-Feng Lee
tools/testing/selftests/bpf/test_progs.c +27
-0+struct netns_obj *netns_new (const char *nsname,bool open) +{ + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj)); + const char *test_name, *subtest_name; + int r; + + if (!netns_obj) + return NULL; + memset(netns_obj, 0, sizeof(*netns_obj)); + + netns_obj->nsname = strdup(nsname); + if (!netns_obj->nsname) + goto fail; + + /* Create the network namespace */ + r = make_netns(nsname); + if (r) + goto fail; + + /* Set the network namespace of the current process */ + if (open) { + netns_obj->nstoken = open_netns(nsname); + if (!netns_obj->nstoken) + goto fail; + } + + /* Start traffic monitor */Quoted 8 lines
Quoted 6 lines
+ if (
env.test->should_tmon||
+ (env.subtest_state &&env.subtest_state->should_tmon)) { +test_name=env.test->test_name;
+subtest_name= env.subtest_state ?env.subtest_state->name: NULL; +netns_obj->tmon=traffic_monitor_start(nsname, test_name, subtest_name);The
traffic_monitor_start()does open/close_netns().close_netns()will restore to the previous netns. Is it better to dotraffic_monitor_start()before the aboveopen_netns()such that we don't have to worry about the stackingopen_netnsand which netns theclose_netnswill restore?
Quoted 2 lines
Do you mean to
open_netns()in another thread at the same time and interleave with the open_netns()/close_netns() pairs in the current thread?
I didn't mean this case. I don't think there will be a test calling open/close_nets() in different threads... but will it be an issue?
I was trying to say having the close_netns() restoring to the init_netns for the common case. Easier for the brain to reason without too much unnecessary open_netns stacking. Not saying there is an issue in the patch.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
+ if (!netns_obj->tmon)
+fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
+ } else {
+netns_obj->tmon= NULL;
+ }
+
+system("ip link set lo up");
Quoted 5 lines
Quoted 2 lines
The "bool open" could be false here. This command could be acted on the >
init_netnsand the intention is to set lo up at the newly created netns.You are right! I should enclose this call in-between a pair of
open_netns()&close_netns().
I would just move it to make_netns() and do "ip -n nsname link set lo up". Yes, the traffic_monitor_start() is after the lo is up but I think it is fine.
Re: [PATCH bpf-next v6 1/6] selftests/bpf: Add traffic monitor functions.
In reply to Martin KaFai LauQuoted 59 lines · Martin KaFai Lau
Quoted 58 lines · Kui-Feng Lee
tools/testing/selftests/bpf/network_helpers.c +58
-0+static bool is_ethernet (const u_char *packet) +{ + u16 arphdr_type; + + memcpy(&arphdr_type, packet + 8, 2); + arphdr_type = ntohs(arphdr_type); + + /* Except the following cases, the protocol type contains the + * Ethernet protocol type for the packet. + * + * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html + */ + switch (arphdr_type) { + case 770: /* ARPHRD_FRAD */ + case 778: /* ARPHDR_IPGRE */ + case 803: /* ARPHRD_IEEE80211_RADIOTAP */ + return false; + } + return true; +} + +static const char *const pkt_types[] = { + "In", + "B", /* Broadcast */ + "M", /* Multicast */ + "C", /* Captured with the promiscuous mode */ + "Out", +}; + +static const char *pkt_type_str (u16 pkt_type) +{ + if (pkt_type < ARRAY_SIZE(pkt_types)) + return pkt_types[pkt_type]; + return "Unknown"; +} + +/* Show the information of the transport layer in the packet */ +static void show_transport (const u_char *packet,u16 len,u32 ifindex, + const char *src_addr, const char *dst_addr, + u16 proto, bool ipv6, u8 pkt_type) +{ + char *ifname, _ifname[IF_NAMESIZE]; + const char *transport_str; + u16 src_port, dst_port; + struct udphdr *udp; + struct tcphdr *tcp; + + ifname = if_indextoname(ifindex, _ifname); + if (!ifname) { + snprintf(_ifname, sizeof(_ifname), "unknown(%d)", ifindex); + ifname = _ifname; + } + + if (proto == IPPROTO_UDP) { + udp = (struct udphdr *)packet; + src_port = ntohs(udp->source); + dst_port = ntohs(udp->dest); + transport_str = "UDP";
Quoted 8 lines
Quoted 6 lines
+ } else if (proto ==
IPPROTO_TCP) {
+ tcp = (struct tcphdr *)packet;
+src_port=ntohs(tcp->source);
+dst_port=ntohs(tcp->dest);
+transport_str= "TCP"
+;nit. ";" spilled over to a newline.
Got it!
Quoted 8 lines
Quoted 6 lines
+ } else if (proto ==
IPPROTO_ICMP) {
+ printf("IPv4 ICMP packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n",
+src_addr,dst_addr, len, packet[0], packet[1], ifname, +pkt_type_str(pkt_type));nit. Move
pkt_type_str(pkt_type)to the front. That will resemble the tcpdump output to make the output familiar to most people. Same for the other proto below.
Sure!
Quoted 49 lines
Quoted 49 lines
+ return;
+ } else if (proto ==IPPROTO_ICMPV6) {
+ printf("IPv6 ICMPv6 packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n",
+src_addr,dst_addr, len, packet[0], packet[1], ifname, +pkt_type_str(pkt_type));
+ return;
+ } else {
+ printf("%s (proto %d): %s -> %s, ifname %s (%s)\n", + ipv6 ? "IPv6" : "IPv4", proto,src_addr,dst_addr, + ifname,pkt_type_str(pkt_type));
+ return;
+ }tools/testing/selftests/bpf/network_helpers.c +6
-0+ + /* TCP */ + + flockfile(stdout); + if (ipv6) + printf("IPv6 %s packet: [%s]:%d -> [%s]:%d, len %d, ifname %s(%s)",
+transport_str,src_addr,src_port,
+dst_addr,dst_port, len, ifname,pkt_type_str(pkt_type)); + else
+ printf("IPv4 %s packet: %s:%d -> %s:%d, len %d, ifname %s (%s)", +transport_str,src_addr,src_port,
+dst_addr,dst_port, len, ifname,pkt_type_str(pkt_type));tools/testing/selftests/bpf/network_helpers.c +17
-0+ + if (proto == IPPROTO_TCP) { + if (tcp->fin) + printf(", FIN"); + if (tcp->syn) + printf(", SYN"); + if (tcp->rst) + printf(", RST"); + if (tcp->ack) + printf(", ACK"); + } + + printf("\n"); + funlockfile(stdout); +} + +static void show_ipv6_packet(const u_char *packet, u32 ifindex, u8
pkt_type)
+{
+ char src_str[INET6_ADDRSTRLEN], dst_str[INET6_ADDRSTRLEN]; + struct ipv6hdr *pkt = (struct ipv6hdr *)packet; + structin6_addrsrc;
Quoted 8 lines
Quoted 6 lines
+ struct
in6_addrdst;
+u_charproto;
+
+memcpy(&src, &pkt->saddr, sizeof(src));
+memcpy(&dst, &pkt->daddr, sizeof(dst));
+inet_ntop(AF_INET6, &src, src_str, sizeof(src_str));nit. In v2, I think Stan has mentioned a similar point that
&pkt->saddrcan be directly used instead of memcpy. I don't see this mentioned in the changelog also. Re-mentioning here just in case it is an overlook.
Got it!
Quoted 4 lines
Does it need to check
inet_ntoperror or it will never fail for whatever address a bpf prog may have written to a packet?Does the src/dst_str need to be initialized if there was a
inet_ntoperror?
It will never fail if passing valid parameters.
In our case, the size of src_str & dst_str is big enough for v6 here for v4 below. And, the address family is valid for sure. It should not return any error for any address.
However, I will check it since you have concern about it.
Quoted 50 lines
Quoted 50 lines
+
inet_ntop(AF_INET6, &dst, dst_str, sizeof(dst_str)); + proto =pkt->nexthdr;
+ show_transport(packet + sizeof(struct ipv6hdr),
+ntohs(pkt->payload_len),
+ ifindex,src_str,dst_str, proto, true,pkt_type);tools/testing/selftests/bpf/network_helpers.c +3
-0+} + +static void show_ipv4_packet(const u_char *packet, u32 ifindex, u8
pkt_type)tools/testing/selftests/bpf/network_helpers.c +40
-0+{ + char src_str[INET_ADDRSTRLEN], dst_str[INET_ADDRSTRLEN]; + struct iphdr *pkt = (struct iphdr *)packet; + struct in_addr src; + struct in_addr dst; + u_char proto; + + memcpy(&src, &pkt->saddr, sizeof(src)); + memcpy(&dst, &pkt->daddr, sizeof(dst)); + inet_ntop(AF_INET, &src, src_str, sizeof(src_str)); + inet_ntop(AF_INET, &dst, dst_str, sizeof(dst_str)); + proto = pkt->protocol; + show_transport(packet + sizeof(struct iphdr), + ntohs(pkt->tot_len), + ifindex, src_str, dst_str, proto, false, pkt_type); +} + +static void *traffic_monitor_thread (void *arg) +{ + char *ifname, _ifname[IF_NAMESIZE]; + const u_char *packet, *payload; + struct tmonitor_ctx *ctx = arg; + pcap_dumper_t *dumper = ctx->dumper; + int fd = ctx->pcap_fd, nfds, r; + int wake_fd = ctx->wake_fd_r; + struct pcap_pkthdr header; + pcap_t *pcap = ctx->pcap; + u32 ifindex; + fd_set fds; + u16 proto; + u8 ptype; + + nfds = (fd > wake_fd ? fd : wake_fd) + 1; + FD_ZERO(&fds); + + while (!ctx->done) { + FD_SET(fd, &fds); + FD_SET(wake_fd, &fds); + r = select(nfds, &fds, NULL, NULL, NULL); + if (!r)
Quoted 8 lines
Quoted 6 lines
+ continue;
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+log_err("Fail to select on pcap fd and wake fd: %s", strerror(errno));nit.
log_erralready has the strerror(errno). There is at least another case in this patch. Please check.
Got it!
Quoted 13 lines
Quoted 13 lines
+ break;
+ }tools/testing/selftests/bpf/network_helpers.c +10
-0+ + packet = pcap_next(pcap, &header); + if (!packet) + continue; + + /* According to the man page of pcap_dump(), first argument + * is the pcap_dumper_t pointer even it's argument type is + * u_char *. + */ + pcap_dump((u_char *)dumper, &header, packet);
Quoted 8 lines
Quoted 6 lines
+
+ /* Not sure what other types of packets look like. Here, we + * parse only Ethernet and compatible packets.
+ */
+ if (!is_ethernet(packet)) {
+printf("Packet captured\n");nit. print the value of the
arphdr_type.
Sure!
Quoted 38 lines
Quoted 38 lines
+ continue;
+ }tools/testing/selftests/bpf/network_helpers.c +24
-0+ + /* Skip SLL2 header + * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html + * + * Although the document doesn't mention that, the payload + * doesn't include the Ethernet header. The payload starts + * from the first byte of the network layer header. + */ + payload = packet + 20; + + memcpy(&proto, packet, 2); + proto = ntohs(proto); + memcpy(&ifindex, packet + 4, 4); + ifindex = ntohl(ifindex); + ptype = packet[10]; + + if (proto == ETH_P_IPV6) { + show_ipv6_packet(payload, ifindex, ptype); + } else if (proto == ETH_P_IP) { + show_ipv4_packet(payload, ifindex, ptype); + } else { + ifname = if_indextoname(ifindex, _ifname); + if (!ifname) { + snprintf(_ifname, sizeof(_ifname), "unknown(%d)",ifindex);
+ ifname =_ifname;
+ }tools/testing/selftests/bpf/network_helpers.c +8
-0+ + printf("Unknown network protocol type %x, ifname %s (%s)\n", + proto, ifname, pkt_type_str(ptype)); + } + } + + return NULL; +}
Re: [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers.
In reply to Martin KaFai LauQuoted 42 lines · Martin KaFai Lau
Quoted 41 lines · Kui-Feng Lee
Quoted 37 lines · Martin KaFai Lau
Quoted 31 lines · Kui-Feng Lee
tools/testing/selftests/bpf/test_progs.c +31
-0+struct netns_obj *netns_new (const char *nsname,bool open) +{ + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj)); + const char *test_name, *subtest_name; + int r; + + if (!netns_obj) + return NULL; + memset(netns_obj, 0, sizeof(*netns_obj)); + + netns_obj->nsname = strdup(nsname); + if (!netns_obj->nsname) + goto fail; + + /* Create the network namespace */ + r = make_netns(nsname); + if (r) + goto fail; + + /* Set the network namespace of the current process */ + if (open) { + netns_obj->nstoken = open_netns(nsname); + if (!netns_obj->nstoken) + goto fail; + } + + /* Start traffic monitor */ + if (env.test->should_tmon || + (env.subtest_state && env.subtest_state->should_tmon)) { + test_name = env.test->test_name; + subtest_name = env.subtest_state ? env.subtest_state->nameQuoted 3 lines
: NULL;
+netns_obj->tmon=traffic_monitor_start(nsname, test_name, subtest_name);The
traffic_monitor_start()does open/close_netns().close_netns()will restore to the previous netns. Is it better to dotraffic_monitor_start()before the aboveopen_netns()such that we don't have to worry about the stackingopen_netnsand which netns theclose_netnswill restore?
Quoted 6 lines
Quoted 2 lines
Do you mean to
open_netns()in another thread at the same time and interleave with the open_netns()/close_netns() pairs in the current thread?I didn't mean this case. I don't think there will be a test calling open/close_nets() in different threads... but will it be an issue?
I was trying to say having the
close_netns()restoring to theinit_netnsfor the common case. Easier for the brain to reason without too much unnecessaryopen_netnsstacking. Not saying there is an issue in the patch.
Got it!
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
+ if (!netns_obj->tmon)
+fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
+ } else {
+netns_obj->tmon= NULL;
+ }
+
+system("ip link set lo up");
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
The "bool open" could be false here. This command could be acted on the >
init_netnsand the intention is to set lo up at the newly created netns.You are right! I should enclose this call in-between a pair of
open_netns()&close_netns().I would just move it to
make_netns()and do "ip -n nsname link set lo up". Yes, thetraffic_monitor_start()is after the lo is up but I think it is fine.
Ok!