This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
349 lines (269 loc) · 5.91 KB
/
types.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* VC5 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 vc5tmp
import (
"errors"
"fmt"
"net"
"net/netip"
"sort"
"sync"
"unsafe"
)
type IP4 [4]byte
type MAC [6]byte
func (i IP4) String() string { return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3]) }
func (i *IP4) IsNil() bool { return i[0] == 0 && i[1] == 0 && i[2] == 0 && i[3] == 0 }
func (m MAC) String() string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", m[0], m[1], m[2], m[3], m[4], m[5])
}
func (m *MAC) string() string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", m[0], m[1], m[2], m[3], m[4], m[5])
}
func (m *MAC) MarshalText() ([]byte, error) {
return []byte(m.string()), nil
}
func (m *MAC) IsNil() bool {
return m[0] == 0 && m[1] == 0 && m[2] == 0 && m[3] == 0 && m[4] == 0 && m[5] == 0
}
type uP = unsafe.Pointer
func nltoh(n [4]byte) uint32 {
return uint32(n[0])<<24 | uint32(n[1])<<16 | uint32(n[2])<<8 | uint32(n[0])
}
var mutex sync.Mutex
const (
TCP protocol = 0x06
UDP protocol = 0x11
)
type Protocol uint8
type protocol = Protocol
type svc struct {
IP IP4
Port uint16
Protocol protocol
}
type nat_map map[[2]IP4]uint16
func (n *nat_map) set(tuples map[[2]IP4]bool) {
mutex.Lock()
defer mutex.Unlock()
nm := natmap(tuples, *n)
*n = nm
}
func (n *nat_map) get() map[[2]IP4]uint16 {
mutex.Lock()
defer mutex.Unlock()
r := map[[2]IP4]uint16{}
for k, v := range *n {
r[k] = v
}
return r
}
func (n *nat_map) rip() (r []IP4) {
mutex.Lock()
defer mutex.Unlock()
m := map[IP4]bool{}
for k, _ := range *n {
m[k[1]] = true
}
for k, _ := range m {
r = append(r, k)
}
return r
}
func (n *nat_map) ent(vip, rip IP4) uint16 {
mutex.Lock()
defer mutex.Unlock()
x := (map[[2]IP4]uint16)(*n)
i, _ := x[[2]IP4{vip, rip}]
return i
}
type tag_map map[IP4]uint16
func (t tag_map) set(ip IP4, id uint16) {
mutex.Lock()
defer mutex.Unlock()
x := (map[IP4]uint16)(t)
x[ip] = id
}
func (n *tag_map) get() map[IP4]uint16 {
mutex.Lock()
defer mutex.Unlock()
r := map[IP4]uint16{}
for k, v := range *n {
r[k] = v
}
return r
}
func (t tag_map) ent(ip IP4) (uint16, bool) {
mutex.Lock()
defer mutex.Unlock()
x := (map[IP4]uint16)(t)
r, ok := x[ip]
return r, ok
}
type vc struct {
vid uint16
net net.IPNet
}
func (c *Client) targets() (r []IP4) {
t := map[IP4]bool{}
nm := c.nat_map.get()
for k, _ := range nm {
rip := k[1]
t[rip] = true
}
for k, _ := range t {
r = append(r, k)
}
return
}
type Service struct {
Address netip.Addr
Port uint16
Protocol protocol
Sticky bool
//Scheduler uint8
//Leastconns bool
//LeastconnsIP IP4
//LeastconnsWeight uint8
backend map[IP4]*Destination
state *be_state
}
func (s *Service) Service(x svc) Service {
var r Service
r = *s
r.backend = map[IP4]*Destination{}
r.state = nil
return r
}
type ServiceExtended struct {
Service Service
Stats Stats
}
func (s *Service) update(u Service) {
//s.Scheduler = u.Scheduler
s.Sticky = u.Sticky
//s.Leastconns = u.Leastconns
//s.LeastconnsIP = u.LeastconnsIP
//s.LeastconnsWeight = u.LeastconnsWeight
}
func (s *Service) svc() (svc, error) {
if !s.Address.Is4() {
return svc{}, errors.New("Not IPv4")
}
ip := s.Address.As4()
return svc{IP: ip, Port: s.Port, Protocol: s.Protocol}, nil
}
type Destination struct {
Address netip.Addr
Weight uint8
}
func (d *Destination) rip() (IP4, error) {
if !d.Address.Is4() {
return IP4{}, errors.New("Not IPv4")
}
return d.Address.As4(), nil
}
func (d *Destination) extend(ip IP4) DestinationExtended {
var de DestinationExtended
de.Destination.Address = netip.AddrFrom4(ip)
de.Destination.Weight = d.Weight
return de
}
type Info struct {
Packets uint64
Octets uint64
Flows uint64
Latency uint64
Dropped uint64
Blocked uint64
NotQueued uint64
}
type Stats struct {
Packets uint64
Octets uint64
Flows uint64
}
func (s Stats) String() string { return fmt.Sprintf("p:%d o:%d f:%d", s.Packets, s.Octets, s.Flows) }
type DestinationExtended struct {
Destination Destination
MAC MAC
Stats Stats
}
func natmap(tuples map[[2]IP4]bool, previous map[[2]IP4]uint16) (mapping map[[2]IP4]uint16) {
mapping = map[[2]IP4]uint16{}
inverse := map[uint16][2]IP4{}
for k, v := range previous {
if _, ok := tuples[k]; ok {
if _, exists := inverse[v]; !exists {
inverse[v] = k
mapping[k] = v
}
}
}
var n uint16
for k, _ := range tuples {
if _, ok := mapping[k]; ok {
continue
}
find:
n++
if n > 65000 {
return
}
if _, ok := inverse[n]; ok {
goto find
}
mapping[k] = n
}
return
}
func (c *Client) vlanIDs() []vc {
var vlans []vc
for k, v := range c.vlans {
vlans = append(vlans, vc{k, v})
}
sort.SliceStable(vlans, func(i, j int) bool {
return vlans[i].vid < vlans[j].vid
})
return vlans
}
func (c *Client) tag(ips []IP4) map[IP4]uint16 {
vlans := c.vlanIDs()
r := map[IP4]uint16{}
outer:
for _, i := range ips {
ip := net.IP(i[:])
for _, v := range vlans {
if v.net.Contains(ip) {
r[i] = v.vid
continue outer
}
}
}
return r
}
func (c *Client) tag1(i IP4) uint16 {
vlans := c.vlanIDs()
ip := net.IP(i[:])
for _, v := range vlans {
if v.net.Contains(ip) {
return v.vid
}
}
return 0
}