khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 22 | "errors" |
| 23 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 24 | "reflect" |
| 25 | "sync" |
| 26 | "time" |
| 27 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/db/model" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 30 | fd "github.com/opencord/voltha-go/rw_core/flowdecomposition" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/rw_core/graph" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 32 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 33 | fu "github.com/opencord/voltha-lib-go/v2/pkg/flows" |
| 34 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 35 | ic "github.com/opencord/voltha-protos/v2/go/inter_container" |
| 36 | ofp "github.com/opencord/voltha-protos/v2/go/openflow_13" |
| 37 | "github.com/opencord/voltha-protos/v2/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | "google.golang.org/grpc/codes" |
| 39 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | ) |
| 41 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | // LogicalDeviceAgent represent attributes of logical device agent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | type LogicalDeviceAgent struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 44 | logicalDeviceID string |
| 45 | rootDeviceID string |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 46 | deviceMgr *DeviceManager |
| 47 | ldeviceMgr *LogicalDeviceManager |
| 48 | clusterDataProxy *model.Proxy |
| 49 | exitChannel chan int |
| 50 | deviceGraph *graph.DeviceGraph |
| 51 | flowProxy *model.Proxy |
| 52 | groupProxy *model.Proxy |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 53 | meterProxy *model.Proxy |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 54 | ldProxy *model.Proxy |
| 55 | portProxies map[string]*model.Proxy |
| 56 | portProxiesLock sync.RWMutex |
| 57 | lockLogicalDevice sync.RWMutex |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 58 | lockDeviceGraph sync.RWMutex |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 59 | logicalPortsNo map[uint32]bool //value is true for NNI port |
| 60 | lockLogicalPortsNo sync.RWMutex |
| 61 | flowDecomposer *fd.FlowDecomposer |
| 62 | defaultTimeout int64 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 63 | logicalDevice *voltha.LogicalDevice |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 64 | } |
| 65 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 66 | func newLogicalDeviceAgent(id string, deviceID string, ldeviceMgr *LogicalDeviceManager, |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 67 | deviceMgr *DeviceManager, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 68 | cdProxy *model.Proxy, timeout int64) *LogicalDeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 69 | var agent LogicalDeviceAgent |
| 70 | agent.exitChannel = make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 71 | agent.logicalDeviceID = id |
| 72 | agent.rootDeviceID = deviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | agent.deviceMgr = deviceMgr |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 74 | agent.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | agent.ldeviceMgr = ldeviceMgr |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 76 | agent.flowDecomposer = fd.NewFlowDecomposer(agent.deviceMgr) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 77 | agent.lockLogicalDevice = sync.RWMutex{} |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 78 | agent.portProxies = make(map[string]*model.Proxy) |
| 79 | agent.portProxiesLock = sync.RWMutex{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 80 | agent.lockLogicalPortsNo = sync.RWMutex{} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 81 | agent.lockDeviceGraph = sync.RWMutex{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 82 | agent.logicalPortsNo = make(map[uint32]bool) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 83 | agent.defaultTimeout = timeout |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 84 | return &agent |
| 85 | } |
| 86 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 87 | // start creates the logical device and add it to the data model |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 88 | func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromdB bool) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 89 | log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceID, "loadFromdB": loadFromdB}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 90 | var ld *voltha.LogicalDevice |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 91 | var err error |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 92 | if !loadFromdB { |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 93 | //Build the logical device based on information retrieved from the device adapter |
| 94 | var switchCap *ic.SwitchCapability |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 95 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 96 | if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceID); err != nil { |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 97 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 98 | return err |
| 99 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 100 | ld = &voltha.LogicalDevice{Id: agent.logicalDeviceID, RootDeviceId: agent.rootDeviceID} |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 101 | |
| 102 | // Create the datapath ID (uint64) using the logical device ID (based on the MAC Address) |
| 103 | var datapathID uint64 |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 104 | if datapathID, err = CreateDataPathID(agent.logicalDeviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 105 | log.Errorw("error-creating-datapath-id", log.Fields{"error": err}) |
| 106 | return err |
| 107 | } |
| 108 | ld.DatapathId = datapathID |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 109 | ld.Desc = (proto.Clone(switchCap.Desc)).(*ofp.OfpDesc) |
| 110 | log.Debugw("Switch-capability", log.Fields{"Desc": ld.Desc, "fromAd": switchCap.Desc}) |
| 111 | ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*ofp.OfpSwitchFeatures) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 112 | ld.Flows = &ofp.Flows{Items: nil} |
| 113 | ld.FlowGroups = &ofp.FlowGroups{Items: nil} |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 114 | ld.Ports = []*voltha.LogicalPort{} |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 115 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 116 | agent.lockLogicalDevice.Lock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 117 | // Save the logical device |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 118 | added, err := agent.clusterDataProxy.AddWithID(ctx, "/logical_devices", ld.Id, ld, "") |
| 119 | if err != nil { |
| 120 | log.Errorw("failed-to-save-logical-devices-to-cluster-proxy", log.Fields{"error": err}) |
| 121 | agent.lockLogicalDevice.Unlock() |
| 122 | return err |
| 123 | } |
| 124 | if added == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 125 | log.Errorw("failed-to-add-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 126 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 127 | log.Debugw("logicaldevice-created", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 128 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 129 | |
| 130 | agent.logicalDevice = proto.Clone(ld).(*voltha.LogicalDevice) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 131 | agent.lockLogicalDevice.Unlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 132 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 133 | // TODO: Set the logical ports in a separate call once the port update issue is fixed. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 134 | go func() { |
| 135 | err := agent.setupLogicalPorts(ctx) |
| 136 | if err != nil { |
| 137 | log.Errorw("unable-to-setup-logical-ports", log.Fields{"error": err}) |
| 138 | } |
| 139 | }() |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 140 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 141 | } else { |
| 142 | // load from dB - the logical may not exist at this time. On error, just return and the calling function |
| 143 | // will destroy this agent. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 144 | agent.lockLogicalDevice.Lock() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 145 | logicalDevice, err := agent.clusterDataProxy.Get(context.Background(), "/logical_devices/"+agent.logicalDeviceID, 0, true, "") |
| 146 | if err != nil { |
| 147 | return status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
| 148 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 149 | ld, ok := logicalDevice.(*voltha.LogicalDevice) |
| 150 | if !ok { |
| 151 | agent.lockLogicalDevice.Unlock() |
| 152 | return status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 153 | } |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 154 | // Update the root device Id |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 155 | agent.rootDeviceID = ld.RootDeviceId |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 156 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 157 | // Update the last data |
| 158 | agent.logicalDevice = proto.Clone(ld).(*voltha.LogicalDevice) |
| 159 | |
| 160 | agent.lockLogicalDevice.Unlock() |
| 161 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 162 | // Setup the local list of logical ports |
| 163 | agent.addLogicalPortsToMap(ld.Ports) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 164 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 165 | agent.lockLogicalDevice.Lock() |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 166 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 167 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 168 | agent.flowProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 169 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 170 | fmt.Sprintf("/logical_devices/%s/flows", agent.logicalDeviceID), |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 171 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 172 | if err != nil { |
| 173 | log.Errorw("failed-to-create-flow-proxy", log.Fields{"error": err}) |
| 174 | return err |
| 175 | } |
| 176 | agent.meterProxy, err = agent.clusterDataProxy.CreateProxy( |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 177 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 178 | fmt.Sprintf("/logical_devices/%s/meters", agent.logicalDeviceID), |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 179 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 180 | if err != nil { |
| 181 | log.Errorw("failed-to-create-meter-proxy", log.Fields{"error": err}) |
| 182 | return err |
| 183 | } |
| 184 | agent.groupProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 185 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 186 | fmt.Sprintf("/logical_devices/%s/flow_groups", agent.logicalDeviceID), |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 187 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 188 | if err != nil { |
| 189 | log.Errorw("failed-to-create-group-proxy", log.Fields{"error": err}) |
| 190 | return err |
| 191 | } |
| 192 | agent.ldProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 193 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 194 | fmt.Sprintf("/logical_devices/%s", agent.logicalDeviceID), |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 195 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 196 | if err != nil { |
| 197 | log.Errorw("failed-to-create-logical-device-proxy", log.Fields{"error": err}) |
| 198 | return err |
| 199 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 200 | // TODO: Use a port proxy once the POST_ADD is fixed |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 201 | if agent.ldProxy != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame^] | 202 | agent.ldProxy.RegisterCallback(model.PostUpdate, agent.portUpdated) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 203 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 204 | log.Errorw("logical-device-proxy-null", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 205 | return status.Error(codes.Internal, "logical-device-proxy-null") |
| 206 | } |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 207 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 208 | // Setup the device graph - run it in its own routine |
| 209 | if loadFromdB { |
| 210 | go agent.generateDeviceGraph() |
| 211 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 212 | return nil |
| 213 | } |
| 214 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 215 | // stop stops the logical devuce agent. This removes the logical device from the data model. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 216 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) error { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 217 | log.Info("stopping-logical_device-agent") |
| 218 | agent.lockLogicalDevice.Lock() |
| 219 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 220 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 221 | //Remove the logical device from the model |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 222 | if removed, err := agent.clusterDataProxy.Remove(ctx, "/logical_devices/"+agent.logicalDeviceID, ""); err != nil { |
| 223 | log.Errorw("failed-to-remove-device", log.Fields{"error": err}) |
| 224 | return err |
| 225 | } else if removed == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 226 | log.Errorw("failed-to-remove-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 227 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 228 | log.Debugw("logicaldevice-removed", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 229 | } |
| 230 | agent.exitChannel <- 1 |
| 231 | log.Info("logical_device-agent-stopped") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 232 | return nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 233 | } |
| 234 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 235 | // GetLogicalDevice returns the latest logical device data |
| 236 | func (agent *LogicalDeviceAgent) GetLogicalDevice() *voltha.LogicalDevice { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 237 | agent.lockLogicalDevice.RLock() |
| 238 | defer agent.lockLogicalDevice.RUnlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 239 | |
| 240 | return proto.Clone(agent.logicalDevice).(*voltha.LogicalDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 241 | } |
| 242 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 243 | // ListLogicalDeviceFlows returns logical device flows |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 244 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() *ofp.Flows { |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 245 | log.Debug("ListLogicalDeviceFlows") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 246 | |
| 247 | logicalDevice := agent.GetLogicalDevice() |
| 248 | if logicalDevice.Flows == nil { |
| 249 | return &ofp.Flows{} |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 250 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 251 | return (proto.Clone(logicalDevice.Flows)).(*ofp.Flows) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 252 | } |
| 253 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 254 | // ListLogicalDeviceMeters returns logical device meters |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 255 | func (agent *LogicalDeviceAgent) ListLogicalDeviceMeters() *ofp.Meters { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 256 | log.Debug("ListLogicalDeviceMeters") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 257 | |
| 258 | logicalDevice := agent.GetLogicalDevice() |
| 259 | if logicalDevice.Meters == nil { |
| 260 | return &ofp.Meters{} |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 261 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 262 | return (proto.Clone(logicalDevice.Meters)).(*ofp.Meters) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 263 | } |
| 264 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 265 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 266 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() *ofp.FlowGroups { |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 267 | log.Debug("ListLogicalDeviceFlowGroups") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 268 | |
| 269 | logicalDevice := agent.GetLogicalDevice() |
| 270 | if logicalDevice.FlowGroups == nil { |
| 271 | return &ofp.FlowGroups{} |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 272 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 273 | return (proto.Clone(logicalDevice.FlowGroups)).(*ofp.FlowGroups) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 274 | } |
| 275 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 276 | // ListLogicalDevicePorts returns logical device ports |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 277 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() *voltha.LogicalPorts { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 278 | log.Debug("ListLogicalDevicePorts") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 279 | logicalDevice := agent.GetLogicalDevice() |
| 280 | lPorts := make([]*voltha.LogicalPort, 0) |
| 281 | lPorts = append(lPorts, logicalDevice.Ports...) |
| 282 | return &voltha.LogicalPorts{Items: lPorts} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 283 | } |
| 284 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 285 | //updateLogicalDeviceFlowsWithoutLock updates the logical device with the latest flows in the model. |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 286 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowsWithoutLock(flows *ofp.Flows) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 287 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 288 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 289 | log.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
| 290 | ld.Flows = flows |
| 291 | |
| 292 | if err := agent.updateLogicalDeviceWithoutLock(ld); err != nil { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 293 | log.Errorw("error-updating-logical-device-with-flows", log.Fields{"error": err}) |
| 294 | return err |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 295 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 296 | return nil |
| 297 | } |
| 298 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 299 | //updateLogicalDeviceMetersWithoutLock updates the logical device with the meters info |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 300 | func (agent *LogicalDeviceAgent) updateLogicalDeviceMetersWithoutLock(meters *ofp.Meters) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 301 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 302 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 303 | log.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
| 304 | ld.Meters = meters |
| 305 | |
| 306 | if err := agent.updateLogicalDeviceWithoutLock(ld); err != nil { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 307 | log.Errorw("error-updating-logical-device-with-meters", log.Fields{"error": err}) |
| 308 | return err |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 309 | } |
| 310 | return nil |
| 311 | } |
| 312 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 313 | //updateLogicalDeviceFlowGroupsWithoutLock updates the logical device with the flow groups |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 314 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowGroupsWithoutLock(flowGroups *ofp.FlowGroups) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 315 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 316 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 317 | log.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
| 318 | ld.FlowGroups = flowGroups |
| 319 | |
| 320 | if err := agent.updateLogicalDeviceWithoutLock(ld); err != nil { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 321 | log.Errorw("error-updating-logical-device-with-flowgroups", log.Fields{"error": err}) |
| 322 | return err |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 323 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 324 | return nil |
| 325 | } |
| 326 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 327 | // getLogicalDeviceWithoutLock returns a cloned logical device to a function that already holds the agent lock. |
| 328 | func (agent *LogicalDeviceAgent) getLogicalDeviceWithoutLock() *voltha.LogicalDevice { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 329 | log.Debug("getLogicalDeviceWithoutLock") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 330 | return proto.Clone(agent.logicalDevice).(*voltha.LogicalDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 331 | } |
| 332 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 333 | func (agent *LogicalDeviceAgent) updateLogicalPort(device *voltha.Device, port *voltha.Port) error { |
| 334 | log.Debugw("updateLogicalPort", log.Fields{"deviceId": device.Id, "port": port}) |
| 335 | var err error |
| 336 | if port.Type == voltha.Port_ETHERNET_NNI { |
| 337 | if _, err = agent.addNNILogicalPort(device, port); err != nil { |
| 338 | return err |
| 339 | } |
| 340 | agent.addLogicalPortToMap(port.PortNo, true) |
| 341 | } else if port.Type == voltha.Port_ETHERNET_UNI { |
| 342 | if _, err = agent.addUNILogicalPort(device, port); err != nil { |
| 343 | return err |
| 344 | } |
| 345 | agent.addLogicalPortToMap(port.PortNo, false) |
| 346 | } else { |
| 347 | // Update the device graph to ensure all routes on the logical device have been calculated |
| 348 | if err = agent.updateRoutes(device, port); err != nil { |
| 349 | log.Errorw("failed-to-update-routes", log.Fields{"deviceId": device.Id, "port": port, "error": err}) |
| 350 | return err |
| 351 | } |
| 352 | } |
| 353 | return nil |
| 354 | } |
| 355 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 356 | // setupLogicalPorts is invoked once the logical device has been created and is ready to get ports |
| 357 | // added to it. While the logical device was being created we could have received requests to add |
| 358 | // NNI and UNI ports which were discarded. Now is the time to add them if needed |
| 359 | func (agent *LogicalDeviceAgent) setupLogicalPorts(ctx context.Context) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 360 | log.Infow("setupLogicalPorts", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 361 | // First add any NNI ports which could have been missing |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 362 | if err := agent.setupNNILogicalPorts(context.TODO(), agent.rootDeviceID); err != nil { |
| 363 | log.Errorw("error-setting-up-NNI-ports", log.Fields{"error": err, "deviceId": agent.rootDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 364 | return err |
| 365 | } |
| 366 | |
| 367 | // Now, set up the UNI ports if needed. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 368 | children, err := agent.deviceMgr.getAllChildDevices(agent.rootDeviceID) |
| 369 | if err != nil { |
| 370 | log.Errorw("error-getting-child-devices", log.Fields{"error": err, "deviceId": agent.rootDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 371 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 372 | } |
| 373 | responses := make([]coreutils.Response, 0) |
| 374 | for _, child := range children.Items { |
| 375 | response := coreutils.NewResponse() |
| 376 | responses = append(responses, response) |
| 377 | go func(child *voltha.Device) { |
| 378 | if err = agent.setupUNILogicalPorts(context.TODO(), child); err != nil { |
| 379 | log.Error("setting-up-UNI-ports-failed", log.Fields{"deviceID": child.Id}) |
| 380 | response.Error(status.Errorf(codes.Internal, "UNI-ports-setup-failed: %s", child.Id)) |
| 381 | } |
| 382 | response.Done() |
| 383 | }(child) |
| 384 | } |
| 385 | // Wait for completion |
| 386 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
| 387 | return status.Errorf(codes.Aborted, "errors-%s", res) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 388 | } |
| 389 | return nil |
| 390 | } |
| 391 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 392 | // setupNNILogicalPorts creates an NNI port on the logical device that represents an NNI interface on a root device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 393 | func (agent *LogicalDeviceAgent) setupNNILogicalPorts(ctx context.Context, deviceID string) error { |
| 394 | log.Infow("setupNNILogicalPorts-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 395 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 396 | var err error |
| 397 | |
| 398 | var device *voltha.Device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 399 | if device, err = agent.deviceMgr.GetDevice(deviceID); err != nil { |
| 400 | log.Errorw("error-retrieving-device", log.Fields{"error": err, "deviceId": deviceID}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 401 | return err |
| 402 | } |
| 403 | |
| 404 | //Get UNI port number |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 405 | for _, port := range device.Ports { |
| 406 | if port.Type == voltha.Port_ETHERNET_NNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 407 | if _, err = agent.addNNILogicalPort(device, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 408 | log.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 409 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 410 | agent.addLogicalPortToMap(port.PortNo, true) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 413 | return err |
| 414 | } |
| 415 | |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 416 | // updatePortState updates the port state of the device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 417 | func (agent *LogicalDeviceAgent) updatePortState(deviceID string, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 418 | log.Infow("updatePortState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "portNo": portNo, "state": operStatus}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 419 | agent.lockLogicalDevice.Lock() |
| 420 | defer agent.lockLogicalDevice.Unlock() |
| 421 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 422 | cloned := agent.getLogicalDeviceWithoutLock() |
| 423 | for idx, lPort := range cloned.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 424 | if lPort.DeviceId == deviceID && lPort.DevicePortNo == portNo { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 425 | if operStatus == voltha.OperStatus_ACTIVE { |
| 426 | cloned.Ports[idx].OfpPort.Config = cloned.Ports[idx].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 427 | cloned.Ports[idx].OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
| 428 | } else { |
| 429 | cloned.Ports[idx].OfpPort.Config = cloned.Ports[idx].OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 430 | cloned.Ports[idx].OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 431 | } |
| 432 | // Update the logical device |
| 433 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 434 | log.Errorw("error-updating-logical-device", log.Fields{"error": err}) |
| 435 | return err |
| 436 | } |
| 437 | return nil |
| 438 | } |
| 439 | } |
| 440 | return status.Errorf(codes.NotFound, "port-%d-not-exist", portNo) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 441 | } |
| 442 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 443 | // updatePortsState updates the ports state related to the device |
| 444 | func (agent *LogicalDeviceAgent) updatePortsState(device *voltha.Device, state voltha.AdminState_AdminState) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 445 | log.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 446 | agent.lockLogicalDevice.Lock() |
| 447 | defer agent.lockLogicalDevice.Unlock() |
| 448 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 449 | cloned := agent.getLogicalDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 450 | for _, lport := range cloned.Ports { |
| 451 | if lport.DeviceId == device.Id { |
| 452 | switch state { |
| 453 | case voltha.AdminState_ENABLED: |
| 454 | lport.OfpPort.Config = lport.OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 455 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
| 456 | case voltha.AdminState_DISABLED: |
| 457 | lport.OfpPort.Config = lport.OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 458 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 459 | default: |
| 460 | log.Warnw("unsupported-state-change", log.Fields{"deviceId": device.Id, "state": state}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 461 | } |
| 462 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 463 | } |
| 464 | // Updating the logical device will trigger the poprt change events to be populated to the controller |
| 465 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 466 | log.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceID, "error": err}) |
| 467 | return err |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 468 | } |
| 469 | return nil |
| 470 | } |
| 471 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 472 | // setupUNILogicalPorts creates a UNI port on the logical device that represents a child UNI interface |
| 473 | func (agent *LogicalDeviceAgent) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 474 | log.Infow("setupUNILogicalPort", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 475 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 476 | var err error |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 477 | var added bool |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 478 | //Get UNI port number |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 479 | for _, port := range childDevice.Ports { |
| 480 | if port.Type == voltha.Port_ETHERNET_UNI { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 481 | if added, err = agent.addUNILogicalPort(childDevice, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 482 | log.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 483 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 484 | if added { |
| 485 | agent.addLogicalPortToMap(port.PortNo, false) |
| 486 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 487 | } |
| 488 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 489 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 490 | } |
| 491 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 492 | // deleteAllLogicalPorts deletes all logical ports associated with this device |
| 493 | func (agent *LogicalDeviceAgent) deleteAllLogicalPorts(device *voltha.Device) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 494 | log.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 495 | agent.lockLogicalDevice.Lock() |
| 496 | defer agent.lockLogicalDevice.Unlock() |
| 497 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 498 | ld := agent.getLogicalDeviceWithoutLock() |
| 499 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 500 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 501 | updateLogicalPorts := []*voltha.LogicalPort{} |
| 502 | for _, lport := range cloned.Ports { |
| 503 | if lport.DeviceId != device.Id { |
| 504 | updateLogicalPorts = append(updateLogicalPorts, lport) |
| 505 | } |
| 506 | } |
| 507 | if len(updateLogicalPorts) < len(cloned.Ports) { |
| 508 | cloned.Ports = updateLogicalPorts |
| 509 | // Updating the logical device will trigger the poprt change events to be populated to the controller |
| 510 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 511 | log.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceID, "error": err}) |
| 512 | return err |
| 513 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 514 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 515 | log.Debugw("no-change-required", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 516 | } |
| 517 | return nil |
| 518 | } |
| 519 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 520 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 521 | func (agent *LogicalDeviceAgent) updateLogicalDeviceWithoutLock(logicalDevice *voltha.LogicalDevice) error { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 522 | updateCtx := context.WithValue(context.Background(), model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 523 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/logical_devices/"+agent.logicalDeviceID, logicalDevice, false, "") |
| 524 | if err != nil { |
| 525 | log.Errorw("failed-to-update-logical-devices-to-cluster-proxy", log.Fields{"error": err}) |
| 526 | return err |
| 527 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 528 | if afterUpdate == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 529 | return status.Errorf(codes.Internal, "failed-updating-logical-device:%s", agent.logicalDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 530 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 531 | agent.logicalDevice = (proto.Clone(logicalDevice)).(*voltha.LogicalDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 532 | return nil |
| 533 | } |
| 534 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 535 | //generateDeviceGraphIfNeeded generates the device graph if the logical device has been updated since the last time |
| 536 | //that device graph was generated. |
| 537 | func (agent *LogicalDeviceAgent) generateDeviceGraphIfNeeded() error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 538 | ld := agent.GetLogicalDevice() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 539 | agent.lockDeviceGraph.Lock() |
| 540 | defer agent.lockDeviceGraph.Unlock() |
| 541 | if agent.deviceGraph != nil && agent.deviceGraph.IsUpToDate(ld) { |
| 542 | return nil |
| 543 | } |
| 544 | log.Debug("Generation of device graph required") |
| 545 | agent.generateDeviceGraph() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 546 | return nil |
| 547 | } |
| 548 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 549 | //updateFlowTable updates the flow table of that logical device |
| 550 | func (agent *LogicalDeviceAgent) updateFlowTable(ctx context.Context, flow *ofp.OfpFlowMod) error { |
| 551 | log.Debug("updateFlowTable") |
| 552 | if flow == nil { |
| 553 | return nil |
| 554 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 555 | if err := agent.generateDeviceGraphIfNeeded(); err != nil { |
| 556 | return err |
| 557 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 558 | switch flow.GetCommand() { |
| 559 | case ofp.OfpFlowModCommand_OFPFC_ADD: |
| 560 | return agent.flowAdd(flow) |
| 561 | case ofp.OfpFlowModCommand_OFPFC_DELETE: |
| 562 | return agent.flowDelete(flow) |
| 563 | case ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT: |
| 564 | return agent.flowDeleteStrict(flow) |
| 565 | case ofp.OfpFlowModCommand_OFPFC_MODIFY: |
| 566 | return agent.flowModify(flow) |
| 567 | case ofp.OfpFlowModCommand_OFPFC_MODIFY_STRICT: |
| 568 | return agent.flowModifyStrict(flow) |
| 569 | } |
| 570 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 571 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, flow.GetCommand()) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | //updateGroupTable updates the group table of that logical device |
| 575 | func (agent *LogicalDeviceAgent) updateGroupTable(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 576 | log.Debug("updateGroupTable") |
| 577 | if groupMod == nil { |
| 578 | return nil |
| 579 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 580 | if err := agent.generateDeviceGraphIfNeeded(); err != nil { |
| 581 | return err |
| 582 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 583 | switch groupMod.GetCommand() { |
| 584 | case ofp.OfpGroupModCommand_OFPGC_ADD: |
| 585 | return agent.groupAdd(groupMod) |
| 586 | case ofp.OfpGroupModCommand_OFPGC_DELETE: |
| 587 | return agent.groupDelete(groupMod) |
| 588 | case ofp.OfpGroupModCommand_OFPGC_MODIFY: |
| 589 | return agent.groupModify(groupMod) |
| 590 | } |
| 591 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 592 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, groupMod.GetCommand()) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 595 | // updateMeterTable updates the meter table of that logical device |
| 596 | func (agent *LogicalDeviceAgent) updateMeterTable(ctx context.Context, meterMod *ofp.OfpMeterMod) error { |
| 597 | log.Debug("updateMeterTable") |
| 598 | if meterMod == nil { |
| 599 | return nil |
| 600 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 601 | if err := agent.generateDeviceGraphIfNeeded(); err != nil { |
| 602 | return err |
| 603 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 604 | switch meterMod.GetCommand() { |
| 605 | case ofp.OfpMeterModCommand_OFPMC_ADD: |
| 606 | return agent.meterAdd(meterMod) |
| 607 | case ofp.OfpMeterModCommand_OFPMC_DELETE: |
| 608 | return agent.meterDelete(meterMod) |
| 609 | case ofp.OfpMeterModCommand_OFPMC_MODIFY: |
| 610 | return agent.meterModify(meterMod) |
| 611 | } |
| 612 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 613 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, meterMod.GetCommand()) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 614 | |
| 615 | } |
| 616 | |
| 617 | func (agent *LogicalDeviceAgent) meterAdd(meterMod *ofp.OfpMeterMod) error { |
| 618 | log.Debugw("meterAdd", log.Fields{"metermod": *meterMod}) |
| 619 | if meterMod == nil { |
| 620 | return nil |
| 621 | } |
| 622 | log.Debug("Waiting for logical device lock!!") |
| 623 | agent.lockLogicalDevice.Lock() |
| 624 | defer agent.lockLogicalDevice.Unlock() |
| 625 | log.Debug("Acquired logical device lock") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 626 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 627 | |
| 628 | var meters []*ofp.OfpMeterEntry |
| 629 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 630 | meters = lDevice.Meters.Items |
| 631 | } |
| 632 | log.Debugw("Available meters", log.Fields{"meters": meters}) |
| 633 | |
| 634 | for _, meter := range meters { |
| 635 | if meterMod.MeterId == meter.Config.MeterId { |
| 636 | log.Infow("Meter-already-exists", log.Fields{"meter": *meterMod}) |
| 637 | return nil |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | meterEntry := fu.MeterEntryFromMeterMod(meterMod) |
| 642 | meters = append(meters, meterEntry) |
| 643 | //Update model |
| 644 | if err := agent.updateLogicalDeviceMetersWithoutLock(&ofp.Meters{Items: meters}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 645 | log.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 646 | return err |
| 647 | } |
| 648 | log.Debugw("Meter-added-successfully", log.Fields{"Added-meter": meterEntry, "updated-meters": lDevice.Meters}) |
| 649 | return nil |
| 650 | } |
| 651 | |
| 652 | func (agent *LogicalDeviceAgent) meterDelete(meterMod *ofp.OfpMeterMod) error { |
| 653 | log.Debug("meterDelete", log.Fields{"meterMod": *meterMod}) |
| 654 | if meterMod == nil { |
| 655 | return nil |
| 656 | } |
| 657 | agent.lockLogicalDevice.Lock() |
| 658 | defer agent.lockLogicalDevice.Unlock() |
| 659 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 660 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 661 | |
| 662 | var meters []*ofp.OfpMeterEntry |
| 663 | var flows []*ofp.OfpFlowStats |
| 664 | updatedFlows := make([]*ofp.OfpFlowStats, 0) |
| 665 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 666 | meters = lDevice.Meters.Items |
| 667 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 668 | |
| 669 | changedMeter := false |
| 670 | changedFow := false |
| 671 | log.Debugw("Available meters", log.Fields{"meters": meters}) |
| 672 | for index, meter := range meters { |
| 673 | if meterMod.MeterId == meter.Config.MeterId { |
| 674 | flows = lDevice.Flows.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 675 | changedFow, updatedFlows = agent.getUpdatedFlowsAfterDeletebyMeterID(flows, meterMod.MeterId) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 676 | meters = append(meters[:index], meters[index+1:]...) |
| 677 | log.Debugw("Meter has been deleted", log.Fields{"meter": meter, "index": index}) |
| 678 | changedMeter = true |
| 679 | break |
| 680 | } |
| 681 | } |
| 682 | if changedMeter { |
| 683 | //Update model |
| 684 | metersToUpdate := &ofp.Meters{} |
| 685 | if lDevice.Meters != nil { |
| 686 | metersToUpdate = &ofp.Meters{Items: meters} |
| 687 | } |
| 688 | if err := agent.updateLogicalDeviceMetersWithoutLock(metersToUpdate); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 689 | log.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 690 | return err |
| 691 | } |
| 692 | log.Debug("Meter-deleted-from-DB-successfully", log.Fields{"updatedMeters": metersToUpdate, "no-of-meter": len(metersToUpdate.Items)}) |
| 693 | |
| 694 | } |
| 695 | if changedFow { |
| 696 | //Update model |
| 697 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: updatedFlows}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 698 | log.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 699 | return err |
| 700 | } |
| 701 | log.Debug("Flows-associated-with-meter-deleted-from-DB-successfully", |
| 702 | log.Fields{"updated-no-of-flows": len(updatedFlows), "meter": meterMod.MeterId}) |
| 703 | } |
| 704 | log.Debugw("meterDelete success", log.Fields{"meterID": meterMod.MeterId}) |
| 705 | return nil |
| 706 | } |
| 707 | |
| 708 | func (agent *LogicalDeviceAgent) meterModify(meterMod *ofp.OfpMeterMod) error { |
| 709 | log.Debug("meterModify") |
| 710 | if meterMod == nil { |
| 711 | return nil |
| 712 | } |
| 713 | agent.lockLogicalDevice.Lock() |
| 714 | defer agent.lockLogicalDevice.Unlock() |
| 715 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 716 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 717 | |
| 718 | var meters []*ofp.OfpMeterEntry |
| 719 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 720 | meters = lDevice.Meters.Items |
| 721 | } |
| 722 | changedMeter := false |
| 723 | for index, meter := range meters { |
| 724 | if meterMod.MeterId == meter.Config.MeterId { |
| 725 | newmeterEntry := fu.MeterEntryFromMeterMod(meterMod) |
| 726 | newmeterEntry.Stats.FlowCount = meter.Stats.FlowCount |
| 727 | meters[index] = newmeterEntry |
| 728 | changedMeter = true |
| 729 | log.Debugw("Found meter, replaced with new meter", log.Fields{"old meter": meter, "new meter": newmeterEntry}) |
| 730 | break |
| 731 | } |
| 732 | } |
| 733 | if changedMeter { |
| 734 | //Update model |
| 735 | metersToUpdate := &ofp.Meters{} |
| 736 | if lDevice.Meters != nil { |
| 737 | metersToUpdate = &ofp.Meters{Items: meters} |
| 738 | } |
| 739 | if err := agent.updateLogicalDeviceMetersWithoutLock(metersToUpdate); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 740 | log.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 741 | return err |
| 742 | } |
| 743 | log.Debugw("meter-updated-in-DB-successfully", log.Fields{"updated_meters": meters}) |
| 744 | return nil |
| 745 | } |
| 746 | |
| 747 | log.Errorw("Meter not found ", log.Fields{"meter": meterMod}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 748 | return fmt.Errorf("no-logical-device-present:%d", meterMod.MeterId) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 749 | |
| 750 | } |
| 751 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 752 | func (agent *LogicalDeviceAgent) getUpdatedFlowsAfterDeletebyMeterID(flows []*ofp.OfpFlowStats, meterID uint32) (bool, []*ofp.OfpFlowStats) { |
| 753 | log.Infow("Delete flows matching meter", log.Fields{"meter": meterID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 754 | changed := false |
| 755 | //updatedFlows := make([]*ofp.OfpFlowStats, 0) |
| 756 | for index := len(flows) - 1; index >= 0; index-- { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 757 | if mID := fu.GetMeterIdFromFlow(flows[index]); mID != 0 && mID == meterID { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 758 | log.Debugw("Flow to be deleted", log.Fields{"flow": flows[index], "index": index}) |
| 759 | flows = append(flows[:index], flows[index+1:]...) |
| 760 | changed = true |
| 761 | } |
| 762 | } |
| 763 | return changed, flows |
| 764 | } |
| 765 | |
| 766 | func (agent *LogicalDeviceAgent) updateFlowCountOfMeterStats(modCommand *ofp.OfpFlowMod, meters []*ofp.OfpMeterEntry, flow *ofp.OfpFlowStats) bool { |
| 767 | |
| 768 | flowCommand := modCommand.GetCommand() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 769 | meterID := fu.GetMeterIdFromFlow(flow) |
| 770 | log.Debugw("Meter-id-in-flow-mod", log.Fields{"meterId": meterID}) |
| 771 | if meterID == 0 { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 772 | log.Debugw("No meter present in the flow", log.Fields{"flow": *flow}) |
| 773 | return false |
| 774 | } |
| 775 | if meters == nil { |
| 776 | log.Debug("No meters present in logical device") |
| 777 | return false |
| 778 | } |
| 779 | changedMeter := false |
| 780 | for _, meter := range meters { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 781 | if meterID == meter.Config.MeterId { // Found meter in Logicaldevice |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 782 | if flowCommand == ofp.OfpFlowModCommand_OFPFC_ADD { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 783 | meter.Stats.FlowCount++ |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 784 | changedMeter = true |
| 785 | } else if flowCommand == ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 786 | meter.Stats.FlowCount-- |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 787 | changedMeter = true |
| 788 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 789 | log.Debugw("Found meter, updated meter flow stats", log.Fields{" meterId": meterID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 790 | break |
| 791 | } |
| 792 | } |
| 793 | return changedMeter |
| 794 | } |
| 795 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 796 | //flowAdd adds a flow to the flow table of that logical device |
| 797 | func (agent *LogicalDeviceAgent) flowAdd(mod *ofp.OfpFlowMod) error { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 798 | log.Debugw("flowAdd", log.Fields{"flow": mod}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 799 | if mod == nil { |
| 800 | return nil |
| 801 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 802 | agent.lockLogicalDevice.Lock() |
| 803 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 804 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 805 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 806 | |
| 807 | var flows []*ofp.OfpFlowStats |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 808 | var meters []*ofp.OfpMeterEntry |
| 809 | var flow *ofp.OfpFlowStats |
| 810 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 811 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 812 | flows = lDevice.Flows.Items |
| 813 | } |
| 814 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 815 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 816 | meters = lDevice.Meters.Items |
| 817 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 818 | updatedFlows := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 819 | changed := false |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 820 | updated := false |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 821 | checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0 |
| 822 | if checkOverlap { |
| 823 | if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 { |
| 824 | // TODO: should this error be notified other than being logged? |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 825 | log.Warnw("overlapped-flows", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 826 | } else { |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 827 | // Add flow |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 828 | flow = fu.FlowStatsEntryFromFlowModMessage(mod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 829 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 830 | updatedFlows = append(updatedFlows, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 831 | changed = true |
| 832 | } |
| 833 | } else { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 834 | flow = fu.FlowStatsEntryFromFlowModMessage(mod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 835 | idx := fu.FindFlows(flows, flow) |
| 836 | if idx >= 0 { |
| 837 | oldFlow := flows[idx] |
| 838 | if (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_RESET_COUNTS)) != 0 { |
| 839 | flow.ByteCount = oldFlow.ByteCount |
| 840 | flow.PacketCount = oldFlow.PacketCount |
| 841 | } |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 842 | if !reflect.DeepEqual(oldFlow, flow) { |
| 843 | flows[idx] = flow |
| 844 | updatedFlows = append(updatedFlows, flow) |
| 845 | changed = true |
| 846 | updated = true |
| 847 | } |
| 848 | } else { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 849 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 850 | updatedFlows = append(updatedFlows, flow) |
| 851 | changed = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 852 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 853 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 854 | log.Debugw("flowAdd-changed", log.Fields{"changed": changed}) |
| 855 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 856 | if changed { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 857 | var flowMetadata voltha.FlowMetadata |
| 858 | if err := agent.GetMeterConfig(updatedFlows, meters, &flowMetadata); err != nil { // This should never happen,meters should be installed before flow arrives |
| 859 | log.Error("Meter-referred-in-flows-not-present") |
| 860 | return err |
| 861 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 862 | deviceRules := agent.flowDecomposer.DecomposeRules(agent, ofp.Flows{Items: updatedFlows}, *lDevice.FlowGroups) |
| 863 | log.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 864 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 865 | if err := agent.addDeviceFlowsAndGroups(deviceRules, &flowMetadata); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 866 | log.Errorw("failure-updating-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 867 | return err |
| 868 | } |
| 869 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 870 | // Update model |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 871 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: flows}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 872 | log.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 873 | return err |
| 874 | } |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 875 | if !updated { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 876 | changedMeterStats := agent.updateFlowCountOfMeterStats(mod, meters, flow) |
| 877 | metersToUpdate := &ofp.Meters{} |
| 878 | if lDevice.Meters != nil { |
| 879 | metersToUpdate = &ofp.Meters{Items: meters} |
| 880 | } |
| 881 | if changedMeterStats { |
| 882 | //Update model |
| 883 | if err := agent.updateLogicalDeviceMetersWithoutLock(metersToUpdate); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 884 | log.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 885 | return err |
| 886 | } |
| 887 | log.Debugw("meter-stats-updated-in-DB-successfully", log.Fields{"updated_meters": meters}) |
| 888 | |
| 889 | } |
| 890 | } |
| 891 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 892 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 893 | return nil |
| 894 | } |
| 895 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 896 | // GetMeterConfig returns meter config |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 897 | func (agent *LogicalDeviceAgent) GetMeterConfig(flows []*ofp.OfpFlowStats, meters []*ofp.OfpMeterEntry, metadata *voltha.FlowMetadata) error { |
| 898 | m := make(map[uint32]bool) |
| 899 | for _, flow := range flows { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 900 | if flowMeterID := fu.GetMeterIdFromFlow(flow); flowMeterID != 0 && !m[flowMeterID] { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 901 | foundMeter := false |
| 902 | // Meter is present in the flow , Get from logical device |
| 903 | for _, meter := range meters { |
| 904 | if flowMeterID == meter.Config.MeterId { |
| 905 | metadata.Meters = append(metadata.Meters, meter.Config) |
| 906 | log.Debugw("Found meter in logical device", |
| 907 | log.Fields{"meterID": flowMeterID, "meter-band": meter.Config}) |
| 908 | m[flowMeterID] = true |
| 909 | foundMeter = true |
| 910 | break |
| 911 | } |
| 912 | } |
| 913 | if !foundMeter { |
| 914 | log.Errorw("Meter-referred-by-flow-is-not-found-in-logicaldevice", |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 915 | log.Fields{"meterID": flowMeterID, "Available-meters": meters, "flow": *flow}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 916 | return errors.New("Meter-referred-by-flow-is-not-found-in-logicaldevice") |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | log.Debugw("meter-bands-for-flows", log.Fields{"flows": len(flows), "metadata": metadata}) |
| 921 | return nil |
| 922 | |
| 923 | } |
| 924 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 925 | //flowDelete deletes a flow from the flow table of that logical device |
| 926 | func (agent *LogicalDeviceAgent) flowDelete(mod *ofp.OfpFlowMod) error { |
| 927 | log.Debug("flowDelete") |
| 928 | if mod == nil { |
| 929 | return nil |
| 930 | } |
| 931 | agent.lockLogicalDevice.Lock() |
| 932 | defer agent.lockLogicalDevice.Unlock() |
| 933 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 934 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 935 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 936 | var meters []*ofp.OfpMeterEntry |
| 937 | var flows []*ofp.OfpFlowStats |
| 938 | |
| 939 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 940 | flows = lDevice.Flows.Items |
| 941 | } |
| 942 | |
| 943 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 944 | meters = lDevice.Meters.Items |
| 945 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 946 | //build a list of what to keep vs what to delete |
| 947 | toKeep := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 948 | toDelete := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 949 | for _, f := range flows { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 950 | // Check whether the flow and the flowmod matches |
| 951 | if fu.FlowMatch(f, fu.FlowStatsEntryFromFlowModMessage(mod)) { |
| 952 | toDelete = append(toDelete, f) |
| 953 | continue |
| 954 | } |
| 955 | // Check wild card match |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 956 | if !fu.FlowMatchesMod(f, mod) { |
| 957 | toKeep = append(toKeep, f) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 958 | } else { |
| 959 | toDelete = append(toDelete, f) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 963 | log.Debugw("flowDelete", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "toKeep": len(toKeep), "toDelete": toDelete}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 964 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 965 | //Update flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 966 | if len(toDelete) > 0 { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 967 | var flowMetadata voltha.FlowMetadata |
| 968 | if err := agent.GetMeterConfig(toDelete, meters, &flowMetadata); err != nil { // This should never happen |
| 969 | log.Error("Meter-referred-in-flows-not-present") |
| 970 | return errors.New("Meter-referred-in-flows-not-present") |
| 971 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 972 | deviceRules := agent.flowDecomposer.DecomposeRules(agent, ofp.Flows{Items: toDelete}, ofp.FlowGroups{}) |
| 973 | log.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 974 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 975 | if err := agent.deleteDeviceFlowsAndGroups(deviceRules, &flowMetadata); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 976 | log.Errorw("failure-updating-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 977 | return err |
| 978 | } |
| 979 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 980 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: toKeep}); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 981 | log.Errorw("cannot-update-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 982 | return err |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | //TODO: send announcement on delete |
| 987 | return nil |
| 988 | } |
| 989 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 990 | func (agent *LogicalDeviceAgent) addDeviceFlowsAndGroups(deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 991 | log.Debugw("addDeviceFlowsAndGroups", log.Fields{"logicalDeviceID": agent.logicalDeviceID, "deviceRules": deviceRules, "flowMetadata": flowMetadata}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 992 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 993 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 994 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 995 | response := coreutils.NewResponse() |
| 996 | responses = append(responses, response) |
| 997 | go func(deviceId string, value *fu.FlowsAndGroups) { |
| 998 | if err := agent.deviceMgr.addFlowsAndGroups(deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 999 | log.Errorw("flow-add-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1000 | response.Error(status.Errorf(codes.Internal, "flow-add-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1001 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1002 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1003 | }(deviceID, value) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1004 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1005 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1006 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1007 | return status.Errorf(codes.Aborted, "errors-%s", res) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1008 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1009 | return nil |
| 1010 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1011 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1012 | func (agent *LogicalDeviceAgent) deleteDeviceFlowsAndGroups(deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1013 | log.Debugw("deleteDeviceFlowsAndGroups", log.Fields{"logicalDeviceID": agent.logicalDeviceID}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1014 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1015 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1016 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1017 | response := coreutils.NewResponse() |
| 1018 | responses = append(responses, response) |
| 1019 | go func(deviceId string, value *fu.FlowsAndGroups) { |
| 1020 | if err := agent.deviceMgr.deleteFlowsAndGroups(deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1021 | log.Error("flow-delete-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1022 | response.Error(status.Errorf(codes.Internal, "flow-delete-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1023 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1024 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1025 | }(deviceID, value) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1026 | } |
| 1027 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1028 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1029 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 1030 | } |
| 1031 | return nil |
| 1032 | } |
| 1033 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1034 | func (agent *LogicalDeviceAgent) updateDeviceFlowsAndGroups(deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1035 | log.Debugw("updateDeviceFlowsAndGroups", log.Fields{"logicalDeviceID": agent.logicalDeviceID}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1036 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1037 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1038 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1039 | response := coreutils.NewResponse() |
| 1040 | responses = append(responses, response) |
| 1041 | go func(deviceId string, value *fu.FlowsAndGroups) { |
| 1042 | if err := agent.deviceMgr.updateFlowsAndGroups(deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1043 | log.Error("flow-update-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1044 | response.Error(status.Errorf(codes.Internal, "flow-update-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1045 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1046 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1047 | }(deviceID, value) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1048 | } |
| 1049 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1050 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1051 | return status.Errorf(codes.Aborted, "errors-%s", res) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1052 | } |
| 1053 | return nil |
| 1054 | } |
| 1055 | |
| 1056 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
| 1057 | func (agent *LogicalDeviceAgent) flowDeleteStrict(mod *ofp.OfpFlowMod) error { |
| 1058 | log.Debug("flowDeleteStrict") |
| 1059 | if mod == nil { |
| 1060 | return nil |
| 1061 | } |
| 1062 | agent.lockLogicalDevice.Lock() |
| 1063 | defer agent.lockLogicalDevice.Unlock() |
| 1064 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1065 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1066 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1067 | var meters []*ofp.OfpMeterEntry |
| 1068 | var flows []*ofp.OfpFlowStats |
| 1069 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 1070 | meters = lDevice.Meters.Items |
| 1071 | } |
| 1072 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 1073 | flows = lDevice.Flows.Items |
| 1074 | } |
| 1075 | |
| 1076 | changedFlow := false |
| 1077 | changedMeter := false |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1078 | flow := fu.FlowStatsEntryFromFlowModMessage(mod) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1079 | flowsToDelete := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1080 | idx := fu.FindFlows(flows, flow) |
| 1081 | if idx >= 0 { |
Gamze Abaka | 6e4ac16 | 2019-10-21 11:10:10 +0000 | [diff] [blame] | 1082 | changedMeter = agent.updateFlowCountOfMeterStats(mod, meters, flows[idx]) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1083 | flowsToDelete = append(flowsToDelete, flows[idx]) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1084 | flows = append(flows[:idx], flows[idx+1:]...) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1085 | changedFlow = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1086 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1087 | return fmt.Errorf("Cannot delete flow - %s", flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1088 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1089 | if changedMeter { |
| 1090 | //Update model |
| 1091 | metersToUpdate := &ofp.Meters{} |
| 1092 | if lDevice.Meters != nil { |
| 1093 | metersToUpdate = &ofp.Meters{Items: meters} |
| 1094 | } |
| 1095 | if err := agent.updateLogicalDeviceMetersWithoutLock(metersToUpdate); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1096 | log.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1097 | return err |
| 1098 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1099 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1100 | } |
| 1101 | if changedFlow { |
| 1102 | var flowMetadata voltha.FlowMetadata |
| 1103 | if err := agent.GetMeterConfig(flowsToDelete, meters, &flowMetadata); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1104 | log.Error("meter-referred-in-flows-not-present") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1105 | return err |
| 1106 | } |
| 1107 | deviceRules := agent.flowDecomposer.DecomposeRules(agent, ofp.Flows{Items: flowsToDelete}, ofp.FlowGroups{}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1108 | log.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 1109 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1110 | if err := agent.deleteDeviceFlowsAndGroups(deviceRules, &flowMetadata); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1111 | log.Errorw("failure-deleting-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1112 | return err |
| 1113 | } |
| 1114 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1115 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: flows}); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1116 | log.Errorw("cannot-update-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1117 | return err |
| 1118 | } |
| 1119 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1120 | return nil |
| 1121 | } |
| 1122 | |
| 1123 | //flowModify modifies a flow from the flow table of that logical device |
| 1124 | func (agent *LogicalDeviceAgent) flowModify(mod *ofp.OfpFlowMod) error { |
| 1125 | return errors.New("flowModify not implemented") |
| 1126 | } |
| 1127 | |
| 1128 | //flowModifyStrict deletes a flow from the flow table of that logical device |
| 1129 | func (agent *LogicalDeviceAgent) flowModifyStrict(mod *ofp.OfpFlowMod) error { |
| 1130 | return errors.New("flowModifyStrict not implemented") |
| 1131 | } |
| 1132 | |
| 1133 | func (agent *LogicalDeviceAgent) groupAdd(groupMod *ofp.OfpGroupMod) error { |
| 1134 | log.Debug("groupAdd") |
| 1135 | if groupMod == nil { |
| 1136 | return nil |
| 1137 | } |
| 1138 | agent.lockLogicalDevice.Lock() |
| 1139 | defer agent.lockLogicalDevice.Unlock() |
| 1140 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1141 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1142 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1143 | groups := lDevice.FlowGroups.Items |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1144 | if fu.FindGroup(groups, groupMod.GroupId) == -1 { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1145 | groups = append(groups, fu.GroupEntryFromGroupMod(groupMod)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1146 | |
Esin Karaman | 2ea5921 | 2019-12-06 11:41:58 +0000 | [diff] [blame] | 1147 | deviceRules := fu.NewDeviceRules() |
| 1148 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 1149 | fg := fu.NewFlowsAndGroups() |
| 1150 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 1151 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 1152 | |
| 1153 | log.Debugw("rules", log.Fields{"rules for group-add": deviceRules.String()}) |
| 1154 | if err := agent.addDeviceFlowsAndGroups(deviceRules, &voltha.FlowMetadata{}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1155 | log.Errorw("failure-updating-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1156 | return err |
| 1157 | } |
| 1158 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1159 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1160 | log.Errorw("cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1161 | return err |
| 1162 | } |
| 1163 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1164 | return fmt.Errorf("Groups %d already present", groupMod.GroupId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1165 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1166 | return nil |
| 1167 | } |
| 1168 | |
| 1169 | func (agent *LogicalDeviceAgent) groupDelete(groupMod *ofp.OfpGroupMod) error { |
| 1170 | log.Debug("groupDelete") |
| 1171 | if groupMod == nil { |
| 1172 | return nil |
| 1173 | } |
| 1174 | agent.lockLogicalDevice.Lock() |
| 1175 | defer agent.lockLogicalDevice.Unlock() |
| 1176 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1177 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1178 | groups := lDevice.FlowGroups.Items |
| 1179 | flows := lDevice.Flows.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1180 | var groupsChanged bool |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1181 | flowsChanged := false |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1182 | groupID := groupMod.GroupId |
| 1183 | if groupID == uint32(ofp.OfpGroup_OFPG_ALL) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1184 | //TODO we must delete all flows that point to this group and |
| 1185 | //signal controller as requested by flow's flag |
| 1186 | groups = []*ofp.OfpGroupEntry{} |
| 1187 | groupsChanged = true |
| 1188 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1189 | idx := fu.FindGroup(groups, groupID) |
| 1190 | if idx == -1 { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1191 | return nil // Valid case |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1192 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1193 | flowsChanged, flows = fu.FlowsDeleteByGroupId(flows, groupID) |
| 1194 | groups = append(groups[:idx], groups[idx+1:]...) |
| 1195 | groupsChanged = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1196 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1197 | if flowsChanged || groupsChanged { |
| 1198 | deviceRules := agent.flowDecomposer.DecomposeRules(agent, ofp.Flows{Items: flows}, ofp.FlowGroups{Items: groups}) |
| 1199 | log.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 1200 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1201 | if err := agent.updateDeviceFlowsAndGroups(deviceRules, nil); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1202 | log.Errorw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1203 | return err |
| 1204 | } |
| 1205 | } |
| 1206 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1207 | if groupsChanged { |
| 1208 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1209 | log.Errorw("cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1210 | return err |
| 1211 | } |
| 1212 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1213 | if flowsChanged { |
| 1214 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: flows}); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1215 | log.Errorw("cannot-update-flow", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1216 | return err |
| 1217 | } |
| 1218 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1219 | return nil |
| 1220 | } |
| 1221 | |
| 1222 | func (agent *LogicalDeviceAgent) groupModify(groupMod *ofp.OfpGroupMod) error { |
| 1223 | log.Debug("groupModify") |
| 1224 | if groupMod == nil { |
| 1225 | return nil |
| 1226 | } |
| 1227 | agent.lockLogicalDevice.Lock() |
| 1228 | defer agent.lockLogicalDevice.Unlock() |
| 1229 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1230 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1231 | groups := lDevice.FlowGroups.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1232 | var groupsChanged bool |
| 1233 | groupID := groupMod.GroupId |
| 1234 | idx := fu.FindGroup(groups, groupID) |
| 1235 | if idx == -1 { |
| 1236 | return fmt.Errorf("group-absent:%d", groupID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1237 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1238 | //replace existing group entry with new group definition |
| 1239 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
| 1240 | groups[idx] = groupEntry |
| 1241 | groupsChanged = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1242 | if groupsChanged { |
Esin Karaman | 2ea5921 | 2019-12-06 11:41:58 +0000 | [diff] [blame] | 1243 | deviceRules := fu.NewDeviceRules() |
| 1244 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 1245 | fg := fu.NewFlowsAndGroups() |
| 1246 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 1247 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1248 | |
Esin Karaman | 2ea5921 | 2019-12-06 11:41:58 +0000 | [diff] [blame] | 1249 | log.Debugw("rules", log.Fields{"rules for group-modify": deviceRules.String()}) |
| 1250 | if err := agent.updateDeviceFlowsAndGroups(deviceRules, &voltha.FlowMetadata{}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1251 | log.Errorw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1252 | return err |
| 1253 | } |
| 1254 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1255 | //lDevice.FlowGroups.Items = groups |
| 1256 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1257 | log.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1258 | return err |
| 1259 | } |
| 1260 | } |
| 1261 | return nil |
| 1262 | } |
| 1263 | |
| 1264 | // deleteLogicalPort removes the logical port |
| 1265 | func (agent *LogicalDeviceAgent) deleteLogicalPort(lPort *voltha.LogicalPort) error { |
| 1266 | agent.lockLogicalDevice.Lock() |
| 1267 | defer agent.lockLogicalDevice.Unlock() |
| 1268 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1269 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
| 1270 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1271 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1272 | for i, logicalPort := range logicalDevice.Ports { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1273 | if logicalPort.Id == lPort.Id { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1274 | index = i |
| 1275 | break |
| 1276 | } |
| 1277 | } |
| 1278 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1279 | copy(logicalDevice.Ports[index:], logicalDevice.Ports[index+1:]) |
| 1280 | logicalDevice.Ports[len(logicalDevice.Ports)-1] = nil |
| 1281 | logicalDevice.Ports = logicalDevice.Ports[:len(logicalDevice.Ports)-1] |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1282 | log.Debugw("logical-port-deleted", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1283 | if err := agent.updateLogicalDeviceWithoutLock(logicalDevice); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1284 | log.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1285 | return err |
| 1286 | } |
| 1287 | // Reset the logical device graph |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1288 | go agent.generateDeviceGraph() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1289 | } |
| 1290 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1293 | // deleteLogicalPorts removes the logical ports associated with that deviceId |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1294 | func (agent *LogicalDeviceAgent) deleteLogicalPorts(deviceID string) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1295 | agent.lockLogicalDevice.Lock() |
| 1296 | defer agent.lockLogicalDevice.Unlock() |
| 1297 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1298 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1299 | updatedLPorts := []*voltha.LogicalPort{} |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1300 | for _, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1301 | if logicalPort.DeviceId != deviceID { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1302 | updatedLPorts = append(updatedLPorts, logicalPort) |
| 1303 | } |
| 1304 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1305 | logicalDevice.Ports = updatedLPorts |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1306 | log.Debugw("updated-logical-ports", log.Fields{"ports": updatedLPorts}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1307 | if err := agent.updateLogicalDeviceWithoutLock(logicalDevice); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1308 | log.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1309 | return err |
| 1310 | } |
| 1311 | // Reset the logical device graph |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1312 | go agent.generateDeviceGraph() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1313 | |
| 1314 | return nil |
| 1315 | } |
| 1316 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1317 | // enableLogicalPort enables the logical port |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1318 | func (agent *LogicalDeviceAgent) enableLogicalPort(lPortID string) error { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1319 | agent.lockLogicalDevice.Lock() |
| 1320 | defer agent.lockLogicalDevice.Unlock() |
| 1321 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1322 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
| 1323 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1324 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1325 | for i, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1326 | if logicalPort.Id == lPortID { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1327 | index = i |
| 1328 | break |
| 1329 | } |
| 1330 | } |
| 1331 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1332 | logicalDevice.Ports[index].OfpPort.Config = logicalDevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 1333 | return agent.updateLogicalDeviceWithoutLock(logicalDevice) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1334 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1335 | return status.Errorf(codes.NotFound, "Port %s on Logical Device %s", lPortID, agent.logicalDeviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | // disableLogicalPort disabled the logical port |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1339 | func (agent *LogicalDeviceAgent) disableLogicalPort(lPortID string) error { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1340 | agent.lockLogicalDevice.Lock() |
| 1341 | defer agent.lockLogicalDevice.Unlock() |
| 1342 | |
| 1343 | // Get the most up to date logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1344 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1345 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1346 | for i, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1347 | if logicalPort.Id == lPortID { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1348 | index = i |
| 1349 | break |
| 1350 | } |
| 1351 | } |
| 1352 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1353 | logicalDevice.Ports[index].OfpPort.Config = (logicalDevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN)) | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 1354 | return agent.updateLogicalDeviceWithoutLock(logicalDevice) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1355 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1356 | return status.Errorf(codes.NotFound, "Port %s on Logical Device %s", lPortID, agent.logicalDeviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1359 | func (agent *LogicalDeviceAgent) getPreCalculatedRoute(ingress, egress uint32) []graph.RouteHop { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1360 | log.Debugw("ROUTE", log.Fields{"len": len(agent.deviceGraph.Routes)}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1361 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1362 | log.Debugw("ROUTELINKS", log.Fields{"ingress": ingress, "egress": egress, "routelink": routeLink}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1363 | if ingress == routeLink.Ingress && egress == routeLink.Egress { |
| 1364 | return route |
| 1365 | } |
| 1366 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1367 | log.Warnw("no-route", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "ingress": ingress, "egress": egress}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1368 | return nil |
| 1369 | } |
| 1370 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1371 | // GetRoute returns route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1372 | func (agent *LogicalDeviceAgent) GetRoute(ingressPortNo uint32, egressPortNo uint32) []graph.RouteHop { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1373 | log.Debugw("getting-route", log.Fields{"ingress-port": ingressPortNo, "egress-port": egressPortNo}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1374 | routes := make([]graph.RouteHop, 0) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1375 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1376 | // Note: A port value of 0 is equivalent to a nil port |
| 1377 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1378 | // Consider different possibilities |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1379 | if egressPortNo != 0 && ((egressPortNo & 0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER)) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1380 | log.Debugw("controller-flow", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
| 1381 | if agent.isNNIPort(ingressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1382 | //This is a trap on the NNI Port |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1383 | if len(agent.deviceGraph.Routes) == 0 { |
| 1384 | // If there are no routes set (usually when the logical device has only NNI port(s), then just return an |
Humera Kouser | 4ff8901 | 2019-08-25 19:01:51 -0400 | [diff] [blame] | 1385 | // route with same IngressHop and EgressHop |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1386 | hop := graph.RouteHop{DeviceID: agent.rootDeviceID, Ingress: ingressPortNo, Egress: ingressPortNo} |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1387 | routes = append(routes, hop) |
| 1388 | routes = append(routes, hop) |
| 1389 | return routes |
| 1390 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1391 | //Return a 'half' route to make the flow decomposer logic happy |
| 1392 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1393 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1394 | routes = append(routes, graph.RouteHop{}) // first hop is set to empty |
| 1395 | routes = append(routes, route[1]) |
| 1396 | return routes |
| 1397 | } |
| 1398 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1399 | log.Warnw("no-upstream-route", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1400 | return nil |
| 1401 | } |
| 1402 | //treat it as if the output port is the first NNI of the OLT |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1403 | var err error |
| 1404 | if egressPortNo, err = agent.getFirstNNIPort(); err != nil { |
| 1405 | log.Warnw("no-nni-port", log.Fields{"error": err}) |
| 1406 | return nil |
| 1407 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1408 | } |
| 1409 | //If ingress port is not specified (nil), it may be a wildcarded |
| 1410 | //route if egress port is OFPP_CONTROLLER or a nni logical port, |
| 1411 | //in which case we need to create a half-route where only the egress |
| 1412 | //hop is filled, the first hop is nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1413 | if ingressPortNo == 0 && agent.isNNIPort(egressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1414 | // We can use the 2nd hop of any upstream route, so just find the first upstream: |
| 1415 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1416 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1417 | routes = append(routes, graph.RouteHop{}) // first hop is set to empty |
| 1418 | routes = append(routes, route[1]) |
| 1419 | return routes |
| 1420 | } |
| 1421 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1422 | log.Warnw("no-upstream-route", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1423 | return nil |
| 1424 | } |
| 1425 | //If egress port is not specified (nil), we can also can return a "half" route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1426 | if egressPortNo == 0 { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1427 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1428 | if routeLink.Ingress == ingressPortNo { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1429 | routes = append(routes, route[0]) |
| 1430 | routes = append(routes, graph.RouteHop{}) |
| 1431 | return routes |
| 1432 | } |
| 1433 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1434 | log.Warnw("no-downstream-route", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1435 | return nil |
| 1436 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1437 | // Return the pre-calculated route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1438 | return agent.getPreCalculatedRoute(ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1441 | //GetWildcardInputPorts filters out the logical port number from the set of logical ports on the device and |
| 1442 | //returns their port numbers. This function is invoked only during flow decomposition where the lock on the logical |
| 1443 | //device is already held. Therefore it is safe to retrieve the logical device without lock. |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1444 | func (agent *LogicalDeviceAgent) GetWildcardInputPorts(excludePort ...uint32) []uint32 { |
| 1445 | lPorts := make([]uint32, 0) |
| 1446 | var exclPort uint32 |
| 1447 | if len(excludePort) == 1 { |
| 1448 | exclPort = excludePort[0] |
| 1449 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1450 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1451 | for _, port := range lDevice.Ports { |
| 1452 | if port.OfpPort.PortNo != exclPort { |
| 1453 | lPorts = append(lPorts, port.OfpPort.PortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | return lPorts |
| 1457 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1458 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1459 | // GetDeviceGraph returns device graph |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1460 | func (agent *LogicalDeviceAgent) GetDeviceGraph() *graph.DeviceGraph { |
| 1461 | return agent.deviceGraph |
| 1462 | } |
| 1463 | |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 1464 | //updateRoutes rebuilds the device graph if not done already |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1465 | func (agent *LogicalDeviceAgent) updateRoutes(device *voltha.Device, port *voltha.Port) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1466 | log.Debugf("updateRoutes", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "device": device.Id, "port": port}) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1467 | agent.lockLogicalDevice.Lock() |
| 1468 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1469 | if agent.deviceGraph == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1470 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceID, agent.deviceMgr.GetDevice) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1471 | } |
| 1472 | // Get all the logical ports on that logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1473 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1474 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1475 | //TODO: Find a better way to refresh only missing routes |
| 1476 | agent.deviceGraph.ComputeRoutes(lDevice.Ports) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1477 | agent.deviceGraph.Print() |
| 1478 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1481 | //updateDeviceGraph updates the device graph if not done already and setup the default rules as well |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1482 | func (agent *LogicalDeviceAgent) updateDeviceGraph(lp *voltha.LogicalPort) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1483 | log.Debugf("updateDeviceGraph", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1484 | agent.lockLogicalDevice.Lock() |
| 1485 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1486 | if agent.deviceGraph == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1487 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceID, agent.deviceMgr.GetDevice) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1488 | } |
| 1489 | agent.deviceGraph.AddPort(lp) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1490 | agent.deviceGraph.Print() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1491 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1492 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1493 | //generateDeviceGraph regenerates the device graph |
| 1494 | func (agent *LogicalDeviceAgent) generateDeviceGraph() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1495 | log.Debugw("generateDeviceGraph", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1496 | agent.lockLogicalDevice.Lock() |
| 1497 | defer agent.lockLogicalDevice.Unlock() |
| 1498 | // Get the latest logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1499 | ld := agent.getLogicalDeviceWithoutLock() |
| 1500 | log.Debugw("generating-graph", log.Fields{"lDeviceId": agent.logicalDeviceID, "lPorts": len(ld.Ports)}) |
| 1501 | if agent.deviceGraph == nil { |
| 1502 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceID, agent.deviceMgr.GetDevice) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1503 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1504 | agent.deviceGraph.ComputeRoutes(ld.Ports) |
| 1505 | agent.deviceGraph.Print() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1506 | } |
| 1507 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1508 | // diff go over two lists of logical ports and return what's new, what's changed and what's removed. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1509 | func diff(oldList, newList []*voltha.LogicalPort) (newPorts, changedPorts, deletedPorts []*voltha.LogicalPort) { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1510 | newPorts = make([]*voltha.LogicalPort, 0) |
| 1511 | changedPorts = make([]*voltha.LogicalPort, 0) |
| 1512 | deletedPorts = make([]*voltha.LogicalPort, 0) |
| 1513 | for _, o := range oldList { |
| 1514 | found := false |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1515 | for _, n := range newList { |
| 1516 | if o.Id == n.Id { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1517 | found = true |
| 1518 | break |
| 1519 | } |
| 1520 | } |
| 1521 | if !found { |
| 1522 | deletedPorts = append(deletedPorts, o) |
| 1523 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1524 | } |
| 1525 | for _, n := range newList { |
| 1526 | found := false |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1527 | changed := false |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1528 | for _, o := range oldList { |
| 1529 | if o.Id == n.Id { |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1530 | changed = !reflect.DeepEqual(o, n) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1531 | found = true |
| 1532 | break |
| 1533 | } |
| 1534 | } |
| 1535 | if !found { |
| 1536 | newPorts = append(newPorts, n) |
| 1537 | } |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1538 | if changed { |
| 1539 | changedPorts = append(changedPorts, n) |
| 1540 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1541 | } |
| 1542 | return |
| 1543 | } |
| 1544 | |
| 1545 | // portUpdated is invoked when a port is updated on the logical device. Until |
| 1546 | // the POST_ADD notification is fixed, we will use the logical device to |
| 1547 | // update that data. |
| 1548 | func (agent *LogicalDeviceAgent) portUpdated(args ...interface{}) interface{} { |
| 1549 | log.Debugw("portUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 1550 | |
| 1551 | var oldLD *voltha.LogicalDevice |
| 1552 | var newlD *voltha.LogicalDevice |
| 1553 | |
| 1554 | var ok bool |
| 1555 | if oldLD, ok = args[0].(*voltha.LogicalDevice); !ok { |
| 1556 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 1557 | return nil |
| 1558 | } |
| 1559 | if newlD, ok = args[1].(*voltha.LogicalDevice); !ok { |
| 1560 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 1561 | return nil |
| 1562 | } |
| 1563 | |
| 1564 | if reflect.DeepEqual(oldLD.Ports, newlD.Ports) { |
| 1565 | log.Debug("ports-have-not-changed") |
| 1566 | return nil |
| 1567 | } |
| 1568 | |
| 1569 | // Get the difference between the two list |
| 1570 | newPorts, changedPorts, deletedPorts := diff(oldLD.Ports, newlD.Ports) |
| 1571 | |
| 1572 | // Send the port change events to the OF controller |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1573 | for _, newP := range newPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1574 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1575 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_ADD, Desc: newP.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1576 | } |
| 1577 | for _, change := range changedPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1578 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1579 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_MODIFY, Desc: change.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1580 | } |
| 1581 | for _, del := range deletedPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1582 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1583 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_DELETE, Desc: del.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | return nil |
| 1587 | } |
| 1588 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1589 | // addNNILogicalPort adds an NNI port to the logical device. It returns a bool representing whether a port has been |
| 1590 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1591 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1592 | // scenario. This also applies to the case where the port was already added. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1593 | func (agent *LogicalDeviceAgent) addNNILogicalPort(device *voltha.Device, port *voltha.Port) (bool, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1594 | log.Debugw("addNNILogicalPort", log.Fields{"NNI": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1595 | if device.AdminState != voltha.AdminState_ENABLED || device.OperStatus != voltha.OperStatus_ACTIVE { |
| 1596 | log.Infow("device-not-ready", log.Fields{"deviceId": device.Id, "admin": device.AdminState, "oper": device.OperStatus}) |
| 1597 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1598 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1599 | agent.lockLogicalDevice.RLock() |
| 1600 | if agent.portExist(device, port) { |
| 1601 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
| 1602 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1603 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1604 | } |
| 1605 | agent.lockLogicalDevice.RUnlock() |
| 1606 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1607 | var portCap *ic.PortCapability |
| 1608 | var err error |
| 1609 | // First get the port capability |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1610 | if portCap, err = agent.deviceMgr.getPortCapability(context.TODO(), device.Id, port.PortNo); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1611 | log.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1612 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1613 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1614 | |
| 1615 | agent.lockLogicalDevice.Lock() |
| 1616 | defer agent.lockLogicalDevice.Unlock() |
| 1617 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1618 | if agent.portExist(device, port) { |
| 1619 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1620 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1623 | portCap.Port.RootPort = true |
| 1624 | lp := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
| 1625 | lp.DeviceId = device.Id |
| 1626 | lp.Id = fmt.Sprintf("nni-%d", port.PortNo) |
| 1627 | lp.OfpPort.PortNo = port.PortNo |
| 1628 | lp.OfpPort.Name = lp.Id |
| 1629 | lp.DevicePortNo = port.PortNo |
| 1630 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1631 | ld := agent.getLogicalDeviceWithoutLock() |
| 1632 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1633 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 1634 | if cloned.Ports == nil { |
| 1635 | cloned.Ports = make([]*voltha.LogicalPort, 0) |
| 1636 | } |
| 1637 | cloned.Ports = append(cloned.Ports, lp) |
| 1638 | |
| 1639 | if err = agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 1640 | log.Errorw("error-updating-logical-device", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1641 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1642 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1643 | |
| 1644 | // Update the device graph with this new logical port |
| 1645 | clonedLP := (proto.Clone(lp)).(*voltha.LogicalPort) |
| 1646 | go agent.updateDeviceGraph(clonedLP) |
| 1647 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1648 | return true, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1649 | } |
| 1650 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1651 | func (agent *LogicalDeviceAgent) portExist(device *voltha.Device, port *voltha.Port) bool { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1652 | ldevice := agent.getLogicalDeviceWithoutLock() |
| 1653 | for _, lPort := range ldevice.Ports { |
| 1654 | if lPort.DeviceId == device.Id && lPort.DevicePortNo == port.PortNo && lPort.Id == port.Label { |
| 1655 | return true |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1656 | } |
| 1657 | } |
| 1658 | return false |
| 1659 | } |
| 1660 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1661 | // addUNILogicalPort adds an UNI port to the logical device. It returns a bool representing whether a port has been |
| 1662 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1663 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1664 | // scenario. This also applies to the case where the port was already added. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1665 | func (agent *LogicalDeviceAgent) addUNILogicalPort(childDevice *voltha.Device, port *voltha.Port) (bool, error) { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1666 | log.Debugw("addUNILogicalPort", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1667 | if childDevice.AdminState != voltha.AdminState_ENABLED || childDevice.OperStatus != voltha.OperStatus_ACTIVE { |
| 1668 | log.Infow("device-not-ready", log.Fields{"deviceId": childDevice.Id, "admin": childDevice.AdminState, "oper": childDevice.OperStatus}) |
| 1669 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1670 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1671 | agent.lockLogicalDevice.RLock() |
| 1672 | if agent.portExist(childDevice, port) { |
| 1673 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
| 1674 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1675 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1676 | } |
| 1677 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1678 | var portCap *ic.PortCapability |
| 1679 | var err error |
| 1680 | // First get the port capability |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1681 | if portCap, err = agent.deviceMgr.getPortCapability(context.TODO(), childDevice.Id, port.PortNo); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1682 | log.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1683 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1684 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1685 | agent.lockLogicalDevice.Lock() |
| 1686 | defer agent.lockLogicalDevice.Unlock() |
| 1687 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1688 | if agent.portExist(childDevice, port) { |
| 1689 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1690 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1691 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1692 | // Get stored logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1693 | ldevice := agent.getLogicalDeviceWithoutLock() |
| 1694 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1695 | log.Debugw("adding-uni", log.Fields{"deviceId": childDevice.Id}) |
| 1696 | portCap.Port.RootPort = false |
| 1697 | portCap.Port.Id = port.Label |
| 1698 | portCap.Port.OfpPort.PortNo = port.PortNo |
| 1699 | portCap.Port.DeviceId = childDevice.Id |
| 1700 | portCap.Port.DevicePortNo = port.PortNo |
| 1701 | cloned := (proto.Clone(ldevice)).(*voltha.LogicalDevice) |
| 1702 | if cloned.Ports == nil { |
| 1703 | cloned.Ports = make([]*voltha.LogicalPort, 0) |
| 1704 | } |
| 1705 | cloned.Ports = append(cloned.Ports, portCap.Port) |
| 1706 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 1707 | return false, err |
| 1708 | } |
| 1709 | // Update the device graph with this new logical port |
| 1710 | clonedLP := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
| 1711 | go agent.updateDeviceGraph(clonedLP) |
| 1712 | return true, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1713 | } |
| 1714 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1715 | func (agent *LogicalDeviceAgent) packetOut(packet *ofp.OfpPacketOut) { |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1716 | log.Debugw("packet-out", log.Fields{ |
| 1717 | "packet": hex.EncodeToString(packet.Data), |
| 1718 | "inPort": packet.GetInPort(), |
| 1719 | }) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1720 | outPort := fu.GetPacketOutPort(packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1721 | //frame := packet.GetData() |
| 1722 | //TODO: Use a channel between the logical agent and the device agent |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1723 | if err := agent.deviceMgr.packetOut(agent.rootDeviceID, outPort, packet); err != nil { |
| 1724 | log.Error("packetout-failed", log.Fields{"logicalDeviceID": agent.rootDeviceID}) |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 1725 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1728 | func (agent *LogicalDeviceAgent) packetIn(port uint32, transactionID string, packet []byte) { |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1729 | log.Debugw("packet-in", log.Fields{ |
| 1730 | "port": port, |
| 1731 | "packet": hex.EncodeToString(packet), |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1732 | "transactionId": transactionID, |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1733 | }) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1734 | packetIn := fu.MkPacketIn(port, packet) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1735 | agent.ldeviceMgr.grpcNbiHdlr.sendPacketIn(agent.logicalDeviceID, transactionID, packetIn) |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1736 | log.Debugw("sending-packet-in", log.Fields{"packet": hex.EncodeToString(packetIn.Data)}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1737 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1738 | |
| 1739 | func (agent *LogicalDeviceAgent) addLogicalPortToMap(portNo uint32, nniPort bool) { |
| 1740 | agent.lockLogicalPortsNo.Lock() |
| 1741 | defer agent.lockLogicalPortsNo.Unlock() |
| 1742 | if exist := agent.logicalPortsNo[portNo]; !exist { |
| 1743 | agent.logicalPortsNo[portNo] = nniPort |
| 1744 | } |
| 1745 | } |
| 1746 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1747 | func (agent *LogicalDeviceAgent) addLogicalPortsToMap(lps []*voltha.LogicalPort) { |
| 1748 | agent.lockLogicalPortsNo.Lock() |
| 1749 | defer agent.lockLogicalPortsNo.Unlock() |
| 1750 | for _, lp := range lps { |
| 1751 | if exist := agent.logicalPortsNo[lp.DevicePortNo]; !exist { |
| 1752 | agent.logicalPortsNo[lp.DevicePortNo] = lp.RootPort |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1757 | func (agent *LogicalDeviceAgent) isNNIPort(portNo uint32) bool { |
| 1758 | agent.lockLogicalPortsNo.RLock() |
| 1759 | defer agent.lockLogicalPortsNo.RUnlock() |
| 1760 | if exist := agent.logicalPortsNo[portNo]; exist { |
| 1761 | return agent.logicalPortsNo[portNo] |
| 1762 | } |
| 1763 | return false |
| 1764 | } |
| 1765 | |
| 1766 | func (agent *LogicalDeviceAgent) getFirstNNIPort() (uint32, error) { |
| 1767 | agent.lockLogicalPortsNo.RLock() |
| 1768 | defer agent.lockLogicalPortsNo.RUnlock() |
| 1769 | for portNo, nni := range agent.logicalPortsNo { |
| 1770 | if nni { |
| 1771 | return portNo, nil |
| 1772 | } |
| 1773 | } |
| 1774 | return 0, status.Error(codes.NotFound, "No NNI port found") |
| 1775 | } |
Esin Karaman | 09959ae | 2019-11-29 13:59:58 +0000 | [diff] [blame] | 1776 | |
| 1777 | //GetNNIPorts returns NNI ports. |
| 1778 | func (agent *LogicalDeviceAgent) GetNNIPorts() []uint32 { |
| 1779 | agent.lockLogicalPortsNo.RLock() |
| 1780 | defer agent.lockLogicalPortsNo.RUnlock() |
| 1781 | nniPorts := make([]uint32, 0) |
| 1782 | for portNo, nni := range agent.logicalPortsNo { |
| 1783 | if nni { |
| 1784 | nniPorts = append(nniPorts, portNo) |
| 1785 | } |
| 1786 | } |
| 1787 | return nniPorts |
| 1788 | } |