2 * mpls_output.c: MPLS Adj rewrite
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/mpls/mpls.h>
22 #include <vnet/ip/ip_frag.h>
25 /* Adjacency taken. */
28 } mpls_output_trace_t;
32 MPLS_OUTPUT_MIDCHAIN_MODE
35 #define foreach_mpls_output_next \
36 _(DROP, "error-drop") \
40 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
41 foreach_mpls_output_next
47 format_mpls_output_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
53 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
55 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
61 mpls_output_inline (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
63 vlib_frame_t * from_frame,
64 mpls_output_mode_t mode)
66 u32 n_left_from, next_index, * from, * to_next, thread_index;
67 vlib_node_runtime_t * error_node;
71 thread_index = vlib_get_thread_index();
72 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
73 from = vlib_frame_vector_args (from_frame);
74 n_left_from = from_frame->n_vectors;
75 next_index = node->cached_next_index;
78 while (n_left_from > 0)
80 vlib_get_next_frame (vm, node, next_index,
81 to_next, n_left_to_next);
83 while (n_left_from >= 4 && n_left_to_next >= 2)
85 ip_adjacency_t * adj0;
86 mpls_unicast_header_t *hdr0;
88 u32 pi0, adj_index0, next0, error0;
91 ip_adjacency_t * adj1;
92 mpls_unicast_header_t *hdr1;
94 u32 pi1, adj_index1, next1, error1;
97 /* Prefetch next iteration. */
99 vlib_buffer_t * p2, * p3;
101 p2 = vlib_get_buffer (vm, from[2]);
102 p3 = vlib_get_buffer (vm, from[3]);
104 vlib_prefetch_buffer_header (p2, STORE);
105 vlib_prefetch_buffer_header (p3, STORE);
107 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
108 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
111 pi0 = to_next[0] = from[0];
112 pi1 = to_next[1] = from[1];
119 p0 = vlib_get_buffer (vm, pi0);
120 p1 = vlib_get_buffer (vm, pi1);
122 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
123 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
125 adj0 = adj_get(adj_index0);
126 adj1 = adj_get(adj_index1);
127 hdr0 = vlib_buffer_get_current (p0);
128 hdr1 = vlib_buffer_get_current (p1);
130 /* Guess we are only writing on simple Ethernet header. */
131 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
132 sizeof (ethernet_header_t));
134 /* Update packet buffer attributes/set output interface. */
135 rw_len0 = adj0[0].rewrite_header.data_bytes;
136 rw_len1 = adj1[0].rewrite_header.data_bytes;
137 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
138 vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
140 /* Bump the adj counters for packet and bytes */
141 vlib_increment_combined_counter
142 (&adjacency_counters,
146 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
147 vlib_increment_combined_counter
148 (&adjacency_counters,
152 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
154 /* Check MTU of outgoing interface. */
155 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
156 adj0[0].rewrite_header.max_l3_packet_bytes))
158 vlib_buffer_advance(p0, -rw_len0);
160 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
161 adj0[0].rewrite_header.sw_if_index;
162 next0 = adj0[0].rewrite_header.next_index;
163 error0 = IP4_ERROR_NONE;
165 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
166 vnet_feature_arc_start (mm->output_feature_arc_index,
167 adj0[0].rewrite_header.sw_if_index,
172 error0 = IP4_ERROR_MTU_EXCEEDED;
173 next0 = MPLS_OUTPUT_NEXT_FRAG;
174 vlib_node_increment_counter (vm, mpls_output_node.index,
175 MPLS_ERROR_PKTS_NEED_FRAG,
178 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
179 adj1[0].rewrite_header.max_l3_packet_bytes))
181 vlib_buffer_advance(p1, -rw_len1);
183 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
184 adj1[0].rewrite_header.sw_if_index;
185 next1 = adj1[0].rewrite_header.next_index;
186 error1 = IP4_ERROR_NONE;
188 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
189 vnet_feature_arc_start (mm->output_feature_arc_index,
190 adj1[0].rewrite_header.sw_if_index,
195 error1 = IP4_ERROR_MTU_EXCEEDED;
196 next1 = MPLS_OUTPUT_NEXT_FRAG;
197 vlib_node_increment_counter (vm, mpls_output_node.index,
198 MPLS_ERROR_PKTS_NEED_FRAG,
201 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
203 adj0->sub_type.midchain.fixup_func
205 adj0->sub_type.midchain.fixup_data);
206 adj1->sub_type.midchain.fixup_func
208 adj1->sub_type.midchain.fixup_data);
211 p0->error = error_node->errors[error0];
212 p1->error = error_node->errors[error1];
214 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
216 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
218 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
219 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
221 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
223 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
225 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
226 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
229 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
230 to_next, n_left_to_next,
231 pi0, pi1, next0, next1);
234 while (n_left_from > 0 && n_left_to_next > 0)
236 ip_adjacency_t * adj0;
237 mpls_unicast_header_t *hdr0;
239 u32 pi0, adj_index0, next0, error0;
242 pi0 = to_next[0] = from[0];
244 p0 = vlib_get_buffer (vm, pi0);
246 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
248 adj0 = adj_get(adj_index0);
249 hdr0 = vlib_buffer_get_current (p0);
251 /* Guess we are only writing on simple Ethernet header. */
252 vnet_rewrite_one_header (adj0[0], hdr0,
253 sizeof (ethernet_header_t));
255 /* Update packet buffer attributes/set output interface. */
256 rw_len0 = adj0[0].rewrite_header.data_bytes;
257 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
259 vlib_increment_combined_counter
260 (&adjacency_counters,
264 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
266 /* Check MTU of outgoing interface. */
267 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
268 adj0[0].rewrite_header.max_l3_packet_bytes))
270 vlib_buffer_advance(p0, -rw_len0);
272 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
273 adj0[0].rewrite_header.sw_if_index;
274 next0 = adj0[0].rewrite_header.next_index;
275 error0 = IP4_ERROR_NONE;
277 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
278 vnet_feature_arc_start (mm->output_feature_arc_index,
279 adj0[0].rewrite_header.sw_if_index,
284 error0 = IP4_ERROR_MTU_EXCEEDED;
285 next0 = MPLS_OUTPUT_NEXT_FRAG;
286 vlib_node_increment_counter (vm, mpls_output_node.index,
287 MPLS_ERROR_PKTS_NEED_FRAG,
290 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
292 adj0->sub_type.midchain.fixup_func
294 adj0->sub_type.midchain.fixup_data);
297 p0->error = error_node->errors[error0];
304 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
306 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
308 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
309 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
312 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
313 to_next, n_left_to_next,
317 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
319 vlib_node_increment_counter (vm, mpls_output_node.index,
320 MPLS_ERROR_PKTS_ENCAP,
321 from_frame->n_vectors);
323 return from_frame->n_vectors;
326 static char * mpls_error_strings[] = {
327 #define mpls_error(n,s) s,
332 VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm,
333 vlib_node_runtime_t * node,
334 vlib_frame_t * from_frame)
336 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MODE));
339 VLIB_REGISTER_NODE (mpls_output_node) = {
340 .name = "mpls-output",
341 /* Takes a vector of packets. */
342 .vector_size = sizeof (u32),
343 .n_errors = MPLS_N_ERROR,
344 .error_strings = mpls_error_strings,
346 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
348 [MPLS_OUTPUT_NEXT_DROP] = "mpls-drop",
349 [MPLS_OUTPUT_NEXT_FRAG] = "mpls-frag",
351 .format_trace = format_mpls_output_trace,
354 VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm,
355 vlib_node_runtime_t * node,
356 vlib_frame_t * from_frame)
358 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MIDCHAIN_MODE));
361 VLIB_REGISTER_NODE (mpls_midchain_node) = {
362 .name = "mpls-midchain",
363 .vector_size = sizeof (u32),
365 .n_errors = MPLS_N_ERROR,
366 .error_strings = mpls_error_strings,
368 .sibling_of = "mpls-output",
369 .format_trace = format_mpls_output_trace,
372 static char *mpls_frag_error_strings[] = {
373 #define _(sym,string) string,
374 foreach_ip_frag_error
378 typedef struct mpls_frag_trace_t_
386 MPLS_FRAG_NEXT_REWRITE,
387 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN,
388 MPLS_FRAG_NEXT_ICMP_ERROR,
394 mpls_frag (vlib_main_t * vm,
395 vlib_node_runtime_t * node,
396 vlib_frame_t * frame)
398 u32 n_left_from, next_index, * from, * to_next, n_left_to_next, *frags;
399 vlib_node_runtime_t * error_node;
401 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
402 from = vlib_frame_vector_args (frame);
403 n_left_from = frame->n_vectors;
404 next_index = node->cached_next_index;
407 while (n_left_from > 0)
409 vlib_get_next_frame (vm, node, next_index,
410 to_next, n_left_to_next);
412 while (n_left_from > 0 && n_left_to_next > 0)
414 ip_adjacency_t * adj0;
416 mpls_frag_next_t next0;
418 ip_frag_error_t error0 = IP_FRAG_ERROR_NONE;
422 pi0 = to_next[0] = from[0];
423 p0 = vlib_get_buffer (vm, pi0);
426 is_ip4 = vnet_buffer (p0)->mpls.pyld_proto == DPO_PROTO_IP4;
428 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
429 adj0 = adj_get(adj_index0);
431 /* the size of the MPLS stack */
432 encap_size = vnet_buffer(p0)->l3_hdr_offset - p0->current_data;
434 /* IP fragmentation */
436 error0 = ip4_frag_do_fragment (vm, pi0,
437 adj0->rewrite_header.max_l3_packet_bytes,
440 error0 = ip6_frag_do_fragment (vm, pi0,
441 adj0->rewrite_header.max_l3_packet_bytes,
444 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
446 mpls_frag_trace_t *tr =
447 vlib_add_trace (vm, node, p0, sizeof (*tr));
448 tr->mtu = adj0->rewrite_header.max_l3_packet_bytes;
449 tr->pkt_size = vlib_buffer_length_in_chain(vm, p0);
452 if (PREDICT_TRUE(error0 == IP_FRAG_ERROR_NONE))
454 /* Free original buffer chain */
455 vlib_buffer_free_one (vm, pi0); /* Free original packet */
456 next0 = (IP_LOOKUP_NEXT_MIDCHAIN == adj0->lookup_next_index ?
457 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN :
458 MPLS_FRAG_NEXT_REWRITE);
460 else if (is_ip4 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
462 icmp4_error_set_vnet_buffer (
463 p0, ICMP4_destination_unreachable,
464 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
465 vnet_buffer (p0)->ip_frag.mtu);
466 next0 = MPLS_FRAG_NEXT_ICMP_ERROR;
470 vlib_error_count (vm, next_index, error0, 1);
471 vec_add1 (frags, pi0); /* Get rid of the original buffer */
472 next0 = MPLS_FRAG_NEXT_DROP;
475 /* Send fragments that were added in the frame */
476 u32 *frag_from, frag_left;
479 frag_left = vec_len (frags);
481 while (frag_left > 0)
483 while (frag_left > 0 && n_left_to_next > 0)
486 i = to_next[0] = frag_from[0];
492 p0 = vlib_get_buffer (vm, i);
493 p0->error = error_node->errors[error0];
495 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
496 to_next, n_left_to_next, i,
499 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
500 vlib_get_next_frame (vm, node, next_index, to_next,
503 vec_reset_length (frags);
505 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
509 return frame->n_vectors;
513 format_mpls_frag_trace (u8 * s, va_list * args)
515 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
516 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
517 mpls_frag_trace_t *t = va_arg (*args, mpls_frag_trace_t *);
519 s = format (s, "mtu:%d pkt-size:%d", t->mtu, t->pkt_size);
523 VLIB_REGISTER_NODE (mpls_frag_node) = {
524 .function = mpls_frag,
526 .vector_size = sizeof (u32),
527 .format_trace = format_mpls_frag_trace,
528 .type = VLIB_NODE_TYPE_INTERNAL,
530 .n_errors = IP_FRAG_N_ERROR,
531 .error_strings = mpls_frag_error_strings,
533 .n_next_nodes = MPLS_FRAG_N_NEXT,
535 [MPLS_FRAG_NEXT_REWRITE] = "mpls-output",
536 [MPLS_FRAG_NEXT_REWRITE_MIDCHAIN] = "mpls-midchain",
537 [MPLS_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
538 [MPLS_FRAG_NEXT_DROP] = "mpls-drop"
543 * @brief Next index values from the MPLS incomplete adj node
545 #define foreach_mpls_adj_incomplete_next \
546 _(DROP, "error-drop") \
548 _(IP6, "ip6-discover-neighbor")
551 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
552 foreach_mpls_adj_incomplete_next
554 MPLS_ADJ_INCOMPLETE_N_NEXT,
555 } mpls_adj_incomplete_next_t;
558 * @brief A struct to hold tracing information for the MPLS label imposition
561 typedef struct mpls_adj_incomplete_trace_t_
564 } mpls_adj_incomplete_trace_t;
568 * @brief Graph node for incomplete MPLS adjacency.
569 * This node will push traffic to either the v4-arp or v6-nd node
570 * based on the next-hop proto of the adj.
571 * We pay a cost for this 'routing' node, but an incomplete adj is the
574 VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm,
575 vlib_node_runtime_t * node,
576 vlib_frame_t * from_frame)
578 u32 n_left_from, next_index, * from, * to_next;
580 from = vlib_frame_vector_args (from_frame);
581 n_left_from = from_frame->n_vectors;
582 next_index = node->cached_next_index;
584 while (n_left_from > 0)
588 vlib_get_next_frame (vm, node, next_index,
589 to_next, n_left_to_next);
591 while (n_left_from > 0 && n_left_to_next > 0)
593 u32 pi0, next0, adj_index0;
594 ip_adjacency_t * adj0;
597 pi0 = to_next[0] = from[0];
598 p0 = vlib_get_buffer (vm, pi0);
604 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
606 adj0 = adj_get(adj_index0);
608 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
610 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
614 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
617 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
619 mpls_adj_incomplete_trace_t *tr =
620 vlib_add_trace (vm, node, p0, sizeof (*tr));
624 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
625 to_next, n_left_to_next,
629 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
632 return from_frame->n_vectors;
636 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
638 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
639 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
640 mpls_adj_incomplete_trace_t * t;
643 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
644 indent = format_get_indent (s);
646 s = format (s, "%Unext:%d",
647 format_white_space, indent,
652 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
653 .name = "mpls-adj-incomplete",
654 .format_trace = format_mpls_adj_incomplete_trace,
655 /* Takes a vector of packets. */
656 .vector_size = sizeof (u32),
657 .n_errors = MPLS_N_ERROR,
658 .error_strings = mpls_error_strings,
660 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
662 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
663 foreach_mpls_adj_incomplete_next