forked from boogie-byte/sflow-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlan_map.go
56 lines (44 loc) · 896 Bytes
/
vlan_map.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
package main
import (
"github.com/go-yaml/yaml"
"io/ioutil"
"sync/atomic"
"unsafe"
)
type vlanMap struct {
r map[uint32]bool
}
func newVlanMap() *vlanMap {
return &vlanMap{
r: make(map[uint32]bool),
}
}
func (m *vlanMap) Set(vid uint32, action bool) {
m.r[vid] = action
}
func (m *vlanMap) Get(vid uint32) bool {
if val, ok := m.r[vid]; ok {
return val
}
return true
}
var vlanMapPointer unsafe.Pointer
func vlanMapLookup(vid uint32) bool {
return (*vlanMap)(atomic.LoadPointer(&vlanMapPointer)).Get(vid)
}
func vlanMapReload() error {
data, err := ioutil.ReadFile(flagVlanPath)
if err != nil {
return err
}
fileMap := make(map[uint32]bool)
if err := yaml.Unmarshal(data, fileMap); err != nil {
return err
}
l := newVlanMap()
for vid, action := range fileMap {
l.Set(vid, action)
}
atomic.StorePointer(&vlanMapPointer, unsafe.Pointer(l))
return nil
}