public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Martin KaFai Lau <martin.lau@linux.dev>
To: George Guo <guodongtai@kylinos.cn>
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org, masahiroy@kernel.org,
	ndesaulniers@google.com, nathan@kernel.org, nicolas@fjasle.eu
Subject: Re: [PATCH] samples/bpf: Update sockex2: get the expected output results
Date: Thu, 27 Jul 2023 11:02:57 -0700	[thread overview]
Message-ID: <78844509-f01f-20cd-4719-49d7480d7aee@linux.dev> (raw)
In-Reply-To: <20230726070955.178288-1-guodongtai@kylinos.cn>

On 7/26/23 12:09 AM, George Guo wrote:
> Running "ping -4 -c5 localhost" only shows 4 times prints not 5:
> 
> $ sudo ./samples/bpf/sockex2
> ip 127.0.0.1 bytes 392 packets 4
> ip 127.0.0.1 bytes 784 packets 8
> ip 127.0.0.1 bytes 1176 packets 12
> ip 127.0.0.1 bytes 1568 packets 16
> 
> debug it with num prints:
> $ sudo ./samples/bpf/sockex2
> num = 1: ip 127.0.0.1 bytes 392 packets 4
> num = 2: ip 127.0.0.1 bytes 784 packets 8
> num = 3: ip 127.0.0.1 bytes 1176 packets 12
> num = 4: ip 127.0.0.1 bytes 1568 packets 16
> 
> The reason is that we check it faster, just put sleep(1) before check
> while(bpf_map_get_next_key(map_fd, &key, &next_key) == 0).
> Now we get the expected results:
> 
> $ sudo ./samples/bpf/sockex2
> num = 0: ip 127.0.0.1 bytes 392 packets 4
> num = 1: ip 127.0.0.1 bytes 784 packets 8
> num = 2: ip 127.0.0.1 bytes 1176 packets 12
> num = 3: ip 127.0.0.1 bytes 1568 packets 16
> num = 4: ip 127.0.0.1 bytes 1960 packets 20
> 
> Signed-off-by: George Guo <guodongtai@kylinos.cn>
> ---
>   samples/bpf/sockex2_user.c | 13 +++++++------
>   1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c
> index 2c18471336f0..84bf1ab77649 100644
> --- a/samples/bpf/sockex2_user.c
> +++ b/samples/bpf/sockex2_user.c
> @@ -18,8 +18,8 @@ int main(int ac, char **argv)
>   	struct bpf_program *prog;
>   	struct bpf_object *obj;
>   	int map_fd, prog_fd;
> -	char filename[256];
> -	int i, sock, err;
> +	char filename[256], command[64];
> +	int i, sock, err, num = 5;
>   	FILE *f;
>   
>   	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
> @@ -42,21 +42,22 @@ int main(int ac, char **argv)
>   	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
>   			  sizeof(prog_fd)) == 0);
>   
> -	f = popen("ping -4 -c5 localhost", "r");
> +	snprintf(command, sizeof(command), "ping -4 -c%d localhost", num);
> +	f = popen(command, "r");
>   	(void) f;
>   
> -	for (i = 0; i < 5; i++) {
> +	for (i = 0; i < num; i++) {
>   		int key = 0, next_key;
>   		struct pair value;
>   
> +		sleep(1);
>   		while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
>   			bpf_map_lookup_elem(map_fd, &next_key, &value);
> -			printf("ip %s bytes %lld packets %lld\n",
> +			printf("num = %d: ip %s bytes %lld packets %lld\n", i,
>   			       inet_ntoa((struct in_addr){htonl(next_key)}),
>   			       value.bytes, value.packets);
>   			key = next_key;
>   		}
> -		sleep(1);

Moving sleep around is paper wrapping it. e.g. what if the first ping did start 
later than 1s? Please address it properly.

tbf, as an example instead of regression test, displaying fewer line output is fine.

>   	}
>   	return 0;
>   }


      parent reply	other threads:[~2023-07-27 18:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26  7:09 [PATCH] samples/bpf: Update sockex2: get the expected output results George Guo
2023-07-26 21:22 ` Nicolas Schier
2023-07-27  3:32   ` guodongtai
2023-07-27 18:02 ` Martin KaFai Lau [this message]

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=78844509-f01f-20cd-4719-49d7480d7aee@linux.dev \
    --to=martin.lau@linux.dev \
    --cc=bpf@vger.kernel.org \
    --cc=guodongtai@kylinos.cn \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolas@fjasle.eu \
    /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