Support MPLSoGRE with the new FIB 2.0
[vpp.git] / vnet / vnet / mpls / mpls_output.c
1 /*
2  * mpls_output.c: MPLS Adj rewrite
3  *
4  * Copyright (c) 2012-2014 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls.h>
21
22 typedef struct {
23   /* Adjacency taken. */
24   u32 adj_index;
25   u32 flow_hash;
26
27   /* Packet data, possibly *after* rewrite. */
28   u8 packet_data[64 - 1*sizeof(u32)];
29 } mpls_output_trace_t;
30
31 static u8 *
32 format_mpls_output_trace (u8 * s, va_list * args)
33 {
34   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36   mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
37   vnet_main_t * vnm = vnet_get_main();
38   uword indent = format_get_indent (s);
39
40   s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
41               t->adj_index,
42               format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
43               t->flow_hash);
44   s = format (s, "\n%U%U",
45               format_white_space, indent,
46               format_ip_adjacency_packet_data,
47               vnm, t->adj_index,
48               t->packet_data, sizeof (t->packet_data));
49   return s;
50 }
51
52 static inline uword
53 mpls_output_inline (vlib_main_t * vm,
54                     vlib_node_runtime_t * node,
55                     vlib_frame_t * from_frame,
56                     int is_midchain)
57 {
58   u32 n_left_from, next_index, * from, * to_next, cpu_index;
59   vlib_node_runtime_t * error_node;
60
61   cpu_index = os_get_cpu_number();
62   error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
63   from = vlib_frame_vector_args (from_frame);
64   n_left_from = from_frame->n_vectors;
65   next_index = node->cached_next_index;
66
67   while (n_left_from > 0)
68     {
69       u32 n_left_to_next;
70
71       vlib_get_next_frame (vm, node, next_index,
72                            to_next, n_left_to_next);
73
74       while (n_left_from > 0 && n_left_to_next > 0)
75         {
76           ip_adjacency_t * adj0;
77           mpls_unicast_header_t *hdr0;
78           vlib_buffer_t * p0;
79           u32 pi0, rw_len0, adj_index0, next0, error0;
80
81           pi0 = to_next[0] = from[0];
82
83           p0 = vlib_get_buffer (vm, pi0);
84
85           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
86
87           /* We should never rewrite a pkt using the MISS adjacency */
88           ASSERT(adj_index0);
89
90           adj0 = adj_get(adj_index0);
91           hdr0 = vlib_buffer_get_current (p0);
92
93           /* Guess we are only writing on simple Ethernet header. */
94           vnet_rewrite_one_header (adj0[0], hdr0, 
95                                    sizeof (ethernet_header_t));
96           
97           /* Update packet buffer attributes/set output interface. */
98           rw_len0 = adj0[0].rewrite_header.data_bytes;
99           
100           if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
101               vlib_increment_combined_counter 
102                   (&adjacency_counters,
103                    cpu_index, adj_index0, 
104                    /* packet increment */ 0,
105                    /* byte increment */ rw_len0-sizeof(ethernet_header_t));
106           
107           /* Check MTU of outgoing interface. */
108           error0 = (vlib_buffer_length_in_chain (vm, p0) 
109                     > adj0[0].rewrite_header.max_l3_packet_bytes
110                     ? IP4_ERROR_MTU_EXCEEDED
111                     : IP4_ERROR_NONE);
112
113           p0->error = error_node->errors[error0];
114
115           /* Don't adjust the buffer for ttl issue; icmp-error node wants
116            * to see the IP headerr */
117           if (PREDICT_TRUE(error0 == IP4_ERROR_NONE))
118             {
119               p0->current_data -= rw_len0;
120               p0->current_length += rw_len0;
121
122               vnet_buffer (p0)->sw_if_index[VLIB_TX] =
123                   adj0[0].rewrite_header.sw_if_index;
124               next0 = adj0[0].rewrite_header.next_index;
125
126               if (is_midchain)
127                 {
128                   adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
129                 }
130             }
131           else
132             {
133               next0 = MPLS_OUTPUT_NEXT_DROP;
134             }
135
136           from += 1;
137           n_left_from -= 1;
138           to_next += 1;
139           n_left_to_next -= 1;
140       
141           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) 
142             {
143               mpls_output_trace_t *tr = vlib_add_trace (vm, node, 
144                                                         p0, sizeof (*tr));
145               tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
146               tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
147             }
148
149           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
150                                            to_next, n_left_to_next,
151                                            pi0, next0);
152         }
153
154       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
155     }
156   vlib_node_increment_counter (vm, mpls_output_node.index,
157                                MPLS_ERROR_PKTS_ENCAP,
158                                from_frame->n_vectors);
159
160   return from_frame->n_vectors;
161 }
162
163 static char * mpls_error_strings[] = {
164 #define mpls_error(n,s) s,
165 #include "error.def"
166 #undef mpls_error
167 };
168
169 static inline uword
170 mpls_output (vlib_main_t * vm,
171              vlib_node_runtime_t * node,
172              vlib_frame_t * from_frame)
173 {
174     return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
175 }
176
177 VLIB_REGISTER_NODE (mpls_output_node) = {
178   .function = mpls_output,
179   .name = "mpls-output",
180   /* Takes a vector of packets. */
181   .vector_size = sizeof (u32),
182   .n_errors = MPLS_N_ERROR,
183   .error_strings = mpls_error_strings,
184
185   .n_next_nodes = MPLS_OUTPUT_N_NEXT,
186   .next_nodes = {
187 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
188     foreach_mpls_output_next
189 #undef _
190   },
191
192   .format_trace = format_mpls_output_trace,
193 };
194
195 VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
196
197 static inline uword
198 mpls_midchain (vlib_main_t * vm,
199                vlib_node_runtime_t * node,
200                vlib_frame_t * from_frame)
201 {
202     return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
203 }
204
205 VLIB_REGISTER_NODE (mpls_midchain_node) = {
206   .function = mpls_midchain,
207   .name = "mpls-midchain",
208   .vector_size = sizeof (u32),
209
210   .format_trace = format_mpls_output_trace,
211
212   .sibling_of = "mpls-output",
213 };
214
215 VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
216
217 /**
218  * @brief Next index values from the MPLS incomplete adj node
219  */
220 #define foreach_mpls_adj_incomplete_next        \
221 _(DROP, "error-drop")                   \
222 _(IP4,  "ip4-arp")                      \
223 _(IP6,  "ip6-discover-neighbor")
224
225 typedef enum {
226 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
227   foreach_mpls_adj_incomplete_next
228 #undef _
229   MPLS_ADJ_INCOMPLETE_N_NEXT,
230 } mpls_adj_incomplete_next_t;
231
232 /**
233  * @brief A struct to hold tracing information for the MPLS label imposition
234  * node.
235  */
236 typedef struct mpls_adj_incomplete_trace_t_
237 {
238     u32 next;
239 } mpls_adj_incomplete_trace_t;
240
241
242 /**
243  * @brief Graph node for incomplete MPLS adjacency.
244  * This node will push traffic to either the v4-arp or v6-nd node
245  * based on the next-hop proto of the adj.
246  * We pay a cost for this 'routing' node, but an incomplete adj is the
247  * exception case.
248  */
249 static inline uword
250 mpls_adj_incomplete (vlib_main_t * vm,
251                      vlib_node_runtime_t * node,
252                      vlib_frame_t * from_frame)
253 {
254     u32 n_left_from, next_index, * from, * to_next;
255
256   from = vlib_frame_vector_args (from_frame);
257   n_left_from = from_frame->n_vectors;
258   next_index = node->cached_next_index;
259
260   while (n_left_from > 0)
261     {
262       u32 n_left_to_next;
263
264       vlib_get_next_frame (vm, node, next_index,
265                            to_next, n_left_to_next);
266
267       while (n_left_from > 0 && n_left_to_next > 0)
268         {
269           u32 pi0, next0, adj_index0;
270           ip_adjacency_t * adj0;
271           vlib_buffer_t * p0;
272
273           pi0 = to_next[0] = from[0];
274           p0 = vlib_get_buffer (vm, pi0);
275           from += 1;
276           n_left_from -= 1;
277           to_next += 1;
278           n_left_to_next -= 1;
279
280           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
281           ASSERT(adj_index0);
282
283           adj0 = adj_get(adj_index0);
284
285           if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
286           {
287               next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
288           }
289           else
290           {
291               next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
292           }              
293
294           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) 
295           {
296               mpls_adj_incomplete_trace_t *tr =
297                   vlib_add_trace (vm, node, p0, sizeof (*tr));
298               tr->next = next0;
299           }
300
301           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
302                                            to_next, n_left_to_next,
303                                            pi0, next0);
304         }
305
306       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307     }
308
309   return from_frame->n_vectors;
310 }
311
312 static u8 *
313 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
314 {
315     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
316     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
317     mpls_adj_incomplete_trace_t * t;
318     uword indent;
319
320     t = va_arg (*args, mpls_adj_incomplete_trace_t *);
321     indent = format_get_indent (s);
322
323     s = format (s, "%Unext:%d",
324                 format_white_space, indent,
325                 t->next);
326     return (s);
327 }
328
329 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
330   .function = mpls_adj_incomplete,
331   .name = "mpls-adj-incomplete",
332   .format_trace = format_mpls_adj_incomplete_trace,
333   /* Takes a vector of packets. */
334   .vector_size = sizeof (u32),
335   .n_errors = MPLS_N_ERROR,
336   .error_strings = mpls_error_strings,
337
338   .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
339   .next_nodes = {
340 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
341     foreach_mpls_adj_incomplete_next
342 #undef _
343   },
344
345   .format_trace = format_mpls_output_trace,
346 };
347
348 VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,
349                               mpls_adj_incomplete)