Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020 Facebook
0003 
0004 #include <linux/bpf.h>
0005 #include <bpf/bpf_helpers.h>
0006 
0007 char _license[] SEC("license") = "GPL";
0008 
0009 struct sample {
0010     int pid;
0011     int seq;
0012     long value;
0013     char comm[16];
0014 };
0015 
0016 struct ringbuf_map {
0017     __uint(type, BPF_MAP_TYPE_RINGBUF);
0018     /* libbpf will adjust to valid page size */
0019     __uint(max_entries, 1000);
0020 } ringbuf1 SEC(".maps"),
0021   ringbuf2 SEC(".maps");
0022 
0023 struct {
0024     __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
0025     __uint(max_entries, 4);
0026     __type(key, int);
0027     __array(values, struct ringbuf_map);
0028 } ringbuf_arr SEC(".maps") = {
0029     .values = {
0030         [0] = &ringbuf1,
0031         [2] = &ringbuf2,
0032     },
0033 };
0034 
0035 struct {
0036     __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
0037     __uint(max_entries, 1);
0038     __type(key, int);
0039     __array(values, struct ringbuf_map);
0040 } ringbuf_hash SEC(".maps") = {
0041     .values = {
0042         [0] = &ringbuf1,
0043     },
0044 };
0045 
0046 /* inputs */
0047 int pid = 0;
0048 int target_ring = 0;
0049 long value = 0;
0050 
0051 /* outputs */
0052 long total = 0;
0053 long dropped = 0;
0054 long skipped = 0;
0055 
0056 SEC("tp/syscalls/sys_enter_getpgid")
0057 int test_ringbuf(void *ctx)
0058 {
0059     int cur_pid = bpf_get_current_pid_tgid() >> 32;
0060     struct sample *sample;
0061     void *rb;
0062     int zero = 0;
0063 
0064     if (cur_pid != pid)
0065         return 0;
0066 
0067     rb = bpf_map_lookup_elem(&ringbuf_arr, &target_ring);
0068     if (!rb) {
0069         skipped += 1;
0070         return 1;
0071     }
0072 
0073     sample = bpf_ringbuf_reserve(rb, sizeof(*sample), 0);
0074     if (!sample) {
0075         dropped += 1;
0076         return 1;
0077     }
0078 
0079     sample->pid = pid;
0080     bpf_get_current_comm(sample->comm, sizeof(sample->comm));
0081     sample->value = value;
0082 
0083     sample->seq = total;
0084     total += 1;
0085 
0086     bpf_ringbuf_submit(sample, 0);
0087 
0088     return 0;
0089 }