Imported Upstream version 16.04
[deb_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 #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 };
82
83 struct pkt_tx_queue {
84         int sockfd;
85
86         struct iovec *rd;
87         uint8_t *map;
88         unsigned int framecount;
89         unsigned int framenum;
90
91         volatile unsigned long tx_pkts;
92         volatile unsigned long err_pkts;
93 };
94
95 struct pmd_internals {
96         unsigned nb_queues;
97
98         int if_index;
99         struct ether_addr eth_addr;
100
101         struct tpacket_req req;
102
103         struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
104         struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
105 };
106
107 static const char *valid_arguments[] = {
108         ETH_AF_PACKET_IFACE_ARG,
109         ETH_AF_PACKET_NUM_Q_ARG,
110         ETH_AF_PACKET_BLOCKSIZE_ARG,
111         ETH_AF_PACKET_FRAMESIZE_ARG,
112         ETH_AF_PACKET_FRAMECOUNT_ARG,
113         NULL
114 };
115
116 static const char *drivername = "AF_PACKET PMD";
117
118 static struct rte_eth_link pmd_link = {
119         .link_speed = ETH_SPEED_NUM_10G,
120         .link_duplex = ETH_LINK_FULL_DUPLEX,
121         .link_status = ETH_LINK_DOWN,
122         .link_autoneg = ETH_LINK_SPEED_AUTONEG
123 };
124
125 static uint16_t
126 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
127 {
128         unsigned i;
129         struct tpacket2_hdr *ppd;
130         struct rte_mbuf *mbuf;
131         uint8_t *pbuf;
132         struct pkt_rx_queue *pkt_q = queue;
133         uint16_t num_rx = 0;
134         unsigned int framecount, framenum;
135
136         if (unlikely(nb_pkts == 0))
137                 return 0;
138
139         /*
140          * Reads the given number of packets from the AF_PACKET socket one by
141          * one and copies the packet data into a newly allocated mbuf.
142          */
143         framecount = pkt_q->framecount;
144         framenum = pkt_q->framenum;
145         for (i = 0; i < nb_pkts; i++) {
146                 /* point at the next incoming frame */
147                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
148                 if ((ppd->tp_status & TP_STATUS_USER) == 0)
149                         break;
150
151                 /* allocate the next mbuf */
152                 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool);
153                 if (unlikely(mbuf == NULL))
154                         break;
155
156                 /* packet will fit in the mbuf, go ahead and receive it */
157                 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf) = ppd->tp_snaplen;
158                 pbuf = (uint8_t *) ppd + ppd->tp_mac;
159                 memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf));
160
161                 /* release incoming frame and advance ring buffer */
162                 ppd->tp_status = TP_STATUS_KERNEL;
163                 if (++framenum >= framecount)
164                         framenum = 0;
165                 mbuf->port = pkt_q->in_port;
166
167                 /* account for the receive frame */
168                 bufs[i] = mbuf;
169                 num_rx++;
170         }
171         pkt_q->framenum = framenum;
172         pkt_q->rx_pkts += num_rx;
173         return num_rx;
174 }
175
176 /*
177  * Callback to handle sending packets through a real NIC.
178  */
179 static uint16_t
180 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
181 {
182         struct tpacket2_hdr *ppd;
183         struct rte_mbuf *mbuf;
184         uint8_t *pbuf;
185         unsigned int framecount, framenum;
186         struct pollfd pfd;
187         struct pkt_tx_queue *pkt_q = queue;
188         uint16_t num_tx = 0;
189         int i;
190
191         if (unlikely(nb_pkts == 0))
192                 return 0;
193
194         memset(&pfd, 0, sizeof(pfd));
195         pfd.fd = pkt_q->sockfd;
196         pfd.events = POLLOUT;
197         pfd.revents = 0;
198
199         framecount = pkt_q->framecount;
200         framenum = pkt_q->framenum;
201         ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
202         for (i = 0; i < nb_pkts; i++) {
203                 /* point at the next incoming frame */
204                 if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
205                     (poll(&pfd, 1, -1) < 0))
206                                 continue;
207
208                 /* copy the tx frame data */
209                 mbuf = bufs[num_tx];
210                 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
211                         sizeof(struct sockaddr_ll);
212                 memcpy(pbuf, rte_pktmbuf_mtod(mbuf, void*), rte_pktmbuf_data_len(mbuf));
213                 ppd->tp_len = ppd->tp_snaplen = rte_pktmbuf_data_len(mbuf);
214
215                 /* release incoming frame and advance ring buffer */
216                 ppd->tp_status = TP_STATUS_SEND_REQUEST;
217                 if (++framenum >= framecount)
218                         framenum = 0;
219                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
220
221                 num_tx++;
222                 rte_pktmbuf_free(mbuf);
223         }
224
225         /* kick-off transmits */
226         if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1)
227                 return 0; /* error sending -- no packets transmitted */
228
229         pkt_q->framenum = framenum;
230         pkt_q->tx_pkts += num_tx;
231         pkt_q->err_pkts += nb_pkts - num_tx;
232         return num_tx;
233 }
234
235 static int
236 eth_dev_start(struct rte_eth_dev *dev)
237 {
238         dev->data->dev_link.link_status = ETH_LINK_UP;
239         return 0;
240 }
241
242 /*
243  * This function gets called when the current port gets stopped.
244  */
245 static void
246 eth_dev_stop(struct rte_eth_dev *dev)
247 {
248         unsigned i;
249         int sockfd;
250         struct pmd_internals *internals = dev->data->dev_private;
251
252         for (i = 0; i < internals->nb_queues; i++) {
253                 sockfd = internals->rx_queue[i].sockfd;
254                 if (sockfd != -1)
255                         close(sockfd);
256                 sockfd = internals->tx_queue[i].sockfd;
257                 if (sockfd != -1)
258                         close(sockfd);
259         }
260
261         dev->data->dev_link.link_status = ETH_LINK_DOWN;
262 }
263
264 static int
265 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
266 {
267         return 0;
268 }
269
270 static void
271 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
272 {
273         struct pmd_internals *internals = dev->data->dev_private;
274
275         dev_info->driver_name = drivername;
276         dev_info->if_index = internals->if_index;
277         dev_info->max_mac_addrs = 1;
278         dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
279         dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
280         dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
281         dev_info->min_rx_bufsize = 0;
282         dev_info->pci_dev = NULL;
283 }
284
285 static void
286 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
287 {
288         unsigned i, imax;
289         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
290         const struct pmd_internals *internal = dev->data->dev_private;
291
292         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
293                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
294         for (i = 0; i < imax; i++) {
295                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
296                 rx_total += igb_stats->q_ipackets[i];
297         }
298
299         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
300                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
301         for (i = 0; i < imax; i++) {
302                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
303                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
304                 tx_total += igb_stats->q_opackets[i];
305                 tx_err_total += igb_stats->q_errors[i];
306         }
307
308         igb_stats->ipackets = rx_total;
309         igb_stats->opackets = tx_total;
310         igb_stats->oerrors = tx_err_total;
311 }
312
313 static void
314 eth_stats_reset(struct rte_eth_dev *dev)
315 {
316         unsigned i;
317         struct pmd_internals *internal = dev->data->dev_private;
318
319         for (i = 0; i < internal->nb_queues; i++)
320                 internal->rx_queue[i].rx_pkts = 0;
321
322         for (i = 0; i < internal->nb_queues; i++) {
323                 internal->tx_queue[i].tx_pkts = 0;
324                 internal->tx_queue[i].err_pkts = 0;
325         }
326 }
327
328 static void
329 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
330 {
331 }
332
333 static void
334 eth_queue_release(void *q __rte_unused)
335 {
336 }
337
338 static int
339 eth_link_update(struct rte_eth_dev *dev __rte_unused,
340                 int wait_to_complete __rte_unused)
341 {
342         return 0;
343 }
344
345 static int
346 eth_rx_queue_setup(struct rte_eth_dev *dev,
347                    uint16_t rx_queue_id,
348                    uint16_t nb_rx_desc __rte_unused,
349                    unsigned int socket_id __rte_unused,
350                    const struct rte_eth_rxconf *rx_conf __rte_unused,
351                    struct rte_mempool *mb_pool)
352 {
353         struct pmd_internals *internals = dev->data->dev_private;
354         struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
355         uint16_t buf_size;
356
357         pkt_q->mb_pool = mb_pool;
358
359         /* Now get the space available for data in the mbuf */
360         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
361                 RTE_PKTMBUF_HEADROOM);
362
363         if (ETH_FRAME_LEN > buf_size) {
364                 RTE_LOG(ERR, PMD,
365                         "%s: %d bytes will not fit in mbuf (%d bytes)\n",
366                         dev->data->name, ETH_FRAME_LEN, buf_size);
367                 return -ENOMEM;
368         }
369
370         dev->data->rx_queues[rx_queue_id] = pkt_q;
371         pkt_q->in_port = dev->data->port_id;
372
373         return 0;
374 }
375
376 static int
377 eth_tx_queue_setup(struct rte_eth_dev *dev,
378                    uint16_t tx_queue_id,
379                    uint16_t nb_tx_desc __rte_unused,
380                    unsigned int socket_id __rte_unused,
381                    const struct rte_eth_txconf *tx_conf __rte_unused)
382 {
383
384         struct pmd_internals *internals = dev->data->dev_private;
385
386         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
387         return 0;
388 }
389
390 static const struct eth_dev_ops ops = {
391         .dev_start = eth_dev_start,
392         .dev_stop = eth_dev_stop,
393         .dev_close = eth_dev_close,
394         .dev_configure = eth_dev_configure,
395         .dev_infos_get = eth_dev_info,
396         .rx_queue_setup = eth_rx_queue_setup,
397         .tx_queue_setup = eth_tx_queue_setup,
398         .rx_queue_release = eth_queue_release,
399         .tx_queue_release = eth_queue_release,
400         .link_update = eth_link_update,
401         .stats_get = eth_stats_get,
402         .stats_reset = eth_stats_reset,
403 };
404
405 /*
406  * Opens an AF_PACKET socket
407  */
408 static int
409 open_packet_iface(const char *key __rte_unused,
410                   const char *value __rte_unused,
411                   void *extra_args)
412 {
413         int *sockfd = extra_args;
414
415         /* Open an AF_PACKET socket... */
416         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
417         if (*sockfd == -1) {
418                 RTE_LOG(ERR, PMD, "Could not open AF_PACKET socket\n");
419                 return -1;
420         }
421
422         return 0;
423 }
424
425 static int
426 rte_pmd_init_internals(const char *name,
427                        const int sockfd,
428                        const unsigned nb_queues,
429                        unsigned int blocksize,
430                        unsigned int blockcnt,
431                        unsigned int framesize,
432                        unsigned int framecnt,
433                        const unsigned numa_node,
434                        struct pmd_internals **internals,
435                        struct rte_eth_dev **eth_dev,
436                        struct rte_kvargs *kvlist)
437 {
438         struct rte_eth_dev_data *data = NULL;
439         struct rte_kvargs_pair *pair = NULL;
440         struct ifreq ifr;
441         size_t ifnamelen;
442         unsigned k_idx;
443         struct sockaddr_ll sockaddr;
444         struct tpacket_req *req;
445         struct pkt_rx_queue *rx_queue;
446         struct pkt_tx_queue *tx_queue;
447         int rc, tpver, discard;
448         int qsockfd = -1;
449         unsigned int i, q, rdsize;
450         int fanout_arg __rte_unused, bypass __rte_unused;
451
452         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
453                 pair = &kvlist->pairs[k_idx];
454                 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
455                         break;
456         }
457         if (pair == NULL) {
458                 RTE_LOG(ERR, PMD,
459                         "%s: no interface specified for AF_PACKET ethdev\n",
460                         name);
461                 goto error_early;
462         }
463
464         RTE_LOG(INFO, PMD,
465                 "%s: creating AF_PACKET-backed ethdev on numa socket %u\n",
466                 name, numa_node);
467
468         /*
469          * now do all data allocation - for eth_dev structure, dummy pci driver
470          * and internal (private) data
471          */
472         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
473         if (data == NULL)
474                 goto error_early;
475
476         *internals = rte_zmalloc_socket(name, sizeof(**internals),
477                                         0, numa_node);
478         if (*internals == NULL)
479                 goto error_early;
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_early;
502         }
503         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
504                 RTE_LOG(ERR, PMD,
505                         "%s: ioctl failed (SIOCGIFINDEX)\n",
506                         name);
507                 goto error_early;
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_early;
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 in eth_dev
659          * - point eth_dev_data to internals
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         strncpy(data->name,
672                 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
673
674         (*eth_dev)->data = data;
675         (*eth_dev)->dev_ops = &ops;
676         (*eth_dev)->driver = NULL;
677         (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
678         (*eth_dev)->data->drv_name = drivername;
679         (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
680         (*eth_dev)->data->numa_node = numa_node;
681
682         return 0;
683
684 error:
685         if (qsockfd != -1)
686                 close(qsockfd);
687         for (q = 0; q < nb_queues; q++) {
688                 munmap((*internals)->rx_queue[q].map,
689                        2 * req->tp_block_size * req->tp_block_nr);
690
691                 rte_free((*internals)->rx_queue[q].rd);
692                 rte_free((*internals)->tx_queue[q].rd);
693                 if (((*internals)->rx_queue[q].sockfd != 0) &&
694                         ((*internals)->rx_queue[q].sockfd != qsockfd))
695                         close((*internals)->rx_queue[q].sockfd);
696         }
697         rte_free(*internals);
698 error_early:
699         rte_free(data);
700         return -1;
701 }
702
703 static int
704 rte_eth_from_packet(const char *name,
705                     int const *sockfd,
706                     const unsigned numa_node,
707                     struct rte_kvargs *kvlist)
708 {
709         struct pmd_internals *internals = NULL;
710         struct rte_eth_dev *eth_dev = NULL;
711         struct rte_kvargs_pair *pair = NULL;
712         unsigned k_idx;
713         unsigned int blockcount;
714         unsigned int blocksize = DFLT_BLOCK_SIZE;
715         unsigned int framesize = DFLT_FRAME_SIZE;
716         unsigned int framecount = DFLT_FRAME_COUNT;
717         unsigned int qpairs = 1;
718
719         /* do some parameter checking */
720         if (*sockfd < 0)
721                 return -1;
722
723         /*
724          * Walk arguments for configurable settings
725          */
726         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
727                 pair = &kvlist->pairs[k_idx];
728                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
729                         qpairs = atoi(pair->value);
730                         if (qpairs < 1 ||
731                             qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
732                                 RTE_LOG(ERR, PMD,
733                                         "%s: invalid qpairs value\n",
734                                         name);
735                                 return -1;
736                         }
737                         continue;
738                 }
739                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
740                         blocksize = atoi(pair->value);
741                         if (!blocksize) {
742                                 RTE_LOG(ERR, PMD,
743                                         "%s: invalid blocksize value\n",
744                                         name);
745                                 return -1;
746                         }
747                         continue;
748                 }
749                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
750                         framesize = atoi(pair->value);
751                         if (!framesize) {
752                                 RTE_LOG(ERR, PMD,
753                                         "%s: invalid framesize value\n",
754                                         name);
755                                 return -1;
756                         }
757                         continue;
758                 }
759                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
760                         framecount = atoi(pair->value);
761                         if (!framecount) {
762                                 RTE_LOG(ERR, PMD,
763                                         "%s: invalid framecount value\n",
764                                         name);
765                                 return -1;
766                         }
767                         continue;
768                 }
769         }
770
771         if (framesize > blocksize) {
772                 RTE_LOG(ERR, PMD,
773                         "%s: AF_PACKET MMAP frame size exceeds block size!\n",
774                         name);
775                 return -1;
776         }
777
778         blockcount = framecount / (blocksize / framesize);
779         if (!blockcount) {
780                 RTE_LOG(ERR, PMD,
781                         "%s: invalid AF_PACKET MMAP parameters\n", name);
782                 return -1;
783         }
784
785         RTE_LOG(INFO, PMD, "%s: AF_PACKET MMAP parameters:\n", name);
786         RTE_LOG(INFO, PMD, "%s:\tblock size %d\n", name, blocksize);
787         RTE_LOG(INFO, PMD, "%s:\tblock count %d\n", name, blockcount);
788         RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize);
789         RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount);
790
791         if (rte_pmd_init_internals(name, *sockfd, qpairs,
792                                    blocksize, blockcount,
793                                    framesize, framecount,
794                                    numa_node, &internals, &eth_dev,
795                                    kvlist) < 0)
796                 return -1;
797
798         eth_dev->rx_pkt_burst = eth_af_packet_rx;
799         eth_dev->tx_pkt_burst = eth_af_packet_tx;
800
801         return 0;
802 }
803
804 static int
805 rte_pmd_af_packet_devinit(const char *name, const char *params)
806 {
807         unsigned numa_node;
808         int ret = 0;
809         struct rte_kvargs *kvlist;
810         int sockfd = -1;
811
812         RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name);
813
814         numa_node = rte_socket_id();
815
816         kvlist = rte_kvargs_parse(params, valid_arguments);
817         if (kvlist == NULL) {
818                 ret = -1;
819                 goto exit;
820         }
821
822         /*
823          * If iface argument is passed we open the NICs and use them for
824          * reading / writing
825          */
826         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
827
828                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
829                                          &open_packet_iface, &sockfd);
830                 if (ret < 0)
831                         goto exit;
832         }
833
834         ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
835         close(sockfd); /* no longer needed */
836
837 exit:
838         rte_kvargs_free(kvlist);
839         return ret;
840 }
841
842 static int
843 rte_pmd_af_packet_devuninit(const char *name)
844 {
845         struct rte_eth_dev *eth_dev = NULL;
846         struct pmd_internals *internals;
847         unsigned q;
848
849         RTE_LOG(INFO, PMD, "Closing AF_PACKET ethdev on numa socket %u\n",
850                         rte_socket_id());
851
852         if (name == NULL)
853                 return -1;
854
855         /* find the ethdev entry */
856         eth_dev = rte_eth_dev_allocated(name);
857         if (eth_dev == NULL)
858                 return -1;
859
860         internals = eth_dev->data->dev_private;
861         for (q = 0; q < internals->nb_queues; q++) {
862                 rte_free(internals->rx_queue[q].rd);
863                 rte_free(internals->tx_queue[q].rd);
864         }
865
866         rte_free(eth_dev->data->dev_private);
867         rte_free(eth_dev->data);
868
869         rte_eth_dev_release_port(eth_dev);
870
871         return 0;
872 }
873
874 static struct rte_driver pmd_af_packet_drv = {
875         .name = "eth_af_packet",
876         .type = PMD_VDEV,
877         .init = rte_pmd_af_packet_devinit,
878         .uninit = rte_pmd_af_packet_devuninit,
879 };
880
881 PMD_REGISTER_DRIVER(pmd_af_packet_drv);