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