forked from theojepsen/p4app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
66 lines (51 loc) · 2.11 KB
/
main.py
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
from p4app import P4Mininet
from mininet.topo import Topo
n = 3
mgid = 10
class SingleSwitchTopo(Topo):
def __init__(self, n, **opts):
Topo.__init__(self, **opts)
sw = self.addSwitch('s1')
for i in range(1, n+1):
host = self.addHost('h%d' % i,
ip = "10.0.0.%d" % i,
mac = '00:00:00:00:00:%02x' % i)
self.addLink(host, sw, port2=i)
topo = SingleSwitchTopo(n)
net = P4Mininet(program='multicast.p4', topo=topo)
net.start()
sw = net.get('s1')
for i in range(1, n+1):
h = net.get('h%d' % i)
h.cmd('arp -s 10.0.0.255 ff:ff:ff:ff:ff:ff')
sw.insertTableEntry(table_name='MyIngress.ipv4_lpm',
match_fields={'hdr.ipv4.dstAddr': ["10.0.0.%d" % i, 32]},
action_name='MyIngress.set_egr',
action_params={'port': i})
sw.insertTableEntry(table_name='MyEgress.send_frame',
match_fields={'standard_metadata.egress_port': i},
action_name='MyEgress.rewrite_dst',
action_params={'mac': '00:00:00:00:00:%02x' % i,
'ip': '10.0.0.%d' % i})
sw.insertTableEntry(table_name='MyIngress.ipv4_lpm',
match_fields={'hdr.ipv4.dstAddr': ["10.0.0.255", 32]},
action_name='MyIngress.set_mgid',
action_params={'mgid': mgid})
sw.addMulticastGroup(mgid=mgid, ports=range(1, n+1))
loss = net.pingAll()
assert loss == 0
# Should receive a pong from h2 and h3 (i.e. a duplicate pong).
out = net.get('h1').cmd('ping -c2 10.0.0.255')
print(out)
assert 'from 10.0.0.3' in out
assert 'from 10.0.0.2' in out
# Update group. Should only receive a pong from h2.
sw.updateMulticastGroup(mgid=mgid, ports=[2])
out = net.get('h1').cmd('ping -c2 10.0.0.255')
assert 'from 10.0.0.2' in out
assert 'from 10.0.0.3' not in out
# Delete group. Packets should not be forwarded.
sw.deleteMulticastGroup(mgid=mgid, ports=[])
out = net.get('h1').cmd('ping -W1 -c2 10.0.0.255')
assert '0 received, 100% packet loss' in out
print("OK")