rxe_recv.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 (qpn != IB_MULTICAST_QPN) {
  198. index = (qpn == 0) ? port->qp_smi_index :
  199. ((qpn == 1) ? port->qp_gsi_index : qpn);
  200. qp = rxe_pool_get_index(&rxe->qp_pool, index);
  201. if (unlikely(!qp)) {
  202. pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn);
  203. goto err1;
  204. }
  205. err = check_type_state(rxe, pkt, qp);
  206. if (unlikely(err))
  207. goto err2;
  208. err = check_addr(rxe, pkt, qp);
  209. if (unlikely(err))
  210. goto err2;
  211. err = check_keys(rxe, pkt, qpn, qp);
  212. if (unlikely(err))
  213. goto err2;
  214. } else {
  215. if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) {
  216. pr_warn_ratelimited("no grh for mcast qpn\n");
  217. goto err1;
  218. }
  219. }
  220. pkt->qp = qp;
  221. return 0;
  222. err2:
  223. if (qp)
  224. rxe_drop_ref(qp);
  225. err1:
  226. return -EINVAL;
  227. }
  228. static inline void rxe_rcv_pkt(struct rxe_dev *rxe,
  229. struct rxe_pkt_info *pkt,
  230. struct sk_buff *skb)
  231. {
  232. if (pkt->mask & RXE_REQ_MASK)
  233. rxe_resp_queue_pkt(rxe, pkt->qp, skb);
  234. else
  235. rxe_comp_queue_pkt(rxe, pkt->qp, skb);
  236. }
  237. static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb)
  238. {
  239. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  240. struct rxe_mc_grp *mcg;
  241. struct sk_buff *skb_copy;
  242. struct rxe_mc_elem *mce;
  243. struct rxe_qp *qp;
  244. union ib_gid dgid;
  245. int err;
  246. if (skb->protocol == htons(ETH_P_IP))
  247. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  248. (struct in6_addr *)&dgid);
  249. else if (skb->protocol == htons(ETH_P_IPV6))
  250. memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
  251. /* lookup mcast group corresponding to mgid, takes a ref */
  252. mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
  253. if (!mcg)
  254. goto err1; /* mcast group not registered */
  255. spin_lock_bh(&mcg->mcg_lock);
  256. list_for_each_entry(mce, &mcg->qp_list, qp_list) {
  257. qp = mce->qp;
  258. pkt = SKB_TO_PKT(skb);
  259. /* validate qp for incoming packet */
  260. err = check_type_state(rxe, pkt, qp);
  261. if (err)
  262. continue;
  263. err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
  264. if (err)
  265. continue;
  266. /* if *not* the last qp in the list
  267. * make a copy of the skb to post to the next qp
  268. */
  269. skb_copy = (mce->qp_list.next != &mcg->qp_list) ?
  270. skb_clone(skb, GFP_ATOMIC) : NULL;
  271. pkt->qp = qp;
  272. rxe_add_ref(qp);
  273. rxe_rcv_pkt(rxe, pkt, skb);
  274. skb = skb_copy;
  275. if (!skb)
  276. break;
  277. }
  278. spin_unlock_bh(&mcg->mcg_lock);
  279. rxe_drop_ref(mcg); /* drop ref from rxe_pool_get_key. */
  280. err1:
  281. if (skb)
  282. kfree_skb(skb);
  283. }
  284. static int rxe_match_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
  285. {
  286. union ib_gid dgid;
  287. union ib_gid *pdgid;
  288. u16 index;
  289. if (skb->protocol == htons(ETH_P_IP)) {
  290. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  291. (struct in6_addr *)&dgid);
  292. pdgid = &dgid;
  293. } else {
  294. pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
  295. }
  296. return ib_find_cached_gid_by_port(&rxe->ib_dev, pdgid,
  297. IB_GID_TYPE_ROCE_UDP_ENCAP,
  298. 1, rxe->ndev, &index);
  299. }
  300. /* rxe_rcv is called from the interface driver */
  301. int rxe_rcv(struct sk_buff *skb)
  302. {
  303. int err;
  304. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  305. struct rxe_dev *rxe = pkt->rxe;
  306. __be32 *icrcp;
  307. u32 calc_icrc, pack_icrc;
  308. pkt->offset = 0;
  309. if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
  310. goto drop;
  311. if (unlikely(rxe_match_dgid(rxe, skb) < 0)) {
  312. pr_warn_ratelimited("failed matching dgid\n");
  313. goto drop;
  314. }
  315. pkt->opcode = bth_opcode(pkt);
  316. pkt->psn = bth_psn(pkt);
  317. pkt->qp = NULL;
  318. pkt->mask |= rxe_opcode[pkt->opcode].mask;
  319. if (unlikely(skb->len < header_size(pkt)))
  320. goto drop;
  321. err = hdr_check(pkt);
  322. if (unlikely(err))
  323. goto drop;
  324. /* Verify ICRC */
  325. icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
  326. pack_icrc = be32_to_cpu(*icrcp);
  327. calc_icrc = rxe_icrc_hdr(pkt, skb);
  328. calc_icrc = crc32_le(calc_icrc, (u8 *)payload_addr(pkt),
  329. payload_size(pkt));
  330. calc_icrc = cpu_to_be32(~calc_icrc);
  331. if (unlikely(calc_icrc != pack_icrc)) {
  332. char saddr[sizeof(struct in6_addr)];
  333. if (skb->protocol == htons(ETH_P_IPV6))
  334. sprintf(saddr, "%pI6", &ipv6_hdr(skb)->saddr);
  335. else if (skb->protocol == htons(ETH_P_IP))
  336. sprintf(saddr, "%pI4", &ip_hdr(skb)->saddr);
  337. else
  338. sprintf(saddr, "unknown");
  339. pr_warn_ratelimited("bad ICRC from %s\n", saddr);
  340. goto drop;
  341. }
  342. if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
  343. rxe_rcv_mcast_pkt(rxe, skb);
  344. else
  345. rxe_rcv_pkt(rxe, pkt, skb);
  346. return 0;
  347. drop:
  348. if (pkt->qp)
  349. rxe_drop_ref(pkt->qp);
  350. kfree_skb(skb);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(rxe_rcv);