Skip to content

Commit

Permalink
net/smc: Introduce BPF injection capability for SMC
Browse files Browse the repository at this point in the history
This PATCH attempts to introduce BPF injection capability for SMC.
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 PATCH implement injection capability for SMC via struct_ops.
In that way, we can add new injection scenarios in the future.

Signed-off-by: D. Wythe <alibuda@linux.alibaba.com>
  • Loading branch information
D. Wythe authored and intel-lab-lkp committed Feb 21, 2023
1 parent f2b6cfd commit e2b31ae
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 221 deletions.
15 changes: 15 additions & 0 deletions include/linux/btf_ids.h
Expand Up @@ -264,6 +264,21 @@ BTF_TRACING_TYPE_xxx
MAX_BTF_TRACING_TYPE,
};

#if IS_ENABLED(CONFIG_SMC)
#define BTF_SMC_TYPE_xxx \
BTF_SMC_TYPE(BTF_SMC_TYPE_SOCK, smc_sock) \
BTF_SMC_TYPE(BTF_SMC_TYPE_CONNECTION, smc_connection) \
BTF_SMC_TYPE(BTF_SMC_TYPE_HOST_CURSOR, smc_host_cursor)

enum {
#define BTF_SMC_TYPE(name, type) name,
BTF_SMC_TYPE_xxx
#undef BTF_SMC_TYPE
MAX_BTF_SMC_TYPE,
};
extern u32 btf_smc_ids[];
#endif

extern u32 btf_tracing_ids[];
extern u32 bpf_cgroup_btf_id[];
extern u32 bpf_local_storage_map_btf_id[];
Expand Down
254 changes: 254 additions & 0 deletions include/net/smc.h
Expand Up @@ -11,13 +11,16 @@
#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/bpf.h>
#include "linux/ism.h"

struct sock;
struct smc_diag_conninfo;

#define SMC_MAX_PNETID_LEN 16 /* Max. length of PNET id */

Expand Down Expand Up @@ -90,4 +93,255 @@ struct smcd_dev {
u8 going_away : 1;
};

#if IS_ENABLED(CONFIG_SMC)

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

enum smc_urg_state {
SMC_URG_VALID = 1, /* data present */
SMC_URG_NOTYET = 2, /* data pending */
SMC_URG_READ = 3, /* data was already read */
};

/* 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 ATOMIC64_INIT
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);

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

#define SMC_SOCK_CLOSED_TIMING (0)

/* BPF struct ops for smc protocol negotiator */
struct smc_sock_negotiator_ops {
/* ret for negotiate */
int (*negotiate)(struct smc_sock *sk);

/* info gathering timing */
void (*collect_info)(struct smc_sock *sk, int timing);
};

/* Query if current sock should go with SMC protocol
* SK_PASS for yes, otherwise for no.
*/
int smc_sock_should_select_smc(const struct smc_sock *smc);

/* At some specific points in time,
* let negotiator can perform info gathering
* on target sock.
*/
void smc_sock_perform_collecting_info(const struct smc_sock *smc, int timing);

#else
struct smc_sock {};
struct smc_connection {};
struct smc_sock_negotiator_ops {};
union smc_host_cursor {};
#endif /* CONFIG_SMC */

#endif /* _SMC_H */
4 changes: 4 additions & 0 deletions kernel/bpf/bpf_struct_ops_types.h
Expand Up @@ -9,4 +9,8 @@ BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
#include <net/tcp.h>
BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
#endif
#if IS_ENABLED(CONFIG_SMC)
#include <net/smc.h>
BPF_STRUCT_OPS_TYPE(smc_sock_negotiator_ops)
#endif
#endif
5 changes: 5 additions & 0 deletions net/Makefile
Expand Up @@ -52,6 +52,11 @@ obj-$(CONFIG_TIPC) += tipc/
obj-$(CONFIG_NETLABEL) += netlabel/
obj-$(CONFIG_IUCV) += iucv/
obj-$(CONFIG_SMC) += smc/
ifneq ($(CONFIG_SMC),)
ifeq ($(CONFIG_BPF_SYSCALL),y)
obj-y += smc/bpf_smc_struct_ops.o
endif
endif
obj-$(CONFIG_RFKILL) += rfkill/
obj-$(CONFIG_NET_9P) += 9p/
obj-$(CONFIG_CAIF) += caif/
Expand Down
10 changes: 9 additions & 1 deletion net/smc/af_smc.c
Expand Up @@ -166,6 +166,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;
}

Expand Down Expand Up @@ -320,6 +323,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 */
Expand Down Expand Up @@ -1627,7 +1633,9 @@ 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;
tcp_sk(smc->clcsock->sk)->syn_smc = (smc_sock_should_select_smc(smc) == SK_PASS) ?
1 : 0;

if (smc->connect_nonblock) {
rc = -EALREADY;
goto out;
Expand Down

0 comments on commit e2b31ae

Please sign in to comment.