-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipvs.go
50 lines (42 loc) · 1014 Bytes
/
ipvs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"syscall"
"github.com/moby/ipvs"
)
type ipvsWrapper struct {
handler *ipvs.Handle
}
//NewIpvsWrapper an ipvs wrapper to interact with kernel module ip_vs
func NewIpvsWrapper() (*ipvsWrapper, error) {
if handle, err := ipvs.New(""); err != nil {
return nil, err
} else {
return &ipvsWrapper{
handler: handle,
}, nil
}
}
//GetServices fetch all vs service instances
func (s *ipvsWrapper) GetServices() ([]*ipvs.Service, error) {
return s.handler.GetServices()
}
//GetDestinations fetch all destination of one vs
func (s *ipvsWrapper) GetDestinations(svc *ipvs.Service) ([]*ipvs.Destination, error) {
return s.handler.GetDestinations(svc)
}
//Close close ip_vs handler
func (s *ipvsWrapper) Close() {
s.handler.Close()
}
//Protocol proto string represent
func (s *ipvsWrapper) Protocol(proto uint16) string {
switch proto {
case syscall.IPPROTO_TCP:
return "TCP"
case syscall.IPPROTO_UDP:
return "UDP"
default:
return fmt.Sprintf("IP(%d)", proto)
}
}