Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Shared Memory Communications over RDMA (SMC-R) and RoCE
0004  *
0005  * Monitoring SMC transport protocol sockets
0006  *
0007  * Copyright IBM Corp. 2016
0008  *
0009  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/types.h>
0015 #include <linux/init.h>
0016 #include <linux/sock_diag.h>
0017 #include <linux/inet_diag.h>
0018 #include <linux/smc_diag.h>
0019 #include <net/netlink.h>
0020 #include <net/smc.h>
0021 
0022 #include "smc.h"
0023 #include "smc_core.h"
0024 
0025 struct smc_diag_dump_ctx {
0026     int pos[2];
0027 };
0028 
0029 static struct smc_diag_dump_ctx *smc_dump_context(struct netlink_callback *cb)
0030 {
0031     return (struct smc_diag_dump_ctx *)cb->ctx;
0032 }
0033 
0034 static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)
0035 {
0036     struct smc_sock *smc = smc_sk(sk);
0037 
0038     memset(r, 0, sizeof(*r));
0039     r->diag_family = sk->sk_family;
0040     sock_diag_save_cookie(sk, r->id.idiag_cookie);
0041     if (!smc->clcsock)
0042         return;
0043     r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);
0044     r->id.idiag_dport = smc->clcsock->sk->sk_dport;
0045     r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if;
0046     if (sk->sk_protocol == SMCPROTO_SMC) {
0047         r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr;
0048         r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr;
0049 #if IS_ENABLED(CONFIG_IPV6)
0050     } else if (sk->sk_protocol == SMCPROTO_SMC6) {
0051         memcpy(&r->id.idiag_src, &smc->clcsock->sk->sk_v6_rcv_saddr,
0052                sizeof(smc->clcsock->sk->sk_v6_rcv_saddr));
0053         memcpy(&r->id.idiag_dst, &smc->clcsock->sk->sk_v6_daddr,
0054                sizeof(smc->clcsock->sk->sk_v6_daddr));
0055 #endif
0056     }
0057 }
0058 
0059 static int smc_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
0060                    struct smc_diag_msg *r,
0061                    struct user_namespace *user_ns)
0062 {
0063     if (nla_put_u8(skb, SMC_DIAG_SHUTDOWN, sk->sk_shutdown))
0064         return 1;
0065 
0066     r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
0067     r->diag_inode = sock_i_ino(sk);
0068     return 0;
0069 }
0070 
0071 static int __smc_diag_dump(struct sock *sk, struct sk_buff *skb,
0072                struct netlink_callback *cb,
0073                const struct smc_diag_req *req,
0074                struct nlattr *bc)
0075 {
0076     struct smc_sock *smc = smc_sk(sk);
0077     struct smc_diag_fallback fallback;
0078     struct user_namespace *user_ns;
0079     struct smc_diag_msg *r;
0080     struct nlmsghdr *nlh;
0081 
0082     nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
0083             cb->nlh->nlmsg_type, sizeof(*r), NLM_F_MULTI);
0084     if (!nlh)
0085         return -EMSGSIZE;
0086 
0087     r = nlmsg_data(nlh);
0088     smc_diag_msg_common_fill(r, sk);
0089     r->diag_state = sk->sk_state;
0090     if (smc->use_fallback)
0091         r->diag_mode = SMC_DIAG_MODE_FALLBACK_TCP;
0092     else if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd)
0093         r->diag_mode = SMC_DIAG_MODE_SMCD;
0094     else
0095         r->diag_mode = SMC_DIAG_MODE_SMCR;
0096     user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk);
0097     if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns))
0098         goto errout;
0099 
0100     fallback.reason = smc->fallback_rsn;
0101     fallback.peer_diagnosis = smc->peer_diagnosis;
0102     if (nla_put(skb, SMC_DIAG_FALLBACK, sizeof(fallback), &fallback) < 0)
0103         goto errout;
0104 
0105     if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) &&
0106         smc->conn.alert_token_local) {
0107         struct smc_connection *conn = &smc->conn;
0108         struct smc_diag_conninfo cinfo = {
0109             .token = conn->alert_token_local,
0110             .sndbuf_size = conn->sndbuf_desc ?
0111                 conn->sndbuf_desc->len : 0,
0112             .rmbe_size = conn->rmb_desc ? conn->rmb_desc->len : 0,
0113             .peer_rmbe_size = conn->peer_rmbe_size,
0114 
0115             .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap,
0116             .rx_prod.count = conn->local_rx_ctrl.prod.count,
0117             .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap,
0118             .rx_cons.count = conn->local_rx_ctrl.cons.count,
0119 
0120             .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap,
0121             .tx_prod.count = conn->local_tx_ctrl.prod.count,
0122             .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap,
0123             .tx_cons.count = conn->local_tx_ctrl.cons.count,
0124 
0125             .tx_prod_flags =
0126                 *(u8 *)&conn->local_tx_ctrl.prod_flags,
0127             .tx_conn_state_flags =
0128                 *(u8 *)&conn->local_tx_ctrl.conn_state_flags,
0129             .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags,
0130             .rx_conn_state_flags =
0131                 *(u8 *)&conn->local_rx_ctrl.conn_state_flags,
0132 
0133             .tx_prep.wrap = conn->tx_curs_prep.wrap,
0134             .tx_prep.count = conn->tx_curs_prep.count,
0135             .tx_sent.wrap = conn->tx_curs_sent.wrap,
0136             .tx_sent.count = conn->tx_curs_sent.count,
0137             .tx_fin.wrap = conn->tx_curs_fin.wrap,
0138             .tx_fin.count = conn->tx_curs_fin.count,
0139         };
0140 
0141         if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0)
0142             goto errout;
0143     }
0144 
0145     if (smc_conn_lgr_valid(&smc->conn) && !smc->conn.lgr->is_smcd &&
0146         (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) &&
0147         !list_empty(&smc->conn.lgr->list)) {
0148         struct smc_link *link = smc->conn.lnk;
0149 
0150         struct smc_diag_lgrinfo linfo = {
0151             .role = smc->conn.lgr->role,
0152             .lnk[0].ibport = link->ibport,
0153             .lnk[0].link_id = link->link_id,
0154         };
0155 
0156         memcpy(linfo.lnk[0].ibname,
0157                smc->conn.lgr->lnk[0].smcibdev->ibdev->name,
0158                sizeof(link->smcibdev->ibdev->name));
0159         smc_gid_be16_convert(linfo.lnk[0].gid, link->gid);
0160         smc_gid_be16_convert(linfo.lnk[0].peer_gid, link->peer_gid);
0161 
0162         if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0)
0163             goto errout;
0164     }
0165     if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd &&
0166         (req->diag_ext & (1 << (SMC_DIAG_DMBINFO - 1))) &&
0167         !list_empty(&smc->conn.lgr->list)) {
0168         struct smc_connection *conn = &smc->conn;
0169         struct smcd_diag_dmbinfo dinfo;
0170 
0171         memset(&dinfo, 0, sizeof(dinfo));
0172 
0173         dinfo.linkid = *((u32 *)conn->lgr->id);
0174         dinfo.peer_gid = conn->lgr->peer_gid;
0175         dinfo.my_gid = conn->lgr->smcd->local_gid;
0176         dinfo.token = conn->rmb_desc->token;
0177         dinfo.peer_token = conn->peer_token;
0178 
0179         if (nla_put(skb, SMC_DIAG_DMBINFO, sizeof(dinfo), &dinfo) < 0)
0180             goto errout;
0181     }
0182 
0183     nlmsg_end(skb, nlh);
0184     return 0;
0185 
0186 errout:
0187     nlmsg_cancel(skb, nlh);
0188     return -EMSGSIZE;
0189 }
0190 
0191 static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb,
0192                    struct netlink_callback *cb, int p_type)
0193 {
0194     struct smc_diag_dump_ctx *cb_ctx = smc_dump_context(cb);
0195     struct net *net = sock_net(skb->sk);
0196     int snum = cb_ctx->pos[p_type];
0197     struct nlattr *bc = NULL;
0198     struct hlist_head *head;
0199     int rc = 0, num = 0;
0200     struct sock *sk;
0201 
0202     read_lock(&prot->h.smc_hash->lock);
0203     head = &prot->h.smc_hash->ht;
0204     if (hlist_empty(head))
0205         goto out;
0206 
0207     sk_for_each(sk, head) {
0208         if (!net_eq(sock_net(sk), net))
0209             continue;
0210         if (num < snum)
0211             goto next;
0212         rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc);
0213         if (rc < 0)
0214             goto out;
0215 next:
0216         num++;
0217     }
0218 
0219 out:
0220     read_unlock(&prot->h.smc_hash->lock);
0221     cb_ctx->pos[p_type] = num;
0222     return rc;
0223 }
0224 
0225 static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
0226 {
0227     int rc = 0;
0228 
0229     rc = smc_diag_dump_proto(&smc_proto, skb, cb, SMCPROTO_SMC);
0230     if (!rc)
0231         smc_diag_dump_proto(&smc_proto6, skb, cb, SMCPROTO_SMC6);
0232     return skb->len;
0233 }
0234 
0235 static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
0236 {
0237     struct net *net = sock_net(skb->sk);
0238 
0239     if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
0240         h->nlmsg_flags & NLM_F_DUMP) {
0241         {
0242             struct netlink_dump_control c = {
0243                 .dump = smc_diag_dump,
0244                 .min_dump_alloc = SKB_WITH_OVERHEAD(32768),
0245             };
0246             return netlink_dump_start(net->diag_nlsk, skb, h, &c);
0247         }
0248     }
0249     return 0;
0250 }
0251 
0252 static const struct sock_diag_handler smc_diag_handler = {
0253     .family = AF_SMC,
0254     .dump = smc_diag_handler_dump,
0255 };
0256 
0257 static int __init smc_diag_init(void)
0258 {
0259     return sock_diag_register(&smc_diag_handler);
0260 }
0261 
0262 static void __exit smc_diag_exit(void)
0263 {
0264     sock_diag_unregister(&smc_diag_handler);
0265 }
0266 
0267 module_init(smc_diag_init);
0268 module_exit(smc_diag_exit);
0269 MODULE_LICENSE("GPL");
0270 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */);
0271 MODULE_ALIAS_GENL_FAMILY(SMCR_GENL_FAMILY_NAME);