Skip to contentSkip to conversation

net/smc: Introduce BPF injection capability

PATCH In review D. Wythe 4 participants 13 messages May 12, 2023 – May 17, 2023

Subsystems (8)
lore
Share & tools
lore

D. Wythe

author maintainer
#0 lore raw

cover net/smc: Introduce BPF injection capability

From: "D. Wythe" <[email protected]>

This patches attempt to introduce BPF injection capability for SMC, and add selftest to ensure code stability.

As we all know that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This patches implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

v1:

1. split bpf_smc.c
2. remove unnecessary symbol exports

D. Wythe (5):
net/smc: move smc_sock related structure definition net/smc: allow smc to negotiate protocols on policies net/smc: allow set or get smc negotiator by sockopt
bpf: add smc negotiator support in BPF struct_ops bpf/selftests: add selftest for SMC bpf capability

 include/net/smc.h                                | 257 ++++++++++++++++++++++
 include/uapi/linux/smc.h                         |   1 +
 kernel/bpf/bpf_struct_ops_types.h                |   4 +
 net/Makefile                                     |   1 +
 net/smc/Kconfig                                  |  11 +
 net/smc/af_smc.c                                 | 265 ++++++++++++++++++++---
 net/smc/bpf_smc.c                                | 171 +++++++++++++++
 net/smc/smc.h                                    | 224 -------------------
 net/smc/smc_negotiator.c                         | 119 ++++++++++
 net/smc/smc_negotiator.h                         | 116 ++++++++++
 tools/testing/selftests/bpf/prog_tests/bpf_smc.c | 107 +++++++++
 tools/testing/selftests/bpf/progs/bpf_smc.c      | 265 +++++++++++++++++++++++
 12 files changed, 1282 insertions(+), 259 deletions(-)
 create mode 100644 net/smc/bpf_smc.c
 create mode 100644 net/smc/smc_negotiator.c
 create mode 100644 net/smc/smc_negotiator.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 1/5 net/smc: move smc_sock related structure definition

From: "D. Wythe" <[email protected]>

This patch only try to move the definition of smc_sock and its related structure, from net/smc/smc.h to include/net/smc/smc.h. In that way can ebpf generate the BTF ID corresponding to our structure.

Of course, we can also choose to hide the structure and only to expose an intermediate structure, but it requires an additional transformation. If we need to obtain some information frequently, this may cause some performance problems.

---
 include/net/smc.h | 225 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/smc/smc.h     | 224 -----------------------------------------------------
 2 files changed, 225 insertions(+), 224 deletions(-)
include/net/smc.h +225 -0
index a002552..6d076f5 100644
@@ -11,12 +11,17 @@
 #ifndef _SMC_H
 #define _SMC_H
 
+#include <net/inet_connection_sock.h>
 #include <linux/device.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
 #include <linux/wait.h>
 #include "linux/ism.h"
 
+#ifdef ATOMIC64_INIT
+#define KERNEL_HAS_ATOMIC64
+#endif
+
 struct sock;
 
 #define SMC_MAX_PNETID_LEN	16	/* Max. length of PNET id */
@@ -91,4 +96,224 @@ struct smcd_dev {
 	u8 going_away : 1;
 };
 
+struct smc_wr_rx_hdr {	/* common prefix part of LLC and CDC to demultiplex */
+	union {
+		u8 type;
+#if defined(__BIG_ENDIAN_BITFIELD)
+		struct {
+			u8 llc_version:4,
+			   llc_type:4;
+		};
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+		struct {
+			u8 llc_type:4,
+			   llc_version:4;
+		};
+#endif
+	};
+} __aligned(1);
+
+struct smc_cdc_conn_state_flags {
+#if defined(__BIG_ENDIAN_BITFIELD)
+	u8	peer_done_writing : 1;	/* Sending done indicator */
+	u8	peer_conn_closed : 1;	/* Peer connection closed indicator */
+	u8	peer_conn_abort : 1;	/* Abnormal close indicator */
+	u8	reserved : 5;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+	u8	reserved : 5;
+	u8	peer_conn_abort : 1;
+	u8	peer_conn_closed : 1;
+	u8	peer_done_writing : 1;
+#endif
+};
+
+struct smc_cdc_producer_flags {
+#if defined(__BIG_ENDIAN_BITFIELD)
+	u8	write_blocked : 1;	/* Writing Blocked, no rx buf space */
+	u8	urg_data_pending : 1;	/* Urgent Data Pending */
+	u8	urg_data_present : 1;	/* Urgent Data Present */
+	u8	cons_curs_upd_req : 1;	/* cursor update requested */
+	u8	failover_validation : 1;/* message replay due to failover */
+	u8	reserved : 3;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+	u8	reserved : 3;
+	u8	failover_validation : 1;
+	u8	cons_curs_upd_req : 1;
+	u8	urg_data_present : 1;
+	u8	urg_data_pending : 1;
+	u8	write_blocked : 1;
+#endif
+};
+
+/* in host byte order */
+union smc_host_cursor {	/* SMC cursor - an offset in an RMBE */
+	struct {
+		u16	reserved;
+		u16	wrap;		/* window wrap sequence number */
+		u32	count;		/* cursor (= offset) part */
+	};
+#ifdef KERNEL_HAS_ATOMIC64
+	atomic64_t		acurs;	/* for atomic processing */
+#else
+	u64			acurs;	/* for atomic processing */
+#endif
+} __aligned(8);
+
+/* in host byte order, except for flag bitfields in network byte order */
+struct smc_host_cdc_msg {		/* Connection Data Control message */
+	struct smc_wr_rx_hdr		common; /* .type = 0xFE */
+	u8				len;	/* length = 44 */
+	u16				seqno;	/* connection seq # */
+	u32				token;	/* alert_token */
+	union smc_host_cursor		prod;		/* producer cursor */
+	union smc_host_cursor		cons;		/* consumer cursor,
+							 * piggy backed "ack"
+							 */
+	struct smc_cdc_producer_flags	prod_flags;	/* conn. tx/rx status */
+	struct smc_cdc_conn_state_flags	conn_state_flags; /* peer conn. status*/
+	u8				reserved[18];
+} __aligned(8);
+
+enum smc_urg_state {
+	SMC_URG_VALID	= 1,			/* data present */
+	SMC_URG_NOTYET	= 2,			/* data pending */
+	SMC_URG_READ	= 3,			/* data was already read */
+};
+
+struct smc_connection {
+	struct rb_node		alert_node;
+	struct smc_link_group	*lgr;		/* link group of connection */
+	struct smc_link		*lnk;		/* assigned SMC-R link */
+	u32			alert_token_local; /* unique conn. id */
+	u8			peer_rmbe_idx;	/* from tcp handshake */
+	int			peer_rmbe_size;	/* size of peer rx buffer */
+	atomic_t		peer_rmbe_space;/* remaining free bytes in peer
+						 * rmbe
+						 */
+	int			rtoken_idx;	/* idx to peer RMB rkey/addr */
+
+	struct smc_buf_desc	*sndbuf_desc;	/* send buffer descriptor */
+	struct smc_buf_desc	*rmb_desc;	/* RMBE descriptor */
+	int			rmbe_size_short;/* compressed notation */
+	int			rmbe_update_limit;
+						/* lower limit for consumer
+						 * cursor update
+						 */
+
+	struct smc_host_cdc_msg	local_tx_ctrl;	/* host byte order staging
+						 * buffer for CDC msg send
+						 * .prod cf. TCP snd_nxt
+						 * .cons cf. TCP sends ack
+						 */
+	union smc_host_cursor	local_tx_ctrl_fin;
+						/* prod crsr - confirmed by peer
+						 */
+	union smc_host_cursor	tx_curs_prep;	/* tx - prepared data
+						 * snd_max..wmem_alloc
+						 */
+	union smc_host_cursor	tx_curs_sent;	/* tx - sent data
+						 * snd_nxt ?
+						 */
+	union smc_host_cursor	tx_curs_fin;	/* tx - confirmed by peer
+						 * snd-wnd-begin ?
+						 */
+	atomic_t		sndbuf_space;	/* remaining space in sndbuf */
+	u16			tx_cdc_seq;	/* sequence # for CDC send */
+	u16			tx_cdc_seq_fin;	/* sequence # - tx completed */
+	spinlock_t		send_lock;	/* protect wr_sends */
+	atomic_t		cdc_pend_tx_wr; /* number of pending tx CDC wqe
+						 * - inc when post wqe,
+						 * - dec on polled tx cqe
+						 */
+	wait_queue_head_t	cdc_pend_tx_wq; /* wakeup on no cdc_pend_tx_wr*/
+	atomic_t		tx_pushing;     /* nr_threads trying tx push */
+	struct delayed_work	tx_work;	/* retry of smc_cdc_msg_send */
+	u32			tx_off;		/* base offset in peer rmb */
+
+	struct smc_host_cdc_msg	local_rx_ctrl;	/* filled during event_handl.
+						 * .prod cf. TCP rcv_nxt
+						 * .cons cf. TCP snd_una
+						 */
+	union smc_host_cursor	rx_curs_confirmed; /* confirmed to peer
+						    * source of snd_una ?
+						    */
+	union smc_host_cursor	urg_curs;	/* points at urgent byte */
+	enum smc_urg_state	urg_state;
+	bool			urg_tx_pend;	/* urgent data staged */
+	bool			urg_rx_skip_pend;
+						/* indicate urgent oob data
+						 * read, but previous regular
+						 * data still pending
+						 */
+	char			urg_rx_byte;	/* urgent byte */
+	bool			tx_in_release_sock;
+						/* flush pending tx data in
+						 * sock release_cb()
+						 */
+	atomic_t		bytes_to_rcv;	/* arrived data,
+						 * not yet received
+						 */
+	atomic_t		splice_pending;	/* number of spliced bytes
+						 * pending processing
+						 */
+#ifndef KERNEL_HAS_ATOMIC64
+	spinlock_t		acurs_lock;	/* protect cursors */
+#endif
+	struct work_struct	close_work;	/* peer sent some closing */
+	struct work_struct	abort_work;	/* abort the connection */
+	struct tasklet_struct	rx_tsklet;	/* Receiver tasklet for SMC-D */
+	u8			rx_off;		/* receive offset:
+						 * 0 for SMC-R, 32 for SMC-D
+						 */
+	u64			peer_token;	/* SMC-D token of peer */
+	u8			killed : 1;	/* abnormal termination */
+	u8			freed : 1;	/* normal termiation */
+	u8			out_of_sync : 1; /* out of sync with peer */
+};
+
+struct smc_sock {				/* smc sock container */
+	struct sock		sk;
+	struct socket		*clcsock;	/* internal tcp socket */
+	void			(*clcsk_state_change)(struct sock *sk);
+						/* original stat_change fct. */
+	void			(*clcsk_data_ready)(struct sock *sk);
+						/* original data_ready fct. */
+	void			(*clcsk_write_space)(struct sock *sk);
+						/* original write_space fct. */
+	void			(*clcsk_error_report)(struct sock *sk);
+						/* original error_report fct. */
+	struct smc_connection	conn;		/* smc connection */
+	struct smc_sock		*listen_smc;	/* listen parent */
+	struct work_struct	connect_work;	/* handle non-blocking connect*/
+	struct work_struct	tcp_listen_work;/* handle tcp socket accepts */
+	struct work_struct	smc_listen_work;/* prepare new accept socket */
+	struct list_head	accept_q;	/* sockets to be accepted */
+	spinlock_t		accept_q_lock;	/* protects accept_q */
+	bool			limit_smc_hs;	/* put constraint on handshake */
+	bool			use_fallback;	/* fallback to tcp */
+	int			fallback_rsn;	/* reason for fallback */
+	u32			peer_diagnosis; /* decline reason from peer */
+	atomic_t                queued_smc_hs;  /* queued smc handshakes */
+	struct inet_connection_sock_af_ops		af_ops;
+	const struct inet_connection_sock_af_ops	*ori_af_ops;
+						/* original af ops */
+	int			sockopt_defer_accept;
+						/* sockopt TCP_DEFER_ACCEPT
+						 * value
+						 */
+	u8			wait_close_tx_prepared : 1;
+						/* shutdown wr or close
+						 * started, waiting for unsent
+						 * data to be sent
+						 */
+	u8			connect_nonblock : 1;
+						/* non-blocking connect in
+						 * flight
+						 */
+	struct mutex            clcsock_release_lock;
+						/* protects clcsock of a listen
+						 * socket
+						 */
+};
+
 #endif	/* _SMC_H */
