2 * Copyright (c) 2018 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.
17 * @brief NAT44 worker handoff
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/handoff.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/error.h>
26 #include <nat/nat_inlines.h>
30 u32 next_worker_index;
34 } nat44_handoff_trace_t;
36 #define foreach_nat44_handoff_error \
37 _(CONGESTION_DROP, "congestion drop") \
38 _(SAME_WORKER, "same worker") \
39 _(DO_HANDOFF, "do handoff")
43 #define _(sym,str) NAT44_HANDOFF_ERROR_##sym,
44 foreach_nat44_handoff_error
46 NAT44_HANDOFF_N_ERROR,
47 } nat44_handoff_error_t;
49 static char *nat44_handoff_error_strings[] = {
50 #define _(sym,string) string,
51 foreach_nat44_handoff_error
57 format_nat44_handoff_trace (u8 * s, va_list * args)
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 nat44_handoff_trace_t *t = va_arg (*args, nat44_handoff_trace_t *);
64 tag = t->in2out ? "IN2OUT" : "OUT2IN";
65 output = t->output ? "OUTPUT-FEATURE" : "";
67 format (s, "NAT44_%s_WORKER_HANDOFF %s: next-worker %d trace index %d",
68 tag, output, t->next_worker_index, t->trace_index);
74 nat44_worker_handoff_fn_inline (vlib_main_t * vm,
75 vlib_node_runtime_t * node,
76 vlib_frame_t * frame, u8 is_output,
79 u32 n_enq, n_left_from, *from, do_handoff = 0, same_worker = 0;
81 u16 thread_indices[VLIB_FRAME_SIZE], *ti = thread_indices;
82 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
83 snat_main_t *sm = &snat_main;
85 snat_get_worker_function_t *get_worker;
86 u32 fq_index, thread_index = vm->thread_index;
88 from = vlib_frame_vector_args (frame);
89 n_left_from = frame->n_vectors;
91 vlib_get_buffers (vm, from, b, n_left_from);
95 fq_index = is_output ? sm->fq_in2out_output_index : sm->fq_in2out_index;
96 get_worker = sm->worker_in2out_cb;
100 fq_index = sm->fq_out2in_index;
101 get_worker = sm->worker_out2in_cb;
104 while (n_left_from >= 4)
106 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
107 u32 rx_fib_index0, rx_fib_index1, rx_fib_index2, rx_fib_index3;
108 u32 iph_offset0 = 0, iph_offset1 = 0, iph_offset2 = 0, iph_offset3 = 0;
109 ip4_header_t *ip0, *ip1, *ip2, *ip3;
111 if (PREDICT_TRUE (n_left_from >= 8))
113 vlib_prefetch_buffer_header (b[4], STORE);
114 vlib_prefetch_buffer_header (b[5], STORE);
115 vlib_prefetch_buffer_header (b[6], STORE);
116 vlib_prefetch_buffer_header (b[7], STORE);
117 CLIB_PREFETCH (&b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
118 CLIB_PREFETCH (&b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
119 CLIB_PREFETCH (&b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
120 CLIB_PREFETCH (&b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
125 iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
126 iph_offset1 = vnet_buffer (b[1])->ip.save_rewrite_length;
127 iph_offset2 = vnet_buffer (b[2])->ip.save_rewrite_length;
128 iph_offset3 = vnet_buffer (b[3])->ip.save_rewrite_length;
131 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) +
133 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[1]) +
135 ip2 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[2]) +
137 ip3 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[3]) +
140 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
141 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
142 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
143 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
145 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
146 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
147 rx_fib_index2 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index2);
148 rx_fib_index3 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index3);
150 ti[0] = get_worker (ip0, rx_fib_index0, is_output);
151 ti[1] = get_worker (ip1, rx_fib_index1, is_output);
152 ti[2] = get_worker (ip2, rx_fib_index2, is_output);
153 ti[3] = get_worker (ip3, rx_fib_index3, is_output);
155 if (ti[0] == thread_index)
160 if (ti[1] == thread_index)
165 if (ti[2] == thread_index)
170 if (ti[3] == thread_index)
180 while (n_left_from > 0)
189 iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
191 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) +
194 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
195 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
197 ti[0] = get_worker (ip0, rx_fib_index0, is_output);
199 if (ti[0] == thread_index)
209 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
215 for (i = 0; i < frame->n_vectors; i++)
217 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
219 nat44_handoff_trace_t *t =
220 vlib_add_trace (vm, node, b[0], sizeof (*t));
221 t->next_worker_index = ti[0];
222 t->trace_index = vlib_buffer_get_trace_index (b[0]);
223 t->in2out = is_in2out;
224 t->output = is_output;
234 n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
235 frame->n_vectors, 1);
237 if (n_enq < frame->n_vectors)
239 vlib_node_increment_counter (vm, node->node_index,
240 NAT44_HANDOFF_ERROR_CONGESTION_DROP,
241 frame->n_vectors - n_enq);
244 vlib_node_increment_counter (vm, node->node_index,
245 NAT44_HANDOFF_ERROR_SAME_WORKER, same_worker);
246 vlib_node_increment_counter (vm, node->node_index,
247 NAT44_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
248 return frame->n_vectors;
253 VLIB_NODE_FN (snat_in2out_worker_handoff_node) (vlib_main_t * vm,
254 vlib_node_runtime_t * node,
255 vlib_frame_t * frame)
257 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
261 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
262 .name = "nat44-in2out-worker-handoff",
263 .vector_size = sizeof (u32),
264 .sibling_of = "nat-default",
265 .format_trace = format_nat44_handoff_trace,
266 .type = VLIB_NODE_TYPE_INTERNAL,
267 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
268 .error_strings = nat44_handoff_error_strings,
272 VLIB_NODE_FN (snat_in2out_output_worker_handoff_node) (vlib_main_t * vm,
273 vlib_node_runtime_t *
275 vlib_frame_t * frame)
277 return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
281 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
282 .name = "nat44-in2out-output-worker-handoff",
283 .vector_size = sizeof (u32),
284 .sibling_of = "nat-default",
285 .format_trace = format_nat44_handoff_trace,
286 .type = VLIB_NODE_TYPE_INTERNAL,
287 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
288 .error_strings = nat44_handoff_error_strings,
292 VLIB_NODE_FN (snat_out2in_worker_handoff_node) (vlib_main_t * vm,
293 vlib_node_runtime_t * node,
294 vlib_frame_t * frame)
296 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
300 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
301 .name = "nat44-out2in-worker-handoff",
302 .vector_size = sizeof (u32),
303 .sibling_of = "nat-default",
304 .format_trace = format_nat44_handoff_trace,
305 .type = VLIB_NODE_TYPE_INTERNAL,
306 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
307 .error_strings = nat44_handoff_error_strings,
312 * fd.io coding-style-patch-verification: ON
315 * eval: (c-set-style "gnu")