lag.c 3.6 KB

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