vxlan.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef __NET_VXLAN_H
  2. #define __NET_VXLAN_H 1
  3. #include <linux/ip.h>
  4. #include <linux/ipv6.h>
  5. #include <linux/if_vlan.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/udp.h>
  9. #define VNI_HASH_BITS 10
  10. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  11. /*
  12. * VXLAN Group Based Policy Extension:
  13. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  14. * |1|-|-|-|1|-|-|-|R|D|R|R|A|R|R|R| Group Policy ID |
  15. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  16. * | VXLAN Network Identifier (VNI) | Reserved |
  17. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  18. *
  19. * D = Don't Learn bit. When set, this bit indicates that the egress
  20. * VTEP MUST NOT learn the source address of the encapsulated frame.
  21. *
  22. * A = Indicates that the group policy has already been applied to
  23. * this packet. Policies MUST NOT be applied by devices when the
  24. * A bit is set.
  25. *
  26. * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy
  27. */
  28. struct vxlanhdr_gbp {
  29. __u8 vx_flags;
  30. #ifdef __LITTLE_ENDIAN_BITFIELD
  31. __u8 reserved_flags1:3,
  32. policy_applied:1,
  33. reserved_flags2:2,
  34. dont_learn:1,
  35. reserved_flags3:1;
  36. #elif defined(__BIG_ENDIAN_BITFIELD)
  37. __u8 reserved_flags1:1,
  38. dont_learn:1,
  39. reserved_flags2:2,
  40. policy_applied:1,
  41. reserved_flags3:3;
  42. #else
  43. #error "Please fix <asm/byteorder.h>"
  44. #endif
  45. __be16 policy_id;
  46. __be32 vx_vni;
  47. };
  48. #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF)
  49. /* skb->mark mapping
  50. *
  51. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  52. * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  53. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  54. */
  55. #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
  56. #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
  57. #define VXLAN_GBP_ID_MASK (0xFFFF)
  58. /* VXLAN protocol header:
  59. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. * |G|R|R|R|I|R|R|C| Reserved |
  61. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. * | VXLAN Network Identifier (VNI) | Reserved |
  63. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  64. *
  65. * G = 1 Group Policy (VXLAN-GBP)
  66. * I = 1 VXLAN Network Identifier (VNI) present
  67. * C = 1 Remote checksum offload (RCO)
  68. */
  69. struct vxlanhdr {
  70. __be32 vx_flags;
  71. __be32 vx_vni;
  72. };
  73. /* VXLAN header flags. */
  74. #define VXLAN_HF_RCO BIT(24)
  75. #define VXLAN_HF_VNI BIT(27)
  76. #define VXLAN_HF_GBP BIT(31)
  77. /* Remote checksum offload header option */
  78. #define VXLAN_RCO_MASK 0x7f /* Last byte of vni field */
  79. #define VXLAN_RCO_UDP 0x80 /* Indicate UDP RCO (TCP when not set *) */
  80. #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
  81. #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
  82. #define VXLAN_MAX_REMCSUM_START (VXLAN_RCO_MASK << VXLAN_RCO_SHIFT)
  83. #define VXLAN_N_VID (1u << 24)
  84. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  85. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  86. struct vxlan_metadata {
  87. __be32 vni;
  88. u32 gbp;
  89. };
  90. struct vxlan_sock;
  91. typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb,
  92. struct vxlan_metadata *md);
  93. /* per UDP socket information */
  94. struct vxlan_sock {
  95. struct hlist_node hlist;
  96. vxlan_rcv_t *rcv;
  97. void *data;
  98. struct work_struct del_work;
  99. struct socket *sock;
  100. struct rcu_head rcu;
  101. struct hlist_head vni_list[VNI_HASH_SIZE];
  102. atomic_t refcnt;
  103. struct udp_offload udp_offloads;
  104. u32 flags;
  105. };
  106. #define VXLAN_F_LEARN 0x01
  107. #define VXLAN_F_PROXY 0x02
  108. #define VXLAN_F_RSC 0x04
  109. #define VXLAN_F_L2MISS 0x08
  110. #define VXLAN_F_L3MISS 0x10
  111. #define VXLAN_F_IPV6 0x20
  112. #define VXLAN_F_UDP_CSUM 0x40
  113. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  114. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  115. #define VXLAN_F_REMCSUM_TX 0x200
  116. #define VXLAN_F_REMCSUM_RX 0x400
  117. #define VXLAN_F_GBP 0x800
  118. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  119. /* Flags that are used in the receive patch. These flags must match in
  120. * order for a socket to be shareable
  121. */
  122. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  123. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  124. VXLAN_F_REMCSUM_RX | \
  125. VXLAN_F_REMCSUM_NOPARTIAL)
  126. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  127. vxlan_rcv_t *rcv, void *data,
  128. bool no_share, u32 flags);
  129. void vxlan_sock_release(struct vxlan_sock *vs);
  130. int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
  131. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  132. __be16 src_port, __be16 dst_port, struct vxlan_metadata *md,
  133. bool xnet, u32 vxflags);
  134. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  135. netdev_features_t features)
  136. {
  137. u8 l4_hdr = 0;
  138. if (!skb->encapsulation)
  139. return features;
  140. switch (vlan_get_protocol(skb)) {
  141. case htons(ETH_P_IP):
  142. l4_hdr = ip_hdr(skb)->protocol;
  143. break;
  144. case htons(ETH_P_IPV6):
  145. l4_hdr = ipv6_hdr(skb)->nexthdr;
  146. break;
  147. default:
  148. return features;;
  149. }
  150. if ((l4_hdr == IPPROTO_UDP) &&
  151. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  152. skb->inner_protocol != htons(ETH_P_TEB) ||
  153. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  154. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  155. return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
  156. return features;
  157. }
  158. /* IP header + UDP + VXLAN + Ethernet header */
  159. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  160. /* IPv6 header + UDP + VXLAN + Ethernet header */
  161. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  162. #if IS_ENABLED(CONFIG_VXLAN)
  163. void vxlan_get_rx_port(struct net_device *netdev);
  164. #else
  165. static inline void vxlan_get_rx_port(struct net_device *netdev)
  166. {
  167. }
  168. #endif
  169. #endif