Skip to content

Commit 40808a2

Browse files
fomichevAlexei Starovoitov
authored andcommitted
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: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f6642de commit 40808a2

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

tools/testing/selftests/bpf/prog_tests/xdp_metadata.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static int open_xsk(int ifindex, struct xsk *xsk)
5656
.fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
5757
.comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
5858
.frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
59-
.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG,
59+
.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG | XDP_UMEM_TX_SW_CSUM,
60+
.tx_metadata_len = sizeof(struct xsk_tx_metadata),
6061
};
6162
__u32 idx;
6263
u64 addr;
@@ -138,6 +139,7 @@ static void ip_csum(struct iphdr *iph)
138139

139140
static int generate_packet(struct xsk *xsk, __u16 dst_port)
140141
{
142+
struct xsk_tx_metadata *meta;
141143
struct xdp_desc *tx_desc;
142144
struct udphdr *udph;
143145
struct ethhdr *eth;
@@ -151,10 +153,14 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
151153
return -1;
152154

153155
tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx);
154-
tx_desc->addr = idx % (UMEM_NUM / 2) * UMEM_FRAME_SIZE;
156+
tx_desc->addr = idx % (UMEM_NUM / 2) * UMEM_FRAME_SIZE + sizeof(struct xsk_tx_metadata);
155157
printf("%p: tx_desc[%u]->addr=%llx\n", xsk, idx, tx_desc->addr);
156158
data = xsk_umem__get_data(xsk->umem_area, tx_desc->addr);
157159

160+
meta = data - sizeof(struct xsk_tx_metadata);
161+
memset(meta, 0, sizeof(*meta));
162+
meta->flags = XDP_TXMD_FLAGS_TIMESTAMP;
163+
158164
eth = data;
159165
iph = (void *)(eth + 1);
160166
udph = (void *)(iph + 1);
@@ -178,11 +184,17 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
178184
udph->source = htons(AF_XDP_SOURCE_PORT);
179185
udph->dest = htons(dst_port);
180186
udph->len = htons(sizeof(*udph) + UDP_PAYLOAD_BYTES);
181-
udph->check = 0;
187+
udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
188+
ntohs(udph->len), IPPROTO_UDP, 0);
182189

183190
memset(udph + 1, 0xAA, UDP_PAYLOAD_BYTES);
184191

192+
meta->flags |= XDP_TXMD_FLAGS_CHECKSUM;
193+
meta->request.csum_start = sizeof(*eth) + sizeof(*iph);
194+
meta->request.csum_offset = offsetof(struct udphdr, check);
195+
185196
tx_desc->len = sizeof(*eth) + sizeof(*iph) + sizeof(*udph) + UDP_PAYLOAD_BYTES;
197+
tx_desc->options |= XDP_TX_METADATA;
186198
xsk_ring_prod__submit(&xsk->tx, 1);
187199

188200
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)
194206

195207
static void complete_tx(struct xsk *xsk)
196208
{
197-
__u32 idx;
209+
struct xsk_tx_metadata *meta;
198210
__u64 addr;
211+
void *data;
212+
__u32 idx;
199213

200214
if (ASSERT_EQ(xsk_ring_cons__peek(&xsk->comp, 1, &idx), 1, "xsk_ring_cons__peek")) {
201215
addr = *xsk_ring_cons__comp_addr(&xsk->comp, idx);
202216

203217
printf("%p: complete tx idx=%u addr=%llx\n", xsk, idx, addr);
218+
219+
data = xsk_umem__get_data(xsk->umem_area, addr);
220+
meta = data - sizeof(struct xsk_tx_metadata);
221+
222+
ASSERT_NEQ(meta->completion.tx_timestamp, 0, "tx_timestamp");
223+
204224
xsk_ring_cons__release(&xsk->comp, 1);
205225
}
206226
}
@@ -221,6 +241,7 @@ static int verify_xsk_metadata(struct xsk *xsk)
221241
const struct xdp_desc *rx_desc;
222242
struct pollfd fds = {};
223243
struct xdp_meta *meta;
244+
struct udphdr *udph;
224245
struct ethhdr *eth;
225246
struct iphdr *iph;
226247
__u64 comp_addr;
@@ -257,6 +278,7 @@ static int verify_xsk_metadata(struct xsk *xsk)
257278
ASSERT_EQ(eth->h_proto, htons(ETH_P_IP), "eth->h_proto");
258279
iph = (void *)(eth + 1);
259280
ASSERT_EQ((int)iph->version, 4, "iph->version");
281+
udph = (void *)(iph + 1);
260282

261283
/* custom metadata */
262284

@@ -270,6 +292,9 @@ static int verify_xsk_metadata(struct xsk *xsk)
270292

271293
ASSERT_EQ(meta->rx_hash_type, 0, "rx_hash_type");
272294

295+
/* checksum offload */
296+
ASSERT_EQ(udph->check, htons(0x721c), "csum");
297+
273298
xsk_ring_cons__release(&xsk->rx, 1);
274299
refill_rx(xsk, comp_addr);
275300

0 commit comments

Comments
 (0)