Initial commit of vpp code.
[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 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-classify */
99   u32 ip_classify_mpls_policy_encap_next_index;
100
101   /* convenience */
102   vlib_main_t * vlib_main;
103   vnet_main_t * vnet_main;
104 } mpls_main_t;
105
106 mpls_main_t mpls_main;
107
108 format_function_t format_mpls_protocol;
109 format_function_t format_mpls_header;
110 format_function_t format_mpls_header_with_length;
111 format_function_t format_mpls_gre_header_with_length;
112 format_function_t format_mpls_eth_header_with_length;
113 format_function_t format_mpls_unicast_label;
114 format_function_t format_mpls_encap_index;
115
116 vlib_node_registration_t mpls_input_node;
117 vlib_node_registration_t mpls_policy_encap_node;
118
119 vnet_device_class_t mpls_gre_device_class;
120
121 /* Parse mpls protocol as 0xXXXX or protocol name.
122    In either host or network byte order. */
123 unformat_function_t unformat_mpls_protocol_host_byte_order;
124 unformat_function_t unformat_mpls_protocol_net_byte_order;
125 unformat_function_t unformat_mpls_label_net_byte_order;
126 unformat_function_t unformat_mpls_gre_header;
127 unformat_function_t unformat_pg_mpls_gre_header;
128
129 /* Parse mpls header. */
130 unformat_function_t unformat_mpls_header;
131 unformat_function_t unformat_pg_mpls_header;
132
133 /* manually added to the interface output node in mpls.c */
134 #define MPLS_GRE_OUTPUT_NEXT_LOOKUP     1
135 #define MPLS_GRE_OUTPUT_NEXT_DROP       VNET_INTERFACE_TX_NEXT_DROP
136
137 mpls_encap_t * 
138 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address);
139
140 int mpls_label_from_fib_id_and_dest (mpls_main_t *gm, u32 fib_id,
141                                      u32 dst_address, u32 *labelp);
142
143 int vnet_mpls_gre_add_del_tunnel (ip4_address_t *src,
144                                   ip4_address_t *dst,
145                                   ip4_address_t *intfc,
146                                   u32 mask_width,
147                                   u32 inner_fib_id, u32 outer_fib_id,
148                                   u32 * tunnel_intfc_sw_if_index,
149                                   u8 l2_only,
150                                   u8 is_add);
151
152 int vnet_mpls_ethernet_add_del_tunnel (u8 *dst,
153                                        ip4_address_t *intfc,
154                                        u32 mask_width,
155                                        u32 inner_fib_id, 
156                                        u32 tx_sw_if_index,
157                                        u32 * tunnel_sw_if_index,
158                                        u8 l2_only,
159                                        u8 is_add);
160
161 int vnet_mpls_gre_delete_fib_tunnels (u32 fib_id);
162
163 int mpls_fib_reset_labels (u32 fib_id);
164
165 int vnet_mpls_add_del_decap (u32 rx_fib_id, 
166                              u32 tx_fib_id,
167                              u32 label_host_byte_order, 
168                              int s_bit, int next_index, int is_add);
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 typedef struct {
179   u32 lookup_miss;
180
181   /* Tunnel-id / index in tunnel vector */
182   u32 tunnel_id;
183
184   /* mpls encap index */
185   u32 mpls_encap_index;
186
187   /* pkt length */
188   u32 length;
189
190   /* tunnel ip4 addresses */
191   ip4_address_t src;
192   ip4_address_t dst;
193 } mpls_gre_tx_trace_t;
194
195 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args);
196 u8 * format_mpls_gre_header (u8 * s, va_list * args);
197
198 #define foreach_mpls_input_next                 \
199 _(DROP, "error-drop")                           \
200 _(IP4_INPUT, "ip4-input")                       \
201 _(L2_OUTPUT, "l2-output")
202
203 typedef enum {
204 #define _(s,n) MPLS_INPUT_NEXT_##s,
205   foreach_mpls_input_next
206 #undef _
207   MPLS_INPUT_N_NEXT,
208 } mpls_input_next_t;
209
210
211 typedef struct {
212   u32 lookup_miss;
213
214   /* Tunnel-id / index in tunnel vector */
215   u32 tunnel_id;
216
217   /* output interface */
218   u32 tx_sw_if_index;
219
220   /* mpls encap index */
221   u32 mpls_encap_index;
222
223   /* pkt length */
224   u32 length;
225
226   u8 dst[6];
227 } mpls_eth_tx_trace_t;
228
229 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args);
230
231 #endif /* included_vnet_mpls_gre_h */