rxe_recv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* check that QP matches packet opcode type and is in a valid state */
  37. static int check_type_state(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  38. struct rxe_qp *qp)
  39. {
  40. unsigned int pkt_type;
  41. if (unlikely(!qp->valid))
  42. goto err1;
  43. pkt_type = pkt->opcode & 0xe0;
  44. switch (qp_type(qp)) {
  45. case IB_QPT_RC:
  46. if (unlikely(pkt_type != IB_OPCODE_RC)) {
  47. pr_warn_ratelimited("bad qp type\n");
  48. goto err1;
  49. }
  50. break;
  51. case IB_QPT_UC:
  52. if (unlikely(pkt_type != IB_OPCODE_UC)) {
  53. pr_warn_ratelimited("bad qp type\n");
  54. goto err1;
  55. }
  56. break;
  57. case IB_QPT_UD:
  58. case IB_QPT_SMI:
  59. case IB_QPT_GSI:
  60. if (unlikely(pkt_type != IB_OPCODE_UD)) {
  61. pr_warn_ratelimited("bad qp type\n");
  62. goto err1;
  63. }
  64. break;
  65. default:
  66. pr_warn_ratelimited("unsupported qp type\n");
  67. goto err1;
  68. }
  69. if (pkt->mask & RXE_REQ_MASK) {
  70. if (unlikely(qp->resp.state != QP_STATE_READY))
  71. goto err1;
  72. } else if (unlikely(qp->req.state < QP_STATE_READY ||
  73. qp->req.state > QP_STATE_DRAINED)) {
  74. goto err1;
  75. }
  76. return 0;
  77. err1:
  78. return -EINVAL;
  79. }
  80. static void set_bad_pkey_cntr(struct rxe_port *port)
  81. {
  82. spin_lock_bh(&port->port_lock);
  83. port->attr.bad_pkey_cntr = min((u32)0xffff,
  84. port->attr.bad_pkey_cntr + 1);
  85. spin_unlock_bh(&port->port_lock);
  86. }
  87. static void set_qkey_viol_cntr(struct rxe_port *port)
  88. {
  89. spin_lock_bh(&port->port_lock);
  90. port->attr.qkey_viol_cntr = min((u32)0xffff,
  91. port->attr.qkey_viol_cntr + 1);
  92. spin_unlock_bh(&port->port_lock);
  93. }
  94. static int check_keys(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  95. u32 qpn, struct rxe_qp *qp)
  96. {
  97. int i;
  98. int found_pkey = 0;
  99. struct rxe_port *port = &rxe->port;
  100. u16 pkey = bth_pkey(pkt);
  101. pkt->pkey_index = 0;
  102. if (qpn == 1) {
  103. for (i = 0; i < port->attr.pkey_tbl_len; i++) {
  104. if (pkey_match(pkey, port->pkey_tbl[i])) {
  105. pkt->pkey_index = i;
  106. found_pkey = 1;
  107. break;
  108. }
  109. }
  110. if (!found_pkey) {
  111. pr_warn_ratelimited("bad pkey = 0x%x\n", pkey);
  112. set_bad_pkey_cntr(port);
  113. goto err1;
  114. }
  115. } else if (qpn != 0) {
  116. if (unlikely(!pkey_match(pkey,
  117. port->pkey_tbl[qp->attr.pkey_index]
  118. ))) {
  119. pr_warn_ratelimited("bad pkey = 0x%0x\n", pkey);
  120. set_bad_pkey_cntr(port);
  121. goto err1;
  122. }
  123. pkt->pkey_index = qp->attr.pkey_index;
  124. }
  125. if ((qp_type(qp) == IB_QPT_UD || qp_type(qp) == IB_QPT_GSI) &&
  126. qpn != 0 && pkt->mask) {
  127. u32 qkey = (qpn == 1) ? GSI_QKEY : qp->attr.qkey;
  128. if (unlikely(deth_qkey(pkt) != qkey)) {
  129. pr_warn_ratelimited("bad qkey, got 0x%x expected 0x%x for qpn 0x%x\n",
  130. deth_qkey(pkt), qkey, qpn);
  131. set_qkey_viol_cntr(port);
  132. goto err1;
  133. }
  134. }
  135. return 0;
  136. err1:
  137. return -EINVAL;
  138. }
  139. static int check_addr(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
  140. struct rxe_qp *qp)
  141. {
  142. struct sk_buff *skb = PKT_TO_SKB(pkt);
  143. if (qp_type(qp) != IB_QPT_RC && qp_type(qp) != IB_QPT_UC)
  144. goto done;
  145. if (unlikely(pkt->port_num != qp->attr.port_num)) {
  146. pr_warn_ratelimited("port %d != qp port %d\n",
  147. pkt->port_num, qp->attr.port_num);
  148. goto err1;
  149. }
  150. if (skb->protocol == htons(ETH_P_IP)) {
  151. struct in_addr *saddr =
  152. &qp->pri_av.sgid_addr._sockaddr_in.sin_addr;
  153. struct in_addr *daddr =
  154. &qp->pri_av.dgid_addr._sockaddr_in.sin_addr;
  155. if (ip_hdr(skb)->daddr != saddr->s_addr) {
  156. pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n",
  157. &ip_hdr(skb)->daddr,
  158. &saddr->s_addr);
  159. goto err1;
  160. }
  161. if (ip_hdr(skb)->saddr != daddr->s_addr) {
  162. pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n",
  163. &ip_hdr(skb)->saddr,
  164. &daddr->s_addr);
  165. goto err1;
  166. }
  167. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  168. struct in6_addr *saddr =
  169. &qp->pri_av.sgid_addr._sockaddr_in6.sin6_addr;
  170. struct in6_addr *daddr =
  171. &qp->pri_av.dgid_addr._sockaddr_in6.sin6_addr;
  172. if (memcmp(&ipv6_hdr(skb)->daddr, saddr, sizeof(*saddr))) {
  173. pr_warn_ratelimited("dst addr %pI6 != qp source addr %pI6\n",
  174. &ipv6_hdr(skb)->daddr, saddr);
  175. goto err1;
  176. }
  177. if (memcmp(&ipv6_hdr(skb)->saddr, daddr, sizeof(*daddr))) {
  178. pr_warn_ratelimited("source addr %pI6 != qp dst addr %pI6\n",
  179. &ipv6_hdr(skb)->saddr, daddr);
  180. goto err1;
  181. }
  182. }
  183. done:
  184. return 0;
  185. err1:
  186. return -EINVAL;
  187. }
  188. static int hdr_check(struct rxe_pkt_info *pkt)
  189. {
  190. struct rxe_dev *rxe = pkt->rxe;
  191. struct rxe_port *port = &rxe->port;
  192. struct rxe_qp *qp = NULL;
  193. u32 qpn = bth_qpn(pkt);
  194. int index;
  195. int err;
  196. if (unlikely(bth_tver(pkt) != BTH_TVER)) {
  197. pr_warn_ratelimited("bad tver\n");
  198. goto err1;
  199. }
  200. if (unlikely(qpn == 0)) {
  201. pr_warn_once("QP 0 not supported");
  202. goto err1;
  203. }
  204. if (qpn != IB_MULTICAST_QPN) {
  205. index = (qpn == 1) ? port->qp_gsi_index : qpn;
  206. qp = rxe_pool_get_index(&rxe->qp_pool, index);
  207. if (unlikely(!qp)) {
  208. pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn);
  209. goto err1;
  210. }
  211. err = check_type_state(rxe, pkt, qp);
  212. if (unlikely(err))
  213. goto err2;
  214. err = check_addr(rxe, pkt, qp);
  215. if (unlikely(err))
  216. goto err2;
  217. err = check_keys(rxe, pkt, qpn, qp);
  218. if (unlikely(err))
  219. goto err2;
  220. } else {
  221. if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) {
  222. pr_warn_ratelimited("no grh for mcast qpn\n");
  223. goto err1;
  224. }
  225. }
  226. pkt->qp = qp;
  227. return 0;
  228. err2:
  229. rxe_drop_ref(qp);
  230. err1:
  231. return -EINVAL;
  232. }
  233. static inline void rxe_rcv_pkt(struct rxe_dev *rxe,
  234. struct rxe_pkt_info *pkt,
  235. struct sk_buff *skb)
  236. {
  237. if (pkt->mask & RXE_REQ_MASK)
  238. rxe_resp_queue_pkt(rxe, pkt->qp, skb);
  239. else
  240. rxe_comp_queue_pkt(rxe, pkt->qp, skb);
  241. }
  242. static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb)
  243. {
  244. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  245. struct rxe_mc_grp *mcg;
  246. struct rxe_mc_elem *mce;
  247. struct rxe_qp *qp;
  248. union ib_gid dgid;
  249. int err;
  250. if (skb->protocol == htons(ETH_P_IP))
  251. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  252. (struct in6_addr *)&dgid);
  253. else if (skb->protocol == htons(ETH_P_IPV6))
  254. memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
  255. /* lookup mcast group corresponding to mgid, takes a ref */
  256. mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
  257. if (!mcg)
  258. goto err1; /* mcast group not registered */
  259. spin_lock_bh(&mcg->mcg_lock);
  260. list_for_each_entry(mce, &mcg->qp_list, qp_list) {
  261. qp = mce->qp;
  262. pkt = SKB_TO_PKT(skb);
  263. /* validate qp for incoming packet */
  264. err = check_type_state(rxe, pkt, qp);
  265. if (err)
  266. continue;
  267. err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
  268. if (err)
  269. continue;
  270. /* if *not* the last qp in the list
  271. * increase the users of the skb then post to the next qp
  272. */
  273. if (mce->qp_list.next != &mcg->qp_list)
  274. skb_get(skb);
  275. pkt->qp = qp;
  276. rxe_add_ref(qp);
  277. rxe_rcv_pkt(rxe, pkt, skb);
  278. }
  279. spin_unlock_bh(&mcg->mcg_lock);
  280. rxe_drop_ref(mcg); /* drop ref from rxe_pool_get_key. */
  281. err1:
  282. kfree_skb(skb);
  283. }
  284. static int rxe_match_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
  285. {
  286. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  287. const struct ib_gid_attr *gid_attr;
  288. union ib_gid dgid;
  289. union ib_gid *pdgid;
  290. if (pkt->mask & RXE_LOOPBACK_MASK)
  291. return 0;
  292. if (skb->protocol == htons(ETH_P_IP)) {
  293. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  294. (struct in6_addr *)&dgid);
  295. pdgid = &dgid;
  296. } else {
  297. pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
  298. }
  299. gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid,
  300. IB_GID_TYPE_ROCE_UDP_ENCAP,
  301. 1, skb->dev);
  302. if (IS_ERR(gid_attr))
  303. return PTR_ERR(gid_attr);
  304. rdma_put_gid_attr(gid_attr);
  305. return 0;
  306. }
  307. /* rxe_rcv is called from the interface driver */
  308. void rxe_rcv(struct sk_buff *skb)
  309. {
  310. int err;
  311. struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
  312. struct rxe_dev *rxe = pkt->rxe;
  313. __be32 *icrcp;
  314. u32 calc_icrc, pack_icrc;
  315. pkt->offset = 0;
  316. if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
  317. goto drop;
  318. if (rxe_match_dgid(rxe, skb) < 0) {
  319. pr_warn_ratelimited("failed matching dgid\n");
  320. goto drop;
  321. }
  322. pkt->opcode = bth_opcode(pkt);
  323. pkt->psn = bth_psn(pkt);
  324. pkt->qp = NULL;
  325. pkt->mask |= rxe_opcode[pkt->opcode].mask;
  326. if (unlikely(skb->len < header_size(pkt)))
  327. goto drop;
  328. err = hdr_check(pkt);
  329. if (unlikely(err))
  330. goto drop;
  331. /* Verify ICRC */
  332. icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
  333. pack_icrc = be32_to_cpu(*icrcp);
  334. calc_icrc = rxe_icrc_hdr(pkt, skb);
  335. calc_icrc = rxe_crc32(rxe, calc_icrc, (u8 *)payload_addr(pkt),
  336. payload_size(pkt) + bth_pad(pkt));
  337. calc_icrc = (__force u32)cpu_to_be32(~calc_icrc);
  338. if (unlikely(calc_icrc != pack_icrc)) {
  339. if (skb->protocol == htons(ETH_P_IPV6))
  340. pr_warn_ratelimited("bad ICRC from %pI6c\n",
  341. &ipv6_hdr(skb)->saddr);
  342. else if (skb->protocol == htons(ETH_P_IP))
  343. pr_warn_ratelimited("bad ICRC from %pI4\n",
  344. &ip_hdr(skb)->saddr);
  345. else
  346. pr_warn_ratelimited("bad ICRC from unknown\n");
  347. goto drop;
  348. }
  349. rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS);
  350. if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
  351. rxe_rcv_mcast_pkt(rxe, skb);
  352. else
  353. rxe_rcv_pkt(rxe, pkt, skb);
  354. return;
  355. drop:
  356. if (pkt->qp)
  357. rxe_drop_ref(pkt->qp);
  358. kfree_skb(skb);
  359. }