github.com/elfadel/cilium@v1.6.12/pkg/bpf/bpf.go (about)

     1  // Copyright 2016-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bpf
    16  
    17  import (
    18  	"sync/atomic"
    19  
    20  	"github.com/cilium/cilium/pkg/logging"
    21  	"github.com/cilium/cilium/pkg/logging/logfields"
    22  )
    23  
    24  var (
    25  	log = logging.DefaultLogger.WithField(logfields.LogSubsys, "bpf")
    26  
    27  	preAllocateMapSetting uint32 = BPF_F_NO_PREALLOC
    28  )
    29  
    30  const (
    31  	// BPF map type constants. Must match enum bpf_map_type from linux/bpf.h
    32  	BPF_MAP_TYPE_UNSPEC              = 0
    33  	BPF_MAP_TYPE_HASH                = 1
    34  	BPF_MAP_TYPE_ARRAY               = 2
    35  	BPF_MAP_TYPE_PROG_ARRAY          = 3
    36  	BPF_MAP_TYPE_PERF_EVENT_ARRAY    = 4
    37  	BPF_MAP_TYPE_PERCPU_HASH         = 5
    38  	BPF_MAP_TYPE_PERCPU_ARRAY        = 6
    39  	BPF_MAP_TYPE_STACK_TRACE         = 7
    40  	BPF_MAP_TYPE_CGROUP_ARRAY        = 8
    41  	BPF_MAP_TYPE_LRU_HASH            = 9
    42  	BPF_MAP_TYPE_LRU_PERCPU_HASH     = 10
    43  	BPF_MAP_TYPE_LPM_TRIE            = 11
    44  	BPF_MAP_TYPE_ARRAY_OF_MAPS       = 12
    45  	BPF_MAP_TYPE_HASH_OF_MAPS        = 13
    46  	BPF_MAP_TYPE_DEVMAP              = 14
    47  	BPF_MAP_TYPE_SOCKMAP             = 15
    48  	BPF_MAP_TYPE_CPUMAP              = 16
    49  	BPF_MAP_TYPE_XSKMAP              = 17
    50  	BPF_MAP_TYPE_SOCKHASH            = 18
    51  	BPF_MAP_TYPE_CGROUP_STORAGE      = 19
    52  	BPF_MAP_TYPE_REUSEPORT_SOCKARRAY = 20
    53  
    54  	// BPF syscall command constants. Must match enum bpf_cmd from linux/bpf.h
    55  	BPF_MAP_CREATE          = 0
    56  	BPF_MAP_LOOKUP_ELEM     = 1
    57  	BPF_MAP_UPDATE_ELEM     = 2
    58  	BPF_MAP_DELETE_ELEM     = 3
    59  	BPF_MAP_GET_NEXT_KEY    = 4
    60  	BPF_PROG_LOAD           = 5
    61  	BPF_OBJ_PIN             = 6
    62  	BPF_OBJ_GET             = 7
    63  	BPF_PROG_ATTACH         = 8
    64  	BPF_PROG_DETACH         = 9
    65  	BPF_PROG_TEST_RUN       = 10
    66  	BPF_PROG_GET_NEXT_ID    = 11
    67  	BPF_MAP_GET_NEXT_ID     = 12
    68  	BPF_PROG_GET_FD_BY_ID   = 13
    69  	BPF_MAP_GET_FD_BY_ID    = 14
    70  	BPF_OBJ_GET_INFO_BY_FD  = 15
    71  	BPF_PROG_QUERY          = 16
    72  	BPF_RAW_TRACEPOINT_OPEN = 17
    73  	BPF_BTF_LOAD            = 18
    74  	BPF_BTF_GET_FD_BY_ID    = 19
    75  	BPF_TASK_FD_QUERY       = 20
    76  
    77  	// BPF syscall attach types
    78  	BPF_CGROUP_INET_INGRESS     = 0
    79  	BPF_CGROUP_INET_EGRESS      = 1
    80  	BPF_CGROUP_INET_SOCK_CREATE = 2
    81  	BPF_CGROUP_SOCK_OPS         = 3
    82  	BPF_SK_SKB_STREAM_PARSER    = 4
    83  	BPF_SK_SKB_STREAM_VERDICT   = 5
    84  	BPF_CGROUP_DEVICE           = 6
    85  	BPF_SK_MSG_VERDICT          = 7
    86  	BPF_CGROUP_INET4_BIND       = 8
    87  	BPF_CGROUP_INET6_BIND       = 9
    88  	BPF_CGROUP_INET4_CONNECT    = 10
    89  	BPF_CGROUP_INET6_CONNECT    = 11
    90  	BPF_CGROUP_INET4_POST_BIND  = 12
    91  	BPF_CGROUP_INET6_POST_BIND  = 13
    92  	BPF_CGROUP_UDP4_SENDMSG     = 14
    93  	BPF_CGROUP_UDP6_SENDMSG     = 15
    94  	BPF_LIRC_MODE2              = 16
    95  	BPF_FLOW_DISSECTOR          = 17
    96  	BPF_CGROUP_SYSCTL           = 18
    97  	BPF_CGROUP_UDP4_RECVMSG     = 19
    98  	BPF_CGROUP_UDP6_RECVMSG     = 20
    99  
   100  	// Flags for BPF_MAP_UPDATE_ELEM. Must match values from linux/bpf.h
   101  	BPF_ANY     = 0
   102  	BPF_NOEXIST = 1
   103  	BPF_EXIST   = 2
   104  
   105  	// Flags for BPF_MAP_CREATE. Must match values from linux/bpf.h
   106  	BPF_F_NO_PREALLOC   = 1 << 0
   107  	BPF_F_NO_COMMON_LRU = 1 << 1
   108  	BPF_F_NUMA_NODE     = 1 << 2
   109  
   110  	// Flags for BPF_PROG_QUERY
   111  	BPF_F_QUERY_EFFECTVE = 1 << 0
   112  
   113  	// Flags for accessing BPF object
   114  	BPF_F_RDONLY = 1 << 3
   115  	BPF_F_WRONLY = 1 << 4
   116  
   117  	// Flag for stack_map, store build_id+offset instead of pointer
   118  	BPF_F_STACK_BUILD_ID = 1 << 5
   119  )
   120  
   121  // EnableMapPreAllocation enables BPF map pre-allocation on map types that
   122  // support it. This does not take effect on existing map although some map
   123  // types could be recreated later when objCheck() runs.
   124  func EnableMapPreAllocation() {
   125  	atomic.StoreUint32(&preAllocateMapSetting, 0)
   126  }
   127  
   128  // DisableMapPreAllocation disables BPF map pre-allocation as a default
   129  // setting. Some map types enforces pre-alloc strategy so this does not
   130  // take effect in that case. Also note that this does not take effect on
   131  // existing map although could be recreated later when objCheck() runs.
   132  func DisableMapPreAllocation() {
   133  	atomic.StoreUint32(&preAllocateMapSetting, 1)
   134  }
   135  
   136  // GetPreAllocateMapFlags returns the map flags for map which use conditional
   137  // pre-allocation.
   138  func GetPreAllocateMapFlags(t MapType) uint32 {
   139  	switch {
   140  	case !t.allowsPreallocation():
   141  		return BPF_F_NO_PREALLOC
   142  	case t.requiresPreallocation():
   143  		return 0
   144  	}
   145  	return atomic.LoadUint32(&preAllocateMapSetting)
   146  }