4 * Copyright(c) 2014 John W. Linville <linville@tuxdriver.com>
6 * Originally based upon librte_pmd_pcap code:
8 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2014 6WIND S.A.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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
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.
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.
40 #include <rte_ethdev.h>
41 #include <rte_malloc.h>
42 #include <rte_kvargs.h>
45 #include <linux/if_ether.h>
46 #include <linux/if_packet.h>
47 #include <arpa/inet.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
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"
62 #define DFLT_BLOCK_SIZE (1 << 12)
63 #define DFLT_FRAME_SIZE (1 << 11)
64 #define DFLT_FRAME_COUNT (1 << 9)
66 #define RTE_PMD_AF_PACKET_MAX_RINGS 16
73 unsigned int framecount;
74 unsigned int framenum;
76 struct rte_mempool *mb_pool;
79 volatile unsigned long rx_pkts;
80 volatile unsigned long err_pkts;
81 volatile unsigned long rx_bytes;
86 unsigned int frame_data_size;
90 unsigned int framecount;
91 unsigned int framenum;
93 volatile unsigned long tx_pkts;
94 volatile unsigned long err_pkts;
95 volatile unsigned long tx_bytes;
98 struct pmd_internals {
103 struct ether_addr eth_addr;
105 struct tpacket_req req;
107 struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
108 struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
111 static const char *valid_arguments[] = {
112 ETH_AF_PACKET_IFACE_ARG,
113 ETH_AF_PACKET_NUM_Q_ARG,
114 ETH_AF_PACKET_BLOCKSIZE_ARG,
115 ETH_AF_PACKET_FRAMESIZE_ARG,
116 ETH_AF_PACKET_FRAMECOUNT_ARG,
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
128 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
131 struct tpacket2_hdr *ppd;
132 struct rte_mbuf *mbuf;
134 struct pkt_rx_queue *pkt_q = queue;
136 unsigned long num_rx_bytes = 0;
137 unsigned int framecount, framenum;
139 if (unlikely(nb_pkts == 0))
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.
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)
154 /* allocate the next mbuf */
155 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool);
156 if (unlikely(mbuf == NULL))
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));
164 /* check for vlan info */
165 if (ppd->tp_status & TP_STATUS_VLAN_VALID) {
166 mbuf->vlan_tci = ppd->tp_vlan_tci;
167 mbuf->ol_flags |= (PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED);
170 /* release incoming frame and advance ring buffer */
171 ppd->tp_status = TP_STATUS_KERNEL;
172 if (++framenum >= framecount)
174 mbuf->port = pkt_q->in_port;
176 /* account for the receive frame */
179 num_rx_bytes += mbuf->pkt_len;
181 pkt_q->framenum = framenum;
182 pkt_q->rx_pkts += num_rx;
183 pkt_q->rx_bytes += num_rx_bytes;
188 * Callback to handle sending packets through a real NIC.
191 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
193 struct tpacket2_hdr *ppd;
194 struct rte_mbuf *mbuf;
196 unsigned int framecount, framenum;
198 struct pkt_tx_queue *pkt_q = queue;
200 unsigned long num_tx_bytes = 0;
203 if (unlikely(nb_pkts == 0))
206 memset(&pfd, 0, sizeof(pfd));
207 pfd.fd = pkt_q->sockfd;
208 pfd.events = POLLOUT;
211 framecount = pkt_q->framecount;
212 framenum = pkt_q->framenum;
213 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
214 for (i = 0; i < nb_pkts; i++) {
217 /* drop oversized packets */
218 if (rte_pktmbuf_data_len(mbuf) > pkt_q->frame_data_size) {
219 rte_pktmbuf_free(mbuf);
223 /* insert vlan info if necessary */
224 if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
225 if (rte_vlan_insert(&mbuf)) {
226 rte_pktmbuf_free(mbuf);
231 /* point at the next incoming frame */
232 if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
233 (poll(&pfd, 1, -1) < 0))
236 /* copy the tx frame data */
237 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
238 sizeof(struct sockaddr_ll);
239 memcpy(pbuf, rte_pktmbuf_mtod(mbuf, void*), rte_pktmbuf_data_len(mbuf));
240 ppd->tp_len = ppd->tp_snaplen = rte_pktmbuf_data_len(mbuf);
242 /* release incoming frame and advance ring buffer */
243 ppd->tp_status = TP_STATUS_SEND_REQUEST;
244 if (++framenum >= framecount)
246 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
249 num_tx_bytes += mbuf->pkt_len;
250 rte_pktmbuf_free(mbuf);
253 /* kick-off transmits */
254 if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1)
255 num_tx = 0; /* error sending -- no packets transmitted */
257 pkt_q->framenum = framenum;
258 pkt_q->tx_pkts += num_tx;
259 pkt_q->err_pkts += i - num_tx;
260 pkt_q->tx_bytes += num_tx_bytes;
265 eth_dev_start(struct rte_eth_dev *dev)
267 dev->data->dev_link.link_status = ETH_LINK_UP;
272 * This function gets called when the current port gets stopped.
275 eth_dev_stop(struct rte_eth_dev *dev)
279 struct pmd_internals *internals = dev->data->dev_private;
281 for (i = 0; i < internals->nb_queues; i++) {
282 sockfd = internals->rx_queue[i].sockfd;
286 /* Prevent use after free in case tx fd == rx fd */
287 if (sockfd != internals->tx_queue[i].sockfd) {
288 sockfd = internals->tx_queue[i].sockfd;
293 internals->rx_queue[i].sockfd = -1;
294 internals->tx_queue[i].sockfd = -1;
297 dev->data->dev_link.link_status = ETH_LINK_DOWN;
301 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
307 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
309 struct pmd_internals *internals = dev->data->dev_private;
311 dev_info->if_index = internals->if_index;
312 dev_info->max_mac_addrs = 1;
313 dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
314 dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
315 dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
316 dev_info->min_rx_bufsize = 0;
320 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
323 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
324 unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
325 const struct pmd_internals *internal = dev->data->dev_private;
327 imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
328 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
329 for (i = 0; i < imax; i++) {
330 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
331 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
332 rx_total += igb_stats->q_ipackets[i];
333 rx_bytes_total += igb_stats->q_ibytes[i];
336 imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
337 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
338 for (i = 0; i < imax; i++) {
339 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
340 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
341 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
342 tx_total += igb_stats->q_opackets[i];
343 tx_err_total += igb_stats->q_errors[i];
344 tx_bytes_total += igb_stats->q_obytes[i];
347 igb_stats->ipackets = rx_total;
348 igb_stats->ibytes = rx_bytes_total;
349 igb_stats->opackets = tx_total;
350 igb_stats->oerrors = tx_err_total;
351 igb_stats->obytes = tx_bytes_total;
355 eth_stats_reset(struct rte_eth_dev *dev)
358 struct pmd_internals *internal = dev->data->dev_private;
360 for (i = 0; i < internal->nb_queues; i++) {
361 internal->rx_queue[i].rx_pkts = 0;
362 internal->rx_queue[i].rx_bytes = 0;
365 for (i = 0; i < internal->nb_queues; i++) {
366 internal->tx_queue[i].tx_pkts = 0;
367 internal->tx_queue[i].err_pkts = 0;
368 internal->tx_queue[i].tx_bytes = 0;
373 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
378 eth_queue_release(void *q __rte_unused)
383 eth_link_update(struct rte_eth_dev *dev __rte_unused,
384 int wait_to_complete __rte_unused)
390 eth_rx_queue_setup(struct rte_eth_dev *dev,
391 uint16_t rx_queue_id,
392 uint16_t nb_rx_desc __rte_unused,
393 unsigned int socket_id __rte_unused,
394 const struct rte_eth_rxconf *rx_conf __rte_unused,
395 struct rte_mempool *mb_pool)
397 struct pmd_internals *internals = dev->data->dev_private;
398 struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
399 unsigned int buf_size, data_size;
401 pkt_q->mb_pool = mb_pool;
403 /* Now get the space available for data in the mbuf */
404 buf_size = rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
405 RTE_PKTMBUF_HEADROOM;
406 data_size = internals->req.tp_frame_size;
407 data_size -= TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
409 if (data_size > buf_size) {
411 "%s: %d bytes will not fit in mbuf (%d bytes)\n",
412 dev->data->name, data_size, buf_size);
416 dev->data->rx_queues[rx_queue_id] = pkt_q;
417 pkt_q->in_port = dev->data->port_id;
423 eth_tx_queue_setup(struct rte_eth_dev *dev,
424 uint16_t tx_queue_id,
425 uint16_t nb_tx_desc __rte_unused,
426 unsigned int socket_id __rte_unused,
427 const struct rte_eth_txconf *tx_conf __rte_unused)
430 struct pmd_internals *internals = dev->data->dev_private;
432 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
437 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
439 struct pmd_internals *internals = dev->data->dev_private;
440 struct ifreq ifr = { .ifr_mtu = mtu };
443 unsigned int data_size = internals->req.tp_frame_size -
445 sizeof(struct sockaddr_ll);
450 s = socket(PF_INET, SOCK_DGRAM, 0);
454 strncpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
455 ret = ioctl(s, SIOCSIFMTU, &ifr);
465 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
470 s = socket(PF_INET, SOCK_DGRAM, 0);
474 strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
475 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
477 ifr.ifr_flags &= mask;
478 ifr.ifr_flags |= flags;
479 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
486 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
488 struct pmd_internals *internals = dev->data->dev_private;
490 eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
494 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
496 struct pmd_internals *internals = dev->data->dev_private;
498 eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
501 static const struct eth_dev_ops ops = {
502 .dev_start = eth_dev_start,
503 .dev_stop = eth_dev_stop,
504 .dev_close = eth_dev_close,
505 .dev_configure = eth_dev_configure,
506 .dev_infos_get = eth_dev_info,
507 .mtu_set = eth_dev_mtu_set,
508 .promiscuous_enable = eth_dev_promiscuous_enable,
509 .promiscuous_disable = eth_dev_promiscuous_disable,
510 .rx_queue_setup = eth_rx_queue_setup,
511 .tx_queue_setup = eth_tx_queue_setup,
512 .rx_queue_release = eth_queue_release,
513 .tx_queue_release = eth_queue_release,
514 .link_update = eth_link_update,
515 .stats_get = eth_stats_get,
516 .stats_reset = eth_stats_reset,
520 * Opens an AF_PACKET socket
523 open_packet_iface(const char *key __rte_unused,
524 const char *value __rte_unused,
527 int *sockfd = extra_args;
529 /* Open an AF_PACKET socket... */
530 *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
532 RTE_LOG(ERR, PMD, "Could not open AF_PACKET socket\n");
539 static struct rte_vdev_driver pmd_af_packet_drv;
542 rte_pmd_init_internals(const char *name,
544 const unsigned nb_queues,
545 unsigned int blocksize,
546 unsigned int blockcnt,
547 unsigned int framesize,
548 unsigned int framecnt,
549 const unsigned numa_node,
550 struct pmd_internals **internals,
551 struct rte_eth_dev **eth_dev,
552 struct rte_kvargs *kvlist)
554 struct rte_eth_dev_data *data = NULL;
555 struct rte_kvargs_pair *pair = NULL;
559 struct sockaddr_ll sockaddr;
560 struct tpacket_req *req;
561 struct pkt_rx_queue *rx_queue;
562 struct pkt_tx_queue *tx_queue;
563 int rc, tpver, discard;
565 unsigned int i, q, rdsize;
566 int fanout_arg __rte_unused, bypass __rte_unused;
568 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
569 pair = &kvlist->pairs[k_idx];
570 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
575 "%s: no interface specified for AF_PACKET ethdev\n",
581 "%s: creating AF_PACKET-backed ethdev on numa socket %u\n",
585 * now do all data allocation - for eth_dev structure, dummy pci driver
586 * and internal (private) data
588 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
592 *internals = rte_zmalloc_socket(name, sizeof(**internals),
594 if (*internals == NULL)
597 for (q = 0; q < nb_queues; q++) {
598 (*internals)->rx_queue[q].map = MAP_FAILED;
599 (*internals)->tx_queue[q].map = MAP_FAILED;
602 req = &((*internals)->req);
604 req->tp_block_size = blocksize;
605 req->tp_block_nr = blockcnt;
606 req->tp_frame_size = framesize;
607 req->tp_frame_nr = framecnt;
609 ifnamelen = strlen(pair->value);
610 if (ifnamelen < sizeof(ifr.ifr_name)) {
611 memcpy(ifr.ifr_name, pair->value, ifnamelen);
612 ifr.ifr_name[ifnamelen] = '\0';
615 "%s: I/F name too long (%s)\n",
619 if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
621 "%s: ioctl failed (SIOCGIFINDEX)\n",
625 (*internals)->if_name = strdup(pair->value);
626 (*internals)->if_index = ifr.ifr_ifindex;
628 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
630 "%s: ioctl failed (SIOCGIFHWADDR)\n",
634 memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
636 memset(&sockaddr, 0, sizeof(sockaddr));
637 sockaddr.sll_family = AF_PACKET;
638 sockaddr.sll_protocol = htons(ETH_P_ALL);
639 sockaddr.sll_ifindex = (*internals)->if_index;
641 #if defined(PACKET_FANOUT)
642 fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
643 fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
644 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
645 fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
649 for (q = 0; q < nb_queues; q++) {
650 /* Open an AF_PACKET socket for this queue... */
651 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
654 "%s: could not open AF_PACKET socket\n",
660 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
661 &tpver, sizeof(tpver));
664 "%s: could not set PACKET_VERSION on AF_PACKET "
665 "socket for %s\n", name, pair->value);
670 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
671 &discard, sizeof(discard));
674 "%s: could not set PACKET_LOSS on "
675 "AF_PACKET socket for %s\n", name, pair->value);
679 #if defined(PACKET_QDISC_BYPASS)
681 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
682 &bypass, sizeof(bypass));
685 "%s: could not set PACKET_QDISC_BYPASS "
686 "on AF_PACKET socket for %s\n", name,
692 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
695 "%s: could not set PACKET_RX_RING on AF_PACKET "
696 "socket for %s\n", name, pair->value);
700 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
703 "%s: could not set PACKET_TX_RING on AF_PACKET "
704 "socket for %s\n", name, pair->value);
708 rx_queue = &((*internals)->rx_queue[q]);
709 rx_queue->framecount = req->tp_frame_nr;
711 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
712 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
714 if (rx_queue->map == MAP_FAILED) {
716 "%s: call to mmap failed on AF_PACKET socket for %s\n",
721 /* rdsize is same for both Tx and Rx */
722 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
724 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
725 if (rx_queue->rd == NULL)
727 for (i = 0; i < req->tp_frame_nr; ++i) {
728 rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
729 rx_queue->rd[i].iov_len = req->tp_frame_size;
731 rx_queue->sockfd = qsockfd;
733 tx_queue = &((*internals)->tx_queue[q]);
734 tx_queue->framecount = req->tp_frame_nr;
735 tx_queue->frame_data_size = req->tp_frame_size;
736 tx_queue->frame_data_size -= TPACKET2_HDRLEN -
737 sizeof(struct sockaddr_ll);
739 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
741 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
742 if (tx_queue->rd == NULL)
744 for (i = 0; i < req->tp_frame_nr; ++i) {
745 tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
746 tx_queue->rd[i].iov_len = req->tp_frame_size;
748 tx_queue->sockfd = qsockfd;
750 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
753 "%s: could not bind AF_PACKET socket to %s\n",
758 #if defined(PACKET_FANOUT)
759 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
760 &fanout_arg, sizeof(fanout_arg));
763 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
764 "for %s\n", name, pair->value);
770 /* reserve an ethdev entry */
771 *eth_dev = rte_eth_dev_allocate(name);
772 if (*eth_dev == NULL)
776 * now put it all together
777 * - store queue data in internals,
778 * - store numa_node in eth_dev
779 * - point eth_dev_data to internals
780 * - and point eth_dev structure to new eth_dev_data structure
783 (*internals)->nb_queues = nb_queues;
785 data->dev_private = *internals;
786 data->port_id = (*eth_dev)->data->port_id;
787 data->nb_rx_queues = (uint16_t)nb_queues;
788 data->nb_tx_queues = (uint16_t)nb_queues;
789 data->dev_link = pmd_link;
790 data->mac_addrs = &(*internals)->eth_addr;
792 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
794 (*eth_dev)->data = data;
795 (*eth_dev)->dev_ops = &ops;
796 (*eth_dev)->driver = NULL;
797 (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
798 (*eth_dev)->data->drv_name = pmd_af_packet_drv.driver.name;
799 (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
800 (*eth_dev)->data->numa_node = numa_node;
807 for (q = 0; q < nb_queues; q++) {
808 munmap((*internals)->rx_queue[q].map,
809 2 * req->tp_block_size * req->tp_block_nr);
811 rte_free((*internals)->rx_queue[q].rd);
812 rte_free((*internals)->tx_queue[q].rd);
813 if (((*internals)->rx_queue[q].sockfd != 0) &&
814 ((*internals)->rx_queue[q].sockfd != qsockfd))
815 close((*internals)->rx_queue[q].sockfd);
817 free((*internals)->if_name);
818 rte_free(*internals);
825 rte_eth_from_packet(const char *name,
827 const unsigned numa_node,
828 struct rte_kvargs *kvlist)
830 struct pmd_internals *internals = NULL;
831 struct rte_eth_dev *eth_dev = NULL;
832 struct rte_kvargs_pair *pair = NULL;
834 unsigned int blockcount;
835 unsigned int blocksize = DFLT_BLOCK_SIZE;
836 unsigned int framesize = DFLT_FRAME_SIZE;
837 unsigned int framecount = DFLT_FRAME_COUNT;
838 unsigned int qpairs = 1;
840 /* do some parameter checking */
845 * Walk arguments for configurable settings
847 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
848 pair = &kvlist->pairs[k_idx];
849 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
850 qpairs = atoi(pair->value);
852 qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
854 "%s: invalid qpairs value\n",
860 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
861 blocksize = atoi(pair->value);
864 "%s: invalid blocksize value\n",
870 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
871 framesize = atoi(pair->value);
874 "%s: invalid framesize value\n",
880 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
881 framecount = atoi(pair->value);
884 "%s: invalid framecount value\n",
892 if (framesize > blocksize) {
894 "%s: AF_PACKET MMAP frame size exceeds block size!\n",
899 blockcount = framecount / (blocksize / framesize);
902 "%s: invalid AF_PACKET MMAP parameters\n", name);
906 RTE_LOG(INFO, PMD, "%s: AF_PACKET MMAP parameters:\n", name);
907 RTE_LOG(INFO, PMD, "%s:\tblock size %d\n", name, blocksize);
908 RTE_LOG(INFO, PMD, "%s:\tblock count %d\n", name, blockcount);
909 RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize);
910 RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount);
912 if (rte_pmd_init_internals(name, *sockfd, qpairs,
913 blocksize, blockcount,
914 framesize, framecount,
915 numa_node, &internals, ð_dev,
919 eth_dev->rx_pkt_burst = eth_af_packet_rx;
920 eth_dev->tx_pkt_burst = eth_af_packet_tx;
926 rte_pmd_af_packet_probe(const char *name, const char *params)
930 struct rte_kvargs *kvlist;
933 RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name);
935 numa_node = rte_socket_id();
937 kvlist = rte_kvargs_parse(params, valid_arguments);
938 if (kvlist == NULL) {
944 * If iface argument is passed we open the NICs and use them for
947 if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
949 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
950 &open_packet_iface, &sockfd);
955 ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
956 close(sockfd); /* no longer needed */
959 rte_kvargs_free(kvlist);
964 rte_pmd_af_packet_remove(const char *name)
966 struct rte_eth_dev *eth_dev = NULL;
967 struct pmd_internals *internals;
970 RTE_LOG(INFO, PMD, "Closing AF_PACKET ethdev on numa socket %u\n",
976 /* find the ethdev entry */
977 eth_dev = rte_eth_dev_allocated(name);
981 internals = eth_dev->data->dev_private;
982 for (q = 0; q < internals->nb_queues; q++) {
983 rte_free(internals->rx_queue[q].rd);
984 rte_free(internals->tx_queue[q].rd);
986 free(internals->if_name);
988 rte_free(eth_dev->data->dev_private);
989 rte_free(eth_dev->data);
991 rte_eth_dev_release_port(eth_dev);
996 static struct rte_vdev_driver pmd_af_packet_drv = {
997 .probe = rte_pmd_af_packet_probe,
998 .remove = rte_pmd_af_packet_remove,
1001 RTE_PMD_REGISTER_VDEV(net_af_packet, pmd_af_packet_drv);
1002 RTE_PMD_REGISTER_ALIAS(net_af_packet, eth_af_packet);
1003 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,