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" |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 29 | fu "github.com/opencord/voltha-lib-go/v5/pkg/flows" |
| 30 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 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 { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 39 | flowIDs := agent.flowCache.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 { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 42 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 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. |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 107 | flowHandle, created, err := agent.flowCache.LockOrCreate(ctx, flow) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 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 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 143 | groupIDs := agent.groupCache.ListIDs() |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 144 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 145 | for groupID := range groupIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 146 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 147 | groups[groupID] = groupHandle.GetReadOnly() |
| 148 | groupHandle.Unlock() |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, updatedFlows, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 153 | if err != nil { |
| 154 | return changed, updated, err |
| 155 | } |
| 156 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 157 | logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 158 | // Update store and cache |
| 159 | if updated { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 160 | if err := flowHandle.Update(ctx, flow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 161 | return changed, updated, err |
| 162 | } |
| 163 | } |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 164 | respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules) |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 165 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 166 | // Create the go routines to wait |
| 167 | go func() { |
| 168 | // Wait for completion |
| 169 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChannels...); res != nil { |
ssiddiqui | 21e54c3 | 2021-07-27 11:30:46 +0530 | [diff] [blame^] | 170 | logger.Errorw(ctx, "failed-to-add-flow-will-attempt-deletion", log.Fields{ |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 171 | "errors": res, |
| 172 | "logical-device-id": agent.logicalDeviceID, |
Himani Chawla | 531af4f | 2020-09-22 10:42:17 +0530 | [diff] [blame] | 173 | "flow": flow, |
Matteo Scandolo | 45e514a | 2020-08-05 15:27:10 -0700 | [diff] [blame] | 174 | "groups": groups, |
| 175 | }) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 176 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 177 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 178 | // Revert added flows |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 179 | if err := agent.revertAddedFlows(subCtx, mod, flow, flowToReplace, deviceRules); 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 | } |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 187 | // send event |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame] | 188 | agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 189 | context := make(map[string]string) |
| 190 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
Himani Chawla | 0351077 | 2021-02-17 12:48:49 +0530 | [diff] [blame] | 191 | context["flow-id"] = fmt.Sprintf("%v", flow.Id) |
| 192 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 193 | context["logical-device-id"] = agent.logicalDeviceID |
| 194 | if deviceRules != nil { |
| 195 | context["device-rules"] = deviceRules.String() |
| 196 | } |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 197 | agent.ldeviceMgr.SendRPCEvent(ctx, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 198 | agent.logicalDeviceID, "failed-to-add-flow", context, "RPC_ERROR_RAISE_EVENT", |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 199 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 200 | } |
| 201 | }() |
| 202 | } |
| 203 | return changed, updated, nil |
| 204 | } |
| 205 | |
| 206 | // revertAddedFlows reverts flows after the flowAdd request has failed. All flows corresponding to that flowAdd request |
| 207 | // will be reverted, both from the logical devices and the devices. |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 208 | func (agent *LogicalAgent) revertAddedFlows(ctx context.Context, mod *ofp.OfpFlowMod, addedFlow *ofp.OfpFlowStats, replacedFlow *ofp.OfpFlowStats, deviceRules *fu.DeviceRules) error { |
| 209 | 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] | 210 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 211 | flowHandle, have := agent.flowCache.Lock(addedFlow.Id) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 212 | if !have { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 213 | // Not found - do nothing |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 214 | logger.Debugw(ctx, "flow-not-found", log.Fields{"added-flow": addedFlow}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 215 | return nil |
| 216 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 217 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 218 | |
| 219 | if replacedFlow != nil { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 220 | if err := flowHandle.Update(ctx, replacedFlow); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 221 | return err |
| 222 | } |
| 223 | } else { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 224 | if err := flowHandle.Delete(ctx); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 225 | return err |
| 226 | } |
| 227 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 228 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 229 | // Revert meters |
| 230 | if changedMeterStats := agent.updateFlowCountOfMeterStats(ctx, mod, addedFlow, true); !changedMeterStats { |
| 231 | return fmt.Errorf("Unable-to-revert-meterstats-for-flow-%s", strconv.FormatUint(addedFlow.Id, 10)) |
| 232 | } |
| 233 | |
| 234 | // Update the devices |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 235 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 236 | |
| 237 | // Wait for the responses |
| 238 | go func() { |
| 239 | // Since this action is taken following an add failure, we may also receive a failure for the revert |
| 240 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 241 | logger.Warnw(ctx, "failure-reverting-added-flows", log.Fields{ |
Matteo Scandolo | 367162b | 2020-06-22 15:07:33 -0700 | [diff] [blame] | 242 | "logical-device-id": agent.logicalDeviceID, |
| 243 | "flow-cookie": mod.Cookie, |
| 244 | "errors": res, |
| 245 | }) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 246 | } |
| 247 | }() |
| 248 | |
| 249 | return nil |
| 250 | } |
| 251 | |
| 252 | //flowDelete deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 253 | func (agent *LogicalAgent) flowDelete(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 254 | logger.Debug(ctx, "flow-delete") |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 255 | mod := flowUpdate.FlowMod |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 256 | if mod == nil { |
| 257 | return nil |
| 258 | } |
| 259 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 260 | //build a list of what to delete |
| 261 | toDelete := make(map[uint64]*ofp.OfpFlowStats) |
| 262 | |
| 263 | // add perfectly matching entry if exists |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 264 | fs, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 265 | if err != nil { |
| 266 | return err |
| 267 | } |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 268 | if handle, have := agent.flowCache.Lock(fs.Id); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 269 | toDelete[fs.Id] = handle.GetReadOnly() |
| 270 | handle.Unlock() |
| 271 | } |
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 | // search through all the flows |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 274 | for flowID := range agent.flowCache.ListIDs() { |
| 275 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 276 | if flow := flowHandle.GetReadOnly(); fu.FlowMatchesMod(flow, mod) { |
| 277 | toDelete[flow.Id] = flow |
| 278 | } |
| 279 | flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 282 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 283 | //Delete the matched flows |
| 284 | if len(toDelete) > 0 { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 285 | 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] | 286 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 287 | for _, flow := range toDelete { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 288 | if flowHandle, have := agent.flowCache.Lock(flow.Id); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 289 | // TODO: Flow should only be updated if meter is updated, and meter should only be updated if flow is updated |
| 290 | // currently an error while performing the second operation will leave an inconsistent state in kv. |
| 291 | // This should be a single atomic operation down to the kv. |
| 292 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flowHandle.GetReadOnly(), false); !changedMeter { |
| 293 | flowHandle.Unlock() |
| 294 | return fmt.Errorf("cannot-delete-flow-%d. Meter-update-failed", flow.Id) |
| 295 | } |
| 296 | // Update store and cache |
| 297 | if err := flowHandle.Delete(ctx); err != nil { |
| 298 | flowHandle.Unlock() |
| 299 | return fmt.Errorf("cannot-delete-flows-%d. Delete-from-store-failed", flow.Id) |
| 300 | } |
| 301 | flowHandle.Unlock() |
| 302 | // TODO: since this is executed in a loop without also updating meter stats, and error part way through this |
| 303 | // operation will leave inconsistent state in the meter stats & flows on the devices. |
| 304 | // 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] | 305 | } |
| 306 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 307 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 308 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 309 | for groupID := range agent.groupCache.ListIDs() { |
| 310 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 311 | groups[groupID] = groupHandle.GetReadOnly() |
| 312 | groupHandle.Unlock() |
| 313 | } |
| 314 | } |
| 315 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 316 | var respChnls []coreutils.Response |
| 317 | var partialRoute bool |
| 318 | var deviceRules *fu.DeviceRules |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 319 | deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, toDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 320 | if err != nil { |
| 321 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 322 | // child device is deleted and a request to delete flows from the parent device is received |
| 323 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 324 | 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] | 325 | return err |
| 326 | } |
| 327 | partialRoute = true |
| 328 | } |
| 329 | |
| 330 | // Update the devices |
| 331 | if partialRoute { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 332 | respChnls = agent.deleteFlowsFromParentDevice(ctx, toDelete, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 333 | } else { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 334 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Wait for the responses |
| 338 | go func() { |
| 339 | // Wait for completion |
| 340 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 341 | logger.Errorw(ctx, "failure-updating-device-flows", log.Fields{"logical-device-id": agent.logicalDeviceID, "errors": res}) |
| 342 | context := make(map[string]string) |
| 343 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
Himani Chawla | 0351077 | 2021-02-17 12:48:49 +0530 | [diff] [blame] | 344 | context["logical-device-id"] = agent.logicalDeviceID |
| 345 | context["flow-id"] = fmt.Sprintf("%v", fs.Id) |
| 346 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 347 | if deviceRules != nil { |
| 348 | context["device-rules"] = deviceRules.String() |
| 349 | } |
| 350 | |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 351 | agent.ldeviceMgr.SendRPCEvent(ctx, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 352 | agent.logicalDeviceID, "failed-to-update-device-flows", context, "RPC_ERROR_RAISE_EVENT", |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 353 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 354 | // TODO: Revert the flow deletion |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 355 | // send event, and allow any queued events to be sent as well |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame] | 356 | agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 357 | } |
| 358 | }() |
| 359 | } |
| 360 | //TODO: send announcement on delete |
| 361 | return nil |
| 362 | } |
| 363 | |
| 364 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 365 | func (agent *LogicalAgent) flowDeleteStrict(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error { |
| 366 | mod := flowUpdate.FlowMod |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 367 | logger.Debugw(ctx, "flow-delete-strict", log.Fields{"mod": mod}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 368 | if mod == nil { |
| 369 | return nil |
| 370 | } |
| 371 | |
| 372 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 373 | if err != nil { |
| 374 | return err |
| 375 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 376 | logger.Debugw(ctx, "flow-id-in-flow-delete-strict", log.Fields{"flow-id": flow.Id}) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 377 | flowHandle, have := agent.flowCache.Lock(flow.Id) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 378 | if !have { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 379 | 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] | 380 | return nil |
| 381 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 382 | defer flowHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 383 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 384 | groups := make(map[uint32]*ofp.OfpGroupEntry) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 385 | for groupID := range agent.groupCache.ListIDs() { |
| 386 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 387 | groups[groupID] = groupHandle.GetReadOnly() |
| 388 | groupHandle.Unlock() |
| 389 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 390 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 391 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 392 | if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !changedMeter { |
| 393 | return fmt.Errorf("Cannot delete flow - %s. Meter update failed", flow) |
| 394 | } |
| 395 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 396 | flowsToDelete := map[uint64]*ofp.OfpFlowStats{flow.Id: flowHandle.GetReadOnly()} |
| 397 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 398 | var respChnls []coreutils.Response |
| 399 | var partialRoute bool |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 400 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, flowsToDelete, groups) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 401 | if err != nil { |
| 402 | // A no route error means no route exists between the ports specified in the flow. This can happen when the |
| 403 | // child device is deleted and a request to delete flows from the parent device is received |
| 404 | if !errors.Is(err, route.ErrNoRoute) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 405 | 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] | 406 | return err |
| 407 | } |
| 408 | partialRoute = true |
| 409 | } |
| 410 | |
| 411 | // Update the model |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 412 | if err := flowHandle.Delete(ctx); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 413 | return err |
| 414 | } |
| 415 | // Update the devices |
| 416 | if partialRoute { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 417 | respChnls = agent.deleteFlowsFromParentDevice(ctx, flowsToDelete, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 418 | } else { |
Gamze Abaka | fac8c19 | 2021-06-28 12:04:32 +0000 | [diff] [blame] | 419 | respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // Wait for completion |
| 423 | go func() { |
| 424 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 425 | logger.Warnw(ctx, "failure-deleting-device-flows", log.Fields{ |
Matteo Scandolo | 367162b | 2020-06-22 15:07:33 -0700 | [diff] [blame] | 426 | "flow-cookie": mod.Cookie, |
| 427 | "logical-device-id": agent.logicalDeviceID, |
| 428 | "errors": res, |
| 429 | }) |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 430 | // TODO: Revert flow changes |
| 431 | // send event, and allow any queued events to be sent as well |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame] | 432 | agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 433 | context := make(map[string]string) |
| 434 | context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx) |
Himani Chawla | 0351077 | 2021-02-17 12:48:49 +0530 | [diff] [blame] | 435 | context["flow-id"] = fmt.Sprintf("%v", flow.Id) |
| 436 | context["flow-cookie"] = fmt.Sprintf("%v", flowUpdate.FlowMod.Cookie) |
| 437 | context["logical-device-id"] = agent.logicalDeviceID |
| 438 | if deviceRules != nil { |
| 439 | context["device-rules"] = deviceRules.String() |
| 440 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 441 | // Create context and send extra information as part of it. |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 442 | agent.ldeviceMgr.SendRPCEvent(ctx, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 443 | agent.logicalDeviceID, "failed-to-delete-device-flows", context, "RPC_ERROR_RAISE_EVENT", |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 444 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 445 | } |
| 446 | }() |
| 447 | |
| 448 | return nil |
| 449 | } |
| 450 | |
| 451 | //flowModify modifies a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 452 | func (agent *LogicalAgent) flowModify(flowUpdate *ofp.FlowTableUpdate) error { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 453 | return errors.New("flowModify not implemented") |
| 454 | } |
| 455 | |
| 456 | //flowModifyStrict deletes a flow from the flow table of that logical device |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 457 | func (agent *LogicalAgent) flowModifyStrict(flowUpdate *ofp.FlowTableUpdate) error { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 458 | return errors.New("flowModifyStrict not implemented") |
| 459 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 460 | |
| 461 | // TODO: Remove this helper, just pass the map through to functions directly |
| 462 | func toMetadata(meters map[uint32]*ofp.OfpMeterConfig) *voltha.FlowMetadata { |
| 463 | ctr, ret := 0, make([]*ofp.OfpMeterConfig, len(meters)) |
| 464 | for _, meter := range meters { |
| 465 | ret[ctr] = meter |
| 466 | ctr++ |
| 467 | } |
| 468 | return &voltha.FlowMetadata{Meters: ret} |
| 469 | } |
| 470 | |
| 471 | func (agent *LogicalAgent) deleteFlowsHavingMeter(ctx context.Context, meterID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 472 | logger.Infow(ctx, "delete-flows-matching-meter", log.Fields{"meter": meterID}) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 473 | for flowID := range agent.flowCache.ListIDs() { |
| 474 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 475 | if flowMeterID := fu.GetMeterIdFromFlow(flowHandle.GetReadOnly()); flowMeterID != 0 && flowMeterID == meterID { |
| 476 | if err := flowHandle.Delete(ctx); err != nil { |
| 477 | //TODO: Think on carrying on and deleting the remaining flows, instead of returning. |
| 478 | //Anyways this returns an error to controller which possibly results with a re-deletion. |
| 479 | //Then how can we handle the new deletion request(Same for group deletion)? |
| 480 | return err |
| 481 | } |
| 482 | } |
| 483 | flowHandle.Unlock() |
| 484 | } |
| 485 | } |
| 486 | return nil |
| 487 | } |
| 488 | |
| 489 | 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] | 490 | logger.Infow(ctx, "delete-flows-matching-group", log.Fields{"group-id": groupID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 491 | flowsRemoved := make(map[uint64]*ofp.OfpFlowStats) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 492 | for flowID := range agent.flowCache.ListIDs() { |
| 493 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 494 | if flow := flowHandle.GetReadOnly(); fu.FlowHasOutGroup(flow, groupID) { |
| 495 | if err := flowHandle.Delete(ctx); err != nil { |
| 496 | return nil, err |
| 497 | } |
| 498 | flowsRemoved[flowID] = flow |
| 499 | } |
| 500 | flowHandle.Unlock() |
| 501 | } |
| 502 | } |
| 503 | return flowsRemoved, nil |
| 504 | } |