github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/policymap/callmap.go (about) 1 // Copyright 2018-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 policymap 16 17 import ( 18 "fmt" 19 "unsafe" 20 21 "github.com/cilium/cilium/pkg/bpf" 22 ) 23 24 // PolicyPlumbingMap maps endpoint IDs to the fd for the program which 25 // implements its policy. 26 type PolicyPlumbingMap struct { 27 *bpf.Map 28 } 29 30 // +k8s:deepcopy-gen=true 31 // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey 32 type PlumbingKey struct { 33 key uint32 34 } 35 36 // +k8s:deepcopy-gen=true 37 // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue 38 type PlumbingValue struct { 39 fd uint32 40 } 41 42 func (k *PlumbingKey) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) } 43 func (k *PlumbingKey) NewValue() bpf.MapValue { return &PlumbingValue{} } 44 45 func (k *PlumbingKey) String() string { 46 return fmt.Sprintf("Endpoint: %d", k.key) 47 } 48 49 func (v *PlumbingValue) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(v) } 50 51 func (v *PlumbingValue) String() string { 52 return fmt.Sprintf("fd: %d", v.fd) 53 } 54 55 // RemoveGlobalMapping removes the mapping from the specified endpoint ID to 56 // the BPF policy program for that endpoint. 57 func RemoveGlobalMapping(id uint32) error { 58 gpm, err := OpenCallMap() 59 if err == nil { 60 k := PlumbingKey{ 61 key: id, 62 } 63 err = gpm.Map.Delete(&k) 64 gpm.Close() 65 } 66 67 return err 68 } 69 70 // OpenCallMap opens the map that maps endpoint IDs to program file 71 // descriptors, which allows tail calling into the policy datapath code from 72 // other BPF programs. 73 func OpenCallMap() (*PolicyPlumbingMap, error) { 74 m, err := bpf.OpenMap(CallMapName) 75 if err != nil { 76 return nil, err 77 } 78 m.MapKey = &PlumbingKey{} 79 m.MapValue = &PlumbingValue{} 80 return &PolicyPlumbingMap{Map: m}, nil 81 } 82 83 // CallString returns the string which indicates the calls map by index in the 84 // ELF, and index into that call map for a specific endpoint. 85 // 86 // Derived from __section_tail(CILIUM_MAP_CALLS, NAME) per bpf/lib/tailcall.h. 87 func CallString(id uint16) string { 88 return fmt.Sprintf("1/%#04x", id) 89 }