net/smc/smc.h +0 -224

242 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 2/5 net/smc: allow smc to negotiate protocols on policies

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

As we all know, the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not.

Just like the congestion control implementation in TCP, this patch provides a generic negotiator implementation. If necessary, we can provide different protocol negotiation strategies for apps based on this implementation.

But most importantly, this patch provides the possibility of eBPF injection, allowing users to implement their own protocol negotiation policy in userspace.

---
 include/net/smc.h        |  32 +++++++++++
 net/Makefile             |   1 +
 net/smc/Kconfig          |  11 ++++
 net/smc/af_smc.c         | 134 ++++++++++++++++++++++++++++++++++++++++++++++-
 net/smc/smc_negotiator.c | 119 +++++++++++++++++++++++++++++++++++++++++
 net/smc/smc_negotiator.h | 116 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 412 insertions(+), 1 deletion(-)
 create mode 100644 net/smc/smc_negotiator.c
 create mode 100644 net/smc/smc_negotiator.h
include/net/smc.h +32 -0
index 6d076f5..191061c 100644
@@ -296,6 +296,8 @@ struct smc_sock {				/* smc sock container */
 	atomic_t                queued_smc_hs;  /* queued smc handshakes */
 	struct inet_connection_sock_af_ops		af_ops;
 	const struct inet_connection_sock_af_ops	*ori_af_ops;
+	/* protocol negotiator ops */
+	const struct smc_sock_negotiator_ops *negotiator_ops;
 						/* original af ops */
 	int			sockopt_defer_accept;
 						/* sockopt TCP_DEFER_ACCEPT
@@ -316,4 +318,34 @@ struct smc_sock {				/* smc sock container */
 						 */
 };
 
+#ifdef CONFIG_SMC_BPF
+/* BPF struct ops for smc protocol negotiator */
+struct smc_sock_negotiator_ops {
+
+	struct list_head	list;
+
+	/* ops name */
+	char		name[16];
+	/* key for name */
+	u32			key;
+
+	/* init with sk */
+	void (*init)(struct sock *sk);
+
+	/* release with sk */
+	void (*release)(struct sock *sk);
+
+	/* advice for negotiate */
+	int (*negotiate)(struct sock *sk);
+
+	/* info gathering timing */
+	void (*collect_info)(struct sock *sk, int timing);
+
+	/* module owner */
+	struct module *owner;
+};
+#else
+struct smc_sock_negotiator_ops {};
+#endif
+
 #endif	/* _SMC_H */
net/Makefile +1 -0
index 4c4dc53..222916a 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_TIPC)		+= tipc/
 obj-$(CONFIG_NETLABEL)		+= netlabel/
 obj-$(CONFIG_IUCV)		+= iucv/
 obj-$(CONFIG_SMC)		+= smc/
+obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o
 obj-$(CONFIG_RFKILL)		+= rfkill/
 obj-$(CONFIG_NET_9P)		+= 9p/
 obj-$(CONFIG_CAIF)		+= caif/
net/smc/Kconfig +11 -0
index 1ab3c5a..bdcc9f1 100644
@@ -19,3 +19,14 @@ config SMC_DIAG
 	  smcss.
 
 	  if unsure, say Y.
+
+config SMC_BPF
+	bool "SMC: support eBPF" if SMC
+	depends on BPF_SYSCALL
+	default n
+	help
+	  Supports eBPF to allows user mode participation in SMC's protocol process
+	  via ebpf programs. Alternatively, obtain information about the SMC socks
+	  through the ebpf program.
+
+	  If unsure, say N.
net/smc/af_smc.c +133 -1

176 lines not on this page · raw message

net/smc/smc_negotiator.c new file +119 -0

119 lines not on this page · raw message

net/smc/smc_negotiator.h new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#8 lore raw

PATCH 3/5 net/smc: allow set or get smc negotiator by sockopt

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

Allow applications to set specific protocol negotiation rules for SMC Socks. Typically, applications need to know the name of the negotiator and then set it through the syscall setsockopt, for examples:

const char name[] = "apps";
setsockopt(fd, SOL_SMC, SMC_NEGOTIATOR, name, sizeof(name) - 1);

Noted that there is no default negotiator in SMC implementation, the application needs to inject the specific implementation through eBPF before setting it up. Although no default negotiator implementation is provided,

Note that SMC does not provide a default negotiator in SMC implementation,
and the application needs to inject the specific implementation through eBPF before setting it up. Although no default negotiator implementation is provided, logically it can be seen as an implementation that always return SK_PASS.

---
 include/uapi/linux/smc.h |   1 +
 net/smc/af_smc.c         | 131 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 98 insertions(+), 34 deletions(-)
