/
table_based_simple.py
147 lines (128 loc) · 4.8 KB
/
table_based_simple.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
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
# ################################################################################
# ##
# ## https://github.com/NetASM/NetASM-python
# ##
# ## File:
# ## table_based_simple.py
# ##
# ## Project:
# ## NetASM: A Network Assembly Language for Programmable Dataplanes
# ##
# ## Author:
# ## Muhammad Shahbaz
# ##
# ## Copyright notice:
# ## Copyright (C) 2014 Princeton University
# ## Network Operations and Internet Security Lab
# ##
# ## Licence:
# ## This file is a part of the NetASM development base package.
# ##
# ## This file is free code: you can redistribute it and/or modify it under
# ## the terms of the GNU Lesser General Public License version 2.1 as
# ## published by the Free Software Foundation.
# ##
# ## This package is distributed in the hope that it will be useful, but
# ## WITHOUT ANY WARRANTY; without even the implied warranty of
# ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# ## Lesser General Public License for more details.
# ##
# ## You should have received a copy of the GNU Lesser General Public
# ## License along with the NetASM source package. If not, see
# ## http://www.gnu.org/licenses/.
__author__ = 'shahbaz'
from netasm.netasm.core import *
def main():
### Declarations ###
decls = Decls(TableDecls())
## Tables ##
TABLE_SIZE = Size(2)
decls.table_decls[TableId('match_table')] = \
Table(TableFieldsCollection.MatchFields(),
TABLE_SIZE,
TableTypeCollection.CAM)
match_table = decls.table_decls[TableId('match_table')]
match_table.table_fields[Field('eth_src')] = Size(48), MatchTypeCollection.Binary
decls.table_decls[TableId('params_table')] = \
Table(TableFieldsCollection.SimpleFields(),
TABLE_SIZE,
TableTypeCollection.RAM)
params_table = decls.table_decls[TableId('params_table')]
params_table.table_fields[Field('outport_bitmap')] = Size(2)
# Note: outport_bitmap is a bitmap
### Code ###
code = I.Code(
##################
### Arguments ####
##################
Fields(),
##################
## Instructions ##
##################
I.Instructions(
##################
## Parse packet ##
##################
# Add ethernet header fields in the header set
I.ADD(O.Field(Field('eth_dst')),
Size(48)),
I.ADD(O.Field(Field('eth_src')),
Size(48)),
I.ADD(O.Field(Field('eth_type')),
Size(16)),
# Load fields with default values
I.LD(O.Field(Field('eth_dst')),
O.Value(Value(0, Size(48)))),
I.LD(O.Field(Field('eth_src')),
O.Value(Value(0, Size(48)))),
I.LD(O.Field(Field('eth_type')),
O.Value(Value(0, Size(16)))),
# Parse ethernet
# load ethernet header fields from the packet
I.LD(O.Field(Field('eth_dst')),
O.Location(
Location(
O.Value(Value(0, Size(16)))))),
I.LD(O.Field(Field('eth_src')),
O.Location(
Location(
O.Value(Value(48, Size(16)))))),
I.LD(O.Field(Field('eth_type')),
O.Location(
Location(
O.Value(Value(96, Size(16)))))),
##################
## Pass through ##
##################
# Add the following header fields in the header set
I.ADD(O.Field(Field('index')),
Size(16)),
# Lookup in the match table and store the matched index
I.LKt(O.Field(Field('index')),
TableId('match_table'),
O.Operands_(
O.Field(Field('eth_src')))),
I.BR(O.Field(Field('index')),
Op.Neq,
O.Value(Value(-1, Size(16))),
Label('LBL_PT_0')),
# Case: there is not match in the match table
# send to controller
I.CTR(Reason('MATCH_TABLE_MISS', '')),
I.JMP(Label('LBL_HLT')),
# Case: there is a match in the l2 match table
I.LBL(Label('LBL_PT_0')),
# Load output port and others from the l2 parameters table
I.LDt(
O.Operands__(
O.Field(Field('outport_bitmap'))),
TableId('params_table'),
O.Field(Field('index'))),
##########
## Halt ##
##########
I.LBL(Label('LBL_HLT')),
I.HLT()
)
)
return Policy(decls, code)