-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_test.go
342 lines (272 loc) · 8.64 KB
/
peer_test.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package metalbond
import (
"fmt"
"math/rand"
"net"
"net/netip"
"sync"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
)
var _ = Describe("Peer", func() {
var (
mbServer *MetalBond
serverAddress string
client *DummyClient
)
BeforeEach(func() {
log.Info("----- START -----")
config := Config{}
client = NewDummyClient()
mbServer = NewMetalBond(config, client)
serverAddress = fmt.Sprintf("127.0.0.1:%d", getRandomTCPPort())
err := mbServer.StartServer(serverAddress)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
mbServer.Shutdown()
})
It("should subscribe", func() {
mbClient := NewMetalBond(Config{}, client)
localIP := net.ParseIP("127.0.0.2")
err := mbClient.AddPeer(serverAddress, localIP.String())
Expect(err).NotTo(HaveOccurred())
time.Sleep(5 * time.Second)
vni := VNI(200)
err = mbClient.Subscribe(vni)
if err != nil {
log.Errorf("subscribe failed: %v", err)
}
Expect(err).NotTo(HaveOccurred())
vnis := mbClient.GetSubscribedVnis()
Expect(len(vnis)).To(Equal(1))
Expect(vnis[0]).To(Equal(vni))
err = mbClient.Unsubscribe(vni)
Expect(err).NotTo(HaveOccurred())
vnis = mbClient.GetSubscribedVnis()
Expect(len(vnis)).To(Equal(0))
err = mbClient.RemovePeer(serverAddress)
Expect(err).NotTo(HaveOccurred())
mbClient.Shutdown()
})
It("should reset", func() {
mbClient := NewMetalBond(Config{}, client)
err := mbClient.AddPeer(serverAddress, "127.0.0.2")
Expect(err).NotTo(HaveOccurred())
clientAddr := getLocalAddr(mbClient, "")
Expect(clientAddr).NotTo(Equal(""))
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
var p *metalBondPeer
for _, peer := range mbServer.peers {
p = peer
break
}
// Reset the peer a few times
p.Reset()
p.Reset()
p.Reset()
// expect the peer state to be closed
Expect(p.GetState()).To(Equal(CLOSED))
clientAddr = getLocalAddr(mbClient, clientAddr)
Expect(clientAddr).NotTo(Equal(""))
// wait for the peer to be established again
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
})
It("should reconnect", func() {
mbClient := NewMetalBond(Config{}, client)
err := mbClient.AddPeer(serverAddress, "127.0.0.2")
Expect(err).NotTo(HaveOccurred())
clientAddr := getLocalAddr(mbClient, "")
Expect(clientAddr).NotTo(Equal(""))
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
var p *metalBondPeer
for _, peer := range mbServer.peers {
p = peer
break
}
// Close the peer
p.Close()
// expect the peer state to be closed
Expect(p.GetState()).To(Equal(CLOSED))
clientAddr = getLocalAddr(mbClient, clientAddr)
Expect(clientAddr).NotTo(Equal(""))
// wait for the peer to be established again
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
})
It("client timeout", func() {
mbClient := NewMetalBond(Config{}, client)
err := mbClient.AddPeer(serverAddress, "127.0.0.2")
Expect(err).NotTo(HaveOccurred())
clientAddr := getLocalAddr(mbClient, "")
Expect(clientAddr).NotTo(Equal(""))
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
vni := VNI(200)
err = mbClient.Subscribe(vni)
Expect(err).NotTo(HaveOccurred())
var p *metalBondPeer
for _, peer := range mbClient.peers {
p = peer
break
}
err = mbClient.Unsubscribe(vni)
Expect(err).NotTo(HaveOccurred())
// Close the keepalive
p.keepaliveStop <- true
time.Sleep(12 * time.Second)
// expect the peer state to be closed
Expect(p.GetState()).To(Equal(RETRY))
err = mbClient.RemovePeer(serverAddress)
Expect(err).NotTo(HaveOccurred())
})
It("should announce", func() {
totalClients := 600 // TODO: was 1000 (local test works for this large value), but it is reduced to this value to make CI/CD happy
var wg sync.WaitGroup
for i := 1; i < totalClients+1; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
mbClient := NewMetalBond(Config{}, client)
localIP := net.ParseIP("127.0.0.2")
localIP = incrementIPv4(localIP, index)
err := mbClient.AddPeer(serverAddress, localIP.String())
Expect(err).NotTo(HaveOccurred())
// wait for the peer loop to start
time.Sleep(1 * time.Second)
clientAddr := getLocalAddr(mbClient, "")
Expect(clientAddr).NotTo(Equal(""))
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
mbServer.mtxPeers.RLock()
p := mbServer.peers[clientAddr]
mbServer.mtxPeers.RUnlock()
Expect(waitForPeerState(mbClient, serverAddress, ESTABLISHED)).NotTo(BeFalse())
vni := VNI(index % 10)
err = mbClient.Subscribe(vni)
if err != nil {
log.Errorf("subscribe failed: %v", err)
}
Expect(err).NotTo(HaveOccurred())
// prepare the route
startIP := net.ParseIP("100.64.0.0")
ip := incrementIPv4(startIP, index)
addr, err := netip.ParseAddr(ip.String())
Expect(err).NotTo(HaveOccurred())
underlayRoute, err := netip.ParseAddr(fmt.Sprintf("b198:5b10:3880:fd32:fb80:80dd:46f7:%d", index))
Expect(err).NotTo(HaveOccurred())
dest := Destination{
Prefix: netip.PrefixFrom(addr, 32),
IPVersion: IPV4,
}
nextHop := NextHop{
TargetVNI: uint32(vni),
TargetAddress: underlayRoute,
}
err = mbClient.AnnounceRoute(vni, dest, nextHop)
Expect(err).NotTo(HaveOccurred())
// wait for the route to be received
time.Sleep(3 * time.Second)
// check if the route was received
_, exists := p.receivedRoutes.routes[vni][dest][nextHop][p]
Expect(exists).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
// Close the peer
err = p.metalbond.RemovePeer(p.remoteAddr)
Expect(err).NotTo(HaveOccurred())
// expect the peer state to be closed
Expect(p.GetState()).To(Equal(CLOSED))
// wait for the peer to be established again
wait := rand.Intn(20) + 1
time.Sleep(time.Duration(wait) * time.Second)
notExcept := clientAddr
clientAddr = getLocalAddr(mbClient, notExcept)
if clientAddr == "" {
log.Errorf("clientAddr is empty '%s'", clientAddr)
}
Expect(clientAddr).ShouldNot(BeEmpty())
// check if the peer is established again
Expect(waitForPeerState(mbServer, clientAddr, ESTABLISHED)).NotTo(BeFalse())
mbServer.mtxPeers.RLock()
p = mbServer.peers[clientAddr]
mbServer.mtxPeers.RUnlock()
// wait for the route to be received
time.Sleep(3 * time.Second)
// check if the route was received
_, exists = p.receivedRoutes.routes[vni][dest][nextHop][p]
if !exists {
log.Errorf("route not received vni %v, dest %v, nextHop %v, clientAddr %s", vni, dest, nextHop, clientAddr)
for vni, dest := range p.receivedRoutes.routes {
log.Errorf("vni %v", vni)
for dest, nextHop := range dest {
log.Errorf("dest %v", dest)
for nextHop, peers := range nextHop {
log.Errorf("nextHop %v", nextHop)
for peer := range peers {
log.Errorf("peer %v", peer)
}
}
}
}
}
Expect(exists).To(BeTrue())
}(i)
}
wg.Wait()
})
})
func waitForPeerState(mbServer *MetalBond, clientAddr string, expectedState ConnectionState) bool {
// Call the checkPeerState function repeatedly until it returns true or a timeout is reached
timeout := 30 * time.Second
start := time.Now()
for {
mbServer.mtxPeers.RLock()
peer := mbServer.peers[clientAddr]
mbServer.mtxPeers.RUnlock()
if peer != nil && peer.GetState() == expectedState {
return true
}
if time.Since(start) >= timeout {
state := "NONE"
if peer != nil {
state = peer.GetState().String()
}
log.Errorf("Timeout reached while waiting for peer (%s) to reach expected state %s, but state is %s", clientAddr, expectedState, state)
return false
}
// Wait a short time before checking again
time.Sleep(500 * time.Millisecond)
}
}
func getLocalAddr(mbClient *MetalBond, notExcept string) string {
timeout := 30 * time.Second
start := time.Now()
for {
for _, peer := range mbClient.peers {
if peer.localAddr != "" && peer.localAddr != notExcept {
return peer.localAddr
}
}
if time.Since(start) >= timeout {
return ""
}
// Wait a short time before checking again
time.Sleep(500 * time.Millisecond)
}
}
func incrementIPv4(ip net.IP, count int) net.IP {
// Increment the IP address by the count
for i := len(ip) - 1; i >= 0; i-- {
octet := int(ip[i]) + (count % 256)
count /= 256
if octet > 255 {
octet = 255
}
ip[i] = byte(octet)
if count == 0 {
break
}
}
return ip
}