bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user
@ 2022-04-21 12:09 Zhengchao Shao
  2022-04-26  4:35 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Zhengchao Shao @ 2022-04-21 12:09 UTC (permalink / raw)
  To: bpf, netdev, linux-kernel, ast, daniel, davem, kuba, hawk,
	john.fastabend, andrii, kafai, songliubraving, yhs, kpsingh
  Cc: weiyongjun1, shaozhengchao, yuehaibing

When xdp_rxq_info_user program exits unexpectedly, it doesn't detach xdp
prog of device, and other xdp prog can't be attached to the device. So
call init_exit() to detach xdp prog when program exits unexpectedly.

Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
 samples/bpf/xdp_rxq_info_user.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
index f2d90cba5164..6378007e085a 100644
--- a/samples/bpf/xdp_rxq_info_user.c
+++ b/samples/bpf/xdp_rxq_info_user.c
@@ -18,7 +18,7 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
 #include <getopt.h>
 #include <net/if.h>
 #include <time.h>
-
+#include <limits.h>
 #include <arpa/inet.h>
 #include <linux/if_link.h>
 
@@ -44,6 +44,9 @@ static struct bpf_map *rx_queue_index_map;
 #define EXIT_FAIL_BPF		4
 #define EXIT_FAIL_MEM		5
 
+#define FAIL_MEM_SIG		INT_MAX
+#define FAIL_STAT_SIG		(INT_MAX - 1)
+
 static const struct option long_options[] = {
 	{"help",	no_argument,		NULL, 'h' },
 	{"dev",		required_argument,	NULL, 'd' },
@@ -77,6 +80,12 @@ static void int_exit(int sig)
 			printf("program on interface changed, not removing\n");
 		}
 	}
+
+	if (sig == FAIL_MEM_SIG)
+		exit(EXIT_FAIL_MEM);
+	else if (sig == FAIL_STAT_SIG)
+		exit(EXIT_FAIL);
+
 	exit(EXIT_OK);
 }
 
@@ -141,7 +150,7 @@ static char* options2str(enum cfg_options_flags flag)
 	if (flag & READ_MEM)
 		return "read";
 	fprintf(stderr, "ERR: Unknown config option flags");
-	exit(EXIT_FAIL);
+	int_exit(FAIL_STAT_SIG);
 }
 
 static void usage(char *argv[])
@@ -174,7 +183,7 @@ static __u64 gettime(void)
 	res = clock_gettime(CLOCK_MONOTONIC, &t);
 	if (res < 0) {
 		fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
-		exit(EXIT_FAIL);
+		int_exit(FAIL_STAT_SIG);
 	}
 	return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
 }
@@ -202,7 +211,7 @@ static struct datarec *alloc_record_per_cpu(void)
 	array = calloc(nr_cpus, sizeof(struct datarec));
 	if (!array) {
 		fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
-		exit(EXIT_FAIL_MEM);
+		int_exit(FAIL_MEM_SIG);
 	}
 	return array;
 }
@@ -215,7 +224,7 @@ static struct record *alloc_record_per_rxq(void)
 	array = calloc(nr_rxqs, sizeof(struct record));
 	if (!array) {
 		fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
-		exit(EXIT_FAIL_MEM);
+		int_exit(FAIL_MEM_SIG);
 	}
 	return array;
 }
@@ -229,7 +238,7 @@ static struct stats_record *alloc_stats_record(void)
 	rec = calloc(1, sizeof(struct stats_record));
 	if (!rec) {
 		fprintf(stderr, "Mem alloc error\n");
-		exit(EXIT_FAIL_MEM);
+		int_exit(FAIL_MEM_SIG);
 	}
 	rec->rxq = alloc_record_per_rxq();
 	for (i = 0; i < nr_rxqs; i++)
-- 
2.17.1


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

