59fc761ea2c93cc12ececdbc2b0f9e8e58ca8589
[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   /* pool of ethernet tunnel instances */
98   mpls_eth_tunnel_t *eth_tunnels;
99   u32 * free_eth_sw_if_indices;
100
101   /* Encap side: map (fib, dst_address) to mpls label stack */
102   mpls_encap_t * encaps;
103   uword * mpls_encap_by_fib_and_dest;
104
105   /* mpls-o-e policy tunnel next index for ip4/ip6-classify */
106   u32 ip4_classify_mpls_policy_encap_next_index;
107   u32 ip6_classify_mpls_policy_encap_next_index;
108
109   /* Feature arc indices */
110   u8 input_feature_arc_index;
111   u8 output_feature_arc_index;
112
113   /* IP4 enabled count by software interface */
114   u8 * mpls_enabled_by_sw_if_index;
115
116   /* convenience */
117   vlib_main_t * vlib_main;
118   vnet_main_t * vnet_main;
119 } mpls_main_t;
120
121 extern mpls_main_t mpls_main;
122
123 extern clib_error_t * mpls_feature_init(vlib_main_t * vm);
124
125 format_function_t format_mpls_protocol;
126 format_function_t format_mpls_eth_header_with_length;
127 format_function_t format_mpls_encap_index;
128
129 format_function_t format_mpls_eos_bit;
130 format_function_t format_mpls_unicast_header_net_byte_order;
131 format_function_t format_mpls_unicast_label;
132 format_function_t format_mpls_header;
133
134 extern vlib_node_registration_t mpls_input_node;
135 extern vlib_node_registration_t mpls_policy_encap_node;
136 extern vlib_node_registration_t mpls_output_node;
137 extern vlib_node_registration_t mpls_midchain_node;
138
139 /* Parse mpls protocol as 0xXXXX or protocol name.
140    In either host or network byte order. */
141 unformat_function_t unformat_mpls_protocol_host_byte_order;
142 unformat_function_t unformat_mpls_protocol_net_byte_order;
143 unformat_function_t unformat_mpls_label_net_byte_order;
144 unformat_function_t unformat_mpls_unicast_label;
145
146 /* Parse mpls header. */
147 unformat_function_t unformat_mpls_header;
148 unformat_function_t unformat_pg_mpls_header;
149
150 void mpls_sw_interface_enable_disable (mpls_main_t * mm,
151                                        u32 sw_if_index,
152                                        u8 is_enable);
153
154 u8 mpls_sw_interface_is_enabled (u32 sw_if_index);
155
156 mpls_encap_t *
157 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
158
159 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
160                                        ip4_address_t *intfc,
161                                        u32 mask_width,
162                                        u32 inner_fib_id,
163                                        u32 tx_sw_if_index,
164                                        u32 * tunnel_sw_if_index,
165                                        u8 l2_only,
166                                        u8 is_add);
167
168 int mpls_fib_reset_labels (u32 fib_id);
169
170 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id,
171                              u32 *labels_host_byte_order,
172                              u32 policy_tunnel_index,
173                              int no_dst_hash, u32 * indexp, int is_add);
174
175 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm,
176                                          mpls_encap_t * e,
177                                          u32 policy_tunnel_index);
178
179 #define foreach_mpls_input_next                 \
180 _(DROP, "error-drop")                           \
181 _(LOOKUP, "mpls-lookup")
182
183 typedef enum {
184 #define _(s,n) MPLS_INPUT_NEXT_##s,
185   foreach_mpls_input_next
186 #undef _
187   MPLS_INPUT_N_NEXT,
188 } mpls_input_next_t;
189
190 #define foreach_mpls_lookup_next                \
191 _(DROP, "error-drop")                           \
192 _(IP4_INPUT, "ip4-input")                       \
193 _(L2_OUTPUT, "l2-output")
194
195 // FIXME remove.
196 typedef enum {
197 #define _(s,n) MPLS_LOOKUP_NEXT_##s,
198   foreach_mpls_lookup_next
199 #undef _
200   MPLS_LOOKUP_N_NEXT,
201 } mpls_lookup_next_t;
202
203 #define foreach_mpls_output_next                \
204 _(DROP, "error-drop")
205
206 typedef enum {
207 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
208   foreach_mpls_output_next
209 #undef _
210   MPLS_OUTPUT_N_NEXT,
211 } mpls_output_next_t;
212
213 typedef struct {
214   u32 lookup_miss;
215
216   /* Tunnel-id / index in tunnel vector */
217   u32 tunnel_id;
218
219   /* output interface */
220   u32 tx_sw_if_index;
221
222   /* mpls encap index */
223   u32 mpls_encap_index;
224
225   /* pkt length */
226   u32 length;
227
228   u8 dst[6];
229 } mpls_eth_tx_trace_t;
230
231 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
232
233 typedef struct {
234   u32 fib_index;
235   u32 entry_index;
236   u32 dest;
237   u32 s_bit;
238   u32 label;
239 } show_mpls_fib_t;
240
241 int
242 mpls_dest_cmp(void * a1, void * a2);
243
244 int
245 mpls_fib_index_cmp(void * a1, void * a2);
246
247 int
248 mpls_label_cmp(void * a1, void * a2);
249
250 #endif /* included_vnet_mpls_h */