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. */
28 /* Packet data, possibly *after* rewrite. */
29 u8 packet_data[64 - 1*sizeof(u32)];
30 } mpls_output_trace_t;
32 #define foreach_mpls_output_next \
36 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
37 foreach_mpls_output_next
43 format_mpls_output_trace (u8 * s, va_list * args)
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);
50 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
52 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
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));
62 mpls_output_inline (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * from_frame,
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, rw_len0, adj_index0, next0, error0;
91 ip_adjacency_t * adj1;
92 mpls_unicast_header_t *hdr1;
94 u32 pi1, rw_len1, adj_index1, next1, error1;
96 /* Prefetch next iteration. */
98 vlib_buffer_t * p2, * p3;
100 p2 = vlib_get_buffer (vm, from[2]);
101 p3 = vlib_get_buffer (vm, from[3]);
103 vlib_prefetch_buffer_header (p2, STORE);
104 vlib_prefetch_buffer_header (p3, STORE);
106 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
107 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
110 pi0 = to_next[0] = from[0];
111 pi1 = to_next[1] = from[1];
118 p0 = vlib_get_buffer (vm, pi0);
119 p1 = vlib_get_buffer (vm, pi1);
121 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
122 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
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);
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));
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;
137 /* Bump the adj counters for packet and bytes */
138 vlib_increment_combined_counter
139 (&adjacency_counters,
143 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
144 vlib_increment_combined_counter
145 (&adjacency_counters,
149 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
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))
155 p0->current_data -= rw_len0;
156 p0->current_length += rw_len0;
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;
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,
170 error0 = IP4_ERROR_MTU_EXCEEDED;
171 next0 = MPLS_OUTPUT_NEXT_DROP;
173 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
174 adj1[0].rewrite_header.max_l3_packet_bytes))
176 p1->current_data -= rw_len1;
177 p1->current_length += rw_len1;
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;
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,
191 error1 = IP4_ERROR_MTU_EXCEEDED;
192 next1 = MPLS_OUTPUT_NEXT_DROP;
196 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
197 adj1->sub_type.midchain.fixup_func(vm, adj1, p1);
200 p0->error = error_node->errors[error0];
201 p1->error = error_node->errors[error1];
203 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
205 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
207 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
208 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
210 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
212 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
214 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
215 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
218 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
219 to_next, n_left_to_next,
220 pi0, pi1, next0, next1);
223 while (n_left_from > 0 && n_left_to_next > 0)
225 ip_adjacency_t * adj0;
226 mpls_unicast_header_t *hdr0;
228 u32 pi0, rw_len0, adj_index0, next0, error0;
230 pi0 = to_next[0] = from[0];
232 p0 = vlib_get_buffer (vm, pi0);
234 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
236 adj0 = adj_get(adj_index0);
237 hdr0 = vlib_buffer_get_current (p0);
239 /* Guess we are only writing on simple Ethernet header. */
240 vnet_rewrite_one_header (adj0[0], hdr0,
241 sizeof (ethernet_header_t));
243 /* Update packet buffer attributes/set output interface. */
244 rw_len0 = adj0[0].rewrite_header.data_bytes;
246 vlib_increment_combined_counter
247 (&adjacency_counters,
251 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
253 /* Check MTU of outgoing interface. */
254 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
255 adj0[0].rewrite_header.max_l3_packet_bytes))
257 p0->current_data -= rw_len0;
258 p0->current_length += rw_len0;
260 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
261 adj0[0].rewrite_header.sw_if_index;
262 next0 = adj0[0].rewrite_header.next_index;
263 error0 = IP4_ERROR_NONE;
265 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
266 vnet_feature_arc_start (mm->output_feature_arc_index,
267 adj0[0].rewrite_header.sw_if_index,
272 error0 = IP4_ERROR_MTU_EXCEEDED;
273 next0 = MPLS_OUTPUT_NEXT_DROP;
277 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
280 p0->error = error_node->errors[error0];
287 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
289 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
291 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
292 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
295 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
296 to_next, n_left_to_next,
300 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302 vlib_node_increment_counter (vm, mpls_output_node.index,
303 MPLS_ERROR_PKTS_ENCAP,
304 from_frame->n_vectors);
306 return from_frame->n_vectors;
309 static char * mpls_error_strings[] = {
310 #define mpls_error(n,s) s,
316 mpls_output (vlib_main_t * vm,
317 vlib_node_runtime_t * node,
318 vlib_frame_t * from_frame)
320 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
323 VLIB_REGISTER_NODE (mpls_output_node) = {
324 .function = mpls_output,
325 .name = "mpls-output",
326 /* Takes a vector of packets. */
327 .vector_size = sizeof (u32),
328 .n_errors = MPLS_N_ERROR,
329 .error_strings = mpls_error_strings,
331 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
333 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
334 foreach_mpls_output_next
338 .format_trace = format_mpls_output_trace,
341 VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
344 mpls_midchain (vlib_main_t * vm,
345 vlib_node_runtime_t * node,
346 vlib_frame_t * from_frame)
348 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
351 VLIB_REGISTER_NODE (mpls_midchain_node) = {
352 .function = mpls_midchain,
353 .name = "mpls-midchain",
354 .vector_size = sizeof (u32),
356 .format_trace = format_mpls_output_trace,
358 .sibling_of = "mpls-output",
361 VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
364 * @brief Next index values from the MPLS incomplete adj node
366 #define foreach_mpls_adj_incomplete_next \
367 _(DROP, "error-drop") \
369 _(IP6, "ip6-discover-neighbor")
372 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
373 foreach_mpls_adj_incomplete_next
375 MPLS_ADJ_INCOMPLETE_N_NEXT,
376 } mpls_adj_incomplete_next_t;
379 * @brief A struct to hold tracing information for the MPLS label imposition
382 typedef struct mpls_adj_incomplete_trace_t_
385 } mpls_adj_incomplete_trace_t;
389 * @brief Graph node for incomplete MPLS adjacency.
390 * This node will push traffic to either the v4-arp or v6-nd node
391 * based on the next-hop proto of the adj.
392 * We pay a cost for this 'routing' node, but an incomplete adj is the
396 mpls_adj_incomplete (vlib_main_t * vm,
397 vlib_node_runtime_t * node,
398 vlib_frame_t * from_frame)
400 u32 n_left_from, next_index, * from, * to_next;
402 from = vlib_frame_vector_args (from_frame);
403 n_left_from = from_frame->n_vectors;
404 next_index = node->cached_next_index;
406 while (n_left_from > 0)
410 vlib_get_next_frame (vm, node, next_index,
411 to_next, n_left_to_next);
413 while (n_left_from > 0 && n_left_to_next > 0)
415 u32 pi0, next0, adj_index0;
416 ip_adjacency_t * adj0;
419 pi0 = to_next[0] = from[0];
420 p0 = vlib_get_buffer (vm, pi0);
426 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
428 adj0 = adj_get(adj_index0);
430 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
432 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
436 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
439 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
441 mpls_adj_incomplete_trace_t *tr =
442 vlib_add_trace (vm, node, p0, sizeof (*tr));
446 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
447 to_next, n_left_to_next,
451 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
454 return from_frame->n_vectors;
458 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
460 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
461 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
462 mpls_adj_incomplete_trace_t * t;
465 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
466 indent = format_get_indent (s);
468 s = format (s, "%Unext:%d",
469 format_white_space, indent,
474 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
475 .function = mpls_adj_incomplete,
476 .name = "mpls-adj-incomplete",
477 .format_trace = format_mpls_adj_incomplete_trace,
478 /* Takes a vector of packets. */
479 .vector_size = sizeof (u32),
480 .n_errors = MPLS_N_ERROR,
481 .error_strings = mpls_error_strings,
483 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
485 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
486 foreach_mpls_adj_incomplete_next
491 VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,