github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/ipvs/ipvs.go (about) 1 // +build linux 2 3 package ipvs 4 5 import ( 6 "net" 7 "syscall" 8 9 "github.com/vishvananda/netlink/nl" 10 "github.com/vishvananda/netns" 11 ) 12 13 // Service defines an IPVS service in its entirety. 14 type Service struct { 15 // Virtual service address. 16 Address net.IP 17 Protocol uint16 18 Port uint16 19 FWMark uint32 // Firewall mark of the service. 20 21 // Virtual service options. 22 SchedName string 23 Flags uint32 24 Timeout uint32 25 Netmask uint32 26 AddressFamily uint16 27 PEName string 28 } 29 30 // Destination defines an IPVS destination (real server) in its 31 // entirety. 32 type Destination struct { 33 Address net.IP 34 Port uint16 35 Weight int 36 ConnectionFlags uint32 37 AddressFamily uint16 38 UpperThreshold uint32 39 LowerThreshold uint32 40 } 41 42 // Handle provides a namespace specific ipvs handle to program ipvs 43 // rules. 44 type Handle struct { 45 sock *nl.NetlinkSocket 46 } 47 48 // New provides a new ipvs handle in the namespace pointed to by the 49 // passed path. It will return a valid handle or an error in case an 50 // error occured while creating the handle. 51 func New(path string) (*Handle, error) { 52 setup() 53 54 n := netns.None() 55 if path != "" { 56 var err error 57 n, err = netns.GetFromPath(path) 58 if err != nil { 59 return nil, err 60 } 61 } 62 defer n.Close() 63 64 sock, err := nl.GetNetlinkSocketAt(n, netns.None(), syscall.NETLINK_GENERIC) 65 if err != nil { 66 return nil, err 67 } 68 69 return &Handle{sock: sock}, nil 70 } 71 72 // Close closes the ipvs handle. The handle is invalid after Close 73 // returns. 74 func (i *Handle) Close() { 75 if i.sock != nil { 76 i.sock.Close() 77 } 78 } 79 80 // NewService creates a new ipvs service in the passed handle. 81 func (i *Handle) NewService(s *Service) error { 82 return i.doCmd(s, nil, ipvsCmdNewService) 83 } 84 85 // UpdateService updates an already existing service in the passed 86 // handle. 87 func (i *Handle) UpdateService(s *Service) error { 88 return i.doCmd(s, nil, ipvsCmdSetService) 89 } 90 91 // DelService deletes an already existing service in the passed 92 // handle. 93 func (i *Handle) DelService(s *Service) error { 94 return i.doCmd(s, nil, ipvsCmdDelService) 95 } 96 97 // NewDestination creates a new real server in the passed ipvs 98 // service which should already be existing in the passed handle. 99 func (i *Handle) NewDestination(s *Service, d *Destination) error { 100 return i.doCmd(s, d, ipvsCmdNewDest) 101 } 102 103 // UpdateDestination updates an already existing real server in the 104 // passed ipvs service in the passed handle. 105 func (i *Handle) UpdateDestination(s *Service, d *Destination) error { 106 return i.doCmd(s, d, ipvsCmdSetDest) 107 } 108 109 // DelDestination deletes an already existing real server in the 110 // passed ipvs service in the passed handle. 111 func (i *Handle) DelDestination(s *Service, d *Destination) error { 112 return i.doCmd(s, d, ipvsCmdDelDest) 113 }