Cache a 'has-features' flag on the adjacency for faster access. Reclaim the node_inde...
[vpp.git] / src / 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/ip/ip.h>
21 #include <vnet/mpls/mpls.h>
22
23 typedef struct {
24   /* Adjacency taken. */
25   u32 adj_index;
26   u32 flow_hash;
27
28   /* Packet data, possibly *after* rewrite. */
29   u8 packet_data[64 - 1*sizeof(u32)];
30 } mpls_output_trace_t;
31
32 static u8 *
33 format_mpls_output_trace (u8 * s, va_list * args)
34 {
35   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37   mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
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               t->adj_index, t->packet_data, sizeof (t->packet_data));
48   return s;
49 }
50
51 static inline uword
52 mpls_output_inline (vlib_main_t * vm,
53                     vlib_node_runtime_t * node,
54                     vlib_frame_t * from_frame,
55                     int is_midchain)
56 {
57   u32 n_left_from, next_index, * from, * to_next, cpu_index;
58   vlib_node_runtime_t * error_node;
59   u32 n_left_to_next;
60   mpls_main_t *mm;
61
62   cpu_index = os_get_cpu_number();
63   error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
64   from = vlib_frame_vector_args (from_frame);
65   n_left_from = from_frame->n_vectors;
66   next_index = node->cached_next_index;
67   mm = &mpls_main;
68
69   while (n_left_from > 0)
70     {
71       vlib_get_next_frame (vm, node, next_index,
72                            to_next, n_left_to_next);
73
74       while (n_left_from >= 4 && n_left_to_next >= 2)
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           ip_adjacency_t * adj1;
82           mpls_unicast_header_t *hdr1;
83           vlib_buffer_t * p1;
84           u32 pi1, rw_len1, adj_index1, next1, error1;
85
86           /* Prefetch next iteration. */
87           {
88             vlib_buffer_t * p2, * p3;
89
90             p2 = vlib_get_buffer (vm, from[2]);
91             p3 = vlib_get_buffer (vm, from[3]);
92
93             vlib_prefetch_buffer_header (p2, STORE);
94             vlib_prefetch_buffer_header (p3, STORE);
95
96             CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
97             CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
98           }
99
100           pi0 = to_next[0] = from[0];
101           pi1 = to_next[1] = from[1];
102
103           from += 2;
104           n_left_from -= 2;
105           to_next += 2;
106           n_left_to_next -= 2;
107
108           p0 = vlib_get_buffer (vm, pi0);
109           p1 = vlib_get_buffer (vm, pi1);
110
111           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
112           adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
113
114           /* We should never rewrite a pkt using the MISS adjacency */
115           ASSERT(adj_index0);
116           ASSERT(adj_index1);
117
118           adj0 = adj_get(adj_index0);
119           adj1 = adj_get(adj_index1);
120           hdr0 = vlib_buffer_get_current (p0);
121           hdr1 = vlib_buffer_get_current (p1);
122
123           /* Guess we are only writing on simple Ethernet header. */
124           vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
125                                    sizeof (ethernet_header_t));
126
127           /* Update packet buffer attributes/set output interface. */
128           rw_len0 = adj0[0].rewrite_header.data_bytes;
129           rw_len1 = adj1[0].rewrite_header.data_bytes;
130
131           /* Bump the adj counters for packet and bytes */
132           vlib_increment_combined_counter
133               (&adjacency_counters,
134                cpu_index,
135                adj_index0,
136                1,
137                vlib_buffer_length_in_chain (vm, p0) + rw_len0);
138           vlib_increment_combined_counter
139               (&adjacency_counters,
140                cpu_index,
141                adj_index1,
142                1,
143                vlib_buffer_length_in_chain (vm, p1) + rw_len1);
144
145           /* Check MTU of outgoing interface. */
146           if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
147                            adj0[0].rewrite_header.max_l3_packet_bytes))
148             {
149               p0->current_data -= rw_len0;
150               p0->current_length += rw_len0;
151
152               vnet_buffer (p0)->sw_if_index[VLIB_TX] =
153                   adj0[0].rewrite_header.sw_if_index;
154               next0 = adj0[0].rewrite_header.next_index;
155               error0 = IP4_ERROR_NONE;
156
157               if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
158                 vnet_feature_arc_start (mm->output_feature_arc_index,
159                                         adj0[0].rewrite_header.sw_if_index,
160                                         &next0, p0);
161             }
162           else
163             {
164               error0 = IP4_ERROR_MTU_EXCEEDED;
165               next0 = MPLS_OUTPUT_NEXT_DROP;
166             }
167           if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
168                            adj1[0].rewrite_header.max_l3_packet_bytes))
169             {
170               p1->current_data -= rw_len1;
171               p1->current_length += rw_len1;
172
173               vnet_buffer (p1)->sw_if_index[VLIB_TX] =
174                   adj1[0].rewrite_header.sw_if_index;
175               next1 = adj1[0].rewrite_header.next_index;
176               error1 = IP4_ERROR_NONE;
177
178               if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
179                 vnet_feature_arc_start (mm->output_feature_arc_index,
180                                         adj1[0].rewrite_header.sw_if_index,
181                                         &next1, p1);
182             }
183           else
184             {
185               error1 = IP4_ERROR_MTU_EXCEEDED;
186               next1 = MPLS_OUTPUT_NEXT_DROP;
187             }
188           if (is_midchain)
189           {
190               adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
191               adj1->sub_type.midchain.fixup_func(vm, adj1, p1);
192           }
193
194           p0->error = error_node->errors[error0];
195           p1->error = error_node->errors[error1];
196
197           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
198             {
199               mpls_output_trace_t *tr = vlib_add_trace (vm, node,
200                                                         p0, sizeof (*tr));
201               tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
202               tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
203             }
204           if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
205             {
206               mpls_output_trace_t *tr = vlib_add_trace (vm, node,
207                                                         p1, sizeof (*tr));
208               tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
209               tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
210             }
211
212           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
213                                            to_next, n_left_to_next,
214                                            pi0, pi1, next0, next1);
215         }
216
217       while (n_left_from > 0 && n_left_to_next > 0)
218         {
219           ip_adjacency_t * adj0;
220           mpls_unicast_header_t *hdr0;
221           vlib_buffer_t * p0;
222           u32 pi0, rw_len0, adj_index0, next0, error0;
223
224           pi0 = to_next[0] = from[0];
225
226           p0 = vlib_get_buffer (vm, pi0);
227
228           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
229
230           /* We should never rewrite a pkt using the MISS adjacency */
231           ASSERT(adj_index0);
232
233           adj0 = adj_get(adj_index0);
234           hdr0 = vlib_buffer_get_current (p0);
235
236           /* Guess we are only writing on simple Ethernet header. */
237           vnet_rewrite_one_header (adj0[0], hdr0, 
238                                    sizeof (ethernet_header_t));
239           
240           /* Update packet buffer attributes/set output interface. */
241           rw_len0 = adj0[0].rewrite_header.data_bytes;
242           
243           vlib_increment_combined_counter
244               (&adjacency_counters,
245                cpu_index,
246                adj_index0,
247                1,
248                vlib_buffer_length_in_chain (vm, p0) + rw_len0);
249           
250           /* Check MTU of outgoing interface. */
251           if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
252                            adj0[0].rewrite_header.max_l3_packet_bytes))
253             {
254               p0->current_data -= rw_len0;
255               p0->current_length += rw_len0;
256
257               vnet_buffer (p0)->sw_if_index[VLIB_TX] =
258                   adj0[0].rewrite_header.sw_if_index;
259               next0 = adj0[0].rewrite_header.next_index;
260               error0 = IP4_ERROR_NONE;
261
262               if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
263                 vnet_feature_arc_start (mm->output_feature_arc_index,
264                                         adj0[0].rewrite_header.sw_if_index,
265                                         &next0, p0);
266             }
267           else
268             {
269               error0 = IP4_ERROR_MTU_EXCEEDED;
270               next0 = MPLS_OUTPUT_NEXT_DROP;
271             }
272           if (is_midchain)
273           {
274               adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
275           }
276
277           p0->error = error_node->errors[error0];
278
279           from += 1;
280           n_left_from -= 1;
281           to_next += 1;
282           n_left_to_next -= 1;
283       
284           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) 
285             {
286               mpls_output_trace_t *tr = vlib_add_trace (vm, node, 
287                                                         p0, sizeof (*tr));
288               tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
289               tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
290             }
291
292           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
293                                            to_next, n_left_to_next,
294                                            pi0, next0);
295         }
296
297       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
298     }
299   vlib_node_increment_counter (vm, mpls_output_node.index,
300                                MPLS_ERROR_PKTS_ENCAP,
301                                from_frame->n_vectors);
302
303   return from_frame->n_vectors;
304 }
305
306 static char * mpls_error_strings[] = {
307 #define mpls_error(n,s) s,
308 #include "error.def"
309 #undef mpls_error
310 };
311
312 static inline uword
313 mpls_output (vlib_main_t * vm,
314              vlib_node_runtime_t * node,
315              vlib_frame_t * from_frame)
316 {
317     return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
318 }
319
320 VLIB_REGISTER_NODE (mpls_output_node) = {
321   .function = mpls_output,
322   .name = "mpls-output",
323   /* Takes a vector of packets. */
324   .vector_size = sizeof (u32),
325   .n_errors = MPLS_N_ERROR,
326   .error_strings = mpls_error_strings,
327
328   .n_next_nodes = MPLS_OUTPUT_N_NEXT,
329   .next_nodes = {
330 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
331     foreach_mpls_output_next
332 #undef _
333   },
334
335   .format_trace = format_mpls_output_trace,
336 };
337
338 VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
339
340 static inline uword
341 mpls_midchain (vlib_main_t * vm,
342                vlib_node_runtime_t * node,
343                vlib_frame_t * from_frame)
344 {
345     return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
346 }
347
348 VLIB_REGISTER_NODE (mpls_midchain_node) = {
349   .function = mpls_midchain,
350   .name = "mpls-midchain",
351   .vector_size = sizeof (u32),
352
353   .format_trace = format_mpls_output_trace,
354
355   .sibling_of = "mpls-output",
356 };
357
358 VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
359
360 /**
361  * @brief Next index values from the MPLS incomplete adj node
362  */
363 #define foreach_mpls_adj_incomplete_next        \
364 _(DROP, "error-drop")                   \
365 _(IP4,  "ip4-arp")                      \
366 _(IP6,  "ip6-discover-neighbor")
367
368 typedef enum {
369 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
370   foreach_mpls_adj_incomplete_next
371 #undef _
372   MPLS_ADJ_INCOMPLETE_N_NEXT,
373 } mpls_adj_incomplete_next_t;
374
375 /**
376  * @brief A struct to hold tracing information for the MPLS label imposition
377  * node.
378  */
379 typedef struct mpls_adj_incomplete_trace_t_
380 {
381     u32 next;
382 } mpls_adj_incomplete_trace_t;
383
384
385 /**
386  * @brief Graph node for incomplete MPLS adjacency.
387  * This node will push traffic to either the v4-arp or v6-nd node
388  * based on the next-hop proto of the adj.
389  * We pay a cost for this 'routing' node, but an incomplete adj is the
390  * exception case.
391  */
392 static inline uword
393 mpls_adj_incomplete (vlib_main_t * vm,
394                      vlib_node_runtime_t * node,
395                      vlib_frame_t * from_frame)
396 {
397     u32 n_left_from, next_index, * from, * to_next;
398
399   from = vlib_frame_vector_args (from_frame);
400   n_left_from = from_frame->n_vectors;
401   next_index = node->cached_next_index;
402
403   while (n_left_from > 0)
404     {
405       u32 n_left_to_next;
406
407       vlib_get_next_frame (vm, node, next_index,
408                            to_next, n_left_to_next);
409
410       while (n_left_from > 0 && n_left_to_next > 0)
411         {
412           u32 pi0, next0, adj_index0;
413           ip_adjacency_t * adj0;
414           vlib_buffer_t * p0;
415
416           pi0 = to_next[0] = from[0];
417           p0 = vlib_get_buffer (vm, pi0);
418           from += 1;
419           n_left_from -= 1;
420           to_next += 1;
421           n_left_to_next -= 1;
422
423           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
424           ASSERT(adj_index0);
425
426           adj0 = adj_get(adj_index0);
427
428           if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
429           {
430               next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
431           }
432           else
433           {
434               next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
435           }              
436
437           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) 
438           {
439               mpls_adj_incomplete_trace_t *tr =
440                   vlib_add_trace (vm, node, p0, sizeof (*tr));
441               tr->next = next0;
442           }
443
444           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
445                                            to_next, n_left_to_next,
446                                            pi0, next0);
447         }
448
449       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
450     }
451
452   return from_frame->n_vectors;
453 }
454
455 static u8 *
456 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
457 {
458     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
459     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
460     mpls_adj_incomplete_trace_t * t;
461     uword indent;
462
463     t = va_arg (*args, mpls_adj_incomplete_trace_t *);
464     indent = format_get_indent (s);
465
466     s = format (s, "%Unext:%d",
467                 format_white_space, indent,
468                 t->next);
469     return (s);
470 }
471
472 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
473   .function = mpls_adj_incomplete,
474   .name = "mpls-adj-incomplete",
475   .format_trace = format_mpls_adj_incomplete_trace,
476   /* Takes a vector of packets. */
477   .vector_size = sizeof (u32),
478   .n_errors = MPLS_N_ERROR,
479   .error_strings = mpls_error_strings,
480
481   .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
482   .next_nodes = {
483 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
484     foreach_mpls_adj_incomplete_next
485 #undef _
486   },
487 };
488
489 VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,
490                               mpls_adj_incomplete)