2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/vnet.h>
17 #include <vnet/adj/adj_nsh.h>
18 #include <vnet/ip/ip.h>
20 #ifndef CLIB_MARCH_VARIANT
21 nsh_main_dummy_t nsh_main_dummy;
22 #endif /* CLIB_MARCH_VARIANT */
25 * @brief Trace data for a NSH Midchain
27 typedef struct adj_nsh_trace_t_ {
28 /** Adjacency index taken. */
33 format_adj_nsh_trace (u8 * s, va_list * args)
35 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37 adj_nsh_trace_t * t = va_arg (*args, adj_nsh_trace_t *);
39 s = format (s, "adj-idx %d : %U",
41 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE);
45 typedef enum adj_nsh_rewrite_next_t_
47 ADJ_NSH_REWRITE_NEXT_DROP,
48 } adj_gpe_rewrite_next_t;
51 adj_nsh_rewrite_inline (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
56 u32 * from = vlib_frame_vector_args (frame);
57 u32 n_left_from, n_left_to_next, * to_next, next_index;
58 u32 thread_index = vlib_get_thread_index();
60 n_left_from = frame->n_vectors;
61 next_index = node->cached_next_index;
63 while (n_left_from > 0)
65 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
67 while (n_left_from > 0 && n_left_to_next > 0)
69 ip_adjacency_t * adj0;
72 u32 pi0, rw_len0, adj_index0, next0 = 0;
75 pi0 = to_next[0] = from[0];
81 p0 = vlib_get_buffer (vm, pi0);
82 h0 = vlib_buffer_get_current (p0);
84 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
86 /* We should never rewrite a pkt using the MISS adjacency */
89 adj0 = adj_get (adj_index0);
91 /* Guess we are only writing on simple IP4 header. */
92 vnet_rewrite_one_header(adj0[0], h0, sizeof(ip4_header_t));
94 /* Update packet buffer attributes/set output interface. */
95 rw_len0 = adj0[0].rewrite_header.data_bytes;
96 vnet_buffer(p0)->ip.save_rewrite_length = rw_len0;
98 vlib_increment_combined_counter(&adjacency_counters,
101 /* packet increment */ 0,
102 /* byte increment */ rw_len0);
104 /* Check MTU of outgoing interface. */
105 if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0) <=
106 adj0[0].rewrite_header.max_l3_packet_bytes)))
108 /* Don't adjust the buffer for ttl issue; icmp-error node wants
109 * to see the IP header */
110 p0->current_data -= rw_len0;
111 p0->current_length += rw_len0;
112 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
116 adj0->sub_type.midchain.fixup_func(
118 adj0->sub_type.midchain.fixup_data);
121 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
124 * Follow the feature ARC. this will result eventually in
125 * the midchain-tx node
127 vnet_feature_arc_start (nsh_main_dummy.output_feature_arc_index,
128 tx_sw_if_index0, &next0, p0);
132 /* can't fragment NSH */
133 next0 = ADJ_NSH_REWRITE_NEXT_DROP;
136 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
138 adj_nsh_trace_t *tr = vlib_add_trace (vm, node,
140 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
143 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
144 to_next, n_left_to_next,
148 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
151 return frame->n_vectors;
154 VLIB_NODE_FN (adj_nsh_rewrite_node) (vlib_main_t * vm,
155 vlib_node_runtime_t * node,
156 vlib_frame_t * frame)
158 return adj_nsh_rewrite_inline (vm, node, frame, 0);
161 VLIB_NODE_FN (adj_nsh_midchain_node) (vlib_main_t * vm,
162 vlib_node_runtime_t * node,
163 vlib_frame_t * frame)
165 return adj_nsh_rewrite_inline (vm, node, frame, 1);
168 VLIB_REGISTER_NODE (adj_nsh_rewrite_node) = {
169 .name = "adj-nsh-rewrite",
170 .vector_size = sizeof (u32),
172 .format_trace = format_adj_nsh_trace,
176 [ADJ_NSH_REWRITE_NEXT_DROP] = "error-drop",
180 VLIB_REGISTER_NODE (adj_nsh_midchain_node) = {
181 .name = "adj-nsh-midchain",
182 .vector_size = sizeof (u32),
184 .format_trace = format_adj_nsh_trace,
188 [ADJ_NSH_REWRITE_NEXT_DROP] = "error-drop",
192 /* Built-in ip4 tx feature path definition */
194 VNET_FEATURE_ARC_INIT (nsh_output, static) =
196 .arc_name = "nsh-output",
197 .start_nodes = VNET_FEATURES ("adj-nsh-midchain"),
198 .arc_index_ptr = &nsh_main_dummy.output_feature_arc_index,
201 VNET_FEATURE_INIT (nsh_tx_drop, static) =
203 .arc_name = "nsh-output",
204 .node_name = "error-drop",
205 .runs_before = 0, /* not before any other features */