Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [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 ( |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 20 | "context" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 21 | "fmt" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/gogo/protobuf/proto" |
| 24 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 25 | fu "github.com/opencord/voltha-lib-go/v5/pkg/flows" |
| 26 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v4/go/common" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 28 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | // listDeviceFlows returns device flows |
| 35 | func (agent *Agent) listDeviceFlows() map[uint64]*ofp.OfpFlowStats { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 36 | flowIDs := agent.flowCache.ListIDs() |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 37 | flows := make(map[uint64]*ofp.OfpFlowStats, len(flowIDs)) |
| 38 | for flowID := range flowIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 39 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 40 | flows[flowID] = flowHandle.GetReadOnly() |
| 41 | flowHandle.Unlock() |
| 42 | } |
| 43 | } |
| 44 | return flows |
| 45 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 46 | |
| 47 | func (agent *Agent) addFlowsToAdapter(ctx context.Context, newFlows []*ofp.OfpFlowStats, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 48 | logger.Debugw(ctx, "add-flows-to-adapters", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "flow-metadata": flowMetadata}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 49 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 50 | var desc string |
| 51 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 52 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 53 | if (len(newFlows)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 54 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": newFlows}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 55 | return coreutils.DoneResponse(), nil |
| 56 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 57 | device, err := agent.getDeviceReadOnly(ctx) |
| 58 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 59 | desc = err.Error() |
| 60 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 61 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 62 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 63 | |
| 64 | if !agent.proceedWithRequest(device) { |
| 65 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", agent.deviceID) |
| 66 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
| 67 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 68 | } |
| 69 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 70 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 71 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 72 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 73 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 74 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 75 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 76 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 77 | flowsToAdd := make([]*ofp.OfpFlowStats, 0) |
| 78 | flowsToDelete := make([]*ofp.OfpFlowStats, 0) |
| 79 | for _, flow := range newFlows { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 80 | flowHandle, created, err := agent.flowCache.LockOrCreate(ctx, flow) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 81 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 82 | desc = err.Error() |
| 83 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 84 | return coreutils.DoneResponse(), err |
| 85 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 86 | if created { |
| 87 | flowsToAdd = append(flowsToAdd, flow) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 88 | } else { |
| 89 | flowToReplace := flowHandle.GetReadOnly() |
| 90 | if !proto.Equal(flowToReplace, flow) { |
| 91 | //Flow needs to be updated. |
| 92 | if err := flowHandle.Update(ctx, flow); err != nil { |
| 93 | flowHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 94 | desc = fmt.Sprintf("failure-updating-flow-%d-to-device-%s", flow.Id, agent.deviceID) |
| 95 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 96 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-flow-%d-to-device-%s", flow.Id, agent.deviceID) |
| 97 | } |
| 98 | flowsToDelete = append(flowsToDelete, flowToReplace) |
| 99 | flowsToAdd = append(flowsToAdd, flow) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 100 | } else { |
| 101 | //No need to change the flow. It is already exist. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 102 | logger.Debugw(ctx, "no-need-to-change-already-existing-flow", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "flow-metadata": flowMetadata}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 105 | flowHandle.Unlock() |
| 106 | } |
| 107 | |
| 108 | // Sanity check |
| 109 | if (len(flowsToAdd)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 110 | logger.Debugw(ctx, "no-flows-to-update", log.Fields{"device-id": agent.deviceID, "flows": newFlows}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 111 | return coreutils.DoneResponse(), nil |
| 112 | } |
| 113 | |
| 114 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 115 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 116 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 117 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 118 | response := coreutils.NewResponse() |
| 119 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 120 | |
| 121 | updatedAllFlows := agent.listDeviceFlows() |
| 122 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, updatedAllFlows, nil, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 123 | if err != nil { |
| 124 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 125 | desc = err.Error() |
| 126 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 127 | return coreutils.DoneResponse(), err |
| 128 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 129 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addFlowsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 130 | } else { |
| 131 | flowChanges := &ofp.FlowChanges{ |
| 132 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 133 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 134 | } |
| 135 | groupChanges := &ofp.FlowGroupChanges{ |
| 136 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 137 | ToRemove: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 138 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 139 | } |
| 140 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 141 | if err != nil { |
| 142 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 143 | desc = err.Error() |
| 144 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 145 | return coreutils.DoneResponse(), err |
| 146 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 147 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addFlowsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 148 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 149 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 150 | agent.logDeviceUpdate(ctx, "addFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 151 | return response, nil |
| 152 | } |
| 153 | |
| 154 | func (agent *Agent) deleteFlowsFromAdapter(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 155 | logger.Debugw(ctx, "delete-flows-from-adapter", log.Fields{"device-id": agent.deviceID, "flows": flowsToDel}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 156 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 157 | var desc string |
| 158 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 159 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 160 | if (len(flowsToDel)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 161 | logger.Debugw(ctx, "nothing-to-delete", log.Fields{"device-id": agent.deviceID, "flows": flowsToDel}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 162 | return coreutils.DoneResponse(), nil |
| 163 | } |
| 164 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 165 | defer agent.logDeviceUpdate(ctx, "deleteFlowsFromAdapter", nil, nil, operStatus, &desc) |
| 166 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 167 | device, err := agent.getDeviceReadOnly(ctx) |
| 168 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 169 | desc = err.Error() |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 170 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 171 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 172 | |
| 173 | if !agent.proceedWithRequest(device) { |
| 174 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", device.Id) |
| 175 | agent.logDeviceUpdate(ctx, "deleteFlowsFromAdapter", nil, nil, operStatus, &desc) |
| 176 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 177 | } |
| 178 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 179 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 180 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 181 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 182 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 183 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 184 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 185 | for _, flow := range flowsToDel { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 186 | if flowHandle, have := agent.flowCache.Lock(flow.Id); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 187 | // Update the store and cache |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 188 | if err := flowHandle.Delete(ctx); err != nil { |
| 189 | flowHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 190 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 191 | return coreutils.DoneResponse(), err |
| 192 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 193 | flowHandle.Unlock() |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 198 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 199 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 200 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 201 | response := coreutils.NewResponse() |
| 202 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 203 | |
| 204 | updatedAllFlows := agent.listDeviceFlows() |
| 205 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, updatedAllFlows, nil, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 206 | if err != nil { |
| 207 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 208 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 209 | return coreutils.DoneResponse(), err |
| 210 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 211 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteFlowToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 212 | } else { |
| 213 | flowChanges := &ofp.FlowChanges{ |
| 214 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 215 | ToRemove: &voltha.Flows{Items: flowsToDel}, |
| 216 | } |
| 217 | groupChanges := &ofp.FlowGroupChanges{ |
| 218 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 219 | ToRemove: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 220 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 221 | } |
| 222 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 223 | if err != nil { |
| 224 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 225 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 226 | return coreutils.DoneResponse(), err |
| 227 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 228 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteFlowToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 229 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 230 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 231 | return response, nil |
| 232 | } |
| 233 | |
| 234 | func (agent *Agent) updateFlowsToAdapter(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 235 | logger.Debugw(ctx, "update-flows-to-adapter", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 236 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 237 | var desc string |
| 238 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 239 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 240 | if (len(updatedFlows)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 241 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 242 | return coreutils.DoneResponse(), nil |
| 243 | } |
| 244 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 245 | device, err := agent.getDeviceReadOnly(ctx) |
| 246 | if err != nil { |
| 247 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 248 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 249 | |
| 250 | if !agent.proceedWithRequest(device) { |
| 251 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", device.Id) |
| 252 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
| 253 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 254 | } |
| 255 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 256 | if device.OperStatus != voltha.OperStatus_ACTIVE || device.ConnectStatus != voltha.ConnectStatus_REACHABLE || device.AdminState != voltha.AdminState_ENABLED { |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 257 | desc = "invalid device states" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 258 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 259 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "invalid device states") |
| 260 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 261 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 262 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 263 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 264 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 265 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
| 266 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 267 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 268 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 269 | |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 270 | flowsToAdd := make([]*ofp.OfpFlowStats, 0, len(updatedFlows)) |
| 271 | flowsToDelete := make([]*ofp.OfpFlowStats, 0, len(updatedFlows)) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 272 | for _, flow := range updatedFlows { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 273 | if flowHandle, have := agent.flowCache.Lock(flow.Id); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 274 | flowToDelete := flowHandle.GetReadOnly() |
| 275 | // Update the store and cache |
| 276 | if err := flowHandle.Update(ctx, flow); err != nil { |
| 277 | flowHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 278 | desc = err.Error() |
| 279 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 280 | return coreutils.DoneResponse(), err |
| 281 | } |
| 282 | |
| 283 | flowsToDelete = append(flowsToDelete, flowToDelete) |
| 284 | flowsToAdd = append(flowsToAdd, flow) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 285 | flowHandle.Unlock() |
| 286 | } |
| 287 | } |
| 288 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 289 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 290 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 291 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 292 | response := coreutils.NewResponse() |
| 293 | // Process bulk flow update differently than incremental update |
| 294 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 295 | updatedAllFlows := agent.listDeviceFlows() |
| 296 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, updatedAllFlows, nil, nil) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 297 | if err != nil { |
| 298 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 299 | desc = err.Error() |
| 300 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 301 | return coreutils.DoneResponse(), err |
| 302 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 303 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateFlowToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 304 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 305 | logger.Debugw(ctx, "updating-flows-and-groups", |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 306 | log.Fields{ |
| 307 | "device-id": agent.deviceID, |
| 308 | "flows-to-add": flowsToAdd, |
| 309 | "flows-to-delete": flowsToDelete, |
| 310 | }) |
| 311 | // Sanity check |
| 312 | if (len(flowsToAdd) | len(flowsToDelete)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 313 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 314 | cancel() |
| 315 | return coreutils.DoneResponse(), nil |
| 316 | } |
| 317 | |
| 318 | flowChanges := &ofp.FlowChanges{ |
| 319 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 320 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 321 | } |
| 322 | groupChanges := &ofp.FlowGroupChanges{ |
| 323 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 324 | ToRemove: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 325 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 326 | } |
| 327 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 328 | if err != nil { |
| 329 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 330 | desc = err.Error() |
| 331 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 332 | return coreutils.DoneResponse(), err |
| 333 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 334 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateFlowToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 337 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 338 | agent.logDeviceUpdate(ctx, "updateFlowsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 339 | return response, nil |
| 340 | } |
| 341 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 342 | //filterOutFlows removes flows from a device using the uni-port as filter |
| 343 | func (agent *Agent) filterOutFlows(ctx context.Context, uniPort uint32, flowMetadata *voltha.FlowMetadata) error { |
| 344 | var flowsToDelete []*ofp.OfpFlowStats |
| 345 | // If an existing flow has the uniPort as an InPort or OutPort or as a Tunnel ID then it needs to be removed |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 346 | for flowID := range agent.flowCache.ListIDs() { |
| 347 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 348 | flow := flowHandle.GetReadOnly() |
| 349 | if flow != nil && (fu.GetInPort(flow) == uniPort || fu.GetOutPort(flow) == uniPort || fu.GetTunnelId(flow) == uint64(uniPort)) { |
| 350 | flowsToDelete = append(flowsToDelete, flow) |
| 351 | } |
| 352 | flowHandle.Unlock() |
| 353 | } |
| 354 | } |
| 355 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 356 | logger.Debugw(ctx, "flows-to-delete", log.Fields{"device-id": agent.deviceID, "uni-port": uniPort, "flows": flowsToDelete}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 357 | if len(flowsToDelete) == 0 { |
| 358 | return nil |
| 359 | } |
| 360 | |
| 361 | response, err := agent.deleteFlowsFromAdapter(ctx, flowsToDelete, flowMetadata) |
| 362 | if err != nil { |
| 363 | return err |
| 364 | } |
| 365 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
| 366 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 367 | } |
| 368 | return nil |
| 369 | } |
| 370 | |
| 371 | //deleteAllFlows deletes all flows in the device table |
| 372 | func (agent *Agent) deleteAllFlows(ctx context.Context) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 373 | logger.Debugw(ctx, "deleteAllFlows", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 374 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 375 | var error string |
| 376 | var desc string |
| 377 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 378 | |
| 379 | defer agent.logDeviceUpdate(ctx, "deleteAllFlows", nil, nil, operStatus, &desc) |
| 380 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 381 | for flowID := range agent.flowCache.ListIDs() { |
| 382 | if flowHandle, have := agent.flowCache.Lock(flowID); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 383 | // Update the store and cache |
| 384 | if err := flowHandle.Delete(ctx); err != nil { |
| 385 | flowHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 386 | error += fmt.Sprintf("%v ", flowID) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 387 | logger.Errorw(ctx, "unable-to-delete-flow", log.Fields{"device-id": agent.deviceID, "flowID": flowID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 388 | continue |
| 389 | } |
| 390 | flowHandle.Unlock() |
| 391 | } |
| 392 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 393 | |
| 394 | if error != "" { |
| 395 | desc = fmt.Sprintf("Unable to delete flows : %s", error) |
| 396 | } else { |
| 397 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 398 | } |
| 399 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 400 | return nil |
| 401 | } |