Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : :
5 : : #include <rte_atomic.h>
6 : : #include <rte_branch_prediction.h>
7 : : #include <rte_byteorder.h>
8 : : #include <rte_common.h>
9 : : #include <rte_mbuf.h>
10 : : #include <ethdev_driver.h>
11 : : #include <ethdev_vdev.h>
12 : : #include <rte_malloc.h>
13 : : #include <bus_vdev_driver.h>
14 : : #include <rte_kvargs.h>
15 : : #include <rte_net.h>
16 : : #include <rte_debug.h>
17 : : #include <rte_ip.h>
18 : : #include <rte_ethdev.h>
19 : : #include <rte_errno.h>
20 : : #include <rte_cycles.h>
21 : :
22 : : #include <assert.h>
23 : : #include <sys/types.h>
24 : : #include <sys/stat.h>
25 : : #include <sys/socket.h>
26 : : #include <sys/ioctl.h>
27 : : #include <sys/utsname.h>
28 : : #include <sys/mman.h>
29 : : #include <errno.h>
30 : : #include <signal.h>
31 : : #include <stdbool.h>
32 : : #include <stdint.h>
33 : : #include <stdlib.h>
34 : : #include <sys/uio.h>
35 : : #include <unistd.h>
36 : : #include <arpa/inet.h>
37 : : #include <net/if.h>
38 : : #include <linux/if_tun.h>
39 : : #include <linux/if_ether.h>
40 : : #include <fcntl.h>
41 : : #include <ctype.h>
42 : :
43 : : #include <tap_rss.h>
44 : : #include <rte_eth_tap.h>
45 : : #include <tap_flow.h>
46 : : #include <tap_netlink.h>
47 : : #include <tap_tcmsgs.h>
48 : :
49 : : /* Linux based path to the TUN device */
50 : : #define TUN_TAP_DEV_PATH "/dev/net/tun"
51 : : #define DEFAULT_TAP_NAME "dtap"
52 : : #define DEFAULT_TUN_NAME "dtun"
53 : :
54 : : #define ETH_TAP_IFACE_ARG "iface"
55 : : #define ETH_TAP_REMOTE_ARG "remote"
56 : : #define ETH_TAP_MAC_ARG "mac"
57 : : #define ETH_TAP_MAC_FIXED "fixed"
58 : : #define ETH_TAP_PERSIST_ARG "persist"
59 : :
60 : : #define ETH_TAP_USR_MAC_FMT "xx:xx:xx:xx:xx:xx"
61 : : #define ETH_TAP_CMP_MAC_FMT "0123456789ABCDEFabcdef"
62 : : #define ETH_TAP_MAC_ARG_FMT ETH_TAP_MAC_FIXED "|" ETH_TAP_USR_MAC_FMT
63 : :
64 : : #define TAP_GSO_MBUFS_PER_CORE 128
65 : : #define TAP_GSO_MBUF_SEG_SIZE 128
66 : : #define TAP_GSO_MBUF_CACHE_SIZE 4
67 : : #define TAP_GSO_MBUFS_NUM \
68 : : (TAP_GSO_MBUFS_PER_CORE * TAP_GSO_MBUF_CACHE_SIZE)
69 : :
70 : : /* IPC key for queue fds sync */
71 : : #define TAP_MP_KEY "tap_mp_sync_queues"
72 : : #define TAP_MP_REQ_START_RXTX "tap_mp_req_start_rxtx"
73 : :
74 : : static_assert(RTE_PMD_TAP_MAX_QUEUES <= RTE_MP_MAX_FD_NUM, "TAP max queues exceeds MP fd limit");
75 : :
76 : : #define TAP_IOV_DEFAULT_MAX 1024
77 : :
78 : : #define TAP_RX_OFFLOAD (RTE_ETH_RX_OFFLOAD_SCATTER | \
79 : : RTE_ETH_RX_OFFLOAD_IPV4_CKSUM | \
80 : : RTE_ETH_RX_OFFLOAD_UDP_CKSUM | \
81 : : RTE_ETH_RX_OFFLOAD_TCP_CKSUM)
82 : :
83 : : #define TAP_TX_OFFLOAD (RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
84 : : RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
85 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM | \
86 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM | \
87 : : RTE_ETH_TX_OFFLOAD_TCP_TSO)
88 : :
89 : : static int tap_devices_count;
90 : :
91 : : static const char *tuntap_types[ETH_TUNTAP_TYPE_MAX] = {
92 : : "UNKNOWN", "TUN", "TAP"
93 : : };
94 : :
95 : : static const char *valid_arguments[] = {
96 : : ETH_TAP_IFACE_ARG,
97 : : ETH_TAP_REMOTE_ARG,
98 : : ETH_TAP_MAC_ARG,
99 : : ETH_TAP_PERSIST_ARG,
100 : : NULL
101 : : };
102 : :
103 : : static volatile uint32_t tap_trigger; /* Rx trigger */
104 : :
105 : : static struct rte_eth_link pmd_link = {
106 : : .link_speed = RTE_ETH_SPEED_NUM_10G,
107 : : .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
108 : : .link_status = RTE_ETH_LINK_DOWN,
109 : : .link_autoneg = RTE_ETH_LINK_FIXED,
110 : : };
111 : :
112 : : static void
113 : 0 : tap_trigger_cb(int sig __rte_unused)
114 : : {
115 : : /* Valid trigger values are nonzero */
116 : 0 : tap_trigger = (tap_trigger + 1) | 0x80000000;
117 : 0 : }
118 : :
119 : : /* Specifies on what netdevices the ioctl should be applied */
120 : : enum ioctl_mode {
121 : : LOCAL_AND_REMOTE,
122 : : LOCAL_ONLY,
123 : : REMOTE_ONLY,
124 : : };
125 : :
126 : : /* Message header to synchronize queues via IPC */
127 : : struct ipc_queues {
128 : : char port_name[RTE_DEV_NAME_MAX_LEN];
129 : : int q_count;
130 : : /*
131 : : * The file descriptors are in the dedicated part
132 : : * of the Unix message to be translated by the kernel.
133 : : */
134 : : };
135 : :
136 : : static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
137 : :
138 : : /**
139 : : * Tun/Tap allocation routine
140 : : *
141 : : * @param[in] pmd
142 : : * Pointer to private structure.
143 : : *
144 : : * @param[in] is_keepalive
145 : : * Keepalive flag
146 : : *
147 : : * @param[in] persistent
148 : : * Mark device as persistent
149 : : *
150 : : * @return
151 : : * -1 on failure, fd on success
152 : : */
153 : : static int
154 [ # # ]: 0 : tun_alloc(struct pmd_internals *pmd, int is_keepalive, int persistent)
155 : : {
156 : : struct ifreq ifr;
157 : : #ifdef IFF_MULTI_QUEUE
158 : : unsigned int features;
159 : : #endif
160 : : int fd, signo, flags;
161 : :
162 : : memset(&ifr, 0, sizeof(struct ifreq));
163 : :
164 : : /*
165 : : * Do not set IFF_NO_PI as packet information header will be needed
166 : : * to check if a received packet has been truncated.
167 : : */
168 [ # # ]: 0 : ifr.ifr_flags = (pmd->type == ETH_TUNTAP_TYPE_TAP) ?
169 : : IFF_TAP : IFF_TUN | IFF_POINTOPOINT;
170 : 0 : strlcpy(ifr.ifr_name, pmd->name, IFNAMSIZ);
171 : :
172 : : fd = open(TUN_TAP_DEV_PATH, O_RDWR);
173 [ # # ]: 0 : if (fd < 0) {
174 : 0 : TAP_LOG(ERR, "Unable to open %s interface", TUN_TAP_DEV_PATH);
175 : 0 : goto error;
176 : : }
177 : :
178 : : #ifdef IFF_MULTI_QUEUE
179 : : /* Grab the TUN features to verify we can work multi-queue */
180 [ # # ]: 0 : if (ioctl(fd, TUNGETFEATURES, &features) < 0) {
181 : 0 : TAP_LOG(ERR, "unable to get TUN/TAP features");
182 : 0 : goto error;
183 : : }
184 : 0 : TAP_LOG(DEBUG, "%s Features %08x", TUN_TAP_DEV_PATH, features);
185 : :
186 [ # # ]: 0 : if (features & IFF_MULTI_QUEUE) {
187 : 0 : TAP_LOG(DEBUG, " Multi-queue support for %d queues",
188 : : RTE_PMD_TAP_MAX_QUEUES);
189 : 0 : ifr.ifr_flags |= IFF_MULTI_QUEUE;
190 : : } else
191 : : #endif
192 : : {
193 : 0 : ifr.ifr_flags |= IFF_ONE_QUEUE;
194 : 0 : TAP_LOG(DEBUG, " Single queue only support");
195 : : }
196 : :
197 : : /* Set the TUN/TAP configuration and set the name if needed */
198 [ # # ]: 0 : if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
199 : 0 : TAP_LOG(WARNING, "Unable to set TUNSETIFF for %s: %s",
200 : : ifr.ifr_name, strerror(errno));
201 : 0 : goto error;
202 : : }
203 : :
204 : : /* Keep the device after application exit */
205 [ # # # # ]: 0 : if (persistent && ioctl(fd, TUNSETPERSIST, 1) < 0) {
206 : 0 : TAP_LOG(WARNING,
207 : : "Unable to set persist %s: %s",
208 : : ifr.ifr_name, strerror(errno));
209 : 0 : goto error;
210 : : }
211 : :
212 : : /*
213 : : * Name passed to kernel might be wildcard like dtun%d
214 : : * and need to find the resulting device.
215 : : */
216 : 0 : TAP_LOG(DEBUG, "Device name is '%s'", ifr.ifr_name);
217 : : strlcpy(pmd->name, ifr.ifr_name, RTE_ETH_NAME_MAX_LEN);
218 : :
219 [ # # ]: 0 : if (is_keepalive) {
220 : : /*
221 : : * Detach the TUN/TAP keep-alive queue
222 : : * to avoid traffic through it
223 : : */
224 : 0 : ifr.ifr_flags = IFF_DETACH_QUEUE;
225 [ # # ]: 0 : if (ioctl(fd, TUNSETQUEUE, (void *)&ifr) < 0) {
226 : 0 : TAP_LOG(WARNING,
227 : : "Unable to detach keep-alive queue for %s: %s",
228 : : ifr.ifr_name, strerror(errno));
229 : 0 : goto error;
230 : : }
231 : : }
232 : :
233 : 0 : flags = fcntl(fd, F_GETFL);
234 [ # # ]: 0 : if (flags == -1) {
235 : 0 : TAP_LOG(WARNING, "Unable to get %s current flags: %s",
236 : : ifr.ifr_name, strerror(errno));
237 : 0 : goto error;
238 : : }
239 : :
240 : : /* Always set the file descriptor to non-blocking */
241 : 0 : flags |= O_NONBLOCK;
242 [ # # ]: 0 : if (fcntl(fd, F_SETFL, flags) < 0) {
243 : 0 : TAP_LOG(WARNING,
244 : : "Unable to set %s to nonblocking: %s",
245 : : ifr.ifr_name, strerror(errno));
246 : 0 : goto error;
247 : : }
248 : :
249 : : /* Find a free realtime signal */
250 [ # # ]: 0 : for (signo = SIGRTMIN + 1; signo < SIGRTMAX; signo++) {
251 : : struct sigaction sa;
252 : :
253 [ # # ]: 0 : if (sigaction(signo, NULL, &sa) == -1) {
254 : 0 : TAP_LOG(WARNING,
255 : : "Unable to get current rt-signal %d handler",
256 : : signo);
257 : 0 : goto error;
258 : : }
259 : :
260 : : /* Already have the handler we want on this signal */
261 [ # # ]: 0 : if (sa.sa_handler == tap_trigger_cb)
262 : : break;
263 : :
264 : : /* Is handler in use by application */
265 [ # # ]: 0 : if (sa.sa_handler != SIG_DFL) {
266 : 0 : TAP_LOG(DEBUG,
267 : : "Skipping used rt-signal %d", signo);
268 : 0 : continue;
269 : : }
270 : :
271 : 0 : sa = (struct sigaction) {
272 : : .sa_flags = SA_RESTART,
273 : : .sa_handler = tap_trigger_cb,
274 : : };
275 : :
276 [ # # ]: 0 : if (sigaction(signo, &sa, NULL) == -1) {
277 : 0 : TAP_LOG(WARNING,
278 : : "Unable to set rt-signal %d handler", signo);
279 : 0 : goto error;
280 : : }
281 : :
282 : : /* Found a good signal to use */
283 : 0 : TAP_LOG(DEBUG,
284 : : "Using rt-signal %d", signo);
285 : 0 : break;
286 : : }
287 : :
288 [ # # ]: 0 : if (signo == SIGRTMAX) {
289 : 0 : TAP_LOG(WARNING, "All rt-signals are in use");
290 : :
291 : : /* Disable trigger globally in case of error */
292 : 0 : tap_trigger = 0;
293 : 0 : TAP_LOG(NOTICE, "No Rx trigger signal available");
294 : : } else {
295 : : /* Enable signal on file descriptor */
296 [ # # ]: 0 : if (fcntl(fd, F_SETSIG, signo) < 0) {
297 : 0 : TAP_LOG(WARNING, "Unable to set signo %d for fd %d: %s",
298 : : signo, fd, strerror(errno));
299 : 0 : goto error;
300 : : }
301 [ # # ]: 0 : if (fcntl(fd, F_SETFL, flags | O_ASYNC) < 0) {
302 : 0 : TAP_LOG(WARNING, "Unable to set fcntl flags: %s",
303 : : strerror(errno));
304 : 0 : goto error;
305 : : }
306 : :
307 [ # # ]: 0 : if (fcntl(fd, F_SETOWN, getpid()) < 0) {
308 : 0 : TAP_LOG(WARNING, "Unable to set fcntl owner: %s",
309 : : strerror(errno));
310 : 0 : goto error;
311 : : }
312 : : }
313 : : return fd;
314 : :
315 : 0 : error:
316 [ # # ]: 0 : if (fd >= 0)
317 : 0 : close(fd);
318 : : return -1;
319 : : }
320 : :
321 : : static void
322 : 0 : tap_verify_csum(struct rte_mbuf *mbuf)
323 : : {
324 : 0 : uint32_t l2 = mbuf->packet_type & RTE_PTYPE_L2_MASK;
325 : 0 : uint32_t l3 = mbuf->packet_type & RTE_PTYPE_L3_MASK;
326 : 0 : uint32_t l4 = mbuf->packet_type & RTE_PTYPE_L4_MASK;
327 : : unsigned int l2_len = sizeof(struct rte_ether_hdr);
328 : : unsigned int l3_len;
329 : : uint16_t cksum = 0;
330 : : void *l3_hdr;
331 : : void *l4_hdr;
332 : : struct rte_udp_hdr *udp_hdr;
333 : :
334 [ # # ]: 0 : if (l2 == RTE_PTYPE_L2_ETHER_VLAN)
335 : : l2_len += 4;
336 [ # # ]: 0 : else if (l2 == RTE_PTYPE_L2_ETHER_QINQ)
337 : : l2_len += 8;
338 : : /* Don't verify checksum for packets with discontinuous L2 header */
339 [ # # ]: 0 : if (unlikely(l2_len + sizeof(struct rte_ipv4_hdr) >
340 : : rte_pktmbuf_data_len(mbuf)))
341 : : return;
342 : 0 : l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len);
343 [ # # ]: 0 : if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
344 : : struct rte_ipv4_hdr *iph = l3_hdr;
345 : :
346 : 0 : l3_len = rte_ipv4_hdr_len(iph);
347 [ # # ]: 0 : if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
348 : : return;
349 : : /* check that the total length reported by header is not
350 : : * greater than the total received size
351 : : */
352 [ # # # # ]: 0 : if (l2_len + rte_be_to_cpu_16(iph->total_length) >
353 : : rte_pktmbuf_data_len(mbuf))
354 : : return;
355 : :
356 : 0 : cksum = ~rte_raw_cksum(iph, l3_len);
357 : 0 : mbuf->ol_flags |= cksum ?
358 [ # # ]: 0 : RTE_MBUF_F_RX_IP_CKSUM_BAD :
359 : : RTE_MBUF_F_RX_IP_CKSUM_GOOD;
360 [ # # ]: 0 : } else if (l3 == RTE_PTYPE_L3_IPV6) {
361 : : struct rte_ipv6_hdr *iph = l3_hdr;
362 : :
363 : : l3_len = sizeof(struct rte_ipv6_hdr);
364 : : /* check that the total length reported by header is not
365 : : * greater than the total received size
366 : : */
367 [ # # ]: 0 : if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) >
368 [ # # ]: 0 : rte_pktmbuf_data_len(mbuf))
369 : : return;
370 : : } else {
371 : : /* - RTE_PTYPE_L3_IPV4_EXT_UNKNOWN cannot happen because
372 : : * mbuf->packet_type is filled by rte_net_get_ptype() which
373 : : * never returns this value.
374 : : * - IPv6 extensions are not supported.
375 : : */
376 : : return;
377 : : }
378 [ # # ]: 0 : if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) {
379 : : int cksum_ok;
380 : :
381 : 0 : l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);
382 : : /* Don't verify checksum for multi-segment packets. */
383 [ # # ]: 0 : if (mbuf->nb_segs > 1)
384 : : return;
385 [ # # ]: 0 : if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
386 [ # # ]: 0 : if (l4 == RTE_PTYPE_L4_UDP) {
387 : : udp_hdr = (struct rte_udp_hdr *)l4_hdr;
388 [ # # ]: 0 : if (udp_hdr->dgram_cksum == 0) {
389 : : /*
390 : : * For IPv4, a zero UDP checksum
391 : : * indicates that the sender did not
392 : : * generate one [RFC 768].
393 : : */
394 : 0 : mbuf->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
395 : 0 : return;
396 : : }
397 : : }
398 : 0 : cksum_ok = !rte_ipv4_udptcp_cksum_verify(l3_hdr,
399 : : l4_hdr);
400 : : } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */
401 : 0 : cksum_ok = !rte_ipv6_udptcp_cksum_verify(l3_hdr,
402 : : l4_hdr);
403 : : }
404 : 0 : mbuf->ol_flags |= cksum_ok ?
405 [ # # ]: 0 : RTE_MBUF_F_RX_L4_CKSUM_GOOD : RTE_MBUF_F_RX_L4_CKSUM_BAD;
406 : : }
407 : : }
408 : :
409 : : static void
410 : : tap_rxq_pool_free(struct rte_mbuf *pool)
411 : : {
412 : : struct rte_mbuf *mbuf = pool;
413 : : uint16_t nb_segs = 1;
414 : :
415 : 0 : if (mbuf == NULL)
416 : : return;
417 : :
418 [ # # # # : 0 : while (mbuf->next) {
# # # # ]
419 : : mbuf = mbuf->next;
420 : 0 : nb_segs++;
421 : : }
422 : 0 : pool->nb_segs = nb_segs;
423 : 0 : rte_pktmbuf_free(pool);
424 : : }
425 : :
426 : : /* Callback to handle the rx burst of packets to the correct interface and
427 : : * file descriptor(s) in a multi-queue setup.
428 : : */
429 : : static uint16_t
430 : 0 : pmd_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
431 : : {
432 : : struct rx_queue *rxq = queue;
433 : : struct pmd_process_private *process_private;
434 : : uint16_t num_rx;
435 : : unsigned long num_rx_bytes = 0;
436 : 0 : uint32_t trigger = tap_trigger;
437 : :
438 [ # # ]: 0 : if (trigger == rxq->trigger_seen)
439 : : return 0;
440 : :
441 : 0 : process_private = rte_eth_devices[rxq->in_port].process_private;
442 [ # # ]: 0 : for (num_rx = 0; num_rx < nb_pkts; ) {
443 [ # # ]: 0 : struct rte_mbuf *mbuf = rxq->pool;
444 : : struct rte_mbuf *seg = NULL;
445 : : struct rte_mbuf *new_tail = NULL;
446 : : uint16_t data_off = rte_pktmbuf_headroom(mbuf);
447 : : int len;
448 : :
449 : 0 : len = readv(process_private->fds[rxq->queue_id],
450 : 0 : *rxq->iovecs,
451 [ # # ]: 0 : 1 + (rxq->rxmode->offloads & RTE_ETH_RX_OFFLOAD_SCATTER ?
452 : 0 : rxq->nb_rx_desc : 1));
453 [ # # ]: 0 : if (len < (int)sizeof(struct tun_pi))
454 : : break;
455 : :
456 : : /* Packet couldn't fit in the provided mbuf */
457 [ # # ]: 0 : if (unlikely(rxq->pi.flags & TUN_PKT_STRIP)) {
458 : 0 : rxq->stats.ierrors++;
459 : 0 : continue;
460 : : }
461 : :
462 : 0 : len -= sizeof(struct tun_pi);
463 : :
464 : 0 : mbuf->pkt_len = len;
465 : 0 : mbuf->port = rxq->in_port;
466 : 0 : while (1) {
467 : 0 : struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
468 : :
469 [ # # ]: 0 : if (unlikely(!buf)) {
470 : 0 : rxq->stats.rx_nombuf++;
471 : : /* No new buf has been allocated: do nothing */
472 [ # # ]: 0 : if (!new_tail || !seg)
473 : 0 : goto end;
474 : :
475 : 0 : seg->next = NULL;
476 : : tap_rxq_pool_free(mbuf);
477 : :
478 : 0 : goto end;
479 : : }
480 [ # # ]: 0 : seg = seg ? seg->next : mbuf;
481 [ # # ]: 0 : if (rxq->pool == mbuf)
482 : 0 : rxq->pool = buf;
483 [ # # ]: 0 : if (new_tail)
484 : 0 : new_tail->next = buf;
485 : : new_tail = buf;
486 : 0 : new_tail->next = seg->next;
487 : :
488 : : /* iovecs[0] is reserved for packet info (pi) */
489 : 0 : (*rxq->iovecs)[mbuf->nb_segs].iov_len =
490 : 0 : buf->buf_len - data_off;
491 : 0 : (*rxq->iovecs)[mbuf->nb_segs].iov_base =
492 : 0 : (char *)buf->buf_addr + data_off;
493 : :
494 : 0 : seg->data_len = RTE_MIN(seg->buf_len - data_off, len);
495 : 0 : seg->data_off = data_off;
496 : :
497 : 0 : len -= seg->data_len;
498 [ # # ]: 0 : if (len <= 0)
499 : : break;
500 : 0 : mbuf->nb_segs++;
501 : : /* First segment has headroom, not the others */
502 : : data_off = 0;
503 : : }
504 : 0 : seg->next = NULL;
505 : 0 : mbuf->packet_type = rte_net_get_ptype(mbuf, NULL,
506 : : RTE_PTYPE_ALL_MASK);
507 [ # # ]: 0 : if (rxq->rxmode->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM)
508 : 0 : tap_verify_csum(mbuf);
509 : :
510 : : /* account for the receive frame */
511 : 0 : bufs[num_rx++] = mbuf;
512 : 0 : num_rx_bytes += mbuf->pkt_len;
513 : : }
514 : 0 : end:
515 : 0 : rxq->stats.ipackets += num_rx;
516 : 0 : rxq->stats.ibytes += num_rx_bytes;
517 : :
518 [ # # ]: 0 : if (trigger && num_rx < nb_pkts)
519 : 0 : rxq->trigger_seen = trigger;
520 : :
521 : : return num_rx;
522 : : }
523 : :
524 : : static inline int
525 : 0 : tap_write_mbufs(struct tx_queue *txq, uint16_t num_mbufs,
526 : : struct rte_mbuf **pmbufs,
527 : : uint16_t *num_packets, unsigned long *num_tx_bytes)
528 : : {
529 : : struct pmd_process_private *process_private;
530 : : int i;
531 : :
532 : 0 : process_private = rte_eth_devices[txq->out_port].process_private;
533 : :
534 [ # # ]: 0 : for (i = 0; i < num_mbufs; i++) {
535 : 0 : struct rte_mbuf *mbuf = pmbufs[i];
536 : 0 : struct iovec iovecs[mbuf->nb_segs + 2];
537 : 0 : struct tun_pi pi = { .flags = 0, .proto = 0x00 };
538 : : struct rte_mbuf *seg = mbuf;
539 : : uint64_t l4_ol_flags;
540 : : int proto;
541 : : int n;
542 : : int j;
543 : : int k; /* current index in iovecs for copying segments */
544 : :
545 [ # # ]: 0 : if (txq->type == ETH_TUNTAP_TYPE_TUN) {
546 : : /*
547 : : * TUN and TAP are created with IFF_NO_PI disabled.
548 : : * For TUN PMD this mandatory as fields are used by
549 : : * Kernel tun.c to determine whether its IP or non IP
550 : : * packets.
551 : : *
552 : : * The logic fetches the first byte of data from mbuf
553 : : * then compares whether its v4 or v6. If first byte
554 : : * is 4 or 6, then protocol field is updated.
555 : : */
556 : 0 : char *buff_data = rte_pktmbuf_mtod(seg, void *);
557 : 0 : proto = (*buff_data & 0xf0);
558 [ # # # # ]: 0 : pi.proto = (proto == 0x40) ?
559 : : rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) :
560 : : ((proto == 0x60) ?
561 : : rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) :
562 : : 0x00);
563 : : }
564 : :
565 : : k = 0;
566 : 0 : iovecs[k].iov_base = π
567 : 0 : iovecs[k].iov_len = sizeof(pi);
568 : : k++;
569 : :
570 : 0 : l4_ol_flags = mbuf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
571 [ # # # # ]: 0 : if (txq->csum && (mbuf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM ||
572 : 0 : l4_ol_flags == RTE_MBUF_F_TX_UDP_CKSUM ||
573 [ # # ]: 0 : l4_ol_flags == RTE_MBUF_F_TX_TCP_CKSUM)) {
574 : 0 : unsigned int hdrlens = mbuf->l2_len + mbuf->l3_len;
575 : : uint16_t *l4_cksum;
576 : : void *l3_hdr;
577 : :
578 [ # # ]: 0 : if (l4_ol_flags == RTE_MBUF_F_TX_UDP_CKSUM)
579 : 0 : hdrlens += sizeof(struct rte_udp_hdr);
580 [ # # ]: 0 : else if (l4_ol_flags == RTE_MBUF_F_TX_TCP_CKSUM)
581 : 0 : hdrlens += sizeof(struct rte_tcp_hdr);
582 [ # # ]: 0 : else if (l4_ol_flags != RTE_MBUF_F_TX_L4_NO_CKSUM)
583 : 0 : return -1;
584 : :
585 : : /* Support only packets with at least layer 4
586 : : * header included in the first segment
587 : : */
588 [ # # ]: 0 : if (rte_pktmbuf_data_len(mbuf) < hdrlens)
589 : 0 : return -1;
590 : :
591 : : /* To change checksums (considering that a mbuf can be
592 : : * indirect, for example), copy l2, l3 and l4 headers
593 : : * in a new segment and chain it to existing data
594 : : */
595 : 0 : seg = rte_pktmbuf_copy(mbuf, mbuf->pool, 0, hdrlens);
596 [ # # ]: 0 : if (seg == NULL)
597 : 0 : return -1;
598 [ # # ]: 0 : rte_pktmbuf_adj(mbuf, hdrlens);
599 : : rte_pktmbuf_chain(seg, mbuf);
600 : 0 : pmbufs[i] = mbuf = seg;
601 : :
602 : 0 : l3_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, mbuf->l2_len);
603 [ # # ]: 0 : if (mbuf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
604 : : struct rte_ipv4_hdr *iph = l3_hdr;
605 : :
606 : 0 : iph->hdr_checksum = 0;
607 : 0 : iph->hdr_checksum = rte_ipv4_cksum(iph);
608 : : }
609 : :
610 [ # # ]: 0 : if (l4_ol_flags == RTE_MBUF_F_TX_L4_NO_CKSUM)
611 : 0 : goto skip_l4_cksum;
612 : :
613 [ # # ]: 0 : if (l4_ol_flags == RTE_MBUF_F_TX_UDP_CKSUM) {
614 : : struct rte_udp_hdr *udp_hdr;
615 : :
616 : 0 : udp_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_udp_hdr *,
617 : : mbuf->l2_len + mbuf->l3_len);
618 : 0 : l4_cksum = &udp_hdr->dgram_cksum;
619 : : } else {
620 : : struct rte_tcp_hdr *tcp_hdr;
621 : :
622 : 0 : tcp_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_tcp_hdr *,
623 : : mbuf->l2_len + mbuf->l3_len);
624 : 0 : l4_cksum = &tcp_hdr->cksum;
625 : : }
626 : :
627 : 0 : *l4_cksum = 0;
628 [ # # ]: 0 : if (mbuf->ol_flags & RTE_MBUF_F_TX_IPV4) {
629 : 0 : *l4_cksum = rte_ipv4_udptcp_cksum_mbuf(mbuf, l3_hdr,
630 : 0 : mbuf->l2_len + mbuf->l3_len);
631 : : } else {
632 : 0 : *l4_cksum = rte_ipv6_udptcp_cksum_mbuf(mbuf, l3_hdr,
633 : 0 : mbuf->l2_len + mbuf->l3_len);
634 : : }
635 : : }
636 : :
637 : 0 : skip_l4_cksum:
638 [ # # ]: 0 : for (j = 0; j < mbuf->nb_segs; j++) {
639 : 0 : iovecs[k].iov_len = rte_pktmbuf_data_len(seg);
640 : 0 : iovecs[k].iov_base = rte_pktmbuf_mtod(seg, void *);
641 : 0 : k++;
642 : 0 : seg = seg->next;
643 : : }
644 : :
645 : : /* copy the tx frame data */
646 : 0 : n = writev(process_private->fds[txq->queue_id], iovecs, k);
647 [ # # ]: 0 : if (n <= 0)
648 : 0 : return -1;
649 : :
650 : 0 : (*num_packets)++;
651 : 0 : (*num_tx_bytes) += rte_pktmbuf_pkt_len(mbuf);
652 : : }
653 : : return 0;
654 : : }
655 : :
656 : : /* Callback to handle sending packets from the tap interface
657 : : */
658 : : static uint16_t
659 : 0 : pmd_tx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
660 : : {
661 : : struct tx_queue *txq = queue;
662 : : uint16_t num_tx = 0;
663 : 0 : uint16_t num_packets = 0;
664 : 0 : unsigned long num_tx_bytes = 0;
665 : : uint32_t max_size;
666 : : int i;
667 : :
668 [ # # ]: 0 : if (unlikely(nb_pkts == 0))
669 : : return 0;
670 : :
671 : : struct rte_mbuf *gso_mbufs[MAX_GSO_MBUFS];
672 : 0 : max_size = *txq->mtu + (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + 4);
673 [ # # ]: 0 : for (i = 0; i < nb_pkts; i++) {
674 : 0 : struct rte_mbuf *mbuf_in = bufs[num_tx];
675 : : struct rte_mbuf **mbuf;
676 : : uint16_t num_mbufs = 0;
677 : : uint16_t tso_segsz = 0;
678 : : int ret;
679 : : int num_tso_mbufs;
680 : : uint16_t hdrs_len;
681 : : uint64_t tso;
682 : :
683 : 0 : tso = mbuf_in->ol_flags & RTE_MBUF_F_TX_TCP_SEG;
684 [ # # ]: 0 : if (tso) {
685 : 0 : struct rte_gso_ctx *gso_ctx = &txq->gso_ctx;
686 : :
687 : : /* TCP segmentation implies TCP checksum offload */
688 : 0 : mbuf_in->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
689 : :
690 : : /* gso size is calculated without RTE_ETHER_CRC_LEN */
691 : 0 : hdrs_len = mbuf_in->l2_len + mbuf_in->l3_len +
692 : 0 : mbuf_in->l4_len;
693 : 0 : tso_segsz = mbuf_in->tso_segsz + hdrs_len;
694 [ # # ]: 0 : if (unlikely(tso_segsz == hdrs_len) ||
695 [ # # ]: 0 : tso_segsz > *txq->mtu) {
696 : 0 : txq->stats.errs++;
697 : 0 : break;
698 : : }
699 : 0 : gso_ctx->gso_size = tso_segsz;
700 : : /* 'mbuf_in' packet to segment */
701 : 0 : num_tso_mbufs = rte_gso_segment(mbuf_in,
702 : : gso_ctx, /* gso control block */
703 : : (struct rte_mbuf **)&gso_mbufs, /* out mbufs */
704 : : RTE_DIM(gso_mbufs)); /* max tso mbufs */
705 : :
706 : : /* ret contains the number of new created mbufs */
707 [ # # ]: 0 : if (num_tso_mbufs < 0)
708 : : break;
709 : :
710 [ # # ]: 0 : if (num_tso_mbufs >= 1) {
711 : : mbuf = gso_mbufs;
712 : 0 : num_mbufs = num_tso_mbufs;
713 : : } else {
714 : : /* 0 means it can be transmitted directly
715 : : * without gso.
716 : : */
717 : : mbuf = &mbuf_in;
718 : : num_mbufs = 1;
719 : : }
720 : : } else {
721 : : /* stats.errs will be incremented */
722 [ # # ]: 0 : if (rte_pktmbuf_pkt_len(mbuf_in) > max_size)
723 : : break;
724 : :
725 : : /* ret 0 indicates no new mbufs were created */
726 : : num_tso_mbufs = 0;
727 : : mbuf = &mbuf_in;
728 : : num_mbufs = 1;
729 : : }
730 : :
731 : 0 : ret = tap_write_mbufs(txq, num_mbufs, mbuf,
732 : : &num_packets, &num_tx_bytes);
733 [ # # ]: 0 : if (ret == -1) {
734 : 0 : txq->stats.errs++;
735 : : /* free tso mbufs */
736 [ # # ]: 0 : if (num_tso_mbufs > 0)
737 : 0 : rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
738 : : break;
739 : : }
740 : 0 : num_tx++;
741 [ # # ]: 0 : if (num_tso_mbufs == 0) {
742 : : /* tap_write_mbufs may prepend a segment to mbuf_in */
743 : 0 : rte_pktmbuf_free(mbuf[0]);
744 : : } else {
745 : : /* free original mbuf */
746 : 0 : rte_pktmbuf_free(mbuf_in);
747 : : /* free tso mbufs */
748 : 0 : rte_pktmbuf_free_bulk(mbuf, num_tso_mbufs);
749 : : }
750 : : }
751 : :
752 : 0 : txq->stats.opackets += num_packets;
753 : 0 : txq->stats.errs += nb_pkts - num_tx;
754 : 0 : txq->stats.obytes += num_tx_bytes;
755 : :
756 : 0 : return num_tx;
757 : : }
758 : :
759 : : static const char *
760 : : tap_ioctl_req2str(unsigned long request)
761 : : {
762 [ # # # # : 0 : switch (request) {
# # ]
763 : : case SIOCSIFFLAGS:
764 : : return "SIOCSIFFLAGS";
765 : 0 : case SIOCGIFFLAGS:
766 : 0 : return "SIOCGIFFLAGS";
767 : 0 : case SIOCGIFHWADDR:
768 : 0 : return "SIOCGIFHWADDR";
769 : 0 : case SIOCSIFHWADDR:
770 : 0 : return "SIOCSIFHWADDR";
771 : 0 : case SIOCSIFMTU:
772 : 0 : return "SIOCSIFMTU";
773 : : }
774 : 0 : return "UNKNOWN";
775 : : }
776 : :
777 : : static int
778 : 0 : tap_ioctl(struct pmd_internals *pmd, unsigned long request,
779 : : struct ifreq *ifr, int set, enum ioctl_mode mode)
780 : : {
781 : 0 : short req_flags = ifr->ifr_flags;
782 [ # # ]: 0 : int remote = pmd->remote_if_index &&
783 [ # # ]: 0 : (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
784 : :
785 [ # # # # ]: 0 : if (!pmd->remote_if_index && mode == REMOTE_ONLY)
786 : : return 0;
787 : : /*
788 : : * If there is a remote netdevice, apply ioctl on it, then apply it on
789 : : * the tap netdevice.
790 : : */
791 : 0 : apply:
792 [ # # ]: 0 : if (remote)
793 : 0 : strlcpy(ifr->ifr_name, pmd->remote_iface, IFNAMSIZ);
794 [ # # ]: 0 : else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
795 : 0 : strlcpy(ifr->ifr_name, pmd->name, IFNAMSIZ);
796 [ # # # ]: 0 : switch (request) {
797 : 0 : case SIOCSIFFLAGS:
798 : : /* fetch current flags to leave other flags untouched */
799 [ # # ]: 0 : if (ioctl(pmd->ioctl_sock, SIOCGIFFLAGS, ifr) < 0)
800 : 0 : goto error;
801 [ # # ]: 0 : if (set)
802 : 0 : ifr->ifr_flags |= req_flags;
803 : : else
804 : 0 : ifr->ifr_flags &= ~req_flags;
805 : : break;
806 : : case SIOCGIFFLAGS:
807 : : case SIOCGIFHWADDR:
808 : : case SIOCSIFHWADDR:
809 : : case SIOCSIFMTU:
810 : : break;
811 : 0 : default:
812 : 0 : TAP_LOG(WARNING, "%s: ioctl() called with wrong arg",
813 : : pmd->name);
814 : 0 : return -EINVAL;
815 : : }
816 [ # # ]: 0 : if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
817 : 0 : goto error;
818 [ # # # # ]: 0 : if (remote-- && mode == LOCAL_AND_REMOTE)
819 : 0 : goto apply;
820 : : return 0;
821 : :
822 : 0 : error:
823 : 0 : TAP_LOG(DEBUG, "%s(%s) failed: %s(%d)", ifr->ifr_name,
824 : : tap_ioctl_req2str(request), strerror(errno), errno);
825 : 0 : return -errno;
826 : : }
827 : :
828 : : static int
829 : 0 : tap_link_set_down(struct rte_eth_dev *dev)
830 : : {
831 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
832 : 0 : struct ifreq ifr = { .ifr_flags = IFF_UP };
833 : :
834 : 0 : dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
835 : 0 : return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_ONLY);
836 : : }
837 : :
838 : : static int
839 : 0 : tap_link_set_up(struct rte_eth_dev *dev)
840 : : {
841 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
842 : 0 : struct ifreq ifr = { .ifr_flags = IFF_UP };
843 : :
844 : 0 : dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
845 : 0 : return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
846 : : }
847 : :
848 : : static int
849 : 0 : tap_mp_req_on_rxtx(struct rte_eth_dev *dev)
850 : : {
851 : : struct rte_mp_msg msg;
852 : : struct ipc_queues *request_param = (struct ipc_queues *)msg.param;
853 : : int err;
854 : 0 : struct pmd_process_private *process_private = dev->process_private;
855 : : int i;
856 : :
857 : : memset(&msg, 0, sizeof(msg));
858 : : strlcpy(msg.name, TAP_MP_REQ_START_RXTX, sizeof(msg.name));
859 : 0 : strlcpy(request_param->port_name, dev->data->name, sizeof(request_param->port_name));
860 : 0 : msg.len_param = sizeof(*request_param);
861 : :
862 : : /* rx and tx share file descriptors and nb_tx_queues == nb_rx_queues */
863 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
864 : 0 : msg.fds[i] = process_private->fds[i];
865 : :
866 : 0 : request_param->q_count = dev->data->nb_rx_queues;
867 : 0 : msg.num_fds = dev->data->nb_rx_queues;
868 : :
869 : 0 : err = rte_mp_sendmsg(&msg);
870 [ # # ]: 0 : if (err < 0) {
871 : 0 : TAP_LOG(ERR, "Failed to send start req to secondary %d",
872 : : rte_errno);
873 : 0 : return -1;
874 : : }
875 : :
876 : : return 0;
877 : : }
878 : :
879 : : static int
880 : 0 : tap_dev_start(struct rte_eth_dev *dev)
881 : : {
882 : : int err, i;
883 : :
884 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
885 : 0 : tap_mp_req_on_rxtx(dev);
886 : :
887 : 0 : err = tap_intr_handle_set(dev, 1);
888 [ # # ]: 0 : if (err)
889 : : return err;
890 : :
891 : 0 : err = tap_link_set_up(dev);
892 [ # # ]: 0 : if (err)
893 : : return err;
894 : :
895 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
896 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
897 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
898 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
899 : :
900 : : return err;
901 : : }
902 : :
903 : : static int
904 : 0 : tap_mp_req_start_rxtx(const struct rte_mp_msg *request, __rte_unused const void *peer)
905 : : {
906 : : struct rte_eth_dev *dev;
907 : : const struct ipc_queues *request_param =
908 : : (const struct ipc_queues *)request->param;
909 : : struct pmd_process_private *process_private;
910 : :
911 : 0 : dev = rte_eth_dev_get_by_name(request_param->port_name);
912 [ # # ]: 0 : if (!dev) {
913 : 0 : TAP_LOG(ERR, "Failed to get dev for %s",
914 : : request_param->port_name);
915 : 0 : return -1;
916 : : }
917 : :
918 : 0 : process_private = dev->process_private;
919 : 0 : TAP_LOG(DEBUG, "tap_attach q:%d", request_param->q_count);
920 : :
921 [ # # ]: 0 : for (int q = 0; q < request_param->q_count; q++)
922 : 0 : process_private->fds[q] = request->fds[q];
923 : :
924 : :
925 : : return 0;
926 : : }
927 : :
928 : : /* This function gets called when the current port gets stopped.
929 : : */
930 : : static int
931 : 0 : tap_dev_stop(struct rte_eth_dev *dev)
932 : : {
933 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
934 : : int i;
935 : :
936 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
937 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
938 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
939 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
940 : :
941 : 0 : tap_intr_handle_set(dev, 0);
942 [ # # ]: 0 : if (!pmd->persist)
943 : 0 : tap_link_set_down(dev);
944 : :
945 : 0 : return 0;
946 : : }
947 : :
948 : : static int
949 : 0 : tap_dev_configure(struct rte_eth_dev *dev)
950 : : {
951 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
952 : :
953 [ # # ]: 0 : if (dev->data->nb_rx_queues != dev->data->nb_tx_queues) {
954 : 0 : TAP_LOG(ERR,
955 : : "%s: number of rx queues %d must be equal to number of tx queues %d",
956 : : dev->device->name,
957 : : dev->data->nb_rx_queues,
958 : : dev->data->nb_tx_queues);
959 : 0 : return -1;
960 : : }
961 : :
962 : 0 : TAP_LOG(INFO, "%s: %s: TX configured queues number: %u",
963 : : dev->device->name, pmd->name, dev->data->nb_tx_queues);
964 : :
965 : 0 : TAP_LOG(INFO, "%s: %s: RX configured queues number: %u",
966 : : dev->device->name, pmd->name, dev->data->nb_rx_queues);
967 : :
968 : 0 : return 0;
969 : : }
970 : :
971 : : static uint32_t
972 : 0 : tap_dev_speed_capa(void)
973 : : {
974 : 0 : uint32_t speed = pmd_link.link_speed;
975 : : uint32_t capa = 0;
976 : :
977 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_10M)
978 : : capa |= RTE_ETH_LINK_SPEED_10M;
979 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_100M)
980 : 0 : capa |= RTE_ETH_LINK_SPEED_100M;
981 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_1G)
982 : 0 : capa |= RTE_ETH_LINK_SPEED_1G;
983 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_5G)
984 : 0 : capa |= RTE_ETH_LINK_SPEED_2_5G;
985 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_5G)
986 : 0 : capa |= RTE_ETH_LINK_SPEED_5G;
987 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_10G)
988 : 0 : capa |= RTE_ETH_LINK_SPEED_10G;
989 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_20G)
990 : 0 : capa |= RTE_ETH_LINK_SPEED_20G;
991 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_25G)
992 : 0 : capa |= RTE_ETH_LINK_SPEED_25G;
993 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_40G)
994 : 0 : capa |= RTE_ETH_LINK_SPEED_40G;
995 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_50G)
996 : 0 : capa |= RTE_ETH_LINK_SPEED_50G;
997 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_56G)
998 : 0 : capa |= RTE_ETH_LINK_SPEED_56G;
999 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_100G)
1000 : 0 : capa |= RTE_ETH_LINK_SPEED_100G;
1001 : :
1002 : 0 : return capa;
1003 : : }
1004 : :
1005 : : static int
1006 : 0 : tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1007 : : {
1008 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1009 : :
1010 : 0 : dev_info->if_index = internals->if_index;
1011 : 0 : dev_info->max_mac_addrs = 1;
1012 : 0 : dev_info->max_rx_pktlen = RTE_ETHER_MAX_JUMBO_FRAME_LEN;
1013 : 0 : dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
1014 : 0 : dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
1015 : 0 : dev_info->min_rx_bufsize = 0;
1016 : 0 : dev_info->speed_capa = tap_dev_speed_capa();
1017 : 0 : dev_info->rx_queue_offload_capa = TAP_RX_OFFLOAD;
1018 : 0 : dev_info->rx_offload_capa = dev_info->rx_queue_offload_capa;
1019 : 0 : dev_info->tx_queue_offload_capa = TAP_TX_OFFLOAD;
1020 : 0 : dev_info->tx_offload_capa = dev_info->tx_queue_offload_capa;
1021 : 0 : dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE;
1022 : : /*
1023 : : * limitation: TAP supports all of IP, UDP and TCP hash
1024 : : * functions together and not in partial combinations
1025 : : */
1026 : 0 : dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK;
1027 : 0 : dev_info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
1028 : :
1029 : 0 : return 0;
1030 : : }
1031 : :
1032 : : static int
1033 : 0 : tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats)
1034 : : {
1035 : : unsigned int i, imax;
1036 : : unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
1037 : : unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
1038 : : unsigned long rx_nombuf = 0, ierrors = 0;
1039 : 0 : const struct pmd_internals *pmd = dev->data->dev_private;
1040 : :
1041 : : /* rx queue statistics */
1042 : 0 : imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1043 : 0 : dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1044 [ # # ]: 0 : for (i = 0; i < imax; i++) {
1045 : 0 : tap_stats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
1046 : 0 : tap_stats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
1047 : 0 : rx_total += tap_stats->q_ipackets[i];
1048 : 0 : rx_bytes_total += tap_stats->q_ibytes[i];
1049 : 0 : rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
1050 : 0 : ierrors += pmd->rxq[i].stats.ierrors;
1051 : : }
1052 : :
1053 : : /* tx queue statistics */
1054 : 0 : imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1055 : 0 : dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1056 : :
1057 [ # # ]: 0 : for (i = 0; i < imax; i++) {
1058 : 0 : tap_stats->q_opackets[i] = pmd->txq[i].stats.opackets;
1059 : 0 : tap_stats->q_obytes[i] = pmd->txq[i].stats.obytes;
1060 : 0 : tx_total += tap_stats->q_opackets[i];
1061 : 0 : tx_err_total += pmd->txq[i].stats.errs;
1062 : 0 : tx_bytes_total += tap_stats->q_obytes[i];
1063 : : }
1064 : :
1065 : 0 : tap_stats->ipackets = rx_total;
1066 : 0 : tap_stats->ibytes = rx_bytes_total;
1067 : 0 : tap_stats->ierrors = ierrors;
1068 : 0 : tap_stats->rx_nombuf = rx_nombuf;
1069 : 0 : tap_stats->opackets = tx_total;
1070 : 0 : tap_stats->oerrors = tx_err_total;
1071 : 0 : tap_stats->obytes = tx_bytes_total;
1072 : 0 : return 0;
1073 : : }
1074 : :
1075 : : static int
1076 : 0 : tap_stats_reset(struct rte_eth_dev *dev)
1077 : : {
1078 : : int i;
1079 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1080 : :
1081 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1082 : 0 : pmd->rxq[i].stats.ipackets = 0;
1083 : 0 : pmd->rxq[i].stats.ibytes = 0;
1084 : 0 : pmd->rxq[i].stats.ierrors = 0;
1085 : 0 : pmd->rxq[i].stats.rx_nombuf = 0;
1086 : :
1087 : 0 : pmd->txq[i].stats.opackets = 0;
1088 : 0 : pmd->txq[i].stats.errs = 0;
1089 : 0 : pmd->txq[i].stats.obytes = 0;
1090 : : }
1091 : :
1092 : 0 : return 0;
1093 : : }
1094 : :
1095 : : static void
1096 : : tap_queue_close(struct pmd_process_private *process_private, uint16_t qid)
1097 : : {
1098 [ # # ]: 0 : if (process_private->fds[qid] != -1) {
1099 : 0 : close(process_private->fds[qid]);
1100 : 0 : process_private->fds[qid] = -1;
1101 : : }
1102 : : }
1103 : :
1104 : : static int
1105 : 0 : tap_dev_close(struct rte_eth_dev *dev)
1106 : : {
1107 : : int i;
1108 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1109 : 0 : struct pmd_process_private *process_private = dev->process_private;
1110 : :
1111 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1112 : 0 : free(dev->process_private);
1113 : 0 : dev->process_private = NULL;
1114 [ # # ]: 0 : if (tap_devices_count == 1)
1115 : 0 : rte_mp_action_unregister(TAP_MP_REQ_START_RXTX);
1116 : 0 : tap_devices_count--;
1117 : 0 : return 0;
1118 : : }
1119 : :
1120 [ # # ]: 0 : if (!internals->persist)
1121 : 0 : tap_link_set_down(dev);
1122 : :
1123 : : #ifdef HAVE_TCA_FLOWER
1124 [ # # ]: 0 : if (internals->nlsk_fd != -1) {
1125 : 0 : tap_flow_flush(dev, NULL);
1126 : 0 : tap_flow_implicit_flush(internals, NULL);
1127 : 0 : tap_nl_final(internals->nlsk_fd);
1128 : 0 : internals->nlsk_fd = -1;
1129 : 0 : tap_flow_bpf_destroy(internals);
1130 : : }
1131 : : #endif
1132 : :
1133 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1134 : : struct rx_queue *rxq = &internals->rxq[i];
1135 : :
1136 : : tap_queue_close(process_private, i);
1137 : :
1138 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1139 : 0 : rte_free(rxq->iovecs);
1140 : 0 : rxq->pool = NULL;
1141 : 0 : rxq->iovecs = NULL;
1142 : : }
1143 : :
1144 [ # # ]: 0 : if (internals->remote_if_index) {
1145 : : /* Restore initial remote state */
1146 : 0 : int ret = ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
1147 : : &internals->remote_initial_flags);
1148 [ # # ]: 0 : if (ret)
1149 : 0 : TAP_LOG(ERR, "restore remote state failed: %d", ret);
1150 : :
1151 : : }
1152 : :
1153 : 0 : rte_mempool_free(internals->gso_ctx_mp);
1154 : 0 : internals->gso_ctx_mp = NULL;
1155 : :
1156 [ # # ]: 0 : if (internals->ka_fd != -1) {
1157 : 0 : close(internals->ka_fd);
1158 : 0 : internals->ka_fd = -1;
1159 : : }
1160 : :
1161 : : /* mac_addrs must not be freed alone because part of dev_private */
1162 : 0 : dev->data->mac_addrs = NULL;
1163 : :
1164 : 0 : internals = dev->data->dev_private;
1165 : 0 : TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u",
1166 : : tuntap_types[internals->type], rte_socket_id());
1167 : :
1168 : 0 : rte_intr_instance_free(internals->intr_handle);
1169 : :
1170 [ # # ]: 0 : if (internals->ioctl_sock != -1) {
1171 : 0 : close(internals->ioctl_sock);
1172 : 0 : internals->ioctl_sock = -1;
1173 : : }
1174 : 0 : free(dev->process_private);
1175 : 0 : dev->process_private = NULL;
1176 : :
1177 [ # # ]: 0 : if (tap_devices_count == 1)
1178 : 0 : rte_mp_action_unregister(TAP_MP_KEY);
1179 : 0 : tap_devices_count--;
1180 : : /*
1181 : : * Since TUN device has no more opened file descriptors
1182 : : * it will be removed from kernel
1183 : : */
1184 : :
1185 : 0 : return 0;
1186 : : }
1187 : :
1188 : : static void
1189 : 0 : tap_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1190 : : {
1191 : 0 : struct rx_queue *rxq = dev->data->rx_queues[qid];
1192 : : struct pmd_process_private *process_private;
1193 : :
1194 [ # # ]: 0 : if (!rxq)
1195 : : return;
1196 : :
1197 : 0 : process_private = rte_eth_devices[rxq->in_port].process_private;
1198 : :
1199 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1200 : 0 : rte_free(rxq->iovecs);
1201 : 0 : rxq->pool = NULL;
1202 : 0 : rxq->iovecs = NULL;
1203 : :
1204 [ # # ]: 0 : if (dev->data->tx_queues[qid] == NULL)
1205 [ # # ]: 0 : tap_queue_close(process_private, qid);
1206 : : }
1207 : :
1208 : : static void
1209 : 0 : tap_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1210 : : {
1211 : 0 : struct tx_queue *txq = dev->data->tx_queues[qid];
1212 : : struct pmd_process_private *process_private;
1213 : :
1214 [ # # ]: 0 : if (!txq)
1215 : : return;
1216 : :
1217 : 0 : process_private = rte_eth_devices[txq->out_port].process_private;
1218 [ # # ]: 0 : if (dev->data->rx_queues[qid] == NULL)
1219 [ # # ]: 0 : tap_queue_close(process_private, qid);
1220 : : }
1221 : :
1222 : : static int
1223 : 0 : tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
1224 : : {
1225 : 0 : struct rte_eth_link *dev_link = &dev->data->dev_link;
1226 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1227 : 0 : struct ifreq ifr = { .ifr_flags = 0 };
1228 : :
1229 [ # # ]: 0 : if (pmd->remote_if_index) {
1230 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
1231 [ # # ]: 0 : if (!(ifr.ifr_flags & IFF_UP) ||
1232 : : !(ifr.ifr_flags & IFF_RUNNING)) {
1233 : 0 : dev_link->link_status = RTE_ETH_LINK_DOWN;
1234 : 0 : return 0;
1235 : : }
1236 : : }
1237 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
1238 : 0 : dev_link->link_status =
1239 : 0 : ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
1240 : 0 : RTE_ETH_LINK_UP :
1241 : : RTE_ETH_LINK_DOWN);
1242 : 0 : return 0;
1243 : : }
1244 : :
1245 : : static int
1246 : 0 : tap_promisc_enable(struct rte_eth_dev *dev)
1247 : : {
1248 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1249 : 0 : struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1250 : : int ret;
1251 : :
1252 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1253 [ # # ]: 0 : if (ret != 0)
1254 : : return ret;
1255 : :
1256 : : #ifdef HAVE_TCA_FLOWER
1257 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1258 : 0 : dev->data->promiscuous = 1;
1259 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
1260 [ # # ]: 0 : if (ret != 0) {
1261 : : /* Rollback promisc flag */
1262 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1263 : : /*
1264 : : * rte_eth_dev_promiscuous_enable() rollback
1265 : : * dev->data->promiscuous in the case of failure.
1266 : : */
1267 : 0 : return ret;
1268 : : }
1269 : : }
1270 : : #endif
1271 : : return 0;
1272 : : }
1273 : :
1274 : : static int
1275 : 0 : tap_promisc_disable(struct rte_eth_dev *dev)
1276 : : {
1277 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1278 : 0 : struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1279 : : int ret;
1280 : :
1281 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1282 [ # # ]: 0 : if (ret != 0)
1283 : : return ret;
1284 : :
1285 : : #ifdef HAVE_TCA_FLOWER
1286 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1287 : 0 : dev->data->promiscuous = 0;
1288 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
1289 [ # # ]: 0 : if (ret != 0) {
1290 : : /* Rollback promisc flag */
1291 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1292 : : /*
1293 : : * rte_eth_dev_promiscuous_disable() rollback
1294 : : * dev->data->promiscuous in the case of failure.
1295 : : */
1296 : 0 : return ret;
1297 : : }
1298 : : }
1299 : : #endif
1300 : :
1301 : : return 0;
1302 : : }
1303 : :
1304 : : static int
1305 : 0 : tap_allmulti_enable(struct rte_eth_dev *dev)
1306 : : {
1307 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1308 : 0 : struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1309 : : int ret;
1310 : :
1311 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1312 [ # # ]: 0 : if (ret != 0)
1313 : : return ret;
1314 : :
1315 : : #ifdef HAVE_TCA_FLOWER
1316 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1317 : 0 : dev->data->all_multicast = 1;
1318 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
1319 [ # # ]: 0 : if (ret != 0) {
1320 : : /* Rollback allmulti flag */
1321 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1322 : : /*
1323 : : * rte_eth_dev_allmulticast_enable() rollback
1324 : : * dev->data->all_multicast in the case of failure.
1325 : : */
1326 : 0 : return ret;
1327 : : }
1328 : : }
1329 : : #endif
1330 : :
1331 : : return 0;
1332 : : }
1333 : :
1334 : : static int
1335 : 0 : tap_allmulti_disable(struct rte_eth_dev *dev)
1336 : : {
1337 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1338 : 0 : struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1339 : : int ret;
1340 : :
1341 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1342 [ # # ]: 0 : if (ret != 0)
1343 : : return ret;
1344 : :
1345 : : #ifdef HAVE_TCA_FLOWER
1346 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1347 : 0 : dev->data->all_multicast = 0;
1348 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
1349 [ # # ]: 0 : if (ret != 0) {
1350 : : /* Rollback allmulti flag */
1351 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1352 : : /*
1353 : : * rte_eth_dev_allmulticast_disable() rollback
1354 : : * dev->data->all_multicast in the case of failure.
1355 : : */
1356 : 0 : return ret;
1357 : : }
1358 : : }
1359 : : #endif
1360 : :
1361 : : return 0;
1362 : : }
1363 : :
1364 : : static int
1365 : 0 : tap_mac_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1366 : : {
1367 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1368 : : enum ioctl_mode mode = LOCAL_ONLY;
1369 : : struct ifreq ifr;
1370 : : int ret;
1371 : :
1372 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TUN) {
1373 : 0 : TAP_LOG(ERR, "%s: can't MAC address for TUN",
1374 : : dev->device->name);
1375 : 0 : return -ENOTSUP;
1376 : : }
1377 : :
1378 [ # # ]: 0 : if (rte_is_zero_ether_addr(mac_addr)) {
1379 : 0 : TAP_LOG(ERR, "%s: can't set an empty MAC address",
1380 : : dev->device->name);
1381 : 0 : return -EINVAL;
1382 : : }
1383 : : /* Check the actual current MAC address on the tap netdevice */
1384 : 0 : ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY);
1385 [ # # ]: 0 : if (ret < 0)
1386 : : return ret;
1387 [ # # ]: 0 : if (rte_is_same_ether_addr(
1388 : : (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1389 : : mac_addr))
1390 : : return 0;
1391 : : /* Check the current MAC address on the remote */
1392 : 0 : ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY);
1393 [ # # ]: 0 : if (ret < 0)
1394 : : return ret;
1395 [ # # ]: 0 : if (!rte_is_same_ether_addr(
1396 : : (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1397 : : mac_addr))
1398 : : mode = LOCAL_AND_REMOTE;
1399 : 0 : ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1400 : :
1401 : : rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data);
1402 : 0 : ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode);
1403 [ # # ]: 0 : if (ret < 0)
1404 : : return ret;
1405 : :
1406 : : rte_ether_addr_copy(mac_addr, &pmd->eth_addr);
1407 : :
1408 : : #ifdef HAVE_TCA_FLOWER
1409 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1410 : : /* Replace MAC redirection rule after a MAC change */
1411 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC);
1412 [ # # ]: 0 : if (ret < 0) {
1413 : 0 : TAP_LOG(ERR,
1414 : : "%s: Couldn't delete MAC redirection rule",
1415 : : dev->device->name);
1416 : 0 : return ret;
1417 : : }
1418 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC);
1419 [ # # ]: 0 : if (ret < 0) {
1420 : 0 : TAP_LOG(ERR,
1421 : : "%s: Couldn't add MAC redirection rule",
1422 : : dev->device->name);
1423 : 0 : return ret;
1424 : : }
1425 : : }
1426 : : #endif
1427 : :
1428 : : return 0;
1429 : : }
1430 : :
1431 : : static int
1432 : 0 : tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev)
1433 : : {
1434 : : uint32_t gso_types;
1435 : : char pool_name[64];
1436 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1437 : : int ret;
1438 : :
1439 : : /* initialize GSO context */
1440 : : gso_types = RTE_ETH_TX_OFFLOAD_TCP_TSO;
1441 [ # # ]: 0 : if (!pmd->gso_ctx_mp) {
1442 : : /*
1443 : : * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE
1444 : : * bytes size per mbuf use this pool for both direct and
1445 : : * indirect mbufs
1446 : : */
1447 : 0 : ret = snprintf(pool_name, sizeof(pool_name), "mp_%s",
1448 [ # # ]: 0 : dev->device->name);
1449 [ # # ]: 0 : if (ret < 0 || ret >= (int)sizeof(pool_name)) {
1450 : 0 : TAP_LOG(ERR,
1451 : : "%s: failed to create mbuf pool name for device %s,"
1452 : : "device name too long or output error, ret: %d",
1453 : : pmd->name, dev->device->name, ret);
1454 : 0 : return -ENAMETOOLONG;
1455 : : }
1456 : 0 : pmd->gso_ctx_mp = rte_pktmbuf_pool_create(pool_name,
1457 : : TAP_GSO_MBUFS_NUM, TAP_GSO_MBUF_CACHE_SIZE, 0,
1458 : : RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE,
1459 : : SOCKET_ID_ANY);
1460 [ # # ]: 0 : if (!pmd->gso_ctx_mp) {
1461 : 0 : TAP_LOG(ERR,
1462 : : "%s: failed to create mbuf pool for device %s",
1463 : : pmd->name, dev->device->name);
1464 : 0 : return -1;
1465 : : }
1466 : : }
1467 : :
1468 : 0 : gso_ctx->direct_pool = pmd->gso_ctx_mp;
1469 : 0 : gso_ctx->indirect_pool = pmd->gso_ctx_mp;
1470 : 0 : gso_ctx->gso_types = gso_types;
1471 : 0 : gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */
1472 : 0 : gso_ctx->flag = 0;
1473 : :
1474 : 0 : return 0;
1475 : : }
1476 : :
1477 : : static int
1478 : 0 : tap_setup_queue(struct rte_eth_dev *dev,
1479 : : struct pmd_internals *internals,
1480 : : uint16_t qid,
1481 : : int is_rx)
1482 : : {
1483 : : int fd, ret;
1484 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1485 : 0 : struct pmd_process_private *process_private = dev->process_private;
1486 : 0 : struct rx_queue *rx = &internals->rxq[qid];
1487 : : struct tx_queue *tx = &internals->txq[qid];
1488 [ # # ]: 0 : struct rte_gso_ctx *gso_ctx = is_rx ? NULL : &tx->gso_ctx;
1489 [ # # ]: 0 : const char *dir = is_rx ? "rx" : "tx";
1490 : :
1491 : 0 : fd = process_private->fds[qid];
1492 [ # # ]: 0 : if (fd != -1) {
1493 : : /* fd for this queue already exists */
1494 : 0 : TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists",
1495 : : pmd->name, fd, dir, qid);
1496 : : gso_ctx = NULL;
1497 : : } else {
1498 : 0 : fd = tun_alloc(pmd, 0, 0);
1499 [ # # ]: 0 : if (fd < 0) {
1500 : 0 : TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name);
1501 : 0 : return -1;
1502 : : }
1503 : :
1504 : 0 : TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d",
1505 : : pmd->name, dir, qid, fd);
1506 : :
1507 : 0 : process_private->fds[qid] = fd;
1508 : : }
1509 : :
1510 : 0 : tx->mtu = &dev->data->mtu;
1511 : 0 : rx->rxmode = &dev->data->dev_conf.rxmode;
1512 [ # # ]: 0 : if (gso_ctx) {
1513 : 0 : ret = tap_gso_ctx_setup(gso_ctx, dev);
1514 [ # # ]: 0 : if (ret)
1515 : : return -1;
1516 : : }
1517 : :
1518 : 0 : tx->type = pmd->type;
1519 : :
1520 : 0 : return fd;
1521 : : }
1522 : :
1523 : : static int
1524 : 0 : tap_rx_queue_setup(struct rte_eth_dev *dev,
1525 : : uint16_t rx_queue_id,
1526 : : uint16_t nb_rx_desc,
1527 : : unsigned int socket_id,
1528 : : const struct rte_eth_rxconf *rx_conf __rte_unused,
1529 : : struct rte_mempool *mp)
1530 : : {
1531 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1532 : 0 : struct pmd_process_private *process_private = dev->process_private;
1533 : 0 : struct rx_queue *rxq = &internals->rxq[rx_queue_id];
1534 : 0 : struct rte_mbuf **tmp = &rxq->pool;
1535 : 0 : long iov_max = sysconf(_SC_IOV_MAX);
1536 : :
1537 [ # # ]: 0 : if (iov_max <= 0) {
1538 : 0 : TAP_LOG(WARNING,
1539 : : "_SC_IOV_MAX is not defined. Using %d as default",
1540 : : TAP_IOV_DEFAULT_MAX);
1541 : : iov_max = TAP_IOV_DEFAULT_MAX;
1542 : : }
1543 : 0 : uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
1544 : 0 : struct iovec (*iovecs)[nb_desc + 1];
1545 : : int data_off = RTE_PKTMBUF_HEADROOM;
1546 : : int ret = 0;
1547 : : int fd;
1548 : : int i;
1549 : :
1550 [ # # # # ]: 0 : if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
1551 : 0 : TAP_LOG(WARNING,
1552 : : "nb_rx_queues %d too small or mempool NULL",
1553 : : dev->data->nb_rx_queues);
1554 : 0 : return -1;
1555 : : }
1556 : :
1557 : 0 : rxq->mp = mp;
1558 : 0 : rxq->trigger_seen = 1; /* force initial burst */
1559 : 0 : rxq->in_port = dev->data->port_id;
1560 : 0 : rxq->queue_id = rx_queue_id;
1561 : 0 : rxq->nb_rx_desc = nb_desc;
1562 : 0 : iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1563 : : socket_id);
1564 [ # # ]: 0 : if (!iovecs) {
1565 : 0 : TAP_LOG(WARNING,
1566 : : "%s: Couldn't allocate %d RX descriptors",
1567 : : dev->device->name, nb_desc);
1568 : 0 : return -ENOMEM;
1569 : : }
1570 : 0 : rxq->iovecs = iovecs;
1571 : :
1572 : 0 : dev->data->rx_queues[rx_queue_id] = rxq;
1573 : 0 : fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1574 [ # # ]: 0 : if (fd == -1) {
1575 : : ret = fd;
1576 : 0 : goto error;
1577 : : }
1578 : :
1579 : 0 : (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1580 : 0 : (*rxq->iovecs)[0].iov_base = &rxq->pi;
1581 : :
1582 [ # # ]: 0 : for (i = 1; i <= nb_desc; i++) {
1583 : 0 : *tmp = rte_pktmbuf_alloc(rxq->mp);
1584 [ # # ]: 0 : if (!*tmp) {
1585 : 0 : TAP_LOG(WARNING,
1586 : : "%s: couldn't allocate memory for queue %d",
1587 : : dev->device->name, rx_queue_id);
1588 : : ret = -ENOMEM;
1589 : 0 : goto error;
1590 : : }
1591 : 0 : (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1592 : 0 : (*rxq->iovecs)[i].iov_base =
1593 : 0 : (char *)(*tmp)->buf_addr + data_off;
1594 : : data_off = 0;
1595 : 0 : tmp = &(*tmp)->next;
1596 : : }
1597 : :
1598 : 0 : TAP_LOG(DEBUG, " RX TUNTAP device name %s, qid %d on fd %d",
1599 : : internals->name, rx_queue_id,
1600 : : process_private->fds[rx_queue_id]);
1601 : :
1602 : 0 : return 0;
1603 : :
1604 : 0 : error:
1605 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1606 : 0 : rxq->pool = NULL;
1607 : 0 : rte_free(rxq->iovecs);
1608 : 0 : rxq->iovecs = NULL;
1609 : 0 : return ret;
1610 : : }
1611 : :
1612 : : static int
1613 : 0 : tap_tx_queue_setup(struct rte_eth_dev *dev,
1614 : : uint16_t tx_queue_id,
1615 : : uint16_t nb_tx_desc __rte_unused,
1616 : : unsigned int socket_id __rte_unused,
1617 : : const struct rte_eth_txconf *tx_conf)
1618 : : {
1619 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1620 : 0 : struct pmd_process_private *process_private = dev->process_private;
1621 : : struct tx_queue *txq;
1622 : : int ret;
1623 : : uint64_t offloads;
1624 : :
1625 [ # # ]: 0 : if (tx_queue_id >= dev->data->nb_tx_queues)
1626 : : return -1;
1627 : 0 : dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1628 : 0 : txq = dev->data->tx_queues[tx_queue_id];
1629 : 0 : txq->out_port = dev->data->port_id;
1630 : 0 : txq->queue_id = tx_queue_id;
1631 : :
1632 : 0 : offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1633 : 0 : txq->csum = !!(offloads &
1634 : : (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1635 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1636 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM));
1637 : :
1638 : 0 : ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1639 [ # # ]: 0 : if (ret == -1)
1640 : : return -1;
1641 [ # # ]: 0 : TAP_LOG(DEBUG,
1642 : : " TX TUNTAP device name %s, qid %d on fd %d csum %s",
1643 : : internals->name, tx_queue_id,
1644 : : process_private->fds[tx_queue_id],
1645 : : txq->csum ? "on" : "off");
1646 : :
1647 : 0 : return 0;
1648 : : }
1649 : :
1650 : : static int
1651 : 0 : tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1652 : : {
1653 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1654 : 0 : struct ifreq ifr = { .ifr_mtu = mtu };
1655 : :
1656 : 0 : return tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1657 : : }
1658 : :
1659 : : static int
1660 : 0 : tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1661 : : struct rte_ether_addr *mc_addr_set __rte_unused,
1662 : : uint32_t nb_mc_addr __rte_unused)
1663 : : {
1664 : : /*
1665 : : * Nothing to do actually: the tap has no filtering whatsoever, every
1666 : : * packet is received.
1667 : : */
1668 : 0 : return 0;
1669 : : }
1670 : :
1671 : : static int
1672 : 0 : tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1673 : : {
1674 : : struct rte_eth_dev *dev = arg;
1675 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1676 : : struct ifinfomsg *info = NLMSG_DATA(nh);
1677 : :
1678 [ # # ]: 0 : if (nh->nlmsg_type != RTM_NEWLINK ||
1679 [ # # ]: 0 : (info->ifi_index != pmd->if_index &&
1680 [ # # ]: 0 : info->ifi_index != pmd->remote_if_index))
1681 : : return 0;
1682 : 0 : return tap_link_update(dev, 0);
1683 : : }
1684 : :
1685 : : static void
1686 : 0 : tap_dev_intr_handler(void *cb_arg)
1687 : : {
1688 : : struct rte_eth_dev *dev = cb_arg;
1689 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1690 : :
1691 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) >= 0)
1692 : 0 : tap_nl_recv(rte_intr_fd_get(pmd->intr_handle),
1693 : : tap_nl_msg_handler, dev);
1694 : 0 : }
1695 : :
1696 : : static int
1697 : 0 : tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
1698 : : {
1699 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1700 : : int ret;
1701 : :
1702 : : /* In any case, disable interrupt if the conf is no longer there. */
1703 [ # # ]: 0 : if (!dev->data->dev_conf.intr_conf.lsc) {
1704 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) != -1)
1705 : 0 : goto clean;
1706 : :
1707 : : return 0;
1708 : : }
1709 [ # # ]: 0 : if (set) {
1710 : 0 : rte_intr_fd_set(pmd->intr_handle, tap_nl_init(RTMGRP_LINK));
1711 [ # # ]: 0 : if (unlikely(rte_intr_fd_get(pmd->intr_handle) == -1))
1712 : : return -EBADF;
1713 : 0 : return rte_intr_callback_register(
1714 : 0 : pmd->intr_handle, tap_dev_intr_handler, dev);
1715 : : }
1716 : :
1717 : 0 : clean:
1718 : : do {
1719 : 0 : ret = rte_intr_callback_unregister(pmd->intr_handle,
1720 : : tap_dev_intr_handler, dev);
1721 [ # # ]: 0 : if (ret >= 0) {
1722 : : break;
1723 [ # # ]: 0 : } else if (ret == -EAGAIN) {
1724 : : rte_delay_ms(100);
1725 : : } else {
1726 : 0 : TAP_LOG(ERR, "intr callback unregister failed: %d",
1727 : : ret);
1728 : 0 : break;
1729 : : }
1730 : : } while (true);
1731 : :
1732 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) >= 0) {
1733 : 0 : tap_nl_final(rte_intr_fd_get(pmd->intr_handle));
1734 : 0 : rte_intr_fd_set(pmd->intr_handle, -1);
1735 : : }
1736 : :
1737 : : return 0;
1738 : : }
1739 : :
1740 : : static int
1741 : 0 : tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1742 : : {
1743 : : int err;
1744 : :
1745 : 0 : err = tap_lsc_intr_handle_set(dev, set);
1746 [ # # ]: 0 : if (err < 0) {
1747 [ # # ]: 0 : if (!set)
1748 : 0 : tap_rx_intr_vec_set(dev, 0);
1749 : 0 : return err;
1750 : : }
1751 : 0 : err = tap_rx_intr_vec_set(dev, set);
1752 [ # # ]: 0 : if (err && set)
1753 : 0 : tap_lsc_intr_handle_set(dev, 0);
1754 : : return err;
1755 : : }
1756 : :
1757 : : static const uint32_t*
1758 : 0 : tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused,
1759 : : size_t *no_of_elements)
1760 : : {
1761 : : static const uint32_t ptypes[] = {
1762 : : RTE_PTYPE_INNER_L2_ETHER,
1763 : : RTE_PTYPE_INNER_L2_ETHER_VLAN,
1764 : : RTE_PTYPE_INNER_L2_ETHER_QINQ,
1765 : : RTE_PTYPE_INNER_L3_IPV4,
1766 : : RTE_PTYPE_INNER_L3_IPV4_EXT,
1767 : : RTE_PTYPE_INNER_L3_IPV6,
1768 : : RTE_PTYPE_INNER_L3_IPV6_EXT,
1769 : : RTE_PTYPE_INNER_L4_FRAG,
1770 : : RTE_PTYPE_INNER_L4_UDP,
1771 : : RTE_PTYPE_INNER_L4_TCP,
1772 : : RTE_PTYPE_INNER_L4_SCTP,
1773 : : RTE_PTYPE_L2_ETHER,
1774 : : RTE_PTYPE_L2_ETHER_VLAN,
1775 : : RTE_PTYPE_L2_ETHER_QINQ,
1776 : : RTE_PTYPE_L3_IPV4,
1777 : : RTE_PTYPE_L3_IPV4_EXT,
1778 : : RTE_PTYPE_L3_IPV6_EXT,
1779 : : RTE_PTYPE_L3_IPV6,
1780 : : RTE_PTYPE_L4_FRAG,
1781 : : RTE_PTYPE_L4_UDP,
1782 : : RTE_PTYPE_L4_TCP,
1783 : : RTE_PTYPE_L4_SCTP,
1784 : : };
1785 : :
1786 : 0 : *no_of_elements = RTE_DIM(ptypes);
1787 : 0 : return ptypes;
1788 : : }
1789 : :
1790 : : static int
1791 : 0 : tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1792 : : struct rte_eth_fc_conf *fc_conf)
1793 : : {
1794 : 0 : fc_conf->mode = RTE_ETH_FC_NONE;
1795 : 0 : return 0;
1796 : : }
1797 : :
1798 : : static int
1799 : 0 : tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1800 : : struct rte_eth_fc_conf *fc_conf)
1801 : : {
1802 [ # # ]: 0 : if (fc_conf->mode != RTE_ETH_FC_NONE)
1803 : 0 : return -ENOTSUP;
1804 : : return 0;
1805 : : }
1806 : :
1807 : : /**
1808 : : * DPDK callback to update the RSS hash configuration.
1809 : : *
1810 : : * @param dev
1811 : : * Pointer to Ethernet device structure.
1812 : : * @param[in] rss_conf
1813 : : * RSS configuration data.
1814 : : *
1815 : : * @return
1816 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1817 : : */
1818 : : static int
1819 : 0 : tap_rss_hash_update(struct rte_eth_dev *dev,
1820 : : struct rte_eth_rss_conf *rss_conf)
1821 : : {
1822 [ # # ]: 0 : if (rss_conf->rss_hf & TAP_RSS_HF_MASK) {
1823 : 0 : rte_errno = EINVAL;
1824 : 0 : return -rte_errno;
1825 : : }
1826 [ # # # # ]: 0 : if (rss_conf->rss_key && rss_conf->rss_key_len) {
1827 : : /*
1828 : : * Currently TAP RSS key is hard coded
1829 : : * and cannot be updated
1830 : : */
1831 : 0 : TAP_LOG(ERR,
1832 : : "port %u RSS key cannot be updated",
1833 : : dev->data->port_id);
1834 : 0 : rte_errno = EINVAL;
1835 : 0 : return -rte_errno;
1836 : : }
1837 : : return 0;
1838 : : }
1839 : :
1840 : : static int
1841 : 0 : tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1842 : : {
1843 : 0 : dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1844 : :
1845 : 0 : return 0;
1846 : : }
1847 : :
1848 : : static int
1849 : 0 : tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1850 : : {
1851 : 0 : dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1852 : :
1853 : 0 : return 0;
1854 : : }
1855 : :
1856 : : static int
1857 : 0 : tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1858 : : {
1859 : 0 : dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1860 : :
1861 : 0 : return 0;
1862 : : }
1863 : :
1864 : : static int
1865 : 0 : tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1866 : : {
1867 : 0 : dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1868 : :
1869 : 0 : return 0;
1870 : : }
1871 : : static const struct eth_dev_ops ops = {
1872 : : .dev_start = tap_dev_start,
1873 : : .dev_stop = tap_dev_stop,
1874 : : .dev_close = tap_dev_close,
1875 : : .dev_configure = tap_dev_configure,
1876 : : .dev_infos_get = tap_dev_info,
1877 : : .rx_queue_setup = tap_rx_queue_setup,
1878 : : .tx_queue_setup = tap_tx_queue_setup,
1879 : : .rx_queue_start = tap_rx_queue_start,
1880 : : .tx_queue_start = tap_tx_queue_start,
1881 : : .rx_queue_stop = tap_rx_queue_stop,
1882 : : .tx_queue_stop = tap_tx_queue_stop,
1883 : : .rx_queue_release = tap_rx_queue_release,
1884 : : .tx_queue_release = tap_tx_queue_release,
1885 : : .flow_ctrl_get = tap_flow_ctrl_get,
1886 : : .flow_ctrl_set = tap_flow_ctrl_set,
1887 : : .link_update = tap_link_update,
1888 : : .dev_set_link_up = tap_link_set_up,
1889 : : .dev_set_link_down = tap_link_set_down,
1890 : : .promiscuous_enable = tap_promisc_enable,
1891 : : .promiscuous_disable = tap_promisc_disable,
1892 : : .allmulticast_enable = tap_allmulti_enable,
1893 : : .allmulticast_disable = tap_allmulti_disable,
1894 : : .mac_addr_set = tap_mac_set,
1895 : : .mtu_set = tap_mtu_set,
1896 : : .set_mc_addr_list = tap_set_mc_addr_list,
1897 : : .stats_get = tap_stats_get,
1898 : : .stats_reset = tap_stats_reset,
1899 : : .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1900 : : .rss_hash_update = tap_rss_hash_update,
1901 : : #ifdef HAVE_TCA_FLOWER
1902 : : .flow_ops_get = tap_dev_flow_ops_get,
1903 : : #endif
1904 : : };
1905 : :
1906 : : static int
1907 : 0 : eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name,
1908 : : char *remote_iface, struct rte_ether_addr *mac_addr,
1909 : : enum rte_tuntap_type type, int persist)
1910 : : {
1911 : 0 : int numa_node = rte_socket_id();
1912 : : struct rte_eth_dev *dev;
1913 : : struct pmd_internals *pmd;
1914 : : struct pmd_process_private *process_private;
1915 : 0 : const char *tuntap_name = tuntap_types[type];
1916 : : struct rte_eth_dev_data *data;
1917 : : struct ifreq ifr;
1918 : : int i;
1919 : :
1920 : 0 : TAP_LOG(DEBUG, "%s device on numa %u", tuntap_name, rte_socket_id());
1921 : :
1922 : 0 : dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1923 [ # # ]: 0 : if (!dev) {
1924 : 0 : TAP_LOG(ERR, "%s Unable to allocate device struct",
1925 : : tuntap_name);
1926 : 0 : goto error_exit_nodev;
1927 : : }
1928 : :
1929 : 0 : process_private = malloc(sizeof(struct pmd_process_private));
1930 [ # # ]: 0 : if (process_private == NULL) {
1931 : 0 : TAP_LOG(ERR, "Failed to alloc memory for process private");
1932 : 0 : return -1;
1933 : : }
1934 : : memset(process_private, 0, sizeof(struct pmd_process_private));
1935 : :
1936 : 0 : pmd = dev->data->dev_private;
1937 : 0 : dev->process_private = process_private;
1938 : 0 : pmd->dev = dev;
1939 : 0 : strlcpy(pmd->name, tap_name, sizeof(pmd->name));
1940 : 0 : pmd->type = type;
1941 : 0 : pmd->ka_fd = -1;
1942 : :
1943 : : #ifdef HAVE_TCA_FLOWER
1944 : 0 : pmd->nlsk_fd = -1;
1945 : : #endif
1946 : 0 : pmd->gso_ctx_mp = NULL;
1947 : :
1948 : 0 : pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1949 [ # # ]: 0 : if (pmd->ioctl_sock == -1) {
1950 : 0 : TAP_LOG(ERR,
1951 : : "%s Unable to get a socket for management: %s",
1952 : : tuntap_name, strerror(errno));
1953 : 0 : goto error_exit;
1954 : : }
1955 : :
1956 : : /* Allocate interrupt instance */
1957 : 0 : pmd->intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
1958 [ # # ]: 0 : if (pmd->intr_handle == NULL) {
1959 : 0 : TAP_LOG(ERR, "Failed to allocate intr handle");
1960 : 0 : goto error_exit;
1961 : : }
1962 : :
1963 : : /* Setup some default values */
1964 : 0 : data = dev->data;
1965 : 0 : data->dev_private = pmd;
1966 : 0 : data->dev_flags = RTE_ETH_DEV_INTR_LSC |
1967 : : RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1968 : 0 : data->numa_node = numa_node;
1969 : :
1970 : 0 : data->dev_link = pmd_link;
1971 : 0 : data->mac_addrs = &pmd->eth_addr;
1972 : : /* Set the number of RX and TX queues */
1973 : 0 : data->nb_rx_queues = 0;
1974 : 0 : data->nb_tx_queues = 0;
1975 : :
1976 : 0 : dev->dev_ops = &ops;
1977 : 0 : dev->rx_pkt_burst = pmd_rx_burst;
1978 : 0 : dev->tx_pkt_burst = pmd_tx_burst;
1979 : :
1980 : 0 : rte_intr_type_set(pmd->intr_handle, RTE_INTR_HANDLE_EXT);
1981 : 0 : rte_intr_fd_set(pmd->intr_handle, -1);
1982 : 0 : dev->intr_handle = pmd->intr_handle;
1983 : :
1984 : : /* Presetup the fds to -1 as being not valid */
1985 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++)
1986 : 0 : process_private->fds[i] = -1;
1987 : :
1988 : :
1989 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1990 [ # # ]: 0 : if (rte_is_zero_ether_addr(mac_addr))
1991 : 0 : rte_eth_random_addr((uint8_t *)&pmd->eth_addr);
1992 : : else
1993 : : rte_ether_addr_copy(mac_addr, &pmd->eth_addr);
1994 : : }
1995 : :
1996 : : /*
1997 : : * Allocate a TUN device keep-alive file descriptor that will only be
1998 : : * closed when the TUN device itself is closed or removed.
1999 : : * This keep-alive file descriptor will guarantee that the TUN device
2000 : : * exists even when all of its queues are closed
2001 : : */
2002 : 0 : pmd->ka_fd = tun_alloc(pmd, 1, persist);
2003 [ # # ]: 0 : if (pmd->ka_fd == -1) {
2004 : 0 : TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
2005 : 0 : goto error_exit;
2006 : : }
2007 : 0 : TAP_LOG(DEBUG, "allocated %s", pmd->name);
2008 : :
2009 : 0 : ifr.ifr_mtu = dev->data->mtu;
2010 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
2011 : 0 : goto error_exit;
2012 : :
2013 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
2014 : : memset(&ifr, 0, sizeof(struct ifreq));
2015 : 0 : ifr.ifr_hwaddr.sa_family = AF_LOCAL;
2016 : : rte_ether_addr_copy(&pmd->eth_addr, (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data);
2017 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
2018 : 0 : goto error_exit;
2019 : : }
2020 : :
2021 : : /* Make network device persist after application exit */
2022 : 0 : pmd->persist = persist;
2023 : :
2024 : : #ifdef HAVE_TCA_FLOWER
2025 : : /*
2026 : : * Set up everything related to rte_flow:
2027 : : * - netlink socket
2028 : : * - tap / remote if_index
2029 : : * - mandatory QDISCs
2030 : : * - rte_flow actual/implicit lists
2031 : : * - implicit rules
2032 : : */
2033 : 0 : pmd->nlsk_fd = tap_nl_init(0);
2034 [ # # ]: 0 : if (pmd->nlsk_fd == -1) {
2035 : 0 : TAP_LOG(WARNING, "%s: failed to create netlink socket.",
2036 : : pmd->name);
2037 : 0 : goto disable_rte_flow;
2038 : : }
2039 : 0 : pmd->if_index = if_nametoindex(pmd->name);
2040 [ # # ]: 0 : if (!pmd->if_index) {
2041 : 0 : TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name);
2042 : 0 : goto disable_rte_flow;
2043 : : }
2044 [ # # ]: 0 : if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
2045 : 0 : TAP_LOG(ERR, "%s: failed to create multiq qdisc.",
2046 : : pmd->name);
2047 : 0 : goto disable_rte_flow;
2048 : : }
2049 [ # # ]: 0 : if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
2050 : 0 : TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2051 : : pmd->name);
2052 : 0 : goto disable_rte_flow;
2053 : : }
2054 : :
2055 : 0 : LIST_INIT(&pmd->flows);
2056 : :
2057 [ # # ]: 0 : if (strlen(remote_iface)) {
2058 : 0 : pmd->remote_if_index = if_nametoindex(remote_iface);
2059 [ # # ]: 0 : if (!pmd->remote_if_index) {
2060 : 0 : TAP_LOG(ERR, "%s: failed to get %s if_index.",
2061 : : pmd->name, remote_iface);
2062 : 0 : goto error_remote;
2063 : : }
2064 : 0 : strlcpy(pmd->remote_iface, remote_iface, RTE_ETH_NAME_MAX_LEN);
2065 : :
2066 : : /* Save state of remote device */
2067 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
2068 : :
2069 : : /* Replicate remote MAC address */
2070 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
2071 : 0 : TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2072 : : pmd->name, pmd->remote_iface);
2073 : 0 : goto error_remote;
2074 : : }
2075 : :
2076 : : rte_ether_addr_copy((struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data, &pmd->eth_addr);
2077 : : /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
2078 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
2079 : 0 : TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2080 : : pmd->name, remote_iface);
2081 : 0 : goto error_remote;
2082 : : }
2083 : :
2084 : : /*
2085 : : * Flush usually returns negative value because it tries to
2086 : : * delete every QDISC (and on a running device, one QDISC at
2087 : : * least is needed). Ignore negative return value.
2088 : : */
2089 : 0 : qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
2090 [ # # ]: 0 : if (qdisc_create_ingress(pmd->nlsk_fd,
2091 : 0 : pmd->remote_if_index) < 0) {
2092 : 0 : TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2093 : : pmd->remote_iface);
2094 : 0 : goto error_remote;
2095 : : }
2096 : 0 : LIST_INIT(&pmd->implicit_flows);
2097 [ # # # # ]: 0 : if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
2098 [ # # ]: 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
2099 [ # # ]: 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
2100 : 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
2101 : 0 : TAP_LOG(ERR,
2102 : : "%s: failed to create implicit rules.",
2103 : : pmd->name);
2104 : 0 : goto error_remote;
2105 : : }
2106 : : }
2107 : : #endif
2108 : :
2109 : 0 : rte_eth_dev_probing_finish(dev);
2110 : 0 : return 0;
2111 : :
2112 : 0 : disable_rte_flow:
2113 : 0 : TAP_LOG(ERR, " Disabling rte flow support: %s(%d)",
2114 : : strerror(errno), errno);
2115 [ # # ]: 0 : if (strlen(remote_iface)) {
2116 : 0 : TAP_LOG(ERR, "Remote feature requires flow support.");
2117 : 0 : goto error_exit;
2118 : : }
2119 : 0 : rte_eth_dev_probing_finish(dev);
2120 : 0 : return 0;
2121 : :
2122 : : #ifdef HAVE_TCA_FLOWER
2123 : 0 : error_remote:
2124 : 0 : TAP_LOG(ERR, " Can't set up remote feature: %s(%d)",
2125 : : strerror(errno), errno);
2126 : 0 : tap_flow_implicit_flush(pmd, NULL);
2127 : : #endif
2128 : :
2129 : 0 : error_exit:
2130 : : #ifdef HAVE_TCA_FLOWER
2131 [ # # ]: 0 : if (pmd->nlsk_fd != -1)
2132 : 0 : close(pmd->nlsk_fd);
2133 : : #endif
2134 [ # # ]: 0 : if (pmd->ka_fd != -1)
2135 : 0 : close(pmd->ka_fd);
2136 [ # # ]: 0 : if (pmd->ioctl_sock != -1)
2137 : 0 : close(pmd->ioctl_sock);
2138 : : /* mac_addrs must not be freed alone because part of dev_private */
2139 : 0 : dev->data->mac_addrs = NULL;
2140 : 0 : rte_intr_instance_free(pmd->intr_handle);
2141 : 0 : rte_eth_dev_release_port(dev);
2142 : :
2143 [ # # ]: 0 : error_exit_nodev:
2144 : 0 : TAP_LOG(ERR, "%s Unable to initialize %s",
2145 : : tuntap_name, rte_vdev_device_name(vdev));
2146 : :
2147 : 0 : return -EINVAL;
2148 : : }
2149 : :
2150 : : /* make sure name is a possible Linux network device name */
2151 : : static bool
2152 : 0 : is_valid_iface(const char *name)
2153 : : {
2154 [ # # ]: 0 : if (*name == '\0')
2155 : : return false;
2156 : :
2157 [ # # ]: 0 : if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
2158 : : return false;
2159 : :
2160 [ # # ]: 0 : while (*name) {
2161 [ # # # # ]: 0 : if (*name == '/' || *name == ':' || isspace(*name))
2162 : : return false;
2163 : 0 : name++;
2164 : : }
2165 : : return true;
2166 : : }
2167 : :
2168 : : static int
2169 : 0 : set_interface_name(const char *key __rte_unused,
2170 : : const char *value,
2171 : : void *extra_args)
2172 : : {
2173 : : char *name = (char *)extra_args;
2174 : :
2175 [ # # ]: 0 : if (value) {
2176 [ # # ]: 0 : if (!is_valid_iface(value)) {
2177 : 0 : TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2178 : : value);
2179 : 0 : return -1;
2180 : : }
2181 : : strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2182 : : } else {
2183 : : /* use tap%d which causes kernel to choose next available */
2184 : : strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2185 : : }
2186 : : return 0;
2187 : : }
2188 : :
2189 : : static int
2190 : 0 : set_remote_iface(const char *key __rte_unused,
2191 : : const char *value,
2192 : : void *extra_args)
2193 : : {
2194 : : char *name = (char *)extra_args;
2195 : :
2196 [ # # ]: 0 : if (value) {
2197 [ # # ]: 0 : if (!is_valid_iface(value)) {
2198 : 0 : TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2199 : : value);
2200 : 0 : return -1;
2201 : : }
2202 : : strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2203 : : }
2204 : :
2205 : : return 0;
2206 : : }
2207 : :
2208 : : static int
2209 : 0 : set_mac_type(const char *key __rte_unused,
2210 : : const char *value,
2211 : : void *extra_args)
2212 : : {
2213 : : struct rte_ether_addr *user_mac = extra_args;
2214 : :
2215 [ # # ]: 0 : if (!value)
2216 : : return 0;
2217 : :
2218 [ # # ]: 0 : if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) {
2219 : : static int iface_idx;
2220 : :
2221 : : /* fixed mac = 02:64:74:61:70:<iface_idx> */
2222 : 0 : memcpy((char *)user_mac->addr_bytes, "\002dtap",
2223 : : RTE_ETHER_ADDR_LEN);
2224 : 0 : user_mac->addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
2225 : 0 : iface_idx++ + '0';
2226 : 0 : goto success;
2227 : : }
2228 : :
2229 [ # # ]: 0 : if (rte_ether_unformat_addr(value, user_mac) < 0)
2230 : 0 : goto error;
2231 : 0 : success:
2232 : 0 : TAP_LOG(DEBUG, "TAP user MAC param (%s)", value);
2233 : 0 : return 0;
2234 : :
2235 : : error:
2236 : 0 : TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)",
2237 : : value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT);
2238 : 0 : return -1;
2239 : : }
2240 : :
2241 : : /*
2242 : : * Open a TUN interface device. TUN PMD
2243 : : * 1) sets tap_type as false
2244 : : * 2) intakes iface as argument.
2245 : : * 3) as interface is virtual set speed to 10G
2246 : : */
2247 : : static int
2248 [ # # ]: 0 : rte_pmd_tun_probe(struct rte_vdev_device *dev)
2249 : : {
2250 : : const char *name, *params;
2251 : : int ret;
2252 : : struct rte_kvargs *kvlist = NULL;
2253 : : char tun_name[RTE_ETH_NAME_MAX_LEN];
2254 : : char remote_iface[RTE_ETH_NAME_MAX_LEN];
2255 : : struct rte_eth_dev *eth_dev;
2256 : :
2257 : : name = rte_vdev_device_name(dev);
2258 : : params = rte_vdev_device_args(dev);
2259 : : memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2260 : :
2261 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
2262 [ # # ]: 0 : strlen(params) == 0) {
2263 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
2264 [ # # ]: 0 : if (!eth_dev) {
2265 : 0 : TAP_LOG(ERR, "Failed to probe %s", name);
2266 : 0 : return -1;
2267 : : }
2268 : 0 : eth_dev->dev_ops = &ops;
2269 : 0 : eth_dev->device = &dev->device;
2270 : 0 : rte_eth_dev_probing_finish(eth_dev);
2271 : 0 : return 0;
2272 : : }
2273 : :
2274 : : /* use tun%d which causes kernel to choose next available */
2275 : : strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2276 : :
2277 [ # # # # ]: 0 : if (params && (params[0] != '\0')) {
2278 : 0 : TAP_LOG(DEBUG, "parameters (%s)", params);
2279 : :
2280 : 0 : kvlist = rte_kvargs_parse(params, valid_arguments);
2281 [ # # ]: 0 : if (kvlist) {
2282 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2283 : 0 : ret = rte_kvargs_process_opt(kvlist,
2284 : : ETH_TAP_IFACE_ARG,
2285 : : &set_interface_name,
2286 : : tun_name);
2287 : :
2288 [ # # ]: 0 : if (ret == -1)
2289 : 0 : goto leave;
2290 : : }
2291 : : }
2292 : : }
2293 : 0 : pmd_link.link_speed = RTE_ETH_SPEED_NUM_10G;
2294 : :
2295 : 0 : TAP_LOG(DEBUG, "Initializing pmd_tun for %s", name);
2296 : :
2297 : 0 : ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0,
2298 : : ETH_TUNTAP_TYPE_TUN, 0);
2299 : :
2300 : 0 : leave:
2301 [ # # ]: 0 : if (ret == -1) {
2302 : 0 : TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2303 : : name, tun_name);
2304 : : }
2305 : 0 : rte_kvargs_free(kvlist);
2306 : :
2307 : 0 : return ret;
2308 : : }
2309 : :
2310 : : /* Request queue file descriptors from secondary to primary. */
2311 : : static int
2312 : 0 : tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev)
2313 : : {
2314 : : int ret;
2315 : 0 : struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
2316 : : struct rte_mp_msg request, *reply;
2317 : : struct rte_mp_reply replies;
2318 : : struct ipc_queues *request_param = (struct ipc_queues *)request.param;
2319 : : struct ipc_queues *reply_param;
2320 : 0 : struct pmd_process_private *process_private = dev->process_private;
2321 : :
2322 : : /* Prepare the request */
2323 : : memset(&request, 0, sizeof(request));
2324 : : strlcpy(request.name, TAP_MP_KEY, sizeof(request.name));
2325 : : strlcpy(request_param->port_name, port_name,
2326 : : sizeof(request_param->port_name));
2327 : 0 : request.len_param = sizeof(*request_param);
2328 : : /* Send request and receive reply */
2329 : 0 : ret = rte_mp_request_sync(&request, &replies, &timeout);
2330 [ # # # # ]: 0 : if (ret < 0 || replies.nb_received != 1) {
2331 : 0 : TAP_LOG(ERR, "Failed to request queues from primary: %d",
2332 : : rte_errno);
2333 : 0 : return -1;
2334 : : }
2335 : 0 : reply = &replies.msgs[0];
2336 : : reply_param = (struct ipc_queues *)reply->param;
2337 : 0 : TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name);
2338 : :
2339 : : /* Attach the queues from received file descriptors */
2340 [ # # ]: 0 : if (reply_param->q_count != reply->num_fds) {
2341 : 0 : TAP_LOG(ERR, "Unexpected number of fds received");
2342 : 0 : return -1;
2343 : : }
2344 : :
2345 : 0 : dev->data->nb_rx_queues = reply_param->q_count;
2346 : 0 : dev->data->nb_tx_queues = reply_param->q_count;
2347 : :
2348 [ # # ]: 0 : for (int q = 0; q < reply_param->q_count; q++)
2349 : 0 : process_private->fds[q] = reply->fds[q];
2350 : :
2351 : 0 : free(reply);
2352 : 0 : return 0;
2353 : : }
2354 : :
2355 : : /* Send the queue file descriptors from the primary process to secondary. */
2356 : : static int
2357 : 0 : tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer)
2358 : : {
2359 : : struct rte_eth_dev *dev;
2360 : : struct pmd_process_private *process_private;
2361 : : struct rte_mp_msg reply;
2362 : : const struct ipc_queues *request_param =
2363 : : (const struct ipc_queues *)request->param;
2364 : : struct ipc_queues *reply_param =
2365 : : (struct ipc_queues *)reply.param;
2366 : : int queue;
2367 : :
2368 : : /* Get requested port */
2369 : 0 : TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name);
2370 : 0 : dev = rte_eth_dev_get_by_name(request_param->port_name);
2371 [ # # ]: 0 : if (!dev) {
2372 : 0 : TAP_LOG(ERR, "Failed to get port id for %s",
2373 : : request_param->port_name);
2374 : 0 : return -1;
2375 : : }
2376 : 0 : process_private = dev->process_private;
2377 : :
2378 : : /* Fill file descriptors for all queues */
2379 : 0 : reply.num_fds = 0;
2380 : 0 : reply_param->q_count = 0;
2381 : :
2382 : : RTE_ASSERT(dev->data->nb_rx_queues == dev->data->nb_tx_queues);
2383 : :
2384 [ # # ]: 0 : if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
2385 : 0 : TAP_LOG(ERR, "Number of rx/tx queues %u exceeds max number of fds %u",
2386 : : dev->data->nb_rx_queues, RTE_PMD_TAP_MAX_QUEUES);
2387 : 0 : return -1;
2388 : : }
2389 : :
2390 [ # # ]: 0 : for (queue = 0; queue < dev->data->nb_rx_queues; queue++) {
2391 : 0 : reply.fds[reply.num_fds++] = process_private->fds[queue];
2392 : 0 : reply_param->q_count++;
2393 : : }
2394 : :
2395 : : /* Send reply */
2396 : 0 : strlcpy(reply.name, request->name, sizeof(reply.name));
2397 : : strlcpy(reply_param->port_name, request_param->port_name,
2398 : : sizeof(reply_param->port_name));
2399 : 0 : reply.len_param = sizeof(*reply_param);
2400 [ # # ]: 0 : if (rte_mp_reply(&reply, peer) < 0) {
2401 : 0 : TAP_LOG(ERR, "Failed to reply an IPC request to sync queues");
2402 : 0 : return -1;
2403 : : }
2404 : : return 0;
2405 : : }
2406 : :
2407 : : /* Open a TAP interface device.
2408 : : */
2409 : : static int
2410 : 0 : rte_pmd_tap_probe(struct rte_vdev_device *dev)
2411 : : {
2412 : : const char *name, *params;
2413 : : int ret;
2414 : : struct rte_kvargs *kvlist = NULL;
2415 : : int speed;
2416 : : char tap_name[RTE_ETH_NAME_MAX_LEN];
2417 : : char remote_iface[RTE_ETH_NAME_MAX_LEN];
2418 [ # # ]: 0 : struct rte_ether_addr user_mac = { .addr_bytes = {0} };
2419 : : struct rte_eth_dev *eth_dev;
2420 : : int tap_devices_count_increased = 0;
2421 : : int persist = 0;
2422 : :
2423 : : name = rte_vdev_device_name(dev);
2424 : : params = rte_vdev_device_args(dev);
2425 : :
2426 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2427 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
2428 [ # # ]: 0 : if (!eth_dev) {
2429 : 0 : TAP_LOG(ERR, "Failed to probe %s", name);
2430 : 0 : return -1;
2431 : : }
2432 : 0 : eth_dev->dev_ops = &ops;
2433 : 0 : eth_dev->device = &dev->device;
2434 : 0 : eth_dev->rx_pkt_burst = pmd_rx_burst;
2435 : 0 : eth_dev->tx_pkt_burst = pmd_tx_burst;
2436 [ # # ]: 0 : if (!rte_eal_primary_proc_alive(NULL)) {
2437 : 0 : TAP_LOG(ERR, "Primary process is missing");
2438 : 0 : return -1;
2439 : : }
2440 : 0 : eth_dev->process_private = malloc(sizeof(struct pmd_process_private));
2441 [ # # ]: 0 : if (eth_dev->process_private == NULL) {
2442 : 0 : TAP_LOG(ERR,
2443 : : "Failed to alloc memory for process private");
2444 : 0 : return -1;
2445 : : }
2446 : : memset(eth_dev->process_private, 0, sizeof(struct pmd_process_private));
2447 : :
2448 : 0 : ret = tap_mp_attach_queues(name, eth_dev);
2449 [ # # ]: 0 : if (ret != 0)
2450 : : return -1;
2451 : :
2452 [ # # ]: 0 : if (!tap_devices_count) {
2453 : 0 : ret = rte_mp_action_register(TAP_MP_REQ_START_RXTX, tap_mp_req_start_rxtx);
2454 [ # # # # ]: 0 : if (ret < 0 && rte_errno != ENOTSUP) {
2455 : 0 : TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2456 : : strerror(rte_errno));
2457 : 0 : return -1;
2458 : : }
2459 : : }
2460 : 0 : tap_devices_count++;
2461 : 0 : rte_eth_dev_probing_finish(eth_dev);
2462 : 0 : return 0;
2463 : : }
2464 : :
2465 : : speed = RTE_ETH_SPEED_NUM_10G;
2466 : :
2467 : : /* use tap%d which causes kernel to choose next available */
2468 : : strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2469 : : memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2470 : :
2471 [ # # # # ]: 0 : if (params && (params[0] != '\0')) {
2472 : 0 : TAP_LOG(DEBUG, "parameters (%s)", params);
2473 : :
2474 : 0 : kvlist = rte_kvargs_parse(params, valid_arguments);
2475 [ # # ]: 0 : if (kvlist) {
2476 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2477 : 0 : ret = rte_kvargs_process_opt(kvlist,
2478 : : ETH_TAP_IFACE_ARG,
2479 : : &set_interface_name,
2480 : : tap_name);
2481 [ # # ]: 0 : if (ret == -1)
2482 : 0 : goto leave;
2483 : : }
2484 : :
2485 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
2486 : 0 : ret = rte_kvargs_process(kvlist,
2487 : : ETH_TAP_REMOTE_ARG,
2488 : : &set_remote_iface,
2489 : : remote_iface);
2490 [ # # ]: 0 : if (ret == -1)
2491 : 0 : goto leave;
2492 : : }
2493 : :
2494 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
2495 : 0 : ret = rte_kvargs_process(kvlist,
2496 : : ETH_TAP_MAC_ARG,
2497 : : &set_mac_type,
2498 : : &user_mac);
2499 [ # # ]: 0 : if (ret == -1)
2500 : 0 : goto leave;
2501 : : }
2502 : :
2503 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_PERSIST_ARG) == 1)
2504 : : persist = 1;
2505 : : }
2506 : : }
2507 : 0 : pmd_link.link_speed = speed;
2508 : :
2509 : 0 : TAP_LOG(DEBUG, "Initializing pmd_tap for %s", name);
2510 : :
2511 : : /* Register IPC feed callback */
2512 [ # # ]: 0 : if (!tap_devices_count) {
2513 : 0 : ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues);
2514 [ # # # # ]: 0 : if (ret < 0 && rte_errno != ENOTSUP) {
2515 : 0 : TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2516 : : strerror(rte_errno));
2517 : 0 : goto leave;
2518 : : }
2519 : : }
2520 : 0 : tap_devices_count++;
2521 : : tap_devices_count_increased = 1;
2522 : 0 : ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac,
2523 : : ETH_TUNTAP_TYPE_TAP, persist);
2524 : :
2525 : 0 : leave:
2526 [ # # ]: 0 : if (ret == -1) {
2527 : 0 : TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2528 : : name, tap_name);
2529 [ # # ]: 0 : if (tap_devices_count_increased == 1) {
2530 [ # # ]: 0 : if (tap_devices_count == 1)
2531 : 0 : rte_mp_action_unregister(TAP_MP_KEY);
2532 : 0 : tap_devices_count--;
2533 : : }
2534 : : }
2535 : 0 : rte_kvargs_free(kvlist);
2536 : :
2537 : 0 : return ret;
2538 : : }
2539 : :
2540 : : /* detach a TUNTAP device.
2541 : : */
2542 : : static int
2543 [ # # ]: 0 : rte_pmd_tap_remove(struct rte_vdev_device *dev)
2544 : : {
2545 : : struct rte_eth_dev *eth_dev = NULL;
2546 : :
2547 : : /* find the ethdev entry */
2548 : 0 : eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2549 [ # # ]: 0 : if (!eth_dev)
2550 : : return 0;
2551 : :
2552 : 0 : tap_dev_close(eth_dev);
2553 : 0 : rte_eth_dev_release_port(eth_dev);
2554 : :
2555 : 0 : return 0;
2556 : : }
2557 : :
2558 : : static struct rte_vdev_driver pmd_tun_drv = {
2559 : : .probe = rte_pmd_tun_probe,
2560 : : .remove = rte_pmd_tap_remove,
2561 : : };
2562 : :
2563 : : static struct rte_vdev_driver pmd_tap_drv = {
2564 : : .probe = rte_pmd_tap_probe,
2565 : : .remove = rte_pmd_tap_remove,
2566 : : };
2567 : :
2568 : 252 : RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
2569 : 252 : RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv);
2570 : : RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
2571 : : RTE_PMD_REGISTER_PARAM_STRING(net_tun,
2572 : : ETH_TAP_IFACE_ARG "=<string> ");
2573 : : RTE_PMD_REGISTER_PARAM_STRING(net_tap,
2574 : : ETH_TAP_IFACE_ARG "=<string> "
2575 : : ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " "
2576 : : ETH_TAP_REMOTE_ARG "=<string>");
2577 [ - + ]: 252 : RTE_LOG_REGISTER_DEFAULT(tap_logtype, NOTICE);
|