af_packet: remove fake pci interface
[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_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         if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1)
224                 return 0; /* error sending -- no packets transmitted */
225
226         pkt_q->framenum = framenum;
227         pkt_q->tx_pkts += num_tx;
228         pkt_q->err_pkts += nb_pkts - num_tx;
229         return num_tx;
230 }
231
232 static int
233 eth_dev_start(struct rte_eth_dev *dev)
234 {
235         dev->data->dev_link.link_status = 1;
236         return 0;
237 }
238
239 /*
240  * This function gets called when the current port gets stopped.
241  */
242 static void
243 eth_dev_stop(struct rte_eth_dev *dev)
244 {
245         unsigned i;
246         int sockfd;
247         struct pmd_internals *internals = dev->data->dev_private;
248
249         for (i = 0; i < internals->nb_queues; i++) {
250                 sockfd = internals->rx_queue[i].sockfd;
251                 if (sockfd != -1)
252                         close(sockfd);
253                 sockfd = internals->tx_queue[i].sockfd;
254                 if (sockfd != -1)
255                         close(sockfd);
256         }
257
258         dev->data->dev_link.link_status = 0;
259 }
260
261 static int
262 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
263 {
264         return 0;
265 }
266
267 static void
268 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
269 {
270         struct pmd_internals *internals = dev->data->dev_private;
271
272         dev_info->driver_name = drivername;
273         dev_info->if_index = internals->if_index;
274         dev_info->max_mac_addrs = 1;
275         dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
276         dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
277         dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
278         dev_info->min_rx_bufsize = 0;
279         dev_info->pci_dev = NULL;
280 }
281
282 static void
283 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
284 {
285         unsigned i, imax;
286         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
287         const struct pmd_internals *internal = dev->data->dev_private;
288
289         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
290                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
291         for (i = 0; i < imax; i++) {
292                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
293                 rx_total += igb_stats->q_ipackets[i];
294         }
295
296         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
297                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
298         for (i = 0; i < imax; i++) {
299                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
300                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
301                 tx_total += igb_stats->q_opackets[i];
302                 tx_err_total += igb_stats->q_errors[i];
303         }
304
305         igb_stats->ipackets = rx_total;
306         igb_stats->opackets = tx_total;
307         igb_stats->oerrors = tx_err_total;
308 }
309
310 static void
311 eth_stats_reset(struct rte_eth_dev *dev)
312 {
313         unsigned i;
314         struct pmd_internals *internal = dev->data->dev_private;
315
316         for (i = 0; i < internal->nb_queues; i++)
317                 internal->rx_queue[i].rx_pkts = 0;
318
319         for (i = 0; i < internal->nb_queues; i++) {
320                 internal->tx_queue[i].tx_pkts = 0;
321                 internal->tx_queue[i].err_pkts = 0;
322         }
323 }
324
325 static void
326 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
327 {
328 }
329
330 static void
331 eth_queue_release(void *q __rte_unused)
332 {
333 }
334
335 static int
336 eth_link_update(struct rte_eth_dev *dev __rte_unused,
337                 int wait_to_complete __rte_unused)
338 {
339         return 0;
340 }
341
342 static int
343 eth_rx_queue_setup(struct rte_eth_dev *dev,
344                    uint16_t rx_queue_id,
345                    uint16_t nb_rx_desc __rte_unused,
346                    unsigned int socket_id __rte_unused,
347                    const struct rte_eth_rxconf *rx_conf __rte_unused,
348                    struct rte_mempool *mb_pool)
349 {
350         struct pmd_internals *internals = dev->data->dev_private;
351         struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
352         uint16_t buf_size;
353
354         pkt_q->mb_pool = mb_pool;
355
356         /* Now get the space available for data in the mbuf */
357         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
358                 RTE_PKTMBUF_HEADROOM);
359
360         if (ETH_FRAME_LEN > buf_size) {
361                 RTE_LOG(ERR, PMD,
362                         "%s: %d bytes will not fit in mbuf (%d bytes)\n",
363                         dev->data->name, ETH_FRAME_LEN, buf_size);
364                 return -ENOMEM;
365         }
366
367         dev->data->rx_queues[rx_queue_id] = pkt_q;
368
369         return 0;
370 }
371
372 static int
373 eth_tx_queue_setup(struct rte_eth_dev *dev,
374                    uint16_t tx_queue_id,
375                    uint16_t nb_tx_desc __rte_unused,
376                    unsigned int socket_id __rte_unused,
377                    const struct rte_eth_txconf *tx_conf __rte_unused)
378 {
379
380         struct pmd_internals *internals = dev->data->dev_private;
381
382         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
383         return 0;
384 }
385
386 static const struct eth_dev_ops ops = {
387         .dev_start = eth_dev_start,
388         .dev_stop = eth_dev_stop,
389         .dev_close = eth_dev_close,
390         .dev_configure = eth_dev_configure,
391         .dev_infos_get = eth_dev_info,
392         .rx_queue_setup = eth_rx_queue_setup,
393         .tx_queue_setup = eth_tx_queue_setup,
394         .rx_queue_release = eth_queue_release,
395         .tx_queue_release = eth_queue_release,
396         .link_update = eth_link_update,
397         .stats_get = eth_stats_get,
398         .stats_reset = eth_stats_reset,
399 };
400
401 /*
402  * Opens an AF_PACKET socket
403  */
404 static int
405 open_packet_iface(const char *key __rte_unused,
406                   const char *value __rte_unused,
407                   void *extra_args)
408 {
409         int *sockfd = extra_args;
410
411         /* Open an AF_PACKET socket... */
412         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
413         if (*sockfd == -1) {
414                 RTE_LOG(ERR, PMD, "Could not open AF_PACKET socket\n");
415                 return -1;
416         }
417
418         return 0;
419 }
420
421 static int
422 rte_pmd_init_internals(const char *name,
423                        const int sockfd,
424                        const unsigned nb_queues,
425                        unsigned int blocksize,
426                        unsigned int blockcnt,
427                        unsigned int framesize,
428                        unsigned int framecnt,
429                        const unsigned numa_node,
430                        struct pmd_internals **internals,
431                        struct rte_eth_dev **eth_dev,
432                        struct rte_kvargs *kvlist)
433 {
434         struct rte_eth_dev_data *data = 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_early;
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_early;
471
472         *internals = rte_zmalloc_socket(name, sizeof(**internals),
473                                         0, numa_node);
474         if (*internals == NULL)
475                 goto error_early;
476
477         for (q = 0; q < nb_queues; q++) {
478                 (*internals)->rx_queue[q].map = MAP_FAILED;
479                 (*internals)->tx_queue[q].map = MAP_FAILED;
480         }
481
482         req = &((*internals)->req);
483
484         req->tp_block_size = blocksize;
485         req->tp_block_nr = blockcnt;
486         req->tp_frame_size = framesize;
487         req->tp_frame_nr = framecnt;
488
489         ifnamelen = strlen(pair->value);
490         if (ifnamelen < sizeof(ifr.ifr_name)) {
491                 memcpy(ifr.ifr_name, pair->value, ifnamelen);
492                 ifr.ifr_name[ifnamelen] = '\0';
493         } else {
494                 RTE_LOG(ERR, PMD,
495                         "%s: I/F name too long (%s)\n",
496                         name, pair->value);
497                 goto error_early;
498         }
499         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
500                 RTE_LOG(ERR, PMD,
501                         "%s: ioctl failed (SIOCGIFINDEX)\n",
502                         name);
503                 goto error_early;
504         }
505         (*internals)->if_index = ifr.ifr_ifindex;
506
507         if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
508                 RTE_LOG(ERR, PMD,
509                         "%s: ioctl failed (SIOCGIFHWADDR)\n",
510                         name);
511                 goto error_early;
512         }
513         memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
514
515         memset(&sockaddr, 0, sizeof(sockaddr));
516         sockaddr.sll_family = AF_PACKET;
517         sockaddr.sll_protocol = htons(ETH_P_ALL);
518         sockaddr.sll_ifindex = (*internals)->if_index;
519
520 #if defined(PACKET_FANOUT)
521         fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
522         fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
523 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
524         fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
525 #endif
526 #endif
527
528         for (q = 0; q < nb_queues; q++) {
529                 /* Open an AF_PACKET socket for this queue... */
530                 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
531                 if (qsockfd == -1) {
532                         RTE_LOG(ERR, PMD,
533                                 "%s: could not open AF_PACKET socket\n",
534                                 name);
535                         return -1;
536                 }
537
538                 tpver = TPACKET_V2;
539                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
540                                 &tpver, sizeof(tpver));
541                 if (rc == -1) {
542                         RTE_LOG(ERR, PMD,
543                                 "%s: could not set PACKET_VERSION on AF_PACKET "
544                                 "socket for %s\n", name, pair->value);
545                         goto error;
546                 }
547
548                 discard = 1;
549                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
550                                 &discard, sizeof(discard));
551                 if (rc == -1) {
552                         RTE_LOG(ERR, PMD,
553                                 "%s: could not set PACKET_LOSS on "
554                                 "AF_PACKET socket for %s\n", name, pair->value);
555                         goto error;
556                 }
557
558 #if defined(PACKET_QDISC_BYPASS)
559                 bypass = 1;
560                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
561                                 &bypass, sizeof(bypass));
562                 if (rc == -1) {
563                         RTE_LOG(ERR, PMD,
564                                 "%s: could not set PACKET_QDISC_BYPASS "
565                                 "on AF_PACKET socket for %s\n", name,
566                                 pair->value);
567                         goto error;
568                 }
569 #endif
570
571                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
572                 if (rc == -1) {
573                         RTE_LOG(ERR, PMD,
574                                 "%s: could not set PACKET_RX_RING on AF_PACKET "
575                                 "socket for %s\n", name, pair->value);
576                         goto error;
577                 }
578
579                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
580                 if (rc == -1) {
581                         RTE_LOG(ERR, PMD,
582                                 "%s: could not set PACKET_TX_RING on AF_PACKET "
583                                 "socket for %s\n", name, pair->value);
584                         goto error;
585                 }
586
587                 rx_queue = &((*internals)->rx_queue[q]);
588                 rx_queue->framecount = req->tp_frame_nr;
589
590                 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
591                                     PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
592                                     qsockfd, 0);
593                 if (rx_queue->map == MAP_FAILED) {
594                         RTE_LOG(ERR, PMD,
595                                 "%s: call to mmap failed on AF_PACKET socket for %s\n",
596                                 name, pair->value);
597                         goto error;
598                 }
599
600                 /* rdsize is same for both Tx and Rx */
601                 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
602
603                 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
604                 if (rx_queue->rd == NULL)
605                         goto error;
606                 for (i = 0; i < req->tp_frame_nr; ++i) {
607                         rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
608                         rx_queue->rd[i].iov_len = req->tp_frame_size;
609                 }
610                 rx_queue->sockfd = qsockfd;
611
612                 tx_queue = &((*internals)->tx_queue[q]);
613                 tx_queue->framecount = req->tp_frame_nr;
614
615                 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
616
617                 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
618                 if (tx_queue->rd == NULL)
619                         goto error;
620                 for (i = 0; i < req->tp_frame_nr; ++i) {
621                         tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
622                         tx_queue->rd[i].iov_len = req->tp_frame_size;
623                 }
624                 tx_queue->sockfd = qsockfd;
625
626                 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
627                 if (rc == -1) {
628                         RTE_LOG(ERR, PMD,
629                                 "%s: could not bind AF_PACKET socket to %s\n",
630                                 name, pair->value);
631                         goto error;
632                 }
633
634 #if defined(PACKET_FANOUT)
635                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
636                                 &fanout_arg, sizeof(fanout_arg));
637                 if (rc == -1) {
638                         RTE_LOG(ERR, PMD,
639                                 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
640                                 "for %s\n", name, pair->value);
641                         goto error;
642                 }
643 #endif
644         }
645
646         /* reserve an ethdev entry */
647         *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
648         if (*eth_dev == NULL)
649                 goto error;
650
651         /*
652          * now put it all together
653          * - store queue data in internals,
654          * - store numa_node in eth_dev
655          * - point eth_dev_data to internals
656          * - and point eth_dev structure to new eth_dev_data structure
657          */
658
659         (*internals)->nb_queues = nb_queues;
660
661         data->dev_private = *internals;
662         data->port_id = (*eth_dev)->data->port_id;
663         data->nb_rx_queues = (uint16_t)nb_queues;
664         data->nb_tx_queues = (uint16_t)nb_queues;
665         data->dev_link = pmd_link;
666         data->mac_addrs = &(*internals)->eth_addr;
667
668         (*eth_dev)->data = data;
669         (*eth_dev)->dev_ops = &ops;
670         (*eth_dev)->driver = NULL;
671         (*eth_dev)->data->dev_flags = 0;
672         (*eth_dev)->data->drv_name = drivername;
673         (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
674         (*eth_dev)->data->numa_node = numa_node;
675
676         return 0;
677
678 error:
679         if (qsockfd != -1)
680                 close(qsockfd);
681         for (q = 0; q < nb_queues; q++) {
682                 munmap((*internals)->rx_queue[q].map,
683                        2 * req->tp_block_size * req->tp_block_nr);
684
685                 rte_free((*internals)->rx_queue[q].rd);
686                 rte_free((*internals)->tx_queue[q].rd);
687                 if (((*internals)->rx_queue[q].sockfd != 0) &&
688                         ((*internals)->rx_queue[q].sockfd != qsockfd))
689                         close((*internals)->rx_queue[q].sockfd);
690         }
691         rte_free(*internals);
692 error_early:
693         rte_free(data);
694         return -1;
695 }
696
697 static int
698 rte_eth_from_packet(const char *name,
699                     int const *sockfd,
700                     const unsigned numa_node,
701                     struct rte_kvargs *kvlist)
702 {
703         struct pmd_internals *internals = NULL;
704         struct rte_eth_dev *eth_dev = NULL;
705         struct rte_kvargs_pair *pair = NULL;
706         unsigned k_idx;
707         unsigned int blockcount;
708         unsigned int blocksize = DFLT_BLOCK_SIZE;
709         unsigned int framesize = DFLT_FRAME_SIZE;
710         unsigned int framecount = DFLT_FRAME_COUNT;
711         unsigned int qpairs = 1;
712
713         /* do some parameter checking */
714         if (*sockfd < 0)
715                 return -1;
716
717         /*
718          * Walk arguments for configurable settings
719          */
720         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
721                 pair = &kvlist->pairs[k_idx];
722                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
723                         qpairs = atoi(pair->value);
724                         if (qpairs < 1 ||
725                             qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
726                                 RTE_LOG(ERR, PMD,
727                                         "%s: invalid qpairs value\n",
728                                         name);
729                                 return -1;
730                         }
731                         continue;
732                 }
733                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
734                         blocksize = atoi(pair->value);
735                         if (!blocksize) {
736                                 RTE_LOG(ERR, PMD,
737                                         "%s: invalid blocksize value\n",
738                                         name);
739                                 return -1;
740                         }
741                         continue;
742                 }
743                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
744                         framesize = atoi(pair->value);
745                         if (!framesize) {
746                                 RTE_LOG(ERR, PMD,
747                                         "%s: invalid framesize value\n",
748                                         name);
749                                 return -1;
750                         }
751                         continue;
752                 }
753                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
754                         framecount = atoi(pair->value);
755                         if (!framecount) {
756                                 RTE_LOG(ERR, PMD,
757                                         "%s: invalid framecount value\n",
758                                         name);
759                                 return -1;
760                         }
761                         continue;
762                 }
763         }
764
765         if (framesize > blocksize) {
766                 RTE_LOG(ERR, PMD,
767                         "%s: AF_PACKET MMAP frame size exceeds block size!\n",
768                         name);
769                 return -1;
770         }
771
772         blockcount = framecount / (blocksize / framesize);
773         if (!blockcount) {
774                 RTE_LOG(ERR, PMD,
775                         "%s: invalid AF_PACKET MMAP parameters\n", name);
776                 return -1;
777         }
778
779         RTE_LOG(INFO, PMD, "%s: AF_PACKET MMAP parameters:\n", name);
780         RTE_LOG(INFO, PMD, "%s:\tblock size %d\n", name, blocksize);
781         RTE_LOG(INFO, PMD, "%s:\tblock count %d\n", name, blockcount);
782         RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize);
783         RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount);
784
785         if (rte_pmd_init_internals(name, *sockfd, qpairs,
786                                    blocksize, blockcount,
787                                    framesize, framecount,
788                                    numa_node, &internals, &eth_dev,
789                                    kvlist) < 0)
790                 return -1;
791
792         eth_dev->rx_pkt_burst = eth_af_packet_rx;
793         eth_dev->tx_pkt_burst = eth_af_packet_tx;
794
795         return 0;
796 }
797
798 int
799 rte_pmd_af_packet_devinit(const char *name, const char *params)
800 {
801         unsigned numa_node;
802         int ret = 0;
803         struct rte_kvargs *kvlist;
804         int sockfd = -1;
805
806         RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name);
807
808         numa_node = rte_socket_id();
809
810         kvlist = rte_kvargs_parse(params, valid_arguments);
811         if (kvlist == NULL) {
812                 ret = -1;
813                 goto exit;
814         }
815
816         /*
817          * If iface argument is passed we open the NICs and use them for
818          * reading / writing
819          */
820         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
821
822                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
823                                          &open_packet_iface, &sockfd);
824                 if (ret < 0)
825                         goto exit;
826         }
827
828         ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
829         close(sockfd); /* no longer needed */
830
831 exit:
832         rte_kvargs_free(kvlist);
833         return ret;
834 }
835
836 static struct rte_driver pmd_af_packet_drv = {
837         .name = "eth_af_packet",
838         .type = PMD_VDEV,
839         .init = rte_pmd_af_packet_devinit,
840 };
841
842 PMD_REGISTER_DRIVER(pmd_af_packet_drv);