blob: ec00cb1f973b41282f505ec456431a18316d72a1 [file] [log] [blame]
Scott Baker456b8932019-10-24 10:36:39 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package flows
17
18import (
Esin Karaman20b6de92019-11-05 08:29:16 +000019 "bytes"
serkant.uluderyab38671c2019-11-01 09:35:38 -070020 "strings"
21 "testing"
22
23 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
Scott Baker456b8932019-10-24 10:36:39 -070024 "github.com/stretchr/testify/assert"
25 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
Scott Baker456b8932019-10-24 10:36:39 -070027)
28
29var (
30 timeoutError error
31 taskFailureError error
32)
33
34func init() {
Scott Baker456b8932019-10-24 10:36:39 -070035 timeoutError = status.Errorf(codes.Aborted, "timeout")
36 taskFailureError = status.Error(codes.Internal, "test failure task")
37 timeoutError = status.Errorf(codes.Aborted, "timeout")
38}
39
40func TestFlowsAndGroups_AddFlow(t *testing.T) {
41 fg := NewFlowsAndGroups()
42 allFlows := fg.ListFlows()
43 assert.Equal(t, 0, len(allFlows))
44 fg.AddFlow(nil)
45 allFlows = fg.ListFlows()
46 assert.Equal(t, 0, len(allFlows))
47
David K. Bainbridge7c75cac2020-02-19 08:53:46 -080048 fa := &FlowArgs{
Scott Baker456b8932019-10-24 10:36:39 -070049 KV: OfpFlowModArgs{"priority": 500},
50 MatchFields: []*ofp.OfpOxmOfbField{
51 InPort(1),
52 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1),
53 TunnelId(uint64(1)),
54 EthType(0x0800),
55 Ipv4Dst(0xffffffff),
56 IpProto(17),
57 UdpSrc(68),
58 UdpDst(67),
59 },
60 Actions: []*ofp.OfpAction{
61 PushVlan(0x8100),
62 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 4000)),
63 Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
64 },
65 }
Scott Baker6256a342020-02-21 15:36:26 -080066 flow, err := MkFlowStat(fa)
67 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -070068 fg.AddFlow(flow)
69
70 allFlows = fg.ListFlows()
71 assert.Equal(t, 1, len(allFlows))
72 assert.True(t, FlowMatch(flow, allFlows[0]))
73}
74
75func TestFlowsAndGroups_AddGroup(t *testing.T) {
76 var ga *GroupArgs
77
78 fg := NewFlowsAndGroups()
79 allGroups := fg.ListGroups()
80 assert.Equal(t, 0, len(allGroups))
81 fg.AddGroup(nil)
82 allGroups = fg.ListGroups()
83 assert.Equal(t, 0, len(allGroups))
84
85 ga = &GroupArgs{
86 GroupId: 10,
87 Buckets: []*ofp.OfpBucket{
88 {Actions: []*ofp.OfpAction{
89 PopVlan(),
90 Output(1),
91 },
92 },
93 },
94 }
95 group := MkGroupStat(ga)
96 fg.AddGroup(group)
97
98 allGroups = fg.ListGroups()
99 assert.Equal(t, 1, len(allGroups))
100 assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId)
101}
102
103func TestFlowsAndGroups_Copy(t *testing.T) {
104 fg := NewFlowsAndGroups()
105 var fa *FlowArgs
106 var ga *GroupArgs
107
108 fa = &FlowArgs{
109 KV: OfpFlowModArgs{"priority": 500},
110 MatchFields: []*ofp.OfpOxmOfbField{
111 InPort(2),
112 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
113 },
114 Actions: []*ofp.OfpAction{
115 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 10)),
116 Output(1),
117 },
118 }
Scott Baker6256a342020-02-21 15:36:26 -0800119 flow, err := MkFlowStat(fa)
120 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700121 fg.AddFlow(flow)
122
123 ga = &GroupArgs{
124 GroupId: 10,
125 Buckets: []*ofp.OfpBucket{
126 {Actions: []*ofp.OfpAction{
127 PopVlan(),
128 Output(1),
129 },
130 },
131 },
132 }
133 group := MkGroupStat(ga)
134 fg.AddGroup(group)
135
136 fgCopy := fg.Copy()
137
138 allFlows := fgCopy.ListFlows()
139 assert.Equal(t, 1, len(allFlows))
140 assert.True(t, FlowMatch(flow, allFlows[0]))
141
142 allGroups := fgCopy.ListGroups()
143 assert.Equal(t, 1, len(allGroups))
144 assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId)
145
146 fg = NewFlowsAndGroups()
147 fgCopy = fg.Copy()
148 allFlows = fgCopy.ListFlows()
149 allGroups = fgCopy.ListGroups()
150 assert.Equal(t, 0, len(allFlows))
151 assert.Equal(t, 0, len(allGroups))
152}
153
154func TestFlowsAndGroups_GetFlow(t *testing.T) {
155 fg := NewFlowsAndGroups()
156 var fa1 *FlowArgs
157 var fa2 *FlowArgs
158 var ga *GroupArgs
159
160 fa1 = &FlowArgs{
161 KV: OfpFlowModArgs{"priority": 500},
162 MatchFields: []*ofp.OfpOxmOfbField{
163 InPort(2),
164 Metadata_ofp((1000 << 32) | 1),
165 VlanPcp(0),
166 },
167 Actions: []*ofp.OfpAction{
168 PopVlan(),
169 },
170 }
Scott Baker6256a342020-02-21 15:36:26 -0800171 flow1, err := MkFlowStat(fa1)
172 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700173
174 fa2 = &FlowArgs{
175 KV: OfpFlowModArgs{"priority": 1500},
176 MatchFields: []*ofp.OfpOxmOfbField{
177 InPort(5),
178 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
179 },
180 Actions: []*ofp.OfpAction{
181 PushVlan(0x8100),
182 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1000)),
183 SetField(VlanPcp(0)),
184 Output(2),
185 },
186 }
Scott Baker6256a342020-02-21 15:36:26 -0800187 flow2, err := MkFlowStat(fa2)
188 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700189
190 fg.AddFlow(flow1)
191 fg.AddFlow(flow2)
192
193 ga = &GroupArgs{
194 GroupId: 10,
195 Buckets: []*ofp.OfpBucket{
196 {Actions: []*ofp.OfpAction{
197 PopVlan(),
198 Output(1),
199 },
200 },
201 },
202 }
203 group := MkGroupStat(ga)
204 fg.AddGroup(group)
205
206 gf1 := fg.GetFlow(0)
207 assert.True(t, FlowMatch(flow1, gf1))
208
209 gf2 := fg.GetFlow(1)
210 assert.True(t, FlowMatch(flow2, gf2))
211
212 gf3 := fg.GetFlow(2)
213 assert.Nil(t, gf3)
214
215 allFlows := fg.ListFlows()
216 assert.True(t, FlowMatch(flow1, allFlows[0]))
217 assert.True(t, FlowMatch(flow2, allFlows[1]))
218}
219
220func TestFlowsAndGroups_String(t *testing.T) {
221 fg := NewFlowsAndGroups()
222 var fa *FlowArgs
223 var ga *GroupArgs
224
225 str := fg.String()
226 assert.True(t, str == "")
227
228 fa = &FlowArgs{
229 KV: OfpFlowModArgs{"priority": 500},
230 MatchFields: []*ofp.OfpOxmOfbField{
231 InPort(2),
232 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
233 VlanPcp(0),
234 EthType(0x800),
235 Ipv4Dst(0xe00a0a0a),
236 },
237 Actions: []*ofp.OfpAction{
238 Group(10),
239 },
240 }
Scott Baker6256a342020-02-21 15:36:26 -0800241 flow, err := MkFlowStat(fa)
242 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700243 fg.AddFlow(flow)
244
245 ga = &GroupArgs{
246 GroupId: 10,
247 Buckets: []*ofp.OfpBucket{
248 {Actions: []*ofp.OfpAction{
249 PopVlan(),
250 Output(1),
251 },
252 },
253 },
254 }
255 group := MkGroupStat(ga)
256 fg.AddGroup(group)
257
258 str = fg.String()
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400259 assert.True(t, strings.Contains(str, "id: 11819684229970388353"))
Scott Baker456b8932019-10-24 10:36:39 -0700260 assert.True(t, strings.Contains(str, "group_id: 10"))
261 assert.True(t, strings.Contains(str, "oxm_class: OFPXMC_OPENFLOW_BASICOFPXMC_OPENFLOW_BASIC"))
262 assert.True(t, strings.Contains(str, "type: OFPXMT_OFB_VLAN_VIDOFPXMT_OFB_VLAN_VID"))
263 assert.True(t, strings.Contains(str, "vlan_vid: 4096"))
264 assert.True(t, strings.Contains(str, "buckets:"))
265}
266
267func TestFlowsAndGroups_AddFrom(t *testing.T) {
268 fg := NewFlowsAndGroups()
269 var fa *FlowArgs
270 var ga *GroupArgs
271
272 str := fg.String()
273 assert.True(t, str == "")
274
275 fa = &FlowArgs{
276 KV: OfpFlowModArgs{"priority": 500},
277 MatchFields: []*ofp.OfpOxmOfbField{
278 InPort(2),
279 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
280 Metadata_ofp(1000),
281 TunnelId(uint64(1)),
282 VlanPcp(0),
283 },
284 Actions: []*ofp.OfpAction{
285 PopVlan(),
286 Output(1),
287 },
288 }
Scott Baker6256a342020-02-21 15:36:26 -0800289 flow, err := MkFlowStat(fa)
290 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700291 fg.AddFlow(flow)
292
293 ga = &GroupArgs{
294 GroupId: 10,
295 Buckets: []*ofp.OfpBucket{
296 {Actions: []*ofp.OfpAction{
297 PopVlan(),
298 Output(1),
299 },
300 },
301 },
302 }
303 group := MkGroupStat(ga)
304 fg.AddGroup(group)
305
306 fg1 := NewFlowsAndGroups()
307 fg1.AddFrom(fg)
308
309 allFlows := fg1.ListFlows()
310 allGroups := fg1.ListGroups()
311 assert.Equal(t, 1, len(allFlows))
312 assert.Equal(t, 1, len(allGroups))
313 assert.True(t, FlowMatch(flow, allFlows[0]))
314 assert.Equal(t, group.Desc.GroupId, allGroups[0].Desc.GroupId)
315}
316
317func TestDeviceRules_AddFlow(t *testing.T) {
318 dr := NewDeviceRules()
319 rules := dr.GetRules()
320 assert.True(t, len(rules) == 0)
321
322 dr.AddFlow("123456", nil)
323 rules = dr.GetRules()
324 assert.True(t, len(rules) == 1)
325 val, ok := rules["123456"]
326 assert.True(t, ok)
327 assert.Equal(t, 0, len(val.ListFlows()))
328 assert.Equal(t, 0, len(val.ListGroups()))
329
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800330 fa := &FlowArgs{
Scott Baker456b8932019-10-24 10:36:39 -0700331 KV: OfpFlowModArgs{"priority": 500},
332 MatchFields: []*ofp.OfpOxmOfbField{
333 InPort(2),
334 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
335 Metadata_ofp(1000),
336 TunnelId(uint64(1)),
337 VlanPcp(0),
338 },
339 Actions: []*ofp.OfpAction{
340 PopVlan(),
341 Output(1),
342 },
343 }
Scott Baker6256a342020-02-21 15:36:26 -0800344 flow, err := MkFlowStat(fa)
345 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700346 dr.AddFlow("123456", flow)
347 rules = dr.GetRules()
348 assert.True(t, len(rules) == 1)
349 val, ok = rules["123456"]
350 assert.True(t, ok)
351 assert.Equal(t, 1, len(val.ListFlows()))
352 assert.True(t, FlowMatch(flow, val.ListFlows()[0]))
353 assert.Equal(t, 0, len(val.ListGroups()))
354}
355
356func TestDeviceRules_AddFlowsAndGroup(t *testing.T) {
357 fg := NewFlowsAndGroups()
358 var fa *FlowArgs
359 var ga *GroupArgs
360
361 str := fg.String()
362 assert.True(t, str == "")
363
364 fa = &FlowArgs{
365 KV: OfpFlowModArgs{"priority": 2000},
366 MatchFields: []*ofp.OfpOxmOfbField{
367 InPort(2),
368 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
369 Metadata_ofp(1000),
370 TunnelId(uint64(1)),
371 VlanPcp(0),
372 },
373 Actions: []*ofp.OfpAction{
374 PopVlan(),
375 Output(1),
376 },
377 }
Scott Baker6256a342020-02-21 15:36:26 -0800378 flow, err := MkFlowStat(fa)
379 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700380 fg.AddFlow(flow)
381
382 ga = &GroupArgs{
383 GroupId: 10,
384 Buckets: []*ofp.OfpBucket{
385 {Actions: []*ofp.OfpAction{
386 PopVlan(),
387 Output(1),
388 },
389 },
390 },
391 }
392 group := MkGroupStat(ga)
393 fg.AddGroup(group)
394
395 dr := NewDeviceRules()
396 dr.AddFlowsAndGroup("123456", fg)
397 rules := dr.GetRules()
398 assert.True(t, len(rules) == 1)
399 val, ok := rules["123456"]
400 assert.True(t, ok)
401 assert.Equal(t, 1, len(val.ListFlows()))
402 assert.Equal(t, 1, len(val.ListGroups()))
403 assert.True(t, FlowMatch(flow, val.ListFlows()[0]))
404 assert.Equal(t, 10, int(val.ListGroups()[0].Desc.GroupId))
405}
406
407func TestFlowHasOutPort(t *testing.T) {
408 var flow *ofp.OfpFlowStats
409 assert.False(t, FlowHasOutPort(flow, 1))
410
411 fa := &FlowArgs{
412 KV: OfpFlowModArgs{"priority": 2000},
413 MatchFields: []*ofp.OfpOxmOfbField{
414 InPort(2),
415 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
416 Metadata_ofp(1000),
417 TunnelId(uint64(1)),
418 VlanPcp(0),
419 },
420 Actions: []*ofp.OfpAction{
421 PopVlan(),
422 Output(1),
423 },
424 }
Scott Baker6256a342020-02-21 15:36:26 -0800425 flow, err := MkFlowStat(fa)
426 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700427 assert.True(t, FlowHasOutPort(flow, 1))
428 assert.False(t, FlowHasOutPort(flow, 2))
429
430 fa = &FlowArgs{
431 KV: OfpFlowModArgs{"priority": 2000},
432 MatchFields: []*ofp.OfpOxmOfbField{
433 InPort(2),
434 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
435 },
436 }
Scott Baker6256a342020-02-21 15:36:26 -0800437 flow, err = MkFlowStat(fa)
438 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700439 assert.False(t, FlowHasOutPort(flow, 1))
440}
441
442func TestFlowHasOutGroup(t *testing.T) {
443 var flow *ofp.OfpFlowStats
444 assert.False(t, FlowHasOutGroup(flow, 10))
445
446 fa := &FlowArgs{
447 KV: OfpFlowModArgs{"priority": 500},
448 MatchFields: []*ofp.OfpOxmOfbField{
449 InPort(2),
450 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
451 VlanPcp(0),
452 EthType(0x800),
453 Ipv4Dst(0xe00a0a0a),
454 },
455 Actions: []*ofp.OfpAction{
456 Group(10),
457 },
458 }
Scott Baker6256a342020-02-21 15:36:26 -0800459 flow, err := MkFlowStat(fa)
460 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700461 assert.True(t, FlowHasOutGroup(flow, 10))
462 assert.False(t, FlowHasOutGroup(flow, 11))
463
464 fa = &FlowArgs{
465 KV: OfpFlowModArgs{"priority": 500},
466 MatchFields: []*ofp.OfpOxmOfbField{
467 InPort(2),
468 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
469 VlanPcp(0),
470 EthType(0x800),
471 Ipv4Dst(0xe00a0a0a),
472 },
473 Actions: []*ofp.OfpAction{
474 Output(1),
475 },
476 }
Scott Baker6256a342020-02-21 15:36:26 -0800477 flow, err = MkFlowStat(fa)
478 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700479 assert.False(t, FlowHasOutGroup(flow, 1))
480}
481
482func TestMatchFlow(t *testing.T) {
483 assert.False(t, FlowMatch(nil, nil))
484 fa := &FlowArgs{
485 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
486 MatchFields: []*ofp.OfpOxmOfbField{
487 InPort(2),
488 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
489 VlanPcp(0),
490 EthType(0x800),
491 Ipv4Dst(0xe00a0a0a),
492 },
493 Actions: []*ofp.OfpAction{
494 Group(10),
495 },
496 }
Scott Baker6256a342020-02-21 15:36:26 -0800497 flow1, err := MkFlowStat(fa)
498 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700499 assert.False(t, FlowMatch(flow1, nil))
500
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400501 // different table_id, cookie, flags
Scott Baker456b8932019-10-24 10:36:39 -0700502 fa = &FlowArgs{
503 KV: OfpFlowModArgs{"priority": 500},
504 MatchFields: []*ofp.OfpOxmOfbField{
505 InPort(2),
506 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
507 VlanPcp(0),
508 EthType(0x800),
509 Ipv4Dst(0xe00a0a0a),
510 },
511 Actions: []*ofp.OfpAction{
512 Group(10),
513 },
514 }
Scott Baker6256a342020-02-21 15:36:26 -0800515 flow2, err := MkFlowStat(fa)
516 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700517 assert.False(t, FlowMatch(flow1, flow2))
518 assert.False(t, FlowMatch(nil, flow2))
519
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400520 // no difference
Scott Baker456b8932019-10-24 10:36:39 -0700521 fa = &FlowArgs{
522 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
523 MatchFields: []*ofp.OfpOxmOfbField{
524 InPort(2),
525 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
526 VlanPcp(0),
527 EthType(0x800),
528 Ipv4Dst(0xe00a0a0a),
529 },
530 }
Scott Baker6256a342020-02-21 15:36:26 -0800531 flow2, err = MkFlowStat(fa)
532 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700533 assert.True(t, FlowMatch(flow1, flow2))
534
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400535 // different priority
Scott Baker456b8932019-10-24 10:36:39 -0700536 fa = &FlowArgs{
537 KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12},
538 MatchFields: []*ofp.OfpOxmOfbField{
539 InPort(2),
540 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
541 VlanPcp(0),
542 EthType(0x800),
543 Ipv4Dst(0xe00a0a0a),
544 },
545 Actions: []*ofp.OfpAction{
546 Group(10),
547 },
548 }
Scott Baker6256a342020-02-21 15:36:26 -0800549 flow2, err = MkFlowStat(fa)
550 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700551 assert.False(t, FlowMatch(flow1, flow2))
552
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400553 // different table id
Scott Baker456b8932019-10-24 10:36:39 -0700554 fa = &FlowArgs{
555 KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12},
556 MatchFields: []*ofp.OfpOxmOfbField{
557 InPort(2),
558 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
559 VlanPcp(0),
560 EthType(0x800),
561 Ipv4Dst(0xe00a0a0a),
562 },
563 }
Scott Baker6256a342020-02-21 15:36:26 -0800564 flow2, err = MkFlowStat(fa)
565 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700566 assert.False(t, FlowMatch(flow1, flow2))
567
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400568 // different cookie
Scott Baker456b8932019-10-24 10:36:39 -0700569 fa = &FlowArgs{
570 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12},
571 MatchFields: []*ofp.OfpOxmOfbField{
572 InPort(2),
573 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
574 VlanPcp(0),
575 EthType(0x800),
576 Ipv4Dst(0xe00a0a0a),
577 },
578 }
Scott Baker6256a342020-02-21 15:36:26 -0800579 flow2, err = MkFlowStat(fa)
580 assert.Nil(t, err)
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400581 assert.True(t, FlowMatch(flow1, flow2))
Scott Baker456b8932019-10-24 10:36:39 -0700582
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400583 // different flags
Scott Baker456b8932019-10-24 10:36:39 -0700584 fa = &FlowArgs{
585 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14},
586 MatchFields: []*ofp.OfpOxmOfbField{
587 InPort(2),
588 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
589 VlanPcp(0),
590 EthType(0x800),
591 Ipv4Dst(0xe00a0a0a),
592 },
593 }
Scott Baker6256a342020-02-21 15:36:26 -0800594 flow2, err = MkFlowStat(fa)
595 assert.Nil(t, err)
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400596 assert.True(t, FlowMatch(flow1, flow2))
Scott Baker456b8932019-10-24 10:36:39 -0700597
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400598 // different match InPort
Scott Baker456b8932019-10-24 10:36:39 -0700599 fa = &FlowArgs{
600 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
601 MatchFields: []*ofp.OfpOxmOfbField{
602 InPort(4),
603 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
604 VlanPcp(0),
605 EthType(0x800),
606 Ipv4Dst(0xe00a0a0a),
607 },
608 }
Scott Baker6256a342020-02-21 15:36:26 -0800609 flow2, err = MkFlowStat(fa)
610 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700611 assert.False(t, FlowMatch(flow1, flow2))
612
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400613 // different match Ipv4Dst
Scott Baker456b8932019-10-24 10:36:39 -0700614 fa = &FlowArgs{
615 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
616 MatchFields: []*ofp.OfpOxmOfbField{
617 InPort(2),
618 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
619 VlanPcp(0),
620 EthType(0x800),
621 },
622 }
Scott Baker6256a342020-02-21 15:36:26 -0800623 flow2, err = MkFlowStat(fa)
624 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700625 assert.False(t, FlowMatch(flow1, flow2))
626
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400627 // different actions
Scott Baker456b8932019-10-24 10:36:39 -0700628 fa = &FlowArgs{
629 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
630 MatchFields: []*ofp.OfpOxmOfbField{
631 InPort(2),
632 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
633 VlanPcp(0),
634 EthType(0x800),
635 Ipv4Dst(0xe00a0a0a),
636 },
637 Actions: []*ofp.OfpAction{
638 PopVlan(),
639 Output(1),
640 },
641 }
Scott Baker6256a342020-02-21 15:36:26 -0800642 flow2, err = MkFlowStat(fa)
643 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700644 assert.True(t, FlowMatch(flow1, flow2))
645}
646
647func TestFlowMatchesMod(t *testing.T) {
648 assert.False(t, FlowMatchesMod(nil, nil))
649 fa := &FlowArgs{
650 KV: OfpFlowModArgs{"priority": 500, "table_id": 1},
651 MatchFields: []*ofp.OfpOxmOfbField{
652 InPort(2),
653 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
654 VlanPcp(0),
655 EthType(0x800),
656 Ipv4Dst(0xe00a0a0a),
657 },
658 Actions: []*ofp.OfpAction{
659 Output(1),
660 Group(10),
661 },
662 }
Scott Baker6256a342020-02-21 15:36:26 -0800663 flow, err := MkFlowStat(fa)
664 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700665 assert.False(t, FlowMatchesMod(flow, nil))
666
667 fa = &FlowArgs{
668 KV: OfpFlowModArgs{"priority": 500, "table_id": 1},
669 MatchFields: []*ofp.OfpOxmOfbField{
670 InPort(2),
671 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
672 VlanPcp(0),
673 EthType(0x800),
674 Ipv4Dst(0xe00a0a0a),
675 },
676 Actions: []*ofp.OfpAction{
677 PopVlan(),
678 Output(1),
679 },
680 }
681 flowMod := MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
682 assert.False(t, FlowMatchesMod(nil, flowMod))
683 assert.False(t, FlowMatchesMod(flow, flowMod))
Scott Baker6256a342020-02-21 15:36:26 -0800684 entry, err := FlowStatsEntryFromFlowModMessage(flowMod)
685 assert.Nil(t, err)
686 assert.True(t, FlowMatch(flow, entry))
Scott Baker456b8932019-10-24 10:36:39 -0700687
688 fa = &FlowArgs{
689 KV: OfpFlowModArgs{"table_id": uint64(ofp.OfpTable_OFPTT_ALL),
690 "cookie_mask": 0,
691 "out_port": uint64(ofp.OfpPortNo_OFPP_ANY),
692 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
693 },
694 }
695 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
696 assert.True(t, FlowMatchesMod(flow, flowMod))
697
698 fa = &FlowArgs{
699 KV: OfpFlowModArgs{"table_id": 1,
700 "cookie_mask": 0,
701 "out_port": uint64(ofp.OfpPortNo_OFPP_ANY),
702 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
703 },
704 }
705 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
706 assert.True(t, FlowMatchesMod(flow, flowMod))
707
708 fa = &FlowArgs{
709 KV: OfpFlowModArgs{"table_id": 1,
710 "cookie_mask": 0,
711 "out_port": 1,
712 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
713 },
714 }
715 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
716 assert.True(t, FlowMatchesMod(flow, flowMod))
717
718 fa = &FlowArgs{
719 KV: OfpFlowModArgs{"table_id": 1,
720 "cookie_mask": 0,
721 "out_port": 1,
722 "out_group": 10,
723 },
724 }
725 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
726 assert.True(t, FlowMatchesMod(flow, flowMod))
727}
Esin Karaman20b6de92019-11-05 08:29:16 +0000728
729func TestIsMulticastIpAddress(t *testing.T) {
730 isMcastIp := IsMulticastIp(3776315393) //225.22.0.1
731 assert.True(t, isMcastIp)
732 isMcastIp = IsMulticastIp(3232243777) //192.168.32.65
733 assert.True(t, !isMcastIp)
734}
735
736func TestConvertToMulticastMac(t *testing.T) {
737 mcastIp := uint32(4001431809) //238.129.1.1
738 expectedMacInBytes := []byte{1, 0, 94, 1, 1, 1} //01:00:5e:01:01:01
739 macInBytes := ConvertToMulticastMacBytes(mcastIp)
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800740 assert.True(t, bytes.Equal(macInBytes, expectedMacInBytes))
Esin Karaman20b6de92019-11-05 08:29:16 +0000741}