Deprecate MPLSoGRE tunnels (VPP-502)
[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_h
16 #define included_vnet_mpls_h
17
18 #include <vnet/vnet.h>
19 #include <vnet/mpls/packet.h>
20 #include <vnet/mpls/mpls_types.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/fib/fib_node.h>
24 #include <vnet/adj/adj.h>
25
26 typedef enum {
27 #define mpls_error(n,s) MPLS_ERROR_##n,
28 #include <vnet/mpls/error.def>
29 #undef mpls_error
30   MPLS_N_ERROR,
31 } mpls_error_t;
32
33 /*
34  * No protocol info, MPLS labels don't have a next-header field
35  * presumably the label field tells all...
36  */
37 typedef struct {
38   u8 tunnel_dst[6];
39   ip4_address_t intfc_address;
40   u32 tx_sw_if_index;
41   u32 inner_fib_index;
42   u32 mask_width;
43   u32 encap_index;
44   u32 hw_if_index;
45   u8 * rewrite_data;
46   u8 l2_only;
47   fib_node_index_t fei;
48 } mpls_eth_tunnel_t;
49
50 typedef struct {
51   mpls_unicast_header_t *labels;
52   /* only for policy tunnels */
53   u8 * rewrite;
54   u32 output_next_index;
55 } mpls_encap_t;
56
57 #define MPLS_FIB_DEFAULT_TABLE_ID 0
58
59 /**
60  * Type exposure is to allow the DP fast/inlined access
61  */
62 #define MPLS_FIB_KEY_SIZE 21
63 #define MPLS_FIB_DB_SIZE (1 << (MPLS_FIB_KEY_SIZE-1))
64
65 typedef struct mpls_fib_t_
66 {
67   /**
68    * A hash table of entries. 21 bit key
69    * Hash table for reduced memory footprint
70    */
71   uword * mf_entries;
72
73   /**
74    * The load-balance indeices keyed by 21 bit label+eos bit.
75    * A flat array for maximum lookup performace.
76    */
77   index_t mf_lbs[MPLS_FIB_DB_SIZE];
78 } mpls_fib_t;
79
80 /**
81  * @brief Definition of a callback for receiving MPLS interface state change
82  * notifications
83  */
84 typedef void (*mpls_interface_state_change_callback_t)(u32 sw_if_index,
85                                                        u32 is_enable);
86
87 typedef struct {
88   /* MPLS FIB index for each software interface */
89   u32 *fib_index_by_sw_if_index;
90
91   /**  A pool of all the MPLS FIBs */
92   struct fib_table_t_ *fibs;
93
94   /** A hash table to lookup the mpls_fib by table ID */
95   uword *fib_index_by_table_id;
96
97   /* rx/tx interface/feature configuration. */
98   ip_config_main_t feature_config_mains[VNET_N_IP_FEAT];
99
100   /* Built-in unicast feature path indices, see vnet_feature_arc_init(...)  */
101   u32 mpls_rx_feature_lookup;
102   u32 mpls_rx_feature_not_enabled;
103   u32 mpls_tx_feature_interface_output;
104
105   /* pool of ethernet tunnel instances */
106   mpls_eth_tunnel_t *eth_tunnels;
107   u32 * free_eth_sw_if_indices;
108
109   /* Encap side: map (fib, dst_address) to mpls label stack */
110   mpls_encap_t * encaps;
111   uword * mpls_encap_by_fib_and_dest;
112
113   /* mpls-o-e policy tunnel next index for ip4/ip6-classify */
114   u32 ip4_classify_mpls_policy_encap_next_index;
115   u32 ip6_classify_mpls_policy_encap_next_index;
116
117   /* feature path configuration lists */
118   vnet_feature_registration_t * next_feature[VNET_N_IP_FEAT];
119
120   /* Save feature results for show command */
121   char **feature_nodes[VNET_N_IP_FEAT];
122
123   /* IP4 enabled count by software interface */
124   u8 * mpls_enabled_by_sw_if_index;
125
126   /* convenience */
127   vlib_main_t * vlib_main;
128   vnet_main_t * vnet_main;
129 } mpls_main_t;
130
131 extern mpls_main_t mpls_main;
132
133 #define VNET_MPLS_FEATURE_INIT(x,...)                           \
134   __VA_ARGS__ vnet_feature_registration_t uc_##x;            \
135 static void __vnet_add_feature_registration_uc_##x (void)       \
136   __attribute__((__constructor__)) ;                            \
137 static void __vnet_add_feature_registration_uc_##x (void)       \
138 {                                                               \
139   mpls_main_t * mm = &mpls_main;                                \
140   uc_##x.next = mm->next_feature[VNET_IP_RX_UNICAST_FEAT];      \
141   mm->next_feature[VNET_IP_RX_UNICAST_FEAT] = &uc_##x;          \
142 }                                                               \
143 __VA_ARGS__ vnet_feature_registration_t uc_##x
144
145 #define VNET_MPLS_TX_FEATURE_INIT(x,...)                        \
146   __VA_ARGS__ vnet_feature_registration_t tx_##x;            \
147 static void __vnet_add_feature_registration_tx_##x (void)       \
148   __attribute__((__constructor__)) ;                            \
149 static void __vnet_add_feature_registration_tx_##x (void)       \
150 {                                                               \
151   mpls_main_t * mm = &mpls_main;                                \
152   tx_##x.next = mm->next_feature[VNET_IP_TX_FEAT];              \
153   mm->next_feature[VNET_IP_TX_FEAT] = &tx_##x;                  \
154 }                                                               \
155 __VA_ARGS__ vnet_feature_registration_t tx_##x
156
157 extern clib_error_t * mpls_feature_init(vlib_main_t * vm);
158
159 format_function_t format_mpls_protocol;
160 format_function_t format_mpls_eth_header_with_length;
161 format_function_t format_mpls_encap_index;
162
163 format_function_t format_mpls_eos_bit;
164 format_function_t format_mpls_unicast_header_net_byte_order;
165 format_function_t format_mpls_unicast_label;
166 format_function_t format_mpls_header;
167
168 extern vlib_node_registration_t mpls_input_node;
169 extern vlib_node_registration_t mpls_policy_encap_node;
170 extern vlib_node_registration_t mpls_output_node;
171 extern vlib_node_registration_t mpls_midchain_node;
172
173 /* Parse mpls protocol as 0xXXXX or protocol name.
174    In either host or network byte order. */
175 unformat_function_t unformat_mpls_protocol_host_byte_order;
176 unformat_function_t unformat_mpls_protocol_net_byte_order;
177 unformat_function_t unformat_mpls_label_net_byte_order;
178 unformat_function_t unformat_mpls_unicast_label;
179
180 /* Parse mpls header. */
181 unformat_function_t unformat_mpls_header;
182 unformat_function_t unformat_pg_mpls_header;
183
184 void mpls_sw_interface_enable_disable (mpls_main_t * mm,
185                                        u32 sw_if_index,
186                                        u8 is_enable);
187
188 u8 mpls_sw_interface_is_enabled (u32 sw_if_index);
189
190 mpls_encap_t *
191 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
192
193 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
194                                        ip4_address_t *intfc,
195                                        u32 mask_width,
196                                        u32 inner_fib_id,
197                                        u32 tx_sw_if_index,
198                                        u32 * tunnel_sw_if_index,
199                                        u8 l2_only,
200                                        u8 is_add);
201
202 int mpls_fib_reset_labels (u32 fib_id);
203
204 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id,
205                              u32 *labels_host_byte_order,
206                              u32 policy_tunnel_index,
207                              int no_dst_hash, u32 * indexp, int is_add);
208
209 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm,
210                                          mpls_encap_t * e,
211                                          u32 policy_tunnel_index);
212
213 #define foreach_mpls_input_next                 \
214 _(DROP, "error-drop")                           \
215 _(LOOKUP, "mpls-lookup")
216
217 typedef enum {
218 #define _(s,n) MPLS_INPUT_NEXT_##s,
219   foreach_mpls_input_next
220 #undef _
221   MPLS_INPUT_N_NEXT,
222 } mpls_input_next_t;
223
224 #define foreach_mpls_lookup_next                \
225 _(DROP, "error-drop")                           \
226 _(IP4_INPUT, "ip4-input")                       \
227 _(L2_OUTPUT, "l2-output")
228
229 // FIXME remove.
230 typedef enum {
231 #define _(s,n) MPLS_LOOKUP_NEXT_##s,
232   foreach_mpls_lookup_next
233 #undef _
234   MPLS_LOOKUP_N_NEXT,
235 } mpls_lookup_next_t;
236
237 #define foreach_mpls_output_next                \
238 _(DROP, "error-drop")
239
240 typedef enum {
241 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
242   foreach_mpls_output_next
243 #undef _
244   MPLS_OUTPUT_N_NEXT,
245 } mpls_output_next_t;
246
247 typedef struct {
248   u32 lookup_miss;
249
250   /* Tunnel-id / index in tunnel vector */
251   u32 tunnel_id;
252
253   /* output interface */
254   u32 tx_sw_if_index;
255
256   /* mpls encap index */
257   u32 mpls_encap_index;
258
259   /* pkt length */
260   u32 length;
261
262   u8 dst[6];
263 } mpls_eth_tx_trace_t;
264
265 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
266
267 typedef struct {
268   u32 fib_index;
269   u32 entry_index;
270   u32 dest;
271   u32 s_bit;
272   u32 label;
273 } show_mpls_fib_t;
274
275 int
276 mpls_dest_cmp(void * a1, void * a2);
277
278 int
279 mpls_fib_index_cmp(void * a1, void * a2);
280
281 int
282 mpls_label_cmp(void * a1, void * a2);
283
284 #endif /* included_vnet_mpls_h */