forked from antrea-io/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofnetPolicy_test.go
executable file
·211 lines (173 loc) · 6.04 KB
/
ofnetPolicy_test.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ofnet
import (
"fmt"
"net"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/contiv/ofnet/ovsdbDriver"
)
func TestPolicyAddDelete(t *testing.T) {
var resp bool
rpcPort := uint16(9600)
ovsPort := uint16(9601)
lclIP := net.ParseIP("10.10.10.10")
ofnetAgent, err := NewOfnetAgent("", "vrouter", lclIP, rpcPort, ovsPort, nil)
if err != nil {
t.Fatalf("Error creating ofnet agent. Err: %v", err)
}
defer func() { ofnetAgent.Delete() }()
// Override MyAddr to local host
ofnetAgent.MyAddr = "127.0.0.1"
// Create a Master
ofnetMaster := NewOfnetMaster("", uint16(9602))
defer func() { ofnetMaster.Delete() }()
masterInfo := OfnetNode{
HostAddr: "127.0.0.1",
HostPort: uint16(9602),
}
// connect vrtr agent to master
err = ofnetAgent.AddMaster(&masterInfo, &resp)
if err != nil {
t.Errorf("Error adding master %+v. Err: %v", masterInfo, err)
}
log.Infof("Created vrouter ofnet agent: %v", ofnetAgent)
brName := "ovsbr60"
ovsDriver := ovsdbDriver.NewOvsDriver(brName)
err = ovsDriver.AddController("127.0.0.1", ovsPort)
if err != nil {
t.Fatalf("Error adding controller to ovs: %s", brName)
}
// Wait for switch to connect to controller
ofnetAgent.WaitForSwitchConnection()
// Create a vlan for the endpoint
ofnetAgent.AddNetwork(1, 1, "", "default")
macAddr, _ := net.ParseMAC("00:01:02:03:04:05")
endpoint := EndpointInfo{
EndpointGroup: 100,
PortNo: 12,
MacAddr: macAddr,
Vlan: 1,
IpAddr: net.ParseIP("10.2.2.2"),
}
log.Infof("Adding Local endpoint: %+v", endpoint)
// Add an Endpoint
err = ofnetAgent.AddLocalEndpoint(endpoint)
if err != nil {
t.Errorf("Error adding endpoint. Err: %v", err)
return
}
tcpRule := &OfnetPolicyRule{
RuleId: "tcpRule",
Priority: 100,
SrcEndpointGroup: 100,
DstEndpointGroup: 200,
SrcIpAddr: "10.10.10.10/24",
DstIpAddr: "10.1.1.1/24",
IpProtocol: 6,
DstPort: 100,
SrcPort: 200,
Action: "allow",
}
log.Infof("Adding rule: %+v", tcpRule)
// Add a policy
err = ofnetMaster.AddRule(tcpRule)
if err != nil {
t.Errorf("Error installing tcpRule {%+v}. Err: %v", tcpRule, err)
return
}
udpRule := &OfnetPolicyRule{
RuleId: "udpRule",
Priority: 100,
SrcEndpointGroup: 300,
DstEndpointGroup: 400,
SrcIpAddr: "20.20.20.20/24",
DstIpAddr: "20.2.2.2/24",
IpProtocol: 17,
DstPort: 300,
SrcPort: 400,
Action: "deny",
}
log.Infof("Adding rule: %+v", udpRule)
// Add the policy
err = ofnetMaster.AddRule(udpRule)
if err != nil {
t.Errorf("Error installing udpRule {%+v}. Err: %v", udpRule, err)
return
}
// Get all the flows
flowList, err := ofctlFlowDump(brName)
if err != nil {
t.Errorf("Error getting flow entries. Err: %v", err)
return
}
// verify src group flow
srcGrpFlowMatch := fmt.Sprintf("priority=10,in_port=12 actions=write_metadata:0x100640000/0xff7fff0000")
if !ofctlFlowMatch(flowList, VLAN_TBL_ID, srcGrpFlowMatch) {
fmt.Printf("Flows:\n%+v", flowList)
t.Fatalf("Could not find the flow %s on ovs %s", srcGrpFlowMatch, brName)
}
log.Infof("Found src group %s on ovs %s", srcGrpFlowMatch, brName)
// verify dst group flow
dstGrpFlowMatch := fmt.Sprintf("priority=100,ip,metadata=0x100000000/0xff00000000,nw_dst=10.2.2.2 actions=write_metadata:0xc8/0xfffe")
if !ofctlFlowMatch(flowList, DST_GRP_TBL_ID, dstGrpFlowMatch) {
t.Fatalf("Could not find the flow %s on ovs %s", dstGrpFlowMatch, brName)
}
log.Infof("Found dst group %s on ovs %s", dstGrpFlowMatch, brName)
// verify tcp rule flow entry exists
tcpFlowMatch := fmt.Sprintf("priority=110,tcp,metadata=0x640190/0x7ffffffe,nw_src=10.10.10.0/24,nw_dst=10.1.1.0/24,tp_src=200,tp_dst=100")
if !ofctlFlowMatch(flowList, POLICY_TBL_ID, tcpFlowMatch) {
t.Fatalf("Could not find the flow %s on ovs %s", tcpFlowMatch, brName)
}
log.Infof("Found tcp rule %s on ovs %s", tcpFlowMatch, brName)
// verify udp rule flow
udpFlowMatch := fmt.Sprintf("priority=110,udp,metadata=0x12c0320/0x7ffffffe,nw_src=20.20.20.0/24,nw_dst=20.2.2.0/24,tp_src=400,tp_dst=300")
if !ofctlFlowMatch(flowList, POLICY_TBL_ID, udpFlowMatch) {
t.Fatalf("Could not find the flow %s on ovs %s", udpFlowMatch, brName)
}
log.Infof("Found udp rule %s on ovs %s", udpFlowMatch, brName)
// Delete policies
err = ofnetMaster.DelRule(tcpRule)
if err != nil {
t.Fatalf("Error deleting tcpRule {%+v}. Err: %v", tcpRule, err)
}
err = ofnetMaster.DelRule(udpRule)
if err != nil {
t.Fatalf("Error deleting udpRule {%+v}. Err: %v", udpRule, err)
}
err = ofnetAgent.RemoveLocalEndpoint(endpoint.PortNo)
if err != nil {
t.Fatalf("Error deleting endpoint: %+v. Err: %v", endpoint, err)
}
log.Infof("Deleted all policy entries")
// Get the flows again
flowList, err = ofctlFlowDump(brName)
if err != nil {
t.Fatalf("Error getting flow entries. Err: %v", err)
}
// Make sure flows are gone
if ofctlFlowMatch(flowList, VLAN_TBL_ID, srcGrpFlowMatch) {
t.Fatalf("Still found the flow %s on ovs %s", srcGrpFlowMatch, brName)
}
if ofctlFlowMatch(flowList, DST_GRP_TBL_ID, dstGrpFlowMatch) {
t.Fatalf("Still found the flow %s on ovs %s", dstGrpFlowMatch, brName)
}
if ofctlFlowMatch(flowList, POLICY_TBL_ID, tcpFlowMatch) {
t.Fatalf("Still found the flow %s on ovs %s", tcpFlowMatch, brName)
}
if ofctlFlowMatch(flowList, POLICY_TBL_ID, udpFlowMatch) {
t.Fatalf("Still found the flow %s on ovs %s", udpFlowMatch, brName)
}
log.Infof("Verified all flows are deleted")
}