forked from platinasystems/xeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifcache.go
286 lines (267 loc) · 7.01 KB
/
ifcache.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
/* 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 (
"bytes"
"fmt"
"net"
"sort"
)
type NoValue struct{}
type Associates map[int32]NoValue
type InterfaceEntry struct {
Ifinfo
EthtoolPrivFlags
EthtoolSettings
IPNets []*net.IPNet
Uppers Associates
Lowers Associates
}
type Ifcache struct {
indexes []int32
index map[int32]*InterfaceEntry
// only map XETH_DEVTYPE_XETH_PORT by name
dir map[string]*InterfaceEntry
}
var Interface Ifcache
func (c *Ifcache) Indexed(ifindex int32) *InterfaceEntry {
if entry, found := c.index[ifindex]; found {
return entry
}
if p, err := net.InterfaceByIndex(int(ifindex)); err == nil {
return c.cache(int32(p.Index), p)
}
return nil
}
// Call given function with each cached interface entry ceasing on error.
func (c *Ifcache) Iterate(f func(*InterfaceEntry) error) error {
sort.Slice(c.indexes, func(i, j int) bool {
return c.indexes[i] < c.indexes[j]
})
for _, ifindex := range c.indexes {
if err := f(c.index[ifindex]); err != nil {
return err
}
}
return nil
}
func (c *Ifcache) Named(name string) *InterfaceEntry {
return c.dir[name]
}
func (c *Ifcache) cache(ifindex int32, args ...interface{}) *InterfaceEntry {
entry, found := c.index[ifindex]
if !found {
entry = c.newEntry(ifindex)
}
entry.cache(args...)
return entry
}
func (c *Ifcache) del(ifindex int32) {
if entry := c.Indexed(ifindex); entry != nil {
if len(entry.IPNets) > 0 {
entry.IPNets = entry.IPNets[:0]
}
delete(c.index, ifindex)
delete(c.dir, entry.String())
for i := range c.indexes {
if c.indexes[i] == ifindex {
copy(c.indexes[i:], c.indexes[i+1:])
c.indexes = c.indexes[:len(c.indexes)-1]
break
}
}
}
}
func (c *Ifcache) newEntry(ifindex int32) *InterfaceEntry {
entry := new(InterfaceEntry)
entry.Index = ifindex
c.index[ifindex] = entry
c.indexes = append(c.indexes, ifindex)
return entry
}
func (entry *InterfaceEntry) String() string {
buf := new(bytes.Buffer)
fmt.Fprint(buf, entry.Ifinfo.Index, ": ", entry.Ifinfo.Name)
if entry.Ifinfo.Link > 0 {
fmt.Fprint(buf, "@", Interface.Indexed(entry.Link).Ifinfo.Name)
}
fmt.Fprint(buf, ":")
if entry.Ifinfo.Flags != 0 {
fmt.Fprint(buf, " <", entry.Ifinfo.Flags, ">")
}
fmt.Fprint(buf, " reason ", entry.Ifinfo.Reason)
if entry.Ifinfo.Id != 0 {
fmt.Fprint(buf, " id ", entry.Ifinfo.Id)
}
if entry.Ifinfo.Port >= 0 {
fmt.Fprint(buf, " port ", entry.Ifinfo.Port)
}
if entry.Ifinfo.Subport >= 0 {
fmt.Fprint(buf, " subport ", entry.Ifinfo.Subport)
}
if entry.Ifinfo.Netns != DefaultNetns {
fmt.Fprint(buf, " netns ", entry.Ifinfo.Netns)
}
fmt.Fprint(buf, "\n link/", entry.Ifinfo.DevType)
fmt.Fprint(buf, " ", entry.Ifinfo.HardwareAddr())
if entry.EthtoolPrivFlags != 0 {
fmt.Fprint(buf, " <", entry.EthtoolPrivFlags, ">")
}
if entry.EthtoolSettings.Speed != 0 {
fmt.Fprint(buf, " speed ", entry.EthtoolSettings.Speed)
fmt.Fprint(buf, " autoneg ", entry.EthtoolSettings.Autoneg)
}
if entry.Uppers.NotEmpty() {
fmt.Fprint(buf, " uppers [", entry.Uppers, "]")
}
if entry.Lowers.NotEmpty() {
fmt.Fprint(buf, " lowers [", entry.Lowers, "]")
}
for _, ipnet := range entry.IPNets {
fmt.Fprint(buf, "\n ")
if ipnet.IP.To4() != nil {
fmt.Fprint(buf, "inet ", ipnet)
} else {
fmt.Fprint(buf, "inet6 ", ipnet)
}
}
return buf.String()
}
func (entry *InterfaceEntry) cache(args ...interface{}) {
for _, v := range args {
switch t := v.(type) {
case string:
entry.dub(t)
case *net.Interface:
// don't override Index set by newEntry
entry.dub(t.Name)
entry.Link = -1
entry.Netns = DefaultNetns
copy(entry.addr[:], t.HardwareAddr)
entry.Flags = t.Flags
entry.DevType = XETH_DEVTYPE_LINUX_UNKNOWN
entry.Reason = XETH_IFINFO_REASON_NEW
entry.Id = 0
entry.Port = -1
entry.Subport = -1
case *MsgChangeUpper:
upper := Interface.Indexed(t.Upper)
if entry.Uppers == nil {
entry.Uppers = make(Associates)
}
if upper.Lowers == nil {
upper.Lowers = make(Associates)
}
if t.Linking > 0 {
entry.Uppers.Add(t.Upper)
upper.Lowers.Add(t.Lower)
} else {
entry.Uppers.Del(t.Upper)
entry.Uppers.Del(t.Lower)
}
case *MsgIfinfo:
entry.dub((*Ifname)(&t.Ifname).String())
entry.Link = t.Iflinkindex
entry.Netns = Netns(t.Net)
copy(entry.addr[:], t.Addr[:])
entry.Ifinfo.Flags = net.Flags(t.Flags)
entry.DevType = DevType(t.Devtype)
entry.Reason = IfinfoReason(t.Reason)
entry.Id = t.Id
entry.Port = t.Portindex
entry.Subport = t.Subportindex
case IfinfoReason:
entry.Reason = t
case net.HardwareAddr:
copy(entry.addr[:], t)
case DevType:
entry.DevType = t
case net.Flags:
entry.Flags = t
case Netns:
entry.Netns = t
case *MsgIfa:
switch t.Event {
case IFA_ADD:
entry.IPNets = append(entry.IPNets, t.IPNet())
case IFA_DEL:
ipnet := t.IPNet()
n := len(entry.IPNets)
for i, x := range entry.IPNets {
if x.IP.Equal(ipnet.IP) {
copy(entry.IPNets[i:],
entry.IPNets[i+1:])
entry.IPNets[n-1] = nil
entry.IPNets =
entry.IPNets[:n-1]
break
}
}
}
case *MsgEthtoolFlags:
entry.EthtoolPrivFlags.cache(t)
case EthtoolPrivFlags:
entry.EthtoolPrivFlags.cache(t)
case *MsgEthtoolSettings:
entry.EthtoolSettings.cache(t)
case Mbps:
entry.EthtoolSettings.cache(t)
case Duplex:
entry.EthtoolSettings.cache(t)
case DevPort:
entry.EthtoolSettings.cache(t)
case Autoneg:
entry.EthtoolSettings.cache(t)
}
}
}
func (entry *InterfaceEntry) dub(name string) {
if entry.Name == name {
return
}
if entry.DevType == XETH_DEVTYPE_XETH_PORT {
if len(entry.Name) > 0 {
delete(Interface.dir, entry.Name)
}
Interface.dir[name] = entry
}
entry.Name = name
}
func (associates Associates) NotEmpty() bool {
return associates != nil && len(associates) > 0
}
func (associates Associates) Add(ifindex int32) {
associates[ifindex] = NoValue{}
}
func (associates Associates) Del(ifindex int32) {
delete(associates, ifindex)
}
func (associates Associates) String() string {
buf := new(bytes.Buffer)
sep := ""
for ifindex := range associates {
fmt.Fprint(buf, sep, Interface.Indexed(ifindex).Ifinfo.Name)
sep = ", "
}
return buf.String()
}