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