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