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 recursive - %s", strerror(ret));
148 : 0 : goto out;
149 : : }
150 : : /* Allow mutex to be shared between processes. */
151 : 0 : ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
152 [ # # ]: 0 : if (ret) {
153 : 0 : ERROR("Cannot set mutex pshared - %s", strerror(ret));
154 : 0 : goto out;
155 : : }
156 : 0 : pthread_mutex_init(&priv->hotplug_mutex, &attr);
157 : :
158 : 0 : out:
159 : 0 : pthread_mutexattr_destroy(&attr);
160 : 0 : return ret;
161 : : }
162 : :
163 : : static int
164 : 0 : fs_eth_dev_create(struct rte_vdev_device *vdev)
165 : : {
166 : : struct rte_eth_dev *dev;
167 : : struct rte_ether_addr *mac;
168 : : struct fs_priv *priv;
169 : : struct sub_device *sdev;
170 : : const char *params;
171 : : unsigned int socket_id;
172 : : uint8_t i;
173 : : int ret;
174 : :
175 : : dev = NULL;
176 : : priv = NULL;
177 : 0 : socket_id = rte_socket_id();
178 : 0 : INFO("Creating fail-safe device on NUMA socket %u", socket_id);
179 : : params = rte_vdev_device_args(vdev);
180 [ # # ]: 0 : if (params == NULL) {
181 : 0 : ERROR("This PMD requires sub-devices, none provided");
182 : 0 : return -1;
183 : : }
184 : 0 : dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
185 [ # # ]: 0 : if (dev == NULL) {
186 : 0 : ERROR("Unable to allocate rte_eth_dev");
187 : 0 : return -1;
188 : : }
189 : 0 : priv = PRIV(dev);
190 : 0 : priv->data = dev->data;
191 : 0 : priv->rxp = FS_RX_PROXY_INIT;
192 : 0 : dev->dev_ops = &failsafe_ops;
193 : 0 : dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
194 : 0 : dev->data->dev_link = eth_link;
195 : 0 : PRIV(dev)->nb_mac_addr = 1;
196 : 0 : TAILQ_INIT(&PRIV(dev)->flow_list);
197 : 0 : dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
198 : 0 : dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
199 : 0 : ret = fs_sub_device_alloc(dev, params);
200 [ # # ]: 0 : if (ret) {
201 : 0 : ERROR("Could not allocate sub_devices");
202 : 0 : goto free_dev;
203 : : }
204 : 0 : ret = failsafe_args_parse(dev, params);
205 [ # # ]: 0 : if (ret)
206 : 0 : goto free_subs;
207 : 0 : ret = rte_eth_dev_owner_new(&priv->my_owner.id);
208 [ # # ]: 0 : if (ret) {
209 : 0 : ERROR("Failed to get unique owner identifier");
210 : 0 : goto free_args;
211 : : }
212 : 0 : snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
213 : : FAILSAFE_OWNER_NAME);
214 : 0 : DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id,
215 : : priv->my_owner.name, priv->my_owner.id);
216 : 0 : ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
217 : : failsafe_eth_new_event_callback,
218 : : dev);
219 [ # # ]: 0 : if (ret) {
220 : 0 : ERROR("Failed to register NEW callback");
221 : 0 : goto free_args;
222 : : }
223 : 0 : ret = failsafe_eal_init(dev);
224 [ # # ]: 0 : if (ret)
225 : 0 : goto unregister_new_callback;
226 : 0 : ret = fs_mutex_init(priv);
227 [ # # ]: 0 : if (ret)
228 : 0 : goto unregister_new_callback;
229 : 0 : ret = failsafe_hotplug_alarm_install(dev);
230 [ # # ]: 0 : if (ret) {
231 : 0 : ERROR("Could not set up plug-in event detection");
232 : 0 : goto unregister_new_callback;
233 : : }
234 : 0 : mac = &dev->data->mac_addrs[0];
235 [ # # ]: 0 : if (failsafe_mac_from_arg) {
236 : : /*
237 : : * If MAC address was provided as a parameter,
238 : : * apply to all probed subdevices.
239 : : */
240 [ # # ]: 0 : FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
241 : 0 : ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
242 : : mac);
243 [ # # ]: 0 : if (ret) {
244 : 0 : ERROR("Failed to set default MAC address");
245 : 0 : goto cancel_alarm;
246 : : }
247 : : }
248 : : } else {
249 : : /*
250 : : * Use the ether_addr from first probed
251 : : * device, either preferred or fallback.
252 : : */
253 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, dev)
254 [ # # ]: 0 : if (sdev->state >= DEV_PROBED) {
255 : : rte_ether_addr_copy(
256 [ # # ]: 0 : Ð(sdev)->data->mac_addrs[0], mac);
257 : : break;
258 : : }
259 : : /*
260 : : * If no device has been probed and no ether_addr
261 : : * has been provided on the command line, use a random
262 : : * valid one.
263 : : * It will be applied during future state syncs to
264 : : * probed subdevices.
265 : : */
266 [ # # ]: 0 : if (i == priv->subs_tail)
267 : 0 : rte_eth_random_addr(&mac->addr_bytes[0]);
268 : : }
269 : 0 : INFO("MAC address is " RTE_ETHER_ADDR_PRT_FMT,
270 : : RTE_ETHER_ADDR_BYTES(mac));
271 : 0 : dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC |
272 : : RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
273 : :
274 : : /* Allocate interrupt instance */
275 : 0 : PRIV(dev)->intr_handle =
276 : 0 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
277 [ # # ]: 0 : if (PRIV(dev)->intr_handle == NULL) {
278 : 0 : ERROR("Failed to allocate intr handle");
279 : 0 : goto cancel_alarm;
280 : : }
281 : :
282 [ # # ]: 0 : if (rte_intr_fd_set(PRIV(dev)->intr_handle, -1))
283 : 0 : goto cancel_alarm;
284 : :
285 [ # # ]: 0 : if (rte_intr_type_set(PRIV(dev)->intr_handle, RTE_INTR_HANDLE_EXT))
286 : 0 : goto cancel_alarm;
287 : :
288 : 0 : rte_eth_dev_probing_finish(dev);
289 : :
290 : 0 : return 0;
291 : 0 : cancel_alarm:
292 : 0 : failsafe_hotplug_alarm_cancel(dev);
293 : 0 : unregister_new_callback:
294 : 0 : rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
295 : : failsafe_eth_new_event_callback, dev);
296 : 0 : free_args:
297 : 0 : failsafe_args_free(dev);
298 : 0 : free_subs:
299 : 0 : rte_free(PRIV(dev)->subs);
300 : 0 : free_dev:
301 : : /* mac_addrs must not be freed alone because part of dev_private */
302 : 0 : dev->data->mac_addrs = NULL;
303 : 0 : rte_eth_dev_release_port(dev);
304 : 0 : return -1;
305 : : }
306 : :
307 : : static int
308 : 0 : fs_rte_eth_free(const char *name)
309 : : {
310 : : struct rte_eth_dev *dev;
311 : : int ret;
312 : :
313 : 0 : dev = rte_eth_dev_allocated(name);
314 [ # # ]: 0 : if (dev == NULL)
315 : : return 0; /* port already released */
316 : 0 : ret = failsafe_eth_dev_close(dev);
317 : 0 : rte_intr_instance_free(PRIV(dev)->intr_handle);
318 : 0 : rte_eth_dev_release_port(dev);
319 : 0 : return ret;
320 : : }
321 : :
322 : : static bool
323 : 0 : devargs_already_listed(struct rte_devargs *devargs)
324 : : {
325 : : struct rte_devargs *list_da;
326 : :
327 [ # # ]: 0 : RTE_EAL_DEVARGS_FOREACH(devargs->bus->name, list_da) {
328 [ # # ]: 0 : if (strcmp(list_da->name, devargs->name) == 0)
329 : : /* devargs already in the list */
330 : : return true;
331 : : }
332 : : return false;
333 : : }
334 : :
335 : : static int
336 [ # # ]: 0 : rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
337 : : {
338 : : const char *name;
339 : : struct rte_eth_dev *eth_dev;
340 : : struct sub_device *sdev;
341 : : struct rte_devargs devargs;
342 : : uint8_t i;
343 : : int ret;
344 : :
345 : : name = rte_vdev_device_name(vdev);
346 : 0 : INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
347 : : name);
348 : :
349 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
350 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
351 [ # # ]: 0 : if (!eth_dev) {
352 : 0 : ERROR("Failed to probe %s", name);
353 : 0 : return -1;
354 : : }
355 : 0 : eth_dev->dev_ops = &failsafe_ops;
356 : 0 : eth_dev->device = &vdev->device;
357 : 0 : eth_dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
358 [ # # ]: 0 : eth_dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
359 : : /*
360 : : * Failsafe will attempt to probe all of its sub-devices.
361 : : * Any failure in sub-devices is not a fatal error.
362 : : * A sub-device can be plugged later.
363 : : */
364 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, eth_dev) {
365 : : /* skip empty devargs */
366 [ # # ]: 0 : if (sdev->devargs.name[0] == '\0')
367 : 0 : continue;
368 : :
369 : : /* rebuild devargs to be able to get the bus name. */
370 : 0 : ret = rte_devargs_parse(&devargs,
371 : 0 : sdev->devargs.name);
372 [ # # ]: 0 : if (ret != 0) {
373 : 0 : ERROR("Failed to parse devargs %s",
374 : : devargs.name);
375 : 0 : continue;
376 : : }
377 [ # # ]: 0 : if (!devargs_already_listed(&devargs)) {
378 : 0 : ret = rte_dev_probe(devargs.name);
379 [ # # ]: 0 : if (ret < 0) {
380 : 0 : ERROR("Failed to probe devargs %s",
381 : : devargs.name);
382 : 0 : continue;
383 : : }
384 : : }
385 : : }
386 : 0 : rte_eth_dev_probing_finish(eth_dev);
387 : 0 : return 0;
388 : : }
389 : :
390 : 0 : return fs_eth_dev_create(vdev);
391 : : }
392 : :
393 : : static int
394 [ # # ]: 0 : rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
395 : : {
396 : : const char *name;
397 : :
398 : : name = rte_vdev_device_name(vdev);
399 : 0 : INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
400 : 0 : return fs_rte_eth_free(name);
401 : : }
402 : :
403 : : static struct rte_vdev_driver failsafe_drv = {
404 : : .probe = rte_pmd_failsafe_probe,
405 : : .remove = rte_pmd_failsafe_remove,
406 : : };
407 : :
408 : 286 : RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
409 : : RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);
410 [ - + ]: 286 : RTE_LOG_REGISTER_DEFAULT(failsafe_logtype, NOTICE)
|