All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>,
	Martin KaFai Lau <kafai@fb.com>
Subject: [PATCH bpf-next v3 09/15] bpf: add bpf_skc_to_udp6_sock() helper
Date: Mon, 22 Jun 2020 17:36:36 -0700	[thread overview]
Message-ID: <20200623003636.3074473-1-yhs@fb.com> (raw)
In-Reply-To: <20200623003626.3072825-1-yhs@fb.com>

The helper is used in tracing programs to cast a socket
pointer to a udp6_sock pointer.
The return value could be NULL if the casting is illegal.

Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
---
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       |  9 ++++++++-
 kernel/trace/bpf_trace.c       |  2 ++
 net/core/filter.c              | 22 ++++++++++++++++++++++
 scripts/bpf_helpers_doc.py     |  2 ++
 tools/include/uapi/linux/bpf.h |  9 ++++++++-
 6 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index cc3f89827b89..3f5c6bb5e3a7 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1649,6 +1649,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
+extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
 
 const struct bpf_func_proto *bpf_tracing_func_proto(
 	enum bpf_func_id func_id, const struct bpf_prog *prog);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index e256417d94c2..3f4b12c5c563 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3276,6 +3276,12 @@ union bpf_attr {
  *		Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
  *	Return
  *		*sk* if casting is valid, or NULL otherwise.
+ *
+ * struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
+ * 	Description
+ *		Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
+ *	Return
+ *		*sk* if casting is valid, or NULL otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3417,7 +3423,8 @@ union bpf_attr {
 	FN(skc_to_tcp6_sock),		\
 	FN(skc_to_tcp_sock),		\
 	FN(skc_to_tcp_timewait_sock),	\
-	FN(skc_to_tcp_request_sock),
+	FN(skc_to_tcp_request_sock),	\
+	FN(skc_to_udp6_sock),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index de5fbe66e1ca..d10ab16c4a2f 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1523,6 +1523,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_skc_to_tcp_timewait_sock_proto;
 	case BPF_FUNC_skc_to_tcp_request_sock:
 		return &bpf_skc_to_tcp_request_sock_proto;
+	case BPF_FUNC_skc_to_udp6_sock:
+		return &bpf_skc_to_udp6_sock_proto;
 #endif
 	case BPF_FUNC_seq_printf:
 		return prog->expected_attach_type == BPF_TRACE_ITER ?
diff --git a/net/core/filter.c b/net/core/filter.c
index 140fc0fdf3e1..9a98f3616273 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -9325,3 +9325,25 @@ const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
 	.check_btf_id		= check_arg_btf_id,
 	.ret_btf_id		= &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
 };
+
+BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
+{
+	/* udp6_sock type is not generated in dwarf and hence btf,
+	 * trigger an explicit type generation here.
+	 */
+	BTF_TYPE_EMIT(struct udp6_sock);
+	if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
+	    sk->sk_family == AF_INET6)
+		return (unsigned long)sk;
+
+	return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
+	.func			= bpf_skc_to_udp6_sock,
+	.gpl_only		= false,
+	.ret_type		= RET_PTR_TO_BTF_ID_OR_NULL,
+	.arg1_type		= ARG_PTR_TO_BTF_ID,
+	.check_btf_id		= check_arg_btf_id,
+	.ret_btf_id		= &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
+};
diff --git a/scripts/bpf_helpers_doc.py b/scripts/bpf_helpers_doc.py
index d886657c6aaa..6bab40ff442e 100755
--- a/scripts/bpf_helpers_doc.py
+++ b/scripts/bpf_helpers_doc.py
@@ -425,6 +425,7 @@ class PrinterHelpers(Printer):
             'struct tcp_sock',
             'struct tcp_timewait_sock',
             'struct tcp_request_sock',
+            'struct udp6_sock',
 
             'struct __sk_buff',
             'struct sk_msg_md',
@@ -466,6 +467,7 @@ class PrinterHelpers(Printer):
             'struct tcp_sock',
             'struct tcp_timewait_sock',
             'struct tcp_request_sock',
