]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-dpdk.c
netdev-dpdk: Don't try to unregister empty vhost_id.
[mirror_ovs.git] / lib / netdev-dpdk.c
1 /*
2 * Copyright (c) 2014, 2015, 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "netdev-dpdk.h"
19
20 #include <string.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include <rte_config.h>
27 #include <rte_cycles.h>
28 #include <rte_errno.h>
29 #include <rte_eth_ring.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
32 #include <rte_mbuf.h>
33 #include <rte_meter.h>
34 #include <rte_virtio_net.h>
35
36 #include "dirs.h"
37 #include "dp-packet.h"
38 #include "dpdk.h"
39 #include "dpif-netdev.h"
40 #include "fatal-signal.h"
41 #include "netdev-provider.h"
42 #include "netdev-vport.h"
43 #include "odp-util.h"
44 #include "openvswitch/dynamic-string.h"
45 #include "openvswitch/list.h"
46 #include "openvswitch/ofp-print.h"
47 #include "openvswitch/vlog.h"
48 #include "ovs-numa.h"
49 #include "ovs-thread.h"
50 #include "ovs-rcu.h"
51 #include "packets.h"
52 #include "openvswitch/shash.h"
53 #include "smap.h"
54 #include "sset.h"
55 #include "unaligned.h"
56 #include "timeval.h"
57 #include "unixctl.h"
58
59 VLOG_DEFINE_THIS_MODULE(netdev_dpdk);
60 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
61
62 #define DPDK_PORT_WATCHDOG_INTERVAL 5
63
64 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
65 #define OVS_VPORT_DPDK "ovs_dpdk"
66
67 /*
68 * need to reserve tons of extra space in the mbufs so we can align the
69 * DMA addresses to 4KB.
70 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
71 * performance for standard Ethernet MTU.
72 */
73 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
74 + (2 * VLAN_HEADER_LEN))
75 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
76 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
77 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
78 - ETHER_HDR_LEN - ETHER_CRC_LEN)
79 #define MBUF_SIZE(mtu) (MTU_TO_MAX_FRAME_LEN(mtu) \
80 + sizeof(struct dp_packet) \
81 + RTE_PKTMBUF_HEADROOM)
82 #define NETDEV_DPDK_MBUF_ALIGN 1024
83 #define NETDEV_DPDK_MAX_PKT_LEN 9728
84
85 /* Max and min number of packets in the mempool. OVS tries to allocate a
86 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
87 * enough hugepages) we keep halving the number until the allocation succeeds
88 * or we reach MIN_NB_MBUF */
89
90 #define MAX_NB_MBUF (4096 * 64)
91 #define MIN_NB_MBUF (4096 * 4)
92 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
93
94 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
95 BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0);
96
97 /* The smallest possible NB_MBUF that we're going to try should be a multiple
98 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
99 BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF))
100 % MP_CACHE_SZ == 0);
101
102 /*
103 * DPDK XSTATS Counter names definition
104 */
105 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
106 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
107 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
108 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
109 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
110 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
111 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
112
113 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
114 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
115 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
116 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
117 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
118 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
119 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
120
121 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
122 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
123 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
124 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
125 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
126 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
127 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
128
129 #define SOCKET0 0
130
131 /* Default size of Physical NIC RXQ */
132 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
133 /* Default size of Physical NIC TXQ */
134 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
135 /* Maximum size of Physical NIC Queues */
136 #define NIC_PORT_MAX_Q_SIZE 4096
137
138 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
139 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
140 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
141 * yet mapped to another queue. */
142
143 #define VHOST_ENQ_RETRY_NUM 8
144 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
145
146 static const struct rte_eth_conf port_conf = {
147 .rxmode = {
148 .mq_mode = ETH_MQ_RX_RSS,
149 .split_hdr_size = 0,
150 .header_split = 0, /* Header Split disabled */
151 .hw_ip_checksum = 0, /* IP checksum offload disabled */
152 .hw_vlan_filter = 0, /* VLAN filtering disabled */
153 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
154 .hw_strip_crc = 0,
155 },
156 .rx_adv_conf = {
157 .rss_conf = {
158 .rss_key = NULL,
159 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
160 },
161 },
162 .txmode = {
163 .mq_mode = ETH_MQ_TX_NONE,
164 },
165 };
166
167 enum { DPDK_RING_SIZE = 256 };
168 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
169 enum { DRAIN_TSC = 200000ULL };
170
171 enum dpdk_dev_type {
172 DPDK_DEV_ETH = 0,
173 DPDK_DEV_VHOST = 1,
174 };
175
176 /* Quality of Service */
177
178 /* An instance of a QoS configuration. Always associated with a particular
179 * network device.
180 *
181 * Each QoS implementation subclasses this with whatever additional data it
182 * needs.
183 */
184 struct qos_conf {
185 const struct dpdk_qos_ops *ops;
186 rte_spinlock_t lock;
187 };
188
189 /* A particular implementation of dpdk QoS operations.
190 *
191 * The functions below return 0 if successful or a positive errno value on
192 * failure, except where otherwise noted. All of them must be provided, except
193 * where otherwise noted.
194 */
195 struct dpdk_qos_ops {
196
197 /* Name of the QoS type */
198 const char *qos_name;
199
200 /* Called to construct a qos_conf object. The implementation should make
201 * the appropriate calls to configure QoS according to 'details'.
202 *
203 * The contents of 'details' should be documented as valid for 'ovs_name'
204 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
205 * (which is built as ovs-vswitchd.conf.db(8)).
206 *
207 * This function must return 0 if and only if it sets '*conf' to an
208 * initialized 'struct qos_conf'.
209 *
210 * For all QoS implementations it should always be non-null.
211 */
212 int (*qos_construct)(const struct smap *details, struct qos_conf **conf);
213
214 /* Destroys the data structures allocated by the implementation as part of
215 * 'qos_conf'.
216 *
217 * For all QoS implementations it should always be non-null.
218 */
219 void (*qos_destruct)(struct qos_conf *conf);
220
221 /* Retrieves details of 'conf' configuration into 'details'.
222 *
223 * The contents of 'details' should be documented as valid for 'ovs_name'
224 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
225 * (which is built as ovs-vswitchd.conf.db(8)).
226 */
227 int (*qos_get)(const struct qos_conf *conf, struct smap *details);
228
229 /* Returns true if 'conf' is already configured according to 'details'.
230 *
231 * The contents of 'details' should be documented as valid for 'ovs_name'
232 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
233 * (which is built as ovs-vswitchd.conf.db(8)).
234 *
235 * For all QoS implementations it should always be non-null.
236 */
237 bool (*qos_is_equal)(const struct qos_conf *conf,
238 const struct smap *details);
239
240 /* Modify an array of rte_mbufs. The modification is specific to
241 * each qos implementation.
242 *
243 * The function should take and array of mbufs and an int representing
244 * the current number of mbufs present in the array.
245 *
246 * After the function has performed a qos modification to the array of
247 * mbufs it returns an int representing the number of mbufs now present in
248 * the array. This value is can then be passed to the port send function
249 * along with the modified array for transmission.
250 *
251 * For all QoS implementations it should always be non-null.
252 */
253 int (*qos_run)(struct qos_conf *qos_conf, struct rte_mbuf **pkts,
254 int pkt_cnt);
255 };
256
257 /* dpdk_qos_ops for each type of user space QoS implementation */
258 static const struct dpdk_qos_ops egress_policer_ops;
259
260 /*
261 * Array of dpdk_qos_ops, contains pointer to all supported QoS
262 * operations.
263 */
264 static const struct dpdk_qos_ops *const qos_confs[] = {
265 &egress_policer_ops,
266 NULL
267 };
268
269 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
270
271 /* Contains all 'struct dpdk_dev's. */
272 static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
273 = OVS_LIST_INITIALIZER(&dpdk_list);
274
275 static struct ovs_mutex dpdk_mp_mutex OVS_ACQ_AFTER(dpdk_mutex)
276 = OVS_MUTEX_INITIALIZER;
277
278 static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mp_mutex)
279 = OVS_LIST_INITIALIZER(&dpdk_mp_list);
280
281 struct dpdk_mp {
282 struct rte_mempool *mp;
283 int mtu;
284 int socket_id;
285 int refcount;
286 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mp_mutex);
287 };
288
289 /* There should be one 'struct dpdk_tx_queue' created for
290 * each cpu core. */
291 struct dpdk_tx_queue {
292 rte_spinlock_t tx_lock; /* Protects the members and the NIC queue
293 * from concurrent access. It is used only
294 * if the queue is shared among different
295 * pmd threads (see 'concurrent_txq'). */
296 int map; /* Mapping of configured vhost-user queues
297 * to enabled by guest. */
298 };
299
300 /* dpdk has no way to remove dpdk ring ethernet devices
301 so we have to keep them around once they've been created
302 */
303
304 static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
305 = OVS_LIST_INITIALIZER(&dpdk_ring_list);
306
307 struct dpdk_ring {
308 /* For the client rings */
309 struct rte_ring *cring_tx;
310 struct rte_ring *cring_rx;
311 unsigned int user_port_id; /* User given port no, parsed from port name */
312 int eth_port_id; /* ethernet device port id */
313 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
314 };
315
316 struct ingress_policer {
317 struct rte_meter_srtcm_params app_srtcm_params;
318 struct rte_meter_srtcm in_policer;
319 rte_spinlock_t policer_lock;
320 };
321
322 struct netdev_dpdk {
323 struct netdev up;
324 int port_id;
325 int max_packet_len;
326 enum dpdk_dev_type type;
327
328 struct dpdk_tx_queue *tx_q;
329
330 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
331
332 struct dpdk_mp *dpdk_mp;
333 int mtu;
334 int socket_id;
335 int buf_size;
336 struct netdev_stats stats;
337 /* Protects stats */
338 rte_spinlock_t stats_lock;
339
340 struct eth_addr hwaddr;
341 enum netdev_flags flags;
342
343 struct rte_eth_link link;
344 int link_reset_cnt;
345
346 /* virtio identifier for vhost devices */
347 ovsrcu_index vid;
348
349 /* True if vHost device is 'up' and has been reconfigured at least once */
350 bool vhost_reconfigured;
351
352 /* Identifier used to distinguish vhost devices from each other. */
353 char vhost_id[PATH_MAX];
354
355 /* In dpdk_list. */
356 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
357
358 /* QoS configuration and lock for the device */
359 OVSRCU_TYPE(struct qos_conf *) qos_conf;
360
361 /* The following properties cannot be changed when a device is running,
362 * so we remember the request and update them next time
363 * netdev_dpdk*_reconfigure() is called */
364 int requested_mtu;
365 int requested_n_txq;
366 int requested_n_rxq;
367 int requested_rxq_size;
368 int requested_txq_size;
369
370 /* Number of rx/tx descriptors for physical devices */
371 int rxq_size;
372 int txq_size;
373
374 /* Socket ID detected when vHost device is brought up */
375 int requested_socket_id;
376
377 /* Denotes whether vHost port is client/server mode */
378 uint64_t vhost_driver_flags;
379
380 /* Ingress Policer */
381 OVSRCU_TYPE(struct ingress_policer *) ingress_policer;
382 uint32_t policer_rate;
383 uint32_t policer_burst;
384
385 /* DPDK-ETH Flow control */
386 struct rte_eth_fc_conf fc_conf;
387 };
388
389 struct netdev_rxq_dpdk {
390 struct netdev_rxq up;
391 int port_id;
392 };
393
394 static int netdev_dpdk_class_init(void);
395 static int netdev_dpdk_vhost_class_init(void);
396
397 int netdev_dpdk_get_vid(const struct netdev_dpdk *dev);
398
399 struct ingress_policer *
400 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev);
401
402 static bool
403 is_dpdk_class(const struct netdev_class *class)
404 {
405 return class->init == netdev_dpdk_class_init
406 || class->init == netdev_dpdk_vhost_class_init;
407 }
408
409 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
410 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
411 * value, insufficient buffers are allocated to accomodate the packet in its
412 * entirety. Furthermore, certain drivers need to ensure that there is also
413 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
414 * frames). If the RX buffer is too small, then the driver enables scatter RX
415 * behaviour, which reduces performance. To prevent this, use a buffer size
416 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
417 */
418 static uint32_t
419 dpdk_buf_size(int mtu)
420 {
421 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) + RTE_PKTMBUF_HEADROOM),
422 NETDEV_DPDK_MBUF_ALIGN);
423 }
424
425 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
426 *
427 * Unlike xmalloc(), this function can return NULL on failure. */
428 static void *
429 dpdk_rte_mzalloc(size_t sz)
430 {
431 return rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
432 }
433
434 void
435 free_dpdk_buf(struct dp_packet *p)
436 {
437 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
438
439 rte_pktmbuf_free(pkt);
440 }
441
442 static void
443 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
444 void *opaque_arg OVS_UNUSED,
445 void *_p,
446 unsigned i OVS_UNUSED)
447 {
448 struct rte_mbuf *pkt = _p;
449
450 rte_pktmbuf_init(mp, opaque_arg, _p, i);
451
452 dp_packet_init_dpdk((struct dp_packet *) pkt, pkt->buf_len);
453 }
454
455 static struct dpdk_mp *
456 dpdk_mp_create(int socket_id, int mtu)
457 {
458 struct rte_pktmbuf_pool_private mbp_priv;
459 struct dpdk_mp *dmp;
460 unsigned mp_size;
461 char *mp_name;
462
463 dmp = dpdk_rte_mzalloc(sizeof *dmp);
464 if (!dmp) {
465 return NULL;
466 }
467 dmp->socket_id = socket_id;
468 dmp->mtu = mtu;
469 dmp->refcount = 1;
470 mbp_priv.mbuf_data_room_size = MBUF_SIZE(mtu) - sizeof(struct dp_packet);
471 mbp_priv.mbuf_priv_size = sizeof(struct dp_packet)
472 - sizeof(struct rte_mbuf);
473 /* XXX: this is a really rough method of provisioning memory.
474 * It's impossible to determine what the exact memory requirements are
475 * when the number of ports and rxqs that utilize a particular mempool can
476 * change dynamically at runtime. For now, use this rough heurisitic.
477 */
478 if (mtu >= ETHER_MTU) {
479 mp_size = MAX_NB_MBUF;
480 } else {
481 mp_size = MIN_NB_MBUF;
482 }
483
484 do {
485 mp_name = xasprintf("ovs_mp_%d_%d_%u", dmp->mtu, dmp->socket_id,
486 mp_size);
487
488 dmp->mp = rte_mempool_create(mp_name, mp_size, MBUF_SIZE(mtu),
489 MP_CACHE_SZ,
490 sizeof(struct rte_pktmbuf_pool_private),
491 rte_pktmbuf_pool_init, &mbp_priv,
492 ovs_rte_pktmbuf_init, NULL,
493 socket_id, 0);
494 if (dmp->mp) {
495 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
496 mp_name, mp_size);
497 }
498 free(mp_name);
499 if (dmp->mp) {
500 return dmp;
501 }
502 } while (rte_errno == ENOMEM && (mp_size /= 2) >= MIN_NB_MBUF);
503
504 rte_free(dmp);
505 return NULL;
506 }
507
508 static struct dpdk_mp *
509 dpdk_mp_get(int socket_id, int mtu)
510 {
511 struct dpdk_mp *dmp;
512
513 ovs_mutex_lock(&dpdk_mp_mutex);
514 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
515 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
516 dmp->refcount++;
517 goto out;
518 }
519 }
520
521 dmp = dpdk_mp_create(socket_id, mtu);
522 ovs_list_push_back(&dpdk_mp_list, &dmp->list_node);
523
524 out:
525 ovs_mutex_unlock(&dpdk_mp_mutex);
526
527 return dmp;
528 }
529
530 static void
531 dpdk_mp_put(struct dpdk_mp *dmp)
532 {
533 if (!dmp) {
534 return;
535 }
536
537 ovs_mutex_lock(&dpdk_mp_mutex);
538 ovs_assert(dmp->refcount);
539
540 if (!--dmp->refcount) {
541 ovs_list_remove(&dmp->list_node);
542 rte_mempool_free(dmp->mp);
543 rte_free(dmp);
544 }
545 ovs_mutex_unlock(&dpdk_mp_mutex);
546 }
547
548 /* Tries to allocate new mempool on requested_socket_id with
549 * mbuf size corresponding to requested_mtu.
550 * On success new configuration will be applied.
551 * On error, device will be left unchanged. */
552 static int
553 netdev_dpdk_mempool_configure(struct netdev_dpdk *dev)
554 OVS_REQUIRES(dev->mutex)
555 {
556 uint32_t buf_size = dpdk_buf_size(dev->requested_mtu);
557 struct dpdk_mp *mp;
558
559 mp = dpdk_mp_get(dev->requested_socket_id, FRAME_LEN_TO_MTU(buf_size));
560 if (!mp) {
561 VLOG_ERR("Insufficient memory to create memory pool for netdev "
562 "%s, with MTU %d on socket %d\n",
563 dev->up.name, dev->requested_mtu, dev->requested_socket_id);
564 return ENOMEM;
565 } else {
566 dpdk_mp_put(dev->dpdk_mp);
567 dev->dpdk_mp = mp;
568 dev->mtu = dev->requested_mtu;
569 dev->socket_id = dev->requested_socket_id;
570 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
571 }
572
573 return 0;
574 }
575
576 static void
577 check_link_status(struct netdev_dpdk *dev)
578 {
579 struct rte_eth_link link;
580
581 rte_eth_link_get_nowait(dev->port_id, &link);
582
583 if (dev->link.link_status != link.link_status) {
584 netdev_change_seq_changed(&dev->up);
585
586 dev->link_reset_cnt++;
587 dev->link = link;
588 if (dev->link.link_status) {
589 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
590 dev->port_id, (unsigned) dev->link.link_speed,
591 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
592 ("full-duplex") : ("half-duplex"));
593 } else {
594 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
595 }
596 }
597 }
598
599 static void *
600 dpdk_watchdog(void *dummy OVS_UNUSED)
601 {
602 struct netdev_dpdk *dev;
603
604 pthread_detach(pthread_self());
605
606 for (;;) {
607 ovs_mutex_lock(&dpdk_mutex);
608 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
609 ovs_mutex_lock(&dev->mutex);
610 if (dev->type == DPDK_DEV_ETH) {
611 check_link_status(dev);
612 }
613 ovs_mutex_unlock(&dev->mutex);
614 }
615 ovs_mutex_unlock(&dpdk_mutex);
616 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
617 }
618
619 return NULL;
620 }
621
622 static int
623 dpdk_eth_dev_queue_setup(struct netdev_dpdk *dev, int n_rxq, int n_txq)
624 {
625 int diag = 0;
626 int i;
627 struct rte_eth_conf conf = port_conf;
628
629 if (dev->mtu > ETHER_MTU) {
630 conf.rxmode.jumbo_frame = 1;
631 conf.rxmode.max_rx_pkt_len = dev->max_packet_len;
632 } else {
633 conf.rxmode.jumbo_frame = 0;
634 conf.rxmode.max_rx_pkt_len = 0;
635 }
636 /* A device may report more queues than it makes available (this has
637 * been observed for Intel xl710, which reserves some of them for
638 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
639 * available. When this happens we can retry the configuration
640 * and request less queues */
641 while (n_rxq && n_txq) {
642 if (diag) {
643 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq);
644 }
645
646 diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &conf);
647 if (diag) {
648 VLOG_WARN("Interface %s eth_dev setup error %s\n",
649 dev->up.name, rte_strerror(-diag));
650 break;
651 }
652
653 for (i = 0; i < n_txq; i++) {
654 diag = rte_eth_tx_queue_setup(dev->port_id, i, dev->txq_size,
655 dev->socket_id, NULL);
656 if (diag) {
657 VLOG_INFO("Interface %s txq(%d) setup error: %s",
658 dev->up.name, i, rte_strerror(-diag));
659 break;
660 }
661 }
662
663 if (i != n_txq) {
664 /* Retry with less tx queues */
665 n_txq = i;
666 continue;
667 }
668
669 for (i = 0; i < n_rxq; i++) {
670 diag = rte_eth_rx_queue_setup(dev->port_id, i, dev->rxq_size,
671 dev->socket_id, NULL,
672 dev->dpdk_mp->mp);
673 if (diag) {
674 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
675 dev->up.name, i, rte_strerror(-diag));
676 break;
677 }
678 }
679
680 if (i != n_rxq) {
681 /* Retry with less rx queues */
682 n_rxq = i;
683 continue;
684 }
685
686 dev->up.n_rxq = n_rxq;
687 dev->up.n_txq = n_txq;
688
689 return 0;
690 }
691
692 return diag;
693 }
694
695 static void
696 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk *dev) OVS_REQUIRES(dev->mutex)
697 {
698 if (rte_eth_dev_flow_ctrl_set(dev->port_id, &dev->fc_conf)) {
699 VLOG_WARN("Failed to enable flow control on device %d", dev->port_id);
700 }
701 }
702
703 static int
704 dpdk_eth_dev_init(struct netdev_dpdk *dev)
705 OVS_REQUIRES(dev->mutex)
706 {
707 struct rte_pktmbuf_pool_private *mbp_priv;
708 struct rte_eth_dev_info info;
709 struct ether_addr eth_addr;
710 int diag;
711 int n_rxq, n_txq;
712
713 if (!rte_eth_dev_is_valid_port(dev->port_id)) {
714 return ENODEV;
715 }
716
717 rte_eth_dev_info_get(dev->port_id, &info);
718
719 n_rxq = MIN(info.max_rx_queues, dev->up.n_rxq);
720 n_txq = MIN(info.max_tx_queues, dev->up.n_txq);
721
722 diag = dpdk_eth_dev_queue_setup(dev, n_rxq, n_txq);
723 if (diag) {
724 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
725 dev->up.name, n_rxq, n_txq, rte_strerror(-diag));
726 return -diag;
727 }
728
729 diag = rte_eth_dev_start(dev->port_id);
730 if (diag) {
731 VLOG_ERR("Interface %s start error: %s", dev->up.name,
732 rte_strerror(-diag));
733 return -diag;
734 }
735
736 rte_eth_promiscuous_enable(dev->port_id);
737 rte_eth_allmulticast_enable(dev->port_id);
738
739 memset(&eth_addr, 0x0, sizeof(eth_addr));
740 rte_eth_macaddr_get(dev->port_id, &eth_addr);
741 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
742 dev->port_id, ETH_ADDR_BYTES_ARGS(eth_addr.addr_bytes));
743
744 memcpy(dev->hwaddr.ea, eth_addr.addr_bytes, ETH_ADDR_LEN);
745 rte_eth_link_get_nowait(dev->port_id, &dev->link);
746
747 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
748 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
749
750 dev->flags = NETDEV_UP | NETDEV_PROMISC;
751
752 /* Get the Flow control configuration for DPDK-ETH */
753 diag = rte_eth_dev_flow_ctrl_get(dev->port_id, &dev->fc_conf);
754 if (diag) {
755 VLOG_DBG("cannot get flow control parameters on port=%d, err=%d",
756 dev->port_id, diag);
757 }
758
759 return 0;
760 }
761
762 static struct netdev_dpdk *
763 netdev_dpdk_cast(const struct netdev *netdev)
764 {
765 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
766 }
767
768 static struct netdev *
769 netdev_dpdk_alloc(void)
770 {
771 struct netdev_dpdk *dev;
772
773 dev = dpdk_rte_mzalloc(sizeof *dev);
774 if (dev) {
775 return &dev->up;
776 }
777
778 return NULL;
779 }
780
781 static struct dpdk_tx_queue *
782 netdev_dpdk_alloc_txq(unsigned int n_txqs)
783 {
784 struct dpdk_tx_queue *txqs;
785 unsigned i;
786
787 txqs = dpdk_rte_mzalloc(n_txqs * sizeof *txqs);
788 if (txqs) {
789 for (i = 0; i < n_txqs; i++) {
790 /* Initialize map for vhost devices. */
791 txqs[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
792 rte_spinlock_init(&txqs[i].tx_lock);
793 }
794 }
795
796 return txqs;
797 }
798
799 static int
800 netdev_dpdk_init(struct netdev *netdev, unsigned int port_no,
801 enum dpdk_dev_type type)
802 OVS_REQUIRES(dpdk_mutex)
803 {
804 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
805 int sid;
806 int err = 0;
807
808 ovs_mutex_init(&dev->mutex);
809 ovs_mutex_lock(&dev->mutex);
810
811 rte_spinlock_init(&dev->stats_lock);
812
813 /* If the 'sid' is negative, it means that the kernel fails
814 * to obtain the pci numa info. In that situation, always
815 * use 'SOCKET0'. */
816 if (type == DPDK_DEV_ETH) {
817 sid = rte_eth_dev_socket_id(port_no);
818 } else {
819 sid = rte_lcore_to_socket_id(rte_get_master_lcore());
820 }
821
822 dev->socket_id = sid < 0 ? SOCKET0 : sid;
823 dev->requested_socket_id = dev->socket_id;
824 dev->port_id = port_no;
825 dev->type = type;
826 dev->flags = 0;
827 dev->requested_mtu = dev->mtu = ETHER_MTU;
828 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
829 ovsrcu_index_init(&dev->vid, -1);
830 dev->vhost_reconfigured = false;
831
832 err = netdev_dpdk_mempool_configure(dev);
833 if (err) {
834 goto unlock;
835 }
836
837 ovsrcu_init(&dev->qos_conf, NULL);
838
839 ovsrcu_init(&dev->ingress_policer, NULL);
840 dev->policer_rate = 0;
841 dev->policer_burst = 0;
842
843 netdev->n_rxq = NR_QUEUE;
844 netdev->n_txq = NR_QUEUE;
845 dev->requested_n_rxq = netdev->n_rxq;
846 dev->requested_n_txq = netdev->n_txq;
847 dev->rxq_size = NIC_PORT_DEFAULT_RXQ_SIZE;
848 dev->txq_size = NIC_PORT_DEFAULT_TXQ_SIZE;
849 dev->requested_rxq_size = dev->rxq_size;
850 dev->requested_txq_size = dev->txq_size;
851
852 /* Initialize the flow control to NULL */
853 memset(&dev->fc_conf, 0, sizeof dev->fc_conf);
854 if (type == DPDK_DEV_ETH) {
855 err = dpdk_eth_dev_init(dev);
856 if (err) {
857 goto unlock;
858 }
859 dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq);
860 } else {
861 dev->tx_q = netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM);
862 /* Enable DPDK_DEV_VHOST device and set promiscuous mode flag. */
863 dev->flags = NETDEV_UP | NETDEV_PROMISC;
864 }
865
866 if (!dev->tx_q) {
867 err = ENOMEM;
868 goto unlock;
869 }
870
871 ovs_list_push_back(&dpdk_list, &dev->list_node);
872
873 unlock:
874 ovs_mutex_unlock(&dev->mutex);
875 return err;
876 }
877
878 /* dev_name must be the prefix followed by a positive decimal number.
879 * (no leading + or - signs are allowed) */
880 static int
881 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
882 unsigned int *port_no)
883 {
884 const char *cport;
885
886 if (strncmp(dev_name, prefix, strlen(prefix))) {
887 return ENODEV;
888 }
889
890 cport = dev_name + strlen(prefix);
891
892 if (str_to_uint(cport, 10, port_no)) {
893 return 0;
894 } else {
895 return ENODEV;
896 }
897 }
898
899 static int
900 netdev_dpdk_vhost_construct(struct netdev *netdev)
901 {
902 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
903 const char *name = netdev->name;
904 int err;
905
906 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
907 * the file system. '/' or '\' would traverse directories, so they're not
908 * acceptable in 'name'. */
909 if (strchr(name, '/') || strchr(name, '\\')) {
910 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
911 "A valid name must not include '/' or '\\'",
912 name);
913 return EINVAL;
914 }
915
916 ovs_mutex_lock(&dpdk_mutex);
917 /* Take the name of the vhost-user port and append it to the location where
918 * the socket is to be created, then register the socket.
919 */
920 snprintf(dev->vhost_id, sizeof dev->vhost_id, "%s/%s",
921 dpdk_get_vhost_sock_dir(), name);
922
923 dev->vhost_driver_flags &= ~RTE_VHOST_USER_CLIENT;
924 err = rte_vhost_driver_register(dev->vhost_id, dev->vhost_driver_flags);
925 if (err) {
926 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
927 dev->vhost_id);
928 } else {
929 fatal_signal_add_file_to_unlink(dev->vhost_id);
930 VLOG_INFO("Socket %s created for vhost-user port %s\n",
931 dev->vhost_id, name);
932 }
933 err = netdev_dpdk_init(netdev, -1, DPDK_DEV_VHOST);
934
935 ovs_mutex_unlock(&dpdk_mutex);
936 return err;
937 }
938
939 static int
940 netdev_dpdk_vhost_client_construct(struct netdev *netdev)
941 {
942 int err;
943
944 ovs_mutex_lock(&dpdk_mutex);
945 err = netdev_dpdk_init(netdev, -1, DPDK_DEV_VHOST);
946 ovs_mutex_unlock(&dpdk_mutex);
947 return err;
948 }
949
950 static int
951 netdev_dpdk_construct(struct netdev *netdev)
952 {
953 unsigned int port_no;
954 int err;
955
956 /* Names always start with "dpdk" */
957 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
958 if (err) {
959 return err;
960 }
961
962 ovs_mutex_lock(&dpdk_mutex);
963 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
964 ovs_mutex_unlock(&dpdk_mutex);
965 return err;
966 }
967
968 static void
969 netdev_dpdk_destruct(struct netdev *netdev)
970 {
971 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
972
973 ovs_mutex_lock(&dpdk_mutex);
974 ovs_mutex_lock(&dev->mutex);
975
976 rte_eth_dev_stop(dev->port_id);
977 free(ovsrcu_get_protected(struct ingress_policer *,
978 &dev->ingress_policer));
979
980 rte_free(dev->tx_q);
981 ovs_list_remove(&dev->list_node);
982 dpdk_mp_put(dev->dpdk_mp);
983
984 ovs_mutex_unlock(&dev->mutex);
985 ovs_mutex_unlock(&dpdk_mutex);
986 }
987
988 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
989 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
990 * deadlock, none of the mutexes must be held while calling this function. */
991 static int
992 dpdk_vhost_driver_unregister(struct netdev_dpdk *dev OVS_UNUSED,
993 char *vhost_id)
994 OVS_EXCLUDED(dpdk_mutex)
995 OVS_EXCLUDED(dev->mutex)
996 {
997 return rte_vhost_driver_unregister(vhost_id);
998 }
999
1000 static void
1001 netdev_dpdk_vhost_destruct(struct netdev *netdev)
1002 {
1003 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1004 char *vhost_id;
1005
1006 ovs_mutex_lock(&dpdk_mutex);
1007 ovs_mutex_lock(&dev->mutex);
1008
1009 /* Guest becomes an orphan if still attached. */
1010 if (netdev_dpdk_get_vid(dev) >= 0
1011 && !(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
1012 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1013 netdev->name);
1014 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1015 "socket '%s' must be restarted.", dev->vhost_id);
1016 }
1017
1018 free(ovsrcu_get_protected(struct ingress_policer *,
1019 &dev->ingress_policer));
1020
1021 rte_free(dev->tx_q);
1022 ovs_list_remove(&dev->list_node);
1023 dpdk_mp_put(dev->dpdk_mp);
1024
1025 vhost_id = xstrdup(dev->vhost_id);
1026
1027 ovs_mutex_unlock(&dev->mutex);
1028 ovs_mutex_unlock(&dpdk_mutex);
1029
1030 if (!strlen(dev->vhost_id)) {
1031 goto out;
1032 }
1033
1034 if (dpdk_vhost_driver_unregister(dev, vhost_id)) {
1035 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1036 netdev->name, vhost_id);
1037 } else if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
1038 /* OVS server mode - remove this socket from list for deletion */
1039 fatal_signal_remove_file_to_unlink(vhost_id);
1040 }
1041 out:
1042 free(vhost_id);
1043 }
1044
1045 static void
1046 netdev_dpdk_dealloc(struct netdev *netdev)
1047 {
1048 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1049
1050 rte_free(dev);
1051 }
1052
1053 static int
1054 netdev_dpdk_get_config(const struct netdev *netdev, struct smap *args)
1055 {
1056 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1057
1058 ovs_mutex_lock(&dev->mutex);
1059
1060 smap_add_format(args, "requested_rx_queues", "%d", dev->requested_n_rxq);
1061 smap_add_format(args, "configured_rx_queues", "%d", netdev->n_rxq);
1062 smap_add_format(args, "requested_tx_queues", "%d", dev->requested_n_txq);
1063 smap_add_format(args, "configured_tx_queues", "%d", netdev->n_txq);
1064 smap_add_format(args, "mtu", "%d", dev->mtu);
1065
1066 if (dev->type == DPDK_DEV_ETH) {
1067 smap_add_format(args, "requested_rxq_descriptors", "%d",
1068 dev->requested_rxq_size);
1069 smap_add_format(args, "configured_rxq_descriptors", "%d",
1070 dev->rxq_size);
1071 smap_add_format(args, "requested_txq_descriptors", "%d",
1072 dev->requested_txq_size);
1073 smap_add_format(args, "configured_txq_descriptors", "%d",
1074 dev->txq_size);
1075 }
1076 ovs_mutex_unlock(&dev->mutex);
1077
1078 return 0;
1079 }
1080
1081 static void
1082 dpdk_set_rxq_config(struct netdev_dpdk *dev, const struct smap *args)
1083 OVS_REQUIRES(dev->mutex)
1084 {
1085 int new_n_rxq;
1086
1087 new_n_rxq = MAX(smap_get_int(args, "n_rxq", dev->requested_n_rxq), 1);
1088 if (new_n_rxq != dev->requested_n_rxq) {
1089 dev->requested_n_rxq = new_n_rxq;
1090 netdev_request_reconfigure(&dev->up);
1091 }
1092 }
1093
1094 static void
1095 dpdk_process_queue_size(struct netdev *netdev, const struct smap *args,
1096 const char *flag, int default_size, int *new_size)
1097 {
1098 int queue_size = smap_get_int(args, flag, default_size);
1099
1100 if (queue_size <= 0 || queue_size > NIC_PORT_MAX_Q_SIZE
1101 || !is_pow2(queue_size)) {
1102 queue_size = default_size;
1103 }
1104
1105 if (queue_size != *new_size) {
1106 *new_size = queue_size;
1107 netdev_request_reconfigure(netdev);
1108 }
1109 }
1110
1111 static int
1112 netdev_dpdk_set_config(struct netdev *netdev, const struct smap *args)
1113 {
1114 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1115 bool rx_fc_en, tx_fc_en, autoneg;
1116 enum rte_eth_fc_mode fc_mode;
1117 static const enum rte_eth_fc_mode fc_mode_set[2][2] = {
1118 {RTE_FC_NONE, RTE_FC_TX_PAUSE},
1119 {RTE_FC_RX_PAUSE, RTE_FC_FULL }
1120 };
1121
1122 ovs_mutex_lock(&dev->mutex);
1123
1124 dpdk_set_rxq_config(dev, args);
1125
1126 dpdk_process_queue_size(netdev, args, "n_rxq_desc",
1127 NIC_PORT_DEFAULT_RXQ_SIZE,
1128 &dev->requested_rxq_size);
1129 dpdk_process_queue_size(netdev, args, "n_txq_desc",
1130 NIC_PORT_DEFAULT_TXQ_SIZE,
1131 &dev->requested_txq_size);
1132
1133 rx_fc_en = smap_get_bool(args, "rx-flow-ctrl", false);
1134 tx_fc_en = smap_get_bool(args, "tx-flow-ctrl", false);
1135 autoneg = smap_get_bool(args, "flow-ctrl-autoneg", false);
1136
1137 fc_mode = fc_mode_set[tx_fc_en][rx_fc_en];
1138 if (dev->fc_conf.mode != fc_mode || autoneg != dev->fc_conf.autoneg) {
1139 dev->fc_conf.mode = fc_mode;
1140 dev->fc_conf.autoneg = autoneg;
1141 dpdk_eth_flow_ctrl_setup(dev);
1142 }
1143
1144 ovs_mutex_unlock(&dev->mutex);
1145
1146 return 0;
1147 }
1148
1149 static int
1150 netdev_dpdk_ring_set_config(struct netdev *netdev, const struct smap *args)
1151 {
1152 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1153
1154 ovs_mutex_lock(&dev->mutex);
1155 dpdk_set_rxq_config(dev, args);
1156 ovs_mutex_unlock(&dev->mutex);
1157
1158 return 0;
1159 }
1160
1161 static int
1162 netdev_dpdk_vhost_client_set_config(struct netdev *netdev,
1163 const struct smap *args)
1164 {
1165 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1166 const char *path;
1167
1168 ovs_mutex_lock(&dev->mutex);
1169 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
1170 path = smap_get(args, "vhost-server-path");
1171 if (path && strcmp(path, dev->vhost_id)) {
1172 strcpy(dev->vhost_id, path);
1173 netdev_request_reconfigure(netdev);
1174 }
1175 }
1176 ovs_mutex_unlock(&dev->mutex);
1177
1178 return 0;
1179 }
1180
1181 static int
1182 netdev_dpdk_get_numa_id(const struct netdev *netdev)
1183 {
1184 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1185
1186 return dev->socket_id;
1187 }
1188
1189 /* Sets the number of tx queues for the dpdk interface. */
1190 static int
1191 netdev_dpdk_set_tx_multiq(struct netdev *netdev, unsigned int n_txq)
1192 {
1193 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1194
1195 ovs_mutex_lock(&dev->mutex);
1196
1197 if (dev->requested_n_txq == n_txq) {
1198 goto out;
1199 }
1200
1201 dev->requested_n_txq = n_txq;
1202 netdev_request_reconfigure(netdev);
1203
1204 out:
1205 ovs_mutex_unlock(&dev->mutex);
1206 return 0;
1207 }
1208
1209 static struct netdev_rxq *
1210 netdev_dpdk_rxq_alloc(void)
1211 {
1212 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
1213
1214 if (rx) {
1215 return &rx->up;
1216 }
1217
1218 return NULL;
1219 }
1220
1221 static struct netdev_rxq_dpdk *
1222 netdev_rxq_dpdk_cast(const struct netdev_rxq *rxq)
1223 {
1224 return CONTAINER_OF(rxq, struct netdev_rxq_dpdk, up);
1225 }
1226
1227 static int
1228 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq)
1229 {
1230 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1231 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1232
1233 ovs_mutex_lock(&dev->mutex);
1234 rx->port_id = dev->port_id;
1235 ovs_mutex_unlock(&dev->mutex);
1236
1237 return 0;
1238 }
1239
1240 static void
1241 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq OVS_UNUSED)
1242 {
1243 }
1244
1245 static void
1246 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq)
1247 {
1248 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1249
1250 rte_free(rx);
1251 }
1252
1253 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1254 * 'pkts', even in case of failure.
1255 *
1256 * Returns the number of packets that weren't transmitted. */
1257 static inline int
1258 netdev_dpdk_eth_tx_burst(struct netdev_dpdk *dev, int qid,
1259 struct rte_mbuf **pkts, int cnt)
1260 {
1261 uint32_t nb_tx = 0;
1262
1263 while (nb_tx != cnt) {
1264 uint32_t ret;
1265
1266 ret = rte_eth_tx_burst(dev->port_id, qid, pkts + nb_tx, cnt - nb_tx);
1267 if (!ret) {
1268 break;
1269 }
1270
1271 nb_tx += ret;
1272 }
1273
1274 if (OVS_UNLIKELY(nb_tx != cnt)) {
1275 /* Free buffers, which we couldn't transmit, one at a time (each
1276 * packet could come from a different mempool) */
1277 int i;
1278
1279 for (i = nb_tx; i < cnt; i++) {
1280 rte_pktmbuf_free(pkts[i]);
1281 }
1282 }
1283
1284 return cnt - nb_tx;
1285 }
1286
1287 static inline bool
1288 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm *meter,
1289 struct rte_mbuf *pkt, uint64_t time)
1290 {
1291 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
1292
1293 return rte_meter_srtcm_color_blind_check(meter, time, pkt_len) ==
1294 e_RTE_METER_GREEN;
1295 }
1296
1297 static int
1298 netdev_dpdk_policer_run(struct rte_meter_srtcm *meter,
1299 struct rte_mbuf **pkts, int pkt_cnt)
1300 {
1301 int i = 0;
1302 int cnt = 0;
1303 struct rte_mbuf *pkt = NULL;
1304 uint64_t current_time = rte_rdtsc();
1305
1306 for (i = 0; i < pkt_cnt; i++) {
1307 pkt = pkts[i];
1308 /* Handle current packet */
1309 if (netdev_dpdk_policer_pkt_handle(meter, pkt, current_time)) {
1310 if (cnt != i) {
1311 pkts[cnt] = pkt;
1312 }
1313 cnt++;
1314 } else {
1315 rte_pktmbuf_free(pkt);
1316 }
1317 }
1318
1319 return cnt;
1320 }
1321
1322 static int
1323 ingress_policer_run(struct ingress_policer *policer, struct rte_mbuf **pkts,
1324 int pkt_cnt)
1325 {
1326 int cnt = 0;
1327
1328 rte_spinlock_lock(&policer->policer_lock);
1329 cnt = netdev_dpdk_policer_run(&policer->in_policer, pkts, pkt_cnt);
1330 rte_spinlock_unlock(&policer->policer_lock);
1331
1332 return cnt;
1333 }
1334
1335 static bool
1336 is_vhost_running(struct netdev_dpdk *dev)
1337 {
1338 return (netdev_dpdk_get_vid(dev) >= 0 && dev->vhost_reconfigured);
1339 }
1340
1341 static inline void
1342 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats *stats,
1343 unsigned int packet_size)
1344 {
1345 /* Hard-coded search for the size bucket. */
1346 if (packet_size < 256) {
1347 if (packet_size >= 128) {
1348 stats->rx_128_to_255_packets++;
1349 } else if (packet_size <= 64) {
1350 stats->rx_1_to_64_packets++;
1351 } else {
1352 stats->rx_65_to_127_packets++;
1353 }
1354 } else {
1355 if (packet_size >= 1523) {
1356 stats->rx_1523_to_max_packets++;
1357 } else if (packet_size >= 1024) {
1358 stats->rx_1024_to_1522_packets++;
1359 } else if (packet_size < 512) {
1360 stats->rx_256_to_511_packets++;
1361 } else {
1362 stats->rx_512_to_1023_packets++;
1363 }
1364 }
1365 }
1366
1367 static inline void
1368 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats *stats,
1369 struct dp_packet **packets, int count,
1370 int dropped)
1371 {
1372 int i;
1373 unsigned int packet_size;
1374 struct dp_packet *packet;
1375
1376 stats->rx_packets += count;
1377 stats->rx_dropped += dropped;
1378 for (i = 0; i < count; i++) {
1379 packet = packets[i];
1380 packet_size = dp_packet_size(packet);
1381
1382 if (OVS_UNLIKELY(packet_size < ETH_HEADER_LEN)) {
1383 /* This only protects the following multicast counting from
1384 * too short packets, but it does not stop the packet from
1385 * further processing. */
1386 stats->rx_errors++;
1387 stats->rx_length_errors++;
1388 continue;
1389 }
1390
1391 netdev_dpdk_vhost_update_rx_size_counters(stats, packet_size);
1392
1393 struct eth_header *eh = (struct eth_header *) dp_packet_data(packet);
1394 if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) {
1395 stats->multicast++;
1396 }
1397
1398 stats->rx_bytes += packet_size;
1399 }
1400 }
1401
1402 /*
1403 * The receive path for the vhost port is the TX path out from guest.
1404 */
1405 static int
1406 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq,
1407 struct dp_packet_batch *batch)
1408 {
1409 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1410 int qid = rxq->queue_id;
1411 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
1412 uint16_t nb_rx = 0;
1413 uint16_t dropped = 0;
1414
1415 if (OVS_UNLIKELY(!is_vhost_running(dev)
1416 || !(dev->flags & NETDEV_UP))) {
1417 return EAGAIN;
1418 }
1419
1420 nb_rx = rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev),
1421 qid * VIRTIO_QNUM + VIRTIO_TXQ,
1422 dev->dpdk_mp->mp,
1423 (struct rte_mbuf **) batch->packets,
1424 NETDEV_MAX_BURST);
1425 if (!nb_rx) {
1426 return EAGAIN;
1427 }
1428
1429 if (policer) {
1430 dropped = nb_rx;
1431 nb_rx = ingress_policer_run(policer,
1432 (struct rte_mbuf **) batch->packets,
1433 nb_rx);
1434 dropped -= nb_rx;
1435 }
1436
1437 rte_spinlock_lock(&dev->stats_lock);
1438 netdev_dpdk_vhost_update_rx_counters(&dev->stats, batch->packets,
1439 nb_rx, dropped);
1440 rte_spinlock_unlock(&dev->stats_lock);
1441
1442 batch->count = (int) nb_rx;
1443 return 0;
1444 }
1445
1446 static int
1447 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch)
1448 {
1449 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1450 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1451 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
1452 int nb_rx;
1453 int dropped = 0;
1454
1455 nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id,
1456 (struct rte_mbuf **) batch->packets,
1457 NETDEV_MAX_BURST);
1458 if (!nb_rx) {
1459 return EAGAIN;
1460 }
1461
1462 if (policer) {
1463 dropped = nb_rx;
1464 nb_rx = ingress_policer_run(policer,
1465 (struct rte_mbuf **) batch->packets,
1466 nb_rx);
1467 dropped -= nb_rx;
1468 }
1469
1470 /* Update stats to reflect dropped packets */
1471 if (OVS_UNLIKELY(dropped)) {
1472 rte_spinlock_lock(&dev->stats_lock);
1473 dev->stats.rx_dropped += dropped;
1474 rte_spinlock_unlock(&dev->stats_lock);
1475 }
1476
1477 batch->count = nb_rx;
1478
1479 return 0;
1480 }
1481
1482 static inline int
1483 netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1484 int cnt)
1485 {
1486 struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf);
1487
1488 if (qos_conf) {
1489 rte_spinlock_lock(&qos_conf->lock);
1490 cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt);
1491 rte_spinlock_unlock(&qos_conf->lock);
1492 }
1493
1494 return cnt;
1495 }
1496
1497 static int
1498 netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1499 int pkt_cnt)
1500 {
1501 int i = 0;
1502 int cnt = 0;
1503 struct rte_mbuf *pkt;
1504
1505 for (i = 0; i < pkt_cnt; i++) {
1506 pkt = pkts[i];
1507 if (OVS_UNLIKELY(pkt->pkt_len > dev->max_packet_len)) {
1508 VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " max_packet_len %d",
1509 dev->up.name, pkt->pkt_len, dev->max_packet_len);
1510 rte_pktmbuf_free(pkt);
1511 continue;
1512 }
1513
1514 if (OVS_UNLIKELY(i != cnt)) {
1515 pkts[cnt] = pkt;
1516 }
1517 cnt++;
1518 }
1519
1520 return cnt;
1521 }
1522
1523 static inline void
1524 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
1525 struct dp_packet **packets,
1526 int attempted,
1527 int dropped)
1528 {
1529 int i;
1530 int sent = attempted - dropped;
1531
1532 stats->tx_packets += sent;
1533 stats->tx_dropped += dropped;
1534
1535 for (i = 0; i < sent; i++) {
1536 stats->tx_bytes += dp_packet_size(packets[i]);
1537 }
1538 }
1539
1540 static void
1541 __netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1542 struct dp_packet **pkts, int cnt)
1543 {
1544 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1545 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
1546 unsigned int total_pkts = cnt;
1547 unsigned int dropped = 0;
1548 int i, retries = 0;
1549
1550 qid = dev->tx_q[qid % netdev->n_txq].map;
1551
1552 if (OVS_UNLIKELY(!is_vhost_running(dev) || qid < 0
1553 || !(dev->flags & NETDEV_UP))) {
1554 rte_spinlock_lock(&dev->stats_lock);
1555 dev->stats.tx_dropped+= cnt;
1556 rte_spinlock_unlock(&dev->stats_lock);
1557 goto out;
1558 }
1559
1560 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1561
1562 cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt);
1563 /* Check has QoS has been configured for the netdev */
1564 cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt);
1565 dropped = total_pkts - cnt;
1566
1567 do {
1568 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
1569 unsigned int tx_pkts;
1570
1571 tx_pkts = rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev),
1572 vhost_qid, cur_pkts, cnt);
1573 if (OVS_LIKELY(tx_pkts)) {
1574 /* Packets have been sent.*/
1575 cnt -= tx_pkts;
1576 /* Prepare for possible retry.*/
1577 cur_pkts = &cur_pkts[tx_pkts];
1578 } else {
1579 /* No packets sent - do not retry.*/
1580 break;
1581 }
1582 } while (cnt && (retries++ <= VHOST_ENQ_RETRY_NUM));
1583
1584 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1585
1586 rte_spinlock_lock(&dev->stats_lock);
1587 netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts,
1588 cnt + dropped);
1589 rte_spinlock_unlock(&dev->stats_lock);
1590
1591 out:
1592 for (i = 0; i < total_pkts - dropped; i++) {
1593 dp_packet_delete(pkts[i]);
1594 }
1595 }
1596
1597 /* Tx function. Transmit packets indefinitely */
1598 static void
1599 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)
1600 OVS_NO_THREAD_SAFETY_ANALYSIS
1601 {
1602 #if !defined(__CHECKER__) && !defined(_WIN32)
1603 const size_t PKT_ARRAY_SIZE = batch->count;
1604 #else
1605 /* Sparse or MSVC doesn't like variable length array. */
1606 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
1607 #endif
1608 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1609 struct rte_mbuf *pkts[PKT_ARRAY_SIZE];
1610 int dropped = 0;
1611 int newcnt = 0;
1612 int i;
1613
1614 dp_packet_batch_apply_cutlen(batch);
1615
1616 for (i = 0; i < batch->count; i++) {
1617 int size = dp_packet_size(batch->packets[i]);
1618
1619 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1620 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1621 (int) size, dev->max_packet_len);
1622
1623 dropped++;
1624 continue;
1625 }
1626
1627 pkts[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
1628
1629 if (!pkts[newcnt]) {
1630 dropped += batch->count - i;
1631 break;
1632 }
1633
1634 /* We have to do a copy for now */
1635 memcpy(rte_pktmbuf_mtod(pkts[newcnt], void *),
1636 dp_packet_data(batch->packets[i]), size);
1637
1638 rte_pktmbuf_data_len(pkts[newcnt]) = size;
1639 rte_pktmbuf_pkt_len(pkts[newcnt]) = size;
1640
1641 newcnt++;
1642 }
1643
1644 if (dev->type == DPDK_DEV_VHOST) {
1645 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) pkts,
1646 newcnt);
1647 } else {
1648 unsigned int qos_pkts = newcnt;
1649
1650 /* Check if QoS has been configured for this netdev. */
1651 newcnt = netdev_dpdk_qos_run(dev, pkts, newcnt);
1652
1653 dropped += qos_pkts - newcnt;
1654 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, newcnt);
1655 }
1656
1657 if (OVS_UNLIKELY(dropped)) {
1658 rte_spinlock_lock(&dev->stats_lock);
1659 dev->stats.tx_dropped += dropped;
1660 rte_spinlock_unlock(&dev->stats_lock);
1661 }
1662 }
1663
1664 static int
1665 netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1666 struct dp_packet_batch *batch,
1667 bool may_steal, bool concurrent_txq OVS_UNUSED)
1668 {
1669
1670 if (OVS_UNLIKELY(!may_steal || batch->packets[0]->source != DPBUF_DPDK)) {
1671 dpdk_do_tx_copy(netdev, qid, batch);
1672 dp_packet_delete_batch(batch, may_steal);
1673 } else {
1674 dp_packet_batch_apply_cutlen(batch);
1675 __netdev_dpdk_vhost_send(netdev, qid, batch->packets, batch->count);
1676 }
1677 return 0;
1678 }
1679
1680 static inline void
1681 netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
1682 struct dp_packet_batch *batch, bool may_steal,
1683 bool concurrent_txq)
1684 {
1685 if (OVS_UNLIKELY(concurrent_txq)) {
1686 qid = qid % dev->up.n_txq;
1687 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1688 }
1689
1690 if (OVS_UNLIKELY(!may_steal ||
1691 batch->packets[0]->source != DPBUF_DPDK)) {
1692 struct netdev *netdev = &dev->up;
1693
1694 dpdk_do_tx_copy(netdev, qid, batch);
1695 dp_packet_delete_batch(batch, may_steal);
1696 } else {
1697 int dropped;
1698 int cnt = batch->count;
1699 struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets;
1700
1701 dp_packet_batch_apply_cutlen(batch);
1702
1703 cnt = netdev_dpdk_filter_packet_len(dev, pkts, cnt);
1704 cnt = netdev_dpdk_qos_run(dev, pkts, cnt);
1705 dropped = batch->count - cnt;
1706
1707 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, cnt);
1708
1709 if (OVS_UNLIKELY(dropped)) {
1710 rte_spinlock_lock(&dev->stats_lock);
1711 dev->stats.tx_dropped += dropped;
1712 rte_spinlock_unlock(&dev->stats_lock);
1713 }
1714 }
1715
1716 if (OVS_UNLIKELY(concurrent_txq)) {
1717 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1718 }
1719 }
1720
1721 static int
1722 netdev_dpdk_eth_send(struct netdev *netdev, int qid,
1723 struct dp_packet_batch *batch, bool may_steal,
1724 bool concurrent_txq)
1725 {
1726 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1727
1728 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
1729 return 0;
1730 }
1731
1732 static int
1733 netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1734 {
1735 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1736
1737 ovs_mutex_lock(&dev->mutex);
1738 if (!eth_addr_equals(dev->hwaddr, mac)) {
1739 dev->hwaddr = mac;
1740 netdev_change_seq_changed(netdev);
1741 }
1742 ovs_mutex_unlock(&dev->mutex);
1743
1744 return 0;
1745 }
1746
1747 static int
1748 netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1749 {
1750 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1751
1752 ovs_mutex_lock(&dev->mutex);
1753 *mac = dev->hwaddr;
1754 ovs_mutex_unlock(&dev->mutex);
1755
1756 return 0;
1757 }
1758
1759 static int
1760 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1761 {
1762 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1763
1764 ovs_mutex_lock(&dev->mutex);
1765 *mtup = dev->mtu;
1766 ovs_mutex_unlock(&dev->mutex);
1767
1768 return 0;
1769 }
1770
1771 static int
1772 netdev_dpdk_set_mtu(struct netdev *netdev, int mtu)
1773 {
1774 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1775
1776 if (MTU_TO_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN
1777 || mtu < ETHER_MIN_MTU) {
1778 VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu);
1779 return EINVAL;
1780 }
1781
1782 ovs_mutex_lock(&dev->mutex);
1783 if (dev->requested_mtu != mtu) {
1784 dev->requested_mtu = mtu;
1785 netdev_request_reconfigure(netdev);
1786 }
1787 ovs_mutex_unlock(&dev->mutex);
1788
1789 return 0;
1790 }
1791
1792 static int
1793 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier);
1794
1795 static int
1796 netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
1797 struct netdev_stats *stats)
1798 {
1799 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1800
1801 ovs_mutex_lock(&dev->mutex);
1802
1803 rte_spinlock_lock(&dev->stats_lock);
1804 /* Supported Stats */
1805 stats->rx_packets += dev->stats.rx_packets;
1806 stats->tx_packets += dev->stats.tx_packets;
1807 stats->rx_dropped = dev->stats.rx_dropped;
1808 stats->tx_dropped += dev->stats.tx_dropped;
1809 stats->multicast = dev->stats.multicast;
1810 stats->rx_bytes = dev->stats.rx_bytes;
1811 stats->tx_bytes = dev->stats.tx_bytes;
1812 stats->rx_errors = dev->stats.rx_errors;
1813 stats->rx_length_errors = dev->stats.rx_length_errors;
1814
1815 stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets;
1816 stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets;
1817 stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets;
1818 stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets;
1819 stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets;
1820 stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets;
1821 stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets;
1822
1823 rte_spinlock_unlock(&dev->stats_lock);
1824
1825 ovs_mutex_unlock(&dev->mutex);
1826
1827 return 0;
1828 }
1829
1830 static void
1831 netdev_dpdk_convert_xstats(struct netdev_stats *stats,
1832 const struct rte_eth_xstat *xstats,
1833 const struct rte_eth_xstat_name *names,
1834 const unsigned int size)
1835 {
1836 for (unsigned int i = 0; i < size; i++) {
1837 if (strcmp(XSTAT_RX_64_PACKETS, names[i].name) == 0) {
1838 stats->rx_1_to_64_packets = xstats[i].value;
1839 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS, names[i].name) == 0) {
1840 stats->rx_65_to_127_packets = xstats[i].value;
1841 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS, names[i].name) == 0) {
1842 stats->rx_128_to_255_packets = xstats[i].value;
1843 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS, names[i].name) == 0) {
1844 stats->rx_256_to_511_packets = xstats[i].value;
1845 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS, names[i].name) == 0) {
1846 stats->rx_512_to_1023_packets = xstats[i].value;
1847 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS, names[i].name) == 0) {
1848 stats->rx_1024_to_1522_packets = xstats[i].value;
1849 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
1850 stats->rx_1523_to_max_packets = xstats[i].value;
1851 } else if (strcmp(XSTAT_TX_64_PACKETS, names[i].name) == 0) {
1852 stats->tx_1_to_64_packets = xstats[i].value;
1853 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS, names[i].name) == 0) {
1854 stats->tx_65_to_127_packets = xstats[i].value;
1855 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS, names[i].name) == 0) {
1856 stats->tx_128_to_255_packets = xstats[i].value;
1857 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS, names[i].name) == 0) {
1858 stats->tx_256_to_511_packets = xstats[i].value;
1859 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS, names[i].name) == 0) {
1860 stats->tx_512_to_1023_packets = xstats[i].value;
1861 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS, names[i].name) == 0) {
1862 stats->tx_1024_to_1522_packets = xstats[i].value;
1863 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
1864 stats->tx_1523_to_max_packets = xstats[i].value;
1865 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS, names[i].name) == 0) {
1866 stats->tx_multicast_packets = xstats[i].value;
1867 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS, names[i].name) == 0) {
1868 stats->rx_broadcast_packets = xstats[i].value;
1869 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS, names[i].name) == 0) {
1870 stats->tx_broadcast_packets = xstats[i].value;
1871 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS, names[i].name) == 0) {
1872 stats->rx_undersized_errors = xstats[i].value;
1873 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS, names[i].name) == 0) {
1874 stats->rx_fragmented_errors = xstats[i].value;
1875 } else if (strcmp(XSTAT_RX_JABBER_ERRORS, names[i].name) == 0) {
1876 stats->rx_jabber_errors = xstats[i].value;
1877 }
1878 }
1879 }
1880
1881 static int
1882 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1883 {
1884 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1885 struct rte_eth_stats rte_stats;
1886 bool gg;
1887
1888 netdev_dpdk_get_carrier(netdev, &gg);
1889 ovs_mutex_lock(&dev->mutex);
1890
1891 struct rte_eth_xstat *rte_xstats = NULL;
1892 struct rte_eth_xstat_name *rte_xstats_names = NULL;
1893 int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret;
1894
1895 if (rte_eth_stats_get(dev->port_id, &rte_stats)) {
1896 VLOG_ERR("Can't get ETH statistics for port: %i.", dev->port_id);
1897 ovs_mutex_unlock(&dev->mutex);
1898 return EPROTO;
1899 }
1900
1901 /* Get length of statistics */
1902 rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0);
1903 if (rte_xstats_len < 0) {
1904 VLOG_WARN("Cannot get XSTATS values for port: %i", dev->port_id);
1905 goto out;
1906 }
1907 /* Reserve memory for xstats names and values */
1908 rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names);
1909 rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats);
1910
1911 /* Retreive xstats names */
1912 rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id,
1913 rte_xstats_names,
1914 rte_xstats_len);
1915 if (rte_xstats_new_len != rte_xstats_len) {
1916 VLOG_WARN("Cannot get XSTATS names for port: %i.", dev->port_id);
1917 goto out;
1918 }
1919 /* Retreive xstats values */
1920 memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len);
1921 rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats,
1922 rte_xstats_len);
1923 if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) {
1924 netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names,
1925 rte_xstats_len);
1926 } else {
1927 VLOG_WARN("Cannot get XSTATS values for port: %i.", dev->port_id);
1928 }
1929
1930 out:
1931 free(rte_xstats);
1932 free(rte_xstats_names);
1933
1934 stats->rx_packets = rte_stats.ipackets;
1935 stats->tx_packets = rte_stats.opackets;
1936 stats->rx_bytes = rte_stats.ibytes;
1937 stats->tx_bytes = rte_stats.obytes;
1938 /* DPDK counts imissed as errors, but count them here as dropped instead */
1939 stats->rx_errors = rte_stats.ierrors - rte_stats.imissed;
1940 stats->tx_errors = rte_stats.oerrors;
1941
1942 rte_spinlock_lock(&dev->stats_lock);
1943 stats->tx_dropped = dev->stats.tx_dropped;
1944 stats->rx_dropped = dev->stats.rx_dropped;
1945 rte_spinlock_unlock(&dev->stats_lock);
1946
1947 /* These are the available DPDK counters for packets not received due to
1948 * local resource constraints in DPDK and NIC respectively. */
1949 stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed;
1950 stats->rx_missed_errors = rte_stats.imissed;
1951
1952 ovs_mutex_unlock(&dev->mutex);
1953
1954 return 0;
1955 }
1956
1957 static int
1958 netdev_dpdk_get_features(const struct netdev *netdev,
1959 enum netdev_features *current,
1960 enum netdev_features *advertised,
1961 enum netdev_features *supported,
1962 enum netdev_features *peer)
1963 {
1964 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1965 struct rte_eth_link link;
1966
1967 ovs_mutex_lock(&dev->mutex);
1968 link = dev->link;
1969 ovs_mutex_unlock(&dev->mutex);
1970
1971 if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1972 if (link.link_speed == ETH_SPEED_NUM_10M) {
1973 *current = NETDEV_F_10MB_HD;
1974 }
1975 if (link.link_speed == ETH_SPEED_NUM_100M) {
1976 *current = NETDEV_F_100MB_HD;
1977 }
1978 if (link.link_speed == ETH_SPEED_NUM_1G) {
1979 *current = NETDEV_F_1GB_HD;
1980 }
1981 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1982 if (link.link_speed == ETH_SPEED_NUM_10M) {
1983 *current = NETDEV_F_10MB_FD;
1984 }
1985 if (link.link_speed == ETH_SPEED_NUM_100M) {
1986 *current = NETDEV_F_100MB_FD;
1987 }
1988 if (link.link_speed == ETH_SPEED_NUM_1G) {
1989 *current = NETDEV_F_1GB_FD;
1990 }
1991 if (link.link_speed == ETH_SPEED_NUM_10G) {
1992 *current = NETDEV_F_10GB_FD;
1993 }
1994 }
1995
1996 if (link.link_autoneg) {
1997 *current |= NETDEV_F_AUTONEG;
1998 }
1999
2000 *advertised = *supported = *peer = 0;
2001
2002 return 0;
2003 }
2004
2005 static struct ingress_policer *
2006 netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst)
2007 {
2008 struct ingress_policer *policer = NULL;
2009 uint64_t rate_bytes;
2010 uint64_t burst_bytes;
2011 int err = 0;
2012
2013 policer = xmalloc(sizeof *policer);
2014 rte_spinlock_init(&policer->policer_lock);
2015
2016 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2017 rate_bytes = rate * 1000/8;
2018 burst_bytes = burst * 1000/8;
2019
2020 policer->app_srtcm_params.cir = rate_bytes;
2021 policer->app_srtcm_params.cbs = burst_bytes;
2022 policer->app_srtcm_params.ebs = 0;
2023 err = rte_meter_srtcm_config(&policer->in_policer,
2024 &policer->app_srtcm_params);
2025 if (err) {
2026 VLOG_ERR("Could not create rte meter for ingress policer");
2027 return NULL;
2028 }
2029
2030 return policer;
2031 }
2032
2033 static int
2034 netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate,
2035 uint32_t policer_burst)
2036 {
2037 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2038 struct ingress_policer *policer;
2039
2040 /* Force to 0 if no rate specified,
2041 * default to 8000 kbits if burst is 0,
2042 * else stick with user-specified value.
2043 */
2044 policer_burst = (!policer_rate ? 0
2045 : !policer_burst ? 8000
2046 : policer_burst);
2047
2048 ovs_mutex_lock(&dev->mutex);
2049
2050 policer = ovsrcu_get_protected(struct ingress_policer *,
2051 &dev->ingress_policer);
2052
2053 if (dev->policer_rate == policer_rate &&
2054 dev->policer_burst == policer_burst) {
2055 /* Assume that settings haven't changed since we last set them. */
2056 ovs_mutex_unlock(&dev->mutex);
2057 return 0;
2058 }
2059
2060 /* Destroy any existing ingress policer for the device if one exists */
2061 if (policer) {
2062 ovsrcu_postpone(free, policer);
2063 }
2064
2065 if (policer_rate != 0) {
2066 policer = netdev_dpdk_policer_construct(policer_rate, policer_burst);
2067 } else {
2068 policer = NULL;
2069 }
2070 ovsrcu_set(&dev->ingress_policer, policer);
2071 dev->policer_rate = policer_rate;
2072 dev->policer_burst = policer_burst;
2073 ovs_mutex_unlock(&dev->mutex);
2074
2075 return 0;
2076 }
2077
2078 static int
2079 netdev_dpdk_get_ifindex(const struct netdev *netdev)
2080 {
2081 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2082 int ifindex;
2083
2084 ovs_mutex_lock(&dev->mutex);
2085 ifindex = dev->port_id;
2086 ovs_mutex_unlock(&dev->mutex);
2087
2088 return ifindex;
2089 }
2090
2091 static int
2092 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier)
2093 {
2094 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2095
2096 ovs_mutex_lock(&dev->mutex);
2097 check_link_status(dev);
2098 *carrier = dev->link.link_status;
2099
2100 ovs_mutex_unlock(&dev->mutex);
2101
2102 return 0;
2103 }
2104
2105 static int
2106 netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier)
2107 {
2108 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2109
2110 ovs_mutex_lock(&dev->mutex);
2111
2112 if (is_vhost_running(dev)) {
2113 *carrier = 1;
2114 } else {
2115 *carrier = 0;
2116 }
2117
2118 ovs_mutex_unlock(&dev->mutex);
2119
2120 return 0;
2121 }
2122
2123 static long long int
2124 netdev_dpdk_get_carrier_resets(const struct netdev *netdev)
2125 {
2126 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2127 long long int carrier_resets;
2128
2129 ovs_mutex_lock(&dev->mutex);
2130 carrier_resets = dev->link_reset_cnt;
2131 ovs_mutex_unlock(&dev->mutex);
2132
2133 return carrier_resets;
2134 }
2135
2136 static int
2137 netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED,
2138 long long int interval OVS_UNUSED)
2139 {
2140 return EOPNOTSUPP;
2141 }
2142
2143 static int
2144 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
2145 enum netdev_flags off, enum netdev_flags on,
2146 enum netdev_flags *old_flagsp)
2147 OVS_REQUIRES(dev->mutex)
2148 {
2149 int err;
2150
2151 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
2152 return EINVAL;
2153 }
2154
2155 *old_flagsp = dev->flags;
2156 dev->flags |= on;
2157 dev->flags &= ~off;
2158
2159 if (dev->flags == *old_flagsp) {
2160 return 0;
2161 }
2162
2163 if (dev->type == DPDK_DEV_ETH) {
2164 if (dev->flags & NETDEV_UP) {
2165 err = rte_eth_dev_start(dev->port_id);
2166 if (err)
2167 return -err;
2168 }
2169
2170 if (dev->flags & NETDEV_PROMISC) {
2171 rte_eth_promiscuous_enable(dev->port_id);
2172 }
2173
2174 if (!(dev->flags & NETDEV_UP)) {
2175 rte_eth_dev_stop(dev->port_id);
2176 }
2177
2178 netdev_change_seq_changed(&dev->up);
2179 } else {
2180 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2181 * running then change netdev's change_seq to trigger link state
2182 * update. */
2183
2184 if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off)))
2185 && is_vhost_running(dev)) {
2186 netdev_change_seq_changed(&dev->up);
2187
2188 /* Clear statistics if device is getting up. */
2189 if (NETDEV_UP & on) {
2190 rte_spinlock_lock(&dev->stats_lock);
2191 memset(&dev->stats, 0, sizeof dev->stats);
2192 rte_spinlock_unlock(&dev->stats_lock);
2193 }
2194 }
2195 }
2196
2197 return 0;
2198 }
2199
2200 static int
2201 netdev_dpdk_update_flags(struct netdev *netdev,
2202 enum netdev_flags off, enum netdev_flags on,
2203 enum netdev_flags *old_flagsp)
2204 {
2205 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2206 int error;
2207
2208 ovs_mutex_lock(&dev->mutex);
2209 error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp);
2210 ovs_mutex_unlock(&dev->mutex);
2211
2212 return error;
2213 }
2214
2215 static int
2216 netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args)
2217 {
2218 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2219 struct rte_eth_dev_info dev_info;
2220
2221 if (!rte_eth_dev_is_valid_port(dev->port_id)) {
2222 return ENODEV;
2223 }
2224
2225 ovs_mutex_lock(&dev->mutex);
2226 rte_eth_dev_info_get(dev->port_id, &dev_info);
2227 ovs_mutex_unlock(&dev->mutex);
2228
2229 smap_add_format(args, "port_no", "%d", dev->port_id);
2230 smap_add_format(args, "numa_id", "%d",
2231 rte_eth_dev_socket_id(dev->port_id));
2232 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
2233 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
2234 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
2235 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
2236 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
2237 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
2238 smap_add_format(args, "max_hash_mac_addrs", "%u",
2239 dev_info.max_hash_mac_addrs);
2240 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
2241 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
2242
2243 if (dev_info.pci_dev) {
2244 smap_add_format(args, "pci-vendor_id", "0x%u",
2245 dev_info.pci_dev->id.vendor_id);
2246 smap_add_format(args, "pci-device_id", "0x%x",
2247 dev_info.pci_dev->id.device_id);
2248 }
2249
2250 return 0;
2251 }
2252
2253 static void
2254 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
2255 OVS_REQUIRES(dev->mutex)
2256 {
2257 enum netdev_flags old_flags;
2258
2259 if (admin_state) {
2260 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
2261 } else {
2262 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
2263 }
2264 }
2265
2266 static void
2267 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
2268 const char *argv[], void *aux OVS_UNUSED)
2269 {
2270 bool up;
2271
2272 if (!strcasecmp(argv[argc - 1], "up")) {
2273 up = true;
2274 } else if ( !strcasecmp(argv[argc - 1], "down")) {
2275 up = false;
2276 } else {
2277 unixctl_command_reply_error(conn, "Invalid Admin State");
2278 return;
2279 }
2280
2281 if (argc > 2) {
2282 struct netdev *netdev = netdev_from_name(argv[1]);
2283 if (netdev && is_dpdk_class(netdev->netdev_class)) {
2284 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
2285
2286 ovs_mutex_lock(&dpdk_dev->mutex);
2287 netdev_dpdk_set_admin_state__(dpdk_dev, up);
2288 ovs_mutex_unlock(&dpdk_dev->mutex);
2289
2290 netdev_close(netdev);
2291 } else {
2292 unixctl_command_reply_error(conn, "Not a DPDK Interface");
2293 netdev_close(netdev);
2294 return;
2295 }
2296 } else {
2297 struct netdev_dpdk *netdev;
2298
2299 ovs_mutex_lock(&dpdk_mutex);
2300 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
2301 ovs_mutex_lock(&netdev->mutex);
2302 netdev_dpdk_set_admin_state__(netdev, up);
2303 ovs_mutex_unlock(&netdev->mutex);
2304 }
2305 ovs_mutex_unlock(&dpdk_mutex);
2306 }
2307 unixctl_command_reply(conn, "OK");
2308 }
2309
2310 /*
2311 * Set virtqueue flags so that we do not receive interrupts.
2312 */
2313 static void
2314 set_irq_status(int vid)
2315 {
2316 uint32_t i;
2317 uint64_t idx;
2318
2319 for (i = 0; i < rte_vhost_get_queue_num(vid); i++) {
2320 idx = i * VIRTIO_QNUM;
2321 rte_vhost_enable_guest_notification(vid, idx + VIRTIO_RXQ, 0);
2322 rte_vhost_enable_guest_notification(vid, idx + VIRTIO_TXQ, 0);
2323 }
2324 }
2325
2326 /*
2327 * Fixes mapping for vhost-user tx queues. Must be called after each
2328 * enabling/disabling of queues and n_txq modifications.
2329 */
2330 static void
2331 netdev_dpdk_remap_txqs(struct netdev_dpdk *dev)
2332 OVS_REQUIRES(dev->mutex)
2333 {
2334 int *enabled_queues, n_enabled = 0;
2335 int i, k, total_txqs = dev->up.n_txq;
2336
2337 enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues);
2338
2339 for (i = 0; i < total_txqs; i++) {
2340 /* Enabled queues always mapped to themselves. */
2341 if (dev->tx_q[i].map == i) {
2342 enabled_queues[n_enabled++] = i;
2343 }
2344 }
2345
2346 if (n_enabled == 0 && total_txqs != 0) {
2347 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
2348 n_enabled = 1;
2349 }
2350
2351 k = 0;
2352 for (i = 0; i < total_txqs; i++) {
2353 if (dev->tx_q[i].map != i) {
2354 dev->tx_q[i].map = enabled_queues[k];
2355 k = (k + 1) % n_enabled;
2356 }
2357 }
2358
2359 VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id);
2360 for (i = 0; i < total_txqs; i++) {
2361 VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map);
2362 }
2363
2364 free(enabled_queues);
2365 }
2366
2367 /*
2368 * A new virtio-net device is added to a vhost port.
2369 */
2370 static int
2371 new_device(int vid)
2372 {
2373 struct netdev_dpdk *dev;
2374 bool exists = false;
2375 int newnode = 0;
2376 char ifname[IF_NAME_SZ];
2377
2378 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2379
2380 ovs_mutex_lock(&dpdk_mutex);
2381 /* Add device to the vhost port with the same name as that passed down. */
2382 LIST_FOR_EACH(dev, list_node, &dpdk_list) {
2383 ovs_mutex_lock(&dev->mutex);
2384 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2385 uint32_t qp_num = rte_vhost_get_queue_num(vid);
2386
2387 /* Get NUMA information */
2388 newnode = rte_vhost_get_numa_node(vid);
2389 if (newnode == -1) {
2390 #ifdef VHOST_NUMA
2391 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2392 ifname);
2393 #endif
2394 newnode = dev->socket_id;
2395 }
2396
2397 if (dev->requested_n_txq != qp_num
2398 || dev->requested_n_rxq != qp_num
2399 || dev->requested_socket_id != newnode) {
2400 dev->requested_socket_id = newnode;
2401 dev->requested_n_rxq = qp_num;
2402 dev->requested_n_txq = qp_num;
2403 netdev_request_reconfigure(&dev->up);
2404 } else {
2405 /* Reconfiguration not required. */
2406 dev->vhost_reconfigured = true;
2407 }
2408
2409 ovsrcu_index_set(&dev->vid, vid);
2410 exists = true;
2411
2412 /* Disable notifications. */
2413 set_irq_status(vid);
2414 netdev_change_seq_changed(&dev->up);
2415 ovs_mutex_unlock(&dev->mutex);
2416 break;
2417 }
2418 ovs_mutex_unlock(&dev->mutex);
2419 }
2420 ovs_mutex_unlock(&dpdk_mutex);
2421
2422 if (!exists) {
2423 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname);
2424
2425 return -1;
2426 }
2427
2428 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2429 ifname, newnode);
2430
2431 return 0;
2432 }
2433
2434 /* Clears mapping for all available queues of vhost interface. */
2435 static void
2436 netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
2437 OVS_REQUIRES(dev->mutex)
2438 {
2439 int i;
2440
2441 for (i = 0; i < dev->up.n_txq; i++) {
2442 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
2443 }
2444 }
2445
2446 /*
2447 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2448 * flag to stop any more packets from being sent or received to/from a VM and
2449 * ensure all currently queued packets have been sent/received before removing
2450 * the device.
2451 */
2452 static void
2453 destroy_device(int vid)
2454 {
2455 struct netdev_dpdk *dev;
2456 bool exists = false;
2457 char ifname[IF_NAME_SZ];
2458
2459 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2460
2461 ovs_mutex_lock(&dpdk_mutex);
2462 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2463 if (netdev_dpdk_get_vid(dev) == vid) {
2464
2465 ovs_mutex_lock(&dev->mutex);
2466 dev->vhost_reconfigured = false;
2467 ovsrcu_index_set(&dev->vid, -1);
2468 netdev_dpdk_txq_map_clear(dev);
2469
2470 netdev_change_seq_changed(&dev->up);
2471 ovs_mutex_unlock(&dev->mutex);
2472 exists = true;
2473 break;
2474 }
2475 }
2476
2477 ovs_mutex_unlock(&dpdk_mutex);
2478
2479 if (exists) {
2480 /*
2481 * Wait for other threads to quiesce after setting the 'virtio_dev'
2482 * to NULL, before returning.
2483 */
2484 ovsrcu_synchronize();
2485 /*
2486 * As call to ovsrcu_synchronize() will end the quiescent state,
2487 * put thread back into quiescent state before returning.
2488 */
2489 ovsrcu_quiesce_start();
2490 VLOG_INFO("vHost Device '%s' has been removed", ifname);
2491 } else {
2492 VLOG_INFO("vHost Device '%s' not found", ifname);
2493 }
2494 }
2495
2496 static int
2497 vring_state_changed(int vid, uint16_t queue_id, int enable)
2498 {
2499 struct netdev_dpdk *dev;
2500 bool exists = false;
2501 int qid = queue_id / VIRTIO_QNUM;
2502 char ifname[IF_NAME_SZ];
2503
2504 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2505
2506 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
2507 return 0;
2508 }
2509
2510 ovs_mutex_lock(&dpdk_mutex);
2511 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2512 ovs_mutex_lock(&dev->mutex);
2513 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2514 if (enable) {
2515 dev->tx_q[qid].map = qid;
2516 } else {
2517 dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
2518 }
2519 netdev_dpdk_remap_txqs(dev);
2520 exists = true;
2521 ovs_mutex_unlock(&dev->mutex);
2522 break;
2523 }
2524 ovs_mutex_unlock(&dev->mutex);
2525 }
2526 ovs_mutex_unlock(&dpdk_mutex);
2527
2528 if (exists) {
2529 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2530 "changed to \'%s\'", queue_id, qid, ifname,
2531 (enable == 1) ? "enabled" : "disabled");
2532 } else {
2533 VLOG_INFO("vHost Device '%s' not found", ifname);
2534 return -1;
2535 }
2536
2537 return 0;
2538 }
2539
2540 int
2541 netdev_dpdk_get_vid(const struct netdev_dpdk *dev)
2542 {
2543 return ovsrcu_index_get(&dev->vid);
2544 }
2545
2546 struct ingress_policer *
2547 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev)
2548 {
2549 return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer);
2550 }
2551
2552 /*
2553 * These callbacks allow virtio-net devices to be added to vhost ports when
2554 * configuration has been fully complete.
2555 */
2556 static const struct virtio_net_device_ops virtio_net_device_ops =
2557 {
2558 .new_device = new_device,
2559 .destroy_device = destroy_device,
2560 .vring_state_changed = vring_state_changed
2561 };
2562
2563 static void *
2564 start_vhost_loop(void *dummy OVS_UNUSED)
2565 {
2566 pthread_detach(pthread_self());
2567 /* Put the vhost thread into quiescent state. */
2568 ovsrcu_quiesce_start();
2569 rte_vhost_driver_session_start();
2570 return NULL;
2571 }
2572
2573 static int
2574 netdev_dpdk_class_init(void)
2575 {
2576 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2577
2578 /* This function can be called for different classes. The initialization
2579 * needs to be done only once */
2580 if (ovsthread_once_start(&once)) {
2581 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
2582 unixctl_command_register("netdev-dpdk/set-admin-state",
2583 "[netdev] up|down", 1, 2,
2584 netdev_dpdk_set_admin_state, NULL);
2585
2586 ovsthread_once_done(&once);
2587 }
2588
2589 return 0;
2590 }
2591
2592 static int
2593 netdev_dpdk_vhost_class_init(void)
2594 {
2595 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2596
2597 /* This function can be called for different classes. The initialization
2598 * needs to be done only once */
2599 if (ovsthread_once_start(&once)) {
2600 rte_vhost_driver_callback_register(&virtio_net_device_ops);
2601 rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_HOST_TSO4
2602 | 1ULL << VIRTIO_NET_F_HOST_TSO6
2603 | 1ULL << VIRTIO_NET_F_CSUM
2604 | 1ULL << VIRTIO_RING_F_INDIRECT_DESC);
2605 ovs_thread_create("vhost_thread", start_vhost_loop, NULL);
2606
2607 ovsthread_once_done(&once);
2608 }
2609
2610 return 0;
2611 }
2612
2613 /* Client Rings */
2614
2615 static int
2616 dpdk_ring_create(const char dev_name[], unsigned int port_no,
2617 unsigned int *eth_port_id)
2618 {
2619 struct dpdk_ring *ivshmem;
2620 char *ring_name;
2621 int err;
2622
2623 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
2624 if (!ivshmem) {
2625 return ENOMEM;
2626 }
2627
2628 /* XXX: Add support for multiquque ring. */
2629 ring_name = xasprintf("%s_tx", dev_name);
2630
2631 /* Create single producer tx ring, netdev does explicit locking. */
2632 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2633 RING_F_SP_ENQ);
2634 free(ring_name);
2635 if (ivshmem->cring_tx == NULL) {
2636 rte_free(ivshmem);
2637 return ENOMEM;
2638 }
2639
2640 ring_name = xasprintf("%s_rx", dev_name);
2641
2642 /* Create single consumer rx ring, netdev does explicit locking. */
2643 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2644 RING_F_SC_DEQ);
2645 free(ring_name);
2646 if (ivshmem->cring_rx == NULL) {
2647 rte_free(ivshmem);
2648 return ENOMEM;
2649 }
2650
2651 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
2652 &ivshmem->cring_tx, 1, SOCKET0);
2653
2654 if (err < 0) {
2655 rte_free(ivshmem);
2656 return ENODEV;
2657 }
2658
2659 ivshmem->user_port_id = port_no;
2660 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
2661 ovs_list_push_back(&dpdk_ring_list, &ivshmem->list_node);
2662
2663 *eth_port_id = ivshmem->eth_port_id;
2664 return 0;
2665 }
2666
2667 static int
2668 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id)
2669 OVS_REQUIRES(dpdk_mutex)
2670 {
2671 struct dpdk_ring *ivshmem;
2672 unsigned int port_no;
2673 int err = 0;
2674
2675 /* Names always start with "dpdkr" */
2676 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
2677 if (err) {
2678 return err;
2679 }
2680
2681 /* Look through our list to find the device */
2682 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
2683 if (ivshmem->user_port_id == port_no) {
2684 VLOG_INFO("Found dpdk ring device %s:", dev_name);
2685 /* Really all that is needed */
2686 *eth_port_id = ivshmem->eth_port_id;
2687 return 0;
2688 }
2689 }
2690 /* Need to create the device rings */
2691 return dpdk_ring_create(dev_name, port_no, eth_port_id);
2692 }
2693
2694 static int
2695 netdev_dpdk_ring_send(struct netdev *netdev, int qid,
2696 struct dp_packet_batch *batch, bool may_steal,
2697 bool concurrent_txq)
2698 {
2699 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2700 unsigned i;
2701
2702 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2703 * the rss hash field is clear. This is because the same mbuf may be
2704 * modified by the consumer of the ring and return into the datapath
2705 * without recalculating the RSS hash. */
2706 for (i = 0; i < batch->count; i++) {
2707 dp_packet_rss_invalidate(batch->packets[i]);
2708 }
2709
2710 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
2711 return 0;
2712 }
2713
2714 static int
2715 netdev_dpdk_ring_construct(struct netdev *netdev)
2716 {
2717 unsigned int port_no = 0;
2718 int err = 0;
2719
2720 ovs_mutex_lock(&dpdk_mutex);
2721
2722 err = dpdk_ring_open(netdev->name, &port_no);
2723 if (err) {
2724 goto unlock_dpdk;
2725 }
2726
2727 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
2728
2729 unlock_dpdk:
2730 ovs_mutex_unlock(&dpdk_mutex);
2731 return err;
2732 }
2733
2734 /* QoS Functions */
2735
2736 /*
2737 * Initialize QoS configuration operations.
2738 */
2739 static void
2740 qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
2741 {
2742 conf->ops = ops;
2743 rte_spinlock_init(&conf->lock);
2744 }
2745
2746 /*
2747 * Search existing QoS operations in qos_ops and compare each set of
2748 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2749 * else return NULL
2750 */
2751 static const struct dpdk_qos_ops *
2752 qos_lookup_name(const char *name)
2753 {
2754 const struct dpdk_qos_ops *const *opsp;
2755
2756 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2757 const struct dpdk_qos_ops *ops = *opsp;
2758 if (!strcmp(name, ops->qos_name)) {
2759 return ops;
2760 }
2761 }
2762 return NULL;
2763 }
2764
2765 static int
2766 netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
2767 struct sset *types)
2768 {
2769 const struct dpdk_qos_ops *const *opsp;
2770
2771 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2772 const struct dpdk_qos_ops *ops = *opsp;
2773 if (ops->qos_construct && ops->qos_name[0] != '\0') {
2774 sset_add(types, ops->qos_name);
2775 }
2776 }
2777 return 0;
2778 }
2779
2780 static int
2781 netdev_dpdk_get_qos(const struct netdev *netdev,
2782 const char **typep, struct smap *details)
2783 {
2784 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2785 struct qos_conf *qos_conf;
2786 int error = 0;
2787
2788 ovs_mutex_lock(&dev->mutex);
2789 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
2790 if (qos_conf) {
2791 *typep = qos_conf->ops->qos_name;
2792 error = (qos_conf->ops->qos_get
2793 ? qos_conf->ops->qos_get(qos_conf, details): 0);
2794 } else {
2795 /* No QoS configuration set, return an empty string */
2796 *typep = "";
2797 }
2798 ovs_mutex_unlock(&dev->mutex);
2799
2800 return error;
2801 }
2802
2803 static int
2804 netdev_dpdk_set_qos(struct netdev *netdev, const char *type,
2805 const struct smap *details)
2806 {
2807 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2808 const struct dpdk_qos_ops *new_ops = NULL;
2809 struct qos_conf *qos_conf, *new_qos_conf = NULL;
2810 int error = 0;
2811
2812 ovs_mutex_lock(&dev->mutex);
2813
2814 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
2815
2816 new_ops = qos_lookup_name(type);
2817
2818 if (!new_ops || !new_ops->qos_construct) {
2819 new_qos_conf = NULL;
2820 if (type && type[0]) {
2821 error = EOPNOTSUPP;
2822 }
2823 } else if (qos_conf && qos_conf->ops == new_ops
2824 && qos_conf->ops->qos_is_equal(qos_conf, details)) {
2825 new_qos_conf = qos_conf;
2826 } else {
2827 error = new_ops->qos_construct(details, &new_qos_conf);
2828 }
2829
2830 if (error) {
2831 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
2832 type, netdev->name, rte_strerror(error));
2833 }
2834
2835 if (new_qos_conf != qos_conf) {
2836 ovsrcu_set(&dev->qos_conf, new_qos_conf);
2837 if (qos_conf) {
2838 ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf);
2839 }
2840 }
2841
2842 ovs_mutex_unlock(&dev->mutex);
2843
2844 return error;
2845 }
2846
2847 /* egress-policer details */
2848
2849 struct egress_policer {
2850 struct qos_conf qos_conf;
2851 struct rte_meter_srtcm_params app_srtcm_params;
2852 struct rte_meter_srtcm egress_meter;
2853 };
2854
2855 static void
2856 egress_policer_details_to_param(const struct smap *details,
2857 struct rte_meter_srtcm_params *params)
2858 {
2859 memset(params, 0, sizeof *params);
2860 params->cir = smap_get_ullong(details, "cir", 0);
2861 params->cbs = smap_get_ullong(details, "cbs", 0);
2862 params->ebs = 0;
2863 }
2864
2865 static int
2866 egress_policer_qos_construct(const struct smap *details,
2867 struct qos_conf **conf)
2868 {
2869 struct egress_policer *policer;
2870 int err = 0;
2871
2872 policer = xmalloc(sizeof *policer);
2873 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
2874 egress_policer_details_to_param(details, &policer->app_srtcm_params);
2875 err = rte_meter_srtcm_config(&policer->egress_meter,
2876 &policer->app_srtcm_params);
2877 if (!err) {
2878 *conf = &policer->qos_conf;
2879 } else {
2880 free(policer);
2881 *conf = NULL;
2882 err = -err;
2883 }
2884
2885 return err;
2886 }
2887
2888 static void
2889 egress_policer_qos_destruct(struct qos_conf *conf)
2890 {
2891 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
2892 qos_conf);
2893 free(policer);
2894 }
2895
2896 static int
2897 egress_policer_qos_get(const struct qos_conf *conf, struct smap *details)
2898 {
2899 struct egress_policer *policer =
2900 CONTAINER_OF(conf, struct egress_policer, qos_conf);
2901
2902 smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir);
2903 smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs);
2904
2905 return 0;
2906 }
2907
2908 static bool
2909 egress_policer_qos_is_equal(const struct qos_conf *conf, const struct smap *details)
2910 {
2911 struct egress_policer *policer =
2912 CONTAINER_OF(conf, struct egress_policer, qos_conf);
2913 struct rte_meter_srtcm_params params;
2914
2915 egress_policer_details_to_param(details, &params);
2916
2917 return !memcmp(&params, &policer->app_srtcm_params, sizeof params);
2918 }
2919
2920 static int
2921 egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt)
2922 {
2923 int cnt = 0;
2924 struct egress_policer *policer =
2925 CONTAINER_OF(conf, struct egress_policer, qos_conf);
2926
2927 cnt = netdev_dpdk_policer_run(&policer->egress_meter, pkts, pkt_cnt);
2928
2929 return cnt;
2930 }
2931
2932 static const struct dpdk_qos_ops egress_policer_ops = {
2933 "egress-policer", /* qos_name */
2934 egress_policer_qos_construct,
2935 egress_policer_qos_destruct,
2936 egress_policer_qos_get,
2937 egress_policer_qos_is_equal,
2938 egress_policer_run
2939 };
2940
2941 static int
2942 netdev_dpdk_reconfigure(struct netdev *netdev)
2943 {
2944 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2945 int err = 0;
2946
2947 ovs_mutex_lock(&dev->mutex);
2948
2949 if (netdev->n_txq == dev->requested_n_txq
2950 && netdev->n_rxq == dev->requested_n_rxq
2951 && dev->mtu == dev->requested_mtu
2952 && dev->rxq_size == dev->requested_rxq_size
2953 && dev->txq_size == dev->requested_txq_size) {
2954 /* Reconfiguration is unnecessary */
2955
2956 goto out;
2957 }
2958
2959 rte_eth_dev_stop(dev->port_id);
2960
2961 if (dev->mtu != dev->requested_mtu) {
2962 netdev_dpdk_mempool_configure(dev);
2963 }
2964
2965 netdev->n_txq = dev->requested_n_txq;
2966 netdev->n_rxq = dev->requested_n_rxq;
2967
2968 dev->rxq_size = dev->requested_rxq_size;
2969 dev->txq_size = dev->requested_txq_size;
2970
2971 rte_free(dev->tx_q);
2972 err = dpdk_eth_dev_init(dev);
2973 dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq);
2974 if (!dev->tx_q) {
2975 err = ENOMEM;
2976 }
2977
2978 netdev_change_seq_changed(netdev);
2979
2980 out:
2981 ovs_mutex_unlock(&dev->mutex);
2982 return err;
2983 }
2984
2985 static void
2986 dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev)
2987 OVS_REQUIRES(dev->mutex)
2988 {
2989 dev->up.n_txq = dev->requested_n_txq;
2990 dev->up.n_rxq = dev->requested_n_rxq;
2991
2992 /* Enable TX queue 0 by default if it wasn't disabled. */
2993 if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
2994 dev->tx_q[0].map = 0;
2995 }
2996
2997 netdev_dpdk_remap_txqs(dev);
2998
2999 if (dev->requested_socket_id != dev->socket_id
3000 || dev->requested_mtu != dev->mtu) {
3001 if (!netdev_dpdk_mempool_configure(dev)) {
3002 netdev_change_seq_changed(&dev->up);
3003 }
3004 }
3005
3006 if (netdev_dpdk_get_vid(dev) >= 0) {
3007 dev->vhost_reconfigured = true;
3008 }
3009 }
3010
3011 static int
3012 netdev_dpdk_vhost_reconfigure(struct netdev *netdev)
3013 {
3014 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3015
3016 ovs_mutex_lock(&dev->mutex);
3017 dpdk_vhost_reconfigure_helper(dev);
3018 ovs_mutex_unlock(&dev->mutex);
3019 return 0;
3020 }
3021
3022 static int
3023 netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev)
3024 {
3025 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3026 int err = 0;
3027
3028 ovs_mutex_lock(&dev->mutex);
3029
3030 dpdk_vhost_reconfigure_helper(dev);
3031
3032 /* Configure vHost client mode if requested and if the following criteria
3033 * are met:
3034 * 1. Device hasn't been registered yet.
3035 * 2. A path has been specified.
3036 */
3037 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)
3038 && strlen(dev->vhost_id)) {
3039 /* Register client-mode device */
3040 err = rte_vhost_driver_register(dev->vhost_id,
3041 RTE_VHOST_USER_CLIENT);
3042 if (err) {
3043 VLOG_ERR("vhost-user device setup failure for device %s\n",
3044 dev->vhost_id);
3045 } else {
3046 /* Configuration successful */
3047 dev->vhost_driver_flags |= RTE_VHOST_USER_CLIENT;
3048 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3049 "using client socket '%s'",
3050 dev->up.name, dev->vhost_id);
3051 }
3052 }
3053
3054 ovs_mutex_unlock(&dev->mutex);
3055
3056 return 0;
3057 }
3058
3059 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3060 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3061 GET_CARRIER, GET_STATS, \
3062 GET_FEATURES, GET_STATUS, \
3063 RECONFIGURE, RXQ_RECV) \
3064 { \
3065 NAME, \
3066 true, /* is_pmd */ \
3067 INIT, /* init */ \
3068 NULL, /* netdev_dpdk_run */ \
3069 NULL, /* netdev_dpdk_wait */ \
3070 \
3071 netdev_dpdk_alloc, \
3072 CONSTRUCT, \
3073 DESTRUCT, \
3074 netdev_dpdk_dealloc, \
3075 netdev_dpdk_get_config, \
3076 SET_CONFIG, \
3077 NULL, /* get_tunnel_config */ \
3078 NULL, /* build header */ \
3079 NULL, /* push header */ \
3080 NULL, /* pop header */ \
3081 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3082 SET_TX_MULTIQ, \
3083 \
3084 SEND, /* send */ \
3085 NULL, /* send_wait */ \
3086 \
3087 netdev_dpdk_set_etheraddr, \
3088 netdev_dpdk_get_etheraddr, \
3089 netdev_dpdk_get_mtu, \
3090 netdev_dpdk_set_mtu, \
3091 netdev_dpdk_get_ifindex, \
3092 GET_CARRIER, \
3093 netdev_dpdk_get_carrier_resets, \
3094 netdev_dpdk_set_miimon, \
3095 GET_STATS, \
3096 GET_FEATURES, \
3097 NULL, /* set_advertisements */ \
3098 \
3099 netdev_dpdk_set_policing, \
3100 netdev_dpdk_get_qos_types, \
3101 NULL, /* get_qos_capabilities */ \
3102 netdev_dpdk_get_qos, \
3103 netdev_dpdk_set_qos, \
3104 NULL, /* get_queue */ \
3105 NULL, /* set_queue */ \
3106 NULL, /* delete_queue */ \
3107 NULL, /* get_queue_stats */ \
3108 NULL, /* queue_dump_start */ \
3109 NULL, /* queue_dump_next */ \
3110 NULL, /* queue_dump_done */ \
3111 NULL, /* dump_queue_stats */ \
3112 \
3113 NULL, /* set_in4 */ \
3114 NULL, /* get_addr_list */ \
3115 NULL, /* add_router */ \
3116 NULL, /* get_next_hop */ \
3117 GET_STATUS, \
3118 NULL, /* arp_lookup */ \
3119 \
3120 netdev_dpdk_update_flags, \
3121 RECONFIGURE, \
3122 \
3123 netdev_dpdk_rxq_alloc, \
3124 netdev_dpdk_rxq_construct, \
3125 netdev_dpdk_rxq_destruct, \
3126 netdev_dpdk_rxq_dealloc, \
3127 RXQ_RECV, \
3128 NULL, /* rx_wait */ \
3129 NULL, /* rxq_drain */ \
3130 }
3131
3132 static const struct netdev_class dpdk_class =
3133 NETDEV_DPDK_CLASS(
3134 "dpdk",
3135 netdev_dpdk_class_init,
3136 netdev_dpdk_construct,
3137 netdev_dpdk_destruct,
3138 netdev_dpdk_set_config,
3139 netdev_dpdk_set_tx_multiq,
3140 netdev_dpdk_eth_send,
3141 netdev_dpdk_get_carrier,
3142 netdev_dpdk_get_stats,
3143 netdev_dpdk_get_features,
3144 netdev_dpdk_get_status,
3145 netdev_dpdk_reconfigure,
3146 netdev_dpdk_rxq_recv);
3147
3148 static const struct netdev_class dpdk_ring_class =
3149 NETDEV_DPDK_CLASS(
3150 "dpdkr",
3151 netdev_dpdk_class_init,
3152 netdev_dpdk_ring_construct,
3153 netdev_dpdk_destruct,
3154 netdev_dpdk_ring_set_config,
3155 netdev_dpdk_set_tx_multiq,
3156 netdev_dpdk_ring_send,
3157 netdev_dpdk_get_carrier,
3158 netdev_dpdk_get_stats,
3159 netdev_dpdk_get_features,
3160 netdev_dpdk_get_status,
3161 netdev_dpdk_reconfigure,
3162 netdev_dpdk_rxq_recv);
3163
3164 static const struct netdev_class dpdk_vhost_class =
3165 NETDEV_DPDK_CLASS(
3166 "dpdkvhostuser",
3167 netdev_dpdk_vhost_class_init,
3168 netdev_dpdk_vhost_construct,
3169 netdev_dpdk_vhost_destruct,
3170 NULL,
3171 NULL,
3172 netdev_dpdk_vhost_send,
3173 netdev_dpdk_vhost_get_carrier,
3174 netdev_dpdk_vhost_get_stats,
3175 NULL,
3176 NULL,
3177 netdev_dpdk_vhost_reconfigure,
3178 netdev_dpdk_vhost_rxq_recv);
3179 static const struct netdev_class dpdk_vhost_client_class =
3180 NETDEV_DPDK_CLASS(
3181 "dpdkvhostuserclient",
3182 netdev_dpdk_vhost_class_init,
3183 netdev_dpdk_vhost_client_construct,
3184 netdev_dpdk_vhost_destruct,
3185 netdev_dpdk_vhost_client_set_config,
3186 NULL,
3187 netdev_dpdk_vhost_send,
3188 netdev_dpdk_vhost_get_carrier,
3189 netdev_dpdk_vhost_get_stats,
3190 NULL,
3191 NULL,
3192 netdev_dpdk_vhost_client_reconfigure,
3193 netdev_dpdk_vhost_rxq_recv);
3194
3195 void
3196 netdev_dpdk_register(void)
3197 {
3198 netdev_register_provider(&dpdk_class);
3199 netdev_register_provider(&dpdk_ring_class);
3200 netdev_register_provider(&dpdk_vhost_class);
3201 netdev_register_provider(&dpdk_vhost_client_class);
3202 }