Skip to content

Commit 17bedab

Browse files
iamkafaidavem330
authored andcommitted
bpf: xdp: Allow head adjustment in XDP prog
This patch allows XDP prog to extend/remove the packet data at the head (like adding or removing header). It is done by adding a new XDP helper bpf_xdp_adjust_head(). It also renames bpf_helper_changes_skb_data() to bpf_helper_changes_pkt_data() to better reflect that XDP prog does not work on skb. This patch adds one "xdp_adjust_head" bit to bpf_prog for the XDP-capable driver to check if the XDP prog requires bpf_xdp_adjust_head() support. The driver can then decide to error out during XDP_SETUP_PROG. Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8a03cf2 commit 17bedab

File tree

13 files changed

+67
-11
lines changed

13 files changed

+67
-11
lines changed

Diff for: arch/powerpc/net/bpf_jit_comp64.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
766766
func = (u8 *) __bpf_call_base + imm;
767767

768768
/* Save skb pointer if we need to re-cache skb data */
769-
if (bpf_helper_changes_skb_data(func))
769+
if (bpf_helper_changes_pkt_data(func))
770770
PPC_BPF_STL(3, 1, bpf_jit_stack_local(ctx));
771771

772772
bpf_jit_emit_func_call(image, ctx, (u64)func);
@@ -775,7 +775,7 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
775775
PPC_MR(b2p[BPF_REG_0], 3);
776776