include/uapi/linux/smc.h +1 -0
index bb4dacc..1887ed5 100644
@@ -299,5 +299,6 @@ enum {
 
 /* SMC socket options */
 #define SMC_LIMIT_HS 1	/* constraint on smc handshake */
+#define SMC_NEGOTIATOR 2 /* SMC protocol negotiator */
 
 #endif /* _UAPI_LINUX_SMC_H */
net/smc/af_smc.c +97 -34

180 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#9 lore raw

PATCH 4/5 bpf: add smc negotiator support in BPF struct_ops

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

This PATCH attempts to introduce BPF injection capability for SMC. Considering that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCH implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

---
 kernel/bpf/bpf_struct_ops_types.h |   4 +
 net/Makefile                      |   2 +-
 net/smc/bpf_smc.c                 | 171 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 1 deletion(-)
 create mode 100644 net/smc/bpf_smc.c
kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9d..d952b85 100644
@@ -9,4 +9,8 @@
 #include <net/tcp.h>
 BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
 #endif
+#if IS_ENABLED(CONFIG_SMC_BPF)
+#include <net/smc.h>
+BPF_STRUCT_OPS_TYPE(smc_sock_negotiator_ops)
+#endif
 #endif
net/Makefile +1 -1
index 222916a..2139fa4 100644
@@ -52,7 +52,7 @@ obj-$(CONFIG_TIPC)		+= tipc/
 obj-$(CONFIG_NETLABEL)		+= netlabel/
 obj-$(CONFIG_IUCV)		+= iucv/
 obj-$(CONFIG_SMC)		+= smc/
-obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o
+obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o smc/bpf_smc.o
 obj-$(CONFIG_RFKILL)		+= rfkill/
 obj-$(CONFIG_NET_9P)		+= 9p/
 obj-$(CONFIG_CAIF)		+= caif/
net/smc/bpf_smc.c new file +171 -0

172 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#12 lore raw

PATCH 5/5 bpf/selftests: add selftest for SMC bpf capability

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

This PATCH adds a tiny selftest for SMC bpf capability, making decisions on whether to use SMC by collecting certain information from kernel smc sock.

Follow the steps below to run this test.

make -C tools/testing/selftests/bpf
cd tools/testing/selftests/bpf
sudo ./test_progs -t smc

Results shows:

18/1    bpf_smc/load:OK
18/2    bpf_smc/update:OK
18/3    bpf_smc/ref:OK
18      bpf_smc:OK

Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED

---
 tools/testing/selftests/bpf/prog_tests/bpf_smc.c | 107 +++++++++
 tools/testing/selftests/bpf/progs/bpf_smc.c      | 265 +++++++++++++++++++++++
 2 files changed, 372 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_smc.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_smc.c
tools/testing/selftests/bpf/prog_tests/bpf_smc.c new file +107 -0
new file mode 100644
index 0000000..e668857
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/err.h>
+#include <netinet/tcp.h>
+#include <test_progs.h>
+#include "network_helpers.h"
+#include "bpf_smc.skel.h"
+
+#define SOL_SMC 286
+#define SMC_NEGOTIATOR 2
+static const char name[] = "apps";
+
+void run_smc(void)
+{
+	int fd, err;
+
+	fd = socket(AF_SMC, SOCK_STREAM, 0);
+	ASSERT_GT(fd, 0, "create smc socket");
+
+	err = setsockopt(fd, SOL_SMC, SMC_NEGOTIATOR, name, sizeof(name) / sizeof(char));
+	ASSERT_EQ(err, 0, "setsockopt");
+
+	close(fd);
+}
+
+void test_load(void)
+{
+	struct bpf_smc *smc_skel;
+	struct bpf_link *link;
+
+	smc_skel = bpf_smc__open_and_load();
+	if (!ASSERT_OK_PTR(smc_skel, "skel_open"))
+		return;
+
+	link = bpf_map__attach_struct_ops(smc_skel->maps.ops);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto error;
+
+	bpf_link__destroy(link);
+error:
+	bpf_smc__destroy(smc_skel);
+}
+
+void test_update(void)
+{
+	struct bpf_smc *smc_skel;
+	struct bpf_link *link;
+	int err;
+
+	smc_skel = bpf_smc__open_and_load();
+	if (!ASSERT_OK_PTR(smc_skel, "skel_open"))
+		return;
+
+	link = bpf_map__attach_struct_ops(smc_skel->maps.accept);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto error;
+
+	run_smc();
+	ASSERT_EQ(smc_skel->bss->accept_cnt, 1, "accept_cnt");
+
+	err = bpf_link__update_map(link, smc_skel->maps.drop);
+	ASSERT_OK(err, "update_map");
+
+	run_smc();
+	ASSERT_EQ(smc_skel->bss->accept_cnt, 1, "accept_cnt");
+	ASSERT_EQ(smc_skel->bss->drop_cnt, 1, "drop_cnt");
+
+	bpf_link__destroy(link);
+error:
+	bpf_smc__destroy(smc_skel);
+}
+
+void test_ref(void)
+{
+	struct bpf_smc *smc_skel;
+	struct bpf_link *link;
+	int fd = 0, err;
+
+	smc_skel = bpf_smc__open_and_load();
+	if (!ASSERT_OK_PTR(smc_skel, "skel_open"))
+		return;
+
+	link = bpf_map__attach_struct_ops(smc_skel->maps.accept);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto error;
+
+	fd = socket(AF_SMC, SOCK_STREAM, 0);
+	ASSERT_GT(fd, 0, "create smc socket");
+	err = setsockopt(fd, SOL_SMC, SMC_NEGOTIATOR, name, sizeof(name) / sizeof(char));
+	ASSERT_EQ(err, 0, "setsockopt");
+	bpf_link__destroy(link);
+	if (fd > 0)
+		close(fd);
+	ASSERT_EQ(smc_skel->bss->accept_release_cnt, 1, "accept_release_cnt");
+error:
+	bpf_smc__destroy(smc_skel);
+}
+
+void test_bpf_smc(void)
+{
+	if (test__start_subtest("load"))
+		test_load();
+	if (test__start_subtest("update"))
+		test_update();
+	if (test__start_subtest("ref"))
+		test_ref();
+}
tools/testing/selftests/bpf/progs/bpf_smc.c new file +265 -0

265 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies

In reply to D. Wythe
kernel test robot noticed the following build errors:

Hi Wythe,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/D-Wythe/net-smc-move-smc_sock-related-structure-definition/20230512-142700
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/1683872684-64872-3-git-send-email-alibuda%40linux.alibaba.com
patch subject: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies
config: mips-allmodconfig (https://download.01.org/0day-ci/archive/20230512/[email protected]/config)
compiler: mips-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/db8daea84b78121c3612ad5e5ba1d1eaac2f4171
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review D-Wythe/net-smc-move-smc_sock-related-structure-definition/20230512-142700 git checkout db8daea84b78121c3612ad5e5ba1d1eaac2f4171
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "bpf_struct_ops_get" [net/smc/smc.ko] undefined!
>> ERROR: modpost: "bpf_struct_ops_put" [net/smc/smc.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#10 lore raw

Re: [PATCH bpf-next v1 4/5] bpf: add smc negotiator support in BPF struct_ops

In reply to D. Wythe
Quoted 65 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This PATCH attempts to introduce BPF injection capability for SMC. Considering that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCH implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

---
  kernel/bpf/bpf_struct_ops_types.h |   4 +
  net/Makefile                      |   2 +-
  net/smc/bpf_smc.c                 | 171 ++++++++++++++++++++++++++++++++++++++
  3 files changed, 176 insertions(+), 1 deletion(-)

create mode 100644 net/smc/bpf_smc.c

kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9d..d952b85 100644
@@ -9,4 +9,8 @@
  #include <net/tcp.h>
  BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
  #endif
+#if IS_ENABLED(CONFIG_SMC_BPF)
+#include <net/smc.h>
+BPF_STRUCT_OPS_TYPE(smc_sock_negotiator_ops)
+#endif
  #endif
net/Makefile +1 -1
index 222916a..2139fa4 100644
@@ -52,7 +52,7 @@ obj-$(CONFIG_TIPC)		+= tipc/
  obj-$(CONFIG_NETLABEL)		+= netlabel/
  obj-$(CONFIG_IUCV)		+= iucv/
  obj-$(CONFIG_SMC)		+= smc/
-obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o
+obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o smc/bpf_smc.o
  obj-$(CONFIG_RFKILL)		+= rfkill/
  obj-$(CONFIG_NET_9P)		+= 9p/
  obj-$(CONFIG_CAIF)		+= caif/
net/smc/bpf_smc.c new file +0 -0
new file mode 100644
index 0000000..ac9a9ae91

net/smc/bpf_smc.c line 5

Quoted 5 lines
net/smc/bpf_smc.c new file +5 -0
@@ -0,0 +1,171 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ *  Support eBPF for Shared Memory Communications over RDMA (SMC-R) and RoCE
+ *
+ *  Copyright IBM Corp. 2016, 2018

The above description and copyright sound very wierd.

net/smc/bpf_smc.c line 7

Quoted 2 lines
net/smc/bpf_smc.c new file +2 -0
+ *
+ *  Author(s):  D. Wythe <[email protected]>

One author, so just "Author: ...".

Quoted 32 lines
net/smc/bpf_smc.c new file +32 -0
+ */
+
+#include <linux/bpf_verifier.h>
+#include <linux/btf_ids.h>
+#include <linux/kernel.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include "smc_negotiator.h"
+
+extern struct bpf_struct_ops bpf_smc_sock_negotiator_ops;
+static u32 smc_sock_id, sock_id;
+
+static int bpf_smc_negotiator_init(struct btf *btf)
+{
+	s32 type_id;
+
+	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
+	if (type_id < 0)
+		return -EINVAL;
+	sock_id = type_id;
+
+	type_id = btf_find_by_name_kind(btf, "smc_sock", BTF_KIND_STRUCT);
+	if (type_id < 0)
+		return -EINVAL;
+	smc_sock_id = type_id;
+
+	return 0;
+}
+
+/* register ops */
+static int bpf_smc_negotiator_reg(void *kdata)
+{

net/smc/bpf_smc.c line 49

Quoted 10 lines
net/smc/bpf_smc.c new file +10 -0
+	return smc_sock_register_negotiator_ops(kdata);
+}
+
+/* unregister ops */
+static void bpf_smc_negotiator_unreg(void *kdata)
+{
+	smc_sock_unregister_negotiator_ops(kdata);
+}
+
+/* unregister ops */

update ops?
Also I think the above comments like
'register ops', 'unregister ops' and 'update ops' are not necessary. The code itself is self-explanary.

Quoted 66 lines
net/smc/bpf_smc.c new file +66 -0
+static int bpf_smc_negotiator_update(void *kdata, void *old_kdata)
+{
+	return smc_sock_update_negotiator_ops(kdata, old_kdata);
+}
+
+static int bpf_smc_negotiator_validate(void *kdata)
+{
+	return smc_sock_validate_negotiator_ops(kdata);
+}
+
+static int bpf_smc_negotiator_check_member(const struct btf_type *t,
+					   const struct btf_member *member,
+					   const struct bpf_prog *prog)
+{
+	return 0;
+}
+
+static int bpf_smc_negotiator_init_member(const struct btf_type *t,
+					  const struct btf_member *member,
+					  void *kdata, const void *udata)
+{
+	const struct smc_sock_negotiator_ops *uops;
+	struct smc_sock_negotiator_ops *ops;
+	u32 moff;
+
+	uops = (const struct smc_sock_negotiator_ops *)udata;
+	ops = (struct smc_sock_negotiator_ops *)kdata;
+
+	moff = __btf_member_bit_offset(t, member) / 8;
+
+	/* init name */
+	if (moff ==  offsetof(struct smc_sock_negotiator_ops, name)) {
+		if (bpf_obj_name_cpy(ops->name, uops->name,
+				     sizeof(uops->name)) <= 0)
+			return -EINVAL;
+		return 1;
+	}
+
+	return 0;
+}
+
+BPF_CALL_1(bpf_smc_skc_to_tcp_sock, struct sock *, sk)
+{
+	if (sk && sk_fullsock(sk) && sk->sk_family == AF_SMC)
+		return (unsigned long)((struct smc_sock *)(sk))->clcsock->sk;
+
+	return (unsigned long)NULL;
+}
+
+static const struct bpf_func_proto bpf_smc_skc_to_tcp_sock_proto = {
+	.func			= bpf_smc_skc_to_tcp_sock,
+	.gpl_only		= false,
+	.ret_type		= RET_PTR_TO_BTF_ID_OR_NULL,
+	.arg1_type		= ARG_PTR_TO_BTF_ID_SOCK_COMMON,
+	.ret_btf_id		= &btf_sock_ids[BTF_SOCK_TYPE_TCP],
+};
+
+static const struct bpf_func_proto *
+smc_negotiator_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+	const struct btf_member *m;
+	const struct btf_type *t;
+	u32 midx, moff;
+
+	midx = prog->expected_attach_type;
+	t = bpf_smc_sock_negotiator_ops.type;

net/smc/bpf_smc.c line 125

Quoted 10 lines
net/smc/bpf_smc.c new file +10 -0
+	m = &btf_type_member(t)[midx];
+
+	moff = __btf_member_bit_offset(t, m) / 8;
+
+	switch (func_id) {
+	case BPF_FUNC_setsockopt:
+		switch (moff) {
+		/* Avoid potential deadloop risk */
+		case offsetof(struct smc_sock_negotiator_ops, init):
+			fallthrough;

I am not sure whether a 'fallthrough' is needed here or since the case itself does not have any code. Any warning will show up if 'fallthrough;' is removed?

net/smc/bpf_smc.c line 126

Quoted 1 line
net/smc/bpf_smc.c new file +1 -0
+		/* Avoid potential leak risk */

I think more detailed explanation about 'deadloop risk' and 'leak risk' is necessary.

Quoted 36 lines
net/smc/bpf_smc.c new file +36 -0
+		case offsetof(struct smc_sock_negotiator_ops, release):
+			return NULL;
+		}
+		return &bpf_sk_setsockopt_proto;
+	case BPF_FUNC_getsockopt:
+		return &bpf_sk_getsockopt_proto;
+	case BPF_FUNC_skc_to_tcp_sock:
+		return &bpf_smc_skc_to_tcp_sock_proto;
+	default:
+		return bpf_base_func_proto(func_id);
+	}
+}
+
+static bool smc_negotiator_prog_is_valid_access(int off, int size, enum bpf_access_type type,
+						const struct bpf_prog *prog,
+						struct bpf_insn_access_aux *info)
+{
+	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
+		return false;
+
+	/* promote it to smc_sock */
+	if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
+	    !bpf_type_has_unsafe_modifiers(info->reg_type) &&
+	    info->btf_id == sock_id)
+		info->btf_id = smc_sock_id;
+
+	return true;
+}
+
+static const struct bpf_verifier_ops bpf_smc_negotiator_verifier_ops = {
+	.get_func_proto  = smc_negotiator_prog_func_proto,
+	.is_valid_access = smc_negotiator_prog_is_valid_access,
+};
+
+struct bpf_struct_ops bpf_smc_sock_negotiator_ops = {
+	.verifier_ops = &bpf_smc_negotiator_verifier_ops,

net/smc/bpf_smc.c line 171

Quoted 10 lines
net/smc/bpf_smc.c new file +9 -0
+	.init = bpf_smc_negotiator_init,
+	.check_member = bpf_smc_negotiator_check_member,
+	.init_member = bpf_smc_negotiator_init_member,
+	.reg = bpf_smc_negotiator_reg,
+	.update = bpf_smc_negotiator_update,
+	.unreg = bpf_smc_negotiator_unreg,
+	.validate = bpf_smc_negotiator_validate,
+	.name = "smc_sock_negotiator_ops",
+};
\ No newline at end of file

Empty line at the end?

author maintainer
#11 lore raw

Re: [PATCH bpf-next v1 4/5] bpf: add smc negotiator support in BPF struct_ops

Quoted 69 lines · Yonghong Song
Quoted 66 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This PATCH attempts to introduce BPF injection capability for SMC. Considering that the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not, for example, apps can limit the scope of the SMC to a specific IP address or port.

Based on the consideration of transparent replacement, we hope that apps can remain transparent even if they need to formulate some specific strategies for SMC using. That is, do not need to recompile their code.

On the other hand, we need to ensure the scalability of strategies implementation. Although it is simple to use socket options or sysctl, it will bring more complexity to subsequent expansion.

Fortunately, BPF can solve these concerns very well, users can write thire own strategies in eBPF to choose whether to use SMC or not. And it's quite easy for them to modify their strategies in the future.

This PATCH implement injection capability for SMC via struct_ops. In that way, we can add new injection scenarios in the future.

---
  kernel/bpf/bpf_struct_ops_types.h |   4 +
  net/Makefile                      |   2 +-
  net/smc/bpf_smc.c                 | 171
++++++++++++++++++++++++++++++++++++++

  3 files changed, 176 insertions(+), 1 deletion(-)

  create mode 100644 net/smc/bpf_smc.c

kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9d..d952b85 100644
@@ -9,4 +9,8 @@
   #include <net/tcp.h>
   BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
   #endif
+#if IS_ENABLED(CONFIG_SMC_BPF)
+#include <net/smc.h>
+BPF_STRUCT_OPS_TYPE(smc_sock_negotiator_ops)
+#endif
   #endif
net/Makefile +0 -0
index 222916a..2139fa4 100644
@@ -52,7 +52,7 @@ obj-$(CONFIG_TIPC)        += tipc/

  obj-$(CONFIG_NETLABEL)        += netlabel/
  obj-$(CONFIG_IUCV)        += iucv/
  obj-$(CONFIG_SMC)        += smc/

net/Makefile +1 -1
-obj-$(CONFIG_SMC_BPF)        += smc/smc_negotiator.o
+obj-$(CONFIG_SMC_BPF)        += smc/smc_negotiator.o smc/bpf_smc.o

  obj-$(CONFIG_RFKILL)        += rfkill/
  obj-$(CONFIG_NET_9P)        += 9p/
  obj-$(CONFIG_CAIF)        += caif/

net/smc/bpf_smc.c new file +0 -0
new file mode 100644
index 0000000..ac9a9ae91
@@ -0,0 +1,171 @@
Quoted 8 lines
Quoted 6 lines
net/smc/bpf_smc.c new file +3 -0
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ *  Support eBPF for Shared Memory Communications over RDMA (SMC-R) 

and RoCE
+ *
+ *  Copyright IBM Corp. 2016, 2018

The above description and copyright sound very wierd.

Received, let me see how to modify it.

Quoted 5 lines
Quoted 3 lines

+ *
+ *  Author(s):  D. Wythe <[email protected]>

One author, so just "Author: ...".

Got it. I will fix that.

Quoted 39 lines
Quoted 39 lines

+ */

net/smc/bpf_smc.c new file +38 -0
+
+#include <linux/bpf_verifier.h>
+#include <linux/btf_ids.h>
+#include <linux/kernel.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include "smc_negotiator.h"
+
+extern struct bpf_struct_ops bpf_smc_sock_negotiator_ops;
+static u32 smc_sock_id, sock_id;
+
+static int bpf_smc_negotiator_init(struct btf *btf)
+{
+    s32 type_id;
+
+    type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
+    if (type_id < 0)
+        return -EINVAL;
+    sock_id = type_id;
+
+    type_id = btf_find_by_name_kind(btf, "smc_sock", BTF_KIND_STRUCT);
+    if (type_id < 0)
+        return -EINVAL;
+    smc_sock_id = type_id;
+
+    return 0;
+}
+
+/* register ops */
+static int bpf_smc_negotiator_reg(void *kdata)
+{
+    return smc_sock_register_negotiator_ops(kdata);
+}
+
+/* unregister ops */
+static void bpf_smc_negotiator_unreg(void *kdata)
+{
+    smc_sock_unregister_negotiator_ops(kdata);
Quoted 8 lines

net/smc/bpf_smc.c line 43

Quoted 3 lines
net/smc/bpf_smc.c new file +3 -0
+}
+
+/* unregister ops */

update ops?
Also I think the above comments like
'register ops', 'unregister ops' and 'update ops' are not necessary. The code itself is self-explanary.

My mistake, thank you very much for your suggestion. The annotations here are unnecessary indeed.

Quoted 74 lines
Quoted 74 lines
net/smc/bpf_smc.c new file +59 -0
+static int bpf_smc_negotiator_update(void *kdata, void *old_kdata)
+{
+    return smc_sock_update_negotiator_ops(kdata, old_kdata);
+}
+
+static int bpf_smc_negotiator_validate(void *kdata)
+{
+    return smc_sock_validate_negotiator_ops(kdata);
+}
+
+static int bpf_smc_negotiator_check_member(const struct btf_type *t,
+                       const struct btf_member *member,
+                       const struct bpf_prog *prog)
+{
+    return 0;
+}
+
+static int bpf_smc_negotiator_init_member(const struct btf_type *t,
+                      const struct btf_member *member,
+                      void *kdata, const void *udata)
+{
+    const struct smc_sock_negotiator_ops *uops;
+    struct smc_sock_negotiator_ops *ops;
+    u32 moff;
+
+    uops = (const struct smc_sock_negotiator_ops *)udata;
+    ops = (struct smc_sock_negotiator_ops *)kdata;
+
+    moff = __btf_member_bit_offset(t, member) / 8;
+
+    /* init name */
+    if (moff ==  offsetof(struct smc_sock_negotiator_ops, name)) {
+        if (bpf_obj_name_cpy(ops->name, uops->name,
+                     sizeof(uops->name)) <= 0)
+            return -EINVAL;
+        return 1;
+    }
+
+    return 0;
+}
+
+BPF_CALL_1(bpf_smc_skc_to_tcp_sock, struct sock *, sk)
+{
+    if (sk && sk_fullsock(sk) && sk->sk_family == AF_SMC)
+        return (unsigned long)((struct smc_sock *)(sk))->clcsock->sk;
+
+    return (unsigned long)NULL;
+}
+
+static const struct bpf_func_proto bpf_smc_skc_to_tcp_sock_proto = {
+    .func            = bpf_smc_skc_to_tcp_sock,
+    .gpl_only        = false,
+    .ret_type        = RET_PTR_TO_BTF_ID_OR_NULL,
+    .arg1_type        = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
+    .ret_btf_id        = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
+};
+
+static const struct bpf_func_proto *
+smc_negotiator_prog_func_proto(enum bpf_func_id func_id, const 

struct bpf_prog *prog)

net/smc/bpf_smc.c new file +13 -0
+{
+    const struct btf_member *m;
+    const struct btf_type *t;
+    u32 midx, moff;
+
+    midx = prog->expected_attach_type;
+    t = bpf_smc_sock_negotiator_ops.type;
+    m = &btf_type_member(t)[midx];
+
+    moff = __btf_member_bit_offset(t, m) / 8;
+
+    switch (func_id) {
+    case BPF_FUNC_setsockopt:
Quoted 8 lines
Quoted 4 lines

+        switch (moff) {
+        /* Avoid potential deadloop risk */
+        case offsetof(struct smc_sock_negotiator_ops, init):
+            fallthrough;

I am not sure whether a 'fallthrough' is needed here or since the case itself does not have any code. Any warning will show up if 'fallthrough;' is removed?

Yes, if there is no code, fallthrough is unnecessary, I will fix it in the next version.

Quoted 5 lines
Quoted 2 lines

+        /* Avoid potential leak risk */

I think more detailed explanation about 'deadloop risk' and 'leak risk' is necessary.

Got it, i will add more detailed explanation.

Quoted 43 lines
Quoted 43 lines

+        case offsetof(struct smc_sock_negotiator_ops, release):
+            return NULL;
+        }
+        return &bpf_sk_setsockopt_proto;
+    case BPF_FUNC_getsockopt:
+        return &bpf_sk_getsockopt_proto;
+    case BPF_FUNC_skc_to_tcp_sock:
+        return &bpf_smc_skc_to_tcp_sock_proto;
+    default:
+        return bpf_base_func_proto(func_id);
+    }

net/smc/bpf_smc.c new file +3 -0
+}
+
+static bool smc_negotiator_prog_is_valid_access(int off, int size, 

enum bpf_access_type type,
+                        const struct bpf_prog *prog, +                        struct bpf_insn_access_aux *info)

net/smc/bpf_smc.c new file +14 -0
+{
+    if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
+        return false;
+
+    /* promote it to smc_sock */
+    if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
+        !bpf_type_has_unsafe_modifiers(info->reg_type) &&
+        info->btf_id == sock_id)
+        info->btf_id = smc_sock_id;
+
+    return true;
+}
+
+static const struct bpf_verifier_ops bpf_smc_negotiator_verifier_ops 

