xref: /linux/tools/testing/selftests/bpf/progs/test_sockmap_update.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Cloudflare
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 
6 struct {
7 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
8 	__uint(max_entries, 1);
9 	__type(key, __u32);
10 	__type(value, __u64);
11 } src SEC(".maps");
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
15 	__uint(max_entries, 1);
16 	__type(key, __u32);
17 	__type(value, __u64);
18 } dst_sock_map SEC(".maps");
19 
20 struct {
21 	__uint(type, BPF_MAP_TYPE_SOCKHASH);
22 	__uint(max_entries, 1);
23 	__type(key, __u32);
24 	__type(value, __u64);
25 } dst_sock_hash SEC(".maps");
26 
27 SEC("tc")
28 int copy_sock_map(void *ctx)
29 {
30 	struct bpf_sock *sk;
31 	bool failed = false;
32 	__u32 key = 0;
33 
34 	sk = bpf_map_lookup_elem(&src, &key);
35 	if (!sk)
36 		return SK_DROP;
37 
38 	if (bpf_map_update_elem(&dst_sock_map, &key, sk, 0))
39 		failed = true;
40 
41 	if (bpf_map_update_elem(&dst_sock_hash, &key, sk, 0))
42 		failed = true;
43 
44 	bpf_sk_release(sk);
45 	return failed ? SK_DROP : SK_PASS;
46 }
47 
48 char _license[] SEC("license") = "GPL";
49