forked from yale-build-a-router/maclearning.p4app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.py
More file actions
85 lines (72 loc) · 2.71 KB
/
tables.py
File metadata and controls
85 lines (72 loc) · 2.71 KB
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
class TableEntry(dict):
def __init__(self, table_name, match_fields, action_name, action_params, priority):
super(TableEntry, self).__init__()
self['table_name'] = table_name
self['match_fields'] = match_fields
self['action_name'] = action_name
self['action_params'] = action_params
self['priority'] = priority
def __getattr__(self, attr):
return self[attr]
def __setattr__(self, attr, value):
self[attr] = value
class ArpTableEntry(TableEntry):
def __init__(self, nextHopIP, srcMAC, dstMAC):
super(ArpTableEntry, self).__init__(
table_name="MyIngress.arp",
match_fields={"meta.nextHop": [nextHopIP]},
action_name="MyIngress.ipv4_forward",
action_params={"dstAddr": dstMAC, "srcAddr": srcMAC},
priority=None
)
class RoutingTableEntry(TableEntry):
def __init__(self, keyIP, dstIP, port, priority, mask=0xffffffff):
super(RoutingTableEntry, self).__init__(
table_name="MyIngress.routing",
match_fields={"hdr.ipv4.dstAddr": [keyIP, mask]},
action_name="MyIngress.next_hop",
action_params={"dstAddr": dstIP, "port": port},
priority=priority
)
class LocalTableEntry(TableEntry):
def __init__(self, dstIP, t):
super(LocalTableEntry, self).__init__(
table_name="MyIngress.local",
match_fields={"hdr.ipv4.dstAddr": [dstIP]},
action_name="MyIngress.send_to_cpu",
action_params={"type": t},
priority=None
)
class Table:
def __init__(self, name, size=1024, entries=[]):
self.name = name
self.entries = entries
self.size = size
def __str__(self):
return f"Table {self.name}:\n" + '\n'.join([str(entry) for entry in self.entries])
def add_entry(self, entry):
self.entries.append(entry)
def remove_entry(self, entry):
self.entries.remove(entry)
def get_entry(self, **kwargs):
for entry in self.entries:
for key, value in kwargs.items():
if entry[key] != value:
break
else:
return entry
return None
def get_entries(self, **kwargs):
entries = []
for entry in self.entries:
for key, value in kwargs.items():
if entry[key] != value:
break
else:
entries.append(entry)
return entries
def reset(self):
self.entries = []
class RoutingTable(Table):
def __init__(self, size=1024, entries=[]):
super(RoutingTable, self).__init__("MyIngress.routing", size, entries)