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