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