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 vnet_dev_arg_free (&dev->args);
164 pool_put_index (dm->devices, dev->index);
168 vnet_dev_reset (vlib_main_t *vm, vnet_dev_t *dev)
172 ASSERT (dev->initialized == 1);
173 vnet_dev_validate (vm, dev);
175 if (dev->ops.reset == 0)
176 return VNET_DEV_ERR_NOT_SUPPORTED;
178 if ((rv = dev->ops.reset (vm, dev)) != VNET_DEV_OK)
180 log_err (dev, "device reset failed [rv %d]", rv);
188 vnet_dev_detach (vlib_main_t *vm, vnet_dev_t *dev)
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);
198 vnet_dev_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size, u32 align,
201 vnet_dev_main_t *dm = &vnet_dev_main;
202 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
205 vnet_dev_validate (vm, dev);
207 if (!bus->ops.dma_mem_alloc_fn)
208 return VNET_DEV_ERR_NOT_SUPPORTED;
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,
219 vnet_dev_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
221 vnet_dev_main_t *dm = &vnet_dev_main;
222 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
224 vnet_dev_validate (vm, dev);
226 if (p == 0 || !bus->ops.dma_mem_free_fn)
229 return bus->ops.dma_mem_free_fn (vm, dev, p);
233 vnet_dev_admin_up_down_fn (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
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;
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);
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);
254 vnet_dev_feature_update_cb (u32 sw_if_index, u8 arc_index, u8 is_enable,
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;
266 int update_runtime = 0;
268 if (arc_index != vdm->eth_port_rx_feature_arc_index)
271 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
272 port = vnet_dev_get_port_from_dev_instance (hw->dev_instance);
274 if (port == 0 || port->intf.sw_if_index != sw_if_index)
277 if (vnet_have_features (arc_index, sw_if_index))
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, ¤t_config_index,
284 if (port->intf.feature_arc == 0 ||
285 port->intf.rx_next_index != next_index ||
286 port->intf.current_config_index != current_config_index)
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;
297 if (port->intf.feature_arc)
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;
312 foreach_vnet_dev_port_rx_queue (rxq, port)
313 vnet_dev_rx_queue_rt_request (
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");
323 sort_driver_registrations (void *a0, void *a1)
325 vnet_dev_driver_registration_t **r0 = a0;
326 vnet_dev_driver_registration_t **r1 = a1;
328 if (r0[0]->priority > r1[0]->priority)
330 else if (r0[0]->priority < r1[0]->priority)
336 static clib_error_t *
337 vnet_dev_main_init (vlib_main_t *vm)
339 vnet_dev_main_t *dm = &vnet_dev_main;
340 vnet_dev_driver_registration_t **drv = 0;
341 u32 temp_space_sz = 0;
343 dm->device_index_by_id = hash_create_string (0, sizeof (uword));
345 for (vnet_dev_bus_registration_t *r = dm->bus_registrations; r;
346 r = r->next_registration)
349 pool_get_zero (dm->buses, bus);
350 bus->registration = r;
351 bus->index = bus - dm->buses;
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);
358 log_debug (0, "bus '%s' registered", r->name);
361 for (vnet_dev_driver_registration_t *r = dm->driver_registrations; r;
362 r = r->next_registration)
365 vec_sort_with_function (drv, sort_driver_registrations);
367 vec_foreach_pointer (r, drv)
369 vnet_dev_driver_t *driver;
371 vnet_device_class_t *dev_class;
374 pool_foreach (bus, dm->buses)
376 if (strcmp (bus->registration->name, r->bus) == 0)
378 bus_index = bus->index;
384 return clib_error_return (0, "unknown bus '%s'", r->bus);
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){
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,
399 .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,
402 .set_rss_queues_function = vnet_dev_interface_set_rss_queues,
404 driver->dev_class_index = vnet_register_device_class (vm, dev_class);
405 log_debug (0, "driver '%s' registered on bus '%s'", r->name,
406 bus->registration->name);
408 if (temp_space_sz < r->runtime_temp_space_sz)
409 temp_space_sz = r->runtime_temp_space_sz;
412 if (dm->startup_config)
413 log_debug (0, "startup config: %v", dm->startup_config);
417 if (temp_space_sz > 0)
419 const u32 align = CLIB_CACHE_LINE_BYTES;
420 u32 sz = round_pow2 (temp_space_sz, align);
421 dm->log2_runtime_temp_space_sz =
422 get_lowest_set_bit_index (max_pow2 (sz));
423 sz = 1 << dm->log2_runtime_temp_space_sz;
424 sz *= vlib_get_n_threads ();
425 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
426 clib_memset (dm->runtime_temp_spaces, 0, sz);
428 "requested %u bytes for runtime temp storage, allocated %u "
429 "per thread (total %u)",
430 temp_space_sz, 1 << dm->log2_runtime_temp_space_sz, sz);
433 vnet_feature_register (vnet_dev_feature_update_cb, 0);
438 VLIB_INIT_FUNCTION (vnet_dev_main_init);
441 vnet_dev_num_workers_change (vlib_main_t *vm)
443 vnet_dev_main_t *dm = &vnet_dev_main;
445 if (dm->log2_runtime_temp_space_sz > 0)
447 const u32 align = CLIB_CACHE_LINE_BYTES;
449 (1ULL << dm->log2_runtime_temp_space_sz) * vlib_get_n_threads ();
450 if (dm->runtime_temp_spaces)
451 clib_mem_free (dm->runtime_temp_spaces);
452 dm->runtime_temp_spaces = clib_mem_alloc_aligned (sz, align);
453 clib_memset (dm->runtime_temp_spaces, 0, sz);
454 log_debug (0, "runtime temp storage resized to %u", sz);
460 VLIB_NUM_WORKERS_CHANGE_FN (vnet_dev_num_workers_change);