FD.io VPP  v19.01.1-17-ge106252
Vector Packet Processing
nat44_handoff.c
Go to the documentation of this file.
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 {
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,
42 #undef _
45 
46 static char *nat44_handoff_error_strings[] = {
47 #define _(sym,string) string,
49 #undef _
50 };
51 
52 
56 
57 static u8 *
58 format_nat44_handoff_trace (u8 * s, va_list * args)
59 {
60  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62  nat44_handoff_trace_t *t = va_arg (*args, nat44_handoff_trace_t *);
63  char *tag;
64 
65  tag = t->in2out ? "IN2OUT" : "OUT2IN";
66  s =
67  format (s, "NAT44_%s_WORKER_HANDOFF: next-worker %d", tag,
69 
70  return s;
71 }
72 
73 static inline uword
75  vlib_frame_t * frame, u8 is_output,
76  u8 is_in2out)
77 {
78  snat_main_t *sm = &snat_main;
79  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
80  u32 n_enq, n_left_from, *from;
81  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
82  u32 fq_index;
83  snat_get_worker_function_t *get_worker;
84  u32 thread_index = vm->thread_index;
85  u32 do_handoff = 0, same_worker = 0;
86 
87  from = vlib_frame_vector_args (frame);
88  n_left_from = frame->n_vectors;
89  vlib_get_buffers (vm, from, bufs, n_left_from);
90 
91  b = bufs;
92  ti = thread_indices;
93 
94  ASSERT (vec_len (sm->workers));
95 
96  if (is_in2out)
97  {
98  get_worker = sm->worker_in2out_cb;
99  if (is_output)
100  fq_index = sm->fq_in2out_output_index;
101  else
102  fq_index = sm->fq_in2out_index;
103  }
104  else
105  {
106  fq_index = sm->fq_out2in_index;
107  get_worker = sm->worker_out2in_cb;
108  }
109 
110  while (n_left_from > 0)
111  {
112  u32 sw_if_index0;
113  u32 rx_fib_index0;
114  ip4_header_t *ip0;
115 
116  sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
117  rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
118  ip0 = vlib_buffer_get_current (b[0]);
119  ti[0] = get_worker (ip0, rx_fib_index0);
120 
121  if (ti[0] != thread_index)
122  do_handoff++;
123  else
124  same_worker++;
125 
127  && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
128  {
130  vlib_add_trace (vm, node, b[0], sizeof (*t));
131  t->next_worker_index = ti[0];
132  t->in2out = is_in2out;
133  }
134 
135  n_left_from -= 1;
136  ti += 1;
137  b += 1;
138  }
139 
140  n_enq =
141  vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
142  frame->n_vectors, 1);
143 
144  if (n_enq < frame->n_vectors)
146  NAT44_HANDOFF_ERROR_CONGESTION_DROP,
147  frame->n_vectors - n_enq);
149  NAT44_HANDOFF_ERROR_SAME_WORKER, same_worker);
151  NAT44_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
152  return frame->n_vectors;
153 }
154 
155 static uword
157  vlib_node_runtime_t * node,
158  vlib_frame_t * frame)
159 {
160  return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 1);
161 }
162 
163 /* *INDENT-OFF* */
165  .function = snat_in2out_worker_handoff_fn,
166  .name = "nat44-in2out-worker-handoff",
167  .vector_size = sizeof (u32),
168  .format_trace = format_nat44_handoff_trace,
169  .type = VLIB_NODE_TYPE_INTERNAL,
171  .error_strings = nat44_handoff_error_strings,
172  .n_next_nodes = 1,
173  .next_nodes = {
174  [0] = "error-drop",
175  },
176 };
177 /* *INDENT-ON* */
178 
181 
182 static uword
184  vlib_node_runtime_t * node,
185  vlib_frame_t * frame)
186 {
187  return nat44_worker_handoff_fn_inline (vm, node, frame, 1, 1);
188 }
189 
190 /* *INDENT-OFF* */
193  .name = "nat44-in2out-output-worker-handoff",
194  .vector_size = sizeof (u32),
195  .format_trace = format_nat44_handoff_trace,
196  .type = VLIB_NODE_TYPE_INTERNAL,
198  .error_strings = nat44_handoff_error_strings,
199  .n_next_nodes = 1,
200  .next_nodes = {
201  [0] = "error-drop",
202  },
203 };
204 /* *INDENT-ON* */
205 
208 
209 static uword
211  vlib_node_runtime_t * node,
212  vlib_frame_t * frame)
213 {
214  return nat44_worker_handoff_fn_inline (vm, node, frame, 0, 0);
215 }
216 
217 /* *INDENT-OFF* */
219  .function = snat_out2in_worker_handoff_fn,
220  .name = "nat44-out2in-worker-handoff",
221  .vector_size = sizeof (u32),
222  .format_trace = format_nat44_handoff_trace,
223  .type = VLIB_NODE_TYPE_INTERNAL,
225  .error_strings = nat44_handoff_error_strings,
226  .n_next_nodes = 1,
227  .next_nodes = {
228  [0] = "error-drop",
229  },
230 };
231 /* *INDENT-ON* */
232 
235 
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "gnu")
241  * End:
242  */
#define CLIB_UNUSED(x)
Definition: clib.h:82
static char * nat44_handoff_error_strings[]
Definition: nat44_handoff.c:46
#define foreach_nat44_handoff_error
Definition: nat44_handoff.c:33
static uword snat_in2out_worker_handoff_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
u32 fq_in2out_output_index
Definition: nat.h:493
u32 thread_index
Definition: main.h:179
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
Definition: nat44_handoff.c:53
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unsigned char u8
Definition: types.h:56
u32( snat_get_worker_function_t)(ip4_header_t *ip, u32 rx_fib_index)
Definition: nat.h:418
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: ip4_fib.c:224
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:401
unsigned short u16
Definition: types.h:57
nat44_handoff_error_t
Definition: nat44_handoff.c:38
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
VLIB_NODE_FUNCTION_MULTIARCH(snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn)
#define PREDICT_FALSE(x)
Definition: clib.h:111
u32 node_index
Node index.
Definition: node.h:519
static uword snat_in2out_output_worker_handoff_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
u32 fq_out2in_index
Definition: nat.h:494
snat_main_t snat_main
Definition: nat.c:38
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:420
vlib_main_t * vm
Definition: buffer.c:301
snat_get_worker_function_t * worker_in2out_cb
Definition: nat.h:440
#define ARRAY_LEN(x)
Definition: clib.h:62
#define ASSERT(truth)
snat_get_worker_function_t * worker_out2in_cb
Definition: nat.h:441
u32 fq_in2out_index
Definition: nat.h:492
static uword nat44_worker_handoff_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 is_output, u8 is_in2out)
Definition: nat44_handoff.c:74
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
struct _vlib_node_registration vlib_node_registration_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 * workers
Definition: nat.h:439
u64 uword
Definition: types.h:112
static uword snat_out2in_worker_handoff_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:274
vlib_node_registration_t snat_out2in_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node)
Definition: nat44_handoff.c:55
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:487
#define vnet_buffer(b)
Definition: buffer.h:368
vlib_node_registration_t snat_in2out_output_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node)
Definition: nat44_handoff.c:54
u16 flags
Copy of main node flags.
Definition: node.h:532
static u8 * format_nat44_handoff_trace(u8 *s, va_list *args)
Definition: nat44_handoff.c:58
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:145
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:326
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:117
Definition: defs.h:46