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