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 | "errors" |
| 22 | "fmt" |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 23 | "strconv" |
| 24 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 25 | "github.com/gogo/protobuf/proto" |
| 26 | "github.com/opencord/voltha-go/rw_core/route" |
| 27 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 28 | fu "github.com/opencord/voltha-lib-go/v4/pkg/flows" |
| 29 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 30 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 31 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 32 | "google.golang.org/grpc/codes" |
| 33 | "google.golang.org/grpc/status" |
| 34 | ) |
| 35 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 36 | // listLogicalDeviceFlows returns logical device flows |
| 37 | func (agent *LogicalAgent) listLogicalDeviceFlows() map[uint64]*ofp.OfpFlowStats { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 38 | flowIDs := agent.flowLoader.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 39 | flows := make(map[uint64]*ofp.OfpFlowStats, len(flowIDs)) |
| 40 | for flowID := range flowIDs { |
| 41 | if flowHandle, have := agent.flowLoader.Lock(flowID); have { |
| 42 | flows[flowID] = flowHandle.GetReadOnly() |
| 43 | flowHandle.Unlock() |
| 44 | } |
| 45 | } |
| 46 | return flows |
| 47 | } |
| 48 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 49 | //updateFlowTable updates the flow table of that logical device |
| 50 | func (agent *LogicalAgent) updateFlowTable(ctx context.Context, flow *ofp.OfpFlowMod) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 51 | logger.Debug(ctx, "UpdateFlowTable") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 52 | if flow == nil { |
| 53 | return nil |
| 54 | } |
| 55 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 56 | switch flow.GetCommand() { |
| 57 | case ofp.OfpFlowModCommand_OFPFC_ADD: |
| 58 | return agent.flowAdd(ctx, flow) |
| 59 | case ofp.OfpFlowModCommand_OFPFC_DELETE: |
| 60 | return agent.flowDelete(ctx, flow) |
| 61 | case ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT: |
| 62 | return agent.flowDeleteStrict(ctx, flow) |
| 63 | case ofp.OfpFlowModCommand_OFPFC_MODIFY: |
| 64 | return agent.flowModify(flow) |
| 65 | case ofp.OfpFlowModCommand_OFPFC_MODIFY_STRICT: |
| 66 | return agent.flowModifyStrict(flow) |
| 67 | } |
| 68 | return status.Errorf(codes.Internal, |
| 69 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, flow.GetCommand()) |
| 70 | } |
| 71 | |
| 72 | //flowAdd adds a flow to the flow table of that logical device |
| 73 | func (agent *LogicalAgent) flowAdd(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 74 | logger.Debugw(ctx, "flowAdd", log.Fields{"flow": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 75 | if mod == nil { |
| 76 | return nil |
| 77 | } |
| 78 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 79 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 80 | logger.Errorw(ctx, "flowAdd-failed", log.Fields{"flowMod": mod, "err": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 81 | return err |
| 82 | } |
| 83 | var updated bool |
| 84 | var changed bool |
| 85 | if changed, updated, err = agent.decomposeAndAdd(ctx, flow, mod); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 86 | logger.Errorw(ctx, "flow-decompose-and-add-failed ", log.Fields{"flowMod": mod, "err": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 87 | return err |
| 88 | } |
| 89 | if changed && !updated { |
| 90 | if dbupdated := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !dbupdated { |
| 91 | return fmt.Errorf("couldnt-updated-flow-stats-%s", strconv.FormatUint(flow.Id, 10)) |
| 92 | } |
| 93 | } |
| 94 | return nil |
| 95 | |
| 96 | } |
| 97 | |
| 98 | func (agent *LogicalAgent) decomposeAndAdd(ctx context.Context, flow *ofp.OfpFlowStats, mod *ofp.OfpFlowMod) (bool, bool, error) { |
| 99 | changed := false |
| 100 | updated := false |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 101 | var flowToReplace *ofp.OfpFlowStats |
| 102 | |
| 103 | //if flow is not found in the map, create a new entry, otherwise get the existing one. |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 104 | flowHandle, created, err := agent.flowLoader.LockOrCreate(ctx, flow) |
| 105 | if err != nil { |
| 106 | return changed, updated, err |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 107 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 108 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 109 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 110 | flows := make([]*ofp.OfpFlowStats, 0) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 111 | checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0 |
| 112 | if checkOverlap { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 113 | // TODO: this currently does nothing |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 114 | if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 115 | // TODO: should this error be notified other than being logged? |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 116 | logger.Warnw(ctx, "overlapped-flows", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 117 | } else { |
| 118 | // Add flow |
| 119 | changed = true |
| 120 | } |
| 121 | } else { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 122 | if !created { |
| 123 | flowToReplace = flowHandle.GetReadOnly() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 124 | if (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_RESET_COUNTS)) != 0 { |
| 125 | flow.ByteCount = flowToReplace.ByteCount |
| 126 | flow.PacketCount = flowToReplace.PacketCount |
| 127 | } |
| 128 | if !proto.Equal(flowToReplace, flow) { |
| 129 | changed = true |
| 130 | updated = true |
| 131 | } |
| 132 | } else { |
| 133 | changed = true |
| 134 | } |
| 135 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 136 | logger.Debugw(ctx, "flowAdd-changed", log.Fields{"changed": changed, "updated": updated}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 137 | if changed { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 138 | updatedFlows := map[uint64]*ofp.OfpFlowStats{flow.Id: flow} |
| 139 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 140 | flowMeterConfig, err := agent.GetMeterConfig(ctx, updatedFlows) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 141 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 142 | logger.Error(ctx, "Meter-referred-in-flow-not-present") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 143 | return changed, updated, err |
| 144 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 145 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 146 | groupIDs := agent.groupLoader.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 147 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 148 | for groupID := range groupIDs { |
| 149 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
| 150 | groups[groupID] = groupHandle.GetReadOnly() |
| 151 | groupHandle.Unlock() |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, updatedFlows, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 156 | if err != nil { |
| 157 | return changed, updated, err |
| 158 | } |
| 159 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 160 | logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 161 | // Update store and cache |
| 162 | if updated { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 163 | if err := flowHandle.Update(ctx, flow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 164 | return changed, updated, err |
| 165 | } |
| 166 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 167 | respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules, toMetadata(flowMeterConfig)) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 168 | // Create the go routines to wait |
| 169 | go func() { |
| 170 | // Wait for completion |
| 171 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChannels...); res != nil { |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 172 | logger.Infow(ctx, "failed-to-add-flow-will-attempt-deletion", log.Fields{ |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 173 | "errors": res, |
| 174 | "logical-device-id": agent.logicalDeviceID, |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 175 | "flow": flow, |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 176 | "groups": groups, |
| 177 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 178 | // Revert added flows |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 179 | if err := agent.revertAddedFlows(log.WithSpanFromContext(context.Background(), ctx), mod, flow, flowToReplace, deviceRules, toMetadata(flowMeterConfig)); err != nil { |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 180 | logger.Errorw(ctx, "failure-to-delete-flow-after-failed-addition", log.Fields{ |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 181 | "error": err, |
| 182 | "logical-device-id": agent.logicalDeviceID, |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 183 | "flow": flow, |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 184 | "groups": groups, |
| 185 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | }() |
| 189 | } |
| 190 | return changed, updated, nil |
| 191 | } |
| 192 | |
| 193 | // revertAddedFlows reverts flows after the flowAdd request has failed. All flows corresponding to that flowAdd request |
| 194 | // will be reverted, both from the logical devices and the devices. |
| 195 | func (agent *LogicalAgent) revertAddedFlows(ctx context.Context, mod *ofp.OfpFlowMod, addedFlow *ofp.OfpFlowStats, replacedFlow *ofp.OfpFlowStats, deviceRules *fu.DeviceRules, metadata *voltha.FlowMetadata) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 196 | logger.Debugw(ctx, "revertFlowAdd", log.Fields{"added-flow": addedFlow, "replaced-flow": replacedFlow, "device-rules": deviceRules, "metadata": metadata}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 197 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 198 | flowHandle, have := agent.flowLoader.Lock(addedFlow.Id) |
| 199 | if !have { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 200 | // Not found - do nothing |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 201 | logger.Debugw(ctx, "flow-not-found", log.Fields{"added-flow": addedFlow}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 202 | return nil |
| 203 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 204 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 205 | |
| 206 | if replacedFlow != nil { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 207 | if err := flowHandle.Update(ctx, replacedFlow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 208 | return err |
| 209 | } |
| 210 | } else { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 211 | if err := flowHandle.Delete(ctx); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 212 | return err |
| 213 | } |
| 214 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 215 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 216 | // Revert meters |
| 217 | if changedMeterStats := agent.updateFlowCountOfMeterStats(ctx, mod, addedFlow, true); !changedMeterStats { |
| 218 | return fmt.Errorf("Unable-to-revert-meterstats-for-flow-%s", strconv.FormatUint(addedFlow.Id, 10)) |
| 219 | } |
| 220 | |
| 221 | // Update the devices |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 222 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, metadata, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 223 | |
| 224 | // Wait for the responses |
| 225 | go func() { |
| 226 | // Since this action is taken following an add failure, we may also receive a failure for the revert |
| 227 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 228 | logger.Warnw(ctx, "failure-reverting-added-flows", log.Fields{ |
Matteo Scandolo | 367162b | 2020-06-22 15:07:33 -0700 | [diff] [blame] | 229 | "logical-device-id": agent.logicalDeviceID, |
| 230 | "flow-cookie": mod.Cookie, |
| 231 | "errors": res, |
| 232 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 233 | } |
| 234 | }() |
| 235 | |
| 236 | return nil |
| 237 | } |
| 238 | |
| 239 | //flowDelete deletes a flow from the flow table of that logical device |
| 240 | func (agent *LogicalAgent) flowDelete(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 241 | logger.Debug(ctx, "flowDelete") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 242 | if mod == nil { |
| 243 | return nil |
| 244 | } |
| 245 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 246 | //build a list of what to delete |
| 247 | toDelete := make(map[uint64]*ofp.OfpFlowStats) |
| 248 | |
| 249 | // add perfectly matching entry if exists |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 250 | fs, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 254 | if handle, have := agent.flowLoader.Lock(fs.Id); have { |
| 255 | toDelete[fs.Id] = handle.GetReadOnly() |
| 256 | handle.Unlock() |
| 257 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 258 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 259 | // search through all the flows |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 260 | for flowID := range agent.flowLoader.ListIDs() { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 261 | if flowHandle, have := agent.flowLoader.Lock(flowID); have { |
| 262 | if flow := flowHandle.GetReadOnly(); fu.FlowMatchesMod(flow, mod) { |
| 263 | toDelete[flow.Id] = flow |
| 264 | } |
| 265 | flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 268 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 269 | //Delete the matched flows |
| 270 | if len(toDelete) > 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 271 | logger.Debugw(ctx, "flowDelete", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "toDelete": len(toDelete)}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 272 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 273 | for _, flow := range toDelete { |
| 274 | if flowHandle, have := agent.flowLoader.Lock(flow.Id); have { |
| 275 | // TODO: Flow should only be updated if meter is updated, and meter should only be updated if flow is updated |
| 276 | // currently an error while performing the second operation will leave an inconsistent state in kv. |
| 277 | // This should be a single atomic operation down to the kv. |
| 278 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flowHandle.GetReadOnly(), false); !changedMeter { |
| 279 | flowHandle.Unlock() |
| 280 | return fmt.Errorf("cannot-delete-flow-%d. Meter-update-failed", flow.Id) |
| 281 | } |
| 282 | // Update store and cache |
| 283 | if err := flowHandle.Delete(ctx); err != nil { |
| 284 | flowHandle.Unlock() |
| 285 | return fmt.Errorf("cannot-delete-flows-%d. Delete-from-store-failed", flow.Id) |
| 286 | } |
| 287 | flowHandle.Unlock() |
| 288 | // TODO: since this is executed in a loop without also updating meter stats, and error part way through this |
| 289 | // operation will leave inconsistent state in the meter stats & flows on the devices. |
| 290 | // This & related meter updates should be a single atomic operation down to the kv. |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 293 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 294 | metersConfig, err := agent.GetMeterConfig(ctx, toDelete) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 295 | if err != nil { // This should never happen |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 296 | logger.Error(ctx, "Meter-referred-in-flows-not-present") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 297 | return err |
| 298 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 299 | |
| 300 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 301 | for groupID := range agent.groupLoader.ListIDs() { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 302 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
| 303 | groups[groupID] = groupHandle.GetReadOnly() |
| 304 | groupHandle.Unlock() |
| 305 | } |
| 306 | } |
| 307 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 308 | var respChnls []coreutils.Response |
| 309 | var partialRoute bool |
| 310 | var deviceRules *fu.DeviceRules |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 311 | deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, toDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 312 | if err != nil { |
| 313 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 314 | // child device is deleted and a request to delete flows from the parent device is received |
| 315 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 316 | logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": toDelete, "error": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 317 | return err |
| 318 | } |
| 319 | partialRoute = true |
| 320 | } |
| 321 | |
| 322 | // Update the devices |
| 323 | if partialRoute { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 324 | respChnls = agent.deleteFlowsFromParentDevice(ctx, toDelete, toMetadata(metersConfig), mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 325 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 326 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, toMetadata(metersConfig), mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Wait for the responses |
| 330 | go func() { |
| 331 | // Wait for completion |
| 332 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 333 | logger.Errorw(ctx, "failure-updating-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 334 | // TODO: Revert the flow deletion |
| 335 | } |
| 336 | }() |
| 337 | } |
| 338 | //TODO: send announcement on delete |
| 339 | return nil |
| 340 | } |
| 341 | |
| 342 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
| 343 | func (agent *LogicalAgent) flowDeleteStrict(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 344 | logger.Debugw(ctx, "flowDeleteStrict", log.Fields{"mod": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 345 | if mod == nil { |
| 346 | return nil |
| 347 | } |
| 348 | |
| 349 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 350 | if err != nil { |
| 351 | return err |
| 352 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 353 | logger.Debugw(ctx, "flow-id-in-flow-delete-strict", log.Fields{"flowID": flow.Id}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 354 | flowHandle, have := agent.flowLoader.Lock(flow.Id) |
| 355 | if !have { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 356 | logger.Debugw(ctx, "Skipping-flow-delete-strict-request. No-flow-found", log.Fields{"flowMod": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 357 | return nil |
| 358 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 359 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 360 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 361 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 362 | for groupID := range agent.groupLoader.ListIDs() { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 363 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
| 364 | groups[groupID] = groupHandle.GetReadOnly() |
| 365 | groupHandle.Unlock() |
| 366 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 367 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 368 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 369 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !changedMeter { |
| 370 | return fmt.Errorf("Cannot delete flow - %s. Meter update failed", flow) |
| 371 | } |
| 372 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 373 | flowsToDelete := map[uint64]*ofp.OfpFlowStats{flow.Id: flowHandle.GetReadOnly()} |
| 374 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 375 | flowMetadata, err := agent.GetMeterConfig(ctx, flowsToDelete) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 376 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 377 | logger.Error(ctx, "meter-referred-in-flows-not-present") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 378 | return err |
| 379 | } |
| 380 | var respChnls []coreutils.Response |
| 381 | var partialRoute bool |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 382 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, flowsToDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 383 | if err != nil { |
| 384 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 385 | // child device is deleted and a request to delete flows from the parent device is received |
| 386 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 387 | logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": flowsToDelete, "error": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 388 | return err |
| 389 | } |
| 390 | partialRoute = true |
| 391 | } |
| 392 | |
| 393 | // Update the model |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 394 | if err := flowHandle.Delete(ctx); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 395 | return err |
| 396 | } |
| 397 | // Update the devices |
| 398 | if partialRoute { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 399 | respChnls = agent.deleteFlowsFromParentDevice(ctx, flowsToDelete, toMetadata(flowMetadata), mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 400 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 401 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, toMetadata(flowMetadata), mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // Wait for completion |
| 405 | go func() { |
| 406 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 407 | logger.Warnw(ctx, "failure-deleting-device-flows", log.Fields{ |
Matteo Scandolo | 367162b | 2020-06-22 15:07:33 -0700 | [diff] [blame] | 408 | "flow-cookie": mod.Cookie, |
| 409 | "logical-device-id": agent.logicalDeviceID, |
| 410 | "errors": res, |
| 411 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 412 | //TODO: Revert flow changes |
| 413 | } |
| 414 | }() |
| 415 | |
| 416 | return nil |
| 417 | } |
| 418 | |
| 419 | //flowModify modifies a flow from the flow table of that logical device |
| 420 | func (agent *LogicalAgent) flowModify(mod *ofp.OfpFlowMod) error { |
| 421 | return errors.New("flowModify not implemented") |
| 422 | } |
| 423 | |
| 424 | //flowModifyStrict deletes a flow from the flow table of that logical device |
| 425 | func (agent *LogicalAgent) flowModifyStrict(mod *ofp.OfpFlowMod) error { |
| 426 | return errors.New("flowModifyStrict not implemented") |
| 427 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 428 | |
| 429 | // TODO: Remove this helper, just pass the map through to functions directly |
| 430 | func toMetadata(meters map[uint32]*ofp.OfpMeterConfig) *voltha.FlowMetadata { |
| 431 | ctr, ret := 0, make([]*ofp.OfpMeterConfig, len(meters)) |
| 432 | for _, meter := range meters { |
| 433 | ret[ctr] = meter |
| 434 | ctr++ |
| 435 | } |
| 436 | return &voltha.FlowMetadata{Meters: ret} |
| 437 | } |
| 438 | |
| 439 | func (agent *LogicalAgent) deleteFlowsHavingMeter(ctx context.Context, meterID uint32) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 440 | logger.Infow(ctx, "Delete-flows-matching-meter", log.Fields{"meter": meterID}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 441 | for flowID := range agent.flowLoader.ListIDs() { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 442 | if flowHandle, have := agent.flowLoader.Lock(flowID); have { |
| 443 | if flowMeterID := fu.GetMeterIdFromFlow(flowHandle.GetReadOnly()); flowMeterID != 0 && flowMeterID == meterID { |
| 444 | if err := flowHandle.Delete(ctx); err != nil { |
| 445 | //TODO: Think on carrying on and deleting the remaining flows, instead of returning. |
| 446 | //Anyways this returns an error to controller which possibly results with a re-deletion. |
| 447 | //Then how can we handle the new deletion request(Same for group deletion)? |
| 448 | return err |
| 449 | } |
| 450 | } |
| 451 | flowHandle.Unlock() |
| 452 | } |
| 453 | } |
| 454 | return nil |
| 455 | } |
| 456 | |
| 457 | func (agent *LogicalAgent) deleteFlowsHavingGroup(ctx context.Context, groupID uint32) (map[uint64]*ofp.OfpFlowStats, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 458 | logger.Infow(ctx, "Delete-flows-matching-group", log.Fields{"groupID": groupID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 459 | flowsRemoved := make(map[uint64]*ofp.OfpFlowStats) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 460 | for flowID := range agent.flowLoader.ListIDs() { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 461 | if flowHandle, have := agent.flowLoader.Lock(flowID); have { |
| 462 | if flow := flowHandle.GetReadOnly(); fu.FlowHasOutGroup(flow, groupID) { |
| 463 | if err := flowHandle.Delete(ctx); err != nil { |
| 464 | return nil, err |
| 465 | } |
| 466 | flowsRemoved[flowID] = flow |
| 467 | } |
| 468 | flowHandle.Unlock() |
| 469 | } |
| 470 | } |
| 471 | return flowsRemoved, nil |
| 472 | } |