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"
47 #define PMD_FAILSAFE_MAC_KVARG "mac"
48 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
49 #define PMD_FAILSAFE_PARAM_STRING \
51 "exec(<shell command>)," \
57 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
59 #define FAILSAFE_MAX_ETHPORTS 2
60 #define FAILSAFE_MAX_ETHADDR 128
62 #define DEVARGS_MAXLEN 4096
67 /* epoll file descriptor */
69 /* event vector to be used by epoll */
70 struct rte_epoll_event *evec;
76 /* next sub_device to poll */
77 struct sub_device *sdev;
78 unsigned int socket_id;
80 unsigned int enable_events:1;
81 struct rte_eth_rxq_info info;
82 rte_atomic64_t refcnt[];
88 unsigned int socket_id;
89 struct rte_eth_txq_info info;
90 rte_atomic64_t refcnt[];
94 TAILQ_ENTRY(rte_flow) next;
96 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
97 /* flow description for synchronization */
98 struct rte_flow_desc *fd;
110 struct rte_eth_stats stats;
115 /* Exhaustive DPDK device description */
116 struct sub_device *next;
117 struct rte_devargs devargs;
119 struct rte_device *dev;
120 struct rte_eth_dev *edev;
122 /* Device state machine */
123 enum dev_state state;
124 /* Last stats snapshot passed to user */
125 struct fs_stats stats_snapshot;
126 /* Some device are defined as a command line */
128 /* Others are retrieved through a file descriptor */
130 /* fail-safe device backreference */
131 struct rte_eth_dev *fs_dev;
132 /* flag calling for recollection */
133 volatile unsigned int remove:1;
134 /* flow isolation state */
139 struct rte_eth_dev *dev;
141 * Set of sub_devices.
142 * subs[0] is the preferred device
143 * any other is just another slave
145 struct sub_device *subs;
146 uint8_t subs_head; /* if head == tail, no subs */
147 uint8_t subs_tail; /* first invalid */
148 uint8_t subs_tx; /* current emitting device */
149 uint8_t current_probed;
151 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
152 /* current number of mac_addr slots allocated. */
153 uint32_t nb_mac_addr;
154 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
155 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
156 /* current capabilities */
157 struct rte_eth_dev_info infos;
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
175 unsigned int pending_alarm:1; /* An alarm is pending */
176 /* flow isolation state */
182 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
183 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
184 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
185 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
189 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
190 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
194 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
196 uint16_t failsafe_rx_burst(void *rxq,
197 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
198 uint16_t failsafe_tx_burst(void *txq,
199 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
201 uint16_t failsafe_rx_burst_fast(void *rxq,
202 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
203 uint16_t failsafe_tx_burst_fast(void *txq,
204 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
208 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
209 void failsafe_args_free(struct rte_eth_dev *dev);
210 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
211 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
215 int failsafe_eal_init(struct rte_eth_dev *dev);
216 int failsafe_eal_uninit(struct rte_eth_dev *dev);
220 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
221 void failsafe_dev_remove(struct rte_eth_dev *dev);
222 void failsafe_stats_increment(struct rte_eth_stats *to,
223 struct rte_eth_stats *from);
224 int failsafe_eth_rmv_event_callback(uint16_t port_id,
225 enum rte_eth_event_type type,
226 void *arg, void *out);
227 int failsafe_eth_lsc_event_callback(uint16_t port_id,
228 enum rte_eth_event_type event,
229 void *cb_arg, void *out);
233 extern const char pmd_failsafe_driver_name[];
234 extern const struct eth_dev_ops failsafe_ops;
235 extern const struct rte_flow_ops fs_flow_ops;
236 extern uint64_t hotplug_poll;
237 extern int mac_from_arg;
241 /* dev: (struct rte_eth_dev *) fail-safe device */
243 ((struct fs_priv *)(dev)->data->dev_private)
245 /* sdev: (struct sub_device *) */
249 /* sdev: (struct sub_device *) */
250 #define PORT_ID(sdev) \
251 (ETH(sdev)->data->port_id)
253 /* sdev: (struct sub_device *) */
254 #define SUB_ID(sdev) \
258 * Stateful iterator construct over fail-safe sub-devices:
259 * s: (struct sub_device *), iterator
260 * i: (uint8_t), increment
261 * dev: (struct rte_eth_dev *), fail-safe ethdev
262 * state: (enum dev_state), minimum acceptable device state
264 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
265 for (s = fs_find_next((dev), 0, state, &i); \
267 s = fs_find_next((dev), i + 1, state, &i))
270 * Iterator construct over fail-safe sub-devices:
271 * s: (struct sub_device *), iterator
272 * i: (uint8_t), increment
273 * dev: (struct rte_eth_dev *), fail-safe ethdev
275 #define FOREACH_SUBDEV(s, i, dev) \
276 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
278 /* dev: (struct rte_eth_dev *) fail-safe device */
279 #define PREFERRED_SUBDEV(dev) \
280 (&PRIV(dev)->subs[0])
282 /* dev: (struct rte_eth_dev *) fail-safe device */
283 #define TX_SUBDEV(dev) \
284 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
285 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
286 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
289 * s: (struct sub_device *)
290 * ops: (struct eth_dev_ops) member
292 #define SUBOPS(s, ops) \
293 (ETH(s)->dev_ops->ops)
300 * a: (rte_atomic64_t)
302 #define FS_ATOMIC_P(a) \
303 rte_atomic64_set(&(a), 1)
306 * a: (rte_atomic64_t)
308 #define FS_ATOMIC_V(a) \
309 rte_atomic64_set(&(a), 0)
312 * s: (struct sub_device *)
315 #define FS_ATOMIC_RX(s, i) \
317 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
320 * s: (struct sub_device *)
323 #define FS_ATOMIC_TX(s, i) \
325 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
328 #define LOG__(level, m, ...) \
329 RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
330 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
331 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
332 #define INFO(...) LOG_(INFO, __VA_ARGS__)
333 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
334 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
336 /* inlined functions */
338 static inline struct sub_device *
339 fs_find_next(struct rte_eth_dev *dev,
341 enum dev_state min_state,
344 struct sub_device *subs;
347 subs = PRIV(dev)->subs;
348 tail = PRIV(dev)->subs_tail;
350 if (subs[sid].state >= min_state)
361 * Switch emitting device.
362 * If banned is set, banned must not be considered for
363 * the role of emitting device.
366 fs_switch_dev(struct rte_eth_dev *dev,
367 struct sub_device *banned)
369 struct sub_device *txd;
370 enum dev_state req_state;
372 req_state = PRIV(dev)->state;
373 txd = TX_SUBDEV(dev);
374 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
375 PREFERRED_SUBDEV(dev) != banned) {
376 if (txd != PREFERRED_SUBDEV(dev) &&
378 (req_state == DEV_STARTED) ||
379 (txd && txd->state < DEV_STARTED))) {
380 DEBUG("Switching tx_dev to preferred sub_device");
381 PRIV(dev)->subs_tx = 0;
383 } else if ((txd && txd->state < req_state) ||
386 struct sub_device *sdev = NULL;
389 /* Using acceptable device */
390 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
393 DEBUG("Switching tx_dev to sub_device %d",
395 PRIV(dev)->subs_tx = i;
398 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
399 DEBUG("No device ready, deactivating tx_dev");
400 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
405 set_burst_fn(dev, 0);
410 * Adjust error value and rte_errno to the fail-safe actual error value.
413 fs_err(struct sub_device *sdev, int err)
415 /* A device removal shouldn't be reported as an error. */
416 if (sdev->remove == 1 || err == -EIO)
417 return rte_errno = 0;
420 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */