blob: 72b915fb6ebe8ebc147e854249b17e2372ba8103 [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"
yasin sapli5458a1c2021-06-14 22:24:38 +000029 fu "github.com/opencord/voltha-lib-go/v5/pkg/flows"
30 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Maninderdfadc982020-10-28 14:04:33 +053031 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 {
khenaidoo7585a962021-06-10 16:15:38 -040039 flowIDs := agent.flowCache.ListIDs()
Kent Hagerman433a31a2020-05-20 19:04:48 -040040 flows := make(map[uint64]*ofp.OfpFlowStats, len(flowIDs))
41 for flowID := range flowIDs {
khenaidoo7585a962021-06-10 16:15:38 -040042 if flowHandle, have := agent.flowCache.Lock(flowID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -040043 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.
khenaidoo7585a962021-06-10 16:15:38 -0400107 flowHandle, created, err := agent.flowCache.LockOrCreate(ctx, flow)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400108 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
khenaidoo7585a962021-06-10 16:15:38 -0400143 groupIDs := agent.groupCache.ListIDs()
Kent Hagerman433a31a2020-05-20 19:04:48 -0400144 groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs))
145 for groupID := range groupIDs {
khenaidoo7585a962021-06-10 16:15:38 -0400146 if groupHandle, have := agent.groupCache.Lock(groupID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400147 groups[groupID] = groupHandle.GetReadOnly()
148 groupHandle.Unlock()
149 }
150 }
151
152 deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, updatedFlows, groups)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400153 if err != nil {
154 return changed, updated, err
155 }
156
Rohan Agrawal31f21802020-06-12 05:38:46 +0000157 logger.Debugw(ctx, "rules", log.Fields{"rules": deviceRules.String()})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400158 // Update store and cache
159 if updated {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400160 if err := flowHandle.Update(ctx, flow); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400161 return changed, updated, err
162 }
163 }
Gamze Abakafac8c192021-06-28 12:04:32 +0000164 respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules)
Maninderf421da62020-12-04 11:44:58 +0530165
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400166 // Create the go routines to wait
167 go func() {
168 // Wait for completion
169 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChannels...); res != nil {
ssiddiqui21e54c32021-07-27 11:30:46 +0530170 logger.Errorw(ctx, "failed-to-add-flow-will-attempt-deletion", log.Fields{
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700171 "errors": res,
172 "logical-device-id": agent.logicalDeviceID,
Himani Chawla531af4f2020-09-22 10:42:17 +0530173 "flow": flow,
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700174 "groups": groups,
175 })
Himani Chawlab4c25912020-11-12 17:16:38 +0530176 subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx)
177
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400178 // Revert added flows
Gamze Abakafac8c192021-06-28 12:04:32 +0000179 if err := agent.revertAddedFlows(subCtx, mod, flow, flowToReplace, deviceRules); err != nil {
Himani Chawla531af4f2020-09-22 10:42:17 +0530180 logger.Errorw(ctx, "failure-to-delete-flow-after-failed-addition", log.Fields{
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700181 "error": err,
182 "logical-device-id": agent.logicalDeviceID,
Himani Chawla531af4f2020-09-22 10:42:17 +0530183 "flow": flow,
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700184 "groups": groups,
185 })
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400186 }
Maninderf421da62020-12-04 11:44:58 +0530187 // send event
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100188 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Himani Chawlab4c25912020-11-12 17:16:38 +0530189 context := make(map[string]string)
190 context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx)
Himani Chawla03510772021-02-17 12:48:49 +0530191 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 Chawla606a4f02021-03-23 19:45:58 +0530197 agent.ldeviceMgr.SendRPCEvent(ctx,
Himani Chawlab4c25912020-11-12 17:16:38 +0530198 agent.logicalDeviceID, "failed-to-add-flow", context, "RPC_ERROR_RAISE_EVENT",
Himani Chawla606a4f02021-03-23 19:45:58 +0530199 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400200 }
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 Abakafac8c192021-06-28 12:04:32 +0000208func (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 Hagerman3136fbd2020-05-14 10:30:45 -0400210
khenaidoo7585a962021-06-10 16:15:38 -0400211 flowHandle, have := agent.flowCache.Lock(addedFlow.Id)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400212 if !have {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400213 // Not found - do nothing
Girish Kumar3e8ee212020-08-19 17:50:11 +0000214 logger.Debugw(ctx, "flow-not-found", log.Fields{"added-flow": addedFlow})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400215 return nil
216 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400217 defer flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400218
219 if replacedFlow != nil {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400220 if err := flowHandle.Update(ctx, replacedFlow); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400221 return err
222 }
223 } else {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400224 if err := flowHandle.Delete(ctx); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400225 return err
226 }
227 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400228
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400229 // 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 Abakafac8c192021-06-28 12:04:32 +0000235 respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400236
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 Agrawal31f21802020-06-12 05:38:46 +0000241 logger.Warnw(ctx, "failure-reverting-added-flows", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700242 "logical-device-id": agent.logicalDeviceID,
243 "flow-cookie": mod.Cookie,
244 "errors": res,
245 })
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400246 }
247 }()
248
249 return nil
250}
251
252//flowDelete deletes a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530253func (agent *LogicalAgent) flowDelete(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530254 logger.Debug(ctx, "flow-delete")
Maninderf421da62020-12-04 11:44:58 +0530255 mod := flowUpdate.FlowMod
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400256 if mod == nil {
257 return nil
258 }
259
Kent Hagerman433a31a2020-05-20 19:04:48 -0400260 //build a list of what to delete
261 toDelete := make(map[uint64]*ofp.OfpFlowStats)
262
263 // add perfectly matching entry if exists
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400264 fs, err := fu.FlowStatsEntryFromFlowModMessage(mod)
265 if err != nil {
266 return err
267 }
khenaidoo7585a962021-06-10 16:15:38 -0400268 if handle, have := agent.flowCache.Lock(fs.Id); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400269 toDelete[fs.Id] = handle.GetReadOnly()
270 handle.Unlock()
271 }
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400272
Kent Hagerman433a31a2020-05-20 19:04:48 -0400273 // search through all the flows
khenaidoo7585a962021-06-10 16:15:38 -0400274 for flowID := range agent.flowCache.ListIDs() {
275 if flowHandle, have := agent.flowCache.Lock(flowID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400276 if flow := flowHandle.GetReadOnly(); fu.FlowMatchesMod(flow, mod) {
277 toDelete[flow.Id] = flow
278 }
279 flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400280 }
281 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400282
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400283 //Delete the matched flows
284 if len(toDelete) > 0 {
Himani Chawlab4c25912020-11-12 17:16:38 +0530285 logger.Debugw(ctx, "flow-delete", log.Fields{"logical-device-id": agent.logicalDeviceID, "to-delete": len(toDelete)})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400286
Kent Hagerman433a31a2020-05-20 19:04:48 -0400287 for _, flow := range toDelete {
khenaidoo7585a962021-06-10 16:15:38 -0400288 if flowHandle, have := agent.flowCache.Lock(flow.Id); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400289 // 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 Hagerman3136fbd2020-05-14 10:30:45 -0400305 }
306 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400307
Kent Hagerman433a31a2020-05-20 19:04:48 -0400308 groups := make(map[uint32]*ofp.OfpGroupEntry)
khenaidoo7585a962021-06-10 16:15:38 -0400309 for groupID := range agent.groupCache.ListIDs() {
310 if groupHandle, have := agent.groupCache.Lock(groupID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400311 groups[groupID] = groupHandle.GetReadOnly()
312 groupHandle.Unlock()
313 }
314 }
315
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400316 var respChnls []coreutils.Response
317 var partialRoute bool
318 var deviceRules *fu.DeviceRules
Kent Hagerman433a31a2020-05-20 19:04:48 -0400319 deviceRules, err = agent.flowDecomposer.DecomposeRules(ctx, agent, toDelete, groups)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400320 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 Agrawal31f21802020-06-12 05:38:46 +0000324 logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": toDelete, "error": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400325 return err
326 }
327 partialRoute = true
328 }
329
330 // Update the devices
331 if partialRoute {
Gamze Abakafac8c192021-06-28 12:04:32 +0000332 respChnls = agent.deleteFlowsFromParentDevice(ctx, toDelete, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400333 } else {
Gamze Abakafac8c192021-06-28 12:04:32 +0000334 respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400335 }
336
337 // Wait for the responses
338 go func() {
339 // Wait for completion
340 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +0530341 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 Chawla03510772021-02-17 12:48:49 +0530344 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 Chawla606a4f02021-03-23 19:45:58 +0530351 agent.ldeviceMgr.SendRPCEvent(ctx,
Himani Chawlab4c25912020-11-12 17:16:38 +0530352 agent.logicalDeviceID, "failed-to-update-device-flows", context, "RPC_ERROR_RAISE_EVENT",
Himani Chawla606a4f02021-03-23 19:45:58 +0530353 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400354 // TODO: Revert the flow deletion
Maninderf421da62020-12-04 11:44:58 +0530355 // send event, and allow any queued events to be sent as well
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100356 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400357 }
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
Maninderf421da62020-12-04 11:44:58 +0530365func (agent *LogicalAgent) flowDeleteStrict(ctx context.Context, flowUpdate *ofp.FlowTableUpdate) error {
366 mod := flowUpdate.FlowMod
Himani Chawlab4c25912020-11-12 17:16:38 +0530367 logger.Debugw(ctx, "flow-delete-strict", log.Fields{"mod": mod})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400368 if mod == nil {
369 return nil
370 }
371
372 flow, err := fu.FlowStatsEntryFromFlowModMessage(mod)
373 if err != nil {
374 return err
375 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530376 logger.Debugw(ctx, "flow-id-in-flow-delete-strict", log.Fields{"flow-id": flow.Id})
khenaidoo7585a962021-06-10 16:15:38 -0400377 flowHandle, have := agent.flowCache.Lock(flow.Id)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400378 if !have {
Himani Chawlab4c25912020-11-12 17:16:38 +0530379 logger.Debugw(ctx, "skipping-flow-delete-strict-request-no-flow-found", log.Fields{"flow-mod": mod})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400380 return nil
381 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400382 defer flowHandle.Unlock()
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400383
Kent Hagerman433a31a2020-05-20 19:04:48 -0400384 groups := make(map[uint32]*ofp.OfpGroupEntry)
khenaidoo7585a962021-06-10 16:15:38 -0400385 for groupID := range agent.groupCache.ListIDs() {
386 if groupHandle, have := agent.groupCache.Lock(groupID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400387 groups[groupID] = groupHandle.GetReadOnly()
388 groupHandle.Unlock()
389 }
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400390 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400391
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400392 if changedMeter := agent.updateFlowCountOfMeterStats(ctx, mod, flow, false); !changedMeter {
393 return fmt.Errorf("Cannot delete flow - %s. Meter update failed", flow)
394 }
395
Kent Hagerman433a31a2020-05-20 19:04:48 -0400396 flowsToDelete := map[uint64]*ofp.OfpFlowStats{flow.Id: flowHandle.GetReadOnly()}
397
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400398 var respChnls []coreutils.Response
399 var partialRoute bool
Kent Hagerman433a31a2020-05-20 19:04:48 -0400400 deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, flowsToDelete, groups)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400401 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 Agrawal31f21802020-06-12 05:38:46 +0000405 logger.Errorw(ctx, "unexpected-error-received", log.Fields{"flows-to-delete": flowsToDelete, "error": err})
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400406 return err
407 }
408 partialRoute = true
409 }
410
411 // Update the model
Kent Hagerman433a31a2020-05-20 19:04:48 -0400412 if err := flowHandle.Delete(ctx); err != nil {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400413 return err
414 }
415 // Update the devices
416 if partialRoute {
Gamze Abakafac8c192021-06-28 12:04:32 +0000417 respChnls = agent.deleteFlowsFromParentDevice(ctx, flowsToDelete, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400418 } else {
Gamze Abakafac8c192021-06-28 12:04:32 +0000419 respChnls = agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, mod)
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400420 }
421
422 // Wait for completion
423 go func() {
424 if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000425 logger.Warnw(ctx, "failure-deleting-device-flows", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700426 "flow-cookie": mod.Cookie,
427 "logical-device-id": agent.logicalDeviceID,
428 "errors": res,
429 })
Maninderf421da62020-12-04 11:44:58 +0530430 // TODO: Revert flow changes
431 // send event, and allow any queued events to be sent as well
Andrea Campanella4afb2f02021-01-29 09:38:57 +0100432 agent.ldeviceMgr.SendFlowChangeEvent(ctx, agent.logicalDeviceID, res, flowUpdate.Xid, flowUpdate.FlowMod.Cookie)
Himani Chawlab4c25912020-11-12 17:16:38 +0530433 context := make(map[string]string)
434 context["rpc"] = coreutils.GetRPCMetadataFromContext(ctx)
Himani Chawla03510772021-02-17 12:48:49 +0530435 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 Chawlab4c25912020-11-12 17:16:38 +0530441 // Create context and send extra information as part of it.
Himani Chawla606a4f02021-03-23 19:45:58 +0530442 agent.ldeviceMgr.SendRPCEvent(ctx,
Himani Chawlab4c25912020-11-12 17:16:38 +0530443 agent.logicalDeviceID, "failed-to-delete-device-flows", context, "RPC_ERROR_RAISE_EVENT",
Himani Chawla606a4f02021-03-23 19:45:58 +0530444 voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400445 }
446 }()
447
448 return nil
449}
450
451//flowModify modifies a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530452func (agent *LogicalAgent) flowModify(flowUpdate *ofp.FlowTableUpdate) error {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400453 return errors.New("flowModify not implemented")
454}
455
456//flowModifyStrict deletes a flow from the flow table of that logical device
Maninderf421da62020-12-04 11:44:58 +0530457func (agent *LogicalAgent) flowModifyStrict(flowUpdate *ofp.FlowTableUpdate) error {
Kent Hagerman3136fbd2020-05-14 10:30:45 -0400458 return errors.New("flowModifyStrict not implemented")
459}
Kent Hagerman433a31a2020-05-20 19:04:48 -0400460
461// TODO: Remove this helper, just pass the map through to functions directly
462func 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
471func (agent *LogicalAgent) deleteFlowsHavingMeter(ctx context.Context, meterID uint32) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530472 logger.Infow(ctx, "delete-flows-matching-meter", log.Fields{"meter": meterID})
khenaidoo7585a962021-06-10 16:15:38 -0400473 for flowID := range agent.flowCache.ListIDs() {
474 if flowHandle, have := agent.flowCache.Lock(flowID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400475 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
489func (agent *LogicalAgent) deleteFlowsHavingGroup(ctx context.Context, groupID uint32) (map[uint64]*ofp.OfpFlowStats, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530490 logger.Infow(ctx, "delete-flows-matching-group", log.Fields{"group-id": groupID})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400491 flowsRemoved := make(map[uint64]*ofp.OfpFlowStats)
khenaidoo7585a962021-06-10 16:15:38 -0400492 for flowID := range agent.flowCache.ListIDs() {
493 if flowHandle, have := agent.flowCache.Lock(flowID); have {
Kent Hagerman433a31a2020-05-20 19:04:48 -0400494 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}