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