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