vxlan.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_VNI_MASK (VXLAN_VID_MASK << 8)
  86. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  87. struct vxlan_metadata {
  88. __be32 vni;
  89. u32 gbp;
  90. };
  91. struct vxlan_sock;
  92. typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb,
  93. struct vxlan_metadata *md);
  94. /* per UDP socket information */
  95. struct vxlan_sock {
  96. struct hlist_node hlist;
  97. vxlan_rcv_t *rcv;
  98. void *data;
  99. struct work_struct del_work;
  100. struct socket *sock;
  101. struct rcu_head rcu;
  102. struct hlist_head vni_list[VNI_HASH_SIZE];
  103. atomic_t refcnt;
  104. struct udp_offload udp_offloads;
  105. u32 flags;
  106. };
  107. #define VXLAN_F_LEARN 0x01
  108. #define VXLAN_F_PROXY 0x02
  109. #define VXLAN_F_RSC 0x04
  110. #define VXLAN_F_L2MISS 0x08
  111. #define VXLAN_F_L3MISS 0x10
  112. #define VXLAN_F_IPV6 0x20
  113. #define VXLAN_F_UDP_CSUM 0x40
  114. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  115. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  116. #define VXLAN_F_REMCSUM_TX 0x200
  117. #define VXLAN_F_REMCSUM_RX 0x400
  118. #define VXLAN_F_GBP 0x800
  119. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  120. /* Flags that are used in the receive path. These flags must match in
  121. * order for a socket to be shareable
  122. */
  123. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  124. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  125. VXLAN_F_REMCSUM_RX | \
  126. VXLAN_F_REMCSUM_NOPARTIAL)
  127. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  128. vxlan_rcv_t *rcv, void *data,
  129. bool no_share, u32 flags);
  130. void vxlan_sock_release(struct vxlan_sock *vs);
  131. int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  132. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  133. __be16 src_port, __be16 dst_port, struct vxlan_metadata *md,
  134. bool xnet, u32 vxflags);
  135. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  136. netdev_features_t features)
  137. {
  138. u8 l4_hdr = 0;
  139. if (!skb->encapsulation)
  140. return features;
  141. switch (vlan_get_protocol(skb)) {
  142. case htons(ETH_P_IP):
  143. l4_hdr = ip_hdr(skb)->protocol;
  144. break;
  145. case htons(ETH_P_IPV6):
  146. l4_hdr = ipv6_hdr(skb)->nexthdr;
  147. break;
  148. default:
  149. return features;;
  150. }
  151. if ((l4_hdr == IPPROTO_UDP) &&
  152. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  153. skb->inner_protocol != htons(ETH_P_TEB) ||
  154. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  155. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  156. return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
  157. return features;
  158. }
  159. /* IP header + UDP + VXLAN + Ethernet header */
  160. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  161. /* IPv6 header + UDP + VXLAN + Ethernet header */
  162. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  163. #if IS_ENABLED(CONFIG_VXLAN)
  164. void vxlan_get_rx_port(struct net_device *netdev);
  165. #else
  166. static inline void vxlan_get_rx_port(struct net_device *netdev)
  167. {
  168. }
  169. #endif
  170. #endif