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)
95 if ((rv = dev->ops.alloc (vm, dev)) != VNET_DEV_OK)
97 log_err (dev, "device init failed [rv %d]", rv);
99 dev->ops.deinit (vm, dev);
101 dev->ops.free (vm, dev);
105 if ((rv = dev->ops.init (vm, dev)) != VNET_DEV_OK)
107 log_err (dev, "device init failed [rv %d]", rv);
109 dev->ops.deinit (vm, dev);
111 dev->ops.free (vm, dev);
115 dev->initialized = 1;
116 dev->not_first_init = 1;
121 vnet_dev_deinit (vlib_main_t *vm, vnet_dev_t *dev)
123 ASSERT (dev->initialized == 1);
126 vnet_dev_validate (vm, dev);
128 foreach_vnet_dev_port (p, dev)
129 ASSERT (p->interface_created == 0);
132 dev->ops.deinit (vm, dev);
134 bus = vnet_dev_get_bus (dev);
135 if (bus->ops.device_close)
136 bus->ops.device_close (vm, dev);
138 vnet_dev_process_quit (vm, dev);
140 dev->initialized = 0;
144 vnet_dev_free (vlib_main_t *vm, vnet_dev_t *dev)
146 vnet_dev_main_t *dm = &vnet_dev_main;
148 vnet_dev_validate (vm, dev);
150 ASSERT (dev->initialized == 0);
152 foreach_vnet_dev_port (p, dev)
153 vnet_dev_port_free (vm, p);
155 vec_free (dev->description);
156 pool_free (dev->ports);
157 pool_free (dev->periodic_ops);
158 hash_unset (dm->device_index_by_id, dev->device_id);
159 pool_put_index (dm->devices, dev->index);
163 vnet_dev_reset (vlib_main_t *vm, vnet_dev_t *dev)
167 ASSERT (dev->initialized == 1);
168 vnet_dev_validate (vm, dev);
170 if (dev->ops.reset == 0)
171 return VNET_DEV_ERR_NOT_SUPPORTED;
173 if ((rv = dev->ops.reset (vm, dev)) != VNET_DEV_OK)
175 log_err (dev, "device reset failed [rv %d]", rv);
183 vnet_dev_detach (vlib_main_t *vm, vnet_dev_t *dev)
185 foreach_vnet_dev_port (p, dev)
186 if (p->interface_created)
187 vnet_dev_port_if_remove (vm, p);
188 vnet_dev_deinit (vm, dev);
189 vnet_dev_free (vm, dev);
193 vnet_dev_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size, u32 align,
196 vnet_dev_main_t *dm = &vnet_dev_main;
197 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
200 vnet_dev_validate (vm, dev);
202 if (!bus->ops.dma_mem_alloc_fn)
203 return VNET_DEV_ERR_NOT_SUPPORTED;
205 rv = bus->ops.dma_mem_alloc_fn (vm, dev, size, align, pp);
206 if (rv == VNET_DEV_OK)
207 log_debug (dev, "%u bytes va %p dma-addr 0x%lx numa %u align %u", size,
208 *pp, vnet_dev_get_dma_addr (vm, dev, *pp), dev->numa_node,
214 vnet_dev_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
216 vnet_dev_main_t *dm = &vnet_dev_main;
217 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
219 vnet_dev_validate (vm, dev);
221 if (p == 0 || !bus->ops.dma_mem_free_fn)
224 return bus->ops.dma_mem_free_fn (vm, dev, p);
228 vnet_dev_admin_up_down_fn (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
230 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
231 vlib_main_t *vm = vlib_get_main ();
232 vnet_dev_port_t *p = vnet_dev_get_port_from_dev_instance (hi->dev_instance);
233 vnet_dev_rv_t rv = VNET_DEV_OK;
234 u32 is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
236 if (is_up && p->started == 0)
237 rv = vnet_dev_process_call_port_op (vm, p, vnet_dev_port_start);
238 else if (!is_up && p->started)
239 rv = vnet_dev_process_call_port_op_no_rv (vm, p, vnet_dev_port_stop);
241 if (rv != VNET_DEV_OK)
242 return clib_error_return (0, "failed to change port admin state: %U",
243 format_vnet_dev_rv, rv);
249 vnet_dev_feature_update_cb (u32 sw_if_index, u8 arc_index, u8 is_enable,
252 vlib_main_t *vm = vlib_get_main ();
253 vnet_main_t *vnm = vnet_get_main ();
254 vnet_feature_main_t *fm = &feature_main;
255 vnet_feature_config_main_t *cm;
256 vnet_dev_main_t *vdm = &vnet_dev_main;
257 vnet_dev_port_t *port;
258 vnet_hw_interface_t *hw;
259 u32 current_config_index = ~0;
261 int update_runtime = 0;
263 if (arc_index != vdm->eth_port_rx_feature_arc_index)
266 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
267 port = vnet_dev_get_port_from_dev_instance (hw->dev_instance);
269 if (port == 0 || port->intf.sw_if_index != sw_if_index)
272 if (vnet_have_features (arc_index, sw_if_index))
274 cm = &fm->feature_config_mains[arc_index];
275 current_config_index =
276 vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
277 vnet_get_config_data (&cm->config_main, ¤t_config_index,
279 if (port->intf.feature_arc == 0 ||
280 port->intf.rx_next_index != next_index ||
281 port->intf.current_config_index != current_config_index)
283 port->intf.current_config_index = current_config_index;
284 port->intf.rx_next_index = next_index;
285 port->intf.feature_arc_index = arc_index;
286 port->intf.feature_arc = 1;
292 if (port->intf.feature_arc)
294 port->intf.current_config_index = 0;
295 port->intf.rx_next_index =
296 port->intf.redirect_to_node ?
297 port->intf.redirect_to_node_next_index :
298 vnet_dev_default_next_index_by_port_type[port->attr.type];
299 port->intf.feature_arc_index = 0;
300 port->intf.feature_arc = 0;
307 foreach_vnet_dev_port_rx_queue (rxq, port)
308 vnet_dev_rx_queue_rt_request (
310 (vnet_dev_rx_queue_rt_req_t){ .update_next_index = 1,
311 .update_feature_arc = 1 });
312 log_debug (port->dev, "runtime update requested due to chgange in "
313 "feature arc configuration");
318 sort_driver_registrations (void *a0, void *a1)
320 vnet_dev_driver_registration_t **r0 = a0;
321 vnet_dev_driver_registration_t **r1 = a1;
323 if (r0[0]->priority > r1[0]->priority)
325 else if (r0[0]->priority < r1[0]->priority)
331 static clib_error_t *
332 vnet_dev_main_init (vlib_main_t *vm)
334 vnet_dev_main_t *dm = &vnet_dev_main;
335 vnet_dev_driver_registration_t **drv = 0;
336 u32 temp_space_sz = 0;
338 dm->device_index_by_id = hash_create_string (0, sizeof (uword));
340 for (vnet_dev_bus_registration_t *r = dm->bus_registrations; r;
341 r = r->next_registration)
344 pool_get_zero (dm->buses, bus);
345 bus->registration = r;
346 bus->index = bus - dm->buses;
348 if (!r->device_data_size ||
349 r->device_data_size > STRUCT_SIZE_OF (vnet_dev_t, bus_data))
350 return clib_error_return (
351 0, "bus device data for bus '%s' is too big not specified", r->name);
353 log_debug (0, "bus '%s' registered", r->name);
356 for (vnet_dev_driver_registration_t *r = dm->driver_registrations; r;
357 r = r->next_registration)
360 vec_sort_with_function (drv, sort_driver_registrations);
362 vec_foreach_pointer (r, drv)
364 vnet_dev_driver_t *driver;
366 vnet_device_class_t *dev_class;
369 pool_foreach (bus, dm->buses)
371 if (strcmp (bus->registration->name, r->bus) == 0)
373 bus_index = bus->index;
379 return clib_error_return (0, "unknown bus '%s'", r->bus);
381 pool_get_zero (dm->drivers, driver);
382 driver->registration = r;
383 driver->index = driver - dm->drivers;
384 driver->bus_index = bus_index;
385 driver->ops = r->ops;
386 dev_class = clib_mem_alloc (sizeof (vnet_device_class_t));
387 *dev_class = (vnet_device_class_t){
389 .format_device_name = format_vnet_dev_interface_name,
390 .format_device = format_vnet_dev_interface_info,
391 .admin_up_down_function = vnet_dev_admin_up_down_fn,
392 .rx_redirect_to_node = vnet_dev_set_interface_next_node,
393 .clear_counters = vnet_dev_clear_hw_interface_counters,
394 .mac_addr_change_function = vnet_dev_port_mac_change,
395 .mac_addr_add_del_function = vnet_dev_add_del_mac_address,
396 .flow_ops_function = vnet_dev_flow_ops_fn,
397 .set_rss_queues_function = vnet_dev_interface_set_rss_queues,
399 driver->dev_class_index = vnet_register_device_class (vm, dev_class);
400 log_debug (0, "driver '%s' registered on bus '%s'", r->name,
401 bus->registration->name);
403 if (temp_space_sz < r->runtime_temp_space_sz)
404 temp_space_sz = r->runtime_temp_space_sz;
407 if (dm->startup_config)
408 log_debug (0, "startup config: %v", dm->startup_config);
412 if (temp_space_sz > 0)
414 const u32 align = CLIB_CACHE_LINE_BYTES;
415 u32 sz = round_pow2 (temp_space_sz, align);
416 dm->log2_runtime_temp_space_sz =
417 get_lowest_set_bit_index (max_pow2 (sz));
418 sz = 1 << dm->log2_runtime_temp_space_sz;
419 sz *= vlib_get_n_threads ();
420 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
421 clib_memset (dm->runtime_temp_spaces, 0, sz);
423 "requested %u bytes for runtime temp storage, allocated %u "
424 "per thread (total %u)",
425 temp_space_sz, 1 << dm->log2_runtime_temp_space_sz, sz);
428 vnet_feature_register (vnet_dev_feature_update_cb, 0);
433 VLIB_INIT_FUNCTION (vnet_dev_main_init);
436 vnet_dev_num_workers_change (vlib_main_t *vm)
438 vnet_dev_main_t *dm = &vnet_dev_main;
440 if (dm->log2_runtime_temp_space_sz > 0)
442 const u32 align = CLIB_CACHE_LINE_BYTES;
444 (1ULL << dm->log2_runtime_temp_space_sz) * vlib_get_n_threads ();
445 if (dm->runtime_temp_spaces)
446 clib_mem_free (dm->runtime_temp_spaces);
447 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
448 clib_memset (dm->runtime_temp_spaces, 0, sz);
449 log_debug (0, "runtime temp storage resized to %u", sz);
455 VLIB_NUM_WORKERS_CHANGE_FN (vnet_dev_num_workers_change);