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