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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
#!/usr/bin/env python
"""L2 FIB Test Case HLD:
**config 1**
- add 4 pg-l2 interfaces
- configure them into l2bd
- configure 100 MAC entries in L2 fib - 25 MACs per interface
- L2 MAC learning and unknown unicast flooding disabled in l2bd
- configure 100 MAC entries in L2 fib - 25 MACs per interface
**test 1**
- send L2 MAC frames between all 4 pg-l2 interfaces for all of 100 MAC \
entries in the FIB
**verify 1**
- all packets received correctly
**config 2**
- delete 12 MAC entries - 3 MACs per interface
**test 2a**
- send L2 MAC frames between all 4 pg-l2 interfaces for non-deleted MAC \
entries
**verify 2a**
- all packets received correctly
**test 2b**
- send L2 MAC frames between all 4 pg-l2 interfaces for all of 12 deleted \
MAC entries
**verify 2b**
- no packet received on all 4 pg-l2 interfaces
**config 3**
- configure new 100 MAC entries in L2 fib - 25 MACs per interface
**test 3**
- send L2 MAC frames between all 4 pg-l2 interfaces for all of 188 MAC \
entries in the FIB
**verify 3**
- all packets received correctly
**config 4**
- delete 160 MAC entries, 40 MACs per interface
**test 4a**
- send L2 MAC frames between all 4 pg-l2 interfaces for all of 28 \
non-deleted MAC entries
**verify 4a**
- all packets received correctly
**test 4b**
- try send L2 MAC frames between all 4 pg-l2 interfaces for all of 172 \
deleted MAC entries
**verify 4b**
- no packet received on all 4 pg-l2 interfaces
"""
import unittest
import random
from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP
from framework import VppTestCase, VppTestRunner
from util import Host, ppp
class TestL2fib(VppTestCase):
""" L2 FIB Test Case """
@classmethod
def bd_ifs(cls, bd_id):
return range((bd_id - 1) * cls.n_ifs_per_bd, bd_id * cls.n_ifs_per_bd)
@classmethod
def setUpClass(cls):
"""
Perform standard class setup (defined by class method setUpClass in
class VppTestCase) before running the test case, set test case related
variables and configure VPP.
:var int bd_id: Bridge domain ID.
"""
super(TestL2fib, cls).setUpClass()
try:
n_brs = cls.n_brs = range(1, 3)
cls.n_ifs_per_bd = 4
n_ifs = range(cls.n_ifs_per_bd * len(cls.n_brs))
# Create 4 pg interfaces
cls.create_pg_interfaces(n_ifs)
cls.flows = dict()
for bd_id in n_brs:
# Packet flows mapping pg0 -> pg1, pg2, pg3 etc.
ifs = cls.bd_ifs(bd_id)
for j in ifs:
cls.flows[cls.pg_interfaces[j]] = [
cls.pg_interfaces[x] for x in ifs if x != j]
# Packet sizes
cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
for bd_id in n_brs:
# Create BD with MAC learning and unknown unicast flooding
# disabled and put interfaces to this BD
cls.vapi.bridge_domain_add_del(
bd_id=bd_id, uu_flood=0, learn=0)
ifs = [cls.pg_interfaces[i] for i in cls.bd_ifs(bd_id)]
for pg_if in ifs:
cls.vapi.sw_interface_set_l2_bridge(pg_if.sw_if_index,
bd_id=bd_id)
# Set up all interfaces
for i in cls.pg_interfaces:
i.admin_up()
# Mapping between packet-generator index and lists of test hosts
cls.hosts = dict()
cls.learned_hosts = dict()
cls.fib_hosts = dict()
cls.deleted_hosts = dict()
for pg_if in cls.pg_interfaces:
swif = pg_if.sw_if_index
cls.hosts[swif] = []
cls.learned_hosts[swif] = []
cls.fib_hosts[swif] = []
cls.deleted_hosts[swif] = []
except Exception:
super(TestL2fib, cls).tearDownClass()
raise
def setUp(self):
super(TestL2fib, self).setUp()
self.reset_packet_infos()
def tearDown(self):
"""
Show various debug prints after each test.
"""
super(TestL2fib, self).tearDown()
if not self.vpp_dead:
self.logger.info(self.vapi.ppcli("show l2fib verbose"))
for bd_id in self.n_brs:
self.logger.info(self.vapi.ppcli("show bridge-domain %s detail"
% bd_id))
def create_hosts(self, n_hosts_per_if, subnet):
"""
Create required number of host MAC addresses and distribute them among
interfaces. Create host IPv4 address for every host MAC address.
:param int n_hosts_per_if: Number of per interface hosts to
create MAC/IPv4 addresses for.
"""
for pg_if in self.pg_interfaces:
swif = pg_if.sw_if_index
def mac(j): return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
def ip(j): return "172.%02u.1%02x.%u" % (subnet, swif, j)
def h(j): return Host(mac(j), ip(j))
self.hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
def learn_hosts(self, bd_id, n_hosts_per_if):
"""
Create L2 MAC packet stream with host MAC addresses per interface to
let the bridge domain learn these MAC addresses.
:param int bd_id: BD to teach
:param int n_hosts_per_if: number of hosts
"""
self.vapi.bridge_flags(bd_id, 1, 1)
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for pg_if in ifs:
swif = pg_if.sw_if_index
hosts = self.hosts[swif]
lhosts = self.learned_hosts[swif]
packets = []
for j in range(n_hosts_per_if):
host = hosts.pop()
lhosts.append(host)
packet = (Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac))
packets.append(packet)
pg_if.add_stream(packets)
self.logger.info("Sending broadcast eth frames for MAC learning")
self.pg_start()
def config_l2_fib_entries(self, bd_id, n_hosts_per_if):
"""
Config required number of L2 FIB entries.
:param int bd_id: BD's id
:param int count: Number of L2 FIB entries to be created.
:param int start: Starting index of the host list. (Default value = 0)
"""
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for pg_if in ifs:
swif = pg_if.sw_if_index
hosts = self.hosts[swif]
fhosts = self.fib_hosts[swif]
for j in range(n_hosts_per_if):
host = hosts.pop()
self.vapi.l2fib_add_del(
host.mac, bd_id, swif, static_mac=1)
fhosts.append(host)
# del hosts[0]
self.logger.info("Configure %d L2 FIB entries .." %
len(self.pg_interfaces) * n_hosts_per_if)
self.logger.info(self.vapi.ppcli("show l2fib"))
def delete_l2_fib_entry(self, bd_id, n_hosts_per_if):
"""
Delete required number of L2 FIB entries.
:param int count: Number of L2 FIB entries to be created.
"""
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for pg_if in ifs:
swif = pg_if.sw_if_index
hosts = self.fib_hosts[swif]
dhosts = self.deleted_hosts[swif]
for j in range(n_hosts_per_if):
host = hosts.pop()
self.vapi.l2fib_add_del(
host.mac, bd_id, swif, is_add=0)
dhosts.append(host)
self.logger.info(self.vapi.ppcli("show l2fib"))
def flush_int(self, swif):
"""
Flush swif L2 FIB entries.
:param int swif: sw if index.
"""
flushed = dict()
self.vapi.l2fib_flush_int(swif)
self.deleted_hosts[swif] = self.learned_hosts[swif] + \
self.deleted_hosts[swif]
flushed[swif] = self.learned_hosts[swif]
self.learned_hosts[swif] = []
self.logger.info(self.vapi.ppcli("show l2fib"))
return flushed
def flush_bd(self, bd_id):
"""
Flush bd_id L2 FIB entries.
:param int bd_id: Bridge Domain id.
"""
self.vapi.l2fib_flush_bd(bd_id)
flushed = dict()
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for pg_if in ifs:
swif = pg_if.sw_if_index
self.deleted_hosts[swif] = self.learned_hosts[swif] + \
self.deleted_hosts[swif]
flushed[swif] = self.learned_hosts[swif]
self.learned_hosts[swif] = []
self.logger.info(self.vapi.ppcli("show l2fib"))
return flushed
def flush_all(self):
"""
Flush All L2 FIB entries.
"""
self.vapi.l2fib_flush_all()
flushed = dict()
for pg_if in self.pg_interfaces:
swif = pg_if.sw_if_index
self.deleted_hosts[swif] = self.learned_hosts[swif] + \
self.deleted_hosts[swif]
flushed[swif] = self.learned_hosts[swif]
self.learned_hosts[swif] = []
self.logger.info(self.vapi.ppcli("show l2fib"))
return flushed
def create_stream(self, src_if, packet_sizes, if_src_hosts=None,
if_dst_hosts=None):
"""
Create input packet stream for defined interface using hosts or
deleted_hosts list.
:param object src_if: Interface to create packet stream for.
:param list packet_sizes: List of required packet sizes.
:param boolean deleted: Set to True if deleted_hosts list required.
:return: Stream of packets.
"""
if not if_src_hosts:
if_src_hosts = self.fib_hosts
if not if_dst_hosts:
if_dst_hosts = self.fib_hosts
src_hosts = if_src_hosts[src_if.sw_if_index]
if not src_hosts:
return []
pkts = []
for dst_if in self.flows[src_if]:
dst_swif = dst_if.sw_if_index
if dst_swif not in if_dst_hosts:
continue
dst_hosts = if_dst_hosts[dst_swif]
for i in range(0, len(dst_hosts)):
dst_host = dst_hosts[i]
src_host = random.choice(src_hosts)
pkt_info = self.create_packet_info(src_if, dst_if)
payload = self.info_to_payload(pkt_info)
p = (Ether(dst=dst_host.mac, src=src_host.mac) /
IP(src=src_host.ip4, dst=dst_host.ip4) /
UDP(sport=1234, dport=1234) /
Raw(payload))
pkt_info.data = p.copy()
size = random.choice(packet_sizes)
self.extend_packet(p, size)
pkts.append(p)
return pkts
def verify_capture(self, pg_if, capture):
"""
Verify captured input packet stream for defined interface.
:param object pg_if: Interface to verify captured packet stream for.
:param list capture: Captured packet stream.
"""
last_info = dict()
for i in self.pg_interfaces:
last_info[i.sw_if_index] = None
dst_sw_if_index = pg_if.sw_if_index
for packet in capture:
payload_info = self.payload_to_info(str(packet[Raw]))
try:
ip = packet[IP]
udp = packet[UDP]
packet_index = payload_info.index
self.assertEqual(payload_info.dst, dst_sw_if_index)
self.logger.debug("Got packet on port %s: src=%u (id=%u)" %
(pg_if.name, payload_info.src, packet_index))
next_info = self.get_next_packet_info_for_interface2(
payload_info.src, dst_sw_if_index,
last_info[payload_info.src])
last_info[payload_info.src] = next_info
self.assertTrue(next_info is not None)
self.assertEqual(packet_index, next_info.index)
saved_packet = next_info.data
# Check standard fields
self.assertEqual(ip.src, saved_packet[IP].src)
self.assertEqual(ip.dst, saved_packet[IP].dst)
self.assertEqual(udp.sport, saved_packet[UDP].sport)
self.assertEqual(udp.dport, saved_packet[UDP].dport)
except:
self.logger.error(ppp("Unexpected or invalid packet:", packet))
raise
for i in self.pg_interfaces:
remaining_packet = self.get_next_packet_info_for_interface2(
i, dst_sw_if_index, last_info[i.sw_if_index])
self.assertTrue(
remaining_packet is None,
"Port %u: Packet expected from source %u didn't arrive" %
(dst_sw_if_index, i.sw_if_index))
def run_verify_test(self, bd_id, dst_hosts=None):
# Test
# Create incoming packet streams for packet-generator interfaces
if not dst_hosts:
dst_hosts = self.fib_hosts
self.reset_packet_infos()
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for i in ifs:
pkts = self.create_stream(
i, self.pg_if_packet_sizes, if_dst_hosts=dst_hosts)
i.add_stream(pkts)
self.vapi.bridge_flags(bd_id, 0, 1)
# Enable packet capture and start packet sending
self.pg_enable_capture(ifs)
self.pg_start()
# Verify
# Verify outgoing packet streams per packet-generator interface
for i in ifs:
if not dst_hosts[i.sw_if_index]:
continue
capture = i.get_capture()
self.logger.info("Verifying capture on interface %s" % i.name)
self.verify_capture(i, capture)
def run_verify_negat_test(self, bd_id, dst_hosts=None):
# Test
# Create incoming packet streams for packet-generator interfaces for
# deleted MAC addresses
if not dst_hosts:
dst_hosts = self.deleted_hosts
self.reset_packet_infos()
ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
for i in ifs:
pkts = self.create_stream(
i, self.pg_if_packet_sizes, if_dst_hosts=dst_hosts)
if pkts:
i.add_stream(pkts)
self.vapi.bridge_flags(bd_id, 0, 1)
# Enable packet capture and start packet sending
self.pg_enable_capture(ifs)
self.pg_start()
# Verify
# Verify outgoing packet streams per packet-generator interface
timeout = 1
for i in ifs:
i.get_capture(0, timeout=timeout)
i.assert_nothing_captured(remark="outgoing interface")
timeout = 0.1
def test_l2_fib_01(self):
""" L2 FIB test 1 - program 100 MAC addresses
"""
# Config 1
# Create test host entries
self.create_hosts(100, subnet=17)
# Add first 100 MAC entries to L2 FIB
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=100)
# Test 1
self.run_verify_test(bd_id=1)
def test_l2_fib_02(self):
""" L2 FIB test 2 - delete 12 MAC entries
"""
# Config 2
# Delete 12 MAC entries per interface from L2 FIB
self.delete_l2_fib_entry(bd_id=1, n_hosts_per_if=12)
# Test 2a
self.run_verify_test(bd_id=1)
# Verify 2a
self.run_verify_negat_test(bd_id=1)
def test_l2_fib_03(self):
""" L2 FIB test 3 - program new 100 MAC addresses
"""
# Config 3
# Create new test host entries
self.create_hosts(100, subnet=22)
# Add new 100 MAC entries to L2 FIB
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=100)
# Test 3
self.run_verify_test(bd_id=1)
def test_l2_fib_04(self):
""" L2 FIB test 4 - delete 160 MAC entries
"""
# Config 4
# Delete 160 MAC entries per interface from L2 FIB
self.delete_l2_fib_entry(bd_id=1, n_hosts_per_if=160)
# Test 4a
self.run_verify_negat_test(bd_id=1)
def test_l2_fib_05(self):
""" L2 FIB test 5 - Program 10 new MAC entries, learn 10
"""
self.create_hosts(20, subnet=35)
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.learn_hosts(bd_id=2, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=2, n_hosts_per_if=10)
self.run_verify_test(bd_id=1, dst_hosts=self.learned_hosts)
self.run_verify_test(bd_id=2, dst_hosts=self.learned_hosts)
def test_l2_fib_06(self):
""" L2 FIB test 6 - flush first interface
"""
self.create_hosts(20, subnet=36)
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.learn_hosts(bd_id=2, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=2, n_hosts_per_if=10)
flushed = self.flush_int(self.pg_interfaces[0].sw_if_index)
self.run_verify_test(bd_id=1, dst_hosts=self.learned_hosts)
self.run_verify_negat_test(bd_id=1, dst_hosts=flushed)
def test_l2_fib_07(self):
""" L2 FIB test 7 - flush bd_id
"""
self.create_hosts(20, subnet=37)
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.learn_hosts(bd_id=2, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=2, n_hosts_per_if=10)
flushed = self.flush_bd(bd_id=1)
self.run_verify_negat_test(bd_id=1, dst_hosts=flushed)
self.run_verify_test(bd_id=2, dst_hosts=self.learned_hosts)
def test_l2_fib_08(self):
""" L2 FIB test 8 - flush all
"""
self.create_hosts(20, subnet=38)
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.learn_hosts(bd_id=2, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=1, n_hosts_per_if=10)
self.config_l2_fib_entries(bd_id=2, n_hosts_per_if=10)
flushed = self.flush_all()
self.run_verify_negat_test(bd_id=1, dst_hosts=flushed)
self.run_verify_negat_test(bd_id=2, dst_hosts=flushed)
def test_l2_fib_09(self):
""" L2 FIB test 9 - mac learning events
"""
self.create_hosts(10, subnet=39)
self.vapi.want_macs_learn_events()
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.sleep(1)
self.logger.info(self.vapi.ppcli("show l2fib"))
evs = self.vapi.collect_events()
learned_macs = {
e.mac[i].mac_addr for e in evs for i in range(e.n_macs)}
macs = {h.bin_mac for swif_hs in self.learned_hosts.itervalues()
for h in swif_hs}
self.vapi.want_macs_learn_events(enable_disable=0)
self.assertEqual(len(learned_macs ^ macs), 0)
def test_l2_fib_10(self):
""" L2 FIB test 10 - mac learning max macs in event
"""
flushed = self.flush_all()
self.create_hosts(10, subnet=40)
ev_macs = 1
self.vapi.want_macs_learn_events(max_macs_in_event=ev_macs)
self.learn_hosts(bd_id=1, n_hosts_per_if=10)
self.sleep(1)
self.logger.info(self.vapi.ppcli("show l2fib"))
evs = self.vapi.collect_events()
learned_macs = {
e.mac[i].mac_addr for e in evs for i in range(e.n_macs)}
macs = {h.bin_mac for swif_hs in self.learned_hosts.itervalues()
for h in swif_hs}
self.vapi.want_macs_learn_events(enable_disable=0)
for e in evs:
self.assertLess(len(e), ev_macs * 10)
self.assertEqual(len(learned_macs ^ macs), 0)
if __name__ == '__main__':
unittest.main(testRunner=VppTestRunner)
|