nat: use SVR
[vpp.git] / src / vnet / vxlan-gbp / vxlan_gbp.h
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_vnet_vxlan_gbp_h
16 #define included_vnet_vxlan_gbp_h
17
18 #include <vppinfra/error.h>
19 #include <vppinfra/hash.h>
20 #include <vppinfra/bihash_16_8.h>
21 #include <vppinfra/bihash_24_8.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/l2/l2_input.h>
25 #include <vnet/l2/l2_output.h>
26 #include <vnet/l2/l2_bd.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
29 #include <vnet/ip/ip4_packet.h>
30 #include <vnet/ip/ip6_packet.h>
31 #include <vnet/udp/udp.h>
32 #include <vnet/dpo/dpo.h>
33 #include <vnet/adj/adj_types.h>
34
35 /* *INDENT-OFF* */
36 typedef CLIB_PACKED (struct {
37   ip4_header_t ip4;     /* 20 bytes */
38   udp_header_t udp;     /* 8 bytes */
39   vxlan_gbp_header_t vxlan_gbp; /* 8 bytes */
40 }) ip4_vxlan_gbp_header_t;
41
42 typedef CLIB_PACKED (struct {
43   ip6_header_t ip6;     /* 40 bytes */
44   udp_header_t udp;     /* 8 bytes */
45   vxlan_gbp_header_t vxlan_gbp; /* 8 bytes */
46 }) ip6_vxlan_gbp_header_t;
47 /* *INDENT-ON* */
48
49 /*
50 * Key fields: remote ip, vni on incoming VXLAN packet
51 * all fields in NET byte order
52 */
53 typedef clib_bihash_kv_16_8_t vxlan4_gbp_tunnel_key_t;
54
55 /*
56 * Key fields: remote ip, vni and fib index on incoming VXLAN packet
57 * ip, vni fields in NET byte order
58 * fib index field in host byte order
59 */
60 typedef clib_bihash_kv_24_8_t vxlan6_gbp_tunnel_key_t;
61
62 typedef enum vxlan_gbp_tunnel_mode_t_
63 {
64   VXLAN_GBP_TUNNEL_MODE_L2,
65   VXLAN_GBP_TUNNEL_MODE_L3,
66 } vxlan_gbp_tunnel_mode_t;
67
68 extern u8 *format_vxlan_gbp_tunnel_mode (u8 * s, va_list * args);
69
70 typedef struct
71 {
72   /* Required for pool_get_aligned */
73   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
74
75   /* FIB DPO for IP forwarding of VXLAN encap packet */
76   dpo_id_t next_dpo;
77
78   /* flags */
79   u16 flags;
80
81   /* vxlan VNI in HOST byte order */
82   u32 vni;
83
84   /* tunnel src and dst addresses */
85   ip46_address_t src;
86   ip46_address_t dst;
87
88   /* mcast packet output intfc index (used only if dst is mcast) */
89   u32 mcast_sw_if_index;
90
91   /* The FIB index for src/dst addresses */
92   u32 encap_fib_index;
93
94   /* vnet intfc index */
95   u32 sw_if_index;
96   u32 hw_if_index;
97
98   /** Next node after VxLAN-GBP encap */
99   uword encap_next_node;
100
101   /**
102    * Tunnel mode.
103    * L2 tunnels decap to L2 path, L3 tunnels to the L3 path
104    */
105   vxlan_gbp_tunnel_mode_t mode;
106
107   /**
108    * Linkage into the FIB object graph
109    */
110   fib_node_t node;
111
112   /*
113    * The FIB entry for (depending on VXLAN-GBP tunnel is unicast or mcast)
114    * sending unicast VXLAN-GBP encap packets or receiving mcast VXLAN-GBP packets
115    */
116   fib_node_index_t fib_entry_index;
117   adj_index_t mcast_adj_index;
118
119   /**
120    * The tunnel is a child of the FIB entry for its destination. This is
121    * so it receives updates when the forwarding information for that entry
122    * changes.
123    * The tunnels sibling index on the FIB entry's dependency list.
124    */
125   u32 sibling_index;
126
127   u32 dev_instance;             /* Real device instance in tunnel vector */
128   u32 user_instance;            /* Instance name being shown to user */
129
130
131     VNET_DECLARE_REWRITE;
132 } vxlan_gbp_tunnel_t;
133
134 #define foreach_vxlan_gbp_input_next         \
135   _(DROP, "error-drop")                      \
136   _(PUNT, "punt-dispatch")                   \
137   _(L2_INPUT, "l2-input")                    \
138   _(IP4_INPUT, "ip4-input")                  \
139   _(IP6_INPUT, "ip6-input")
140
141 typedef enum
142 {
143 #define _(s,n) VXLAN_GBP_INPUT_NEXT_##s,
144   foreach_vxlan_gbp_input_next
145 #undef _
146     VXLAN_GBP_INPUT_N_NEXT,
147 } vxlan_gbp_input_next_t;
148
149 typedef enum
150 {
151 #define vxlan_gbp_error(n,s) VXLAN_GBP_ERROR_##n,
152 #include <vnet/vxlan-gbp/vxlan_gbp_error.def>
153 #undef vxlan_gbp_error
154   VXLAN_GBP_N_ERROR,
155 } vxlan_gbp_input_error_t;
156
157 /**
158  * Call back function packets that do not match a configured tunnel
159  */
160 typedef vxlan_gbp_input_next_t (*vxlan_bgp_no_tunnel_t) (vlib_buffer_t * b,
161                                                          u32 thread_index,
162                                                          u8 is_ip6);
163
164 typedef struct
165 {
166   /* vector of encap tunnel instances */
167   vxlan_gbp_tunnel_t *tunnels;
168
169   /* lookup tunnel by key */
170   clib_bihash_16_8_t vxlan4_gbp_tunnel_by_key;  /* keyed on ipv4.dst + fib + vni */
171   clib_bihash_24_8_t vxlan6_gbp_tunnel_by_key;  /* keyed on ipv6.dst + fib + vni */
172
173   /* local VTEP IPs ref count used by vxlan-bypass node to check if
174      received VXLAN packet DIP matches any local VTEP address */
175   uword *vtep4;                 /* local ip4 VTEPs keyed on their ip4 addr */
176   uword *vtep6;                 /* local ip6 VTEPs keyed on their ip6 addr */
177
178   /* mcast shared info */
179   uword *mcast_shared;          /* keyed on mcast ip46 addr */
180
181   /* Mapping from sw_if_index to tunnel index */
182   u32 *tunnel_index_by_sw_if_index;
183
184   /* On demand udp port registration */
185   u32 udp_ports_registered;
186
187   /* convenience */
188   vlib_main_t *vlib_main;
189   vnet_main_t *vnet_main;
190
191   /* Record used instances */
192   uword *instance_used;
193
194   /**
195    * Punt reasons for no such tunnel
196    */
197   vlib_punt_reason_t punt_no_such_tunnel[FIB_PROTOCOL_IP_MAX];
198 } vxlan_gbp_main_t;
199
200 extern vxlan_gbp_main_t vxlan_gbp_main;
201
202 extern vlib_node_registration_t vxlan4_gbp_input_node;
203 extern vlib_node_registration_t vxlan6_gbp_input_node;
204 extern vlib_node_registration_t vxlan4_gbp_encap_node;
205 extern vlib_node_registration_t vxlan6_gbp_encap_node;
206 extern void vxlan_gbp_register_udp_ports (void);
207 extern void vxlan_gbp_unregister_udp_ports (void);
208
209 u8 *format_vxlan_gbp_encap_trace (u8 * s, va_list * args);
210
211 typedef struct
212 {
213   u8 is_add;
214   u8 is_ip6;
215   u32 instance;
216   vxlan_gbp_tunnel_mode_t mode;
217   ip46_address_t src, dst;
218   u32 mcast_sw_if_index;
219   u32 encap_fib_index;
220   u32 vni;
221 } vnet_vxlan_gbp_tunnel_add_del_args_t;
222
223 int vnet_vxlan_gbp_tunnel_add_del
224   (vnet_vxlan_gbp_tunnel_add_del_args_t * a, u32 * sw_if_indexp);
225 int vnet_vxlan_gbp_tunnel_del (u32 sw_if_indexp);
226
227 void vnet_int_vxlan_gbp_bypass_mode (u32 sw_if_index, u8 is_ip6,
228                                      u8 is_enable);
229
230 always_inline u32
231 vxlan_gbp_tunnel_by_sw_if_index (u32 sw_if_index)
232 {
233   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
234
235   if (sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index))
236     return ~0;
237
238   return (vxm->tunnel_index_by_sw_if_index[sw_if_index]);
239 }
240
241 #endif /* included_vnet_vxlan_gbp_h */
242
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */