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