blob: 4d556c72367c444a279b4e86935d74cfbd90677a [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
93 if ((args->flags.e & VNET_DEV_F_NO_STATS) == 0)
94 dev->poll_stats = 1;
95
96 log_debug (0, "found '%v'", dev->description);
97
98 rv = vnet_dev_process_call_op (vm, dev, vnet_dev_init);
99
100done:
101 if (bus_dev_info)
102 bus->ops.free_device_info (vm, bus_dev_info);
103
104 if (rv != VNET_DEV_OK && dev)
105 vnet_dev_process_call_op_no_rv (vm, dev, vnet_dev_free);
106
107 return rv;
108}
109
110vnet_dev_rv_t
111vnet_dev_api_detach (vlib_main_t *vm, vnet_dev_api_detach_args_t *args)
112{
113 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
114
115 log_debug (dev, "detach");
116
117 if (dev)
118 return vnet_dev_process_call_op_no_rv (vm, dev, vnet_dev_detach);
119
120 return VNET_DEV_ERR_NOT_FOUND;
121}
122
123vnet_dev_rv_t
124vnet_dev_api_reset (vlib_main_t *vm, vnet_dev_api_reset_args_t *args)
125{
126 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
127
128 log_debug (dev, "detach");
129
130 if (!dev)
131 return VNET_DEV_ERR_NOT_FOUND;
132
133 if (dev->ops.reset)
134 return VNET_DEV_ERR_NOT_SUPPORTED;
135
136 return vnet_dev_process_call_op (vm, dev, vnet_dev_reset);
137}
138
139vnet_dev_rv_t
140vnet_dev_api_create_port_if (vlib_main_t *vm,
141 vnet_dev_api_create_port_if_args_t *args)
142{
143 vnet_dev_t *dev = vnet_dev_by_id (args->device_id);
144 vnet_dev_port_t *port = 0;
145 u16 n_threads = vlib_get_n_threads ();
146
147 log_debug (dev,
148 "create_port_if: device '%s' port %u intf_name '%s' num_rx_q %u "
149 "num_tx_q %u rx_q_sz %u tx_q_sz %u, flags '%U' args '%v'",
150 args->device_id, args->port_id, args->intf_name,
151 args->num_rx_queues, args->num_tx_queues, args->rx_queue_size,
152 args->tx_queue_size, format_vnet_dev_port_flags, &args->flags,
153 args->args);
154
155 if (dev == 0)
156 return VNET_DEV_ERR_NOT_FOUND;
157
158 foreach_vnet_dev_port (p, dev)
159 if (p->port_id == args->port_id)
160 {
161 port = p;
162 break;
163 }
164
165 if (!port)
166 return VNET_DEV_ERR_INVALID_DEVICE_ID;
167
168 if (port->interface_created)
169 return VNET_DEV_ERR_ALREADY_EXISTS;
170
171 if (args->num_rx_queues)
172 {
173 if (args->num_rx_queues > port->attr.max_rx_queues)
174 return VNET_DEV_ERR_INVALID_NUM_RX_QUEUES;
175 port->intf.num_rx_queues = args->num_rx_queues;
176 }
177 else
178 port->intf.num_rx_queues = clib_min (port->attr.max_tx_queues, 1);
179
180 if (args->num_tx_queues)
181 {
182 if (args->num_tx_queues > port->attr.max_tx_queues)
183 return VNET_DEV_ERR_INVALID_NUM_TX_QUEUES;
184 port->intf.num_tx_queues = args->num_tx_queues;
185 }
186 else
187 port->intf.num_tx_queues = clib_min (port->attr.max_tx_queues, n_threads);
188
189 if (args->rx_queue_size)
190 {
191 if (!_vnet_dev_queue_size_validate (args->rx_queue_size,
192 port->rx_queue_config))
193 return VNET_DEV_ERR_INVALID_RX_QUEUE_SIZE;
194 port->intf.rxq_sz = args->rx_queue_size;
195 }
196 else
197 port->intf.rxq_sz = port->rx_queue_config.default_size;
198
199 if (args->tx_queue_size)
200 {
201 if (!_vnet_dev_queue_size_validate (args->tx_queue_size,
202 port->tx_queue_config))
203 return VNET_DEV_ERR_INVALID_TX_QUEUE_SIZE;
204 port->intf.txq_sz = args->tx_queue_size;
205 }
206 else
207 port->intf.txq_sz = port->tx_queue_config.default_size;
208
209 clib_memcpy (port->intf.name, args->intf_name, sizeof (port->intf.name));
210
211 return vnet_dev_process_call_port_op (vm, port, vnet_dev_port_if_create);
212}
213
214vnet_dev_rv_t
215vnet_dev_api_remove_port_if (vlib_main_t *vm,
216 vnet_dev_api_remove_port_if_args_t *args)
217{
218 vnet_dev_main_t *dm = &vnet_dev_main;
219 vnet_main_t *vnm = vnet_get_main ();
220 vnet_sw_interface_t *si;
221 vnet_hw_interface_t *hi;
222 vnet_dev_port_t *port;
223
224 si = vnet_get_sw_interface_or_null (vnm, args->sw_if_index);
225 if (!si)
226 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
227
228 hi = vnet_get_hw_interface_or_null (vnm, si->hw_if_index);
229 if (!hi)
230 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
231
232 if (pool_is_free_index (dm->ports_by_dev_instance, hi->dev_instance))
233 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
234
235 port = vnet_dev_get_port_from_dev_instance (hi->dev_instance);
236
237 if (port->intf.hw_if_index != si->hw_if_index)
238 return VNET_DEV_ERR_UNKNOWN_INTERFACE;
239
240 return vnet_dev_process_call_port_op (vm, port, vnet_dev_port_if_remove);
241}