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
69 /* next sub_device to poll */
70 struct sub_device *sdev;
71 unsigned int socket_id;
73 unsigned int enable_events:1;
74 struct rte_eth_rxq_info info;
75 rte_atomic64_t refcnt[];
81 unsigned int socket_id;
82 struct rte_eth_txq_info info;
83 rte_atomic64_t refcnt[];
87 TAILQ_ENTRY(rte_flow) next;
89 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
90 /* flow description for synchronization */
91 struct rte_flow_desc *fd;
103 struct rte_eth_stats stats;
108 /* Exhaustive DPDK device description */
109 struct sub_device *next;
110 struct rte_devargs devargs;
112 struct rte_device *dev;
113 struct rte_eth_dev *edev;
115 /* Device state machine */
116 enum dev_state state;
117 /* Last stats snapshot passed to user */
118 struct fs_stats stats_snapshot;
119 /* Some device are defined as a command line */
121 /* Others are retrieved through a file descriptor */
123 /* fail-safe device backreference */
124 struct rte_eth_dev *fs_dev;
125 /* flag calling for recollection */
126 volatile unsigned int remove:1;
127 /* flow isolation state */
132 struct rte_eth_dev *dev;
134 * Set of sub_devices.
135 * subs[0] is the preferred device
136 * any other is just another slave
138 struct sub_device *subs;
139 uint8_t subs_head; /* if head == tail, no subs */
140 uint8_t subs_tail; /* first invalid */
141 uint8_t subs_tx; /* current emitting device */
142 uint8_t current_probed;
144 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
145 /* current number of mac_addr slots allocated. */
146 uint32_t nb_mac_addr;
147 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
148 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
149 /* current capabilities */
150 struct rte_eth_dev_info infos;
151 struct rte_intr_handle intr_handle; /* Port interrupt handle. */
153 * Fail-safe state machine.
154 * This level will be tracking state of the EAL and eth
155 * layer at large as defined by the user application.
156 * It will then steer the sub_devices toward the same
157 * synchronized state.
159 enum dev_state state;
160 struct rte_eth_stats stats_accumulator;
161 unsigned int pending_alarm:1; /* An alarm is pending */
162 /* flow isolation state */
168 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
169 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
173 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
174 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
178 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
180 uint16_t failsafe_rx_burst(void *rxq,
181 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
182 uint16_t failsafe_tx_burst(void *txq,
183 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
185 uint16_t failsafe_rx_burst_fast(void *rxq,
186 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
187 uint16_t failsafe_tx_burst_fast(void *txq,
188 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
192 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
193 void failsafe_args_free(struct rte_eth_dev *dev);
194 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
195 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
199 int failsafe_eal_init(struct rte_eth_dev *dev);
200 int failsafe_eal_uninit(struct rte_eth_dev *dev);
204 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
205 void failsafe_dev_remove(struct rte_eth_dev *dev);
206 void failsafe_stats_increment(struct rte_eth_stats *to,
207 struct rte_eth_stats *from);
208 int failsafe_eth_rmv_event_callback(uint16_t port_id,
209 enum rte_eth_event_type type,
210 void *arg, void *out);
211 int failsafe_eth_lsc_event_callback(uint16_t port_id,
212 enum rte_eth_event_type event,
213 void *cb_arg, void *out);
217 extern const char pmd_failsafe_driver_name[];
218 extern const struct eth_dev_ops failsafe_ops;
219 extern const struct rte_flow_ops fs_flow_ops;
220 extern uint64_t hotplug_poll;
221 extern int mac_from_arg;
225 /* dev: (struct rte_eth_dev *) fail-safe device */
227 ((struct fs_priv *)(dev)->data->dev_private)
229 /* sdev: (struct sub_device *) */
233 /* sdev: (struct sub_device *) */
234 #define PORT_ID(sdev) \
235 (ETH(sdev)->data->port_id)
237 /* sdev: (struct sub_device *) */
238 #define SUB_ID(sdev) \
242 * Stateful iterator construct over fail-safe sub-devices:
243 * s: (struct sub_device *), iterator
244 * i: (uint8_t), increment
245 * dev: (struct rte_eth_dev *), fail-safe ethdev
246 * state: (enum dev_state), minimum acceptable device state
248 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
249 for (s = fs_find_next((dev), 0, state, &i); \
251 s = fs_find_next((dev), i + 1, state, &i))
254 * Iterator construct over fail-safe sub-devices:
255 * s: (struct sub_device *), iterator
256 * i: (uint8_t), increment
257 * dev: (struct rte_eth_dev *), fail-safe ethdev
259 #define FOREACH_SUBDEV(s, i, dev) \
260 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
262 /* dev: (struct rte_eth_dev *) fail-safe device */
263 #define PREFERRED_SUBDEV(dev) \
264 (&PRIV(dev)->subs[0])
266 /* dev: (struct rte_eth_dev *) fail-safe device */
267 #define TX_SUBDEV(dev) \
268 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
269 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
270 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
273 * s: (struct sub_device *)
274 * ops: (struct eth_dev_ops) member
276 #define SUBOPS(s, ops) \
277 (ETH(s)->dev_ops->ops)
284 * a: (rte_atomic64_t)
286 #define FS_ATOMIC_P(a) \
287 rte_atomic64_set(&(a), 1)
290 * a: (rte_atomic64_t)
292 #define FS_ATOMIC_V(a) \
293 rte_atomic64_set(&(a), 0)
296 * s: (struct sub_device *)
299 #define FS_ATOMIC_RX(s, i) \
301 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
304 * s: (struct sub_device *)
307 #define FS_ATOMIC_TX(s, i) \
309 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
312 #define LOG__(level, m, ...) \
313 RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
314 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
315 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
316 #define INFO(...) LOG_(INFO, __VA_ARGS__)
317 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
318 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
320 /* inlined functions */
322 static inline struct sub_device *
323 fs_find_next(struct rte_eth_dev *dev,
325 enum dev_state min_state,
328 struct sub_device *subs;
331 subs = PRIV(dev)->subs;
332 tail = PRIV(dev)->subs_tail;
334 if (subs[sid].state >= min_state)
345 * Switch emitting device.
346 * If banned is set, banned must not be considered for
347 * the role of emitting device.
350 fs_switch_dev(struct rte_eth_dev *dev,
351 struct sub_device *banned)
353 struct sub_device *txd;
354 enum dev_state req_state;
356 req_state = PRIV(dev)->state;
357 txd = TX_SUBDEV(dev);
358 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
359 PREFERRED_SUBDEV(dev) != banned) {
360 if (txd != PREFERRED_SUBDEV(dev) &&
362 (req_state == DEV_STARTED) ||
363 (txd && txd->state < DEV_STARTED))) {
364 DEBUG("Switching tx_dev to preferred sub_device");
365 PRIV(dev)->subs_tx = 0;
367 } else if ((txd && txd->state < req_state) ||
370 struct sub_device *sdev = NULL;
373 /* Using acceptable device */
374 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
377 DEBUG("Switching tx_dev to sub_device %d",
379 PRIV(dev)->subs_tx = i;
382 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
383 DEBUG("No device ready, deactivating tx_dev");
384 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
389 set_burst_fn(dev, 0);
394 * Adjust error value and rte_errno to the fail-safe actual error value.
397 fs_err(struct sub_device *sdev, int err)
399 /* A device removal shouldn't be reported as an error. */
400 if (sdev->remove == 1 || err == -EIO)
401 return rte_errno = 0;
404 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */