rxe_icrc.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "rxe.h"
  34. #include "rxe_loc.h"
  35. /* Compute a partial ICRC for all the IB transport headers. */
  36. u32 rxe_icrc_hdr(struct rxe_pkt_info *pkt, struct sk_buff *skb)
  37. {
  38. unsigned int bth_offset = 0;
  39. struct iphdr *ip4h = NULL;
  40. struct ipv6hdr *ip6h = NULL;
  41. struct udphdr *udph;
  42. struct rxe_bth *bth;
  43. int crc;
  44. int length;
  45. int hdr_size = sizeof(struct udphdr) +
  46. (skb->protocol == htons(ETH_P_IP) ?
  47. sizeof(struct iphdr) : sizeof(struct ipv6hdr));
  48. /* pseudo header buffer size is calculate using ipv6 header size since
  49. * it is bigger than ipv4
  50. */
  51. u8 pshdr[sizeof(struct udphdr) +
  52. sizeof(struct ipv6hdr) +
  53. RXE_BTH_BYTES];
  54. /* This seed is the result of computing a CRC with a seed of
  55. * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
  56. */
  57. crc = 0xdebb20e3;
  58. if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
  59. memcpy(pshdr, ip_hdr(skb), hdr_size);
  60. ip4h = (struct iphdr *)pshdr;
  61. udph = (struct udphdr *)(ip4h + 1);
  62. ip4h->ttl = 0xff;
  63. ip4h->check = CSUM_MANGLED_0;
  64. ip4h->tos = 0xff;
  65. } else { /* IPv6 */
  66. memcpy(pshdr, ipv6_hdr(skb), hdr_size);
  67. ip6h = (struct ipv6hdr *)pshdr;
  68. udph = (struct udphdr *)(ip6h + 1);
  69. memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
  70. ip6h->priority = 0xf;
  71. ip6h->hop_limit = 0xff;
  72. }
  73. udph->check = CSUM_MANGLED_0;
  74. bth_offset += hdr_size;
  75. memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
  76. bth = (struct rxe_bth *)&pshdr[bth_offset];
  77. /* exclude bth.resv8a */
  78. bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
  79. length = hdr_size + RXE_BTH_BYTES;
  80. crc = crc32_le(crc, pshdr, length);
  81. /* And finish to compute the CRC on the remainder of the headers. */
  82. crc = crc32_le(crc, pkt->hdr + RXE_BTH_BYTES,
  83. rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
  84. return crc;
  85. }