Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 
0004 #include "vmlinux.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007 
0008 struct my_key { long x; };
0009 struct my_value { long x; };
0010 
0011 struct {
0012     __uint(type, BPF_MAP_TYPE_HASH);
0013     __type(key, struct my_key);
0014     __type(value, struct my_value);
0015     __uint(max_entries, 16);
0016 } map1 SEC(".maps");
0017 
0018  /* Matches map2 definition in linked_maps2.c. Order of the attributes doesn't
0019   * matter.
0020   */
0021 typedef struct {
0022     __uint(max_entries, 8);
0023     __type(key, int);
0024     __type(value, int);
0025     __uint(type, BPF_MAP_TYPE_ARRAY);
0026 } map2_t;
0027 
0028 extern map2_t map2 SEC(".maps");
0029 
0030 /* This should be the winning map definition, but we have no way of verifying,
0031  * so we just make sure that it links and works without errors
0032  */
0033 struct {
0034     __uint(type, BPF_MAP_TYPE_ARRAY);
0035     __type(key, int);
0036     __type(value, int);
0037     __uint(max_entries, 16);
0038 } map_weak __weak SEC(".maps");
0039 
0040 int output_first1;
0041 int output_second1;
0042 int output_weak1;
0043 
0044 SEC("raw_tp/sys_enter")
0045 int BPF_PROG(handler_enter1)
0046 {
0047     /* update values with key = 1 */
0048     int key = 1, val = 1;
0049     struct my_key key_struct = { .x = 1 };
0050     struct my_value val_struct = { .x = 1000 };
0051 
0052     bpf_map_update_elem(&map1, &key_struct, &val_struct, 0);
0053     bpf_map_update_elem(&map2, &key, &val, 0);
0054     bpf_map_update_elem(&map_weak, &key, &val, 0);
0055 
0056     return 0;
0057 }
0058 
0059 SEC("raw_tp/sys_exit")
0060 int BPF_PROG(handler_exit1)
0061 {
0062     /* lookup values with key = 2, set in another file */
0063     int key = 2, *val;
0064     struct my_key key_struct = { .x = 2 };
0065     struct my_value *value_struct;
0066 
0067     value_struct = bpf_map_lookup_elem(&map1, &key_struct);
0068     if (value_struct)
0069         output_first1 = value_struct->x;
0070 
0071     val = bpf_map_lookup_elem(&map2, &key);
0072     if (val)
0073         output_second1 = *val;
0074 
0075     val = bpf_map_lookup_elem(&map_weak, &key);
0076     if (val)
0077         output_weak1 = *val;
0078 
0079     return 0;
0080 }
0081 
0082 char LICENSE[] SEC("license") = "GPL";