+            'struct udp6_sock',
     }
     mapped_types = {
             'u8': '__u8',
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index e256417d94c2..3f4b12c5c563 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -3276,6 +3276,12 @@ union bpf_attr {
  *		Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
  *	Return
  *		*sk* if casting is valid, or NULL otherwise.
+ *
+ * struct udp6_sock *bpf_skc_to_udp6_sock(void *sk)
+ * 	Description
+ *		Dynamically cast a *sk* pointer to a *udp6_sock* pointer.
+ *	Return
+ *		*sk* if casting is valid, or NULL otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3417,7 +3423,8 @@ union bpf_attr {
 	FN(skc_to_tcp6_sock),		\
 	FN(skc_to_tcp_sock),		\
 	FN(skc_to_tcp_timewait_sock),	\
-	FN(skc_to_tcp_request_sock),
+	FN(skc_to_tcp_request_sock),	\
+	FN(skc_to_udp6_sock),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
-- 
2.24.1


  parent reply	other threads:[~2020-06-23  0:36 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-23  0:36 [PATCH bpf-next v3 00/15] implement bpf iterator for tcp and udp sockets Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 01/15] net: bpf: add bpf_seq_afinfo in tcp_iter_state Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 02/15] net: bpf: implement bpf iterator for tcp Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 03/15] bpf: support 'X' in bpf_seq_printf() helper Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 04/15] bpf: allow tracing programs to use bpf_jiffies64() helper Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 05/15] bpf: add bpf_skc_to_tcp6_sock() helper Yonghong Song
2020-06-23  5:46   ` kernel test robot
2020-06-23  5:46     ` kernel test robot
2020-06-23  5:53   ` kernel test robot
2020-06-23  5:53     ` kernel test robot
2020-06-23  6:39   ` Andrii Nakryiko
2020-06-23 14:52     ` Yonghong Song
2020-06-23 18:23       ` Andrii Nakryiko
2020-06-23 19:45         ` Yonghong Song
2020-06-23 20:11           ` Andrii Nakryiko
2020-06-23 20:46             ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 06/15] bpf: add bpf_skc_to_{tcp,tcp_timewait,tcp_request}_sock() helpers Yonghong Song
2020-06-23  5:18   ` kernel test robot
2020-06-23  5:18     ` [PATCH bpf-next v3 06/15] bpf: add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers kernel test robot
2020-06-23  6:39   ` [PATCH bpf-next v3 06/15] bpf: add bpf_skc_to_{tcp,tcp_timewait,tcp_request}_sock() helpers kernel test robot
2020-06-23  6:39     ` [PATCH bpf-next v3 06/15] bpf: add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers kernel test robot
2020-06-23  0:36 ` [PATCH bpf-next v3 07/15] net: bpf: add bpf_seq_afinfo in udp_iter_state Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 08/15] net: bpf: implement bpf iterator for udp Yonghong Song
2020-06-23  0:36 ` Yonghong Song [this message]
2020-06-23  1:47   ` [PATCH bpf-next v3 09/15] bpf: add bpf_skc_to_udp6_sock() helper Eric Dumazet
2020-06-23  2:22     ` Yonghong Song
2020-06-23 16:27       ` Eric Dumazet
2020-06-23 17:03         ` Yonghong Song
2020-06-23 22:11           ` Eric Dumazet
2020-06-23 22:44             ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 10/15] bpf/selftests: move newer bpf_iter_* type redefining to a new header file Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 11/15] tools/bpf: refactor some net macros to libbpf bpf_tracing_net.h Yonghong Song
2020-06-23  6:45   ` Andrii Nakryiko
2020-06-23 14:56     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 12/15] tools/libbpf: add more common macros to bpf_tracing_net.h Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 13/15] tools/bpf: selftests: implement sample tcp/tcp6 bpf_iter programs Yonghong Song
2020-06-23  6:56   ` Andrii Nakryiko
2020-06-23 15:03     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 14/15] tools/bpf: add udp4/udp6 bpf iterator Yonghong Song
2020-06-23  6:57   ` Andrii Nakryiko
2020-06-23 15:03     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 15/15] bpf/selftests: add tcp/udp iterator programs to selftests Yonghong Song
2020-06-23  6:59   ` Andrii Nakryiko

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=20200623003636.3074473-1-yhs@fb.com \
    --to=yhs@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.