Skip to contentSkip to conversation

bpf: allow BPF programs access skb->skb_iif and skb->dev->ifindex fields

PATCH Applied Alexei Starovoitov 3 participants 6 messages May 27, 2015 – May 31, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: allow BPF programs access skb->skb_iif and skb->dev->ifindex fields

classic BPF already exposes skb->dev->ifindex via SKF_AD_IFINDEX extension. Allow eBPF program to access it as well. Note that classic aborts execution of the program if 'skb->dev == NULL' (which is inconvenient for program writers), whereas eBPF returns zero in such case.
Also expose the 'skb_iif' field, since programs triggered by redirected packet need to known the original interface index.
Summary:
__skb->ifindex -> skb->dev->ifindex
__skb->ingress_ifindex -> skb->skb_iif

---
 include/uapi/linux/bpf.h |    2 ++
 net/core/filter.c        |   18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
include/uapi/linux/bpf.h +2 -0
index f0a9af8b4dae..72f3080afa1e 100644
@@ -236,6 +236,8 @@ struct __sk_buff {
 	__u32 vlan_tci;
 	__u32 vlan_proto;
 	__u32 priority;
+	__u32 ingress_ifindex;
+	__u32 ifindex;
 };
 
 #endif /* _UAPI__LINUX_BPF_H__ */
net/core/filter.c +18 -0
index 3adcca6f17a4..2c30d6632d66 100644
@@ -1499,6 +1499,24 @@ static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
 				      offsetof(struct sk_buff, priority));
 		break;
 
+	case offsetof(struct __sk_buff, ingress_ifindex):
+		BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
+
+		*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
+				      offsetof(struct sk_buff, skb_iif));
+		break;
+
+	case offsetof(struct __sk_buff, ifindex):
+		BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
+
+		*insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
+				      dst_reg, src_reg,
+				      offsetof(struct sk_buff, dev));
+		*insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
+		*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
+				      offsetof(struct net_device, ifindex));
+		break;
+
 	case offsetof(struct __sk_buff, mark):
 		return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
 
Signature · 1 line
1.7.9.5
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Alexei Starovoitov

classic BPF already exposes skb->dev->ifindex via SKF_AD_IFINDEX extension. Allow eBPF program to access it as well. Note that classic aborts execution of the program if 'skb->dev == NULL' (which is inconvenient for program writers), whereas eBPF returns zero in such case.

That's better, yep.

Quoted 7 lines

Also expose the 'skb_iif' field, since programs triggered by redirected packet need to known the original interface index.
Summary:
__skb->ifindex -> skb->dev->ifindex
__skb->ingress_ifindex -> skb->skb_iif

maintainer
#3 lore raw

From: Alexei Starovoitov <[email protected]>
Date: Thu, 28 May 2015 20:40:44 -0700

Quoted 12 lines · Daniel Borkmann
Quoted 4 lines
Quoted 2 lines

btw, Daniel, your ack appeared on the mailing list, but didn't make it into patchwork...
Same issue as I was having.
I noticed when I send email via gmail web interface it always appears as separate thread and doesn't register in patchwork. So now I've switched to mutt and thunderbird only :)

patchwork lost 22 hours of list traffic, but it should be functioning normally now

maintainer Acked-by
#4 lore raw
Quoted 2 lines · David Miller

From: Alexei Starovoitov <[email protected]>
Date: Thu, 28 May 2015 20:40:44 -0700

...

Quoted 9 lines
Quoted 6 lines

btw, Daniel, your ack appeared on the mailing list, but didn't make it into patchwork...
Same issue as I was having.
I noticed when I send email via gmail web interface it always appears as separate thread and doesn't register in patchwork. So now I've switched to mutt and thunderbird only :)

patchwork lost 22 hours of list traffic, but it should be functioning normally now

Ok, great. So, lets try once more. :)

maintainer Applied
#5 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Wed, 27 May 2015 15:30:39 -0700

Quoted 11 lines

classic BPF already exposes skb->dev->ifindex via SKF_AD_IFINDEX extension. Allow eBPF program to access it as well. Note that classic aborts execution of the program if 'skb->dev == NULL' (which is inconvenient for program writers), whereas eBPF returns zero in such case.
Also expose the 'skb_iif' field, since programs triggered by redirected packet need to known the original interface index.
Summary:
__skb->ifindex -> skb->dev->ifindex
__skb->ingress_ifindex -> skb->skb_iif

Applied, thank you.