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