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>
24 /* Adjacency taken. */
27 } mpls_output_trace_t;
29 #define foreach_mpls_output_next \
33 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
34 foreach_mpls_output_next
40 format_mpls_output_trace (u8 * s, va_list * args)
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 *);
46 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
48 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
54 mpls_output_inline (vlib_main_t * vm,
55 vlib_node_runtime_t * node,
56 vlib_frame_t * from_frame,
59 u32 n_left_from, next_index, * from, * to_next, thread_index;
60 vlib_node_runtime_t * error_node;
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;
71 while (n_left_from > 0)
73 vlib_get_next_frame (vm, node, next_index,
74 to_next, n_left_to_next);
76 while (n_left_from >= 4 && n_left_to_next >= 2)
78 ip_adjacency_t * adj0;
79 mpls_unicast_header_t *hdr0;
81 u32 pi0, rw_len0, adj_index0, next0, error0;
83 ip_adjacency_t * adj1;
84 mpls_unicast_header_t *hdr1;
86 u32 pi1, rw_len1, adj_index1, next1, error1;
88 /* Prefetch next iteration. */
90 vlib_buffer_t * p2, * p3;
92 p2 = vlib_get_buffer (vm, from[2]);
93 p3 = vlib_get_buffer (vm, from[3]);
95 vlib_prefetch_buffer_header (p2, STORE);
96 vlib_prefetch_buffer_header (p3, STORE);
98 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
99 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
102 pi0 = to_next[0] = from[0];
103 pi1 = to_next[1] = from[1];
110 p0 = vlib_get_buffer (vm, pi0);
111 p1 = vlib_get_buffer (vm, pi1);
113 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
114 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
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);
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));
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;
129 /* Bump the adj counters for packet and bytes */
130 vlib_increment_combined_counter
131 (&adjacency_counters,
135 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
136 vlib_increment_combined_counter
137 (&adjacency_counters,
141 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
143 /* Check MTU of outgoing interface. */
144 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
145 adj0[0].rewrite_header.max_l3_packet_bytes))
147 p0->current_data -= rw_len0;
148 p0->current_length += rw_len0;
150 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
151 adj0[0].rewrite_header.sw_if_index;
152 next0 = adj0[0].rewrite_header.next_index;
153 error0 = IP4_ERROR_NONE;
155 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
156 vnet_feature_arc_start (mm->output_feature_arc_index,
157 adj0[0].rewrite_header.sw_if_index,
162 error0 = IP4_ERROR_MTU_EXCEEDED;
163 next0 = MPLS_OUTPUT_NEXT_DROP;
165 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
166 adj1[0].rewrite_header.max_l3_packet_bytes))
168 p1->current_data -= rw_len1;
169 p1->current_length += rw_len1;
171 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
172 adj1[0].rewrite_header.sw_if_index;
173 next1 = adj1[0].rewrite_header.next_index;
174 error1 = IP4_ERROR_NONE;
176 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
177 vnet_feature_arc_start (mm->output_feature_arc_index,
178 adj1[0].rewrite_header.sw_if_index,
183 error1 = IP4_ERROR_MTU_EXCEEDED;
184 next1 = MPLS_OUTPUT_NEXT_DROP;
188 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
189 adj1->sub_type.midchain.fixup_func(vm, adj1, p1);
192 p0->error = error_node->errors[error0];
193 p1->error = error_node->errors[error1];
195 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
197 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
199 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
200 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
202 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
204 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
206 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
207 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
210 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
211 to_next, n_left_to_next,
212 pi0, pi1, next0, next1);
215 while (n_left_from > 0 && n_left_to_next > 0)
217 ip_adjacency_t * adj0;
218 mpls_unicast_header_t *hdr0;
220 u32 pi0, rw_len0, adj_index0, next0, error0;
222 pi0 = to_next[0] = from[0];
224 p0 = vlib_get_buffer (vm, pi0);
226 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
228 adj0 = adj_get(adj_index0);
229 hdr0 = vlib_buffer_get_current (p0);
231 /* Guess we are only writing on simple Ethernet header. */
232 vnet_rewrite_one_header (adj0[0], hdr0,
233 sizeof (ethernet_header_t));
235 /* Update packet buffer attributes/set output interface. */
236 rw_len0 = adj0[0].rewrite_header.data_bytes;
238 vlib_increment_combined_counter
239 (&adjacency_counters,
243 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
245 /* Check MTU of outgoing interface. */
246 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
247 adj0[0].rewrite_header.max_l3_packet_bytes))
249 p0->current_data -= rw_len0;
250 p0->current_length += rw_len0;
252 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
253 adj0[0].rewrite_header.sw_if_index;
254 next0 = adj0[0].rewrite_header.next_index;
255 error0 = IP4_ERROR_NONE;
257 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
258 vnet_feature_arc_start (mm->output_feature_arc_index,
259 adj0[0].rewrite_header.sw_if_index,
264 error0 = IP4_ERROR_MTU_EXCEEDED;
265 next0 = MPLS_OUTPUT_NEXT_DROP;
269 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
272 p0->error = error_node->errors[error0];
279 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
281 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
283 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
284 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
287 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
288 to_next, n_left_to_next,
292 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
294 vlib_node_increment_counter (vm, mpls_output_node.index,
295 MPLS_ERROR_PKTS_ENCAP,
296 from_frame->n_vectors);
298 return from_frame->n_vectors;
301 static char * mpls_error_strings[] = {
302 #define mpls_error(n,s) s,
308 mpls_output (vlib_main_t * vm,
309 vlib_node_runtime_t * node,
310 vlib_frame_t * from_frame)
312 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
315 VLIB_REGISTER_NODE (mpls_output_node) = {
316 .function = mpls_output,
317 .name = "mpls-output",
318 /* Takes a vector of packets. */
319 .vector_size = sizeof (u32),
320 .n_errors = MPLS_N_ERROR,
321 .error_strings = mpls_error_strings,
323 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
325 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
326 foreach_mpls_output_next
330 .format_trace = format_mpls_output_trace,
333 VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
336 mpls_midchain (vlib_main_t * vm,
337 vlib_node_runtime_t * node,
338 vlib_frame_t * from_frame)
340 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
343 VLIB_REGISTER_NODE (mpls_midchain_node) = {
344 .function = mpls_midchain,
345 .name = "mpls-midchain",
346 .vector_size = sizeof (u32),
348 .format_trace = format_mpls_output_trace,
350 .sibling_of = "mpls-output",
353 VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
356 * @brief Next index values from the MPLS incomplete adj node
358 #define foreach_mpls_adj_incomplete_next \
359 _(DROP, "error-drop") \
361 _(IP6, "ip6-discover-neighbor")
364 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
365 foreach_mpls_adj_incomplete_next
367 MPLS_ADJ_INCOMPLETE_N_NEXT,
368 } mpls_adj_incomplete_next_t;
371 * @brief A struct to hold tracing information for the MPLS label imposition
374 typedef struct mpls_adj_incomplete_trace_t_
377 } mpls_adj_incomplete_trace_t;
381 * @brief Graph node for incomplete MPLS adjacency.
382 * This node will push traffic to either the v4-arp or v6-nd node
383 * based on the next-hop proto of the adj.
384 * We pay a cost for this 'routing' node, but an incomplete adj is the
388 mpls_adj_incomplete (vlib_main_t * vm,
389 vlib_node_runtime_t * node,
390 vlib_frame_t * from_frame)
392 u32 n_left_from, next_index, * from, * to_next;
394 from = vlib_frame_vector_args (from_frame);
395 n_left_from = from_frame->n_vectors;
396 next_index = node->cached_next_index;
398 while (n_left_from > 0)
402 vlib_get_next_frame (vm, node, next_index,
403 to_next, n_left_to_next);
405 while (n_left_from > 0 && n_left_to_next > 0)
407 u32 pi0, next0, adj_index0;
408 ip_adjacency_t * adj0;
411 pi0 = to_next[0] = from[0];
412 p0 = vlib_get_buffer (vm, pi0);
418 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
420 adj0 = adj_get(adj_index0);
422 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
424 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
428 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
431 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
433 mpls_adj_incomplete_trace_t *tr =
434 vlib_add_trace (vm, node, p0, sizeof (*tr));
438 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
439 to_next, n_left_to_next,
443 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
446 return from_frame->n_vectors;
450 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
452 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
453 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
454 mpls_adj_incomplete_trace_t * t;
457 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
458 indent = format_get_indent (s);
460 s = format (s, "%Unext:%d",
461 format_white_space, indent,
466 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
467 .function = mpls_adj_incomplete,
468 .name = "mpls-adj-incomplete",
469 .format_trace = format_mpls_adj_incomplete_trace,
470 /* Takes a vector of packets. */
471 .vector_size = sizeof (u32),
472 .n_errors = MPLS_N_ERROR,
473 .error_strings = mpls_error_strings,
475 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
477 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
478 foreach_mpls_adj_incomplete_next
483 VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,