Skip to content

Commit 64b5902

Browse files
dsahernAlexei Starovoitov
authored andcommitted
xdp: Add xdp_txq_info to xdp_buff
Add xdp_txq_info as the Tx counterpart to xdp_rxq_info. At the moment only the device is added. Other fields (queue_index) can be added as use cases arise. >From a UAPI perspective, add egress_ifindex to xdp context for bpf programs to see the Tx device. Update the verifier to only allow accesses to egress_ifindex by XDP programs with BPF_XDP_DEVMAP expected attach type. Signed-off-by: David Ahern <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Toke Høiland-Jørgensen <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent fbee97f commit 64b5902

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

include/net/xdp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ struct xdp_rxq_info {
6161
struct xdp_mem_info mem;
6262
} ____cacheline_aligned; /* perf critical, avoid false-sharing */
6363

64+
struct xdp_txq_info {
65+
struct net_device *dev;
66+
};
67+
6468
struct xdp_buff {
6569
void *data;
6670
void *data_end;
6771
void *data_meta;
6872
void *data_hard_start;
6973
struct xdp_rxq_info *rxq;
74+
struct xdp_txq_info *txq;
7075
u32 frame_sz; /* frame size to deduce data_hard_end/reserved tailroom*/
7176
};
7277

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,6 +3707,8 @@ struct xdp_md {
37073707
/* Below access go through struct xdp_rxq_info */
37083708
__u32 ingress_ifindex; /* rxq->dev->ifindex */
37093709
__u32 rx_queue_index; /* rxq->queue_index */
3710+
3711+
__u32 egress_ifindex; /* txq->dev->ifindex */
37103712
};
37113713

37123714
enum sk_action {

kernel/bpf/devmap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,11 @@ static struct xdp_buff *dev_map_run_prog(struct net_device *dev,
476476
struct xdp_buff *xdp,
477477
struct bpf_prog *xdp_prog)
478478
{
479+
struct xdp_txq_info txq = { .dev = dev };
479480
u32 act;
480481

482+
xdp->txq = &txq;
483+
481484
act = bpf_prog_run_xdp(xdp_prog, xdp);
482485
switch (act) {
483486
case XDP_PASS:

net/core/filter.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7015,6 +7015,13 @@ static bool xdp_is_valid_access(int off, int size,
70157015
const struct bpf_prog *prog,
70167016
struct bpf_insn_access_aux *info)
70177017
{
7018+
if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
7019+
switch (off) {
7020+
case offsetof(struct xdp_md, egress_ifindex):
7021+
return false;
7022+
}
7023+
}
7024+
70187025
if (type == BPF_WRITE) {
70197026
if (bpf_prog_is_dev_bound(prog->aux)) {
70207027
switch (off) {
@@ -7985,6 +7992,16 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
79857992
offsetof(struct xdp_rxq_info,
79867993
queue_index));
79877994
break;
7995+
case offsetof(struct xdp_md, egress_ifindex):
7996+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
7997+
si->dst_reg, si->src_reg,
7998+
offsetof(struct xdp_buff, txq));
7999+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
8000+
si->dst_reg, si->dst_reg,
8001+
offsetof(struct xdp_txq_info, dev));
8002+
*insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8003+
offsetof(struct net_device, ifindex));
8004+
break;
79888005
}
79898006

79908007
return insn - insn_buf;

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,6 +3706,8 @@ struct xdp_md {
37063706
/* Below access go through struct xdp_rxq_info */
37073707
__u32 ingress_ifindex; /* rxq->dev->ifindex */
37083708
__u32 rx_queue_index; /* rxq->queue_index */
3709+
3710+
__u32 egress_ifindex; /* txq->dev->ifindex */
37093711
};
37103712

37113713
enum sk_action {

0 commit comments

Comments
 (0)