github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/tunnel/tunnel.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 tunnel 16 17 import ( 18 "net" 19 "unsafe" 20 21 "github.com/cilium/cilium/pkg/bpf" 22 23 "github.com/sirupsen/logrus" 24 ) 25 26 const ( 27 MapName = "cilium_tunnel_map" 28 29 // MaxEntries is the maximum entries in the tunnel endpoint map 30 MaxEntries = 65536 31 ) 32 33 var ( 34 // TunnelMap represents the BPF map for tunnels 35 TunnelMap = NewTunnelMap(MapName) 36 ) 37 38 // Map implements tunnel connectivity configuration in the BPF datapath. 39 type Map struct { 40 *bpf.Map 41 } 42 43 // NewTunnelMap returns a new tunnel map with the specified name. 44 func NewTunnelMap(name string) *Map { 45 return &Map{Map: bpf.NewMap(MapName, 46 bpf.MapTypeHash, 47 &TunnelEndpoint{}, 48 int(unsafe.Sizeof(TunnelEndpoint{})), 49 &TunnelEndpoint{}, 50 int(unsafe.Sizeof(TunnelEndpoint{})), 51 MaxEntries, 52 0, 0, 53 bpf.ConvertKeyValue, 54 ).WithCache(), 55 } 56 } 57 58 func init() { 59 TunnelMap.NonPersistent = true 60 } 61 62 // +k8s:deepcopy-gen=true 63 // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey 64 // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue 65 type TunnelEndpoint struct { 66 bpf.EndpointKey 67 } 68 69 func newTunnelEndpoint(ip net.IP) *TunnelEndpoint { 70 return &TunnelEndpoint{ 71 EndpointKey: bpf.NewEndpointKey(ip), 72 } 73 } 74 75 func (v TunnelEndpoint) NewValue() bpf.MapValue { return &TunnelEndpoint{} } 76 77 // SetTunnelEndpoint adds/replaces a prefix => tunnel-endpoint mapping 78 func (m *Map) SetTunnelEndpoint(encryptKey uint8, prefix, endpoint net.IP) error { 79 key, val := newTunnelEndpoint(prefix), newTunnelEndpoint(endpoint) 80 val.EndpointKey.Key = encryptKey 81 log.WithFields(logrus.Fields{ 82 fieldPrefix: prefix, 83 fieldEndpoint: endpoint, 84 fieldKey: encryptKey, 85 }).Debug("Updating tunnel map entry") 86 87 return TunnelMap.Update(key, val) 88 } 89 90 // GetTunnelEndpoint removes a prefix => tunnel-endpoint mapping 91 func (m *Map) GetTunnelEndpoint(prefix net.IP) (net.IP, error) { 92 val, err := TunnelMap.Lookup(newTunnelEndpoint(prefix)) 93 if err != nil { 94 return net.IP{}, err 95 } 96 97 return val.(*TunnelEndpoint).ToIP(), nil 98 } 99 100 // DeleteTunnelEndpoint removes a prefix => tunnel-endpoint mapping 101 func (m *Map) DeleteTunnelEndpoint(prefix net.IP) error { 102 log.WithField(fieldPrefix, prefix).Debug("Deleting tunnel map entry") 103 return TunnelMap.Delete(newTunnelEndpoint(prefix)) 104 }