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