4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_ETH_FAILSAFE_PRIVATE_H_
35 #define _RTE_ETH_FAILSAFE_PRIVATE_H_
37 #include <sys/queue.h>
39 #include <rte_atomic.h>
41 #include <rte_ethdev_driver.h>
42 #include <rte_devargs.h>
43 #include <rte_interrupts.h>
45 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
46 #define FAILSAFE_OWNER_NAME "Fail-safe"
48 #define PMD_FAILSAFE_MAC_KVARG "mac"
49 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
50 #define PMD_FAILSAFE_PARAM_STRING \
52 "exec(<shell command>)," \
58 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
60 #define FAILSAFE_MAX_ETHPORTS 2
61 #define FAILSAFE_MAX_ETHADDR 128
63 #define DEVARGS_MAXLEN 4096
65 enum rxp_service_state {
75 /* epoll file descriptor */
77 /* event vector to be used by epoll */
78 struct rte_epoll_event *evec;
83 enum rxp_service_state sstate;
89 /* next sub_device to poll */
90 struct sub_device *sdev;
91 unsigned int socket_id;
93 unsigned int enable_events:1;
94 struct rte_eth_rxq_info info;
95 rte_atomic64_t refcnt[];
101 unsigned int socket_id;
102 struct rte_eth_txq_info info;
103 rte_atomic64_t refcnt[];
107 TAILQ_ENTRY(rte_flow) next;
109 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
110 /* flow description for synchronization */
111 struct rte_flow_desc *fd;
123 struct rte_eth_stats stats;
128 /* Exhaustive DPDK device description */
129 struct sub_device *next;
130 struct rte_devargs devargs;
132 struct rte_device *dev;
133 struct rte_eth_dev *edev;
135 /* Device state machine */
136 enum dev_state state;
137 /* Last stats snapshot passed to user */
138 struct fs_stats stats_snapshot;
139 /* Some device are defined as a command line */
141 /* Others are retrieved through a file descriptor */
143 /* fail-safe device backreference */
144 struct rte_eth_dev *fs_dev;
145 /* flag calling for recollection */
146 volatile unsigned int remove:1;
147 /* flow isolation state */
152 struct rte_eth_dev *dev;
154 * Set of sub_devices.
155 * subs[0] is the preferred device
156 * any other is just another slave
158 struct sub_device *subs;
159 uint8_t subs_head; /* if head == tail, no subs */
160 uint8_t subs_tail; /* first invalid */
161 uint8_t subs_tx; /* current emitting device */
162 uint8_t current_probed;
164 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
165 /* current number of mac_addr slots allocated. */
166 uint32_t nb_mac_addr;
167 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
168 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
169 /* current capabilities */
170 struct rte_eth_dev_info infos;
171 struct rte_eth_dev_owner my_owner; /* Unique owner. */
172 struct rte_intr_handle intr_handle; /* Port interrupt handle. */
174 * Fail-safe state machine.
175 * This level will be tracking state of the EAL and eth
176 * layer at large as defined by the user application.
177 * It will then steer the sub_devices toward the same
178 * synchronized state.
180 enum dev_state state;
181 struct rte_eth_stats stats_accumulator;
183 * Rx interrupts/events proxy.
184 * The PMD issues Rx events to the EAL on behalf of its subdevices,
185 * it does that by registering an event-fd for each of its queues with
186 * the EAL. A PMD service thread listens to all the Rx events from the
187 * subdevices, when an Rx event is issued by a subdevice it will be
188 * caught by this service with will trigger an Rx event in the
189 * appropriate failsafe Rx queue.
192 unsigned int pending_alarm:1; /* An alarm is pending */
193 /* flow isolation state */
199 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
200 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
201 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
202 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
206 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
207 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
211 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
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);
218 uint16_t failsafe_rx_burst_fast(void *rxq,
219 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
220 uint16_t failsafe_tx_burst_fast(void *txq,
221 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
225 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
226 void failsafe_args_free(struct rte_eth_dev *dev);
227 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
228 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
232 int failsafe_eal_init(struct rte_eth_dev *dev);
233 int failsafe_eal_uninit(struct rte_eth_dev *dev);
237 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
238 void failsafe_dev_remove(struct rte_eth_dev *dev);
239 void failsafe_stats_increment(struct rte_eth_stats *to,
240 struct rte_eth_stats *from);
241 int failsafe_eth_rmv_event_callback(uint16_t port_id,
242 enum rte_eth_event_type type,
243 void *arg, void *out);
244 int failsafe_eth_lsc_event_callback(uint16_t port_id,
245 enum rte_eth_event_type event,
246 void *cb_arg, void *out);
250 extern const char pmd_failsafe_driver_name[];
251 extern const struct eth_dev_ops failsafe_ops;
252 extern const struct rte_flow_ops fs_flow_ops;
253 extern uint64_t hotplug_poll;
254 extern int mac_from_arg;
258 /* dev: (struct rte_eth_dev *) fail-safe device */
260 ((struct fs_priv *)(dev)->data->dev_private)
262 /* sdev: (struct sub_device *) */
266 /* sdev: (struct sub_device *) */
267 #define PORT_ID(sdev) \
268 (ETH(sdev)->data->port_id)
270 /* sdev: (struct sub_device *) */
271 #define SUB_ID(sdev) \
275 * Stateful iterator construct over fail-safe sub-devices:
276 * s: (struct sub_device *), iterator
277 * i: (uint8_t), increment
278 * dev: (struct rte_eth_dev *), fail-safe ethdev
279 * state: (enum dev_state), minimum acceptable device state
281 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
282 for (s = fs_find_next((dev), 0, state, &i); \
284 s = fs_find_next((dev), i + 1, state, &i))
287 * Iterator construct over fail-safe sub-devices:
288 * s: (struct sub_device *), iterator
289 * i: (uint8_t), increment
290 * dev: (struct rte_eth_dev *), fail-safe ethdev
292 #define FOREACH_SUBDEV(s, i, dev) \
293 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
295 /* dev: (struct rte_eth_dev *) fail-safe device */
296 #define PREFERRED_SUBDEV(dev) \
297 (&PRIV(dev)->subs[0])
299 /* dev: (struct rte_eth_dev *) fail-safe device */
300 #define TX_SUBDEV(dev) \
301 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
302 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
303 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
306 * s: (struct sub_device *)
307 * ops: (struct eth_dev_ops) member
309 #define SUBOPS(s, ops) \
310 (ETH(s)->dev_ops->ops)
317 * a: (rte_atomic64_t)
319 #define FS_ATOMIC_P(a) \
320 rte_atomic64_set(&(a), 1)
323 * a: (rte_atomic64_t)
325 #define FS_ATOMIC_V(a) \
326 rte_atomic64_set(&(a), 0)
329 * s: (struct sub_device *)
332 #define FS_ATOMIC_RX(s, i) \
334 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
337 * s: (struct sub_device *)
340 #define FS_ATOMIC_TX(s, i) \
342 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
345 #define LOG__(level, m, ...) \
346 RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
347 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
348 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
349 #define INFO(...) LOG_(INFO, __VA_ARGS__)
350 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
351 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
353 /* inlined functions */
355 static inline struct sub_device *
356 fs_find_next(struct rte_eth_dev *dev,
358 enum dev_state min_state,
361 struct sub_device *subs;
364 subs = PRIV(dev)->subs;
365 tail = PRIV(dev)->subs_tail;
367 if (subs[sid].state >= min_state)
378 * Switch emitting device.
379 * If banned is set, banned must not be considered for
380 * the role of emitting device.
383 fs_switch_dev(struct rte_eth_dev *dev,
384 struct sub_device *banned)
386 struct sub_device *txd;
387 enum dev_state req_state;
389 req_state = PRIV(dev)->state;
390 txd = TX_SUBDEV(dev);
391 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
392 PREFERRED_SUBDEV(dev) != banned) {
393 if (txd != PREFERRED_SUBDEV(dev) &&
395 (req_state == DEV_STARTED) ||
396 (txd && txd->state < DEV_STARTED))) {
397 DEBUG("Switching tx_dev to preferred sub_device");
398 PRIV(dev)->subs_tx = 0;
400 } else if ((txd && txd->state < req_state) ||
403 struct sub_device *sdev = NULL;
406 /* Using acceptable device */
407 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
410 DEBUG("Switching tx_dev to sub_device %d",
412 PRIV(dev)->subs_tx = i;
415 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
416 DEBUG("No device ready, deactivating tx_dev");
417 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
422 set_burst_fn(dev, 0);
427 * Adjust error value and rte_errno to the fail-safe actual error value.
430 fs_err(struct sub_device *sdev, int err)
432 /* A device removal shouldn't be reported as an error. */
433 if (sdev->remove == 1 || err == -EIO)
434 return rte_errno = 0;
437 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */