forked from mqliang/libipvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
194 lines (167 loc) · 4.17 KB
/
api.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package libipvs
import (
"fmt"
"net"
"syscall"
"github.com/hkwi/nlgo"
)
type AddressFamily uint16
func (af AddressFamily) String() string {
switch af {
case syscall.AF_INET:
return "inet"
case syscall.AF_INET6:
return "inet6"
default:
return fmt.Sprintf("%d", af)
}
}
type Protocol uint16
func (p Protocol) String() string {
switch p {
case syscall.IPPROTO_TCP:
return "tcp"
case syscall.IPPROTO_UDP:
return "udp"
case syscall.IPPROTO_SCTP:
return "sctp"
default:
return fmt.Sprintf("%d", p)
}
}
type Flags struct {
Flags uint32
Mask uint32
}
// Service defines an IPVS service in its entirety.
type Service struct {
// Virtual service address.
Address net.IP
Protocol Protocol
Port uint16
FWMark uint32 // Firewall mark of the service.
// Virtual service options.
SchedName string
Flags Flags
Timeout uint32
Netmask uint32
AddressFamily AddressFamily
PEName string
Stats Stats
}
// Destination defines an IPVS destination (real server) in its
// entirety.
type Destination struct {
AddressFamily AddressFamily
Address net.IP
Port uint16
FwdMethod FwdMethod
Weight uint32
UThresh uint32
LThresh uint32
ActiveConns uint32
InactConns uint32
PersistConns uint32
Stats Stats
}
type Stats struct {
Connections uint32
PacketsIn uint32
PacketsOut uint32
BytesIn uint64
BytesOut uint64
CPS uint32
PPSIn uint32
PPSOut uint32
BPSIn uint32
BPSOut uint32
}
// Pack Service to a set of nlattrs.
// If full is given, include service settings, otherwise only the identifying fields are given.
func (self *Service) attrs(full bool) nlgo.AttrSlice {
var attrs nlgo.AttrSlice
if self.FWMark != 0 {
attrs = append(attrs,
nlattr(IPVS_SVC_ATTR_AF, nlgo.U16(self.AddressFamily)),
nlattr(IPVS_SVC_ATTR_FWMARK, nlgo.U32(self.FWMark)),
)
} else if self.Protocol != 0 && self.Address != nil {
attrs = append(attrs,
nlattr(IPVS_SVC_ATTR_AF, nlgo.U16(self.AddressFamily)),
nlattr(IPVS_SVC_ATTR_PROTOCOL, nlgo.U16(self.Protocol)),
nlattr(IPVS_SVC_ATTR_ADDR, packAddr(self.AddressFamily, self.Address)),
nlattr(IPVS_SVC_ATTR_PORT, packPort(self.Port)),
)
} else {
panic("Incomplete service id fields")
}
if full {
attrs = append(attrs,
nlattr(IPVS_SVC_ATTR_SCHED_NAME, nlgo.NulString(self.SchedName)),
nlattr(IPVS_SVC_ATTR_FLAGS, self.Flags.pack()),
nlattr(IPVS_SVC_ATTR_TIMEOUT, nlgo.U32(self.Timeout)),
nlattr(IPVS_SVC_ATTR_NETMASK, nlgo.U32(self.Netmask)),
)
}
return attrs
}
// Dump Dest as nl attrs, using the Af of the corresponding Service.
// If full, includes Dest setting attrs, otherwise only identifying attrs.
func (self *Destination) attrs(full bool) nlgo.AttrSlice {
var attrs nlgo.AttrSlice
attrs = append(attrs,
nlattr(IPVS_DEST_ATTR_ADDR_FAMILY, nlgo.U16(self.AddressFamily)),
nlattr(IPVS_DEST_ATTR_ADDR, packAddr(self.AddressFamily, self.Address)),
nlattr(IPVS_DEST_ATTR_PORT, packPort(self.Port)),
)
if full {
attrs = append(attrs,
nlattr(IPVS_DEST_ATTR_FWD_METHOD, nlgo.U32(self.FwdMethod)),
nlattr(IPVS_DEST_ATTR_WEIGHT, nlgo.U32(self.Weight)),
nlattr(IPVS_DEST_ATTR_U_THRESH, nlgo.U32(self.UThresh)),
nlattr(IPVS_DEST_ATTR_L_THRESH, nlgo.U32(self.LThresh)),
)
}
return attrs
}
type FwdMethod uint32
func (self FwdMethod) String() string {
switch value := (uint32(self) & IP_VS_CONN_F_FWD_MASK); value {
case IP_VS_CONN_F_MASQ:
return "masq"
case IP_VS_CONN_F_LOCALNODE:
return "localnode"
case IP_VS_CONN_F_TUNNEL:
return "tunnel"
case IP_VS_CONN_F_DROUTE:
return "droute"
case IP_VS_CONN_F_BYPASS:
return "bypass"
default:
return fmt.Sprintf("%#04x", value)
}
}
func ParseFwdMethod(value string) (FwdMethod, error) {
switch value {
case "masq":
return IP_VS_CONN_F_MASQ, nil
case "tunnel":
return IP_VS_CONN_F_TUNNEL, nil
case "droute":
return IP_VS_CONN_F_DROUTE, nil
default:
return 0, fmt.Errorf("Invalid FwdMethod: %s", value)
}
}
type Version uint32
func (version Version) String() string {
return fmt.Sprintf("%d.%d.%d",
(version>>16)&0xFF,
(version>>8)&0xFF,
(version>>0)&0xFF,
)
}
type Info struct {
Version Version
ConnTabSize uint32
}