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