35755333e1d73f148997c18be0e99458643d210c
[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 vnet_feature_arc_init(...)  */
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_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   /* convenience */
167   vlib_main_t * vlib_main;
168   vnet_main_t * vnet_main;
169 } mpls_main_t;
170
171 extern mpls_main_t mpls_main;
172
173 #define VNET_MPLS_FEATURE_INIT(x,...)                           \
174   __VA_ARGS__ vnet_feature_registration_t uc_##x;            \
175 static void __vnet_add_feature_registration_uc_##x (void)       \
176   __attribute__((__constructor__)) ;                            \
177 static void __vnet_add_feature_registration_uc_##x (void)       \
178 {                                                               \
179   mpls_main_t * mm = &mpls_main;                                \
180   uc_##x.next = mm->next_feature[VNET_IP_RX_UNICAST_FEAT];      \
181   mm->next_feature[VNET_IP_RX_UNICAST_FEAT] = &uc_##x;          \
182 }                                                               \
183 __VA_ARGS__ vnet_feature_registration_t uc_##x
184
185 #define VNET_MPLS_TX_FEATURE_INIT(x,...)                        \
186   __VA_ARGS__ vnet_feature_registration_t tx_##x;            \
187 static void __vnet_add_feature_registration_tx_##x (void)       \
188   __attribute__((__constructor__)) ;                            \
189 static void __vnet_add_feature_registration_tx_##x (void)       \
190 {                                                               \
191   mpls_main_t * mm = &mpls_main;                                \
192   tx_##x.next = mm->next_feature[VNET_IP_TX_FEAT];              \
193   mm->next_feature[VNET_IP_TX_FEAT] = &tx_##x;                  \
194 }                                                               \
195 __VA_ARGS__ vnet_feature_registration_t tx_##x
196
197 extern clib_error_t * mpls_feature_init(vlib_main_t * vm);
198
199 format_function_t format_mpls_protocol;
200 format_function_t format_mpls_gre_header_with_length;
201 format_function_t format_mpls_eth_header_with_length;
202 format_function_t format_mpls_encap_index;
203
204 format_function_t format_mpls_eos_bit;
205 format_function_t format_mpls_unicast_header_net_byte_order;
206 format_function_t format_mpls_unicast_label;
207 format_function_t format_mpls_header;
208
209 extern vlib_node_registration_t mpls_input_node;
210 extern vlib_node_registration_t mpls_policy_encap_node;
211 extern vlib_node_registration_t mpls_output_node;
212 extern vlib_node_registration_t mpls_midchain_node;
213
214 extern vnet_device_class_t mpls_gre_device_class;
215
216 /* Parse mpls protocol as 0xXXXX or protocol name.
217    In either host or network byte order. */
218 unformat_function_t unformat_mpls_protocol_host_byte_order;
219 unformat_function_t unformat_mpls_protocol_net_byte_order;
220 unformat_function_t unformat_mpls_label_net_byte_order;
221 unformat_function_t unformat_mpls_gre_header;
222 unformat_function_t unformat_pg_mpls_gre_header;
223 unformat_function_t unformat_mpls_unicast_label;
224
225 /* Parse mpls header. */
226 unformat_function_t unformat_mpls_header;
227 unformat_function_t unformat_pg_mpls_header;
228
229 /* manually added to the interface output node in mpls.c */
230 #define MPLS_GRE_OUTPUT_NEXT_LOOKUP     1
231 #define MPLS_GRE_OUTPUT_NEXT_DROP       VNET_INTERFACE_TX_NEXT_DROP
232
233 void mpls_sw_interface_enable_disable (mpls_main_t * mm,
234                                        u32 sw_if_index,
235                                        u8 is_enable);
236
237 u8 mpls_sw_interface_is_enabled (u32 sw_if_index);
238
239 mpls_encap_t *
240 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
241
242 int mpls_label_from_fib_id_and_dest (mpls_main_t *gm, u32 fib_id,
243                                      u32 dst_address, u32 *labelp);
244
245 int vnet_mpls_gre_add_del_tunnel (ip4_address_t *src,
246                                   ip4_address_t *dst,
247                                   ip4_address_t *intfc,
248                                   u32 mask_width,
249                                   u32 inner_fib_id, u32 outer_fib_id,
250                                   u32 * tunnel_intfc_sw_if_index,
251                                   u8 l2_only,
252                                   u8 is_add);
253
254 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
255                                        ip4_address_t *intfc,
256                                        u32 mask_width,
257                                        u32 inner_fib_id,
258                                        u32 tx_sw_if_index,
259                                        u32 * tunnel_sw_if_index,
260                                        u8 l2_only,
261                                        u8 is_add);
262
263 int vnet_mpls_gre_delete_fib_tunnels (u32 fib_id);
264
265 int mpls_fib_reset_labels (u32 fib_id);
266
267 int vnet_mpls_add_del_decap (u32 rx_fib_id,
268                              u32 tx_fib_id,
269                              u32 label_host_byte_order,
270                              int s_bit, int next_index, int is_add);
271
272 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id,
273                              u32 *labels_host_byte_order,
274                              u32 policy_tunnel_index,
275                              int no_dst_hash, u32 * indexp, int is_add);
276
277 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm,
278                                          mpls_encap_t * e,
279                                          u32 policy_tunnel_index);
280
281 typedef struct {
282   u32 lookup_miss;
283
284   /* Tunnel-id / index in tunnel vector */
285   u32 tunnel_id;
286
287   /* mpls encap index */
288   u32 mpls_encap_index;
289
290   /* pkt length */
291   u32 length;
292
293   /* tunnel ip4 addresses */
294   ip4_address_t src;
295   ip4_address_t dst;
296 } mpls_gre_tx_trace_t;
297
298 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args);
299 u8 * format_mpls_gre_header (u8 * s, va_list * args);
300
301 #define foreach_mpls_input_next                 \
302 _(DROP, "error-drop")                           \
303 _(LOOKUP, "mpls-lookup")
304
305 typedef enum {
306 #define _(s,n) MPLS_INPUT_NEXT_##s,
307   foreach_mpls_input_next
308 #undef _
309   MPLS_INPUT_N_NEXT,
310 } mpls_input_next_t;
311
312 #define foreach_mpls_lookup_next                \
313 _(DROP, "error-drop")                           \
314 _(IP4_INPUT, "ip4-input")                       \
315 _(L2_OUTPUT, "l2-output")
316
317 // FIXME remove.
318 typedef enum {
319 #define _(s,n) MPLS_LOOKUP_NEXT_##s,
320   foreach_mpls_lookup_next
321 #undef _
322   MPLS_LOOKUP_N_NEXT,
323 } mpls_lookup_next_t;
324
325 #define foreach_mpls_output_next                \
326 _(DROP, "error-drop")
327
328 typedef enum {
329 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
330   foreach_mpls_output_next
331 #undef _
332   MPLS_OUTPUT_N_NEXT,
333 } mpls_output_next_t;
334
335 typedef struct {
336   u32 lookup_miss;
337
338   /* Tunnel-id / index in tunnel vector */
339   u32 tunnel_id;
340
341   /* output interface */
342   u32 tx_sw_if_index;
343
344   /* mpls encap index */
345   u32 mpls_encap_index;
346
347   /* pkt length */
348   u32 length;
349
350   u8 dst[6];
351 } mpls_eth_tx_trace_t;
352
353 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
354
355 typedef struct {
356   u32 fib_index;
357   u32 entry_index;
358   u32 dest;
359   u32 s_bit;
360   u32 label;
361 } show_mpls_fib_t;
362
363 int
364 mpls_dest_cmp(void * a1, void * a2);
365
366 int
367 mpls_fib_index_cmp(void * a1, void * a2);
368
369 int
370 mpls_label_cmp(void * a1, void * a2);
371
372 #endif /* included_vnet_mpls_gre_h */