blob: 0b4db757ec47635ddab68eb248da21ce9e9bf835 [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)
47
48
49def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
50 # group table
51 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +080052 group_id_list=[]
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)
76
77 if send_barrier:
78 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +080079
80 return group_id_list
macauley97557232015-07-16 17:28:07 +080081
82def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
83 buckets=[]
84 for of_port in ports:
85 group_id = encode_l2_interface_group_id(vlanid, of_port)
86 action=[ofp.action.group(group_id)]
87 buckets.append(ofp.bucket(actions=action))
88
89 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
90 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
91 group_id=group_id,
92 buckets=buckets
93 )
94 ctrl.message_send(request)
95
96
97def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
98 # table 10: vlan
99 # goto to table 20
100 for of_port in ports:
101 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
102 match = ofp.match()
103 match.oxm_list.append(ofp.oxm.in_port(of_port))
104 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
105 request = ofp.message.flow_add(
106 table_id=10,
107 cookie=42,
108 match=match,
109 instructions=[
110 ofp.instruction.goto_table(20)
111 ],
112 priority=0)
113 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
114 ctrl.message_send(request)
115
116 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
117 match = ofp.match()
118 match.oxm_list.append(ofp.oxm.in_port(of_port))
119 match.oxm_list.append(ofp.oxm.vlan_vid(0))
120 request = ofp.message.flow_add(
121 table_id=10,
122 cookie=42,
123 match=match,
124 instructions=[
125 ofp.instruction.apply_actions(
126 actions=[
127 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
128 ]
129 ),
130 ofp.instruction.goto_table(20)
131 ],
132 priority=0)
133 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
134 ctrl.message_send(request)
135
136 if send_barrier:
137 do_barrier(ctrl)
138
139def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
140 match = ofp.match()
141 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
142 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
143 request = ofp.message.flow_add(
144 table_id=50,
145 cookie=42,
146 match=match,
147 instructions=[
148 ofp.instruction.write_actions(
149 actions=[
150 ofp.action.group(group_id)]),
151 ofp.instruction.goto_table(60)
152 ],
153 buffer_id=ofp.OFP_NO_BUFFER,
154 priority=1000)
155
156 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
157 ctrl.message_send(request)
158
159 if send_barrier:
160 do_barrier(ctrl)