public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Kui-Feng Lee <sinquersw@gmail.com>
To: Martin KaFai Lau <martin.lau@linux.dev>,
	Kui-Feng Lee <thinker.li@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, song@kernel.org,
	kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me,
	geliang@kernel.org, kuifeng@meta.com
Subject: Re: [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers.
Date: Fri, 9 Aug 2024 09:54:41 -0700	[thread overview]
Message-ID: <d4d1b0b5-232c-45ed-bea1-c8628d9b0ed1@gmail.com> (raw)
In-Reply-To: <bd8ee84e-bc30-4635-a82a-f144e99ee345@linux.dev>



On 8/8/24 14:56, Martin KaFai Lau wrote:
> On 8/8/24 1:38 PM, Kui-Feng Lee wrote:
>>
>>
>> On 8/8/24 13:27, Martin KaFai Lau wrote:
>>> On 8/7/24 11:31 AM, Kui-Feng Lee wrote:
>>>> +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->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?
>>
>> 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.

Got it!

> 
>>
>>>
>>>
>>>> +        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.
>>>
>>
>> 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.
> 
> 

Ok!

  reply	other threads:[~2024-08-09 16:54 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-07 18:31 [PATCH bpf-next v6 0/6] monitor network traffic for flaky test cases Kui-Feng Lee
2024-08-07 18:31 ` [PATCH bpf-next v6 1/6] selftests/bpf: Add traffic monitor functions Kui-Feng Lee
2024-08-08 21:35   ` Martin KaFai Lau
2024-08-09 16:01     ` Kui-Feng Lee
2024-08-07 18:31 ` [PATCH bpf-next v6 2/6] selftests/bpf: Add the traffic monitor option to test_progs Kui-Feng Lee
2024-08-08 19:44   ` Martin KaFai Lau
2024-08-08 20:23     ` Kui-Feng Lee
2024-08-07 18:31 ` [PATCH bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers Kui-Feng Lee
2024-08-08 20:27   ` Martin KaFai Lau
2024-08-08 20:38     ` Kui-Feng Lee
2024-08-08 21:56       ` Martin KaFai Lau
2024-08-09 16:54         ` Kui-Feng Lee [this message]
2024-08-07 18:31 ` [PATCH bpf-next v6 4/6] selftests/bpf: Monitor traffic for tc_redirect Kui-Feng Lee
2024-08-07 18:31 ` [PATCH bpf-next v6 5/6] selftests/bpf: Monitor traffic for sockmap_listen Kui-Feng Lee
2024-08-07 18:31 ` [PATCH bpf-next v6 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=d4d1b0b5-232c-45ed-bea1-c8628d9b0ed1@gmail.com \
    --to=sinquersw@gmail.com \
    --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=sdf@fomichev.me \
    --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