-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxvs.go
More file actions
231 lines (191 loc) · 6.18 KB
/
xvs.go
File metadata and controls
231 lines (191 loc) · 6.18 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* vc5/xvs load balancer. Copyright (C) 2021-present David Coles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package xvs
import (
"fmt"
"net/netip"
)
type Logger interface {
Debug(msg string, args ...any)
}
type Protocol uint8
type TunnelType uint8
type tunnelFlags uint8
type flags uint8
const (
TCP Protocol = 0x06
UDP Protocol = 0x11
)
type Config struct {
IPv4VLANs map[uint16]netip.Prefix
IPv6VLANs map[uint16]netip.Prefix
Routes map[netip.Prefix]uint16
}
type Options struct {
DriverMode bool // Use XDP_FLAGS_DRV_MODE flag when attaching interface
Bonding bool // Explicitly declare interfaces to be aggregated
BPFProgram []byte // Override the embedded BPF program with this object code
FlowsPerCPU uint32 // Override default size of flow tracking tables
InterfaceInitDelay uint8 // Pause (seconds) between each link attach/detach; to prevent bonds flapping
IPv4VLANs map[uint16]netip.Prefix // VLAN ID/IPv4 Prefix mapping
IPv6VLANs map[uint16]netip.Prefix // VLAN ID/IPv6 Prefix mapping
Routes map[netip.Prefix]uint16 // Override route selection for layer 3 backends; prefix-to-VLAN ID map
Logger Logger
}
func (o *Options) config() *Config {
return &Config{IPv4VLANs: o.IPv4VLANs, IPv6VLANs: o.IPv6VLANs, Routes: o.Routes}
}
type Client interface {
Info() (Info, error)
Config() (Config, error)
SetConfig(Config) error
Services() ([]ServiceExtended, error)
Service(Service) (ServiceExtended, error)
CreateService(Service) error
UpdateService(Service) error
RemoveService(Service) error
Destinations(Service) ([]DestinationExtended, error)
CreateDestination(Service, Destination) error
UpdateDestination(Service, Destination) error
RemoveDestination(Service, Destination) error
// SetService combines the functionality of CreateService,
// UpdateService, CreateDestination, UpdateDestination and
// RemoveDestination. If the service does not exist it will be
// created with the given parameters and destinations, or updated
// to match them if extant.
SetService(Service, ...Destination) error
VIPs() ([]VIP, error)
VIP(netip.Addr) (VIP, error)
// NAT returns an address which can be used to query a specific
// virtual IP on a backend ("real") server, this can be used to
// implement health checks which accurately reflect the ability of
// the backend to serve traffic for a particular VIP.
NAT(vip, rip netip.Addr) (nat netip.Addr)
// ReadFlow retrieves an opaque flow record from a queue written
// to by the kernel. If no flow records are available then a zero
// length slice is returned. This can be used to share flow state
// with peers, storing the flow with the WriteFlow()
// function. Stale records in the queue (older than a few seconds)
// are skipped.
ReadFlow() []byte
WriteFlow([]byte)
}
type Service struct {
Address netip.Addr
Port uint16
Protocol Protocol
Sticky bool
//Flags Flags
}
type Stats struct {
Connections uint64
IncomingPackets uint64
IncomingBytes uint64
/*
Packets uint64
Octets uint64
Flows uint64
Current uint64
Errors uint64
*/
}
type ServiceExtended struct {
Service Service
Stats Stats
Metrics map[string]uint64
}
type Destination struct {
Address netip.Addr
TunnelType TunnelType
TunnelPort uint16
TunnelEncapNoChecksum bool
Disable bool
}
func (d *Destination) tunnelFlags() (f tunnelFlags) {
if d.TunnelEncapNoChecksum {
f |= tunnelEncapNoChecksums
}
return
}
type DestinationExtended struct {
Destination Destination
ActiveConnections uint32
Stats Stats
Metrics map[string]uint64
MAC [6]byte
}
type VIP struct {
Address netip.Addr
Stats Stats
Metrics map[string]uint64
}
func (s *Service) vrpp(d netip.Addr) bpf_vrpp {
return bpf_vrpp{vaddr: as16(s.Address), raddr: as16(d), vport: s.Port, protocol: uint16(s.Protocol)}
}
func (s *Service) check() error {
if !s.Address.IsValid() || s.Address.IsUnspecified() || s.Address.IsMulticast() || s.Address.IsLoopback() {
return fmt.Errorf("Bad IP address")
}
if s.Port == 0 {
return fmt.Errorf("Reserved port")
}
if s.Protocol != TCP && s.Protocol != UDP {
return fmt.Errorf("Unsupported protocol")
}
return nil
}
func (c *Config) copy() (r Config, e error) {
r = *c
r.IPv4VLANs = make(map[uint16]netip.Prefix, len(c.IPv4VLANs))
r.IPv6VLANs = make(map[uint16]netip.Prefix, len(c.IPv6VLANs))
r.Routes = make(map[netip.Prefix]uint16, len(c.Routes))
for k, v := range c.Routes {
r.Routes[k] = v
}
for k, v := range c.IPv4VLANs {
if k == 0 || k > 4094 {
return r, fmt.Errorf("%d is not a valid VLAN ID", k)
}
if !v.Addr().Is4() {
return r, fmt.Errorf("Non-IPv4 prefix in VLANs4: %s", v)
}
r.IPv4VLANs[k] = v
}
for k, v := range c.IPv6VLANs {
if k == 0 || k > 4094 {
return r, fmt.Errorf("%d is not a valid VLAN ID", k)
}
if !v.Addr().Is6() {
return r, fmt.Errorf("Non-IPv6 prefix in VLANs6: %s", v)
}
r.IPv6VLANs[k] = v
}
return
}
func New(interfaces ...string) (Client, error) {
return newClient(interfaces...)
}
func NewWithOptions(options Options, interfaces ...string) (Client, error) {
return newClientWithOptions(options, interfaces...)
}
func (d Destination) check() error {
if !d.Address.IsValid() || d.Address.IsUnspecified() || d.Address.IsMulticast() || d.Address.IsLoopback() {
return fmt.Errorf("Bad destination address: %s", d.Address)
}
return nil
}