Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2017 6WIND S.A.
3 : : * Copyright 2017 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <stdbool.h>
7 : :
8 : : #include <rte_alarm.h>
9 : : #include <rte_malloc.h>
10 : : #include <ethdev_driver.h>
11 : : #include <ethdev_vdev.h>
12 : : #include <rte_devargs.h>
13 : : #include <rte_kvargs.h>
14 : : #include <bus_driver.h>
15 : : #include <bus_vdev_driver.h>
16 : :
17 : : #include "failsafe_private.h"
18 : :
19 : : const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
20 : : static const struct rte_eth_link eth_link = {
21 : : .link_speed = RTE_ETH_SPEED_NUM_10G,
22 : : .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
23 : : .link_status = RTE_ETH_LINK_UP,
24 : : .link_autoneg = RTE_ETH_LINK_AUTONEG,
25 : : };
26 : :
27 : : static int
28 : 0 : fs_sub_device_alloc(struct rte_eth_dev *dev,
29 : : const char *params)
30 : : {
31 : : uint8_t nb_subs;
32 : : int ret;
33 : : int i;
34 : : struct sub_device *sdev;
35 : : uint8_t sdev_iterator;
36 : :
37 : 0 : ret = failsafe_args_count_subdevice(dev, params);
38 [ # # ]: 0 : if (ret)
39 : : return ret;
40 [ # # ]: 0 : if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
41 : 0 : ERROR("Cannot allocate more than %d ports",
42 : : FAILSAFE_MAX_ETHPORTS);
43 : 0 : return -ENOSPC;
44 : : }
45 : : nb_subs = PRIV(dev)->subs_tail;
46 : 0 : PRIV(dev)->subs = rte_zmalloc(NULL,
47 : : sizeof(struct sub_device) * nb_subs,
48 : : RTE_CACHE_LINE_SIZE);
49 [ # # ]: 0 : if (PRIV(dev)->subs == NULL) {
50 : 0 : ERROR("Could not allocate sub_devices");
51 : 0 : return -ENOMEM;
52 : : }
53 : : /* Initiate static sub devices linked list. */
54 [ # # ]: 0 : for (i = 1; i < nb_subs; i++)
55 : 0 : PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i;
56 [ # # ]: 0 : PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs;
57 : :
58 [ # # ]: 0 : FOREACH_SUBDEV(sdev, sdev_iterator, dev) {
59 [ # # ]: 0 : sdev->sdev_port_id = RTE_MAX_ETHPORTS;
60 : : }
61 : : return 0;
62 : : }
63 : :
64 : : static void fs_hotplug_alarm(void *arg);
65 : :
66 : : int
67 : 0 : failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
68 : : {
69 : : int ret;
70 : :
71 [ # # ]: 0 : if (dev == NULL)
72 : : return -EINVAL;
73 [ # # ]: 0 : if (PRIV(dev)->pending_alarm)
74 : : return 0;
75 : 0 : ret = rte_eal_alarm_set(failsafe_hotplug_poll * 1000,
76 : : fs_hotplug_alarm,
77 : : dev);
78 [ # # ]: 0 : if (ret) {
79 : 0 : ERROR("Could not set up plug-in event detection");
80 : 0 : return ret;
81 : : }
82 : 0 : PRIV(dev)->pending_alarm = 1;
83 : 0 : return 0;
84 : : }
85 : :
86 : : int
87 : 0 : failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
88 : : {
89 : : int ret = 0;
90 : :
91 : 0 : rte_errno = 0;
92 : 0 : rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
93 [ # # ]: 0 : if (rte_errno) {
94 : 0 : ERROR("rte_eal_alarm_cancel failed (errno: %s)",
95 : : strerror(rte_errno));
96 : 0 : ret = -rte_errno;
97 : : } else {
98 : 0 : PRIV(dev)->pending_alarm = 0;
99 : : }
100 : 0 : return ret;
101 : : }
102 : :
103 : : static void
104 : 0 : fs_hotplug_alarm(void *arg)
105 : : {
106 : : struct rte_eth_dev *dev = arg;
107 : : struct sub_device *sdev;
108 : : int ret;
109 : : uint8_t i;
110 : :
111 [ # # ]: 0 : if (!PRIV(dev)->pending_alarm)
112 : : return;
113 [ # # ]: 0 : PRIV(dev)->pending_alarm = 0;
114 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, dev)
115 [ # # ]: 0 : if (sdev->state != PRIV(dev)->state)
116 : : break;
117 : : /* if we have non-probed device */
118 [ # # ]: 0 : if (i != PRIV(dev)->subs_tail) {
119 [ # # ]: 0 : if (fs_lock(dev, 1) != 0)
120 : 0 : goto reinstall;
121 : 0 : ret = failsafe_eth_dev_state_sync(dev);
122 : 0 : fs_unlock(dev, 1);
123 [ # # ]: 0 : if (ret)
124 : 0 : ERROR("Unable to synchronize sub_device state");
125 : : }
126 : 0 : failsafe_dev_remove(dev);
127 : 0 : reinstall:
128 : 0 : ret = failsafe_hotplug_alarm_install(dev);
129 [ # # ]: 0 : if (ret)
130 : 0 : ERROR("Unable to set up next alarm");
131 : : }
132 : :
133 : : static int
134 : 0 : fs_mutex_init(struct fs_priv *priv)
135 : : {
136 : : int ret;
137 : : pthread_mutexattr_t attr;
138 : :
139 : 0 : ret = pthread_mutexattr_init(&attr);
140 [ # # ]: 0 : if (ret) {
141 : 0 : ERROR("Cannot initiate mutex attributes - %s", strerror(ret));
142 : 0 : return ret;
143 : : }
144 : : /* Allow mutex relocks for the thread holding the mutex. */
145 : 0 : ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
146 [ # # ]: 0 : if (ret) {
147 : 0 : ERROR("Cannot set mutex type - %s", strerror(ret));
148 : 0 : return ret;
149 : : }
150 : 0 : ret = pthread_mutex_init(&priv->hotplug_mutex, &attr);
151 [ # # ]: 0 : if (ret) {
152 : 0 : ERROR("Cannot initiate mutex - %s", strerror(ret));
153 : 0 : return ret;
154 : : }
155 : : return 0;
156 : : }
157 : :
158 : : static int
159 : 0 : fs_eth_dev_create(struct rte_vdev_device *vdev)
160 : : {
161 : : struct rte_eth_dev *dev;
162 : : struct rte_ether_addr *mac;
163 : : struct fs_priv *priv;
164 : : struct sub_device *sdev;
165 : : const char *params;
166 : : unsigned int socket_id;
167 : : uint8_t i;
168 : : int ret;
169 : :
170 : : dev = NULL;
171 : : priv = NULL;
172 : 0 : socket_id = rte_socket_id();
173 : 0 : INFO("Creating fail-safe device on NUMA socket %u", socket_id);
174 : : params = rte_vdev_device_args(vdev);
175 [ # # ]: 0 : if (params == NULL) {
176 : 0 : ERROR("This PMD requires sub-devices, none provided");
177 : 0 : return -1;
178 : : }
179 : 0 : dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
180 [ # # ]: 0 : if (dev == NULL) {
181 : 0 : ERROR("Unable to allocate rte_eth_dev");
182 : 0 : return -1;
183 : : }
184 : 0 : priv = PRIV(dev);
185 : 0 : priv->data = dev->data;
186 : 0 : priv->rxp = FS_RX_PROXY_INIT;
187 : 0 : dev->dev_ops = &failsafe_ops;
188 : 0 : dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
189 : 0 : dev->data->dev_link = eth_link;
190 : 0 : PRIV(dev)->nb_mac_addr = 1;
191 : 0 : TAILQ_INIT(&PRIV(dev)->flow_list);
192 : 0 : dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
193 : 0 : dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
194 : 0 : ret = fs_sub_device_alloc(dev, params);
195 [ # # ]: 0 : if (ret) {
196 : 0 : ERROR("Could not allocate sub_devices");
197 : 0 : goto free_dev;
198 : : }
199 : 0 : ret = failsafe_args_parse(dev, params);
200 [ # # ]: 0 : if (ret)
201 : 0 : goto free_subs;
202 : 0 : ret = rte_eth_dev_owner_new(&priv->my_owner.id);
203 [ # # ]: 0 : if (ret) {
204 : 0 : ERROR("Failed to get unique owner identifier");
205 : 0 : goto free_args;
206 : : }
207 : 0 : snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
208 : : FAILSAFE_OWNER_NAME);
209 : 0 : DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id,
210 : : priv->my_owner.name, priv->my_owner.id);
211 : 0 : ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
212 : : failsafe_eth_new_event_callback,
213 : : dev);
214 [ # # ]: 0 : if (ret) {
215 : 0 : ERROR("Failed to register NEW callback");
216 : 0 : goto free_args;
217 : : }
218 : 0 : ret = failsafe_eal_init(dev);
219 [ # # ]: 0 : if (ret)
220 : 0 : goto unregister_new_callback;
221 : 0 : ret = fs_mutex_init(priv);
222 [ # # ]: 0 : if (ret)
223 : 0 : goto unregister_new_callback;
224 : 0 : ret = failsafe_hotplug_alarm_install(dev);
225 [ # # ]: 0 : if (ret) {
226 : 0 : ERROR("Could not set up plug-in event detection");
227 : 0 : goto unregister_new_callback;
228 : : }
229 : 0 : mac = &dev->data->mac_addrs[0];
230 [ # # ]: 0 : if (failsafe_mac_from_arg) {
231 : : /*
232 : : * If MAC address was provided as a parameter,
233 : : * apply to all probed subdevices.
234 : : */
235 [ # # ]: 0 : FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
236 : 0 : ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
237 : : mac);
238 [ # # ]: 0 : if (ret) {
239 : 0 : ERROR("Failed to set default MAC address");
240 : 0 : goto cancel_alarm;
241 : : }
242 : : }
243 : : } else {
244 : : /*
245 : : * Use the ether_addr from first probed
246 : : * device, either preferred or fallback.
247 : : */
248 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, dev)
249 [ # # ]: 0 : if (sdev->state >= DEV_PROBED) {
250 : : rte_ether_addr_copy(
251 [ # # ]: 0 : Ð(sdev)->data->mac_addrs[0], mac);
252 : : break;
253 : : }
254 : : /*
255 : : * If no device has been probed and no ether_addr
256 : : * has been provided on the command line, use a random
257 : : * valid one.
258 : : * It will be applied during future state syncs to
259 : : * probed subdevices.
260 : : */
261 [ # # ]: 0 : if (i == priv->subs_tail)
262 : 0 : rte_eth_random_addr(&mac->addr_bytes[0]);
263 : : }
264 : 0 : INFO("MAC address is " RTE_ETHER_ADDR_PRT_FMT,
265 : : RTE_ETHER_ADDR_BYTES(mac));
266 : 0 : dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC |
267 : : RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
268 : :
269 : : /* Allocate interrupt instance */
270 : 0 : PRIV(dev)->intr_handle =
271 : 0 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
272 [ # # ]: 0 : if (PRIV(dev)->intr_handle == NULL) {
273 : 0 : ERROR("Failed to allocate intr handle");
274 : 0 : goto cancel_alarm;
275 : : }
276 : :
277 [ # # ]: 0 : if (rte_intr_fd_set(PRIV(dev)->intr_handle, -1))
278 : 0 : goto cancel_alarm;
279 : :
280 [ # # ]: 0 : if (rte_intr_type_set(PRIV(dev)->intr_handle, RTE_INTR_HANDLE_EXT))
281 : 0 : goto cancel_alarm;
282 : :
283 : 0 : rte_eth_dev_probing_finish(dev);
284 : :
285 : 0 : return 0;
286 : 0 : cancel_alarm:
287 : 0 : failsafe_hotplug_alarm_cancel(dev);
288 : 0 : unregister_new_callback:
289 : 0 : rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
290 : : failsafe_eth_new_event_callback, dev);
291 : 0 : free_args:
292 : 0 : failsafe_args_free(dev);
293 : 0 : free_subs:
294 : 0 : rte_free(PRIV(dev)->subs);
295 : 0 : free_dev:
296 : : /* mac_addrs must not be freed alone because part of dev_private */
297 : 0 : dev->data->mac_addrs = NULL;
298 : 0 : rte_eth_dev_release_port(dev);
299 : 0 : return -1;
300 : : }
301 : :
302 : : static int
303 : 0 : fs_rte_eth_free(const char *name)
304 : : {
305 : : struct rte_eth_dev *dev;
306 : : int ret;
307 : :
308 : 0 : dev = rte_eth_dev_allocated(name);
309 [ # # ]: 0 : if (dev == NULL)
310 : : return 0; /* port already released */
311 : 0 : ret = failsafe_eth_dev_close(dev);
312 : 0 : rte_intr_instance_free(PRIV(dev)->intr_handle);
313 : 0 : rte_eth_dev_release_port(dev);
314 : 0 : return ret;
315 : : }
316 : :
317 : : static bool
318 : 0 : devargs_already_listed(struct rte_devargs *devargs)
319 : : {
320 : : struct rte_devargs *list_da;
321 : :
322 [ # # ]: 0 : RTE_EAL_DEVARGS_FOREACH(devargs->bus->name, list_da) {
323 [ # # ]: 0 : if (strcmp(list_da->name, devargs->name) == 0)
324 : : /* devargs already in the list */
325 : : return true;
326 : : }
327 : : return false;
328 : : }
329 : :
330 : : static int
331 [ # # ]: 0 : rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
332 : : {
333 : : const char *name;
334 : : struct rte_eth_dev *eth_dev;
335 : : struct sub_device *sdev;
336 : : struct rte_devargs devargs;
337 : : uint8_t i;
338 : : int ret;
339 : :
340 : : name = rte_vdev_device_name(vdev);
341 : 0 : INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
342 : : name);
343 : :
344 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
345 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
346 [ # # ]: 0 : if (!eth_dev) {
347 : 0 : ERROR("Failed to probe %s", name);
348 : 0 : return -1;
349 : : }
350 : 0 : eth_dev->dev_ops = &failsafe_ops;
351 : 0 : eth_dev->device = &vdev->device;
352 : 0 : eth_dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
353 [ # # ]: 0 : eth_dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
354 : : /*
355 : : * Failsafe will attempt to probe all of its sub-devices.
356 : : * Any failure in sub-devices is not a fatal error.
357 : : * A sub-device can be plugged later.
358 : : */
359 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, eth_dev) {
360 : : /* skip empty devargs */
361 [ # # ]: 0 : if (sdev->devargs.name[0] == '\0')
362 : 0 : continue;
363 : :
364 : : /* rebuild devargs to be able to get the bus name. */
365 : 0 : ret = rte_devargs_parse(&devargs,
366 : 0 : sdev->devargs.name);
367 [ # # ]: 0 : if (ret != 0) {
368 : 0 : ERROR("Failed to parse devargs %s",
369 : : devargs.name);
370 : 0 : continue;
371 : : }
372 [ # # ]: 0 : if (!devargs_already_listed(&devargs)) {
373 : 0 : ret = rte_dev_probe(devargs.name);
374 [ # # ]: 0 : if (ret < 0) {
375 : 0 : ERROR("Failed to probe devargs %s",
376 : : devargs.name);
377 : 0 : continue;
378 : : }
379 : : }
380 : : }
381 : 0 : rte_eth_dev_probing_finish(eth_dev);
382 : 0 : return 0;
383 : : }
384 : :
385 : 0 : return fs_eth_dev_create(vdev);
386 : : }
387 : :
388 : : static int
389 [ # # ]: 0 : rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
390 : : {
391 : : const char *name;
392 : :
393 : : name = rte_vdev_device_name(vdev);
394 : 0 : INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
395 : 0 : return fs_rte_eth_free(name);
396 : : }
397 : :
398 : : static struct rte_vdev_driver failsafe_drv = {
399 : : .probe = rte_pmd_failsafe_probe,
400 : : .remove = rte_pmd_failsafe_remove,
401 : : };
402 : :
403 : 251 : RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
404 : : RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);
405 [ - + ]: 251 : RTE_LOG_REGISTER_DEFAULT(failsafe_logtype, NOTICE)
|