Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
f16a7dd5 UB |
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" | |
b40584d1 | 24 | #include "smc_ism.h" |
f16a7dd5 | 25 | |
8418cb40 GG |
26 | struct smc_diag_dump_ctx { |
27 | int pos[2]; | |
28 | }; | |
29 | ||
30 | static struct smc_diag_dump_ctx *smc_dump_context(struct netlink_callback *cb) | |
31 | { | |
32 | return (struct smc_diag_dump_ctx *)cb->ctx; | |
33 | } | |
34 | ||
f16a7dd5 UB |
35 | static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk) |
36 | { | |
37 | struct smc_sock *smc = smc_sk(sk); | |
38 | ||
457fed77 | 39 | memset(r, 0, sizeof(*r)); |
232dc8ef | 40 | r->diag_family = sk->sk_family; |
457fed77 | 41 | sock_diag_save_cookie(sk, r->id.idiag_cookie); |
f16a7dd5 UB |
42 | if (!smc->clcsock) |
43 | return; | |
44 | r->id.idiag_sport = htons(smc->clcsock->sk->sk_num); | |
45 | r->id.idiag_dport = smc->clcsock->sk->sk_dport; | |
46 | r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if; | |
ed75986f | 47 | if (sk->sk_protocol == SMCPROTO_SMC) { |
ed75986f KG |
48 | r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr; |
49 | r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr; | |
50 | #if IS_ENABLED(CONFIG_IPV6) | |
51 | } else if (sk->sk_protocol == SMCPROTO_SMC6) { | |
ed75986f KG |
52 | memcpy(&r->id.idiag_src, &smc->clcsock->sk->sk_v6_rcv_saddr, |
53 | sizeof(smc->clcsock->sk->sk_v6_rcv_saddr)); | |
54 | memcpy(&r->id.idiag_dst, &smc->clcsock->sk->sk_v6_daddr, | |
55 | sizeof(smc->clcsock->sk->sk_v6_daddr)); | |
56 | #endif | |
57 | } | |
f16a7dd5 UB |
58 | } |
59 | ||
60 | static int smc_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, | |
61 | struct smc_diag_msg *r, | |
62 | struct user_namespace *user_ns) | |
63 | { | |
64 | if (nla_put_u8(skb, SMC_DIAG_SHUTDOWN, sk->sk_shutdown)) | |
65 | return 1; | |
66 | ||
67 | r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); | |
68 | r->diag_inode = sock_i_ino(sk); | |
69 | return 0; | |
70 | } | |
71 | ||
72 | static int __smc_diag_dump(struct sock *sk, struct sk_buff *skb, | |
73 | struct netlink_callback *cb, | |
74 | const struct smc_diag_req *req, | |
75 | struct nlattr *bc) | |
76 | { | |
77 | struct smc_sock *smc = smc_sk(sk); | |
603cc149 | 78 | struct smc_diag_fallback fallback; |
f16a7dd5 UB |
79 | struct user_namespace *user_ns; |
80 | struct smc_diag_msg *r; | |
81 | struct nlmsghdr *nlh; | |
82 | ||
83 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, | |
84 | cb->nlh->nlmsg_type, sizeof(*r), NLM_F_MULTI); | |
85 | if (!nlh) | |
86 | return -EMSGSIZE; | |
87 | ||
88 | r = nlmsg_data(nlh); | |
89 | smc_diag_msg_common_fill(r, sk); | |
90 | r->diag_state = sk->sk_state; | |
c601171d KG |
91 | if (smc->use_fallback) |
92 | r->diag_mode = SMC_DIAG_MODE_FALLBACK_TCP; | |
ea89c6c0 | 93 | else if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd) |
c601171d KG |
94 | r->diag_mode = SMC_DIAG_MODE_SMCD; |
95 | else | |
96 | r->diag_mode = SMC_DIAG_MODE_SMCR; | |
f16a7dd5 UB |
97 | user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk); |
98 | if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns)) | |
99 | goto errout; | |
100 | ||
603cc149 KG |
101 | fallback.reason = smc->fallback_rsn; |
102 | fallback.peer_diagnosis = smc->peer_diagnosis; | |
103 | if (nla_put(skb, SMC_DIAG_FALLBACK, sizeof(fallback), &fallback) < 0) | |
104 | goto errout; | |
105 | ||
1a0a04c7 UB |
106 | if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) && |
107 | smc->conn.alert_token_local) { | |
f16a7dd5 UB |
108 | struct smc_connection *conn = &smc->conn; |
109 | struct smc_diag_conninfo cinfo = { | |
110 | .token = conn->alert_token_local, | |
69cb7dc0 HW |
111 | .sndbuf_size = conn->sndbuf_desc ? |
112 | conn->sndbuf_desc->len : 0, | |
113 | .rmbe_size = conn->rmb_desc ? conn->rmb_desc->len : 0, | |
f16a7dd5 UB |
114 | .peer_rmbe_size = conn->peer_rmbe_size, |
115 | ||
116 | .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap, | |
117 | .rx_prod.count = conn->local_rx_ctrl.prod.count, | |
118 | .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap, | |
119 | .rx_cons.count = conn->local_rx_ctrl.cons.count, | |
120 | ||
121 | .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap, | |
122 | .tx_prod.count = conn->local_tx_ctrl.prod.count, | |
123 | .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap, | |
124 | .tx_cons.count = conn->local_tx_ctrl.cons.count, | |
125 | ||
126 | .tx_prod_flags = | |
127 | *(u8 *)&conn->local_tx_ctrl.prod_flags, | |
128 | .tx_conn_state_flags = | |
129 | *(u8 *)&conn->local_tx_ctrl.conn_state_flags, | |
130 | .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags, | |
131 | .rx_conn_state_flags = | |
132 | *(u8 *)&conn->local_rx_ctrl.conn_state_flags, | |
133 | ||
134 | .tx_prep.wrap = conn->tx_curs_prep.wrap, | |
135 | .tx_prep.count = conn->tx_curs_prep.count, | |
136 | .tx_sent.wrap = conn->tx_curs_sent.wrap, | |
137 | .tx_sent.count = conn->tx_curs_sent.count, | |
138 | .tx_fin.wrap = conn->tx_curs_fin.wrap, | |
139 | .tx_fin.count = conn->tx_curs_fin.count, | |
140 | }; | |
141 | ||
142 | if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0) | |
143 | goto errout; | |
144 | } | |
145 | ||
ea89c6c0 | 146 | if (smc_conn_lgr_valid(&smc->conn) && !smc->conn.lgr->is_smcd && |
c6ba7c9b | 147 | (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) && |
1a0a04c7 | 148 | !list_empty(&smc->conn.lgr->list)) { |
79d39fc5 | 149 | struct smc_link *link = smc->conn.lnk; |
79d39fc5 | 150 | |
f16a7dd5 UB |
151 | struct smc_diag_lgrinfo linfo = { |
152 | .role = smc->conn.lgr->role, | |
79d39fc5 TL |
153 | .lnk[0].ibport = link->ibport, |
154 | .lnk[0].link_id = link->link_id, | |
f16a7dd5 UB |
155 | }; |
156 | ||
9dbe086c | 157 | memcpy(linfo.lnk[0].ibname, link->smcibdev->ibdev->name, |
79d39fc5 TL |
158 | sizeof(link->smcibdev->ibdev->name)); |
159 | smc_gid_be16_convert(linfo.lnk[0].gid, link->gid); | |
160 | smc_gid_be16_convert(linfo.lnk[0].peer_gid, link->peer_gid); | |
f16a7dd5 UB |
161 | |
162 | if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0) | |
163 | goto errout; | |
164 | } | |
ea89c6c0 | 165 | if (smc_conn_lgr_valid(&smc->conn) && smc->conn.lgr->is_smcd && |
4b1b7d3b | 166 | (req->diag_ext & (1 << (SMC_DIAG_DMBINFO - 1))) && |
dbc153fd | 167 | !list_empty(&smc->conn.lgr->list) && smc->conn.rmb_desc) { |
4b1b7d3b | 168 | struct smc_connection *conn = &smc->conn; |
ce51f63e | 169 | struct smcd_diag_dmbinfo dinfo; |
9de4df7b | 170 | struct smcd_dev *smcd = conn->lgr->smcd; |
b40584d1 | 171 | struct smcd_gid smcd_gid; |
ce51f63e PY |
172 | |
173 | memset(&dinfo, 0, sizeof(dinfo)); | |
174 | ||
175 | dinfo.linkid = *((u32 *)conn->lgr->id); | |
b40584d1 | 176 | dinfo.peer_gid = conn->lgr->peer_gid.gid; |
01fd1617 | 177 | dinfo.peer_gid_ext = conn->lgr->peer_gid.gid_ext; |
b40584d1 WG |
178 | smcd->ops->get_local_gid(smcd, &smcd_gid); |
179 | dinfo.my_gid = smcd_gid.gid; | |
01fd1617 | 180 | dinfo.my_gid_ext = smcd_gid.gid_ext; |
ce51f63e PY |
181 | dinfo.token = conn->rmb_desc->token; |
182 | dinfo.peer_token = conn->peer_token; | |
4b1b7d3b HW |
183 | |
184 | if (nla_put(skb, SMC_DIAG_DMBINFO, sizeof(dinfo), &dinfo) < 0) | |
185 | goto errout; | |
186 | } | |
f16a7dd5 UB |
187 | |
188 | nlmsg_end(skb, nlh); | |
189 | return 0; | |
190 | ||
191 | errout: | |
192 | nlmsg_cancel(skb, nlh); | |
193 | return -EMSGSIZE; | |
194 | } | |
195 | ||
ed75986f | 196 | static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb, |
8418cb40 | 197 | struct netlink_callback *cb, int p_type) |
f16a7dd5 | 198 | { |
8418cb40 | 199 | struct smc_diag_dump_ctx *cb_ctx = smc_dump_context(cb); |
f16a7dd5 | 200 | struct net *net = sock_net(skb->sk); |
8418cb40 | 201 | int snum = cb_ctx->pos[p_type]; |
f16a7dd5 UB |
202 | struct nlattr *bc = NULL; |
203 | struct hlist_head *head; | |
8418cb40 | 204 | int rc = 0, num = 0; |
f16a7dd5 | 205 | struct sock *sk; |
f16a7dd5 | 206 | |
ed75986f KG |
207 | read_lock(&prot->h.smc_hash->lock); |
208 | head = &prot->h.smc_hash->ht; | |
f16a7dd5 UB |
209 | if (hlist_empty(head)) |
210 | goto out; | |
211 | ||
212 | sk_for_each(sk, head) { | |
213 | if (!net_eq(sock_net(sk), net)) | |
214 | continue; | |
8418cb40 GG |
215 | if (num < snum) |
216 | goto next; | |
f16a7dd5 | 217 | rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc); |
8418cb40 GG |
218 | if (rc < 0) |
219 | goto out; | |
220 | next: | |
221 | num++; | |
f16a7dd5 UB |
222 | } |
223 | ||
224 | out: | |
ed75986f | 225 | read_unlock(&prot->h.smc_hash->lock); |
8418cb40 | 226 | cb_ctx->pos[p_type] = num; |
ed75986f KG |
227 | return rc; |
228 | } | |
229 | ||
230 | static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
231 | { | |
232 | int rc = 0; | |
233 | ||
8418cb40 | 234 | rc = smc_diag_dump_proto(&smc_proto, skb, cb, SMCPROTO_SMC); |
ed75986f | 235 | if (!rc) |
8418cb40 GG |
236 | smc_diag_dump_proto(&smc_proto6, skb, cb, SMCPROTO_SMC6); |
237 | return skb->len; | |
f16a7dd5 UB |
238 | } |
239 | ||
240 | static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) | |
241 | { | |
242 | struct net *net = sock_net(skb->sk); | |
243 | ||
244 | if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && | |
245 | h->nlmsg_flags & NLM_F_DUMP) { | |
246 | { | |
247 | struct netlink_dump_control c = { | |
248 | .dump = smc_diag_dump, | |
249 | .min_dump_alloc = SKB_WITH_OVERHEAD(32768), | |
250 | }; | |
251 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); | |
252 | } | |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static const struct sock_diag_handler smc_diag_handler = { | |
114b4bb1 | 258 | .owner = THIS_MODULE, |
f16a7dd5 UB |
259 | .family = AF_SMC, |
260 | .dump = smc_diag_handler_dump, | |
261 | }; | |
262 | ||
263 | static int __init smc_diag_init(void) | |
264 | { | |
265 | return sock_diag_register(&smc_diag_handler); | |
266 | } | |
267 | ||
268 | static void __exit smc_diag_exit(void) | |
269 | { | |
270 | sock_diag_unregister(&smc_diag_handler); | |
271 | } | |
272 | ||
273 | module_init(smc_diag_init); | |
274 | module_exit(smc_diag_exit); | |
275 | MODULE_LICENSE("GPL"); | |
938dbead | 276 | MODULE_DESCRIPTION("SMC socket monitoring via SOCK_DIAG"); |
f16a7dd5 | 277 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */); |
28ec53f3 | 278 | MODULE_ALIAS_GENL_FAMILY(SMCR_GENL_FAMILY_NAME); |