777777
/* refresh skb cache */
778-
if (bpf_helper_changes_skb_data(func)) {
778+
if (bpf_helper_changes_pkt_data(func)) {
779779
/* reload skb pointer to r3 */
780780
PPC_BPF_LL(3, 1, bpf_jit_stack_local(ctx));
781781
bpf_jit_emit_skb_loads(image, ctx);

Diff for: arch/s390/net/bpf_jit_comp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i
981981
EMIT2(0x0d00, REG_14, REG_W1);
982982
/* lgr %b0,%r2: load return value into %b0 */
983983
EMIT4(0xb9040000, BPF_REG_0, REG_2);
984-
if (bpf_helper_changes_skb_data((void *)func)) {
984+
if (bpf_helper_changes_pkt_data((void *)func)) {
985985
jit->seen |= SEEN_SKB_CHANGE;
986986
/* lg %b1,ST_OFF_SKBP(%r15) */
987987
EMIT6_DISP_LH(0xe3000000, 0x0004, BPF_REG_1, REG_0,

Diff for: arch/x86/net/bpf_jit_comp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ xadd: if (is_imm8(insn->off))
853853
func = (u8 *) __bpf_call_base + imm32;
854854
jmp_offset = func - (image + addrs[i]);
855855
if (seen_ld_abs) {
856-
reload_skb_data = bpf_helper_changes_skb_data(func);
856+
reload_skb_data = bpf_helper_changes_pkt_data(func);
857857
if (reload_skb_data) {
858858
EMIT1(0x57); /* push %rdi */
859859
jmp_offset += 22; /* pop, mov, sub, mov */

Diff for: drivers/net/ethernet/mellanox/mlx4/en_netdev.c

+5
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,11 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
26862686
int err;
26872687
int i;
26882688

2689+
if (prog && prog->xdp_adjust_head) {
2690+
en_err(priv, "Does not support bpf_xdp_adjust_head()\n");
2691+
return -EOPNOTSUPP;
2692+
}
2693+
26892694
xdp_ring_num = prog ? priv->rx_ring_num : 0;
26902695

26912696
/* No need to reconfigure buffers when simply swapping the

Diff for: drivers/net/ethernet/mellanox/mlx5/core/en_main.c

+5
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,11 @@ static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
31833183
bool reset, was_opened;
31843184
int i;
31853185

3186+
if (prog && prog->xdp_adjust_head) {
3187+
netdev_err(netdev, "Does not support bpf_xdp_adjust_head()\n");
3188+
return -EOPNOTSUPP;
3189+
}
3190+
31863191
mutex_lock(&priv->state_lock);
31873192

31883193
if ((netdev->features & NETIF_F_LRO) && prog) {

Diff for: drivers/net/ethernet/netronome/nfp/nfp_net_common.c

+4
Original file line numberDiff line numberDiff line change
@@ -2946,6 +2946,10 @@ static int nfp_net_xdp_setup(struct nfp_net *nn, struct bpf_prog *prog)
29462946
};
29472947
int err;
29482948

2949+
if (prog && prog->xdp_adjust_head) {
2950+
nn_err(nn, "Does not support bpf_xdp_adjust_head()\n");
2951+
return -EOPNOTSUPP;
2952+
}
29492953
if (!prog && !nn->xdp_prog)
29502954
return 0;
29512955
if (prog && nn->xdp_prog) {

Diff for: drivers/net/ethernet/qlogic/qede/qede_main.c

+5
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,11 @@ static int qede_xdp_set(struct qede_dev *edev, struct bpf_prog *prog)
25072507
{
25082508
struct qede_reload_args args;
25092509

2510+
if (prog && prog->xdp_adjust_head) {
2511+
DP_ERR(edev, "Does not support bpf_xdp_adjust_head()\n");
2512+
return -EOPNOTSUPP;
2513+
}
2514+
25102515
/* If we're called, there was already a bpf reference increment */
25112516
args.func = &qede_xdp_reload_func;
25122517
args.u.new_prog = prog;

Diff for: include/linux/filter.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ struct bpf_prog {
406406
u16 jited:1, /* Is our filter JIT'ed? */
407407
gpl_compatible:1, /* Is filter GPL compatible? */
408408
cb_access:1, /* Is control block accessed? */
409-
dst_needed:1; /* Do we need dst entry? */
409+
dst_needed:1, /* Do we need dst entry? */
410+
xdp_adjust_head:1; /* Adjusting pkt head? */
410411
kmemcheck_bitfield_end(meta);
411412
enum bpf_prog_type type; /* Type of BPF program */
412413
u32 len; /* Number of filter blocks */
@@ -440,6 +441,7 @@ struct bpf_skb_data_end {
440441
struct xdp_buff {
441442
void *data;
442443
void *data_end;
444+
void *data_hard_start;
443445
};
444446

445447
/* compute the linear packet data range [data, data_end) which
@@ -595,7 +597,7 @@ void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
595597
u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
596598

597599
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
598-
bool bpf_helper_changes_skb_data(void *func);
600+
bool bpf_helper_changes_pkt_data(void *func);
599601

600602
struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
601603
const struct bpf_insn *patch, u32 len);

Diff for: include/uapi/linux/bpf.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ union bpf_attr {
424424
* @len: length of header to be pushed in front
425425
* @flags: Flags (unused for now)
426426
* Return: 0 on success or negative error
427+
*
428+
* int bpf_xdp_adjust_head(xdp_md, delta)
429+
* Adjust the xdp_md.data by delta
430+
* @xdp_md: pointer to xdp_md
431+
* @delta: An positive/negative integer to be added to xdp_md.data
432+
* Return: 0 on success or negative on error
427433
*/
428434
#define __BPF_FUNC_MAPPER(FN) \
429435
FN(unspec), \
@@ -469,7 +475,8 @@ union bpf_attr {
469475
FN(csum_update), \
470476
FN(set_hash_invalid), \
471477
FN(get_numa_node_id), \
472-
FN(skb_change_head),
478+
FN(skb_change_head), \
479+
FN(xdp_adjust_head),
473480

474481
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
475482
* function eBPF program intends to call
@@ -576,6 +583,8 @@ struct bpf_sock {
576583
__u32 protocol;
577584
};
578585

586+
#define XDP_PACKET_HEADROOM 256
587+
579588
/* User return codes for XDP prog type.
580589
* A valid XDP program must return one of these defined values. All other
581590
* return codes are reserved for future use. Unknown return codes will result

Diff for: kernel/bpf/core.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
11431143
return prog;
11441144
}
11451145

1146-
bool __weak bpf_helper_changes_skb_data(void *func)
1146+
bool __weak bpf_helper_changes_pkt_data(void *func)
11471147
{
11481148
return false;
11491149
}

Diff for: kernel/bpf/syscall.c

+2
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ static void fixup_bpf_calls(struct bpf_prog *prog)
579579
prog->dst_needed = 1;
580580
if (insn->imm == BPF_FUNC_get_prandom_u32)
581581
bpf_user_rnd_init_once();
582+
if (insn->imm == BPF_FUNC_xdp_adjust_head)
583+
prog->xdp_adjust_head = 1;
582584
if (insn->imm == BPF_FUNC_tail_call) {
583585
/* mark bpf_tail_call as different opcode
584586
* to avoid conditional branch in

Diff for: kernel/bpf/verifier.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
12161216
return -EINVAL;
12171217
}
12181218

1219-
changes_data = bpf_helper_changes_skb_data(fn->func);
1219+
changes_data = bpf_helper_changes_pkt_data(fn->func);
12201220

12211221
memset(&meta, 0, sizeof(meta));
12221222
meta.pkt_access = fn->pkt_access;

Diff for: net/core/filter.c

+26-2
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,28 @@ static const struct bpf_func_proto bpf_skb_change_head_proto = {
22342234
.arg3_type = ARG_ANYTHING,
22352235
};
22362236

2237-
bool bpf_helper_changes_skb_data(void *func)
2237+
BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2238+
{
2239+
void *data = xdp->data + offset;
2240+
2241+
if (unlikely(data < xdp->data_hard_start ||
2242+
data > xdp->data_end - ETH_HLEN))
2243+
return -EINVAL;
2244+
2245+
xdp->data = data;
2246+
2247+
return 0;
2248+
}
2249+
2250+
static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2251+
.func = bpf_xdp_adjust_head,
2252+
.gpl_only = false,
2253+
.ret_type = RET_INTEGER,
2254+
.arg1_type = ARG_PTR_TO_CTX,
2255+
.arg2_type = ARG_ANYTHING,
2256+
};
2257+
2258+
bool bpf_helper_changes_pkt_data(void *func)
22382259
{
22392260
if (func == bpf_skb_vlan_push ||
22402261
func == bpf_skb_vlan_pop ||
@@ -2244,7 +2265,8 @@ bool bpf_helper_changes_skb_data(void *func)
22442265
func == bpf_skb_change_tail ||
22452266
func == bpf_skb_pull_data ||
22462267
func == bpf_l3_csum_replace ||
2247-
func == bpf_l4_csum_replace)
2268+
func == bpf_l4_csum_replace ||
2269+
func == bpf_xdp_adjust_head)
22482270
return true;
22492271

22502272
return false;
@@ -2670,6 +2692,8 @@ xdp_func_proto(enum bpf_func_id func_id)
26702692
return &bpf_xdp_event_output_proto;
26712693
case BPF_FUNC_get_smp_processor_id:
26722694
return &bpf_get_smp_processor_id_proto;
2695+
case BPF_FUNC_xdp_adjust_head:
2696+
return &bpf_xdp_adjust_head_proto;
26732697
default:
26742698
return sk_filter_func_proto(func_id);
26752699
}

0 commit comments

Comments
 (0)