net/af_packet: fix fd use after free
[dpdk.git] / drivers / net / af_packet / rte_eth_af_packet.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014 John W. Linville <linville@tuxdriver.com>
5  *
6  *   Originally based upon librte_pmd_pcap code:
7  *
8  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
9  *   Copyright(c) 2014 6WIND S.A.
10  *   All rights reserved.
11  *
12  *   Redistribution and use in source and binary forms, with or without
13  *   modification, are permitted provided that the following conditions
14  *   are met:
15  *
16  *     * Redistributions of source code must retain the above copyright
17  *       notice, this list of conditions and the following disclaimer.
18  *     * Redistributions in binary form must reproduce the above copyright
19  *       notice, this list of conditions and the following disclaimer in
20  *       the documentation and/or other materials provided with the
21  *       distribution.
22  *     * Neither the name of Intel Corporation nor the names of its
23  *       contributors may be used to endorse or promote products derived
24  *       from this software without specific prior written permission.
25  *
26  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include <rte_mbuf.h>
40 #include <rte_ethdev.h>
41 #include <rte_malloc.h>
42 #include <rte_kvargs.h>
43 #include <rte_vdev.h>
44
45 #include <linux/if_ether.h>
46 #include <linux/if_packet.h>
47 #include <arpa/inet.h>
48 #include <net/if.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/mman.h>
53 #include <unistd.h>
54 #include <poll.h>
55
56 #define ETH_AF_PACKET_IFACE_ARG         "iface"
57 #define ETH_AF_PACKET_NUM_Q_ARG         "qpairs"
58 #define ETH_AF_PACKET_BLOCKSIZE_ARG     "blocksz"
59 #define ETH_AF_PACKET_FRAMESIZE_ARG     "framesz"
60 #define ETH_AF_PACKET_FRAMECOUNT_ARG    "framecnt"
61
62 #define DFLT_BLOCK_SIZE         (1 << 12)
63 #define DFLT_FRAME_SIZE         (1 << 11)
64 #define DFLT_FRAME_COUNT        (1 << 9)
65
66 #define RTE_PMD_AF_PACKET_MAX_RINGS 16
67
68 struct pkt_rx_queue {
69         int sockfd;
70
71         struct iovec *rd;
72         uint8_t *map;
73         unsigned int framecount;
74         unsigned int framenum;
75
76         struct rte_mempool *mb_pool;
77         uint8_t in_port;
78
79         volatile unsigned long rx_pkts;
80         volatile unsigned long err_pkts;
81         volatile unsigned long rx_bytes;
82 };
83
84 struct pkt_tx_queue {
85         int sockfd;
86         unsigned int frame_data_size;
87
88         struct iovec *rd;
89         uint8_t *map;
90         unsigned int framecount;
91         unsigned int framenum;
92
93         volatile unsigned long tx_pkts;
94         volatile unsigned long err_pkts;
95         volatile unsigned long tx_bytes;
96 };
97
98 struct pmd_internals {
99         unsigned nb_queues;
100
101         int if_index;
102         struct ether_addr eth_addr;
103
104         struct tpacket_req req;
105
106         struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
107         struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
108 };
109
110 static const char *valid_arguments[] = {
111         ETH_AF_PACKET_IFACE_ARG,
112         ETH_AF_PACKET_NUM_Q_ARG,
113         ETH_AF_PACKET_BLOCKSIZE_ARG,
114         ETH_AF_PACKET_FRAMESIZE_ARG,
115         ETH_AF_PACKET_FRAMECOUNT_ARG,
116         NULL
117 };
118
119 static struct rte_eth_link pmd_link = {
120         .link_speed = ETH_SPEED_NUM_10G,
121         .link_duplex = ETH_LINK_FULL_DUPLEX,
122         .link_status = ETH_LINK_DOWN,
123         .link_autoneg = ETH_LINK_SPEED_AUTONEG
124 };
125
126 static uint16_t
127 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
128 {
129         unsigned i;
130         struct tpacket2_hdr *ppd;
131         struct rte_mbuf *mbuf;
132         uint8_t *pbuf;
133         struct pkt_rx_queue *pkt_q = queue;
134         uint16_t num_rx = 0;
135         unsigned long num_rx_bytes = 0;
136         unsigned int framecount, framenum;
137
138         if (unlikely(nb_pkts == 0))
139                 return 0;
140
141         /*
142          * Reads the given number of packets from the AF_PACKET socket one by
143          * one and copies the packet data into a newly allocated mbuf.
144          */
145         framecount = pkt_q->framecount;
146         framenum = pkt_q->framenum;
147         for (i = 0; i < nb_pkts; i++) {
148                 /* point at the next incoming frame */
149                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
150                 if ((ppd->tp_status & TP_STATUS_USER) == 0)
151                         break;
152
153                 /* allocate the next mbuf */
154                 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool);
155                 if (unlikely(mbuf == NULL))
156                         break;
157
158                 /* packet will fit in the mbuf, go ahead and receive it */
159                 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf) = ppd->tp_snaplen;
160                 pbuf = (uint8_t *) ppd + ppd->tp_mac;
161                 memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf));
162
163                 /* release incoming frame and advance ring buffer */
164                 ppd->tp_status = TP_STATUS_KERNEL;
165                 if (++framenum >= framecount)
166                         framenum = 0;
167                 mbuf->port = pkt_q->in_port;
168
169                 /* account for the receive frame */
170                 bufs[i] = mbuf;
171                 num_rx++;
172                 num_rx_bytes += mbuf->pkt_len;
173         }
174         pkt_q->framenum = framenum;
175         pkt_q->rx_pkts += num_rx;
176         pkt_q->rx_bytes += num_rx_bytes;
177         return num_rx;
178 }
179
180 /*
181  * Callback to handle sending packets through a real NIC.
182  */
183 static uint16_t
184 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
185 {
186         struct tpacket2_hdr *ppd;
187         struct rte_mbuf *mbuf;
188         uint8_t *pbuf;
189         unsigned int framecount, framenum;
190         struct pollfd pfd;
191         struct pkt_tx_queue *pkt_q = queue;
192         uint16_t num_tx = 0;
193         unsigned long num_tx_bytes = 0;
194         int i;
195
196         if (unlikely(nb_pkts == 0))
197                 return 0;
198
199         memset(&pfd, 0, sizeof(pfd));
200         pfd.fd = pkt_q->sockfd;
201         pfd.events = POLLOUT;
202         pfd.revents = 0;
203
204         framecount = pkt_q->framecount;
205         framenum = pkt_q->framenum;
206         ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
207         for (i = 0; i < nb_pkts; i++) {
208                 mbuf = *bufs++;
209
210                 /* drop oversized packets */
211                 if (rte_pktmbuf_data_len(mbuf) > pkt_q->frame_data_size) {
212                         rte_pktmbuf_free(mbuf);
213                         continue;
214                 }
215
216                 /* point at the next incoming frame */
217                 if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
218                     (poll(&pfd, 1, -1) < 0))
219                         break;
220
221                 /* copy the tx frame data */
222                 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
223                         sizeof(struct sockaddr_ll);
224                 memcpy(pbuf, rte_pktmbuf_mtod(mbuf, void*), rte_pktmbuf_data_len(mbuf));
225                 ppd->tp_len = ppd->tp_snaplen = rte_pktmbuf_data_len(mbuf);
226
227                 /* release incoming frame and advance ring buffer */
228                 ppd->tp_status = TP_STATUS_SEND_REQUEST;
229                 if (++framenum >= framecount)
230                         framenum = 0;
231                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
232
233                 num_tx++;
234                 num_tx_bytes += mbuf->pkt_len;
235                 rte_pktmbuf_free(mbuf);
236         }
237
238         /* kick-off transmits */
239         if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1)
240                 num_tx = 0; /* error sending -- no packets transmitted */
241
242         pkt_q->framenum = framenum;
243         pkt_q->tx_pkts += num_tx;
244         pkt_q->err_pkts += i - num_tx;
245         pkt_q->tx_bytes += num_tx_bytes;
246         return i;
247 }
248
249 static int
250 eth_dev_start(struct rte_eth_dev *dev)
251 {
252         dev->data->dev_link.link_status = ETH_LINK_UP;
253         return 0;
254 }
255
256 /*
257  * This function gets called when the current port gets stopped.
258  */
259 static void
260 eth_dev_stop(struct rte_eth_dev *dev)
261 {
262         unsigned i;
263         int sockfd;
264         struct pmd_internals *internals = dev->data->dev_private;
265
266         for (i = 0; i < internals->nb_queues; i++) {
267                 sockfd = internals->rx_queue[i].sockfd;
268                 if (sockfd != -1)
269                         close(sockfd);
270
271                 /* Prevent use after free in case tx fd == rx fd */
272                 if (sockfd != internals->tx_queue[i].sockfd) {
273                         sockfd = internals->tx_queue[i].sockfd;
274                         if (sockfd != -1)
275                                 close(sockfd);
276                 }
277
278                 internals->rx_queue[i].sockfd = -1;
279                 internals->tx_queue[i].sockfd = -1;
280         }
281
282         dev->data->dev_link.link_status = ETH_LINK_DOWN;
283 }
284
285 static int
286 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
287 {
288         return 0;
289 }
290
291 static void
292 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
293 {
294         struct pmd_internals *internals = dev->data->dev_private;
295
296         dev_info->if_index = internals->if_index;
297         dev_info->max_mac_addrs = 1;
298         dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
299         dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
300         dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
301         dev_info->min_rx_bufsize = 0;
302 }
303
304 static void
305 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
306 {
307         unsigned i, imax;
308         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
309         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
310         const struct pmd_internals *internal = dev->data->dev_private;
311
312         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
313                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
314         for (i = 0; i < imax; i++) {
315                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
316                 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
317                 rx_total += igb_stats->q_ipackets[i];
318                 rx_bytes_total += igb_stats->q_ibytes[i];
319         }
320
321         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
322                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
323         for (i = 0; i < imax; i++) {
324                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
325                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
326                 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
327                 tx_total += igb_stats->q_opackets[i];
328                 tx_err_total += igb_stats->q_errors[i];
329                 tx_bytes_total += igb_stats->q_obytes[i];
330         }
331
332         igb_stats->ipackets = rx_total;
333         igb_stats->ibytes = rx_bytes_total;
334         igb_stats->opackets = tx_total;
335         igb_stats->oerrors = tx_err_total;
336         igb_stats->obytes = tx_bytes_total;
337 }
338
339 static void
340 eth_stats_reset(struct rte_eth_dev *dev)
341 {
342         unsigned i;
343         struct pmd_internals *internal = dev->data->dev_private;
344
345         for (i = 0; i < internal->nb_queues; i++) {
346                 internal->rx_queue[i].rx_pkts = 0;
347                 internal->rx_queue[i].rx_bytes = 0;
348         }
349
350         for (i = 0; i < internal->nb_queues; i++) {
351                 internal->tx_queue[i].tx_pkts = 0;
352                 internal->tx_queue[i].err_pkts = 0;
353                 internal->tx_queue[i].tx_bytes = 0;
354         }
355 }
356
357 static void
358 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
359 {
360 }
361
362 static void
363 eth_queue_release(void *q __rte_unused)
364 {
365 }
366
367 static int
368 eth_link_update(struct rte_eth_dev *dev __rte_unused,
369                 int wait_to_complete __rte_unused)
370 {
371         return 0;
372 }
373
374 static int
375 eth_rx_queue_setup(struct rte_eth_dev *dev,
376                    uint16_t rx_queue_id,
377                    uint16_t nb_rx_desc __rte_unused,
378                    unsigned int socket_id __rte_unused,
379                    const struct rte_eth_rxconf *rx_conf __rte_unused,
380                    struct rte_mempool *mb_pool)
381 {
382         struct pmd_internals *internals = dev->data->dev_private;
383         struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
384         unsigned int buf_size, data_size;
385
386         pkt_q->mb_pool = mb_pool;
387
388         /* Now get the space available for data in the mbuf */
389         buf_size = rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
390                 RTE_PKTMBUF_HEADROOM;
391         data_size = internals->req.tp_frame_size;
392         data_size -= TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
393
394         if (data_size > buf_size) {
395                 RTE_LOG(ERR, PMD,
396                         "%s: %d bytes will not fit in mbuf (%d bytes)\n",
397                         dev->data->name, data_size, buf_size);
398                 return -ENOMEM;
399         }
400
401         dev->data->rx_queues[rx_queue_id] = pkt_q;
402         pkt_q->in_port = dev->data->port_id;
403
404         return 0;
405 }
406
407 static int
408 eth_tx_queue_setup(struct rte_eth_dev *dev,
409                    uint16_t tx_queue_id,
410                    uint16_t nb_tx_desc __rte_unused,
411                    unsigned int socket_id __rte_unused,
412                    const struct rte_eth_txconf *tx_conf __rte_unused)
413 {
414
415         struct pmd_internals *internals = dev->data->dev_private;
416
417         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
418         return 0;
419 }
420
421 static const struct eth_dev_ops ops = {
422         .dev_start = eth_dev_start,
423         .dev_stop = eth_dev_stop,
424         .dev_close = eth_dev_close,
425         .dev_configure = eth_dev_configure,
426         .dev_infos_get = eth_dev_info,
427         .rx_queue_setup = eth_rx_queue_setup,
428         .tx_queue_setup = eth_tx_queue_setup,
429         .rx_queue_release = eth_queue_release,
430         .tx_queue_release = eth_queue_release,
431         .link_update = eth_link_update,
432         .stats_get = eth_stats_get,
433         .stats_reset = eth_stats_reset,
434 };
435
436 /*
437  * Opens an AF_PACKET socket
438  */
439 static int
440 open_packet_iface(const char *key __rte_unused,
441                   const char *value __rte_unused,
442                   void *extra_args)
443 {
444         int *sockfd = extra_args;
445
446         /* Open an AF_PACKET socket... */
447         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
448         if (*sockfd == -1) {
449                 RTE_LOG(ERR, PMD, "Could not open AF_PACKET socket\n");
450                 return -1;
451         }
452
453         return 0;
454 }
455
456 static struct rte_vdev_driver pmd_af_packet_drv;
457
458 static int
459 rte_pmd_init_internals(const char *name,
460                        const int sockfd,
461                        const unsigned nb_queues,
462                        unsigned int blocksize,
463                        unsigned int blockcnt,
464                        unsigned int framesize,
465                        unsigned int framecnt,
466                        const unsigned numa_node,
467                        struct pmd_internals **internals,
468                        struct rte_eth_dev **eth_dev,
469                        struct rte_kvargs *kvlist)
470 {
471         struct rte_eth_dev_data *data = NULL;
472         struct rte_kvargs_pair *pair = NULL;
473         struct ifreq ifr;
474         size_t ifnamelen;
475         unsigned k_idx;
476         struct sockaddr_ll sockaddr;
477         struct tpacket_req *req;
478         struct pkt_rx_queue *rx_queue;
479         struct pkt_tx_queue *tx_queue;
480         int rc, tpver, discard;
481         int qsockfd = -1;
482         unsigned int i, q, rdsize;
483         int fanout_arg __rte_unused, bypass __rte_unused;
484
485         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
486                 pair = &kvlist->pairs[k_idx];
487                 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
488                         break;
489         }
490         if (pair == NULL) {
491                 RTE_LOG(ERR, PMD,
492                         "%s: no interface specified for AF_PACKET ethdev\n",
493                         name);
494                 goto error_early;
495         }
496
497         RTE_LOG(INFO, PMD,
498                 "%s: creating AF_PACKET-backed ethdev on numa socket %u\n",
499                 name, numa_node);
500
501         /*
502          * now do all data allocation - for eth_dev structure, dummy pci driver
503          * and internal (private) data
504          */
505         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
506         if (data == NULL)
507                 goto error_early;
508
509         *internals = rte_zmalloc_socket(name, sizeof(**internals),
510                                         0, numa_node);
511         if (*internals == NULL)
512                 goto error_early;
513
514         for (q = 0; q < nb_queues; q++) {
515                 (*internals)->rx_queue[q].map = MAP_FAILED;
516                 (*internals)->tx_queue[q].map = MAP_FAILED;
517         }
518
519         req = &((*internals)->req);
520
521         req->tp_block_size = blocksize;
522         req->tp_block_nr = blockcnt;
523         req->tp_frame_size = framesize;
524         req->tp_frame_nr = framecnt;
525
526         ifnamelen = strlen(pair->value);
527         if (ifnamelen < sizeof(ifr.ifr_name)) {
528                 memcpy(ifr.ifr_name, pair->value, ifnamelen);
529                 ifr.ifr_name[ifnamelen] = '\0';
530         } else {
531                 RTE_LOG(ERR, PMD,
532                         "%s: I/F name too long (%s)\n",
533                         name, pair->value);
534                 goto error_early;
535         }
536         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
537                 RTE_LOG(ERR, PMD,
538                         "%s: ioctl failed (SIOCGIFINDEX)\n",
539                         name);
540                 goto error_early;
541         }
542         (*internals)->if_index = ifr.ifr_ifindex;
543
544         if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
545                 RTE_LOG(ERR, PMD,
546                         "%s: ioctl failed (SIOCGIFHWADDR)\n",
547                         name);
548                 goto error_early;
549         }
550         memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
551
552         memset(&sockaddr, 0, sizeof(sockaddr));
553         sockaddr.sll_family = AF_PACKET;
554         sockaddr.sll_protocol = htons(ETH_P_ALL);
555         sockaddr.sll_ifindex = (*internals)->if_index;
556
557 #if defined(PACKET_FANOUT)
558         fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
559         fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
560 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
561         fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
562 #endif
563 #endif
564
565         for (q = 0; q < nb_queues; q++) {
566                 /* Open an AF_PACKET socket for this queue... */
567                 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
568                 if (qsockfd == -1) {
569                         RTE_LOG(ERR, PMD,
570                                 "%s: could not open AF_PACKET socket\n",
571                                 name);
572                         return -1;
573                 }
574
575                 tpver = TPACKET_V2;
576                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
577                                 &tpver, sizeof(tpver));
578                 if (rc == -1) {
579                         RTE_LOG(ERR, PMD,
580                                 "%s: could not set PACKET_VERSION on AF_PACKET "
581                                 "socket for %s\n", name, pair->value);
582                         goto error;
583                 }
584
585                 discard = 1;
586                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
587                                 &discard, sizeof(discard));
588                 if (rc == -1) {
589                         RTE_LOG(ERR, PMD,
590                                 "%s: could not set PACKET_LOSS on "
591                                 "AF_PACKET socket for %s\n", name, pair->value);
592                         goto error;
593                 }
594
595 #if defined(PACKET_QDISC_BYPASS)
596                 bypass = 1;
597                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
598                                 &bypass, sizeof(bypass));
599                 if (rc == -1) {
600                         RTE_LOG(ERR, PMD,
601                                 "%s: could not set PACKET_QDISC_BYPASS "
602                                 "on AF_PACKET socket for %s\n", name,
603                                 pair->value);
604                         goto error;
605                 }
606 #endif
607
608                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
609                 if (rc == -1) {
610                         RTE_LOG(ERR, PMD,
611                                 "%s: could not set PACKET_RX_RING on AF_PACKET "
612                                 "socket for %s\n", name, pair->value);
613                         goto error;
614                 }
615
616                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
617                 if (rc == -1) {
618                         RTE_LOG(ERR, PMD,
619                                 "%s: could not set PACKET_TX_RING on AF_PACKET "
620                                 "socket for %s\n", name, pair->value);
621                         goto error;
622                 }
623
624                 rx_queue = &((*internals)->rx_queue[q]);
625                 rx_queue->framecount = req->tp_frame_nr;
626
627                 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
628                                     PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
629                                     qsockfd, 0);
630                 if (rx_queue->map == MAP_FAILED) {
631                         RTE_LOG(ERR, PMD,
632                                 "%s: call to mmap failed on AF_PACKET socket for %s\n",
633                                 name, pair->value);
634                         goto error;
635                 }
636
637                 /* rdsize is same for both Tx and Rx */
638                 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
639
640                 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
641                 if (rx_queue->rd == NULL)
642                         goto error;
643                 for (i = 0; i < req->tp_frame_nr; ++i) {
644                         rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
645                         rx_queue->rd[i].iov_len = req->tp_frame_size;
646                 }
647                 rx_queue->sockfd = qsockfd;
648
649                 tx_queue = &((*internals)->tx_queue[q]);
650                 tx_queue->framecount = req->tp_frame_nr;
651                 tx_queue->frame_data_size = req->tp_frame_size;
652                 tx_queue->frame_data_size -= TPACKET2_HDRLEN -
653                         sizeof(struct sockaddr_ll);
654
655                 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
656
657                 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
658                 if (tx_queue->rd == NULL)
659                         goto error;
660                 for (i = 0; i < req->tp_frame_nr; ++i) {
661                         tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
662                         tx_queue->rd[i].iov_len = req->tp_frame_size;
663                 }
664                 tx_queue->sockfd = qsockfd;
665
666                 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
667                 if (rc == -1) {
668                         RTE_LOG(ERR, PMD,
669                                 "%s: could not bind AF_PACKET socket to %s\n",
670                                 name, pair->value);
671                         goto error;
672                 }
673
674 #if defined(PACKET_FANOUT)
675                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
676                                 &fanout_arg, sizeof(fanout_arg));
677                 if (rc == -1) {
678                         RTE_LOG(ERR, PMD,
679                                 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
680                                 "for %s\n", name, pair->value);
681                         goto error;
682                 }
683 #endif
684         }
685
686         /* reserve an ethdev entry */
687         *eth_dev = rte_eth_dev_allocate(name);
688         if (*eth_dev == NULL)
689                 goto error;
690
691         /*
692          * now put it all together
693          * - store queue data in internals,
694          * - store numa_node in eth_dev
695          * - point eth_dev_data to internals
696          * - and point eth_dev structure to new eth_dev_data structure
697          */
698
699         (*internals)->nb_queues = nb_queues;
700
701         data->dev_private = *internals;
702         data->port_id = (*eth_dev)->data->port_id;
703         data->nb_rx_queues = (uint16_t)nb_queues;
704         data->nb_tx_queues = (uint16_t)nb_queues;
705         data->dev_link = pmd_link;
706         data->mac_addrs = &(*internals)->eth_addr;
707         strncpy(data->name,
708                 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
709
710         (*eth_dev)->data = data;
711         (*eth_dev)->dev_ops = &ops;
712         (*eth_dev)->driver = NULL;
713         (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
714         (*eth_dev)->data->drv_name = pmd_af_packet_drv.driver.name;
715         (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
716         (*eth_dev)->data->numa_node = numa_node;
717
718         return 0;
719
720 error:
721         if (qsockfd != -1)
722                 close(qsockfd);
723         for (q = 0; q < nb_queues; q++) {
724                 munmap((*internals)->rx_queue[q].map,
725                        2 * req->tp_block_size * req->tp_block_nr);
726
727                 rte_free((*internals)->rx_queue[q].rd);
728                 rte_free((*internals)->tx_queue[q].rd);
729                 if (((*internals)->rx_queue[q].sockfd != 0) &&
730                         ((*internals)->rx_queue[q].sockfd != qsockfd))
731                         close((*internals)->rx_queue[q].sockfd);
732         }
733         rte_free(*internals);
734 error_early:
735         rte_free(data);
736         return -1;
737 }
738
739 static int
740 rte_eth_from_packet(const char *name,
741                     int const *sockfd,
742                     const unsigned numa_node,
743                     struct rte_kvargs *kvlist)
744 {
745         struct pmd_internals *internals = NULL;
746         struct rte_eth_dev *eth_dev = NULL;
747         struct rte_kvargs_pair *pair = NULL;
748         unsigned k_idx;
749         unsigned int blockcount;
750         unsigned int blocksize = DFLT_BLOCK_SIZE;
751         unsigned int framesize = DFLT_FRAME_SIZE;
752         unsigned int framecount = DFLT_FRAME_COUNT;
753         unsigned int qpairs = 1;
754
755         /* do some parameter checking */
756         if (*sockfd < 0)
757                 return -1;
758
759         /*
760          * Walk arguments for configurable settings
761          */
762         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
763                 pair = &kvlist->pairs[k_idx];
764                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
765                         qpairs = atoi(pair->value);
766                         if (qpairs < 1 ||
767                             qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
768                                 RTE_LOG(ERR, PMD,
769                                         "%s: invalid qpairs value\n",
770                                         name);
771                                 return -1;
772                         }
773                         continue;
774                 }
775                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
776                         blocksize = atoi(pair->value);
777                         if (!blocksize) {
778                                 RTE_LOG(ERR, PMD,
779                                         "%s: invalid blocksize value\n",
780                                         name);
781                                 return -1;
782                         }
783                         continue;
784                 }
785                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
786                         framesize = atoi(pair->value);
787                         if (!framesize) {
788                                 RTE_LOG(ERR, PMD,
789                                         "%s: invalid framesize value\n",
790                                         name);
791                                 return -1;
792                         }
793                         continue;
794                 }
795                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
796                         framecount = atoi(pair->value);
797                         if (!framecount) {
798                                 RTE_LOG(ERR, PMD,
799                                         "%s: invalid framecount value\n",
800                                         name);
801                                 return -1;
802                         }
803                         continue;
804                 }
805         }
806
807         if (framesize > blocksize) {
808                 RTE_LOG(ERR, PMD,
809                         "%s: AF_PACKET MMAP frame size exceeds block size!\n",
810                         name);
811                 return -1;
812         }
813
814         blockcount = framecount / (blocksize / framesize);
815         if (!blockcount) {
816                 RTE_LOG(ERR, PMD,
817                         "%s: invalid AF_PACKET MMAP parameters\n", name);
818                 return -1;
819         }
820
821         RTE_LOG(INFO, PMD, "%s: AF_PACKET MMAP parameters:\n", name);
822         RTE_LOG(INFO, PMD, "%s:\tblock size %d\n", name, blocksize);
823         RTE_LOG(INFO, PMD, "%s:\tblock count %d\n", name, blockcount);
824         RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize);
825         RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount);
826
827         if (rte_pmd_init_internals(name, *sockfd, qpairs,
828                                    blocksize, blockcount,
829                                    framesize, framecount,
830                                    numa_node, &internals, &eth_dev,
831                                    kvlist) < 0)
832                 return -1;
833
834         eth_dev->rx_pkt_burst = eth_af_packet_rx;
835         eth_dev->tx_pkt_burst = eth_af_packet_tx;
836
837         return 0;
838 }
839
840 static int
841 rte_pmd_af_packet_probe(const char *name, const char *params)
842 {
843         unsigned numa_node;
844         int ret = 0;
845         struct rte_kvargs *kvlist;
846         int sockfd = -1;
847
848         RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name);
849
850         numa_node = rte_socket_id();
851
852         kvlist = rte_kvargs_parse(params, valid_arguments);
853         if (kvlist == NULL) {
854                 ret = -1;
855                 goto exit;
856         }
857
858         /*
859          * If iface argument is passed we open the NICs and use them for
860          * reading / writing
861          */
862         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
863
864                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
865                                          &open_packet_iface, &sockfd);
866                 if (ret < 0)
867                         goto exit;
868         }
869
870         ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
871         close(sockfd); /* no longer needed */
872
873 exit:
874         rte_kvargs_free(kvlist);
875         return ret;
876 }
877
878 static int
879 rte_pmd_af_packet_remove(const char *name)
880 {
881         struct rte_eth_dev *eth_dev = NULL;
882         struct pmd_internals *internals;
883         unsigned q;
884
885         RTE_LOG(INFO, PMD, "Closing AF_PACKET ethdev on numa socket %u\n",
886                         rte_socket_id());
887
888         if (name == NULL)
889                 return -1;
890
891         /* find the ethdev entry */
892         eth_dev = rte_eth_dev_allocated(name);
893         if (eth_dev == NULL)
894                 return -1;
895
896         internals = eth_dev->data->dev_private;
897         for (q = 0; q < internals->nb_queues; q++) {
898                 rte_free(internals->rx_queue[q].rd);
899                 rte_free(internals->tx_queue[q].rd);
900         }
901
902         rte_free(eth_dev->data->dev_private);
903         rte_free(eth_dev->data);
904
905         rte_eth_dev_release_port(eth_dev);
906
907         return 0;
908 }
909
910 static struct rte_vdev_driver pmd_af_packet_drv = {
911         .probe = rte_pmd_af_packet_probe,
912         .remove = rte_pmd_af_packet_remove,
913 };
914
915 RTE_PMD_REGISTER_VDEV(net_af_packet, pmd_af_packet_drv);
916 RTE_PMD_REGISTER_ALIAS(net_af_packet, eth_af_packet);
917 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
918         "iface=<string> "
919         "qpairs=<int> "
920         "blocksz=<int> "
921         "framesz=<int> "
922         "framecnt=<int>");