blob: 2ab06c2b0e59df1628df61ed2399d7a603abb135 [file] [log] [blame]
Scott Baker456b8932019-10-24 10:36:39 -07001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Scott Baker456b8932019-10-24 10:36:39 -07003 *
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"
madhumatigouda374f1b82026-03-17 17:52:04 +053022 "time"
serkant.uluderyab38671c2019-11-01 09:35:38 -070023
khenaidoo26721882021-08-11 17:42:52 -040024 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
Scott Baker456b8932019-10-24 10:36:39 -070025 "github.com/stretchr/testify/assert"
26 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
Scott Baker456b8932019-10-24 10:36:39 -070028)
29
30var (
31 timeoutError error
32 taskFailureError error
33)
34
35func init() {
Scott Baker456b8932019-10-24 10:36:39 -070036 timeoutError = status.Errorf(codes.Aborted, "timeout")
37 taskFailureError = status.Error(codes.Internal, "test failure task")
38 timeoutError = status.Errorf(codes.Aborted, "timeout")
39}
40
41func TestFlowsAndGroups_AddFlow(t *testing.T) {
42 fg := NewFlowsAndGroups()
43 allFlows := fg.ListFlows()
44 assert.Equal(t, 0, len(allFlows))
45 fg.AddFlow(nil)
46 allFlows = fg.ListFlows()
47 assert.Equal(t, 0, len(allFlows))
48
David K. Bainbridge7c75cac2020-02-19 08:53:46 -080049 fa := &FlowArgs{
Scott Baker456b8932019-10-24 10:36:39 -070050 KV: OfpFlowModArgs{"priority": 500},
51 MatchFields: []*ofp.OfpOxmOfbField{
52 InPort(1),
53 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1),
54 TunnelId(uint64(1)),
55 EthType(0x0800),
56 Ipv4Dst(0xffffffff),
57 IpProto(17),
58 UdpSrc(68),
59 UdpDst(67),
60 },
61 Actions: []*ofp.OfpAction{
62 PushVlan(0x8100),
63 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 4000)),
64 Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)),
65 },
66 }
Scott Baker6256a342020-02-21 15:36:26 -080067 flow, err := MkFlowStat(fa)
68 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -070069 fg.AddFlow(flow)
70
71 allFlows = fg.ListFlows()
72 assert.Equal(t, 1, len(allFlows))
73 assert.True(t, FlowMatch(flow, allFlows[0]))
74}
75
76func TestFlowsAndGroups_AddGroup(t *testing.T) {
77 var ga *GroupArgs
78
79 fg := NewFlowsAndGroups()
80 allGroups := fg.ListGroups()
81 assert.Equal(t, 0, len(allGroups))
82 fg.AddGroup(nil)
83 allGroups = fg.ListGroups()
84 assert.Equal(t, 0, len(allGroups))
85
86 ga = &GroupArgs{
87 GroupId: 10,
88 Buckets: []*ofp.OfpBucket{
89 {Actions: []*ofp.OfpAction{
90 PopVlan(),
91 Output(1),
92 },
93 },
94 },
95 }
96 group := MkGroupStat(ga)
97 fg.AddGroup(group)
98
99 allGroups = fg.ListGroups()
100 assert.Equal(t, 1, len(allGroups))
101 assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId)
102}
103
104func TestFlowsAndGroups_Copy(t *testing.T) {
105 fg := NewFlowsAndGroups()
106 var fa *FlowArgs
107 var ga *GroupArgs
108
109 fa = &FlowArgs{
110 KV: OfpFlowModArgs{"priority": 500},
111 MatchFields: []*ofp.OfpOxmOfbField{
112 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530113 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700114 },
115 Actions: []*ofp.OfpAction{
116 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 10)),
117 Output(1),
118 },
119 }
Scott Baker6256a342020-02-21 15:36:26 -0800120 flow, err := MkFlowStat(fa)
121 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700122 fg.AddFlow(flow)
123
124 ga = &GroupArgs{
125 GroupId: 10,
126 Buckets: []*ofp.OfpBucket{
127 {Actions: []*ofp.OfpAction{
128 PopVlan(),
129 Output(1),
130 },
131 },
132 },
133 }
134 group := MkGroupStat(ga)
135 fg.AddGroup(group)
136
137 fgCopy := fg.Copy()
138
139 allFlows := fgCopy.ListFlows()
140 assert.Equal(t, 1, len(allFlows))
141 assert.True(t, FlowMatch(flow, allFlows[0]))
142
143 allGroups := fgCopy.ListGroups()
144 assert.Equal(t, 1, len(allGroups))
145 assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId)
146
147 fg = NewFlowsAndGroups()
148 fgCopy = fg.Copy()
149 allFlows = fgCopy.ListFlows()
150 allGroups = fgCopy.ListGroups()
151 assert.Equal(t, 0, len(allFlows))
152 assert.Equal(t, 0, len(allGroups))
153}
154
155func TestFlowsAndGroups_GetFlow(t *testing.T) {
156 fg := NewFlowsAndGroups()
157 var fa1 *FlowArgs
158 var fa2 *FlowArgs
159 var ga *GroupArgs
160
161 fa1 = &FlowArgs{
162 KV: OfpFlowModArgs{"priority": 500},
163 MatchFields: []*ofp.OfpOxmOfbField{
164 InPort(2),
165 Metadata_ofp((1000 << 32) | 1),
166 VlanPcp(0),
167 },
168 Actions: []*ofp.OfpAction{
169 PopVlan(),
170 },
171 }
Scott Baker6256a342020-02-21 15:36:26 -0800172 flow1, err := MkFlowStat(fa1)
173 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700174
175 fa2 = &FlowArgs{
176 KV: OfpFlowModArgs{"priority": 1500},
177 MatchFields: []*ofp.OfpOxmOfbField{
178 InPort(5),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530179 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700180 },
181 Actions: []*ofp.OfpAction{
182 PushVlan(0x8100),
183 SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1000)),
184 SetField(VlanPcp(0)),
185 Output(2),
186 },
187 }
Scott Baker6256a342020-02-21 15:36:26 -0800188 flow2, err := MkFlowStat(fa2)
189 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700190
191 fg.AddFlow(flow1)
192 fg.AddFlow(flow2)
193
194 ga = &GroupArgs{
195 GroupId: 10,
196 Buckets: []*ofp.OfpBucket{
197 {Actions: []*ofp.OfpAction{
198 PopVlan(),
199 Output(1),
200 },
201 },
202 },
203 }
204 group := MkGroupStat(ga)
205 fg.AddGroup(group)
206
207 gf1 := fg.GetFlow(0)
208 assert.True(t, FlowMatch(flow1, gf1))
209
210 gf2 := fg.GetFlow(1)
211 assert.True(t, FlowMatch(flow2, gf2))
212
213 gf3 := fg.GetFlow(2)
214 assert.Nil(t, gf3)
215
216 allFlows := fg.ListFlows()
217 assert.True(t, FlowMatch(flow1, allFlows[0]))
218 assert.True(t, FlowMatch(flow2, allFlows[1]))
219}
220
221func TestFlowsAndGroups_String(t *testing.T) {
222 fg := NewFlowsAndGroups()
223 var fa *FlowArgs
224 var ga *GroupArgs
225
226 str := fg.String()
madhumatigouda374f1b82026-03-17 17:52:04 +0530227 time.Sleep(2 * time.Millisecond) // Added sleep to address potential timing issues
Scott Baker456b8932019-10-24 10:36:39 -0700228 assert.True(t, str == "")
229
230 fa = &FlowArgs{
231 KV: OfpFlowModArgs{"priority": 500},
232 MatchFields: []*ofp.OfpOxmOfbField{
233 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530234 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700235 VlanPcp(0),
236 EthType(0x800),
237 Ipv4Dst(0xe00a0a0a),
238 },
239 Actions: []*ofp.OfpAction{
240 Group(10),
241 },
242 }
Scott Baker6256a342020-02-21 15:36:26 -0800243 flow, err := MkFlowStat(fa)
244 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700245 fg.AddFlow(flow)
246
247 ga = &GroupArgs{
248 GroupId: 10,
249 Buckets: []*ofp.OfpBucket{
250 {Actions: []*ofp.OfpAction{
251 PopVlan(),
252 Output(1),
253 },
254 },
255 },
256 }
257 group := MkGroupStat(ga)
258 fg.AddGroup(group)
259
260 str = fg.String()
madhumatigouda374f1b82026-03-17 17:52:04 +0530261 time.Sleep(2 * time.Millisecond) // Added sleep to address potential timing issues
Abhay Kumar062cda52025-12-23 06:49:37 +0000262 assert.True(t, strings.Contains(str, "id: 11819684229970388353"))
263 assert.True(t, strings.Contains(str, "group_id: 10"))
264 assert.True(t, strings.Contains(str, "oxm_class: OFPXMC_OPENFLOW_BASIC"))
265 assert.True(t, strings.Contains(str, "type: OFPXMT_OFB_VLAN_VID"))
266 assert.True(t, strings.Contains(str, "vlan_vid: 4096"))
267 assert.True(t, strings.Contains(str, "buckets: {"))
Scott Baker456b8932019-10-24 10:36:39 -0700268}
269
270func TestFlowsAndGroups_AddFrom(t *testing.T) {
271 fg := NewFlowsAndGroups()
272 var fa *FlowArgs
273 var ga *GroupArgs
274
275 str := fg.String()
276 assert.True(t, str == "")
277
278 fa = &FlowArgs{
279 KV: OfpFlowModArgs{"priority": 500},
280 MatchFields: []*ofp.OfpOxmOfbField{
281 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530282 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700283 Metadata_ofp(1000),
284 TunnelId(uint64(1)),
285 VlanPcp(0),
286 },
287 Actions: []*ofp.OfpAction{
288 PopVlan(),
289 Output(1),
290 },
291 }
Scott Baker6256a342020-02-21 15:36:26 -0800292 flow, err := MkFlowStat(fa)
293 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700294 fg.AddFlow(flow)
295
296 ga = &GroupArgs{
297 GroupId: 10,
298 Buckets: []*ofp.OfpBucket{
299 {Actions: []*ofp.OfpAction{
300 PopVlan(),
301 Output(1),
302 },
303 },
304 },
305 }
306 group := MkGroupStat(ga)
307 fg.AddGroup(group)
308
309 fg1 := NewFlowsAndGroups()
310 fg1.AddFrom(fg)
311
312 allFlows := fg1.ListFlows()
313 allGroups := fg1.ListGroups()
314 assert.Equal(t, 1, len(allFlows))
315 assert.Equal(t, 1, len(allGroups))
316 assert.True(t, FlowMatch(flow, allFlows[0]))
317 assert.Equal(t, group.Desc.GroupId, allGroups[0].Desc.GroupId)
318}
319
Elia Battiston02d5b642022-02-08 16:50:10 +0100320func TestDeviceRules_FilterRules(t *testing.T) {
321 dr := NewDeviceRules()
322 rules := dr.GetRules()
323 assert.True(t, len(rules) == 0)
324
325 dr.AddFlow("1", nil)
326 dr.AddFlow("2", nil)
327 dr.AddFlow("3", nil)
328 dr.AddFlow("4", nil)
329 rules = dr.GetRules()
330 assert.True(t, len(rules) == 4)
331
332 keep := map[string]string{"1": "1", "3": "3", "5": "5"}
333 result := dr.FilterRules(keep)
334 rules = result.GetRules()
335 assert.True(t, len(rules) == 2)
336
337 _, ok := rules["1"]
338 assert.True(t, ok)
339 _, ok = rules["2"]
340 assert.False(t, ok)
341 _, ok = rules["3"]
342 assert.True(t, ok)
343 _, ok = rules["4"]
344 assert.False(t, ok)
345}
346
347func TestDeviceRules_RemoveRule(t *testing.T) {
348 dr := NewDeviceRules()
349 rules := dr.GetRules()
350 assert.True(t, len(rules) == 0)
351
352 dr.AddFlow("1", nil)
353 dr.AddFlow("2", nil)
354 dr.AddFlow("3", nil)
355 dr.AddFlow("4", nil)
356 rules = dr.GetRules()
357 assert.True(t, len(rules) == 4)
358
359 dr.RemoveRule("3")
360 rules = dr.GetRules()
361 assert.True(t, len(rules) == 3)
362
363 _, ok := rules["1"]
364 assert.True(t, ok)
365 _, ok = rules["2"]
366 assert.True(t, ok)
367 _, ok = rules["3"]
368 assert.False(t, ok)
369 _, ok = rules["4"]
370 assert.True(t, ok)
371
372 dr.RemoveRule("5")
373 rules = dr.GetRules()
374 assert.True(t, len(rules) == 3)
375}
376
Scott Baker456b8932019-10-24 10:36:39 -0700377func TestDeviceRules_AddFlow(t *testing.T) {
378 dr := NewDeviceRules()
379 rules := dr.GetRules()
380 assert.True(t, len(rules) == 0)
381
382 dr.AddFlow("123456", nil)
383 rules = dr.GetRules()
384 assert.True(t, len(rules) == 1)
385 val, ok := rules["123456"]
386 assert.True(t, ok)
387 assert.Equal(t, 0, len(val.ListFlows()))
388 assert.Equal(t, 0, len(val.ListGroups()))
389
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800390 fa := &FlowArgs{
Scott Baker456b8932019-10-24 10:36:39 -0700391 KV: OfpFlowModArgs{"priority": 500},
392 MatchFields: []*ofp.OfpOxmOfbField{
393 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530394 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700395 Metadata_ofp(1000),
396 TunnelId(uint64(1)),
397 VlanPcp(0),
398 },
399 Actions: []*ofp.OfpAction{
400 PopVlan(),
401 Output(1),
402 },
403 }
Scott Baker6256a342020-02-21 15:36:26 -0800404 flow, err := MkFlowStat(fa)
405 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700406 dr.AddFlow("123456", flow)
407 rules = dr.GetRules()
408 assert.True(t, len(rules) == 1)
409 val, ok = rules["123456"]
410 assert.True(t, ok)
411 assert.Equal(t, 1, len(val.ListFlows()))
412 assert.True(t, FlowMatch(flow, val.ListFlows()[0]))
413 assert.Equal(t, 0, len(val.ListGroups()))
414}
415
416func TestDeviceRules_AddFlowsAndGroup(t *testing.T) {
417 fg := NewFlowsAndGroups()
418 var fa *FlowArgs
419 var ga *GroupArgs
420
421 str := fg.String()
422 assert.True(t, str == "")
423
424 fa = &FlowArgs{
425 KV: OfpFlowModArgs{"priority": 2000},
426 MatchFields: []*ofp.OfpOxmOfbField{
427 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530428 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700429 Metadata_ofp(1000),
430 TunnelId(uint64(1)),
431 VlanPcp(0),
432 },
433 Actions: []*ofp.OfpAction{
434 PopVlan(),
435 Output(1),
436 },
437 }
Scott Baker6256a342020-02-21 15:36:26 -0800438 flow, err := MkFlowStat(fa)
439 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700440 fg.AddFlow(flow)
441
442 ga = &GroupArgs{
443 GroupId: 10,
444 Buckets: []*ofp.OfpBucket{
445 {Actions: []*ofp.OfpAction{
446 PopVlan(),
447 Output(1),
448 },
449 },
450 },
451 }
452 group := MkGroupStat(ga)
453 fg.AddGroup(group)
454
455 dr := NewDeviceRules()
456 dr.AddFlowsAndGroup("123456", fg)
457 rules := dr.GetRules()
458 assert.True(t, len(rules) == 1)
459 val, ok := rules["123456"]
460 assert.True(t, ok)
461 assert.Equal(t, 1, len(val.ListFlows()))
462 assert.Equal(t, 1, len(val.ListGroups()))
463 assert.True(t, FlowMatch(flow, val.ListFlows()[0]))
464 assert.Equal(t, 10, int(val.ListGroups()[0].Desc.GroupId))
465}
466
467func TestFlowHasOutPort(t *testing.T) {
468 var flow *ofp.OfpFlowStats
469 assert.False(t, FlowHasOutPort(flow, 1))
470
471 fa := &FlowArgs{
472 KV: OfpFlowModArgs{"priority": 2000},
473 MatchFields: []*ofp.OfpOxmOfbField{
474 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530475 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700476 Metadata_ofp(1000),
477 TunnelId(uint64(1)),
478 VlanPcp(0),
479 },
480 Actions: []*ofp.OfpAction{
481 PopVlan(),
482 Output(1),
483 },
484 }
Scott Baker6256a342020-02-21 15:36:26 -0800485 flow, err := MkFlowStat(fa)
486 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700487 assert.True(t, FlowHasOutPort(flow, 1))
488 assert.False(t, FlowHasOutPort(flow, 2))
489
490 fa = &FlowArgs{
491 KV: OfpFlowModArgs{"priority": 2000},
492 MatchFields: []*ofp.OfpOxmOfbField{
493 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530494 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700495 },
496 }
Scott Baker6256a342020-02-21 15:36:26 -0800497 flow, err = MkFlowStat(fa)
498 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700499 assert.False(t, FlowHasOutPort(flow, 1))
500}
501
502func TestFlowHasOutGroup(t *testing.T) {
503 var flow *ofp.OfpFlowStats
504 assert.False(t, FlowHasOutGroup(flow, 10))
505
506 fa := &FlowArgs{
507 KV: OfpFlowModArgs{"priority": 500},
508 MatchFields: []*ofp.OfpOxmOfbField{
509 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530510 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700511 VlanPcp(0),
512 EthType(0x800),
513 Ipv4Dst(0xe00a0a0a),
514 },
515 Actions: []*ofp.OfpAction{
516 Group(10),
517 },
518 }
Scott Baker6256a342020-02-21 15:36:26 -0800519 flow, err := MkFlowStat(fa)
520 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700521 assert.True(t, FlowHasOutGroup(flow, 10))
522 assert.False(t, FlowHasOutGroup(flow, 11))
523
524 fa = &FlowArgs{
525 KV: OfpFlowModArgs{"priority": 500},
526 MatchFields: []*ofp.OfpOxmOfbField{
527 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530528 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700529 VlanPcp(0),
530 EthType(0x800),
531 Ipv4Dst(0xe00a0a0a),
532 },
533 Actions: []*ofp.OfpAction{
534 Output(1),
535 },
536 }
Scott Baker6256a342020-02-21 15:36:26 -0800537 flow, err = MkFlowStat(fa)
538 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700539 assert.False(t, FlowHasOutGroup(flow, 1))
540}
541
542func TestMatchFlow(t *testing.T) {
543 assert.False(t, FlowMatch(nil, nil))
544 fa := &FlowArgs{
545 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
546 MatchFields: []*ofp.OfpOxmOfbField{
547 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530548 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700549 VlanPcp(0),
550 EthType(0x800),
551 Ipv4Dst(0xe00a0a0a),
552 },
553 Actions: []*ofp.OfpAction{
554 Group(10),
555 },
556 }
Scott Baker6256a342020-02-21 15:36:26 -0800557 flow1, err := MkFlowStat(fa)
558 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700559 assert.False(t, FlowMatch(flow1, nil))
560
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400561 // different table_id, cookie, flags
Scott Baker456b8932019-10-24 10:36:39 -0700562 fa = &FlowArgs{
563 KV: OfpFlowModArgs{"priority": 500},
564 MatchFields: []*ofp.OfpOxmOfbField{
565 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530566 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700567 VlanPcp(0),
568 EthType(0x800),
569 Ipv4Dst(0xe00a0a0a),
570 },
571 Actions: []*ofp.OfpAction{
572 Group(10),
573 },
574 }
Scott Baker6256a342020-02-21 15:36:26 -0800575 flow2, err := MkFlowStat(fa)
576 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700577 assert.False(t, FlowMatch(flow1, flow2))
578 assert.False(t, FlowMatch(nil, flow2))
579
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400580 // no difference
Scott Baker456b8932019-10-24 10:36:39 -0700581 fa = &FlowArgs{
582 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
583 MatchFields: []*ofp.OfpOxmOfbField{
584 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530585 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700586 VlanPcp(0),
587 EthType(0x800),
588 Ipv4Dst(0xe00a0a0a),
589 },
590 }
Scott Baker6256a342020-02-21 15:36:26 -0800591 flow2, err = MkFlowStat(fa)
592 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700593 assert.True(t, FlowMatch(flow1, flow2))
594
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400595 // different priority
Scott Baker456b8932019-10-24 10:36:39 -0700596 fa = &FlowArgs{
597 KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12},
598 MatchFields: []*ofp.OfpOxmOfbField{
599 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530600 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700601 VlanPcp(0),
602 EthType(0x800),
603 Ipv4Dst(0xe00a0a0a),
604 },
605 Actions: []*ofp.OfpAction{
606 Group(10),
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 table id
Scott Baker456b8932019-10-24 10:36:39 -0700614 fa = &FlowArgs{
615 KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12},
616 MatchFields: []*ofp.OfpOxmOfbField{
617 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530618 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700619 VlanPcp(0),
620 EthType(0x800),
621 Ipv4Dst(0xe00a0a0a),
622 },
623 }
Scott Baker6256a342020-02-21 15:36:26 -0800624 flow2, err = MkFlowStat(fa)
625 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700626 assert.False(t, FlowMatch(flow1, flow2))
627
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400628 // different cookie
Scott Baker456b8932019-10-24 10:36:39 -0700629 fa = &FlowArgs{
630 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12},
631 MatchFields: []*ofp.OfpOxmOfbField{
632 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530633 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700634 VlanPcp(0),
635 EthType(0x800),
636 Ipv4Dst(0xe00a0a0a),
637 },
638 }
Scott Baker6256a342020-02-21 15:36:26 -0800639 flow2, err = MkFlowStat(fa)
640 assert.Nil(t, err)
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400641 assert.True(t, FlowMatch(flow1, flow2))
Scott Baker456b8932019-10-24 10:36:39 -0700642
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400643 // different flags
Scott Baker456b8932019-10-24 10:36:39 -0700644 fa = &FlowArgs{
645 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14},
646 MatchFields: []*ofp.OfpOxmOfbField{
647 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530648 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700649 VlanPcp(0),
650 EthType(0x800),
651 Ipv4Dst(0xe00a0a0a),
652 },
653 }
Scott Baker6256a342020-02-21 15:36:26 -0800654 flow2, err = MkFlowStat(fa)
655 assert.Nil(t, err)
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400656 assert.True(t, FlowMatch(flow1, flow2))
Scott Baker456b8932019-10-24 10:36:39 -0700657
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400658 // different match InPort
Scott Baker456b8932019-10-24 10:36:39 -0700659 fa = &FlowArgs{
660 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
661 MatchFields: []*ofp.OfpOxmOfbField{
662 InPort(4),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530663 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700664 VlanPcp(0),
665 EthType(0x800),
666 Ipv4Dst(0xe00a0a0a),
667 },
668 }
Scott Baker6256a342020-02-21 15:36:26 -0800669 flow2, err = MkFlowStat(fa)
670 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700671 assert.False(t, FlowMatch(flow1, flow2))
672
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400673 // different match Ipv4Dst
Scott Baker456b8932019-10-24 10:36:39 -0700674 fa = &FlowArgs{
675 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
676 MatchFields: []*ofp.OfpOxmOfbField{
677 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530678 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700679 VlanPcp(0),
680 EthType(0x800),
681 },
682 }
Scott Baker6256a342020-02-21 15:36:26 -0800683 flow2, err = MkFlowStat(fa)
684 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700685 assert.False(t, FlowMatch(flow1, flow2))
686
Kent Hagermanf9cbe692020-05-05 17:50:26 -0400687 // different actions
Scott Baker456b8932019-10-24 10:36:39 -0700688 fa = &FlowArgs{
689 KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12},
690 MatchFields: []*ofp.OfpOxmOfbField{
691 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530692 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700693 VlanPcp(0),
694 EthType(0x800),
695 Ipv4Dst(0xe00a0a0a),
696 },
697 Actions: []*ofp.OfpAction{
698 PopVlan(),
699 Output(1),
700 },
701 }
Scott Baker6256a342020-02-21 15:36:26 -0800702 flow2, err = MkFlowStat(fa)
703 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700704 assert.True(t, FlowMatch(flow1, flow2))
705}
706
707func TestFlowMatchesMod(t *testing.T) {
708 assert.False(t, FlowMatchesMod(nil, nil))
709 fa := &FlowArgs{
710 KV: OfpFlowModArgs{"priority": 500, "table_id": 1},
711 MatchFields: []*ofp.OfpOxmOfbField{
712 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530713 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700714 VlanPcp(0),
715 EthType(0x800),
716 Ipv4Dst(0xe00a0a0a),
717 },
718 Actions: []*ofp.OfpAction{
719 Output(1),
720 Group(10),
721 },
722 }
Scott Baker6256a342020-02-21 15:36:26 -0800723 flow, err := MkFlowStat(fa)
724 assert.Nil(t, err)
Scott Baker456b8932019-10-24 10:36:39 -0700725 assert.False(t, FlowMatchesMod(flow, nil))
726
727 fa = &FlowArgs{
728 KV: OfpFlowModArgs{"priority": 500, "table_id": 1},
729 MatchFields: []*ofp.OfpOxmOfbField{
730 InPort(2),
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +0530731 VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
Scott Baker456b8932019-10-24 10:36:39 -0700732 VlanPcp(0),
733 EthType(0x800),
734 Ipv4Dst(0xe00a0a0a),
735 },
736 Actions: []*ofp.OfpAction{
737 PopVlan(),
738 Output(1),
739 },
740 }
741 flowMod := MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
742 assert.False(t, FlowMatchesMod(nil, flowMod))
743 assert.False(t, FlowMatchesMod(flow, flowMod))
Scott Baker6256a342020-02-21 15:36:26 -0800744 entry, err := FlowStatsEntryFromFlowModMessage(flowMod)
745 assert.Nil(t, err)
746 assert.True(t, FlowMatch(flow, entry))
Scott Baker456b8932019-10-24 10:36:39 -0700747
748 fa = &FlowArgs{
749 KV: OfpFlowModArgs{"table_id": uint64(ofp.OfpTable_OFPTT_ALL),
750 "cookie_mask": 0,
751 "out_port": uint64(ofp.OfpPortNo_OFPP_ANY),
752 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
753 },
754 }
755 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
756 assert.True(t, FlowMatchesMod(flow, flowMod))
757
758 fa = &FlowArgs{
759 KV: OfpFlowModArgs{"table_id": 1,
760 "cookie_mask": 0,
761 "out_port": uint64(ofp.OfpPortNo_OFPP_ANY),
762 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
763 },
764 }
765 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
766 assert.True(t, FlowMatchesMod(flow, flowMod))
767
768 fa = &FlowArgs{
769 KV: OfpFlowModArgs{"table_id": 1,
770 "cookie_mask": 0,
771 "out_port": 1,
772 "out_group": uint64(ofp.OfpGroup_OFPG_ANY),
773 },
774 }
775 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
776 assert.True(t, FlowMatchesMod(flow, flowMod))
777
778 fa = &FlowArgs{
779 KV: OfpFlowModArgs{"table_id": 1,
780 "cookie_mask": 0,
781 "out_port": 1,
782 "out_group": 10,
783 },
784 }
785 flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV)
786 assert.True(t, FlowMatchesMod(flow, flowMod))
787}
Esin Karaman20b6de92019-11-05 08:29:16 +0000788
789func TestIsMulticastIpAddress(t *testing.T) {
790 isMcastIp := IsMulticastIp(3776315393) //225.22.0.1
791 assert.True(t, isMcastIp)
792 isMcastIp = IsMulticastIp(3232243777) //192.168.32.65
793 assert.True(t, !isMcastIp)
794}
795
796func TestConvertToMulticastMac(t *testing.T) {
797 mcastIp := uint32(4001431809) //238.129.1.1
798 expectedMacInBytes := []byte{1, 0, 94, 1, 1, 1} //01:00:5e:01:01:01
799 macInBytes := ConvertToMulticastMacBytes(mcastIp)
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800800 assert.True(t, bytes.Equal(macInBytes, expectedMacInBytes))
Esin Karaman20b6de92019-11-05 08:29:16 +0000801}