= {
+    .get_func_proto  = smc_negotiator_prog_func_proto, +    .is_valid_access = smc_negotiator_prog_is_valid_access,

net/smc/bpf_smc.c new file +8 -0
+};
+
+struct bpf_struct_ops bpf_smc_sock_negotiator_ops = {
+    .verifier_ops = &bpf_smc_negotiator_verifier_ops,
+    .init = bpf_smc_negotiator_init,
+    .check_member = bpf_smc_negotiator_check_member,
+    .init_member = bpf_smc_negotiator_init_member,
+    .reg = bpf_smc_negotiator_reg,
Quoted 8 lines
Quoted 6 lines

+    .update = bpf_smc_negotiator_update,
+    .unreg = bpf_smc_negotiator_unreg,
+    .validate = bpf_smc_negotiator_validate,
+    .name = "smc_sock_negotiator_ops",
+};
\ No newline at end of file

Empty line at the end?

Will fix that, thanks.

#4 lore raw

Re: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies

In reply to D. Wythe
Quoted 93 lines · D. Wythe

From: "D. Wythe" <[email protected]>

As we all know, the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not.

Just like the congestion control implementation in TCP, this patch provides a generic negotiator implementation. If necessary, we can provide different protocol negotiation strategies for apps based on this implementation.

