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 : 0 : tap_intr_handle_set(dev, 0);
894 : 0 : return err;
895 : : }
896 : :
897 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
898 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
899 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
900 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
901 : :
902 : : return err;
903 : : }
904 : :
905 : : static int
906 : 0 : tap_mp_req_start_rxtx(const struct rte_mp_msg *request, __rte_unused const void *peer)
907 : : {
908 : : struct rte_eth_dev *dev;
909 : : const struct ipc_queues *request_param =
910 : : (const struct ipc_queues *)request->param;
911 : : struct pmd_process_private *process_private;
912 : :
913 : 0 : dev = rte_eth_dev_get_by_name(request_param->port_name);
914 [ # # ]: 0 : if (!dev) {
915 : 0 : TAP_LOG(ERR, "Failed to get dev for %s",
916 : : request_param->port_name);
917 : 0 : return -1;
918 : : }
919 : :
920 : 0 : process_private = dev->process_private;
921 : 0 : TAP_LOG(DEBUG, "tap_attach q:%d", request_param->q_count);
922 : :
923 [ # # ]: 0 : for (int q = 0; q < request_param->q_count; q++)
924 : 0 : process_private->fds[q] = request->fds[q];
925 : :
926 : :
927 : : return 0;
928 : : }
929 : :
930 : : /* This function gets called when the current port gets stopped.
931 : : */
932 : : static int
933 : 0 : tap_dev_stop(struct rte_eth_dev *dev)
934 : : {
935 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
936 : : int i;
937 : :
938 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
939 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
940 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
941 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
942 : :
943 : 0 : tap_intr_handle_set(dev, 0);
944 [ # # ]: 0 : if (!pmd->persist)
945 : 0 : tap_link_set_down(dev);
946 : :
947 : 0 : return 0;
948 : : }
949 : :
950 : : static int
951 : 0 : tap_dev_configure(struct rte_eth_dev *dev)
952 : : {
953 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
954 : :
955 [ # # ]: 0 : if (dev->data->nb_rx_queues != dev->data->nb_tx_queues) {
956 : 0 : TAP_LOG(ERR,
957 : : "%s: number of rx queues %d must be equal to number of tx queues %d",
958 : : dev->device->name,
959 : : dev->data->nb_rx_queues,
960 : : dev->data->nb_tx_queues);
961 : 0 : return -1;
962 : : }
963 : :
964 : 0 : TAP_LOG(INFO, "%s: %s: TX configured queues number: %u",
965 : : dev->device->name, pmd->name, dev->data->nb_tx_queues);
966 : :
967 : 0 : TAP_LOG(INFO, "%s: %s: RX configured queues number: %u",
968 : : dev->device->name, pmd->name, dev->data->nb_rx_queues);
969 : :
970 : 0 : return 0;
971 : : }
972 : :
973 : : static uint32_t
974 : 0 : tap_dev_speed_capa(void)
975 : : {
976 : 0 : uint32_t speed = pmd_link.link_speed;
977 : : uint32_t capa = 0;
978 : :
979 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_10M)
980 : : capa |= RTE_ETH_LINK_SPEED_10M;
981 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_100M)
982 : 0 : capa |= RTE_ETH_LINK_SPEED_100M;
983 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_1G)
984 : 0 : capa |= RTE_ETH_LINK_SPEED_1G;
985 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_5G)
986 : 0 : capa |= RTE_ETH_LINK_SPEED_2_5G;
987 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_5G)
988 : 0 : capa |= RTE_ETH_LINK_SPEED_5G;
989 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_10G)
990 : 0 : capa |= RTE_ETH_LINK_SPEED_10G;
991 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_20G)
992 : 0 : capa |= RTE_ETH_LINK_SPEED_20G;
993 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_25G)
994 : 0 : capa |= RTE_ETH_LINK_SPEED_25G;
995 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_40G)
996 : 0 : capa |= RTE_ETH_LINK_SPEED_40G;
997 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_50G)
998 : 0 : capa |= RTE_ETH_LINK_SPEED_50G;
999 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_56G)
1000 : 0 : capa |= RTE_ETH_LINK_SPEED_56G;
1001 [ # # ]: 0 : if (speed >= RTE_ETH_SPEED_NUM_100G)
1002 : 0 : capa |= RTE_ETH_LINK_SPEED_100G;
1003 : :
1004 : 0 : return capa;
1005 : : }
1006 : :
1007 : : static int
1008 : 0 : tap_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1009 : : {
1010 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1011 : :
1012 : 0 : dev_info->if_index = internals->if_index;
1013 : 0 : dev_info->max_mac_addrs = 1;
1014 : 0 : dev_info->max_rx_pktlen = RTE_ETHER_MAX_JUMBO_FRAME_LEN;
1015 : 0 : dev_info->max_rx_queues = RTE_PMD_TAP_MAX_QUEUES;
1016 : 0 : dev_info->max_tx_queues = RTE_PMD_TAP_MAX_QUEUES;
1017 : 0 : dev_info->min_rx_bufsize = 0;
1018 : 0 : dev_info->speed_capa = tap_dev_speed_capa();
1019 : 0 : dev_info->rx_queue_offload_capa = TAP_RX_OFFLOAD;
1020 : 0 : dev_info->rx_offload_capa = dev_info->rx_queue_offload_capa;
1021 : 0 : dev_info->tx_queue_offload_capa = TAP_TX_OFFLOAD;
1022 : 0 : dev_info->tx_offload_capa = dev_info->tx_queue_offload_capa;
1023 : 0 : dev_info->hash_key_size = TAP_RSS_HASH_KEY_SIZE;
1024 : : /*
1025 : : * limitation: TAP supports all of IP, UDP and TCP hash
1026 : : * functions together and not in partial combinations
1027 : : */
1028 : 0 : dev_info->flow_type_rss_offloads = ~TAP_RSS_HF_MASK;
1029 : 0 : dev_info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
1030 : :
1031 : 0 : return 0;
1032 : : }
1033 : :
1034 : : static int
1035 : 0 : tap_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *tap_stats,
1036 : : struct eth_queue_stats *qstats)
1037 : : {
1038 : : unsigned int i, imax;
1039 : : unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
1040 : : unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
1041 : : unsigned long rx_nombuf = 0, ierrors = 0;
1042 : 0 : const struct pmd_internals *pmd = dev->data->dev_private;
1043 : :
1044 : : /* rx queue statistics */
1045 : 0 : imax = (dev->data->nb_rx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1046 : 0 : dev->data->nb_rx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1047 [ # # ]: 0 : for (i = 0; i < imax; i++) {
1048 [ # # ]: 0 : if (qstats != NULL) {
1049 : 0 : qstats->q_ipackets[i] = pmd->rxq[i].stats.ipackets;
1050 : 0 : qstats->q_ibytes[i] = pmd->rxq[i].stats.ibytes;
1051 : : }
1052 : 0 : rx_total += pmd->rxq[i].stats.ipackets;
1053 : 0 : rx_bytes_total += pmd->rxq[i].stats.ibytes;
1054 : 0 : rx_nombuf += pmd->rxq[i].stats.rx_nombuf;
1055 : 0 : ierrors += pmd->rxq[i].stats.ierrors;
1056 : : }
1057 : :
1058 : : /* tx queue statistics */
1059 : 0 : imax = (dev->data->nb_tx_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS) ?
1060 : 0 : dev->data->nb_tx_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS;
1061 : :
1062 [ # # ]: 0 : for (i = 0; i < imax; i++) {
1063 [ # # ]: 0 : if (qstats != NULL) {
1064 : 0 : qstats->q_opackets[i] = pmd->txq[i].stats.opackets;
1065 : 0 : qstats->q_obytes[i] = pmd->txq[i].stats.obytes;
1066 : : }
1067 : 0 : tx_total += pmd->txq[i].stats.opackets;
1068 : 0 : tx_bytes_total += pmd->txq[i].stats.obytes;
1069 : 0 : tx_err_total += pmd->txq[i].stats.errs;
1070 : : }
1071 : :
1072 : 0 : tap_stats->ipackets = rx_total;
1073 : 0 : tap_stats->ibytes = rx_bytes_total;
1074 : 0 : tap_stats->ierrors = ierrors;
1075 : 0 : tap_stats->rx_nombuf = rx_nombuf;
1076 : 0 : tap_stats->opackets = tx_total;
1077 : 0 : tap_stats->oerrors = tx_err_total;
1078 : 0 : tap_stats->obytes = tx_bytes_total;
1079 : 0 : return 0;
1080 : : }
1081 : :
1082 : : static int
1083 : 0 : tap_stats_reset(struct rte_eth_dev *dev)
1084 : : {
1085 : : int i;
1086 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1087 : :
1088 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1089 : 0 : pmd->rxq[i].stats.ipackets = 0;
1090 : 0 : pmd->rxq[i].stats.ibytes = 0;
1091 : 0 : pmd->rxq[i].stats.ierrors = 0;
1092 : 0 : pmd->rxq[i].stats.rx_nombuf = 0;
1093 : :
1094 : 0 : pmd->txq[i].stats.opackets = 0;
1095 : 0 : pmd->txq[i].stats.errs = 0;
1096 : 0 : pmd->txq[i].stats.obytes = 0;
1097 : : }
1098 : :
1099 : 0 : return 0;
1100 : : }
1101 : :
1102 : : static void
1103 : : tap_queue_close(struct pmd_process_private *process_private, uint16_t qid)
1104 : : {
1105 [ # # ]: 0 : if (process_private->fds[qid] != -1) {
1106 : 0 : close(process_private->fds[qid]);
1107 : 0 : process_private->fds[qid] = -1;
1108 : : }
1109 : : }
1110 : :
1111 : : static int
1112 : 0 : tap_dev_close(struct rte_eth_dev *dev)
1113 : : {
1114 : : int i;
1115 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1116 : 0 : struct pmd_process_private *process_private = dev->process_private;
1117 : :
1118 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1119 : 0 : free(dev->process_private);
1120 : 0 : dev->process_private = NULL;
1121 [ # # ]: 0 : if (tap_devices_count == 1)
1122 : 0 : rte_mp_action_unregister(TAP_MP_REQ_START_RXTX);
1123 : 0 : tap_devices_count--;
1124 : 0 : return 0;
1125 : : }
1126 : :
1127 [ # # ]: 0 : if (!internals->persist)
1128 : 0 : tap_link_set_down(dev);
1129 : :
1130 : : #ifdef HAVE_TCA_FLOWER
1131 [ # # ]: 0 : if (internals->nlsk_fd != -1) {
1132 : 0 : tap_flow_flush(dev, NULL);
1133 : 0 : tap_flow_implicit_flush(internals, NULL);
1134 : 0 : tap_nl_final(internals->nlsk_fd);
1135 : 0 : internals->nlsk_fd = -1;
1136 : 0 : tap_flow_bpf_destroy(internals);
1137 : : }
1138 : : #endif
1139 : :
1140 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++) {
1141 : : struct rx_queue *rxq = &internals->rxq[i];
1142 : :
1143 : : tap_queue_close(process_private, i);
1144 : :
1145 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1146 : 0 : rte_free(rxq->iovecs);
1147 : 0 : rxq->pool = NULL;
1148 : 0 : rxq->iovecs = NULL;
1149 : : }
1150 : :
1151 [ # # ]: 0 : if (internals->remote_if_index) {
1152 : : /* Restore initial remote state */
1153 : 0 : int ret = ioctl(internals->ioctl_sock, SIOCSIFFLAGS,
1154 : : &internals->remote_initial_flags);
1155 [ # # ]: 0 : if (ret)
1156 : 0 : TAP_LOG(ERR, "restore remote state failed: %d", ret);
1157 : :
1158 : : }
1159 : :
1160 : 0 : rte_mempool_free(internals->gso_ctx_mp);
1161 : 0 : internals->gso_ctx_mp = NULL;
1162 : :
1163 [ # # ]: 0 : if (internals->ka_fd != -1) {
1164 : 0 : close(internals->ka_fd);
1165 : 0 : internals->ka_fd = -1;
1166 : : }
1167 : :
1168 : : /* mac_addrs must not be freed alone because part of dev_private */
1169 : 0 : dev->data->mac_addrs = NULL;
1170 : :
1171 : 0 : internals = dev->data->dev_private;
1172 : 0 : TAP_LOG(DEBUG, "Closing %s Ethernet device on numa %u",
1173 : : tuntap_types[internals->type], rte_socket_id());
1174 : :
1175 : 0 : rte_intr_instance_free(internals->intr_handle);
1176 : :
1177 [ # # ]: 0 : if (internals->ioctl_sock != -1) {
1178 : 0 : close(internals->ioctl_sock);
1179 : 0 : internals->ioctl_sock = -1;
1180 : : }
1181 : 0 : free(dev->process_private);
1182 : 0 : dev->process_private = NULL;
1183 : :
1184 [ # # ]: 0 : if (tap_devices_count == 1)
1185 : 0 : rte_mp_action_unregister(TAP_MP_KEY);
1186 : 0 : tap_devices_count--;
1187 : : /*
1188 : : * Since TUN device has no more opened file descriptors
1189 : : * it will be removed from kernel
1190 : : */
1191 : :
1192 : 0 : return 0;
1193 : : }
1194 : :
1195 : : static void
1196 : 0 : tap_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1197 : : {
1198 : 0 : struct rx_queue *rxq = dev->data->rx_queues[qid];
1199 : : struct pmd_process_private *process_private;
1200 : :
1201 [ # # ]: 0 : if (!rxq)
1202 : : return;
1203 : :
1204 : 0 : process_private = rte_eth_devices[rxq->in_port].process_private;
1205 : :
1206 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1207 : 0 : rte_free(rxq->iovecs);
1208 : 0 : rxq->pool = NULL;
1209 : 0 : rxq->iovecs = NULL;
1210 : :
1211 [ # # ]: 0 : if (dev->data->tx_queues[qid] == NULL)
1212 [ # # ]: 0 : tap_queue_close(process_private, qid);
1213 : : }
1214 : :
1215 : : static void
1216 : 0 : tap_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1217 : : {
1218 : 0 : struct tx_queue *txq = dev->data->tx_queues[qid];
1219 : : struct pmd_process_private *process_private;
1220 : :
1221 [ # # ]: 0 : if (!txq)
1222 : : return;
1223 : :
1224 : 0 : process_private = rte_eth_devices[txq->out_port].process_private;
1225 [ # # ]: 0 : if (dev->data->rx_queues[qid] == NULL)
1226 [ # # ]: 0 : tap_queue_close(process_private, qid);
1227 : : }
1228 : :
1229 : : static int
1230 : 0 : tap_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
1231 : : {
1232 : 0 : struct rte_eth_link *dev_link = &dev->data->dev_link;
1233 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1234 : 0 : struct ifreq ifr = { .ifr_flags = 0 };
1235 : :
1236 [ # # ]: 0 : if (pmd->remote_if_index) {
1237 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
1238 [ # # ]: 0 : if (!(ifr.ifr_flags & IFF_UP) ||
1239 : : !(ifr.ifr_flags & IFF_RUNNING)) {
1240 : 0 : dev_link->link_status = RTE_ETH_LINK_DOWN;
1241 : 0 : return 0;
1242 : : }
1243 : : }
1244 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
1245 : 0 : dev_link->link_status =
1246 : 0 : ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
1247 : 0 : RTE_ETH_LINK_UP :
1248 : : RTE_ETH_LINK_DOWN);
1249 : 0 : return 0;
1250 : : }
1251 : :
1252 : : static int
1253 : 0 : tap_promisc_enable(struct rte_eth_dev *dev)
1254 : : {
1255 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1256 : 0 : struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1257 : : int ret;
1258 : :
1259 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1260 [ # # ]: 0 : if (ret != 0)
1261 : : return ret;
1262 : :
1263 : : #ifdef HAVE_TCA_FLOWER
1264 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1265 : 0 : dev->data->promiscuous = 1;
1266 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
1267 [ # # ]: 0 : if (ret != 0) {
1268 : : /* Rollback promisc flag */
1269 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1270 : : /*
1271 : : * rte_eth_dev_promiscuous_enable() rollback
1272 : : * dev->data->promiscuous in the case of failure.
1273 : : */
1274 : 0 : return ret;
1275 : : }
1276 : : }
1277 : : #endif
1278 : : return 0;
1279 : : }
1280 : :
1281 : : static int
1282 : 0 : tap_promisc_disable(struct rte_eth_dev *dev)
1283 : : {
1284 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1285 : 0 : struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
1286 : : int ret;
1287 : :
1288 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1289 [ # # ]: 0 : if (ret != 0)
1290 : : return ret;
1291 : :
1292 : : #ifdef HAVE_TCA_FLOWER
1293 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1294 : 0 : dev->data->promiscuous = 0;
1295 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
1296 [ # # ]: 0 : if (ret != 0) {
1297 : : /* Rollback promisc flag */
1298 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1299 : : /*
1300 : : * rte_eth_dev_promiscuous_disable() rollback
1301 : : * dev->data->promiscuous in the case of failure.
1302 : : */
1303 : 0 : return ret;
1304 : : }
1305 : : }
1306 : : #endif
1307 : :
1308 : : return 0;
1309 : : }
1310 : :
1311 : : static int
1312 : 0 : tap_allmulti_enable(struct rte_eth_dev *dev)
1313 : : {
1314 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1315 : 0 : struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1316 : : int ret;
1317 : :
1318 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1319 [ # # ]: 0 : if (ret != 0)
1320 : : return ret;
1321 : :
1322 : : #ifdef HAVE_TCA_FLOWER
1323 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1324 : 0 : dev->data->all_multicast = 1;
1325 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
1326 [ # # ]: 0 : if (ret != 0) {
1327 : : /* Rollback allmulti flag */
1328 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1329 : : /*
1330 : : * rte_eth_dev_allmulticast_enable() rollback
1331 : : * dev->data->all_multicast in the case of failure.
1332 : : */
1333 : 0 : return ret;
1334 : : }
1335 : : }
1336 : : #endif
1337 : :
1338 : : return 0;
1339 : : }
1340 : :
1341 : : static int
1342 : 0 : tap_allmulti_disable(struct rte_eth_dev *dev)
1343 : : {
1344 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1345 : 0 : struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
1346 : : int ret;
1347 : :
1348 : 0 : ret = tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
1349 [ # # ]: 0 : if (ret != 0)
1350 : : return ret;
1351 : :
1352 : : #ifdef HAVE_TCA_FLOWER
1353 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1354 : 0 : dev->data->all_multicast = 0;
1355 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
1356 [ # # ]: 0 : if (ret != 0) {
1357 : : /* Rollback allmulti flag */
1358 : 0 : tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
1359 : : /*
1360 : : * rte_eth_dev_allmulticast_disable() rollback
1361 : : * dev->data->all_multicast in the case of failure.
1362 : : */
1363 : 0 : return ret;
1364 : : }
1365 : : }
1366 : : #endif
1367 : :
1368 : : return 0;
1369 : : }
1370 : :
1371 : : static int
1372 : 0 : tap_mac_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1373 : : {
1374 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1375 : : enum ioctl_mode mode = LOCAL_ONLY;
1376 : : struct ifreq ifr;
1377 : : int ret;
1378 : :
1379 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TUN) {
1380 : 0 : TAP_LOG(ERR, "%s: can't MAC address for TUN",
1381 : : dev->device->name);
1382 : 0 : return -ENOTSUP;
1383 : : }
1384 : :
1385 [ # # ]: 0 : if (rte_is_zero_ether_addr(mac_addr)) {
1386 : 0 : TAP_LOG(ERR, "%s: can't set an empty MAC address",
1387 : : dev->device->name);
1388 : 0 : return -EINVAL;
1389 : : }
1390 : : /* Check the actual current MAC address on the tap netdevice */
1391 : 0 : ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY);
1392 [ # # ]: 0 : if (ret < 0)
1393 : : return ret;
1394 [ # # ]: 0 : if (rte_is_same_ether_addr(
1395 : : (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1396 : : mac_addr))
1397 : : return 0;
1398 : : /* Check the current MAC address on the remote */
1399 : 0 : ret = tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY);
1400 [ # # ]: 0 : if (ret < 0)
1401 : : return ret;
1402 [ # # ]: 0 : if (!rte_is_same_ether_addr(
1403 : : (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data,
1404 : : mac_addr))
1405 : : mode = LOCAL_AND_REMOTE;
1406 : 0 : ifr.ifr_hwaddr.sa_family = AF_LOCAL;
1407 : :
1408 : : rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data);
1409 : 0 : ret = tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, mode);
1410 [ # # ]: 0 : if (ret < 0)
1411 : : return ret;
1412 : :
1413 : : rte_ether_addr_copy(mac_addr, &pmd->eth_addr);
1414 : :
1415 : : #ifdef HAVE_TCA_FLOWER
1416 [ # # # # ]: 0 : if (pmd->remote_if_index && !pmd->flow_isolate) {
1417 : : /* Replace MAC redirection rule after a MAC change */
1418 : 0 : ret = tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC);
1419 [ # # ]: 0 : if (ret < 0) {
1420 : 0 : TAP_LOG(ERR,
1421 : : "%s: Couldn't delete MAC redirection rule",
1422 : : dev->device->name);
1423 : 0 : return ret;
1424 : : }
1425 : 0 : ret = tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC);
1426 [ # # ]: 0 : if (ret < 0) {
1427 : 0 : TAP_LOG(ERR,
1428 : : "%s: Couldn't add MAC redirection rule",
1429 : : dev->device->name);
1430 : 0 : return ret;
1431 : : }
1432 : : }
1433 : : #endif
1434 : :
1435 : : return 0;
1436 : : }
1437 : :
1438 : : static int
1439 : 0 : tap_gso_ctx_setup(struct rte_gso_ctx *gso_ctx, struct rte_eth_dev *dev)
1440 : : {
1441 : : uint32_t gso_types;
1442 : : char pool_name[64];
1443 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1444 : : int ret;
1445 : :
1446 : : /* initialize GSO context */
1447 : : gso_types = RTE_ETH_TX_OFFLOAD_TCP_TSO;
1448 [ # # ]: 0 : if (!pmd->gso_ctx_mp) {
1449 : : /*
1450 : : * Create private mbuf pool with TAP_GSO_MBUF_SEG_SIZE
1451 : : * bytes size per mbuf use this pool for both direct and
1452 : : * indirect mbufs
1453 : : */
1454 : 0 : ret = snprintf(pool_name, sizeof(pool_name), "mp_%s",
1455 [ # # ]: 0 : dev->device->name);
1456 [ # # ]: 0 : if (ret < 0 || ret >= (int)sizeof(pool_name)) {
1457 : 0 : TAP_LOG(ERR,
1458 : : "%s: failed to create mbuf pool name for device %s,"
1459 : : "device name too long or output error, ret: %d",
1460 : : pmd->name, dev->device->name, ret);
1461 : 0 : return -ENAMETOOLONG;
1462 : : }
1463 : 0 : pmd->gso_ctx_mp = rte_pktmbuf_pool_create(pool_name,
1464 : : TAP_GSO_MBUFS_NUM, TAP_GSO_MBUF_CACHE_SIZE, 0,
1465 : : RTE_PKTMBUF_HEADROOM + TAP_GSO_MBUF_SEG_SIZE,
1466 : : SOCKET_ID_ANY);
1467 [ # # ]: 0 : if (!pmd->gso_ctx_mp) {
1468 : 0 : TAP_LOG(ERR,
1469 : : "%s: failed to create mbuf pool for device %s",
1470 : : pmd->name, dev->device->name);
1471 : 0 : return -1;
1472 : : }
1473 : : }
1474 : :
1475 : 0 : gso_ctx->direct_pool = pmd->gso_ctx_mp;
1476 : 0 : gso_ctx->indirect_pool = pmd->gso_ctx_mp;
1477 : 0 : gso_ctx->gso_types = gso_types;
1478 : 0 : gso_ctx->gso_size = 0; /* gso_size is set in tx_burst() per packet */
1479 : 0 : gso_ctx->flag = 0;
1480 : :
1481 : 0 : return 0;
1482 : : }
1483 : :
1484 : : static int
1485 : 0 : tap_setup_queue(struct rte_eth_dev *dev,
1486 : : struct pmd_internals *internals,
1487 : : uint16_t qid,
1488 : : int is_rx)
1489 : : {
1490 : : int fd, ret;
1491 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1492 : 0 : struct pmd_process_private *process_private = dev->process_private;
1493 : 0 : struct rx_queue *rx = &internals->rxq[qid];
1494 : : struct tx_queue *tx = &internals->txq[qid];
1495 [ # # ]: 0 : struct rte_gso_ctx *gso_ctx = is_rx ? NULL : &tx->gso_ctx;
1496 [ # # ]: 0 : const char *dir = is_rx ? "rx" : "tx";
1497 : :
1498 : 0 : fd = process_private->fds[qid];
1499 [ # # ]: 0 : if (fd != -1) {
1500 : : /* fd for this queue already exists */
1501 : 0 : TAP_LOG(DEBUG, "%s: fd %d for %s queue qid %d exists",
1502 : : pmd->name, fd, dir, qid);
1503 : : gso_ctx = NULL;
1504 : : } else {
1505 : 0 : fd = tun_alloc(pmd, 0, 0);
1506 [ # # ]: 0 : if (fd < 0) {
1507 : 0 : TAP_LOG(ERR, "%s: tun_alloc() failed.", pmd->name);
1508 : 0 : return -1;
1509 : : }
1510 : :
1511 : 0 : TAP_LOG(DEBUG, "%s: add %s queue for qid %d fd %d",
1512 : : pmd->name, dir, qid, fd);
1513 : :
1514 : 0 : process_private->fds[qid] = fd;
1515 : : }
1516 : :
1517 : 0 : tx->mtu = &dev->data->mtu;
1518 : 0 : rx->rxmode = &dev->data->dev_conf.rxmode;
1519 [ # # ]: 0 : if (gso_ctx) {
1520 : 0 : ret = tap_gso_ctx_setup(gso_ctx, dev);
1521 [ # # ]: 0 : if (ret)
1522 : : return -1;
1523 : : }
1524 : :
1525 : 0 : tx->type = pmd->type;
1526 : :
1527 : 0 : return fd;
1528 : : }
1529 : :
1530 : : static int
1531 : 0 : tap_rx_queue_setup(struct rte_eth_dev *dev,
1532 : : uint16_t rx_queue_id,
1533 : : uint16_t nb_rx_desc,
1534 : : unsigned int socket_id,
1535 : : const struct rte_eth_rxconf *rx_conf __rte_unused,
1536 : : struct rte_mempool *mp)
1537 : : {
1538 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1539 : 0 : struct pmd_process_private *process_private = dev->process_private;
1540 : 0 : struct rx_queue *rxq = &internals->rxq[rx_queue_id];
1541 : 0 : struct rte_mbuf **tmp = &rxq->pool;
1542 : 0 : long iov_max = sysconf(_SC_IOV_MAX);
1543 : :
1544 [ # # ]: 0 : if (iov_max <= 0) {
1545 : 0 : TAP_LOG(WARNING,
1546 : : "_SC_IOV_MAX is not defined. Using %d as default",
1547 : : TAP_IOV_DEFAULT_MAX);
1548 : : iov_max = TAP_IOV_DEFAULT_MAX;
1549 : : }
1550 : 0 : uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
1551 : 0 : struct iovec (*iovecs)[nb_desc + 1];
1552 : : int data_off = RTE_PKTMBUF_HEADROOM;
1553 : : int ret = 0;
1554 : : int fd;
1555 : : int i;
1556 : :
1557 [ # # # # ]: 0 : if (rx_queue_id >= dev->data->nb_rx_queues || !mp) {
1558 : 0 : TAP_LOG(WARNING,
1559 : : "nb_rx_queues %d too small or mempool NULL",
1560 : : dev->data->nb_rx_queues);
1561 : 0 : return -1;
1562 : : }
1563 : :
1564 : 0 : rxq->mp = mp;
1565 : 0 : rxq->trigger_seen = 1; /* force initial burst */
1566 : 0 : rxq->in_port = dev->data->port_id;
1567 : 0 : rxq->queue_id = rx_queue_id;
1568 : 0 : rxq->nb_rx_desc = nb_desc;
1569 : 0 : iovecs = rte_zmalloc_socket(dev->device->name, sizeof(*iovecs), 0,
1570 : : socket_id);
1571 [ # # ]: 0 : if (!iovecs) {
1572 : 0 : TAP_LOG(WARNING,
1573 : : "%s: Couldn't allocate %d RX descriptors",
1574 : : dev->device->name, nb_desc);
1575 : 0 : return -ENOMEM;
1576 : : }
1577 : 0 : rxq->iovecs = iovecs;
1578 : :
1579 : 0 : dev->data->rx_queues[rx_queue_id] = rxq;
1580 : 0 : fd = tap_setup_queue(dev, internals, rx_queue_id, 1);
1581 [ # # ]: 0 : if (fd == -1) {
1582 : : ret = fd;
1583 : 0 : goto error;
1584 : : }
1585 : :
1586 : 0 : (*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
1587 : 0 : (*rxq->iovecs)[0].iov_base = &rxq->pi;
1588 : :
1589 [ # # ]: 0 : for (i = 1; i <= nb_desc; i++) {
1590 : 0 : *tmp = rte_pktmbuf_alloc(rxq->mp);
1591 [ # # ]: 0 : if (!*tmp) {
1592 : 0 : TAP_LOG(WARNING,
1593 : : "%s: couldn't allocate memory for queue %d",
1594 : : dev->device->name, rx_queue_id);
1595 : : ret = -ENOMEM;
1596 : 0 : goto error;
1597 : : }
1598 : 0 : (*rxq->iovecs)[i].iov_len = (*tmp)->buf_len - data_off;
1599 : 0 : (*rxq->iovecs)[i].iov_base =
1600 : 0 : (char *)(*tmp)->buf_addr + data_off;
1601 : : data_off = 0;
1602 : 0 : tmp = &(*tmp)->next;
1603 : : }
1604 : :
1605 : 0 : TAP_LOG(DEBUG, " RX TUNTAP device name %s, qid %d on fd %d",
1606 : : internals->name, rx_queue_id,
1607 : : process_private->fds[rx_queue_id]);
1608 : :
1609 : 0 : return 0;
1610 : :
1611 : 0 : error:
1612 [ # # ]: 0 : tap_rxq_pool_free(rxq->pool);
1613 : 0 : rxq->pool = NULL;
1614 : 0 : rte_free(rxq->iovecs);
1615 : 0 : rxq->iovecs = NULL;
1616 : 0 : return ret;
1617 : : }
1618 : :
1619 : : static int
1620 : 0 : tap_tx_queue_setup(struct rte_eth_dev *dev,
1621 : : uint16_t tx_queue_id,
1622 : : uint16_t nb_tx_desc __rte_unused,
1623 : : unsigned int socket_id __rte_unused,
1624 : : const struct rte_eth_txconf *tx_conf)
1625 : : {
1626 : 0 : struct pmd_internals *internals = dev->data->dev_private;
1627 : 0 : struct pmd_process_private *process_private = dev->process_private;
1628 : : struct tx_queue *txq;
1629 : : int ret;
1630 : : uint64_t offloads;
1631 : :
1632 [ # # ]: 0 : if (tx_queue_id >= dev->data->nb_tx_queues)
1633 : : return -1;
1634 : 0 : dev->data->tx_queues[tx_queue_id] = &internals->txq[tx_queue_id];
1635 : 0 : txq = dev->data->tx_queues[tx_queue_id];
1636 : 0 : txq->out_port = dev->data->port_id;
1637 : 0 : txq->queue_id = tx_queue_id;
1638 : :
1639 : 0 : offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1640 : 0 : txq->csum = !!(offloads &
1641 : : (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1642 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1643 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM));
1644 : :
1645 : 0 : ret = tap_setup_queue(dev, internals, tx_queue_id, 0);
1646 [ # # ]: 0 : if (ret == -1)
1647 : : return -1;
1648 [ # # ]: 0 : TAP_LOG(DEBUG,
1649 : : " TX TUNTAP device name %s, qid %d on fd %d csum %s",
1650 : : internals->name, tx_queue_id,
1651 : : process_private->fds[tx_queue_id],
1652 : : txq->csum ? "on" : "off");
1653 : :
1654 : 0 : return 0;
1655 : : }
1656 : :
1657 : : static int
1658 : 0 : tap_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1659 : : {
1660 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1661 : 0 : struct ifreq ifr = { .ifr_mtu = mtu };
1662 : :
1663 : 0 : return tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
1664 : : }
1665 : :
1666 : : static int
1667 : 0 : tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
1668 : : struct rte_ether_addr *mc_addr_set __rte_unused,
1669 : : uint32_t nb_mc_addr __rte_unused)
1670 : : {
1671 : : /*
1672 : : * Nothing to do actually: the tap has no filtering whatsoever, every
1673 : : * packet is received.
1674 : : */
1675 : 0 : return 0;
1676 : : }
1677 : :
1678 : : static int
1679 : 0 : tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
1680 : : {
1681 : : struct rte_eth_dev *dev = arg;
1682 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1683 : : struct ifinfomsg *info = NLMSG_DATA(nh);
1684 : :
1685 [ # # ]: 0 : if (nh->nlmsg_type != RTM_NEWLINK ||
1686 [ # # ]: 0 : (info->ifi_index != pmd->if_index &&
1687 [ # # ]: 0 : info->ifi_index != pmd->remote_if_index))
1688 : : return 0;
1689 : 0 : return tap_link_update(dev, 0);
1690 : : }
1691 : :
1692 : : static void
1693 : 0 : tap_dev_intr_handler(void *cb_arg)
1694 : : {
1695 : : struct rte_eth_dev *dev = cb_arg;
1696 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1697 : :
1698 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) >= 0)
1699 : 0 : tap_nl_recv(rte_intr_fd_get(pmd->intr_handle),
1700 : : tap_nl_msg_handler, dev);
1701 : 0 : }
1702 : :
1703 : : static int
1704 : 0 : tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
1705 : : {
1706 : 0 : struct pmd_internals *pmd = dev->data->dev_private;
1707 : : int ret;
1708 : :
1709 : : /* In any case, disable interrupt if the conf is no longer there. */
1710 [ # # ]: 0 : if (!dev->data->dev_conf.intr_conf.lsc) {
1711 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) != -1)
1712 : 0 : goto clean;
1713 : :
1714 : : return 0;
1715 : : }
1716 [ # # ]: 0 : if (set) {
1717 : 0 : rte_intr_fd_set(pmd->intr_handle, tap_nl_init(RTMGRP_LINK));
1718 [ # # ]: 0 : if (unlikely(rte_intr_fd_get(pmd->intr_handle) == -1))
1719 : : return -EBADF;
1720 : 0 : return rte_intr_callback_register(
1721 : 0 : pmd->intr_handle, tap_dev_intr_handler, dev);
1722 : : }
1723 : :
1724 : 0 : clean:
1725 : : do {
1726 : 0 : ret = rte_intr_callback_unregister(pmd->intr_handle,
1727 : : tap_dev_intr_handler, dev);
1728 [ # # ]: 0 : if (ret >= 0) {
1729 : : break;
1730 [ # # ]: 0 : } else if (ret == -EAGAIN) {
1731 : : rte_delay_ms(100);
1732 : : } else {
1733 : 0 : TAP_LOG(ERR, "intr callback unregister failed: %d",
1734 : : ret);
1735 : 0 : break;
1736 : : }
1737 : : } while (true);
1738 : :
1739 [ # # ]: 0 : if (rte_intr_fd_get(pmd->intr_handle) >= 0) {
1740 : 0 : tap_nl_final(rte_intr_fd_get(pmd->intr_handle));
1741 : 0 : rte_intr_fd_set(pmd->intr_handle, -1);
1742 : : }
1743 : :
1744 : : return 0;
1745 : : }
1746 : :
1747 : : static int
1748 : 0 : tap_intr_handle_set(struct rte_eth_dev *dev, int set)
1749 : : {
1750 : : int err;
1751 : :
1752 : 0 : err = tap_lsc_intr_handle_set(dev, set);
1753 [ # # ]: 0 : if (err < 0) {
1754 [ # # ]: 0 : if (!set)
1755 : 0 : tap_rx_intr_vec_set(dev, 0);
1756 : 0 : return err;
1757 : : }
1758 : 0 : err = tap_rx_intr_vec_set(dev, set);
1759 [ # # ]: 0 : if (err && set)
1760 : 0 : tap_lsc_intr_handle_set(dev, 0);
1761 : : return err;
1762 : : }
1763 : :
1764 : : static const uint32_t*
1765 : 0 : tap_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused,
1766 : : size_t *no_of_elements)
1767 : : {
1768 : : static const uint32_t ptypes[] = {
1769 : : RTE_PTYPE_INNER_L2_ETHER,
1770 : : RTE_PTYPE_INNER_L2_ETHER_VLAN,
1771 : : RTE_PTYPE_INNER_L2_ETHER_QINQ,
1772 : : RTE_PTYPE_INNER_L3_IPV4,
1773 : : RTE_PTYPE_INNER_L3_IPV4_EXT,
1774 : : RTE_PTYPE_INNER_L3_IPV6,
1775 : : RTE_PTYPE_INNER_L3_IPV6_EXT,
1776 : : RTE_PTYPE_INNER_L4_FRAG,
1777 : : RTE_PTYPE_INNER_L4_UDP,
1778 : : RTE_PTYPE_INNER_L4_TCP,
1779 : : RTE_PTYPE_INNER_L4_SCTP,
1780 : : RTE_PTYPE_L2_ETHER,
1781 : : RTE_PTYPE_L2_ETHER_VLAN,
1782 : : RTE_PTYPE_L2_ETHER_QINQ,
1783 : : RTE_PTYPE_L3_IPV4,
1784 : : RTE_PTYPE_L3_IPV4_EXT,
1785 : : RTE_PTYPE_L3_IPV6_EXT,
1786 : : RTE_PTYPE_L3_IPV6,
1787 : : RTE_PTYPE_L4_FRAG,
1788 : : RTE_PTYPE_L4_UDP,
1789 : : RTE_PTYPE_L4_TCP,
1790 : : RTE_PTYPE_L4_SCTP,
1791 : : };
1792 : :
1793 : 0 : *no_of_elements = RTE_DIM(ptypes);
1794 : 0 : return ptypes;
1795 : : }
1796 : :
1797 : : static int
1798 : 0 : tap_flow_ctrl_get(struct rte_eth_dev *dev __rte_unused,
1799 : : struct rte_eth_fc_conf *fc_conf)
1800 : : {
1801 : 0 : fc_conf->mode = RTE_ETH_FC_NONE;
1802 : 0 : return 0;
1803 : : }
1804 : :
1805 : : static int
1806 : 0 : tap_flow_ctrl_set(struct rte_eth_dev *dev __rte_unused,
1807 : : struct rte_eth_fc_conf *fc_conf)
1808 : : {
1809 [ # # ]: 0 : if (fc_conf->mode != RTE_ETH_FC_NONE)
1810 : 0 : return -ENOTSUP;
1811 : : return 0;
1812 : : }
1813 : :
1814 : : /**
1815 : : * DPDK callback to update the RSS hash configuration.
1816 : : *
1817 : : * @param dev
1818 : : * Pointer to Ethernet device structure.
1819 : : * @param[in] rss_conf
1820 : : * RSS configuration data.
1821 : : *
1822 : : * @return
1823 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1824 : : */
1825 : : static int
1826 : 0 : tap_rss_hash_update(struct rte_eth_dev *dev,
1827 : : struct rte_eth_rss_conf *rss_conf)
1828 : : {
1829 [ # # ]: 0 : if (rss_conf->rss_hf & TAP_RSS_HF_MASK) {
1830 : 0 : rte_errno = EINVAL;
1831 : 0 : return -rte_errno;
1832 : : }
1833 [ # # # # ]: 0 : if (rss_conf->rss_key && rss_conf->rss_key_len) {
1834 : : /*
1835 : : * Currently TAP RSS key is hard coded
1836 : : * and cannot be updated
1837 : : */
1838 : 0 : TAP_LOG(ERR,
1839 : : "port %u RSS key cannot be updated",
1840 : : dev->data->port_id);
1841 : 0 : rte_errno = EINVAL;
1842 : 0 : return -rte_errno;
1843 : : }
1844 : : return 0;
1845 : : }
1846 : :
1847 : : static int
1848 : 0 : tap_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1849 : : {
1850 : 0 : dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1851 : :
1852 : 0 : return 0;
1853 : : }
1854 : :
1855 : : static int
1856 : 0 : tap_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1857 : : {
1858 : 0 : dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1859 : :
1860 : 0 : return 0;
1861 : : }
1862 : :
1863 : : static int
1864 : 0 : tap_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1865 : : {
1866 : 0 : dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1867 : :
1868 : 0 : return 0;
1869 : : }
1870 : :
1871 : : static int
1872 : 0 : tap_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1873 : : {
1874 : 0 : dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1875 : :
1876 : 0 : return 0;
1877 : : }
1878 : : static const struct eth_dev_ops ops = {
1879 : : .dev_start = tap_dev_start,
1880 : : .dev_stop = tap_dev_stop,
1881 : : .dev_close = tap_dev_close,
1882 : : .dev_configure = tap_dev_configure,
1883 : : .dev_infos_get = tap_dev_info,
1884 : : .rx_queue_setup = tap_rx_queue_setup,
1885 : : .tx_queue_setup = tap_tx_queue_setup,
1886 : : .rx_queue_start = tap_rx_queue_start,
1887 : : .tx_queue_start = tap_tx_queue_start,
1888 : : .rx_queue_stop = tap_rx_queue_stop,
1889 : : .tx_queue_stop = tap_tx_queue_stop,
1890 : : .rx_queue_release = tap_rx_queue_release,
1891 : : .tx_queue_release = tap_tx_queue_release,
1892 : : .flow_ctrl_get = tap_flow_ctrl_get,
1893 : : .flow_ctrl_set = tap_flow_ctrl_set,
1894 : : .link_update = tap_link_update,
1895 : : .dev_set_link_up = tap_link_set_up,
1896 : : .dev_set_link_down = tap_link_set_down,
1897 : : .promiscuous_enable = tap_promisc_enable,
1898 : : .promiscuous_disable = tap_promisc_disable,
1899 : : .allmulticast_enable = tap_allmulti_enable,
1900 : : .allmulticast_disable = tap_allmulti_disable,
1901 : : .mac_addr_set = tap_mac_set,
1902 : : .mtu_set = tap_mtu_set,
1903 : : .set_mc_addr_list = tap_set_mc_addr_list,
1904 : : .stats_get = tap_stats_get,
1905 : : .stats_reset = tap_stats_reset,
1906 : : .dev_supported_ptypes_get = tap_dev_supported_ptypes_get,
1907 : : .rss_hash_update = tap_rss_hash_update,
1908 : : #ifdef HAVE_TCA_FLOWER
1909 : : .flow_ops_get = tap_dev_flow_ops_get,
1910 : : #endif
1911 : : };
1912 : :
1913 : : static int
1914 : 0 : eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name,
1915 : : char *remote_iface, struct rte_ether_addr *mac_addr,
1916 : : enum rte_tuntap_type type, int persist)
1917 : : {
1918 : 0 : int numa_node = rte_socket_id();
1919 : : struct rte_eth_dev *dev;
1920 : : struct pmd_internals *pmd;
1921 : : struct pmd_process_private *process_private;
1922 : 0 : const char *tuntap_name = tuntap_types[type];
1923 : : struct rte_eth_dev_data *data;
1924 : : struct ifreq ifr;
1925 : : int i;
1926 : :
1927 : 0 : TAP_LOG(DEBUG, "%s device on numa %u", tuntap_name, rte_socket_id());
1928 : :
1929 : 0 : dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
1930 [ # # ]: 0 : if (!dev) {
1931 : 0 : TAP_LOG(ERR, "%s Unable to allocate device struct",
1932 : : tuntap_name);
1933 : 0 : goto error_exit_nodev;
1934 : : }
1935 : :
1936 : 0 : process_private = malloc(sizeof(struct pmd_process_private));
1937 [ # # ]: 0 : if (process_private == NULL) {
1938 : 0 : TAP_LOG(ERR, "Failed to alloc memory for process private");
1939 : 0 : return -1;
1940 : : }
1941 : : memset(process_private, 0, sizeof(struct pmd_process_private));
1942 : :
1943 : 0 : pmd = dev->data->dev_private;
1944 : 0 : dev->process_private = process_private;
1945 : 0 : pmd->dev = dev;
1946 : 0 : strlcpy(pmd->name, tap_name, sizeof(pmd->name));
1947 : 0 : pmd->type = type;
1948 : 0 : pmd->ka_fd = -1;
1949 : :
1950 : : #ifdef HAVE_TCA_FLOWER
1951 : 0 : pmd->nlsk_fd = -1;
1952 : : #endif
1953 : 0 : pmd->gso_ctx_mp = NULL;
1954 : :
1955 : 0 : pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
1956 [ # # ]: 0 : if (pmd->ioctl_sock == -1) {
1957 : 0 : TAP_LOG(ERR,
1958 : : "%s Unable to get a socket for management: %s",
1959 : : tuntap_name, strerror(errno));
1960 : 0 : goto error_exit;
1961 : : }
1962 : :
1963 : : /* Allocate interrupt instance */
1964 : 0 : pmd->intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
1965 [ # # ]: 0 : if (pmd->intr_handle == NULL) {
1966 : 0 : TAP_LOG(ERR, "Failed to allocate intr handle");
1967 : 0 : goto error_exit;
1968 : : }
1969 : :
1970 : : /* Setup some default values */
1971 : 0 : data = dev->data;
1972 : 0 : data->dev_private = pmd;
1973 : 0 : data->dev_flags = RTE_ETH_DEV_INTR_LSC |
1974 : : RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1975 : 0 : data->numa_node = numa_node;
1976 : :
1977 : 0 : data->dev_link = pmd_link;
1978 : 0 : data->mac_addrs = &pmd->eth_addr;
1979 : : /* Set the number of RX and TX queues */
1980 : 0 : data->nb_rx_queues = 0;
1981 : 0 : data->nb_tx_queues = 0;
1982 : :
1983 : 0 : dev->dev_ops = &ops;
1984 : 0 : dev->rx_pkt_burst = pmd_rx_burst;
1985 : 0 : dev->tx_pkt_burst = pmd_tx_burst;
1986 : :
1987 : 0 : rte_intr_type_set(pmd->intr_handle, RTE_INTR_HANDLE_EXT);
1988 : 0 : rte_intr_fd_set(pmd->intr_handle, -1);
1989 : 0 : dev->intr_handle = pmd->intr_handle;
1990 : :
1991 : : /* Presetup the fds to -1 as being not valid */
1992 [ # # ]: 0 : for (i = 0; i < RTE_PMD_TAP_MAX_QUEUES; i++)
1993 : 0 : process_private->fds[i] = -1;
1994 : :
1995 : :
1996 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
1997 [ # # ]: 0 : if (rte_is_zero_ether_addr(mac_addr))
1998 : 0 : rte_eth_random_addr((uint8_t *)&pmd->eth_addr);
1999 : : else
2000 : : rte_ether_addr_copy(mac_addr, &pmd->eth_addr);
2001 : : }
2002 : :
2003 : : /*
2004 : : * Allocate a TUN device keep-alive file descriptor that will only be
2005 : : * closed when the TUN device itself is closed or removed.
2006 : : * This keep-alive file descriptor will guarantee that the TUN device
2007 : : * exists even when all of its queues are closed
2008 : : */
2009 : 0 : pmd->ka_fd = tun_alloc(pmd, 1, persist);
2010 [ # # ]: 0 : if (pmd->ka_fd == -1) {
2011 : 0 : TAP_LOG(ERR, "Unable to create %s interface", tuntap_name);
2012 : 0 : goto error_exit;
2013 : : }
2014 : 0 : TAP_LOG(DEBUG, "allocated %s", pmd->name);
2015 : :
2016 : 0 : ifr.ifr_mtu = dev->data->mtu;
2017 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE) < 0)
2018 : 0 : goto error_exit;
2019 : :
2020 [ # # ]: 0 : if (pmd->type == ETH_TUNTAP_TYPE_TAP) {
2021 : : memset(&ifr, 0, sizeof(struct ifreq));
2022 : 0 : ifr.ifr_hwaddr.sa_family = AF_LOCAL;
2023 : : rte_ether_addr_copy(&pmd->eth_addr, (struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data);
2024 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
2025 : 0 : goto error_exit;
2026 : : }
2027 : :
2028 : : /* Make network device persist after application exit */
2029 : 0 : pmd->persist = persist;
2030 : :
2031 : : #ifdef HAVE_TCA_FLOWER
2032 : : /*
2033 : : * Set up everything related to rte_flow:
2034 : : * - netlink socket
2035 : : * - tap / remote if_index
2036 : : * - mandatory QDISCs
2037 : : * - rte_flow actual/implicit lists
2038 : : * - implicit rules
2039 : : */
2040 : 0 : pmd->nlsk_fd = tap_nl_init(0);
2041 [ # # ]: 0 : if (pmd->nlsk_fd == -1) {
2042 : 0 : TAP_LOG(WARNING, "%s: failed to create netlink socket.",
2043 : : pmd->name);
2044 : 0 : goto disable_rte_flow;
2045 : : }
2046 : 0 : pmd->if_index = if_nametoindex(pmd->name);
2047 [ # # ]: 0 : if (!pmd->if_index) {
2048 : 0 : TAP_LOG(ERR, "%s: failed to get if_index.", pmd->name);
2049 : 0 : goto disable_rte_flow;
2050 : : }
2051 [ # # ]: 0 : if (qdisc_create_multiq(pmd->nlsk_fd, pmd->if_index) < 0) {
2052 : 0 : TAP_LOG(ERR, "%s: failed to create multiq qdisc.",
2053 : : pmd->name);
2054 : 0 : goto disable_rte_flow;
2055 : : }
2056 [ # # ]: 0 : if (qdisc_create_ingress(pmd->nlsk_fd, pmd->if_index) < 0) {
2057 : 0 : TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2058 : : pmd->name);
2059 : 0 : goto disable_rte_flow;
2060 : : }
2061 : :
2062 : 0 : LIST_INIT(&pmd->flows);
2063 : :
2064 [ # # ]: 0 : if (strlen(remote_iface)) {
2065 : 0 : pmd->remote_if_index = if_nametoindex(remote_iface);
2066 [ # # ]: 0 : if (!pmd->remote_if_index) {
2067 : 0 : TAP_LOG(ERR, "%s: failed to get %s if_index.",
2068 : : pmd->name, remote_iface);
2069 : 0 : goto error_remote;
2070 : : }
2071 : 0 : strlcpy(pmd->remote_iface, remote_iface, RTE_ETH_NAME_MAX_LEN);
2072 : :
2073 : : /* Save state of remote device */
2074 : 0 : tap_ioctl(pmd, SIOCGIFFLAGS, &pmd->remote_initial_flags, 0, REMOTE_ONLY);
2075 : :
2076 : : /* Replicate remote MAC address */
2077 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
2078 : 0 : TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2079 : : pmd->name, pmd->remote_iface);
2080 : 0 : goto error_remote;
2081 : : }
2082 : :
2083 : : rte_ether_addr_copy((struct rte_ether_addr *)&ifr.ifr_hwaddr.sa_data, &pmd->eth_addr);
2084 : : /* The desired MAC is already in ifreq after SIOCGIFHWADDR. */
2085 [ # # ]: 0 : if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0) {
2086 : 0 : TAP_LOG(ERR, "%s: failed to get %s MAC address.",
2087 : : pmd->name, remote_iface);
2088 : 0 : goto error_remote;
2089 : : }
2090 : :
2091 : : /*
2092 : : * Flush usually returns negative value because it tries to
2093 : : * delete every QDISC (and on a running device, one QDISC at
2094 : : * least is needed). Ignore negative return value.
2095 : : */
2096 : 0 : qdisc_flush(pmd->nlsk_fd, pmd->remote_if_index);
2097 [ # # ]: 0 : if (qdisc_create_ingress(pmd->nlsk_fd,
2098 : 0 : pmd->remote_if_index) < 0) {
2099 : 0 : TAP_LOG(ERR, "%s: failed to create ingress qdisc.",
2100 : : pmd->remote_iface);
2101 : 0 : goto error_remote;
2102 : : }
2103 : 0 : LIST_INIT(&pmd->implicit_flows);
2104 [ # # # # ]: 0 : if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0 ||
2105 [ # # ]: 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0 ||
2106 [ # # ]: 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0 ||
2107 : 0 : tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0) {
2108 : 0 : TAP_LOG(ERR,
2109 : : "%s: failed to create implicit rules.",
2110 : : pmd->name);
2111 : 0 : goto error_remote;
2112 : : }
2113 : : }
2114 : : #endif
2115 : :
2116 : 0 : rte_eth_dev_probing_finish(dev);
2117 : 0 : return 0;
2118 : :
2119 : 0 : disable_rte_flow:
2120 : 0 : TAP_LOG(ERR, " Disabling rte flow support: %s(%d)",
2121 : : strerror(errno), errno);
2122 [ # # ]: 0 : if (strlen(remote_iface)) {
2123 : 0 : TAP_LOG(ERR, "Remote feature requires flow support.");
2124 : 0 : goto error_exit;
2125 : : }
2126 : 0 : rte_eth_dev_probing_finish(dev);
2127 : 0 : return 0;
2128 : :
2129 : : #ifdef HAVE_TCA_FLOWER
2130 : 0 : error_remote:
2131 : 0 : TAP_LOG(ERR, " Can't set up remote feature: %s(%d)",
2132 : : strerror(errno), errno);
2133 : 0 : tap_flow_implicit_flush(pmd, NULL);
2134 : : #endif
2135 : :
2136 : 0 : error_exit:
2137 : : #ifdef HAVE_TCA_FLOWER
2138 [ # # ]: 0 : if (pmd->nlsk_fd != -1)
2139 : 0 : close(pmd->nlsk_fd);
2140 : : #endif
2141 [ # # ]: 0 : if (pmd->ka_fd != -1)
2142 : 0 : close(pmd->ka_fd);
2143 [ # # ]: 0 : if (pmd->ioctl_sock != -1)
2144 : 0 : close(pmd->ioctl_sock);
2145 : : /* mac_addrs must not be freed alone because part of dev_private */
2146 : 0 : dev->data->mac_addrs = NULL;
2147 : 0 : rte_intr_instance_free(pmd->intr_handle);
2148 : 0 : rte_eth_dev_release_port(dev);
2149 : :
2150 [ # # ]: 0 : error_exit_nodev:
2151 : 0 : TAP_LOG(ERR, "%s Unable to initialize %s",
2152 : : tuntap_name, rte_vdev_device_name(vdev));
2153 : :
2154 : 0 : return -EINVAL;
2155 : : }
2156 : :
2157 : : /* make sure name is a possible Linux network device name */
2158 : : static bool
2159 : 0 : is_valid_iface(const char *name)
2160 : : {
2161 [ # # ]: 0 : if (*name == '\0')
2162 : : return false;
2163 : :
2164 [ # # ]: 0 : if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
2165 : : return false;
2166 : :
2167 [ # # ]: 0 : while (*name) {
2168 [ # # # # ]: 0 : if (*name == '/' || *name == ':' || isspace(*name))
2169 : : return false;
2170 : 0 : name++;
2171 : : }
2172 : : return true;
2173 : : }
2174 : :
2175 : : static int
2176 : 0 : set_interface_name(const char *key __rte_unused,
2177 : : const char *value,
2178 : : void *extra_args)
2179 : : {
2180 : : char *name = (char *)extra_args;
2181 : :
2182 [ # # ]: 0 : if (value) {
2183 [ # # ]: 0 : if (!is_valid_iface(value)) {
2184 : 0 : TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2185 : : value);
2186 : 0 : return -1;
2187 : : }
2188 : : strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2189 : : } else {
2190 : : /* use tap%d which causes kernel to choose next available */
2191 : : strlcpy(name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2192 : : }
2193 : : return 0;
2194 : : }
2195 : :
2196 : : static int
2197 : 0 : set_remote_iface(const char *key __rte_unused,
2198 : : const char *value,
2199 : : void *extra_args)
2200 : : {
2201 : : char *name = (char *)extra_args;
2202 : :
2203 [ # # ]: 0 : if (value) {
2204 [ # # ]: 0 : if (!is_valid_iface(value)) {
2205 : 0 : TAP_LOG(ERR, "TAP invalid remote interface name (%s)",
2206 : : value);
2207 : 0 : return -1;
2208 : : }
2209 : : strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
2210 : : }
2211 : :
2212 : : return 0;
2213 : : }
2214 : :
2215 : : static int
2216 : 0 : set_mac_type(const char *key __rte_unused,
2217 : : const char *value,
2218 : : void *extra_args)
2219 : : {
2220 : : struct rte_ether_addr *user_mac = extra_args;
2221 : :
2222 [ # # ]: 0 : if (!value)
2223 : : return 0;
2224 : :
2225 [ # # ]: 0 : if (!strncasecmp(ETH_TAP_MAC_FIXED, value, strlen(ETH_TAP_MAC_FIXED))) {
2226 : : static int iface_idx;
2227 : :
2228 : : /* fixed mac = 02:64:74:61:70:<iface_idx> */
2229 : 0 : memcpy((char *)user_mac->addr_bytes, "\002dtap",
2230 : : RTE_ETHER_ADDR_LEN);
2231 : 0 : user_mac->addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
2232 : 0 : iface_idx++ + '0';
2233 : 0 : goto success;
2234 : : }
2235 : :
2236 [ # # ]: 0 : if (rte_ether_unformat_addr(value, user_mac) < 0)
2237 : 0 : goto error;
2238 : 0 : success:
2239 : 0 : TAP_LOG(DEBUG, "TAP user MAC param (%s)", value);
2240 : 0 : return 0;
2241 : :
2242 : : error:
2243 : 0 : TAP_LOG(ERR, "TAP user MAC (%s) is not in format (%s|%s)",
2244 : : value, ETH_TAP_MAC_FIXED, ETH_TAP_USR_MAC_FMT);
2245 : 0 : return -1;
2246 : : }
2247 : :
2248 : : /*
2249 : : * Open a TUN interface device. TUN PMD
2250 : : * 1) sets tap_type as false
2251 : : * 2) intakes iface as argument.
2252 : : * 3) as interface is virtual set speed to 10G
2253 : : */
2254 : : static int
2255 [ # # ]: 0 : rte_pmd_tun_probe(struct rte_vdev_device *dev)
2256 : : {
2257 : : const char *name, *params;
2258 : : int ret;
2259 : : struct rte_kvargs *kvlist = NULL;
2260 : : char tun_name[RTE_ETH_NAME_MAX_LEN];
2261 : : char remote_iface[RTE_ETH_NAME_MAX_LEN];
2262 : : struct rte_eth_dev *eth_dev;
2263 : :
2264 : : name = rte_vdev_device_name(dev);
2265 : : params = rte_vdev_device_args(dev);
2266 : : memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2267 : :
2268 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
2269 [ # # ]: 0 : strlen(params) == 0) {
2270 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
2271 [ # # ]: 0 : if (!eth_dev) {
2272 : 0 : TAP_LOG(ERR, "Failed to probe %s", name);
2273 : 0 : return -1;
2274 : : }
2275 : 0 : eth_dev->dev_ops = &ops;
2276 : 0 : eth_dev->device = &dev->device;
2277 : 0 : rte_eth_dev_probing_finish(eth_dev);
2278 : 0 : return 0;
2279 : : }
2280 : :
2281 : : /* use tun%d which causes kernel to choose next available */
2282 : : strlcpy(tun_name, DEFAULT_TUN_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2283 : :
2284 [ # # # # ]: 0 : if (params && (params[0] != '\0')) {
2285 : 0 : TAP_LOG(DEBUG, "parameters (%s)", params);
2286 : :
2287 : 0 : kvlist = rte_kvargs_parse(params, valid_arguments);
2288 [ # # ]: 0 : if (kvlist) {
2289 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2290 : 0 : ret = rte_kvargs_process_opt(kvlist,
2291 : : ETH_TAP_IFACE_ARG,
2292 : : &set_interface_name,
2293 : : tun_name);
2294 : :
2295 [ # # ]: 0 : if (ret == -1)
2296 : 0 : goto leave;
2297 : : }
2298 : : }
2299 : : }
2300 : 0 : pmd_link.link_speed = RTE_ETH_SPEED_NUM_10G;
2301 : :
2302 : 0 : TAP_LOG(DEBUG, "Initializing pmd_tun for %s", name);
2303 : :
2304 : 0 : ret = eth_dev_tap_create(dev, tun_name, remote_iface, 0,
2305 : : ETH_TUNTAP_TYPE_TUN, 0);
2306 : :
2307 : 0 : leave:
2308 [ # # ]: 0 : if (ret == -1) {
2309 : 0 : TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2310 : : name, tun_name);
2311 : : }
2312 : 0 : rte_kvargs_free(kvlist);
2313 : :
2314 : 0 : return ret;
2315 : : }
2316 : :
2317 : : /* Request queue file descriptors from secondary to primary. */
2318 : : static int
2319 : 0 : tap_mp_attach_queues(const char *port_name, struct rte_eth_dev *dev)
2320 : : {
2321 : : int ret;
2322 : 0 : struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
2323 : : struct rte_mp_msg request, *reply;
2324 : : struct rte_mp_reply replies;
2325 : : struct ipc_queues *request_param = (struct ipc_queues *)request.param;
2326 : : struct ipc_queues *reply_param;
2327 : 0 : struct pmd_process_private *process_private = dev->process_private;
2328 : :
2329 : : /* Prepare the request */
2330 : : memset(&request, 0, sizeof(request));
2331 : : strlcpy(request.name, TAP_MP_KEY, sizeof(request.name));
2332 : : strlcpy(request_param->port_name, port_name,
2333 : : sizeof(request_param->port_name));
2334 : 0 : request.len_param = sizeof(*request_param);
2335 : : /* Send request and receive reply */
2336 : 0 : ret = rte_mp_request_sync(&request, &replies, &timeout);
2337 [ # # # # ]: 0 : if (ret < 0 || replies.nb_received != 1) {
2338 : 0 : TAP_LOG(ERR, "Failed to request queues from primary: %d",
2339 : : rte_errno);
2340 : 0 : return -1;
2341 : : }
2342 : 0 : reply = &replies.msgs[0];
2343 : : reply_param = (struct ipc_queues *)reply->param;
2344 : 0 : TAP_LOG(DEBUG, "Received IPC reply for %s", reply_param->port_name);
2345 : :
2346 : : /* Attach the queues from received file descriptors */
2347 [ # # ]: 0 : if (reply_param->q_count != reply->num_fds) {
2348 : 0 : TAP_LOG(ERR, "Unexpected number of fds received");
2349 : 0 : return -1;
2350 : : }
2351 : :
2352 : 0 : dev->data->nb_rx_queues = reply_param->q_count;
2353 : 0 : dev->data->nb_tx_queues = reply_param->q_count;
2354 : :
2355 [ # # ]: 0 : for (int q = 0; q < reply_param->q_count; q++)
2356 : 0 : process_private->fds[q] = reply->fds[q];
2357 : :
2358 : 0 : free(reply);
2359 : 0 : return 0;
2360 : : }
2361 : :
2362 : : /* Send the queue file descriptors from the primary process to secondary. */
2363 : : static int
2364 : 0 : tap_mp_sync_queues(const struct rte_mp_msg *request, const void *peer)
2365 : : {
2366 : : struct rte_eth_dev *dev;
2367 : : struct pmd_process_private *process_private;
2368 : : struct rte_mp_msg reply;
2369 : : const struct ipc_queues *request_param =
2370 : : (const struct ipc_queues *)request->param;
2371 : : struct ipc_queues *reply_param =
2372 : : (struct ipc_queues *)reply.param;
2373 : : int queue;
2374 : :
2375 : : /* Get requested port */
2376 : 0 : TAP_LOG(DEBUG, "Received IPC request for %s", request_param->port_name);
2377 : 0 : dev = rte_eth_dev_get_by_name(request_param->port_name);
2378 [ # # ]: 0 : if (!dev) {
2379 : 0 : TAP_LOG(ERR, "Failed to get port id for %s",
2380 : : request_param->port_name);
2381 : 0 : return -1;
2382 : : }
2383 : 0 : process_private = dev->process_private;
2384 : :
2385 : : /* Fill file descriptors for all queues */
2386 : 0 : reply.num_fds = 0;
2387 : 0 : reply_param->q_count = 0;
2388 : :
2389 : : RTE_ASSERT(dev->data->nb_rx_queues == dev->data->nb_tx_queues);
2390 : :
2391 [ # # ]: 0 : if (dev->data->nb_rx_queues > RTE_PMD_TAP_MAX_QUEUES) {
2392 : 0 : TAP_LOG(ERR, "Number of rx/tx queues %u exceeds max number of fds %u",
2393 : : dev->data->nb_rx_queues, RTE_PMD_TAP_MAX_QUEUES);
2394 : 0 : return -1;
2395 : : }
2396 : :
2397 [ # # ]: 0 : for (queue = 0; queue < dev->data->nb_rx_queues; queue++) {
2398 : 0 : reply.fds[reply.num_fds++] = process_private->fds[queue];
2399 : 0 : reply_param->q_count++;
2400 : : }
2401 : :
2402 : : /* Send reply */
2403 : 0 : strlcpy(reply.name, request->name, sizeof(reply.name));
2404 : : strlcpy(reply_param->port_name, request_param->port_name,
2405 : : sizeof(reply_param->port_name));
2406 : 0 : reply.len_param = sizeof(*reply_param);
2407 [ # # ]: 0 : if (rte_mp_reply(&reply, peer) < 0) {
2408 : 0 : TAP_LOG(ERR, "Failed to reply an IPC request to sync queues");
2409 : 0 : return -1;
2410 : : }
2411 : : return 0;
2412 : : }
2413 : :
2414 : : /* Open a TAP interface device.
2415 : : */
2416 : : static int
2417 : 0 : rte_pmd_tap_probe(struct rte_vdev_device *dev)
2418 : : {
2419 : : const char *name, *params;
2420 : : int ret;
2421 : : struct rte_kvargs *kvlist = NULL;
2422 : : int speed;
2423 : : char tap_name[RTE_ETH_NAME_MAX_LEN];
2424 : : char remote_iface[RTE_ETH_NAME_MAX_LEN];
2425 [ # # ]: 0 : struct rte_ether_addr user_mac = { .addr_bytes = {0} };
2426 : : struct rte_eth_dev *eth_dev;
2427 : : int tap_devices_count_increased = 0;
2428 : : int persist = 0;
2429 : :
2430 : : name = rte_vdev_device_name(dev);
2431 : : params = rte_vdev_device_args(dev);
2432 : :
2433 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2434 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
2435 [ # # ]: 0 : if (!eth_dev) {
2436 : 0 : TAP_LOG(ERR, "Failed to probe %s", name);
2437 : 0 : return -1;
2438 : : }
2439 : 0 : eth_dev->dev_ops = &ops;
2440 : 0 : eth_dev->device = &dev->device;
2441 : 0 : eth_dev->rx_pkt_burst = pmd_rx_burst;
2442 : 0 : eth_dev->tx_pkt_burst = pmd_tx_burst;
2443 [ # # ]: 0 : if (!rte_eal_primary_proc_alive(NULL)) {
2444 : 0 : TAP_LOG(ERR, "Primary process is missing");
2445 : 0 : return -1;
2446 : : }
2447 : 0 : eth_dev->process_private = malloc(sizeof(struct pmd_process_private));
2448 [ # # ]: 0 : if (eth_dev->process_private == NULL) {
2449 : 0 : TAP_LOG(ERR,
2450 : : "Failed to alloc memory for process private");
2451 : 0 : return -1;
2452 : : }
2453 : : memset(eth_dev->process_private, 0, sizeof(struct pmd_process_private));
2454 : :
2455 : 0 : ret = tap_mp_attach_queues(name, eth_dev);
2456 [ # # ]: 0 : if (ret != 0)
2457 : : return -1;
2458 : :
2459 [ # # ]: 0 : if (!tap_devices_count) {
2460 : 0 : ret = rte_mp_action_register(TAP_MP_REQ_START_RXTX, tap_mp_req_start_rxtx);
2461 [ # # # # ]: 0 : if (ret < 0 && rte_errno != ENOTSUP) {
2462 : 0 : TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2463 : : strerror(rte_errno));
2464 : 0 : return -1;
2465 : : }
2466 : : }
2467 : 0 : tap_devices_count++;
2468 : 0 : rte_eth_dev_probing_finish(eth_dev);
2469 : 0 : return 0;
2470 : : }
2471 : :
2472 : : speed = RTE_ETH_SPEED_NUM_10G;
2473 : :
2474 : : /* use tap%d which causes kernel to choose next available */
2475 : : strlcpy(tap_name, DEFAULT_TAP_NAME "%d", RTE_ETH_NAME_MAX_LEN);
2476 : : memset(remote_iface, 0, RTE_ETH_NAME_MAX_LEN);
2477 : :
2478 [ # # # # ]: 0 : if (params && (params[0] != '\0')) {
2479 : 0 : TAP_LOG(DEBUG, "parameters (%s)", params);
2480 : :
2481 : 0 : kvlist = rte_kvargs_parse(params, valid_arguments);
2482 [ # # ]: 0 : if (kvlist) {
2483 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_IFACE_ARG) == 1) {
2484 : 0 : ret = rte_kvargs_process_opt(kvlist,
2485 : : ETH_TAP_IFACE_ARG,
2486 : : &set_interface_name,
2487 : : tap_name);
2488 [ # # ]: 0 : if (ret == -1)
2489 : 0 : goto leave;
2490 : : }
2491 : :
2492 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_REMOTE_ARG) == 1) {
2493 : 0 : ret = rte_kvargs_process(kvlist,
2494 : : ETH_TAP_REMOTE_ARG,
2495 : : &set_remote_iface,
2496 : : remote_iface);
2497 [ # # ]: 0 : if (ret == -1)
2498 : 0 : goto leave;
2499 : : }
2500 : :
2501 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_MAC_ARG) == 1) {
2502 : 0 : ret = rte_kvargs_process(kvlist,
2503 : : ETH_TAP_MAC_ARG,
2504 : : &set_mac_type,
2505 : : &user_mac);
2506 [ # # ]: 0 : if (ret == -1)
2507 : 0 : goto leave;
2508 : : }
2509 : :
2510 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_TAP_PERSIST_ARG) == 1)
2511 : : persist = 1;
2512 : : }
2513 : : }
2514 : 0 : pmd_link.link_speed = speed;
2515 : :
2516 : 0 : TAP_LOG(DEBUG, "Initializing pmd_tap for %s", name);
2517 : :
2518 : : /* Register IPC feed callback */
2519 [ # # ]: 0 : if (!tap_devices_count) {
2520 : 0 : ret = rte_mp_action_register(TAP_MP_KEY, tap_mp_sync_queues);
2521 [ # # # # ]: 0 : if (ret < 0 && rte_errno != ENOTSUP) {
2522 : 0 : TAP_LOG(ERR, "tap: Failed to register IPC callback: %s",
2523 : : strerror(rte_errno));
2524 : 0 : goto leave;
2525 : : }
2526 : : }
2527 : 0 : tap_devices_count++;
2528 : : tap_devices_count_increased = 1;
2529 : 0 : ret = eth_dev_tap_create(dev, tap_name, remote_iface, &user_mac,
2530 : : ETH_TUNTAP_TYPE_TAP, persist);
2531 : :
2532 : 0 : leave:
2533 [ # # ]: 0 : if (ret == -1) {
2534 : 0 : TAP_LOG(ERR, "Failed to create pmd for %s as %s",
2535 : : name, tap_name);
2536 [ # # ]: 0 : if (tap_devices_count_increased == 1) {
2537 [ # # ]: 0 : if (tap_devices_count == 1)
2538 : 0 : rte_mp_action_unregister(TAP_MP_KEY);
2539 : 0 : tap_devices_count--;
2540 : : }
2541 : : }
2542 : 0 : rte_kvargs_free(kvlist);
2543 : :
2544 : 0 : return ret;
2545 : : }
2546 : :
2547 : : /* detach a TUNTAP device.
2548 : : */
2549 : : static int
2550 [ # # ]: 0 : rte_pmd_tap_remove(struct rte_vdev_device *dev)
2551 : : {
2552 : : struct rte_eth_dev *eth_dev = NULL;
2553 : :
2554 : : /* find the ethdev entry */
2555 : 0 : eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2556 [ # # ]: 0 : if (!eth_dev)
2557 : : return 0;
2558 : :
2559 : 0 : tap_dev_close(eth_dev);
2560 : 0 : rte_eth_dev_release_port(eth_dev);
2561 : :
2562 : 0 : return 0;
2563 : : }
2564 : :
2565 : : static struct rte_vdev_driver pmd_tun_drv = {
2566 : : .probe = rte_pmd_tun_probe,
2567 : : .remove = rte_pmd_tap_remove,
2568 : : };
2569 : :
2570 : : static struct rte_vdev_driver pmd_tap_drv = {
2571 : : .probe = rte_pmd_tap_probe,
2572 : : .remove = rte_pmd_tap_remove,
2573 : : };
2574 : :
2575 : 253 : RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
2576 : 253 : RTE_PMD_REGISTER_VDEV(net_tun, pmd_tun_drv);
2577 : : RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
2578 : : RTE_PMD_REGISTER_PARAM_STRING(net_tun,
2579 : : ETH_TAP_IFACE_ARG "=<string> ");
2580 : : RTE_PMD_REGISTER_PARAM_STRING(net_tap,
2581 : : ETH_TAP_IFACE_ARG "=<string> "
2582 : : ETH_TAP_MAC_ARG "=" ETH_TAP_MAC_ARG_FMT " "
2583 : : ETH_TAP_REMOTE_ARG "=<string>");
2584 [ - + ]: 253 : RTE_LOG_REGISTER_DEFAULT(tap_logtype, NOTICE);
|