xref: /openbmc/linux/net/smc/smc_diag.c (revision 83a530e1)
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 	r->diag_family = sk->sk_family;
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;
47 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
48 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
49 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
50 	r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr;
51 	r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr;
52 }
53 
54 static int smc_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
55 				   struct smc_diag_msg *r,
56 				   struct user_namespace *user_ns)
57 {
58 	if (nla_put_u8(skb, SMC_DIAG_SHUTDOWN, sk->sk_shutdown))
59 		return 1;
60 
61 	r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
62 	r->diag_inode = sock_i_ino(sk);
63 	return 0;
64 }
65 
66 static int __smc_diag_dump(struct sock *sk, struct sk_buff *skb,
67 			   struct netlink_callback *cb,
68 			   const struct smc_diag_req *req,
69 			   struct nlattr *bc)
70 {
71 	struct smc_sock *smc = smc_sk(sk);
72 	struct user_namespace *user_ns;
73 	struct smc_diag_msg *r;
74 	struct nlmsghdr *nlh;
75 
76 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
77 			cb->nlh->nlmsg_type, sizeof(*r), NLM_F_MULTI);
78 	if (!nlh)
79 		return -EMSGSIZE;
80 
81 	r = nlmsg_data(nlh);
82 	smc_diag_msg_common_fill(r, sk);
83 	r->diag_state = sk->sk_state;
84 	r->diag_fallback = smc->use_fallback;
85 	user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk);
86 	if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns))
87 		goto errout;
88 
89 	if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) &&
90 	    smc->conn.alert_token_local) {
91 		struct smc_connection *conn = &smc->conn;
92 		struct smc_diag_conninfo cinfo = {
93 			.token = conn->alert_token_local,
94 			.sndbuf_size = conn->sndbuf_size,
95 			.rmbe_size = conn->rmbe_size,
96 			.peer_rmbe_size = conn->peer_rmbe_size,
97 
98 			.rx_prod.wrap = conn->local_rx_ctrl.prod.wrap,
99 			.rx_prod.count = conn->local_rx_ctrl.prod.count,
100 			.rx_cons.wrap = conn->local_rx_ctrl.cons.wrap,
101 			.rx_cons.count = conn->local_rx_ctrl.cons.count,
102 
103 			.tx_prod.wrap = conn->local_tx_ctrl.prod.wrap,
104 			.tx_prod.count = conn->local_tx_ctrl.prod.count,
105 			.tx_cons.wrap = conn->local_tx_ctrl.cons.wrap,
106 			.tx_cons.count = conn->local_tx_ctrl.cons.count,
107 
108 			.tx_prod_flags =
109 				*(u8 *)&conn->local_tx_ctrl.prod_flags,
110 			.tx_conn_state_flags =
111 				*(u8 *)&conn->local_tx_ctrl.conn_state_flags,
112 			.rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags,
113 			.rx_conn_state_flags =
114 				*(u8 *)&conn->local_rx_ctrl.conn_state_flags,
115 
116 			.tx_prep.wrap = conn->tx_curs_prep.wrap,
117 			.tx_prep.count = conn->tx_curs_prep.count,
118 			.tx_sent.wrap = conn->tx_curs_sent.wrap,
119 			.tx_sent.count = conn->tx_curs_sent.count,
120 			.tx_fin.wrap = conn->tx_curs_fin.wrap,
121 			.tx_fin.count = conn->tx_curs_fin.count,
122 		};
123 
124 		if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0)
125 			goto errout;
126 	}
127 
128 	if ((req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) && smc->conn.lgr &&
129 	    !list_empty(&smc->conn.lgr->list)) {
130 		struct smc_diag_lgrinfo linfo = {
131 			.role = smc->conn.lgr->role,
132 			.lnk[0].ibport = smc->conn.lgr->lnk[0].ibport,
133 			.lnk[0].link_id = smc->conn.lgr->lnk[0].link_id,
134 		};
135 
136 		memcpy(linfo.lnk[0].ibname,
137 		       smc->conn.lgr->lnk[0].smcibdev->ibdev->name,
138 		       sizeof(smc->conn.lgr->lnk[0].smcibdev->ibdev->name));
139 		smc_gid_be16_convert(linfo.lnk[0].gid,
140 				     smc->conn.lgr->lnk[0].gid.raw);
141 		smc_gid_be16_convert(linfo.lnk[0].peer_gid,
142 				     smc->conn.lgr->lnk[0].peer_gid);
143 
144 		if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0)
145 			goto errout;
146 	}
147 
148 	nlmsg_end(skb, nlh);
149 	return 0;
150 
151 errout:
152 	nlmsg_cancel(skb, nlh);
153 	return -EMSGSIZE;
154 }
155 
156 static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
157 {
158 	struct net *net = sock_net(skb->sk);
159 	struct nlattr *bc = NULL;
160 	struct hlist_head *head;
161 	struct sock *sk;
162 	int rc = 0;
163 
164 	read_lock(&smc_proto.h.smc_hash->lock);
165 	head = &smc_proto.h.smc_hash->ht;
166 	if (hlist_empty(head))
167 		goto out;
168 
169 	sk_for_each(sk, head) {
170 		if (!net_eq(sock_net(sk), net))
171 			continue;
172 		rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc);
173 		if (rc)
174 			break;
175 	}
176 
177 out:
178 	read_unlock(&smc_proto.h.smc_hash->lock);
179 	return rc;
180 }
181 
182 static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
183 {
184 	struct net *net = sock_net(skb->sk);
185 
186 	if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
187 	    h->nlmsg_flags & NLM_F_DUMP) {
188 		{
189 			struct netlink_dump_control c = {
190 				.dump = smc_diag_dump,
191 				.min_dump_alloc = SKB_WITH_OVERHEAD(32768),
192 			};
193 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
194 		}
195 	}
196 	return 0;
197 }
198 
199 static const struct sock_diag_handler smc_diag_handler = {
200 	.family = AF_SMC,
201 	.dump = smc_diag_handler_dump,
202 };
203 
204 static int __init smc_diag_init(void)
205 {
206 	return sock_diag_register(&smc_diag_handler);
207 }
208 
209 static void __exit smc_diag_exit(void)
210 {
211 	sock_diag_unregister(&smc_diag_handler);
212 }
213 
214 module_init(smc_diag_init);
215 module_exit(smc_diag_exit);
216 MODULE_LICENSE("GPL");
217 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */);
218