rxe_recv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/skbuff.h>
  34. #include "rxe.h"
  35. #include "rxe_loc.h"
  36. static int check_type_state(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  37. struct rxe_qp *qp)
  38. {
  39. if (unlikely(!qp->valid))
  40. goto err1;
  41. switch (qp_type(qp)) {
  42. case IB_QPT_RC:
  43. if (unlikely((pkt->opcode & IB_OPCODE_RC) != 0)) {
  44. pr_warn_ratelimited("bad qp type\n");
  45. goto err1;
  46. }
  47. break;
  48. case IB_QPT_UC:
  49. if (unlikely(!(pkt->opcode & IB_OPCODE_UC))) {
  50. pr_warn_ratelimited("bad qp type\n");
  51. goto err1;
  52. }
  53. break;
  54. case IB_QPT_UD:
  55. case IB_QPT_SMI:
  56. case IB_QPT_GSI:
  57. if (unlikely(!(pkt->opcode & IB_OPCODE_UD))) {
  58. pr_warn_ratelimited("bad qp type\n");
  59. goto err1;
  60. }
  61. break;
  62. default:
  63. pr_warn_ratelimited("unsupported qp type\n");
  64. goto err1;
  65. }
  66. if (pkt->mask & RXE_REQ_MASK) {
  67. if (unlikely(qp->resp.state != QP_STATE_READY))
  68. goto err1;
  69. } else if (unlikely(qp->req.state < QP_STATE_READY ||
  70. qp->req.state > QP_STATE_DRAINED)) {
  71. goto err1;
  72. }
  73. return 0;
  74. err1:
  75. return -EINVAL;
  76. }
  77. static void set_bad_pkey_cntr(struct rxe_port *port)
  78. {
  79. spin_lock_bh(&port->port_lock);
  80. port->attr.bad_pkey_cntr = min((u32)0xffff,
  81. port->attr.bad_pkey_cntr + 1);
  82. spin_unlock_bh(&port->port_lock);
  83. }
  84. static void set_qkey_viol_cntr(struct rxe_port *port)
  85. {
  86. spin_lock_bh(&port->port_lock);
  87. port->attr.qkey_viol_cntr = min((u32)0xffff,
  88. port->attr.qkey_viol_cntr + 1);
  89. spin_unlock_bh(&port->port_lock);
  90. }
  91. static int check_keys(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  92. u32 qpn, struct rxe_qp *qp)
  93. {
  94. int i;
  95. int found_pkey = 0;
  96. struct rxe_port *port = &rxe->port;
  97. u16 pkey = bth_pkey(pkt);
  98. pkt->pkey_index = 0;
  99. if (qpn == 1) {
  100. for (i = 0; i < port->attr.pkey_tbl_len; i++) {
  101. if (pkey_match(pkey, port->pkey_tbl[i])) {
  102. pkt->pkey_index = i;
  103. found_pkey = 1;
  104. break;
  105. }
  106. }
  107. if (!found_pkey) {
  108. pr_warn_ratelimited("bad pkey = 0x%x\n", pkey);
  109. set_bad_pkey_cntr(port);
  110. goto err1;
  111. }
  112. } else if (qpn != 0) {
  113. if (unlikely(!pkey_match(pkey,
  114. port->pkey_tbl[qp->attr.pkey_index]
  115. ))) {
  116. pr_warn_ratelimited("bad pkey = 0x%0x\n", pkey);
  117. set_bad_pkey_cntr(port);
  118. goto err1;
  119. }
  120. pkt->pkey_index = qp->attr.pkey_index;
  121. }
  122. if ((qp_type(qp) == IB_QPT_UD || qp_type(qp) == IB_QPT_GSI) &&
  123. qpn != 0 && pkt->mask) {
  124. u32 qkey = (qpn == 1) ? GSI_QKEY : qp->attr.qkey;
  125. if (unlikely(deth_qkey(pkt) != qkey)) {
  126. pr_warn_ratelimited("bad qkey, got 0x%x expected 0x%x for qpn 0x%x\n",
  127. deth_qkey(pkt), qkey, qpn);
  128. set_qkey_viol_cntr(port);
  129. goto err1;
  130. }
  131. }
  132. return 0;
  133. err1:
  134. return -EINVAL;
  135. }
  136. static int check_addr(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  137. struct rxe_qp *qp)
  138. {
  139. struct sk_buff *skb = PKT_TO_SKB(pkt);
  140. if (qp_type(qp) != IB_QPT_RC && qp_type(qp) != IB_QPT_UC)
  141. goto done;
  142. if (unlikely(pkt->port_num != qp->attr.port_num)) {
  143. pr_warn_ratelimited("port %d != qp port %d\n",
  144. pkt->port_num, qp->attr.port_num);
  145. goto err1;
  146. }
  147. if (skb->protocol == htons(ETH_P_IP)) {
  148. struct in_addr *saddr =
  149. &qp->pri_av.sgid_addr._sockaddr_in.sin_addr;
  150. struct in_addr *daddr =
  151. &qp->pri_av.dgid_addr._sockaddr_in.sin_addr;
  152. if (ip_hdr(skb)->daddr != saddr->s_addr) {
  153. pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n",
  154. &ip_hdr(skb)->daddr,
  155. &saddr->s_addr);
  156. goto err1;
  157. }
  158. if (ip_hdr(skb)->saddr != daddr->s_addr) {
  159. pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n",
  160. &ip_hdr(skb)->saddr,
  161. &daddr->s_addr);
  162. goto err1;
  163. }
  164. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  165. struct in6_addr *saddr =
  166. &qp->pri_av.sgid_addr._sockaddr_in6.sin6_addr;
  167. struct in6_addr *daddr =
  168. &qp->pri_av.dgid_addr._sockaddr_in6.sin6_addr;
  169. if (memcmp(&ipv6_hdr(skb)->daddr, saddr, sizeof(*saddr))) {
  170. pr_warn_ratelimited("dst addr %pI6 != qp source addr %pI6\n",
  171. &ipv6_hdr(skb)->daddr, saddr);
  172. goto err1;
  173. }
  174. if (memcmp(&ipv6_hdr(skb)->saddr, daddr, sizeof(*daddr))) {
  175. pr_warn_ratelimited("source addr %pI6 != qp dst addr %pI6\n",
  176. &ipv6_hdr(skb)->saddr, daddr);
  177. goto err1;
  178. }
  179. }
  180. done:
  181. return 0;
  182. err1:
  183. return -EINVAL;
  184. }
  185. static int hdr_check(struct rxe_pkt_info *pkt)
  186. {
  187. struct rxe_dev *rxe = pkt->rxe;
  188. struct rxe_port *port = &rxe->port;
  189. struct rxe_qp *qp = NULL;
  190. u32 qpn = bth_qpn(pkt);
  191. int index;
  192. int err;
  193. if (unlikely(bth_tver(pkt) != BTH_TVER)) {
  194. pr_warn_ratelimited("bad tver\n");
  195. goto err1;
  196. }
  197. if (unlikely(qpn == 0)) {
  198. pr_warn_once("QP 0 not supported");
  199. goto err1;
  200. }
  201. if (qpn != IB_MULTICAST_QPN) {
  202. index = (qpn == 1) ? port->qp_gsi_index : qpn;
  203. qp = rxe_pool_get_index(&rxe->qp_pool, index);
  204. if (unlikely(!qp)) {
  205. pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn);
  206. goto err1;
  207. }
  208. err = check_type_state(rxe, pkt, qp);
  209. if (unlikely(err))
  210. goto err2;
  211. err = check_addr(rxe, pkt, qp);
  212. if (unlikely(err))
  213. goto err2;
  214. err = check_keys(rxe, pkt, qpn, qp);
  215. if (unlikely(err))
  216. goto err2;
  217. } else {
  218. if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) {
  219. pr_warn_ratelimited("no grh for mcast qpn\n");
  220. goto err1;
  221. }
  222. }
  223. pkt->qp = qp;
  224. return 0;
  225. err2:
  226. rxe_drop_ref(qp);
  227. err1:
  228. return -EINVAL;
  229. }
  230. static inline void rxe_rcv_pkt(struct rxe_dev *rxe,
  231. struct rxe_pkt_info *pkt,
  232. struct sk_buff *skb)
  233. {
  234. if (pkt->mask & RXE_REQ_MASK)
  235. rxe_resp_queue_pkt(rxe, pkt->qp, skb);
  236. else
  237. rxe_comp_queue_pkt(rxe, pkt->qp, skb);
  238. }
  239. static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb)
  240. {
  241. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  242. struct rxe_mc_grp *mcg;
  243. struct rxe_mc_elem *mce;
  244. struct rxe_qp *qp;
  245. union ib_gid dgid;
  246. int err;
  247. if (skb->protocol == htons(ETH_P_IP))
  248. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  249. (struct in6_addr *)&dgid);
  250. else if (skb->protocol == htons(ETH_P_IPV6))
  251. memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
  252. /* lookup mcast group corresponding to mgid, takes a ref */
  253. mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
  254. if (!mcg)
  255. goto err1; /* mcast group not registered */
  256. spin_lock_bh(&mcg->mcg_lock);
  257. list_for_each_entry(mce, &mcg->qp_list, qp_list) {
  258. qp = mce->qp;
  259. pkt = SKB_TO_PKT(skb);
  260. /* validate qp for incoming packet */
  261. err = check_type_state(rxe, pkt, qp);
  262. if (err)
  263. continue;
  264. err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
  265. if (err)
  266. continue;
  267. /* if *not* the last qp in the list
  268. * increase the users of the skb then post to the next qp
  269. */
  270. if (mce->qp_list.next != &mcg->qp_list)
  271. skb_get(skb);
  272. pkt->qp = qp;
  273. rxe_add_ref(qp);
  274. rxe_rcv_pkt(rxe, pkt, skb);
  275. }
  276. spin_unlock_bh(&mcg->mcg_lock);
  277. rxe_drop_ref(mcg); /* drop ref from rxe_pool_get_key. */
  278. err1:
  279. kfree_skb(skb);
  280. }
  281. static int rxe_match_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
  282. {
  283. const struct ib_gid_attr *gid_attr;
  284. union ib_gid dgid;
  285. union ib_gid *pdgid;
  286. if (skb->protocol == htons(ETH_P_IP)) {
  287. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  288. (struct in6_addr *)&dgid);
  289. pdgid = &dgid;
  290. } else {
  291. pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
  292. }
  293. gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid,
  294. IB_GID_TYPE_ROCE_UDP_ENCAP,
  295. 1, skb->dev);
  296. if (IS_ERR(gid_attr))
  297. return PTR_ERR(gid_attr);
  298. rdma_put_gid_attr(gid_attr);
  299. return 0;
  300. }
  301. /* rxe_rcv is called from the interface driver */
  302. void rxe_rcv(struct sk_buff *skb)
  303. {
  304. int err;
  305. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  306. struct rxe_dev *rxe = pkt->rxe;
  307. __be32 *icrcp;
  308. u32 calc_icrc, pack_icrc;
  309. pkt->offset = 0;
  310. if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
  311. goto drop;
  312. if (unlikely(rxe_match_dgid(rxe, skb) < 0)) {
  313. pr_warn_ratelimited("failed matching dgid\n");
  314. goto drop;
  315. }
  316. pkt->opcode = bth_opcode(pkt);
  317. pkt->psn = bth_psn(pkt);
  318. pkt->qp = NULL;
  319. pkt->mask |= rxe_opcode[pkt->opcode].mask;
  320. if (unlikely(skb->len < header_size(pkt)))
  321. goto drop;
  322. err = hdr_check(pkt);
  323. if (unlikely(err))
  324. goto drop;
  325. /* Verify ICRC */
  326. icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
  327. pack_icrc = be32_to_cpu(*icrcp);
  328. calc_icrc = rxe_icrc_hdr(pkt, skb);
  329. calc_icrc = rxe_crc32(rxe, calc_icrc, (u8 *)payload_addr(pkt),
  330. payload_size(pkt) + bth_pad(pkt));
  331. calc_icrc = (__force u32)cpu_to_be32(~calc_icrc);
  332. if (unlikely(calc_icrc != pack_icrc)) {
  333. if (skb->protocol == htons(ETH_P_IPV6))
  334. pr_warn_ratelimited("bad ICRC from %pI6c\n",
  335. &ipv6_hdr(skb)->saddr);
  336. else if (skb->protocol == htons(ETH_P_IP))
  337. pr_warn_ratelimited("bad ICRC from %pI4\n",
  338. &ip_hdr(skb)->saddr);
  339. else
  340. pr_warn_ratelimited("bad ICRC from unknown\n");
  341. goto drop;
  342. }
  343. rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS);
  344. if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
  345. rxe_rcv_mcast_pkt(rxe, skb);
  346. else
  347. rxe_rcv_pkt(rxe, pkt, skb);
  348. return;
  349. drop:
  350. if (pkt->qp)
  351. rxe_drop_ref(pkt->qp);
  352. kfree_skb(skb);
  353. }