blob: 561d68909c2cde56750ba272aa2b72d7a4a84432 [file] [log] [blame]
macauley97557232015-07-16 17:28:07 +08001import logging
2
3from oftest import config
4import oftest.base_tests as base_tests
5import ofp
6import time
7from oftest.testutils import *
8
9OFDPA_GROUP_TYPE_SHIFT=28
10OFDPA_VLAN_ID_SHIFT =16
11OFDPA_TUNNEL_ID_SHIFT =16
12
13#VLAN_TABLE_FLAGS
14VLAN_TABLE_FLAG_ONLY_UNTAG=1
15VLAN_TABLE_FLAG_ONLY_TAG =2
16VLAN_TABLE_FLAG_ONLY_BOTH =3
17
18def encode_l2_interface_group_id(vlan, id):
19 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
20
21def encode_l2_rewrite_group_id(id):
22 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
23
24def encode_l3_unicast_group_id(id):
25 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
26
27def encode_l2_mcast_group_id(vlan, id):
28 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
29
30def encode_l2_flood_group_id(vlan, id):
31 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
32
33def encode_l3_interface_group_id(id):
34 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
35
36def encode_l3_mcast_group_id(vlan, id):
37 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
38
39def encode_l3_ecmp_group_id(id):
40 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
41
42def encode_l2_overlay_flood_group_id(tunnel_id, index):
43 return id + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
44
45def encode_l2_overlay_mcast_group_id(tunnel_id, index):
46 return id + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+(9 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +080047
48def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
49 # group table
50 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +080051 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +080052 msgs=[]
macauley97557232015-07-16 17:28:07 +080053 for of_port in ports:
54 # do stuff
55 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +080056 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +080057 if is_tagged:
58 actions = [
59 ofp.action.output(of_port),
60 ]
61 else:
62 actions = [
63 ofp.action.pop_vlan(),
64 ofp.action.output(of_port),
65 ]
66
67 buckets = [
68 ofp.bucket(actions=actions),
69 ]
70
71 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
72 group_id=group_id,
73 buckets=buckets
74 )
75 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +080076 msgs.append(request)
macauley97557232015-07-16 17:28:07 +080077
78 if send_barrier:
79 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +080080
macauley15909e72015-07-17 15:58:57 +080081 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +080082
83def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
84 buckets=[]
85 for of_port in ports:
86 group_id = encode_l2_interface_group_id(vlanid, of_port)
87 action=[ofp.action.group(group_id)]
88 buckets.append(ofp.bucket(actions=action))
89
90 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
91 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
92 group_id=group_id,
93 buckets=buckets
94 )
95 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +080096 return request
macauley97557232015-07-16 17:28:07 +080097
macauley15909e72015-07-17 15:58:57 +080098def add_l2_flood_group(ctrl, ports, vlanid, id):
99 buckets=[]
100 for of_port in ports:
101 group_id = encode_l2_interface_group_id(vlanid, of_port)
102 action=[ofp.action.group(group_id)]
103 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800104
macauley15909e72015-07-17 15:58:57 +0800105 group_id =encode_l2_flood_group_id(vlanid, id)
106 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
107 group_id=group_id,
108 buckets=buckets
109 )
110 ctrl.message_send(request)
111 return request
112
113def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
114 group_id = encode_l2_interface_group_id(vlanid, port)
115
116 action=[]
117 if src_mac is not None:
118 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
119
120 if dst_mac is not None:
121 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
122
123 action.append(ofp.action.group(group_id))
124
125 buckets = [ofp.bucket(actions=action)]
126
127 group_id =encode_l2_rewrite_group_id(id)
128 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
129 group_id=group_id,
130 buckets=buckets
131 )
132 ctrl.message_send(request)
133 return request
134
135def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
136 group_id = encode_l2_interface_group_id(vlanid, port)
137
138 action=[]
139 if src_mac is not None:
140 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
141
142 if dst_mac is not None:
143 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
144
145 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
146
147 action.append(ofp.action.group(group_id))
148
149 buckets = [ofp.bucket(actions=action)]
150
151 group_id =encode_l3_unicast_group_id(id)
152 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
153 group_id=group_id,
154 buckets=buckets
155 )
156 ctrl.message_send(request)
157 return request
158
159def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
160 group_id = encode_l2_interface_group_id(vlanid, port)
161
162 action=[]
163 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
164 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
165 action.append(ofp.action.group(group_id))
166
167 buckets = [ofp.bucket(actions=action)]
168
169 group_id =encode_l3_interface_group_id(id)
170 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
171 group_id=group_id,
172 buckets=buckets
173 )
174 ctrl.message_send(request)
175 return request
176
177def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
178 buckets=[]
179 for group in l3_ucast_groups:
180 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
181
182 group_id =encode_l3_ecmp_group_id(id)
183 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
184 group_id=group_id,
185 buckets=buckets
186 )
187 ctrl.message_send(request)
188 return request
189
190def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
191 buckets=[]
192 for group in groups_on_buckets:
193 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
194
195 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
196 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
197 group_id=group_id,
198 buckets=buckets
199 )
200 ctrl.message_send(request)
201 return request
202
macauley97557232015-07-16 17:28:07 +0800203def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
204 # table 10: vlan
205 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800206 msgs=[]
macauley97557232015-07-16 17:28:07 +0800207 for of_port in ports:
208 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
209 match = ofp.match()
210 match.oxm_list.append(ofp.oxm.in_port(of_port))
211 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
212 request = ofp.message.flow_add(
213 table_id=10,
214 cookie=42,
215 match=match,
216 instructions=[
217 ofp.instruction.goto_table(20)
218 ],
219 priority=0)
220 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
221 ctrl.message_send(request)
222
223 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
224 match = ofp.match()
225 match.oxm_list.append(ofp.oxm.in_port(of_port))
226 match.oxm_list.append(ofp.oxm.vlan_vid(0))
227 request = ofp.message.flow_add(
228 table_id=10,
229 cookie=42,
230 match=match,
231 instructions=[
232 ofp.instruction.apply_actions(
233 actions=[
234 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
235 ]
236 ),
237 ofp.instruction.goto_table(20)
238 ],
239 priority=0)
240 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
241 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800242 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800243
244 if send_barrier:
245 do_barrier(ctrl)
246
macauley15909e72015-07-17 15:58:57 +0800247 return msgs
248
macauley97557232015-07-16 17:28:07 +0800249def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
250 match = ofp.match()
251 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
252 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
253 request = ofp.message.flow_add(
254 table_id=50,
255 cookie=42,
256 match=match,
257 instructions=[
258 ofp.instruction.write_actions(
259 actions=[
260 ofp.action.group(group_id)]),
261 ofp.instruction.goto_table(60)
262 ],
263 buffer_id=ofp.OFP_NO_BUFFER,
264 priority=1000)
265
266 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
267 ctrl.message_send(request)
268
269 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800270 do_barrier(ctrl)
271
272 return request