NAT: VPP-1552 code migration from old multiarch scheme
[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
27 typedef struct
28 {
29   u32 next_worker_index;
30   u8 in2out;
31 } nat44_handoff_trace_t;
32
33 #define foreach_nat44_handoff_error                       \
34 _(CONGESTION_DROP, "congestion drop")                     \
35 _(SAME_WORKER, "same worker")                             \
36 _(DO_HANDOFF, "do handoff")
37
38 typedef enum
39 {
40 #define _(sym,str) NAT44_HANDOFF_ERROR_##sym,
41   foreach_nat44_handoff_error
42 #undef _
43     NAT44_HANDOFF_N_ERROR,
44 } nat44_handoff_error_t;
45
46 static char *nat44_handoff_error_strings[] = {
47 #define _(sym,string) string,
48   foreach_nat44_handoff_error
49 #undef _
50 };
51
52
53 static u8 *
54 format_nat44_handoff_trace (u8 * s, va_list * args)
55 {
56   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
58   nat44_handoff_trace_t *t = va_arg (*args, nat44_handoff_trace_t *);
59   char *tag;
60
61   tag = t->in2out ? "IN2OUT" : "OUT2IN";
62   s =
63     format (s, "NAT44_%s_WORKER_HANDOFF: next-worker %d", tag,
64             t->next_worker_index);
65
66   return s;
67 }
68
69 static inline uword
70 nat44_worker_handoff_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
71                                 vlib_frame_t * frame, u8 is_output,
72                                 u8 is_in2out)
73 {
74   snat_main_t *sm = &snat_main;
75   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
76   u32 n_enq, n_left_from, *from;
77   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
78   u32 fq_index;
79   snat_get_worker_function_t *get_worker;
80   u32 thread_index = vm->thread_index;
81   u32 do_handoff = 0, same_worker = 0;
82
83   from = vlib_frame_vector_args (frame);
84   n_left_from = frame->n_vectors;
85   vlib_get_buffers (vm, from, bufs, n_left_from);
86
87   b = bufs;
88   ti = thread_indices;
89
90   ASSERT (vec_len (sm->workers));
91
92   if (is_in2out)
93     {
94       get_worker = sm->worker_in2out_cb;
95       if (is_output)
96         fq_index = sm->fq_in2out_output_index;
97       else
98         fq_index = sm->fq_in2out_index;
99     }
100   else
101     {
102       fq_index = sm->fq_out2in_index;
103       get_worker = sm->worker_out2in_cb;
104     }
105
106   while (n_left_from > 0)
107     {
108       u32 sw_if_index0;
109       u32 rx_fib_index0;
110       ip4_header_t *ip0;
111
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);
116
117       if (ti[0] != thread_index)
118         do_handoff++;
119       else
120         same_worker++;
121
122       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
123                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
124         {
125           nat44_handoff_trace_t *t =
126             vlib_add_trace (vm, node, b[0], sizeof (*t));
127           t->next_worker_index = ti[0];
128           t->in2out = is_in2out;
129         }
130
131       n_left_from -= 1;
132       ti += 1;
133       b += 1;
134     }
135
136   n_enq =
137     vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
138                                    frame->n_vectors, 1);
139
140   if (n_enq < frame->n_vectors)
141     vlib_node_increment_counter (vm, node->node_index,
142                                  NAT44_HANDOFF_ERROR_CONGESTION_DROP,
143                                  frame->n_vectors - n_enq);
144   vlib_node_increment_counter (vm, node->node_index,
145                                NAT44_HANDOFF_ERROR_SAME_WORKER, same_worker);
146   vlib_node_increment_counter (vm, node->node_index,
147                                NAT44_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
148   return frame->n_vectors;
149 }
150
151 VLIB_NODE_FN (snat_in2out_worker_handoff_node) (vlib_main_t * vm,
152                                                 vlib_node_runtime_t * node,
153                                                 vlib_frame_t * frame)
154 {
155   return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
156 }
157
158 /* *INDENT-OFF* */
159 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
160   .name = "nat44-in2out-worker-handoff",
161   .vector_size = sizeof (u32),
162   .format_trace = format_nat44_handoff_trace,
163   .type = VLIB_NODE_TYPE_INTERNAL,
164   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
165   .error_strings = nat44_handoff_error_strings,
166   .n_next_nodes = 1,
167   .next_nodes = {
168     [0] = "error-drop",
169   },
170 };
171 /* *INDENT-ON* */
172
173 VLIB_NODE_FN (snat_in2out_output_worker_handoff_node) (vlib_main_t * vm,
174                                                        vlib_node_runtime_t *
175                                                        node,
176                                                        vlib_frame_t * frame)
177 {
178   return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
179 }
180
181 /* *INDENT-OFF* */
182 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
183   .name = "nat44-in2out-output-worker-handoff",
184   .vector_size = sizeof (u32),
185   .format_trace = format_nat44_handoff_trace,
186   .type = VLIB_NODE_TYPE_INTERNAL,
187   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
188   .error_strings = nat44_handoff_error_strings,
189   .n_next_nodes = 1,
190   .next_nodes = {
191     [0] = "error-drop",
192   },
193 };
194 /* *INDENT-ON* */
195
196 VLIB_NODE_FN (snat_out2in_worker_handoff_node) (vlib_main_t * vm,
197                                                 vlib_node_runtime_t * node,
198                                                 vlib_frame_t * frame)
199 {
200   return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
201 }
202
203 /* *INDENT-OFF* */
204 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
205   .name = "nat44-out2in-worker-handoff",
206   .vector_size = sizeof (u32),
207   .format_trace = format_nat44_handoff_trace,
208   .type = VLIB_NODE_TYPE_INTERNAL,
209   .n_errors = ARRAY_LEN(nat44_handoff_error_strings),
210   .error_strings = nat44_handoff_error_strings,
211   .n_next_nodes = 1,
212   .next_nodes = {
213     [0] = "error-drop",
214   },
215 };
216 /* *INDENT-ON* */
217
218 /*
219  * fd.io coding-style-patch-verification: ON
220  *
221  * Local Variables:
222  * eval: (c-set-style "gnu")
223  * End:
224  */