FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
handoff.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <vnet/vnet.h>
18 #include <vppinfra/xxhash.h>
19 #include <vlib/threads.h>
20 #include <vnet/handoff.h>
21 #include <vnet/feature/feature.h>
22 
23 typedef struct
24 {
28 
29 typedef struct
30 {
34 
36 
37  /* Worker handoff index */
39 
40  u64 (*hash_fn) (ethernet_header_t *);
42 
44 
45 #ifndef CLIB_MARCH_VARIANT
47 #endif /* CLIB_MARCH_VARIANT */
48 
49 typedef struct
50 {
55 
56 #define foreach_worker_handoff_error \
57  _(CONGESTION_DROP, "congestion drop")
58 
59 typedef enum
60 {
61 #define _(sym,str) WORKER_HANDOFF_ERROR_##sym,
63 #undef _
66 
67 static char *worker_handoff_error_strings[] = {
68 #define _(sym,string) string,
70 #undef _
71 };
72 
73 /* packet trace format function */
74 static u8 *
75 format_worker_handoff_trace (u8 * s, va_list * args)
76 {
77  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
79  worker_handoff_trace_t *t = va_arg (*args, worker_handoff_trace_t *);
80 
81  s =
82  format (s, "worker-handoff: sw_if_index %d, next_worker %d, buffer 0x%x",
84  return s;
85 }
86 
90 {
93  u32 n_enq, n_left_from, *from;
94  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
95 
97  n_left_from = frame->n_vectors;
99 
100  b = bufs;
101  ti = thread_indices;
102 
103  while (n_left_from > 0)
104  {
105  u32 sw_if_index0;
106  u32 hash;
107  u64 hash_key;
109  u32 index0;
110 
111 
112  sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
113  ASSERT (hm->if_data);
114  ihd0 = vec_elt_at_index (hm->if_data, sw_if_index0);
115 
116  /*
117  * Force unknown traffic onto worker 0,
118  * and into ethernet-input. $$$$ add more hashes.
119  */
120 
121  /* Compute ingress LB hash */
122  hash_key = hm->hash_fn ((ethernet_header_t *)
124  hash = (u32) clib_xxhash (hash_key);
125 
126  /* if input node did not specify next index, then packet
127  should go to ethernet-input */
128 
129  if (PREDICT_TRUE (is_pow2 (vec_len (ihd0->workers))))
130  index0 = hash & (vec_len (ihd0->workers) - 1);
131  else
132  index0 = hash % vec_len (ihd0->workers);
133 
134  ti[0] = hm->first_worker_index + ihd0->workers[index0];
135 
136  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
137  && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
138  {
140  vlib_add_trace (vm, node, b[0], sizeof (*t));
141  t->sw_if_index = sw_if_index0;
142  t->next_worker_index = ti[0];
144  }
145 
146  /* next */
147  n_left_from -= 1;
148  ti += 1;
149  b += 1;
150  }
151 
153  thread_indices, frame->n_vectors, 1);
154 
155  if (n_enq < frame->n_vectors)
156  vlib_node_increment_counter (vm, node->node_index,
157  WORKER_HANDOFF_ERROR_CONGESTION_DROP,
158  frame->n_vectors - n_enq);
159  return frame->n_vectors;
160 }
161 
162 /* *INDENT-OFF* */
164  .name = "worker-handoff",
165  .vector_size = sizeof (u32),
166  .format_trace = format_worker_handoff_trace,
169  .error_strings = worker_handoff_error_strings,
170 
171  .n_next_nodes = 1,
172  .next_nodes = {
173  [0] = "error-drop",
174  },
175 };
176 
177 /* *INDENT-ON* */
178 
179 #ifndef CLIB_MARCH_VARIANT
180 int
182  uword * bitmap, int enable_disable)
183 {
186  vnet_main_t *vnm = vnet_get_main ();
188  int i, rv = 0;
189 
191  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
192 
195  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
196 
197  if (clib_bitmap_last_set (bitmap) >= hm->num_workers)
198  return VNET_API_ERROR_INVALID_WORKER;
199 
200  if (hm->frame_queue_index == ~0)
201  {
202  vlib_node_t *n = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
204  }
205 
208 
209  vec_free (d->workers);
211 
212  if (enable_disable)
213  {
214  d->workers_bitmap = bitmap;
215  /* *INDENT-OFF* */
216  clib_bitmap_foreach (i, bitmap)
217  {
218  vec_add1(d->workers, i);
219  }
220  /* *INDENT-ON* */
221  }
222 
223  vnet_feature_enable_disable ("device-input", "worker-handoff",
224  sw_if_index, enable_disable, 0, 0);
225  return rv;
226 }
227 
228 static clib_error_t *
230  unformat_input_t * input,
231  vlib_cli_command_t * cmd)
232 {
234  u32 sw_if_index = ~0;
235  int enable_disable = 1;
236  uword *bitmap = 0;
237  u32 sym = ~0;
238 
239  int rv = 0;
240 
242  {
243  if (unformat (input, "disable"))
244  enable_disable = 0;
245  else if (unformat (input, "workers %U", unformat_bitmap_list, &bitmap))
246  ;
247  else if (unformat (input, "%U", unformat_vnet_sw_interface,
249  ;
250  else if (unformat (input, "symmetrical"))
251  sym = 1;
252  else if (unformat (input, "asymmetrical"))
253  sym = 0;
254  else
255  break;
256  }
257 
258  if (sw_if_index == ~0)
259  return clib_error_return (0, "Please specify an interface...");
260 
261  if (bitmap == 0)
262  return clib_error_return (0, "Please specify list of workers...");
263 
264  rv =
266  enable_disable);
267 
268  switch (rv)
269  {
270  case 0:
271  break;
272 
273  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
274  return clib_error_return (0, "Invalid interface");
275  break;
276 
277  case VNET_API_ERROR_INVALID_WORKER:
278  return clib_error_return (0, "Invalid worker(s)");
279  break;
280 
281  case VNET_API_ERROR_UNIMPLEMENTED:
282  return clib_error_return (0,
283  "Device driver doesn't support redirection");
284  break;
285 
286  default:
287  return clib_error_return (0, "unknown return value %d", rv);
288  }
289 
290  if (sym == 1)
291  hm->hash_fn = eth_get_sym_key;
292  else if (sym == 0)
293  hm->hash_fn = eth_get_key;
294 
295  return 0;
296 }
297 
298 /* *INDENT-OFF* */
300  .path = "set interface handoff",
301  .short_help =
302  "set interface handoff <interface-name> workers <workers-list> [symmetrical|asymmetrical]",
304 };
305 /* *INDENT-ON* */
306 
307 clib_error_t *
309 {
313  uword *p;
314 
316  return error;
317 
319  /* Only the standard vnet worker threads are supported */
320  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
321  if (p)
322  {
323  tr = (vlib_thread_registration_t *) p[0];
324  if (tr)
325  {
326  hm->num_workers = tr->count;
328  }
329  }
330 
331  hm->hash_fn = eth_get_key;
332  hm->frame_queue_index = ~0;
333 
334  return 0;
335 }
336 
338 
339 #endif /* CLIB_MARCH_VARIANT */
340 /*
341  * fd.io coding-style-patch-verification: ON
342  *
343  * Local Variables:
344  * eval: (c-set-style "gnu")
345  * End:
346  */
worker_handoff_trace_t::buffer_index
u32 buffer_index
Definition: handoff.c:53
handoff_main_t::num_workers
u32 num_workers
Definition: handoff.c:32
vnet_sw_interface_t::type
vnet_sw_interface_type_t type
Definition: interface.h:871
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vnet_sw_interface_t
Definition: interface.h:869
vlib_buffer_enqueue_to_thread
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:383
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
per_inteface_handoff_data_t::workers_bitmap
uword * workers_bitmap
Definition: handoff.c:25
interface_handoff_enable_disable
int interface_handoff_enable_disable(vlib_main_t *vm, u32 sw_if_index, uword *bitmap, int enable_disable)
Definition: handoff.c:181
handoff_main
handoff_main_t handoff_main
Definition: handoff.c:46
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
worker_handoff_node
vlib_node_registration_t worker_handoff_node
(constructor) VLIB_REGISTER_NODE (worker_handoff_node)
Definition: handoff.c:163
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
handoff_init
clib_error_t * handoff_init(vlib_main_t *vm)
Definition: handoff.c:308
threads.h
per_inteface_handoff_data_t
Definition: handoff.c:23
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
set_interface_handoff_command
static vlib_cli_command_t set_interface_handoff_command
(constructor) VLIB_CLI_COMMAND (set_interface_handoff_command)
Definition: handoff.c:299
u16
unsigned short u16
Definition: types.h:57
vlib_call_init_function
#define vlib_call_init_function(vm, x)
Definition: init.h:259
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
worker_handoff_error_t
worker_handoff_error_t
Definition: handoff.c:59
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vnet_get_sw_interface
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:58
handoff_main_t::hash_fn
u64(* hash_fn)(ethernet_header_t *)
Definition: handoff.c:40
worker_handoff_trace_t
Definition: handoff.c:49
handoff.h
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_frame_t
Definition: node.h:372
vlib_thread_registration_::count
u32 count
Definition: threads.h:38
threads_init
clib_error_t * threads_init(vlib_main_t *vm)
Definition: threads.c:1628
error
Definition: cJSON.c:88
vlib_frame_queue_main_init
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1561
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
handoff_main_t::first_worker_index
u32 first_worker_index
Definition: handoff.c:33
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
ti
u32 ti
Definition: interface_output.c:425
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_node_t::index
u32 index
Definition: node.h:269
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
feature.h
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_get_buffer_index
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:324
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
handoff_main_t
Definition: handoff.c:29
uword
u64 uword
Definition: types.h:112
format_worker_handoff_trace
static u8 * format_worker_handoff_trace(u8 *s, va_list *args)
Definition: handoff.c:75
ethernet_header_t
Definition: packet.h:52
WORKER_HANDOFF_N_ERROR
@ WORKER_HANDOFF_N_ERROR
Definition: handoff.c:64
eth_get_sym_key
static u64 eth_get_sym_key(ethernet_header_t *h0)
Definition: handoff.h:123
vlib_thread_main_t::thread_registrations_by_name
uword * thread_registrations_by_name
Definition: threads.h:251
vlib_thread_registration_::first_index
u32 first_index
Definition: threads.h:44
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
clib_xxhash
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vnet_interface_main_t::sw_interfaces
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:1015
worker_handoff_error_strings
static char * worker_handoff_error_strings[]
Definition: handoff.c:67
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
set_interface_handoff_command_fn
static clib_error_t * set_interface_handoff_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: handoff.c:229
xxhash.h
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vlib_get_node_by_name
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
handoff_main_t::frame_queue_index
u32 frame_queue_index
Definition: handoff.c:38
handoff_main_t::if_data
per_inteface_handoff_data_t * if_data
Definition: handoff.c:35
u64
unsigned long u64
Definition: types.h:89
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:462
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
foreach_worker_handoff_error
#define foreach_worker_handoff_error
Definition: handoff.c:56
vlib_thread_main_t
Definition: threads.h:243
n_vectors
return frame n_vectors
Definition: nat44_ei_hairpinning.c:488
vnet_feature_enable_disable
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: pnat_test_stubs.h:50
clib_bitmap_last_set
static uword clib_bitmap_last_set(uword *ai)
Return the higest numbered set bit in a bitmap.
Definition: bitmap.h:410
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
per_inteface_handoff_data_t::workers
u32 * workers
Definition: handoff.c:26
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
eth_get_key
static u64 eth_get_key(ethernet_header_t *h0)
Definition: handoff.h:189
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
i
int i
Definition: flowhash_template.h:376
rv
int __clib_unused rv
Definition: application.c:491
handoff_main_t::cached_next_index
u32 cached_next_index
Definition: handoff.c:31
vnet.h
VNET_SW_INTERFACE_TYPE_HARDWARE
@ VNET_SW_INTERFACE_TYPE_HARDWARE
Definition: interface.h:764
clib_bitmap_foreach
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
vlib_node_runtime_t
Definition: node.h:454
vlib_thread_registration_
Definition: threads.h:27
vlib_cli_command_t
Definition: cli.h:92
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
is_pow2
static uword is_pow2(uword x)
Definition: clib.h:267
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
worker_handoff_trace_t::sw_if_index
u32 sw_if_index
Definition: handoff.c:51
unformat_bitmap_list
__clib_export uword unformat_bitmap_list(unformat_input_t *input, va_list *va)
unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
Definition: bitmap.c:55
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
worker_handoff_trace_t::next_worker_index
u32 next_worker_index
Definition: handoff.c:52