From: "D. Wythe" <alibuda@xxxxxxxxxxxxxxxxx> 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. Signed-off-by: D. Wythe <alibuda@xxxxxxxxxxxxxxxxx> --- include/uapi/linux/smc.h | 1 + net/smc/af_smc.c | 135 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 102 insertions(+), 34 deletions(-) diff --git a/include/uapi/linux/smc.h b/include/uapi/linux/smc.h index bb4dacc..1887ed5 100644 --- a/include/uapi/linux/smc.h +++ b/include/uapi/linux/smc.h @@ -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 */ diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index 8bfb918..efc9b81 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -2932,48 +2932,41 @@ static int smc_shutdown(struct socket *sock, int how) return rc ? rc : rc1; } -static int __smc_getsockopt(struct socket *sock, int level, int optname, - char __user *optval, int __user *optlen) +/* set smc negotoiatior by name */ +static int smc_setsockopt_negotiator(struct sock *sk, sockptr_t optval, + unsigned int optlen) { - struct smc_sock *smc; - int val, len; - - smc = smc_sk(sock->sk); - - if (get_user(len, optlen)) - return -EFAULT; - - len = min_t(int, len, sizeof(int)); +#ifdef CONFIG_SMC_BPF + char name[SMC_NEGOTIATOR_NAME_MAX]; + struct smc_sock *smc = smc_sk(sk); + int val, rc; - if (len < 0) + if (optlen < 1) return -EINVAL; - switch (optname) { - case SMC_LIMIT_HS: - val = smc->limit_smc_hs; - break; - default: - return -EOPNOTSUPP; - } - - if (put_user(len, optlen)) - return -EFAULT; - if (copy_to_user(optval, &val, len)) + val = strncpy_from_sockptr(name, optval, + min_t(long, SMC_NEGOTIATOR_NAME_MAX - 1, optlen)); + if (val < 0) return -EFAULT; - return 0; + /* typical c str */ + name[val] = 0; + + sockopt_lock_sock(sk); + rc = smc_sock_assign_negotiator_ops(smc, name); + sockopt_release_sock(sk); + return rc; +#else + return -EOPNOTSUPP; +#endif } -static int __smc_setsockopt(struct socket *sock, int level, int optname, +static int __smc_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, unsigned int optlen) { - struct sock *sk = sock->sk; - struct smc_sock *smc; + struct smc_sock *smc = smc_sk(sk); int val, rc; - smc = smc_sk(sk); - - lock_sock(sk); switch (optname) { case SMC_LIMIT_HS: if (optlen < sizeof(int)) { @@ -2984,15 +2977,17 @@ static int __smc_setsockopt(struct socket *sock, int level, int optname, rc = -EFAULT; break; } - + sockopt_lock_sock(sk); smc->limit_smc_hs = !!val; + sockopt_release_sock(sk); rc = 0; break; + case SMC_NEGOTIATOR: + return smc_setsockopt_negotiator(sk, optval, optlen); default: rc = -EOPNOTSUPP; break; } - release_sock(sk); return rc; } @@ -3007,7 +3002,7 @@ static int smc_setsockopt(struct socket *sock, int level, int optname, if (level == SOL_TCP && optname == TCP_ULP) return -EOPNOTSUPP; else if (level == SOL_SMC) - return __smc_setsockopt(sock, level, optname, optval, optlen); + return __smc_setsockopt(sk, level, optname, optval, optlen); smc = smc_sk(sk); @@ -3084,6 +3079,77 @@ static int smc_setsockopt(struct socket *sock, int level, int optname, return rc; } +/* get current negotoiatior sock used */ +static int smc_getsockopt_negotiator(struct sock *sk, sockptr_t optval, + sockptr_t optlen) +{ +#ifdef CONFIG_SMC_BPF + const struct smc_sock_negotiator_ops *ops; + struct smc_sock *smc = smc_sk(sk); + int len; + + if (copy_from_sockptr(&len, optlen, sizeof(int))) + return -EFAULT; + + len = min_t(unsigned int, len, sizeof(int)); + + if (len < 0) + return -EINVAL; + + rcu_read_lock(); + ops = READ_ONCE(smc->negotiator_ops); + if (ops) { + len = min_t(unsigned int, len, SMC_NEGOTIATOR_NAME_MAX); + if (copy_to_sockptr(optval, ops->name, len)) { + rcu_read_unlock(); + return -EFAULT; + } + } else { + len = 0; + } + rcu_read_unlock(); + + if (copy_to_sockptr(optlen, &len, sizeof(int))) + return -EFAULT; + + return 0; +#else + return -EOPNOTSUPP; +#endif +} + +static int __smc_getsockopt(struct sock *sk, int level, int optname, + sockptr_t optval, sockptr_t optlen) +{ + struct smc_sock *smc = smc_sk(sk); + int val, len; + + if (copy_from_sockptr(&len, optlen, sizeof(int))) + return -EFAULT; + + len = min_t(unsigned int, len, sizeof(int)); + + if (len < 0) + return -EINVAL; + + switch (optname) { + case SMC_LIMIT_HS: + val = smc->limit_smc_hs; + break; + case SMC_NEGOTIATOR: + return smc_getsockopt_negotiator(sk, optval, optlen); + default: + return -EOPNOTSUPP; + } + + if (copy_to_sockptr(optval, &val, len)) + return -EFAULT; + if (copy_to_sockptr(optlen, &len, sizeof(int))) + return -EFAULT; + + return 0; +} + static int smc_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) { @@ -3091,7 +3157,8 @@ static int smc_getsockopt(struct socket *sock, int level, int optname, int rc; if (level == SOL_SMC) - return __smc_getsockopt(sock, level, optname, optval, optlen); + return __smc_getsockopt(sock->sk, level, optname, + USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); smc = smc_sk(sock->sk); mutex_lock(&smc->clcsock_release_lock); -- 1.8.3.1