1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include <bpf/bpf_core_read.h>
9 #define ATTR __always_inline
10 #include "test_jhash.h"
13 __uint(type, BPF_MAP_TYPE_ARRAY);
16 __uint(max_entries, 256);
17 } array1 SEC(".maps");
20 __uint(type, BPF_MAP_TYPE_ARRAY);
23 __uint(max_entries, 256);
24 } array2 SEC(".maps");
26 static __noinline int randmap(int v, const struct net_device *dev)
28 struct bpf_map *map = (struct bpf_map *)&array1;
29 int key = bpf_get_prandom_u32() & 0xff;
32 if (bpf_get_prandom_u32() & 1)
33 map = (struct bpf_map *)&array2;
35 val = bpf_map_lookup_elem(map, &key);
37 *val = bpf_get_prandom_u32() + v + dev->mtu;
42 SEC("tp_btf/xdp_devmap_xmit")
43 int BPF_PROG(tp_xdp_devmap_xmit_multi, const struct net_device
44 *from_dev, const struct net_device *to_dev, int sent, int drops,
47 return randmap(from_dev->ifindex, from_dev);
50 SEC("fentry/eth_type_trans")
51 int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb,
52 struct net_device *dev, unsigned short protocol)
54 return randmap(dev->ifindex + skb->len, dev);
57 SEC("fexit/eth_type_trans")
58 int BPF_PROG(fexit_eth_type_trans, struct sk_buff *skb,
59 struct net_device *dev, unsigned short protocol)
61 return randmap(dev->ifindex + skb->len, dev);
64 volatile const int never;
66 struct __sk_bUfF /* it will not exist in vmlinux */ {
68 } __attribute__((preserve_access_index));
70 struct bpf_testmod_test_read_ctx /* it exists in bpf_testmod */ {
72 } __attribute__((preserve_access_index));
75 int balancer_ingress(struct __sk_buff *ctx)
77 void *data_end = (void *)(long)ctx->data_end;
78 void *data = (void *)(long)ctx->data;
84 /* pragma unroll doesn't work on large loops */
87 if (ptr + nh_off > data_end) \
89 ctx->tc_index = jhash(ptr, nh_off, ctx->cb[0] + i++); \
91 /* below is a dead code with unresolvable CO-RE relo */ \
92 i += ((struct __sk_bUfF *)ctx)->len; \
93 /* this CO-RE relo may or may not resolve
94 * depending on whether bpf_testmod is loaded.
96 i += ((struct bpf_testmod_test_read_ctx *)ctx)->len; \
99 #define C30 C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;
100 C30;C30;C30; /* 90 calls */
104 typedef int (*func_proto_typedef___match)(long);
105 typedef int (*func_proto_typedef___doesnt_match)(char *);
106 typedef int (*func_proto_typedef_nested1)(func_proto_typedef___match);
110 SEC("raw_tracepoint/sys_enter")
111 int core_relo_proto(void *ctx)
113 proto_out[0] = bpf_core_type_exists(func_proto_typedef___match);
114 proto_out[1] = bpf_core_type_exists(func_proto_typedef___doesnt_match);
115 proto_out[2] = bpf_core_type_exists(func_proto_typedef_nested1);
120 char LICENSE[] SEC("license") = "GPL";