1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Cloudflare */
3 #include "bpf_iter.h"
4 #include "bpf_tracing_net.h"
5 #include "bpf_iter_sockmap.h"
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include <errno.h>
9 
10 char _license[] SEC("license") = "GPL";
11 
12 struct {
13 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
14 	__uint(max_entries, SOCKMAP_MAX_ENTRIES);
15 	__type(key, __u32);
16 	__type(value, __u64);
17 } sockmap SEC(".maps");
18 
19 struct {
20 	__uint(type, BPF_MAP_TYPE_SOCKHASH);
21 	__uint(max_entries, SOCKMAP_MAX_ENTRIES);
22 	__type(key, __u32);
23 	__type(value, __u64);
24 } sockhash SEC(".maps");
25 
26 __u32 elems = 0;
27 __u32 socks = 0;
28 
29 SEC("iter/sockmap")
30 int count_elems(struct bpf_iter__sockmap *ctx)
31 {
32 	struct sock *sk = ctx->sk;
33 	__u32 tmp, *key = ctx->key;
34 	int ret;
35 
36 	if (key)
37 		elems++;
38 
39 	if (sk)
40 		socks++;
41 
42 	return 0;
43 }
44