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