lag.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2020 Mellanox Technologies. All rights reserved.
  4. */
  5. #include <rdma/ib_verbs.h>
  6. #include <rdma/ib_cache.h>
  7. #include <rdma/lag.h>
  8. static struct sk_buff *rdma_build_skb(struct net_device *netdev,
  9. struct rdma_ah_attr *ah_attr,
  10. gfp_t flags)
  11. {
  12. struct ipv6hdr *ip6h;
  13. struct sk_buff *skb;
  14. struct ethhdr *eth;
  15. struct iphdr *iph;
  16. struct udphdr *uh;
  17. u8 smac[ETH_ALEN];
  18. bool is_ipv4;
  19. int hdr_len;
  20. is_ipv4 = ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw);
  21. hdr_len = ETH_HLEN + sizeof(struct udphdr) + LL_RESERVED_SPACE(netdev);
  22. hdr_len += is_ipv4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr);
  23. skb = alloc_skb(hdr_len, flags);
  24. if (!skb)
  25. return NULL;
  26. skb->dev = netdev;
  27. skb_reserve(skb, hdr_len);
  28. skb_push(skb, sizeof(struct udphdr));
  29. skb_reset_transport_header(skb);
  30. uh = udp_hdr(skb);
  31. uh->source =
  32. htons(rdma_flow_label_to_udp_sport(ah_attr->grh.flow_label));
  33. uh->dest = htons(ROCE_V2_UDP_DPORT);
  34. uh->len = htons(sizeof(struct udphdr));
  35. if (is_ipv4) {
  36. skb_push(skb, sizeof(struct iphdr));
  37. skb_reset_network_header(skb);
  38. iph = ip_hdr(skb);
  39. iph->frag_off = 0;
  40. iph->version = 4;
  41. iph->protocol = IPPROTO_UDP;
  42. iph->ihl = 0x5;
  43. iph->tot_len = htons(sizeof(struct udphdr) + sizeof(struct
  44. iphdr));
  45. memcpy(&iph->saddr, ah_attr->grh.sgid_attr->gid.raw + 12,
  46. sizeof(struct in_addr));
  47. memcpy(&iph->daddr, ah_attr->grh.dgid.raw + 12,
  48. sizeof(struct in_addr));
  49. } else {
  50. skb_push(skb, sizeof(struct ipv6hdr));
  51. skb_reset_network_header(skb);
  52. ip6h = ipv6_hdr(skb);
  53. ip6h->version = 6;
  54. ip6h->nexthdr = IPPROTO_UDP;
  55. memcpy(&ip6h->flow_lbl, &ah_attr->grh.flow_label,
  56. sizeof(*ip6h->flow_lbl));
  57. memcpy(&ip6h->saddr, ah_attr->grh.sgid_attr->gid.raw,
  58. sizeof(struct in6_addr));
  59. memcpy(&ip6h->daddr, ah_attr->grh.dgid.raw,
  60. sizeof(struct in6_addr));
  61. }
  62. skb_push(skb, sizeof(struct ethhdr));
  63. skb_reset_mac_header(skb);
  64. eth = eth_hdr(skb);
  65. skb->protocol = eth->h_proto = htons(is_ipv4 ? ETH_P_IP : ETH_P_IPV6);
  66. rdma_read_gid_l2_fields(ah_attr->grh.sgid_attr, NULL, smac);
  67. memcpy(eth->h_source, smac, ETH_ALEN);
  68. memcpy(eth->h_dest, ah_attr->roce.dmac, ETH_ALEN);
  69. return skb;
  70. }
  71. static struct net_device *rdma_get_xmit_slave_udp(struct ib_device *device,
  72. struct net_device *master,
  73. struct rdma_ah_attr *ah_attr,
  74. gfp_t flags)
  75. {
  76. struct net_device *slave;
  77. struct sk_buff *skb;
  78. skb = rdma_build_skb(master, ah_attr, flags);
  79. if (!skb)
  80. return ERR_PTR(-ENOMEM);
  81. rcu_read_lock();
  82. slave = netdev_get_xmit_slave(master, skb,
  83. !!(device->lag_flags &
  84. RDMA_LAG_FLAGS_HASH_ALL_SLAVES));
  85. dev_hold(slave);
  86. rcu_read_unlock();
  87. kfree_skb(skb);
  88. return slave;
  89. }
  90. void rdma_lag_put_ah_roce_slave(struct net_device *xmit_slave)
  91. {
  92. dev_put(xmit_slave);
  93. }
  94. struct net_device *rdma_lag_get_ah_roce_slave(struct ib_device *device,
  95. struct rdma_ah_attr *ah_attr,
  96. gfp_t flags)
  97. {
  98. struct net_device *slave = NULL;
  99. struct net_device *master;
  100. if (!(ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE &&
  101. ah_attr->grh.sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP &&
  102. ah_attr->grh.flow_label))
  103. return NULL;
  104. rcu_read_lock();
  105. master = rdma_read_gid_attr_ndev_rcu(ah_attr->grh.sgid_attr);
  106. if (IS_ERR(master)) {
  107. rcu_read_unlock();
  108. return master;
  109. }
  110. dev_hold(master);
  111. rcu_read_unlock();
  112. if (!netif_is_bond_master(master))
  113. goto put;
  114. slave = rdma_get_xmit_slave_udp(device, master, ah_attr, flags);
  115. put:
  116. dev_put(master);
  117. return slave;
  118. }