blob: ffac47e22dfd284845101d6915db2a49b3577438 [file] [log] [blame]
Damjan Marion0247b462016-06-08 01:37:11 +02001
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>
Damjan Marion22311502016-10-28 20:30:15 +020021#include <vnet/feature/feature.h>
Damjan Marion0247b462016-06-08 01:37:11 +020022
Dave Barachba868bb2016-08-08 09:51:21 -040023typedef struct
24{
25 uword *workers_bitmap;
26 u32 *workers;
Damjan Marion0247b462016-06-08 01:37:11 +020027} per_inteface_handoff_data_t;
28
Dave Barachba868bb2016-08-08 09:51:21 -040029typedef struct
30{
Damjan Marion0247b462016-06-08 01:37:11 +020031 u32 cached_next_index;
32 u32 num_workers;
33 u32 first_worker_index;
34
Dave Barachba868bb2016-08-08 09:51:21 -040035 per_inteface_handoff_data_t *if_data;
Damjan Marion0247b462016-06-08 01:37:11 +020036
Damjan Marionaaef1eb2016-11-08 17:37:01 +010037 /* Worker handoff index */
38 u32 frame_queue_index;
39
Gabriel Gannec0a36c02016-11-16 16:57:00 +010040 u64 (*hash_fn) (ethernet_header_t *);
Damjan Marion0247b462016-06-08 01:37:11 +020041} handoff_main_t;
42
43handoff_main_t handoff_main;
44
Dave Barachba868bb2016-08-08 09:51:21 -040045typedef struct
46{
Damjan Marion0247b462016-06-08 01:37:11 +020047 u32 sw_if_index;
48 u32 next_worker_index;
49 u32 buffer_index;
50} worker_handoff_trace_t;
51
Damjan Marion78fd7e82018-07-20 18:47:05 +020052#define foreach_worker_handoff_error \
53 _(CONGESTION_DROP, "congestion drop")
54
55typedef enum
56{
57#define _(sym,str) WORKER_HANDOFF_ERROR_##sym,
58 foreach_worker_handoff_error
59#undef _
60 WORKER_HANDOFF_N_ERROR,
61} worker_handoff_error_t;
62
63static char *worker_handoff_error_strings[] = {
64#define _(sym,string) string,
65 foreach_worker_handoff_error
66#undef _
67};
68
Damjan Marion0247b462016-06-08 01:37:11 +020069/* packet trace format function */
Dave Barachba868bb2016-08-08 09:51:21 -040070static u8 *
71format_worker_handoff_trace (u8 * s, va_list * args)
Damjan Marion0247b462016-06-08 01:37:11 +020072{
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040075 worker_handoff_trace_t *t = va_arg (*args, worker_handoff_trace_t *);
Damjan Marion0247b462016-06-08 01:37:11 +020076
Dave Barachba868bb2016-08-08 09:51:21 -040077 s =
78 format (s, "worker-handoff: sw_if_index %d, next_worker %d, buffer 0x%x",
79 t->sw_if_index, t->next_worker_index, t->buffer_index);
Damjan Marion0247b462016-06-08 01:37:11 +020080 return s;
81}
82
83vlib_node_registration_t handoff_node;
84
85static uword
86worker_handoff_node_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -040087 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marion0247b462016-06-08 01:37:11 +020088{
Dave Barachba868bb2016-08-08 09:51:21 -040089 handoff_main_t *hm = &handoff_main;
Damjan Marion4d56e052018-07-19 17:52:31 +020090 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Damjan Marion78fd7e82018-07-20 18:47:05 +020091 u32 n_enq, n_left_from, *from;
Damjan Marion4d56e052018-07-19 17:52:31 +020092 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
Damjan Marion0247b462016-06-08 01:37:11 +020093
94 from = vlib_frame_vector_args (frame);
95 n_left_from = frame->n_vectors;
Damjan Marion4d56e052018-07-19 17:52:31 +020096 vlib_get_buffers (vm, from, bufs, n_left_from);
97
98 b = bufs;
99 ti = thread_indices;
Damjan Marion0247b462016-06-08 01:37:11 +0200100
101 while (n_left_from > 0)
102 {
Damjan Marion0247b462016-06-08 01:37:11 +0200103 u32 sw_if_index0;
104 u32 hash;
105 u64 hash_key;
Dave Barachba868bb2016-08-08 09:51:21 -0400106 per_inteface_handoff_data_t *ihd0;
Damjan Marion0247b462016-06-08 01:37:11 +0200107 u32 index0;
108
Damjan Marion0247b462016-06-08 01:37:11 +0200109
Damjan Marion4d56e052018-07-19 17:52:31 +0200110 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
Damjan Marion0247b462016-06-08 01:37:11 +0200111 ASSERT (hm->if_data);
112 ihd0 = vec_elt_at_index (hm->if_data, sw_if_index0);
113
Damjan Marion0247b462016-06-08 01:37:11 +0200114 /*
115 * Force unknown traffic onto worker 0,
116 * and into ethernet-input. $$$$ add more hashes.
117 */
118
119 /* Compute ingress LB hash */
Damjan Marion4d56e052018-07-19 17:52:31 +0200120 hash_key = hm->hash_fn ((ethernet_header_t *)
121 vlib_buffer_get_current (b[0]));
Damjan Marion0247b462016-06-08 01:37:11 +0200122 hash = (u32) clib_xxhash (hash_key);
123
124 /* if input node did not specify next index, then packet
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700125 should go to ethernet-input */
Damjan Marion0247b462016-06-08 01:37:11 +0200126
127 if (PREDICT_TRUE (is_pow2 (vec_len (ihd0->workers))))
128 index0 = hash & (vec_len (ihd0->workers) - 1);
129 else
130 index0 = hash % vec_len (ihd0->workers);
131
Damjan Marion4d56e052018-07-19 17:52:31 +0200132 ti[0] = hm->first_worker_index + ihd0->workers[index0];
Damjan Marion0247b462016-06-08 01:37:11 +0200133
Dave Barachba868bb2016-08-08 09:51:21 -0400134 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
Damjan Marion4d56e052018-07-19 17:52:31 +0200135 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
Damjan Marion0247b462016-06-08 01:37:11 +0200136 {
137 worker_handoff_trace_t *t =
Damjan Marion4d56e052018-07-19 17:52:31 +0200138 vlib_add_trace (vm, node, b[0], sizeof (*t));
Damjan Marion0247b462016-06-08 01:37:11 +0200139 t->sw_if_index = sw_if_index0;
Damjan Marion4d56e052018-07-19 17:52:31 +0200140 t->next_worker_index = ti[0];
141 t->buffer_index = vlib_get_buffer_index (vm, b[0]);
Damjan Marion0247b462016-06-08 01:37:11 +0200142 }
143
Damjan Marion4d56e052018-07-19 17:52:31 +0200144 /* next */
145 n_left_from -= 1;
146 ti += 1;
147 b += 1;
Damjan Marion0247b462016-06-08 01:37:11 +0200148 }
149
Damjan Marion78fd7e82018-07-20 18:47:05 +0200150 n_enq = vlib_buffer_enqueue_to_thread (vm, hm->frame_queue_index, from,
151 thread_indices, frame->n_vectors, 1);
152
153 if (n_enq < frame->n_vectors)
154 vlib_node_increment_counter (vm, node->node_index,
155 WORKER_HANDOFF_ERROR_CONGESTION_DROP,
156 frame->n_vectors - n_enq);
Damjan Marion0247b462016-06-08 01:37:11 +0200157 return frame->n_vectors;
158}
159
Dave Barachba868bb2016-08-08 09:51:21 -0400160/* *INDENT-OFF* */
Damjan Marion0247b462016-06-08 01:37:11 +0200161VLIB_REGISTER_NODE (worker_handoff_node) = {
162 .function = worker_handoff_node_fn,
163 .name = "worker-handoff",
164 .vector_size = sizeof (u32),
165 .format_trace = format_worker_handoff_trace,
166 .type = VLIB_NODE_TYPE_INTERNAL,
Damjan Marion78fd7e82018-07-20 18:47:05 +0200167 .n_errors = ARRAY_LEN(worker_handoff_error_strings),
168 .error_strings = worker_handoff_error_strings,
Damjan Marion0247b462016-06-08 01:37:11 +0200169
170 .n_next_nodes = 1,
171 .next_nodes = {
Dave Barachba868bb2016-08-08 09:51:21 -0400172 [0] = "error-drop",
Damjan Marion0247b462016-06-08 01:37:11 +0200173 },
174};
175
176VLIB_NODE_FUNCTION_MULTIARCH (worker_handoff_node, worker_handoff_node_fn)
Damjan Marione33b9e02016-10-17 12:51:18 +0200177/* *INDENT-ON* */
178
179int
180interface_handoff_enable_disable (vlib_main_t * vm, u32 sw_if_index,
181 uword * bitmap, int enable_disable)
Damjan Marion0247b462016-06-08 01:37:11 +0200182{
Dave Barachba868bb2016-08-08 09:51:21 -0400183 handoff_main_t *hm = &handoff_main;
184 vnet_sw_interface_t *sw;
185 vnet_main_t *vnm = vnet_get_main ();
186 per_inteface_handoff_data_t *d;
Damjan Marion22311502016-10-28 20:30:15 +0200187 int i, rv = 0;
Damjan Marion0247b462016-06-08 01:37:11 +0200188
Dave Barachba868bb2016-08-08 09:51:21 -0400189 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
Damjan Marion0247b462016-06-08 01:37:11 +0200190 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
191
192 sw = vnet_get_sw_interface (vnm, sw_if_index);
193 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
194 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
195
Dave Barachba868bb2016-08-08 09:51:21 -0400196 if (clib_bitmap_last_set (bitmap) >= hm->num_workers)
Damjan Marion0247b462016-06-08 01:37:11 +0200197 return VNET_API_ERROR_INVALID_WORKER;
198
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100199 if (hm->frame_queue_index == ~0)
Damjan Marion4d56e052018-07-19 17:52:31 +0200200 {
201 vlib_node_t *n = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
202 hm->frame_queue_index = vlib_frame_queue_main_init (n->index, 0);
203 }
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100204
Damjan Marion0247b462016-06-08 01:37:11 +0200205 vec_validate (hm->if_data, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400206 d = vec_elt_at_index (hm->if_data, sw_if_index);
Damjan Marion0247b462016-06-08 01:37:11 +0200207
208 vec_free (d->workers);
209 vec_free (d->workers_bitmap);
210
211 if (enable_disable)
212 {
213 d->workers_bitmap = bitmap;
Dave Barachba868bb2016-08-08 09:51:21 -0400214 /* *INDENT-OFF* */
Damjan Marion0247b462016-06-08 01:37:11 +0200215 clib_bitmap_foreach (i, bitmap,
216 ({
217 vec_add1(d->workers, i);
218 }));
Dave Barachba868bb2016-08-08 09:51:21 -0400219 /* *INDENT-ON* */
Damjan Marion0247b462016-06-08 01:37:11 +0200220 }
221
Damjan Marion22311502016-10-28 20:30:15 +0200222 vnet_feature_enable_disable ("device-input", "worker-handoff",
223 sw_if_index, enable_disable, 0, 0);
Damjan Marion0247b462016-06-08 01:37:11 +0200224 return rv;
225}
226
227static clib_error_t *
228set_interface_handoff_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400229 unformat_input_t * input,
230 vlib_cli_command_t * cmd)
Damjan Marion0247b462016-06-08 01:37:11 +0200231{
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100232 handoff_main_t *hm = &handoff_main;
Damjan Marion0247b462016-06-08 01:37:11 +0200233 u32 sw_if_index = ~0;
234 int enable_disable = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400235 uword *bitmap = 0;
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100236 u32 sym = ~0;
Damjan Marion0247b462016-06-08 01:37:11 +0200237
238 int rv = 0;
239
Dave Barachba868bb2016-08-08 09:51:21 -0400240 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
241 {
242 if (unformat (input, "disable"))
243 enable_disable = 0;
244 else if (unformat (input, "workers %U", unformat_bitmap_list, &bitmap))
245 ;
246 else if (unformat (input, "%U", unformat_vnet_sw_interface,
247 vnet_get_main (), &sw_if_index))
248 ;
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100249 else if (unformat (input, "symmetrical"))
250 sym = 1;
251 else if (unformat (input, "asymmetrical"))
252 sym = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400253 else
254 break;
255 }
Damjan Marion0247b462016-06-08 01:37:11 +0200256
257 if (sw_if_index == ~0)
258 return clib_error_return (0, "Please specify an interface...");
259
260 if (bitmap == 0)
261 return clib_error_return (0, "Please specify list of workers...");
262
Dave Barachba868bb2016-08-08 09:51:21 -0400263 rv =
264 interface_handoff_enable_disable (vm, sw_if_index, bitmap,
265 enable_disable);
Damjan Marion0247b462016-06-08 01:37:11 +0200266
Dave Barachba868bb2016-08-08 09:51:21 -0400267 switch (rv)
268 {
Damjan Marion0247b462016-06-08 01:37:11 +0200269 case 0:
270 break;
271
272 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
273 return clib_error_return (0, "Invalid interface");
274 break;
275
276 case VNET_API_ERROR_INVALID_WORKER:
277 return clib_error_return (0, "Invalid worker(s)");
278 break;
279
280 case VNET_API_ERROR_UNIMPLEMENTED:
Dave Barachba868bb2016-08-08 09:51:21 -0400281 return clib_error_return (0,
282 "Device driver doesn't support redirection");
Damjan Marion0247b462016-06-08 01:37:11 +0200283 break;
284
285 default:
286 return clib_error_return (0, "unknown return value %d", rv);
Dave Barachba868bb2016-08-08 09:51:21 -0400287 }
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100288
289 if (sym == 1)
290 hm->hash_fn = eth_get_sym_key;
291 else if (sym == 0)
292 hm->hash_fn = eth_get_key;
293
Damjan Marion0247b462016-06-08 01:37:11 +0200294 return 0;
295}
296
Dave Barachba868bb2016-08-08 09:51:21 -0400297/* *INDENT-OFF* */
Damjan Marion0247b462016-06-08 01:37:11 +0200298VLIB_CLI_COMMAND (set_interface_handoff_command, static) = {
Dave Barachba868bb2016-08-08 09:51:21 -0400299 .path = "set interface handoff",
300 .short_help =
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100301 "set interface handoff <interface-name> workers <workers-list> [symmetrical|asymmetrical]",
Dave Barachba868bb2016-08-08 09:51:21 -0400302 .function = set_interface_handoff_command_fn,
Damjan Marion0247b462016-06-08 01:37:11 +0200303};
Dave Barachba868bb2016-08-08 09:51:21 -0400304/* *INDENT-ON* */
Damjan Marion0247b462016-06-08 01:37:11 +0200305
Damjan Marione33b9e02016-10-17 12:51:18 +0200306clib_error_t *
307handoff_init (vlib_main_t * vm)
Damjan Marion0247b462016-06-08 01:37:11 +0200308{
Dave Barachba868bb2016-08-08 09:51:21 -0400309 handoff_main_t *hm = &handoff_main;
310 vlib_thread_main_t *tm = vlib_get_thread_main ();
311 clib_error_t *error;
312 uword *p;
Damjan Marion0247b462016-06-08 01:37:11 +0200313
Damjan Marion0f8ecf02016-06-27 08:30:30 +0200314 if ((error = vlib_call_init_function (vm, threads_init)))
315 return error;
316
Dave Barachba868bb2016-08-08 09:51:21 -0400317 vlib_thread_registration_t *tr;
Damjan Marion0247b462016-06-08 01:37:11 +0200318 /* Only the standard vnet worker threads are supported */
319 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
Dave Barach5a9c9b82016-06-13 18:16:27 -0400320 if (p)
Damjan Marion0247b462016-06-08 01:37:11 +0200321 {
Dave Barach5a9c9b82016-06-13 18:16:27 -0400322 tr = (vlib_thread_registration_t *) p[0];
323 if (tr)
Dave Barachba868bb2016-08-08 09:51:21 -0400324 {
325 hm->num_workers = tr->count;
326 hm->first_worker_index = tr->first_index;
327 }
Damjan Marion0247b462016-06-08 01:37:11 +0200328 }
329
Gabriel Gannec0a36c02016-11-16 16:57:00 +0100330 hm->hash_fn = eth_get_key;
Damjan Marionaaef1eb2016-11-08 17:37:01 +0100331 hm->frame_queue_index = ~0;
Damjan Marion0f8ecf02016-06-27 08:30:30 +0200332
Damjan Marion0247b462016-06-08 01:37:11 +0200333 return 0;
334}
335
336VLIB_INIT_FUNCTION (handoff_init);
Dave Barachba868bb2016-08-08 09:51:21 -0400337
338/*
339 * fd.io coding-style-patch-verification: ON
340 *
341 * Local Variables:
342 * eval: (c-set-style "gnu")
343 * End:
344 */