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