But most importantly, this patch provides the possibility of eBPF injection, allowing users to implement their own protocol negotiation policy in userspace.

---
  include/net/smc.h        |  32 +++++++++++
  net/Makefile             |   1 +
  net/smc/Kconfig          |  11 ++++
  net/smc/af_smc.c         | 134 ++++++++++++++++++++++++++++++++++++++++++++++-
  net/smc/smc_negotiator.c | 119 +++++++++++++++++++++++++++++++++++++++++
  net/smc/smc_negotiator.h | 116 ++++++++++++++++++++++++++++++++++++++++
  6 files changed, 412 insertions(+), 1 deletion(-)

create mode 100644 net/smc/smc_negotiator.c
create mode 100644 net/smc/smc_negotiator.h

include/net/smc.h +32 -0
index 6d076f5..191061c 100644
@@ -296,6 +296,8 @@ struct smc_sock {				/* smc sock container */
  	atomic_t                queued_smc_hs;  /* queued smc handshakes */
  	struct inet_connection_sock_af_ops		af_ops;
  	const struct inet_connection_sock_af_ops	*ori_af_ops;
+	/* protocol negotiator ops */
+	const struct smc_sock_negotiator_ops *negotiator_ops;
  						/* original af ops */
  	int			sockopt_defer_accept;
  						/* sockopt TCP_DEFER_ACCEPT
@@ -316,4 +318,34 @@ struct smc_sock {				/* smc sock container */
  						 */
  };
 
+#ifdef CONFIG_SMC_BPF
+/* BPF struct ops for smc protocol negotiator */
+struct smc_sock_negotiator_ops {
+
+	struct list_head	list;
+
+	/* ops name */
+	char		name[16];
+	/* key for name */
+	u32			key;
+
+	/* init with sk */
+	void (*init)(struct sock *sk);
+
+	/* release with sk */
+	void (*release)(struct sock *sk);
+
+	/* advice for negotiate */
+	int (*negotiate)(struct sock *sk);
+
+	/* info gathering timing */
+	void (*collect_info)(struct sock *sk, int timing);
+
+	/* module owner */
+	struct module *owner;
+};
+#else
+struct smc_sock_negotiator_ops {};
+#endif
+
  #endif	/* _SMC_H */
net/Makefile +1 -0
index 4c4dc53..222916a 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_TIPC)		+= tipc/
  obj-$(CONFIG_NETLABEL)		+= netlabel/
  obj-$(CONFIG_IUCV)		+= iucv/
  obj-$(CONFIG_SMC)		+= smc/
+obj-$(CONFIG_SMC_BPF)		+= smc/smc_negotiator.o >   obj-$(CONFIG_RFKILL)		+= rfkill/
  obj-$(CONFIG_NET_9P)		+= 9p/
  obj-$(CONFIG_CAIF)		+= caif/
net/smc/Kconfig +0 -0
index 1ab3c5a..bdcc9f1 100644

net/smc/Kconfig config SMC_DIAG · line 24

Quoted 6 lines
net/smc/Kconfig +3 -0
@@ -19,3 +19,14 @@ config SMC_DIAG
  	  smcss.
 
  	  if unsure, say Y.
+
+config SMC_BPF
+	bool "SMC: support eBPF" if SMC

so smc_negotiator will always be in the kernel image even af_smc is compiled as a module? If the SMC_BPF needs to support af_smc as a module, proper implementation needs to be added to bpf_struct_ops to support module first. It is work-in-progress.

Quoted 29 lines
net/smc/Kconfig +8 -0
+	depends on BPF_SYSCALL
+	default n
+	help
+	  Supports eBPF to allows user mode participation in SMC's protocol process
+	  via ebpf programs. Alternatively, obtain information about the SMC socks
+	  through the ebpf program.
+
+	  If unsure, say N.
net/smc/af_smc.c +6 -0
index 50c38b6..7406fd4 100644
@@ -52,6 +52,7 @@
  #include "smc_close.h"
  #include "smc_stats.h"
  #include "smc_tracepoint.h"
+#include "smc_negotiator.h"
  #include "smc_sysctl.h"
 
  static DEFINE_MUTEX(smc_server_lgr_pending);	/* serialize link group
@@ -68,6 +69,119 @@
  static void smc_tcp_listen_work(struct work_struct *);
  static void smc_connect_work(struct work_struct *);
 
+#ifdef CONFIG_SMC_BPF
+
+/* Check if sock should use smc */
+int smc_sock_should_select_smc(const struct smc_sock *smc)
+{

net/smc/af_smc.c line 86

Quoted 10 lines
net/smc/af_smc.c +10 -0
+	const struct smc_sock_negotiator_ops *ops;
+	int ret;
+
+	rcu_read_lock();
+	ops = READ_ONCE(smc->negotiator_ops);
+
+	/* No negotiator_ops supply or no negotiate func set,
+	 * always pass it.
+	 */
+	if (!ops || !ops->negotiate) {

A smc_sock_negotiator_ops without ->negotiate? Is it useful at all to allow the register in the first place?

Quoted 37 lines
net/smc/af_smc.c +37 -0
+		rcu_read_unlock();
+		return SK_PASS;
+	}
+
+	ret = ops->negotiate((struct sock *)&smc->sk);
+	rcu_read_unlock();
+	return ret;
+}
+
+void smc_sock_perform_collecting_info(const struct smc_sock *smc, int timing)
+{
+	const struct smc_sock_negotiator_ops *ops;
+
+	rcu_read_lock();
+	ops = READ_ONCE(smc->negotiator_ops);
+
+	if (!ops || !ops->collect_info) {
+		rcu_read_unlock();
+		return;
+	}
+
+	ops->collect_info((struct sock *)&smc->sk, timing);
+	rcu_read_unlock();
+}
+
+int smc_sock_assign_negotiator_ops(struct smc_sock *smc, const char *name)
+{
+	struct smc_sock_negotiator_ops *ops;
+	int ret = -EINVAL;
+
+	/* already set */
+	if (READ_ONCE(smc->negotiator_ops))
+		smc_sock_cleanup_negotiator_ops(smc, /* might be still referenced */ false);
+
+	/* Just for clear negotiator_ops */
+	if (!name || !strlen(name))
+		return 0;

net/smc/af_smc.c line 133

Quoted 10 lines
net/smc/af_smc.c +10 -0
+
+	rcu_read_lock();
+	ops = smc_negotiator_ops_get_by_name(name);
+	if (likely(ops)) {
+		if (unlikely(!bpf_try_module_get(ops, ops->owner))) {
+			ret = -EACCES;
+		} else {
+			WRITE_ONCE(smc->negotiator_ops, ops);
+			/* make sure ops can be seen */
+			smp_wmb();

This rcu_read_lock(), WRITE_ONCE, and smp_wmb() combo looks very suspicious. smc->negotiator_ops is protected by rcu (+refcnt) or lock_sock()?

I am going to stop reviewing here.

Quoted 109 lines
net/smc/af_smc.c +70 -1
+			if (ops->init)
+				ops->init(&smc->sk);
+			ret = 0;
+		}
+	}
+	rcu_read_unlock();
+	return ret;
+}
+
+void smc_sock_cleanup_negotiator_ops(struct smc_sock *smc, bool no_more)
+{
+	const struct smc_sock_negotiator_ops *ops;
+
+	ops = READ_ONCE(smc->negotiator_ops);
+
+	/* not all smc sock has negotiator_ops */
+	if (!ops)
+		return;
+
+	might_sleep();
+
+	/* Just ensure data integrity */
+	WRITE_ONCE(smc->negotiator_ops, NULL);
+	/* make sure NULL can be seen */
+	smp_wmb();
+	/* if the socks may have references to the negotiator ops to be removed.
+	 * it means that we might need to wait for the readers of ops
+	 * to complete. It's slow though.
+	 */
+	if (unlikely(!no_more))
+		synchronize_rcu();
+	if (ops->release)
+		ops->release(&smc->sk);
+	bpf_module_put(ops, ops->owner);
+}
+
+void smc_sock_clone_negotiator_ops(struct sock *parent, struct sock *child)
+{
+	const struct smc_sock_negotiator_ops *ops;
+
+	rcu_read_lock();
+	ops = READ_ONCE(smc_sk(parent)->negotiator_ops);
+	if (ops && bpf_try_module_get(ops, ops->owner)) {
+		smc_sk(child)->negotiator_ops = ops;
+		if (ops->init)
+			ops->init(child);
+	}
+	rcu_read_unlock();
+}
+#endif
+
  int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
@@ -166,6 +280,9 @@ static bool smc_hs_congested(const struct sock *sk)
  	if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
  		return true;
 
+	if (!smc_sock_should_select_smc(smc))
+		return true;
+
  	return false;
  }
 
@@ -320,6 +437,9 @@ static int smc_release(struct socket *sock)
  	sock_hold(sk); /* sock_put below */
  	smc = smc_sk(sk);
 
+	/* trigger info gathering if needed.*/
+	smc_sock_perform_collecting_info(smc, SMC_SOCK_CLOSED_TIMING);
+
  	old_state = sk->sk_state;
 
  	/* cleanup for a dangling non-blocking connect */
