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>
29 u32 next_worker_index;
31 } nat44_handoff_trace_t;
33 #define foreach_nat44_handoff_error \
34 _(CONGESTION_DROP, "congestion drop") \
35 _(SAME_WORKER, "same worker") \
36 _(DO_HANDOFF, "do handoff")
40 #define _(sym,str) NAT44_HANDOFF_ERROR_##sym,
41 foreach_nat44_handoff_error
43 NAT44_HANDOFF_N_ERROR,
44 } nat44_handoff_error_t;
46 static char *nat44_handoff_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_nat44_handoff_error
53 vlib_node_registration_t snat_in2out_worker_handoff_node;
54 vlib_node_registration_t snat_in2out_output_worker_handoff_node;
55 vlib_node_registration_t snat_out2in_worker_handoff_node;
58 format_nat44_handoff_trace (u8 * s, va_list * args)
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62 nat44_handoff_trace_t *t = va_arg (*args, nat44_handoff_trace_t *);
65 tag = t->in2out ? "IN2OUT" : "OUT2IN";
67 format (s, "NAT44_%s_WORKER_HANDOFF: next-worker %d", tag,
68 t->next_worker_index);
74 nat44_worker_handoff_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
75 vlib_frame_t * frame, u8 is_output,
78 snat_main_t *sm = &snat_main;
79 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
80 u32 n_enq, n_left_from, *from;
81 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
83 snat_get_worker_function_t *get_worker;
84 u32 thread_index = vm->thread_index;
85 u32 do_handoff = 0, same_worker = 0;
87 from = vlib_frame_vector_args (frame);
88 n_left_from = frame->n_vectors;
89 vlib_get_buffers (vm, from, bufs, n_left_from);
94 ASSERT (vec_len (sm->workers));
98 get_worker = sm->worker_in2out_cb;
100 fq_index = sm->fq_in2out_output_index;
102 fq_index = sm->fq_in2out_index;
106 fq_index = sm->fq_out2in_index;
107 get_worker = sm->worker_out2in_cb;
110 while (n_left_from > 0)
116 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
117 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
118 ip0 = vlib_buffer_get_current (b[0]);
119 ti[0] = get_worker (ip0, rx_fib_index0);
121 if (ti[0] != thread_index)
126 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
127 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
129 nat44_handoff_trace_t *t =
130 vlib_add_trace (vm, node, b[0], sizeof (*t));
131 t->next_worker_index = ti[0];
132 t->in2out = is_in2out;
141 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
142 frame->n_vectors, 1);
144 if (n_enq < frame->n_vectors)
145 vlib_node_increment_counter (vm, node->node_index,
146 NAT44_HANDOFF_ERROR_CONGESTION_DROP,
147 frame->n_vectors - n_enq);
148 vlib_node_increment_counter (vm, node->node_index,
149 NAT44_HANDOFF_ERROR_SAME_WORKER, same_worker);
150 vlib_node_increment_counter (vm, node->node_index,
151 NAT44_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
152 return frame->n_vectors;
156 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
157 vlib_node_runtime_t * node,
158 vlib_frame_t * frame)
160 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
164 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
165 .function = snat_in2out_worker_handoff_fn,
166 .name = "nat44-in2out-worker-handoff",
167 .vector_size = sizeof (u32),
168 .format_trace = format_nat44_handoff_trace,
169 .type = VLIB_NODE_TYPE_INTERNAL,
170 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
171 .error_strings = nat44_handoff_error_strings,
179 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
180 snat_in2out_worker_handoff_fn);
183 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
184 vlib_node_runtime_t * node,
185 vlib_frame_t * frame)
187 return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
191 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
192 .function = snat_in2out_output_worker_handoff_fn,
193 .name = "nat44-in2out-output-worker-handoff",
194 .vector_size = sizeof (u32),
195 .format_trace = format_nat44_handoff_trace,
196 .type = VLIB_NODE_TYPE_INTERNAL,
197 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
198 .error_strings = nat44_handoff_error_strings,
206 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
207 snat_in2out_output_worker_handoff_fn);
210 snat_out2in_worker_handoff_fn (vlib_main_t * vm,
211 vlib_node_runtime_t * node,
212 vlib_frame_t * frame)
214 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
218 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
219 .function = snat_out2in_worker_handoff_fn,
220 .name = "nat44-out2in-worker-handoff",
221 .vector_size = sizeof (u32),
222 .format_trace = format_nat44_handoff_trace,
223 .type = VLIB_NODE_TYPE_INTERNAL,
224 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
225 .error_strings = nat44_handoff_error_strings,
233 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_worker_handoff_node,
234 snat_out2in_worker_handoff_fn);
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")