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.h>
42 #include <rte_devargs.h>
44 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
46 #define PMD_FAILSAFE_MAC_KVARG "mac"
47 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
48 #define PMD_FAILSAFE_PARAM_STRING \
50 "exec(<shell command>)," \
55 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
57 #define FAILSAFE_MAX_ETHPORTS 2
58 #define FAILSAFE_MAX_ETHADDR 128
65 /* id of last sub_device polled */
67 unsigned int socket_id;
68 struct rte_eth_rxq_info info;
69 rte_atomic64_t refcnt[];
75 unsigned int socket_id;
76 struct rte_eth_txq_info info;
77 rte_atomic64_t refcnt[];
81 TAILQ_ENTRY(rte_flow) next;
83 struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
84 /* flow description for synchronization */
85 struct rte_flow_desc *fd;
97 struct rte_eth_stats stats;
102 /* Exhaustive DPDK device description */
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 /* fail-safe device backreference */
115 struct rte_eth_dev *fs_dev;
116 /* flag calling for recollection */
117 volatile unsigned int remove:1;
118 /* flow isolation state */
123 struct rte_eth_dev *dev;
125 * Set of sub_devices.
126 * subs[0] is the preferred device
127 * any other is just another slave
129 struct sub_device *subs;
130 uint8_t subs_head; /* if head == tail, no subs */
131 uint8_t subs_tail; /* first invalid */
132 uint8_t subs_tx; /* current emitting device */
133 uint8_t current_probed;
135 TAILQ_HEAD(sub_flows, rte_flow) flow_list;
136 /* current number of mac_addr slots allocated. */
137 uint32_t nb_mac_addr;
138 struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
139 uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
140 /* current capabilities */
141 struct rte_eth_dev_info infos;
143 * Fail-safe state machine.
144 * This level will be tracking state of the EAL and eth
145 * layer at large as defined by the user application.
146 * It will then steer the sub_devices toward the same
147 * synchronized state.
149 enum dev_state state;
150 struct rte_eth_stats stats_accumulator;
151 unsigned int pending_alarm:1; /* An alarm is pending */
152 /* flow isolation state */
158 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
159 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
163 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
165 uint16_t failsafe_rx_burst(void *rxq,
166 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
167 uint16_t failsafe_tx_burst(void *txq,
168 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
170 uint16_t failsafe_rx_burst_fast(void *rxq,
171 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
172 uint16_t failsafe_tx_burst_fast(void *txq,
173 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
177 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
178 void failsafe_args_free(struct rte_eth_dev *dev);
179 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
180 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
184 int failsafe_eal_init(struct rte_eth_dev *dev);
185 int failsafe_eal_uninit(struct rte_eth_dev *dev);
189 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
190 void failsafe_dev_remove(struct rte_eth_dev *dev);
191 void failsafe_stats_increment(struct rte_eth_stats *to,
192 struct rte_eth_stats *from);
193 int failsafe_eth_rmv_event_callback(uint16_t port_id,
194 enum rte_eth_event_type type,
195 void *arg, void *out);
196 int failsafe_eth_lsc_event_callback(uint16_t port_id,
197 enum rte_eth_event_type event,
198 void *cb_arg, void *out);
202 extern const char pmd_failsafe_driver_name[];
203 extern const struct eth_dev_ops failsafe_ops;
204 extern const struct rte_flow_ops fs_flow_ops;
205 extern uint64_t hotplug_poll;
206 extern int mac_from_arg;
210 /* dev: (struct rte_eth_dev *) fail-safe device */
212 ((struct fs_priv *)(dev)->data->dev_private)
214 /* sdev: (struct sub_device *) */
218 /* sdev: (struct sub_device *) */
219 #define PORT_ID(sdev) \
220 (ETH(sdev)->data->port_id)
222 /* sdev: (struct sub_device *) */
223 #define SUB_ID(sdev) \
227 * Stateful iterator construct over fail-safe sub-devices:
228 * s: (struct sub_device *), iterator
229 * i: (uint8_t), increment
230 * dev: (struct rte_eth_dev *), fail-safe ethdev
231 * state: (enum dev_state), minimum acceptable device state
233 #define FOREACH_SUBDEV_STATE(s, i, dev, state) \
234 for (s = fs_find_next((dev), 0, state, &i); \
236 s = fs_find_next((dev), i + 1, state, &i))
239 * Iterator construct over fail-safe sub-devices:
240 * s: (struct sub_device *), iterator
241 * i: (uint8_t), increment
242 * dev: (struct rte_eth_dev *), fail-safe ethdev
244 #define FOREACH_SUBDEV(s, i, dev) \
245 FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
247 /* dev: (struct rte_eth_dev *) fail-safe device */
248 #define PREFERRED_SUBDEV(dev) \
249 (&PRIV(dev)->subs[0])
251 /* dev: (struct rte_eth_dev *) fail-safe device */
252 #define TX_SUBDEV(dev) \
253 (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail ? NULL \
254 : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
255 : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
258 * s: (struct sub_device *)
259 * ops: (struct eth_dev_ops) member
261 #define SUBOPS(s, ops) \
262 (ETH(s)->dev_ops->ops)
269 * a: (rte_atomic64_t)
271 #define FS_ATOMIC_P(a) \
272 rte_atomic64_add(&(a), 1)
275 * a: (rte_atomic64_t)
277 #define FS_ATOMIC_V(a) \
278 rte_atomic64_sub(&(a), 1)
281 * s: (struct sub_device *)
284 #define FS_ATOMIC_RX(s, i) \
286 &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
289 * s: (struct sub_device *)
292 #define FS_ATOMIC_TX(s, i) \
294 &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
297 #define LOG__(level, m, ...) \
298 RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
299 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
300 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
301 #define INFO(...) LOG_(INFO, __VA_ARGS__)
302 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
303 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
305 /* inlined functions */
307 static inline struct sub_device *
308 fs_find_next(struct rte_eth_dev *dev,
310 enum dev_state min_state,
313 struct sub_device *subs;
316 subs = PRIV(dev)->subs;
317 tail = PRIV(dev)->subs_tail;
319 if (subs[sid].state >= min_state)
330 * Switch emitting device.
331 * If banned is set, banned must not be considered for
332 * the role of emitting device.
335 fs_switch_dev(struct rte_eth_dev *dev,
336 struct sub_device *banned)
338 struct sub_device *txd;
339 enum dev_state req_state;
341 req_state = PRIV(dev)->state;
342 txd = TX_SUBDEV(dev);
343 if (PREFERRED_SUBDEV(dev)->state >= req_state &&
344 PREFERRED_SUBDEV(dev) != banned) {
345 if (txd != PREFERRED_SUBDEV(dev) &&
347 (req_state == DEV_STARTED) ||
348 (txd && txd->state < DEV_STARTED))) {
349 DEBUG("Switching tx_dev to preferred sub_device");
350 PRIV(dev)->subs_tx = 0;
352 } else if ((txd && txd->state < req_state) ||
355 struct sub_device *sdev = NULL;
358 /* Using acceptable device */
359 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
362 DEBUG("Switching tx_dev to sub_device %d",
364 PRIV(dev)->subs_tx = i;
367 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
368 DEBUG("No device ready, deactivating tx_dev");
369 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
374 set_burst_fn(dev, 0);
378 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */