xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_map.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "bpf_iter.h"
4 #include "bpf_tracing_net.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 char _license[] SEC("license") = "GPL";
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
12 	__uint(map_flags, BPF_F_NO_PREALLOC);
13 	__type(key, int);
14 	__type(value, int);
15 } sk_stg_map SEC(".maps");
16 
17 __u32 val_sum = 0;
18 __u32 ipv6_sk_count = 0;
19 __u32 to_add_val = 0;
20 
21 SEC("iter/bpf_sk_storage_map")
rw_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map * ctx)22 int rw_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
23 {
24 	struct sock *sk = ctx->sk;
25 	__u32 *val = ctx->value;
26 
27 	if (sk == NULL || val == NULL)
28 		return 0;
29 
30 	if (sk->sk_family == AF_INET6)
31 		ipv6_sk_count++;
32 
33 	val_sum += *val;
34 
35 	*val += to_add_val;
36 
37 	return 0;
38 }
39 
40 SEC("iter/bpf_sk_storage_map")
oob_write_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map * ctx)41 int oob_write_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
42 {
43 	struct sock *sk = ctx->sk;
44 	__u32 *val = ctx->value;
45 
46 	if (sk == NULL || val == NULL)
47 		return 0;
48 
49 	*(val + 1) = 0xdeadbeef;
50 
51 	return 0;
52 }
53