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