public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] samples/bpf: Update sockex2: get the expected output results
@ 2023-07-26  7:09 George Guo
  2023-07-26 21:22 ` Nicolas Schier
  2023-07-27 18:02 ` Martin KaFai Lau
  0 siblings, 2 replies; 4+ messages in thread
From: George Guo @ 2023-07-26  7:09 UTC (permalink / raw)
  To: masahiroy, ndesaulniers, nathan, nicolas; +Cc: linux-kbuild, linux-kernel, bpf

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);
 	}
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] samples/bpf: Update sockex2: get the expected output results
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Schier @ 2023-07-26 21:22 UTC (permalink / raw)
  To: George Guo
  Cc: masahiroy, ndesaulniers, nathan, linux-kbuild, linux-kernel, bpf

[-- Attachment #1: Type: text/plain, Size: 2947 bytes --]

On Wed, Jul 26, 2023 at 03:09:55PM +0800 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>
> ---

Thanks, sounds reasonable to me (but I haven't checked it).  Might you want to
minimize the diff to only contain the move of the sleep call?

Kind regards,
Nicolas


>  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);
>  	}
>  	return 0;
>  }
> -- 
> 2.34.1

-- 
epost|xmpp: nicolas@fjasle.eu          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] samples/bpf: Update sockex2: get the expected output results
  2023-07-26 21:22 ` Nicolas Schier
@ 2023-07-27  3:32   ` guodongtai
  0 siblings, 0 replies; 4+ messages in thread
From: guodongtai @ 2023-07-27  3:32 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: masahiroy, ndesaulniers, nathan, linux-kbuild, linux-kernel, bpf

On Wed, 26 Jul 2023 23:22:49 +0200
Nicolas Schier <nicolas@fjasle.eu> wrote:

> On Wed, Jul 26, 2023 at 03:09:55PM +0800 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>
> > ---  
> 
> Thanks, sounds reasonable to me (but I haven't checked it).  Might
> you want to minimize the diff to only contain the move of the sleep
> call?
> 
> Kind regards,
> Nicolas
> 
> 
> >  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);
> >  	}
> >  	return 0;
> >  }
> > -- 
> > 2.34.1  
> 

hi,

the diff to only contain the move of the sleep call likes this:


diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c
index 2c18471336f0..82bb38b9cab0 100644
--- a/samples/bpf/sockex2_user.c
+++ b/samples/bpf/sockex2_user.c
@@ -49,6 +49,7 @@ int main(int ac, char **argv)
                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",
@@ -56,7 +57,6 @@ int main(int ac, char **argv)
                               value.bytes, value.packets);
                        key = next_key;
                }
-               sleep(1);
        }
        return 0;
 }

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] samples/bpf: Update sockex2: get the expected output results
  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 18:02 ` Martin KaFai Lau
  1 sibling, 0 replies; 4+ messages in thread
From: Martin KaFai Lau @ 2023-07-27 18:02 UTC (permalink / raw)
  To: George Guo
  Cc: linux-kbuild, linux-kernel, bpf, masahiroy, ndesaulniers, nathan,
	nicolas

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;
>   }


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-07-27 18:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox