forked from platinasystems/xeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kind.go
194 lines (176 loc) · 4.76 KB
/
kind.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
/* 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"
"net"
"unsafe"
)
const (
XETH_MSG_KIND_BREAK = iota
XETH_MSG_KIND_LINK_STAT
XETH_MSG_KIND_ETHTOOL_STAT
XETH_MSG_KIND_ETHTOOL_FLAGS
XETH_MSG_KIND_ETHTOOL_SETTINGS
XETH_MSG_KIND_DUMP_IFINFO
XETH_MSG_KIND_CARRIER
XETH_MSG_KIND_SPEED
XETH_MSG_KIND_IFINFO
XETH_MSG_KIND_IFA
XETH_MSG_KIND_DUMP_FIBINFO
XETH_MSG_KIND_FIBENTRY
XETH_MSG_KIND_IFDEL
XETH_MSG_KIND_NEIGH_UPDATE
XETH_MSG_KIND_IFVID
XETH_MSG_KIND_CHANGE_UPPER
)
const XETH_MSG_KIND_NOT_MSG = 0xff
type Kind int
func ToMsg(buf []byte) *Msg {
return (*Msg)(unsafe.Pointer(&buf[0]))
}
func KindOf(buf []byte) Kind {
var kind Kind = XETH_MSG_KIND_NOT_MSG
msg := ToMsg(buf)
if len(buf) >= SizeofMsg &&
msg.Z64 == 0 &&
msg.Z32 == 0 &&
msg.Z16 == 0 &&
msg.Z8 == 0 {
kind = Kind(msg.Kind)
}
return kind
}
func (kind Kind) String() string {
var kinds = []string{
"break",
"link-stat",
"ethtool-stat",
"ethtool-flags",
"ethtool-settings",
"dump-ifinfo",
"carrier",
"speed",
"ifinfo",
"ifa",
"dump-fibinfo",
"fib-entry",
"ifdel",
"neigh-update",
"ifvid",
"change-upper",
}
i := int(kind)
if kind == XETH_MSG_KIND_NOT_MSG {
return "not-message"
} else if i < len(kinds) {
return kinds[i]
}
return fmt.Sprint("@", i)
}
func (kind Kind) cache(buf []byte) {
switch kind {
case XETH_MSG_KIND_CHANGE_UPPER:
msg := ToMsgChangeUpper(buf)
Interface.cache(msg.Lower, msg)
case XETH_MSG_KIND_IFA:
msg := ToMsgIfa(buf)
Interface.cache(msg.Ifindex, msg)
case XETH_MSG_KIND_IFINFO:
msg := ToMsgIfinfo(buf)
switch msg.Reason {
case XETH_IFINFO_REASON_NEW:
Interface.cache(msg.Ifindex, msg)
case XETH_IFINFO_REASON_DEL:
Interface.del(msg.Ifindex)
case XETH_IFINFO_REASON_UP:
Interface.cache(msg.Ifindex, net.Flags(msg.Flags))
case XETH_IFINFO_REASON_DOWN:
Interface.cache(msg.Ifindex, net.Flags(msg.Flags))
case XETH_IFINFO_REASON_DUMP:
Interface.cache(msg.Ifindex, msg)
case XETH_IFINFO_REASON_REG:
entry, found := Interface.index[msg.Ifindex]
if found {
entry.Netns = Netns(msg.Net)
} else {
Interface.cache(msg.Ifindex, msg)
}
case XETH_IFINFO_REASON_UNREG:
entry, found := Interface.index[msg.Ifindex]
if found {
entry.Netns = DefaultNetns
}
}
case XETH_MSG_KIND_ETHTOOL_FLAGS:
msg := ToMsgEthtoolFlags(buf)
Interface.cache(msg.Ifindex, msg)
case XETH_MSG_KIND_ETHTOOL_SETTINGS:
msg := ToMsgEthtoolSettings(buf)
Interface.cache(msg.Ifindex, msg)
}
}
func (kind Kind) validate(buf []byte) error {
if kind == XETH_MSG_KIND_NOT_MSG {
return fmt.Errorf("corrupt message")
}
n, found := map[Kind]int{
XETH_MSG_KIND_CHANGE_UPPER: SizeofMsgChangeUpper,
XETH_MSG_KIND_ETHTOOL_FLAGS: SizeofMsgEthtoolFlags,
XETH_MSG_KIND_ETHTOOL_SETTINGS: SizeofMsgEthtoolSettings,
XETH_MSG_KIND_IFA: SizeofMsgIfa,
XETH_MSG_KIND_IFINFO: SizeofMsgIfinfo,
XETH_MSG_KIND_NEIGH_UPDATE: SizeofMsgNeighUpdate,
}[kind]
if found && n != len(buf) {
return fmt.Errorf("mismatched %s", kind)
}
return nil
}
func ToMsgCarrier(buf []byte) *MsgCarrier {
return (*MsgCarrier)(unsafe.Pointer(&buf[0]))
}
func ToMsgChangeUpper(buf []byte) *MsgChangeUpper {
return (*MsgChangeUpper)(unsafe.Pointer(&buf[0]))
}
func ToMsgEthtoolFlags(buf []byte) *MsgEthtoolFlags {
return (*MsgEthtoolFlags)(unsafe.Pointer(&buf[0]))
}
func ToMsgEthtoolSettings(buf []byte) *MsgEthtoolSettings {
return (*MsgEthtoolSettings)(unsafe.Pointer(&buf[0]))
}
func ToMsgIfa(buf []byte) *MsgIfa {
return (*MsgIfa)(unsafe.Pointer(&buf[0]))
}
func ToMsgIfinfo(buf []byte) *MsgIfinfo {
return (*MsgIfinfo)(unsafe.Pointer(&buf[0]))
}
func ToMsgNeighUpdate(buf []byte) *MsgNeighUpdate {
return (*MsgNeighUpdate)(unsafe.Pointer(&buf[0]))
}
func ToMsgSpeed(buf []byte) *MsgSpeed {
return (*MsgSpeed)(unsafe.Pointer(&buf[0]))
}
func ToMsgStat(buf []byte) *MsgStat {
return (*MsgStat)(unsafe.Pointer(&buf[0]))
}