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