1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 // Copyright (c) 2020 Anton Protopopov 3 #ifndef __MAPS_BPF_H 4 #define __MAPS_BPF_H 5 6 #include <bpf/bpf_helpers.h> 7 #include <asm-generic/errno.h> 8 9 static __always_inline void * bpf_map_lookup_or_try_init(void * map,const void * key,const void * init)10bpf_map_lookup_or_try_init(void *map, const void *key, const void *init) 11 { 12 void *val; 13 long err; 14 15 val = bpf_map_lookup_elem(map, key); 16 if (val) 17 return val; 18 19 err = bpf_map_update_elem(map, key, init, BPF_NOEXIST); 20 if (err && err != -EEXIST) 21 return 0; 22 23 return bpf_map_lookup_elem(map, key); 24 } 25 26 #endif /* __MAPS_BPF_H */ 27