|
| 1 | +// Copyright 2023 Antrea Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package openflow |
| 16 | + |
| 17 | +import ( |
| 18 | + "net" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "antrea.io/libOpenflow/openflow15" |
| 22 | + "antrea.io/libOpenflow/util" |
| 23 | + "antrea.io/ofnet/ofctrl" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func getGroupMod(t *testing.T, g Group) *openflow15.GroupMod { |
| 28 | + msgs, err := g.GetBundleMessages(AddMessage) |
| 29 | + assert.NoError(t, err) |
| 30 | + assert.Equal(t, 1, len(msgs)) |
| 31 | + return msgs[0].GetMessage().(*openflow15.GroupMod) |
| 32 | +} |
| 33 | + |
| 34 | +func TestBucketBuilder(t *testing.T) { |
| 35 | + t.Run("Weight", func(t *testing.T) { |
| 36 | + weight := uint16(100) |
| 37 | + g := &ofGroup{ofctrl: &ofctrl.Group{}} |
| 38 | + group := g.Bucket().Weight(weight).Done() |
| 39 | + groupMod := getGroupMod(t, group) |
| 40 | + assert.Equal(t, 1, len(groupMod.Buckets)) |
| 41 | + assert.Equal(t, 1, len(groupMod.Buckets[0].Properties)) |
| 42 | + assert.Equal(t, weight, groupMod.Buckets[0].Properties[0].(*openflow15.GroupBucketPropWeight).Weight) |
| 43 | + assert.Contains(t, GroupModToString(groupMod), "weight:100") |
| 44 | + }) |
| 45 | + t.Run("LoadToRegField", func(t *testing.T) { |
| 46 | + testCases := []struct { |
| 47 | + regField *RegField |
| 48 | + value uint32 |
| 49 | + expected *openflow15.ActionSetField |
| 50 | + expectedActionStr string |
| 51 | + }{ |
| 52 | + { |
| 53 | + regField: NewRegField(1, 0, 31), |
| 54 | + value: uint32(0xffff_ffff), |
| 55 | + expected: &openflow15.ActionSetField{ |
| 56 | + Field: openflow15.MatchField{ |
| 57 | + Class: openflow15.OXM_CLASS_NXM_1, |
| 58 | + Field: openflow15.NXM_NX_REG1, |
| 59 | + Value: &openflow15.Uint32Message{Data: uint32(0xffff_ffff)}, |
| 60 | + Mask: &openflow15.Uint32Message{Data: uint32(0xffff_ffff)}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + expectedActionStr: "set_field:0xffffffff->reg1", |
| 64 | + }, |
| 65 | + { |
| 66 | + regField: NewRegField(1, 4, 15), |
| 67 | + value: uint32(0xf), |
| 68 | + expected: &openflow15.ActionSetField{ |
| 69 | + Field: openflow15.MatchField{ |
| 70 | + Class: openflow15.OXM_CLASS_NXM_1, |
| 71 | + Field: openflow15.NXM_NX_REG1, |
| 72 | + Value: &openflow15.Uint32Message{Data: uint32(0xf0)}, |
| 73 | + Mask: &openflow15.Uint32Message{Data: uint32(0xfff0)}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + expectedActionStr: "set_field:0xf0/0xfff0->reg1", |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, tc := range testCases { |
| 80 | + g := &ofGroup{ofctrl: &ofctrl.Group{}} |
| 81 | + group := g.Bucket().LoadToRegField(tc.regField, tc.value).Done() |
| 82 | + groupMod := getGroupMod(t, group) |
| 83 | + assert.Equal(t, 1, len(groupMod.Buckets)) |
| 84 | + checkActionSetFields(t, []*openflow15.ActionSetField{tc.expected}, groupMod.Buckets[0].Actions) |
| 85 | + assert.Contains(t, GroupModToString(groupMod), tc.expectedActionStr) |
| 86 | + } |
| 87 | + }) |
| 88 | + t.Run("LoadXXReg", func(t *testing.T) { |
| 89 | + regID := 0 |
| 90 | + data := []byte{0x11, 0x22, 0x33, 0x44} |
| 91 | + expected := &openflow15.ActionSetField{ |
| 92 | + Field: openflow15.MatchField{ |
| 93 | + Class: openflow15.OXM_CLASS_NXM_1, |
| 94 | + Field: openflow15.NXM_NX_XXREG0, |
| 95 | + Value: util.NewBuffer(data), |
| 96 | + }, |
| 97 | + } |
| 98 | + expectedActionStr := "set_field:0x11223344->xxreg0" |
| 99 | + |
| 100 | + g := &ofGroup{ofctrl: &ofctrl.Group{}} |
| 101 | + group := g.Bucket().LoadXXReg(regID, data).Done() |
| 102 | + groupMod := getGroupMod(t, group) |
| 103 | + assert.Equal(t, 1, len(groupMod.Buckets)) |
| 104 | + checkActionSetFields(t, []*openflow15.ActionSetField{expected}, groupMod.Buckets[0].Actions) |
| 105 | + assert.Contains(t, GroupModToString(groupMod), expectedActionStr) |
| 106 | + }) |
| 107 | + t.Run("SetTunnelDst", func(t *testing.T) { |
| 108 | + testCases := []struct { |
| 109 | + dstIP net.IP |
| 110 | + expected *openflow15.ActionSetField |
| 111 | + expectedActionStr string |
| 112 | + }{ |
| 113 | + { |
| 114 | + dstIP: net.ParseIP("1.1.1.1"), |
| 115 | + expected: &openflow15.ActionSetField{ |
| 116 | + Field: openflow15.MatchField{ |
| 117 | + Class: openflow15.OXM_CLASS_NXM_1, |
| 118 | + Field: openflow15.NXM_NX_TUN_IPV4_DST, |
| 119 | + Value: &openflow15.TunnelIpv4DstField{ |
| 120 | + TunnelIpv4Dst: net.ParseIP("1.1.1.1"), |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + expectedActionStr: "set_field:1.1.1.1->tun_dst", |
| 125 | + }, |
| 126 | + { |
| 127 | + dstIP: net.ParseIP("fec0::1111"), |
| 128 | + expected: &openflow15.ActionSetField{ |
| 129 | + Field: openflow15.MatchField{ |
| 130 | + Class: openflow15.OXM_CLASS_NXM_1, |
| 131 | + Field: openflow15.NXM_NX_TUN_IPV6_DST, |
| 132 | + Value: &openflow15.Ipv6DstField{ |
| 133 | + Ipv6Dst: net.ParseIP("fec0::1111"), |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expectedActionStr: "set_field:fec0::1111->tun_ipv6_dst", |
| 138 | + }, |
| 139 | + } |
| 140 | + for _, tc := range testCases { |
| 141 | + g := &ofGroup{ofctrl: &ofctrl.Group{}} |
| 142 | + group := g.Bucket().SetTunnelDst(tc.dstIP).Done() |
| 143 | + groupMod := getGroupMod(t, group) |
| 144 | + assert.Equal(t, 1, len(groupMod.Buckets)) |
| 145 | + checkActionSetFields(t, []*openflow15.ActionSetField{tc.expected}, groupMod.Buckets[0].Actions) |
| 146 | + assert.Contains(t, GroupModToString(groupMod), tc.expectedActionStr) |
| 147 | + } |
| 148 | + }) |
| 149 | + t.Run("ResubmitToTable", func(t *testing.T) { |
| 150 | + tableID := uint8(100) |
| 151 | + g := &ofGroup{ofctrl: &ofctrl.Group{}} |
| 152 | + group := g.Bucket().ResubmitToTable(tableID).Done() |
| 153 | + groupMod := getGroupMod(t, group) |
| 154 | + assert.Equal(t, 1, len(groupMod.Buckets)) |
| 155 | + assert.Equal(t, 1, len(groupMod.Buckets[0].Actions)) |
| 156 | + assert.IsType(t, &openflow15.NXActionResubmitTable{}, groupMod.Buckets[0].Actions[0]) |
| 157 | + |
| 158 | + action := groupMod.Buckets[0].Actions[0].(*openflow15.NXActionResubmitTable) |
| 159 | + assert.Equal(t, uint16(openflow15.OFPP_IN_PORT), action.InPort) |
| 160 | + assert.Equal(t, tableID, action.TableID) |
| 161 | + assert.Contains(t, GroupModToString(groupMod), "resubmit:100") |
| 162 | + }) |
| 163 | +} |
0 commit comments