Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 22 | |
| 23 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
| 24 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 26 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/v3/go/voltha" |
| 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
| 30 | ) |
| 31 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 32 | // listLogicalDeviceGroups returns logical device flow groups |
| 33 | func (agent *LogicalAgent) listLogicalDeviceGroups() map[uint32]*ofp.OfpGroupEntry { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 34 | groupIDs := agent.groupLoader.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 35 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 36 | for groupID := range groupIDs { |
| 37 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
| 38 | groups[groupID] = groupHandle.GetReadOnly() |
| 39 | groupHandle.Unlock() |
| 40 | } |
| 41 | } |
| 42 | return groups |
| 43 | } |
| 44 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 45 | //updateGroupTable updates the group table of that logical device |
| 46 | func (agent *LogicalAgent) updateGroupTable(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 47 | logger.Debug("updateGroupTable") |
| 48 | if groupMod == nil { |
| 49 | return nil |
| 50 | } |
| 51 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 52 | switch groupMod.GetCommand() { |
| 53 | case ofp.OfpGroupModCommand_OFPGC_ADD: |
| 54 | return agent.groupAdd(ctx, groupMod) |
| 55 | case ofp.OfpGroupModCommand_OFPGC_DELETE: |
| 56 | return agent.groupDelete(ctx, groupMod) |
| 57 | case ofp.OfpGroupModCommand_OFPGC_MODIFY: |
| 58 | return agent.groupModify(ctx, groupMod) |
| 59 | } |
| 60 | return status.Errorf(codes.Internal, "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, groupMod.GetCommand()) |
| 61 | } |
| 62 | |
| 63 | func (agent *LogicalAgent) groupAdd(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 64 | if groupMod == nil { |
| 65 | return nil |
| 66 | } |
| 67 | logger.Debugw("groupAdd", log.Fields{"GroupId": groupMod.GroupId}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 68 | |
| 69 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 70 | |
| 71 | groupHandle, created, err := agent.groupLoader.LockOrCreate(ctx, groupEntry) |
| 72 | if err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 73 | return err |
| 74 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 75 | groupHandle.Unlock() |
| 76 | |
| 77 | if !created { |
| 78 | return fmt.Errorf("group %d already exists", groupMod.GroupId) |
| 79 | } |
| 80 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 81 | fg := fu.NewFlowsAndGroups() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 82 | fg.AddGroup(groupEntry) |
| 83 | deviceRules := fu.NewDeviceRules() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 84 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 85 | |
| 86 | logger.Debugw("rules", log.Fields{"rules for group-add": deviceRules.String()}) |
| 87 | |
| 88 | // Update the devices |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 89 | respChnls := agent.addFlowsAndGroupsToDevices(deviceRules, &voltha.FlowMetadata{}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 90 | |
| 91 | // Wait for completion |
| 92 | go func() { |
| 93 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
| 94 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
| 95 | //TODO: Revert flow changes |
| 96 | } |
| 97 | }() |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | func (agent *LogicalAgent) groupDelete(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 102 | logger.Debug("groupDelete") |
| 103 | if groupMod == nil { |
| 104 | return nil |
| 105 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 106 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 107 | affectedFlows := make(map[uint64]*ofp.OfpFlowStats) |
| 108 | affectedGroups := make(map[uint32]*ofp.OfpGroupEntry) |
| 109 | var groupsChanged bool |
| 110 | |
| 111 | toDelete := map[uint32]struct{}{groupMod.GroupId: {}} |
| 112 | if groupMod.GroupId == uint32(ofp.OfpGroup_OFPG_ALL) { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 113 | toDelete = agent.groupLoader.ListIDs() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 116 | for groupID := range toDelete { |
| 117 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
Girish Gowdra | 4c4face | 2020-06-08 16:51:09 -0700 | [diff] [blame] | 118 | affectedGroups[groupID] = groupHandle.GetReadOnly() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 119 | if err := groupHandle.Delete(ctx); err != nil { |
| 120 | return err |
| 121 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 122 | groupHandle.Unlock() |
| 123 | |
| 124 | //TODO: this is another case where ordering guarantees are not being made, |
| 125 | // group deletion does not guarantee deletion of corresponding flows. |
| 126 | // an error while deleting flows can cause inconsistent state. |
| 127 | flows, err := agent.deleteFlowsHavingGroup(ctx, groupID) |
| 128 | if err != nil { |
| 129 | logger.Errorw("cannot-update-flow-for-group-delete", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "groupID": groupID}) |
| 130 | return err |
| 131 | } |
| 132 | for flowID, flow := range flows { |
| 133 | affectedFlows[flowID] = flow |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | groupsChanged = true |
| 138 | |
| 139 | //TODO: groupsChanged is always true here? use `len(affectedFlows)!=0` or `len(affectedGroups)!=0` instead? |
| 140 | if groupsChanged { |
| 141 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, affectedFlows, affectedGroups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | logger.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 146 | |
| 147 | // Update the devices |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 148 | respChnls := agent.updateFlowsAndGroupsOfDevice(deviceRules, nil) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 149 | |
| 150 | // Wait for completion |
| 151 | go func() { |
| 152 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
| 153 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
| 154 | //TODO: Revert flow changes |
| 155 | } |
| 156 | }() |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | func (agent *LogicalAgent) groupModify(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 162 | logger.Debug("groupModify") |
| 163 | if groupMod == nil { |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | groupID := groupMod.GroupId |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 168 | |
| 169 | groupHandle, have := agent.groupLoader.Lock(groupID) |
| 170 | if !have { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 171 | return fmt.Errorf("group-absent:%d", groupID) |
| 172 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 173 | defer groupHandle.Unlock() |
| 174 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 175 | //replace existing group entry with new group definition |
| 176 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
| 177 | deviceRules := fu.NewDeviceRules() |
| 178 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 179 | fg := fu.NewFlowsAndGroups() |
| 180 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 181 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 182 | |
| 183 | logger.Debugw("rules", log.Fields{"rules-for-group-modify": deviceRules.String()}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 184 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 185 | //update KV |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 186 | if err := groupHandle.Update(ctx, groupEntry); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 187 | logger.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
| 188 | return err |
| 189 | } |
| 190 | |
| 191 | // Update the devices |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 192 | respChnls := agent.updateFlowsAndGroupsOfDevice(deviceRules, &voltha.FlowMetadata{}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 193 | |
| 194 | // Wait for completion |
| 195 | go func() { |
| 196 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
| 197 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
| 198 | //TODO: Revert flow changes |
| 199 | } |
| 200 | }() |
| 201 | return nil |
| 202 | } |