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" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 24 | "time" |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 25 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 26 | "github.com/gogo/protobuf/proto" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/rw_core/core/device/flow" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/rw_core/route" |
| 29 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 30 | fu "github.com/opencord/voltha-lib-go/v7/pkg/flows" |
| 31 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 32 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 33 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 34 | "google.golang.org/grpc/codes" |
| 35 | "google.golang.org/grpc/status" |
| 36 | ) |
| 37 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 38 | // listLogicalDeviceFlows returns logical device flows |
| 39 | func (agent *LogicalAgent) listLogicalDeviceFlows() map[uint64]*ofp.OfpFlowStats { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 40 | flowIDs := agent.flowCache.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 41 | flows := make(map[uint64]*ofp.OfpFlowStats, len(flowIDs)) |
| 42 | for flowID := range flowIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 43 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 44 | flows[flowID] = flowHandle.GetReadOnly() |
| 45 | flowHandle.Unlock() |
| 46 | } |
| 47 | } |
| 48 | return flows |
| 49 | } |
| 50 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 51 | //updateFlowTable updates the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 52 | func (agent *LogicalAgent) updateFlowTable(ctx context.Context, flow *ofp.FlowTableUpdate) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 53 | logger.Debug(ctx, "update-flow-table") |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 54 | if flow == nil { |
| 55 | return nil |
| 56 | } |
| 57 | |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 58 | switch flow.FlowMod.GetCommand() { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 59 | case ofp.OfpFlowModCommand_OFPFC_ADD: |
| 60 | return agent.flowAdd(ctx, flow) |
| 61 | case ofp.OfpFlowModCommand_OFPFC_DELETE: |
| 62 | return agent.flowDelete(ctx, flow) |
| 63 | case ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT: |
| 64 | return agent.flowDeleteStrict(ctx, flow) |
| 65 | case ofp.OfpFlowModCommand_OFPFC_MODIFY: |
| 66 | return agent.flowModify(flow) |
| 67 | case ofp.OfpFlowModCommand_OFPFC_MODIFY_STRICT: |
| 68 | return agent.flowModifyStrict(flow) |
| 69 | } |
| 70 | return status.Errorf(codes.Internal, |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 71 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, flow.FlowMod.GetCommand()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //flowAdd adds a flow to the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 75 | func (agent *LogicalAgent) flowAdd(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error { |
| 76 | mod := flowUpdate.FlowMod |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 77 | logger.Debugw(ctx, "flow-add", log.Fields{"flow": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 78 | if mod == nil { |
| 79 | return nil |
| 80 | } |
| 81 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 82 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 83 | logger.Errorw(ctx, "flow-add-failed", log.Fields{"flow-mod": mod, "err": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 84 | return err |
| 85 | } |
| 86 | var updated bool |
| 87 | var changed bool |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 88 | if changed, updated, err = agent.decomposeAndAdd(ctx, flow, flowUpdate); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 89 | logger.Errorw(ctx, "flow-decompose-and-add-failed ", log.Fields{"flow-mod": mod, "err": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 90 | return err |
| 91 | } |
| 92 | if changed && !updated { |
| 93 | if dbupdated := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !dbupdated { |
| 94 | return fmt.Errorf("couldnt-updated-flow-stats-%s", strconv.FormatUint(flow.Id, 10)) |
| 95 | } |
| 96 | } |
| 97 | return nil |
| 98 | |
| 99 | } |
| 100 | |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 101 | func (agent *LogicalAgent) decomposeAndAdd(ctx context.Context, flow *ofp.OfpFlowStats, flowUpdate *ofp.FlowTableUpdate) (bool, bool, error) { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 102 | changed := false |
| 103 | updated := false |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 104 | mod := flowUpdate.FlowMod |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 105 | var flowToReplace *ofp.OfpFlowStats |
| 106 | |
| 107 | //if flow is not found in the map, create a new entry, otherwise get the existing one. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 108 | flowHandle, flowCreated, err := agent.flowCache.LockOrCreate(ctx, flow) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 109 | if err != nil { |
| 110 | return changed, updated, err |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 111 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 112 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 113 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 114 | flows := make([]*ofp.OfpFlowStats, 0) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 115 | checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0 |
| 116 | if checkOverlap { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 117 | // TODO: this currently does nothing |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 118 | if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 119 | // TODO: should this error be notified other than being logged? |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 120 | logger.Warnw(ctx, "overlapped-flows", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 121 | } else { |
| 122 | // Add flow |
| 123 | changed = true |
| 124 | } |
| 125 | } else { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 126 | if !flowCreated { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 127 | flowToReplace = flowHandle.GetReadOnly() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 128 | if (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_RESET_COUNTS)) != 0 { |
| 129 | flow.ByteCount = flowToReplace.ByteCount |
| 130 | flow.PacketCount = flowToReplace.PacketCount |
| 131 | } |
| 132 | if !proto.Equal(flowToReplace, flow) { |
| 133 | changed = true |
| 134 | updated = true |
| 135 | } |
| 136 | } else { |
| 137 | changed = true |
| 138 | } |
| 139 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 140 | logger.Debugw(ctx, "flowAdd-changed", log.Fields{"changed": changed, "updated": updated}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 141 | if changed { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 142 | updatedFlows := map[uint64]*ofp.OfpFlowStats{flow.Id: flow} |
| 143 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 144 | groupIDs := agent.groupCache.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 145 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 146 | for groupID := range groupIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 147 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 148 | groups[groupID] = groupHandle.GetReadOnly() |
| 149 | groupHandle.Unlock() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, updatedFlows, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 154 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 155 | if flowCreated { |
| 156 | if er := flowHandle.Delete(ctx); er != nil { |
| 157 | logger.Errorw(ctx, "deleting-flow-from-cache-failed", log.Fields{"error": er, "flow-id": flow.Id}) |
| 158 | } |
| 159 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 160 | return changed, updated, err |
| 161 | } |
| 162 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 163 | // Verify whether the flow request can proceed, usually to multiple adapters |
| 164 | // This is an optimization to address the case where a decomposed set of flows need to |
| 165 | // be sent to multiple adapters. One or more adapters may not be ready at this time. |
| 166 | // If one adapter is not ready this will result in flows being reverted from the |
| 167 | // other adapters, at times continuously as the OF controller will keep sending the |
| 168 | // flows until they are successfully added. |
| 169 | if err := agent.deviceMgr.canMultipleAdapterRequestProceed(ctx, deviceRules.Keys()); err != nil { |
| 170 | logger.Warnw(ctx, "adapters-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "flow-id": flow.Id, "error": err}) |
| 171 | if flowCreated { |
| 172 | if er := flowHandle.Delete(ctx); er != nil { |
| 173 | logger.Errorw(ctx, "deleting-flow-from-cache-failed", log.Fields{"error": er, "flow-id": flow.Id}) |
| 174 | } |
| 175 | } |
| 176 | return false, false, err |
| 177 | } |
| 178 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 179 | logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 180 | // Update store and cache |
| 181 | if updated { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 182 | if err := flowHandle.Update(ctx, flow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 183 | return changed, updated, err |
| 184 | } |
| 185 | } |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 186 | respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules) |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 187 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 188 | // Create the go routines to wait |
| 189 | go func() { |
| 190 | // Wait for completion |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 191 | if res := coreutils.WaitForNilOrErrorResponses(agent.internalTimeout, respChannels...); res != nil { |
ssiddiqui | 21e54c3 | 2021-07-27 11:30:46 +0530 | [diff] [blame] | 192 | logger.Errorw(ctx, "failed-to-add-flow-will-attempt-deletion", log.Fields{ |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 193 | "errors": res, |
| 194 | "logical-device-id": agent.logicalDeviceID, |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 195 | "flow": flow, |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 196 | "groups": groups, |
| 197 | }) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 198 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 199 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 200 | // Revert added flows |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 201 | if err := agent.revertAddedFlows(subCtx, mod, flow, flowToReplace, deviceRules); err != nil { |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 202 | logger.Errorw(ctx, "failure-to-delete-flow-after-failed-addition", log.Fields{ |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 203 | "error": err, |
| 204 | "logical-device-id": agent.logicalDeviceID, |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 205 | "flow": flow, |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 206 | "groups": groups, |
| 207 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 208 | } |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 209 | // send event |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame] | 210 | agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 211 | context := make(map[string]string) |
| 212 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
Himani Chawla | 0351077 | 2021-02-17 12:48:49 +0530 | [diff] [blame] | 213 | context["flow-id"] = fmt.Sprintf("%v", flow.Id) |
| 214 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 215 | context["logical-device-id"] = agent.logicalDeviceID |
| 216 | if deviceRules != nil { |
| 217 | context["device-rules"] = deviceRules.String() |
| 218 | } |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 219 | agent.ldeviceMgr.SendRPCEvent(ctx, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 220 | agent.logicalDeviceID, "failed-to-add-flow", context, "RPC_ERROR_RAISE_EVENT", |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 221 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 222 | } |
| 223 | }() |
| 224 | } |
| 225 | return changed, updated, nil |
| 226 | } |
| 227 | |
| 228 | // revertAddedFlows reverts flows after the flowAdd request has failed. All flows corresponding to that flowAdd request |
| 229 | // will be reverted, both from the logical devices and the devices. |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 230 | func (agent *LogicalAgent) revertAddedFlows(ctx context.Context, mod *ofp.OfpFlowMod, addedFlow *ofp.OfpFlowStats, replacedFlow *ofp.OfpFlowStats, deviceRules *fu.DeviceRules) error { |
| 231 | logger.Debugw(ctx, "revert-flow-add", log.Fields{"added-flow": addedFlow, "replaced-flow": replacedFlow, "device-rules": deviceRules}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 232 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 233 | flowHandle, have := agent.flowCache.Lock(addedFlow.Id) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 234 | if !have { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 235 | // Not found - do nothing |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 236 | logger.Debugw(ctx, "flow-not-found", log.Fields{"added-flow": addedFlow}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 237 | return nil |
| 238 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 239 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 240 | |
| 241 | if replacedFlow != nil { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 242 | if err := flowHandle.Update(ctx, replacedFlow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 243 | return err |
| 244 | } |
| 245 | } else { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 246 | if err := flowHandle.Delete(ctx); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 247 | return err |
| 248 | } |
| 249 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 250 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 251 | // Revert meters |
| 252 | if changedMeterStats := agent.updateFlowCountOfMeterStats(ctx, mod, addedFlow, true); !changedMeterStats { |
| 253 | return fmt.Errorf("Unable-to-revert-meterstats-for-flow-%s", strconv.FormatUint(addedFlow.Id, 10)) |
| 254 | } |
| 255 | |
| 256 | // Update the devices |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 257 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 258 | |
| 259 | // Wait for the responses |
| 260 | go func() { |
| 261 | // Since this action is taken following an add failure, we may also receive a failure for the revert |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 262 | if res := coreutils.WaitForNilOrErrorResponses(agent.internalTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 263 | logger.Warnw(ctx, "failure-reverting-added-flows", log.Fields{ |
Matteo Scandolo | 367162b | 2020-06-22 15:07:33 -0700 | [diff] [blame] | 264 | "logical-device-id": agent.logicalDeviceID, |
| 265 | "flow-cookie": mod.Cookie, |
| 266 | "errors": res, |
| 267 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 268 | } |
| 269 | }() |
| 270 | |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | //flowDelete deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 275 | func (agent *LogicalAgent) flowDelete(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 276 | logger.Debug(ctx, "flow-delete") |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 277 | mod := flowUpdate.FlowMod |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 278 | if mod == nil { |
| 279 | return nil |
| 280 | } |
| 281 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 282 | //build a list of what to delete |
| 283 | toDelete := make(map[uint64]*ofp.OfpFlowStats) |
| 284 | |
| 285 | // add perfectly matching entry if exists |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 286 | fs, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 287 | if err != nil { |
| 288 | return err |
| 289 | } |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 290 | if handle, have := agent.flowCache.Lock(fs.Id); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 291 | toDelete[fs.Id] = handle.GetReadOnly() |
| 292 | handle.Unlock() |
| 293 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 294 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 295 | // search through all the flows |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 296 | for flowID := range agent.flowCache.ListIDs() { |
| 297 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 298 | if flow := flowHandle.GetReadOnly(); fu.FlowMatchesMod(flow, mod) { |
| 299 | toDelete[flow.Id] = flow |
| 300 | } |
| 301 | flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 304 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 305 | //Delete the matched flows |
| 306 | if len(toDelete) > 0 { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 307 | logger.Debugw(ctx, "flow-delete", log.Fields{"logical-device-id": agent.logicalDeviceID, "to-delete": len(toDelete)}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 308 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 309 | for _, flow := range toDelete { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 310 | if flowHandle, have := agent.flowCache.Lock(flow.Id); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 311 | // TODO: Flow should only be updated if meter is updated, and meter should only be updated if flow is updated |
| 312 | // currently an error while performing the second operation will leave an inconsistent state in kv. |
| 313 | // This should be a single atomic operation down to the kv. |
| 314 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flowHandle.GetReadOnly(), false); !changedMeter { |
| 315 | flowHandle.Unlock() |
| 316 | return fmt.Errorf("cannot-delete-flow-%d. Meter-update-failed", flow.Id) |
| 317 | } |
| 318 | // Update store and cache |
| 319 | if err := flowHandle.Delete(ctx); err != nil { |
| 320 | flowHandle.Unlock() |
| 321 | return fmt.Errorf("cannot-delete-flows-%d. Delete-from-store-failed", flow.Id) |
| 322 | } |
| 323 | flowHandle.Unlock() |
| 324 | // TODO: since this is executed in a loop without also updating meter stats, and error part way through this |
| 325 | // operation will leave inconsistent state in the meter stats & flows on the devices. |
| 326 | // 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] | 327 | } |
| 328 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 329 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 330 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 331 | for groupID := range agent.groupCache.ListIDs() { |
| 332 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 333 | groups[groupID] = groupHandle.GetReadOnly() |
| 334 | groupHandle.Unlock() |
| 335 | } |
| 336 | } |
| 337 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 338 | var respChnls []coreutils.Response |
| 339 | var partialRoute bool |
| 340 | var deviceRules *fu.DeviceRules |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 341 | deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, toDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 342 | if err != nil { |
| 343 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 344 | // child device is deleted and a request to delete flows from the parent device is received |
| 345 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 346 | 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] | 347 | return err |
| 348 | } |
| 349 | partialRoute = true |
| 350 | } |
| 351 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 352 | var devicesInFlows []string |
| 353 | if deviceRules != nil { |
| 354 | devicesInFlows = deviceRules.Keys() |
| 355 | } else { |
| 356 | devicesInFlows = []string{agent.rootDeviceID} |
| 357 | } |
| 358 | |
Elia Battiston | 509fdc7 | 2022-01-04 13:28:09 +0100 | [diff] [blame] | 359 | for _, deviceID := range devicesInFlows { |
| 360 | if err := agent.deviceMgr.canAdapterRequestProceed(ctx, deviceID); err != nil { |
| 361 | //If the error has code.NotFound the device is not there anymore, there is no need to delete flows, just ignore it |
| 362 | if status.Code(err) != codes.NotFound { |
| 363 | logger.Warnw(ctx, "adapters-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "flow": toDelete, "error": err}) |
| 364 | return err |
| 365 | } else { |
| 366 | logger.Debugw(ctx, "flow-delete-for-nil-device-proceeding-deletion", log.Fields{"deviceID": deviceID}) |
| 367 | if deviceRules != nil { |
| 368 | deviceRules.RemoveRule(deviceID) |
| 369 | partialRoute = true |
| 370 | } |
| 371 | } |
| 372 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 375 | // Update the devices |
| 376 | if partialRoute { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 377 | respChnls = agent.deleteFlowsFromParentDevice(ctx, toDelete, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 378 | } else { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 379 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Wait for the responses |
| 383 | go func() { |
| 384 | // Wait for completion |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 385 | if res := coreutils.WaitForNilOrErrorResponses(agent.internalTimeout, respChnls...); res != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 386 | logger.Errorw(ctx, "failure-updating-device-flows", log.Fields{"logical-device-id": agent.logicalDeviceID, "errors": res}) |
| 387 | context := make(map[string]string) |
| 388 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
Himani Chawla | 0351077 | 2021-02-17 12:48:49 +0530 | [diff] [blame] | 389 | context["logical-device-id"] = agent.logicalDeviceID |
| 390 | context["flow-id"] = fmt.Sprintf("%v", fs.Id) |
| 391 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 392 | if deviceRules != nil { |
| 393 | context["device-rules"] = deviceRules.String() |
| 394 | } |
| 395 | |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 396 | agent.ldeviceMgr.SendRPCEvent(ctx, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 397 | agent.logicalDeviceID, "failed-to-update-device-flows", context, "RPC_ERROR_RAISE_EVENT", |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 398 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 399 | // TODO: Revert the flow deletion |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 400 | // send event, and allow any queued events to be sent as well |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame] | 401 | agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 402 | } |
| 403 | }() |
| 404 | } |
| 405 | //TODO: send announcement on delete |
| 406 | return nil |
| 407 | } |
| 408 | |
| 409 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 410 | func (agent *LogicalAgent) flowDeleteStrict(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 411 | var flowHandle *flow.Handle |
| 412 | var have bool |
| 413 | |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 414 | mod := flowUpdate.FlowMod |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 415 | logger.Debugw(ctx, "flow-delete-strict", log.Fields{"mod": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 416 | if mod == nil { |
| 417 | return nil |
| 418 | } |
| 419 | |
| 420 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 421 | if err != nil { |
| 422 | return err |
| 423 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 424 | |
| 425 | defer func() { |
| 426 | if flowHandle != nil { |
| 427 | flowHandle.Unlock() |
| 428 | } |
| 429 | }() |
| 430 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 431 | logger.Debugw(ctx, "flow-id-in-flow-delete-strict", log.Fields{"flow-id": flow.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 432 | flowHandle, have = agent.flowCache.Lock(flow.Id) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 433 | if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 434 | logger.Debugw(ctx, "flow-delete-strict-request-no-flow-found-continuing", log.Fields{"flow-mod": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 435 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 436 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 437 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 438 | for groupID := range agent.groupCache.ListIDs() { |
| 439 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 440 | groups[groupID] = groupHandle.GetReadOnly() |
| 441 | groupHandle.Unlock() |
| 442 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 443 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 444 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 445 | flowsToDelete := map[uint64]*ofp.OfpFlowStats{flow.Id: flow} |
| 446 | if flowHandle != nil { |
| 447 | flowsToDelete = map[uint64]*ofp.OfpFlowStats{flow.Id: flowHandle.GetReadOnly()} |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 448 | } |
| 449 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 450 | var respChnls []coreutils.Response |
| 451 | var partialRoute bool |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 452 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, flowsToDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 453 | if err != nil { |
| 454 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 455 | // child device is deleted and a request to delete flows from the parent device is received |
| 456 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 457 | 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] | 458 | return err |
| 459 | } |
| 460 | partialRoute = true |
| 461 | } |
| 462 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 463 | var devicesInFlows []string |
| 464 | if deviceRules != nil { |
| 465 | devicesInFlows = deviceRules.Keys() |
| 466 | } else { |
| 467 | devicesInFlows = []string{agent.rootDeviceID} |
| 468 | } |
| 469 | |
Elia Battiston | 509fdc7 | 2022-01-04 13:28:09 +0100 | [diff] [blame] | 470 | for _, deviceID := range devicesInFlows { |
| 471 | if err := agent.deviceMgr.canAdapterRequestProceed(ctx, deviceID); err != nil { |
| 472 | //If the error has code.NotFound the device is not there anymore, there is no need to delete flows, just ignore it |
| 473 | if status.Code(err) != codes.NotFound { |
| 474 | logger.Warnw(ctx, "adapters-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "flow": flowsToDelete, "error": err}) |
| 475 | return err |
| 476 | } else { |
| 477 | logger.Debugw(ctx, "flow-delete-strict-for-nil-device-proceeding-deletion", log.Fields{"deviceID": deviceID}) |
| 478 | if deviceRules != nil { |
| 479 | deviceRules.RemoveRule(deviceID) |
| 480 | partialRoute = true |
| 481 | } |
| 482 | } |
| 483 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 484 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 485 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 486 | // Update the devices |
| 487 | if partialRoute { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 488 | respChnls = agent.deleteFlowsFromParentDevice(ctx, flowsToDelete, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 489 | } else { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 490 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Wait for completion |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 494 | if res := coreutils.WaitForNilOrErrorResponses(agent.internalTimeout, respChnls...); res != nil { |
| 495 | logger.Warnw(ctx, "failure-deleting-device-flows", log.Fields{ |
| 496 | "flow-cookie": mod.Cookie, |
| 497 | "logical-device-id": agent.logicalDeviceID, |
| 498 | "errors": res, |
| 499 | }) |
| 500 | context := make(map[string]string) |
| 501 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
| 502 | context["flow-id"] = fmt.Sprintf("%v", flow.Id) |
| 503 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 504 | context["logical-device-id"] = agent.logicalDeviceID |
| 505 | if deviceRules != nil { |
| 506 | context["device-rules"] = deviceRules.String() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 507 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 508 | // Create context and send extra information as part of it. |
| 509 | agent.ldeviceMgr.SendRPCEvent(ctx, |
| 510 | agent.logicalDeviceID, "failed-to-delete-device-flows", context, "RPC_ERROR_RAISE_EVENT", |
| 511 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
| 512 | |
| 513 | return status.Errorf(codes.Aborted, "failed deleting flows id:%d, errors:%v", flow.Id, res) |
| 514 | } |
| 515 | |
| 516 | // Update meter count |
| 517 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !changedMeter { |
| 518 | return fmt.Errorf("cannot delete flow - %s. Meter update failed", flow) |
| 519 | } |
| 520 | |
| 521 | // Update the model |
| 522 | if flowHandle != nil { |
| 523 | if err := flowHandle.Delete(ctx); err != nil { |
| 524 | return err |
| 525 | } |
| 526 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 527 | |
| 528 | return nil |
| 529 | } |
| 530 | |
| 531 | //flowModify modifies a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 532 | func (agent *LogicalAgent) flowModify(flowUpdate *ofp.FlowTableUpdate) error { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 533 | return errors.New("flowModify not implemented") |
| 534 | } |
| 535 | |
| 536 | //flowModifyStrict deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 537 | func (agent *LogicalAgent) flowModifyStrict(flowUpdate *ofp.FlowTableUpdate) error { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 538 | return errors.New("flowModifyStrict not implemented") |
| 539 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 540 | |
| 541 | // TODO: Remove this helper, just pass the map through to functions directly |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 542 | func toMetadata(meters map[uint32]*ofp.OfpMeterConfig) *ofp.FlowMetadata { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 543 | ctr, ret := 0, make([]*ofp.OfpMeterConfig, len(meters)) |
| 544 | for _, meter := range meters { |
| 545 | ret[ctr] = meter |
| 546 | ctr++ |
| 547 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 548 | return &ofp.FlowMetadata{Meters: ret} |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | func (agent *LogicalAgent) deleteFlowsHavingMeter(ctx context.Context, meterID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 552 | logger.Infow(ctx, "delete-flows-matching-meter", log.Fields{"meter": meterID}) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 553 | for flowID := range agent.flowCache.ListIDs() { |
| 554 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 555 | if flowMeterID := fu.GetMeterIdFromFlow(flowHandle.GetReadOnly()); flowMeterID != 0 && flowMeterID == meterID { |
| 556 | if err := flowHandle.Delete(ctx); err != nil { |
| 557 | //TODO: Think on carrying on and deleting the remaining flows, instead of returning. |
| 558 | //Anyways this returns an error to controller which possibly results with a re-deletion. |
| 559 | //Then how can we handle the new deletion request(Same for group deletion)? |
| 560 | return err |
| 561 | } |
| 562 | } |
| 563 | flowHandle.Unlock() |
| 564 | } |
| 565 | } |
| 566 | return nil |
| 567 | } |
| 568 | |
| 569 | func (agent *LogicalAgent) deleteFlowsHavingGroup(ctx context.Context, groupID uint32) (map[uint64]*ofp.OfpFlowStats, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 570 | logger.Infow(ctx, "delete-flows-matching-group", log.Fields{"group-id": groupID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 571 | flowsRemoved := make(map[uint64]*ofp.OfpFlowStats) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 572 | for flowID := range agent.flowCache.ListIDs() { |
| 573 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 574 | if flow := flowHandle.GetReadOnly(); fu.FlowHasOutGroup(flow, groupID) { |
| 575 | if err := flowHandle.Delete(ctx); err != nil { |
| 576 | return nil, err |
| 577 | } |
| 578 | flowsRemoved[flowID] = flow |
| 579 | } |
| 580 | flowHandle.Unlock() |
| 581 | } |
| 582 | } |
| 583 | return flowsRemoved, nil |
| 584 | } |