1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
6 #ifndef _RTE_ETH_FAILSAFE_PRIVATE_H_
7 #define _RTE_ETH_FAILSAFE_PRIVATE_H_
10 #include <sys/queue.h>
13 #include <rte_atomic.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_devargs.h>
18 #include <rte_interrupts.h>
20 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
21 #define FAILSAFE_OWNER_NAME "Fail-safe"
23 #define PMD_FAILSAFE_MAC_KVARG "mac"
24 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
25 #define PMD_FAILSAFE_PARAM_STRING \
27 "exec(<shell command>)," \
33 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
35 #define FAILSAFE_MAX_ETHPORTS 2
36 #define FAILSAFE_MAX_ETHADDR 128
38 #define DEVARGS_MAXLEN 4096
40 enum rxp_service_state {
50 /* epoll file descriptor */
52 /* event vector to be used by epoll */
53 struct rte_epoll_event *evec;
58 enum rxp_service_state sstate;
64 /* next sub_device to poll */
65 struct sub_device *sdev;
66 unsigned int socket_id;
68 unsigned int enable_events:1;
69 struct rte_eth_rxq_info info;
70 rte_atomic64_t refcnt[];
76 unsigned int socket_id;
77 struct rte_eth_txq_info info;
78 rte_atomic64_t refcnt[];
82 TAILQ_ENTRY(rte_flow) next;
84 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
85 /* flow description for synchronization */
86 struct rte_flow_conv_rule rule;
99 struct rte_eth_stats stats;
104 /* Exhaustive DPDK device description */
105 struct sub_device *next;
106 struct rte_devargs devargs;
108 struct rte_device *dev;
109 struct rte_eth_dev *edev;
111 /* Device state machine */
112 enum dev_state state;
113 /* Last stats snapshot passed to user */
114 struct fs_stats stats_snapshot;
115 /* Some device are defined as a command line */
117 /* Others are retrieved through a file descriptor */
119 /* fail-safe device backreference */
120 struct rte_eth_dev *fs_dev;
121 /* flag calling for recollection */
122 volatile unsigned int remove:1;
123 /* flow isolation state */
125 /* RMV callback registration state */
126 unsigned int rmv_callback:1;
127 /* LSC callback registration state */
128 unsigned int lsc_callback:1;
132 * This is referenced by eth_dev->data->dev_private
133 * This is shared between processes.
136 struct rte_eth_dev_data *data; /* backreference to shared data. */
138 * Set of sub_devices.
139 * subs[0] is the preferred device
140 * any other is just another slave
142 struct sub_device *subs;
143 uint8_t subs_head; /* if head == tail, no subs */
144 uint8_t subs_tail; /* first invalid */
145 uint8_t subs_tx; /* current emitting device */
146 uint8_t current_probed;
148 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
149 /* current number of mac_addr slots allocated. */
150 uint32_t nb_mac_addr;
151 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
152 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
153 uint32_t nb_mcast_addr;
154 struct ether_addr *mcast_addrs;
155 /* current capabilities */
156 struct rte_eth_dev_info infos;
157 struct rte_eth_dev_owner my_owner; /* Unique owner. */
158 struct rte_intr_handle intr_handle; /* Port interrupt handle. */
160 * Fail-safe state machine.
161 * This level will be tracking state of the EAL and eth
162 * layer at large as defined by the user application.
163 * It will then steer the sub_devices toward the same
164 * synchronized state.
166 enum dev_state state;
167 struct rte_eth_stats stats_accumulator;
169 * Rx interrupts/events proxy.
170 * The PMD issues Rx events to the EAL on behalf of its subdevices,
171 * it does that by registering an event-fd for each of its queues with
172 * the EAL. A PMD service thread listens to all the Rx events from the
173 * subdevices, when an Rx event is issued by a subdevice it will be
174 * caught by this service with will trigger an Rx event in the
175 * appropriate failsafe Rx queue.
178 pthread_mutex_t hotplug_mutex;
179 /* Hot-plug mutex is locked by the alarm mechanism. */
180 volatile unsigned int alarm_lock:1;
181 unsigned int pending_alarm:1; /* An alarm is pending */
182 /* flow isolation state */
188 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
189 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
190 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
191 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
195 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
196 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
200 void failsafe_set_burst_fn(struct rte_eth_dev *dev, int force_safe);
202 uint16_t failsafe_rx_burst(void *rxq,
203 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
204 uint16_t failsafe_tx_burst(void *txq,
205 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
207 uint16_t failsafe_rx_burst_fast(void *rxq,
208 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
209 uint16_t failsafe_tx_burst_fast(void *txq,
210 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
214 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
215 void failsafe_args_free(struct rte_eth_dev *dev);
216 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
217 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
221 int failsafe_eal_init(struct rte_eth_dev *dev);
222 int failsafe_eal_uninit(struct rte_eth_dev *dev);
226 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
227 void failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev);
228 void failsafe_dev_remove(struct rte_eth_dev *dev);
229 void failsafe_stats_increment(struct rte_eth_stats *to,
230 struct rte_eth_stats *from);
231 int failsafe_eth_rmv_event_callback(uint16_t port_id,
232 enum rte_eth_event_type type,
233 void *arg, void *out);
234 int failsafe_eth_lsc_event_callback(uint16_t port_id,
235 enum rte_eth_event_type event,
236 void *cb_arg, void *out);
237 int failsafe_eth_new_event_callback(uint16_t port_id,
238 enum rte_eth_event_type event,
239 void *cb_arg, void *out);
243 extern const char pmd_failsafe_driver_name[];
244 extern const struct eth_dev_ops failsafe_ops;
245 extern const struct rte_flow_ops fs_flow_ops;
246 extern uint64_t failsafe_hotplug_poll;
247 extern int failsafe_mac_from_arg;
251 /* dev: (struct rte_eth_dev *) fail-safe device */
253 ((struct fs_priv *)(dev)->data->dev_private)
255 /* sdev: (struct sub_device *) */
259 /* sdev: (struct sub_device *) */
260 #define PORT_ID(sdev) \
261 (ETH(sdev)->data->port_id)
263 /* sdev: (struct sub_device *) */
264 #define SUB_ID(sdev) \
268 * Stateful iterator construct over fail-safe sub-devices:
269 * s: (struct sub_device *), iterator
270 * i: (uint8_t), increment
271 * dev: (struct rte_eth_dev *), fail-safe ethdev
272 * state: (enum dev_state), minimum acceptable device state
274 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
275 for (s = fs_find_next((dev), 0, state, &i); \
277 s = fs_find_next((dev), i + 1, state, &i))
280 * Iterator construct over fail-safe sub-devices:
281 * s: (struct sub_device *), iterator
282 * i: (uint8_t), increment
283 * dev: (struct rte_eth_dev *), fail-safe ethdev
285 #define FOREACH_SUBDEV(s, i, dev) \
286 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
288 /* dev: (struct rte_eth_dev *) fail-safe device */
289 #define PREFERRED_SUBDEV(dev) \
290 (&PRIV(dev)->subs[0])
292 /* dev: (struct rte_eth_dev *) fail-safe device */
293 #define TX_SUBDEV(dev) \
294 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
295 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
296 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
299 * s: (struct sub_device *)
300 * ops: (struct eth_dev_ops) member
302 #define SUBOPS(s, ops) \
303 (ETH(s)->dev_ops->ops)
310 * a: (rte_atomic64_t)
312 #define FS_ATOMIC_P(a) \
313 rte_atomic64_set(&(a), 1)
316 * a: (rte_atomic64_t)
318 #define FS_ATOMIC_V(a) \
319 rte_atomic64_set(&(a), 0)
322 * s: (struct sub_device *)
325 #define FS_ATOMIC_RX(s, i) \
327 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
330 * s: (struct sub_device *)
333 #define FS_ATOMIC_TX(s, i) \
335 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
338 #ifdef RTE_EXEC_ENV_FREEBSD
339 #define FS_THREADID_TYPE void*
340 #define FS_THREADID_FMT "p"
342 #define FS_THREADID_TYPE unsigned long
343 #define FS_THREADID_FMT "lu"
346 extern int failsafe_logtype;
348 #define LOG__(l, m, ...) \
349 rte_log(RTE_LOG_ ## l, failsafe_logtype, \
350 "net_failsafe: " m "%c", __VA_ARGS__)
352 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
353 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
354 #define INFO(...) LOG_(INFO, __VA_ARGS__)
355 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
356 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
358 /* inlined functions */
360 static inline struct sub_device *
361 fs_find_next(struct rte_eth_dev *dev,
363 enum dev_state min_state,
366 struct sub_device *subs;
369 subs = PRIV(dev)->subs;
370 tail = PRIV(dev)->subs_tail;
372 if (subs[sid].state >= min_state)
383 * Lock hot-plug mutex.
384 * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
387 fs_lock(struct rte_eth_dev *dev, unsigned int is_alarm)
392 ret = pthread_mutex_trylock(&PRIV(dev)->hotplug_mutex);
394 DEBUG("Hot-plug mutex lock trying failed(%s), will try"
395 " again later...", strerror(ret));
398 PRIV(dev)->alarm_lock = 1;
400 ret = pthread_mutex_lock(&PRIV(dev)->hotplug_mutex);
402 ERROR("Cannot lock mutex(%s)", strerror(ret));
410 * Unlock hot-plug mutex.
411 * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
414 fs_unlock(struct rte_eth_dev *dev, unsigned int is_alarm)
419 RTE_ASSERT(PRIV(dev)->alarm_lock == 1);
420 PRIV(dev)->alarm_lock = 0;
422 ret = pthread_mutex_unlock(&PRIV(dev)->hotplug_mutex);
424 ERROR("Cannot unlock hot-plug mutex(%s)", strerror(ret));
428 * Switch emitting device.
429 * If banned is set, banned must not be considered for
430 * the role of emitting device.
433 fs_switch_dev(struct rte_eth_dev *dev,
434 struct sub_device *banned)
436 struct sub_device *txd;
437 enum dev_state req_state;
439 req_state = PRIV(dev)->state;
440 txd = TX_SUBDEV(dev);
441 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
442 PREFERRED_SUBDEV(dev) != banned) {
443 if (txd != PREFERRED_SUBDEV(dev) &&
445 (req_state == DEV_STARTED) ||
446 (txd && txd->state < DEV_STARTED))) {
447 DEBUG("Switching tx_dev to preferred sub_device");
448 PRIV(dev)->subs_tx = 0;
450 } else if ((txd && txd->state < req_state) ||
453 struct sub_device *sdev = NULL;
456 /* Using acceptable device */
457 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
460 DEBUG("Switching tx_dev to sub_device %d",
462 PRIV(dev)->subs_tx = i;
465 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
466 DEBUG("No device ready, deactivating tx_dev");
467 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
472 failsafe_set_burst_fn(dev, 0);
477 * Adjust error value and rte_errno to the fail-safe actual error value.
480 fs_err(struct sub_device *sdev, int err)
482 /* A device removal shouldn't be reported as an error. */
483 if (sdev->remove == 1 || err == -EIO)
484 return rte_errno = 0;
487 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */