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 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 47 | logger.Debug(ctx, "updateGroupTable") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 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 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 67 | logger.Debugw(ctx, "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 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 86 | logger.Debugw(ctx, "rules", log.Fields{"rules for group-add": deviceRules.String()}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 87 | |
| 88 | // Update the devices |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 89 | respChnls := agent.addFlowsAndGroupsToDevices(ctx, 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 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 94 | logger.Warnw(ctx, "failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 95 | //TODO: Revert flow changes |
| 96 | } |
| 97 | }() |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | func (agent *LogicalAgent) groupDelete(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Esin Karaman | 7eab1b9 | 2020-07-01 11:40:39 +0000 | [diff] [blame^] | 102 | logger.Debugw(ctx, "groupDelete", log.Fields{"groupMod": groupMod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 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) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 109 | |
| 110 | toDelete := map[uint32]struct{}{groupMod.GroupId: {}} |
| 111 | if groupMod.GroupId == uint32(ofp.OfpGroup_OFPG_ALL) { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 112 | toDelete = agent.groupLoader.ListIDs() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 115 | for groupID := range toDelete { |
| 116 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
Girish Gowdra | 4c4face | 2020-06-08 16:51:09 -0700 | [diff] [blame] | 117 | affectedGroups[groupID] = groupHandle.GetReadOnly() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 118 | if err := groupHandle.Delete(ctx); err != nil { |
| 119 | return err |
| 120 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 121 | groupHandle.Unlock() |
| 122 | |
| 123 | //TODO: this is another case where ordering guarantees are not being made, |
| 124 | // group deletion does not guarantee deletion of corresponding flows. |
| 125 | // an error while deleting flows can cause inconsistent state. |
| 126 | flows, err := agent.deleteFlowsHavingGroup(ctx, groupID) |
| 127 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 128 | logger.Errorw(ctx, "cannot-update-flow-for-group-delete", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "groupID": groupID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 129 | return err |
| 130 | } |
| 131 | for flowID, flow := range flows { |
| 132 | affectedFlows[flowID] = flow |
| 133 | } |
| 134 | } |
| 135 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 136 | |
Esin Karaman | 7eab1b9 | 2020-07-01 11:40:39 +0000 | [diff] [blame^] | 137 | if len(affectedGroups) == 0 { |
| 138 | logger.Debugw(ctx, "no-group-to-delete", log.Fields{"groupId": groupMod.GroupId}) |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | var deviceRules *fu.DeviceRules |
| 143 | var err error |
| 144 | |
| 145 | if len(affectedFlows) != 0 { |
| 146 | deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, affectedFlows, affectedGroups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 147 | if err != nil { |
| 148 | return err |
| 149 | } |
Esin Karaman | 7eab1b9 | 2020-07-01 11:40:39 +0000 | [diff] [blame^] | 150 | } else { |
| 151 | //no flow is affected, just remove the groups |
| 152 | deviceRules = fu.NewDeviceRules() |
| 153 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 154 | } |
Esin Karaman | 7eab1b9 | 2020-07-01 11:40:39 +0000 | [diff] [blame^] | 155 | //add groups to deviceRules |
| 156 | for _, groupEntry := range affectedGroups { |
| 157 | fg := fu.NewFlowsAndGroups() |
| 158 | fg.AddGroup(groupEntry) |
| 159 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 160 | } |
| 161 | logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()}) |
| 162 | |
| 163 | // delete groups and related flows, if any |
| 164 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, &voltha.FlowMetadata{}, &ofp.OfpFlowMod{}) |
| 165 | |
| 166 | // Wait for completion |
| 167 | go func() { |
| 168 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
| 169 | logger.Warnw(ctx, "failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
| 170 | //TODO: Revert flow changes |
| 171 | } |
| 172 | }() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 173 | return nil |
| 174 | } |
| 175 | |
| 176 | func (agent *LogicalAgent) groupModify(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | logger.Debug(ctx, "groupModify") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 178 | if groupMod == nil { |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | groupID := groupMod.GroupId |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 183 | |
| 184 | groupHandle, have := agent.groupLoader.Lock(groupID) |
| 185 | if !have { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 186 | return fmt.Errorf("group-absent:%d", groupID) |
| 187 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 188 | defer groupHandle.Unlock() |
| 189 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 190 | //replace existing group entry with new group definition |
| 191 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
| 192 | deviceRules := fu.NewDeviceRules() |
| 193 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 194 | fg := fu.NewFlowsAndGroups() |
| 195 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 196 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 197 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 198 | logger.Debugw(ctx, "rules", log.Fields{"rules-for-group-modify": deviceRules.String()}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 199 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 200 | //update KV |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 201 | if err := groupHandle.Update(ctx, groupEntry); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 202 | logger.Errorw(ctx, "Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 203 | return err |
| 204 | } |
| 205 | |
| 206 | // Update the devices |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 207 | respChnls := agent.updateFlowsAndGroupsOfDevice(ctx, deviceRules, &voltha.FlowMetadata{}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 208 | |
| 209 | // Wait for completion |
| 210 | go func() { |
| 211 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 212 | logger.Warnw(ctx, "failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 213 | //TODO: Revert flow changes |
| 214 | } |
| 215 | }() |
| 216 | return nil |
| 217 | } |