netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
@ 2022-05-09  0:51 Zhengchao Shao
  2022-05-09  9:46 ` Toke Høiland-Jørgensen
  0 siblings, 1 reply; 6+ messages in thread
From: Zhengchao Shao @ 2022-05-09  0:51 UTC (permalink / raw)
  To: bpf, netdev, linux-kernel, ast, daniel, davem, kuba, hawk,
	john.fastabend, andrii, kafai, songliubraving, yhs, kpsingh
  Cc: weiyongjun1, shaozhengchao, yuehaibing

Before detach the prog, we should check detach prog exist or not.

Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
 samples/bpf/xdp_fwd_user.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 1828487bae9a..a273ede3fd73 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -49,7 +49,18 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
 
 static int do_detach(int idx, const char *name)
 {
-	int err;
+	int err = 1;
+	__u32 curr_prog_id;
+
+	if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) {
+		printf("ERROR: bpf_xdp_query_id failed\n");
+		return err;
+	}
+
+	if (!curr_prog_id) {
+		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n", xdp_flags, name);
+		return err;
+	}
 
 	err = bpf_xdp_detach(idx, xdp_flags, NULL);
 	if (err < 0)
-- 
2.17.1


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

end of thread, other threads:[~2022-05-17  9:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09  0:51 [PATCH bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao
2022-05-09  9:46 ` Toke Høiland-Jørgensen
2022-05-09 10:27   ` 答复: " shaozhengchao
2022-05-09 10:55     ` Toke Høiland-Jørgensen
2022-05-17  2:00       ` 答复: " shaozhengchao
2022-05-17  9:42         ` Toke Høiland-Jørgensen

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