smc_diag.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/sock_diag.h>
  15. #include <linux/inet_diag.h>
  16. #include <linux/smc_diag.h>
  17. #include <net/netlink.h>
  18. #include <net/smc.h>
  19. #include "smc.h"
  20. #include "smc_core.h"
  21. static void smc_gid_be16_convert(__u8 *buf, u8 *gid_raw)
  22. {
  23. sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
  24. be16_to_cpu(((__be16 *)gid_raw)[0]),
  25. be16_to_cpu(((__be16 *)gid_raw)[1]),
  26. be16_to_cpu(((__be16 *)gid_raw)[2]),
  27. be16_to_cpu(((__be16 *)gid_raw)[3]),
  28. be16_to_cpu(((__be16 *)gid_raw)[4]),
  29. be16_to_cpu(((__be16 *)gid_raw)[5]),
  30. be16_to_cpu(((__be16 *)gid_raw)[6]),
  31. be16_to_cpu(((__be16 *)gid_raw)[7]));
  32. }
  33. static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)
  34. {
  35. struct smc_sock *smc = smc_sk(sk);
  36. memset(r, 0, sizeof(*r));
  37. r->diag_family = sk->sk_family;
  38. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  39. if (!smc->clcsock)
  40. return;
  41. r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);
  42. r->id.idiag_dport = smc->clcsock->sk->sk_dport;
  43. r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if;
  44. if (sk->sk_protocol == SMCPROTO_SMC) {
  45. r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr;
  46. r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr;
  47. #if IS_ENABLED(CONFIG_IPV6)
  48. } else if (sk->sk_protocol == SMCPROTO_SMC6) {
  49. memcpy(&r->id.idiag_src, &smc->clcsock->sk->sk_v6_rcv_saddr,
  50. sizeof(smc->clcsock->sk->sk_v6_rcv_saddr));
  51. memcpy(&r->id.idiag_dst, &smc->clcsock->sk->sk_v6_daddr,
  52. sizeof(smc->clcsock->sk->sk_v6_daddr));
  53. #endif
  54. }
  55. }
  56. static int smc_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
  57. struct smc_diag_msg *r,
  58. struct user_namespace *user_ns)
  59. {
  60. if (nla_put_u8(skb, SMC_DIAG_SHUTDOWN, sk->sk_shutdown))
  61. return 1;
  62. r->diag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  63. r->diag_inode = sock_i_ino(sk);
  64. return 0;
  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 smc_diag_fallback fallback;
  73. struct user_namespace *user_ns;
  74. struct smc_diag_msg *r;
  75. struct nlmsghdr *nlh;
  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. r = nlmsg_data(nlh);
  81. smc_diag_msg_common_fill(r, sk);
  82. r->diag_state = sk->sk_state;
  83. if (smc->use_fallback)
  84. r->diag_mode = SMC_DIAG_MODE_FALLBACK_TCP;
  85. else if (smc->conn.lgr && smc->conn.lgr->is_smcd)
  86. r->diag_mode = SMC_DIAG_MODE_SMCD;
  87. else
  88. r->diag_mode = SMC_DIAG_MODE_SMCR;
  89. user_ns = sk_user_ns(NETLINK_CB(cb->skb).sk);
  90. if (smc_diag_msg_attrs_fill(sk, skb, r, user_ns))
  91. goto errout;
  92. fallback.reason = smc->fallback_rsn;
  93. fallback.peer_diagnosis = smc->peer_diagnosis;
  94. if (nla_put(skb, SMC_DIAG_FALLBACK, sizeof(fallback), &fallback) < 0)
  95. goto errout;
  96. if ((req->diag_ext & (1 << (SMC_DIAG_CONNINFO - 1))) &&
  97. smc->conn.alert_token_local) {
  98. struct smc_connection *conn = &smc->conn;
  99. struct smc_diag_conninfo cinfo = {
  100. .token = conn->alert_token_local,
  101. .sndbuf_size = conn->sndbuf_desc ?
  102. conn->sndbuf_desc->len : 0,
  103. .rmbe_size = conn->rmb_desc ? conn->rmb_desc->len : 0,
  104. .peer_rmbe_size = conn->peer_rmbe_size,
  105. .rx_prod.wrap = conn->local_rx_ctrl.prod.wrap,
  106. .rx_prod.count = conn->local_rx_ctrl.prod.count,
  107. .rx_cons.wrap = conn->local_rx_ctrl.cons.wrap,
  108. .rx_cons.count = conn->local_rx_ctrl.cons.count,
  109. .tx_prod.wrap = conn->local_tx_ctrl.prod.wrap,
  110. .tx_prod.count = conn->local_tx_ctrl.prod.count,
  111. .tx_cons.wrap = conn->local_tx_ctrl.cons.wrap,
  112. .tx_cons.count = conn->local_tx_ctrl.cons.count,
  113. .tx_prod_flags =
  114. *(u8 *)&conn->local_tx_ctrl.prod_flags,
  115. .tx_conn_state_flags =
  116. *(u8 *)&conn->local_tx_ctrl.conn_state_flags,
  117. .rx_prod_flags = *(u8 *)&conn->local_rx_ctrl.prod_flags,
  118. .rx_conn_state_flags =
  119. *(u8 *)&conn->local_rx_ctrl.conn_state_flags,
  120. .tx_prep.wrap = conn->tx_curs_prep.wrap,
  121. .tx_prep.count = conn->tx_curs_prep.count,
  122. .tx_sent.wrap = conn->tx_curs_sent.wrap,
  123. .tx_sent.count = conn->tx_curs_sent.count,
  124. .tx_fin.wrap = conn->tx_curs_fin.wrap,
  125. .tx_fin.count = conn->tx_curs_fin.count,
  126. };
  127. if (nla_put(skb, SMC_DIAG_CONNINFO, sizeof(cinfo), &cinfo) < 0)
  128. goto errout;
  129. }
  130. if (smc->conn.lgr && !smc->conn.lgr->is_smcd &&
  131. (req->diag_ext & (1 << (SMC_DIAG_LGRINFO - 1))) &&
  132. !list_empty(&smc->conn.lgr->list)) {
  133. struct smc_diag_lgrinfo linfo = {
  134. .role = smc->conn.lgr->role,
  135. .lnk[0].ibport = smc->conn.lgr->lnk[0].ibport,
  136. .lnk[0].link_id = smc->conn.lgr->lnk[0].link_id,
  137. };
  138. memcpy(linfo.lnk[0].ibname,
  139. smc->conn.lgr->lnk[0].smcibdev->ibdev->name,
  140. sizeof(smc->conn.lgr->lnk[0].smcibdev->ibdev->name));
  141. smc_gid_be16_convert(linfo.lnk[0].gid,
  142. smc->conn.lgr->lnk[0].gid);
  143. smc_gid_be16_convert(linfo.lnk[0].peer_gid,
  144. smc->conn.lgr->lnk[0].peer_gid);
  145. if (nla_put(skb, SMC_DIAG_LGRINFO, sizeof(linfo), &linfo) < 0)
  146. goto errout;
  147. }
  148. if (smc->conn.lgr && smc->conn.lgr->is_smcd &&
  149. (req->diag_ext & (1 << (SMC_DIAG_DMBINFO - 1))) &&
  150. !list_empty(&smc->conn.lgr->list)) {
  151. struct smc_connection *conn = &smc->conn;
  152. struct smcd_diag_dmbinfo dinfo = {
  153. .linkid = *((u32 *)conn->lgr->id),
  154. .peer_gid = conn->lgr->peer_gid,
  155. .my_gid = conn->lgr->smcd->local_gid,
  156. .token = conn->rmb_desc->token,
  157. .peer_token = conn->peer_token
  158. };
  159. if (nla_put(skb, SMC_DIAG_DMBINFO, sizeof(dinfo), &dinfo) < 0)
  160. goto errout;
  161. }
  162. nlmsg_end(skb, nlh);
  163. return 0;
  164. errout:
  165. nlmsg_cancel(skb, nlh);
  166. return -EMSGSIZE;
  167. }
  168. static int smc_diag_dump_proto(struct proto *prot, struct sk_buff *skb,
  169. struct netlink_callback *cb)
  170. {
  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. read_lock(&prot->h.smc_hash->lock);
  177. head = &prot->h.smc_hash->ht;
  178. if (hlist_empty(head))
  179. goto out;
  180. sk_for_each(sk, head) {
  181. if (!net_eq(sock_net(sk), net))
  182. continue;
  183. rc = __smc_diag_dump(sk, skb, cb, nlmsg_data(cb->nlh), bc);
  184. if (rc)
  185. break;
  186. }
  187. out:
  188. read_unlock(&prot->h.smc_hash->lock);
  189. return rc;
  190. }
  191. static int smc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  192. {
  193. int rc = 0;
  194. rc = smc_diag_dump_proto(&smc_proto, skb, cb);
  195. if (!rc)
  196. rc = smc_diag_dump_proto(&smc_proto6, skb, cb);
  197. return rc;
  198. }
  199. static int smc_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  200. {
  201. struct net *net = sock_net(skb->sk);
  202. if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
  203. h->nlmsg_flags & NLM_F_DUMP) {
  204. {
  205. struct netlink_dump_control c = {
  206. .dump = smc_diag_dump,
  207. .min_dump_alloc = SKB_WITH_OVERHEAD(32768),
  208. };
  209. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  210. }
  211. }
  212. return 0;
  213. }
  214. static const struct sock_diag_handler smc_diag_handler = {
  215. .family = AF_SMC,
  216. .dump = smc_diag_handler_dump,
  217. };
  218. static int __init smc_diag_init(void)
  219. {
  220. return sock_diag_register(&smc_diag_handler);
  221. }
  222. static void __exit smc_diag_exit(void)
  223. {
  224. sock_diag_unregister(&smc_diag_handler);
  225. }
  226. module_init(smc_diag_init);
  227. module_exit(smc_diag_exit);
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 43 /* AF_SMC */);