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