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_ethdev_vdev.h>
42 #include <rte_malloc.h>
43 #include <rte_kvargs.h>
46 #include <linux/if_ether.h>
47 #include <linux/if_packet.h>
48 #include <arpa/inet.h>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
57 #define ETH_AF_PACKET_IFACE_ARG "iface"
58 #define ETH_AF_PACKET_NUM_Q_ARG "qpairs"
59 #define ETH_AF_PACKET_BLOCKSIZE_ARG "blocksz"
60 #define ETH_AF_PACKET_FRAMESIZE_ARG "framesz"
61 #define ETH_AF_PACKET_FRAMECOUNT_ARG "framecnt"
63 #define DFLT_BLOCK_SIZE (1 << 12)
64 #define DFLT_FRAME_SIZE (1 << 11)
65 #define DFLT_FRAME_COUNT (1 << 9)
67 #define RTE_PMD_AF_PACKET_MAX_RINGS 16
74 unsigned int framecount
;
75 unsigned int framenum
;
77 struct rte_mempool
*mb_pool
;
80 volatile unsigned long rx_pkts
;
81 volatile unsigned long err_pkts
;
82 volatile unsigned long rx_bytes
;
87 unsigned int frame_data_size
;
91 unsigned int framecount
;
92 unsigned int framenum
;
94 volatile unsigned long tx_pkts
;
95 volatile unsigned long err_pkts
;
96 volatile unsigned long tx_bytes
;
99 struct pmd_internals
{
104 struct ether_addr eth_addr
;
106 struct tpacket_req req
;
108 struct pkt_rx_queue rx_queue
[RTE_PMD_AF_PACKET_MAX_RINGS
];
109 struct pkt_tx_queue tx_queue
[RTE_PMD_AF_PACKET_MAX_RINGS
];
112 static const char *valid_arguments
[] = {
113 ETH_AF_PACKET_IFACE_ARG
,
114 ETH_AF_PACKET_NUM_Q_ARG
,
115 ETH_AF_PACKET_BLOCKSIZE_ARG
,
116 ETH_AF_PACKET_FRAMESIZE_ARG
,
117 ETH_AF_PACKET_FRAMECOUNT_ARG
,
121 static struct rte_eth_link pmd_link
= {
122 .link_speed
= ETH_SPEED_NUM_10G
,
123 .link_duplex
= ETH_LINK_FULL_DUPLEX
,
124 .link_status
= ETH_LINK_DOWN
,
125 .link_autoneg
= ETH_LINK_SPEED_AUTONEG
129 eth_af_packet_rx(void *queue
, struct rte_mbuf
**bufs
, uint16_t nb_pkts
)
132 struct tpacket2_hdr
*ppd
;
133 struct rte_mbuf
*mbuf
;
135 struct pkt_rx_queue
*pkt_q
= queue
;
137 unsigned long num_rx_bytes
= 0;
138 unsigned int framecount
, framenum
;
140 if (unlikely(nb_pkts
== 0))
144 * Reads the given number of packets from the AF_PACKET socket one by
145 * one and copies the packet data into a newly allocated mbuf.
147 framecount
= pkt_q
->framecount
;
148 framenum
= pkt_q
->framenum
;
149 for (i
= 0; i
< nb_pkts
; i
++) {
150 /* point at the next incoming frame */
151 ppd
= (struct tpacket2_hdr
*) pkt_q
->rd
[framenum
].iov_base
;
152 if ((ppd
->tp_status
& TP_STATUS_USER
) == 0)
155 /* allocate the next mbuf */
156 mbuf
= rte_pktmbuf_alloc(pkt_q
->mb_pool
);
157 if (unlikely(mbuf
== NULL
))
160 /* packet will fit in the mbuf, go ahead and receive it */
161 rte_pktmbuf_pkt_len(mbuf
) = rte_pktmbuf_data_len(mbuf
) = ppd
->tp_snaplen
;
162 pbuf
= (uint8_t *) ppd
+ ppd
->tp_mac
;
163 memcpy(rte_pktmbuf_mtod(mbuf
, void *), pbuf
, rte_pktmbuf_data_len(mbuf
));
165 /* check for vlan info */
166 if (ppd
->tp_status
& TP_STATUS_VLAN_VALID
) {
167 mbuf
->vlan_tci
= ppd
->tp_vlan_tci
;
168 mbuf
->ol_flags
|= (PKT_RX_VLAN_PKT
| PKT_RX_VLAN_STRIPPED
);
171 /* release incoming frame and advance ring buffer */
172 ppd
->tp_status
= TP_STATUS_KERNEL
;
173 if (++framenum
>= framecount
)
175 mbuf
->port
= pkt_q
->in_port
;
177 /* account for the receive frame */
180 num_rx_bytes
+= mbuf
->pkt_len
;
182 pkt_q
->framenum
= framenum
;
183 pkt_q
->rx_pkts
+= num_rx
;
184 pkt_q
->rx_bytes
+= num_rx_bytes
;
189 * Callback to handle sending packets through a real NIC.
192 eth_af_packet_tx(void *queue
, struct rte_mbuf
**bufs
, uint16_t nb_pkts
)
194 struct tpacket2_hdr
*ppd
;
195 struct rte_mbuf
*mbuf
;
197 unsigned int framecount
, framenum
;
199 struct pkt_tx_queue
*pkt_q
= queue
;
201 unsigned long num_tx_bytes
= 0;
204 if (unlikely(nb_pkts
== 0))
207 memset(&pfd
, 0, sizeof(pfd
));
208 pfd
.fd
= pkt_q
->sockfd
;
209 pfd
.events
= POLLOUT
;
212 framecount
= pkt_q
->framecount
;
213 framenum
= pkt_q
->framenum
;
214 ppd
= (struct tpacket2_hdr
*) pkt_q
->rd
[framenum
].iov_base
;
215 for (i
= 0; i
< nb_pkts
; i
++) {
218 /* drop oversized packets */
219 if (rte_pktmbuf_data_len(mbuf
) > pkt_q
->frame_data_size
) {
220 rte_pktmbuf_free(mbuf
);
224 /* insert vlan info if necessary */
225 if (mbuf
->ol_flags
& PKT_TX_VLAN_PKT
) {
226 if (rte_vlan_insert(&mbuf
)) {
227 rte_pktmbuf_free(mbuf
);
232 /* point at the next incoming frame */
233 if ((ppd
->tp_status
!= TP_STATUS_AVAILABLE
) &&
234 (poll(&pfd
, 1, -1) < 0))
237 /* copy the tx frame data */
238 pbuf
= (uint8_t *) ppd
+ TPACKET2_HDRLEN
-
239 sizeof(struct sockaddr_ll
);
240 memcpy(pbuf
, rte_pktmbuf_mtod(mbuf
, void*), rte_pktmbuf_data_len(mbuf
));
241 ppd
->tp_len
= ppd
->tp_snaplen
= rte_pktmbuf_data_len(mbuf
);
243 /* release incoming frame and advance ring buffer */
244 ppd
->tp_status
= TP_STATUS_SEND_REQUEST
;
245 if (++framenum
>= framecount
)
247 ppd
= (struct tpacket2_hdr
*) pkt_q
->rd
[framenum
].iov_base
;
250 num_tx_bytes
+= mbuf
->pkt_len
;
251 rte_pktmbuf_free(mbuf
);
254 /* kick-off transmits */
255 if (sendto(pkt_q
->sockfd
, NULL
, 0, MSG_DONTWAIT
, NULL
, 0) == -1)
256 num_tx
= 0; /* error sending -- no packets transmitted */
258 pkt_q
->framenum
= framenum
;
259 pkt_q
->tx_pkts
+= num_tx
;
260 pkt_q
->err_pkts
+= i
- num_tx
;
261 pkt_q
->tx_bytes
+= num_tx_bytes
;
266 eth_dev_start(struct rte_eth_dev
*dev
)
268 dev
->data
->dev_link
.link_status
= ETH_LINK_UP
;
273 * This function gets called when the current port gets stopped.
276 eth_dev_stop(struct rte_eth_dev
*dev
)
280 struct pmd_internals
*internals
= dev
->data
->dev_private
;
282 for (i
= 0; i
< internals
->nb_queues
; i
++) {
283 sockfd
= internals
->rx_queue
[i
].sockfd
;
287 /* Prevent use after free in case tx fd == rx fd */
288 if (sockfd
!= internals
->tx_queue
[i
].sockfd
) {
289 sockfd
= internals
->tx_queue
[i
].sockfd
;
294 internals
->rx_queue
[i
].sockfd
= -1;
295 internals
->tx_queue
[i
].sockfd
= -1;
298 dev
->data
->dev_link
.link_status
= ETH_LINK_DOWN
;
302 eth_dev_configure(struct rte_eth_dev
*dev __rte_unused
)
308 eth_dev_info(struct rte_eth_dev
*dev
, struct rte_eth_dev_info
*dev_info
)
310 struct pmd_internals
*internals
= dev
->data
->dev_private
;
312 dev_info
->if_index
= internals
->if_index
;
313 dev_info
->max_mac_addrs
= 1;
314 dev_info
->max_rx_pktlen
= (uint32_t)ETH_FRAME_LEN
;
315 dev_info
->max_rx_queues
= (uint16_t)internals
->nb_queues
;
316 dev_info
->max_tx_queues
= (uint16_t)internals
->nb_queues
;
317 dev_info
->min_rx_bufsize
= 0;
321 eth_stats_get(struct rte_eth_dev
*dev
, struct rte_eth_stats
*igb_stats
)
324 unsigned long rx_total
= 0, tx_total
= 0, tx_err_total
= 0;
325 unsigned long rx_bytes_total
= 0, tx_bytes_total
= 0;
326 const struct pmd_internals
*internal
= dev
->data
->dev_private
;
328 imax
= (internal
->nb_queues
< RTE_ETHDEV_QUEUE_STAT_CNTRS
?
329 internal
->nb_queues
: RTE_ETHDEV_QUEUE_STAT_CNTRS
);
330 for (i
= 0; i
< imax
; i
++) {
331 igb_stats
->q_ipackets
[i
] = internal
->rx_queue
[i
].rx_pkts
;
332 igb_stats
->q_ibytes
[i
] = internal
->rx_queue
[i
].rx_bytes
;
333 rx_total
+= igb_stats
->q_ipackets
[i
];
334 rx_bytes_total
+= igb_stats
->q_ibytes
[i
];
337 imax
= (internal
->nb_queues
< RTE_ETHDEV_QUEUE_STAT_CNTRS
?
338 internal
->nb_queues
: RTE_ETHDEV_QUEUE_STAT_CNTRS
);
339 for (i
= 0; i
< imax
; i
++) {
340 igb_stats
->q_opackets
[i
] = internal
->tx_queue
[i
].tx_pkts
;
341 igb_stats
->q_errors
[i
] = internal
->tx_queue
[i
].err_pkts
;
342 igb_stats
->q_obytes
[i
] = internal
->tx_queue
[i
].tx_bytes
;
343 tx_total
+= igb_stats
->q_opackets
[i
];
344 tx_err_total
+= igb_stats
->q_errors
[i
];
345 tx_bytes_total
+= igb_stats
->q_obytes
[i
];
348 igb_stats
->ipackets
= rx_total
;
349 igb_stats
->ibytes
= rx_bytes_total
;
350 igb_stats
->opackets
= tx_total
;
351 igb_stats
->oerrors
= tx_err_total
;
352 igb_stats
->obytes
= tx_bytes_total
;
356 eth_stats_reset(struct rte_eth_dev
*dev
)
359 struct pmd_internals
*internal
= dev
->data
->dev_private
;
361 for (i
= 0; i
< internal
->nb_queues
; i
++) {
362 internal
->rx_queue
[i
].rx_pkts
= 0;
363 internal
->rx_queue
[i
].rx_bytes
= 0;
366 for (i
= 0; i
< internal
->nb_queues
; i
++) {
367 internal
->tx_queue
[i
].tx_pkts
= 0;
368 internal
->tx_queue
[i
].err_pkts
= 0;
369 internal
->tx_queue
[i
].tx_bytes
= 0;
374 eth_dev_close(struct rte_eth_dev
*dev __rte_unused
)
379 eth_queue_release(void *q __rte_unused
)
384 eth_link_update(struct rte_eth_dev
*dev __rte_unused
,
385 int wait_to_complete __rte_unused
)
391 eth_rx_queue_setup(struct rte_eth_dev
*dev
,
392 uint16_t rx_queue_id
,
393 uint16_t nb_rx_desc __rte_unused
,
394 unsigned int socket_id __rte_unused
,
395 const struct rte_eth_rxconf
*rx_conf __rte_unused
,
396 struct rte_mempool
*mb_pool
)
398 struct pmd_internals
*internals
= dev
->data
->dev_private
;
399 struct pkt_rx_queue
*pkt_q
= &internals
->rx_queue
[rx_queue_id
];
400 unsigned int buf_size
, data_size
;
402 pkt_q
->mb_pool
= mb_pool
;
404 /* Now get the space available for data in the mbuf */
405 buf_size
= rte_pktmbuf_data_room_size(pkt_q
->mb_pool
) -
406 RTE_PKTMBUF_HEADROOM
;
407 data_size
= internals
->req
.tp_frame_size
;
408 data_size
-= TPACKET2_HDRLEN
- sizeof(struct sockaddr_ll
);
410 if (data_size
> buf_size
) {
412 "%s: %d bytes will not fit in mbuf (%d bytes)\n",
413 dev
->data
->name
, data_size
, buf_size
);
417 dev
->data
->rx_queues
[rx_queue_id
] = pkt_q
;
418 pkt_q
->in_port
= dev
->data
->port_id
;
424 eth_tx_queue_setup(struct rte_eth_dev
*dev
,
425 uint16_t tx_queue_id
,
426 uint16_t nb_tx_desc __rte_unused
,
427 unsigned int socket_id __rte_unused
,
428 const struct rte_eth_txconf
*tx_conf __rte_unused
)
431 struct pmd_internals
*internals
= dev
->data
->dev_private
;
433 dev
->data
->tx_queues
[tx_queue_id
] = &internals
->tx_queue
[tx_queue_id
];
438 eth_dev_mtu_set(struct rte_eth_dev
*dev
, uint16_t mtu
)
440 struct pmd_internals
*internals
= dev
->data
->dev_private
;
441 struct ifreq ifr
= { .ifr_mtu
= mtu
};
444 unsigned int data_size
= internals
->req
.tp_frame_size
-
446 sizeof(struct sockaddr_ll
);
451 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
455 snprintf(ifr
.ifr_name
, IFNAMSIZ
, "%s", internals
->if_name
);
456 ret
= ioctl(s
, SIOCSIFMTU
, &ifr
);
466 eth_dev_change_flags(char *if_name
, uint32_t flags
, uint32_t mask
)
471 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
475 snprintf(ifr
.ifr_name
, IFNAMSIZ
, "%s", if_name
);
476 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) < 0)
478 ifr
.ifr_flags
&= mask
;
479 ifr
.ifr_flags
|= flags
;
480 if (ioctl(s
, SIOCSIFFLAGS
, &ifr
) < 0)
487 eth_dev_promiscuous_enable(struct rte_eth_dev
*dev
)
489 struct pmd_internals
*internals
= dev
->data
->dev_private
;
491 eth_dev_change_flags(internals
->if_name
, IFF_PROMISC
, ~0);
495 eth_dev_promiscuous_disable(struct rte_eth_dev
*dev
)
497 struct pmd_internals
*internals
= dev
->data
->dev_private
;
499 eth_dev_change_flags(internals
->if_name
, 0, ~IFF_PROMISC
);
502 static const struct eth_dev_ops ops
= {
503 .dev_start
= eth_dev_start
,
504 .dev_stop
= eth_dev_stop
,
505 .dev_close
= eth_dev_close
,
506 .dev_configure
= eth_dev_configure
,
507 .dev_infos_get
= eth_dev_info
,
508 .mtu_set
= eth_dev_mtu_set
,
509 .promiscuous_enable
= eth_dev_promiscuous_enable
,
510 .promiscuous_disable
= eth_dev_promiscuous_disable
,
511 .rx_queue_setup
= eth_rx_queue_setup
,
512 .tx_queue_setup
= eth_tx_queue_setup
,
513 .rx_queue_release
= eth_queue_release
,
514 .tx_queue_release
= eth_queue_release
,
515 .link_update
= eth_link_update
,
516 .stats_get
= eth_stats_get
,
517 .stats_reset
= eth_stats_reset
,
521 * Opens an AF_PACKET socket
524 open_packet_iface(const char *key __rte_unused
,
525 const char *value __rte_unused
,
528 int *sockfd
= extra_args
;
530 /* Open an AF_PACKET socket... */
531 *sockfd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
533 RTE_LOG(ERR
, PMD
, "Could not open AF_PACKET socket\n");
540 static struct rte_vdev_driver pmd_af_packet_drv
;
543 rte_pmd_init_internals(struct rte_vdev_device
*dev
,
545 const unsigned nb_queues
,
546 unsigned int blocksize
,
547 unsigned int blockcnt
,
548 unsigned int framesize
,
549 unsigned int framecnt
,
550 struct pmd_internals
**internals
,
551 struct rte_eth_dev
**eth_dev
,
552 struct rte_kvargs
*kvlist
)
554 const char *name
= rte_vdev_device_name(dev
);
555 const unsigned int numa_node
= dev
->device
.numa_node
;
556 struct rte_eth_dev_data
*data
= NULL
;
557 struct rte_kvargs_pair
*pair
= NULL
;
561 struct sockaddr_ll sockaddr
;
562 struct tpacket_req
*req
;
563 struct pkt_rx_queue
*rx_queue
;
564 struct pkt_tx_queue
*tx_queue
;
565 int rc
, tpver
, discard
;
567 unsigned int i
, q
, rdsize
;
568 int fanout_arg __rte_unused
, bypass __rte_unused
;
570 for (k_idx
= 0; k_idx
< kvlist
->count
; k_idx
++) {
571 pair
= &kvlist
->pairs
[k_idx
];
572 if (strstr(pair
->key
, ETH_AF_PACKET_IFACE_ARG
) != NULL
)
577 "%s: no interface specified for AF_PACKET ethdev\n",
583 "%s: creating AF_PACKET-backed ethdev on numa socket %u\n",
587 * now do all data allocation - for eth_dev structure, dummy pci driver
588 * and internal (private) data
590 data
= rte_zmalloc_socket(name
, sizeof(*data
), 0, numa_node
);
594 *internals
= rte_zmalloc_socket(name
, sizeof(**internals
),
596 if (*internals
== NULL
)
599 for (q
= 0; q
< nb_queues
; q
++) {
600 (*internals
)->rx_queue
[q
].map
= MAP_FAILED
;
601 (*internals
)->tx_queue
[q
].map
= MAP_FAILED
;
604 req
= &((*internals
)->req
);
606 req
->tp_block_size
= blocksize
;
607 req
->tp_block_nr
= blockcnt
;
608 req
->tp_frame_size
= framesize
;
609 req
->tp_frame_nr
= framecnt
;
611 ifnamelen
= strlen(pair
->value
);
612 if (ifnamelen
< sizeof(ifr
.ifr_name
)) {
613 memcpy(ifr
.ifr_name
, pair
->value
, ifnamelen
);
614 ifr
.ifr_name
[ifnamelen
] = '\0';
617 "%s: I/F name too long (%s)\n",
621 if (ioctl(sockfd
, SIOCGIFINDEX
, &ifr
) == -1) {
623 "%s: ioctl failed (SIOCGIFINDEX)\n",
627 (*internals
)->if_name
= strdup(pair
->value
);
628 (*internals
)->if_index
= ifr
.ifr_ifindex
;
630 if (ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
) == -1) {
632 "%s: ioctl failed (SIOCGIFHWADDR)\n",
636 memcpy(&(*internals
)->eth_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
638 memset(&sockaddr
, 0, sizeof(sockaddr
));
639 sockaddr
.sll_family
= AF_PACKET
;
640 sockaddr
.sll_protocol
= htons(ETH_P_ALL
);
641 sockaddr
.sll_ifindex
= (*internals
)->if_index
;
643 #if defined(PACKET_FANOUT)
644 fanout_arg
= (getpid() ^ (*internals
)->if_index
) & 0xffff;
645 fanout_arg
|= (PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_DEFRAG
) << 16;
646 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
647 fanout_arg
|= PACKET_FANOUT_FLAG_ROLLOVER
<< 16;
651 for (q
= 0; q
< nb_queues
; q
++) {
652 /* Open an AF_PACKET socket for this queue... */
653 qsockfd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
656 "%s: could not open AF_PACKET socket\n",
662 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_VERSION
,
663 &tpver
, sizeof(tpver
));
666 "%s: could not set PACKET_VERSION on AF_PACKET "
667 "socket for %s\n", name
, pair
->value
);
672 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_LOSS
,
673 &discard
, sizeof(discard
));
676 "%s: could not set PACKET_LOSS on "
677 "AF_PACKET socket for %s\n", name
, pair
->value
);
681 #if defined(PACKET_QDISC_BYPASS)
683 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_QDISC_BYPASS
,
684 &bypass
, sizeof(bypass
));
687 "%s: could not set PACKET_QDISC_BYPASS "
688 "on AF_PACKET socket for %s\n", name
,
694 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_RX_RING
, req
, sizeof(*req
));
697 "%s: could not set PACKET_RX_RING on AF_PACKET "
698 "socket for %s\n", name
, pair
->value
);
702 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_TX_RING
, req
, sizeof(*req
));
705 "%s: could not set PACKET_TX_RING on AF_PACKET "
706 "socket for %s\n", name
, pair
->value
);
710 rx_queue
= &((*internals
)->rx_queue
[q
]);
711 rx_queue
->framecount
= req
->tp_frame_nr
;
713 rx_queue
->map
= mmap(NULL
, 2 * req
->tp_block_size
* req
->tp_block_nr
,
714 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_LOCKED
,
716 if (rx_queue
->map
== MAP_FAILED
) {
718 "%s: call to mmap failed on AF_PACKET socket for %s\n",
723 /* rdsize is same for both Tx and Rx */
724 rdsize
= req
->tp_frame_nr
* sizeof(*(rx_queue
->rd
));
726 rx_queue
->rd
= rte_zmalloc_socket(name
, rdsize
, 0, numa_node
);
727 if (rx_queue
->rd
== NULL
)
729 for (i
= 0; i
< req
->tp_frame_nr
; ++i
) {
730 rx_queue
->rd
[i
].iov_base
= rx_queue
->map
+ (i
* framesize
);
731 rx_queue
->rd
[i
].iov_len
= req
->tp_frame_size
;
733 rx_queue
->sockfd
= qsockfd
;
735 tx_queue
= &((*internals
)->tx_queue
[q
]);
736 tx_queue
->framecount
= req
->tp_frame_nr
;
737 tx_queue
->frame_data_size
= req
->tp_frame_size
;
738 tx_queue
->frame_data_size
-= TPACKET2_HDRLEN
-
739 sizeof(struct sockaddr_ll
);
741 tx_queue
->map
= rx_queue
->map
+ req
->tp_block_size
* req
->tp_block_nr
;
743 tx_queue
->rd
= rte_zmalloc_socket(name
, rdsize
, 0, numa_node
);
744 if (tx_queue
->rd
== NULL
)
746 for (i
= 0; i
< req
->tp_frame_nr
; ++i
) {
747 tx_queue
->rd
[i
].iov_base
= tx_queue
->map
+ (i
* framesize
);
748 tx_queue
->rd
[i
].iov_len
= req
->tp_frame_size
;
750 tx_queue
->sockfd
= qsockfd
;
752 rc
= bind(qsockfd
, (const struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
755 "%s: could not bind AF_PACKET socket to %s\n",
760 #if defined(PACKET_FANOUT)
761 rc
= setsockopt(qsockfd
, SOL_PACKET
, PACKET_FANOUT
,
762 &fanout_arg
, sizeof(fanout_arg
));
765 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
766 "for %s\n", name
, pair
->value
);
772 /* reserve an ethdev entry */
773 *eth_dev
= rte_eth_vdev_allocate(dev
, 0);
774 if (*eth_dev
== NULL
)
778 * now put it all together
779 * - store queue data in internals,
780 * - store numa_node in eth_dev
781 * - point eth_dev_data to internals
782 * - and point eth_dev structure to new eth_dev_data structure
785 (*internals
)->nb_queues
= nb_queues
;
787 rte_memcpy(data
, (*eth_dev
)->data
, sizeof(*data
));
788 data
->dev_private
= *internals
;
789 data
->nb_rx_queues
= (uint16_t)nb_queues
;
790 data
->nb_tx_queues
= (uint16_t)nb_queues
;
791 data
->dev_link
= pmd_link
;
792 data
->mac_addrs
= &(*internals
)->eth_addr
;
794 (*eth_dev
)->data
= data
;
795 (*eth_dev
)->dev_ops
= &ops
;
796 (*eth_dev
)->data
->dev_flags
= RTE_ETH_DEV_DETACHABLE
;
803 for (q
= 0; q
< nb_queues
; q
++) {
804 munmap((*internals
)->rx_queue
[q
].map
,
805 2 * req
->tp_block_size
* req
->tp_block_nr
);
807 rte_free((*internals
)->rx_queue
[q
].rd
);
808 rte_free((*internals
)->tx_queue
[q
].rd
);
809 if (((*internals
)->rx_queue
[q
].sockfd
!= 0) &&
810 ((*internals
)->rx_queue
[q
].sockfd
!= qsockfd
))
811 close((*internals
)->rx_queue
[q
].sockfd
);
813 free((*internals
)->if_name
);
814 rte_free(*internals
);
821 rte_eth_from_packet(struct rte_vdev_device
*dev
,
823 struct rte_kvargs
*kvlist
)
825 const char *name
= rte_vdev_device_name(dev
);
826 struct pmd_internals
*internals
= NULL
;
827 struct rte_eth_dev
*eth_dev
= NULL
;
828 struct rte_kvargs_pair
*pair
= NULL
;
830 unsigned int blockcount
;
831 unsigned int blocksize
= DFLT_BLOCK_SIZE
;
832 unsigned int framesize
= DFLT_FRAME_SIZE
;
833 unsigned int framecount
= DFLT_FRAME_COUNT
;
834 unsigned int qpairs
= 1;
836 /* do some parameter checking */
841 * Walk arguments for configurable settings
843 for (k_idx
= 0; k_idx
< kvlist
->count
; k_idx
++) {
844 pair
= &kvlist
->pairs
[k_idx
];
845 if (strstr(pair
->key
, ETH_AF_PACKET_NUM_Q_ARG
) != NULL
) {
846 qpairs
= atoi(pair
->value
);
848 qpairs
> RTE_PMD_AF_PACKET_MAX_RINGS
) {
850 "%s: invalid qpairs value\n",
856 if (strstr(pair
->key
, ETH_AF_PACKET_BLOCKSIZE_ARG
) != NULL
) {
857 blocksize
= atoi(pair
->value
);
860 "%s: invalid blocksize value\n",
866 if (strstr(pair
->key
, ETH_AF_PACKET_FRAMESIZE_ARG
) != NULL
) {
867 framesize
= atoi(pair
->value
);
870 "%s: invalid framesize value\n",
876 if (strstr(pair
->key
, ETH_AF_PACKET_FRAMECOUNT_ARG
) != NULL
) {
877 framecount
= atoi(pair
->value
);
880 "%s: invalid framecount value\n",
888 if (framesize
> blocksize
) {
890 "%s: AF_PACKET MMAP frame size exceeds block size!\n",
895 blockcount
= framecount
/ (blocksize
/ framesize
);
898 "%s: invalid AF_PACKET MMAP parameters\n", name
);
902 RTE_LOG(INFO
, PMD
, "%s: AF_PACKET MMAP parameters:\n", name
);
903 RTE_LOG(INFO
, PMD
, "%s:\tblock size %d\n", name
, blocksize
);
904 RTE_LOG(INFO
, PMD
, "%s:\tblock count %d\n", name
, blockcount
);
905 RTE_LOG(INFO
, PMD
, "%s:\tframe size %d\n", name
, framesize
);
906 RTE_LOG(INFO
, PMD
, "%s:\tframe count %d\n", name
, framecount
);
908 if (rte_pmd_init_internals(dev
, *sockfd
, qpairs
,
909 blocksize
, blockcount
,
910 framesize
, framecount
,
911 &internals
, ð_dev
,
915 eth_dev
->rx_pkt_burst
= eth_af_packet_rx
;
916 eth_dev
->tx_pkt_burst
= eth_af_packet_tx
;
922 rte_pmd_af_packet_probe(struct rte_vdev_device
*dev
)
925 struct rte_kvargs
*kvlist
;
928 RTE_LOG(INFO
, PMD
, "Initializing pmd_af_packet for %s\n",
929 rte_vdev_device_name(dev
));
931 kvlist
= rte_kvargs_parse(rte_vdev_device_args(dev
), valid_arguments
);
932 if (kvlist
== NULL
) {
938 * If iface argument is passed we open the NICs and use them for
941 if (rte_kvargs_count(kvlist
, ETH_AF_PACKET_IFACE_ARG
) == 1) {
943 ret
= rte_kvargs_process(kvlist
, ETH_AF_PACKET_IFACE_ARG
,
944 &open_packet_iface
, &sockfd
);
949 if (dev
->device
.numa_node
== SOCKET_ID_ANY
)
950 dev
->device
.numa_node
= rte_socket_id();
952 ret
= rte_eth_from_packet(dev
, &sockfd
, kvlist
);
953 close(sockfd
); /* no longer needed */
956 rte_kvargs_free(kvlist
);
961 rte_pmd_af_packet_remove(struct rte_vdev_device
*dev
)
963 struct rte_eth_dev
*eth_dev
= NULL
;
964 struct pmd_internals
*internals
;
967 RTE_LOG(INFO
, PMD
, "Closing AF_PACKET ethdev on numa socket %u\n",
973 /* find the ethdev entry */
974 eth_dev
= rte_eth_dev_allocated(rte_vdev_device_name(dev
));
978 internals
= eth_dev
->data
->dev_private
;
979 for (q
= 0; q
< internals
->nb_queues
; q
++) {
980 rte_free(internals
->rx_queue
[q
].rd
);
981 rte_free(internals
->tx_queue
[q
].rd
);
983 free(internals
->if_name
);
985 rte_free(eth_dev
->data
->dev_private
);
986 rte_free(eth_dev
->data
);
988 rte_eth_dev_release_port(eth_dev
);
993 static struct rte_vdev_driver pmd_af_packet_drv
= {
994 .probe
= rte_pmd_af_packet_probe
,
995 .remove
= rte_pmd_af_packet_remove
,
998 RTE_PMD_REGISTER_VDEV(net_af_packet
, pmd_af_packet_drv
);
999 RTE_PMD_REGISTER_ALIAS(net_af_packet
, eth_af_packet
);
1000 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet
,