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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef included_vnet_mpls_gre_h
16 #define included_vnet_mpls_gre_h
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>
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;
33 extern vnet_hw_interface_class_t mpls_gre_hw_interface_class;
36 #define mpls_error(n,s) MPLS_ERROR_##n,
37 #include <vnet/mpls/error.def>
43 * No protocol info, MPLS labels don't have a next-header field
44 * presumably the label field tells all...
49 ip4_address_t tunnel_src;
50 ip4_address_t tunnel_dst;
51 ip4_address_t intfc_address;
56 u32 hw_if_index; /* L2 x-connect capable tunnel intfc */
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 */
66 ip4_address_t intfc_address;
78 mpls_unicast_header_t *labels;
79 /* only for policy tunnels */
81 u32 output_next_index;
86 u32 next_index; /* e.g. ip4/6-input, l2-input */
89 #define MPLS_FIB_DEFAULT_TABLE_ID 0
92 * Type exposure is to allow the DP fast/inlined access
94 #define MPLS_FIB_KEY_SIZE 21
95 #define MPLS_FIB_DB_SIZE (1 << (MPLS_FIB_KEY_SIZE-1))
97 typedef struct mpls_fib_t_
100 * A hash table of entries. 21 bit key
101 * Hash table for reduced memory footprint
106 * The load-balance indeices keyed by 21 bit label+eos bit.
107 * A flat array for maximum lookup performace.
109 index_t mf_lbs[MPLS_FIB_DB_SIZE];
113 * @brief Definition of a callback for receiving MPLS interface state change
116 typedef void (*mpls_interface_state_change_callback_t)(u32 sw_if_index,
120 /* MPLS FIB index for each software interface */
121 u32 *fib_index_by_sw_if_index;
123 /** A pool of all the MPLS FIBs */
124 struct fib_table_t_ *fibs;
126 /** A hash table to lookup the mpls_fib by table ID */
127 uword *fib_index_by_table_id;
129 /* rx/tx interface/feature configuration. */
130 ip_config_main_t feature_config_mains[VNET_N_IP_FEAT];
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;
137 /* pool of gre tunnel instances */
138 mpls_gre_tunnel_t *gre_tunnels;
139 u32 * free_gre_sw_if_indices;
141 /* pool of ethernet tunnel instances */
142 mpls_eth_tunnel_t *eth_tunnels;
143 u32 * free_eth_sw_if_indices;
145 /* Encap side: map (fib, dst_address) to mpls label stack */
146 mpls_encap_t * encaps;
147 uword * mpls_encap_by_fib_and_dest;
149 /* Decap side: map rx label to FIB */
150 mpls_decap_t * decaps;
151 uword * mpls_decap_by_rx_fib_and_label;
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;
157 /* feature path configuration lists */
158 vnet_feature_registration_t * next_feature[VNET_N_IP_FEAT];
160 /* Save feature results for show command */
161 char **feature_nodes[VNET_N_IP_FEAT];
163 /* IP4 enabled count by software interface */
164 u8 * mpls_enabled_by_sw_if_index;
166 /* Functions to call when MPLS state on an interface changes. */
167 mpls_interface_state_change_callback_t * mpls_interface_state_change_callbacks;
170 vlib_main_t * vlib_main;
171 vnet_main_t * vnet_main;
174 extern mpls_main_t mpls_main;
176 #define VNET_MPLS_FEATURE_INIT(x,...) \
177 __VA_ARGS__ vnet_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) \
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; \
186 __VA_ARGS__ vnet_feature_registration_t uc_##x
188 #define VNET_MPLS_TX_FEATURE_INIT(x,...) \
189 __VA_ARGS__ vnet_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) \
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; \
198 __VA_ARGS__ vnet_feature_registration_t tx_##x
200 extern clib_error_t * mpls_feature_init(vlib_main_t * vm);
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;
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;
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;
217 extern vnet_device_class_t mpls_gre_device_class;
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;
228 /* Parse mpls header. */
229 unformat_function_t unformat_mpls_header;
230 unformat_function_t unformat_pg_mpls_header;
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
236 void mpls_sw_interface_enable_disable (mpls_main_t * mm,
240 u8 mpls_sw_interface_is_enabled (u32 sw_if_index);
243 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
245 int mpls_label_from_fib_id_and_dest (mpls_main_t *gm, u32 fib_id,
246 u32 dst_address, u32 *labelp);
248 int vnet_mpls_gre_add_del_tunnel (ip4_address_t *src,
250 ip4_address_t *intfc,
252 u32 inner_fib_id, u32 outer_fib_id,
253 u32 * tunnel_intfc_sw_if_index,
257 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
258 ip4_address_t *intfc,
262 u32 * tunnel_sw_if_index,
266 int vnet_mpls_gre_delete_fib_tunnels (u32 fib_id);
268 int mpls_fib_reset_labels (u32 fib_id);
270 int vnet_mpls_add_del_decap (u32 rx_fib_id,
272 u32 label_host_byte_order,
273 int s_bit, int next_index, int is_add);
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);
280 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm,
282 u32 policy_tunnel_index);
287 /* Tunnel-id / index in tunnel vector */
290 /* mpls encap index */
291 u32 mpls_encap_index;
296 /* tunnel ip4 addresses */
299 } mpls_gre_tx_trace_t;
301 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args);
302 u8 * format_mpls_gre_header (u8 * s, va_list * args);
304 #define foreach_mpls_input_next \
305 _(DROP, "error-drop") \
306 _(LOOKUP, "mpls-lookup")
309 #define _(s,n) MPLS_INPUT_NEXT_##s,
310 foreach_mpls_input_next
315 #define foreach_mpls_lookup_next \
316 _(DROP, "error-drop") \
317 _(IP4_INPUT, "ip4-input") \
318 _(L2_OUTPUT, "l2-output")
322 #define _(s,n) MPLS_LOOKUP_NEXT_##s,
323 foreach_mpls_lookup_next
326 } mpls_lookup_next_t;
328 #define foreach_mpls_output_next \
329 _(DROP, "error-drop")
332 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
333 foreach_mpls_output_next
336 } mpls_output_next_t;
341 /* Tunnel-id / index in tunnel vector */
344 /* output interface */
347 /* mpls encap index */
348 u32 mpls_encap_index;
354 } mpls_eth_tx_trace_t;
356 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
367 mpls_dest_cmp(void * a1, void * a2);
370 mpls_fib_index_cmp(void * a1, void * a2);
373 mpls_label_cmp(void * a1, void * a2);
375 #endif /* included_vnet_mpls_gre_h */