vxlan.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include <net/dst_metadata.h>
  10. #define VNI_HASH_BITS 10
  11. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  12. /*
  13. * VXLAN Group Based Policy Extension:
  14. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  15. * |1|-|-|-|1|-|-|-|R|D|R|R|A|R|R|R| Group Policy ID |
  16. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  17. * | VXLAN Network Identifier (VNI) | Reserved |
  18. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. *
  20. * D = Don't Learn bit. When set, this bit indicates that the egress
  21. * VTEP MUST NOT learn the source address of the encapsulated frame.
  22. *
  23. * A = Indicates that the group policy has already been applied to
  24. * this packet. Policies MUST NOT be applied by devices when the
  25. * A bit is set.
  26. *
  27. * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy
  28. */
  29. struct vxlanhdr_gbp {
  30. __u8 vx_flags;
  31. #ifdef __LITTLE_ENDIAN_BITFIELD
  32. __u8 reserved_flags1:3,
  33. policy_applied:1,
  34. reserved_flags2:2,
  35. dont_learn:1,
  36. reserved_flags3:1;
  37. #elif defined(__BIG_ENDIAN_BITFIELD)
  38. __u8 reserved_flags1:1,
  39. dont_learn:1,
  40. reserved_flags2:2,
  41. policy_applied:1,
  42. reserved_flags3:3;
  43. #else
  44. #error "Please fix <asm/byteorder.h>"
  45. #endif
  46. __be16 policy_id;
  47. __be32 vx_vni;
  48. };
  49. #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF)
  50. /* skb->mark mapping
  51. *
  52. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  54. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. */
  56. #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
  57. #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
  58. #define VXLAN_GBP_ID_MASK (0xFFFF)
  59. /* VXLAN protocol header:
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. * |G|R|R|R|I|R|R|C| Reserved |
  62. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. * | VXLAN Network Identifier (VNI) | Reserved |
  64. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  65. *
  66. * G = 1 Group Policy (VXLAN-GBP)
  67. * I = 1 VXLAN Network Identifier (VNI) present
  68. * C = 1 Remote checksum offload (RCO)
  69. */
  70. struct vxlanhdr {
  71. __be32 vx_flags;
  72. __be32 vx_vni;
  73. };
  74. /* VXLAN header flags. */
  75. #define VXLAN_HF_RCO BIT(24)
  76. #define VXLAN_HF_VNI BIT(27)
  77. #define VXLAN_HF_GBP BIT(31)
  78. /* Remote checksum offload header option */
  79. #define VXLAN_RCO_MASK 0x7f /* Last byte of vni field */
  80. #define VXLAN_RCO_UDP 0x80 /* Indicate UDP RCO (TCP when not set *) */
  81. #define VXLAN_RCO_SHIFT 1 /* Left shift of start */
  82. #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
  83. #define VXLAN_MAX_REMCSUM_START (VXLAN_RCO_MASK << VXLAN_RCO_SHIFT)
  84. #define VXLAN_N_VID (1u << 24)
  85. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  86. #define VXLAN_VNI_MASK (VXLAN_VID_MASK << 8)
  87. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  88. #define VNI_HASH_BITS 10
  89. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  90. #define FDB_HASH_BITS 8
  91. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  92. struct vxlan_metadata {
  93. u32 gbp;
  94. };
  95. /* per UDP socket information */
  96. struct vxlan_sock {
  97. struct hlist_node hlist;
  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. union vxlan_addr {
  107. struct sockaddr_in sin;
  108. struct sockaddr_in6 sin6;
  109. struct sockaddr sa;
  110. };
  111. struct vxlan_rdst {
  112. union vxlan_addr remote_ip;
  113. __be16 remote_port;
  114. u32 remote_vni;
  115. u32 remote_ifindex;
  116. struct list_head list;
  117. struct rcu_head rcu;
  118. };
  119. struct vxlan_config {
  120. union vxlan_addr remote_ip;
  121. union vxlan_addr saddr;
  122. u32 vni;
  123. int remote_ifindex;
  124. int mtu;
  125. __be16 dst_port;
  126. __u16 port_min;
  127. __u16 port_max;
  128. __u8 tos;
  129. __u8 ttl;
  130. u32 flags;
  131. unsigned long age_interval;
  132. unsigned int addrmax;
  133. bool no_share;
  134. };
  135. /* Pseudo network device */
  136. struct vxlan_dev {
  137. struct hlist_node hlist; /* vni hash table */
  138. struct list_head next; /* vxlan's per namespace list */
  139. struct vxlan_sock *vn_sock; /* listening socket */
  140. struct net_device *dev;
  141. struct net *net; /* netns for packet i/o */
  142. struct vxlan_rdst default_dst; /* default destination */
  143. u32 flags; /* VXLAN_F_* in vxlan.h */
  144. struct timer_list age_timer;
  145. spinlock_t hash_lock;
  146. unsigned int addrcnt;
  147. struct gro_cells gro_cells;
  148. struct vxlan_config cfg;
  149. struct hlist_head fdb_head[FDB_HASH_SIZE];
  150. };
  151. #define VXLAN_F_LEARN 0x01
  152. #define VXLAN_F_PROXY 0x02
  153. #define VXLAN_F_RSC 0x04
  154. #define VXLAN_F_L2MISS 0x08
  155. #define VXLAN_F_L3MISS 0x10
  156. #define VXLAN_F_IPV6 0x20
  157. #define VXLAN_F_UDP_CSUM 0x40
  158. #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
  159. #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
  160. #define VXLAN_F_REMCSUM_TX 0x200
  161. #define VXLAN_F_REMCSUM_RX 0x400
  162. #define VXLAN_F_GBP 0x800
  163. #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
  164. #define VXLAN_F_COLLECT_METADATA 0x2000
  165. /* Flags that are used in the receive path. These flags must match in
  166. * order for a socket to be shareable
  167. */
  168. #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
  169. VXLAN_F_UDP_ZERO_CSUM6_RX | \
  170. VXLAN_F_REMCSUM_RX | \
  171. VXLAN_F_REMCSUM_NOPARTIAL | \
  172. VXLAN_F_COLLECT_METADATA)
  173. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  174. u8 name_assign_type, struct vxlan_config *conf);
  175. static inline __be16 vxlan_dev_dst_port(struct vxlan_dev *vxlan)
  176. {
  177. return inet_sk(vxlan->vn_sock->sock->sk)->inet_sport;
  178. }
  179. static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
  180. netdev_features_t features)
  181. {
  182. u8 l4_hdr = 0;
  183. if (!skb->encapsulation)
  184. return features;
  185. switch (vlan_get_protocol(skb)) {
  186. case htons(ETH_P_IP):
  187. l4_hdr = ip_hdr(skb)->protocol;
  188. break;
  189. case htons(ETH_P_IPV6):
  190. l4_hdr = ipv6_hdr(skb)->nexthdr;
  191. break;
  192. default:
  193. return features;;
  194. }
  195. if ((l4_hdr == IPPROTO_UDP) &&
  196. (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  197. skb->inner_protocol != htons(ETH_P_TEB) ||
  198. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  199. sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
  200. return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
  201. return features;
  202. }
  203. /* IP header + UDP + VXLAN + Ethernet header */
  204. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  205. /* IPv6 header + UDP + VXLAN + Ethernet header */
  206. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  207. #if IS_ENABLED(CONFIG_VXLAN)
  208. void vxlan_get_rx_port(struct net_device *netdev);
  209. #else
  210. static inline void vxlan_get_rx_port(struct net_device *netdev)
  211. {
  212. }
  213. #endif
  214. static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
  215. {
  216. return vs->sock->sk->sk_family;
  217. }
  218. #endif