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