2 * Copyright (c) 2020 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 <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/fib/ip4_fib.h>
19 #include <vppinfra/error.h>
21 #include <nat/nat44-ei/nat44_ei.h>
25 u32 next_worker_index;
29 } nat44_ei_handoff_trace_t;
31 #define foreach_nat44_ei_handoff_error \
32 _ (CONGESTION_DROP, "congestion drop") \
33 _ (SAME_WORKER, "same worker") \
34 _ (DO_HANDOFF, "do handoff")
38 #define _(sym, str) NAT44_EI_HANDOFF_ERROR_##sym,
39 foreach_nat44_ei_handoff_error
41 NAT44_EI_HANDOFF_N_ERROR,
42 } nat44_ei_handoff_error_t;
44 static char *nat44_ei_handoff_error_strings[] = {
45 #define _(sym, string) string,
46 foreach_nat44_ei_handoff_error
51 format_nat44_ei_handoff_trace (u8 *s, va_list *args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 nat44_ei_handoff_trace_t *t = va_arg (*args, nat44_ei_handoff_trace_t *);
58 tag = t->in2out ? "IN2OUT" : "OUT2IN";
59 output = t->output ? "OUTPUT-FEATURE" : "";
61 format (s, "NAT44_EI_%s_WORKER_HANDOFF %s: next-worker %d trace index %d",
62 tag, output, t->next_worker_index, t->trace_index);
68 nat44_ei_worker_handoff_fn_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
69 vlib_frame_t *frame, u8 is_output,
72 u32 n_enq, n_left_from, *from, do_handoff = 0, same_worker = 0;
74 u16 thread_indices[VLIB_FRAME_SIZE], *ti = thread_indices;
75 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
76 nat44_ei_main_t *nm = &nat44_ei_main;
78 u32 fq_index, thread_index = vm->thread_index;
80 from = vlib_frame_vector_args (frame);
81 n_left_from = frame->n_vectors;
83 vlib_get_buffers (vm, from, b, n_left_from);
87 fq_index = is_output ? nm->fq_in2out_output_index : nm->fq_in2out_index;
91 fq_index = nm->fq_out2in_index;
94 while (n_left_from >= 4)
96 u32 arc_next0, arc_next1, arc_next2, arc_next3;
97 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
98 u32 rx_fib_index0, rx_fib_index1, rx_fib_index2, rx_fib_index3;
99 u32 iph_offset0 = 0, iph_offset1 = 0, iph_offset2 = 0, iph_offset3 = 0;
100 ip4_header_t *ip0, *ip1, *ip2, *ip3;
102 if (PREDICT_TRUE (n_left_from >= 8))
104 vlib_prefetch_buffer_header (b[4], LOAD);
105 vlib_prefetch_buffer_header (b[5], LOAD);
106 vlib_prefetch_buffer_header (b[6], LOAD);
107 vlib_prefetch_buffer_header (b[7], LOAD);
108 clib_prefetch_load (&b[4]->data);
109 clib_prefetch_load (&b[5]->data);
110 clib_prefetch_load (&b[6]->data);
111 clib_prefetch_load (&b[7]->data);
116 iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
117 iph_offset1 = vnet_buffer (b[1])->ip.save_rewrite_length;
118 iph_offset2 = vnet_buffer (b[2])->ip.save_rewrite_length;
119 iph_offset3 = vnet_buffer (b[3])->ip.save_rewrite_length;
123 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) + iph_offset0);
125 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[1]) + iph_offset1);
127 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[2]) + iph_offset2);
129 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[3]) + iph_offset3);
131 vnet_feature_next (&arc_next0, b[0]);
132 vnet_feature_next (&arc_next1, b[1]);
133 vnet_feature_next (&arc_next2, b[2]);
134 vnet_feature_next (&arc_next3, b[3]);
136 vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
137 vnet_buffer2 (b[1])->nat.arc_next = arc_next1;
138 vnet_buffer2 (b[2])->nat.arc_next = arc_next2;
139 vnet_buffer2 (b[3])->nat.arc_next = arc_next3;
141 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
142 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
143 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
144 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
146 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
147 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
148 rx_fib_index2 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index2);
149 rx_fib_index3 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index3);
154 nat44_ei_get_in2out_worker_index (ip0, rx_fib_index0, is_output);
156 nat44_ei_get_in2out_worker_index (ip1, rx_fib_index1, is_output);
158 nat44_ei_get_in2out_worker_index (ip2, rx_fib_index2, is_output);
160 nat44_ei_get_in2out_worker_index (ip3, rx_fib_index3, is_output);
164 ti[0] = nat44_ei_get_out2in_worker_index (b[0], ip0, rx_fib_index0,
166 ti[1] = nat44_ei_get_out2in_worker_index (b[1], ip1, rx_fib_index1,
168 ti[2] = nat44_ei_get_out2in_worker_index (b[2], ip2, rx_fib_index2,
170 ti[3] = nat44_ei_get_out2in_worker_index (b[3], ip3, rx_fib_index3,
174 if (ti[0] == thread_index)
179 if (ti[1] == thread_index)
184 if (ti[2] == thread_index)
189 if (ti[3] == thread_index)
199 while (n_left_from > 0)
208 iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
211 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) + iph_offset0);
213 vnet_feature_next (&arc_next0, b[0]);
214 vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
216 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
217 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
222 nat44_ei_get_in2out_worker_index (ip0, rx_fib_index0, is_output);
226 ti[0] = nat44_ei_get_out2in_worker_index (b[0], ip0, rx_fib_index0,
230 if (ti[0] == thread_index)
240 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
246 for (i = 0; i < frame->n_vectors; i++)
248 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
250 nat44_ei_handoff_trace_t *t =
251 vlib_add_trace (vm, node, b[0], sizeof (*t));
252 t->next_worker_index = ti[0];
253 t->trace_index = vlib_buffer_get_trace_index (b[0]);
254 t->in2out = is_in2out;
255 t->output = is_output;
265 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
266 thread_indices, frame->n_vectors, 1);
268 if (n_enq < frame->n_vectors)
270 vlib_node_increment_counter (vm, node->node_index,
271 NAT44_EI_HANDOFF_ERROR_CONGESTION_DROP,
272 frame->n_vectors - n_enq);
275 vlib_node_increment_counter (
276 vm, node->node_index, NAT44_EI_HANDOFF_ERROR_SAME_WORKER, same_worker);
277 vlib_node_increment_counter (vm, node->node_index,
278 NAT44_EI_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
279 return frame->n_vectors;
282 VLIB_NODE_FN (nat44_ei_in2out_worker_handoff_node)
283 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
285 return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 0, 1);
288 VLIB_NODE_FN (nat44_ei_in2out_output_worker_handoff_node)
289 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
291 return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 1, 1);
294 VLIB_NODE_FN (nat44_ei_out2in_worker_handoff_node)
295 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
297 return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 0, 0);
300 VLIB_REGISTER_NODE (nat44_ei_in2out_output_worker_handoff_node) = {
301 .name = "nat44-ei-in2out-output-worker-handoff",
302 .vector_size = sizeof (u32),
303 .format_trace = format_nat44_ei_handoff_trace,
304 .type = VLIB_NODE_TYPE_INTERNAL,
305 .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
306 .error_strings = nat44_ei_handoff_error_strings,
309 VLIB_REGISTER_NODE (nat44_ei_in2out_worker_handoff_node) = {
310 .name = "nat44-ei-in2out-worker-handoff",
311 .vector_size = sizeof (u32),
312 .format_trace = format_nat44_ei_handoff_trace,
313 .type = VLIB_NODE_TYPE_INTERNAL,
314 .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
315 .error_strings = nat44_ei_handoff_error_strings,
318 VLIB_REGISTER_NODE (nat44_ei_out2in_worker_handoff_node) = {
319 .name = "nat44-ei-out2in-worker-handoff",
320 .vector_size = sizeof (u32),
321 .format_trace = format_nat44_ei_handoff_trace,
322 .type = VLIB_NODE_TYPE_INTERNAL,
323 .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
324 .error_strings = nat44_ei_handoff_error_strings,
328 * fd.io coding-style-patch-verification: ON
331 * eval: (c-set-style "gnu")