blob: 0bc0f55dced0ac22f0beb8cf2bc3ad2a4ccf050a [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
52 for of_port in ports:
53 # do stuff
54 group_id = encode_l2_interface_group_id(vlan_id, of_port)
55 if is_tagged:
56 actions = [
57 ofp.action.output(of_port),
58 ]
59 else:
60 actions = [
61 ofp.action.pop_vlan(),
62 ofp.action.output(of_port),
63 ]
64
65 buckets = [
66 ofp.bucket(actions=actions),
67 ]
68
69 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
70 group_id=group_id,
71 buckets=buckets
72 )
73 ctrl.message_send(request)
74
75 if send_barrier:
76 do_barrier(ctrl)
77
78
79def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
80 buckets=[]
81 for of_port in ports:
82 group_id = encode_l2_interface_group_id(vlanid, of_port)
83 action=[ofp.action.group(group_id)]
84 buckets.append(ofp.bucket(actions=action))
85
86 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
87 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
88 group_id=group_id,
89 buckets=buckets
90 )
91 ctrl.message_send(request)
92
93
94def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
95 # table 10: vlan
96 # goto to table 20
97 for of_port in ports:
98 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
99 match = ofp.match()
100 match.oxm_list.append(ofp.oxm.in_port(of_port))
101 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
102 request = ofp.message.flow_add(
103 table_id=10,
104 cookie=42,
105 match=match,
106 instructions=[
107 ofp.instruction.goto_table(20)
108 ],
109 priority=0)
110 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
111 ctrl.message_send(request)
112
113 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
114 match = ofp.match()
115 match.oxm_list.append(ofp.oxm.in_port(of_port))
116 match.oxm_list.append(ofp.oxm.vlan_vid(0))
117 request = ofp.message.flow_add(
118 table_id=10,
119 cookie=42,
120 match=match,
121 instructions=[
122 ofp.instruction.apply_actions(
123 actions=[
124 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
125 ]
126 ),
127 ofp.instruction.goto_table(20)
128 ],
129 priority=0)
130 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
131 ctrl.message_send(request)
132
133 if send_barrier:
134 do_barrier(ctrl)
135
136def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
137 match = ofp.match()
138 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
139 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
140 request = ofp.message.flow_add(
141 table_id=50,
142 cookie=42,
143 match=match,
144 instructions=[
145 ofp.instruction.write_actions(
146 actions=[
147 ofp.action.group(group_id)]),
148 ofp.instruction.goto_table(60)
149 ],
150 buffer_id=ofp.OFP_NO_BUFFER,
151 priority=1000)
152
153 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
154 ctrl.message_send(request)
155
156 if send_barrier:
157 do_barrier(ctrl)