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