@@ -356,6 +476,9 @@ static int smc_release(struct socket *sock)
 
  static void smc_destruct(struct sock *sk)
  {
+	/* cleanup negotiator_ops if set */
+	smc_sock_cleanup_negotiator_ops(smc_sk(sk), /* no longer used */ true);
+
  	if (sk->sk_state != SMC_CLOSED)
  		return;
  	if (!sock_flag(sk, SOCK_DEAD))
@@ -1627,7 +1750,14 @@ static int smc_connect(struct socket *sock, struct sockaddr *addr,
  	}
 
  	smc_copy_sock_settings_to_clc(smc);
-	tcp_sk(smc->clcsock->sk)->syn_smc = 1;
+	/* accept out connection as SMC connection */
+	if (smc_sock_should_select_smc(smc) == SK_PASS) {
+		tcp_sk(smc->clcsock->sk)->syn_smc = 1;
+	} else {
+		tcp_sk(smc->clcsock->sk)->syn_smc = 0;
+		smc_switch_to_fallback(smc, /* active fallback */ 0);
+	}
+
  	if (smc->connect_nonblock) {
  		rc = -EALREADY;
  		goto out;
@@ -1679,6 +1809,8 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
  	}
  	*new_smc = smc_sk(new_sk);
 
+	smc_sock_clone_negotiator_ops(lsk, new_sk);
+
  	mutex_lock(&lsmc->clcsock_release_lock);
  	if (lsmc->clcsock)
  		rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
author maintainer
#5 lore raw

Re: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies

Quoted 99 lines · Martin KaFai Lau
Quoted 98 lines · D. Wythe

From: "D. Wythe" <[email protected]>

As we all know, the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not.

Just like the congestion control implementation in TCP, this patch provides a generic negotiator implementation. If necessary, we can provide different protocol negotiation strategies for apps based on this implementation.

But most importantly, this patch provides the possibility of eBPF injection, allowing users to implement their own protocol negotiation policy in userspace.

---
  include/net/smc.h        |  32 +++++++++++
  net/Makefile             |   1 +
  net/smc/Kconfig          |  11 ++++
  net/smc/af_smc.c         | 134
++++++++++++++++++++++++++++++++++++++++++++++-
  net/smc/smc_negotiator.c | 119
+++++++++++++++++++++++++++++++++++++++++
  net/smc/smc_negotiator.h | 116
++++++++++++++++++++++++++++++++++++++++

  6 files changed, 412 insertions(+), 1 deletion(-)

  create mode 100644 net/smc/smc_negotiator.c
  create mode 100644 net/smc/smc_negotiator.h

include/net/smc.h +0 -0
index 6d076f5..191061c 100644
@@ -296,6 +296,8 @@ struct smc_sock {                /* smc sock 

container */
      atomic_t                queued_smc_hs;  /* queued smc handshakes */ struct inet_connection_sock_af_ops        af_ops;
      const struct inet_connection_sock_af_ops    *ori_af_ops; +    /* protocol negotiator ops */
+    const struct smc_sock_negotiator_ops *negotiator_ops;
                          /* original af ops */
      int            sockopt_defer_accept;
                          /* sockopt TCP_DEFER_ACCEPT

include/net/smc.h +0 -0
@@ -316,4 +318,34 @@ struct smc_sock {                /* smc sock 

container */
                           */
  };
  +#ifdef CONFIG_SMC_BPF

include/net/smc.h +29 -0
+/* BPF struct ops for smc protocol negotiator */
+struct smc_sock_negotiator_ops {
+
+    struct list_head    list;
+
+    /* ops name */
+    char        name[16];
+    /* key for name */
+    u32            key;
+
+    /* init with sk */
+    void (*init)(struct sock *sk);
+
+    /* release with sk */
+    void (*release)(struct sock *sk);
+
+    /* advice for negotiate */
+    int (*negotiate)(struct sock *sk);
+
+    /* info gathering timing */
+    void (*collect_info)(struct sock *sk, int timing);
+
+    /* module owner */
+    struct module *owner;
+};
+#else
+struct smc_sock_negotiator_ops {};
+#endif
+

  #endif    /* _SMC_H */

net/Makefile +0 -0
index 4c4dc53..222916a 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_TIPC)        += tipc/

  obj-$(CONFIG_NETLABEL)        += netlabel/ obj-$(CONFIG_IUCV)        += iucv/ obj-$(CONFIG_SMC)        += smc/ +obj-$(CONFIG_SMC_BPF)        += smc/smc_negotiator.o > obj-$(CONFIG_RFKILL)        += rfkill/ obj-$(CONFIG_NET_9P)        += 9p/ obj-$(CONFIG_CAIF)        += caif/

net/smc/Kconfig +0 -0
index 1ab3c5a..bdcc9f1 100644
@@ -19,3 +19,14 @@ config SMC_DIAG
Quoted 9 lines

net/smc/Kconfig config SMC_DIAG · line 24

Quoted 5 lines

        smcss.
          if unsure, say Y.

net/smc/Kconfig +3 -0
+
+config SMC_BPF
+    bool "SMC: support eBPF" if SMC

so smc_negotiator will always be in the kernel image even af_smc is compiled as a module? If the SMC_BPF needs to support af_smc as a module, proper implementation needs to be added to bpf_struct_ops to support module first. It is work-in-progress.

smc_negotiator will not no in the kernel image when af_smc is compiled as a module, it's requires config SMC_BPF also sets to be Y,  while it's default to be N. That's is, even if af_smc is compiled as a module but with no SMC_BPF set, smc_negotiator doesn't exist anywhere.

Quoted 33 lines
Quoted 33 lines

+    depends on BPF_SYSCALL
+    default n
+    help
+      Supports eBPF to allows user mode participation in SMC's protocol process
+      via ebpf programs. Alternatively, obtain information about the SMC socks
+      through the ebpf program.

net/smc/Kconfig +2 -0
+
+      If unsure, say N.
net/smc/af_smc.c +1 -0
index 50c38b6..7406fd4 100644
@@ -52,6 +52,7 @@
   #include "smc_close.h"
   #include "smc_stats.h"
   #include "smc_tracepoint.h"
+#include "smc_negotiator.h"
   #include "smc_sysctl.h"

    static DEFINE_MUTEX(smc_server_lgr_pending);    /* serialize link group

net/smc/af_smc.c +0 -0
@@ -68,6 +69,119 @@
   static void smc_tcp_listen_work(struct work_struct *);
   static void smc_connect_work(struct work_struct *);

  +#ifdef CONFIG_SMC_BPF

net/smc/af_smc.c +8 -0
+
+/* Check if sock should use smc */
+int smc_sock_should_select_smc(const struct smc_sock *smc)
+{
+    const struct smc_sock_negotiator_ops *ops;
+    int ret;
+
+    rcu_read_lock();
Quoted 9 lines
Quoted 6 lines

+    ops = READ_ONCE(smc->negotiator_ops);
+
+    /* No negotiator_ops supply or no negotiate func set, +     * always pass it.
+     */
+    if (!ops || !ops->negotiate) {

A smc_sock_negotiator_ops without ->negotiate? Is it useful at all to allow the register in the first place?

You are right, this can be avoid before registration. I'll fix it.

Quoted 44 lines
Quoted 44 lines

+        rcu_read_unlock();
+        return SK_PASS;
+    }

net/smc/af_smc.c +7 -0
+
+    ret = ops->negotiate((struct sock *)&smc->sk);
+    rcu_read_unlock();
+    return ret;
+}
+
+void smc_sock_perform_collecting_info(const struct smc_sock *smc, 

int timing)

net/smc/af_smc.c +16 -0
+{
+    const struct smc_sock_negotiator_ops *ops;
+
+    rcu_read_lock();
+    ops = READ_ONCE(smc->negotiator_ops);
+
+    if (!ops || !ops->collect_info) {
+        rcu_read_unlock();
+        return;
+    }
+
+    ops->collect_info((struct sock *)&smc->sk, timing);
+    rcu_read_unlock();
+}
+
+int smc_sock_assign_negotiator_ops(struct smc_sock *smc, const char 

*name)

net/smc/af_smc.c +7 -0
+{
+    struct smc_sock_negotiator_ops *ops;
+    int ret = -EINVAL;
+
+    /* already set */
+    if (READ_ONCE(smc->negotiator_ops))
+        smc_sock_cleanup_negotiator_ops(smc, /* might be still 

referenced */ false);

net/smc/af_smc.c +8 -0
+
+    /* Just for clear negotiator_ops */
+    if (!name || !strlen(name))
+        return 0;
+
+    rcu_read_lock();
+    ops = smc_negotiator_ops_get_by_name(name);
+    if (likely(ops)) {
Quoted 9 lines
Quoted 6 lines

+        if (unlikely(!bpf_try_module_get(ops, ops->owner))) { +            ret = -EACCES;
+        } else {
+            WRITE_ONCE(smc->negotiator_ops, ops);
+            /* make sure ops can be seen */
+            smp_wmb();

This rcu_read_lock(), WRITE_ONCE, and smp_wmb() combo looks very suspicious. smc->negotiator_ops is protected by rcu (+refcnt) or lock_sock()?

All access to ops is protected by RCU, and there are no lock_sock. WRITE_ONCE() and smp_wmb() do
not participate in any guarantee of the availability of ops,  The purpose to using them is just wish the latest values can be read as soon as possible , In fact, even if old value is read, there will be no problem in logic because all updates will do synchronize_rcu() and all access to ops is under in rcu_read_lock().

Quoted 2 lines

I am going to stop reviewing here.

Hoping my explanation can answer your questions and still looking forward to your more feedback 😁.

Best wishes.
D. Wythe

Quoted 109 lines
Quoted 108 lines

+            if (ops->init)
+                ops->init(&smc->sk);
+            ret = 0;
+        }
+    }
+    rcu_read_unlock();
+    return ret;

net/smc/af_smc.c +3 -0
+}
+
+void smc_sock_cleanup_negotiator_ops(struct smc_sock *smc, bool 

no_more)

net/smc/af_smc.c +16 -0
+{
+    const struct smc_sock_negotiator_ops *ops;
+
+    ops = READ_ONCE(smc->negotiator_ops);
+
+    /* not all smc sock has negotiator_ops */
+    if (!ops)
+        return;
+
+    might_sleep();
+
+    /* Just ensure data integrity */
+    WRITE_ONCE(smc->negotiator_ops, NULL);
+    /* make sure NULL can be seen */
+    smp_wmb();
+    /* if the socks may have references to the negotiator ops to be 

removed.
+     * it means that we might need to wait for the readers of ops +     * to complete. It's slow though.
+     */
+    if (unlikely(!no_more))
+        synchronize_rcu();
+    if (ops->release)
+        ops->release(&smc->sk);
+    bpf_module_put(ops, ops->owner);

net/smc/af_smc.c +3 -0
+}
+
+void smc_sock_clone_negotiator_ops(struct sock *parent, struct sock 

*child)

net/smc/af_smc.c +14 -0
+{
+    const struct smc_sock_negotiator_ops *ops;
+
+    rcu_read_lock();
+    ops = READ_ONCE(smc_sk(parent)->negotiator_ops);
+    if (ops && bpf_try_module_get(ops, ops->owner)) {
+        smc_sk(child)->negotiator_ops = ops;
+        if (ops->init)
+            ops->init(child);
+    }
+    rcu_read_unlock();
+}
+#endif
+

  int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb) {
      struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);

net/smc/af_smc.c +2 -0
@@ -166,6 +280,9 @@ static bool smc_hs_congested(const struct sock *sk)
       if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
           return true;
   +    if (!smc_sock_should_select_smc(smc))
