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