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