Commit 40808a23 authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Alexei Starovoitov

selftests/bpf: Add TX side to xdp_metadata

Request TX timestamp and make sure it's not empty.
Request TX checksum offload (SW-only) and make sure it's resolved
to the correct one.
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/r/20231127190319.1190813-12-sdf@google.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent f6642de0
...@@ -56,7 +56,8 @@ static int open_xsk(int ifindex, struct xsk *xsk) ...@@ -56,7 +56,8 @@ static int open_xsk(int ifindex, struct xsk *xsk)
.fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
.frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE, .frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG, .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG | XDP_UMEM_TX_SW_CSUM,
.tx_metadata_len = sizeof(struct xsk_tx_metadata),
}; };
__u32 idx; __u32 idx;
u64 addr; u64 addr;
...@@ -138,6 +139,7 @@ static void ip_csum(struct iphdr *iph) ...@@ -138,6 +139,7 @@ static void ip_csum(struct iphdr *iph)
static int generate_packet(struct xsk *xsk, __u16 dst_port) static int generate_packet(struct xsk *xsk, __u16 dst_port)
{ {
struct xsk_tx_metadata *meta;
struct xdp_desc *tx_desc; struct xdp_desc *tx_desc;
struct udphdr *udph; struct udphdr *udph;
struct ethhdr *eth; struct ethhdr *eth;
...@@ -151,10 +153,14 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port) ...@@ -151,10 +153,14 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
return -1; return -1;
tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx); tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx);
tx_desc->addr = idx % (UMEM_NUM / 2) * UMEM_FRAME_SIZE; tx_desc->addr = idx % (UMEM_NUM / 2) * UMEM_FRAME_SIZE + sizeof(struct xsk_tx_metadata);
printf("%p: tx_desc[%u]->addr=%llx\n", xsk, idx, tx_desc->addr); printf("%p: tx_desc[%u]->addr=%llx\n", xsk, idx, tx_desc->addr);
data = xsk_umem__get_data(xsk->umem_area, tx_desc->addr); data = xsk_umem__get_data(xsk->umem_area, tx_desc->addr);
meta = data - sizeof(struct xsk_tx_metadata);
memset(meta, 0, sizeof(*meta));
meta->flags = XDP_TXMD_FLAGS_TIMESTAMP;
eth = data; eth = data;
iph = (void *)(eth + 1); iph = (void *)(eth + 1);
udph = (void *)(iph + 1); udph = (void *)(iph + 1);
...@@ -178,11 +184,17 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port) ...@@ -178,11 +184,17 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
udph->source = htons(AF_XDP_SOURCE_PORT); udph->source = htons(AF_XDP_SOURCE_PORT);
udph->dest = htons(dst_port); udph->dest = htons(dst_port);
udph->len = htons(sizeof(*udph) + UDP_PAYLOAD_BYTES); udph->len = htons(sizeof(*udph) + UDP_PAYLOAD_BYTES);
udph->check = 0; udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
ntohs(udph->len), IPPROTO_UDP, 0);
memset(udph + 1, 0xAA, UDP_PAYLOAD_BYTES); memset(udph + 1, 0xAA, UDP_PAYLOAD_BYTES);
meta->flags |= XDP_TXMD_FLAGS_CHECKSUM;
meta->request.csum_start = sizeof(*eth) + sizeof(*iph);
meta->request.csum_offset = offsetof(struct udphdr, check);
tx_desc->len = sizeof(*eth) + sizeof(*iph) + sizeof(*udph) + UDP_PAYLOAD_BYTES; tx_desc->len = sizeof(*eth) + sizeof(*iph) + sizeof(*udph) + UDP_PAYLOAD_BYTES;
tx_desc->options |= XDP_TX_METADATA;
xsk_ring_prod__submit(&xsk->tx, 1); xsk_ring_prod__submit(&xsk->tx, 1);
ret = sendto(xsk_socket__fd(xsk->socket), NULL, 0, MSG_DONTWAIT, NULL, 0); ret = sendto(xsk_socket__fd(xsk->socket), NULL, 0, MSG_DONTWAIT, NULL, 0);
...@@ -194,13 +206,21 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port) ...@@ -194,13 +206,21 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
static void complete_tx(struct xsk *xsk) static void complete_tx(struct xsk *xsk)
{ {
__u32 idx; struct xsk_tx_metadata *meta;
__u64 addr; __u64 addr;
void *data;
__u32 idx;
if (ASSERT_EQ(xsk_ring_cons__peek(&xsk->comp, 1, &idx), 1, "xsk_ring_cons__peek")) { if (ASSERT_EQ(xsk_ring_cons__peek(&xsk->comp, 1, &idx), 1, "xsk_ring_cons__peek")) {
addr = *xsk_ring_cons__comp_addr(&xsk->comp, idx); addr = *xsk_ring_cons__comp_addr(&xsk->comp, idx);
printf("%p: complete tx idx=%u addr=%llx\n", xsk, idx, addr); printf("%p: complete tx idx=%u addr=%llx\n", xsk, idx, addr);
data = xsk_umem__get_data(xsk->umem_area, addr);
meta = data - sizeof(struct xsk_tx_metadata);
ASSERT_NEQ(meta->completion.tx_timestamp, 0, "tx_timestamp");
xsk_ring_cons__release(&xsk->comp, 1); xsk_ring_cons__release(&xsk->comp, 1);
} }
} }
...@@ -221,6 +241,7 @@ static int verify_xsk_metadata(struct xsk *xsk) ...@@ -221,6 +241,7 @@ static int verify_xsk_metadata(struct xsk *xsk)
const struct xdp_desc *rx_desc; const struct xdp_desc *rx_desc;
struct pollfd fds = {}; struct pollfd fds = {};
struct xdp_meta *meta; struct xdp_meta *meta;
struct udphdr *udph;
struct ethhdr *eth; struct ethhdr *eth;
struct iphdr *iph; struct iphdr *iph;
__u64 comp_addr; __u64 comp_addr;
...@@ -257,6 +278,7 @@ static int verify_xsk_metadata(struct xsk *xsk) ...@@ -257,6 +278,7 @@ static int verify_xsk_metadata(struct xsk *xsk)
ASSERT_EQ(eth->h_proto, htons(ETH_P_IP), "eth->h_proto"); ASSERT_EQ(eth->h_proto, htons(ETH_P_IP), "eth->h_proto");
iph = (void *)(eth + 1); iph = (void *)(eth + 1);
ASSERT_EQ((int)iph->version, 4, "iph->version"); ASSERT_EQ((int)iph->version, 4, "iph->version");
udph = (void *)(iph + 1);
/* custom metadata */ /* custom metadata */
...@@ -270,6 +292,9 @@ static int verify_xsk_metadata(struct xsk *xsk) ...@@ -270,6 +292,9 @@ static int verify_xsk_metadata(struct xsk *xsk)
ASSERT_EQ(meta->rx_hash_type, 0, "rx_hash_type"); ASSERT_EQ(meta->rx_hash_type, 0, "rx_hash_type");
/* checksum offload */
ASSERT_EQ(udph->check, htons(0x721c), "csum");
xsk_ring_cons__release(&xsk->rx, 1); xsk_ring_cons__release(&xsk->rx, 1);
refill_rx(xsk, comp_addr); refill_rx(xsk, comp_addr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment