k8s.io/kubernetes@v1.29.3/pkg/proxy/ipvs/util/ipvs.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ipvs
    18  
    19  import (
    20  	"net"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  )
    25  
    26  // Interface is an injectable interface for running ipvs commands.  Implementations must be goroutine-safe.
    27  type Interface interface {
    28  	// Flush clears all virtual servers in system. return occurred error immediately.
    29  	Flush() error
    30  	// AddVirtualServer creates the specified virtual server.
    31  	AddVirtualServer(*VirtualServer) error
    32  	// UpdateVirtualServer updates an already existing virtual server.  If the virtual server does not exist, return error.
    33  	UpdateVirtualServer(*VirtualServer) error
    34  	// DeleteVirtualServer deletes the specified virtual server.  If the virtual server does not exist, return error.
    35  	DeleteVirtualServer(*VirtualServer) error
    36  	// Given a partial virtual server, GetVirtualServer will return the specified virtual server information in the system.
    37  	GetVirtualServer(*VirtualServer) (*VirtualServer, error)
    38  	// GetVirtualServers lists all virtual servers in the system.
    39  	GetVirtualServers() ([]*VirtualServer, error)
    40  	// AddRealServer creates the specified real server for the specified virtual server.
    41  	AddRealServer(*VirtualServer, *RealServer) error
    42  	// GetRealServers returns all real servers for the specified virtual server.
    43  	GetRealServers(*VirtualServer) ([]*RealServer, error)
    44  	// DeleteRealServer deletes the specified real server from the specified virtual server.
    45  	DeleteRealServer(*VirtualServer, *RealServer) error
    46  	// UpdateRealServer updates the specified real server from the specified virtual server.
    47  	UpdateRealServer(*VirtualServer, *RealServer) error
    48  	// ConfigureTimeouts is the equivalent to running "ipvsadm --set" to configure tcp, tcpfin and udp timeouts
    49  	ConfigureTimeouts(time.Duration, time.Duration, time.Duration) error
    50  }
    51  
    52  // VirtualServer is an user-oriented definition of an IPVS virtual server in its entirety.
    53  type VirtualServer struct {
    54  	Address   net.IP
    55  	Protocol  string
    56  	Port      uint16
    57  	Scheduler string
    58  	Flags     ServiceFlags
    59  	Timeout   uint32
    60  }
    61  
    62  // ServiceFlags is used to specify session affinity, ip hash etc.
    63  type ServiceFlags uint32
    64  
    65  const (
    66  	// FlagPersistent specify IPVS service session affinity
    67  	FlagPersistent = 0x1
    68  	// FlagHashed specify IPVS service hash flag
    69  	FlagHashed = 0x2
    70  	// FlagSourceHash enables IPVS service hashing on source port and source IP
    71  	FlagSourceHash = 0x10
    72  )
    73  
    74  // Equal check the equality of virtual server.
    75  // We don't use struct == since it doesn't work because of slice.
    76  func (svc *VirtualServer) Equal(other *VirtualServer) bool {
    77  	return svc.Address.Equal(other.Address) &&
    78  		svc.Protocol == other.Protocol &&
    79  		svc.Port == other.Port &&
    80  		svc.Scheduler == other.Scheduler &&
    81  		svc.Flags == other.Flags &&
    82  		svc.Timeout == other.Timeout
    83  }
    84  
    85  func (svc *VirtualServer) String() string {
    86  	return net.JoinHostPort(svc.Address.String(), strconv.Itoa(int(svc.Port))) + "/" + svc.Protocol
    87  }
    88  
    89  // RealServer is an user-oriented definition of an IPVS real server in its entirety.
    90  type RealServer struct {
    91  	Address      net.IP
    92  	Port         uint16
    93  	Weight       int
    94  	ActiveConn   int
    95  	InactiveConn int
    96  }
    97  
    98  func (rs *RealServer) String() string {
    99  	return net.JoinHostPort(rs.Address.String(), strconv.Itoa(int(rs.Port)))
   100  }
   101  
   102  // Equal check the equality of real server.
   103  // We don't use struct == since it doesn't work because of slice.
   104  func (rs *RealServer) Equal(other *RealServer) bool {
   105  	return rs.Address.Equal(other.Address) &&
   106  		rs.Port == other.Port
   107  }
   108  
   109  // IsRsGracefulTerminationNeeded returns true if protocol requires graceful termination for the stale connections
   110  func IsRsGracefulTerminationNeeded(proto string) bool {
   111  	return !strings.EqualFold(proto, "UDP") && !strings.EqualFold(proto, "SCTP")
   112  }