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