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