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