github.com/zhyoulun/cilium@v1.6.12/pkg/maps/sockmap/sockmap.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 sockmap
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"unsafe"
    21  
    22  	"github.com/cilium/cilium/common/types"
    23  	"github.com/cilium/cilium/pkg/bpf"
    24  	"github.com/cilium/cilium/pkg/logging"
    25  	"github.com/cilium/cilium/pkg/logging/logfields"
    26  )
    27  
    28  // SockmapKey is the 5-tuple used to lookup a socket
    29  // +k8s:deepcopy-gen=true
    30  // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
    31  type SockmapKey struct {
    32  	DIP    types.IPv6 `align:"$union0"`
    33  	SIP    types.IPv6 `align:"$union1"`
    34  	Family uint8      `align:"family"`
    35  	Pad7   uint8      `align:"pad7"`
    36  	Pad8   uint16     `align:"pad8"`
    37  	SPort  uint32     `align:"sport"`
    38  	DPort  uint32     `align:"dport"`
    39  }
    40  
    41  // SockmapValue is the fd of a socket
    42  // +k8s:deepcopy-gen=true
    43  // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
    44  type SockmapValue struct {
    45  	fd uint32
    46  }
    47  
    48  // String pretty print the 5-tuple as sip:sport->dip:dport
    49  func (v SockmapKey) String() string {
    50  	return fmt.Sprintf("%s:%d->%s:%d", v.SIP.String(), v.SPort, v.DIP.String(), v.DPort)
    51  }
    52  
    53  // String pretty print the file descriptor value, note this is local to agent.
    54  func (v SockmapValue) String() string {
    55  	return fmt.Sprintf("%d", v.fd)
    56  }
    57  
    58  // GetValuePtr returns the unsafe pointer to the BPF value.
    59  func (v *SockmapValue) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(v) }
    60  
    61  // GetKeyPtr returns the unsafe pointer to the BPF key
    62  func (k *SockmapKey) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
    63  
    64  // NewValue returns a new empty instance of the structure representing the BPF
    65  // map value
    66  func (k SockmapKey) NewValue() bpf.MapValue { return &SockmapValue{} }
    67  
    68  // NewSockmapKey returns a new key using 5-tuple input.
    69  func NewSockmapKey(dip, sip net.IP, sport, dport uint32) SockmapKey {
    70  	result := SockmapKey{}
    71  
    72  	if sip4 := sip.To4(); sip4 != nil {
    73  		result.Family = bpf.EndpointKeyIPv4
    74  		copy(result.SIP[:], sip4)
    75  	} else {
    76  		result.Family = bpf.EndpointKeyIPv6
    77  		copy(result.SIP[:], sip)
    78  	}
    79  
    80  	if dip4 := dip.To4(); dip4 != nil {
    81  		result.Family = bpf.EndpointKeyIPv4
    82  		copy(result.SIP[:], dip4)
    83  	} else {
    84  		result.Family = bpf.EndpointKeyIPv6
    85  		copy(result.DIP[:], dip)
    86  	}
    87  
    88  	result.DPort = dport
    89  	result.SPort = sport
    90  	return result
    91  }
    92  
    93  var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "sockmap")
    94  
    95  const (
    96  	mapName = "cilium_sock_ops"
    97  
    98  	// MaxEntries represents the maximum number of endpoints in the map
    99  	MaxEntries = 65535
   100  )
   101  
   102  var (
   103  	// SockMap represents the BPF map for sockets
   104  	SockMap = bpf.NewMap(mapName,
   105  		bpf.MapTypeSockHash,
   106  		&SockmapKey{},
   107  		int(unsafe.Sizeof(SockmapKey{})),
   108  		&SockmapValue{},
   109  		4,
   110  		MaxEntries,
   111  		0, 0,
   112  		bpf.ConvertKeyValue,
   113  	)
   114  )
   115  
   116  // SockmapCreate will create sockmap map
   117  func SockmapCreate() {
   118  	SockMap.OpenOrCreate()
   119  }