github.com/cilium/cilium@v1.16.2/pkg/maps/lbmap/types.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package lbmap
     5  
     6  import (
     7  	"net"
     8  
     9  	"github.com/cilium/cilium/pkg/bpf"
    10  	cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
    11  	"github.com/cilium/cilium/pkg/loadbalancer"
    12  )
    13  
    14  // ServiceKey is the interface describing protocol independent key for services map v2.
    15  type ServiceKey interface {
    16  	bpf.MapKey
    17  
    18  	// Return true if the key is of type IPv6
    19  	IsIPv6() bool
    20  
    21  	// IsSurrogate returns true on zero-address
    22  	IsSurrogate() bool
    23  
    24  	// Return the BPF map matching the key type
    25  	Map() *bpf.Map
    26  
    27  	// Set backend slot for the key
    28  	SetBackendSlot(slot int)
    29  
    30  	// Get backend slot of the key
    31  	GetBackendSlot() int
    32  
    33  	// Set lookup scope for the key
    34  	SetScope(scope uint8)
    35  
    36  	// Get lookup scope for the key
    37  	GetScope() uint8
    38  
    39  	// Get frontend IP address
    40  	GetAddress() net.IP
    41  
    42  	// Get frontend port
    43  	GetPort() uint16
    44  
    45  	// Returns a RevNatValue matching a ServiceKey
    46  	RevNatValue() RevNatValue
    47  
    48  	// Delete entry identified with the key from the matching map
    49  	MapDelete() error
    50  
    51  	// ToNetwork converts fields to network byte order.
    52  	ToNetwork() ServiceKey
    53  
    54  	// ToHost converts fields to host byte order.
    55  	ToHost() ServiceKey
    56  }
    57  
    58  // ServiceValue is the interface describing protocol independent value for services map v2.
    59  type ServiceValue interface {
    60  	bpf.MapValue
    61  
    62  	// Set the number of backends
    63  	SetCount(int)
    64  
    65  	// Get the number of backends
    66  	GetCount() int
    67  
    68  	// Set reverse NAT identifier
    69  	SetRevNat(int)
    70  
    71  	// Get reverse NAT identifier
    72  	GetRevNat() int
    73  
    74  	// Set flags
    75  	SetFlags(uint16)
    76  
    77  	// Get flags
    78  	GetFlags() uint16
    79  
    80  	// Set timeout for sessionAffinity=clientIP
    81  	SetSessionAffinityTimeoutSec(t uint32)
    82  
    83  	// Set proxy port for l7 loadbalancer services
    84  	SetL7LBProxyPort(port uint16)
    85  
    86  	// Set backend identifier
    87  	SetBackendID(id loadbalancer.BackendID)
    88  
    89  	// Get backend identifier
    90  	GetBackendID() loadbalancer.BackendID
    91  
    92  	// Returns a RevNatKey matching a ServiceValue
    93  	RevNatKey() RevNatKey
    94  
    95  	// Convert fields to network byte order.
    96  	ToNetwork() ServiceValue
    97  
    98  	// ToHost converts fields to host byte order.
    99  	ToHost() ServiceValue
   100  }
   101  
   102  // BackendKey is the interface describing protocol independent backend key.
   103  type BackendKey interface {
   104  	bpf.MapKey
   105  
   106  	// Return the BPF map matching the type
   107  	Map() *bpf.Map
   108  
   109  	// Set backend identifier
   110  	SetID(loadbalancer.BackendID)
   111  
   112  	// Get backend identifier
   113  	GetID() loadbalancer.BackendID
   114  }
   115  
   116  // BackendValue is the interface describing protocol independent backend value.
   117  type BackendValue interface {
   118  	bpf.MapValue
   119  
   120  	// Get backend address
   121  	GetAddress() net.IP
   122  
   123  	// Get backend IP + clusterID
   124  	GetIPCluster() cmtypes.AddrCluster
   125  
   126  	// Get backend port
   127  	GetPort() uint16
   128  
   129  	// Get backend flags
   130  	GetFlags() uint8
   131  
   132  	// Get zone
   133  	GetZone() uint8
   134  
   135  	// Convert fields to network byte order.
   136  	ToNetwork() BackendValue
   137  
   138  	// ToHost converts fields to host byte order.
   139  	ToHost() BackendValue
   140  }
   141  
   142  // Backend is the interface describing protocol independent backend used by services v2.
   143  type Backend interface {
   144  	// Return the BPF map matching the type
   145  	Map() *bpf.Map
   146  
   147  	// Get key of the backend entry
   148  	GetKey() BackendKey
   149  
   150  	// Get value of the backend entry
   151  	GetValue() BackendValue
   152  }
   153  
   154  type RevNatKey interface {
   155  	bpf.MapKey
   156  
   157  	// Returns the BPF map matching the key type
   158  	Map() *bpf.Map
   159  
   160  	// ToNetwork converts fields to network byte order.
   161  	ToNetwork() RevNatKey
   162  
   163  	// Returns the key value
   164  	GetKey() uint16
   165  
   166  	// ToHost converts fields to host byte order.
   167  	ToHost() RevNatKey
   168  }
   169  
   170  type RevNatValue interface {
   171  	bpf.MapValue
   172  
   173  	// ToNetwork converts fields to network byte order.
   174  	ToNetwork() RevNatValue
   175  
   176  	// ToHost converts fields to host byte order.
   177  	ToHost() RevNatValue
   178  }
   179  
   180  func svcFrontend(svcKey ServiceKey, svcValue ServiceValue) *loadbalancer.L3n4AddrID {
   181  	feIP := svcKey.GetAddress()
   182  	feAddrCluster := cmtypes.MustAddrClusterFromIP(feIP)
   183  	feL3n4Addr := loadbalancer.NewL3n4Addr(loadbalancer.NONE, feAddrCluster, svcKey.GetPort(), svcKey.GetScope())
   184  	feL3n4AddrID := &loadbalancer.L3n4AddrID{
   185  		L3n4Addr: *feL3n4Addr,
   186  		ID:       loadbalancer.ID(svcValue.GetRevNat()),
   187  	}
   188  	return feL3n4AddrID
   189  }
   190  
   191  func svcBackend(backendID loadbalancer.BackendID, backend BackendValue) *loadbalancer.Backend {
   192  	beIP := backend.GetAddress()
   193  	beAddrCluster := cmtypes.MustAddrClusterFromIP(beIP)
   194  	bePort := backend.GetPort()
   195  	beProto := loadbalancer.NONE
   196  	beState := loadbalancer.GetBackendStateFromFlags(backend.GetFlags())
   197  	beZone := backend.GetZone()
   198  	beBackend := loadbalancer.NewBackendWithState(backendID, beProto, beAddrCluster, bePort, beZone, beState)
   199  	return beBackend
   200  }