Skip to content

Commit 86870d0

Browse files
author
Martin KaFai Lau
committed
Merge branch 'bpf-allow-xdp_redirect-for-xdp-dev-bound-programs'
Lorenzo Bianconi says: ==================== bpf: Allow XDP_REDIRECT for XDP dev-bound programs In the current implementation if the program is dev-bound to a specific device, it will not be possible to perform XDP_REDIRECT into a DEVMAP or CPUMAP even if the program is running in the driver NAPI context. Fix the issue introducing __bpf_prog_map_compatible utility routine in order to avoid bpf_prog_is_dev_bound() during the XDP program load. Continue forbidding to attach a dev-bound program to XDP maps. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 7b05f43 + 3678331 commit 86870d0

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

kernel/bpf/core.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -2358,8 +2358,8 @@ static unsigned int __bpf_prog_ret0_warn(const void *ctx,
23582358
return 0;
23592359
}
23602360

2361-
bool bpf_prog_map_compatible(struct bpf_map *map,
2362-
const struct bpf_prog *fp)
2361+
static bool __bpf_prog_map_compatible(struct bpf_map *map,
2362+
const struct bpf_prog *fp)
23632363
{
23642364
enum bpf_prog_type prog_type = resolve_prog_type(fp);
23652365
bool ret;
@@ -2368,14 +2368,6 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
23682368
if (fp->kprobe_override)
23692369
return false;
23702370

2371-
/* XDP programs inserted into maps are not guaranteed to run on
2372-
* a particular netdev (and can run outside driver context entirely
2373-
* in the case of devmap and cpumap). Until device checks
2374-
* are implemented, prohibit adding dev-bound programs to program maps.
2375-
*/
2376-
if (bpf_prog_is_dev_bound(aux))
2377-
return false;
2378-
23792371
spin_lock(&map->owner.lock);
23802372
if (!map->owner.type) {
23812373
/* There's no owner yet where we could check for
@@ -2409,6 +2401,19 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
24092401
return ret;
24102402
}
24112403

2404+
bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp)
2405+
{
2406+
/* XDP programs inserted into maps are not guaranteed to run on
2407+
* a particular netdev (and can run outside driver context entirely
2408+
* in the case of devmap and cpumap). Until device checks
2409+
* are implemented, prohibit adding dev-bound programs to program maps.
2410+
*/
2411+
if (bpf_prog_is_dev_bound(fp->aux))
2412+
return false;
2413+
2414+
return __bpf_prog_map_compatible(map, fp);
2415+
}
2416+
24122417
static int bpf_check_tail_call(const struct bpf_prog *fp)
24132418
{
24142419
struct bpf_prog_aux *aux = fp->aux;
@@ -2421,7 +2426,7 @@ static int bpf_check_tail_call(const struct bpf_prog *fp)
24212426
if (!map_type_contains_progs(map))
24222427
continue;
24232428

2424-
if (!bpf_prog_map_compatible(map, fp)) {
2429+
if (!__bpf_prog_map_compatible(map, fp)) {
24252430
ret = -EINVAL;
24262431
goto out;
24272432
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,10 @@ void test_xdp_metadata(void)
351351
struct xdp_metadata2 *bpf_obj2 = NULL;
352352
struct xdp_metadata *bpf_obj = NULL;
353353
struct bpf_program *new_prog, *prog;
354+
struct bpf_devmap_val devmap_e = {};
355+
struct bpf_map *prog_arr, *devmap;
354356
struct nstoken *tok = NULL;
355357
__u32 queue_id = QUEUE_ID;
356-
struct bpf_map *prog_arr;
357358
struct xsk tx_xsk = {};
358359
struct xsk rx_xsk = {};
359360
__u32 val, key = 0;
@@ -409,6 +410,13 @@ void test_xdp_metadata(void)
409410
bpf_program__set_ifindex(prog, rx_ifindex);
410411
bpf_program__set_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
411412

413+
/* Make sure we can load a dev-bound program that performs
414+
* XDP_REDIRECT into a devmap.
415+
*/
416+
new_prog = bpf_object__find_program_by_name(bpf_obj->obj, "redirect");
417+
bpf_program__set_ifindex(new_prog, rx_ifindex);
418+
bpf_program__set_flags(new_prog, BPF_F_XDP_DEV_BOUND_ONLY);
419+
412420
if (!ASSERT_OK(xdp_metadata__load(bpf_obj), "load skeleton"))
413421
goto out;
414422

@@ -423,6 +431,18 @@ void test_xdp_metadata(void)
423431
"update prog_arr"))
424432
goto out;
425433

434+
/* Make sure we can't add dev-bound programs to devmaps. */
435+
devmap = bpf_object__find_map_by_name(bpf_obj->obj, "dev_map");
436+
if (!ASSERT_OK_PTR(devmap, "no dev_map found"))
437+
goto out;
438+
439+
devmap_e.bpf_prog.fd = val;
440+
if (!ASSERT_ERR(bpf_map__update_elem(devmap, &key, sizeof(key),
441+
&devmap_e, sizeof(devmap_e),
442+
BPF_ANY),
443+
"update dev_map"))
444+
goto out;
445+
426446
/* Attach BPF program to RX interface. */
427447

428448
ret = bpf_xdp_attach(rx_ifindex,

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

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ struct {
1919
__type(value, __u32);
2020
} prog_arr SEC(".maps");
2121

22+
struct {
23+
__uint(type, BPF_MAP_TYPE_DEVMAP);
24+
__uint(key_size, sizeof(__u32));
25+
__uint(value_size, sizeof(struct bpf_devmap_val));
26+
__uint(max_entries, 1);
27+
} dev_map SEC(".maps");
28+
2229
extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
2330
__u64 *timestamp) __ksym;
2431
extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
@@ -95,4 +102,10 @@ int rx(struct xdp_md *ctx)
95102
return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
96103
}
97104

105+
SEC("xdp")
106+
int redirect(struct xdp_md *ctx)
107+
{
108+
return bpf_redirect_map(&dev_map, ctx->rx_queue_index, XDP_PASS);
109+
}
110+
98111
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)