Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Cloudflare */
0003 #include "bpf_iter.h"
0004 #include "bpf_tracing_net.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007 #include <errno.h>
0008 
0009 char _license[] SEC("license") = "GPL";
0010 
0011 struct {
0012     __uint(type, BPF_MAP_TYPE_SOCKMAP);
0013     __uint(max_entries, 64);
0014     __type(key, __u32);
0015     __type(value, __u64);
0016 } sockmap SEC(".maps");
0017 
0018 struct {
0019     __uint(type, BPF_MAP_TYPE_SOCKHASH);
0020     __uint(max_entries, 64);
0021     __type(key, __u32);
0022     __type(value, __u64);
0023 } sockhash SEC(".maps");
0024 
0025 struct {
0026     __uint(type, BPF_MAP_TYPE_SOCKHASH);
0027     __uint(max_entries, 64);
0028     __type(key, __u32);
0029     __type(value, __u64);
0030 } dst SEC(".maps");
0031 
0032 __u32 elems = 0;
0033 __u32 socks = 0;
0034 
0035 SEC("iter/sockmap")
0036 int copy(struct bpf_iter__sockmap *ctx)
0037 {
0038     struct sock *sk = ctx->sk;
0039     __u32 tmp, *key = ctx->key;
0040     int ret;
0041 
0042     if (!key)
0043         return 0;
0044 
0045     elems++;
0046 
0047     /* We need a temporary buffer on the stack, since the verifier doesn't
0048      * let us use the pointer from the context as an argument to the helper.
0049      */
0050     tmp = *key;
0051 
0052     if (sk) {
0053         socks++;
0054         return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0;
0055     }
0056 
0057     ret = bpf_map_delete_elem(&dst, &tmp);
0058     return ret && ret != -ENOENT;
0059 }