dev: add change_max_rx_frame_size capability
[vpp.git] / src / vnet / dev / dev.c
1 /* 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
12 VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
13   .class_name = "dev",
14 };
15
16 vnet_dev_main_t vnet_dev_main = { .next_rx_queue_thread = 1 };
17
18 vnet_dev_bus_t *
19 vnet_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
45 void *
46 vnet_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
57 vnet_dev_t *
58 vnet_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
83 vnet_dev_rv_t
84 vnet_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
95   if (dev->ops.alloc)
96     {
97       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         }
107     }
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
124 void
125 vnet_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
147 void
148 vnet_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);
163   pool_put_index (dm->devices, dev->index);
164 }
165
166 vnet_dev_rv_t
167 vnet_dev_reset (vlib_main_t *vm, vnet_dev_t *dev)
168 {
169   vnet_dev_rv_t rv;
170
171   ASSERT (dev->initialized == 1);
172   vnet_dev_validate (vm, dev);
173
174   if (dev->ops.reset == 0)
175     return VNET_DEV_ERR_NOT_SUPPORTED;
176
177   if ((rv = dev->ops.reset (vm, dev)) != VNET_DEV_OK)
178     {
179       log_err (dev, "device reset failed [rv %d]", rv);
180       return rv;
181     }
182
183   return VNET_DEV_OK;
184 }
185
186 void
187 vnet_dev_detach (vlib_main_t *vm, vnet_dev_t *dev)
188 {
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);
194 }
195
196 vnet_dev_rv_t
197 vnet_dev_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size, u32 align,
198                         void **pp)
199 {
200   vnet_dev_main_t *dm = &vnet_dev_main;
201   vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
202   vnet_dev_rv_t rv;
203
204   vnet_dev_validate (vm, dev);
205
206   if (!bus->ops.dma_mem_alloc_fn)
207     return VNET_DEV_ERR_NOT_SUPPORTED;
208
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,
213                align);
214   return rv;
215 }
216
217 void
218 vnet_dev_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
219 {
220   vnet_dev_main_t *dm = &vnet_dev_main;
221   vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
222
223   vnet_dev_validate (vm, dev);
224
225   if (p == 0 || !bus->ops.dma_mem_free_fn)
226     return;
227
228   return bus->ops.dma_mem_free_fn (vm, dev, p);
229 }
230
231 clib_error_t *
232 vnet_dev_admin_up_down_fn (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
233 {
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;
239
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);
244
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);
248
249   return 0;
250 }
251
252 static void
253 vnet_dev_feature_update_cb (u32 sw_if_index, u8 arc_index, u8 is_enable,
254                             void *cb)
255 {
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;
264   u32 next_index = ~0;
265   int update_runtime = 0;
266
267   if (arc_index != vdm->eth_port_rx_feature_arc_index)
268     return;
269
270   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
271   port = vnet_dev_get_port_from_dev_instance (hw->dev_instance);
272
273   if (port == 0 || port->intf.sw_if_index != sw_if_index)
274     return;
275
276   if (vnet_have_features (arc_index, sw_if_index))
277     {
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, &current_config_index,
282                             &next_index, 0);
283       if (port->intf.feature_arc == 0 ||
284           port->intf.rx_next_index != next_index ||
285           port->intf.current_config_index != current_config_index)
286         {
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;
291           update_runtime = 1;
292         }
293     }
294   else
295     {
296       if (port->intf.feature_arc)
297         {
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;
305           update_runtime = 1;
306         }
307     }
308
309   if (update_runtime)
310     {
311       foreach_vnet_dev_port_rx_queue (rxq, port)
312         vnet_dev_rx_queue_rt_request (
313           vm, rxq,
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");
318     }
319 }
320
321 static int
322 sort_driver_registrations (void *a0, void *a1)
323 {
324   vnet_dev_driver_registration_t **r0 = a0;
325   vnet_dev_driver_registration_t **r1 = a1;
326
327   if (r0[0]->priority > r1[0]->priority)
328     return -1;
329   else if (r0[0]->priority < r1[0]->priority)
330     return 1;
331
332   return 0;
333 }
334
335 static clib_error_t *
336 vnet_dev_main_init (vlib_main_t *vm)
337 {
338   vnet_dev_main_t *dm = &vnet_dev_main;
339   vnet_dev_driver_registration_t **drv = 0;
340   u32 temp_space_sz = 0;
341
342   dm->device_index_by_id = hash_create_string (0, sizeof (uword));
343
344   for (vnet_dev_bus_registration_t *r = dm->bus_registrations; r;
345        r = r->next_registration)
346     {
347       vnet_dev_bus_t *bus;
348       pool_get_zero (dm->buses, bus);
349       bus->registration = r;
350       bus->index = bus - dm->buses;
351       bus->ops = r->ops;
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);
356
357       log_debug (0, "bus '%s' registered", r->name);
358     }
359
360   for (vnet_dev_driver_registration_t *r = dm->driver_registrations; r;
361        r = r->next_registration)
362     vec_add1 (drv, r);
363
364   vec_sort_with_function (drv, sort_driver_registrations);
365
366   vec_foreach_pointer (r, drv)
367     {
368       vnet_dev_driver_t *driver;
369       vnet_dev_bus_t *bus;
370       vnet_device_class_t *dev_class;
371       int bus_index = -1;
372
373       pool_foreach (bus, dm->buses)
374         {
375           if (strcmp (bus->registration->name, r->bus) == 0)
376             {
377               bus_index = bus->index;
378               break;
379             }
380         }
381
382       if (bus_index < 0)
383         return clib_error_return (0, "unknown bus '%s'", r->bus);
384
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){
392         .name = r->name,
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,
402       };
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);
406
407       if (temp_space_sz < r->runtime_temp_space_sz)
408         temp_space_sz = r->runtime_temp_space_sz;
409     }
410
411   if (dm->startup_config)
412     log_debug (0, "startup config: %v", dm->startup_config);
413
414   vec_free (drv);
415
416   if (temp_space_sz > 0)
417     {
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);
426       log_debug (0,
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);
430     }
431
432   vnet_feature_register (vnet_dev_feature_update_cb, 0);
433
434   return 0;
435 }
436
437 VLIB_INIT_FUNCTION (vnet_dev_main_init);
438
439 clib_error_t *
440 vnet_dev_num_workers_change (vlib_main_t *vm)
441 {
442   vnet_dev_main_t *dm = &vnet_dev_main;
443
444   if (dm->log2_runtime_temp_space_sz > 0)
445     {
446       const u32 align = CLIB_CACHE_LINE_BYTES;
447       uword sz =
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);
454     }
455
456   return 0;
457 }
458
459 VLIB_NUM_WORKERS_CHANGE_FN (vnet_dev_num_workers_change);