From: Stanislav Fomichev <sdf@fomichev.me>
To: Kui-Feng Lee <thinker.li@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev,
song@kernel.org, kernel-team@meta.com, andrii@kernel.org,
geliang@kernel.org, sinquersw@gmail.com, kuifeng@meta.com
Subject: Re: [PATCH bpf-next v4 3/6] selftests/bpf: netns_new() and netns_free() helpers.
Date: Wed, 31 Jul 2024 14:02:33 -0700 [thread overview]
Message-ID: <ZqqmaXpz_xlc6ZJn@mini-arch> (raw)
In-Reply-To: <20240731193140.758210-4-thinker.li@gmail.com>
On 07/31, Kui-Feng Lee wrote:
> 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.
>
> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
> ---
> tools/testing/selftests/bpf/network_helpers.c | 26 ++++++
> tools/testing/selftests/bpf/network_helpers.h | 2 +
> tools/testing/selftests/bpf/test_progs.c | 80 +++++++++++++++++++
> tools/testing/selftests/bpf/test_progs.h | 4 +
> 4 files changed, 112 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
> index a3f0a49fb26f..f2cf43382a8e 100644
> --- a/tools/testing/selftests/bpf/network_helpers.c
> +++ b/tools/testing/selftests/bpf/network_helpers.c
> @@ -432,6 +432,32 @@ char *ping_command(int family)
> return "ping";
> }
>
> +int make_netns(const char *name)
> +{
[..]
> + char cmd[128];
> + int r;
> +
> + snprintf(cmd, sizeof(cmd), "ip netns add %s", name);
> + r = system(cmd);
I doubt that we're gonna see any real problems with that in the tests,
but maybe easier to use apsrint and avoid dealing with fixed 128-byte
string?
> + if (r > 0)
> + /* exit code */
> + return -r;
> + return r;
> +}
> +
> +int remove_netns(const char *name)
> +{
> + char cmd[128];
> + int r;
> +
> + snprintf(cmd, sizeof(cmd), "ip netns del %s >/dev/null 2>&1", name);
> + r = system(cmd);
> + if (r > 0)
> + /* exit code */
> + return -r;
> + return r;
> +}
> +
> struct nstoken {
> int orig_netns_fd;
> };
> diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
> index cce56955371f..f8aa8680a640 100644
> --- a/tools/testing/selftests/bpf/network_helpers.h
> +++ b/tools/testing/selftests/bpf/network_helpers.h
> @@ -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)
> {
> diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> index 95643cd3119a..f86d47efe06e 100644
> --- a/tools/testing/selftests/bpf/test_progs.c
> +++ b/tools/testing/selftests/bpf/test_progs.c
> @@ -1074,6 +1074,86 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
> return err;
> }
>
> +struct netns_obj {
> + char nsname[128];
> + struct tmonitor_ctx *tmon;
> + struct nstoken *nstoken;
> +};
> +
> +/* Create a new network namespace with the given name.
> + *
> + * Create a new network namespace and set the network namespace of the
> + * current process to the new network namespace if the argument "open" is
> + * true. This function should be paired with netns_free() to release the
> + * resource and delete the network namespace.
> + *
> + * It also implements the functionality of the option "-m" by starting
> + * traffic monitor on the background to capture the packets in this network
> + * namespace if the current test or subtest matching the pattern.
> + *
> + * name: the name of the network namespace to create.
> + * open: open the network namespace if true.
> + *
> + * Return: the network namespace object on success, NULL on failure.
> + */
> +struct netns_obj *netns_new(const char *name, bool open)
> +{
> + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj));
> + int r;
> +
> + if (!netns_obj)
> + return NULL;
> + memset(netns_obj, 0, sizeof(*netns_obj));
> +
> + strncpy(netns_obj->nsname, name, sizeof(netns_obj->nsname));
> + netns_obj->nsname[sizeof(netns_obj->nsname) - 1] = '\0';
Same here. Seems easier to have "char *nsname" and do
netns_obj->nsname = strdup(name) here. Trimming the name, in theory,
is problematic because do do remove_netns(netns_obj->nsname) later
on (with potentially trimmed name).
But, again, probably not a huge deal in the selftests. So up to you on
whether you want to address it or not.
next prev parent reply other threads:[~2024-07-31 21:02 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-31 19:31 [PATCH bpf-next v4 0/6] monitor network traffic for flaky test cases Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 1/6] selftests/bpf: Add traffic monitor functions Kui-Feng Lee
2024-07-31 21:07 ` Stanislav Fomichev
2024-08-02 3:54 ` Kui-Feng Lee
2024-08-02 3:29 ` Martin KaFai Lau
2024-08-02 4:31 ` Kui-Feng Lee
2024-08-02 18:58 ` Martin KaFai Lau
2024-08-02 20:37 ` Kui-Feng Lee
2024-08-02 21:12 ` Martin KaFai Lau
2024-08-02 3:43 ` Martin KaFai Lau
2024-08-02 4:35 ` Kui-Feng Lee
2024-08-06 22:07 ` Kui-Feng Lee
2024-08-06 22:22 ` Martin KaFai Lau
2024-08-06 23:18 ` Kui-Feng Lee
2024-08-06 23:41 ` Martin KaFai Lau
2024-08-07 0:17 ` Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 2/6] selftests/bpf: Add the traffic monitor option to test_progs Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 3/6] selftests/bpf: netns_new() and netns_free() helpers Kui-Feng Lee
2024-07-31 21:02 ` Stanislav Fomichev [this message]
2024-08-02 4:42 ` Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 4/6] selftests/bpf: Monitor traffic for tc_redirect Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 5/6] selftests/bpf: Monitor traffic for sockmap_listen Kui-Feng Lee
2024-07-31 21:09 ` Stanislav Fomichev
2024-08-02 4:42 ` Kui-Feng Lee
2024-07-31 19:31 ` [PATCH bpf-next v4 6/6] selftests/bpf: Monitor traffic for select_reuseport Kui-Feng Lee
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZqqmaXpz_xlc6ZJn@mini-arch \
--to=sdf@fomichev.me \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=geliang@kernel.org \
--cc=kernel-team@meta.com \
--cc=kuifeng@meta.com \
--cc=martin.lau@linux.dev \
--cc=sinquersw@gmail.com \
--cc=song@kernel.org \
--cc=thinker.li@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox