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