blob: e04fa161ce230a604f99a1525b08c885166234cf [file] [log] [blame]
Damjan Marion38c61912023-10-17 16:06:26 +00001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
3 */
4
5#include "vppinfra/pool.h"
6#include <vnet/vnet.h>
7#include <vnet/ethernet/ethernet.h>
8#include <vnet/dev/dev.h>
9#include <vnet/dev/log.h>
10#include <vnet/dev/counters.h>
11
12VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
13 .class_name = "dev",
14};
15
16vnet_dev_main_t vnet_dev_main = { .next_rx_queue_thread = 1 };
17
18vnet_dev_bus_t *
19vnet_dev_find_device_bus (vlib_main_t *vm, vnet_dev_device_id_t id)
20{
21 vnet_dev_main_t *dm = &vnet_dev_main;
22 vnet_dev_bus_t *bus;
23
24 pool_foreach (bus, dm->buses)
25 {
26 int n = strlen (bus->registration->name);
27 int l = strlen (id);
28 int dl = strlen (VNET_DEV_DEVICE_ID_PREFIX_DELIMITER);
29
30 if (l <= n + dl)
31 continue;
32
33 if (strncmp (bus->registration->name, id, n))
34 continue;
35
36 if (strncmp (VNET_DEV_DEVICE_ID_PREFIX_DELIMITER, id + n, dl))
37 continue;
38
39 return bus;
40 }
41
42 return 0;
43}
44
45void *
46vnet_dev_get_device_info (vlib_main_t *vm, vnet_dev_device_id_t id)
47{
48 vnet_dev_bus_t *bus;
49
50 bus = vnet_dev_find_device_bus (vm, id);
51 if (bus == 0)
52 return 0;
53
54 return bus->ops.get_device_info (vm, id);
55}
56
57vnet_dev_t *
58vnet_dev_alloc (vlib_main_t *vm, vnet_dev_device_id_t id,
59 vnet_dev_driver_t *driver)
60{
61 vnet_dev_main_t *dm = &vnet_dev_main;
62 vnet_dev_t *dev = 0, **devp = 0;
63
64 dev = vnet_dev_alloc_with_data (sizeof (vnet_dev_t),
65 driver->registration->device_data_sz);
66
67 pool_get (dm->devices, devp);
68 devp[0] = dev;
69 dev->index = devp - dm->devices;
70 dev->driver_index = driver->index;
71 dev->ops = driver->registration->ops;
72 dev->bus_index = driver->bus_index;
73 clib_memcpy (dev->device_id, id, sizeof (dev->device_id));
74 hash_set (dm->device_index_by_id, dev->device_id, dev->index);
75
76 if ((vnet_dev_process_create (vm, dev)) == VNET_DEV_OK)
77 return dev;
78
79 vnet_dev_free (vm, dev);
80 return 0;
81}
82
83vnet_dev_rv_t
84vnet_dev_init (vlib_main_t *vm, vnet_dev_t *dev)
85{
86 vnet_dev_main_t *dm = &vnet_dev_main;
87 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
88 vnet_dev_rv_t rv;
89
90 vnet_dev_validate (vm, dev);
91
92 if ((rv = bus->ops.device_open (vm, dev)) != VNET_DEV_OK)
93 return rv;
94
Damjan Marione596ca12023-11-08 19:12:27 +000095 if (dev->ops.alloc)
Damjan Marion38c61912023-10-17 16:06:26 +000096 {
Damjan Marione596ca12023-11-08 19:12:27 +000097 rv = dev->ops.alloc (vm, dev);
98 if (rv != VNET_DEV_OK)
99 {
100 log_err (dev, "device init failed [rv %d]", rv);
101 if (dev->ops.deinit)
102 dev->ops.deinit (vm, dev);
103 if (dev->ops.free)
104 dev->ops.free (vm, dev);
105 return rv;
106 }
Damjan Marion38c61912023-10-17 16:06:26 +0000107 }
108
109 if ((rv = dev->ops.init (vm, dev)) != VNET_DEV_OK)
110 {
111 log_err (dev, "device init failed [rv %d]", rv);
112 if (dev->ops.deinit)
113 dev->ops.deinit (vm, dev);
114 if (dev->ops.free)
115 dev->ops.free (vm, dev);
116 return rv;
117 }
118
119 dev->initialized = 1;
120 dev->not_first_init = 1;
121 return VNET_DEV_OK;
122}
123
124void
125vnet_dev_deinit (vlib_main_t *vm, vnet_dev_t *dev)
126{
127 ASSERT (dev->initialized == 1);
128 vnet_dev_bus_t *bus;
129
130 vnet_dev_validate (vm, dev);
131
132 foreach_vnet_dev_port (p, dev)
133 ASSERT (p->interface_created == 0);
134
135 if (dev->ops.deinit)
136 dev->ops.deinit (vm, dev);
137
138 bus = vnet_dev_get_bus (dev);
139 if (bus->ops.device_close)
140 bus->ops.device_close (vm, dev);
141
142 vnet_dev_process_quit (vm, dev);
143
144 dev->initialized = 0;
145}
146
147void
148vnet_dev_free (vlib_main_t *vm, vnet_dev_t *dev)
149{
150 vnet_dev_main_t *dm = &vnet_dev_main;
151
152 vnet_dev_validate (vm, dev);
153
154 ASSERT (dev->initialized == 0);
155
156 foreach_vnet_dev_port (p, dev)
157 vnet_dev_port_free (vm, p);
158
159 vec_free (dev->description);
160 pool_free (dev->ports);
161 pool_free (dev->periodic_ops);
162 hash_unset (dm->device_index_by_id, dev->device_id);
Damjan Marion69768d92023-11-13 17:33:32 +0000163 vnet_dev_arg_free (&dev->args);
Damjan Marion38c61912023-10-17 16:06:26 +0000164 pool_put_index (dm->devices, dev->index);
165}
166
167vnet_dev_rv_t
168vnet_dev_reset (vlib_main_t *vm, vnet_dev_t *dev)
169{
170 vnet_dev_rv_t rv;
171
172 ASSERT (dev->initialized == 1);
173 vnet_dev_validate (vm, dev);
174
175 if (dev->ops.reset == 0)
176 return VNET_DEV_ERR_NOT_SUPPORTED;
177
178 if ((rv = dev->ops.reset (vm, dev)) != VNET_DEV_OK)
179 {
180 log_err (dev, "device reset failed [rv %d]", rv);
181 return rv;
182 }
183
184 return VNET_DEV_OK;
185}
186
187void
188vnet_dev_detach (vlib_main_t *vm, vnet_dev_t *dev)
189{
190 foreach_vnet_dev_port (p, dev)
191 if (p->interface_created)
192 vnet_dev_port_if_remove (vm, p);
193 vnet_dev_deinit (vm, dev);
194 vnet_dev_free (vm, dev);
195}
196
197vnet_dev_rv_t
198vnet_dev_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size, u32 align,
199 void **pp)
200{
201 vnet_dev_main_t *dm = &vnet_dev_main;
202 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
203 vnet_dev_rv_t rv;
204
205 vnet_dev_validate (vm, dev);
206
207 if (!bus->ops.dma_mem_alloc_fn)
208 return VNET_DEV_ERR_NOT_SUPPORTED;
209
210 rv = bus->ops.dma_mem_alloc_fn (vm, dev, size, align, pp);
211 if (rv == VNET_DEV_OK)
212 log_debug (dev, "%u bytes va %p dma-addr 0x%lx numa %u align %u", size,
213 *pp, vnet_dev_get_dma_addr (vm, dev, *pp), dev->numa_node,
214 align);
215 return rv;
216}
217
218void
219vnet_dev_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
220{
221 vnet_dev_main_t *dm = &vnet_dev_main;
222 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
223
224 vnet_dev_validate (vm, dev);
225
226 if (p == 0 || !bus->ops.dma_mem_free_fn)
227 return;
228
229 return bus->ops.dma_mem_free_fn (vm, dev, p);
230}
231
232clib_error_t *
233vnet_dev_admin_up_down_fn (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
234{
235 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
236 vlib_main_t *vm = vlib_get_main ();
237 vnet_dev_port_t *p = vnet_dev_get_port_from_dev_instance (hi->dev_instance);
238 vnet_dev_rv_t rv = VNET_DEV_OK;
239 u32 is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
240
241 if (is_up && p->started == 0)
242 rv = vnet_dev_process_call_port_op (vm, p, vnet_dev_port_start);
243 else if (!is_up && p->started)
244 rv = vnet_dev_process_call_port_op_no_rv (vm, p, vnet_dev_port_stop);
245
246 if (rv != VNET_DEV_OK)
247 return clib_error_return (0, "failed to change port admin state: %U",
248 format_vnet_dev_rv, rv);
249
250 return 0;
251}
252
253static void
254vnet_dev_feature_update_cb (u32 sw_if_index, u8 arc_index, u8 is_enable,
255 void *cb)
256{
257 vlib_main_t *vm = vlib_get_main ();
258 vnet_main_t *vnm = vnet_get_main ();
259 vnet_feature_main_t *fm = &feature_main;
260 vnet_feature_config_main_t *cm;
261 vnet_dev_main_t *vdm = &vnet_dev_main;
262 vnet_dev_port_t *port;
263 vnet_hw_interface_t *hw;
264 u32 current_config_index = ~0;
265 u32 next_index = ~0;
266 int update_runtime = 0;
267
268 if (arc_index != vdm->eth_port_rx_feature_arc_index)
269 return;
270
271 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
272 port = vnet_dev_get_port_from_dev_instance (hw->dev_instance);
273
274 if (port == 0 || port->intf.sw_if_index != sw_if_index)
275 return;
276
277 if (vnet_have_features (arc_index, sw_if_index))
278 {
279 cm = &fm->feature_config_mains[arc_index];
280 current_config_index =
281 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
282 vnet_get_config_data (&cm->config_main, &current_config_index,
283 &next_index, 0);
284 if (port->intf.feature_arc == 0 ||
285 port->intf.rx_next_index != next_index ||
286 port->intf.current_config_index != current_config_index)
287 {
288 port->intf.current_config_index = current_config_index;
289 port->intf.rx_next_index = next_index;
290 port->intf.feature_arc_index = arc_index;
291 port->intf.feature_arc = 1;
292 update_runtime = 1;
293 }
294 }
295 else
296 {
297 if (port->intf.feature_arc)
298 {
299 port->intf.current_config_index = 0;
300 port->intf.rx_next_index =
301 port->intf.redirect_to_node ?
302 port->intf.redirect_to_node_next_index :
303 vnet_dev_default_next_index_by_port_type[port->attr.type];
304 port->intf.feature_arc_index = 0;
305 port->intf.feature_arc = 0;
306 update_runtime = 1;
307 }
308 }
309
310 if (update_runtime)
311 {
312 foreach_vnet_dev_port_rx_queue (rxq, port)
313 vnet_dev_rx_queue_rt_request (
314 vm, rxq,
315 (vnet_dev_rx_queue_rt_req_t){ .update_next_index = 1,
316 .update_feature_arc = 1 });
317 log_debug (port->dev, "runtime update requested due to chgange in "
318 "feature arc configuration");
319 }
320}
321
322static int
323sort_driver_registrations (void *a0, void *a1)
324{
325 vnet_dev_driver_registration_t **r0 = a0;
326 vnet_dev_driver_registration_t **r1 = a1;
327
328 if (r0[0]->priority > r1[0]->priority)
329 return -1;
330 else if (r0[0]->priority < r1[0]->priority)
331 return 1;
332
333 return 0;
334}
335
336static clib_error_t *
337vnet_dev_main_init (vlib_main_t *vm)
338{
339 vnet_dev_main_t *dm = &vnet_dev_main;
340 vnet_dev_driver_registration_t **drv = 0;
341 u32 temp_space_sz = 0;
342
343 dm->device_index_by_id = hash_create_string (0, sizeof (uword));
344
345 for (vnet_dev_bus_registration_t *r = dm->bus_registrations; r;
346 r = r->next_registration)
347 {
348 vnet_dev_bus_t *bus;
349 pool_get_zero (dm->buses, bus);
350 bus->registration = r;
351 bus->index = bus - dm->buses;
352 bus->ops = r->ops;
353 if (!r->device_data_size ||
354 r->device_data_size > STRUCT_SIZE_OF (vnet_dev_t, bus_data))
355 return clib_error_return (
356 0, "bus device data for bus '%s' is too big not specified", r->name);
357
358 log_debug (0, "bus '%s' registered", r->name);
359 }
360
361 for (vnet_dev_driver_registration_t *r = dm->driver_registrations; r;
362 r = r->next_registration)
363 vec_add1 (drv, r);
364
365 vec_sort_with_function (drv, sort_driver_registrations);
366
367 vec_foreach_pointer (r, drv)
368 {
369 vnet_dev_driver_t *driver;
370 vnet_dev_bus_t *bus;
371 vnet_device_class_t *dev_class;
372 int bus_index = -1;
373
374 pool_foreach (bus, dm->buses)
375 {
376 if (strcmp (bus->registration->name, r->bus) == 0)
377 {
378 bus_index = bus->index;
379 break;
380 }
381 }
382
383 if (bus_index < 0)
384 return clib_error_return (0, "unknown bus '%s'", r->bus);
385
386 pool_get_zero (dm->drivers, driver);
387 driver->registration = r;
388 driver->index = driver - dm->drivers;
389 driver->bus_index = bus_index;
390 driver->ops = r->ops;
391 dev_class = clib_mem_alloc (sizeof (vnet_device_class_t));
392 *dev_class = (vnet_device_class_t){
393 .name = r->name,
394 .format_device_name = format_vnet_dev_interface_name,
395 .format_device = format_vnet_dev_interface_info,
396 .admin_up_down_function = vnet_dev_admin_up_down_fn,
397 .rx_redirect_to_node = vnet_dev_set_interface_next_node,
398 .clear_counters = vnet_dev_clear_hw_interface_counters,
Damjan Marion38c61912023-10-17 16:06:26 +0000399 .mac_addr_change_function = vnet_dev_port_mac_change,
400 .mac_addr_add_del_function = vnet_dev_add_del_mac_address,
401 .flow_ops_function = vnet_dev_flow_ops_fn,
Monendra Singh Kushwaha4af3fdf2024-02-06 14:02:43 +0530402 .format_flow = format_vnet_dev_flow,
Damjan Marion38c61912023-10-17 16:06:26 +0000403 .set_rss_queues_function = vnet_dev_interface_set_rss_queues,
404 };
405 driver->dev_class_index = vnet_register_device_class (vm, dev_class);
406 log_debug (0, "driver '%s' registered on bus '%s'", r->name,
407 bus->registration->name);
408
409 if (temp_space_sz < r->runtime_temp_space_sz)
410 temp_space_sz = r->runtime_temp_space_sz;
411 }
412
413 if (dm->startup_config)
414 log_debug (0, "startup config: %v", dm->startup_config);
415
416 vec_free (drv);
417
418 if (temp_space_sz > 0)
419 {
420 const u32 align = CLIB_CACHE_LINE_BYTES;
421 u32 sz = round_pow2 (temp_space_sz, align);
422 dm->log2_runtime_temp_space_sz =
423 get_lowest_set_bit_index (max_pow2 (sz));
424 sz = 1 << dm->log2_runtime_temp_space_sz;
425 sz *= vlib_get_n_threads ();
426 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
427 clib_memset (dm->runtime_temp_spaces, 0, sz);
428 log_debug (0,
429 "requested %u bytes for runtime temp storage, allocated %u "
430 "per thread (total %u)",
431 temp_space_sz, 1 << dm->log2_runtime_temp_space_sz, sz);
432 }
433
434 vnet_feature_register (vnet_dev_feature_update_cb, 0);
435
436 return 0;
437}
438
439VLIB_INIT_FUNCTION (vnet_dev_main_init);
440
441clib_error_t *
442vnet_dev_num_workers_change (vlib_main_t *vm)
443{
444 vnet_dev_main_t *dm = &vnet_dev_main;
445
446 if (dm->log2_runtime_temp_space_sz > 0)
447 {
448 const u32 align = CLIB_CACHE_LINE_BYTES;
449 uword sz =
450 (1ULL << dm->log2_runtime_temp_space_sz) * vlib_get_n_threads ();
451 if (dm->runtime_temp_spaces)
452 clib_mem_free (dm->runtime_temp_spaces);
453 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
454 clib_memset (dm->runtime_temp_spaces, 0, sz);
455 log_debug (0, "runtime temp storage resized to %u", sz);
456 }
457
458 return 0;
459}
460
461VLIB_NUM_WORKERS_CHANGE_FN (vnet_dev_num_workers_change);