* Re: [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user
  2022-04-21 12:09 [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user Zhengchao Shao
@ 2022-04-26  4:35 ` Andrii Nakryiko
  2022-04-27  0:45   ` 答复: " shaozhengchao
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2022-04-26  4:35 UTC (permalink / raw)
  To: Zhengchao Shao
  Cc: bpf, Networking, open list, Alexei Starovoitov, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, Jesper Dangaard Brouer,
	john fastabend, Andrii Nakryiko, Martin Lau, Song Liu,
	Yonghong Song, KP Singh, Wei Yongjun, YueHaibing

On Thu, Apr 21, 2022 at 5:07 AM Zhengchao Shao <shaozhengchao@huawei.com> wrote:
>
> When xdp_rxq_info_user program exits unexpectedly, it doesn't detach xdp
> prog of device, and other xdp prog can't be attached to the device. So
> call init_exit() to detach xdp prog when program exits unexpectedly.
>
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> ---
>  samples/bpf/xdp_rxq_info_user.c | 21 +++++++++++++++------
>  1 file changed, 15 insertions(+), 6 deletions(-)
>

you are introducing a new compilation warning, please fix it


/data/users/andriin/linux/samples/bpf/xdp_rxq_info_user.c: In function
‘options2str’:
/data/users/andriin/linux/samples/bpf/xdp_rxq_info_user.c:153:1:
warning: control reaches end of non-void function [-Wreturn-type]
  153 | }
      | ^

It also would be good to instead use bpf_link-based XDP attachment
that would be auto-detached automatically on process crash
(bpf_link_create() FTW). Please consider also converting this sample
to skeleton (and then bpf_program__attach_xdp() as high-level
alternative to bpf_link_create()).

> diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
> index f2d90cba5164..6378007e085a 100644
> --- a/samples/bpf/xdp_rxq_info_user.c
> +++ b/samples/bpf/xdp_rxq_info_user.c
> @@ -18,7 +18,7 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
>  #include <getopt.h>
>  #include <net/if.h>
>  #include <time.h>

[...]

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

* 答复: [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user
  2022-04-26  4:35 ` Andrii Nakryiko
@ 2022-04-27  0:45   ` shaozhengchao
  0 siblings, 0 replies; 3+ messages in thread
From: shaozhengchao @ 2022-04-27  0:45 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Networking, open list, Alexei Starovoitov, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, Jesper Dangaard Brouer,
	john fastabend, Andrii Nakryiko, Martin Lau, Song Liu,
	Yonghong Song, KP Singh, weiyongjun (A),
	yuehaibing

Thank you for your reply.

I will fix the compilation warning firstly, and then convert this sample to skeleton. Thank you very much.

-----邮件原件-----
发件人: Andrii Nakryiko [mailto:andrii.nakryiko@gmail.com] 
发送时间: 2022年4月26日 12:35
收件人: shaozhengchao <shaozhengchao@huawei.com>
抄送: bpf <bpf@vger.kernel.org>; Networking <netdev@vger.kernel.org>; open list <linux-kernel@vger.kernel.org>; Alexei Starovoitov <ast@kernel.org>; Daniel Borkmann <daniel@iogearbox.net>; David S. Miller <davem@davemloft.net>; Jakub Kicinski <kuba@kernel.org>; Jesper Dangaard Brouer <hawk@kernel.org>; john fastabend <john.fastabend@gmail.com>; Andrii Nakryiko <andrii@kernel.org>; Martin Lau <kafai@fb.com>; Song Liu <songliubraving@fb.com>; Yonghong Song <yhs@fb.com>; KP Singh <kpsingh@kernel.org>; weiyongjun (A) <weiyongjun1@huawei.com>; yuehaibing <yuehaibing@huawei.com>
主题: Re: [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user

On Thu, Apr 21, 2022 at 5:07 AM Zhengchao Shao <shaozhengchao@huawei.com> wrote:
>
> When xdp_rxq_info_user program exits unexpectedly, it doesn't detach 
> xdp prog of device, and other xdp prog can't be attached to the 
> device. So call init_exit() to detach xdp prog when program exits unexpectedly.
>
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> ---
>  samples/bpf/xdp_rxq_info_user.c | 21 +++++++++++++++------
>  1 file changed, 15 insertions(+), 6 deletions(-)
>

you are introducing a new compilation warning, please fix it


/data/users/andriin/linux/samples/bpf/xdp_rxq_info_user.c: In function
‘options2str’:
/data/users/andriin/linux/samples/bpf/xdp_rxq_info_user.c:153:1:
warning: control reaches end of non-void function [-Wreturn-type]
  153 | }
      | ^

It also would be good to instead use bpf_link-based XDP attachment that would be auto-detached automatically on process crash
(bpf_link_create() FTW). Please consider also converting this sample to skeleton (and then bpf_program__attach_xdp() as high-level alternative to bpf_link_create()).

> diff --git a/samples/bpf/xdp_rxq_info_user.c 
> b/samples/bpf/xdp_rxq_info_user.c index f2d90cba5164..6378007e085a 
> 100644
> --- a/samples/bpf/xdp_rxq_info_user.c
> +++ b/samples/bpf/xdp_rxq_info_user.c
> @@ -18,7 +18,7 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
>  #include <getopt.h>
>  #include <net/if.h>
>  #include <time.h>

[...]

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

end of thread, other threads:[~2022-04-27  0:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-21 12:09 [PATCH bpf-next] samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user Zhengchao Shao
2022-04-26  4:35 ` Andrii Nakryiko
2022-04-27  0:45   ` 答复: " shaozhengchao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).