]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-dpdk.c
21e46a21f371f553c353fd03040c2cfc7fa07d38
[mirror_ovs.git] / lib / netdev-dpdk.c
1 /*
2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "netdev-dpdk.h"
19
20 #include <string.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <linux/virtio_net.h>
26 #include <sys/socket.h>
27 #include <linux/if.h>
28
29 #include <rte_config.h>
30 #include <rte_cycles.h>
31 #include <rte_errno.h>
32 #include <rte_eth_ring.h>
33 #include <rte_ethdev.h>
34 #include <rte_malloc.h>
35 #include <rte_mbuf.h>
36 #include <rte_meter.h>
37 #include <rte_pci.h>
38 #include <rte_vhost.h>
39
40 #include "dirs.h"
41 #include "dp-packet.h"
42 #include "dpdk.h"
43 #include "dpif-netdev.h"
44 #include "fatal-signal.h"
45 #include "netdev-provider.h"
46 #include "netdev-vport.h"
47 #include "odp-util.h"
48 #include "openvswitch/dynamic-string.h"
49 #include "openvswitch/list.h"
50 #include "openvswitch/ofp-print.h"
51 #include "openvswitch/vlog.h"
52 #include "ovs-numa.h"
53 #include "ovs-thread.h"
54 #include "ovs-rcu.h"
55 #include "packets.h"
56 #include "openvswitch/shash.h"
57 #include "smap.h"
58 #include "sset.h"
59 #include "unaligned.h"
60 #include "timeval.h"
61 #include "unixctl.h"
62
63 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
64
65 VLOG_DEFINE_THIS_MODULE(netdev_dpdk);
66 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
67
68 #define DPDK_PORT_WATCHDOG_INTERVAL 5
69
70 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
71 #define OVS_VPORT_DPDK "ovs_dpdk"
72
73 /*
74 * need to reserve tons of extra space in the mbufs so we can align the
75 * DMA addresses to 4KB.
76 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
77 * performance for standard Ethernet MTU.
78 */
79 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
80 + (2 * VLAN_HEADER_LEN))
81 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
82 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
83 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
84 - ETHER_HDR_LEN - ETHER_CRC_LEN)
85 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
86 + sizeof(struct dp_packet) \
87 + RTE_PKTMBUF_HEADROOM), \
88 RTE_CACHE_LINE_SIZE)
89 #define NETDEV_DPDK_MBUF_ALIGN 1024
90 #define NETDEV_DPDK_MAX_PKT_LEN 9728
91
92 /* Max and min number of packets in the mempool. OVS tries to allocate a
93 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
94 * enough hugepages) we keep halving the number until the allocation succeeds
95 * or we reach MIN_NB_MBUF */
96
97 #define MAX_NB_MBUF (4096 * 64)
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
100
101 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
102 BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0);
103
104 /* The smallest possible NB_MBUF that we're going to try should be a multiple
105 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
106 BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF))
107 % MP_CACHE_SZ == 0);
108
109 /*
110 * DPDK XSTATS Counter names definition
111 */
112 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
113 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
114 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
115 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
116 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
117 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
118 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
119
120 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
121 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
122 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
123 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
124 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
125 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
126 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
127
128 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
129 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
130 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
131 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
132 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
133 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
134 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
135 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
136
137 #define SOCKET0 0
138
139 /* Default size of Physical NIC RXQ */
140 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
141 /* Default size of Physical NIC TXQ */
142 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
143 /* Maximum size of Physical NIC Queues */
144 #define NIC_PORT_MAX_Q_SIZE 4096
145
146 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
147 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
148 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
149 * yet mapped to another queue. */
150
151 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
152
153 /* DPDK library uses uint8_t for port_id. */
154 typedef uint8_t dpdk_port_t;
155
156 #define VHOST_ENQ_RETRY_NUM 8
157 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
158
159 static const struct rte_eth_conf port_conf = {
160 .rxmode = {
161 .mq_mode = ETH_MQ_RX_RSS,
162 .split_hdr_size = 0,
163 .header_split = 0, /* Header Split disabled */
164 .hw_ip_checksum = 0, /* IP checksum offload disabled */
165 .hw_vlan_filter = 0, /* VLAN filtering disabled */
166 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
167 .hw_strip_crc = 0,
168 },
169 .rx_adv_conf = {
170 .rss_conf = {
171 .rss_key = NULL,
172 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
173 },
174 },
175 .txmode = {
176 .mq_mode = ETH_MQ_TX_NONE,
177 },
178 };
179
180 /*
181 * These callbacks allow virtio-net devices to be added to vhost ports when
182 * configuration has been fully completed.
183 */
184 static int new_device(int vid);
185 static void destroy_device(int vid);
186 static int vring_state_changed(int vid, uint16_t queue_id, int enable);
187 static const struct vhost_device_ops virtio_net_device_ops =
188 {
189 .new_device = new_device,
190 .destroy_device = destroy_device,
191 .vring_state_changed = vring_state_changed,
192 .features_changed = NULL
193 };
194
195 enum { DPDK_RING_SIZE = 256 };
196 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
197 enum { DRAIN_TSC = 200000ULL };
198
199 enum dpdk_dev_type {
200 DPDK_DEV_ETH = 0,
201 DPDK_DEV_VHOST = 1,
202 };
203
204 /* Quality of Service */
205
206 /* An instance of a QoS configuration. Always associated with a particular
207 * network device.
208 *
209 * Each QoS implementation subclasses this with whatever additional data it
210 * needs.
211 */
212 struct qos_conf {
213 const struct dpdk_qos_ops *ops;
214 rte_spinlock_t lock;
215 };
216
217 /* A particular implementation of dpdk QoS operations.
218 *
219 * The functions below return 0 if successful or a positive errno value on
220 * failure, except where otherwise noted. All of them must be provided, except
221 * where otherwise noted.
222 */
223 struct dpdk_qos_ops {
224
225 /* Name of the QoS type */
226 const char *qos_name;
227
228 /* Called to construct a qos_conf object. The implementation should make
229 * the appropriate calls to configure QoS according to 'details'.
230 *
231 * The contents of 'details' should be documented as valid for 'ovs_name'
232 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
233 * (which is built as ovs-vswitchd.conf.db(8)).
234 *
235 * This function must return 0 if and only if it sets '*conf' to an
236 * initialized 'struct qos_conf'.
237 *
238 * For all QoS implementations it should always be non-null.
239 */
240 int (*qos_construct)(const struct smap *details, struct qos_conf **conf);
241
242 /* Destroys the data structures allocated by the implementation as part of
243 * 'qos_conf'.
244 *
245 * For all QoS implementations it should always be non-null.
246 */
247 void (*qos_destruct)(struct qos_conf *conf);
248
249 /* Retrieves details of 'conf' configuration into 'details'.
250 *
251 * The contents of 'details' should be documented as valid for 'ovs_name'
252 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
253 * (which is built as ovs-vswitchd.conf.db(8)).
254 */
255 int (*qos_get)(const struct qos_conf *conf, struct smap *details);
256
257 /* Returns true if 'conf' is already configured according to 'details'.
258 *
259 * The contents of 'details' should be documented as valid for 'ovs_name'
260 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
261 * (which is built as ovs-vswitchd.conf.db(8)).
262 *
263 * For all QoS implementations it should always be non-null.
264 */
265 bool (*qos_is_equal)(const struct qos_conf *conf,
266 const struct smap *details);
267
268 /* Modify an array of rte_mbufs. The modification is specific to
269 * each qos implementation.
270 *
271 * The function should take and array of mbufs and an int representing
272 * the current number of mbufs present in the array.
273 *
274 * After the function has performed a qos modification to the array of
275 * mbufs it returns an int representing the number of mbufs now present in
276 * the array. This value is can then be passed to the port send function
277 * along with the modified array for transmission.
278 *
279 * For all QoS implementations it should always be non-null.
280 */
281 int (*qos_run)(struct qos_conf *qos_conf, struct rte_mbuf **pkts,
282 int pkt_cnt, bool may_steal);
283 };
284
285 /* dpdk_qos_ops for each type of user space QoS implementation */
286 static const struct dpdk_qos_ops egress_policer_ops;
287
288 /*
289 * Array of dpdk_qos_ops, contains pointer to all supported QoS
290 * operations.
291 */
292 static const struct dpdk_qos_ops *const qos_confs[] = {
293 &egress_policer_ops,
294 NULL
295 };
296
297 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
298
299 /* Contains all 'struct dpdk_dev's. */
300 static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
301 = OVS_LIST_INITIALIZER(&dpdk_list);
302
303 static struct ovs_mutex dpdk_mp_mutex OVS_ACQ_AFTER(dpdk_mutex)
304 = OVS_MUTEX_INITIALIZER;
305
306 /* There should be one 'struct dpdk_tx_queue' created for
307 * each cpu core. */
308 struct dpdk_tx_queue {
309 rte_spinlock_t tx_lock; /* Protects the members and the NIC queue
310 * from concurrent access. It is used only
311 * if the queue is shared among different
312 * pmd threads (see 'concurrent_txq'). */
313 int map; /* Mapping of configured vhost-user queues
314 * to enabled by guest. */
315 };
316
317 /* dpdk has no way to remove dpdk ring ethernet devices
318 so we have to keep them around once they've been created
319 */
320
321 static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
322 = OVS_LIST_INITIALIZER(&dpdk_ring_list);
323
324 struct dpdk_ring {
325 /* For the client rings */
326 struct rte_ring *cring_tx;
327 struct rte_ring *cring_rx;
328 unsigned int user_port_id; /* User given port no, parsed from port name */
329 dpdk_port_t eth_port_id; /* ethernet device port id */
330 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
331 };
332
333 struct ingress_policer {
334 struct rte_meter_srtcm_params app_srtcm_params;
335 struct rte_meter_srtcm in_policer;
336 rte_spinlock_t policer_lock;
337 };
338
339 enum dpdk_hw_ol_features {
340 NETDEV_RX_CHECKSUM_OFFLOAD = 1 << 0,
341 };
342
343 struct netdev_dpdk {
344 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline0,
345 dpdk_port_t port_id;
346
347 /* If true, device was attached by rte_eth_dev_attach(). */
348 bool attached;
349 struct eth_addr hwaddr;
350 int mtu;
351 int socket_id;
352 int buf_size;
353 int max_packet_len;
354 enum dpdk_dev_type type;
355 enum netdev_flags flags;
356 char *devargs; /* Device arguments for dpdk ports */
357 struct dpdk_tx_queue *tx_q;
358 struct rte_eth_link link;
359 int link_reset_cnt;
360 /* 4 pad bytes here. */
361 );
362
363 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline1,
364 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
365 struct rte_mempool *mp;
366
367 /* virtio identifier for vhost devices */
368 ovsrcu_index vid;
369
370 /* True if vHost device is 'up' and has been reconfigured at least once */
371 bool vhost_reconfigured;
372 /* 3 pad bytes here. */
373 );
374
375 PADDED_MEMBERS(CACHE_LINE_SIZE,
376 /* Identifier used to distinguish vhost devices from each other. */
377 char vhost_id[PATH_MAX];
378 );
379
380 PADDED_MEMBERS(CACHE_LINE_SIZE,
381 struct netdev up;
382 /* In dpdk_list. */
383 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
384
385 /* QoS configuration and lock for the device */
386 OVSRCU_TYPE(struct qos_conf *) qos_conf;
387
388 /* Ingress Policer */
389 OVSRCU_TYPE(struct ingress_policer *) ingress_policer;
390 uint32_t policer_rate;
391 uint32_t policer_burst;
392 );
393
394 PADDED_MEMBERS(CACHE_LINE_SIZE,
395 struct netdev_stats stats;
396 /* Protects stats */
397 rte_spinlock_t stats_lock;
398 /* 44 pad bytes here. */
399 );
400
401 PADDED_MEMBERS(CACHE_LINE_SIZE,
402 /* The following properties cannot be changed when a device is running,
403 * so we remember the request and update them next time
404 * netdev_dpdk*_reconfigure() is called */
405 int requested_mtu;
406 int requested_n_txq;
407 int requested_n_rxq;
408 int requested_rxq_size;
409 int requested_txq_size;
410
411 /* Number of rx/tx descriptors for physical devices */
412 int rxq_size;
413 int txq_size;
414
415 /* Socket ID detected when vHost device is brought up */
416 int requested_socket_id;
417
418 /* Denotes whether vHost port is client/server mode */
419 uint64_t vhost_driver_flags;
420
421 /* DPDK-ETH Flow control */
422 struct rte_eth_fc_conf fc_conf;
423
424 /* DPDK-ETH hardware offload features,
425 * from the enum set 'dpdk_hw_ol_features' */
426 uint32_t hw_ol_features;
427 );
428 };
429
430 struct netdev_rxq_dpdk {
431 struct netdev_rxq up;
432 dpdk_port_t port_id;
433 };
434
435 static void netdev_dpdk_destruct(struct netdev *netdev);
436 static void netdev_dpdk_vhost_destruct(struct netdev *netdev);
437
438 int netdev_dpdk_get_vid(const struct netdev_dpdk *dev);
439
440 struct ingress_policer *
441 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev);
442
443 static bool
444 is_dpdk_class(const struct netdev_class *class)
445 {
446 return class->destruct == netdev_dpdk_destruct
447 || class->destruct == netdev_dpdk_vhost_destruct;
448 }
449
450 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
451 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
452 * value, insufficient buffers are allocated to accomodate the packet in its
453 * entirety. Furthermore, certain drivers need to ensure that there is also
454 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
455 * frames). If the RX buffer is too small, then the driver enables scatter RX
456 * behaviour, which reduces performance. To prevent this, use a buffer size
457 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
458 */
459 static uint32_t
460 dpdk_buf_size(int mtu)
461 {
462 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) + RTE_PKTMBUF_HEADROOM),
463 NETDEV_DPDK_MBUF_ALIGN);
464 }
465
466 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
467 *
468 * Unlike xmalloc(), this function can return NULL on failure. */
469 static void *
470 dpdk_rte_mzalloc(size_t sz)
471 {
472 return rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
473 }
474
475 void
476 free_dpdk_buf(struct dp_packet *p)
477 {
478 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
479
480 rte_pktmbuf_free(pkt);
481 }
482
483 static void
484 ovs_rte_pktmbuf_init(struct rte_mempool *mp OVS_UNUSED,
485 void *opaque_arg OVS_UNUSED,
486 void *_p,
487 unsigned i OVS_UNUSED)
488 {
489 struct rte_mbuf *pkt = _p;
490
491 dp_packet_init_dpdk((struct dp_packet *) pkt, pkt->buf_len);
492 }
493
494 /* Returns a valid pointer when either of the following is true:
495 * - a new mempool was just created;
496 * - a matching mempool already exists. */
497 static struct rte_mempool *
498 dpdk_mp_create(struct netdev_dpdk *dev, int mtu)
499 {
500 char mp_name[RTE_MEMPOOL_NAMESIZE];
501 const char *netdev_name = netdev_get_name(&dev->up);
502 int socket_id = dev->requested_socket_id;
503 uint32_t n_mbufs;
504 uint32_t hash = hash_string(netdev_name, 0);
505 struct rte_mempool *mp = NULL;
506
507 /*
508 * XXX: rough estimation of number of mbufs required for this port:
509 * <packets required to fill the device rxqs>
510 * + <packets that could be stuck on other ports txqs>
511 * + <packets in the pmd threads>
512 * + <additional memory for corner cases>
513 */
514 n_mbufs = dev->requested_n_rxq * dev->requested_rxq_size
515 + dev->requested_n_txq * dev->requested_txq_size
516 + MIN(RTE_MAX_LCORE, dev->requested_n_rxq) * NETDEV_MAX_BURST
517 + MIN_NB_MBUF;
518
519 ovs_mutex_lock(&dpdk_mp_mutex);
520 do {
521 /* Full DPDK memory pool name must be unique and cannot be
522 * longer than RTE_MEMPOOL_NAMESIZE. */
523 int ret = snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_%x_%d_%d_%u",
524 hash, socket_id, mtu, n_mbufs);
525 if (ret < 0 || ret >= RTE_MEMPOOL_NAMESIZE) {
526 VLOG_DBG("snprintf returned %d. "
527 "Failed to generate a mempool name for \"%s\". "
528 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
529 ret, netdev_name, hash, socket_id, mtu, n_mbufs);
530 break;
531 }
532
533 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
534 "on socket %d for %d Rx and %d Tx queues.",
535 netdev_name, n_mbufs, socket_id,
536 dev->requested_n_rxq, dev->requested_n_txq);
537
538 mp = rte_pktmbuf_pool_create(mp_name, n_mbufs, MP_CACHE_SZ,
539 sizeof (struct dp_packet) - sizeof (struct rte_mbuf),
540 MBUF_SIZE(mtu) - sizeof(struct dp_packet), socket_id);
541
542 if (mp) {
543 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
544 mp_name, n_mbufs);
545 /* rte_pktmbuf_pool_create has done some initialization of the
546 * rte_mbuf part of each dp_packet. Some OvS specific fields
547 * of the packet still need to be initialized by
548 * ovs_rte_pktmbuf_init. */
549 rte_mempool_obj_iter(mp, ovs_rte_pktmbuf_init, NULL);
550 } else if (rte_errno == EEXIST) {
551 /* A mempool with the same name already exists. We just
552 * retrieve its pointer to be returned to the caller. */
553 mp = rte_mempool_lookup(mp_name);
554 /* As the mempool create returned EEXIST we can expect the
555 * lookup has returned a valid pointer. If for some reason
556 * that's not the case we keep track of it. */
557 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
558 mp_name, mp);
559 } else {
560 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
561 mp_name, n_mbufs);
562 }
563 } while (!mp && rte_errno == ENOMEM && (n_mbufs /= 2) >= MIN_NB_MBUF);
564
565 ovs_mutex_unlock(&dpdk_mp_mutex);
566 return mp;
567 }
568
569 /* Release an existing mempool. */
570 static void
571 dpdk_mp_free(struct rte_mempool *mp)
572 {
573 if (!mp) {
574 return;
575 }
576
577 ovs_mutex_lock(&dpdk_mp_mutex);
578 VLOG_DBG("Releasing \"%s\" mempool", mp->name);
579 rte_mempool_free(mp);
580 ovs_mutex_unlock(&dpdk_mp_mutex);
581 }
582
583 /* Tries to allocate a new mempool - or re-use an existing one where
584 * appropriate - on requested_socket_id with a size determined by
585 * requested_mtu and requested Rx/Tx queues.
586 * On success - or when re-using an existing mempool - the new configuration
587 * will be applied.
588 * On error, device will be left unchanged. */
589 static int
590 netdev_dpdk_mempool_configure(struct netdev_dpdk *dev)
591 OVS_REQUIRES(dev->mutex)
592 {
593 uint32_t buf_size = dpdk_buf_size(dev->requested_mtu);
594 struct rte_mempool *mp;
595 int ret = 0;
596
597 mp = dpdk_mp_create(dev, FRAME_LEN_TO_MTU(buf_size));
598 if (!mp) {
599 VLOG_ERR("Failed to create memory pool for netdev "
600 "%s, with MTU %d on socket %d: %s\n",
601 dev->up.name, dev->requested_mtu, dev->requested_socket_id,
602 rte_strerror(rte_errno));
603 ret = rte_errno;
604 } else {
605 /* If a new MTU was requested and its rounded value equals the one
606 * that is currently used, then the existing mempool is returned. */
607 if (dev->mp != mp) {
608 /* A new mempool was created, release the previous one. */
609 dpdk_mp_free(dev->mp);
610 } else {
611 ret = EEXIST;
612 }
613 dev->mp = mp;
614 dev->mtu = dev->requested_mtu;
615 dev->socket_id = dev->requested_socket_id;
616 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
617 }
618
619 return ret;
620 }
621
622 static void
623 check_link_status(struct netdev_dpdk *dev)
624 {
625 struct rte_eth_link link;
626
627 rte_eth_link_get_nowait(dev->port_id, &link);
628
629 if (dev->link.link_status != link.link_status) {
630 netdev_change_seq_changed(&dev->up);
631
632 dev->link_reset_cnt++;
633 dev->link = link;
634 if (dev->link.link_status) {
635 VLOG_DBG_RL(&rl, "Port %"PRIu8" Link Up - speed %u Mbps - %s",
636 dev->port_id, (unsigned) dev->link.link_speed,
637 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
638 ("full-duplex") : ("half-duplex"));
639 } else {
640 VLOG_DBG_RL(&rl, "Port %"PRIu8" Link Down", dev->port_id);
641 }
642 }
643 }
644
645 static void *
646 dpdk_watchdog(void *dummy OVS_UNUSED)
647 {
648 struct netdev_dpdk *dev;
649
650 pthread_detach(pthread_self());
651
652 for (;;) {
653 ovs_mutex_lock(&dpdk_mutex);
654 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
655 ovs_mutex_lock(&dev->mutex);
656 if (dev->type == DPDK_DEV_ETH) {
657 check_link_status(dev);
658 }
659 ovs_mutex_unlock(&dev->mutex);
660 }
661 ovs_mutex_unlock(&dpdk_mutex);
662 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
663 }
664
665 return NULL;
666 }
667
668 static int
669 dpdk_eth_dev_queue_setup(struct netdev_dpdk *dev, int n_rxq, int n_txq)
670 {
671 int diag = 0;
672 int i;
673 struct rte_eth_conf conf = port_conf;
674
675 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
676 * enabled. */
677 if (dev->mtu > ETHER_MTU) {
678 conf.rxmode.enable_scatter = 1;
679 }
680
681 conf.rxmode.hw_ip_checksum = (dev->hw_ol_features &
682 NETDEV_RX_CHECKSUM_OFFLOAD) != 0;
683 /* A device may report more queues than it makes available (this has
684 * been observed for Intel xl710, which reserves some of them for
685 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
686 * available. When this happens we can retry the configuration
687 * and request less queues */
688 while (n_rxq && n_txq) {
689 if (diag) {
690 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq);
691 }
692
693 diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &conf);
694 if (diag) {
695 VLOG_WARN("Interface %s eth_dev setup error %s\n",
696 dev->up.name, rte_strerror(-diag));
697 break;
698 }
699
700 diag = rte_eth_dev_set_mtu(dev->port_id, dev->mtu);
701 if (diag) {
702 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
703 dev->up.name, dev->mtu, rte_strerror(-diag));
704 break;
705 }
706
707 for (i = 0; i < n_txq; i++) {
708 diag = rte_eth_tx_queue_setup(dev->port_id, i, dev->txq_size,
709 dev->socket_id, NULL);
710 if (diag) {
711 VLOG_INFO("Interface %s txq(%d) setup error: %s",
712 dev->up.name, i, rte_strerror(-diag));
713 break;
714 }
715 }
716
717 if (i != n_txq) {
718 /* Retry with less tx queues */
719 n_txq = i;
720 continue;
721 }
722
723 for (i = 0; i < n_rxq; i++) {
724 diag = rte_eth_rx_queue_setup(dev->port_id, i, dev->rxq_size,
725 dev->socket_id, NULL, dev->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->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_free(dev->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 bool may_steal)
1506 {
1507 int i = 0;
1508 int cnt = 0;
1509 struct rte_mbuf *pkt = NULL;
1510 uint64_t current_time = rte_rdtsc();
1511
1512 for (i = 0; i < pkt_cnt; i++) {
1513 pkt = pkts[i];
1514 /* Handle current packet */
1515 if (netdev_dpdk_policer_pkt_handle(meter, pkt, current_time)) {
1516 if (cnt != i) {
1517 pkts[cnt] = pkt;
1518 }
1519 cnt++;
1520 } else {
1521 if (may_steal) {
1522 rte_pktmbuf_free(pkt);
1523 }
1524 }
1525 }
1526
1527 return cnt;
1528 }
1529
1530 static int
1531 ingress_policer_run(struct ingress_policer *policer, struct rte_mbuf **pkts,
1532 int pkt_cnt, bool may_steal)
1533 {
1534 int cnt = 0;
1535
1536 rte_spinlock_lock(&policer->policer_lock);
1537 cnt = netdev_dpdk_policer_run(&policer->in_policer, pkts,
1538 pkt_cnt, may_steal);
1539 rte_spinlock_unlock(&policer->policer_lock);
1540
1541 return cnt;
1542 }
1543
1544 static bool
1545 is_vhost_running(struct netdev_dpdk *dev)
1546 {
1547 return (netdev_dpdk_get_vid(dev) >= 0 && dev->vhost_reconfigured);
1548 }
1549
1550 static inline void
1551 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats *stats,
1552 unsigned int packet_size)
1553 {
1554 /* Hard-coded search for the size bucket. */
1555 if (packet_size < 256) {
1556 if (packet_size >= 128) {
1557 stats->rx_128_to_255_packets++;
1558 } else if (packet_size <= 64) {
1559 stats->rx_1_to_64_packets++;
1560 } else {
1561 stats->rx_65_to_127_packets++;
1562 }
1563 } else {
1564 if (packet_size >= 1523) {
1565 stats->rx_1523_to_max_packets++;
1566 } else if (packet_size >= 1024) {
1567 stats->rx_1024_to_1522_packets++;
1568 } else if (packet_size < 512) {
1569 stats->rx_256_to_511_packets++;
1570 } else {
1571 stats->rx_512_to_1023_packets++;
1572 }
1573 }
1574 }
1575
1576 static inline void
1577 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats *stats,
1578 struct dp_packet **packets, int count,
1579 int dropped)
1580 {
1581 int i;
1582 unsigned int packet_size;
1583 struct dp_packet *packet;
1584
1585 stats->rx_packets += count;
1586 stats->rx_dropped += dropped;
1587 for (i = 0; i < count; i++) {
1588 packet = packets[i];
1589 packet_size = dp_packet_size(packet);
1590
1591 if (OVS_UNLIKELY(packet_size < ETH_HEADER_LEN)) {
1592 /* This only protects the following multicast counting from
1593 * too short packets, but it does not stop the packet from
1594 * further processing. */
1595 stats->rx_errors++;
1596 stats->rx_length_errors++;
1597 continue;
1598 }
1599
1600 netdev_dpdk_vhost_update_rx_size_counters(stats, packet_size);
1601
1602 struct eth_header *eh = (struct eth_header *) dp_packet_data(packet);
1603 if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) {
1604 stats->multicast++;
1605 }
1606
1607 stats->rx_bytes += packet_size;
1608 }
1609 }
1610
1611 /*
1612 * The receive path for the vhost port is the TX path out from guest.
1613 */
1614 static int
1615 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq,
1616 struct dp_packet_batch *batch)
1617 {
1618 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1619 int qid = rxq->queue_id;
1620 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
1621 uint16_t nb_rx = 0;
1622 uint16_t dropped = 0;
1623
1624 if (OVS_UNLIKELY(!is_vhost_running(dev)
1625 || !(dev->flags & NETDEV_UP))) {
1626 return EAGAIN;
1627 }
1628
1629 nb_rx = rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev),
1630 qid * VIRTIO_QNUM + VIRTIO_TXQ,
1631 dev->mp,
1632 (struct rte_mbuf **) batch->packets,
1633 NETDEV_MAX_BURST);
1634 if (!nb_rx) {
1635 return EAGAIN;
1636 }
1637
1638 if (policer) {
1639 dropped = nb_rx;
1640 nb_rx = ingress_policer_run(policer,
1641 (struct rte_mbuf **) batch->packets,
1642 nb_rx, true);
1643 dropped -= nb_rx;
1644 }
1645
1646 rte_spinlock_lock(&dev->stats_lock);
1647 netdev_dpdk_vhost_update_rx_counters(&dev->stats, batch->packets,
1648 nb_rx, dropped);
1649 rte_spinlock_unlock(&dev->stats_lock);
1650
1651 batch->count = nb_rx;
1652 dp_packet_batch_init_packet_fields(batch);
1653
1654 return 0;
1655 }
1656
1657 static int
1658 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch)
1659 {
1660 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1661 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1662 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
1663 int nb_rx;
1664 int dropped = 0;
1665
1666 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
1667 return EAGAIN;
1668 }
1669
1670 nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id,
1671 (struct rte_mbuf **) batch->packets,
1672 NETDEV_MAX_BURST);
1673 if (!nb_rx) {
1674 return EAGAIN;
1675 }
1676
1677 if (policer) {
1678 dropped = nb_rx;
1679 nb_rx = ingress_policer_run(policer,
1680 (struct rte_mbuf **) batch->packets,
1681 nb_rx, true);
1682 dropped -= nb_rx;
1683 }
1684
1685 /* Update stats to reflect dropped packets */
1686 if (OVS_UNLIKELY(dropped)) {
1687 rte_spinlock_lock(&dev->stats_lock);
1688 dev->stats.rx_dropped += dropped;
1689 rte_spinlock_unlock(&dev->stats_lock);
1690 }
1691
1692 batch->count = nb_rx;
1693 dp_packet_batch_init_packet_fields(batch);
1694
1695 return 0;
1696 }
1697
1698 static inline int
1699 netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1700 int cnt, bool may_steal)
1701 {
1702 struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf);
1703
1704 if (qos_conf) {
1705 rte_spinlock_lock(&qos_conf->lock);
1706 cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt, may_steal);
1707 rte_spinlock_unlock(&qos_conf->lock);
1708 }
1709
1710 return cnt;
1711 }
1712
1713 static int
1714 netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1715 int pkt_cnt)
1716 {
1717 int i = 0;
1718 int cnt = 0;
1719 struct rte_mbuf *pkt;
1720
1721 for (i = 0; i < pkt_cnt; i++) {
1722 pkt = pkts[i];
1723 if (OVS_UNLIKELY(pkt->pkt_len > dev->max_packet_len)) {
1724 VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " max_packet_len %d",
1725 dev->up.name, pkt->pkt_len, dev->max_packet_len);
1726 rte_pktmbuf_free(pkt);
1727 continue;
1728 }
1729
1730 if (OVS_UNLIKELY(i != cnt)) {
1731 pkts[cnt] = pkt;
1732 }
1733 cnt++;
1734 }
1735
1736 return cnt;
1737 }
1738
1739 static inline void
1740 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
1741 struct dp_packet **packets,
1742 int attempted,
1743 int dropped)
1744 {
1745 int i;
1746 int sent = attempted - dropped;
1747
1748 stats->tx_packets += sent;
1749 stats->tx_dropped += dropped;
1750
1751 for (i = 0; i < sent; i++) {
1752 stats->tx_bytes += dp_packet_size(packets[i]);
1753 }
1754 }
1755
1756 static void
1757 __netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1758 struct dp_packet **pkts, int cnt)
1759 {
1760 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1761 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
1762 unsigned int total_pkts = cnt;
1763 unsigned int dropped = 0;
1764 int i, retries = 0;
1765
1766 qid = dev->tx_q[qid % netdev->n_txq].map;
1767
1768 if (OVS_UNLIKELY(!is_vhost_running(dev) || qid < 0
1769 || !(dev->flags & NETDEV_UP))) {
1770 rte_spinlock_lock(&dev->stats_lock);
1771 dev->stats.tx_dropped+= cnt;
1772 rte_spinlock_unlock(&dev->stats_lock);
1773 goto out;
1774 }
1775
1776 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1777
1778 cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt);
1779 /* Check has QoS has been configured for the netdev */
1780 cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt, true);
1781 dropped = total_pkts - cnt;
1782
1783 do {
1784 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
1785 unsigned int tx_pkts;
1786
1787 tx_pkts = rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev),
1788 vhost_qid, cur_pkts, cnt);
1789 if (OVS_LIKELY(tx_pkts)) {
1790 /* Packets have been sent.*/
1791 cnt -= tx_pkts;
1792 /* Prepare for possible retry.*/
1793 cur_pkts = &cur_pkts[tx_pkts];
1794 } else {
1795 /* No packets sent - do not retry.*/
1796 break;
1797 }
1798 } while (cnt && (retries++ <= VHOST_ENQ_RETRY_NUM));
1799
1800 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1801
1802 rte_spinlock_lock(&dev->stats_lock);
1803 netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts,
1804 cnt + dropped);
1805 rte_spinlock_unlock(&dev->stats_lock);
1806
1807 out:
1808 for (i = 0; i < total_pkts - dropped; i++) {
1809 dp_packet_delete(pkts[i]);
1810 }
1811 }
1812
1813 /* Tx function. Transmit packets indefinitely */
1814 static void
1815 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)
1816 OVS_NO_THREAD_SAFETY_ANALYSIS
1817 {
1818 const size_t batch_cnt = dp_packet_batch_size(batch);
1819 #if !defined(__CHECKER__) && !defined(_WIN32)
1820 const size_t PKT_ARRAY_SIZE = batch_cnt;
1821 #else
1822 /* Sparse or MSVC doesn't like variable length array. */
1823 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
1824 #endif
1825 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1826 struct rte_mbuf *pkts[PKT_ARRAY_SIZE];
1827 uint32_t cnt = batch_cnt;
1828 uint32_t dropped = 0;
1829
1830 if (dev->type != DPDK_DEV_VHOST) {
1831 /* Check if QoS has been configured for this netdev. */
1832 cnt = netdev_dpdk_qos_run(dev, (struct rte_mbuf **) batch->packets,
1833 batch_cnt, false);
1834 dropped += batch_cnt - cnt;
1835 }
1836
1837 dp_packet_batch_apply_cutlen(batch);
1838
1839 uint32_t txcnt = 0;
1840
1841 for (uint32_t i = 0; i < cnt; i++) {
1842 struct dp_packet *packet = batch->packets[i];
1843 uint32_t size = dp_packet_size(packet);
1844
1845 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1846 VLOG_WARN_RL(&rl, "Too big size %u max_packet_len %d",
1847 size, dev->max_packet_len);
1848
1849 dropped++;
1850 continue;
1851 }
1852
1853 pkts[txcnt] = rte_pktmbuf_alloc(dev->mp);
1854 if (OVS_UNLIKELY(!pkts[txcnt])) {
1855 dropped += cnt - i;
1856 break;
1857 }
1858
1859 /* We have to do a copy for now */
1860 memcpy(rte_pktmbuf_mtod(pkts[txcnt], void *),
1861 dp_packet_data(packet), size);
1862 dp_packet_set_size((struct dp_packet *)pkts[txcnt], size);
1863
1864 txcnt++;
1865 }
1866
1867 if (OVS_LIKELY(txcnt)) {
1868 if (dev->type == DPDK_DEV_VHOST) {
1869 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) pkts,
1870 txcnt);
1871 } else {
1872 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, txcnt);
1873 }
1874 }
1875
1876 if (OVS_UNLIKELY(dropped)) {
1877 rte_spinlock_lock(&dev->stats_lock);
1878 dev->stats.tx_dropped += dropped;
1879 rte_spinlock_unlock(&dev->stats_lock);
1880 }
1881 }
1882
1883 static int
1884 netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1885 struct dp_packet_batch *batch,
1886 bool may_steal, bool concurrent_txq OVS_UNUSED)
1887 {
1888
1889 if (OVS_UNLIKELY(!may_steal || batch->packets[0]->source != DPBUF_DPDK)) {
1890 dpdk_do_tx_copy(netdev, qid, batch);
1891 dp_packet_delete_batch(batch, may_steal);
1892 } else {
1893 dp_packet_batch_apply_cutlen(batch);
1894 __netdev_dpdk_vhost_send(netdev, qid, batch->packets, batch->count);
1895 }
1896 return 0;
1897 }
1898
1899 static inline void
1900 netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
1901 struct dp_packet_batch *batch, bool may_steal,
1902 bool concurrent_txq)
1903 {
1904 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
1905 dp_packet_delete_batch(batch, may_steal);
1906 return;
1907 }
1908
1909 if (OVS_UNLIKELY(concurrent_txq)) {
1910 qid = qid % dev->up.n_txq;
1911 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1912 }
1913
1914 if (OVS_UNLIKELY(!may_steal ||
1915 batch->packets[0]->source != DPBUF_DPDK)) {
1916 struct netdev *netdev = &dev->up;
1917
1918 dpdk_do_tx_copy(netdev, qid, batch);
1919 dp_packet_delete_batch(batch, may_steal);
1920 } else {
1921 int tx_cnt, dropped;
1922 int batch_cnt = dp_packet_batch_size(batch);
1923 struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets;
1924
1925 dp_packet_batch_apply_cutlen(batch);
1926
1927 tx_cnt = netdev_dpdk_filter_packet_len(dev, pkts, batch_cnt);
1928 tx_cnt = netdev_dpdk_qos_run(dev, pkts, tx_cnt, true);
1929 dropped = batch_cnt - tx_cnt;
1930
1931 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, tx_cnt);
1932
1933 if (OVS_UNLIKELY(dropped)) {
1934 rte_spinlock_lock(&dev->stats_lock);
1935 dev->stats.tx_dropped += dropped;
1936 rte_spinlock_unlock(&dev->stats_lock);
1937 }
1938 }
1939
1940 if (OVS_UNLIKELY(concurrent_txq)) {
1941 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1942 }
1943 }
1944
1945 static int
1946 netdev_dpdk_eth_send(struct netdev *netdev, int qid,
1947 struct dp_packet_batch *batch, bool may_steal,
1948 bool concurrent_txq)
1949 {
1950 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1951
1952 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
1953 return 0;
1954 }
1955
1956 static int
1957 netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1958 {
1959 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1960
1961 ovs_mutex_lock(&dev->mutex);
1962 if (!eth_addr_equals(dev->hwaddr, mac)) {
1963 dev->hwaddr = mac;
1964 netdev_change_seq_changed(netdev);
1965 }
1966 ovs_mutex_unlock(&dev->mutex);
1967
1968 return 0;
1969 }
1970
1971 static int
1972 netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1973 {
1974 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1975
1976 ovs_mutex_lock(&dev->mutex);
1977 *mac = dev->hwaddr;
1978 ovs_mutex_unlock(&dev->mutex);
1979
1980 return 0;
1981 }
1982
1983 static int
1984 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1985 {
1986 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1987
1988 ovs_mutex_lock(&dev->mutex);
1989 *mtup = dev->mtu;
1990 ovs_mutex_unlock(&dev->mutex);
1991
1992 return 0;
1993 }
1994
1995 static int
1996 netdev_dpdk_set_mtu(struct netdev *netdev, int mtu)
1997 {
1998 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1999
2000 if (MTU_TO_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN
2001 || mtu < ETHER_MIN_MTU) {
2002 VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu);
2003 return EINVAL;
2004 }
2005
2006 ovs_mutex_lock(&dev->mutex);
2007 if (dev->requested_mtu != mtu) {
2008 dev->requested_mtu = mtu;
2009 netdev_request_reconfigure(netdev);
2010 }
2011 ovs_mutex_unlock(&dev->mutex);
2012
2013 return 0;
2014 }
2015
2016 static int
2017 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier);
2018
2019 static int
2020 netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
2021 struct netdev_stats *stats)
2022 {
2023 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2024
2025 ovs_mutex_lock(&dev->mutex);
2026
2027 rte_spinlock_lock(&dev->stats_lock);
2028 /* Supported Stats */
2029 stats->rx_packets = dev->stats.rx_packets;
2030 stats->tx_packets = dev->stats.tx_packets;
2031 stats->rx_dropped = dev->stats.rx_dropped;
2032 stats->tx_dropped = dev->stats.tx_dropped;
2033 stats->multicast = dev->stats.multicast;
2034 stats->rx_bytes = dev->stats.rx_bytes;
2035 stats->tx_bytes = dev->stats.tx_bytes;
2036 stats->rx_errors = dev->stats.rx_errors;
2037 stats->rx_length_errors = dev->stats.rx_length_errors;
2038
2039 stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets;
2040 stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets;
2041 stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets;
2042 stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets;
2043 stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets;
2044 stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets;
2045 stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets;
2046
2047 rte_spinlock_unlock(&dev->stats_lock);
2048
2049 ovs_mutex_unlock(&dev->mutex);
2050
2051 return 0;
2052 }
2053
2054 static void
2055 netdev_dpdk_convert_xstats(struct netdev_stats *stats,
2056 const struct rte_eth_xstat *xstats,
2057 const struct rte_eth_xstat_name *names,
2058 const unsigned int size)
2059 {
2060 for (unsigned int i = 0; i < size; i++) {
2061 if (strcmp(XSTAT_RX_64_PACKETS, names[i].name) == 0) {
2062 stats->rx_1_to_64_packets = xstats[i].value;
2063 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS, names[i].name) == 0) {
2064 stats->rx_65_to_127_packets = xstats[i].value;
2065 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS, names[i].name) == 0) {
2066 stats->rx_128_to_255_packets = xstats[i].value;
2067 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS, names[i].name) == 0) {
2068 stats->rx_256_to_511_packets = xstats[i].value;
2069 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS, names[i].name) == 0) {
2070 stats->rx_512_to_1023_packets = xstats[i].value;
2071 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS, names[i].name) == 0) {
2072 stats->rx_1024_to_1522_packets = xstats[i].value;
2073 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
2074 stats->rx_1523_to_max_packets = xstats[i].value;
2075 } else if (strcmp(XSTAT_TX_64_PACKETS, names[i].name) == 0) {
2076 stats->tx_1_to_64_packets = xstats[i].value;
2077 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS, names[i].name) == 0) {
2078 stats->tx_65_to_127_packets = xstats[i].value;
2079 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS, names[i].name) == 0) {
2080 stats->tx_128_to_255_packets = xstats[i].value;
2081 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS, names[i].name) == 0) {
2082 stats->tx_256_to_511_packets = xstats[i].value;
2083 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS, names[i].name) == 0) {
2084 stats->tx_512_to_1023_packets = xstats[i].value;
2085 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS, names[i].name) == 0) {
2086 stats->tx_1024_to_1522_packets = xstats[i].value;
2087 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
2088 stats->tx_1523_to_max_packets = xstats[i].value;
2089 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS, names[i].name) == 0) {
2090 stats->multicast = xstats[i].value;
2091 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS, names[i].name) == 0) {
2092 stats->tx_multicast_packets = xstats[i].value;
2093 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS, names[i].name) == 0) {
2094 stats->rx_broadcast_packets = xstats[i].value;
2095 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS, names[i].name) == 0) {
2096 stats->tx_broadcast_packets = xstats[i].value;
2097 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS, names[i].name) == 0) {
2098 stats->rx_undersized_errors = xstats[i].value;
2099 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS, names[i].name) == 0) {
2100 stats->rx_fragmented_errors = xstats[i].value;
2101 } else if (strcmp(XSTAT_RX_JABBER_ERRORS, names[i].name) == 0) {
2102 stats->rx_jabber_errors = xstats[i].value;
2103 }
2104 }
2105 }
2106
2107 static int
2108 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
2109 {
2110 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2111 struct rte_eth_stats rte_stats;
2112 bool gg;
2113
2114 netdev_dpdk_get_carrier(netdev, &gg);
2115 ovs_mutex_lock(&dev->mutex);
2116
2117 struct rte_eth_xstat *rte_xstats = NULL;
2118 struct rte_eth_xstat_name *rte_xstats_names = NULL;
2119 int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret;
2120
2121 if (rte_eth_stats_get(dev->port_id, &rte_stats)) {
2122 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8, dev->port_id);
2123 ovs_mutex_unlock(&dev->mutex);
2124 return EPROTO;
2125 }
2126
2127 /* Get length of statistics */
2128 rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0);
2129 if (rte_xstats_len < 0) {
2130 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8, dev->port_id);
2131 goto out;
2132 }
2133 /* Reserve memory for xstats names and values */
2134 rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names);
2135 rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats);
2136
2137 /* Retreive xstats names */
2138 rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id,
2139 rte_xstats_names,
2140 rte_xstats_len);
2141 if (rte_xstats_new_len != rte_xstats_len) {
2142 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8, dev->port_id);
2143 goto out;
2144 }
2145 /* Retreive xstats values */
2146 memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len);
2147 rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats,
2148 rte_xstats_len);
2149 if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) {
2150 netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names,
2151 rte_xstats_len);
2152 } else {
2153 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8, dev->port_id);
2154 }
2155
2156 out:
2157 free(rte_xstats);
2158 free(rte_xstats_names);
2159
2160 stats->rx_packets = rte_stats.ipackets;
2161 stats->tx_packets = rte_stats.opackets;
2162 stats->rx_bytes = rte_stats.ibytes;
2163 stats->tx_bytes = rte_stats.obytes;
2164 stats->rx_errors = rte_stats.ierrors;
2165 stats->tx_errors = rte_stats.oerrors;
2166
2167 rte_spinlock_lock(&dev->stats_lock);
2168 stats->tx_dropped = dev->stats.tx_dropped;
2169 stats->rx_dropped = dev->stats.rx_dropped;
2170 rte_spinlock_unlock(&dev->stats_lock);
2171
2172 /* These are the available DPDK counters for packets not received due to
2173 * local resource constraints in DPDK and NIC respectively. */
2174 stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed;
2175 stats->rx_missed_errors = rte_stats.imissed;
2176
2177 ovs_mutex_unlock(&dev->mutex);
2178
2179 return 0;
2180 }
2181
2182 static int
2183 netdev_dpdk_get_features(const struct netdev *netdev,
2184 enum netdev_features *current,
2185 enum netdev_features *advertised,
2186 enum netdev_features *supported,
2187 enum netdev_features *peer)
2188 {
2189 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2190 struct rte_eth_link link;
2191
2192 ovs_mutex_lock(&dev->mutex);
2193 link = dev->link;
2194 ovs_mutex_unlock(&dev->mutex);
2195
2196 if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
2197 if (link.link_speed == ETH_SPEED_NUM_10M) {
2198 *current = NETDEV_F_10MB_HD;
2199 }
2200 if (link.link_speed == ETH_SPEED_NUM_100M) {
2201 *current = NETDEV_F_100MB_HD;
2202 }
2203 if (link.link_speed == ETH_SPEED_NUM_1G) {
2204 *current = NETDEV_F_1GB_HD;
2205 }
2206 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
2207 if (link.link_speed == ETH_SPEED_NUM_10M) {
2208 *current = NETDEV_F_10MB_FD;
2209 }
2210 if (link.link_speed == ETH_SPEED_NUM_100M) {
2211 *current = NETDEV_F_100MB_FD;
2212 }
2213 if (link.link_speed == ETH_SPEED_NUM_1G) {
2214 *current = NETDEV_F_1GB_FD;
2215 }
2216 if (link.link_speed == ETH_SPEED_NUM_10G) {
2217 *current = NETDEV_F_10GB_FD;
2218 }
2219 }
2220
2221 if (link.link_autoneg) {
2222 *current |= NETDEV_F_AUTONEG;
2223 }
2224
2225 *advertised = *supported = *peer = 0;
2226
2227 return 0;
2228 }
2229
2230 static struct ingress_policer *
2231 netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst)
2232 {
2233 struct ingress_policer *policer = NULL;
2234 uint64_t rate_bytes;
2235 uint64_t burst_bytes;
2236 int err = 0;
2237
2238 policer = xmalloc(sizeof *policer);
2239 rte_spinlock_init(&policer->policer_lock);
2240
2241 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2242 rate_bytes = rate * 1000ULL / 8;
2243 burst_bytes = burst * 1000ULL / 8;
2244
2245 policer->app_srtcm_params.cir = rate_bytes;
2246 policer->app_srtcm_params.cbs = burst_bytes;
2247 policer->app_srtcm_params.ebs = 0;
2248 err = rte_meter_srtcm_config(&policer->in_policer,
2249 &policer->app_srtcm_params);
2250 if (err) {
2251 VLOG_ERR("Could not create rte meter for ingress policer");
2252 return NULL;
2253 }
2254
2255 return policer;
2256 }
2257
2258 static int
2259 netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate,
2260 uint32_t policer_burst)
2261 {
2262 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2263 struct ingress_policer *policer;
2264
2265 /* Force to 0 if no rate specified,
2266 * default to 8000 kbits if burst is 0,
2267 * else stick with user-specified value.
2268 */
2269 policer_burst = (!policer_rate ? 0
2270 : !policer_burst ? 8000
2271 : policer_burst);
2272
2273 ovs_mutex_lock(&dev->mutex);
2274
2275 policer = ovsrcu_get_protected(struct ingress_policer *,
2276 &dev->ingress_policer);
2277
2278 if (dev->policer_rate == policer_rate &&
2279 dev->policer_burst == policer_burst) {
2280 /* Assume that settings haven't changed since we last set them. */
2281 ovs_mutex_unlock(&dev->mutex);
2282 return 0;
2283 }
2284
2285 /* Destroy any existing ingress policer for the device if one exists */
2286 if (policer) {
2287 ovsrcu_postpone(free, policer);
2288 }
2289
2290 if (policer_rate != 0) {
2291 policer = netdev_dpdk_policer_construct(policer_rate, policer_burst);
2292 } else {
2293 policer = NULL;
2294 }
2295 ovsrcu_set(&dev->ingress_policer, policer);
2296 dev->policer_rate = policer_rate;
2297 dev->policer_burst = policer_burst;
2298 ovs_mutex_unlock(&dev->mutex);
2299
2300 return 0;
2301 }
2302
2303 static int
2304 netdev_dpdk_get_ifindex(const struct netdev *netdev)
2305 {
2306 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2307
2308 ovs_mutex_lock(&dev->mutex);
2309 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2310 * postive integer to meet RFC 2863 recommendations.
2311 */
2312 int ifindex = hash_string(netdev->name, 0) % 0xfffffe + 1;
2313 ovs_mutex_unlock(&dev->mutex);
2314
2315 return ifindex;
2316 }
2317
2318 static int
2319 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier)
2320 {
2321 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2322
2323 ovs_mutex_lock(&dev->mutex);
2324 check_link_status(dev);
2325 *carrier = dev->link.link_status;
2326
2327 ovs_mutex_unlock(&dev->mutex);
2328
2329 return 0;
2330 }
2331
2332 static int
2333 netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier)
2334 {
2335 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2336
2337 ovs_mutex_lock(&dev->mutex);
2338
2339 if (is_vhost_running(dev)) {
2340 *carrier = 1;
2341 } else {
2342 *carrier = 0;
2343 }
2344
2345 ovs_mutex_unlock(&dev->mutex);
2346
2347 return 0;
2348 }
2349
2350 static long long int
2351 netdev_dpdk_get_carrier_resets(const struct netdev *netdev)
2352 {
2353 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2354 long long int carrier_resets;
2355
2356 ovs_mutex_lock(&dev->mutex);
2357 carrier_resets = dev->link_reset_cnt;
2358 ovs_mutex_unlock(&dev->mutex);
2359
2360 return carrier_resets;
2361 }
2362
2363 static int
2364 netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED,
2365 long long int interval OVS_UNUSED)
2366 {
2367 return EOPNOTSUPP;
2368 }
2369
2370 static int
2371 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
2372 enum netdev_flags off, enum netdev_flags on,
2373 enum netdev_flags *old_flagsp)
2374 OVS_REQUIRES(dev->mutex)
2375 {
2376 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
2377 return EINVAL;
2378 }
2379
2380 *old_flagsp = dev->flags;
2381 dev->flags |= on;
2382 dev->flags &= ~off;
2383
2384 if (dev->flags == *old_flagsp) {
2385 return 0;
2386 }
2387
2388 if (dev->type == DPDK_DEV_ETH) {
2389 if (dev->flags & NETDEV_PROMISC) {
2390 rte_eth_promiscuous_enable(dev->port_id);
2391 }
2392
2393 netdev_change_seq_changed(&dev->up);
2394 } else {
2395 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2396 * running then change netdev's change_seq to trigger link state
2397 * update. */
2398
2399 if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off)))
2400 && is_vhost_running(dev)) {
2401 netdev_change_seq_changed(&dev->up);
2402
2403 /* Clear statistics if device is getting up. */
2404 if (NETDEV_UP & on) {
2405 rte_spinlock_lock(&dev->stats_lock);
2406 memset(&dev->stats, 0, sizeof dev->stats);
2407 rte_spinlock_unlock(&dev->stats_lock);
2408 }
2409 }
2410 }
2411
2412 return 0;
2413 }
2414
2415 static int
2416 netdev_dpdk_update_flags(struct netdev *netdev,
2417 enum netdev_flags off, enum netdev_flags on,
2418 enum netdev_flags *old_flagsp)
2419 {
2420 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2421 int error;
2422
2423 ovs_mutex_lock(&dev->mutex);
2424 error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp);
2425 ovs_mutex_unlock(&dev->mutex);
2426
2427 return error;
2428 }
2429
2430 static int
2431 netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args)
2432 {
2433 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2434 struct rte_eth_dev_info dev_info;
2435
2436 if (!rte_eth_dev_is_valid_port(dev->port_id)) {
2437 return ENODEV;
2438 }
2439
2440 ovs_mutex_lock(&dev->mutex);
2441 rte_eth_dev_info_get(dev->port_id, &dev_info);
2442 ovs_mutex_unlock(&dev->mutex);
2443
2444 smap_add_format(args, "port_no", "%d", dev->port_id);
2445 smap_add_format(args, "numa_id", "%d",
2446 rte_eth_dev_socket_id(dev->port_id));
2447 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
2448 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
2449 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
2450 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
2451 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
2452 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
2453 smap_add_format(args, "max_hash_mac_addrs", "%u",
2454 dev_info.max_hash_mac_addrs);
2455 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
2456 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
2457
2458 if (dev_info.pci_dev) {
2459 smap_add_format(args, "pci-vendor_id", "0x%u",
2460 dev_info.pci_dev->id.vendor_id);
2461 smap_add_format(args, "pci-device_id", "0x%x",
2462 dev_info.pci_dev->id.device_id);
2463 }
2464
2465 return 0;
2466 }
2467
2468 static void
2469 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
2470 OVS_REQUIRES(dev->mutex)
2471 {
2472 enum netdev_flags old_flags;
2473
2474 if (admin_state) {
2475 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
2476 } else {
2477 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
2478 }
2479 }
2480
2481 static void
2482 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
2483 const char *argv[], void *aux OVS_UNUSED)
2484 {
2485 bool up;
2486
2487 if (!strcasecmp(argv[argc - 1], "up")) {
2488 up = true;
2489 } else if ( !strcasecmp(argv[argc - 1], "down")) {
2490 up = false;
2491 } else {
2492 unixctl_command_reply_error(conn, "Invalid Admin State");
2493 return;
2494 }
2495
2496 if (argc > 2) {
2497 struct netdev *netdev = netdev_from_name(argv[1]);
2498 if (netdev && is_dpdk_class(netdev->netdev_class)) {
2499 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
2500
2501 ovs_mutex_lock(&dpdk_dev->mutex);
2502 netdev_dpdk_set_admin_state__(dpdk_dev, up);
2503 ovs_mutex_unlock(&dpdk_dev->mutex);
2504
2505 netdev_close(netdev);
2506 } else {
2507 unixctl_command_reply_error(conn, "Not a DPDK Interface");
2508 netdev_close(netdev);
2509 return;
2510 }
2511 } else {
2512 struct netdev_dpdk *netdev;
2513
2514 ovs_mutex_lock(&dpdk_mutex);
2515 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
2516 ovs_mutex_lock(&netdev->mutex);
2517 netdev_dpdk_set_admin_state__(netdev, up);
2518 ovs_mutex_unlock(&netdev->mutex);
2519 }
2520 ovs_mutex_unlock(&dpdk_mutex);
2521 }
2522 unixctl_command_reply(conn, "OK");
2523 }
2524
2525 static void
2526 netdev_dpdk_detach(struct unixctl_conn *conn, int argc OVS_UNUSED,
2527 const char *argv[], void *aux OVS_UNUSED)
2528 {
2529 int ret;
2530 char *response;
2531 dpdk_port_t port_id;
2532 char devname[RTE_ETH_NAME_MAX_LEN];
2533 struct netdev_dpdk *dev;
2534
2535 ovs_mutex_lock(&dpdk_mutex);
2536
2537 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv[1],
2538 &port_id)) {
2539 response = xasprintf("Device '%s' not found in DPDK", argv[1]);
2540 goto error;
2541 }
2542
2543 dev = netdev_dpdk_lookup_by_port_id(port_id);
2544 if (dev) {
2545 response = xasprintf("Device '%s' is being used by interface '%s'. "
2546 "Remove it before detaching",
2547 argv[1], netdev_get_name(&dev->up));
2548 goto error;
2549 }
2550
2551 rte_eth_dev_close(port_id);
2552
2553 ret = rte_eth_dev_detach(port_id, devname);
2554 if (ret < 0) {
2555 response = xasprintf("Device '%s' can not be detached", argv[1]);
2556 goto error;
2557 }
2558
2559 response = xasprintf("Device '%s' has been detached", argv[1]);
2560
2561 ovs_mutex_unlock(&dpdk_mutex);
2562 unixctl_command_reply(conn, response);
2563 free(response);
2564 return;
2565
2566 error:
2567 ovs_mutex_unlock(&dpdk_mutex);
2568 unixctl_command_reply_error(conn, response);
2569 free(response);
2570 }
2571
2572 /*
2573 * Set virtqueue flags so that we do not receive interrupts.
2574 */
2575 static void
2576 set_irq_status(int vid)
2577 {
2578 uint32_t i;
2579
2580 for (i = 0; i < rte_vhost_get_vring_num(vid); i++) {
2581 rte_vhost_enable_guest_notification(vid, i, 0);
2582 }
2583 }
2584
2585 /*
2586 * Fixes mapping for vhost-user tx queues. Must be called after each
2587 * enabling/disabling of queues and n_txq modifications.
2588 */
2589 static void
2590 netdev_dpdk_remap_txqs(struct netdev_dpdk *dev)
2591 OVS_REQUIRES(dev->mutex)
2592 {
2593 int *enabled_queues, n_enabled = 0;
2594 int i, k, total_txqs = dev->up.n_txq;
2595
2596 enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues);
2597
2598 for (i = 0; i < total_txqs; i++) {
2599 /* Enabled queues always mapped to themselves. */
2600 if (dev->tx_q[i].map == i) {
2601 enabled_queues[n_enabled++] = i;
2602 }
2603 }
2604
2605 if (n_enabled == 0 && total_txqs != 0) {
2606 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
2607 n_enabled = 1;
2608 }
2609
2610 k = 0;
2611 for (i = 0; i < total_txqs; i++) {
2612 if (dev->tx_q[i].map != i) {
2613 dev->tx_q[i].map = enabled_queues[k];
2614 k = (k + 1) % n_enabled;
2615 }
2616 }
2617
2618 VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id);
2619 for (i = 0; i < total_txqs; i++) {
2620 VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map);
2621 }
2622
2623 free(enabled_queues);
2624 }
2625
2626 /*
2627 * A new virtio-net device is added to a vhost port.
2628 */
2629 static int
2630 new_device(int vid)
2631 {
2632 struct netdev_dpdk *dev;
2633 bool exists = false;
2634 int newnode = 0;
2635 char ifname[IF_NAME_SZ];
2636
2637 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2638
2639 ovs_mutex_lock(&dpdk_mutex);
2640 /* Add device to the vhost port with the same name as that passed down. */
2641 LIST_FOR_EACH(dev, list_node, &dpdk_list) {
2642 ovs_mutex_lock(&dev->mutex);
2643 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2644 uint32_t qp_num = rte_vhost_get_vring_num(vid)/VIRTIO_QNUM;
2645
2646 /* Get NUMA information */
2647 newnode = rte_vhost_get_numa_node(vid);
2648 if (newnode == -1) {
2649 #ifdef VHOST_NUMA
2650 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2651 ifname);
2652 #endif
2653 newnode = dev->socket_id;
2654 }
2655
2656 if (dev->requested_n_txq != qp_num
2657 || dev->requested_n_rxq != qp_num
2658 || dev->requested_socket_id != newnode) {
2659 dev->requested_socket_id = newnode;
2660 dev->requested_n_rxq = qp_num;
2661 dev->requested_n_txq = qp_num;
2662 netdev_request_reconfigure(&dev->up);
2663 } else {
2664 /* Reconfiguration not required. */
2665 dev->vhost_reconfigured = true;
2666 }
2667
2668 ovsrcu_index_set(&dev->vid, vid);
2669 exists = true;
2670
2671 /* Disable notifications. */
2672 set_irq_status(vid);
2673 netdev_change_seq_changed(&dev->up);
2674 ovs_mutex_unlock(&dev->mutex);
2675 break;
2676 }
2677 ovs_mutex_unlock(&dev->mutex);
2678 }
2679 ovs_mutex_unlock(&dpdk_mutex);
2680
2681 if (!exists) {
2682 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname);
2683
2684 return -1;
2685 }
2686
2687 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2688 ifname, newnode);
2689
2690 return 0;
2691 }
2692
2693 /* Clears mapping for all available queues of vhost interface. */
2694 static void
2695 netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
2696 OVS_REQUIRES(dev->mutex)
2697 {
2698 int i;
2699
2700 for (i = 0; i < dev->up.n_txq; i++) {
2701 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
2702 }
2703 }
2704
2705 /*
2706 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2707 * flag to stop any more packets from being sent or received to/from a VM and
2708 * ensure all currently queued packets have been sent/received before removing
2709 * the device.
2710 */
2711 static void
2712 destroy_device(int vid)
2713 {
2714 struct netdev_dpdk *dev;
2715 bool exists = false;
2716 char ifname[IF_NAME_SZ];
2717
2718 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2719
2720 ovs_mutex_lock(&dpdk_mutex);
2721 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2722 if (netdev_dpdk_get_vid(dev) == vid) {
2723
2724 ovs_mutex_lock(&dev->mutex);
2725 dev->vhost_reconfigured = false;
2726 ovsrcu_index_set(&dev->vid, -1);
2727 netdev_dpdk_txq_map_clear(dev);
2728
2729 netdev_change_seq_changed(&dev->up);
2730 ovs_mutex_unlock(&dev->mutex);
2731 exists = true;
2732 break;
2733 }
2734 }
2735
2736 ovs_mutex_unlock(&dpdk_mutex);
2737
2738 if (exists) {
2739 /*
2740 * Wait for other threads to quiesce after setting the 'virtio_dev'
2741 * to NULL, before returning.
2742 */
2743 ovsrcu_synchronize();
2744 /*
2745 * As call to ovsrcu_synchronize() will end the quiescent state,
2746 * put thread back into quiescent state before returning.
2747 */
2748 ovsrcu_quiesce_start();
2749 VLOG_INFO("vHost Device '%s' has been removed", ifname);
2750 } else {
2751 VLOG_INFO("vHost Device '%s' not found", ifname);
2752 }
2753 }
2754
2755 static int
2756 vring_state_changed(int vid, uint16_t queue_id, int enable)
2757 {
2758 struct netdev_dpdk *dev;
2759 bool exists = false;
2760 int qid = queue_id / VIRTIO_QNUM;
2761 char ifname[IF_NAME_SZ];
2762
2763 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
2764
2765 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
2766 return 0;
2767 }
2768
2769 ovs_mutex_lock(&dpdk_mutex);
2770 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2771 ovs_mutex_lock(&dev->mutex);
2772 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2773 if (enable) {
2774 dev->tx_q[qid].map = qid;
2775 } else {
2776 dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
2777 }
2778 netdev_dpdk_remap_txqs(dev);
2779 exists = true;
2780 ovs_mutex_unlock(&dev->mutex);
2781 break;
2782 }
2783 ovs_mutex_unlock(&dev->mutex);
2784 }
2785 ovs_mutex_unlock(&dpdk_mutex);
2786
2787 if (exists) {
2788 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2789 "changed to \'%s\'", queue_id, qid, ifname,
2790 (enable == 1) ? "enabled" : "disabled");
2791 } else {
2792 VLOG_INFO("vHost Device '%s' not found", ifname);
2793 return -1;
2794 }
2795
2796 return 0;
2797 }
2798
2799 int
2800 netdev_dpdk_get_vid(const struct netdev_dpdk *dev)
2801 {
2802 return ovsrcu_index_get(&dev->vid);
2803 }
2804
2805 struct ingress_policer *
2806 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev)
2807 {
2808 return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer);
2809 }
2810
2811 static int
2812 netdev_dpdk_class_init(void)
2813 {
2814 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2815
2816 /* This function can be called for different classes. The initialization
2817 * needs to be done only once */
2818 if (ovsthread_once_start(&once)) {
2819 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
2820 unixctl_command_register("netdev-dpdk/set-admin-state",
2821 "[netdev] up|down", 1, 2,
2822 netdev_dpdk_set_admin_state, NULL);
2823
2824 unixctl_command_register("netdev-dpdk/detach",
2825 "pci address of device", 1, 1,
2826 netdev_dpdk_detach, NULL);
2827
2828 ovsthread_once_done(&once);
2829 }
2830
2831 return 0;
2832 }
2833
2834
2835 /* Client Rings */
2836
2837 static int
2838 dpdk_ring_create(const char dev_name[], unsigned int port_no,
2839 dpdk_port_t *eth_port_id)
2840 {
2841 struct dpdk_ring *ring_pair;
2842 char *ring_name;
2843 int port_id;
2844
2845 ring_pair = dpdk_rte_mzalloc(sizeof *ring_pair);
2846 if (!ring_pair) {
2847 return ENOMEM;
2848 }
2849
2850 /* XXX: Add support for multiquque ring. */
2851 ring_name = xasprintf("%s_tx", dev_name);
2852
2853 /* Create single producer tx ring, netdev does explicit locking. */
2854 ring_pair->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2855 RING_F_SP_ENQ);
2856 free(ring_name);
2857 if (ring_pair->cring_tx == NULL) {
2858 rte_free(ring_pair);
2859 return ENOMEM;
2860 }
2861
2862 ring_name = xasprintf("%s_rx", dev_name);
2863
2864 /* Create single consumer rx ring, netdev does explicit locking. */
2865 ring_pair->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2866 RING_F_SC_DEQ);
2867 free(ring_name);
2868 if (ring_pair->cring_rx == NULL) {
2869 rte_free(ring_pair);
2870 return ENOMEM;
2871 }
2872
2873 port_id = rte_eth_from_rings(dev_name, &ring_pair->cring_rx, 1,
2874 &ring_pair->cring_tx, 1, SOCKET0);
2875
2876 if (port_id < 0) {
2877 rte_free(ring_pair);
2878 return ENODEV;
2879 }
2880
2881 ring_pair->user_port_id = port_no;
2882 ring_pair->eth_port_id = port_id;
2883 *eth_port_id = port_id;
2884
2885 ovs_list_push_back(&dpdk_ring_list, &ring_pair->list_node);
2886
2887 return 0;
2888 }
2889
2890 static int
2891 dpdk_ring_open(const char dev_name[], dpdk_port_t *eth_port_id)
2892 OVS_REQUIRES(dpdk_mutex)
2893 {
2894 struct dpdk_ring *ring_pair;
2895 unsigned int port_no;
2896 int err = 0;
2897
2898 /* Names always start with "dpdkr" */
2899 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
2900 if (err) {
2901 return err;
2902 }
2903
2904 /* Look through our list to find the device */
2905 LIST_FOR_EACH (ring_pair, list_node, &dpdk_ring_list) {
2906 if (ring_pair->user_port_id == port_no) {
2907 VLOG_INFO("Found dpdk ring device %s:", dev_name);
2908 /* Really all that is needed */
2909 *eth_port_id = ring_pair->eth_port_id;
2910 return 0;
2911 }
2912 }
2913 /* Need to create the device rings */
2914 return dpdk_ring_create(dev_name, port_no, eth_port_id);
2915 }
2916
2917 static int
2918 netdev_dpdk_ring_send(struct netdev *netdev, int qid,
2919 struct dp_packet_batch *batch, bool may_steal,
2920 bool concurrent_txq)
2921 {
2922 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2923 struct dp_packet *packet;
2924
2925 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2926 * the rss hash field is clear. This is because the same mbuf may be
2927 * modified by the consumer of the ring and return into the datapath
2928 * without recalculating the RSS hash. */
2929 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
2930 dp_packet_mbuf_rss_flag_reset(packet);
2931 }
2932
2933 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
2934 return 0;
2935 }
2936
2937 static int
2938 netdev_dpdk_ring_construct(struct netdev *netdev)
2939 {
2940 dpdk_port_t port_no = 0;
2941 int err = 0;
2942
2943 ovs_mutex_lock(&dpdk_mutex);
2944
2945 err = dpdk_ring_open(netdev->name, &port_no);
2946 if (err) {
2947 goto unlock_dpdk;
2948 }
2949
2950 err = common_construct(netdev, port_no, DPDK_DEV_ETH,
2951 rte_eth_dev_socket_id(port_no));
2952 unlock_dpdk:
2953 ovs_mutex_unlock(&dpdk_mutex);
2954 return err;
2955 }
2956
2957 /* QoS Functions */
2958
2959 /*
2960 * Initialize QoS configuration operations.
2961 */
2962 static void
2963 qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
2964 {
2965 conf->ops = ops;
2966 rte_spinlock_init(&conf->lock);
2967 }
2968
2969 /*
2970 * Search existing QoS operations in qos_ops and compare each set of
2971 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2972 * else return NULL
2973 */
2974 static const struct dpdk_qos_ops *
2975 qos_lookup_name(const char *name)
2976 {
2977 const struct dpdk_qos_ops *const *opsp;
2978
2979 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2980 const struct dpdk_qos_ops *ops = *opsp;
2981 if (!strcmp(name, ops->qos_name)) {
2982 return ops;
2983 }
2984 }
2985 return NULL;
2986 }
2987
2988 static int
2989 netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
2990 struct sset *types)
2991 {
2992 const struct dpdk_qos_ops *const *opsp;
2993
2994 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2995 const struct dpdk_qos_ops *ops = *opsp;
2996 if (ops->qos_construct && ops->qos_name[0] != '\0') {
2997 sset_add(types, ops->qos_name);
2998 }
2999 }
3000 return 0;
3001 }
3002
3003 static int
3004 netdev_dpdk_get_qos(const struct netdev *netdev,
3005 const char **typep, struct smap *details)
3006 {
3007 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3008 struct qos_conf *qos_conf;
3009 int error = 0;
3010
3011 ovs_mutex_lock(&dev->mutex);
3012 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
3013 if (qos_conf) {
3014 *typep = qos_conf->ops->qos_name;
3015 error = (qos_conf->ops->qos_get
3016 ? qos_conf->ops->qos_get(qos_conf, details): 0);
3017 } else {
3018 /* No QoS configuration set, return an empty string */
3019 *typep = "";
3020 }
3021 ovs_mutex_unlock(&dev->mutex);
3022
3023 return error;
3024 }
3025
3026 static int
3027 netdev_dpdk_set_qos(struct netdev *netdev, const char *type,
3028 const struct smap *details)
3029 {
3030 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3031 const struct dpdk_qos_ops *new_ops = NULL;
3032 struct qos_conf *qos_conf, *new_qos_conf = NULL;
3033 int error = 0;
3034
3035 ovs_mutex_lock(&dev->mutex);
3036
3037 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
3038
3039 new_ops = qos_lookup_name(type);
3040
3041 if (!new_ops || !new_ops->qos_construct) {
3042 new_qos_conf = NULL;
3043 if (type && type[0]) {
3044 error = EOPNOTSUPP;
3045 }
3046 } else if (qos_conf && qos_conf->ops == new_ops
3047 && qos_conf->ops->qos_is_equal(qos_conf, details)) {
3048 new_qos_conf = qos_conf;
3049 } else {
3050 error = new_ops->qos_construct(details, &new_qos_conf);
3051 }
3052
3053 if (error) {
3054 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3055 type, netdev->name, rte_strerror(error));
3056 }
3057
3058 if (new_qos_conf != qos_conf) {
3059 ovsrcu_set(&dev->qos_conf, new_qos_conf);
3060 if (qos_conf) {
3061 ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf);
3062 }
3063 }
3064
3065 ovs_mutex_unlock(&dev->mutex);
3066
3067 return error;
3068 }
3069
3070 /* egress-policer details */
3071
3072 struct egress_policer {
3073 struct qos_conf qos_conf;
3074 struct rte_meter_srtcm_params app_srtcm_params;
3075 struct rte_meter_srtcm egress_meter;
3076 };
3077
3078 static void
3079 egress_policer_details_to_param(const struct smap *details,
3080 struct rte_meter_srtcm_params *params)
3081 {
3082 memset(params, 0, sizeof *params);
3083 params->cir = smap_get_ullong(details, "cir", 0);
3084 params->cbs = smap_get_ullong(details, "cbs", 0);
3085 params->ebs = 0;
3086 }
3087
3088 static int
3089 egress_policer_qos_construct(const struct smap *details,
3090 struct qos_conf **conf)
3091 {
3092 struct egress_policer *policer;
3093 int err = 0;
3094
3095 policer = xmalloc(sizeof *policer);
3096 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
3097 egress_policer_details_to_param(details, &policer->app_srtcm_params);
3098 err = rte_meter_srtcm_config(&policer->egress_meter,
3099 &policer->app_srtcm_params);
3100 if (!err) {
3101 *conf = &policer->qos_conf;
3102 } else {
3103 free(policer);
3104 *conf = NULL;
3105 err = -err;
3106 }
3107
3108 return err;
3109 }
3110
3111 static void
3112 egress_policer_qos_destruct(struct qos_conf *conf)
3113 {
3114 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
3115 qos_conf);
3116 free(policer);
3117 }
3118
3119 static int
3120 egress_policer_qos_get(const struct qos_conf *conf, struct smap *details)
3121 {
3122 struct egress_policer *policer =
3123 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3124
3125 smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir);
3126 smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs);
3127
3128 return 0;
3129 }
3130
3131 static bool
3132 egress_policer_qos_is_equal(const struct qos_conf *conf,
3133 const struct smap *details)
3134 {
3135 struct egress_policer *policer =
3136 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3137 struct rte_meter_srtcm_params params;
3138
3139 egress_policer_details_to_param(details, &params);
3140
3141 return !memcmp(&params, &policer->app_srtcm_params, sizeof params);
3142 }
3143
3144 static int
3145 egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt,
3146 bool may_steal)
3147 {
3148 int cnt = 0;
3149 struct egress_policer *policer =
3150 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3151
3152 cnt = netdev_dpdk_policer_run(&policer->egress_meter, pkts,
3153 pkt_cnt, may_steal);
3154
3155 return cnt;
3156 }
3157
3158 static const struct dpdk_qos_ops egress_policer_ops = {
3159 "egress-policer", /* qos_name */
3160 egress_policer_qos_construct,
3161 egress_policer_qos_destruct,
3162 egress_policer_qos_get,
3163 egress_policer_qos_is_equal,
3164 egress_policer_run
3165 };
3166
3167 static int
3168 netdev_dpdk_reconfigure(struct netdev *netdev)
3169 {
3170 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3171 int err = 0;
3172
3173 ovs_mutex_lock(&dev->mutex);
3174
3175 if (netdev->n_txq == dev->requested_n_txq
3176 && netdev->n_rxq == dev->requested_n_rxq
3177 && dev->mtu == dev->requested_mtu
3178 && dev->rxq_size == dev->requested_rxq_size
3179 && dev->txq_size == dev->requested_txq_size
3180 && dev->socket_id == dev->requested_socket_id) {
3181 /* Reconfiguration is unnecessary */
3182
3183 goto out;
3184 }
3185
3186 rte_eth_dev_stop(dev->port_id);
3187
3188 err = netdev_dpdk_mempool_configure(dev);
3189 if (err && err != EEXIST) {
3190 goto out;
3191 }
3192
3193 netdev->n_txq = dev->requested_n_txq;
3194 netdev->n_rxq = dev->requested_n_rxq;
3195
3196 dev->rxq_size = dev->requested_rxq_size;
3197 dev->txq_size = dev->requested_txq_size;
3198
3199 rte_free(dev->tx_q);
3200 err = dpdk_eth_dev_init(dev);
3201 dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq);
3202 if (!dev->tx_q) {
3203 err = ENOMEM;
3204 }
3205
3206 netdev_change_seq_changed(netdev);
3207
3208 out:
3209 ovs_mutex_unlock(&dev->mutex);
3210 return err;
3211 }
3212
3213 static int
3214 dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev)
3215 OVS_REQUIRES(dev->mutex)
3216 {
3217 dev->up.n_txq = dev->requested_n_txq;
3218 dev->up.n_rxq = dev->requested_n_rxq;
3219 int err;
3220
3221 /* Enable TX queue 0 by default if it wasn't disabled. */
3222 if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
3223 dev->tx_q[0].map = 0;
3224 }
3225
3226 netdev_dpdk_remap_txqs(dev);
3227
3228 err = netdev_dpdk_mempool_configure(dev);
3229 if (!err) {
3230 /* A new mempool was created. */
3231 netdev_change_seq_changed(&dev->up);
3232 } else if (err != EEXIST){
3233 return err;
3234 }
3235 if (netdev_dpdk_get_vid(dev) >= 0) {
3236 if (dev->vhost_reconfigured == false) {
3237 dev->vhost_reconfigured = true;
3238 /* Carrier status may need updating. */
3239 netdev_change_seq_changed(&dev->up);
3240 }
3241 }
3242
3243 return 0;
3244 }
3245
3246 static int
3247 netdev_dpdk_vhost_reconfigure(struct netdev *netdev)
3248 {
3249 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3250 int err;
3251
3252 ovs_mutex_lock(&dev->mutex);
3253 err = dpdk_vhost_reconfigure_helper(dev);
3254 ovs_mutex_unlock(&dev->mutex);
3255
3256 return err;
3257 }
3258
3259 static int
3260 netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev)
3261 {
3262 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3263 int err;
3264
3265 ovs_mutex_lock(&dev->mutex);
3266
3267 /* Configure vHost client mode if requested and if the following criteria
3268 * are met:
3269 * 1. Device hasn't been registered yet.
3270 * 2. A path has been specified.
3271 */
3272 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)
3273 && strlen(dev->vhost_id)) {
3274 /* Register client-mode device */
3275 err = rte_vhost_driver_register(dev->vhost_id,
3276 RTE_VHOST_USER_CLIENT);
3277 if (err) {
3278 VLOG_ERR("vhost-user device setup failure for device %s\n",
3279 dev->vhost_id);
3280 goto unlock;
3281 } else {
3282 /* Configuration successful */
3283 dev->vhost_driver_flags |= RTE_VHOST_USER_CLIENT;
3284 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3285 "using client socket '%s'",
3286 dev->up.name, dev->vhost_id);
3287 }
3288
3289 err = rte_vhost_driver_callback_register(dev->vhost_id,
3290 &virtio_net_device_ops);
3291 if (err) {
3292 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3293 "vhost user client port: %s\n", dev->up.name);
3294 goto unlock;
3295 }
3296
3297 err = rte_vhost_driver_disable_features(dev->vhost_id,
3298 1ULL << VIRTIO_NET_F_HOST_TSO4
3299 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3300 | 1ULL << VIRTIO_NET_F_CSUM);
3301 if (err) {
3302 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3303 "client port: %s\n", dev->up.name);
3304 goto unlock;
3305 }
3306
3307 err = rte_vhost_driver_start(dev->vhost_id);
3308 if (err) {
3309 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3310 "client port: %s\n", dev->up.name);
3311 goto unlock;
3312 }
3313 }
3314
3315 err = dpdk_vhost_reconfigure_helper(dev);
3316
3317 unlock:
3318 ovs_mutex_unlock(&dev->mutex);
3319
3320 return err;
3321 }
3322
3323 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3324 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3325 GET_CARRIER, GET_STATS, \
3326 GET_FEATURES, GET_STATUS, \
3327 RECONFIGURE, RXQ_RECV) \
3328 { \
3329 NAME, \
3330 true, /* is_pmd */ \
3331 INIT, /* init */ \
3332 NULL, /* netdev_dpdk_run */ \
3333 NULL, /* netdev_dpdk_wait */ \
3334 \
3335 netdev_dpdk_alloc, \
3336 CONSTRUCT, \
3337 DESTRUCT, \
3338 netdev_dpdk_dealloc, \
3339 netdev_dpdk_get_config, \
3340 SET_CONFIG, \
3341 NULL, /* get_tunnel_config */ \
3342 NULL, /* build header */ \
3343 NULL, /* push header */ \
3344 NULL, /* pop header */ \
3345 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3346 SET_TX_MULTIQ, \
3347 \
3348 SEND, /* send */ \
3349 NULL, /* send_wait */ \
3350 \
3351 netdev_dpdk_set_etheraddr, \
3352 netdev_dpdk_get_etheraddr, \
3353 netdev_dpdk_get_mtu, \
3354 netdev_dpdk_set_mtu, \
3355 netdev_dpdk_get_ifindex, \
3356 GET_CARRIER, \
3357 netdev_dpdk_get_carrier_resets, \
3358 netdev_dpdk_set_miimon, \
3359 GET_STATS, \
3360 GET_FEATURES, \
3361 NULL, /* set_advertisements */ \
3362 NULL, /* get_pt_mode */ \
3363 \
3364 netdev_dpdk_set_policing, \
3365 netdev_dpdk_get_qos_types, \
3366 NULL, /* get_qos_capabilities */ \
3367 netdev_dpdk_get_qos, \
3368 netdev_dpdk_set_qos, \
3369 NULL, /* get_queue */ \
3370 NULL, /* set_queue */ \
3371 NULL, /* delete_queue */ \
3372 NULL, /* get_queue_stats */ \
3373 NULL, /* queue_dump_start */ \
3374 NULL, /* queue_dump_next */ \
3375 NULL, /* queue_dump_done */ \
3376 NULL, /* dump_queue_stats */ \
3377 \
3378 NULL, /* set_in4 */ \
3379 NULL, /* get_addr_list */ \
3380 NULL, /* add_router */ \
3381 NULL, /* get_next_hop */ \
3382 GET_STATUS, \
3383 NULL, /* arp_lookup */ \
3384 \
3385 netdev_dpdk_update_flags, \
3386 RECONFIGURE, \
3387 \
3388 netdev_dpdk_rxq_alloc, \
3389 netdev_dpdk_rxq_construct, \
3390 netdev_dpdk_rxq_destruct, \
3391 netdev_dpdk_rxq_dealloc, \
3392 RXQ_RECV, \
3393 NULL, /* rx_wait */ \
3394 NULL, /* rxq_drain */ \
3395 NO_OFFLOAD_API \
3396 }
3397
3398 static const struct netdev_class dpdk_class =
3399 NETDEV_DPDK_CLASS(
3400 "dpdk",
3401 netdev_dpdk_class_init,
3402 netdev_dpdk_construct,
3403 netdev_dpdk_destruct,
3404 netdev_dpdk_set_config,
3405 netdev_dpdk_set_tx_multiq,
3406 netdev_dpdk_eth_send,
3407 netdev_dpdk_get_carrier,
3408 netdev_dpdk_get_stats,
3409 netdev_dpdk_get_features,
3410 netdev_dpdk_get_status,
3411 netdev_dpdk_reconfigure,
3412 netdev_dpdk_rxq_recv);
3413
3414 static const struct netdev_class dpdk_ring_class =
3415 NETDEV_DPDK_CLASS(
3416 "dpdkr",
3417 netdev_dpdk_class_init,
3418 netdev_dpdk_ring_construct,
3419 netdev_dpdk_destruct,
3420 netdev_dpdk_ring_set_config,
3421 netdev_dpdk_set_tx_multiq,
3422 netdev_dpdk_ring_send,
3423 netdev_dpdk_get_carrier,
3424 netdev_dpdk_get_stats,
3425 netdev_dpdk_get_features,
3426 netdev_dpdk_get_status,
3427 netdev_dpdk_reconfigure,
3428 netdev_dpdk_rxq_recv);
3429
3430 static const struct netdev_class dpdk_vhost_class =
3431 NETDEV_DPDK_CLASS(
3432 "dpdkvhostuser",
3433 NULL,
3434 netdev_dpdk_vhost_construct,
3435 netdev_dpdk_vhost_destruct,
3436 NULL,
3437 NULL,
3438 netdev_dpdk_vhost_send,
3439 netdev_dpdk_vhost_get_carrier,
3440 netdev_dpdk_vhost_get_stats,
3441 NULL,
3442 NULL,
3443 netdev_dpdk_vhost_reconfigure,
3444 netdev_dpdk_vhost_rxq_recv);
3445 static const struct netdev_class dpdk_vhost_client_class =
3446 NETDEV_DPDK_CLASS(
3447 "dpdkvhostuserclient",
3448 NULL,
3449 netdev_dpdk_vhost_client_construct,
3450 netdev_dpdk_vhost_destruct,
3451 netdev_dpdk_vhost_client_set_config,
3452 NULL,
3453 netdev_dpdk_vhost_send,
3454 netdev_dpdk_vhost_get_carrier,
3455 netdev_dpdk_vhost_get_stats,
3456 NULL,
3457 NULL,
3458 netdev_dpdk_vhost_client_reconfigure,
3459 netdev_dpdk_vhost_rxq_recv);
3460
3461 void
3462 netdev_dpdk_register(void)
3463 {
3464 netdev_register_provider(&dpdk_class);
3465 netdev_register_provider(&dpdk_ring_class);
3466 netdev_register_provider(&dpdk_vhost_class);
3467 netdev_register_provider(&dpdk_vhost_client_class);
3468 }