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/ip/ip.h>
20 #include <vnet/mpls/mpls.h>
21 #include <vnet/ip/ip_frag.h>
22 #include <vnet/adj/adj_dp.h>
23 #include <vnet/mpls/mpls.api_enum.h>
26 /* Adjacency taken. */
29 } mpls_output_trace_t;
33 MPLS_OUTPUT_MIDCHAIN_MODE
36 #define foreach_mpls_output_next \
37 _(DROP, "error-drop") \
41 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
42 foreach_mpls_output_next
48 format_mpls_output_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
54 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
56 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
62 mpls_output_inline (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * from_frame,
65 mpls_output_mode_t mode)
67 u32 n_left_from, next_index, * from, * to_next, thread_index;
68 vlib_node_runtime_t * error_node;
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;
79 while (n_left_from > 0)
81 vlib_get_next_frame (vm, node, next_index,
82 to_next, n_left_to_next);
84 while (n_left_from >= 4 && n_left_to_next >= 2)
86 ip_adjacency_t * adj0;
87 mpls_unicast_header_t *hdr0;
89 u32 pi0, adj_index0, next0, error0;
92 ip_adjacency_t * adj1;
93 mpls_unicast_header_t *hdr1;
95 u32 pi1, adj_index1, next1, error1;
98 /* Prefetch next iteration. */
100 vlib_buffer_t * p2, * p3;
102 p2 = vlib_get_buffer (vm, from[2]);
103 p3 = vlib_get_buffer (vm, from[3]);
105 vlib_prefetch_buffer_header (p2, STORE);
106 vlib_prefetch_buffer_header (p3, STORE);
108 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
109 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
112 pi0 = to_next[0] = from[0];
113 pi1 = to_next[1] = from[1];
120 p0 = vlib_get_buffer (vm, pi0);
121 p1 = vlib_get_buffer (vm, pi1);
123 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
124 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
126 adj0 = adj_get(adj_index0);
127 adj1 = adj_get(adj_index1);
128 hdr0 = vlib_buffer_get_current (p0);
129 hdr1 = vlib_buffer_get_current (p1);
131 /* Guess we are only writing on simple Ethernet header. */
132 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
133 sizeof (ethernet_header_t));
135 /* Update packet buffer attributes/set output interface. */
136 rw_len0 = adj0[0].rewrite_header.data_bytes;
137 rw_len1 = adj1[0].rewrite_header.data_bytes;
138 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
139 vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
141 /* Bump the adj counters for packet and bytes */
142 vlib_increment_combined_counter
143 (&adjacency_counters,
147 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
148 vlib_increment_combined_counter
149 (&adjacency_counters,
153 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
155 /* Check MTU of outgoing interface. */
156 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
157 adj0[0].rewrite_header.max_l3_packet_bytes))
159 vlib_buffer_advance(p0, -rw_len0);
161 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
162 adj0[0].rewrite_header.sw_if_index;
163 next0 = adj0[0].rewrite_header.next_index;
164 error0 = IP4_ERROR_NONE;
166 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
167 vnet_feature_arc_start (mm->output_feature_arc_index,
168 adj0[0].rewrite_header.sw_if_index,
173 error0 = IP4_ERROR_MTU_EXCEEDED;
174 next0 = MPLS_OUTPUT_NEXT_FRAG;
175 vlib_node_increment_counter (vm, mpls_output_node.index,
176 MPLS_ERROR_PKTS_NEED_FRAG,
179 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
180 adj1[0].rewrite_header.max_l3_packet_bytes))
182 vlib_buffer_advance(p1, -rw_len1);
184 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
185 adj1[0].rewrite_header.sw_if_index;
186 next1 = adj1[0].rewrite_header.next_index;
187 error1 = IP4_ERROR_NONE;
189 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
190 vnet_feature_arc_start (mm->output_feature_arc_index,
191 adj1[0].rewrite_header.sw_if_index,
196 error1 = IP4_ERROR_MTU_EXCEEDED;
197 next1 = MPLS_OUTPUT_NEXT_FRAG;
198 vlib_node_increment_counter (vm, mpls_output_node.index,
199 MPLS_ERROR_PKTS_NEED_FRAG,
202 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
204 adj_midchain_fixup (vm, adj0, p0, VNET_LINK_MPLS);
205 adj_midchain_fixup (vm, adj1, p1, VNET_LINK_MPLS);
208 p0->error = error_node->errors[error0];
209 p1->error = error_node->errors[error1];
211 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
213 mpls_output_trace_t *tr =
214 vlib_add_trace (vm, node, p0, sizeof (*tr));
215 tr->adj_index = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
216 tr->flow_hash = vnet_buffer (p0)->ip.flow_hash;
218 if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
220 mpls_output_trace_t *tr =
221 vlib_add_trace (vm, node, p1, sizeof (*tr));
222 tr->adj_index = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
223 tr->flow_hash = vnet_buffer (p1)->ip.flow_hash;
226 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
227 n_left_to_next, pi0, pi1, next0,
231 while (n_left_from > 0 && n_left_to_next > 0)
233 ip_adjacency_t * adj0;
234 mpls_unicast_header_t *hdr0;
236 u32 pi0, adj_index0, next0, error0;
239 pi0 = to_next[0] = from[0];
241 p0 = vlib_get_buffer (vm, pi0);
243 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
245 adj0 = adj_get(adj_index0);
246 hdr0 = vlib_buffer_get_current (p0);
248 /* Guess we are only writing on simple Ethernet header. */
249 vnet_rewrite_one_header (adj0[0], hdr0,
250 sizeof (ethernet_header_t));
252 /* Update packet buffer attributes/set output interface. */
253 rw_len0 = adj0[0].rewrite_header.data_bytes;
254 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
256 vlib_increment_combined_counter
257 (&adjacency_counters,
261 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
263 /* Check MTU of outgoing interface. */
264 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
265 adj0[0].rewrite_header.max_l3_packet_bytes))
267 vlib_buffer_advance(p0, -rw_len0);
269 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
270 adj0[0].rewrite_header.sw_if_index;
271 next0 = adj0[0].rewrite_header.next_index;
272 error0 = IP4_ERROR_NONE;
274 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
275 vnet_feature_arc_start (mm->output_feature_arc_index,
276 adj0[0].rewrite_header.sw_if_index,
281 error0 = IP4_ERROR_MTU_EXCEEDED;
282 next0 = MPLS_OUTPUT_NEXT_FRAG;
283 vlib_node_increment_counter (vm, mpls_output_node.index,
284 MPLS_ERROR_PKTS_NEED_FRAG,
287 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
289 adj_midchain_fixup (vm, adj0, p0, VNET_LINK_MPLS);
292 p0->error = error_node->errors[error0];
299 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
301 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
303 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
304 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
307 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308 to_next, n_left_to_next,
312 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
314 vlib_node_increment_counter (vm, mpls_output_node.index,
315 MPLS_ERROR_PKTS_ENCAP,
316 from_frame->n_vectors);
318 return from_frame->n_vectors;
321 VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm,
322 vlib_node_runtime_t * node,
323 vlib_frame_t * from_frame)
325 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MODE));
328 VLIB_REGISTER_NODE (mpls_output_node) = {
329 .name = "mpls-output",
330 /* Takes a vector of packets. */
331 .vector_size = sizeof (u32),
332 .n_errors = MPLS_N_ERROR,
333 .error_counters = mpls_error_counters,
335 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
337 [MPLS_OUTPUT_NEXT_DROP] = "mpls-drop",
338 [MPLS_OUTPUT_NEXT_FRAG] = "mpls-frag",
340 .format_trace = format_mpls_output_trace,
343 VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm,
344 vlib_node_runtime_t * node,
345 vlib_frame_t * from_frame)
347 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MIDCHAIN_MODE));
350 VLIB_REGISTER_NODE (mpls_midchain_node) = {
351 .name = "mpls-midchain",
352 .vector_size = sizeof (u32),
354 .n_errors = MPLS_N_ERROR,
355 .error_counters = mpls_error_counters,
357 .sibling_of = "mpls-output",
358 .format_trace = format_mpls_output_trace,
361 typedef struct mpls_frag_trace_t_
369 MPLS_FRAG_NEXT_REWRITE,
370 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN,
371 MPLS_FRAG_NEXT_ICMP4_ERROR,
372 MPLS_FRAG_NEXT_ICMP6_ERROR,
378 mpls_frag (vlib_main_t * vm,
379 vlib_node_runtime_t * node,
380 vlib_frame_t * frame)
382 u32 n_left_from, next_index, * from, * to_next, n_left_to_next, *frags;
384 from = vlib_frame_vector_args (frame);
385 n_left_from = frame->n_vectors;
386 next_index = node->cached_next_index;
389 while (n_left_from > 0)
391 vlib_get_next_frame (vm, node, next_index,
392 to_next, n_left_to_next);
394 while (n_left_from > 0 && n_left_to_next > 0)
396 ip_adjacency_t * adj0;
398 mpls_frag_next_t next0;
400 ip_frag_error_t error0 = IP_FRAG_ERROR_NONE;
404 pi0 = to_next[0] = from[0];
405 p0 = vlib_get_buffer (vm, pi0);
408 is_ip4 = vnet_buffer (p0)->mpls.pyld_proto == DPO_PROTO_IP4;
410 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
411 adj0 = adj_get (adj_index0);
413 /* the size of the MPLS stack */
414 encap_size = vnet_buffer (p0)->l3_hdr_offset - p0->current_data;
415 mtu = adj0->rewrite_header.max_l3_packet_bytes - encap_size;
417 /* IP fragmentation */
419 error0 = ip4_frag_do_fragment (vm, pi0, mtu, encap_size, &frags);
422 if (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED))
424 /* only fragment locally generated IPv6 */
425 error0 = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
430 ip6_frag_do_fragment (vm, pi0, mtu, encap_size, &frags);
434 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
436 mpls_frag_trace_t *tr =
437 vlib_add_trace (vm, node, p0, sizeof (*tr));
439 tr->pkt_size = vlib_buffer_length_in_chain (vm, p0);
442 if (PREDICT_TRUE (error0 == IP_FRAG_ERROR_NONE))
444 /* Free original buffer chain */
445 vlib_buffer_free_one (vm, pi0);
446 next0 = (IP_LOOKUP_NEXT_MIDCHAIN == adj0->lookup_next_index ?
447 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN :
448 MPLS_FRAG_NEXT_REWRITE);
452 vlib_error_count (vm, node->node_index, error0, 1);
454 if (error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
456 vlib_buffer_advance (p0, encap_size);
459 icmp4_error_set_vnet_buffer (
460 p0, ICMP4_destination_unreachable,
461 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
463 next0 = MPLS_FRAG_NEXT_ICMP4_ERROR;
467 icmp6_error_set_vnet_buffer (p0, ICMP6_packet_too_big,
469 next0 = MPLS_FRAG_NEXT_ICMP6_ERROR;
474 next0 = MPLS_FRAG_NEXT_DROP;
477 /* Get rid of the original buffer */
478 vec_add1 (frags, pi0);
481 /* Send fragments that were added in the frame */
482 u32 *frag_from, frag_left;
485 frag_left = vec_len (frags);
487 while (frag_left > 0)
489 while (frag_left > 0 && n_left_to_next > 0)
492 i = to_next[0] = frag_from[0];
498 vlib_validate_buffer_enqueue_x1 (
499 vm, node, next_index, to_next, n_left_to_next, i, next0);
501 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
502 vlib_get_next_frame (vm, node, next_index, to_next,
505 vec_reset_length (frags);
507 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
511 return frame->n_vectors;
515 format_mpls_frag_trace (u8 * s, va_list * args)
517 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
518 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
519 mpls_frag_trace_t *t = va_arg (*args, mpls_frag_trace_t *);
521 s = format (s, "mtu:%d pkt-size:%d", t->mtu, t->pkt_size);
525 VLIB_REGISTER_NODE (mpls_frag_node) = {
526 .function = mpls_frag,
528 .vector_size = sizeof (u32),
529 .format_trace = format_mpls_frag_trace,
530 .type = VLIB_NODE_TYPE_INTERNAL,
532 .n_errors = IP_FRAG_N_ERROR,
533 .error_counters = ip_frag_error_counters,
535 .n_next_nodes = MPLS_FRAG_N_NEXT,
536 .next_nodes = { [MPLS_FRAG_NEXT_REWRITE] = "mpls-output",
537 [MPLS_FRAG_NEXT_REWRITE_MIDCHAIN] = "mpls-midchain",
538 [MPLS_FRAG_NEXT_ICMP4_ERROR] = "ip4-icmp-error",
539 [MPLS_FRAG_NEXT_ICMP6_ERROR] = "ip6-icmp-error",
540 [MPLS_FRAG_NEXT_DROP] = "mpls-drop" },
544 * @brief Next index values from the MPLS incomplete adj node
546 #define foreach_mpls_adj_incomplete_next \
547 _(DROP, "error-drop") \
549 _(IP6, "ip6-discover-neighbor")
552 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
553 foreach_mpls_adj_incomplete_next
555 MPLS_ADJ_INCOMPLETE_N_NEXT,
556 } mpls_adj_incomplete_next_t;
559 * @brief A struct to hold tracing information for the MPLS label imposition
562 typedef struct mpls_adj_incomplete_trace_t_
565 } mpls_adj_incomplete_trace_t;
569 * @brief Graph node for incomplete MPLS adjacency.
570 * This node will push traffic to either the v4-arp or v6-nd node
571 * based on the next-hop proto of the adj.
572 * We pay a cost for this 'routing' node, but an incomplete adj is the
575 VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm,
576 vlib_node_runtime_t * node,
577 vlib_frame_t * from_frame)
579 u32 n_left_from, next_index, * from, * to_next;
581 from = vlib_frame_vector_args (from_frame);
582 n_left_from = from_frame->n_vectors;
583 next_index = node->cached_next_index;
585 while (n_left_from > 0)
589 vlib_get_next_frame (vm, node, next_index,
590 to_next, n_left_to_next);
592 while (n_left_from > 0 && n_left_to_next > 0)
594 u32 pi0, next0, adj_index0;
595 ip_adjacency_t * adj0;
598 pi0 = to_next[0] = from[0];
599 p0 = vlib_get_buffer (vm, pi0);
605 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
607 adj0 = adj_get(adj_index0);
609 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
611 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
615 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
618 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
620 mpls_adj_incomplete_trace_t *tr =
621 vlib_add_trace (vm, node, p0, sizeof (*tr));
625 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
626 to_next, n_left_to_next,
630 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
633 return from_frame->n_vectors;
637 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
639 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
640 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
641 mpls_adj_incomplete_trace_t * t;
644 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
645 indent = format_get_indent (s);
647 s = format (s, "%Unext:%d",
648 format_white_space, indent,
653 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
654 .name = "mpls-adj-incomplete",
655 .format_trace = format_mpls_adj_incomplete_trace,
656 /* Takes a vector of packets. */
657 .vector_size = sizeof (u32),
658 .n_errors = MPLS_N_ERROR,
659 .error_counters = mpls_error_counters,
661 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
663 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
664 foreach_mpls_adj_incomplete_next