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