1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
5 #include "vppinfra/pool.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>
12 VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
16 vnet_dev_main_t vnet_dev_main = { .next_rx_queue_thread = 1 };
19 vnet_dev_find_device_bus (vlib_main_t *vm, vnet_dev_device_id_t id)
21 vnet_dev_main_t *dm = &vnet_dev_main;
24 pool_foreach (bus, dm->buses)
26 int n = strlen (bus->registration->name);
28 int dl = strlen (VNET_DEV_DEVICE_ID_PREFIX_DELIMITER);
33 if (strncmp (bus->registration->name, id, n))
36 if (strncmp (VNET_DEV_DEVICE_ID_PREFIX_DELIMITER, id + n, dl))
46 vnet_dev_get_device_info (vlib_main_t *vm, vnet_dev_device_id_t id)
50 bus = vnet_dev_find_device_bus (vm, id);
54 return bus->ops.get_device_info (vm, id);
58 vnet_dev_alloc (vlib_main_t *vm, vnet_dev_device_id_t id,
59 vnet_dev_driver_t *driver)
61 vnet_dev_main_t *dm = &vnet_dev_main;
62 vnet_dev_t *dev = 0, **devp = 0;
64 dev = vnet_dev_alloc_with_data (sizeof (vnet_dev_t),
65 driver->registration->device_data_sz);
67 pool_get (dm->devices, devp);
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);
76 if ((vnet_dev_process_create (vm, dev)) == VNET_DEV_OK)
79 vnet_dev_free (vm, dev);
84 vnet_dev_init (vlib_main_t *vm, vnet_dev_t *dev)
86 vnet_dev_main_t *dm = &vnet_dev_main;
87 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
90 vnet_dev_validate (vm, dev);
92 if ((rv = bus->ops.device_open (vm, dev)) != VNET_DEV_OK)
97 rv = dev->ops.alloc (vm, dev);
98 if (rv != VNET_DEV_OK)
100 log_err (dev, "device init failed [rv %d]", rv);
102 dev->ops.deinit (vm, dev);
104 dev->ops.free (vm, dev);
109 if ((rv = dev->ops.init (vm, dev)) != VNET_DEV_OK)
111 log_err (dev, "device init failed [rv %d]", rv);
113 dev->ops.deinit (vm, dev);
115 dev->ops.free (vm, dev);
119 dev->initialized = 1;
120 dev->not_first_init = 1;
125 vnet_dev_deinit (vlib_main_t *vm, vnet_dev_t *dev)
127 ASSERT (dev->initialized == 1);
130 vnet_dev_validate (vm, dev);
132 foreach_vnet_dev_port (p, dev)
133 ASSERT (p->interface_created == 0);
136 dev->ops.deinit (vm, dev);
138 bus = vnet_dev_get_bus (dev);
139 if (bus->ops.device_close)
140 bus->ops.device_close (vm, dev);
142 vnet_dev_process_quit (vm, dev);
144 dev->initialized = 0;
148 vnet_dev_free (vlib_main_t *vm, vnet_dev_t *dev)
150 vnet_dev_main_t *dm = &vnet_dev_main;
152 vnet_dev_validate (vm, dev);
154 ASSERT (dev->initialized == 0);
156 foreach_vnet_dev_port (p, dev)
157 vnet_dev_port_free (vm, p);
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);
163 pool_put_index (dm->devices, dev->index);
167 vnet_dev_reset (vlib_main_t *vm, vnet_dev_t *dev)
171 ASSERT (dev->initialized == 1);
172 vnet_dev_validate (vm, dev);
174 if (dev->ops.reset == 0)
175 return VNET_DEV_ERR_NOT_SUPPORTED;
177 if ((rv = dev->ops.reset (vm, dev)) != VNET_DEV_OK)
179 log_err (dev, "device reset failed [rv %d]", rv);
187 vnet_dev_detach (vlib_main_t *vm, vnet_dev_t *dev)
189 foreach_vnet_dev_port (p, dev)
190 if (p->interface_created)
191 vnet_dev_port_if_remove (vm, p);
192 vnet_dev_deinit (vm, dev);
193 vnet_dev_free (vm, dev);
197 vnet_dev_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size, u32 align,
200 vnet_dev_main_t *dm = &vnet_dev_main;
201 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
204 vnet_dev_validate (vm, dev);
206 if (!bus->ops.dma_mem_alloc_fn)
207 return VNET_DEV_ERR_NOT_SUPPORTED;
209 rv = bus->ops.dma_mem_alloc_fn (vm, dev, size, align, pp);
210 if (rv == VNET_DEV_OK)
211 log_debug (dev, "%u bytes va %p dma-addr 0x%lx numa %u align %u", size,
212 *pp, vnet_dev_get_dma_addr (vm, dev, *pp), dev->numa_node,
218 vnet_dev_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
220 vnet_dev_main_t *dm = &vnet_dev_main;
221 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
223 vnet_dev_validate (vm, dev);
225 if (p == 0 || !bus->ops.dma_mem_free_fn)
228 return bus->ops.dma_mem_free_fn (vm, dev, p);
232 vnet_dev_admin_up_down_fn (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
234 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
235 vlib_main_t *vm = vlib_get_main ();
236 vnet_dev_port_t *p = vnet_dev_get_port_from_dev_instance (hi->dev_instance);
237 vnet_dev_rv_t rv = VNET_DEV_OK;
238 u32 is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
240 if (is_up && p->started == 0)
241 rv = vnet_dev_process_call_port_op (vm, p, vnet_dev_port_start);
242 else if (!is_up && p->started)
243 rv = vnet_dev_process_call_port_op_no_rv (vm, p, vnet_dev_port_stop);
245 if (rv != VNET_DEV_OK)
246 return clib_error_return (0, "failed to change port admin state: %U",
247 format_vnet_dev_rv, rv);
253 vnet_dev_feature_update_cb (u32 sw_if_index, u8 arc_index, u8 is_enable,
256 vlib_main_t *vm = vlib_get_main ();
257 vnet_main_t *vnm = vnet_get_main ();
258 vnet_feature_main_t *fm = &feature_main;
259 vnet_feature_config_main_t *cm;
260 vnet_dev_main_t *vdm = &vnet_dev_main;
261 vnet_dev_port_t *port;
262 vnet_hw_interface_t *hw;
263 u32 current_config_index = ~0;
265 int update_runtime = 0;
267 if (arc_index != vdm->eth_port_rx_feature_arc_index)
270 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
271 port = vnet_dev_get_port_from_dev_instance (hw->dev_instance);
273 if (port == 0 || port->intf.sw_if_index != sw_if_index)
276 if (vnet_have_features (arc_index, sw_if_index))
278 cm = &fm->feature_config_mains[arc_index];
279 current_config_index =
280 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
281 vnet_get_config_data (&cm->config_main, ¤t_config_index,
283 if (port->intf.feature_arc == 0 ||
284 port->intf.rx_next_index != next_index ||
285 port->intf.current_config_index != current_config_index)
287 port->intf.current_config_index = current_config_index;
288 port->intf.rx_next_index = next_index;
289 port->intf.feature_arc_index = arc_index;
290 port->intf.feature_arc = 1;
296 if (port->intf.feature_arc)
298 port->intf.current_config_index = 0;
299 port->intf.rx_next_index =
300 port->intf.redirect_to_node ?
301 port->intf.redirect_to_node_next_index :
302 vnet_dev_default_next_index_by_port_type[port->attr.type];
303 port->intf.feature_arc_index = 0;
304 port->intf.feature_arc = 0;
311 foreach_vnet_dev_port_rx_queue (rxq, port)
312 vnet_dev_rx_queue_rt_request (
314 (vnet_dev_rx_queue_rt_req_t){ .update_next_index = 1,
315 .update_feature_arc = 1 });
316 log_debug (port->dev, "runtime update requested due to chgange in "
317 "feature arc configuration");
322 sort_driver_registrations (void *a0, void *a1)
324 vnet_dev_driver_registration_t **r0 = a0;
325 vnet_dev_driver_registration_t **r1 = a1;
327 if (r0[0]->priority > r1[0]->priority)
329 else if (r0[0]->priority < r1[0]->priority)
335 static clib_error_t *
336 vnet_dev_main_init (vlib_main_t *vm)
338 vnet_dev_main_t *dm = &vnet_dev_main;
339 vnet_dev_driver_registration_t **drv = 0;
340 u32 temp_space_sz = 0;
342 dm->device_index_by_id = hash_create_string (0, sizeof (uword));
344 for (vnet_dev_bus_registration_t *r = dm->bus_registrations; r;
345 r = r->next_registration)
348 pool_get_zero (dm->buses, bus);
349 bus->registration = r;
350 bus->index = bus - dm->buses;
352 if (!r->device_data_size ||
353 r->device_data_size > STRUCT_SIZE_OF (vnet_dev_t, bus_data))
354 return clib_error_return (
355 0, "bus device data for bus '%s' is too big not specified", r->name);
357 log_debug (0, "bus '%s' registered", r->name);
360 for (vnet_dev_driver_registration_t *r = dm->driver_registrations; r;
361 r = r->next_registration)
364 vec_sort_with_function (drv, sort_driver_registrations);
366 vec_foreach_pointer (r, drv)
368 vnet_dev_driver_t *driver;
370 vnet_device_class_t *dev_class;
373 pool_foreach (bus, dm->buses)
375 if (strcmp (bus->registration->name, r->bus) == 0)
377 bus_index = bus->index;
383 return clib_error_return (0, "unknown bus '%s'", r->bus);
385 pool_get_zero (dm->drivers, driver);
386 driver->registration = r;
387 driver->index = driver - dm->drivers;
388 driver->bus_index = bus_index;
389 driver->ops = r->ops;
390 dev_class = clib_mem_alloc (sizeof (vnet_device_class_t));
391 *dev_class = (vnet_device_class_t){
393 .format_device_name = format_vnet_dev_interface_name,
394 .format_device = format_vnet_dev_interface_info,
395 .admin_up_down_function = vnet_dev_admin_up_down_fn,
396 .rx_redirect_to_node = vnet_dev_set_interface_next_node,
397 .clear_counters = vnet_dev_clear_hw_interface_counters,
398 .mac_addr_change_function = vnet_dev_port_mac_change,
399 .mac_addr_add_del_function = vnet_dev_add_del_mac_address,
400 .flow_ops_function = vnet_dev_flow_ops_fn,
401 .set_rss_queues_function = vnet_dev_interface_set_rss_queues,
403 driver->dev_class_index = vnet_register_device_class (vm, dev_class);
404 log_debug (0, "driver '%s' registered on bus '%s'", r->name,
405 bus->registration->name);
407 if (temp_space_sz < r->runtime_temp_space_sz)
408 temp_space_sz = r->runtime_temp_space_sz;
411 if (dm->startup_config)
412 log_debug (0, "startup config: %v", dm->startup_config);
416 if (temp_space_sz > 0)
418 const u32 align = CLIB_CACHE_LINE_BYTES;
419 u32 sz = round_pow2 (temp_space_sz, align);
420 dm->log2_runtime_temp_space_sz =
421 get_lowest_set_bit_index (max_pow2 (sz));
422 sz = 1 << dm->log2_runtime_temp_space_sz;
423 sz *= vlib_get_n_threads ();
424 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
425 clib_memset (dm->runtime_temp_spaces, 0, sz);
427 "requested %u bytes for runtime temp storage, allocated %u "
428 "per thread (total %u)",
429 temp_space_sz, 1 << dm->log2_runtime_temp_space_sz, sz);
432 vnet_feature_register (vnet_dev_feature_update_cb, 0);
437 VLIB_INIT_FUNCTION (vnet_dev_main_init);
440 vnet_dev_num_workers_change (vlib_main_t *vm)
442 vnet_dev_main_t *dm = &vnet_dev_main;
444 if (dm->log2_runtime_temp_space_sz > 0)
446 const u32 align = CLIB_CACHE_LINE_BYTES;
448 (1ULL << dm->log2_runtime_temp_space_sz) * vlib_get_n_threads ();
449 if (dm->runtime_temp_spaces)
450 clib_mem_free (dm->runtime_temp_spaces);
451 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
452 clib_memset (dm->runtime_temp_spaces, 0, sz);
453 log_debug (0, "runtime temp storage resized to %u", sz);
459 VLIB_NUM_WORKERS_CHANGE_FN (vnet_dev_num_workers_change);