d8ffca22793cba721822f19137cb37cfc1b3c0b0
[vpp.git] / vnet / vnet / mpls-gre / 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-gre/packet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ethernet/ethernet.h>
23
24 typedef CLIB_PACKED (struct {
25   ip4_header_t ip4;             /* 20 bytes */
26   gre_header_t gre;             /* 4 bytes */
27   mpls_unicast_header_t labels[0];   /* 4 bytes each */
28 }) ip4_gre_and_mpls_header_t;
29
30 extern vnet_hw_interface_class_t mpls_gre_hw_interface_class;
31
32 typedef enum {
33 #define mpls_error(n,s) MPLS_ERROR_##n,
34 #include <vnet/mpls-gre/error.def>
35 #undef mpls_error
36   MPLS_N_ERROR,
37 } mpls_gre_error_t;
38
39 /* 
40  * No protocol info, MPLS labels don't have a next-header field 
41  * presumably the label field tells all...
42  */
43
44 typedef struct {
45   ip4_address_t tunnel_src;
46   ip4_address_t tunnel_dst;
47   ip4_address_t intfc_address;
48   u32 mask_width;
49   u32 inner_fib_index;
50   u32 outer_fib_index;
51   u32 encap_index;
52   u32 hw_if_index;              /* L2 x-connect capable tunnel intfc */
53   u8 * rewrite_data;
54   u8 l2_only;
55 } mpls_gre_tunnel_t;
56
57 typedef struct {
58   u8 tunnel_dst[6];
59   ip4_address_t intfc_address;
60   u32 tx_sw_if_index;
61   u32 inner_fib_index;
62   u32 mask_width;
63   u32 encap_index;
64   u32 hw_if_index;
65   u8 * rewrite_data;
66   u8 l2_only;
67 } mpls_eth_tunnel_t;
68
69 typedef struct {
70   mpls_unicast_header_t *labels;
71   /* only for policy tunnels */
72   u8 * rewrite;
73   u32 output_next_index;
74 } mpls_encap_t;
75
76 typedef struct {
77   u32 tx_fib_index;
78   u32 next_index;               /* e.g. ip4/6-input, l2-input */
79 } mpls_decap_t;
80
81 typedef struct {
82   /* pool of gre tunnel instances */
83   mpls_gre_tunnel_t *gre_tunnels;
84   u32 * free_gre_sw_if_indices;
85
86   /* pool of ethernet tunnel instances */
87   mpls_eth_tunnel_t *eth_tunnels;
88   u32 * free_eth_sw_if_indices;
89
90   /* Encap side: map (fib, dst_address) to mpls label stack */
91   mpls_encap_t * encaps;
92   uword * mpls_encap_by_fib_and_dest;
93
94   /* Decap side: map rx label to FIB */
95   mpls_decap_t * decaps;
96   uword * mpls_decap_by_rx_fib_and_label;
97
98   /* mpls-o-e policy tunnel next index for ip4/ip6-classify */
99   u32 ip4_classify_mpls_policy_encap_next_index;
100   u32 ip6_classify_mpls_policy_encap_next_index;
101
102   /* convenience */
103   vlib_main_t * vlib_main;
104   vnet_main_t * vnet_main;
105 } mpls_main_t;
106
107 mpls_main_t mpls_main;
108
109 format_function_t format_mpls_protocol;
110 format_function_t format_mpls_header;
111 format_function_t format_mpls_header_with_length;
112 format_function_t format_mpls_gre_header_with_length;
113 format_function_t format_mpls_eth_header_with_length;
114 format_function_t format_mpls_unicast_label;
115 format_function_t format_mpls_encap_index;
116
117 extern vlib_node_registration_t mpls_input_node;
118 extern vlib_node_registration_t mpls_policy_encap_node;
119
120 extern vnet_device_class_t mpls_gre_device_class;
121
122 /* Parse mpls protocol as 0xXXXX or protocol name.
123    In either host or network byte order. */
124 unformat_function_t unformat_mpls_protocol_host_byte_order;
125 unformat_function_t unformat_mpls_protocol_net_byte_order;
126 unformat_function_t unformat_mpls_label_net_byte_order;
127 unformat_function_t unformat_mpls_gre_header;
128 unformat_function_t unformat_pg_mpls_gre_header;
129
130 /* Parse mpls header. */
131 unformat_function_t unformat_mpls_header;
132 unformat_function_t unformat_pg_mpls_header;
133
134 /* manually added to the interface output node in mpls.c */
135 #define MPLS_GRE_OUTPUT_NEXT_LOOKUP     1
136 #define MPLS_GRE_OUTPUT_NEXT_DROP       VNET_INTERFACE_TX_NEXT_DROP
137
138 mpls_encap_t * 
139 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
140
141 int mpls_label_from_fib_id_and_dest (mpls_main_t *gm, u32 fib_id,
142                                      u32 dst_address, u32 *labelp);
143
144 int vnet_mpls_gre_add_del_tunnel (ip4_address_t *src,
145                                   ip4_address_t *dst,
146                                   ip4_address_t *intfc,
147                                   u32 mask_width,
148                                   u32 inner_fib_id, u32 outer_fib_id,
149                                   u32 * tunnel_intfc_sw_if_index,
150                                   u8 l2_only,
151                                   u8 is_add);
152
153 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
154                                        ip4_address_t *intfc,
155                                        u32 mask_width,
156                                        u32 inner_fib_id, 
157                                        u32 tx_sw_if_index,
158                                        u32 * tunnel_sw_if_index,
159                                        u8 l2_only,
160                                        u8 is_add);
161
162 int vnet_mpls_gre_delete_fib_tunnels (u32 fib_id);
163
164 int mpls_fib_reset_labels (u32 fib_id);
165
166 int vnet_mpls_add_del_decap (u32 rx_fib_id, 
167                              u32 tx_fib_id,
168                              u32 label_host_byte_order, 
169                              int s_bit, int next_index, int is_add);
170
171 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id, 
172                              u32 *labels_host_byte_order,
173                              u32 policy_tunnel_index,
174                              int no_dst_hash, u32 * indexp, int is_add);
175
176 int vnet_mpls_policy_tunnel_add_rewrite (mpls_main_t * mm, 
177                                          mpls_encap_t * e, 
178                                          u32 policy_tunnel_index);
179 typedef struct {
180   u32 lookup_miss;
181
182   /* Tunnel-id / index in tunnel vector */
183   u32 tunnel_id;
184
185   /* mpls encap index */
186   u32 mpls_encap_index;
187
188   /* pkt length */
189   u32 length;
190
191   /* tunnel ip4 addresses */
192   ip4_address_t src;
193   ip4_address_t dst;
194 } mpls_gre_tx_trace_t;
195
196 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args);
197 u8 * format_mpls_gre_header (u8 * s, va_list * args);
198
199 #define foreach_mpls_input_next                 \
200 _(DROP, "error-drop")                           \
201 _(IP4_INPUT, "ip4-input")                       \
202 _(L2_OUTPUT, "l2-output")
203
204 typedef enum {
205 #define _(s,n) MPLS_INPUT_NEXT_##s,
206   foreach_mpls_input_next
207 #undef _
208   MPLS_INPUT_N_NEXT,
209 } mpls_input_next_t;
210
211
212 typedef struct {
213   u32 lookup_miss;
214
215   /* Tunnel-id / index in tunnel vector */
216   u32 tunnel_id;
217
218   /* output interface */
219   u32 tx_sw_if_index;
220
221   /* mpls encap index */
222   u32 mpls_encap_index;
223
224   /* pkt length */
225   u32 length;
226
227   u8 dst[6];
228 } mpls_eth_tx_trace_t;
229
230 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
231
232 typedef struct {
233   u32 fib_index;
234   u32 entry_index;
235   u32 dest;
236   u32 s_bit;
237   u32 label;
238 } show_mpls_fib_t;
239
240 int
241 mpls_dest_cmp(void * a1, void * a2);
242
243 int
244 mpls_fib_index_cmp(void * a1, void * a2);
245
246 int
247 mpls_label_cmp(void * a1, void * a2);
248
249 #endif /* included_vnet_mpls_gre_h */