xref: /dpdk/drivers/net/failsafe/failsafe.c (revision 58825f4138629d35a74011db62863c7af8e64669)
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 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 	ret = failsafe_args_count_subdevice(dev, params);
38 	if (ret)
39 		return ret;
40 	if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
41 		ERROR("Cannot allocate more than %d ports",
42 			FAILSAFE_MAX_ETHPORTS);
43 		return -ENOSPC;
44 	}
45 	nb_subs = PRIV(dev)->subs_tail;
46 	PRIV(dev)->subs = rte_zmalloc(NULL,
47 			sizeof(struct sub_device) * nb_subs,
48 			RTE_CACHE_LINE_SIZE);
49 	if (PRIV(dev)->subs == NULL) {
50 		ERROR("Could not allocate sub_devices");
51 		return -ENOMEM;
52 	}
53 	/* Initiate static sub devices linked list. */
54 	for (i = 1; i < nb_subs; i++)
55 		PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i;
56 	PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs;
57 
58 	FOREACH_SUBDEV(sdev, sdev_iterator, dev) {
59 		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 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
68 {
69 	int ret;
70 
71 	if (dev == NULL)
72 		return -EINVAL;
73 	if (PRIV(dev)->pending_alarm)
74 		return 0;
75 	ret = rte_eal_alarm_set(failsafe_hotplug_poll * 1000,
76 				fs_hotplug_alarm,
77 				dev);
78 	if (ret) {
79 		ERROR("Could not set up plug-in event detection");
80 		return ret;
81 	}
82 	PRIV(dev)->pending_alarm = 1;
83 	return 0;
84 }
85 
86 int
87 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
88 {
89 	int ret = 0;
90 
91 	rte_errno = 0;
92 	rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
93 	if (rte_errno) {
94 		ERROR("rte_eal_alarm_cancel failed (errno: %s)",
95 		      strerror(rte_errno));
96 		ret = -rte_errno;
97 	} else {
98 		PRIV(dev)->pending_alarm = 0;
99 	}
100 	return ret;
101 }
102 
103 static void
104 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 	if (!PRIV(dev)->pending_alarm)
112 		return;
113 	PRIV(dev)->pending_alarm = 0;
114 	FOREACH_SUBDEV(sdev, i, dev)
115 		if (sdev->state != PRIV(dev)->state)
116 			break;
117 	/* if we have non-probed device */
118 	if (i != PRIV(dev)->subs_tail) {
119 		if (fs_lock(dev, 1) != 0)
120 			goto reinstall;
121 		ret = failsafe_eth_dev_state_sync(dev);
122 		fs_unlock(dev, 1);
123 		if (ret)
124 			ERROR("Unable to synchronize sub_device state");
125 	}
126 	failsafe_dev_remove(dev);
127 reinstall:
128 	ret = failsafe_hotplug_alarm_install(dev);
129 	if (ret)
130 		ERROR("Unable to set up next alarm");
131 }
132 
133 static int
134 fs_mutex_init(struct fs_priv *priv)
135 {
136 	int ret;
137 	pthread_mutexattr_t attr;
138 
139 	ret = pthread_mutexattr_init(&attr);
140 	if (ret) {
141 		ERROR("Cannot initiate mutex attributes - %s", strerror(ret));
142 		return ret;
143 	}
144 	/* Allow mutex relocks for the thread holding the mutex. */
145 	ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
146 	if (ret) {
147 		ERROR("Cannot set mutex type - %s", strerror(ret));
148 		return ret;
149 	}
150 
151 	return pthread_mutex_init(&priv->hotplug_mutex, &attr);
152 }
153 
154 static int
155 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 	socket_id = rte_socket_id();
169 	INFO("Creating fail-safe device on NUMA socket %u", socket_id);
170 	params = rte_vdev_device_args(vdev);
171 	if (params == NULL) {
172 		ERROR("This PMD requires sub-devices, none provided");
173 		return -1;
174 	}
175 	dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
176 	if (dev == NULL) {
177 		ERROR("Unable to allocate rte_eth_dev");
178 		return -1;
179 	}
180 	priv = PRIV(dev);
181 	priv->data = dev->data;
182 	priv->rxp = FS_RX_PROXY_INIT;
183 	dev->dev_ops = &failsafe_ops;
184 	dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
185 	dev->data->dev_link = eth_link;
186 	PRIV(dev)->nb_mac_addr = 1;
187 	TAILQ_INIT(&PRIV(dev)->flow_list);
188 	dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
189 	dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
190 	ret = fs_sub_device_alloc(dev, params);
191 	if (ret) {
192 		ERROR("Could not allocate sub_devices");
193 		goto free_dev;
194 	}
195 	ret = failsafe_args_parse(dev, params);
196 	if (ret)
197 		goto free_subs;
198 	ret = rte_eth_dev_owner_new(&priv->my_owner.id);
199 	if (ret) {
200 		ERROR("Failed to get unique owner identifier");
201 		goto free_args;
202 	}
203 	snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
204 		 FAILSAFE_OWNER_NAME);
205 	DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id,
206 	      priv->my_owner.name, priv->my_owner.id);
207 	ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
208 					    failsafe_eth_new_event_callback,
209 					    dev);
210 	if (ret) {
211 		ERROR("Failed to register NEW callback");
212 		goto free_args;
213 	}
214 	ret = failsafe_eal_init(dev);
215 	if (ret)
216 		goto unregister_new_callback;
217 	ret = fs_mutex_init(priv);
218 	if (ret)
219 		goto unregister_new_callback;
220 	ret = failsafe_hotplug_alarm_install(dev);
221 	if (ret) {
222 		ERROR("Could not set up plug-in event detection");
223 		goto unregister_new_callback;
224 	}
225 	mac = &dev->data->mac_addrs[0];
226 	if (failsafe_mac_from_arg) {
227 		/*
228 		 * If MAC address was provided as a parameter,
229 		 * apply to all probed subdevices.
230 		 */
231 		FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
232 			ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
233 							       mac);
234 			if (ret) {
235 				ERROR("Failed to set default MAC address");
236 				goto cancel_alarm;
237 			}
238 		}
239 	} else {
240 		/*
241 		 * Use the ether_addr from first probed
242 		 * device, either preferred or fallback.
243 		 */
244 		FOREACH_SUBDEV(sdev, i, dev)
245 			if (sdev->state >= DEV_PROBED) {
246 				rte_ether_addr_copy(
247 					&ETH(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 		if (i == priv->subs_tail)
258 			rte_eth_random_addr(&mac->addr_bytes[0]);
259 	}
260 	INFO("MAC address is " RTE_ETHER_ADDR_PRT_FMT,
261 		RTE_ETHER_ADDR_BYTES(mac));
262 	dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC |
263 				RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
264 
265 	/* Allocate interrupt instance */
266 	PRIV(dev)->intr_handle =
267 		rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
268 	if (PRIV(dev)->intr_handle == NULL) {
269 		ERROR("Failed to allocate intr handle");
270 		goto cancel_alarm;
271 	}
272 
273 	if (rte_intr_fd_set(PRIV(dev)->intr_handle, -1))
274 		goto cancel_alarm;
275 
276 	if (rte_intr_type_set(PRIV(dev)->intr_handle, RTE_INTR_HANDLE_EXT))
277 		goto cancel_alarm;
278 
279 	rte_eth_dev_probing_finish(dev);
280 
281 	return 0;
282 cancel_alarm:
283 	failsafe_hotplug_alarm_cancel(dev);
284 unregister_new_callback:
285 	rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
286 					failsafe_eth_new_event_callback, dev);
287 free_args:
288 	failsafe_args_free(dev);
289 free_subs:
290 	rte_free(PRIV(dev)->subs);
291 free_dev:
292 	/* mac_addrs must not be freed alone because part of dev_private */
293 	dev->data->mac_addrs = NULL;
294 	rte_eth_dev_release_port(dev);
295 	return -1;
296 }
297 
298 static int
299 fs_rte_eth_free(const char *name)
300 {
301 	struct rte_eth_dev *dev;
302 	int ret;
303 
304 	dev = rte_eth_dev_allocated(name);
305 	if (dev == NULL)
306 		return 0; /* port already released */
307 	ret = failsafe_eth_dev_close(dev);
308 	rte_intr_instance_free(PRIV(dev)->intr_handle);
309 	rte_eth_dev_release_port(dev);
310 	return ret;
311 }
312 
313 static bool
314 devargs_already_listed(struct rte_devargs *devargs)
315 {
316 	struct rte_devargs *list_da;
317 
318 	RTE_EAL_DEVARGS_FOREACH(devargs->bus->name, list_da) {
319 		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 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 	INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
338 			name);
339 
340 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
341 		eth_dev = rte_eth_dev_attach_secondary(name);
342 		if (!eth_dev) {
343 			ERROR("Failed to probe %s", name);
344 			return -1;
345 		}
346 		eth_dev->dev_ops = &failsafe_ops;
347 		eth_dev->device = &vdev->device;
348 		eth_dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
349 		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 		FOREACH_SUBDEV(sdev, i, eth_dev) {
356 			/* skip empty devargs */
357 			if (sdev->devargs.name[0] == '\0')
358 				continue;
359 
360 			/* rebuild devargs to be able to get the bus name. */
361 			ret = rte_devargs_parse(&devargs,
362 						sdev->devargs.name);
363 			if (ret != 0) {
364 				ERROR("Failed to parse devargs %s",
365 					devargs.name);
366 				continue;
367 			}
368 			if (!devargs_already_listed(&devargs)) {
369 				ret = rte_dev_probe(devargs.name);
370 				if (ret < 0) {
371 					ERROR("Failed to probe devargs %s",
372 					      devargs.name);
373 					continue;
374 				}
375 			}
376 		}
377 		rte_eth_dev_probing_finish(eth_dev);
378 		return 0;
379 	}
380 
381 	return fs_eth_dev_create(vdev);
382 }
383 
384 static int
385 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
386 {
387 	const char *name;
388 
389 	name = rte_vdev_device_name(vdev);
390 	INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
391 	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 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
400 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);
401 RTE_LOG_REGISTER_DEFAULT(failsafe_logtype, NOTICE)
402