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