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