blob: 260bc7b2e2241d2cd1052604244d6deffabede78 [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/counters.h>
10#include <vnet/dev/log.h>
11#include <vnet/dev/api.h>
12
13VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
14 .class_name = "dev",
15 .subclass_name = "api",
16};
17
18static int
19_vnet_dev_queue_size_validate (u32 size, vnet_dev_queue_config_t c)
20{
21 if (size < c.min_size)
22 return 0;
23 if (size > c.max_size)
24 return 0;
25 if (c.size_is_power_of_two && count_set_bits (size) != 1)
26 return 0;
27 if (c.multiplier && size % c.multiplier)
28 return 0;
29
30 return 1;
31}
32
33vnet_dev_rv_t
34vnet_dev_api_attach (vlib_main_t *vm, vnet_dev_api_attach_args_t *args)
35{
36 vnet_dev_main_t *dm = &vnet_dev_main;
37 vnet_dev_t *dev = 0;
38 vnet_dev_rv_t rv = VNET_DEV_OK;
39 vnet_dev_bus_t *bus;
40 vnet_dev_driver_t *driver;
41 void *bus_dev_info = 0;
42 u8 *dev_desc = 0;
43
44 log_debug (0, "%s driver %s flags '%U' args '%v'", args->device_id,
45 args->driver_name, format_vnet_dev_flags, &args->flags,
46 args->args);
47
48 if (vnet_dev_by_id (args->device_id))
49 return VNET_DEV_ERR_ALREADY_IN_USE;
50
51 bus = vnet_dev_find_device_bus (vm, args->device_id);
52 if (!bus)
53 {
54 log_err (dev, "unknown bus");
55 rv = VNET_DEV_ERR_INVALID_BUS;
56 goto done;
57 }
58
59 bus_dev_info = vnet_dev_get_device_info (vm, args->device_id);
60 if (!bus_dev_info)
61 {
62 log_err (dev, "invalid or unsupported device id");
63 rv = VNET_DEV_ERR_INVALID_DEVICE_ID;
64 goto done;
65 }
66
67 vec_foreach (driver, dm->drivers)
68 {
69 if (args->driver_name[0] &&
70 strcmp (args->driver_name, driver->registration->name))
71 continue;
72 if (driver->ops.probe &&
73 (dev_desc = driver->ops.probe (vm, bus->index, bus_dev_info)))
74 break;
75 }
76
77 if (!dev_desc)
78 {
79 log_err (dev, "driver not available for %s", args->device_id);
80 rv = VNET_DEV_ERR_DRIVER_NOT_AVAILABLE;
81 goto done;
82 }
83
84 dev = vnet_dev_alloc (vm, args->device_id, driver);
85 if (!dev)
86 {
87 log_err (dev, "dev alloc failed for %s", args->device_id);
88 rv = VNET_DEV_ERR_BUG;
89 goto done;
90 }
91 dev->description = dev_desc;
92
Damjan Marion69768d92023-11-13 17:33:32 +000093 for (vnet_dev_arg_t *a = driver->registration->args;
94 a->type != VNET_DEV_ARG_END; a++)
95 vec_add1 (dev->args, *a);
96
97 if (args->args)
98 {
99 if ((rv = vnet_dev_arg_parse (vm, dev, dev->args, args->args)) !=
100 VNET_DEV_OK)
101 goto done;
102 }
103
Damjan Marion38c61912023-10-17 16:06:26 +0000104 if ((args->flags.e & VNET_DEV_F_NO_STATS) == 0)
105 dev->poll_stats = 1;
106
107 log_debug (0, "found '%v'", dev->description);
108
109 rv = vnet_dev_process_call_op (vm, dev, vnet_dev_init);
110
111done:
112 if (bus_dev_info)
113 bus->ops.free_device_info (vm, bus_dev_info);
114
115 if (rv != VNET_DEV_OK && dev)
116 vnet_dev_process_call_op_no_rv (vm, dev, vnet_dev_free);
117
118 return rv;
119}
120
121vnet_dev_rv_t
122vnet_dev_api_detach (vlib_main_t *vm, vnet_dev_api_detach_args_t *args)
123{
124 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
125
126 log_debug (dev, "detach");
127
128 if (dev)
129 return vnet_dev_process_call_op_no_rv (vm, dev, vnet_dev_detach);
130
131 return VNET_DEV_ERR_NOT_FOUND;
132}
133
134vnet_dev_rv_t
135vnet_dev_api_reset (vlib_main_t *vm, vnet_dev_api_reset_args_t *args)
136{
137 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
138
139 log_debug (dev, "detach");
140
141 if (!dev)
142 return VNET_DEV_ERR_NOT_FOUND;
143
144 if (dev->ops.reset)
145 return VNET_DEV_ERR_NOT_SUPPORTED;
146
147 return vnet_dev_process_call_op (vm, dev, vnet_dev_reset);
148}
149
150vnet_dev_rv_t
151vnet_dev_api_create_port_if (vlib_main_t *vm,
152 vnet_dev_api_create_port_if_args_t *args)
153{
154 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
155 vnet_dev_port_t *port = 0;
156 u16 n_threads = vlib_get_n_threads ();
Damjan Marionb8dd9812023-11-03 13:47:05 +0000157 int default_is_intr_mode;
Damjan Marion69768d92023-11-13 17:33:32 +0000158 vnet_dev_rv_t rv;
Damjan Marion38c61912023-10-17 16:06:26 +0000159
160 log_debug (dev,
161 "create_port_if: device '%s' port %u intf_name '%s' num_rx_q %u "
162 "num_tx_q %u rx_q_sz %u tx_q_sz %u, flags '%U' args '%v'",
163 args->device_id, args->port_id, args->intf_name,
164 args->num_rx_queues, args->num_tx_queues, args->rx_queue_size,
165 args->tx_queue_size, format_vnet_dev_port_flags, &args->flags,
166 args->args);
167
168 if (dev == 0)
169 return VNET_DEV_ERR_NOT_FOUND;
170
171 foreach_vnet_dev_port (p, dev)
172 if (p->port_id == args->port_id)
173 {
174 port = p;
175 break;
176 }
177
178 if (!port)
179 return VNET_DEV_ERR_INVALID_DEVICE_ID;
180
181 if (port->interface_created)
182 return VNET_DEV_ERR_ALREADY_EXISTS;
183
Damjan Marion69768d92023-11-13 17:33:32 +0000184 if (args->args)
185 {
186 rv = vnet_dev_arg_parse (vm, dev, port->args, args->args);
187 if (rv != VNET_DEV_OK)
188 return rv;
189 }
190
Damjan Marionb8dd9812023-11-03 13:47:05 +0000191 default_is_intr_mode = (args->flags.e & VNET_DEV_PORT_F_INTERRUPT_MODE) != 0;
192 if (default_is_intr_mode && port->attr.caps.interrupt_mode == 0)
193 {
194 log_err (dev, "interrupt mode requested and port doesn't support it");
195 return VNET_DEV_ERR_NOT_SUPPORTED;
196 }
197
Damjan Marion38c61912023-10-17 16:06:26 +0000198 if (args->num_rx_queues)
199 {
200 if (args->num_rx_queues > port->attr.max_rx_queues)
201 return VNET_DEV_ERR_INVALID_NUM_RX_QUEUES;
202 port->intf.num_rx_queues = args->num_rx_queues;
203 }
204 else
205 port->intf.num_rx_queues = clib_min (port->attr.max_tx_queues, 1);
206
207 if (args->num_tx_queues)
208 {
209 if (args->num_tx_queues > port->attr.max_tx_queues)
210 return VNET_DEV_ERR_INVALID_NUM_TX_QUEUES;
211 port->intf.num_tx_queues = args->num_tx_queues;
212 }
213 else
214 port->intf.num_tx_queues = clib_min (port->attr.max_tx_queues, n_threads);
215
216 if (args->rx_queue_size)
217 {
218 if (!_vnet_dev_queue_size_validate (args->rx_queue_size,
219 port->rx_queue_config))
220 return VNET_DEV_ERR_INVALID_RX_QUEUE_SIZE;
221 port->intf.rxq_sz = args->rx_queue_size;
222 }
223 else
224 port->intf.rxq_sz = port->rx_queue_config.default_size;
225
226 if (args->tx_queue_size)
227 {
228 if (!_vnet_dev_queue_size_validate (args->tx_queue_size,
229 port->tx_queue_config))
230 return VNET_DEV_ERR_INVALID_TX_QUEUE_SIZE;
231 port->intf.txq_sz = args->tx_queue_size;
232 }
233 else
234 port->intf.txq_sz = port->tx_queue_config.default_size;
235
236 clib_memcpy (port->intf.name, args->intf_name, sizeof (port->intf.name));
Damjan Marionb8dd9812023-11-03 13:47:05 +0000237 port->intf.default_is_intr_mode = default_is_intr_mode;
Damjan Marion38c61912023-10-17 16:06:26 +0000238
239 return vnet_dev_process_call_port_op (vm, port, vnet_dev_port_if_create);
240}
241
242vnet_dev_rv_t
243vnet_dev_api_remove_port_if (vlib_main_t *vm,
244 vnet_dev_api_remove_port_if_args_t *args)
245{
246 vnet_dev_main_t *dm = &vnet_dev_main;
247 vnet_main_t *vnm = vnet_get_main ();
248 vnet_sw_interface_t *si;
249 vnet_hw_interface_t *hi;
250 vnet_dev_port_t *port;
251
252 si = vnet_get_sw_interface_or_null (vnm, args->sw_if_index);
253 if (!si)
254 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
255
256 hi = vnet_get_hw_interface_or_null (vnm, si->hw_if_index);
257 if (!hi)
258 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
259
260 if (pool_is_free_index (dm->ports_by_dev_instance, hi->dev_instance))
261 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
262
263 port = vnet_dev_get_port_from_dev_instance (hi->dev_instance);
264
265 if (port->intf.hw_if_index != si->hw_if_index)
266 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
267
268 return vnet_dev_process_call_port_op (vm, port, vnet_dev_port_if_remove);
269}