smc_diag.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-only
  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. #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. #include "smc.h"
  21. #include "smc_core.h"
  22. struct smc_diag_dump_ctx {
  23. int pos[2];
  24. };
  25. static struct smc_diag_dump_ctx *smc_dump_context(struct netlink_callback *cb)
  26. {
  27. return (struct smc_diag_dump_ctx *)cb->ctx;
  28. }
  29. static void smc_gid_be16_convert(__u8 *buf, u8 *gid_raw)
  30. {
  31. sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
  32. be16_to_cpu(((__be16 *)gid_raw)[0]),
  33. be16_to_cpu(((__be16 *)gid_raw)[1]),
  34. be16_to_cpu(((__be16 *)gid_raw)[2]),
  35. be16_to_cpu(((__be16 *)gid_raw)[3]),
  36. be16_to_cpu(((__be16 *)gid_raw)[4]),
  37. be16_to_cpu(((__be16 *)gid_raw)[5]),
  38. be16_to_cpu(((__be16 *)gid_raw)[6]),
  39. be16_to_cpu(((__be16 *)gid_raw)[7]));
  40. }
  41. static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)
  42. {
  43. struct smc_sock *smc = smc_sk(sk);
  44. memset(r, 0, sizeof(*r));
  45. r->diag_family = sk->sk_family;
  46. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  47. if (!smc->clcsock)
  48. return;
  49. r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);
  50. r->id.idiag_dport = smc->clcsock->sk->sk_dport;
  51. r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if;
  52. if (sk->sk_protocol == SMCPROTO_SMC) {
  53. r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr;
  54. r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr;
  55. #if IS_ENABLED(CONFIG_IPV6)
  56. } else if (sk->sk_protocol == SMCPROTO_SMC6) {
  57. memcpy(&r->id.idiag_src, &smc->clcsock->sk->sk_v6_rcv_saddr,
  58. sizeof(smc->clcsock->sk->sk_v6_rcv_saddr));
  59. memcpy(&r->id.idiag_dst, &smc->clcsock->sk->sk_v6_daddr,
  60. sizeof(smc->clcsock->sk->sk_v6_daddr));
  61. #endif
  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. r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  71. r->diag_inode = sock_i_ino(sk);
  72. return 0;
  73. }
  74. static int __smc_diag_dump(struct sock *sk, struct sk_buff *skb,
  75. struct netlink_callback *cb,
  76. const struct smc_diag_req *req,
  77. struct nlattr *bc)
  78. {
  79. struct smc_sock *smc = smc_sk(sk);
  80. struct smc_diag_fallback fallback;
  81. struct user_namespace *user_ns;
  82. struct smc_diag_msg *r;
  83. struct nlmsghdr *nlh;
  84. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  85. cb->nlh->nlmsg_type, sizeof(*r), NLM_F_MULTI);
  86. if (!nlh)
  87. return -EMSGSIZE;
  88. r = nlmsg_data(nlh);
  89. smc_diag_msg_common_fill(r, sk);
  90. r->diag_state = sk->sk_state;
  91. if (smc->use_fallback)
  92. r->diag_mode = SMC_DIAG_MODE_FALLBACK_TCP;
  93. else if (smc->conn.lgr && smc->conn.lgr->is_smcd)
  94. r->diag_mode = SMC_DIAG_MODE_SMCD;
  95. else
  96. r->diag_mode = SMC_DIAG_MODE_SMCR;
  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. 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. 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. .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap,
  114. .rx_prod.count = conn->local_rx_ctrl.prod.count,
  115. .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap,
  116. .rx_cons.count = conn->local_rx_ctrl.cons.count,
  117. .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap,
  118. .tx_prod.count = conn->local_tx_ctrl.prod.count,
  119. .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap,
  120. .tx_cons.count = conn->local_tx_ctrl.cons.count,
  121. .tx_prod_flags =
  122. *(u8 *)&conn->local_tx_ctrl.prod_flags,
  123. .tx_conn_state_flags =
  124. *(u8 *)&conn->local_tx_ctrl.conn_state_flags,
  125. .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags,
  126. .rx_conn_state_flags =
  127. *(u8 *)&conn->local_rx_ctrl.conn_state_flags,
  128. .tx_prep.wrap = conn->tx_curs_prep.wrap,
  129. .tx_prep.count = conn->tx_curs_prep.count,
  130. .tx_sent.wrap = conn->tx_curs_sent.wrap,
  131. .tx_sent.count = conn->tx_curs_sent.count,
  132. .tx_fin.wrap = conn->tx_curs_fin.wrap,
  133. .tx_fin.count = conn->tx_curs_fin.count,
  134. };
  135. if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0)
  136. goto errout;
  137. }
  138. if (smc->conn.lgr && !smc->conn.lgr->is_smcd &&
  139. (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) &&
  140. !list_empty(&smc->conn.lgr->list)) {
  141. struct smc_diag_lgrinfo linfo = {
  142. .role = smc->conn.lgr->role,
  143. .lnk[0].ibport = smc->conn.lgr->lnk[0].ibport,
  144. .lnk[0].link_id = smc->conn.lgr->lnk[0].link_id,
  145. };
  146. memcpy(linfo.lnk[0].ibname,
  147. smc->conn.lgr->lnk[0].smcibdev->ibdev->name,
  148. sizeof(smc->conn.lgr->lnk[0].smcibdev->ibdev->name));
  149. smc_gid_be16_convert(linfo.lnk[0].gid,
  150. smc->conn.lgr->lnk[0].gid);
  151. smc_gid_be16_convert(linfo.lnk[0].peer_gid,
  152. smc->conn.lgr->lnk[0].peer_gid);
  153. if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0)
  154. goto errout;
  155. }
  156. if (smc->conn.lgr && smc->conn.lgr->is_smcd &&
  157. (req->diag_ext & (1 << (SMC_DIAG_DMBINFO - 1))) &&
  158. !list_empty(&smc->conn.lgr->list)) {
  159. struct smc_connection *conn = &smc->conn;
  160. struct smcd_diag_dmbinfo dinfo;
  161. memset(&dinfo, 0, sizeof(dinfo));
  162. dinfo.linkid = *((u32 *)conn->lgr->id);
  163. dinfo.peer_gid = conn->lgr->peer_gid;
  164. dinfo.my_gid = conn->lgr->smcd->local_gid;
  165. dinfo.token = conn->rmb_desc->token;
  166. dinfo.peer_token = conn->peer_token;
  167. if (nla_put(skb, SMC_DIAG_DMBINFO, sizeof(dinfo), &dinfo) < 0)
  168. goto errout;
  169. }
  170. nlmsg_end(skb, nlh);
  171. return 0;
  172. errout:
  173. nlmsg_cancel(skb, nlh);
  174. return -EMSGSIZE;
  175. }
  176. static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb,
  177. struct netlink_callback *cb, int p_type)
  178. {
  179. struct smc_diag_dump_ctx *cb_ctx = smc_dump_context(cb);
  180. struct net *net = sock_net(skb->sk);
  181. int snum = cb_ctx->pos[p_type];
  182. struct nlattr *bc = NULL;
  183. struct hlist_head *head;
  184. int rc = 0, num = 0;
  185. struct sock *sk;
  186. read_lock(&prot->h.smc_hash->lock);
  187. head = &prot->h.smc_hash->ht;
  188. if (hlist_empty(head))
  189. goto out;
  190. sk_for_each(sk, head) {
  191. if (!net_eq(sock_net(sk), net))
  192. continue;
  193. if (num < snum)
  194. goto next;
  195. rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc);
  196. if (rc < 0)
  197. goto out;
  198. next:
  199. num++;
  200. }
  201. out:
  202. read_unlock(&prot->h.smc_hash->lock);
  203. cb_ctx->pos[p_type] = num;
  204. return rc;
  205. }
  206. static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  207. {
  208. int rc = 0;
  209. rc = smc_diag_dump_proto(&smc_proto, skb, cb, SMCPROTO_SMC);
  210. if (!rc)
  211. smc_diag_dump_proto(&smc_proto6, skb, cb, SMCPROTO_SMC6);
  212. return skb->len;
  213. }
  214. static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  215. {
  216. struct net *net = sock_net(skb->sk);
  217. if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
  218. h->nlmsg_flags & NLM_F_DUMP) {
  219. {
  220. struct netlink_dump_control c = {
  221. .dump = smc_diag_dump,
  222. .min_dump_alloc = SKB_WITH_OVERHEAD(32768),
  223. };
  224. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  225. }
  226. }
  227. return 0;
  228. }
  229. static const struct sock_diag_handler smc_diag_handler = {
  230. .family = AF_SMC,
  231. .dump = smc_diag_handler_dump,
  232. };
  233. static int __init smc_diag_init(void)
  234. {
  235. return sock_diag_register(&smc_diag_handler);
  236. }
  237. static void __exit smc_diag_exit(void)
  238. {
  239. sock_diag_unregister(&smc_diag_handler);
  240. }
  241. module_init(smc_diag_init);
  242. module_exit(smc_diag_exit);
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */);