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