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_
12 #include <rte_atomic.h>
14 #include <rte_ethdev_driver.h>
15 #include <rte_devargs.h>
16 #include <rte_interrupts.h>
18 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
19 #define FAILSAFE_OWNER_NAME "Fail-safe"
21 #define PMD_FAILSAFE_MAC_KVARG "mac"
22 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
23 #define PMD_FAILSAFE_PARAM_STRING \
25 "exec(<shell command>)," \
31 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
33 #define FAILSAFE_MAX_ETHPORTS 2
34 #define FAILSAFE_MAX_ETHADDR 128
36 #define DEVARGS_MAXLEN 4096
38 enum rxp_service_state {
48 /* epoll file descriptor */
50 /* event vector to be used by epoll */
51 struct rte_epoll_event *evec;
56 enum rxp_service_state sstate;
62 /* next sub_device to poll */
63 struct sub_device *sdev;
64 unsigned int socket_id;
66 unsigned int enable_events:1;
67 struct rte_eth_rxq_info info;
68 rte_atomic64_t refcnt[];
74 unsigned int socket_id;
75 struct rte_eth_txq_info info;
76 rte_atomic64_t refcnt[];
80 TAILQ_ENTRY(rte_flow) next;
82 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
83 /* flow description for synchronization */
84 struct rte_flow_desc *fd;
96 struct rte_eth_stats stats;
101 /* Exhaustive DPDK device description */
102 struct sub_device *next;
103 struct rte_devargs devargs;
105 struct rte_device *dev;
106 struct rte_eth_dev *edev;
108 /* Device state machine */
109 enum dev_state state;
110 /* Last stats snapshot passed to user */
111 struct fs_stats stats_snapshot;
112 /* Some device are defined as a command line */
114 /* Others are retrieved through a file descriptor */
116 /* fail-safe device backreference */
117 struct rte_eth_dev *fs_dev;
118 /* flag calling for recollection */
119 volatile unsigned int remove:1;
120 /* flow isolation state */
122 /* RMV callback registration state */
123 unsigned int rmv_callback:1;
124 /* LSC callback registration state */
125 unsigned int lsc_callback:1;
129 struct rte_eth_dev *dev;
131 * Set of sub_devices.
132 * subs[0] is the preferred device
133 * any other is just another slave
135 struct sub_device *subs;
136 uint8_t subs_head; /* if head == tail, no subs */
137 uint8_t subs_tail; /* first invalid */
138 uint8_t subs_tx; /* current emitting device */
139 uint8_t current_probed;
141 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
142 /* current number of mac_addr slots allocated. */
143 uint32_t nb_mac_addr;
144 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
145 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
146 /* current capabilities */
147 struct rte_eth_dev_info infos;
148 struct rte_eth_dev_owner my_owner; /* Unique owner. */
149 struct rte_intr_handle intr_handle; /* Port interrupt handle. */
151 * Fail-safe state machine.
152 * This level will be tracking state of the EAL and eth
153 * layer at large as defined by the user application.
154 * It will then steer the sub_devices toward the same
155 * synchronized state.
157 enum dev_state state;
158 struct rte_eth_stats stats_accumulator;
160 * Rx interrupts/events proxy.
161 * The PMD issues Rx events to the EAL on behalf of its subdevices,
162 * it does that by registering an event-fd for each of its queues with
163 * the EAL. A PMD service thread listens to all the Rx events from the
164 * subdevices, when an Rx event is issued by a subdevice it will be
165 * caught by this service with will trigger an Rx event in the
166 * appropriate failsafe Rx queue.
169 pthread_mutex_t hotplug_mutex;
170 /* Hot-plug mutex is locked by the alarm mechanism. */
171 volatile unsigned int alarm_lock:1;
172 unsigned int pending_alarm:1; /* An alarm is pending */
173 /* flow isolation state */
179 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
180 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
181 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
182 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
186 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
187 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
191 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
193 uint16_t failsafe_rx_burst(void *rxq,
194 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
195 uint16_t failsafe_tx_burst(void *txq,
196 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
198 uint16_t failsafe_rx_burst_fast(void *rxq,
199 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
200 uint16_t failsafe_tx_burst_fast(void *txq,
201 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
205 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
206 void failsafe_args_free(struct rte_eth_dev *dev);
207 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
208 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
212 int failsafe_eal_init(struct rte_eth_dev *dev);
213 int failsafe_eal_uninit(struct rte_eth_dev *dev);
217 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
218 void failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev);
219 void failsafe_dev_remove(struct rte_eth_dev *dev);
220 void failsafe_stats_increment(struct rte_eth_stats *to,
221 struct rte_eth_stats *from);
222 int failsafe_eth_rmv_event_callback(uint16_t port_id,
223 enum rte_eth_event_type type,
224 void *arg, void *out);
225 int failsafe_eth_lsc_event_callback(uint16_t port_id,
226 enum rte_eth_event_type event,
227 void *cb_arg, void *out);
228 int failsafe_eth_new_event_callback(uint16_t port_id,
229 enum rte_eth_event_type event,
230 void *cb_arg, void *out);
234 extern const char pmd_failsafe_driver_name[];
235 extern const struct eth_dev_ops failsafe_ops;
236 extern const struct rte_flow_ops fs_flow_ops;
237 extern uint64_t hotplug_poll;
238 extern int mac_from_arg;
242 /* dev: (struct rte_eth_dev *) fail-safe device */
244 ((struct fs_priv *)(dev)->data->dev_private)
246 /* sdev: (struct sub_device *) */
250 /* sdev: (struct sub_device *) */
251 #define PORT_ID(sdev) \
252 (ETH(sdev)->data->port_id)
254 /* sdev: (struct sub_device *) */
255 #define SUB_ID(sdev) \
259 * Stateful iterator construct over fail-safe sub-devices:
260 * s: (struct sub_device *), iterator
261 * i: (uint8_t), increment
262 * dev: (struct rte_eth_dev *), fail-safe ethdev
263 * state: (enum dev_state), minimum acceptable device state
265 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
266 for (s = fs_find_next((dev), 0, state, &i); \
268 s = fs_find_next((dev), i + 1, state, &i))
271 * Iterator construct over fail-safe sub-devices:
272 * s: (struct sub_device *), iterator
273 * i: (uint8_t), increment
274 * dev: (struct rte_eth_dev *), fail-safe ethdev
276 #define FOREACH_SUBDEV(s, i, dev) \
277 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
279 /* dev: (struct rte_eth_dev *) fail-safe device */
280 #define PREFERRED_SUBDEV(dev) \
281 (&PRIV(dev)->subs[0])
283 /* dev: (struct rte_eth_dev *) fail-safe device */
284 #define TX_SUBDEV(dev) \
285 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
286 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
287 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
290 * s: (struct sub_device *)
291 * ops: (struct eth_dev_ops) member
293 #define SUBOPS(s, ops) \
294 (ETH(s)->dev_ops->ops)
301 * a: (rte_atomic64_t)
303 #define FS_ATOMIC_P(a) \
304 rte_atomic64_set(&(a), 1)
307 * a: (rte_atomic64_t)
309 #define FS_ATOMIC_V(a) \
310 rte_atomic64_set(&(a), 0)
313 * s: (struct sub_device *)
316 #define FS_ATOMIC_RX(s, i) \
318 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
321 * s: (struct sub_device *)
324 #define FS_ATOMIC_TX(s, i) \
326 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
329 #ifdef RTE_EXEC_ENV_BSDAPP
330 #define FS_THREADID_TYPE void*
331 #define FS_THREADID_FMT "p"
333 #define FS_THREADID_TYPE unsigned long
334 #define FS_THREADID_FMT "lu"
337 extern int failsafe_logtype;
339 #define LOG__(l, m, ...) \
340 rte_log(RTE_LOG_ ## l, failsafe_logtype, \
341 "net_failsafe: " m "%c", __VA_ARGS__)
343 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
344 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
345 #define INFO(...) LOG_(INFO, __VA_ARGS__)
346 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
347 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
349 /* inlined functions */
351 static inline struct sub_device *
352 fs_find_next(struct rte_eth_dev *dev,
354 enum dev_state min_state,
357 struct sub_device *subs;
360 subs = PRIV(dev)->subs;
361 tail = PRIV(dev)->subs_tail;
363 if (subs[sid].state >= min_state)
374 * Lock hot-plug mutex.
375 * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
378 fs_lock(struct rte_eth_dev *dev, unsigned int is_alarm)
383 ret = pthread_mutex_trylock(&PRIV(dev)->hotplug_mutex);
385 DEBUG("Hot-plug mutex lock trying failed(%s), will try"
386 " again later...", strerror(ret));
389 PRIV(dev)->alarm_lock = 1;
391 ret = pthread_mutex_lock(&PRIV(dev)->hotplug_mutex);
393 ERROR("Cannot lock mutex(%s)", strerror(ret));
397 DEBUG("Hot-plug mutex was locked by thread %" FS_THREADID_FMT "%s",
398 (FS_THREADID_TYPE)pthread_self(),
399 PRIV(dev)->alarm_lock ? " by the hot-plug alarm" : "");
404 * Unlock hot-plug mutex.
405 * is_alarm means that the caller is, for sure, the hot-plug alarm mechanism.
408 fs_unlock(struct rte_eth_dev *dev, unsigned int is_alarm)
411 unsigned int prev_alarm_lock = PRIV(dev)->alarm_lock;
414 RTE_ASSERT(PRIV(dev)->alarm_lock == 1);
415 PRIV(dev)->alarm_lock = 0;
417 ret = pthread_mutex_unlock(&PRIV(dev)->hotplug_mutex);
419 ERROR("Cannot unlock hot-plug mutex(%s)", strerror(ret));
421 DEBUG("Hot-plug mutex was unlocked by thread %" FS_THREADID_FMT "%s",
422 (FS_THREADID_TYPE)pthread_self(),
423 prev_alarm_lock ? " by the hot-plug alarm" : "");
427 * Switch emitting device.
428 * If banned is set, banned must not be considered for
429 * the role of emitting device.
432 fs_switch_dev(struct rte_eth_dev *dev,
433 struct sub_device *banned)
435 struct sub_device *txd;
436 enum dev_state req_state;
438 req_state = PRIV(dev)->state;
439 txd = TX_SUBDEV(dev);
440 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
441 PREFERRED_SUBDEV(dev) != banned) {
442 if (txd != PREFERRED_SUBDEV(dev) &&
444 (req_state == DEV_STARTED) ||
445 (txd && txd->state < DEV_STARTED))) {
446 DEBUG("Switching tx_dev to preferred sub_device");
447 PRIV(dev)->subs_tx = 0;
449 } else if ((txd && txd->state < req_state) ||
452 struct sub_device *sdev = NULL;
455 /* Using acceptable device */
456 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
459 DEBUG("Switching tx_dev to sub_device %d",
461 PRIV(dev)->subs_tx = i;
464 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
465 DEBUG("No device ready, deactivating tx_dev");
466 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
471 set_burst_fn(dev, 0);
476 * Adjust error value and rte_errno to the fail-safe actual error value.
479 fs_err(struct sub_device *sdev, int err)
481 /* A device removal shouldn't be reported as an error. */
482 if (sdev->remove == 1 || err == -EIO)
483 return rte_errno = 0;
486 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */