fcd80903995911ac1b5d1b6a3d67dbcc5a390842
[dpdk.git] / drivers / net / af_packet / rte_eth_af_packet.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014 John W. Linville <linville@tuxdriver.com>
3  * Originally based upon librte_pmd_pcap code:
4  * Copyright(c) 2010-2015 Intel Corporation.
5  * Copyright(c) 2014 6WIND S.A.
6  * All rights reserved.
7  */
8
9 #include <rte_string_fns.h>
10 #include <rte_mbuf.h>
11 #include <ethdev_driver.h>
12 #include <ethdev_vdev.h>
13 #include <rte_malloc.h>
14 #include <rte_kvargs.h>
15 #include <rte_bus_vdev.h>
16
17 #include <errno.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_packet.h>
20 #include <arpa/inet.h>
21 #include <net/if.h>
22 #include <net/if_arp.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
29 #include <poll.h>
30
31 #define ETH_AF_PACKET_IFACE_ARG         "iface"
32 #define ETH_AF_PACKET_NUM_Q_ARG         "qpairs"
33 #define ETH_AF_PACKET_BLOCKSIZE_ARG     "blocksz"
34 #define ETH_AF_PACKET_FRAMESIZE_ARG     "framesz"
35 #define ETH_AF_PACKET_FRAMECOUNT_ARG    "framecnt"
36 #define ETH_AF_PACKET_QDISC_BYPASS_ARG  "qdisc_bypass"
37
38 #define DFLT_FRAME_SIZE         (1 << 11)
39 #define DFLT_FRAME_COUNT        (1 << 9)
40
41 struct pkt_rx_queue {
42         int sockfd;
43
44         struct iovec *rd;
45         uint8_t *map;
46         unsigned int framecount;
47         unsigned int framenum;
48
49         struct rte_mempool *mb_pool;
50         uint16_t in_port;
51
52         volatile unsigned long rx_pkts;
53         volatile unsigned long rx_bytes;
54 };
55
56 struct pkt_tx_queue {
57         int sockfd;
58         unsigned int frame_data_size;
59
60         struct iovec *rd;
61         uint8_t *map;
62         unsigned int framecount;
63         unsigned int framenum;
64
65         volatile unsigned long tx_pkts;
66         volatile unsigned long err_pkts;
67         volatile unsigned long tx_bytes;
68 };
69
70 struct pmd_internals {
71         unsigned nb_queues;
72
73         int if_index;
74         char *if_name;
75         struct rte_ether_addr eth_addr;
76
77         struct tpacket_req req;
78
79         struct pkt_rx_queue *rx_queue;
80         struct pkt_tx_queue *tx_queue;
81 };
82
83 static const char *valid_arguments[] = {
84         ETH_AF_PACKET_IFACE_ARG,
85         ETH_AF_PACKET_NUM_Q_ARG,
86         ETH_AF_PACKET_BLOCKSIZE_ARG,
87         ETH_AF_PACKET_FRAMESIZE_ARG,
88         ETH_AF_PACKET_FRAMECOUNT_ARG,
89         ETH_AF_PACKET_QDISC_BYPASS_ARG,
90         NULL
91 };
92
93 static struct rte_eth_link pmd_link = {
94         .link_speed = ETH_SPEED_NUM_10G,
95         .link_duplex = ETH_LINK_FULL_DUPLEX,
96         .link_status = ETH_LINK_DOWN,
97         .link_autoneg = ETH_LINK_FIXED,
98 };
99
100 RTE_LOG_REGISTER_DEFAULT(af_packet_logtype, NOTICE);
101
102 #define PMD_LOG(level, fmt, args...) \
103         rte_log(RTE_LOG_ ## level, af_packet_logtype, \
104                 "%s(): " fmt "\n", __func__, ##args)
105
106 #define PMD_LOG_ERRNO(level, fmt, args...) \
107         rte_log(RTE_LOG_ ## level, af_packet_logtype, \
108                 "%s(): " fmt ":%s\n", __func__, ##args, strerror(errno))
109
110 static uint16_t
111 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
112 {
113         unsigned i;
114         struct tpacket2_hdr *ppd;
115         struct rte_mbuf *mbuf;
116         uint8_t *pbuf;
117         struct pkt_rx_queue *pkt_q = queue;
118         uint16_t num_rx = 0;
119         unsigned long num_rx_bytes = 0;
120         unsigned int framecount, framenum;
121
122         if (unlikely(nb_pkts == 0))
123                 return 0;
124
125         /*
126          * Reads the given number of packets from the AF_PACKET socket one by
127          * one and copies the packet data into a newly allocated mbuf.
128          */
129         framecount = pkt_q->framecount;
130         framenum = pkt_q->framenum;
131         for (i = 0; i < nb_pkts; i++) {
132                 /* point at the next incoming frame */
133                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
134                 if ((ppd->tp_status & TP_STATUS_USER) == 0)
135                         break;
136
137                 /* allocate the next mbuf */
138                 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool);
139                 if (unlikely(mbuf == NULL))
140                         break;
141
142                 /* packet will fit in the mbuf, go ahead and receive it */
143                 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf) = ppd->tp_snaplen;
144                 pbuf = (uint8_t *) ppd + ppd->tp_mac;
145                 memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf));
146
147                 /* check for vlan info */
148                 if (ppd->tp_status & TP_STATUS_VLAN_VALID) {
149                         mbuf->vlan_tci = ppd->tp_vlan_tci;
150                         mbuf->ol_flags |= (PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED);
151                 }
152
153                 /* release incoming frame and advance ring buffer */
154                 ppd->tp_status = TP_STATUS_KERNEL;
155                 if (++framenum >= framecount)
156                         framenum = 0;
157                 mbuf->port = pkt_q->in_port;
158
159                 /* account for the receive frame */
160                 bufs[i] = mbuf;
161                 num_rx++;
162                 num_rx_bytes += mbuf->pkt_len;
163         }
164         pkt_q->framenum = framenum;
165         pkt_q->rx_pkts += num_rx;
166         pkt_q->rx_bytes += num_rx_bytes;
167         return num_rx;
168 }
169
170 /*
171  * Check if there is an available frame in the ring
172  */
173 static inline bool
174 tx_ring_status_available(uint32_t tp_status)
175 {
176         /*
177          * We eliminate the timestamp status from the packet status.
178          * This should only matter if timestamping is enabled on the socket,
179          * but there is a bug in the kernel which is fixed in newer releases.
180          *
181          * See the following kernel commit for reference:
182          *     commit 171c3b151118a2fe0fc1e2a9d1b5a1570cfe82d2
183          *     net: packetmmap: fix only tx timestamp on request
184          */
185         tp_status &= ~(TP_STATUS_TS_SOFTWARE | TP_STATUS_TS_RAW_HARDWARE);
186
187         return tp_status == TP_STATUS_AVAILABLE;
188 }
189
190 /*
191  * Callback to handle sending packets through a real NIC.
192  */
193 static uint16_t
194 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
195 {
196         struct tpacket2_hdr *ppd;
197         struct rte_mbuf *mbuf;
198         uint8_t *pbuf;
199         unsigned int framecount, framenum;
200         struct pollfd pfd;
201         struct pkt_tx_queue *pkt_q = queue;
202         uint16_t num_tx = 0;
203         unsigned long num_tx_bytes = 0;
204         int i;
205
206         if (unlikely(nb_pkts == 0))
207                 return 0;
208
209         memset(&pfd, 0, sizeof(pfd));
210         pfd.fd = pkt_q->sockfd;
211         pfd.events = POLLOUT;
212         pfd.revents = 0;
213
214         framecount = pkt_q->framecount;
215         framenum = pkt_q->framenum;
216         ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
217         for (i = 0; i < nb_pkts; i++) {
218                 mbuf = *bufs++;
219
220                 /* drop oversized packets */
221                 if (mbuf->pkt_len > pkt_q->frame_data_size) {
222                         rte_pktmbuf_free(mbuf);
223                         continue;
224                 }
225
226                 /* insert vlan info if necessary */
227                 if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
228                         if (rte_vlan_insert(&mbuf)) {
229                                 rte_pktmbuf_free(mbuf);
230                                 continue;
231                         }
232                 }
233
234                 /* point at the next incoming frame */
235                 if (!tx_ring_status_available(ppd->tp_status) &&
236                     poll(&pfd, 1, -1) < 0)
237                         break;
238
239                 /* copy the tx frame data */
240                 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
241                         sizeof(struct sockaddr_ll);
242
243                 struct rte_mbuf *tmp_mbuf = mbuf;
244                 while (tmp_mbuf) {
245                         uint16_t data_len = rte_pktmbuf_data_len(tmp_mbuf);
246                         memcpy(pbuf, rte_pktmbuf_mtod(tmp_mbuf, void*), data_len);
247                         pbuf += data_len;
248                         tmp_mbuf = tmp_mbuf->next;
249                 }
250
251                 ppd->tp_len = mbuf->pkt_len;
252                 ppd->tp_snaplen = mbuf->pkt_len;
253
254                 /* release incoming frame and advance ring buffer */
255                 ppd->tp_status = TP_STATUS_SEND_REQUEST;
256                 if (++framenum >= framecount)
257                         framenum = 0;
258                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
259
260                 num_tx++;
261                 num_tx_bytes += mbuf->pkt_len;
262                 rte_pktmbuf_free(mbuf);
263         }
264
265         /* kick-off transmits */
266         if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1 &&
267                         errno != ENOBUFS && errno != EAGAIN) {
268                 /*
269                  * In case of a ENOBUFS/EAGAIN error all of the enqueued
270                  * packets will be considered successful even though only some
271                  * are sent.
272                  */
273
274                 num_tx = 0;
275                 num_tx_bytes = 0;
276         }
277
278         pkt_q->framenum = framenum;
279         pkt_q->tx_pkts += num_tx;
280         pkt_q->err_pkts += i - num_tx;
281         pkt_q->tx_bytes += num_tx_bytes;
282         return i;
283 }
284
285 static int
286 eth_dev_start(struct rte_eth_dev *dev)
287 {
288         dev->data->dev_link.link_status = ETH_LINK_UP;
289         return 0;
290 }
291
292 /*
293  * This function gets called when the current port gets stopped.
294  */
295 static int
296 eth_dev_stop(struct rte_eth_dev *dev)
297 {
298         unsigned i;
299         int sockfd;
300         struct pmd_internals *internals = dev->data->dev_private;
301
302         for (i = 0; i < internals->nb_queues; i++) {
303                 sockfd = internals->rx_queue[i].sockfd;
304                 if (sockfd != -1)
305                         close(sockfd);
306
307                 /* Prevent use after free in case tx fd == rx fd */
308                 if (sockfd != internals->tx_queue[i].sockfd) {
309                         sockfd = internals->tx_queue[i].sockfd;
310                         if (sockfd != -1)
311                                 close(sockfd);
312                 }
313
314                 internals->rx_queue[i].sockfd = -1;
315                 internals->tx_queue[i].sockfd = -1;
316         }
317
318         dev->data->dev_link.link_status = ETH_LINK_DOWN;
319         return 0;
320 }
321
322 static int
323 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
324 {
325         return 0;
326 }
327
328 static int
329 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
330 {
331         struct pmd_internals *internals = dev->data->dev_private;
332
333         dev_info->if_index = internals->if_index;
334         dev_info->max_mac_addrs = 1;
335         dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
336         dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
337         dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
338         dev_info->min_rx_bufsize = 0;
339         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
340                 DEV_TX_OFFLOAD_VLAN_INSERT;
341
342         return 0;
343 }
344
345 static int
346 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
347 {
348         unsigned i, imax;
349         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
350         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
351         const struct pmd_internals *internal = dev->data->dev_private;
352
353         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
354                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
355         for (i = 0; i < imax; i++) {
356                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
357                 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
358                 rx_total += igb_stats->q_ipackets[i];
359                 rx_bytes_total += igb_stats->q_ibytes[i];
360         }
361
362         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
363                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
364         for (i = 0; i < imax; i++) {
365                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
366                 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
367                 tx_total += igb_stats->q_opackets[i];
368                 tx_err_total += internal->tx_queue[i].err_pkts;
369                 tx_bytes_total += igb_stats->q_obytes[i];
370         }
371
372         igb_stats->ipackets = rx_total;
373         igb_stats->ibytes = rx_bytes_total;
374         igb_stats->opackets = tx_total;
375         igb_stats->oerrors = tx_err_total;
376         igb_stats->obytes = tx_bytes_total;
377         return 0;
378 }
379
380 static int
381 eth_stats_reset(struct rte_eth_dev *dev)
382 {
383         unsigned i;
384         struct pmd_internals *internal = dev->data->dev_private;
385
386         for (i = 0; i < internal->nb_queues; i++) {
387                 internal->rx_queue[i].rx_pkts = 0;
388                 internal->rx_queue[i].rx_bytes = 0;
389         }
390
391         for (i = 0; i < internal->nb_queues; i++) {
392                 internal->tx_queue[i].tx_pkts = 0;
393                 internal->tx_queue[i].err_pkts = 0;
394                 internal->tx_queue[i].tx_bytes = 0;
395         }
396
397         return 0;
398 }
399
400 static int
401 eth_dev_close(struct rte_eth_dev *dev)
402 {
403         struct pmd_internals *internals;
404         struct tpacket_req *req;
405         unsigned int q;
406
407         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
408                 return 0;
409
410         PMD_LOG(INFO, "Closing AF_PACKET ethdev on NUMA socket %u",
411                 rte_socket_id());
412
413         internals = dev->data->dev_private;
414         req = &internals->req;
415         for (q = 0; q < internals->nb_queues; q++) {
416                 munmap(internals->rx_queue[q].map,
417                         2 * req->tp_block_size * req->tp_block_nr);
418                 rte_free(internals->rx_queue[q].rd);
419                 rte_free(internals->tx_queue[q].rd);
420         }
421         free(internals->if_name);
422         rte_free(internals->rx_queue);
423         rte_free(internals->tx_queue);
424
425         /* mac_addrs must not be freed alone because part of dev_private */
426         dev->data->mac_addrs = NULL;
427         return 0;
428 }
429
430 static void
431 eth_queue_release(void *q __rte_unused)
432 {
433 }
434
435 static int
436 eth_link_update(struct rte_eth_dev *dev __rte_unused,
437                 int wait_to_complete __rte_unused)
438 {
439         return 0;
440 }
441
442 static int
443 eth_rx_queue_setup(struct rte_eth_dev *dev,
444                    uint16_t rx_queue_id,
445                    uint16_t nb_rx_desc __rte_unused,
446                    unsigned int socket_id __rte_unused,
447                    const struct rte_eth_rxconf *rx_conf __rte_unused,
448                    struct rte_mempool *mb_pool)
449 {
450         struct pmd_internals *internals = dev->data->dev_private;
451         struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
452         unsigned int buf_size, data_size;
453
454         pkt_q->mb_pool = mb_pool;
455
456         /* Now get the space available for data in the mbuf */
457         buf_size = rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
458                 RTE_PKTMBUF_HEADROOM;
459         data_size = internals->req.tp_frame_size;
460         data_size -= TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
461
462         if (data_size > buf_size) {
463                 PMD_LOG(ERR,
464                         "%s: %d bytes will not fit in mbuf (%d bytes)",
465                         dev->device->name, data_size, buf_size);
466                 return -ENOMEM;
467         }
468
469         dev->data->rx_queues[rx_queue_id] = pkt_q;
470         pkt_q->in_port = dev->data->port_id;
471
472         return 0;
473 }
474
475 static int
476 eth_tx_queue_setup(struct rte_eth_dev *dev,
477                    uint16_t tx_queue_id,
478                    uint16_t nb_tx_desc __rte_unused,
479                    unsigned int socket_id __rte_unused,
480                    const struct rte_eth_txconf *tx_conf __rte_unused)
481 {
482
483         struct pmd_internals *internals = dev->data->dev_private;
484
485         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
486         return 0;
487 }
488
489 static int
490 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
491 {
492         struct pmd_internals *internals = dev->data->dev_private;
493         struct ifreq ifr = { .ifr_mtu = mtu };
494         int ret;
495         int s;
496         unsigned int data_size = internals->req.tp_frame_size -
497                                  TPACKET2_HDRLEN;
498
499         if (mtu > data_size)
500                 return -EINVAL;
501
502         s = socket(PF_INET, SOCK_DGRAM, 0);
503         if (s < 0)
504                 return -EINVAL;
505
506         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
507         ret = ioctl(s, SIOCSIFMTU, &ifr);
508         close(s);
509
510         if (ret < 0)
511                 return -EINVAL;
512
513         return 0;
514 }
515
516 static int
517 eth_dev_macaddr_set(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
518 {
519         struct pmd_internals *internals = dev->data->dev_private;
520         struct ifreq ifr = { };
521         int sockfd = internals->rx_queue[0].sockfd;
522         int ret;
523
524         if (sockfd == -1) {
525                 PMD_LOG(ERR, "receive socket not found");
526                 return -EINVAL;
527         }
528
529         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
530         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
531         memcpy(ifr.ifr_hwaddr.sa_data, addr, sizeof(*addr));
532         ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
533
534         if (ret < 0) {
535                 PMD_LOG_ERRNO(ERR, "ioctl(SIOCSIFHWADDR) failed");
536                 return -EINVAL;
537         }
538
539         return 0;
540 }
541
542 static int
543 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
544 {
545         struct ifreq ifr;
546         int ret = 0;
547         int s;
548
549         s = socket(PF_INET, SOCK_DGRAM, 0);
550         if (s < 0)
551                 return -errno;
552
553         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
554         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
555                 ret = -errno;
556                 goto out;
557         }
558         ifr.ifr_flags &= mask;
559         ifr.ifr_flags |= flags;
560         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
561                 ret = -errno;
562                 goto out;
563         }
564 out:
565         close(s);
566         return ret;
567 }
568
569 static int
570 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
571 {
572         struct pmd_internals *internals = dev->data->dev_private;
573
574         return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
575 }
576
577 static int
578 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
579 {
580         struct pmd_internals *internals = dev->data->dev_private;
581
582         return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
583 }
584
585 static const struct eth_dev_ops ops = {
586         .dev_start = eth_dev_start,
587         .dev_stop = eth_dev_stop,
588         .dev_close = eth_dev_close,
589         .dev_configure = eth_dev_configure,
590         .dev_infos_get = eth_dev_info,
591         .mac_addr_set = eth_dev_macaddr_set,
592         .mtu_set = eth_dev_mtu_set,
593         .promiscuous_enable = eth_dev_promiscuous_enable,
594         .promiscuous_disable = eth_dev_promiscuous_disable,
595         .rx_queue_setup = eth_rx_queue_setup,
596         .tx_queue_setup = eth_tx_queue_setup,
597         .rx_queue_release = eth_queue_release,
598         .tx_queue_release = eth_queue_release,
599         .link_update = eth_link_update,
600         .stats_get = eth_stats_get,
601         .stats_reset = eth_stats_reset,
602 };
603
604 /*
605  * Opens an AF_PACKET socket
606  */
607 static int
608 open_packet_iface(const char *key __rte_unused,
609                   const char *value __rte_unused,
610                   void *extra_args)
611 {
612         int *sockfd = extra_args;
613
614         /* Open an AF_PACKET socket... */
615         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
616         if (*sockfd == -1) {
617                 PMD_LOG(ERR, "Could not open AF_PACKET socket");
618                 return -1;
619         }
620
621         return 0;
622 }
623
624 static int
625 rte_pmd_init_internals(struct rte_vdev_device *dev,
626                        const int sockfd,
627                        const unsigned nb_queues,
628                        unsigned int blocksize,
629                        unsigned int blockcnt,
630                        unsigned int framesize,
631                        unsigned int framecnt,
632                        unsigned int qdisc_bypass,
633                        struct pmd_internals **internals,
634                        struct rte_eth_dev **eth_dev,
635                        struct rte_kvargs *kvlist)
636 {
637         const char *name = rte_vdev_device_name(dev);
638         const unsigned int numa_node = dev->device.numa_node;
639         struct rte_eth_dev_data *data = NULL;
640         struct rte_kvargs_pair *pair = NULL;
641         struct ifreq ifr;
642         size_t ifnamelen;
643         unsigned k_idx;
644         struct sockaddr_ll sockaddr;
645         struct tpacket_req *req;
646         struct pkt_rx_queue *rx_queue;
647         struct pkt_tx_queue *tx_queue;
648         int rc, tpver, discard;
649         int qsockfd = -1;
650         unsigned int i, q, rdsize;
651 #if defined(PACKET_FANOUT)
652         int fanout_arg;
653 #endif
654
655         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
656                 pair = &kvlist->pairs[k_idx];
657                 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
658                         break;
659         }
660         if (pair == NULL) {
661                 PMD_LOG(ERR,
662                         "%s: no interface specified for AF_PACKET ethdev",
663                         name);
664                 return -1;
665         }
666
667         PMD_LOG(INFO,
668                 "%s: creating AF_PACKET-backed ethdev on numa socket %u",
669                 name, numa_node);
670
671         *internals = rte_zmalloc_socket(name, sizeof(**internals),
672                                         0, numa_node);
673         if (*internals == NULL)
674                 return -1;
675
676
677         (*internals)->rx_queue = rte_calloc_socket("af_packet_rx",
678                                                 nb_queues,
679                                                 sizeof(struct pkt_rx_queue),
680                                                 0, numa_node);
681         (*internals)->tx_queue = rte_calloc_socket("af_packet_tx",
682                                                 nb_queues,
683                                                 sizeof(struct pkt_tx_queue),
684                                                 0, numa_node);
685         if (!(*internals)->rx_queue || !(*internals)->tx_queue) {
686                 goto free_internals;
687         }
688
689         for (q = 0; q < nb_queues; q++) {
690                 (*internals)->rx_queue[q].map = MAP_FAILED;
691                 (*internals)->tx_queue[q].map = MAP_FAILED;
692                 (*internals)->rx_queue[q].sockfd = -1;
693                 (*internals)->tx_queue[q].sockfd = -1;
694         }
695
696         req = &((*internals)->req);
697
698         req->tp_block_size = blocksize;
699         req->tp_block_nr = blockcnt;
700         req->tp_frame_size = framesize;
701         req->tp_frame_nr = framecnt;
702
703         ifnamelen = strlen(pair->value);
704         if (ifnamelen < sizeof(ifr.ifr_name)) {
705                 memcpy(ifr.ifr_name, pair->value, ifnamelen);
706                 ifr.ifr_name[ifnamelen] = '\0';
707         } else {
708                 PMD_LOG(ERR,
709                         "%s: I/F name too long (%s)",
710                         name, pair->value);
711                 goto free_internals;
712         }
713         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
714                 PMD_LOG_ERRNO(ERR, "%s: ioctl failed (SIOCGIFINDEX)", name);
715                 goto free_internals;
716         }
717         (*internals)->if_name = strdup(pair->value);
718         if ((*internals)->if_name == NULL)
719                 goto free_internals;
720         (*internals)->if_index = ifr.ifr_ifindex;
721
722         if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
723                 PMD_LOG_ERRNO(ERR, "%s: ioctl failed (SIOCGIFHWADDR)", name);
724                 goto free_internals;
725         }
726         memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
727
728         memset(&sockaddr, 0, sizeof(sockaddr));
729         sockaddr.sll_family = AF_PACKET;
730         sockaddr.sll_protocol = htons(ETH_P_ALL);
731         sockaddr.sll_ifindex = (*internals)->if_index;
732
733 #if defined(PACKET_FANOUT)
734         fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
735         fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
736 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
737         fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
738 #endif
739 #endif
740
741         for (q = 0; q < nb_queues; q++) {
742                 /* Open an AF_PACKET socket for this queue... */
743                 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
744                 if (qsockfd == -1) {
745                         PMD_LOG_ERRNO(ERR,
746                                 "%s: could not open AF_PACKET socket",
747                                 name);
748                         goto error;
749                 }
750
751                 tpver = TPACKET_V2;
752                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
753                                 &tpver, sizeof(tpver));
754                 if (rc == -1) {
755                         PMD_LOG_ERRNO(ERR,
756                                 "%s: could not set PACKET_VERSION on AF_PACKET socket for %s",
757                                 name, pair->value);
758                         goto error;
759                 }
760
761                 discard = 1;
762                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
763                                 &discard, sizeof(discard));
764                 if (rc == -1) {
765                         PMD_LOG_ERRNO(ERR,
766                                 "%s: could not set PACKET_LOSS on AF_PACKET socket for %s",
767                                 name, pair->value);
768                         goto error;
769                 }
770
771                 if (qdisc_bypass) {
772 #if defined(PACKET_QDISC_BYPASS)
773                         rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
774                                         &qdisc_bypass, sizeof(qdisc_bypass));
775                         if (rc == -1) {
776                                 PMD_LOG_ERRNO(ERR,
777                                         "%s: could not set PACKET_QDISC_BYPASS on AF_PACKET socket for %s",
778                                         name, pair->value);
779                                 goto error;
780                         }
781 #endif
782                 }
783
784                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
785                 if (rc == -1) {
786                         PMD_LOG_ERRNO(ERR,
787                                 "%s: could not set PACKET_RX_RING on AF_PACKET socket for %s",
788                                 name, pair->value);
789                         goto error;
790                 }
791
792                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
793                 if (rc == -1) {
794                         PMD_LOG_ERRNO(ERR,
795                                 "%s: could not set PACKET_TX_RING on AF_PACKET "
796                                 "socket for %s", name, pair->value);
797                         goto error;
798                 }
799
800                 rx_queue = &((*internals)->rx_queue[q]);
801                 rx_queue->framecount = req->tp_frame_nr;
802
803                 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
804                                     PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
805                                     qsockfd, 0);
806                 if (rx_queue->map == MAP_FAILED) {
807                         PMD_LOG_ERRNO(ERR,
808                                 "%s: call to mmap failed on AF_PACKET socket for %s",
809                                 name, pair->value);
810                         goto error;
811                 }
812
813                 /* rdsize is same for both Tx and Rx */
814                 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
815
816                 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
817                 if (rx_queue->rd == NULL)
818                         goto error;
819                 for (i = 0; i < req->tp_frame_nr; ++i) {
820                         rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
821                         rx_queue->rd[i].iov_len = req->tp_frame_size;
822                 }
823                 rx_queue->sockfd = qsockfd;
824
825                 tx_queue = &((*internals)->tx_queue[q]);
826                 tx_queue->framecount = req->tp_frame_nr;
827                 tx_queue->frame_data_size = req->tp_frame_size;
828                 tx_queue->frame_data_size -= TPACKET2_HDRLEN -
829                         sizeof(struct sockaddr_ll);
830
831                 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
832
833                 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
834                 if (tx_queue->rd == NULL)
835                         goto error;
836                 for (i = 0; i < req->tp_frame_nr; ++i) {
837                         tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
838                         tx_queue->rd[i].iov_len = req->tp_frame_size;
839                 }
840                 tx_queue->sockfd = qsockfd;
841
842                 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
843                 if (rc == -1) {
844                         PMD_LOG_ERRNO(ERR,
845                                 "%s: could not bind AF_PACKET socket to %s",
846                                 name, pair->value);
847                         goto error;
848                 }
849
850 #if defined(PACKET_FANOUT)
851                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
852                                 &fanout_arg, sizeof(fanout_arg));
853                 if (rc == -1) {
854                         PMD_LOG_ERRNO(ERR,
855                                 "%s: could not set PACKET_FANOUT on AF_PACKET socket for %s",
856                                 name, pair->value);
857                         goto error;
858                 }
859 #endif
860         }
861
862         /* reserve an ethdev entry */
863         *eth_dev = rte_eth_vdev_allocate(dev, 0);
864         if (*eth_dev == NULL)
865                 goto error;
866
867         /*
868          * now put it all together
869          * - store queue data in internals,
870          * - store numa_node in eth_dev
871          * - point eth_dev_data to internals
872          * - and point eth_dev structure to new eth_dev_data structure
873          */
874
875         (*internals)->nb_queues = nb_queues;
876
877         data = (*eth_dev)->data;
878         data->dev_private = *internals;
879         data->nb_rx_queues = (uint16_t)nb_queues;
880         data->nb_tx_queues = (uint16_t)nb_queues;
881         data->dev_link = pmd_link;
882         data->mac_addrs = &(*internals)->eth_addr;
883         data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
884
885         (*eth_dev)->dev_ops = &ops;
886
887         return 0;
888
889 error:
890         if (qsockfd != -1)
891                 close(qsockfd);
892         for (q = 0; q < nb_queues; q++) {
893                 if ((*internals)->rx_queue[q].map != MAP_FAILED)
894                         munmap((*internals)->rx_queue[q].map,
895                                2 * req->tp_block_size * req->tp_block_nr);
896
897                 rte_free((*internals)->rx_queue[q].rd);
898                 rte_free((*internals)->tx_queue[q].rd);
899                 if (((*internals)->rx_queue[q].sockfd >= 0) &&
900                         ((*internals)->rx_queue[q].sockfd != qsockfd))
901                         close((*internals)->rx_queue[q].sockfd);
902         }
903 free_internals:
904         rte_free((*internals)->rx_queue);
905         rte_free((*internals)->tx_queue);
906         free((*internals)->if_name);
907         rte_free(*internals);
908         return -1;
909 }
910
911 static int
912 rte_eth_from_packet(struct rte_vdev_device *dev,
913                     int const *sockfd,
914                     struct rte_kvargs *kvlist)
915 {
916         const char *name = rte_vdev_device_name(dev);
917         struct pmd_internals *internals = NULL;
918         struct rte_eth_dev *eth_dev = NULL;
919         struct rte_kvargs_pair *pair = NULL;
920         unsigned k_idx;
921         unsigned int blockcount;
922         unsigned int blocksize;
923         unsigned int framesize = DFLT_FRAME_SIZE;
924         unsigned int framecount = DFLT_FRAME_COUNT;
925         unsigned int qpairs = 1;
926         unsigned int qdisc_bypass = 1;
927
928         /* do some parameter checking */
929         if (*sockfd < 0)
930                 return -1;
931
932         blocksize = getpagesize();
933
934         /*
935          * Walk arguments for configurable settings
936          */
937         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
938                 pair = &kvlist->pairs[k_idx];
939                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
940                         qpairs = atoi(pair->value);
941                         if (qpairs < 1) {
942                                 PMD_LOG(ERR,
943                                         "%s: invalid qpairs value",
944                                         name);
945                                 return -1;
946                         }
947                         continue;
948                 }
949                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
950                         blocksize = atoi(pair->value);
951                         if (!blocksize) {
952                                 PMD_LOG(ERR,
953                                         "%s: invalid blocksize value",
954                                         name);
955                                 return -1;
956                         }
957                         continue;
958                 }
959                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
960                         framesize = atoi(pair->value);
961                         if (!framesize) {
962                                 PMD_LOG(ERR,
963                                         "%s: invalid framesize value",
964                                         name);
965                                 return -1;
966                         }
967                         continue;
968                 }
969                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
970                         framecount = atoi(pair->value);
971                         if (!framecount) {
972                                 PMD_LOG(ERR,
973                                         "%s: invalid framecount value",
974                                         name);
975                                 return -1;
976                         }
977                         continue;
978                 }
979                 if (strstr(pair->key, ETH_AF_PACKET_QDISC_BYPASS_ARG) != NULL) {
980                         qdisc_bypass = atoi(pair->value);
981                         if (qdisc_bypass > 1) {
982                                 PMD_LOG(ERR,
983                                         "%s: invalid bypass value",
984                                         name);
985                                 return -1;
986                         }
987                         continue;
988                 }
989         }
990
991         if (framesize > blocksize) {
992                 PMD_LOG(ERR,
993                         "%s: AF_PACKET MMAP frame size exceeds block size!",
994                         name);
995                 return -1;
996         }
997
998         blockcount = framecount / (blocksize / framesize);
999         if (!blockcount) {
1000                 PMD_LOG(ERR,
1001                         "%s: invalid AF_PACKET MMAP parameters", name);
1002                 return -1;
1003         }
1004
1005         PMD_LOG(INFO, "%s: AF_PACKET MMAP parameters:", name);
1006         PMD_LOG(INFO, "%s:\tblock size %d", name, blocksize);
1007         PMD_LOG(INFO, "%s:\tblock count %d", name, blockcount);
1008         PMD_LOG(INFO, "%s:\tframe size %d", name, framesize);
1009         PMD_LOG(INFO, "%s:\tframe count %d", name, framecount);
1010
1011         if (rte_pmd_init_internals(dev, *sockfd, qpairs,
1012                                    blocksize, blockcount,
1013                                    framesize, framecount,
1014                                    qdisc_bypass,
1015                                    &internals, &eth_dev,
1016                                    kvlist) < 0)
1017                 return -1;
1018
1019         eth_dev->rx_pkt_burst = eth_af_packet_rx;
1020         eth_dev->tx_pkt_burst = eth_af_packet_tx;
1021
1022         rte_eth_dev_probing_finish(eth_dev);
1023         return 0;
1024 }
1025
1026 static int
1027 rte_pmd_af_packet_probe(struct rte_vdev_device *dev)
1028 {
1029         int ret = 0;
1030         struct rte_kvargs *kvlist;
1031         int sockfd = -1;
1032         struct rte_eth_dev *eth_dev;
1033         const char *name = rte_vdev_device_name(dev);
1034
1035         PMD_LOG(INFO, "Initializing pmd_af_packet for %s", name);
1036
1037         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1038                 eth_dev = rte_eth_dev_attach_secondary(name);
1039                 if (!eth_dev) {
1040                         PMD_LOG(ERR, "Failed to probe %s", name);
1041                         return -1;
1042                 }
1043                 /* TODO: request info from primary to set up Rx and Tx */
1044                 eth_dev->dev_ops = &ops;
1045                 eth_dev->device = &dev->device;
1046                 rte_eth_dev_probing_finish(eth_dev);
1047                 return 0;
1048         }
1049
1050         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1051         if (kvlist == NULL) {
1052                 ret = -1;
1053                 goto exit;
1054         }
1055
1056         /*
1057          * If iface argument is passed we open the NICs and use them for
1058          * reading / writing
1059          */
1060         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
1061
1062                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
1063                                          &open_packet_iface, &sockfd);
1064                 if (ret < 0)
1065                         goto exit;
1066         }
1067
1068         if (dev->device.numa_node == SOCKET_ID_ANY)
1069                 dev->device.numa_node = rte_socket_id();
1070
1071         ret = rte_eth_from_packet(dev, &sockfd, kvlist);
1072         close(sockfd); /* no longer needed */
1073
1074 exit:
1075         rte_kvargs_free(kvlist);
1076         return ret;
1077 }
1078
1079 static int
1080 rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
1081 {
1082         struct rte_eth_dev *eth_dev;
1083
1084         if (dev == NULL)
1085                 return -1;
1086
1087         /* find the ethdev entry */
1088         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1089         if (eth_dev == NULL)
1090                 return 0; /* port already released */
1091
1092         eth_dev_close(eth_dev);
1093         rte_eth_dev_release_port(eth_dev);
1094
1095         return 0;
1096 }
1097
1098 static struct rte_vdev_driver pmd_af_packet_drv = {
1099         .probe = rte_pmd_af_packet_probe,
1100         .remove = rte_pmd_af_packet_remove,
1101 };
1102
1103 RTE_PMD_REGISTER_VDEV(net_af_packet, pmd_af_packet_drv);
1104 RTE_PMD_REGISTER_ALIAS(net_af_packet, eth_af_packet);
1105 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
1106         "iface=<string> "
1107         "qpairs=<int> "
1108         "blocksz=<int> "
1109         "framesz=<int> "
1110         "framecnt=<int> "
1111         "qdisc_bypass=<0|1>");