L2 over LISP and GRE (VPP-457)
[vpp.git] / vnet / vnet / mpls / mpls.h
1 /*
2  * Copyright (c) 2015 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_mpls_gre_h
16 #define included_vnet_mpls_gre_h
17
18 #include <vnet/vnet.h>
19 #include <vnet/gre/gre.h>
20 #include <vnet/mpls/packet.h>
21 #include <vnet/mpls/mpls_types.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/fib/fib_node.h>
25 #include <vnet/adj/adj.h>
26
27 typedef CLIB_PACKED (struct {
28   ip4_header_t ip4;             /* 20 bytes */
29   gre_header_t gre;             /* 4 bytes */
30   mpls_unicast_header_t labels[0];   /* 4 bytes each */
31 }) ip4_gre_and_mpls_header_t;
32
33 extern vnet_hw_interface_class_t mpls_gre_hw_interface_class;
34
35 typedef enum {
36 #define mpls_error(n,s) MPLS_ERROR_##n,
37 #include <vnet/mpls/error.def>
38 #undef mpls_error
39   MPLS_N_ERROR,
40 } mpls_gre_error_t;
41
42 /* 
43  * No protocol info, MPLS labels don't have a next-header field 
44  * presumably the label field tells all...
45  */
46
47 typedef struct {
48   fib_node_t mgt_node;
49   ip4_address_t tunnel_src;
50   ip4_address_t tunnel_dst;
51   ip4_address_t intfc_address;
52   u32 mask_width;
53   u32 inner_fib_index;
54   u32 outer_fib_index;
55   u32 encap_index;
56   u32 hw_if_index;              /* L2 x-connect capable tunnel intfc */
57   u8 * rewrite_data;
58   u8 l2_only;
59   fib_node_index_t fei; /* FIB Entry index for the tunnel's destination */
60   adj_index_t adj_index; /* The midchain adj this tunnel creates */
61   u32 sibling_index;
62 } mpls_gre_tunnel_t;
63
64 typedef struct {
65   u8 tunnel_dst[6];
66   ip4_address_t intfc_address;
67   u32 tx_sw_if_index;
68   u32 inner_fib_index;
69   u32 mask_width;
70   u32 encap_index;
71   u32 hw_if_index;
72   u8 * rewrite_data;
73   u8 l2_only;
74   fib_node_index_t fei;
75 } mpls_eth_tunnel_t;
76
77 typedef struct {
78   mpls_unicast_header_t *labels;
79   /* only for policy tunnels */
80   u8 * rewrite;
81   u32 output_next_index;
82 } mpls_encap_t;
83
84 typedef struct {
85   u32 tx_fib_index;
86   u32 next_index;               /* e.g. ip4/6-input, l2-input */
87 } mpls_decap_t;
88
89 #define MPLS_FIB_DEFAULT_TABLE_ID 0
90
91 /**
92  * Type exposure is to allow the DP fast/inlined access
93  */
94 #define MPLS_FIB_KEY_SIZE 21
95 #define MPLS_FIB_DB_SIZE (1 << (MPLS_FIB_KEY_SIZE-1))
96
97 typedef struct mpls_fib_t_
98 {
99   /**
100    * A hash table of entries. 21 bit key
101    * Hash table for reduced memory footprint
102    */
103   uword * mf_entries; 
104
105   /**
106    * The load-balance indeices keyed by 21 bit label+eos bit.
107    * A flat array for maximum lookup performace.
108    */
109   index_t mf_lbs[MPLS_FIB_DB_SIZE];
110 } mpls_fib_t;
111
112 /**
113  * @brief Definition of a callback for receiving MPLS interface state change
114  * notifications
115  */
116 typedef void (*mpls_interface_state_change_callback_t)(u32 sw_if_index,
117                                                        u32 is_enable);
118
119 typedef struct {
120   /* MPLS FIB index for each software interface */
121   u32 *fib_index_by_sw_if_index;
122
123   /**  A pool of all the MPLS FIBs */
124   struct fib_table_t_ *fibs;
125
126   /** A hash table to lookup the mpls_fib by table ID */
127   uword *fib_index_by_table_id;
128
129   /* rx/tx interface/feature configuration. */
130   ip_config_main_t feature_config_mains[VNET_N_IP_FEAT];
131
132   /* Built-in unicast feature path indices, see ip_feature_init_cast(...)  */
133   u32 mpls_rx_feature_lookup;
134   u32 mpls_rx_feature_not_enabled;
135   u32 mpls_tx_feature_interface_output;
136
137   /* pool of gre tunnel instances */
138   mpls_gre_tunnel_t *gre_tunnels;
139   u32 * free_gre_sw_if_indices;
140
141   /* pool of ethernet tunnel instances */
142   mpls_eth_tunnel_t *eth_tunnels;
143   u32 * free_eth_sw_if_indices;
144
145   /* Encap side: map (fib, dst_address) to mpls label stack */
146   mpls_encap_t * encaps;
147   uword * mpls_encap_by_fib_and_dest;
148
149   /* Decap side: map rx label to FIB */
150   mpls_decap_t * decaps;
151   uword * mpls_decap_by_rx_fib_and_label;
152
153   /* mpls-o-e policy tunnel next index for ip4/ip6-classify */
154   u32 ip4_classify_mpls_policy_encap_next_index;
155   u32 ip6_classify_mpls_policy_encap_next_index;
156
157   /* feature path configuration lists */
158   vnet_ip_feature_registration_t * next_feature[VNET_N_IP_FEAT];
159
160   /* Save feature results for show command */
161   char **feature_nodes[VNET_N_IP_FEAT];
162
163   /* IP4 enabled count by software interface */
164   u8 * mpls_enabled_by_sw_if_index;
165
166   /* Functions to call when MPLS state on an interface changes. */
167   mpls_interface_state_change_callback_t * mpls_interface_state_change_callbacks;
168
169   /* convenience */
170   vlib_main_t * vlib_main;
171   vnet_main_t * vnet_main;
172 } mpls_main_t;
173
174 extern mpls_main_t mpls_main;
175
176 #define VNET_MPLS_FEATURE_INIT(x,...)                           \
177   __VA_ARGS__ vnet_ip_feature_registration_t uc_##x;            \
178 static void __vnet_add_feature_registration_uc_##x (void)       \
179   __attribute__((__constructor__)) ;                            \
180 static void __vnet_add_feature_registration_uc_##x (void)       \
181 {                                                               \
182   mpls_main_t * mm = &mpls_main;                                \
183   uc_##x.next = mm->next_feature[VNET_IP_RX_UNICAST_FEAT];      \
184   mm->next_feature[VNET_IP_RX_UNICAST_FEAT] = &uc_##x;          \
185 }                                                               \
186 __VA_ARGS__ vnet_ip_feature_registration_t uc_##x
187
188 #define VNET_MPLS_TX_FEATURE_INIT(x,...)                        \
189   __VA_ARGS__ vnet_ip_feature_registration_t tx_##x;            \
190 static void __vnet_add_feature_registration_tx_##x (void)       \
191   __attribute__((__constructor__)) ;                            \
192 static void __vnet_add_feature_registration_tx_##x (void)       \
193 {                                                               \
194   mpls_main_t * mm = &mpls_main;                                \
195   tx_##x.next = mm->next_feature[VNET_IP_TX_FEAT];              \
196   mm->next_feature[VNET_IP_TX_FEAT] = &tx_##x;                  \
197 }                                                               \
198 __VA_ARGS__ vnet_ip_feature_registration_t tx_##x 
199
200 extern clib_error_t * mpls_feature_init(vlib_main_t * vm);
201
202 format_function_t format_mpls_protocol;
203 format_function_t format_mpls_gre_header_with_length;
204 format_function_t format_mpls_eth_header_with_length;
205 format_function_t format_mpls_encap_index;
206
207 format_function_t format_mpls_eos_bit;
208 format_function_t format_mpls_unicast_header_net_byte_order;
209 format_function_t format_mpls_unicast_label;
210 format_function_t format_mpls_header;
211
212 extern vlib_node_registration_t mpls_input_node;
213 extern vlib_node_registration_t mpls_policy_encap_node;
214 extern vlib_node_registration_t mpls_output_node;
215 extern vlib_node_registration_t mpls_midchain_node;
216
217 extern vnet_device_class_t mpls_gre_device_class;
218
219 /* Parse mpls protocol as 0xXXXX or protocol name.
220    In either host or network byte order. */
221 unformat_function_t unformat_mpls_protocol_host_byte_order;
222 unformat_function_t unformat_mpls_protocol_net_byte_order;
223 unformat_function_t unformat_mpls_label_net_byte_order;
224 unformat_function_t unformat_mpls_gre_header;
225 unformat_function_t unformat_pg_mpls_gre_header;
226 unformat_function_t unformat_mpls_unicast_label;
227
228 /* Parse mpls header. */
229 unformat_function_t unformat_mpls_header;
230 unformat_function_t unformat_pg_mpls_header;
231
232 /* manually added to the interface output node in mpls.c */
233 #define MPLS_GRE_OUTPUT_NEXT_LOOKUP     1
234 #define MPLS_GRE_OUTPUT_NEXT_DROP       VNET_INTERFACE_TX_NEXT_DROP
235
236 void mpls_sw_interface_enable_disable (mpls_main_t * mm,
237                                        u32 sw_if_index,
238                                        u8 is_enable);
239
240 u8 mpls_sw_interface_is_enabled (u32 sw_if_index);
241
242 mpls_encap_t * 
243 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
244
245 int mpls_label_from_fib_id_and_dest (mpls_main_t *gm, u32 fib_id,
246                                      u32 dst_address, u32 *labelp);
247
248 int vnet_mpls_gre_add_del_tunnel (ip4_address_t *src,
249                                   ip4_address_t *dst,
250                                   ip4_address_t *intfc,
251                                   u32 mask_width,
252                                   u32 inner_fib_id, u32 outer_fib_id,
253                                   u32 * tunnel_intfc_sw_if_index,
254                                   u8 l2_only,
255                                   u8 is_add);
256
257 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
258                                        ip4_address_t *intfc,
259                                        u32 mask_width,
260                                        u32 inner_fib_id, 
261                                        u32 tx_sw_if_index,
262                                        u32 * tunnel_sw_if_index,
263                                        u8 l2_only,
264                                        u8 is_add);
265
266 int vnet_mpls_gre_delete_fib_tunnels (u32 fib_id);
267
268 int mpls_fib_reset_labels (u32 fib_id);
269
270 int vnet_mpls_add_del_decap (u32 rx_fib_id, 
271                              u32 tx_fib_id,
272                              u32 label_host_byte_order, 
273                              int s_bit, int next_index, int is_add);
274
275 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id, 
276                              u32 *labels_host_byte_order,
277                              u32 policy_tunnel_index,
278                              int no_dst_hash, u32 * indexp, int is_add);
279
280 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm, 
281                                          mpls_encap_t * e, 
282                                          u32 policy_tunnel_index);
283
284 typedef struct {
285   u32 lookup_miss;
286
287   /* Tunnel-id / index in tunnel vector */
288   u32 tunnel_id;
289
290   /* mpls encap index */
291   u32 mpls_encap_index;
292
293   /* pkt length */
294   u32 length;
295
296   /* tunnel ip4 addresses */
297   ip4_address_t src;
298   ip4_address_t dst;
299 } mpls_gre_tx_trace_t;
300
301 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args);
302 u8 * format_mpls_gre_header (u8 * s, va_list * args);
303
304 #define foreach_mpls_input_next                 \
305 _(DROP, "error-drop")                           \
306 _(LOOKUP, "mpls-lookup")
307
308 typedef enum {
309 #define _(s,n) MPLS_INPUT_NEXT_##s,
310   foreach_mpls_input_next
311 #undef _
312   MPLS_INPUT_N_NEXT,
313 } mpls_input_next_t;
314
315 #define foreach_mpls_lookup_next                \
316 _(DROP, "error-drop")                           \
317 _(IP4_INPUT, "ip4-input")                       \
318 _(L2_OUTPUT, "l2-output")
319
320 // FIXME remove.
321 typedef enum {
322 #define _(s,n) MPLS_LOOKUP_NEXT_##s,
323   foreach_mpls_lookup_next
324 #undef _
325   MPLS_LOOKUP_N_NEXT,
326 } mpls_lookup_next_t;
327
328 #define foreach_mpls_output_next                \
329 _(DROP, "error-drop")                           
330
331 typedef enum {
332 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
333   foreach_mpls_output_next
334 #undef _
335   MPLS_OUTPUT_N_NEXT,
336 } mpls_output_next_t;
337
338 typedef struct {
339   u32 lookup_miss;
340
341   /* Tunnel-id / index in tunnel vector */
342   u32 tunnel_id;
343
344   /* output interface */
345   u32 tx_sw_if_index;
346
347   /* mpls encap index */
348   u32 mpls_encap_index;
349
350   /* pkt length */
351   u32 length;
352
353   u8 dst[6];
354 } mpls_eth_tx_trace_t;
355
356 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
357
358 typedef struct {
359   u32 fib_index;
360   u32 entry_index;
361   u32 dest;
362   u32 s_bit;
363   u32 label;
364 } show_mpls_fib_t;
365
366 int
367 mpls_dest_cmp(void * a1, void * a2);
368
369 int
370 mpls_fib_index_cmp(void * a1, void * a2);
371
372 int
373 mpls_label_cmp(void * a1, void * a2);
374
375 #endif /* included_vnet_mpls_gre_h */