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")
38 #define _(sym,str) NAT44_HANDOFF_ERROR_##sym,
39 foreach_nat44_handoff_error
41 NAT44_HANDOFF_N_ERROR,
42 } nat44_handoff_error_t;
44 static char *nat44_handoff_error_strings[] = {
45 #define _(sym,string) string,
46 foreach_nat44_handoff_error
51 vlib_node_registration_t snat_in2out_worker_handoff_node;
52 vlib_node_registration_t snat_in2out_output_worker_handoff_node;
53 vlib_node_registration_t snat_out2in_worker_handoff_node;
56 format_nat44_handoff_trace (u8 * s, va_list * args)
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 nat44_handoff_trace_t *t = va_arg (*args, nat44_handoff_trace_t *);
63 tag = t->in2out ? "IN2OUT" : "OUT2IN";
65 format (s, "NAT44_%s_WORKER_HANDOFF: next-worker %d", tag,
66 t->next_worker_index);
72 nat44_worker_handoff_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
73 vlib_frame_t * frame, u8 is_output,
76 snat_main_t *sm = &snat_main;
77 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
78 u32 n_enq, n_left_from, *from;
79 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
81 snat_get_worker_function_t *get_worker;
83 from = vlib_frame_vector_args (frame);
84 n_left_from = frame->n_vectors;
85 vlib_get_buffers (vm, from, bufs, n_left_from);
90 ASSERT (vec_len (sm->workers));
94 get_worker = sm->worker_in2out_cb;
96 fq_index = sm->fq_in2out_output_index;
98 fq_index = sm->fq_in2out_index;
102 fq_index = sm->fq_out2in_index;
103 get_worker = sm->worker_out2in_cb;
106 while (n_left_from > 0)
112 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
113 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
114 ip0 = vlib_buffer_get_current (b[0]);
115 ti[0] = get_worker (ip0, rx_fib_index0);
117 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
118 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
120 nat44_handoff_trace_t *t =
121 vlib_add_trace (vm, node, b[0], sizeof (*t));
122 t->next_worker_index = ti[0];
123 t->in2out = is_in2out;
132 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
133 frame->n_vectors, 1);
135 if (n_enq < frame->n_vectors)
136 vlib_node_increment_counter (vm, node->node_index,
137 NAT44_HANDOFF_ERROR_CONGESTION_DROP,
138 frame->n_vectors - n_enq);
139 return frame->n_vectors;
143 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
144 vlib_node_runtime_t * node,
145 vlib_frame_t * frame)
147 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
151 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
152 .function = snat_in2out_worker_handoff_fn,
153 .name = "nat44-in2out-worker-handoff",
154 .vector_size = sizeof (u32),
155 .format_trace = format_nat44_handoff_trace,
156 .type = VLIB_NODE_TYPE_INTERNAL,
157 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
158 .error_strings = nat44_handoff_error_strings,
166 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
167 snat_in2out_worker_handoff_fn);
170 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
171 vlib_node_runtime_t * node,
172 vlib_frame_t * frame)
174 return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
178 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
179 .function = snat_in2out_output_worker_handoff_fn,
180 .name = "nat44-in2out-output-worker-handoff",
181 .vector_size = sizeof (u32),
182 .format_trace = format_nat44_handoff_trace,
183 .type = VLIB_NODE_TYPE_INTERNAL,
184 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
185 .error_strings = nat44_handoff_error_strings,
193 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
194 snat_in2out_output_worker_handoff_fn);
197 snat_out2in_worker_handoff_fn (vlib_main_t * vm,
198 vlib_node_runtime_t * node,
199 vlib_frame_t * frame)
201 return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
205 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
206 .function = snat_out2in_worker_handoff_fn,
207 .name = "nat44-out2in-worker-handoff",
208 .vector_size = sizeof (u32),
209 .format_trace = format_nat44_handoff_trace,
210 .type = VLIB_NODE_TYPE_INTERNAL,
211 .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
212 .error_strings = nat44_handoff_error_strings,
220 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_worker_handoff_node,
221 snat_out2in_worker_handoff_fn);
224 * fd.io coding-style-patch-verification: ON
227 * eval: (c-set-style "gnu")