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