Skip to content

Commit 3004932

Browse files
committed
Merge branch 'bpf-misc'
Daniel Borkmann says: ==================== Minor BPF follow-ups Some minor last follow-ups I still had in my queue. The first one adds readability support for __sk_buff's tc_classid member, the remaining two are some minor cleanups. For details please see individual patches. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 019ded3 + fca5fdf commit 3004932

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

include/net/dst.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
398398
__skb_tunnel_rx(skb, dev, net);
399399
}
400400

401+
static inline u32 dst_tclassid(const struct sk_buff *skb)
402+
{
403+
#ifdef CONFIG_IP_ROUTE_CLASSID
404+
const struct dst_entry *dst;
405+
406+
dst = skb_dst(skb);
407+
if (dst)
408+
return dst->tclassid;
409+
#endif
410+
return 0;
411+
}
412+
401413
int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
402414
static inline int dst_discard(struct sk_buff *skb)
403415
{

include/net/ip_tunnels.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <linux/socket.h>
88
#include <linux/types.h>
99
#include <linux/u64_stats_sync.h>
10+
#include <linux/bitops.h>
11+
1012
#include <net/dsfield.h>
1113
#include <net/gro_cells.h>
1214
#include <net/inet_ecn.h>
@@ -57,6 +59,11 @@ struct ip_tunnel_key {
5759
#define IP_TUNNEL_INFO_TX 0x01 /* represents tx tunnel parameters */
5860
#define IP_TUNNEL_INFO_IPV6 0x02 /* key contains IPv6 addresses */
5961

62+
/* Maximum tunnel options length. */
63+
#define IP_TUNNEL_OPTS_MAX \
64+
GENMASK((FIELD_SIZEOF(struct ip_tunnel_info, \
65+
options_len) * BITS_PER_BYTE) - 1, 0)
66+
6067
struct ip_tunnel_info {
6168
struct ip_tunnel_key key;
6269
#ifdef CONFIG_DST_CACHE

net/core/filter.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,14 +1682,7 @@ static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
16821682

16831683
static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
16841684
{
1685-
#ifdef CONFIG_IP_ROUTE_CLASSID
1686-
const struct dst_entry *dst;
1687-
1688-
dst = skb_dst((struct sk_buff *) (unsigned long) r1);
1689-
if (dst)
1690-
return dst->tclassid;
1691-
#endif
1692-
return 0;
1685+
return dst_tclassid((struct sk_buff *) (unsigned long) r1);
16931686
}
16941687

16951688
static const struct bpf_func_proto bpf_get_route_realm_proto = {
@@ -1911,8 +1904,6 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
19111904
.arg4_type = ARG_ANYTHING,
19121905
};
19131906

1914-
#define BPF_TUNLEN_MAX 255
1915-
19161907
static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
19171908
{
19181909
struct sk_buff *skb = (struct sk_buff *) (long) r1;
@@ -1922,7 +1913,7 @@ static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
19221913

19231914
if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
19241915
return -EINVAL;
1925-
if (unlikely(size > BPF_TUNLEN_MAX))
1916+
if (unlikely(size > IP_TUNNEL_OPTS_MAX))
19261917
return -ENOMEM;
19271918

19281919
ip_tunnel_info_opts_set(info, from, size);
@@ -1943,13 +1934,10 @@ static const struct bpf_func_proto *
19431934
bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
19441935
{
19451936
if (!md_dst) {
1946-
BUILD_BUG_ON(FIELD_SIZEOF(struct ip_tunnel_info,
1947-
options_len) != 1);
1948-
19491937
/* Race is not possible, since it's called from verifier
19501938
* that is holding verifier mutex.
19511939
*/
1952-
md_dst = metadata_dst_alloc_percpu(BPF_TUNLEN_MAX,
1940+
md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
19531941
GFP_KERNEL);
19541942
if (!md_dst)
19551943
return NULL;
@@ -2069,16 +2057,14 @@ static bool sk_filter_is_valid_access(int off, int size,
20692057
static bool tc_cls_act_is_valid_access(int off, int size,
20702058
enum bpf_access_type type)
20712059
{
2072-
if (off == offsetof(struct __sk_buff, tc_classid))
2073-
return type == BPF_WRITE ? true : false;
2074-
20752060
if (type == BPF_WRITE) {
20762061
switch (off) {
20772062
case offsetof(struct __sk_buff, mark):
20782063
case offsetof(struct __sk_buff, tc_index):
20792064
case offsetof(struct __sk_buff, priority):
20802065
case offsetof(struct __sk_buff, cb[0]) ...
2081-
offsetof(struct __sk_buff, cb[4]):
2066+
offsetof(struct __sk_buff, cb[4]):
2067+
case offsetof(struct __sk_buff, tc_classid):
20822068
break;
20832069
default:
20842070
return false;
@@ -2195,8 +2181,10 @@ static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
21952181
ctx_off -= offsetof(struct __sk_buff, tc_classid);
21962182
ctx_off += offsetof(struct sk_buff, cb);
21972183
ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
2198-
WARN_ON(type != BPF_WRITE);
2199-
*insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2184+
if (type == BPF_WRITE)
2185+
*insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2186+
else
2187+
*insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
22002188
break;
22012189

22022190
case offsetof(struct __sk_buff, tc_index):

net/ipv4/ip_tunnel_core.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
398398

399399
void __init ip_tunnel_core_init(void)
400400
{
401+
/* If you land here, make sure whether increasing ip_tunnel_info's
402+
* options_len is a reasonable choice with its usage in front ends
403+
* (f.e., it's part of flow keys, etc).
404+
*/
405+
BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
406+
401407
lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
402408
lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
403409
}

net/openvswitch/flow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct ovs_tunnel_info {
5555
FIELD_SIZEOF(struct sw_flow_key, recirc_id))
5656

5757
struct sw_flow_key {
58-
u8 tun_opts[255];
58+
u8 tun_opts[IP_TUNNEL_OPTS_MAX];
5959
u8 tun_opts_len;
6060
struct ip_tunnel_key tun_key; /* Encapsulating tunnel key. */
6161
struct {

0 commit comments

Comments
 (0)