-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbpf.go
137 lines (120 loc) · 2.88 KB
/
bpf.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
/*
* 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 (
"bytes"
"compress/gzip"
_ "embed"
"fmt"
"io/ioutil"
)
//go:embed bpf/bpf.o.gz
var bpf_gz []byte
var bpf_o []byte
func init() {
if z, err := gzip.NewReader(bytes.NewReader(bpf_gz)); err == nil {
bpf_o, _ = ioutil.ReadAll(z)
}
}
type bpf_nat struct {
vip [4]byte
vid uint16
mac [6]byte
}
func (b *bpf_nat) String() string { return fmt.Sprintf("%s|%s|%d", b4s(b.vip), b6s(b.mac), b.vid) }
type bpf_redirect struct {
addr [4]byte
index uint32
dest [6]byte
source [6]byte
}
func (b *bpf_redirect) String() string {
return fmt.Sprintf("%s|%d|%s|%s", b4s(b.addr), b.index, b6s(b.source), b6s(b.dest))
}
type bpf_counter struct {
packets uint64
octets uint64
flows uint64
_pad uint64
}
func (c *bpf_counter) add(a bpf_counter) {
c.octets += a.octets
c.packets += a.packets
c.flows += a.flows
}
type bpf_service struct {
vip [4]byte
port [2]byte
protocol uint8
_pad uint8
}
type bpf_vrpp struct {
vip [4]byte //__be32 vip;
rip [4]byte //__be32 rip;
port [2]byte //__be16 port;
protocol byte //__u8 protocol;
pad byte //__u8 pad;
}
type bpf_backend struct {
real [256]bpf_real
hash [8192]byte
}
type bpf_real struct {
rip [4]byte //__be32
vid uint16
mac [6]byte
flag [4]byte
}
type bpf_global struct {
rx_packets uint64
rx_octets uint64
perf_packets uint64
perf_timens uint64
perf_timer uint64
settings_timer uint64
new_flows uint64
dropped uint64
qfailed uint64
blocked uint64
toobig uint64
}
func (g *bpf_global) add(a bpf_global) {
g.rx_packets += a.rx_packets
g.rx_octets += a.rx_octets
g.perf_packets += a.perf_packets
g.perf_timens += a.perf_timens
g.new_flows += a.new_flows
g.qfailed += a.qfailed
g.dropped += a.dropped
g.blocked += a.blocked
g.toobig += a.toobig
}
func (g *bpf_global) latency() uint64 {
var latency uint64 = 500 // 500ns target value
if g.perf_packets > 0 {
latency = g.perf_timens / g.perf_packets
}
return latency
}
type bpf_setting struct {
heartbeat uint32
era uint8
features uint8
pad1 uint8
pad2 uint8
}