Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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" |
| 22 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 23 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
| 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | timeoutError error |
| 31 | taskFailureError error |
| 32 | ) |
| 33 | |
| 34 | func init() { |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 35 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 36 | taskFailureError = status.Error(codes.Internal, "test failure task") |
| 37 | timeoutError = status.Errorf(codes.Aborted, "timeout") |
| 38 | } |
| 39 | |
| 40 | func 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. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 48 | fa := &FlowArgs{ |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 49 | 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 66 | flow, err := MkFlowStat(fa) |
| 67 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 68 | 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 | |
| 75 | func 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 | |
| 103 | func 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 119 | flow, err := MkFlowStat(fa) |
| 120 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 121 | 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 | |
| 154 | func 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 171 | flow1, err := MkFlowStat(fa1) |
| 172 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 173 | |
| 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 187 | flow2, err := MkFlowStat(fa2) |
| 188 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 189 | |
| 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 | |
| 220 | func 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 241 | flow, err := MkFlowStat(fa) |
| 242 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 243 | 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 Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 259 | assert.True(t, strings.Contains(str, "id: 11819684229970388353")) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 260 | assert.True(t, strings.Contains(str, "group_id: 10")) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 261 | assert.True(t, strings.Contains(str, "oxm_class: OFPXMC_OPENFLOW_BASIC")) |
| 262 | assert.True(t, strings.Contains(str, "type: OFPXMT_OFB_VLAN_VID")) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 263 | assert.True(t, strings.Contains(str, "vlan_vid: 4096")) |
| 264 | assert.True(t, strings.Contains(str, "buckets:")) |
| 265 | } |
| 266 | |
| 267 | func 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 Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 289 | flow, err := MkFlowStat(fa) |
| 290 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 291 | 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 | |
Elia Battiston | 02d5b64 | 2022-02-08 16:50:10 +0100 | [diff] [blame] | 317 | func TestDeviceRules_FilterRules(t *testing.T) { |
| 318 | dr := NewDeviceRules() |
| 319 | rules := dr.GetRules() |
| 320 | assert.True(t, len(rules) == 0) |
| 321 | |
| 322 | dr.AddFlow("1", nil) |
| 323 | dr.AddFlow("2", nil) |
| 324 | dr.AddFlow("3", nil) |
| 325 | dr.AddFlow("4", nil) |
| 326 | rules = dr.GetRules() |
| 327 | assert.True(t, len(rules) == 4) |
| 328 | |
| 329 | keep := map[string]string{"1": "1", "3": "3", "5": "5"} |
| 330 | result := dr.FilterRules(keep) |
| 331 | rules = result.GetRules() |
| 332 | assert.True(t, len(rules) == 2) |
| 333 | |
| 334 | _, ok := rules["1"] |
| 335 | assert.True(t, ok) |
| 336 | _, ok = rules["2"] |
| 337 | assert.False(t, ok) |
| 338 | _, ok = rules["3"] |
| 339 | assert.True(t, ok) |
| 340 | _, ok = rules["4"] |
| 341 | assert.False(t, ok) |
| 342 | } |
| 343 | |
| 344 | func TestDeviceRules_RemoveRule(t *testing.T) { |
| 345 | dr := NewDeviceRules() |
| 346 | rules := dr.GetRules() |
| 347 | assert.True(t, len(rules) == 0) |
| 348 | |
| 349 | dr.AddFlow("1", nil) |
| 350 | dr.AddFlow("2", nil) |
| 351 | dr.AddFlow("3", nil) |
| 352 | dr.AddFlow("4", nil) |
| 353 | rules = dr.GetRules() |
| 354 | assert.True(t, len(rules) == 4) |
| 355 | |
| 356 | dr.RemoveRule("3") |
| 357 | rules = dr.GetRules() |
| 358 | assert.True(t, len(rules) == 3) |
| 359 | |
| 360 | _, ok := rules["1"] |
| 361 | assert.True(t, ok) |
| 362 | _, ok = rules["2"] |
| 363 | assert.True(t, ok) |
| 364 | _, ok = rules["3"] |
| 365 | assert.False(t, ok) |
| 366 | _, ok = rules["4"] |
| 367 | assert.True(t, ok) |
| 368 | |
| 369 | dr.RemoveRule("5") |
| 370 | rules = dr.GetRules() |
| 371 | assert.True(t, len(rules) == 3) |
| 372 | } |
| 373 | |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 374 | func TestDeviceRules_AddFlow(t *testing.T) { |
| 375 | dr := NewDeviceRules() |
| 376 | rules := dr.GetRules() |
| 377 | assert.True(t, len(rules) == 0) |
| 378 | |
| 379 | dr.AddFlow("123456", nil) |
| 380 | rules = dr.GetRules() |
| 381 | assert.True(t, len(rules) == 1) |
| 382 | val, ok := rules["123456"] |
| 383 | assert.True(t, ok) |
| 384 | assert.Equal(t, 0, len(val.ListFlows())) |
| 385 | assert.Equal(t, 0, len(val.ListGroups())) |
| 386 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 387 | fa := &FlowArgs{ |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 388 | KV: OfpFlowModArgs{"priority": 500}, |
| 389 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 390 | InPort(2), |
| 391 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 392 | Metadata_ofp(1000), |
| 393 | TunnelId(uint64(1)), |
| 394 | VlanPcp(0), |
| 395 | }, |
| 396 | Actions: []*ofp.OfpAction{ |
| 397 | PopVlan(), |
| 398 | Output(1), |
| 399 | }, |
| 400 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 401 | flow, err := MkFlowStat(fa) |
| 402 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 403 | dr.AddFlow("123456", flow) |
| 404 | rules = dr.GetRules() |
| 405 | assert.True(t, len(rules) == 1) |
| 406 | val, ok = rules["123456"] |
| 407 | assert.True(t, ok) |
| 408 | assert.Equal(t, 1, len(val.ListFlows())) |
| 409 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 410 | assert.Equal(t, 0, len(val.ListGroups())) |
| 411 | } |
| 412 | |
| 413 | func TestDeviceRules_AddFlowsAndGroup(t *testing.T) { |
| 414 | fg := NewFlowsAndGroups() |
| 415 | var fa *FlowArgs |
| 416 | var ga *GroupArgs |
| 417 | |
| 418 | str := fg.String() |
| 419 | assert.True(t, str == "") |
| 420 | |
| 421 | fa = &FlowArgs{ |
| 422 | KV: OfpFlowModArgs{"priority": 2000}, |
| 423 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 424 | InPort(2), |
| 425 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 426 | Metadata_ofp(1000), |
| 427 | TunnelId(uint64(1)), |
| 428 | VlanPcp(0), |
| 429 | }, |
| 430 | Actions: []*ofp.OfpAction{ |
| 431 | PopVlan(), |
| 432 | Output(1), |
| 433 | }, |
| 434 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 435 | flow, err := MkFlowStat(fa) |
| 436 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 437 | fg.AddFlow(flow) |
| 438 | |
| 439 | ga = &GroupArgs{ |
| 440 | GroupId: 10, |
| 441 | Buckets: []*ofp.OfpBucket{ |
| 442 | {Actions: []*ofp.OfpAction{ |
| 443 | PopVlan(), |
| 444 | Output(1), |
| 445 | }, |
| 446 | }, |
| 447 | }, |
| 448 | } |
| 449 | group := MkGroupStat(ga) |
| 450 | fg.AddGroup(group) |
| 451 | |
| 452 | dr := NewDeviceRules() |
| 453 | dr.AddFlowsAndGroup("123456", fg) |
| 454 | rules := dr.GetRules() |
| 455 | assert.True(t, len(rules) == 1) |
| 456 | val, ok := rules["123456"] |
| 457 | assert.True(t, ok) |
| 458 | assert.Equal(t, 1, len(val.ListFlows())) |
| 459 | assert.Equal(t, 1, len(val.ListGroups())) |
| 460 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 461 | assert.Equal(t, 10, int(val.ListGroups()[0].Desc.GroupId)) |
| 462 | } |
| 463 | |
| 464 | func TestFlowHasOutPort(t *testing.T) { |
| 465 | var flow *ofp.OfpFlowStats |
| 466 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 467 | |
| 468 | fa := &FlowArgs{ |
| 469 | KV: OfpFlowModArgs{"priority": 2000}, |
| 470 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 471 | InPort(2), |
| 472 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 473 | Metadata_ofp(1000), |
| 474 | TunnelId(uint64(1)), |
| 475 | VlanPcp(0), |
| 476 | }, |
| 477 | Actions: []*ofp.OfpAction{ |
| 478 | PopVlan(), |
| 479 | Output(1), |
| 480 | }, |
| 481 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 482 | flow, err := MkFlowStat(fa) |
| 483 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 484 | assert.True(t, FlowHasOutPort(flow, 1)) |
| 485 | assert.False(t, FlowHasOutPort(flow, 2)) |
| 486 | |
| 487 | fa = &FlowArgs{ |
| 488 | KV: OfpFlowModArgs{"priority": 2000}, |
| 489 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 490 | InPort(2), |
| 491 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 492 | }, |
| 493 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 494 | flow, err = MkFlowStat(fa) |
| 495 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 496 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 497 | } |
| 498 | |
| 499 | func TestFlowHasOutGroup(t *testing.T) { |
| 500 | var flow *ofp.OfpFlowStats |
| 501 | assert.False(t, FlowHasOutGroup(flow, 10)) |
| 502 | |
| 503 | fa := &FlowArgs{ |
| 504 | KV: OfpFlowModArgs{"priority": 500}, |
| 505 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 506 | InPort(2), |
| 507 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 508 | VlanPcp(0), |
| 509 | EthType(0x800), |
| 510 | Ipv4Dst(0xe00a0a0a), |
| 511 | }, |
| 512 | Actions: []*ofp.OfpAction{ |
| 513 | Group(10), |
| 514 | }, |
| 515 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 516 | flow, err := MkFlowStat(fa) |
| 517 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 518 | assert.True(t, FlowHasOutGroup(flow, 10)) |
| 519 | assert.False(t, FlowHasOutGroup(flow, 11)) |
| 520 | |
| 521 | fa = &FlowArgs{ |
| 522 | KV: OfpFlowModArgs{"priority": 500}, |
| 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 | Actions: []*ofp.OfpAction{ |
| 531 | Output(1), |
| 532 | }, |
| 533 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 534 | flow, err = MkFlowStat(fa) |
| 535 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 536 | assert.False(t, FlowHasOutGroup(flow, 1)) |
| 537 | } |
| 538 | |
| 539 | func TestMatchFlow(t *testing.T) { |
| 540 | assert.False(t, FlowMatch(nil, nil)) |
| 541 | fa := &FlowArgs{ |
| 542 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 543 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 544 | InPort(2), |
| 545 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 546 | VlanPcp(0), |
| 547 | EthType(0x800), |
| 548 | Ipv4Dst(0xe00a0a0a), |
| 549 | }, |
| 550 | Actions: []*ofp.OfpAction{ |
| 551 | Group(10), |
| 552 | }, |
| 553 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 554 | flow1, err := MkFlowStat(fa) |
| 555 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 556 | assert.False(t, FlowMatch(flow1, nil)) |
| 557 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 558 | // different table_id, cookie, flags |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 559 | fa = &FlowArgs{ |
| 560 | KV: OfpFlowModArgs{"priority": 500}, |
| 561 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 562 | InPort(2), |
| 563 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 564 | VlanPcp(0), |
| 565 | EthType(0x800), |
| 566 | Ipv4Dst(0xe00a0a0a), |
| 567 | }, |
| 568 | Actions: []*ofp.OfpAction{ |
| 569 | Group(10), |
| 570 | }, |
| 571 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 572 | flow2, err := MkFlowStat(fa) |
| 573 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 574 | assert.False(t, FlowMatch(flow1, flow2)) |
| 575 | assert.False(t, FlowMatch(nil, flow2)) |
| 576 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 577 | // no difference |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 578 | fa = &FlowArgs{ |
| 579 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 580 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 581 | InPort(2), |
| 582 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 583 | VlanPcp(0), |
| 584 | EthType(0x800), |
| 585 | Ipv4Dst(0xe00a0a0a), |
| 586 | }, |
| 587 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 588 | flow2, err = MkFlowStat(fa) |
| 589 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 590 | assert.True(t, FlowMatch(flow1, flow2)) |
| 591 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 592 | // different priority |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 593 | fa = &FlowArgs{ |
| 594 | KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 595 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 596 | InPort(2), |
| 597 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 598 | VlanPcp(0), |
| 599 | EthType(0x800), |
| 600 | Ipv4Dst(0xe00a0a0a), |
| 601 | }, |
| 602 | Actions: []*ofp.OfpAction{ |
| 603 | Group(10), |
| 604 | }, |
| 605 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 606 | flow2, err = MkFlowStat(fa) |
| 607 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 608 | assert.False(t, FlowMatch(flow1, flow2)) |
| 609 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 610 | // different table id |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 611 | fa = &FlowArgs{ |
| 612 | KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12}, |
| 613 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 614 | InPort(2), |
| 615 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 616 | VlanPcp(0), |
| 617 | EthType(0x800), |
| 618 | Ipv4Dst(0xe00a0a0a), |
| 619 | }, |
| 620 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 621 | flow2, err = MkFlowStat(fa) |
| 622 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 623 | assert.False(t, FlowMatch(flow1, flow2)) |
| 624 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 625 | // different cookie |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 626 | fa = &FlowArgs{ |
| 627 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12}, |
| 628 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 629 | InPort(2), |
| 630 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 631 | VlanPcp(0), |
| 632 | EthType(0x800), |
| 633 | Ipv4Dst(0xe00a0a0a), |
| 634 | }, |
| 635 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 636 | flow2, err = MkFlowStat(fa) |
| 637 | assert.Nil(t, err) |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 638 | assert.True(t, FlowMatch(flow1, flow2)) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 639 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 640 | // different flags |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 641 | fa = &FlowArgs{ |
| 642 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14}, |
| 643 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 644 | InPort(2), |
| 645 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 646 | VlanPcp(0), |
| 647 | EthType(0x800), |
| 648 | Ipv4Dst(0xe00a0a0a), |
| 649 | }, |
| 650 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 651 | flow2, err = MkFlowStat(fa) |
| 652 | assert.Nil(t, err) |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 653 | assert.True(t, FlowMatch(flow1, flow2)) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 654 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 655 | // different match InPort |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 656 | fa = &FlowArgs{ |
| 657 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 658 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 659 | InPort(4), |
| 660 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 661 | VlanPcp(0), |
| 662 | EthType(0x800), |
| 663 | Ipv4Dst(0xe00a0a0a), |
| 664 | }, |
| 665 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 666 | flow2, err = MkFlowStat(fa) |
| 667 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 668 | assert.False(t, FlowMatch(flow1, flow2)) |
| 669 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 670 | // different match Ipv4Dst |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 671 | fa = &FlowArgs{ |
| 672 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 673 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 674 | InPort(2), |
| 675 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 676 | VlanPcp(0), |
| 677 | EthType(0x800), |
| 678 | }, |
| 679 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 680 | flow2, err = MkFlowStat(fa) |
| 681 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 682 | assert.False(t, FlowMatch(flow1, flow2)) |
| 683 | |
Kent Hagerman | f9cbe69 | 2020-05-05 17:50:26 -0400 | [diff] [blame] | 684 | // different actions |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 685 | fa = &FlowArgs{ |
| 686 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 687 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 688 | InPort(2), |
| 689 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 690 | VlanPcp(0), |
| 691 | EthType(0x800), |
| 692 | Ipv4Dst(0xe00a0a0a), |
| 693 | }, |
| 694 | Actions: []*ofp.OfpAction{ |
| 695 | PopVlan(), |
| 696 | Output(1), |
| 697 | }, |
| 698 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 699 | flow2, err = MkFlowStat(fa) |
| 700 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 701 | assert.True(t, FlowMatch(flow1, flow2)) |
| 702 | } |
| 703 | |
| 704 | func TestFlowMatchesMod(t *testing.T) { |
| 705 | assert.False(t, FlowMatchesMod(nil, nil)) |
| 706 | fa := &FlowArgs{ |
| 707 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 708 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 709 | InPort(2), |
| 710 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 711 | VlanPcp(0), |
| 712 | EthType(0x800), |
| 713 | Ipv4Dst(0xe00a0a0a), |
| 714 | }, |
| 715 | Actions: []*ofp.OfpAction{ |
| 716 | Output(1), |
| 717 | Group(10), |
| 718 | }, |
| 719 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 720 | flow, err := MkFlowStat(fa) |
| 721 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 722 | assert.False(t, FlowMatchesMod(flow, nil)) |
| 723 | |
| 724 | fa = &FlowArgs{ |
| 725 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 726 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 727 | InPort(2), |
| 728 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 729 | VlanPcp(0), |
| 730 | EthType(0x800), |
| 731 | Ipv4Dst(0xe00a0a0a), |
| 732 | }, |
| 733 | Actions: []*ofp.OfpAction{ |
| 734 | PopVlan(), |
| 735 | Output(1), |
| 736 | }, |
| 737 | } |
| 738 | flowMod := MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 739 | assert.False(t, FlowMatchesMod(nil, flowMod)) |
| 740 | assert.False(t, FlowMatchesMod(flow, flowMod)) |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 741 | entry, err := FlowStatsEntryFromFlowModMessage(flowMod) |
| 742 | assert.Nil(t, err) |
| 743 | assert.True(t, FlowMatch(flow, entry)) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 744 | |
| 745 | fa = &FlowArgs{ |
| 746 | KV: OfpFlowModArgs{"table_id": uint64(ofp.OfpTable_OFPTT_ALL), |
| 747 | "cookie_mask": 0, |
| 748 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 749 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 750 | }, |
| 751 | } |
| 752 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 753 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 754 | |
| 755 | fa = &FlowArgs{ |
| 756 | KV: OfpFlowModArgs{"table_id": 1, |
| 757 | "cookie_mask": 0, |
| 758 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 759 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 760 | }, |
| 761 | } |
| 762 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 763 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 764 | |
| 765 | fa = &FlowArgs{ |
| 766 | KV: OfpFlowModArgs{"table_id": 1, |
| 767 | "cookie_mask": 0, |
| 768 | "out_port": 1, |
| 769 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 770 | }, |
| 771 | } |
| 772 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 773 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 774 | |
| 775 | fa = &FlowArgs{ |
| 776 | KV: OfpFlowModArgs{"table_id": 1, |
| 777 | "cookie_mask": 0, |
| 778 | "out_port": 1, |
| 779 | "out_group": 10, |
| 780 | }, |
| 781 | } |
| 782 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 783 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 784 | } |
Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 785 | |
| 786 | func TestIsMulticastIpAddress(t *testing.T) { |
| 787 | isMcastIp := IsMulticastIp(3776315393) //225.22.0.1 |
| 788 | assert.True(t, isMcastIp) |
| 789 | isMcastIp = IsMulticastIp(3232243777) //192.168.32.65 |
| 790 | assert.True(t, !isMcastIp) |
| 791 | } |
| 792 | |
| 793 | func TestConvertToMulticastMac(t *testing.T) { |
| 794 | mcastIp := uint32(4001431809) //238.129.1.1 |
| 795 | expectedMacInBytes := []byte{1, 0, 94, 1, 1, 1} //01:00:5e:01:01:01 |
| 796 | macInBytes := ConvertToMulticastMacBytes(mcastIp) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 797 | assert.True(t, bytes.Equal(macInBytes, expectedMacInBytes)) |
Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 798 | } |