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