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