Skip to content

Commit 8e68a4b

Browse files
walking-machineAlexei Starovoitov
authored andcommitted
selftests/bpf: Add flags and VLAN hint to xdp_hw_metadata
Add VLAN hint to the xdp_hw_metadata program. Also, to make metadata layout more straightforward, add flags field to pass information about validity of every separate hint separately. Acked-by: Stanislav Fomichev <[email protected]> Signed-off-by: Larysa Zaremba <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e71a9fa commit 8e68a4b

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

tools/testing/selftests/bpf/progs/xdp_hw_metadata.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
2020
__u64 *timestamp) __ksym;
2121
extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
2222
enum xdp_rss_hash_type *rss_type) __ksym;
23+
extern int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
24+
__be16 *vlan_proto,
25+
__u16 *vlan_tci) __ksym;
2326

2427
SEC("xdp.frags")
2528
int rx(struct xdp_md *ctx)
@@ -84,15 +87,28 @@ int rx(struct xdp_md *ctx)
8487
return XDP_PASS;
8588
}
8689

90+
meta->hint_valid = 0;
91+
92+
meta->xdp_timestamp = bpf_ktime_get_tai_ns();
8793
err = bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp);
88-
if (!err)
89-
meta->xdp_timestamp = bpf_ktime_get_tai_ns();
94+
if (err)
95+
meta->rx_timestamp_err = err;
96+
else
97+
meta->hint_valid |= XDP_META_FIELD_TS;
98+
99+
err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash,
100+
&meta->rx_hash_type);
101+
if (err)
102+
meta->rx_hash_err = err;
90103
else
91-
meta->rx_timestamp = 0; /* Used by AF_XDP as not avail signal */
104+
meta->hint_valid |= XDP_META_FIELD_RSS;
92105

93-
err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash, &meta->rx_hash_type);
94-
if (err < 0)
95-
meta->rx_hash_err = err; /* Used by AF_XDP as no hash signal */
106+
err = bpf_xdp_metadata_rx_vlan_tag(ctx, &meta->rx_vlan_proto,
107+
&meta->rx_vlan_tci);
108+
if (err)
109+
meta->rx_vlan_tag_err = err;
110+
else
111+
meta->hint_valid |= XDP_META_FIELD_VLAN_TAG;
96112

97113
__sync_add_and_fetch(&pkts_redir, 1);
98114
return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);

tools/testing/selftests/bpf/xdp_hw_metadata.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "xsk.h"
2222

2323
#include <error.h>
24+
#include <linux/kernel.h>
25+
#include <linux/bits.h>
26+
#include <linux/bitfield.h>
2427
#include <linux/errqueue.h>
2528
#include <linux/if_link.h>
2629
#include <linux/net_tstamp.h>
@@ -182,19 +185,31 @@ static void print_tstamp_delta(const char *name, const char *refname,
182185
(double)delta / 1000);
183186
}
184187

188+
#define VLAN_PRIO_MASK GENMASK(15, 13) /* Priority Code Point */
189+
#define VLAN_DEI_MASK GENMASK(12, 12) /* Drop Eligible Indicator */
190+
#define VLAN_VID_MASK GENMASK(11, 0) /* VLAN Identifier */
191+
static void print_vlan_tci(__u16 tag)
192+
{
193+
__u16 vlan_id = FIELD_GET(VLAN_VID_MASK, tag);
194+
__u8 pcp = FIELD_GET(VLAN_PRIO_MASK, tag);
195+
bool dei = FIELD_GET(VLAN_DEI_MASK, tag);
196+
197+
printf("PCP=%u, DEI=%d, VID=0x%X\n", pcp, dei, vlan_id);
198+
}
199+
185200
static void verify_xdp_metadata(void *data, clockid_t clock_id)
186201
{
187202
struct xdp_meta *meta;
188203

189204
meta = data - sizeof(*meta);
190205

191-
if (meta->rx_hash_err < 0)
192-
printf("No rx_hash err=%d\n", meta->rx_hash_err);
193-
else
206+
if (meta->hint_valid & XDP_META_FIELD_RSS)
194207
printf("rx_hash: 0x%X with RSS type:0x%X\n",
195208
meta->rx_hash, meta->rx_hash_type);
209+
else
210+
printf("No rx_hash, err=%d\n", meta->rx_hash_err);
196211

197-
if (meta->rx_timestamp) {
212+
if (meta->hint_valid & XDP_META_FIELD_TS) {
198213
__u64 ref_tstamp = gettime(clock_id);
199214

200215
/* store received timestamps to calculate a delta at tx */
@@ -206,7 +221,16 @@ static void verify_xdp_metadata(void *data, clockid_t clock_id)
206221
print_tstamp_delta("XDP RX-time", "User RX-time",
207222
meta->xdp_timestamp, ref_tstamp);
208223
} else {
209-
printf("No rx_timestamp\n");
224+
printf("No rx_timestamp, err=%d\n", meta->rx_timestamp_err);
225+
}
226+
227+
if (meta->hint_valid & XDP_META_FIELD_VLAN_TAG) {
228+
printf("rx_vlan_proto: 0x%X\n", ntohs(meta->rx_vlan_proto));
229+
printf("rx_vlan_tci: ");
230+
print_vlan_tci(meta->rx_vlan_tci);
231+
} else {
232+
printf("No rx_vlan_tci or rx_vlan_proto, err=%d\n",
233+
meta->rx_vlan_tag_err);
210234
}
211235
}
212236

tools/testing/selftests/bpf/xdp_metadata.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,36 @@
1717
#define ETH_P_8021AD 0x88A8
1818
#endif
1919

20+
#ifndef BIT
21+
#define BIT(nr) (1 << (nr))
22+
#endif
23+
24+
/* Non-existent checksum status */
25+
#define XDP_CHECKSUM_MAGIC BIT(2)
26+
27+
enum xdp_meta_field {
28+
XDP_META_FIELD_TS = BIT(0),
29+
XDP_META_FIELD_RSS = BIT(1),
30+
XDP_META_FIELD_VLAN_TAG = BIT(2),
31+
};
32+
2033
struct xdp_meta {
21-
__u64 rx_timestamp;
34+
union {
35+
__u64 rx_timestamp;
36+
__s32 rx_timestamp_err;
37+
};
2238
__u64 xdp_timestamp;
2339
__u32 rx_hash;
2440
union {
2541
__u32 rx_hash_type;
2642
__s32 rx_hash_err;
2743
};
44+
union {
45+
struct {
46+
__be16 rx_vlan_proto;
47+
__u16 rx_vlan_tci;
48+
};
49+
__s32 rx_vlan_tag_err;
50+
};
51+
enum xdp_meta_field hint_valid;
2852
};

0 commit comments

Comments
 (0)