forked from platinasystems/xeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxeth.go
328 lines (304 loc) · 6.75 KB
/
xeth.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
/* XETH driver sideband control.
*
* Copyright(c) 2018 Platina Systems, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*
* Contact Information:
* Platina Systems, 3180 Del La Cruz Blvd, Santa Clara, CA 95054
*/
package xeth
import (
"fmt"
"io"
"net"
"os"
"syscall"
"time"
)
const netname = "unixpacket"
var (
Count struct {
Tx struct {
Sent, Dropped uint64
}
}
// Receive message channel feed from sock by gorx
RxCh <-chan []byte
xeth struct {
name string
addr *net.UnixAddr
sock *net.UnixConn
rxch chan []byte
txch chan []byte
}
)
// Connect to @xeth socket and run channel service routines
// driver :: XETH driver name (e.g. "platina-mk1")
func Start(driver string) error {
var err error
xeth.name = driver
xeth.addr, err = net.ResolveUnixAddr(netname, "@xeth")
if err != nil {
return err
}
for {
xeth.sock, err = net.DialUnix(netname, nil, xeth.addr)
if err == nil {
break
}
if !isEAGAIN(err) {
return err
}
}
xeth.rxch = make(chan []byte, 4)
xeth.txch = make(chan []byte, 4)
Interface.index = make(map[int32]*InterfaceEntry)
Interface.dir = make(map[string]*InterfaceEntry)
RxCh = xeth.rxch
go gorx()
go gotx()
// load Interface cache
DumpIfinfo()
UntilBreak(func(buf []byte) error {
return nil
})
return nil
}
// Close @xeth socket and shutdown service routines
func Stop() {
const (
SHUT_RD = iota
SHUT_WR
SHUT_RDWR
)
if xeth.sock == nil {
return
}
close(xeth.txch)
sock := xeth.sock
xeth.sock = nil
if f, err := sock.File(); err == nil {
syscall.Shutdown(int(f.Fd()), SHUT_RDWR)
}
sock.Close()
Interface.indexes = Interface.indexes[:0]
for ifindex := range Interface.index {
delete(Interface.index, ifindex)
}
Interface.index = nil
for name := range Interface.dir {
delete(Interface.dir, name)
}
Interface.dir = nil
}
// Return driver name (e.g. "platina-mk1")
func String() string { return xeth.name }
// Send carrier state change message
func Carrier(ifindex int32, flag uint8) error {
buf := Pool.Get(SizeofMsgCarrier)
defer Pool.Put(buf)
msg := ToMsgCarrier(buf)
msg.Kind = uint8(XETH_MSG_KIND_CARRIER)
msg.Ifindex = ifindex
msg.Flag = flag
return tx(buf, 0)
}
// Send DumpFib request
func DumpFib() error {
buf := Pool.Get(SizeofMsgDumpFibinfo)
defer Pool.Put(buf)
msg := ToMsg(buf)
msg.Kind = XETH_MSG_KIND_DUMP_FIBINFO
return tx(buf, 0)
}
// Send DumpIfinfo request then flush RxCh until break to cache ifinfos
func CacheIfinfo() {
if err := DumpIfinfo(); err == nil {
UntilBreak(func(buf []byte) error { return nil })
}
}
// Send DumpIfinfo request
func DumpIfinfo() error {
buf := Pool.Get(SizeofMsgDumpIfinfo)
defer Pool.Put(buf)
msg := ToMsg(buf)
msg.Kind = XETH_MSG_KIND_DUMP_IFINFO
return tx(buf, 0)
}
// Send stat update message
func SetStat(ifindex int32, stat string, count uint64) error {
var statindex uint64
var kind uint8
if linkstat, found := LinkStatOf(stat); found {
kind = uint8(XETH_MSG_KIND_LINK_STAT)
statindex = uint64(linkstat)
} else if ethtoolstat, found := EthtoolStatOf(stat); found {
kind = uint8(XETH_MSG_KIND_ETHTOOL_STAT)
statindex = uint64(ethtoolstat)
} else {
return fmt.Errorf("%q unknown", stat)
}
buf := Pool.Get(SizeofMsgStat)
defer Pool.Put(buf)
msg := ToMsgStat(buf)
msg.Kind = kind
msg.Ifindex = ifindex
msg.Index = statindex
msg.Count = count
return tx(buf, 10*time.Millisecond)
}
// Send speed change message
func Speed(index int, count uint64) error {
buf := Pool.Get(SizeofMsgSpeed)
defer Pool.Put(buf)
msg := ToMsgSpeed(buf)
msg.Kind = uint8(XETH_MSG_KIND_SPEED)
msg.Ifindex = int32(index)
msg.Mbps = uint32(count)
return tx(buf, 0)
}
// Send through leaky bucket
func Tx(buf []byte) {
msg := Pool.Get(len(buf))
copy(msg, buf)
select {
case xeth.txch <- msg:
Count.Tx.Sent++
default:
Count.Tx.Dropped++
Pool.Put(msg)
}
}
func UntilBreak(f func([]byte) error) error {
for buf := range RxCh {
if KindOf(buf) == XETH_MSG_KIND_BREAK {
Pool.Put(buf)
break
}
err := f(buf)
Pool.Put(buf)
if err != nil {
return err
}
}
return nil
}
func UntilSig(sig <-chan os.Signal, f func([]byte) error) error {
for {
select {
case <-sig:
return nil
case buf, ok := <-RxCh:
if !ok {
return nil
}
err := f(buf)
Pool.Put(buf)
if err != nil {
return err
}
}
}
}
func isEAGAIN(err error) bool {
if err != nil {
if operr, ok := err.(*net.OpError); ok {
if oserr, ok := operr.Err.(*os.SyscallError); ok {
if oserr.Err == syscall.EAGAIN {
return true
}
}
}
}
return false
}
func isTimeout(err error) bool {
if err != nil {
if op, ok := err.(*net.OpError); ok {
return op.Timeout()
}
}
return false
}
func gorx() {
const minrxto = 10 * time.Millisecond
const maxrxto = 320 * time.Millisecond
rxto := minrxto
rxbuf := Pool.Get(PageSize)
defer Pool.Put(rxbuf)
rxoob := Pool.Get(PageSize)
defer Pool.Put(rxoob)
defer close(xeth.rxch)
for xeth.sock != nil {
err := xeth.sock.SetReadDeadline(time.Now().Add(rxto))
if err != nil {
fmt.Fprintln(os.Stderr, "xeth set rx deadline", err)
break
}
n, noob, flags, addr, err :=
xeth.sock.ReadMsgUnix(rxbuf, rxoob)
_ = noob
_ = flags
_ = addr
if n == 0 || isTimeout(err) {
if rxto < maxrxto {
rxto *= 2
}
} else if err == nil {
rxto = minrxto
kind := KindOf(rxbuf[:n])
if err = kind.validate(rxbuf[:n]); err != nil {
fmt.Fprintln(os.Stderr, "xeth rx", err)
break
}
kind.cache(rxbuf[:n])
msg := Pool.Get(n)
copy(msg, rxbuf[:n])
xeth.rxch <- msg
} else {
e, ok := err.(*os.SyscallError)
if !ok || e.Err.Error() != "EOF" {
fmt.Fprintln(os.Stderr, "xeth rx", err)
}
break
}
}
}
func gotx() {
for msg := range xeth.txch {
tx(msg, 10*time.Millisecond)
Pool.Put(msg)
}
}
func tx(buf []byte, timeout time.Duration) error {
var oob []byte
var dl time.Time
if xeth.sock == nil {
return io.EOF
}
if timeout != time.Duration(0) {
dl = time.Now().Add(timeout)
}
err := xeth.sock.SetWriteDeadline(dl)
if err != nil {
return err
}
_, _, err = xeth.sock.WriteMsgUnix(buf, oob, nil)
return err
}