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