virtio: enable the interrupt support for uio_pci_generic
[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 static u8 *
56 format_nat44_handoff_trace (u8 * s, va_list * args)
57 {
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 *);
61   char *tag, *output;
62
63   tag = t->in2out ? "IN2OUT" : "OUT2IN";
64   output = t->output ? "OUTPUT-FEATURE" : "";
65   s =
66     format (s, "NAT44_%s_WORKER_HANDOFF %s: next-worker %d trace index %d",
67             tag, output, t->next_worker_index, t->trace_index);
68
69   return s;
70 }
71
72 static inline uword
73 nat44_worker_handoff_fn_inline (vlib_main_t * vm,
74                                 vlib_node_runtime_t * node,
75                                 vlib_frame_t * frame, u8 is_output,
76                                 u8 is_in2out)
77 {
78   u32 n_enq, n_left_from, *from, do_handoff = 0, same_worker = 0;
79
80   u16 thread_indices[VLIB_FRAME_SIZE], *ti = thread_indices;
81   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
82   snat_main_t *sm = &snat_main;
83
84   u32 fq_index, thread_index = vm->thread_index;
85
86   from = vlib_frame_vector_args (frame);
87   n_left_from = frame->n_vectors;
88
89   vlib_get_buffers (vm, from, b, n_left_from);
90
91   if (is_in2out)
92     {
93       fq_index = is_output ? sm->fq_in2out_output_index : sm->fq_in2out_index;
94     }
95   else
96     {
97       fq_index = sm->fq_out2in_index;
98     }
99
100   while (n_left_from >= 4)
101     {
102       u32 arc_next0, arc_next1, arc_next2, arc_next3;
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], LOAD);
111           vlib_prefetch_buffer_header (b[5], LOAD);
112           vlib_prefetch_buffer_header (b[6], LOAD);
113           vlib_prefetch_buffer_header (b[7], LOAD);
114           CLIB_PREFETCH (&b[4]->data, CLIB_CACHE_LINE_BYTES, LOAD);
115           CLIB_PREFETCH (&b[5]->data, CLIB_CACHE_LINE_BYTES, LOAD);
116           CLIB_PREFETCH (&b[6]->data, CLIB_CACHE_LINE_BYTES, LOAD);
117           CLIB_PREFETCH (&b[7]->data, CLIB_CACHE_LINE_BYTES, LOAD);
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       vnet_feature_next (&arc_next0, b[0]);
138       vnet_feature_next (&arc_next1, b[1]);
139       vnet_feature_next (&arc_next2, b[2]);
140       vnet_feature_next (&arc_next3, b[3]);
141
142       vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
143       vnet_buffer2 (b[1])->nat.arc_next = arc_next1;
144       vnet_buffer2 (b[2])->nat.arc_next = arc_next2;
145       vnet_buffer2 (b[3])->nat.arc_next = arc_next3;
146
147       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
148       sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
149       sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
150       sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
151
152       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
153       rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
154       rx_fib_index2 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index2);
155       rx_fib_index3 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index3);
156
157       if (is_in2out)
158         {
159           ti[0] = sm->worker_in2out_cb (ip0, rx_fib_index0, is_output);
160           ti[1] = sm->worker_in2out_cb (ip1, rx_fib_index1, is_output);
161           ti[2] = sm->worker_in2out_cb (ip2, rx_fib_index2, is_output);
162           ti[3] = sm->worker_in2out_cb (ip3, rx_fib_index3, is_output);
163         }
164       else
165         {
166           ti[0] = sm->worker_out2in_cb (b[0], ip0, rx_fib_index0, is_output);
167           ti[1] = sm->worker_out2in_cb (b[1], ip1, rx_fib_index1, is_output);
168           ti[2] = sm->worker_out2in_cb (b[2], ip2, rx_fib_index2, is_output);
169           ti[3] = sm->worker_out2in_cb (b[3], ip3, rx_fib_index3, is_output);
170         }
171
172       if (ti[0] == thread_index)
173         same_worker++;
174       else
175         do_handoff++;
176
177       if (ti[1] == thread_index)
178         same_worker++;
179       else
180         do_handoff++;
181
182       if (ti[2] == thread_index)
183         same_worker++;
184       else
185         do_handoff++;
186
187       if (ti[3] == thread_index)
188         same_worker++;
189       else
190         do_handoff++;
191
192       b += 4;
193       ti += 4;
194       n_left_from -= 4;
195     }
196
197   while (n_left_from > 0)
198     {
199       u32 arc_next0;
200       u32 sw_if_index0;
201       u32 rx_fib_index0;
202       u32 iph_offset0 = 0;
203       ip4_header_t *ip0;
204
205
206       if (is_output)
207         iph_offset0 = vnet_buffer (b[0])->ip.save_rewrite_length;
208
209       ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b[0]) +
210                               iph_offset0);
211
212       vnet_feature_next (&arc_next0, b[0]);
213       vnet_buffer2 (b[0])->nat.arc_next = arc_next0;
214
215       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
216       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
217
218       if (is_in2out)
219         {
220           ti[0] = sm->worker_in2out_cb (ip0, rx_fib_index0, is_output);
221         }
222       else
223         {
224           ti[0] = sm->worker_out2in_cb (b[0], ip0, rx_fib_index0, is_output);
225         }
226
227       if (ti[0] == thread_index)
228         same_worker++;
229       else
230         do_handoff++;
231
232       b += 1;
233       ti += 1;
234       n_left_from -= 1;
235     }
236
237   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
238     {
239       u32 i;
240       b = bufs;
241       ti = thread_indices;
242
243       for (i = 0; i < frame->n_vectors; i++)
244         {
245           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
246             {
247               nat44_handoff_trace_t *t =
248                 vlib_add_trace (vm, node, b[0], sizeof (*t));
249               t->next_worker_index = ti[0];
250               t->trace_index = vlib_buffer_get_trace_index (b[0]);
251               t->in2out = is_in2out;
252               t->output = is_output;
253
254               b += 1;
255               ti += 1;
256             }
257           else
258             break;
259         }
260     }
261
262   n_enq = vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
263                                          frame->n_vectors, 1);
264
265   if (n_enq < frame->n_vectors)
266     {
267       vlib_node_increment_counter (vm, node->node_index,
268                                    NAT44_HANDOFF_ERROR_CONGESTION_DROP,
269                                    frame->n_vectors - n_enq);
270     }
271
272   vlib_node_increment_counter (vm, node->node_index,
273                                NAT44_HANDOFF_ERROR_SAME_WORKER, same_worker);
274   vlib_node_increment_counter (vm, node->node_index,
275                                NAT44_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
276   return frame->n_vectors;
277 }
278
279 VLIB_NODE_FN (snat_in2out_worker_handoff_node) (vlib_main_t * vm,
280                                                 vlib_node_runtime_t * node,
281                                                 vlib_frame_t * frame)
282 {
283   return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
284 }
285
286 /* *INDENT-OFF* */
287 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
288   .name = "nat44-in2out-worker-handoff",
289   .vector_size = sizeof (u32),
290   .sibling_of = "nat-default",
291   .format_trace = format_nat44_handoff_trace,
292   .type = VLIB_NODE_TYPE_INTERNAL,
293   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
294   .error_strings = nat44_handoff_error_strings,
295 };
296 /* *INDENT-ON* */
297
298 VLIB_NODE_FN (snat_in2out_output_worker_handoff_node) (vlib_main_t * vm,
299                                                        vlib_node_runtime_t *
300                                                        node,
301                                                        vlib_frame_t * frame)
302 {
303   return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
304 }
305
306 /* *INDENT-OFF* */
307 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
308   .name = "nat44-in2out-output-worker-handoff",
309   .vector_size = sizeof (u32),
310   .sibling_of = "nat-default",
311   .format_trace = format_nat44_handoff_trace,
312   .type = VLIB_NODE_TYPE_INTERNAL,
313   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
314   .error_strings = nat44_handoff_error_strings,
315 };
316 /* *INDENT-ON* */
317
318 VLIB_NODE_FN (snat_out2in_worker_handoff_node) (vlib_main_t * vm,
319                                                 vlib_node_runtime_t * node,
320                                                 vlib_frame_t * frame)
321 {
322   return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
323 }
324
325 /* *INDENT-OFF* */
326 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
327   .name = "nat44-out2in-worker-handoff",
328   .vector_size = sizeof (u32),
329   .sibling_of = "nat-default",
330   .format_trace = format_nat44_handoff_trace,
331   .type = VLIB_NODE_TYPE_INTERNAL,
332   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
333   .error_strings = nat44_handoff_error_strings,
334 };
335 /* *INDENT-ON* */
336
337 /*
338  * fd.io coding-style-patch-verification: ON
339  *
340  * Local Variables:
341  * eval: (c-set-style "gnu")
342  * End:
343  */