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