blob: 64ce107c24d9a25e47d85dfe4fccc807ce2dc366 [file] [log] [blame]
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001/*
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
24static 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
37static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)
38{
39 struct smc_sock *smc = smc_sk(sk);
40
Ursula Braunf16a7dd2017-01-09 16:55:26 +010041 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);
Karsten Grauled759862018-05-02 16:56:45 +020047 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 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +010062}
63
64static 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
76static 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 r->diag_fallback = smc->use_fallback;
95 user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk);
96 if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns))
97 goto errout;
98
Ursula Braun1a0a04c2018-01-25 11:15:36 +010099 if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) &&
100 smc->conn.alert_token_local) {
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100101 struct smc_connection *conn = &smc->conn;
102 struct smc_diag_conninfo cinfo = {
103 .token = conn->alert_token_local,
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200104 .sndbuf_size = conn->sndbuf_desc ?
105 conn->sndbuf_desc->len : 0,
106 .rmbe_size = conn->rmb_desc ? conn->rmb_desc->len : 0,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100107 .peer_rmbe_size = conn->peer_rmbe_size,
108
109 .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap,
110 .rx_prod.count = conn->local_rx_ctrl.prod.count,
111 .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap,
112 .rx_cons.count = conn->local_rx_ctrl.cons.count,
113
114 .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap,
115 .tx_prod.count = conn->local_tx_ctrl.prod.count,
116 .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap,
117 .tx_cons.count = conn->local_tx_ctrl.cons.count,
118
119 .tx_prod_flags =
120 *(u8 *)&conn->local_tx_ctrl.prod_flags,
121 .tx_conn_state_flags =
122 *(u8 *)&conn->local_tx_ctrl.conn_state_flags,
123 .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags,
124 .rx_conn_state_flags =
125 *(u8 *)&conn->local_rx_ctrl.conn_state_flags,
126
127 .tx_prep.wrap = conn->tx_curs_prep.wrap,
128 .tx_prep.count = conn->tx_curs_prep.count,
129 .tx_sent.wrap = conn->tx_curs_sent.wrap,
130 .tx_sent.count = conn->tx_curs_sent.count,
131 .tx_fin.wrap = conn->tx_curs_fin.wrap,
132 .tx_fin.count = conn->tx_curs_fin.count,
133 };
134
135 if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0)
136 goto errout;
137 }
138
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200139 if (smc->conn.lgr && !smc->conn.lgr->is_smcd &&
140 (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) &&
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100141 !list_empty(&smc->conn.lgr->list)) {
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100142 struct smc_diag_lgrinfo linfo = {
143 .role = smc->conn.lgr->role,
144 .lnk[0].ibport = smc->conn.lgr->lnk[0].ibport,
145 .lnk[0].link_id = smc->conn.lgr->lnk[0].link_id,
146 };
147
148 memcpy(linfo.lnk[0].ibname,
149 smc->conn.lgr->lnk[0].smcibdev->ibdev->name,
150 sizeof(smc->conn.lgr->lnk[0].smcibdev->ibdev->name));
151 smc_gid_be16_convert(linfo.lnk[0].gid,
152 smc->conn.lgr->lnk[0].gid.raw);
153 smc_gid_be16_convert(linfo.lnk[0].peer_gid,
154 smc->conn.lgr->lnk[0].peer_gid);
155
156 if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0)
157 goto errout;
158 }
159
160 nlmsg_end(skb, nlh);
161 return 0;
162
163errout:
164 nlmsg_cancel(skb, nlh);
165 return -EMSGSIZE;
166}
167
Karsten Grauled759862018-05-02 16:56:45 +0200168static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb,
169 struct netlink_callback *cb)
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100170{
171 struct net *net = sock_net(skb->sk);
172 struct nlattr *bc = NULL;
173 struct hlist_head *head;
174 struct sock *sk;
175 int rc = 0;
176
Karsten Grauled759862018-05-02 16:56:45 +0200177 read_lock(&prot->h.smc_hash->lock);
178 head = &prot->h.smc_hash->ht;
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100179 if (hlist_empty(head))
180 goto out;
181
182 sk_for_each(sk, head) {
183 if (!net_eq(sock_net(sk), net))
184 continue;
185 rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc);
186 if (rc)
187 break;
188 }
189
190out:
Karsten Grauled759862018-05-02 16:56:45 +0200191 read_unlock(&prot->h.smc_hash->lock);
192 return rc;
193}
194
195static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
196{
197 int rc = 0;
198
199 rc = smc_diag_dump_proto(&smc_proto, skb, cb);
200 if (!rc)
201 rc = smc_diag_dump_proto(&smc_proto6, skb, cb);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100202 return rc;
203}
204
205static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
206{
207 struct net *net = sock_net(skb->sk);
208
209 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
210 h->nlmsg_flags & NLM_F_DUMP) {
211 {
212 struct netlink_dump_control c = {
213 .dump = smc_diag_dump,
214 .min_dump_alloc = SKB_WITH_OVERHEAD(32768),
215 };
216 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
217 }
218 }
219 return 0;
220}
221
222static const struct sock_diag_handler smc_diag_handler = {
223 .family = AF_SMC,
224 .dump = smc_diag_handler_dump,
225};
226
227static int __init smc_diag_init(void)
228{
229 return sock_diag_register(&smc_diag_handler);
230}
231
232static void __exit smc_diag_exit(void)
233{
234 sock_diag_unregister(&smc_diag_handler);
235}
236
237module_init(smc_diag_init);
238module_exit(smc_diag_exit);
239MODULE_LICENSE("GPL");
240MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */);