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