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