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