+        return true;
+
       return false;
   }
   @@ -320,6 +437,9 @@ static int smc_release(struct socket *sock)

      sock_hold(sk); /* sock_put below */
      smc = smc_sk(sk);
  +    /* trigger info gathering if needed.*/
+    smc_sock_perform_collecting_info(smc, SMC_SOCK_CLOSED_TIMING); +
      old_state = sk->sk_state;
        /* cleanup for a dangling non-blocking connect */

net/smc/af_smc.c +12 -1
@@ -356,6 +476,9 @@ static int smc_release(struct socket *sock)
     static void smc_destruct(struct sock *sk)
   {
+    /* cleanup negotiator_ops if set */
+    smc_sock_cleanup_negotiator_ops(smc_sk(sk), /* no longer used */ 
 true);
+
       if (sk->sk_state != SMC_CLOSED)
           return;
       if (!sock_flag(sk, SOCK_DEAD))
@@ -1627,7 +1750,14 @@ static int smc_connect(struct socket *sock, 
 struct sockaddr *addr,
       }
         smc_copy_sock_settings_to_clc(smc);
-    tcp_sk(smc->clcsock->sk)->syn_smc = 1;
+    /* accept out connection as SMC connection */
+    if (smc_sock_should_select_smc(smc) == SK_PASS) {
+        tcp_sk(smc->clcsock->sk)->syn_smc = 1;
+    } else {
+        tcp_sk(smc->clcsock->sk)->syn_smc = 0;
+        smc_switch_to_fallback(smc, /* active fallback */ 0);
+    }
+
       if (smc->connect_nonblock) {
           rc = -EALREADY;
           goto out;
@@ -1679,6 +1809,8 @@ static int smc_clcsock_accept(struct smc_sock 
 *lsmc, struct smc_sock **new_smc)
       }
       *new_smc = smc_sk(new_sk);
   +    smc_sock_clone_negotiator_ops(lsk, new_sk);
+
       mutex_lock(&lsmc->clcsock_release_lock);
       if (lsmc->clcsock)

          rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);

#6 lore raw

Re: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies

Quoted 107 lines · D. Wythe
Quoted 94 lines · Martin KaFai Lau
Quoted 93 lines · D. Wythe

From: "D. Wythe" <[email protected]>

As we all know, the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not.

Just like the congestion control implementation in TCP, this patch provides a generic negotiator implementation. If necessary, we can provide different protocol negotiation strategies for apps based on this implementation.

But most importantly, this patch provides the possibility of eBPF injection, allowing users to implement their own protocol negotiation policy in userspace.

---
  include/net/smc.h        |  32 +++++++++++
  net/Makefile             |   1 +
  net/smc/Kconfig          |  11 ++++
  net/smc/af_smc.c         | 134 ++++++++++++++++++++++++++++++++++++++++++++++- net/smc/smc_negotiator.c | 119 +++++++++++++++++++++++++++++++++++++++++ net/smc/smc_negotiator.h | 116 ++++++++++++++++++++++++++++++++++++++++

  6 files changed, 412 insertions(+), 1 deletion(-)

  create mode 100644 net/smc/smc_negotiator.c
  create mode 100644 net/smc/smc_negotiator.h

include/net/smc.h +0 -0
index 6d076f5..191061c 100644
@@ -296,6 +296,8 @@ struct smc_sock {                /* smc sock container */

      atomic_t                queued_smc_hs;  /* queued smc handshakes */ struct inet_connection_sock_af_ops        af_ops;
      const struct inet_connection_sock_af_ops    *ori_af_ops; +    /* protocol negotiator ops */
+    const struct smc_sock_negotiator_ops *negotiator_ops;
                          /* original af ops */
      int            sockopt_defer_accept;
                          /* sockopt TCP_DEFER_ACCEPT

include/net/smc.h +0 -0
@@ -316,4 +318,34 @@ struct smc_sock {                /* smc sock container */
                            */
   };

  +#ifdef CONFIG_SMC_BPF

include/net/smc.h +29 -0
+/* BPF struct ops for smc protocol negotiator */
+struct smc_sock_negotiator_ops {
+
+    struct list_head    list;
+
+    /* ops name */
+    char        name[16];
+    /* key for name */
+    u32            key;
+
+    /* init with sk */
+    void (*init)(struct sock *sk);
+
+    /* release with sk */
+    void (*release)(struct sock *sk);
+
+    /* advice for negotiate */
+    int (*negotiate)(struct sock *sk);
+
+    /* info gathering timing */
+    void (*collect_info)(struct sock *sk, int timing);
+
+    /* module owner */
+    struct module *owner;
+};
+#else
+struct smc_sock_negotiator_ops {};
+#endif
+

  #endif    /* _SMC_H */

net/Makefile +0 -0
index 4c4dc53..222916a 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_TIPC)        += tipc/

  obj-$(CONFIG_NETLABEL)        += netlabel/ obj-$(CONFIG_IUCV)        += iucv/ obj-$(CONFIG_SMC)        += smc/ +obj-$(CONFIG_SMC_BPF)        += smc/smc_negotiator.o > obj-$(CONFIG_RFKILL)        += rfkill/ obj-$(CONFIG_NET_9P)        += 9p/ obj-$(CONFIG_CAIF)        += caif/

net/smc/Kconfig +0 -0
index 1ab3c5a..bdcc9f1 100644
@@ -19,3 +19,14 @@ config SMC_DIAG
Quoted 9 lines

net/smc/Kconfig config SMC_DIAG · line 24

Quoted 5 lines

        smcss.
          if unsure, say Y.

net/smc/Kconfig +3 -0
+
+config SMC_BPF
+    bool "SMC: support eBPF" if SMC

so smc_negotiator will always be in the kernel image even af_smc is compiled as a module? If the SMC_BPF needs to support af_smc as a module, proper implementation needs to be added to bpf_struct_ops to support module first. It is work-in-progress.

Quoted 4 lines

smc_negotiator will not no in the kernel image when af_smc is compiled as a module, it's requires config SMC_BPF also sets to be Y,  while it's default to be N. That's is, even if af_smc is compiled as a module but with no SMC_BPF set, smc_negotiator doesn't exist anywhere.

CONFIG_SMC_BPF could be "y" while CONFIG_SMC is "m", no?

