blob: 2b88abc280808f809a62a72cc595bd34e9aaf991 [file] [log] [blame]
Kent Hagerman3136fbd2020-05-14 10:30:45 -04001/*
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
17package device
18
19import (
20 "context"
21 "errors"
22 "fmt"
Himani Chawla531af4f2020-09-22 10:42:17 +053023 "strconv"
Himani Chawlab4c25912020-11-12 17:16:38 +053024 "time"
Himani Chawla531af4f2020-09-22 10:42:17 +053025
Kent Hagerman3136fbd2020-05-14 10:30:45 -040026 "github.com/gogo/protobuf/proto"
27 "github.com/opencord/voltha-go/rw_core/route"
28 coreutils "github.com/opencord/voltha-go/rw_core/utils"
Maninderdfadc982020-10-28 14:04:33 +053029 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 Hagerman3136fbd2020-05-14 10:30:45 -040033 "google.golang.org/grpc/codes"
34 "google.golang.org/grpc/status"
35)
36
Kent Hagerman433a31a2020-05-20 19:04:48 -040037// listLogicalDeviceFlows returns logical device flows
38func (agent *LogicalAgent) listLogicalDeviceFlows() map[uint64]*ofp.OfpFlowStats {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040039 flowIDs := agent.flowLoader.ListIDs()
Kent Hagerman433a31a2020-05-20 19:04:48 -040040 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 Hagerman3136fbd2020-05-14 10:30:45 -040050//updateFlowTable updates the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +053051func (agent *LogicalAgent) updateFlowTable(ctx context.Context, flow *ofp.FlowTableUpdate) error {
Himani Chawlab4c25912020-11-12 17:16:38 +053052 logger.Debug(ctx, "update-flow-table")
Kent Hagerman3136fbd2020-05-14 10:30:45 -040053 if flow == nil {
54 return nil
55 }
56
Maninderf421da62020-12-04 11:44:58 +053057 switch flow.FlowMod.GetCommand() {
Kent Hagerman3136fbd2020-05-14 10:30:45 -040058 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,
Maninderf421da62020-12-04 11:44:58 +053070 "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, flow.FlowMod.GetCommand())
Kent Hagerman3136fbd2020-05-14 10:30:45 -040071}
72
73//flowAdd adds a flow to the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +053074func (agent *LogicalAgent) flowAdd(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error {
75 mod := flowUpdate.FlowMod
Himani Chawlab4c25912020-11-12 17:16:38 +053076 logger.Debugw(ctx, "flow-add", log.Fields{"flow": mod})
Kent Hagerman3136fbd2020-05-14 10:30:45 -040077 if mod == nil {
78 return nil
79 }
80 flow, err := fu.FlowStatsEntryFromFlowModMessage(mod)
81 if err != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +053082 logger.Errorw(ctx, "flow-add-failed", log.Fields{"flow-mod": mod, "err": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -040083 return err
84 }
85 var updated bool
86 var changed bool
Maninderf421da62020-12-04 11:44:58 +053087 if changed, updated, err = agent.decomposeAndAdd(ctx, flow, flowUpdate); err != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +053088 logger.Errorw(ctx, "flow-decompose-and-add-failed ", log.Fields{"flow-mod": mod, "err": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -040089 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
Maninderf421da62020-12-04 11:44:58 +0530100func (agent *LogicalAgent) decomposeAndAdd(ctx context.Context, flow *ofp.OfpFlowStats, flowUpdate *ofp.FlowTableUpdate) (bool, bool, error) {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400101 changed := false
102 updated := false
Maninderf421da62020-12-04 11:44:58 +0530103 mod := flowUpdate.FlowMod
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400104 var flowToReplace *ofp.OfpFlowStats
105
106 //if flow is not found in the map, create a new entry, otherwise get the existing one.
Kent Hagerman433a31a2020-05-20 19:04:48 -0400107 flowHandle, created, err := agent.flowLoader.LockOrCreate(ctx, flow)
108 if err != nil {
109 return changed, updated, err
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400110 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400111 defer flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400112
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400113 flows := make([]*ofp.OfpFlowStats, 0)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400114 checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0
115 if checkOverlap {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400116 // TODO: this currently does nothing
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400117 if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400118 // TODO: should this error be notified other than being logged?
Himani Chawlab4c25912020-11-12 17:16:38 +0530119 logger.Warnw(ctx, "overlapped-flows", log.Fields{"logical-device-id": agent.logicalDeviceID})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400120 } else {
121 // Add flow
122 changed = true
123 }
124 } else {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400125 if !created {
126 flowToReplace = flowHandle.GetReadOnly()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400127 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 Agrawal31f21802020-06-12 05:38:46 +0000139 logger.Debugw(ctx, "flowAdd-changed", log.Fields{"changed": changed, "updated": updated})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400140 if changed {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400141 updatedFlows := map[uint64]*ofp.OfpFlowStats{flow.Id: flow}
142
Rohan Agrawal31f21802020-06-12 05:38:46 +0000143 flowMeterConfig, err := agent.GetMeterConfig(ctx, updatedFlows)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400144 if err != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +0530145 logger.Error(ctx, "meter-referred-in-flow-not-present")
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400146 return changed, updated, err
147 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400148
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400149 groupIDs := agent.groupLoader.ListIDs()
Kent Hagerman433a31a2020-05-20 19:04:48 -0400150 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 Hagerman3136fbd2020-05-14 10:30:45 -0400159 if err != nil {
160 return changed, updated, err
161 }
162
Rohan Agrawal31f21802020-06-12 05:38:46 +0000163 logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400164 // Update store and cache
165 if updated {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400166 if err := flowHandle.Update(ctx, flow); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400167 return changed, updated, err
168 }
169 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000170 respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules, toMetadata(flowMeterConfig))
Maninderf421da62020-12-04 11:44:58 +0530171
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400172 // Create the go routines to wait
173 go func() {
174 // Wait for completion
175 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChannels...); res != nil {
Himani Chawla531af4f2020-09-22 10:42:17 +0530176 logger.Infow(ctx, "failed-to-add-flow-will-attempt-deletion", log.Fields{
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700177 "errors": res,
178 "logical-device-id": agent.logicalDeviceID,
Himani Chawla531af4f2020-09-22 10:42:17 +0530179 "flow": flow,
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700180 "groups": groups,
181 })
Himani Chawlab4c25912020-11-12 17:16:38 +0530182 subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx)
183
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400184 // Revert added flows
Himani Chawlab4c25912020-11-12 17:16:38 +0530185 if err := agent.revertAddedFlows(subCtx, mod, flow, flowToReplace, deviceRules, toMetadata(flowMeterConfig)); err != nil {
Himani Chawla531af4f2020-09-22 10:42:17 +0530186 logger.Errorw(ctx, "failure-to-delete-flow-after-failed-addition", log.Fields{
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700187 "error": err,
188 "logical-device-id": agent.logicalDeviceID,
Himani Chawla531af4f2020-09-22 10:42:17 +0530189 "flow": flow,
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700190 "groups": groups,
191 })
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400192 }
Maninderf421da62020-12-04 11:44:58 +0530193 // send event
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100194 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Himani Chawlab4c25912020-11-12 17:16:38 +0530195 context := make(map[string]string)
196 context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx)
197 context["flow-id"] = string(flow.Id)
198 context["device-rules"] = deviceRules.String()
199 go agent.ldeviceMgr.SendRPCEvent(ctx,
200 agent.logicalDeviceID, "failed-to-add-flow", context, "RPC_ERROR_RAISE_EVENT",
201 voltha.EventCategory_COMMUNICATION, nil, time.Now().UnixNano())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400202 }
203 }()
204 }
205 return changed, updated, nil
206}
207
208// revertAddedFlows reverts flows after the flowAdd request has failed. All flows corresponding to that flowAdd request
209// will be reverted, both from the logical devices and the devices.
210func (agent *LogicalAgent) revertAddedFlows(ctx context.Context, mod *ofp.OfpFlowMod, addedFlow *ofp.OfpFlowStats, replacedFlow *ofp.OfpFlowStats, deviceRules *fu.DeviceRules, metadata *voltha.FlowMetadata) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530211 logger.Debugw(ctx, "revert-flow-add", log.Fields{"added-flow": addedFlow, "replaced-flow": replacedFlow, "device-rules": deviceRules, "metadata": metadata})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400212
Kent Hagerman433a31a2020-05-20 19:04:48 -0400213 flowHandle, have := agent.flowLoader.Lock(addedFlow.Id)
214 if !have {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400215 // Not found - do nothing
Girish Kumar3e8ee212020-08-19 17:50:11 +0000216 logger.Debugw(ctx, "flow-not-found", log.Fields{"added-flow": addedFlow})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400217 return nil
218 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400219 defer flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400220
221 if replacedFlow != nil {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400222 if err := flowHandle.Update(ctx, replacedFlow); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400223 return err
224 }
225 } else {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400226 if err := flowHandle.Delete(ctx); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400227 return err
228 }
229 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400230
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400231 // Revert meters
232 if changedMeterStats := agent.updateFlowCountOfMeterStats(ctx, mod, addedFlow, true); !changedMeterStats {
233 return fmt.Errorf("Unable-to-revert-meterstats-for-flow-%s", strconv.FormatUint(addedFlow.Id, 10))
234 }
235
236 // Update the devices
Rohan Agrawal31f21802020-06-12 05:38:46 +0000237 respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, metadata, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400238
239 // Wait for the responses
240 go func() {
241 // Since this action is taken following an add failure, we may also receive a failure for the revert
242 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000243 logger.Warnw(ctx, "failure-reverting-added-flows", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700244 "logical-device-id": agent.logicalDeviceID,
245 "flow-cookie": mod.Cookie,
246 "errors": res,
247 })
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400248 }
249 }()
250
251 return nil
252}
253
254//flowDelete deletes a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530255func (agent *LogicalAgent) flowDelete(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530256 logger.Debug(ctx, "flow-delete")
Maninderf421da62020-12-04 11:44:58 +0530257 mod := flowUpdate.FlowMod
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400258 if mod == nil {
259 return nil
260 }
261
Kent Hagerman433a31a2020-05-20 19:04:48 -0400262 //build a list of what to delete
263 toDelete := make(map[uint64]*ofp.OfpFlowStats)
264
265 // add perfectly matching entry if exists
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400266 fs, err := fu.FlowStatsEntryFromFlowModMessage(mod)
267 if err != nil {
268 return err
269 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400270 if handle, have := agent.flowLoader.Lock(fs.Id); have {
271 toDelete[fs.Id] = handle.GetReadOnly()
272 handle.Unlock()
273 }
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400274
Kent Hagerman433a31a2020-05-20 19:04:48 -0400275 // search through all the flows
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400276 for flowID := range agent.flowLoader.ListIDs() {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400277 if flowHandle, have := agent.flowLoader.Lock(flowID); have {
278 if flow := flowHandle.GetReadOnly(); fu.FlowMatchesMod(flow, mod) {
279 toDelete[flow.Id] = flow
280 }
281 flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400282 }
283 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400284
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400285 //Delete the matched flows
286 if len(toDelete) > 0 {
Himani Chawlab4c25912020-11-12 17:16:38 +0530287 logger.Debugw(ctx, "flow-delete", log.Fields{"logical-device-id": agent.logicalDeviceID, "to-delete": len(toDelete)})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400288
Kent Hagerman433a31a2020-05-20 19:04:48 -0400289 for _, flow := range toDelete {
290 if flowHandle, have := agent.flowLoader.Lock(flow.Id); have {
291 // TODO: Flow should only be updated if meter is updated, and meter should only be updated if flow is updated
292 // currently an error while performing the second operation will leave an inconsistent state in kv.
293 // This should be a single atomic operation down to the kv.
294 if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flowHandle.GetReadOnly(), false); !changedMeter {
295 flowHandle.Unlock()
296 return fmt.Errorf("cannot-delete-flow-%d. Meter-update-failed", flow.Id)
297 }
298 // Update store and cache
299 if err := flowHandle.Delete(ctx); err != nil {
300 flowHandle.Unlock()
301 return fmt.Errorf("cannot-delete-flows-%d. Delete-from-store-failed", flow.Id)
302 }
303 flowHandle.Unlock()
304 // TODO: since this is executed in a loop without also updating meter stats, and error part way through this
305 // operation will leave inconsistent state in the meter stats & flows on the devices.
306 // This & related meter updates should be a single atomic operation down to the kv.
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400307 }
308 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400309
Rohan Agrawal31f21802020-06-12 05:38:46 +0000310 metersConfig, err := agent.GetMeterConfig(ctx, toDelete)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400311 if err != nil { // This should never happen
Himani Chawlab4c25912020-11-12 17:16:38 +0530312 logger.Error(ctx, "meter-referred-in-flows-not-present")
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400313 return err
314 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400315
316 groups := make(map[uint32]*ofp.OfpGroupEntry)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400317 for groupID := range agent.groupLoader.ListIDs() {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400318 if groupHandle, have := agent.groupLoader.Lock(groupID); have {
319 groups[groupID] = groupHandle.GetReadOnly()
320 groupHandle.Unlock()
321 }
322 }
323
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400324 var respChnls []coreutils.Response
325 var partialRoute bool
326 var deviceRules *fu.DeviceRules
Kent Hagerman433a31a2020-05-20 19:04:48 -0400327 deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, toDelete, groups)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400328 if err != nil {
329 // A no route error means no route exists between the ports specified in the flow. This can happen when the
330 // child device is deleted and a request to delete flows from the parent device is received
331 if !errors.Is(err, route.ErrNoRoute) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000332 logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": toDelete, "error": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400333 return err
334 }
335 partialRoute = true
336 }
337
338 // Update the devices
339 if partialRoute {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000340 respChnls = agent.deleteFlowsFromParentDevice(ctx, toDelete, toMetadata(metersConfig), mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400341 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000342 respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, toMetadata(metersConfig), mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400343 }
344
345 // Wait for the responses
346 go func() {
347 // Wait for completion
348 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +0530349 logger.Errorw(ctx, "failure-updating-device-flows", log.Fields{"logical-device-id": agent.logicalDeviceID, "errors": res})
350 context := make(map[string]string)
351 context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx)
352 context["device-rules"] = deviceRules.String()
353 go agent.ldeviceMgr.SendRPCEvent(ctx,
354 agent.logicalDeviceID, "failed-to-update-device-flows", context, "RPC_ERROR_RAISE_EVENT",
355 voltha.EventCategory_COMMUNICATION, nil, time.Now().UnixNano())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400356 // TODO: Revert the flow deletion
Maninderf421da62020-12-04 11:44:58 +0530357 // send event, and allow any queued events to be sent as well
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100358 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400359 }
360 }()
361 }
362 //TODO: send announcement on delete
363 return nil
364}
365
366//flowDeleteStrict deletes a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530367func (agent *LogicalAgent) flowDeleteStrict(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error {
368 mod := flowUpdate.FlowMod
Himani Chawlab4c25912020-11-12 17:16:38 +0530369 logger.Debugw(ctx, "flow-delete-strict", log.Fields{"mod": mod})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400370 if mod == nil {
371 return nil
372 }
373
374 flow, err := fu.FlowStatsEntryFromFlowModMessage(mod)
375 if err != nil {
376 return err
377 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530378 logger.Debugw(ctx, "flow-id-in-flow-delete-strict", log.Fields{"flow-id": flow.Id})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400379 flowHandle, have := agent.flowLoader.Lock(flow.Id)
380 if !have {
Himani Chawlab4c25912020-11-12 17:16:38 +0530381 logger.Debugw(ctx, "skipping-flow-delete-strict-request-no-flow-found", log.Fields{"flow-mod": mod})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400382 return nil
383 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400384 defer flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400385
Kent Hagerman433a31a2020-05-20 19:04:48 -0400386 groups := make(map[uint32]*ofp.OfpGroupEntry)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400387 for groupID := range agent.groupLoader.ListIDs() {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400388 if groupHandle, have := agent.groupLoader.Lock(groupID); have {
389 groups[groupID] = groupHandle.GetReadOnly()
390 groupHandle.Unlock()
391 }
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400392 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400393
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400394 if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !changedMeter {
395 return fmt.Errorf("Cannot delete flow - %s. Meter update failed", flow)
396 }
397
Kent Hagerman433a31a2020-05-20 19:04:48 -0400398 flowsToDelete := map[uint64]*ofp.OfpFlowStats{flow.Id: flowHandle.GetReadOnly()}
399
Rohan Agrawal31f21802020-06-12 05:38:46 +0000400 flowMetadata, err := agent.GetMeterConfig(ctx, flowsToDelete)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400401 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000402 logger.Error(ctx, "meter-referred-in-flows-not-present")
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400403 return err
404 }
405 var respChnls []coreutils.Response
406 var partialRoute bool
Kent Hagerman433a31a2020-05-20 19:04:48 -0400407 deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, flowsToDelete, groups)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400408 if err != nil {
409 // A no route error means no route exists between the ports specified in the flow. This can happen when the
410 // child device is deleted and a request to delete flows from the parent device is received
411 if !errors.Is(err, route.ErrNoRoute) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000412 logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": flowsToDelete, "error": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400413 return err
414 }
415 partialRoute = true
416 }
417
418 // Update the model
Kent Hagerman433a31a2020-05-20 19:04:48 -0400419 if err := flowHandle.Delete(ctx); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400420 return err
421 }
422 // Update the devices
423 if partialRoute {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000424 respChnls = agent.deleteFlowsFromParentDevice(ctx, flowsToDelete, toMetadata(flowMetadata), mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400425 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000426 respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, toMetadata(flowMetadata), mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400427 }
428
429 // Wait for completion
430 go func() {
431 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000432 logger.Warnw(ctx, "failure-deleting-device-flows", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700433 "flow-cookie": mod.Cookie,
434 "logical-device-id": agent.logicalDeviceID,
435 "errors": res,
436 })
Maninderf421da62020-12-04 11:44:58 +0530437 // TODO: Revert flow changes
438 // send event, and allow any queued events to be sent as well
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100439 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Himani Chawlab4c25912020-11-12 17:16:38 +0530440 context := make(map[string]string)
441 context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx)
442 context["flow-id"] = string(flow.Id)
443 context["device-rules"] = deviceRules.String()
444 // Create context and send extra information as part of it.
445 go agent.ldeviceMgr.SendRPCEvent(ctx,
446 agent.logicalDeviceID, "failed-to-delete-device-flows", context, "RPC_ERROR_RAISE_EVENT",
447 voltha.EventCategory_COMMUNICATION, nil, time.Now().UnixNano())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400448 }
449 }()
450
451 return nil
452}
453
454//flowModify modifies a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530455func (agent *LogicalAgent) flowModify(flowUpdate *ofp.FlowTableUpdate) error {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400456 return errors.New("flowModify not implemented")
457}
458
459//flowModifyStrict deletes a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530460func (agent *LogicalAgent) flowModifyStrict(flowUpdate *ofp.FlowTableUpdate) error {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400461 return errors.New("flowModifyStrict not implemented")
462}
Kent Hagerman433a31a2020-05-20 19:04:48 -0400463
464// TODO: Remove this helper, just pass the map through to functions directly
465func toMetadata(meters map[uint32]*ofp.OfpMeterConfig) *voltha.FlowMetadata {
466 ctr, ret := 0, make([]*ofp.OfpMeterConfig, len(meters))
467 for _, meter := range meters {
468 ret[ctr] = meter
469 ctr++
470 }
471 return &voltha.FlowMetadata{Meters: ret}
472}
473
474func (agent *LogicalAgent) deleteFlowsHavingMeter(ctx context.Context, meterID uint32) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530475 logger.Infow(ctx, "delete-flows-matching-meter", log.Fields{"meter": meterID})
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400476 for flowID := range agent.flowLoader.ListIDs() {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400477 if flowHandle, have := agent.flowLoader.Lock(flowID); have {
478 if flowMeterID := fu.GetMeterIdFromFlow(flowHandle.GetReadOnly()); flowMeterID != 0 && flowMeterID == meterID {
479 if err := flowHandle.Delete(ctx); err != nil {
480 //TODO: Think on carrying on and deleting the remaining flows, instead of returning.
481 //Anyways this returns an error to controller which possibly results with a re-deletion.
482 //Then how can we handle the new deletion request(Same for group deletion)?
483 return err
484 }
485 }
486 flowHandle.Unlock()
487 }
488 }
489 return nil
490}
491
492func (agent *LogicalAgent) deleteFlowsHavingGroup(ctx context.Context, groupID uint32) (map[uint64]*ofp.OfpFlowStats, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530493 logger.Infow(ctx, "delete-flows-matching-group", log.Fields{"group-id": groupID})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400494 flowsRemoved := make(map[uint64]*ofp.OfpFlowStats)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400495 for flowID := range agent.flowLoader.ListIDs() {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400496 if flowHandle, have := agent.flowLoader.Lock(flowID); have {
497 if flow := flowHandle.GetReadOnly(); fu.FlowHasOutGroup(flow, groupID) {
498 if err := flowHandle.Delete(ctx); err != nil {
499 return nil, err
500 }
501 flowsRemoved[flowID] = flow
502 }
503 flowHandle.Unlock()
504 }
505 }
506 return flowsRemoved, nil
507}