The kernel lays out ring frames per block (packet_lookup_frame()), so
when the block size is not a multiple of the frame size each block ends
with unused space. The driver assumed frames were contiguous across the
whole ring, so every frame after the first block was read and written
at the wrong offset.

Compute frame addresses the same way the kernel does.

Fixes: 364e08f2bbc0 ("af_packet: add PMD for AF_PACKET-based virtual devices")
Cc: [email protected]
Cc: [email protected]

Signed-off-by: Scott Mitchell <[email protected]>
---
 drivers/net/af_packet/rte_eth_af_packet.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/net/af_packet/rte_eth_af_packet.c 
b/drivers/net/af_packet/rte_eth_af_packet.c
index 4a8afe08c7..11913e656d 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -909,6 +909,16 @@ get_fanout(const char *fanout_mode, int if_index)
                return PACKET_FANOUT_INVALID;
 }
 
+/* Address of ring frame i, as computed by the kernel's packet_lookup_frame() 
*/
+static uint8_t *
+tpacket_frame_addr(uint8_t *map, const struct tpacket_req *req, unsigned int i)
+{
+       unsigned int frames_per_block = req->tp_block_size / req->tp_frame_size;
+
+       return map + (size_t)(i / frames_per_block) * req->tp_block_size +
+               (size_t)(i % frames_per_block) * req->tp_frame_size;
+}
+
 static int
 rte_pmd_init_internals(struct rte_vdev_device *dev,
                       const int sockfd,
@@ -1132,7 +1142,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                if (rx_queue->rd == NULL)
                        goto error;
                for (i = 0; i < req->tp_frame_nr; ++i) {
-                       rx_queue->rd[i].iov_base = rx_queue->map + (i * 
framesize);
+                       rx_queue->rd[i].iov_base = 
tpacket_frame_addr(rx_queue->map, req, i);
                        rx_queue->rd[i].iov_len = req->tp_frame_size;
                }
                rx_queue->sockfd = qsockfd;
@@ -1149,7 +1159,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                if (tx_queue->rd == NULL)
                        goto error;
                for (i = 0; i < req->tp_frame_nr; ++i) {
-                       tx_queue->rd[i].iov_base = tx_queue->map + (i * 
framesize);
+                       tx_queue->rd[i].iov_base = 
tpacket_frame_addr(tx_queue->map, req, i);
                        tx_queue->rd[i].iov_len = req->tp_frame_size;
                }
                tx_queue->sockfd = qsockfd;
-- 
2.54.0 (Apple Git-157)

Reply via email to