-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_ethernet_device.go
210 lines (172 loc) · 4.56 KB
/
device_ethernet_device.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
package exu
import (
"errors"
log "github.com/sirupsen/logrus"
"math/rand"
"net"
"strconv"
"sync"
)
type EthernetDevice struct {
name string
ports []*VPort
portsMu sync.RWMutex
macAddressMap map[string]*VPort
macAddressMapMu sync.RWMutex
capabilities []Capability
onReceiveFn func(srcPort *VPort, data *EthernetFrame)
onConnectFn func(port *VPort)
onDisconnectFn func(port *VPort)
}
type EthernetReceiver interface {
OnReceive(srcPort *VPort, src net.HardwareAddr, dst net.HardwareAddr, data EthernetFrame)
}
func NewEthernetDevice(name string, numberOfPorts int, onReceive func(srcPort *VPort, data *EthernetFrame), onConnect func(port *VPort), onDisconnect func(port *VPort)) *EthernetDevice {
dev := new(EthernetDevice)
*dev = EthernetDevice{
name: name,
ports: make([]*VPort, numberOfPorts),
portsMu: sync.RWMutex{},
macAddressMap: make(map[string]*VPort),
macAddressMapMu: sync.RWMutex{},
capabilities: make([]Capability, 0),
onReceiveFn: onReceive,
onConnectFn: onConnect,
onDisconnectFn: onDisconnect,
}
for i := 0; i < numberOfPorts; i++ {
mac := make(net.HardwareAddr, 6)
mac[0] = 0x42
mac[1] = 0x69
r := rand.Uint32()
mac[2] = byte(r >> 24)
mac[3] = byte(r >> 16)
mac[4] = byte(r >> 8)
mac[5] = byte(r)
dev.ports[i] = NewVPort(mac, "eth0/"+strconv.Itoa(i))
func(i int) {
dev.ports[i].SetOnReceive(func(data *EthernetFrame) {
// check if we already have the source MAC address in our map
dev.macAddressMapMu.RLock()
_, ok := dev.macAddressMap[data.Source().String()]
dev.macAddressMapMu.RUnlock()
// if we don't have the source MAC address in our map, add it
if !ok && data.Source().String() != "ff:ff:ff:ff:ff:ff" {
dev.macAddressMapMu.Lock()
dev.macAddressMap[data.Source().String()] = dev.ports[i]
dev.macAddressMapMu.Unlock()
log.WithField("mac", data.Source().String()).
WithField("port", dev.ports[i].portCname).
WithField("device", dev.name).
Trace("learned new mac address")
}
// check all capabilities
for _, capability := range dev.capabilities {
if capability.Match(dev.ports[i], data) {
res := capability.HandleRequest(dev.ports[i], data)
switch res {
case CapabilityStatusDone:
goto done
case CapabilityStatusFail:
goto done
case CapabilityStatusPass:
continue
}
}
}
dev.onReceiveFn(dev.ports[i], data)
done:
})
}(i)
}
return dev
}
func (e *EthernetDevice) WriteFromPort(port *VPort, data *EthernetFrame) error {
e.portsMu.RLock()
defer e.portsMu.RUnlock()
found := false
for _, p := range e.ports {
if p == port {
found = true
break
}
}
if !found {
return errors.New("port not found")
}
for i := 6; i < 12; i++ {
(*data)[i] = port.mac[i-6]
}
return port.Write(data)
}
func (e *EthernetDevice) GetFirstFreePort() *VPort {
e.portsMu.Lock()
defer e.portsMu.Unlock()
for _, port := range e.ports {
if port.connectedTo == nil {
return port
}
}
return nil
}
func (e *EthernetDevice) connectPorts(portOnMachine int, target *VPort) {
log.WithField("device", e.name).
WithField("port", target.mac.String()).
Info("connected port")
e.ports[portOnMachine].connectedTo = target
target.connectedTo = e.ports[portOnMachine]
e.onConnectFn(e.ports[portOnMachine])
}
func (e *EthernetDevice) ConnectToFirstAvailablePort(target *VPort) error {
e.portsMu.Lock()
defer e.portsMu.Unlock()
for i, port := range e.ports {
if port.connectedTo == nil {
e.connectPorts(i, target)
return nil
}
}
return errors.New("no available ports")
}
func (e *EthernetDevice) ConnectPorts(portOnMachine, target *VPort) error {
e.portsMu.Lock()
defer e.portsMu.Unlock()
index := -1
for i, port := range e.ports {
if port == portOnMachine {
index = i
break
}
}
if index == -1 {
return errors.New("port not found on machine")
}
if e.ports[index].connectedTo != nil {
return errors.New("port already connected")
}
e.connectPorts(index, target)
return nil
}
func (e *EthernetDevice) DisconnectPort(target *VPort) {
log.WithField("device", e.name).
WithField("port", target.mac.String()).
Info("disconnecting port")
e.portsMu.Lock()
defer e.portsMu.Unlock()
for _, port := range e.ports {
if port.connectedTo == target {
port.connectedTo = nil
port.onReceive = nil
e.onDisconnectFn(port)
break
}
}
e.macAddressMapMu.Lock()
defer e.macAddressMapMu.Unlock()
for mac, port := range e.macAddressMap {
if port == target {
delete(e.macAddressMap, mac)
break
}
}
}