2 * nsh_output.c: NSH Adj rewrite
4 * Copyright (c) 2017-2019 Intel 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>
23 /* Adjacency taken. */
27 /* Packet data, possibly *after* rewrite. */
28 u8 packet_data[64 - 1*sizeof(u32)];
31 #define foreach_nsh_output_next \
32 _(DROP, "error-drop") \
33 _(INTERFACE, "interface-output" )
36 #define _(s,n) NSH_OUTPUT_NEXT_##s,
37 foreach_nsh_output_next
43 format_nsh_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 nsh_output_trace_t * t = va_arg (*args, nsh_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 nsh_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, nsh_eth_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 nsh_base_header_t *hdr0;
88 ethernet_header_t * eth_hdr0;
90 u32 pi0, adj_index0, next0, error0;
92 ip_adjacency_t * adj1;
93 nsh_base_header_t *hdr1;
94 ethernet_header_t * eth_hdr1;
96 u32 pi1, adj_index1, next1, error1;
97 int pkt_len0, pkt_len1;
98 word rw_len0, rw_len1;
100 /* Prefetch next iteration. */
102 vlib_buffer_t * p2, * p3;
104 p2 = vlib_get_buffer (vm, from[2]);
105 p3 = vlib_get_buffer (vm, from[3]);
107 vlib_prefetch_buffer_header (p2, STORE);
108 vlib_prefetch_buffer_header (p3, STORE);
110 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
111 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
114 pi0 = to_next[0] = from[0];
115 pi1 = to_next[1] = from[1];
122 p0 = vlib_get_buffer (vm, pi0);
123 p1 = vlib_get_buffer (vm, pi1);
125 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
126 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
128 adj0 = adj_get(adj_index0);
129 adj1 = adj_get(adj_index1);
130 hdr0 = vlib_buffer_get_current (p0);
131 hdr1 = vlib_buffer_get_current (p1);
133 /* Guess we are only writing on simple Ethernet header. */
134 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
135 sizeof (ethernet_header_t));
137 eth_hdr0 = (ethernet_header_t*)((u8 *)hdr0-sizeof(ethernet_header_t));
138 eth_hdr0->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
139 eth_hdr1 = (ethernet_header_t*)((u8 *)hdr1-sizeof(ethernet_header_t));
140 eth_hdr1->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
142 /* Update packet buffer attributes/set output interface. */
143 rw_len0 = adj0[0].rewrite_header.data_bytes;
144 rw_len1 = adj1[0].rewrite_header.data_bytes;
145 pkt_len0 = vlib_buffer_length_in_chain (vm, p0);
146 pkt_len1 = vlib_buffer_length_in_chain (vm, p1);
148 /* Bump the adj counters for packet and bytes */
149 if (adj_index0 == adj_index1)
151 vlib_increment_combined_counter (&adjacency_counters, thread_index, adj_index0, 2,
152 pkt_len0 + rw_len0 + pkt_len1 + rw_len1);
156 vlib_increment_combined_counter (&adjacency_counters, thread_index, adj_index0, 1,
158 vlib_increment_combined_counter (&adjacency_counters, thread_index, adj_index1, 1,
161 /* Check MTU of outgoing interface. */
162 if (PREDICT_TRUE(pkt_len0 <=
163 adj0[0].rewrite_header.max_l3_packet_bytes))
165 vlib_buffer_advance (p0, -rw_len0);
167 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
168 adj0[0].rewrite_header.sw_if_index;
169 next0 = NSH_OUTPUT_NEXT_INTERFACE;
170 error0 = IP4_ERROR_NONE;
172 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
173 vnet_feature_arc_start (nm->output_feature_arc_index,
174 adj0[0].rewrite_header.sw_if_index,
179 error0 = IP4_ERROR_MTU_EXCEEDED;
180 next0 = NSH_OUTPUT_NEXT_DROP;
182 if (PREDICT_TRUE(pkt_len1 <=
183 adj1[0].rewrite_header.max_l3_packet_bytes))
185 vlib_buffer_advance (p1, -rw_len1);
187 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
188 adj1[0].rewrite_header.sw_if_index;
189 next1 = NSH_OUTPUT_NEXT_INTERFACE;
190 error1 = IP4_ERROR_NONE;
192 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
193 vnet_feature_arc_start (nm->output_feature_arc_index,
194 adj1[0].rewrite_header.sw_if_index,
199 error1 = IP4_ERROR_MTU_EXCEEDED;
200 next1 = NSH_OUTPUT_NEXT_DROP;
204 adj0->sub_type.midchain.fixup_func
205 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
206 adj1->sub_type.midchain.fixup_func
207 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
210 p0->error = error_node->errors[error0];
211 p1->error = error_node->errors[error1];
213 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
215 nsh_output_trace_t *tr = vlib_add_trace (vm, node,
217 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
218 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
220 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
222 nsh_output_trace_t *tr = vlib_add_trace (vm, node,
224 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
225 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
228 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
229 to_next, n_left_to_next,
230 pi0, pi1, next0, next1);
233 while (n_left_from > 0 && n_left_to_next > 0)
235 ip_adjacency_t * adj0;
236 nsh_base_header_t *hdr0;
237 ethernet_header_t * eth_hdr0;
239 u32 pi0, adj_index0, next0, error0;
243 pi0 = to_next[0] = from[0];
245 p0 = vlib_get_buffer (vm, pi0);
247 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
249 adj0 = adj_get(adj_index0);
250 hdr0 = vlib_buffer_get_current (p0);
252 /* Guess we are only writing on simple Ethernet header. */
253 vnet_rewrite_one_header (adj0[0], hdr0,
254 sizeof (ethernet_header_t));
256 eth_hdr0 = (ethernet_header_t*)((u8 *)hdr0-sizeof(ethernet_header_t));
257 eth_hdr0->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
259 /* Update packet buffer attributes/set output interface. */
260 rw_len0 = adj0[0].rewrite_header.data_bytes;
261 pkt_len0 = vlib_buffer_length_in_chain (vm, p0);
263 vlib_increment_combined_counter (&adjacency_counters, thread_index, adj_index0, 1,
266 /* Check MTU of outgoing interface. */
267 if (PREDICT_TRUE(pkt_len0 <= adj0[0].rewrite_header.max_l3_packet_bytes))
269 vlib_buffer_advance (p0, -rw_len0);
271 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
272 adj0[0].rewrite_header.sw_if_index;
273 next0 = NSH_OUTPUT_NEXT_INTERFACE;
274 error0 = IP4_ERROR_NONE;
276 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
277 vnet_feature_arc_start (nm->output_feature_arc_index,
278 adj0[0].rewrite_header.sw_if_index,
283 error0 = IP4_ERROR_MTU_EXCEEDED;
284 next0 = NSH_OUTPUT_NEXT_DROP;
288 adj0->sub_type.midchain.fixup_func
289 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
292 p0->error = error_node->errors[error0];
299 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
301 nsh_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);
315 return from_frame->n_vectors;
318 typedef enum nsh_midchain_next_t_
320 NSH_MIDCHAIN_NEXT_DROP,
321 } nsh_midchain_next_t;
323 VLIB_NODE_FN (nsh_eth_output_node) (vlib_main_t * vm,
324 vlib_node_runtime_t * node,
325 vlib_frame_t * from_frame)
327 return (nsh_output_inline(vm, node, from_frame, /* is_midchain */ 0));
330 VLIB_REGISTER_NODE (nsh_eth_output_node) = {
331 .name = "nsh-eth-output",
332 /* Takes a vector of packets. */
333 .vector_size = sizeof (u32),
334 .n_next_nodes = NSH_OUTPUT_N_NEXT,
336 #define _(s,n) [NSH_OUTPUT_NEXT_##s] = n,
337 foreach_nsh_output_next
341 .format_trace = format_nsh_output_trace,
344 VLIB_NODE_FN (nsh_midchain_node) (vlib_main_t * vm,
345 vlib_node_runtime_t * node,
346 vlib_frame_t * from_frame)
348 return (nsh_output_inline(vm, node, from_frame, /* is_midchain */ 1));
351 VLIB_REGISTER_NODE (nsh_midchain_node) = {
352 .name = "nsh-midchain",
353 .vector_size = sizeof (u32),
354 .format_trace = format_nsh_output_trace,
357 [NSH_MIDCHAIN_NEXT_DROP] = "error-drop",
361 /* Built-in nsh tx feature path definition */
362 VNET_FEATURE_INIT (nsh_interface_output, static) = {
363 .arc_name = "nsh-eth-output",
364 .node_name = "interface-output",
365 .runs_before = 0, /* not before any other features */
368 /* Built-in ip4 tx feature path definition */
369 VNET_FEATURE_ARC_INIT (nsh_eth_output, static) =
371 .arc_name = "nsh-eth-output",
372 .start_nodes = VNET_FEATURES ("nsh-midchain"),
375 VNET_FEATURE_INIT (nsh_eth_tx_drop, static) =
377 .arc_name = "nsh-eth-output",
378 .node_name = "error-drop",
379 .runs_before = 0, /* not before any other features */
382 * @brief Next index values from the NSH incomplete adj node
384 #define foreach_nsh_adj_incomplete_next \
385 _(DROP, "error-drop") \
387 _(IP6, "ip6-discover-neighbor")
390 #define _(s,n) NSH_ADJ_INCOMPLETE_NEXT_##s,
391 foreach_nsh_adj_incomplete_next
393 NSH_ADJ_INCOMPLETE_N_NEXT,
394 } nsh_adj_incomplete_next_t;
397 * @brief A struct to hold tracing information for the NSH label imposition
400 typedef struct nsh_adj_incomplete_trace_t_
403 } nsh_adj_incomplete_trace_t;
407 * @brief Graph node for incomplete NSH adjacency.
408 * This node will push traffic to either the v4-arp or v6-nd node
409 * based on the next-hop proto of the adj.
410 * We pay a cost for this 'routing' node, but an incomplete adj is the
413 VLIB_NODE_FN (nsh_adj_incomplete_node) (vlib_main_t * vm,
414 vlib_node_runtime_t * node,
415 vlib_frame_t * from_frame)
417 u32 n_left_from, next_index, * from, * to_next;
419 from = vlib_frame_vector_args (from_frame);
420 n_left_from = from_frame->n_vectors;
421 next_index = node->cached_next_index;
423 while (n_left_from > 0)
427 vlib_get_next_frame (vm, node, next_index,
428 to_next, n_left_to_next);
430 while (n_left_from > 0 && n_left_to_next > 0)
432 u32 pi0, next0, adj_index0;
433 ip_adjacency_t * adj0;
436 pi0 = to_next[0] = from[0];
437 p0 = vlib_get_buffer (vm, pi0);
443 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
445 adj0 = adj_get(adj_index0);
447 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
449 next0 = NSH_ADJ_INCOMPLETE_NEXT_IP4;
453 next0 = NSH_ADJ_INCOMPLETE_NEXT_IP6;
456 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
458 nsh_adj_incomplete_trace_t *tr =
459 vlib_add_trace (vm, node, p0, sizeof (*tr));
463 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
464 to_next, n_left_to_next,
468 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
471 return from_frame->n_vectors;
475 format_nsh_adj_incomplete_trace (u8 * s, va_list * args)
477 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
478 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
479 nsh_adj_incomplete_trace_t * t;
482 t = va_arg (*args, nsh_adj_incomplete_trace_t *);
483 indent = format_get_indent (s);
485 s = format (s, "%Unext:%d",
486 format_white_space, indent,
491 VLIB_REGISTER_NODE (nsh_adj_incomplete_node) = {
492 .name = "nsh-adj-incomplete",
493 .format_trace = format_nsh_adj_incomplete_trace,
494 /* Takes a vector of packets. */
495 .vector_size = sizeof (u32),
496 .n_next_nodes = NSH_ADJ_INCOMPLETE_N_NEXT,
498 #define _(s,n) [NSH_ADJ_INCOMPLETE_NEXT_##s] = n,
499 foreach_nsh_adj_incomplete_next