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 : : #ifndef _ETH_FAILSAFE_PRIVATE_H_
7 : : #define _ETH_FAILSAFE_PRIVATE_H_
8 : :
9 : : #include <stdint.h>
10 : : #include <sys/queue.h>
11 : : #include <pthread.h>
12 : :
13 : : #include <rte_stdatomic.h>
14 : : #include <dev_driver.h>
15 : : #include <ethdev_driver.h>
16 : : #include <rte_devargs.h>
17 : : #include <rte_flow.h>
18 : : #include <rte_interrupts.h>
19 : :
20 : : #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
21 : : #define FAILSAFE_OWNER_NAME "Fail-safe"
22 : :
23 : : #define PMD_FAILSAFE_MAC_KVARG "mac"
24 : : #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
25 : : #define PMD_FAILSAFE_PARAM_STRING \
26 : : "dev(<ifc>)," \
27 : : "exec(<shell command>)," \
28 : : "fd(<fd number>)," \
29 : : "mac=mac_addr," \
30 : : "hotplug_poll=u64" \
31 : : ""
32 : :
33 : : #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
34 : :
35 : : #define FAILSAFE_MAX_ETHPORTS 2
36 : : #define FAILSAFE_MAX_ETHADDR 128
37 : :
38 : : #define DEVARGS_MAXLEN 4096
39 : :
40 : : enum rxp_service_state {
41 : : SS_NO_SERVICE = 0,
42 : : SS_REGISTERED,
43 : : SS_READY,
44 : : SS_RUNNING,
45 : : };
46 : :
47 : : /* TYPES */
48 : :
49 : : struct rx_proxy {
50 : : /* epoll file descriptor */
51 : : int efd;
52 : : /* event vector to be used by epoll */
53 : : struct rte_epoll_event *evec;
54 : : /* rte service id */
55 : : uint32_t sid;
56 : : /* service core id */
57 : : uint32_t scid;
58 : : enum rxp_service_state sstate;
59 : : };
60 : :
61 : : #define FS_RX_PROXY_INIT (struct rx_proxy){ \
62 : : .efd = -1, \
63 : : .evec = NULL, \
64 : : .sid = 0, \
65 : : .scid = 0, \
66 : : .sstate = SS_NO_SERVICE, \
67 : : }
68 : :
69 : : struct rxq {
70 : : struct fs_priv *priv;
71 : : uint16_t qid;
72 : : /* next sub_device to poll */
73 : : struct sub_device *sdev;
74 : : unsigned int socket_id;
75 : : int event_fd;
76 : : unsigned int enable_events:1;
77 : : struct rte_eth_rxq_info info;
78 : : RTE_ATOMIC(uint64_t) refcnt[];
79 : : };
80 : :
81 : : struct txq {
82 : : struct fs_priv *priv;
83 : : uint16_t qid;
84 : : unsigned int socket_id;
85 : : struct rte_eth_txq_info info;
86 : : RTE_ATOMIC(uint64_t) refcnt[];
87 : : };
88 : :
89 : : struct rte_flow {
90 : : TAILQ_ENTRY(rte_flow) next;
91 : : /* sub_flows */
92 : : struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
93 : : /* flow description for synchronization */
94 : : struct rte_flow_conv_rule rule;
95 : : uint8_t rule_data[];
96 : : };
97 : :
98 : : enum dev_state {
99 : : DEV_UNDEFINED,
100 : : DEV_PARSED,
101 : : DEV_PROBED,
102 : : DEV_ACTIVE,
103 : : DEV_STARTED,
104 : : };
105 : :
106 : : struct fs_stats {
107 : : struct rte_eth_stats stats;
108 : : uint64_t timestamp;
109 : : };
110 : :
111 : : /*
112 : : * Allocated in shared memory.
113 : : */
114 : : struct sub_device {
115 : : /* Exhaustive DPDK device description */
116 : : struct sub_device *next;
117 : : struct rte_devargs devargs;
118 : : struct rte_bus *bus; /* for primary process only. */
119 : : struct rte_device *dev; /* for primary process only. */
120 : : uint8_t sid;
121 : : /* Device state machine */
122 : : enum dev_state state;
123 : : /* Last stats snapshot passed to user */
124 : : struct fs_stats stats_snapshot;
125 : : /* Some device are defined as a command line */
126 : : char *cmdline;
127 : : /* Others are retrieved through a file descriptor */
128 : : char *fd_str;
129 : : /* fail-safe device backreference */
130 : : uint16_t fs_port_id; /* shared between processes */
131 : : /* sub device port id*/
132 : : uint16_t sdev_port_id; /* shared between processes */
133 : : /* flag calling for recollection */
134 : : volatile unsigned int remove:1;
135 : : /* flow isolation state */
136 : : int flow_isolated:1;
137 : : /* RMV callback registration state */
138 : : unsigned int rmv_callback:1;
139 : : /* LSC callback registration state */
140 : : unsigned int lsc_callback:1;
141 : : /* Bus device event (removal) callback registration state */
142 : : unsigned int dev_event_callback:1;
143 : : };
144 : :
145 : : /*
146 : : * This is referenced by eth_dev->data->dev_private
147 : : * This is shared between processes.
148 : : */
149 : : struct fs_priv {
150 : : struct rte_eth_dev_data *data; /* backreference to shared data. */
151 : : /*
152 : : * Set of sub_devices.
153 : : * subs[0] is the preferred device
154 : : * any other is just another sub device
155 : : */
156 : : struct sub_device *subs; /* shared between processes */
157 : : uint8_t subs_head; /* if head == tail, no subs */
158 : : uint8_t subs_tail; /* first invalid */
159 : : uint8_t subs_tx; /* current emitting device */
160 : : uint8_t current_probed;
161 : : /* flow mapping */
162 : : TAILQ_HEAD(sub_flows, rte_flow) flow_list;
163 : : /* current number of mac_addr slots allocated. */
164 : : uint32_t nb_mac_addr;
165 : : struct rte_ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
166 : : uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
167 : : uint32_t nb_mcast_addr;
168 : : struct rte_ether_addr *mcast_addrs;
169 : : /* current capabilities */
170 : : struct rte_eth_dev_owner my_owner; /* Unique owner. */
171 : : struct rte_intr_handle *intr_handle; /* Port interrupt handle. */
172 : : /*
173 : : * Fail-safe state machine.
174 : : * This level will be tracking state of the EAL and eth
175 : : * layer at large as defined by the user application.
176 : : * It will then steer the sub_devices toward the same
177 : : * synchronized state.
178 : : */
179 : : enum dev_state state;
180 : : struct rte_eth_stats stats_accumulator;
181 : : /*
182 : : * Rx interrupts/events proxy.
183 : : * The PMD issues Rx events to the EAL on behalf of its subdevices,
184 : : * it does that by registering an event-fd for each of its queues with
185 : : * the EAL. A PMD service thread listens to all the Rx events from the
186 : : * subdevices, when an Rx event is issued by a subdevice it will be
187 : : * caught by this service with will trigger an Rx event in the
188 : : * appropriate failsafe Rx queue.
189 : : */
190 : : struct rx_proxy rxp;
191 : : pthread_mutex_t hotplug_mutex;
192 : : /* Hot-plug mutex is locked by the alarm mechanism. */
193 : : volatile unsigned int alarm_lock:1;
194 : : unsigned int pending_alarm:1; /* An alarm is pending */
195 : : /* flow isolation state */
196 : : int flow_isolated:1;
197 : : };
198 : :
199 : : /* FAILSAFE_INTR */
200 : :
201 : : int failsafe_rx_intr_install(struct rte_eth_dev *dev);
202 : : void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
203 : : int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
204 : : void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
205 : :
206 : : /* MISC */
207 : :
208 : : int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
209 : : int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
210 : :
211 : : /* RX / TX */
212 : :
213 : : uint16_t failsafe_rx_burst(void *rxq,
214 : : struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
215 : : uint16_t failsafe_tx_burst(void *txq,
216 : : struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
217 : :
218 : : /* ARGS */
219 : :
220 : : int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
221 : : void failsafe_args_free(struct rte_eth_dev *dev);
222 : : int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
223 : : int failsafe_args_parse_subs(struct rte_eth_dev *dev);
224 : :
225 : : /* EAL */
226 : :
227 : : int failsafe_eal_init(struct rte_eth_dev *dev);
228 : : int failsafe_eal_uninit(struct rte_eth_dev *dev);
229 : :
230 : : /* ETH_DEV */
231 : :
232 : : int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
233 : : void failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev);
234 : : int failsafe_eth_dev_close(struct rte_eth_dev *dev);
235 : : void failsafe_dev_remove(struct rte_eth_dev *dev);
236 : : void failsafe_stats_increment(struct rte_eth_stats *to,
237 : : struct rte_eth_stats *from);
238 : : int failsafe_eth_rmv_event_callback(uint16_t port_id,
239 : : enum rte_eth_event_type type,
240 : : void *arg, void *out);
241 : : int failsafe_eth_lsc_event_callback(uint16_t port_id,
242 : : enum rte_eth_event_type event,
243 : : void *cb_arg, void *out);
244 : : void failsafe_dev_event_callback(const char *device_name,
245 : : enum rte_dev_event_type type,
246 : : void *cb_arg);
247 : : int failsafe_eth_new_event_callback(uint16_t port_id,
248 : : enum rte_eth_event_type event,
249 : : void *cb_arg, void *out);
250 : :
251 : : /* GLOBALS */
252 : :
253 : : extern const char pmd_failsafe_driver_name[];
254 : : extern const struct eth_dev_ops failsafe_ops;
255 : : extern const struct rte_flow_ops fs_flow_ops;
256 : : extern uint64_t failsafe_hotplug_poll;
257 : : extern int failsafe_mac_from_arg;
258 : :
259 : : /* HELPERS */
260 : :
261 : : /* dev: (struct rte_eth_dev *) fail-safe device */
262 : : #define PRIV(dev) \
263 : : ((struct fs_priv *)(dev)->data->dev_private)
264 : :
265 : : /* sdev: (struct sub_device *) */
266 : : #define ETH(sdev) \
267 : : ((sdev)->sdev_port_id == RTE_MAX_ETHPORTS ? \
268 : : NULL : &rte_eth_devices[(sdev)->sdev_port_id])
269 : :
270 : : /* sdev: (struct sub_device *) */
271 : : #define PORT_ID(sdev) \
272 : : ((sdev)->sdev_port_id)
273 : :
274 : : /* sdev: (struct sub_device *) */
275 : : #define SUB_ID(sdev) \
276 : : ((sdev)->sid)
277 : :
278 : : /**
279 : : * Stateful iterator construct over fail-safe sub-devices:
280 : : * s: (struct sub_device *), iterator
281 : : * i: (uint8_t), increment
282 : : * dev: (struct rte_eth_dev *), fail-safe ethdev
283 : : * state: (enum dev_state), minimum acceptable device state
284 : : */
285 : : #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
286 : : for (s = fs_find_next((dev), 0, state, &i); \
287 : : s != NULL; \
288 : : s = fs_find_next((dev), i + 1, state, &i))
289 : :
290 : : /**
291 : : * Iterator construct over fail-safe sub-devices:
292 : : * s: (struct sub_device *), iterator
293 : : * i: (uint8_t), increment
294 : : * dev: (struct rte_eth_dev *), fail-safe ethdev
295 : : */
296 : : #define FOREACH_SUBDEV(s, i, dev) \
297 : : FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
298 : :
299 : : /* dev: (struct rte_eth_dev *) fail-safe device */
300 : : #define PREFERRED_SUBDEV(dev) \
301 : : (&PRIV(dev)->subs[0])
302 : :
303 : : /* dev: (struct rte_eth_dev *) fail-safe device */
304 : : #define TX_SUBDEV(dev) \
305 : : (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
306 : : : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
307 : : : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
308 : :
309 : : /**
310 : : * s: (struct sub_device *)
311 : : * ops: (struct eth_dev_ops) member
312 : : */
313 : : #define SUBOPS(s, ops) \
314 : : (ETH(s)->dev_ops->ops)
315 : :
316 : : /**
317 : : * Atomic guard
318 : : */
319 : :
320 : : /**
321 : : * a: RTE_ATOMIC(uint64_t)
322 : : */
323 : : #define FS_ATOMIC_P(a) \
324 : : rte_atomic_exchange_explicit(&(a), 1, rte_memory_order_acquire)
325 : :
326 : : /**
327 : : * a: RTE_ATOMIC(uint64_t)
328 : : */
329 : : #define FS_ATOMIC_V(a) \
330 : : rte_atomic_store_explicit(&(a), 0, rte_memory_order_release)
331 : :
332 : : /**
333 : : * s: (struct sub_device *)
334 : : * i: uint16_t qid
335 : : */
336 : : #define FS_ATOMIC_RX(s, i) \
337 : : rte_atomic_load_explicit( \
338 : : &((struct rxq *) \
339 : : (fs_dev(s)->data->rx_queues[i]))->refcnt[(s)->sid], \
340 : : rte_memory_order_seq_cst)
341 : :
342 : : /**
343 : : * s: (struct sub_device *)
344 : : * i: uint16_t qid
345 : : */
346 : : #define FS_ATOMIC_TX(s, i) \
347 : : rte_atomic_load_explicit( \
348 : : &((struct txq *) \
349 : : (fs_dev(s)->data->tx_queues[i]))->refcnt[(s)->sid], \
350 : : rte_memory_order_seq_cst)
351 : :
352 : : #ifdef RTE_EXEC_ENV_FREEBSD
353 : : #define FS_THREADID_TYPE void*
354 : : #define FS_THREADID_FMT "p"
355 : : #else
356 : : #define FS_THREADID_TYPE unsigned long
357 : : #define FS_THREADID_FMT "lu"
358 : : #endif
359 : :
360 : : extern int failsafe_logtype;
361 : : #define RTE_LOGTYPE_NET_FAILSAFE failsafe_logtype
362 : :
363 : : #define LOG_(l, ...) RTE_LOG_LINE(l, NET_FAILSAFE, __VA_ARGS__)
364 : : #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
365 : : #define INFO(...) LOG_(INFO, __VA_ARGS__)
366 : : #define WARN(...) LOG_(WARNING, __VA_ARGS__)
367 : : #define ERROR(...) LOG_(ERR, __VA_ARGS__)
368 : :
369 : : /* inlined functions */
370 : :
371 : : static inline struct sub_device *
372 : : fs_find_next(struct rte_eth_dev *dev,
373 : : uint8_t sid,
374 : : enum dev_state min_state,
375 : : uint8_t *sid_out)
376 : : {
377 : : struct sub_device *subs;
378 : : uint8_t tail;
379 : :
380 : 0 : subs = PRIV(dev)->subs;
381 : 0 : tail = PRIV(dev)->subs_tail;
382 [ # # # # : 0 : while (sid < tail) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
383 [ # # # # : 0 : if (subs[sid].state >= min_state)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
384 : : break;
385 : 0 : sid++;
386 : : }
387 : : *sid_out = sid;
388 [ # # # # : 0 : if (sid >= tail)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
389 : : return NULL;
390 : 0 : return &subs[sid];
391 : : }
392 : :
393 : : static inline struct rte_eth_dev *
394 : : fs_dev(struct sub_device *sdev) {
395 [ # # # # ]: 0 : return &rte_eth_devices[sdev->fs_port_id];
396 : : }
397 : :
398 : : /*
399 : : * Lock hot-plug mutex.
400 : : * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
401 : : */
402 : : static inline int
403 : 0 : fs_lock(struct rte_eth_dev *dev, unsigned int is_alarm)
404 : : {
405 : : int ret;
406 : :
407 [ # # ]: 0 : if (is_alarm) {
408 : 0 : ret = pthread_mutex_trylock(&PRIV(dev)->hotplug_mutex);
409 [ # # ]: 0 : if (ret) {
410 : 0 : DEBUG("Hot-plug mutex lock trying failed(%s), will try"
411 : : " again later...", strerror(ret));
412 : 0 : return ret;
413 : : }
414 : 0 : PRIV(dev)->alarm_lock = 1;
415 : : } else {
416 : 0 : ret = pthread_mutex_lock(&PRIV(dev)->hotplug_mutex);
417 [ # # ]: 0 : if (ret) {
418 : 0 : ERROR("Cannot lock mutex(%s)", strerror(ret));
419 : 0 : return ret;
420 : : }
421 : : }
422 : : return ret;
423 : : }
424 : :
425 : : /*
426 : : * Unlock hot-plug mutex.
427 : : * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
428 : : */
429 : : static inline void
430 : 0 : fs_unlock(struct rte_eth_dev *dev, unsigned int is_alarm)
431 : : {
432 : : int ret;
433 : :
434 [ # # ]: 0 : if (is_alarm) {
435 : : RTE_ASSERT(PRIV(dev)->alarm_lock == 1);
436 : 0 : PRIV(dev)->alarm_lock = 0;
437 : : }
438 : 0 : ret = pthread_mutex_unlock(&PRIV(dev)->hotplug_mutex);
439 [ # # ]: 0 : if (ret)
440 : 0 : ERROR("Cannot unlock hot-plug mutex(%s)", strerror(ret));
441 : 0 : }
442 : :
443 : : /*
444 : : * Switch emitting device.
445 : : * If banned is set, banned must not be considered for
446 : : * the role of emitting device.
447 : : */
448 : : static inline void
449 : 0 : fs_switch_dev(struct rte_eth_dev *dev,
450 : : struct sub_device *banned)
451 : : {
452 : : struct sub_device *txd;
453 : : enum dev_state req_state;
454 : :
455 : 0 : req_state = PRIV(dev)->state;
456 [ # # # # ]: 0 : txd = TX_SUBDEV(dev);
457 [ # # # # ]: 0 : if (PREFERRED_SUBDEV(dev)->state >= req_state &&
458 : : PREFERRED_SUBDEV(dev) != banned) {
459 [ # # ]: 0 : if (txd != PREFERRED_SUBDEV(dev) &&
460 : 0 : (txd == NULL ||
461 [ # # ]: 0 : (req_state == DEV_STARTED) ||
462 [ # # ]: 0 : (txd && txd->state < DEV_STARTED))) {
463 : 0 : DEBUG("Switching tx_dev to preferred sub_device");
464 : 0 : PRIV(dev)->subs_tx = 0;
465 : : }
466 [ # # # # ]: 0 : } else if ((txd && txd->state < req_state) ||
467 : 0 : txd == NULL ||
468 [ # # ]: 0 : txd == banned) {
469 : : struct sub_device *sdev = NULL;
470 : : uint8_t i;
471 : :
472 : : /* Using acceptable device */
473 [ # # ]: 0 : FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
474 [ # # ]: 0 : if (sdev == banned)
475 : : continue;
476 : 0 : DEBUG("Switching tx_dev to sub_device %d",
477 : : i);
478 : 0 : PRIV(dev)->subs_tx = i;
479 : 0 : break;
480 : : }
481 [ # # # # ]: 0 : if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
482 : 0 : DEBUG("No device ready, deactivating tx_dev");
483 : 0 : PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
484 : : }
485 : : } else {
486 : : return;
487 : : }
488 : : rte_wmb();
489 : : }
490 : :
491 : : /*
492 : : * Adjust error value and rte_errno to the fail-safe actual error value.
493 : : */
494 : : static inline int
495 : : fs_err(struct sub_device *sdev, int err)
496 : : {
497 : : /* A device removal shouldn't be reported as an error. */
498 [ # # # # : 0 : if (sdev->remove == 1 || err == -EIO)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
499 : 0 : return rte_errno = 0;
500 : : return err;
501 : : }
502 : : #endif /* _ETH_FAILSAFE_PRIVATE_H_ */
|