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 | |
| 23 | ofp "github.com/opencord/voltha-protos/v3/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 | |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 40 | func TestHashFlowStatsNil(t *testing.T) { |
| 41 | _, err := HashFlowStats(nil) |
| 42 | assert.EqualError(t, err, "hash-flow-stats-nil-flow") |
| 43 | } |
| 44 | |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 45 | func TestFlowsAndGroups_AddFlow(t *testing.T) { |
| 46 | fg := NewFlowsAndGroups() |
| 47 | allFlows := fg.ListFlows() |
| 48 | assert.Equal(t, 0, len(allFlows)) |
| 49 | fg.AddFlow(nil) |
| 50 | allFlows = fg.ListFlows() |
| 51 | assert.Equal(t, 0, len(allFlows)) |
| 52 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 53 | fa := &FlowArgs{ |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 54 | KV: OfpFlowModArgs{"priority": 500}, |
| 55 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 56 | InPort(1), |
| 57 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1), |
| 58 | TunnelId(uint64(1)), |
| 59 | EthType(0x0800), |
| 60 | Ipv4Dst(0xffffffff), |
| 61 | IpProto(17), |
| 62 | UdpSrc(68), |
| 63 | UdpDst(67), |
| 64 | }, |
| 65 | Actions: []*ofp.OfpAction{ |
| 66 | PushVlan(0x8100), |
| 67 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 4000)), |
| 68 | Output(uint32(ofp.OfpPortNo_OFPP_CONTROLLER)), |
| 69 | }, |
| 70 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 71 | flow, err := MkFlowStat(fa) |
| 72 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 73 | fg.AddFlow(flow) |
| 74 | |
| 75 | allFlows = fg.ListFlows() |
| 76 | assert.Equal(t, 1, len(allFlows)) |
| 77 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 78 | } |
| 79 | |
| 80 | func TestFlowsAndGroups_AddGroup(t *testing.T) { |
| 81 | var ga *GroupArgs |
| 82 | |
| 83 | fg := NewFlowsAndGroups() |
| 84 | allGroups := fg.ListGroups() |
| 85 | assert.Equal(t, 0, len(allGroups)) |
| 86 | fg.AddGroup(nil) |
| 87 | allGroups = fg.ListGroups() |
| 88 | assert.Equal(t, 0, len(allGroups)) |
| 89 | |
| 90 | ga = &GroupArgs{ |
| 91 | GroupId: 10, |
| 92 | Buckets: []*ofp.OfpBucket{ |
| 93 | {Actions: []*ofp.OfpAction{ |
| 94 | PopVlan(), |
| 95 | Output(1), |
| 96 | }, |
| 97 | }, |
| 98 | }, |
| 99 | } |
| 100 | group := MkGroupStat(ga) |
| 101 | fg.AddGroup(group) |
| 102 | |
| 103 | allGroups = fg.ListGroups() |
| 104 | assert.Equal(t, 1, len(allGroups)) |
| 105 | assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId) |
| 106 | } |
| 107 | |
| 108 | func TestFlowsAndGroups_Copy(t *testing.T) { |
| 109 | fg := NewFlowsAndGroups() |
| 110 | var fa *FlowArgs |
| 111 | var ga *GroupArgs |
| 112 | |
| 113 | fa = &FlowArgs{ |
| 114 | KV: OfpFlowModArgs{"priority": 500}, |
| 115 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 116 | InPort(2), |
| 117 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 118 | }, |
| 119 | Actions: []*ofp.OfpAction{ |
| 120 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 10)), |
| 121 | Output(1), |
| 122 | }, |
| 123 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 124 | flow, err := MkFlowStat(fa) |
| 125 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 126 | fg.AddFlow(flow) |
| 127 | |
| 128 | ga = &GroupArgs{ |
| 129 | GroupId: 10, |
| 130 | Buckets: []*ofp.OfpBucket{ |
| 131 | {Actions: []*ofp.OfpAction{ |
| 132 | PopVlan(), |
| 133 | Output(1), |
| 134 | }, |
| 135 | }, |
| 136 | }, |
| 137 | } |
| 138 | group := MkGroupStat(ga) |
| 139 | fg.AddGroup(group) |
| 140 | |
| 141 | fgCopy := fg.Copy() |
| 142 | |
| 143 | allFlows := fgCopy.ListFlows() |
| 144 | assert.Equal(t, 1, len(allFlows)) |
| 145 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 146 | |
| 147 | allGroups := fgCopy.ListGroups() |
| 148 | assert.Equal(t, 1, len(allGroups)) |
| 149 | assert.Equal(t, ga.GroupId, allGroups[0].Desc.GroupId) |
| 150 | |
| 151 | fg = NewFlowsAndGroups() |
| 152 | fgCopy = fg.Copy() |
| 153 | allFlows = fgCopy.ListFlows() |
| 154 | allGroups = fgCopy.ListGroups() |
| 155 | assert.Equal(t, 0, len(allFlows)) |
| 156 | assert.Equal(t, 0, len(allGroups)) |
| 157 | } |
| 158 | |
| 159 | func TestFlowsAndGroups_GetFlow(t *testing.T) { |
| 160 | fg := NewFlowsAndGroups() |
| 161 | var fa1 *FlowArgs |
| 162 | var fa2 *FlowArgs |
| 163 | var ga *GroupArgs |
| 164 | |
| 165 | fa1 = &FlowArgs{ |
| 166 | KV: OfpFlowModArgs{"priority": 500}, |
| 167 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 168 | InPort(2), |
| 169 | Metadata_ofp((1000 << 32) | 1), |
| 170 | VlanPcp(0), |
| 171 | }, |
| 172 | Actions: []*ofp.OfpAction{ |
| 173 | PopVlan(), |
| 174 | }, |
| 175 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 176 | flow1, err := MkFlowStat(fa1) |
| 177 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 178 | |
| 179 | fa2 = &FlowArgs{ |
| 180 | KV: OfpFlowModArgs{"priority": 1500}, |
| 181 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 182 | InPort(5), |
| 183 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 184 | }, |
| 185 | Actions: []*ofp.OfpAction{ |
| 186 | PushVlan(0x8100), |
| 187 | SetField(VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 1000)), |
| 188 | SetField(VlanPcp(0)), |
| 189 | Output(2), |
| 190 | }, |
| 191 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 192 | flow2, err := MkFlowStat(fa2) |
| 193 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 194 | |
| 195 | fg.AddFlow(flow1) |
| 196 | fg.AddFlow(flow2) |
| 197 | |
| 198 | ga = &GroupArgs{ |
| 199 | GroupId: 10, |
| 200 | Buckets: []*ofp.OfpBucket{ |
| 201 | {Actions: []*ofp.OfpAction{ |
| 202 | PopVlan(), |
| 203 | Output(1), |
| 204 | }, |
| 205 | }, |
| 206 | }, |
| 207 | } |
| 208 | group := MkGroupStat(ga) |
| 209 | fg.AddGroup(group) |
| 210 | |
| 211 | gf1 := fg.GetFlow(0) |
| 212 | assert.True(t, FlowMatch(flow1, gf1)) |
| 213 | |
| 214 | gf2 := fg.GetFlow(1) |
| 215 | assert.True(t, FlowMatch(flow2, gf2)) |
| 216 | |
| 217 | gf3 := fg.GetFlow(2) |
| 218 | assert.Nil(t, gf3) |
| 219 | |
| 220 | allFlows := fg.ListFlows() |
| 221 | assert.True(t, FlowMatch(flow1, allFlows[0])) |
| 222 | assert.True(t, FlowMatch(flow2, allFlows[1])) |
| 223 | } |
| 224 | |
| 225 | func TestFlowsAndGroups_String(t *testing.T) { |
| 226 | fg := NewFlowsAndGroups() |
| 227 | var fa *FlowArgs |
| 228 | var ga *GroupArgs |
| 229 | |
| 230 | str := fg.String() |
| 231 | assert.True(t, str == "") |
| 232 | |
| 233 | fa = &FlowArgs{ |
| 234 | KV: OfpFlowModArgs{"priority": 500}, |
| 235 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 236 | InPort(2), |
| 237 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 238 | VlanPcp(0), |
| 239 | EthType(0x800), |
| 240 | Ipv4Dst(0xe00a0a0a), |
| 241 | }, |
| 242 | Actions: []*ofp.OfpAction{ |
| 243 | Group(10), |
| 244 | }, |
| 245 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 246 | flow, err := MkFlowStat(fa) |
| 247 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 248 | fg.AddFlow(flow) |
| 249 | |
| 250 | ga = &GroupArgs{ |
| 251 | GroupId: 10, |
| 252 | Buckets: []*ofp.OfpBucket{ |
| 253 | {Actions: []*ofp.OfpAction{ |
| 254 | PopVlan(), |
| 255 | Output(1), |
| 256 | }, |
| 257 | }, |
| 258 | }, |
| 259 | } |
| 260 | group := MkGroupStat(ga) |
| 261 | fg.AddGroup(group) |
| 262 | |
| 263 | str = fg.String() |
| 264 | assert.True(t, strings.Contains(str, "id: 1143307409938767207")) |
| 265 | assert.True(t, strings.Contains(str, "group_id: 10")) |
| 266 | assert.True(t, strings.Contains(str, "oxm_class: OFPXMC_OPENFLOW_BASICOFPXMC_OPENFLOW_BASIC")) |
| 267 | assert.True(t, strings.Contains(str, "type: OFPXMT_OFB_VLAN_VIDOFPXMT_OFB_VLAN_VID")) |
| 268 | assert.True(t, strings.Contains(str, "vlan_vid: 4096")) |
| 269 | assert.True(t, strings.Contains(str, "buckets:")) |
| 270 | } |
| 271 | |
| 272 | func TestFlowsAndGroups_AddFrom(t *testing.T) { |
| 273 | fg := NewFlowsAndGroups() |
| 274 | var fa *FlowArgs |
| 275 | var ga *GroupArgs |
| 276 | |
| 277 | str := fg.String() |
| 278 | assert.True(t, str == "") |
| 279 | |
| 280 | fa = &FlowArgs{ |
| 281 | KV: OfpFlowModArgs{"priority": 500}, |
| 282 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 283 | InPort(2), |
| 284 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 285 | Metadata_ofp(1000), |
| 286 | TunnelId(uint64(1)), |
| 287 | VlanPcp(0), |
| 288 | }, |
| 289 | Actions: []*ofp.OfpAction{ |
| 290 | PopVlan(), |
| 291 | Output(1), |
| 292 | }, |
| 293 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 294 | flow, err := MkFlowStat(fa) |
| 295 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 296 | fg.AddFlow(flow) |
| 297 | |
| 298 | ga = &GroupArgs{ |
| 299 | GroupId: 10, |
| 300 | Buckets: []*ofp.OfpBucket{ |
| 301 | {Actions: []*ofp.OfpAction{ |
| 302 | PopVlan(), |
| 303 | Output(1), |
| 304 | }, |
| 305 | }, |
| 306 | }, |
| 307 | } |
| 308 | group := MkGroupStat(ga) |
| 309 | fg.AddGroup(group) |
| 310 | |
| 311 | fg1 := NewFlowsAndGroups() |
| 312 | fg1.AddFrom(fg) |
| 313 | |
| 314 | allFlows := fg1.ListFlows() |
| 315 | allGroups := fg1.ListGroups() |
| 316 | assert.Equal(t, 1, len(allFlows)) |
| 317 | assert.Equal(t, 1, len(allGroups)) |
| 318 | assert.True(t, FlowMatch(flow, allFlows[0])) |
| 319 | assert.Equal(t, group.Desc.GroupId, allGroups[0].Desc.GroupId) |
| 320 | } |
| 321 | |
| 322 | func TestDeviceRules_AddFlow(t *testing.T) { |
| 323 | dr := NewDeviceRules() |
| 324 | rules := dr.GetRules() |
| 325 | assert.True(t, len(rules) == 0) |
| 326 | |
| 327 | dr.AddFlow("123456", nil) |
| 328 | rules = dr.GetRules() |
| 329 | assert.True(t, len(rules) == 1) |
| 330 | val, ok := rules["123456"] |
| 331 | assert.True(t, ok) |
| 332 | assert.Equal(t, 0, len(val.ListFlows())) |
| 333 | assert.Equal(t, 0, len(val.ListGroups())) |
| 334 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 335 | fa := &FlowArgs{ |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 336 | KV: OfpFlowModArgs{"priority": 500}, |
| 337 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 338 | InPort(2), |
| 339 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 340 | Metadata_ofp(1000), |
| 341 | TunnelId(uint64(1)), |
| 342 | VlanPcp(0), |
| 343 | }, |
| 344 | Actions: []*ofp.OfpAction{ |
| 345 | PopVlan(), |
| 346 | Output(1), |
| 347 | }, |
| 348 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 349 | flow, err := MkFlowStat(fa) |
| 350 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 351 | dr.AddFlow("123456", flow) |
| 352 | rules = dr.GetRules() |
| 353 | assert.True(t, len(rules) == 1) |
| 354 | val, ok = rules["123456"] |
| 355 | assert.True(t, ok) |
| 356 | assert.Equal(t, 1, len(val.ListFlows())) |
| 357 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 358 | assert.Equal(t, 0, len(val.ListGroups())) |
| 359 | } |
| 360 | |
| 361 | func TestDeviceRules_AddFlowsAndGroup(t *testing.T) { |
| 362 | fg := NewFlowsAndGroups() |
| 363 | var fa *FlowArgs |
| 364 | var ga *GroupArgs |
| 365 | |
| 366 | str := fg.String() |
| 367 | assert.True(t, str == "") |
| 368 | |
| 369 | fa = &FlowArgs{ |
| 370 | KV: OfpFlowModArgs{"priority": 2000}, |
| 371 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 372 | InPort(2), |
| 373 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 374 | Metadata_ofp(1000), |
| 375 | TunnelId(uint64(1)), |
| 376 | VlanPcp(0), |
| 377 | }, |
| 378 | Actions: []*ofp.OfpAction{ |
| 379 | PopVlan(), |
| 380 | Output(1), |
| 381 | }, |
| 382 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 383 | flow, err := MkFlowStat(fa) |
| 384 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 385 | fg.AddFlow(flow) |
| 386 | |
| 387 | ga = &GroupArgs{ |
| 388 | GroupId: 10, |
| 389 | Buckets: []*ofp.OfpBucket{ |
| 390 | {Actions: []*ofp.OfpAction{ |
| 391 | PopVlan(), |
| 392 | Output(1), |
| 393 | }, |
| 394 | }, |
| 395 | }, |
| 396 | } |
| 397 | group := MkGroupStat(ga) |
| 398 | fg.AddGroup(group) |
| 399 | |
| 400 | dr := NewDeviceRules() |
| 401 | dr.AddFlowsAndGroup("123456", fg) |
| 402 | rules := dr.GetRules() |
| 403 | assert.True(t, len(rules) == 1) |
| 404 | val, ok := rules["123456"] |
| 405 | assert.True(t, ok) |
| 406 | assert.Equal(t, 1, len(val.ListFlows())) |
| 407 | assert.Equal(t, 1, len(val.ListGroups())) |
| 408 | assert.True(t, FlowMatch(flow, val.ListFlows()[0])) |
| 409 | assert.Equal(t, 10, int(val.ListGroups()[0].Desc.GroupId)) |
| 410 | } |
| 411 | |
| 412 | func TestFlowHasOutPort(t *testing.T) { |
| 413 | var flow *ofp.OfpFlowStats |
| 414 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 415 | |
| 416 | fa := &FlowArgs{ |
| 417 | KV: OfpFlowModArgs{"priority": 2000}, |
| 418 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 419 | InPort(2), |
| 420 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 421 | Metadata_ofp(1000), |
| 422 | TunnelId(uint64(1)), |
| 423 | VlanPcp(0), |
| 424 | }, |
| 425 | Actions: []*ofp.OfpAction{ |
| 426 | PopVlan(), |
| 427 | Output(1), |
| 428 | }, |
| 429 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 430 | flow, err := MkFlowStat(fa) |
| 431 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 432 | assert.True(t, FlowHasOutPort(flow, 1)) |
| 433 | assert.False(t, FlowHasOutPort(flow, 2)) |
| 434 | |
| 435 | fa = &FlowArgs{ |
| 436 | KV: OfpFlowModArgs{"priority": 2000}, |
| 437 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 438 | InPort(2), |
| 439 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 440 | }, |
| 441 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 442 | flow, err = MkFlowStat(fa) |
| 443 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 444 | assert.False(t, FlowHasOutPort(flow, 1)) |
| 445 | } |
| 446 | |
| 447 | func TestFlowHasOutGroup(t *testing.T) { |
| 448 | var flow *ofp.OfpFlowStats |
| 449 | assert.False(t, FlowHasOutGroup(flow, 10)) |
| 450 | |
| 451 | fa := &FlowArgs{ |
| 452 | KV: OfpFlowModArgs{"priority": 500}, |
| 453 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 454 | InPort(2), |
| 455 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 456 | VlanPcp(0), |
| 457 | EthType(0x800), |
| 458 | Ipv4Dst(0xe00a0a0a), |
| 459 | }, |
| 460 | Actions: []*ofp.OfpAction{ |
| 461 | Group(10), |
| 462 | }, |
| 463 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 464 | flow, err := MkFlowStat(fa) |
| 465 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 466 | assert.True(t, FlowHasOutGroup(flow, 10)) |
| 467 | assert.False(t, FlowHasOutGroup(flow, 11)) |
| 468 | |
| 469 | fa = &FlowArgs{ |
| 470 | KV: OfpFlowModArgs{"priority": 500}, |
| 471 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 472 | InPort(2), |
| 473 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 474 | VlanPcp(0), |
| 475 | EthType(0x800), |
| 476 | Ipv4Dst(0xe00a0a0a), |
| 477 | }, |
| 478 | Actions: []*ofp.OfpAction{ |
| 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.False(t, FlowHasOutGroup(flow, 1)) |
| 485 | } |
| 486 | |
| 487 | func TestMatchFlow(t *testing.T) { |
| 488 | assert.False(t, FlowMatch(nil, nil)) |
| 489 | fa := &FlowArgs{ |
| 490 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 491 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 492 | InPort(2), |
| 493 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 494 | VlanPcp(0), |
| 495 | EthType(0x800), |
| 496 | Ipv4Dst(0xe00a0a0a), |
| 497 | }, |
| 498 | Actions: []*ofp.OfpAction{ |
| 499 | Group(10), |
| 500 | }, |
| 501 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 502 | flow1, err := MkFlowStat(fa) |
| 503 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 504 | assert.False(t, FlowMatch(flow1, nil)) |
| 505 | |
| 506 | fa = &FlowArgs{ |
| 507 | KV: OfpFlowModArgs{"priority": 500}, |
| 508 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 509 | InPort(2), |
| 510 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 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 | flow2, err := MkFlowStat(fa) |
| 520 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 521 | assert.False(t, FlowMatch(flow1, flow2)) |
| 522 | assert.False(t, FlowMatch(nil, flow2)) |
| 523 | |
| 524 | fa = &FlowArgs{ |
| 525 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 526 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 527 | InPort(2), |
| 528 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 529 | VlanPcp(0), |
| 530 | EthType(0x800), |
| 531 | Ipv4Dst(0xe00a0a0a), |
| 532 | }, |
| 533 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 534 | flow2, err = MkFlowStat(fa) |
| 535 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 536 | assert.True(t, FlowMatch(flow1, flow2)) |
| 537 | |
| 538 | fa = &FlowArgs{ |
| 539 | KV: OfpFlowModArgs{"priority": 501, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 540 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 541 | InPort(2), |
| 542 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 543 | VlanPcp(0), |
| 544 | EthType(0x800), |
| 545 | Ipv4Dst(0xe00a0a0a), |
| 546 | }, |
| 547 | Actions: []*ofp.OfpAction{ |
| 548 | Group(10), |
| 549 | }, |
| 550 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 551 | flow2, err = MkFlowStat(fa) |
| 552 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 553 | assert.False(t, FlowMatch(flow1, flow2)) |
| 554 | |
| 555 | fa = &FlowArgs{ |
| 556 | KV: OfpFlowModArgs{"priority": 500, "table_id": 2, "cookie": 38268468, "flags": 12}, |
| 557 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 558 | InPort(2), |
| 559 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 560 | VlanPcp(0), |
| 561 | EthType(0x800), |
| 562 | Ipv4Dst(0xe00a0a0a), |
| 563 | }, |
| 564 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 565 | flow2, err = MkFlowStat(fa) |
| 566 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 567 | assert.False(t, FlowMatch(flow1, flow2)) |
| 568 | |
| 569 | fa = &FlowArgs{ |
| 570 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268467, "flags": 12}, |
| 571 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 572 | InPort(2), |
| 573 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 574 | VlanPcp(0), |
| 575 | EthType(0x800), |
| 576 | Ipv4Dst(0xe00a0a0a), |
| 577 | }, |
| 578 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 579 | flow2, err = MkFlowStat(fa) |
| 580 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 581 | assert.False(t, FlowMatch(flow1, flow2)) |
| 582 | |
| 583 | fa = &FlowArgs{ |
| 584 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 14}, |
| 585 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 586 | InPort(2), |
| 587 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 588 | VlanPcp(0), |
| 589 | EthType(0x800), |
| 590 | Ipv4Dst(0xe00a0a0a), |
| 591 | }, |
| 592 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 593 | flow2, err = MkFlowStat(fa) |
| 594 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 595 | assert.False(t, FlowMatch(flow1, flow2)) |
| 596 | |
| 597 | fa = &FlowArgs{ |
| 598 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 599 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 600 | InPort(4), |
| 601 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 602 | VlanPcp(0), |
| 603 | EthType(0x800), |
| 604 | Ipv4Dst(0xe00a0a0a), |
| 605 | }, |
| 606 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 607 | flow2, err = MkFlowStat(fa) |
| 608 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 609 | assert.False(t, FlowMatch(flow1, flow2)) |
| 610 | |
| 611 | fa = &FlowArgs{ |
| 612 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "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 | }, |
| 619 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 620 | flow2, err = MkFlowStat(fa) |
| 621 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 622 | assert.False(t, FlowMatch(flow1, flow2)) |
| 623 | |
| 624 | fa = &FlowArgs{ |
| 625 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1, "cookie": 38268468, "flags": 12}, |
| 626 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 627 | InPort(2), |
| 628 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 629 | VlanPcp(0), |
| 630 | EthType(0x800), |
| 631 | Ipv4Dst(0xe00a0a0a), |
| 632 | }, |
| 633 | Actions: []*ofp.OfpAction{ |
| 634 | PopVlan(), |
| 635 | Output(1), |
| 636 | }, |
| 637 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 638 | flow2, err = MkFlowStat(fa) |
| 639 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 640 | assert.True(t, FlowMatch(flow1, flow2)) |
| 641 | } |
| 642 | |
| 643 | func TestFlowMatchesMod(t *testing.T) { |
| 644 | assert.False(t, FlowMatchesMod(nil, nil)) |
| 645 | fa := &FlowArgs{ |
| 646 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 647 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 648 | InPort(2), |
| 649 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 650 | VlanPcp(0), |
| 651 | EthType(0x800), |
| 652 | Ipv4Dst(0xe00a0a0a), |
| 653 | }, |
| 654 | Actions: []*ofp.OfpAction{ |
| 655 | Output(1), |
| 656 | Group(10), |
| 657 | }, |
| 658 | } |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 659 | flow, err := MkFlowStat(fa) |
| 660 | assert.Nil(t, err) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 661 | assert.False(t, FlowMatchesMod(flow, nil)) |
| 662 | |
| 663 | fa = &FlowArgs{ |
| 664 | KV: OfpFlowModArgs{"priority": 500, "table_id": 1}, |
| 665 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 666 | InPort(2), |
| 667 | VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0), |
| 668 | VlanPcp(0), |
| 669 | EthType(0x800), |
| 670 | Ipv4Dst(0xe00a0a0a), |
| 671 | }, |
| 672 | Actions: []*ofp.OfpAction{ |
| 673 | PopVlan(), |
| 674 | Output(1), |
| 675 | }, |
| 676 | } |
| 677 | flowMod := MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 678 | assert.False(t, FlowMatchesMod(nil, flowMod)) |
| 679 | assert.False(t, FlowMatchesMod(flow, flowMod)) |
Scott Baker | 6256a34 | 2020-02-21 15:36:26 -0800 | [diff] [blame] | 680 | entry, err := FlowStatsEntryFromFlowModMessage(flowMod) |
| 681 | assert.Nil(t, err) |
| 682 | assert.True(t, FlowMatch(flow, entry)) |
Scott Baker | 456b893 | 2019-10-24 10:36:39 -0700 | [diff] [blame] | 683 | |
| 684 | fa = &FlowArgs{ |
| 685 | KV: OfpFlowModArgs{"table_id": uint64(ofp.OfpTable_OFPTT_ALL), |
| 686 | "cookie_mask": 0, |
| 687 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 688 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 689 | }, |
| 690 | } |
| 691 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 692 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 693 | |
| 694 | fa = &FlowArgs{ |
| 695 | KV: OfpFlowModArgs{"table_id": 1, |
| 696 | "cookie_mask": 0, |
| 697 | "out_port": uint64(ofp.OfpPortNo_OFPP_ANY), |
| 698 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 699 | }, |
| 700 | } |
| 701 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 702 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 703 | |
| 704 | fa = &FlowArgs{ |
| 705 | KV: OfpFlowModArgs{"table_id": 1, |
| 706 | "cookie_mask": 0, |
| 707 | "out_port": 1, |
| 708 | "out_group": uint64(ofp.OfpGroup_OFPG_ANY), |
| 709 | }, |
| 710 | } |
| 711 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 712 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 713 | |
| 714 | fa = &FlowArgs{ |
| 715 | KV: OfpFlowModArgs{"table_id": 1, |
| 716 | "cookie_mask": 0, |
| 717 | "out_port": 1, |
| 718 | "out_group": 10, |
| 719 | }, |
| 720 | } |
| 721 | flowMod = MkSimpleFlowMod(ToOfpOxmField(fa.MatchFields), fa.Actions, fa.Command, fa.KV) |
| 722 | assert.True(t, FlowMatchesMod(flow, flowMod)) |
| 723 | } |
Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 724 | |
| 725 | func TestIsMulticastIpAddress(t *testing.T) { |
| 726 | isMcastIp := IsMulticastIp(3776315393) //225.22.0.1 |
| 727 | assert.True(t, isMcastIp) |
| 728 | isMcastIp = IsMulticastIp(3232243777) //192.168.32.65 |
| 729 | assert.True(t, !isMcastIp) |
| 730 | } |
| 731 | |
| 732 | func TestConvertToMulticastMac(t *testing.T) { |
| 733 | mcastIp := uint32(4001431809) //238.129.1.1 |
| 734 | expectedMacInBytes := []byte{1, 0, 94, 1, 1, 1} //01:00:5e:01:01:01 |
| 735 | macInBytes := ConvertToMulticastMacBytes(mcastIp) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 736 | assert.True(t, bytes.Equal(macInBytes, expectedMacInBytes)) |
Esin Karaman | 20b6de9 | 2019-11-05 08:29:16 +0000 | [diff] [blame] | 737 | } |