Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 4 | * |
| 5 | * Monitoring SMC transport protocol sockets |
| 6 | * |
| 7 | * Copyright IBM Corp. 2016 |
| 8 | * |
| 9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/sock_diag.h> |
| 17 | #include <linux/inet_diag.h> |
| 18 | #include <linux/smc_diag.h> |
| 19 | #include <net/netlink.h> |
| 20 | #include <net/smc.h> |
| 21 | |
| 22 | #include "smc.h" |
| 23 | #include "smc_core.h" |
| 24 | |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 25 | struct smc_diag_dump_ctx { |
| 26 | int pos[2]; |
| 27 | }; |
| 28 | |
| 29 | static struct smc_diag_dump_ctx *smc_dump_context(struct netlink_callback *cb) |
| 30 | { |
| 31 | return (struct smc_diag_dump_ctx *)cb->ctx; |
| 32 | } |
| 33 | |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 34 | static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk) |
| 35 | { |
| 36 | struct smc_sock *smc = smc_sk(sk); |
| 37 | |
Eric Dumazet | 457fed7 | 2020-02-10 11:36:13 -0800 | [diff] [blame] | 38 | memset(r, 0, sizeof(*r)); |
Karsten Graul | 232dc8e | 2019-02-07 15:56:20 +0100 | [diff] [blame] | 39 | r->diag_family = sk->sk_family; |
Eric Dumazet | 457fed7 | 2020-02-10 11:36:13 -0800 | [diff] [blame] | 40 | sock_diag_save_cookie(sk, r->id.idiag_cookie); |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 41 | if (!smc->clcsock) |
| 42 | return; |
| 43 | r->id.idiag_sport = htons(smc->clcsock->sk->sk_num); |
| 44 | r->id.idiag_dport = smc->clcsock->sk->sk_dport; |
| 45 | r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if; |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 46 | if (sk->sk_protocol == SMCPROTO_SMC) { |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 47 | r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr; |
| 48 | r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr; |
| 49 | #if IS_ENABLED(CONFIG_IPV6) |
| 50 | } else if (sk->sk_protocol == SMCPROTO_SMC6) { |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 51 | memcpy(&r->id.idiag_src, &smc->clcsock->sk->sk_v6_rcv_saddr, |
| 52 | sizeof(smc->clcsock->sk->sk_v6_rcv_saddr)); |
| 53 | memcpy(&r->id.idiag_dst, &smc->clcsock->sk->sk_v6_daddr, |
| 54 | sizeof(smc->clcsock->sk->sk_v6_daddr)); |
| 55 | #endif |
| 56 | } |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int smc_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, |
| 60 | struct smc_diag_msg *r, |
| 61 | struct user_namespace *user_ns) |
| 62 | { |
| 63 | if (nla_put_u8(skb, SMC_DIAG_SHUTDOWN, sk->sk_shutdown)) |
| 64 | return 1; |
| 65 | |
| 66 | r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); |
| 67 | r->diag_inode = sock_i_ino(sk); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int __smc_diag_dump(struct sock *sk, struct sk_buff *skb, |
| 72 | struct netlink_callback *cb, |
| 73 | const struct smc_diag_req *req, |
| 74 | struct nlattr *bc) |
| 75 | { |
| 76 | struct smc_sock *smc = smc_sk(sk); |
Karsten Graul | 603cc14 | 2018-07-25 16:35:32 +0200 | [diff] [blame] | 77 | struct smc_diag_fallback fallback; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 78 | struct user_namespace *user_ns; |
| 79 | struct smc_diag_msg *r; |
| 80 | struct nlmsghdr *nlh; |
| 81 | |
| 82 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 83 | cb->nlh->nlmsg_type, sizeof(*r), NLM_F_MULTI); |
| 84 | if (!nlh) |
| 85 | return -EMSGSIZE; |
| 86 | |
| 87 | r = nlmsg_data(nlh); |
| 88 | smc_diag_msg_common_fill(r, sk); |
| 89 | r->diag_state = sk->sk_state; |
Karsten Graul | c601171 | 2018-07-23 13:53:08 +0200 | [diff] [blame] | 90 | if (smc->use_fallback) |
| 91 | r->diag_mode = SMC_DIAG_MODE_FALLBACK_TCP; |
Wen Gu | ea89c6c | 2022-01-13 16:36:41 +0800 | [diff] [blame] | 92 | else if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd) |
Karsten Graul | c601171 | 2018-07-23 13:53:08 +0200 | [diff] [blame] | 93 | r->diag_mode = SMC_DIAG_MODE_SMCD; |
| 94 | else |
| 95 | r->diag_mode = SMC_DIAG_MODE_SMCR; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 96 | user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk); |
| 97 | if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns)) |
| 98 | goto errout; |
| 99 | |
Karsten Graul | 603cc14 | 2018-07-25 16:35:32 +0200 | [diff] [blame] | 100 | fallback.reason = smc->fallback_rsn; |
| 101 | fallback.peer_diagnosis = smc->peer_diagnosis; |
| 102 | if (nla_put(skb, SMC_DIAG_FALLBACK, sizeof(fallback), &fallback) < 0) |
| 103 | goto errout; |
| 104 | |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 105 | if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) && |
| 106 | smc->conn.alert_token_local) { |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 107 | struct smc_connection *conn = &smc->conn; |
| 108 | struct smc_diag_conninfo cinfo = { |
| 109 | .token = conn->alert_token_local, |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 110 | .sndbuf_size = conn->sndbuf_desc ? |
| 111 | conn->sndbuf_desc->len : 0, |
| 112 | .rmbe_size = conn->rmb_desc ? conn->rmb_desc->len : 0, |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 113 | .peer_rmbe_size = conn->peer_rmbe_size, |
| 114 | |
| 115 | .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap, |
| 116 | .rx_prod.count = conn->local_rx_ctrl.prod.count, |
| 117 | .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap, |
| 118 | .rx_cons.count = conn->local_rx_ctrl.cons.count, |
| 119 | |
| 120 | .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap, |
| 121 | .tx_prod.count = conn->local_tx_ctrl.prod.count, |
| 122 | .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap, |
| 123 | .tx_cons.count = conn->local_tx_ctrl.cons.count, |
| 124 | |
| 125 | .tx_prod_flags = |
| 126 | *(u8 *)&conn->local_tx_ctrl.prod_flags, |
| 127 | .tx_conn_state_flags = |
| 128 | *(u8 *)&conn->local_tx_ctrl.conn_state_flags, |
| 129 | .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags, |
| 130 | .rx_conn_state_flags = |
| 131 | *(u8 *)&conn->local_rx_ctrl.conn_state_flags, |
| 132 | |
| 133 | .tx_prep.wrap = conn->tx_curs_prep.wrap, |
| 134 | .tx_prep.count = conn->tx_curs_prep.count, |
| 135 | .tx_sent.wrap = conn->tx_curs_sent.wrap, |
| 136 | .tx_sent.count = conn->tx_curs_sent.count, |
| 137 | .tx_fin.wrap = conn->tx_curs_fin.wrap, |
| 138 | .tx_fin.count = conn->tx_curs_fin.count, |
| 139 | }; |
| 140 | |
| 141 | if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0) |
| 142 | goto errout; |
| 143 | } |
| 144 | |
Wen Gu | ea89c6c | 2022-01-13 16:36:41 +0800 | [diff] [blame] | 145 | if (smc_conn_lgr_valid(&smc->conn) && !smc->conn.lgr->is_smcd && |
Hans Wippel | c6ba7c9 | 2018-06-28 19:05:07 +0200 | [diff] [blame] | 146 | (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) && |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 147 | !list_empty(&smc->conn.lgr->list)) { |
Tony Lu | 79d39fc | 2021-12-28 21:06:10 +0800 | [diff] [blame] | 148 | struct smc_link *link = smc->conn.lnk; |
| 149 | struct net *net = read_pnet(&link->smcibdev->ibdev->coredev.rdma_net); |
| 150 | |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 151 | struct smc_diag_lgrinfo linfo = { |
| 152 | .role = smc->conn.lgr->role, |
Tony Lu | 79d39fc | 2021-12-28 21:06:10 +0800 | [diff] [blame] | 153 | .lnk[0].ibport = link->ibport, |
| 154 | .lnk[0].link_id = link->link_id, |
| 155 | .lnk[0].net_cookie = net->net_cookie, |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | memcpy(linfo.lnk[0].ibname, |
| 159 | smc->conn.lgr->lnk[0].smcibdev->ibdev->name, |
Tony Lu | 79d39fc | 2021-12-28 21:06:10 +0800 | [diff] [blame] | 160 | sizeof(link->smcibdev->ibdev->name)); |
| 161 | smc_gid_be16_convert(linfo.lnk[0].gid, link->gid); |
| 162 | smc_gid_be16_convert(linfo.lnk[0].peer_gid, link->peer_gid); |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 163 | |
| 164 | if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0) |
| 165 | goto errout; |
| 166 | } |
Wen Gu | ea89c6c | 2022-01-13 16:36:41 +0800 | [diff] [blame] | 167 | if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd && |
Hans Wippel | 4b1b7d3b | 2018-06-28 19:05:12 +0200 | [diff] [blame] | 168 | (req->diag_ext & (1 << (SMC_DIAG_DMBINFO - 1))) && |
| 169 | !list_empty(&smc->conn.lgr->list)) { |
| 170 | struct smc_connection *conn = &smc->conn; |
Peilin Ye | ce51f63 | 2020-08-20 16:30:52 +0200 | [diff] [blame] | 171 | struct smcd_diag_dmbinfo dinfo; |
| 172 | |
| 173 | memset(&dinfo, 0, sizeof(dinfo)); |
| 174 | |
| 175 | dinfo.linkid = *((u32 *)conn->lgr->id); |
| 176 | dinfo.peer_gid = conn->lgr->peer_gid; |
| 177 | dinfo.my_gid = conn->lgr->smcd->local_gid; |
| 178 | dinfo.token = conn->rmb_desc->token; |
| 179 | dinfo.peer_token = conn->peer_token; |
Hans Wippel | 4b1b7d3b | 2018-06-28 19:05:12 +0200 | [diff] [blame] | 180 | |
| 181 | if (nla_put(skb, SMC_DIAG_DMBINFO, sizeof(dinfo), &dinfo) < 0) |
| 182 | goto errout; |
| 183 | } |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 184 | |
| 185 | nlmsg_end(skb, nlh); |
| 186 | return 0; |
| 187 | |
| 188 | errout: |
| 189 | nlmsg_cancel(skb, nlh); |
| 190 | return -EMSGSIZE; |
| 191 | } |
| 192 | |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 193 | static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb, |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 194 | struct netlink_callback *cb, int p_type) |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 195 | { |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 196 | struct smc_diag_dump_ctx *cb_ctx = smc_dump_context(cb); |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 197 | struct net *net = sock_net(skb->sk); |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 198 | int snum = cb_ctx->pos[p_type]; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 199 | struct nlattr *bc = NULL; |
| 200 | struct hlist_head *head; |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 201 | int rc = 0, num = 0; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 202 | struct sock *sk; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 203 | |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 204 | read_lock(&prot->h.smc_hash->lock); |
| 205 | head = &prot->h.smc_hash->ht; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 206 | if (hlist_empty(head)) |
| 207 | goto out; |
| 208 | |
| 209 | sk_for_each(sk, head) { |
| 210 | if (!net_eq(sock_net(sk), net)) |
| 211 | continue; |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 212 | if (num < snum) |
| 213 | goto next; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 214 | rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc); |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 215 | if (rc < 0) |
| 216 | goto out; |
| 217 | next: |
| 218 | num++; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | out: |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 222 | read_unlock(&prot->h.smc_hash->lock); |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 223 | cb_ctx->pos[p_type] = num; |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 228 | { |
| 229 | int rc = 0; |
| 230 | |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 231 | rc = smc_diag_dump_proto(&smc_proto, skb, cb, SMCPROTO_SMC); |
Karsten Graul | ed75986 | 2018-05-02 16:56:45 +0200 | [diff] [blame] | 232 | if (!rc) |
Guvenc Gulce | 8418cb4 | 2020-09-10 18:48:28 +0200 | [diff] [blame] | 233 | smc_diag_dump_proto(&smc_proto6, skb, cb, SMCPROTO_SMC6); |
| 234 | return skb->len; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) |
| 238 | { |
| 239 | struct net *net = sock_net(skb->sk); |
| 240 | |
| 241 | if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && |
| 242 | h->nlmsg_flags & NLM_F_DUMP) { |
| 243 | { |
| 244 | struct netlink_dump_control c = { |
| 245 | .dump = smc_diag_dump, |
| 246 | .min_dump_alloc = SKB_WITH_OVERHEAD(32768), |
| 247 | }; |
| 248 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); |
| 249 | } |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static const struct sock_diag_handler smc_diag_handler = { |
| 255 | .family = AF_SMC, |
| 256 | .dump = smc_diag_handler_dump, |
| 257 | }; |
| 258 | |
| 259 | static int __init smc_diag_init(void) |
| 260 | { |
| 261 | return sock_diag_register(&smc_diag_handler); |
| 262 | } |
| 263 | |
| 264 | static void __exit smc_diag_exit(void) |
| 265 | { |
| 266 | sock_diag_unregister(&smc_diag_handler); |
| 267 | } |
| 268 | |
| 269 | module_init(smc_diag_init); |
| 270 | module_exit(smc_diag_exit); |
| 271 | MODULE_LICENSE("GPL"); |
| 272 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */); |