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