nat: Final NAT44 EI/ED split patch
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei_handoff.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/handoff.h>
19 #include <vnet/fib/ip4_fib.h>
20 #include <vppinfra/error.h>
21
22 #include <nat/nat44-ei/nat44_ei.h>
23
24 typedef struct
25 {
26   u32 next_worker_index;
27   u32 trace_index;
28   u8 in2out;
29   u8 output;
30 } nat44_ei_handoff_trace_t;
31
32 #define foreach_nat44_ei_handoff_error                                        \
33   _ (CONGESTION_DROP, "congestion drop")                                      \
34   _ (SAME_WORKER, "same worker")                                              \
35   _ (DO_HANDOFF, "do handoff")
36
37 typedef enum
38 {
39 #define _(sym, str) NAT44_EI_HANDOFF_ERROR_##sym,
40   foreach_nat44_ei_handoff_error
41 #undef _
42     NAT44_EI_HANDOFF_N_ERROR,
43 } nat44_ei_handoff_error_t;
44
45 static char *nat44_ei_handoff_error_strings[] = {
46 #define _(sym, string) string,
47   foreach_nat44_ei_handoff_error
48 #undef _
49 };
50
51 static u8 *
52 format_nat44_ei_handoff_trace (u8 *s, va_list *args)
53 {
54   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56   nat44_ei_handoff_trace_t *t = va_arg (*args, nat44_ei_handoff_trace_t *);
57   char *tag, *output;
58
59   tag = t->in2out ? "IN2OUT" : "OUT2IN";
60   output = t->output ? "OUTPUT-FEATURE" : "";
61   s =
62     format (s, "NAT44_EI_%s_WORKER_HANDOFF %s: next-worker %d trace index %d",
63             tag, output, t->next_worker_index, t->trace_index);
64
65   return s;
66 }
67
68 static inline uword
69 nat44_ei_worker_handoff_fn_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
70                                    vlib_frame_t *frame, u8 is_output,
71                                    u8 is_in2out)
72 {
73   u32 n_enq, n_left_from, *from, do_handoff = 0, same_worker = 0;
74
75   u16 thread_indices[VLIB_FRAME_SIZE], *ti = thread_indices;
76   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
77   nat44_ei_main_t *nm = &nat44_ei_main;
78
79   u32 fq_index, thread_index = vm->thread_index;
80
81   from = vlib_frame_vector_args (frame);
82   n_left_from = frame->n_vectors;
83
84   vlib_get_buffers (vm, from, b, n_left_from);
85
86   // TODO: move to nm
87   // TODO: remove callbacks and use inlines that should be moved here
88   if (is_in2out)
89     {
90       fq_index = is_output ? nm->fq_in2out_output_index : nm->fq_in2out_index;
91     }
92   else
93     {
94       fq_index = nm->fq_out2in_index;
95     }
96
97   while (n_left_from >= 4)
98     {
99       u32 arc_next0, arc_next1, arc_next2, arc_next3;
100       u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
101       u32 rx_fib_index0, rx_fib_index1, rx_fib_index2, rx_fib_index3;
102       u32 iph_offset0 = 0, iph_offset1 = 0, iph_offset2 = 0, iph_offset3 = 0;
103       ip4_header_t *ip0, *ip1, *ip2, *ip3;
104
105       if (PREDICT_TRUE (n_left_from >= 8))
106         {
107           vlib_prefetch_buffer_header (b[4], LOAD);
108           vlib_prefetch_buffer_header (b[5], LOAD);
109           vlib_prefetch_buffer_header (b[6], LOAD);
110           vlib_prefetch_buffer_header (b[7], LOAD);
111           CLIB_PREFETCH (&b[4]->data, CLIB_CACHE_LINE_BYTES, LOAD);
112           CLIB_PREFETCH (&b[5]->data, CLIB_CACHE_LINE_BYTES, LOAD);
113           CLIB_PREFETCH (&b[6]->data, CLIB_CACHE_LINE_BYTES, LOAD);
114           CLIB_PREFETCH (&b[7]->data, CLIB_CACHE_LINE_BYTES, LOAD);
115         }
116
117       if (is_output)
118         {
119           iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
120           iph_offset1 = vnet_buffer (b[1])->ip.save_rewrite_length;
121           iph_offset2 = vnet_buffer (b[2])->ip.save_rewrite_length;
122           iph_offset3 = vnet_buffer (b[3])->ip.save_rewrite_length;
123         }
124
125       ip0 =
126         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) + iph_offset0);
127       ip1 =
128         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[1]) + iph_offset1);
129       ip2 =
130         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[2]) + iph_offset2);
131       ip3 =
132         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[3]) + iph_offset3);
133
134       vnet_feature_next (&arc_next0, b[0]);
135       vnet_feature_next (&arc_next1, b[1]);
136       vnet_feature_next (&arc_next2, b[2]);
137       vnet_feature_next (&arc_next3, b[3]);
138
139       vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
140       vnet_buffer2 (b[1])->nat.arc_next = arc_next1;
141       vnet_buffer2 (b[2])->nat.arc_next = arc_next2;
142       vnet_buffer2 (b[3])->nat.arc_next = arc_next3;
143
144       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
145       sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
146       sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
147       sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
148
149       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
150       rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
151       rx_fib_index2 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index2);
152       rx_fib_index3 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index3);
153
154       if (is_in2out)
155         {
156           ti[0] = nm->worker_in2out_cb (ip0, rx_fib_index0, is_output);
157           ti[1] = nm->worker_in2out_cb (ip1, rx_fib_index1, is_output);
158           ti[2] = nm->worker_in2out_cb (ip2, rx_fib_index2, is_output);
159           ti[3] = nm->worker_in2out_cb (ip3, rx_fib_index3, is_output);
160         }
161       else
162         {
163           ti[0] = nm->worker_out2in_cb (b[0], ip0, rx_fib_index0, is_output);
164           ti[1] = nm->worker_out2in_cb (b[1], ip1, rx_fib_index1, is_output);
165           ti[2] = nm->worker_out2in_cb (b[2], ip2, rx_fib_index2, is_output);
166           ti[3] = nm->worker_out2in_cb (b[3], ip3, rx_fib_index3, is_output);
167         }
168
169       if (ti[0] == thread_index)
170         same_worker++;
171       else
172         do_handoff++;
173
174       if (ti[1] == thread_index)
175         same_worker++;
176       else
177         do_handoff++;
178
179       if (ti[2] == thread_index)
180         same_worker++;
181       else
182         do_handoff++;
183
184       if (ti[3] == thread_index)
185         same_worker++;
186       else
187         do_handoff++;
188
189       b += 4;
190       ti += 4;
191       n_left_from -= 4;
192     }
193
194   while (n_left_from > 0)
195     {
196       u32 arc_next0;
197       u32 sw_if_index0;
198       u32 rx_fib_index0;
199       u32 iph_offset0 = 0;
200       ip4_header_t *ip0;
201
202       if (is_output)
203         iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
204
205       ip0 =
206         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) + iph_offset0);
207
208       vnet_feature_next (&arc_next0, b[0]);
209       vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
210
211       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
212       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
213
214       if (is_in2out)
215         {
216           ti[0] = nm->worker_in2out_cb (ip0, rx_fib_index0, is_output);
217         }
218       else
219         {
220           ti[0] = nm->worker_out2in_cb (b[0], ip0, rx_fib_index0, is_output);
221         }
222
223       if (ti[0] == thread_index)
224         same_worker++;
225       else
226         do_handoff++;
227
228       b += 1;
229       ti += 1;
230       n_left_from -= 1;
231     }
232
233   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
234     {
235       u32 i;
236       b = bufs;
237       ti = thread_indices;
238
239       for (i = 0; i < frame->n_vectors; i++)
240         {
241           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
242             {
243               nat44_ei_handoff_trace_t *t =
244                 vlib_add_trace (vm, node, b[0], sizeof (*t));
245               t->next_worker_index = ti[0];
246               t->trace_index = vlib_buffer_get_trace_index (b[0]);
247               t->in2out = is_in2out;
248               t->output = is_output;
249
250               b += 1;
251               ti += 1;
252             }
253           else
254             break;
255         }
256     }
257
258   n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
259                                          frame->n_vectors, 1);
260
261   if (n_enq < frame->n_vectors)
262     {
263       vlib_node_increment_counter (vm, node->node_index,
264                                    NAT44_EI_HANDOFF_ERROR_CONGESTION_DROP,
265                                    frame->n_vectors - n_enq);
266     }
267
268   vlib_node_increment_counter (
269     vm, node->node_index, NAT44_EI_HANDOFF_ERROR_SAME_WORKER, same_worker);
270   vlib_node_increment_counter (vm, node->node_index,
271                                NAT44_EI_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
272   return frame->n_vectors;
273 }
274
275 VLIB_NODE_FN (nat44_ei_in2out_worker_handoff_node)
276 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
277 {
278   return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 0, 1);
279 }
280
281 VLIB_NODE_FN (nat44_ei_in2out_output_worker_handoff_node)
282 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
283 {
284   return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 1, 1);
285 }
286
287 VLIB_NODE_FN (nat44_ei_out2in_worker_handoff_node)
288 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
289 {
290   return nat44_ei_worker_handoff_fn_inline (vm, node, frame, 0, 0);
291 }
292
293 VLIB_REGISTER_NODE (nat44_ei_in2out_output_worker_handoff_node) = {
294   .name = "nat44-ei-in2out-output-worker-handoff",
295   .vector_size = sizeof (u32),
296   .format_trace = format_nat44_ei_handoff_trace,
297   .type = VLIB_NODE_TYPE_INTERNAL,
298   .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
299   .error_strings = nat44_ei_handoff_error_strings,
300 };
301
302 VLIB_REGISTER_NODE (nat44_ei_in2out_worker_handoff_node) = {
303   .name = "nat44-ei-in2out-worker-handoff",
304   .vector_size = sizeof (u32),
305   .format_trace = format_nat44_ei_handoff_trace,
306   .type = VLIB_NODE_TYPE_INTERNAL,
307   .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
308   .error_strings = nat44_ei_handoff_error_strings,
309 };
310
311 VLIB_REGISTER_NODE (nat44_ei_out2in_worker_handoff_node) = {
312   .name = "nat44-ei-out2in-worker-handoff",
313   .vector_size = sizeof (u32),
314   .format_trace = format_nat44_ei_handoff_trace,
315   .type = VLIB_NODE_TYPE_INTERNAL,
316   .n_errors = ARRAY_LEN (nat44_ei_handoff_error_strings),
317   .error_strings = nat44_ei_handoff_error_strings,
318 };
319
320 /*
321  * fd.io coding-style-patch-verification: ON
322  *
323  * Local Variables:
324  * eval: (c-set-style "gnu")
325  * End:
326  */