Anyway, there is a build error when CONFIG_SMC is "m" :(

Quoted 96 lines
Quoted 32 lines
Quoted 32 lines

+    depends on BPF_SYSCALL
+    default n
+    help
+      Supports eBPF to allows user mode participation in SMC's protocol process +      via ebpf programs. Alternatively, obtain information about the SMC socks +      through the ebpf program.

net/smc/Kconfig +2 -0
+
+      If unsure, say N.
net/smc/af_smc.c +1 -0
index 50c38b6..7406fd4 100644
@@ -52,6 +52,7 @@
   #include "smc_close.h"
   #include "smc_stats.h"
   #include "smc_tracepoint.h"
+#include "smc_negotiator.h"
   #include "smc_sysctl.h"

    static DEFINE_MUTEX(smc_server_lgr_pending);    /* serialize link group

net/smc/af_smc.c +0 -0
@@ -68,6 +69,119 @@
   static void smc_tcp_listen_work(struct work_struct *);
   static void smc_connect_work(struct work_struct *);

  +#ifdef CONFIG_SMC_BPF

net/smc/af_smc.c +8 -0
+
+/* Check if sock should use smc */
+int smc_sock_should_select_smc(const struct smc_sock *smc)
+{
+    const struct smc_sock_negotiator_ops *ops;
+    int ret;
+
+    rcu_read_lock();
Quoted 9 lines
Quoted 6 lines

+    ops = READ_ONCE(smc->negotiator_ops);
+
+    /* No negotiator_ops supply or no negotiate func set, +     * always pass it.
+     */
+    if (!ops || !ops->negotiate) {

A smc_sock_negotiator_ops without ->negotiate? Is it useful at all to allow the register in the first place?

You are right, this can be avoid before registration. I'll fix it.

Quoted 42 lines
Quoted 42 lines

+        rcu_read_unlock();
+        return SK_PASS;
+    }

net/smc/af_smc.c +30 -0
+
+    ret = ops->negotiate((struct sock *)&smc->sk);
+    rcu_read_unlock();
+    return ret;
+}
+
+void smc_sock_perform_collecting_info(const struct smc_sock *smc, int timing)
+{
+    const struct smc_sock_negotiator_ops *ops;
+
+    rcu_read_lock();
+    ops = READ_ONCE(smc->negotiator_ops);
+
+    if (!ops || !ops->collect_info) {
+        rcu_read_unlock();
+        return;
+    }
+
+    ops->collect_info((struct sock *)&smc->sk, timing);
+    rcu_read_unlock();
+}
+
+int smc_sock_assign_negotiator_ops(struct smc_sock *smc, const char *name)
+{
+    struct smc_sock_negotiator_ops *ops;
+    int ret = -EINVAL;
+
+    /* already set */
+    if (READ_ONCE(smc->negotiator_ops))
+        smc_sock_cleanup_negotiator_ops(smc, /* might be still referenced */ 

false);

net/smc/af_smc.c +8 -0
+
+    /* Just for clear negotiator_ops */
+    if (!name || !strlen(name))
+        return 0;
+
+    rcu_read_lock();
+    ops = smc_negotiator_ops_get_by_name(name);
+    if (likely(ops)) {
Quoted 9 lines
Quoted 6 lines

+        if (unlikely(!bpf_try_module_get(ops, ops->owner))) { +            ret = -EACCES;
+        } else {
+            WRITE_ONCE(smc->negotiator_ops, ops);
+            /* make sure ops can be seen */
+            smp_wmb();

This rcu_read_lock(), WRITE_ONCE, and smp_wmb() combo looks very suspicious. smc->negotiator_ops is protected by rcu (+refcnt) or lock_sock()?

Quoted 4 lines

All access to ops is protected by RCU, and there are no lock_sock. WRITE_ONCE() and smp_wmb() do
not participate in any guarantee of the availability of ops,  The purpose to using them is just wish the latest values can be read as soon as possible , In fact, even if old value is read, there will be no problem in logic because all updates will do synchronize_rcu() and all access to ops is under in rcu_read_lock().

The explanation is not encouraging. No clear benefit while having this kind of complexity here. Switching tcp congestion ops also does not require this. Some of the new codes is in af_smc but bpf is the primary user. It is not something that I would like to maintain and then need to reason about this unusual pattern a year later. Beside, this negotiator_ops assignment must be done under a lock_sock(). The same probably is true for calling ops->negotiate() where the bpf prog may be looking at the sk and calling bpf_setsockopt.

Quoted 6 lines
Quoted 3 lines

I am going to stop reviewing here.

Hoping my explanation can answer your questions and still looking forward to your more feedback 😁.

Sorry, based on the review so far (there was some RFC before), it is not something that I want to continue to review and maintain a bpf hook for it. You have to solicit other known community members for review and sponsor this set from now on.

author maintainer
#7 lore raw

Re: [PATCH bpf-next v1 2/5] net/smc: allow smc to negotiate protocols on policies

Quoted 113 lines · Martin KaFai Lau
Quoted 112 lines · D. Wythe
Quoted 108 lines · Martin KaFai Lau
Quoted 100 lines · D. Wythe

From: "D. Wythe" <[email protected]>

As we all know, the SMC protocol is not suitable for all scenarios, especially for short-lived. However, for most applications, they cannot guarantee that there are no such scenarios at all. Therefore, apps may need some specific strategies to decide shall we need to use SMC or not.

Just like the congestion control implementation in TCP, this patch provides a generic negotiator implementation. If necessary, we can provide different protocol negotiation strategies for apps based on this implementation.

But most importantly, this patch provides the possibility of eBPF injection, allowing users to implement their own protocol negotiation policy in userspace.

---
  include/net/smc.h        |  32 +++++++++++
  net/Makefile             |   1 +
  net/smc/Kconfig          |  11 ++++
  net/smc/af_smc.c         | 134
++++++++++++++++++++++++++++++++++++++++++++++-
  net/smc/smc_negotiator.c | 119
+++++++++++++++++++++++++++++++++++++++++
  net/smc/smc_negotiator.h | 116
++++++++++++++++++++++++++++++++++++++++

  6 files changed, 412 insertions(+), 1 deletion(-)

  create mode 100644 net/smc/smc_negotiator.c
  create mode 100644 net/smc/smc_negotiator.h

include/net/smc.h +0 -0
index 6d076f5..191061c 100644
@@ -296,6 +296,8 @@ struct smc_sock {                /* smc sock 

container */
      atomic_t                queued_smc_hs;  /* queued smc handshakes */ struct inet_connection_sock_af_ops        af_ops;
      const struct inet_connection_sock_af_ops *ori_af_ops;
+    /* protocol negotiator ops */
+    const struct smc_sock_negotiator_ops *negotiator_ops;
                          /* original af ops */
      int            sockopt_defer_accept;
                          /* sockopt TCP_DEFER_ACCEPT

include/net/smc.h +0 -0
@@ -316,4 +318,34 @@ struct smc_sock {                /* smc sock 

container */
                           */
  };
  +#ifdef CONFIG_SMC_BPF

include/net/smc.h +29 -0
+/* BPF struct ops for smc protocol negotiator */
+struct smc_sock_negotiator_ops {
+
+    struct list_head    list;
+
+    /* ops name */
+    char        name[16];
+    /* key for name */
+    u32            key;
+
+    /* init with sk */
+    void (*init)(struct sock *sk);
+
+    /* release with sk */
+    void (*release)(struct sock *sk);
+
+    /* advice for negotiate */
+    int (*negotiate)(struct sock *sk);
+
+    /* info gathering timing */
+    void (*collect_info)(struct sock *sk, int timing);
+
+    /* module owner */
+    struct module *owner;
+};
+#else
+struct smc_sock_negotiator_ops {};
+#endif
+

  #endif    /* _SMC_H */

net/Makefile +0 -0
index 4c4dc53..222916a 100644
@@ -52,6 +52,7 @@ obj-$(CONFIG_TIPC)        += tipc/

  obj-$(CONFIG_NETLABEL)        += netlabel/ obj-$(CONFIG_IUCV)        += iucv/ obj-$(CONFIG_SMC)        += smc/ +obj-$(CONFIG_SMC_BPF)        += smc/smc_negotiator.o > obj-$(CONFIG_RFKILL)        += rfkill/ obj-$(CONFIG_NET_9P)        += 9p/ obj-$(CONFIG_CAIF)        += caif/

net/smc/Kconfig +0 -0
index 1ab3c5a..bdcc9f1 100644
@@ -19,3 +19,14 @@ config SMC_DIAG

        smcss.
          if unsure, say Y.

net/smc/Kconfig config SMC_DIAG · line 24

Quoted 3 lines
net/smc/Kconfig +3 -0
+
+config SMC_BPF
+    bool "SMC: support eBPF" if SMC

so smc_negotiator will always be in the kernel image even af_smc is compiled as a module? If the SMC_BPF needs to support af_smc as a module, proper implementation needs to be added to bpf_struct_ops to support module first. It is work-in-progress.

Quoted 9 lines
Quoted 4 lines

smc_negotiator will not no in the kernel image when af_smc is compiled as a module, it's requires config SMC_BPF also sets to be Y,  while it's default to be N. That's is, even if af_smc is compiled as a module but with no SMC_BPF set, smc_negotiator doesn't exist anywhere.

CONFIG_SMC_BPF could be "y" while CONFIG_SMC is "m", no?

Anyway, there is a build error when CONFIG_SMC is "m" :(

I am curious if users who proactively set CONFIG_SMC_BPF to Y would care about the issue you mentioned, while CONFIG_SMC_BPF defaults to N ?

And I'm really sorry about this compilation error. Last time, I had got some comments about symbol export, so I tried to remove some symbol exports, unfortunately, there are compilation issues when BPF_JIT is set (bpf_struct_ops_get is no exported), sorry for my incomplete testing. I will fix this issue in the new version.

Anyway, if bpf_struct_ops can support module, that would be better, and can greatly reduce the trade-offs I make between modules and built-in. Is there any details can shared on your progress ?

Quoted 99 lines
Quoted 99 lines
Quoted 33 lines
Quoted 33 lines

+    depends on BPF_SYSCALL
+    default n
+    help
+      Supports eBPF to allows user mode participation in SMC's protocol process
+      via ebpf programs. Alternatively, obtain information about the SMC socks
+      through the ebpf program.

net/smc/Kconfig +2 -0
+
+      If unsure, say N.
net/smc/af_smc.c +1 -0
index 50c38b6..7406fd4 100644
@@ -52,6 +52,7 @@
   #include "smc_close.h"
   #include "smc_stats.h"
   #include "smc_tracepoint.h"
+#include "smc_negotiator.h"
   #include "smc_sysctl.h"

    static DEFINE_MUTEX(smc_server_lgr_pending);    /* serialize link group

net/smc/af_smc.c +0 -0
@@ -68,6 +69,119 @@
   static void smc_tcp_listen_work(struct work_struct *);
   static void smc_connect_work(struct work_struct *);

  +#ifdef CONFIG_SMC_BPF

net/smc/af_smc.c +8 -0
+
+/* Check if sock should use smc */
+int smc_sock_should_select_smc(const struct smc_sock *smc)
+{
+    const struct smc_sock_negotiator_ops *ops;
+    int ret;
+
+    rcu_read_lock();
Quoted 9 lines
Quoted 6 lines

+    ops = READ_ONCE(smc->negotiator_ops);
+
+    /* No negotiator_ops supply or no negotiate func set, +     * always pass it.
+     */
+    if (!ops || !ops->negotiate) {

A smc_sock_negotiator_ops without ->negotiate? Is it useful at all to allow the register in the first place?

You are right, this can be avoid before registration. I'll fix it.

Quoted 53 lines
Quoted 40 lines

+        rcu_read_unlock();
+        return SK_PASS;
+    }

net/smc/af_smc.c +7 -0
+
+    ret = ops->negotiate((struct sock *)&smc->sk);
+    rcu_read_unlock();
+    return ret;
+}
+
+void smc_sock_perform_collecting_info(const struct smc_sock *smc, 

int timing)

net/smc/af_smc.c +16 -0
+{
+    const struct smc_sock_negotiator_ops *ops;
+
+    rcu_read_lock();
+    ops = READ_ONCE(smc->negotiator_ops);
+
+    if (!ops || !ops->collect_info) {
+        rcu_read_unlock();
+        return;
+    }
+
+    ops->collect_info((struct sock *)&smc->sk, timing);
+    rcu_read_unlock();
+}
+
+int smc_sock_assign_negotiator_ops(struct smc_sock *smc, const 

char *name)

net/smc/af_smc.c +7 -0
+{
+    struct smc_sock_negotiator_ops *ops;
+    int ret = -EINVAL;
+
+    /* already set */
+    if (READ_ONCE(smc->negotiator_ops))
+        smc_sock_cleanup_negotiator_ops(smc, /* might be still 

referenced */ false);

net/smc/af_smc.c +4 -0
+
+    /* Just for clear negotiator_ops */
+    if (!name || !strlen(name))
+        return 0;

net/smc/af_smc.c line 133

Quoted 10 lines
net/smc/af_smc.c +10 -0
+
+    rcu_read_lock();
+    ops = smc_negotiator_ops_get_by_name(name);
+    if (likely(ops)) {
+        if (unlikely(!bpf_try_module_get(ops, ops->owner))) {
+            ret = -EACCES;
+        } else {
+            WRITE_ONCE(smc->negotiator_ops, ops);
+            /* make sure ops can be seen */
+            smp_wmb();

This rcu_read_lock(), WRITE_ONCE, and smp_wmb() combo looks very suspicious. smc->negotiator_ops is protected by rcu (+refcnt) or lock_sock()?

Quoted 6 lines
Quoted 4 lines

All access to ops is protected by RCU, and there are no lock_sock. WRITE_ONCE() and smp_wmb() do
not participate in any guarantee of the availability of ops, The purpose to using them is just wish the latest values can be read as soon as possible , In fact, even if old value is read, there will be no problem in logic because all updates will do synchronize_rcu() and all access to ops is under in rcu_read_lock().

The explanation is not encouraging. No clear benefit while having this kind of complexity here. Switching tcp congestion ops also does not require this. Some of the new codes is in af_smc but bpf is the primary user. It is not something that I would like to maintain and then need to reason about this unusual pattern a year later. Beside, this negotiator_ops assignment must be done under a lock_sock(). The same probably is true for calling ops->negotiate() where the bpf prog may be looking at the sk and calling bpf_setsockopt.

I got you point, If you feel that those code are complexity and unnecessary, I can remove them of course.

Additionally, smc_sock_assign_negotiator_ops is indeed executed under sock lock,  __smc_setsockopt will lock sock for it. I misunderstood your meaning before.

As for ops ->negotiate(), thanks for this point, but considering performance, I might prohibit calling setsockopt in negotiate().

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am going to stop reviewing here.

Hoping my explanation can answer your questions and still looking forward to your more feedback 😁.

Sorry, based on the review so far (there was some RFC before), it is not something that I want to continue to review and maintain a bpf hook for it. You have to solicit other known community members for review and sponsor this set from now on.

Okay, thank you very much for pointing out some issues and suggestions.

Thanks.