| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 1 | /* |
| Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 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 | */ |
| 16 | package flows |
| 17 | |
| 18 | import ( |
| Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 19 | "bytes" |
| serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | "testing" |
| madhumatigouda | 374f1b8 | 2026-03-17 17:52:04 +0530 | [diff] [blame^] | 22 | "time" |
| serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 23 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 24 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 25 | "github.com/stretchr/testify/assert" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | timeoutError error |
| 32 | taskFailureError error |
| 33 | ) |
| 34 | |
| 35 | func init() { |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 36 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 37 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 38 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 39 | } |
| 40 | |
| 41 | func 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. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 49 | fa := &FlowArgs{ |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 50 | 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 67 | flow, err := MkFlowStat(fa) |
| 68 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 69 | 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 | |
| 76 | func 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 | |
| 104 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 113 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 114 | }, |
| 115 | Actions: []*ofp.OfpAction{ |
| 116 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 10)), |
| 117 | Output(1), |
| 118 | }, |
| 119 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 120 | flow, err := MkFlowStat(fa) |
| 121 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 122 | 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 | |
| 155 | func 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 172 | flow1, err := MkFlowStat(fa1) |
| 173 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 174 | |
| 175 | fa2 = &FlowArgs{ |
| 176 | KV: OfpFlowModArgs{"priority": 1500}, |
| 177 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 178 | InPort(5), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 179 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 180 | }, |
| 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 188 | flow2, err := MkFlowStat(fa2) |
| 189 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 190 | |
| 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 | |
| 221 | func TestFlowsAndGroups_String(t *testing.T) { |
| 222 | fg := NewFlowsAndGroups() |
| 223 | var fa *FlowArgs |
| 224 | var ga *GroupArgs |
| 225 | |
| 226 | str := fg.String() |
| madhumatigouda | 374f1b8 | 2026-03-17 17:52:04 +0530 | [diff] [blame^] | 227 | time.Sleep(2 * time.Millisecond) // Added sleep to address potential timing issues |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 228 | assert.True(t, str == "") |
| 229 | |
| 230 | fa = &FlowArgs{ |
| 231 | KV: OfpFlowModArgs{"priority": 500}, |
| 232 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 233 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 234 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 235 | VlanPcp(0), |
| 236 | EthType(0x800), |
| 237 | Ipv4Dst(0xe00a0a0a), |
| 238 | }, |
| 239 | Actions: []*ofp.OfpAction{ |
| 240 | Group(10), |
| 241 | }, |
| 242 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 243 | flow, err := MkFlowStat(fa) |
| 244 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 245 | 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() |
| madhumatigouda | 374f1b8 | 2026-03-17 17:52:04 +0530 | [diff] [blame^] | 261 | time.Sleep(2 * time.Millisecond) // Added sleep to address potential timing issues |
| Abhay Kumar | 062cda5 | 2025-12-23 06:49:37 +0000 | [diff] [blame] | 262 | 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 Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 282 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 283 | Metadata_ofp(1000), |
| 284 | TunnelId(uint64(1)), |
| 285 | VlanPcp(0), |
| 286 | }, |
| 287 | Actions: []*ofp.OfpAction{ |
| 288 | PopVlan(), |
| 289 | Output(1), |
| 290 | }, |
| 291 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 292 | flow, err := MkFlowStat(fa) |
| 293 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 294 | 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 Battiston | 02d5b64 | 2022-02-08 16:50:10 +0100 | [diff] [blame] | 320 | func 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 | |
| 347 | func 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 Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 377 | func 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. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 390 | fa := &FlowArgs{ |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 391 | KV: OfpFlowModArgs{"priority": 500}, |
| 392 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 393 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 394 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 395 | Metadata_ofp(1000), |
| 396 | TunnelId(uint64(1)), |
| 397 | VlanPcp(0), |
| 398 | }, |
| 399 | Actions: []*ofp.OfpAction{ |
| 400 | PopVlan(), |
| 401 | Output(1), |
| 402 | }, |
| 403 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 404 | flow, err := MkFlowStat(fa) |
| 405 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 406 | 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 | |
| 416 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 428 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 429 | Metadata_ofp(1000), |
| 430 | TunnelId(uint64(1)), |
| 431 | VlanPcp(0), |
| 432 | }, |
| 433 | Actions: []*ofp.OfpAction{ |
| 434 | PopVlan(), |
| 435 | Output(1), |
| 436 | }, |
| 437 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 438 | flow, err := MkFlowStat(fa) |
| 439 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 440 | 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 | |
| 467 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 475 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 476 | Metadata_ofp(1000), |
| 477 | TunnelId(uint64(1)), |
| 478 | VlanPcp(0), |
| 479 | }, |
| 480 | Actions: []*ofp.OfpAction{ |
| 481 | PopVlan(), |
| 482 | Output(1), |
| 483 | }, |
| 484 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 485 | flow, err := MkFlowStat(fa) |
| 486 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 487 | 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 494 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 495 | }, |
| 496 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 497 | flow, err = MkFlowStat(fa) |
| 498 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 499 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 500 | } |
| 501 | |
| 502 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 510 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 511 | VlanPcp(0), |
| 512 | EthType(0x800), |
| 513 | Ipv4Dst(0xe00a0a0a), |
| 514 | }, |
| 515 | Actions: []*ofp.OfpAction{ |
| 516 | Group(10), |
| 517 | }, |
| 518 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 519 | flow, err := MkFlowStat(fa) |
| 520 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 521 | 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 528 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 529 | VlanPcp(0), |
| 530 | EthType(0x800), |
| 531 | Ipv4Dst(0xe00a0a0a), |
| 532 | }, |
| 533 | Actions: []*ofp.OfpAction{ |
| 534 | Output(1), |
| 535 | }, |
| 536 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 537 | flow, err = MkFlowStat(fa) |
| 538 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 539 | assert.False(t, FlowHasOutGroup(flow, 1)) |
| 540 | } |
| 541 | |
| 542 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 548 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 549 | VlanPcp(0), |
| 550 | EthType(0x800), |
| 551 | Ipv4Dst(0xe00a0a0a), |
| 552 | }, |
| 553 | Actions: []*ofp.OfpAction{ |
| 554 | Group(10), |
| 555 | }, |
| 556 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 557 | flow1, err := MkFlowStat(fa) |
| 558 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 559 | assert.False(t, FlowMatch(flow1, nil)) |
| 560 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 561 | // different table_id, cookie, flags |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 562 | fa = &FlowArgs{ |
| 563 | KV: OfpFlowModArgs{"priority": 500}, |
| 564 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 565 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 566 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 567 | VlanPcp(0), |
| 568 | EthType(0x800), |
| 569 | Ipv4Dst(0xe00a0a0a), |
| 570 | }, |
| 571 | Actions: []*ofp.OfpAction{ |
| 572 | Group(10), |
| 573 | }, |
| 574 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 575 | flow2, err := MkFlowStat(fa) |
| 576 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 577 | assert.False(t, FlowMatch(flow1, flow2)) |
| 578 | assert.False(t, FlowMatch(nil, flow2)) |
| 579 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 580 | // no difference |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 581 | fa = &FlowArgs{ |
| 582 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 583 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 584 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 585 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 586 | VlanPcp(0), |
| 587 | EthType(0x800), |
| 588 | Ipv4Dst(0xe00a0a0a), |
| 589 | }, |
| 590 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 591 | flow2, err = MkFlowStat(fa) |
| 592 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 593 | assert.True(t, FlowMatch(flow1, flow2)) |
| 594 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 595 | // different priority |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 596 | fa = &FlowArgs{ |
| 597 | KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 598 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 599 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 600 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 601 | VlanPcp(0), |
| 602 | EthType(0x800), |
| 603 | Ipv4Dst(0xe00a0a0a), |
| 604 | }, |
| 605 | Actions: []*ofp.OfpAction{ |
| 606 | Group(10), |
| 607 | }, |
| 608 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 609 | flow2, err = MkFlowStat(fa) |
| 610 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 611 | assert.False(t, FlowMatch(flow1, flow2)) |
| 612 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 613 | // different table id |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 614 | fa = &FlowArgs{ |
| 615 | KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12}, |
| 616 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 617 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 618 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 619 | VlanPcp(0), |
| 620 | EthType(0x800), |
| 621 | Ipv4Dst(0xe00a0a0a), |
| 622 | }, |
| 623 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 624 | flow2, err = MkFlowStat(fa) |
| 625 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 626 | assert.False(t, FlowMatch(flow1, flow2)) |
| 627 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 628 | // different cookie |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 629 | fa = &FlowArgs{ |
| 630 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12}, |
| 631 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 632 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 633 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 634 | VlanPcp(0), |
| 635 | EthType(0x800), |
| 636 | Ipv4Dst(0xe00a0a0a), |
| 637 | }, |
| 638 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 639 | flow2, err = MkFlowStat(fa) |
| 640 | assert.Nil(t, err) |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 641 | assert.True(t, FlowMatch(flow1, flow2)) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 642 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 643 | // different flags |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 644 | fa = &FlowArgs{ |
| 645 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14}, |
| 646 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 647 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 648 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 649 | VlanPcp(0), |
| 650 | EthType(0x800), |
| 651 | Ipv4Dst(0xe00a0a0a), |
| 652 | }, |
| 653 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 654 | flow2, err = MkFlowStat(fa) |
| 655 | assert.Nil(t, err) |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 656 | assert.True(t, FlowMatch(flow1, flow2)) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 657 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 658 | // different match InPort |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 659 | fa = &FlowArgs{ |
| 660 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 661 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 662 | InPort(4), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 663 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 664 | VlanPcp(0), |
| 665 | EthType(0x800), |
| 666 | Ipv4Dst(0xe00a0a0a), |
| 667 | }, |
| 668 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 669 | flow2, err = MkFlowStat(fa) |
| 670 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 671 | assert.False(t, FlowMatch(flow1, flow2)) |
| 672 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 673 | // different match Ipv4Dst |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 674 | fa = &FlowArgs{ |
| 675 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 676 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 677 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 678 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 679 | VlanPcp(0), |
| 680 | EthType(0x800), |
| 681 | }, |
| 682 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 683 | flow2, err = MkFlowStat(fa) |
| 684 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 685 | assert.False(t, FlowMatch(flow1, flow2)) |
| 686 | |
| Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 687 | // different actions |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 688 | fa = &FlowArgs{ |
| 689 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 690 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 691 | InPort(2), |
| Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 692 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 693 | VlanPcp(0), |
| 694 | EthType(0x800), |
| 695 | Ipv4Dst(0xe00a0a0a), |
| 696 | }, |
| 697 | Actions: []*ofp.OfpAction{ |
| 698 | PopVlan(), |
| 699 | Output(1), |
| 700 | }, |
| 701 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 702 | flow2, err = MkFlowStat(fa) |
| 703 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 704 | assert.True(t, FlowMatch(flow1, flow2)) |
| 705 | } |
| 706 | |
| 707 | func 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 713 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 714 | VlanPcp(0), |
| 715 | EthType(0x800), |
| 716 | Ipv4Dst(0xe00a0a0a), |
| 717 | }, |
| 718 | Actions: []*ofp.OfpAction{ |
| 719 | Output(1), |
| 720 | Group(10), |
| 721 | }, |
| 722 | } |
| Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 723 | flow, err := MkFlowStat(fa) |
| 724 | assert.Nil(t, err) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 725 | 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 Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame] | 731 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)), |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 732 | 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 744 | entry, err := FlowStatsEntryFromFlowModMessage(flowMod) |
| 745 | assert.Nil(t, err) |
| 746 | assert.True(t, FlowMatch(flow, entry)) |
| Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 747 | |
| 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 Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 788 | |
| 789 | func 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 | |
| 796 | func 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. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 800 | assert.True(t, bytes.Equal(macInBytes, expectedMacInBytes)) |
| Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 801 | } |