-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
bridge.go
226 lines (192 loc) · 5.85 KB
/
bridge.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
// Copyright 2024 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stack
import (
"math/rand"
"net"
"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
var _ NetworkLinkEndpoint = (*BridgeEndpoint)(nil)
type bridgePort struct {
bridge *BridgeEndpoint
nic *nic
}
// ParseHeader implements stack.LinkEndpoint.
func (p *bridgePort) ParseHeader(pkt *PacketBuffer) bool {
_, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
return ok
}
// DeliverNetworkPacket implements stack.NetworkDispatcher.
func (p *bridgePort) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) {
bridge := p.bridge
bridge.mu.Lock()
defer bridge.mu.Unlock()
// Send the packet to all other ports.
for _, port := range bridge.ports {
if p == port {
continue
}
newPkt := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: int(port.nic.MaxHeaderLength()),
Payload: pkt.ToBuffer(),
})
port.nic.writeRawPacket(newPkt)
newPkt.DecRef()
}
bridge.injectInboundLocked(protocol, pkt)
}
func (p *bridgePort) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) {
}
func getRandMacAddr() tcpip.LinkAddress {
mac := make(net.HardwareAddr, 6)
rand.Read(mac) // Fill with random data.
mac[0] &^= 0x1 // Clear multicast bit.
mac[0] |= 0x2 // Set local assignment bit (IEEE802).
return tcpip.LinkAddress(mac)
}
// NewBridgeEndpoint creates a new bridge endpoint.
func NewBridgeEndpoint(mtu uint32) *BridgeEndpoint {
b := &BridgeEndpoint{
mtu: mtu,
addr: getRandMacAddr(),
}
b.ports = make(map[tcpip.NICID]*bridgePort)
return b
}
// BridgeEndpoint is a bridge endpoint.
type BridgeEndpoint struct {
mu bridgeRWMutex
// +checklocks:mu
ports map[tcpip.NICID]*bridgePort
// +checklocks:mu
dispatcher NetworkDispatcher
// +checklocks:mu
addr tcpip.LinkAddress
// +checklocks:mu
attached bool
mtu uint32
maxHeaderLength atomicbitops.Uint32
}
// WritePackets implements stack.LinkEndpoint.WritePackets.
func (b *BridgeEndpoint) WritePackets(pkts PacketBufferList) (int, tcpip.Error) {
b.mu.RLock()
defer b.mu.RUnlock()
pktsSlice := pkts.AsSlice()
n := len(pktsSlice)
for _, p := range b.ports {
for _, pkt := range pktsSlice {
// In order to properly loop back to the inbound side we must create a
// fresh packet that only contains the underlying payload with no headers
// or struct fields set.
newPkt := NewPacketBuffer(PacketBufferOptions{
Payload: pkt.ToBuffer(),
ReserveHeaderBytes: int(p.nic.MaxHeaderLength()),
})
newPkt.EgressRoute = pkt.EgressRoute
newPkt.NetworkProtocolNumber = pkt.NetworkProtocolNumber
p.nic.writePacket(newPkt)
newPkt.DecRef()
}
}
return n, nil
}
// AddNIC adds the specified NIC to the bridge.
func (b *BridgeEndpoint) AddNIC(n *nic) tcpip.Error {
b.mu.Lock()
defer b.mu.Unlock()
port := &bridgePort{
nic: n,
bridge: b,
}
n.NetworkLinkEndpoint.Attach(port)
b.ports[n.id] = port
if b.maxHeaderLength.Load() < uint32(n.MaxHeaderLength()) {
b.maxHeaderLength.Store(uint32(n.MaxHeaderLength()))
}
return nil
}
// DelNIC remove the specified NIC from the bridge.
func (b *BridgeEndpoint) DelNIC(nic *nic) tcpip.Error {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.ports, nic.id)
nic.NetworkLinkEndpoint.Attach(nic)
return nil
}
// +checklocks:b.mu
func (b *BridgeEndpoint) injectInboundLocked(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) {
d := b.dispatcher
if d != nil {
d.DeliverNetworkPacket(protocol, pkt)
}
}
// MTU implements stack.LinkEndpoint.MTU.
func (b *BridgeEndpoint) MTU() uint32 {
if b.mtu > header.EthernetMinimumSize {
return b.mtu - header.EthernetMinimumSize
}
return 0
}
// MaxHeaderLength implements stack.LinkEndpoint.
func (b *BridgeEndpoint) MaxHeaderLength() uint16 {
return uint16(b.maxHeaderLength.Load())
}
// LinkAddress implements stack.LinkEndpoint.LinkAddress.
func (b *BridgeEndpoint) LinkAddress() tcpip.LinkAddress {
b.mu.Lock()
defer b.mu.Unlock()
return b.addr
}
// SetLinkAddress implements stack.LinkEndpoint.SetLinkAddress.
func (b *BridgeEndpoint) SetLinkAddress(addr tcpip.LinkAddress) {
b.mu.Lock()
defer b.mu.Unlock()
b.addr = addr
}
// Capabilities implements stack.LinkEndpoint.Capabilities.
func (b *BridgeEndpoint) Capabilities() LinkEndpointCapabilities {
return CapabilityRXChecksumOffload | CapabilitySaveRestore | CapabilityResolutionRequired
}
// Attach implements stack.LinkEndpoint.Attach.
func (b *BridgeEndpoint) Attach(dispatcher NetworkDispatcher) {
b.mu.Lock()
defer b.mu.Unlock()
for _, p := range b.ports {
p.nic.Primary = nil
}
b.dispatcher = dispatcher
b.ports = make(map[tcpip.NICID]*bridgePort)
}
// IsAttached implements stack.LinkEndpoint.IsAttached.
func (b *BridgeEndpoint) IsAttached() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return b.dispatcher != nil
}
// Wait implements stack.LinkEndpoint.Wait.
func (b *BridgeEndpoint) Wait() {
}
// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType.
func (b *BridgeEndpoint) ARPHardwareType() header.ARPHardwareType {
return header.ARPHardwareEther
}
// AddHeader implements stack.LinkEndpoint.AddHeader.
func (b *BridgeEndpoint) AddHeader(pkt *PacketBuffer) {
}
// ParseHeader implements stack.LinkEndpoint.ParseHeader.
func (b *BridgeEndpoint) ParseHeader(*PacketBuffer) bool {
return true
}