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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 20 | "errors" |
| 21 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "github.com/gogo/protobuf/proto" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 25 | fd "github.com/opencord/voltha-go/rw_core/flow_decomposition" |
| 26 | "github.com/opencord/voltha-go/rw_core/graph" |
| 27 | fu "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 28 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 29 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 30 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 31 | "google.golang.org/grpc/codes" |
| 32 | "google.golang.org/grpc/status" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 33 | "reflect" |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 34 | "sync" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type LogicalDeviceAgent struct { |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame^] | 38 | logicalDeviceId string |
| 39 | rootDeviceId string |
| 40 | deviceMgr *DeviceManager |
| 41 | ldeviceMgr *LogicalDeviceManager |
| 42 | clusterDataProxy *model.Proxy |
| 43 | exitChannel chan int |
| 44 | deviceGraph *graph.DeviceGraph |
| 45 | flowProxy *model.Proxy |
| 46 | groupProxy *model.Proxy |
| 47 | ldProxy *model.Proxy |
| 48 | portProxies map[string]*model.Proxy |
| 49 | portProxiesLock sync.RWMutex |
| 50 | lockLogicalDevice sync.RWMutex |
| 51 | logicalPortsNo map[uint32]bool //value is true for NNI port |
| 52 | lockLogicalPortsNo sync.RWMutex |
| 53 | flowDecomposer *fd.FlowDecomposer |
| 54 | defaultTimeout int64 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 57 | func newLogicalDeviceAgent(id string, deviceId string, ldeviceMgr *LogicalDeviceManager, |
| 58 | deviceMgr *DeviceManager, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 59 | cdProxy *model.Proxy, timeout int64) *LogicalDeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | var agent LogicalDeviceAgent |
| 61 | agent.exitChannel = make(chan int, 1) |
| 62 | agent.logicalDeviceId = id |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 63 | agent.rootDeviceId = deviceId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 64 | agent.deviceMgr = deviceMgr |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 65 | agent.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 66 | agent.ldeviceMgr = ldeviceMgr |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 67 | agent.flowDecomposer = fd.NewFlowDecomposer(agent.deviceMgr) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 68 | agent.lockLogicalDevice = sync.RWMutex{} |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 69 | agent.portProxies = make(map[string]*model.Proxy) |
| 70 | agent.portProxiesLock = sync.RWMutex{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 71 | agent.lockLogicalPortsNo = sync.RWMutex{} |
| 72 | agent.logicalPortsNo = make(map[uint32]bool) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 73 | agent.defaultTimeout = timeout |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 74 | return &agent |
| 75 | } |
| 76 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 77 | // start creates the logical device and add it to the data model |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 78 | func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromdB bool) error { |
| 79 | log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId, "loadFromdB": loadFromdB}) |
| 80 | var ld *voltha.LogicalDevice |
| 81 | if !loadFromdB { |
| 82 | //Build the logical device based on information retrieved from the device adapter |
| 83 | var switchCap *ic.SwitchCapability |
| 84 | var err error |
| 85 | if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceId); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 86 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 87 | return err |
| 88 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 89 | ld = &voltha.LogicalDevice{Id: agent.logicalDeviceId, RootDeviceId: agent.rootDeviceId} |
| 90 | |
| 91 | // Create the datapath ID (uint64) using the logical device ID (based on the MAC Address) |
| 92 | var datapathID uint64 |
| 93 | if datapathID, err = CreateDataPathId(agent.logicalDeviceId); err != nil { |
| 94 | log.Errorw("error-creating-datapath-id", log.Fields{"error": err}) |
| 95 | return err |
| 96 | } |
| 97 | ld.DatapathId = datapathID |
| 98 | ld.Desc = (proto.Clone(switchCap.Desc)).(*ofp.OfpDesc) |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 99 | log.Debugw("Switch-capability", log.Fields{"Desc": ld.Desc, "fromAd": switchCap.Desc}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 100 | ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*ofp.OfpSwitchFeatures) |
| 101 | ld.Flows = &ofp.Flows{Items: nil} |
| 102 | ld.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 103 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 104 | agent.lockLogicalDevice.Lock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 105 | // Save the logical device |
| 106 | if added := agent.clusterDataProxy.AddWithID("/logical_devices", ld.Id, ld, ""); added == nil { |
| 107 | log.Errorw("failed-to-add-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 108 | } else { |
| 109 | log.Debugw("logicaldevice-created", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 110 | } |
| 111 | agent.lockLogicalDevice.Unlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 112 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 113 | // TODO: Set the logical ports in a separate call once the port update issue is fixed. |
| 114 | go agent.setupLogicalPorts(ctx) |
| 115 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 116 | } else { |
| 117 | // load from dB - the logical may not exist at this time. On error, just return and the calling function |
| 118 | // will destroy this agent. |
| 119 | var err error |
| 120 | if ld, err = agent.GetLogicalDevice(); err != nil { |
| 121 | log.Warnw("failed-to-load-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 122 | return err |
| 123 | } |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 124 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 125 | // Update the root device Id |
| 126 | agent.rootDeviceId = ld.RootDeviceId |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 127 | |
| 128 | // Setup the local list of logical ports |
| 129 | agent.addLogicalPortsToMap(ld.Ports) |
| 130 | |
| 131 | // Setup the device graph |
| 132 | agent.generateDeviceGraph() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 133 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 134 | agent.lockLogicalDevice.Lock() |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 135 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 136 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 137 | agent.flowProxy = agent.clusterDataProxy.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 138 | fmt.Sprintf("/logical_devices/%s/flows", agent.logicalDeviceId), |
| 139 | false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 140 | agent.groupProxy = agent.clusterDataProxy.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 141 | fmt.Sprintf("/logical_devices/%s/flow_groups", agent.logicalDeviceId), |
| 142 | false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 143 | agent.ldProxy = agent.clusterDataProxy.CreateProxy( |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 144 | fmt.Sprintf("/logical_devices/%s", agent.logicalDeviceId), |
| 145 | false) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 146 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 147 | // TODO: Use a port proxy once the POST_ADD is fixed |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 148 | if agent.ldProxy != nil { |
| 149 | agent.ldProxy.RegisterCallback(model.POST_UPDATE, agent.portUpdated) |
| 150 | } else { |
| 151 | log.Errorw("logical-device-proxy-null", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 152 | return status.Error(codes.Internal, "logical-device-proxy-null") |
| 153 | } |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 154 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | return nil |
| 156 | } |
| 157 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 158 | // stop stops the logical devuce agent. This removes the logical device from the data model. |
| 159 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) { |
| 160 | log.Info("stopping-logical_device-agent") |
| 161 | agent.lockLogicalDevice.Lock() |
| 162 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 163 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 164 | //Remove the logical device from the model |
| 165 | if removed := agent.clusterDataProxy.Remove("/logical_devices/"+agent.logicalDeviceId, ""); removed == nil { |
| 166 | log.Errorw("failed-to-remove-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 167 | } else { |
| 168 | log.Debugw("logicaldevice-removed", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 169 | } |
| 170 | agent.exitChannel <- 1 |
| 171 | log.Info("logical_device-agent-stopped") |
| 172 | } |
| 173 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 174 | // GetLogicalDevice locks the logical device model and then retrieves the latest logical device information |
| 175 | func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) { |
| 176 | log.Debug("GetLogicalDevice") |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 177 | agent.lockLogicalDevice.RLock() |
| 178 | defer agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 179 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 180 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 181 | return lDevice, nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 182 | } |
| 183 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 184 | } |
| 185 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 186 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 187 | log.Debug("ListLogicalDevicePorts") |
| 188 | agent.lockLogicalDevice.RLock() |
| 189 | defer agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 190 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 191 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 192 | lPorts := make([]*voltha.LogicalPort, 0) |
| 193 | for _, port := range lDevice.Ports { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 194 | lPorts = append(lPorts, port) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 195 | } |
| 196 | return &voltha.LogicalPorts{Items: lPorts}, nil |
| 197 | } |
| 198 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 199 | } |
| 200 | |
| 201 | // listFlows locks the logical device model and then retrieves the latest flow information |
| 202 | func (agent *LogicalDeviceAgent) listFlows() []*ofp.OfpFlowStats { |
| 203 | log.Debug("listFlows") |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 204 | agent.lockLogicalDevice.RLock() |
| 205 | defer agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 206 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 207 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 208 | return lDevice.Flows.Items |
| 209 | } |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | // listFlowGroups locks the logical device model and then retrieves the latest flow groups information |
| 214 | func (agent *LogicalDeviceAgent) listFlowGroups() []*ofp.OfpGroupEntry { |
| 215 | log.Debug("listFlowGroups") |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 216 | agent.lockLogicalDevice.RLock() |
| 217 | defer agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 218 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 219 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 220 | return lDevice.FlowGroups.Items |
| 221 | } |
| 222 | return nil |
| 223 | } |
| 224 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 225 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 226 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowsWithoutLock(flows *ofp.Flows) error { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 227 | afterUpdate := agent.flowProxy.Update("/", flows, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 228 | if afterUpdate == nil { |
| 229 | return status.Errorf(codes.Internal, "failed-updating-logical-device-flows:%s", agent.logicalDeviceId) |
| 230 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 231 | return nil |
| 232 | } |
| 233 | |
| 234 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 235 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowGroupsWithoutLock(flowGroups *ofp.FlowGroups) error { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 236 | afterUpdate := agent.groupProxy.Update("/", flowGroups, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 237 | if afterUpdate == nil { |
| 238 | return status.Errorf(codes.Internal, "failed-updating-logical-device-flow-groups:%s", agent.logicalDeviceId) |
| 239 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 240 | return nil |
| 241 | } |
| 242 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 243 | // getLogicalDeviceWithoutLock retrieves a logical device from the model without locking it. This is used only by |
| 244 | // functions that have already acquired the logical device lock to the model |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 245 | func (agent *LogicalDeviceAgent) getLogicalDeviceWithoutLock() (*voltha.LogicalDevice, error) { |
| 246 | log.Debug("getLogicalDeviceWithoutLock") |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 247 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 248 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 249 | //log.Debug("getLogicalDeviceWithoutLock", log.Fields{"ldevice": lDevice}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 250 | return lDevice, nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 251 | } |
| 252 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 253 | } |
| 254 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 255 | func (agent *LogicalDeviceAgent) updateLogicalPort(device *voltha.Device, port *voltha.Port) error { |
| 256 | log.Debugw("updateLogicalPort", log.Fields{"deviceId": device.Id, "port": port}) |
| 257 | var err error |
| 258 | if port.Type == voltha.Port_ETHERNET_NNI { |
| 259 | if _, err = agent.addNNILogicalPort(device, port); err != nil { |
| 260 | return err |
| 261 | } |
| 262 | agent.addLogicalPortToMap(port.PortNo, true) |
| 263 | } else if port.Type == voltha.Port_ETHERNET_UNI { |
| 264 | if _, err = agent.addUNILogicalPort(device, port); err != nil { |
| 265 | return err |
| 266 | } |
| 267 | agent.addLogicalPortToMap(port.PortNo, false) |
| 268 | } else { |
| 269 | // Update the device graph to ensure all routes on the logical device have been calculated |
| 270 | if err = agent.updateRoutes(device, port); err != nil { |
| 271 | log.Errorw("failed-to-update-routes", log.Fields{"deviceId": device.Id, "port": port, "error": err}) |
| 272 | return err |
| 273 | } |
| 274 | } |
| 275 | return nil |
| 276 | } |
| 277 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 278 | func (agent *LogicalDeviceAgent) addLogicalPort(device *voltha.Device, port *voltha.Port) error { |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 279 | log.Debugw("addLogicalPort", log.Fields{"deviceId": device.Id, "port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 280 | var err error |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 281 | if port.Type == voltha.Port_ETHERNET_NNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 282 | if _, err = agent.addNNILogicalPort(device, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 283 | return err |
| 284 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 285 | agent.addLogicalPortToMap(port.PortNo, true) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 286 | } else if port.Type == voltha.Port_ETHERNET_UNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 287 | if _, err = agent.addUNILogicalPort(device, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 288 | return err |
| 289 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 290 | agent.addLogicalPortToMap(port.PortNo, false) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 291 | } else { |
| 292 | log.Debugw("invalid-port-type", log.Fields{"deviceId": device.Id, "port": port}) |
| 293 | return nil |
| 294 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 295 | return nil |
| 296 | } |
| 297 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 298 | // setupLogicalPorts is invoked once the logical device has been created and is ready to get ports |
| 299 | // added to it. While the logical device was being created we could have received requests to add |
| 300 | // NNI and UNI ports which were discarded. Now is the time to add them if needed |
| 301 | func (agent *LogicalDeviceAgent) setupLogicalPorts(ctx context.Context) error { |
| 302 | log.Infow("setupLogicalPorts", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 303 | // First add any NNI ports which could have been missing |
| 304 | if err := agent.setupNNILogicalPorts(nil, agent.rootDeviceId); err != nil { |
| 305 | log.Errorw("error-setting-up-NNI-ports", log.Fields{"error": err, "deviceId": agent.rootDeviceId}) |
| 306 | return err |
| 307 | } |
| 308 | |
| 309 | // Now, set up the UNI ports if needed. |
| 310 | if children, err := agent.deviceMgr.getAllChildDevices(agent.rootDeviceId); err != nil { |
| 311 | log.Errorw("error-getting-child-devices", log.Fields{"error": err, "deviceId": agent.rootDeviceId}) |
| 312 | return err |
| 313 | } else { |
| 314 | chnlsList := make([]chan interface{}, 0) |
| 315 | for _, child := range children.Items { |
| 316 | ch := make(chan interface{}) |
| 317 | chnlsList = append(chnlsList, ch) |
| 318 | go func(device *voltha.Device, ch chan interface{}) { |
| 319 | if err = agent.setupUNILogicalPorts(nil, device); err != nil { |
| 320 | log.Error("setting-up-UNI-ports-failed", log.Fields{"deviceID": device.Id}) |
| 321 | ch <- status.Errorf(codes.Internal, "UNI-ports-setup-failed: %s", device.Id) |
| 322 | } |
| 323 | ch <- nil |
| 324 | }(child, ch) |
| 325 | } |
| 326 | // Wait for completion |
| 327 | if res := fu.WaitForNilOrErrorResponses(agent.defaultTimeout, chnlsList...); res != nil { |
| 328 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 329 | } |
| 330 | } |
| 331 | return nil |
| 332 | } |
| 333 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 334 | // setupNNILogicalPorts creates an NNI port on the logical device that represents an NNI interface on a root device |
| 335 | func (agent *LogicalDeviceAgent) setupNNILogicalPorts(ctx context.Context, deviceId string) error { |
| 336 | log.Infow("setupNNILogicalPorts-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 337 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 338 | var err error |
| 339 | |
| 340 | var device *voltha.Device |
| 341 | if device, err = agent.deviceMgr.GetDevice(deviceId); err != nil { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 342 | log.Errorw("error-retrieving-device", log.Fields{"error": err, "deviceId": deviceId}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 343 | return err |
| 344 | } |
| 345 | |
| 346 | //Get UNI port number |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 347 | for _, port := range device.Ports { |
| 348 | if port.Type == voltha.Port_ETHERNET_NNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 349 | if _, err = agent.addNNILogicalPort(device, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 350 | log.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 351 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 352 | agent.addLogicalPortToMap(port.PortNo, true) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 353 | } |
| 354 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 355 | return err |
| 356 | } |
| 357 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 358 | // updatePortsState updates the ports state related to the device |
| 359 | func (agent *LogicalDeviceAgent) updatePortsState(device *voltha.Device, state voltha.AdminState_AdminState) error { |
| 360 | log.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 361 | agent.lockLogicalDevice.Lock() |
| 362 | defer agent.lockLogicalDevice.Unlock() |
| 363 | // Get the latest logical device info |
| 364 | if ld, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
| 365 | log.Warnw("logical-device-unknown", log.Fields{"ldeviceId": agent.logicalDeviceId, "error": err}) |
| 366 | return err |
| 367 | } else { |
| 368 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 369 | for _, lport := range cloned.Ports { |
| 370 | if lport.DeviceId == device.Id { |
| 371 | switch state { |
| 372 | case voltha.AdminState_ENABLED: |
| 373 | lport.OfpPort.Config = lport.OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 374 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 375 | case voltha.AdminState_DISABLED: |
| 376 | lport.OfpPort.Config = lport.OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 377 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 378 | default: |
| 379 | log.Warnw("unsupported-state-change", log.Fields{"deviceId": device.Id, "state": state}) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | // Updating the logical device will trigger the poprt change events to be populated to the controller |
| 384 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 385 | log.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceId, "error": err}) |
| 386 | return err |
| 387 | } |
| 388 | } |
| 389 | return nil |
| 390 | } |
| 391 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 392 | // setupUNILogicalPorts creates a UNI port on the logical device that represents a child UNI interface |
| 393 | func (agent *LogicalDeviceAgent) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device) error { |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 394 | log.Infow("setupUNILogicalPort", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 395 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 396 | var err error |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 397 | |
| 398 | //Get UNI port number |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 399 | for _, port := range childDevice.Ports { |
| 400 | if port.Type == voltha.Port_ETHERNET_UNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 401 | if _, err = agent.addUNILogicalPort(childDevice, port); err != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 402 | log.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 403 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 404 | agent.addLogicalPortToMap(port.PortNo, false) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 407 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 408 | } |
| 409 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 410 | // deleteAllLogicalPorts deletes all logical ports associated with this device |
| 411 | func (agent *LogicalDeviceAgent) deleteAllLogicalPorts(device *voltha.Device) error { |
| 412 | log.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 413 | agent.lockLogicalDevice.Lock() |
| 414 | defer agent.lockLogicalDevice.Unlock() |
| 415 | // Get the latest logical device info |
| 416 | if ld, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
| 417 | log.Warnw("logical-device-unknown", log.Fields{"ldeviceId": agent.logicalDeviceId, "error": err}) |
| 418 | return err |
| 419 | } else { |
| 420 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 421 | updateLogicalPorts := []*voltha.LogicalPort{} |
| 422 | for _, lport := range cloned.Ports { |
| 423 | if lport.DeviceId != device.Id { |
| 424 | updateLogicalPorts = append(updateLogicalPorts, lport) |
| 425 | } |
| 426 | } |
| 427 | if len(updateLogicalPorts) < len(cloned.Ports) { |
| 428 | cloned.Ports = updateLogicalPorts |
| 429 | // Updating the logical device will trigger the poprt change events to be populated to the controller |
| 430 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 431 | log.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceId, "error": err}) |
| 432 | return err |
| 433 | } |
| 434 | } else { |
| 435 | log.Debugw("no-change-required", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 436 | } |
| 437 | } |
| 438 | return nil |
| 439 | } |
| 440 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 441 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 442 | func (agent *LogicalDeviceAgent) updateLogicalDeviceWithoutLock(logicalDevice *voltha.LogicalDevice) error { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 443 | afterUpdate := agent.clusterDataProxy.Update("/logical_devices/"+agent.logicalDeviceId, logicalDevice, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 444 | if afterUpdate == nil { |
| 445 | return status.Errorf(codes.Internal, "failed-updating-logical-device:%s", agent.logicalDeviceId) |
| 446 | } |
| 447 | return nil |
| 448 | } |
| 449 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 450 | //updateFlowTable updates the flow table of that logical device |
| 451 | func (agent *LogicalDeviceAgent) updateFlowTable(ctx context.Context, flow *ofp.OfpFlowMod) error { |
| 452 | log.Debug("updateFlowTable") |
| 453 | if flow == nil { |
| 454 | return nil |
| 455 | } |
| 456 | switch flow.GetCommand() { |
| 457 | case ofp.OfpFlowModCommand_OFPFC_ADD: |
| 458 | return agent.flowAdd(flow) |
| 459 | case ofp.OfpFlowModCommand_OFPFC_DELETE: |
| 460 | return agent.flowDelete(flow) |
| 461 | case ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT: |
| 462 | return agent.flowDeleteStrict(flow) |
| 463 | case ofp.OfpFlowModCommand_OFPFC_MODIFY: |
| 464 | return agent.flowModify(flow) |
| 465 | case ofp.OfpFlowModCommand_OFPFC_MODIFY_STRICT: |
| 466 | return agent.flowModifyStrict(flow) |
| 467 | } |
| 468 | return status.Errorf(codes.Internal, |
| 469 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceId, flow.GetCommand()) |
| 470 | } |
| 471 | |
| 472 | //updateGroupTable updates the group table of that logical device |
| 473 | func (agent *LogicalDeviceAgent) updateGroupTable(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
| 474 | log.Debug("updateGroupTable") |
| 475 | if groupMod == nil { |
| 476 | return nil |
| 477 | } |
| 478 | switch groupMod.GetCommand() { |
| 479 | case ofp.OfpGroupModCommand_OFPGC_ADD: |
| 480 | return agent.groupAdd(groupMod) |
| 481 | case ofp.OfpGroupModCommand_OFPGC_DELETE: |
| 482 | return agent.groupDelete(groupMod) |
| 483 | case ofp.OfpGroupModCommand_OFPGC_MODIFY: |
| 484 | return agent.groupModify(groupMod) |
| 485 | } |
| 486 | return status.Errorf(codes.Internal, |
| 487 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceId, groupMod.GetCommand()) |
| 488 | } |
| 489 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 490 | //flowAdd adds a flow to the flow table of that logical device |
| 491 | func (agent *LogicalDeviceAgent) flowAdd(mod *ofp.OfpFlowMod) error { |
| 492 | log.Debug("flowAdd") |
| 493 | if mod == nil { |
| 494 | return nil |
| 495 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 496 | agent.lockLogicalDevice.Lock() |
| 497 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 498 | |
| 499 | var lDevice *voltha.LogicalDevice |
| 500 | var err error |
| 501 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 502 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 503 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 504 | } |
| 505 | |
| 506 | var flows []*ofp.OfpFlowStats |
| 507 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 508 | flows = lDevice.Flows.Items |
| 509 | } |
| 510 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 511 | updatedFlows := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 512 | //oldData := proto.Clone(lDevice.Flows).(*voltha.Flows) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 513 | changed := false |
| 514 | checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0 |
| 515 | if checkOverlap { |
| 516 | if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 { |
| 517 | // TODO: should this error be notified other than being logged? |
| 518 | log.Warnw("overlapped-flows", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 519 | } else { |
| 520 | // Add flow |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 521 | flow := fu.FlowStatsEntryFromFlowModMessage(mod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 522 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 523 | updatedFlows = append(updatedFlows, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 524 | changed = true |
| 525 | } |
| 526 | } else { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 527 | flow := fu.FlowStatsEntryFromFlowModMessage(mod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 528 | idx := fu.FindFlows(flows, flow) |
| 529 | if idx >= 0 { |
| 530 | oldFlow := flows[idx] |
| 531 | if (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_RESET_COUNTS)) != 0 { |
| 532 | flow.ByteCount = oldFlow.ByteCount |
| 533 | flow.PacketCount = oldFlow.PacketCount |
| 534 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 535 | if !reflect.DeepEqual(oldFlow, flow) { |
| 536 | flows[idx] = flow |
| 537 | updatedFlows = append(updatedFlows, flow) |
| 538 | changed = true |
| 539 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 540 | } else { |
| 541 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 542 | updatedFlows = append(updatedFlows, flow) |
| 543 | changed = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 544 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 545 | } |
| 546 | if changed { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 547 | // Launch a routine to decompose the flows |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame^] | 548 | if err := agent.decomposeAndSendFlows(&ofp.Flows{Items: updatedFlows}, lDevice.FlowGroups); err != nil { |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 549 | log.Errorw("decomposing-and-sending-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 550 | return err |
| 551 | } |
| 552 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 553 | // Update model |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 554 | flowsToUpdate := &ofp.Flows{} |
| 555 | if lDevice.Flows != nil { |
| 556 | flowsToUpdate = &ofp.Flows{Items: flows} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 557 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 558 | if err := agent.updateLogicalDeviceFlowsWithoutLock(flowsToUpdate); err != nil { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 559 | log.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 560 | return err |
| 561 | } |
| 562 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 563 | return nil |
| 564 | } |
| 565 | |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame^] | 566 | func (agent *LogicalDeviceAgent) decomposeAndSendFlows(flows *ofp.Flows, groups *ofp.FlowGroups) error { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 567 | log.Debugw("decomposeAndSendFlows", log.Fields{"logicalDeviceID": agent.logicalDeviceId}) |
| 568 | |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame^] | 569 | deviceRules := agent.flowDecomposer.DecomposeRules(agent, *flows, *groups) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 570 | log.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
| 571 | |
| 572 | chnlsList := make([]chan interface{}, 0) |
| 573 | for deviceId, value := range deviceRules.GetRules() { |
| 574 | ch := make(chan interface{}) |
| 575 | chnlsList = append(chnlsList, ch) |
| 576 | go func(deviceId string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry) { |
| 577 | if err := agent.deviceMgr.addFlowsAndGroups(deviceId, flows, groups); err != nil { |
| 578 | log.Error("flow-update-failed", log.Fields{"deviceID": deviceId}) |
| 579 | ch <- status.Errorf(codes.Internal, "flow-update-failed: %s", deviceId) |
| 580 | } |
| 581 | ch <- nil |
| 582 | }(deviceId, value.ListFlows(), value.ListGroups()) |
| 583 | } |
| 584 | // Wait for completion |
| 585 | if res := fu.WaitForNilOrErrorResponses(agent.defaultTimeout, chnlsList...); res != nil { |
| 586 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 587 | } |
| 588 | return nil |
| 589 | } |
| 590 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 591 | //flowDelete deletes a flow from the flow table of that logical device |
| 592 | func (agent *LogicalDeviceAgent) flowDelete(mod *ofp.OfpFlowMod) error { |
| 593 | log.Debug("flowDelete") |
| 594 | if mod == nil { |
| 595 | return nil |
| 596 | } |
| 597 | agent.lockLogicalDevice.Lock() |
| 598 | defer agent.lockLogicalDevice.Unlock() |
| 599 | |
| 600 | var lDevice *voltha.LogicalDevice |
| 601 | var err error |
| 602 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 603 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 604 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 605 | } |
| 606 | flows := lDevice.Flows.Items |
| 607 | |
| 608 | //build a list of what to keep vs what to delete |
| 609 | toKeep := make([]*ofp.OfpFlowStats, 0) |
| 610 | for _, f := range flows { |
| 611 | if !fu.FlowMatchesMod(f, mod) { |
| 612 | toKeep = append(toKeep, f) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | //Update flows |
| 617 | if len(toKeep) < len(flows) { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 618 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: toKeep}); err != nil { |
| 619 | log.Errorw("Cannot-update-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 620 | return err |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | //TODO: send announcement on delete |
| 625 | return nil |
| 626 | } |
| 627 | |
| 628 | //flowStatsDelete deletes a flow from the flow table of that logical device |
| 629 | func (agent *LogicalDeviceAgent) flowStatsDelete(flow *ofp.OfpFlowStats) error { |
| 630 | log.Debug("flowStatsDelete") |
| 631 | if flow == nil { |
| 632 | return nil |
| 633 | } |
| 634 | agent.lockLogicalDevice.Lock() |
| 635 | defer agent.lockLogicalDevice.Unlock() |
| 636 | |
| 637 | var lDevice *voltha.LogicalDevice |
| 638 | var err error |
| 639 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 640 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 641 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 642 | } |
| 643 | flows := lDevice.Flows.Items |
| 644 | |
| 645 | //build a list of what to keep vs what to delete |
| 646 | toKeep := make([]*ofp.OfpFlowStats, 0) |
| 647 | for _, f := range flows { |
| 648 | if !fu.FlowMatch(f, flow) { |
| 649 | toKeep = append(toKeep, f) |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | //Update flows |
| 654 | if len(toKeep) < len(flows) { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 655 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: toKeep}); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 656 | log.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 657 | return err |
| 658 | } |
| 659 | } |
| 660 | return nil |
| 661 | } |
| 662 | |
| 663 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
| 664 | func (agent *LogicalDeviceAgent) flowDeleteStrict(mod *ofp.OfpFlowMod) error { |
| 665 | log.Debug("flowDeleteStrict") |
| 666 | if mod == 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 { |
| 675 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 676 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 677 | } |
| 678 | flows := lDevice.Flows.Items |
| 679 | changed := false |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 680 | flow := fu.FlowStatsEntryFromFlowModMessage(mod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 681 | idx := fu.FindFlows(flows, flow) |
| 682 | if idx >= 0 { |
| 683 | flows = append(flows[:idx], flows[idx+1:]...) |
| 684 | changed = true |
| 685 | } else { |
| 686 | return errors.New(fmt.Sprintf("Cannot delete flow - %s", flow)) |
| 687 | } |
| 688 | |
| 689 | if changed { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 690 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: flows}); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 691 | log.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 692 | return err |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return nil |
| 697 | } |
| 698 | |
| 699 | //flowModify modifies a flow from the flow table of that logical device |
| 700 | func (agent *LogicalDeviceAgent) flowModify(mod *ofp.OfpFlowMod) error { |
| 701 | return errors.New("flowModify not implemented") |
| 702 | } |
| 703 | |
| 704 | //flowModifyStrict deletes a flow from the flow table of that logical device |
| 705 | func (agent *LogicalDeviceAgent) flowModifyStrict(mod *ofp.OfpFlowMod) error { |
| 706 | return errors.New("flowModifyStrict not implemented") |
| 707 | } |
| 708 | |
| 709 | func (agent *LogicalDeviceAgent) groupAdd(groupMod *ofp.OfpGroupMod) error { |
| 710 | log.Debug("groupAdd") |
| 711 | if groupMod == nil { |
| 712 | return nil |
| 713 | } |
| 714 | agent.lockLogicalDevice.Lock() |
| 715 | defer agent.lockLogicalDevice.Unlock() |
| 716 | |
| 717 | var lDevice *voltha.LogicalDevice |
| 718 | var err error |
| 719 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 720 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 721 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 722 | } |
| 723 | groups := lDevice.FlowGroups.Items |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 724 | if fu.FindGroup(groups, groupMod.GroupId) == -1 { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 725 | groups = append(groups, fu.GroupEntryFromGroupMod(groupMod)) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 726 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
| 727 | log.Errorw("Cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 728 | return err |
| 729 | } |
| 730 | } else { |
| 731 | return errors.New(fmt.Sprintf("Groups %d already present", groupMod.GroupId)) |
| 732 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 733 | return nil |
| 734 | } |
| 735 | |
| 736 | func (agent *LogicalDeviceAgent) groupDelete(groupMod *ofp.OfpGroupMod) error { |
| 737 | log.Debug("groupDelete") |
| 738 | if groupMod == nil { |
| 739 | return nil |
| 740 | } |
| 741 | agent.lockLogicalDevice.Lock() |
| 742 | defer agent.lockLogicalDevice.Unlock() |
| 743 | |
| 744 | var lDevice *voltha.LogicalDevice |
| 745 | var err error |
| 746 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 747 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 748 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 749 | } |
| 750 | groups := lDevice.FlowGroups.Items |
| 751 | flows := lDevice.Flows.Items |
| 752 | groupsChanged := false |
| 753 | flowsChanged := false |
| 754 | groupId := groupMod.GroupId |
| 755 | if groupId == uint32(ofp.OfpGroup_OFPG_ALL) { |
| 756 | //TODO we must delete all flows that point to this group and |
| 757 | //signal controller as requested by flow's flag |
| 758 | groups = []*ofp.OfpGroupEntry{} |
| 759 | groupsChanged = true |
| 760 | } else { |
| 761 | if idx := fu.FindGroup(groups, groupId); idx == -1 { |
| 762 | return nil // Valid case |
| 763 | } else { |
| 764 | flowsChanged, flows = fu.FlowsDeleteByGroupId(flows, groupId) |
| 765 | groups = append(groups[:idx], groups[idx+1:]...) |
| 766 | groupsChanged = true |
| 767 | } |
| 768 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 769 | if groupsChanged { |
| 770 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
| 771 | log.Errorw("Cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 772 | return err |
| 773 | } |
| 774 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 775 | if flowsChanged { |
| 776 | if err := agent.updateLogicalDeviceFlowsWithoutLock(&ofp.Flows{Items: flows}); err != nil { |
| 777 | log.Errorw("Cannot-update-flow", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 778 | return err |
| 779 | } |
| 780 | } |
| 781 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 782 | return nil |
| 783 | } |
| 784 | |
| 785 | func (agent *LogicalDeviceAgent) groupModify(groupMod *ofp.OfpGroupMod) error { |
| 786 | log.Debug("groupModify") |
| 787 | if groupMod == nil { |
| 788 | return nil |
| 789 | } |
| 790 | agent.lockLogicalDevice.Lock() |
| 791 | defer agent.lockLogicalDevice.Unlock() |
| 792 | |
| 793 | var lDevice *voltha.LogicalDevice |
| 794 | var err error |
| 795 | if lDevice, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 796 | log.Errorw("no-logical-device-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 797 | return errors.New(fmt.Sprintf("no-logical-device-present:%s", agent.logicalDeviceId)) |
| 798 | } |
| 799 | groups := lDevice.FlowGroups.Items |
| 800 | groupsChanged := false |
| 801 | groupId := groupMod.GroupId |
| 802 | if idx := fu.FindGroup(groups, groupId); idx == -1 { |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 803 | return errors.New(fmt.Sprintf("group-absent:%d", groupId)) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 804 | } else { |
| 805 | //replace existing group entry with new group definition |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 806 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 807 | groups[idx] = groupEntry |
| 808 | groupsChanged = true |
| 809 | } |
| 810 | if groupsChanged { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 811 | //lDevice.FlowGroups.Items = groups |
| 812 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(&ofp.FlowGroups{Items: groups}); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 813 | log.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 814 | return err |
| 815 | } |
| 816 | } |
| 817 | return nil |
| 818 | } |
| 819 | |
| 820 | // deleteLogicalPort removes the logical port |
| 821 | func (agent *LogicalDeviceAgent) deleteLogicalPort(lPort *voltha.LogicalPort) error { |
| 822 | agent.lockLogicalDevice.Lock() |
| 823 | defer agent.lockLogicalDevice.Unlock() |
| 824 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 825 | // Get the most up to date logical device |
| 826 | var logicaldevice *voltha.LogicalDevice |
| 827 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 828 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "logicalPortId": lPort.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 829 | return nil |
| 830 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 831 | index := -1 |
| 832 | for i, logicalPort := range logicaldevice.Ports { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 833 | if logicalPort.Id == lPort.Id { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 834 | index = i |
| 835 | break |
| 836 | } |
| 837 | } |
| 838 | if index >= 0 { |
| 839 | copy(logicaldevice.Ports[index:], logicaldevice.Ports[index+1:]) |
| 840 | logicaldevice.Ports[len(logicaldevice.Ports)-1] = nil |
| 841 | logicaldevice.Ports = logicaldevice.Ports[:len(logicaldevice.Ports)-1] |
| 842 | log.Debugw("logical-port-deleted", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 843 | if err := agent.updateLogicalDeviceWithoutLock(logicaldevice); err != nil { |
| 844 | log.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 845 | return err |
| 846 | } |
| 847 | // Reset the logical device graph |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 848 | go agent.generateDeviceGraph() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 849 | } |
| 850 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 851 | } |
| 852 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 853 | // deleteLogicalPorts removes the logical ports associated with that deviceId |
| 854 | func (agent *LogicalDeviceAgent) deleteLogicalPorts(deviceId string) error { |
| 855 | agent.lockLogicalDevice.Lock() |
| 856 | defer agent.lockLogicalDevice.Unlock() |
| 857 | |
| 858 | // Get the most up to date logical device |
| 859 | var logicaldevice *voltha.LogicalDevice |
| 860 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
| 861 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 862 | return nil |
| 863 | } |
| 864 | updatedLPorts := []*voltha.LogicalPort{} |
| 865 | for _, logicalPort := range logicaldevice.Ports { |
| 866 | if logicalPort.DeviceId != deviceId { |
| 867 | updatedLPorts = append(updatedLPorts, logicalPort) |
| 868 | } |
| 869 | } |
| 870 | logicaldevice.Ports = updatedLPorts |
| 871 | log.Debugw("updated-logical-ports", log.Fields{"ports": updatedLPorts}) |
| 872 | if err := agent.updateLogicalDeviceWithoutLock(logicaldevice); err != nil { |
| 873 | log.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 874 | return err |
| 875 | } |
| 876 | // Reset the logical device graph |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 877 | go agent.generateDeviceGraph() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 878 | |
| 879 | return nil |
| 880 | } |
| 881 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 882 | // enableLogicalPort enables the logical port |
| 883 | func (agent *LogicalDeviceAgent) enableLogicalPort(lPort *voltha.LogicalPort) error { |
| 884 | agent.lockLogicalDevice.Lock() |
| 885 | defer agent.lockLogicalDevice.Unlock() |
| 886 | |
| 887 | // Get the most up to date logical device |
| 888 | var logicaldevice *voltha.LogicalDevice |
| 889 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
| 890 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "logicalPortId": lPort.Id}) |
| 891 | return nil |
| 892 | } |
| 893 | index := -1 |
| 894 | for i, logicalPort := range logicaldevice.Ports { |
| 895 | if logicalPort.Id == lPort.Id { |
| 896 | index = i |
| 897 | break |
| 898 | } |
| 899 | } |
| 900 | if index >= 0 { |
| 901 | logicaldevice.Ports[index].OfpPort.Config = logicaldevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 902 | return agent.updateLogicalDeviceWithoutLock(logicaldevice) |
| 903 | } |
| 904 | //TODO: Trigger subsequent actions on the device |
| 905 | return nil |
| 906 | } |
| 907 | |
| 908 | // disableLogicalPort disabled the logical port |
| 909 | func (agent *LogicalDeviceAgent) disableLogicalPort(lPort *voltha.LogicalPort) error { |
| 910 | agent.lockLogicalDevice.Lock() |
| 911 | defer agent.lockLogicalDevice.Unlock() |
| 912 | |
| 913 | // Get the most up to date logical device |
| 914 | var logicaldevice *voltha.LogicalDevice |
| 915 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
| 916 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "logicalPortId": lPort.Id}) |
| 917 | return nil |
| 918 | } |
| 919 | index := -1 |
| 920 | for i, logicalPort := range logicaldevice.Ports { |
| 921 | if logicalPort.Id == lPort.Id { |
| 922 | index = i |
| 923 | break |
| 924 | } |
| 925 | } |
| 926 | if index >= 0 { |
| 927 | logicaldevice.Ports[index].OfpPort.Config = (logicaldevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN)) | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 928 | return agent.updateLogicalDeviceWithoutLock(logicaldevice) |
| 929 | } |
| 930 | //TODO: Trigger subsequent actions on the device |
| 931 | return nil |
| 932 | } |
| 933 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 934 | func (agent *LogicalDeviceAgent) getPreCalculatedRoute(ingress, egress uint32) []graph.RouteHop { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 935 | log.Debugw("ROUTE", log.Fields{"len": len(agent.deviceGraph.Routes)}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 936 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 937 | log.Debugw("ROUTELINKS", log.Fields{"ingress": ingress, "egress": egress, "routelink": routeLink}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 938 | if ingress == routeLink.Ingress && egress == routeLink.Egress { |
| 939 | return route |
| 940 | } |
| 941 | } |
| 942 | log.Warnw("no-route", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "ingress": ingress, "egress": egress}) |
| 943 | return nil |
| 944 | } |
| 945 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 946 | func (agent *LogicalDeviceAgent) GetRoute(ingressPortNo uint32, egressPortNo uint32) []graph.RouteHop { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 947 | log.Debugw("getting-route", log.Fields{"ingress-port": ingressPortNo, "egress-port": egressPortNo}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 948 | routes := make([]graph.RouteHop, 0) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 949 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 950 | // Note: A port value of 0 is equivalent to a nil port |
| 951 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 952 | // Consider different possibilities |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 953 | if egressPortNo != 0 && ((egressPortNo & 0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER)) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 954 | log.Debugw("controller-flow", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
| 955 | if agent.isNNIPort(ingressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 956 | log.Debug("returning-half-route") |
| 957 | //This is a trap on the NNI Port |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 958 | if len(agent.deviceGraph.Routes) == 0 { |
| 959 | // If there are no routes set (usually when the logical device has only NNI port(s), then just return an |
| 960 | // internal route |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 961 | hop := graph.RouteHop{DeviceID: agent.rootDeviceId, Ingress: ingressPortNo, Egress: egressPortNo} |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 962 | routes = append(routes, hop) |
| 963 | routes = append(routes, hop) |
| 964 | return routes |
| 965 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 966 | //Return a 'half' route to make the flow decomposer logic happy |
| 967 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 968 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 969 | routes = append(routes, graph.RouteHop{}) // first hop is set to empty |
| 970 | routes = append(routes, route[1]) |
| 971 | return routes |
| 972 | } |
| 973 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 974 | 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] | 975 | return nil |
| 976 | } |
| 977 | //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] | 978 | var err error |
| 979 | if egressPortNo, err = agent.getFirstNNIPort(); err != nil { |
| 980 | log.Warnw("no-nni-port", log.Fields{"error": err}) |
| 981 | return nil |
| 982 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 983 | } |
| 984 | //If ingress port is not specified (nil), it may be a wildcarded |
| 985 | //route if egress port is OFPP_CONTROLLER or a nni logical port, |
| 986 | //in which case we need to create a half-route where only the egress |
| 987 | //hop is filled, the first hop is nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 988 | if ingressPortNo == 0 && agent.isNNIPort(egressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 989 | // We can use the 2nd hop of any upstream route, so just find the first upstream: |
| 990 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 991 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 992 | routes = append(routes, graph.RouteHop{}) // first hop is set to empty |
| 993 | routes = append(routes, route[1]) |
| 994 | return routes |
| 995 | } |
| 996 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 997 | 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] | 998 | return nil |
| 999 | } |
| 1000 | //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] | 1001 | if egressPortNo == 0 { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1002 | for routeLink, route := range agent.deviceGraph.Routes { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1003 | if routeLink.Ingress == ingressPortNo { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1004 | routes = append(routes, route[0]) |
| 1005 | routes = append(routes, graph.RouteHop{}) |
| 1006 | return routes |
| 1007 | } |
| 1008 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1009 | 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] | 1010 | return nil |
| 1011 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1012 | // Return the pre-calculated route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1013 | return agent.getPreCalculatedRoute(ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1016 | //GetWildcardInputPorts filters out the logical port number from the set of logical ports on the device and |
| 1017 | //returns their port numbers. This function is invoked only during flow decomposition where the lock on the logical |
| 1018 | //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] | 1019 | func (agent *LogicalDeviceAgent) GetWildcardInputPorts(excludePort ...uint32) []uint32 { |
| 1020 | lPorts := make([]uint32, 0) |
| 1021 | var exclPort uint32 |
| 1022 | if len(excludePort) == 1 { |
| 1023 | exclPort = excludePort[0] |
| 1024 | } |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1025 | if lDevice, _ := agent.getLogicalDeviceWithoutLock(); lDevice != nil { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1026 | for _, port := range lDevice.Ports { |
| 1027 | if port.OfpPort.PortNo != exclPort { |
| 1028 | lPorts = append(lPorts, port.OfpPort.PortNo) |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | return lPorts |
| 1033 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1034 | |
| 1035 | func (agent *LogicalDeviceAgent) GetDeviceGraph() *graph.DeviceGraph { |
| 1036 | return agent.deviceGraph |
| 1037 | } |
| 1038 | |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame^] | 1039 | //updateRoutes rebuilds the device graph if not done already |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1040 | func (agent *LogicalDeviceAgent) updateRoutes(device *voltha.Device, port *voltha.Port) error { |
| 1041 | log.Debugf("updateRoutes", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "device": device.Id, "port": port}) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1042 | agent.lockLogicalDevice.Lock() |
| 1043 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1044 | if agent.deviceGraph == nil { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1045 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceId, agent.deviceMgr.GetDevice) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1046 | } |
| 1047 | // Get all the logical ports on that logical device |
| 1048 | if lDevice, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 1049 | log.Errorw("unknown-logical-device", log.Fields{"error": err, "logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1050 | return err |
| 1051 | } else { |
| 1052 | //TODO: Find a better way to refresh only missing routes |
| 1053 | agent.deviceGraph.ComputeRoutes(lDevice.Ports) |
| 1054 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1055 | agent.deviceGraph.Print() |
| 1056 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1059 | //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] | 1060 | func (agent *LogicalDeviceAgent) updateDeviceGraph(lp *voltha.LogicalPort) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1061 | log.Debugf("updateDeviceGraph", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 1062 | agent.lockLogicalDevice.Lock() |
| 1063 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1064 | if agent.deviceGraph == nil { |
| 1065 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceId, agent.deviceMgr.GetDevice) |
| 1066 | } |
| 1067 | agent.deviceGraph.AddPort(lp) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1068 | agent.deviceGraph.Print() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1069 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1070 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1071 | //generateDeviceGraph regenerates the device graph |
| 1072 | func (agent *LogicalDeviceAgent) generateDeviceGraph() { |
| 1073 | log.Debugf("generateDeviceGraph", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1074 | agent.lockLogicalDevice.Lock() |
| 1075 | defer agent.lockLogicalDevice.Unlock() |
| 1076 | // Get the latest logical device |
| 1077 | if ld, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
| 1078 | log.Errorw("logical-device-not-present", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "error": err}) |
| 1079 | } else { |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1080 | log.Debugw("generating-graph", log.Fields{"lDeviceId": agent.logicalDeviceId, "deviceGraph": agent.deviceGraph, "lPorts": len(ld.Ports)}) |
| 1081 | if agent.deviceGraph == nil { |
| 1082 | agent.deviceGraph = graph.NewDeviceGraph(agent.logicalDeviceId, agent.deviceMgr.GetDevice) |
| 1083 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1084 | agent.deviceGraph.ComputeRoutes(ld.Ports) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1085 | agent.deviceGraph.Print() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1089 | // portAdded is a callback invoked when a port is added to the logical device. |
| 1090 | // TODO: To use when POST_ADD is fixed. |
| 1091 | func (agent *LogicalDeviceAgent) portAdded(args ...interface{}) interface{} { |
| 1092 | log.Debugw("portAdded-callback", log.Fields{"argsLen": len(args)}) |
| 1093 | |
| 1094 | var port *voltha.LogicalPort |
| 1095 | |
| 1096 | // Sanity check |
| 1097 | if args[0] != nil { |
| 1098 | log.Warnw("previous-data-not-nil", log.Fields{"args0": args[0]}) |
| 1099 | } |
| 1100 | var ok bool |
| 1101 | if port, ok = args[1].(*voltha.LogicalPort); !ok { |
| 1102 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 1103 | return nil |
| 1104 | } |
| 1105 | |
| 1106 | // Set the proxy and callback for that port |
| 1107 | agent.portProxiesLock.Lock() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 1108 | agent.portProxies[port.Id] = agent.clusterDataProxy.CreateProxy( |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1109 | fmt.Sprintf("/logical_devices/%s/ports/%s", agent.logicalDeviceId, port.Id), |
| 1110 | false) |
| 1111 | agent.portProxies[port.Id].RegisterCallback(model.POST_UPDATE, agent.portUpdated) |
| 1112 | agent.portProxiesLock.Unlock() |
| 1113 | |
| 1114 | // Send the port change event to the OF controller |
| 1115 | agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceId, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1116 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_ADD, Desc: port.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1117 | |
| 1118 | return nil |
| 1119 | } |
| 1120 | |
| 1121 | // portRemoved is a callback invoked when a port is removed from the logical device. |
| 1122 | // TODO: To use when POST_ADD is fixed. |
| 1123 | func (agent *LogicalDeviceAgent) portRemoved(args ...interface{}) interface{} { |
| 1124 | log.Debugw("portRemoved-callback", log.Fields{"argsLen": len(args)}) |
| 1125 | |
| 1126 | var port *voltha.LogicalPort |
| 1127 | |
| 1128 | // Sanity check |
| 1129 | if args[1] != nil { |
| 1130 | log.Warnw("data-not-nil", log.Fields{"args1": args[1]}) |
| 1131 | } |
| 1132 | var ok bool |
| 1133 | if port, ok = args[0].(*voltha.LogicalPort); !ok { |
| 1134 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 1135 | return nil |
| 1136 | } |
| 1137 | |
| 1138 | // Remove the proxy and callback for that port |
| 1139 | agent.portProxiesLock.Lock() |
| 1140 | agent.portProxies[port.Id].UnregisterCallback(model.POST_UPDATE, agent.portUpdated) |
| 1141 | delete(agent.portProxies, port.Id) |
| 1142 | agent.portProxiesLock.Unlock() |
| 1143 | |
| 1144 | // Send the port change event to the OF controller |
| 1145 | agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceId, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1146 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_DELETE, Desc: port.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1147 | |
| 1148 | return nil |
| 1149 | } |
| 1150 | |
| 1151 | // 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] | 1152 | func diff(oldList, newList []*voltha.LogicalPort) (newPorts, changedPorts, deletedPorts []*voltha.LogicalPort) { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1153 | newPorts = make([]*voltha.LogicalPort, 0) |
| 1154 | changedPorts = make([]*voltha.LogicalPort, 0) |
| 1155 | deletedPorts = make([]*voltha.LogicalPort, 0) |
| 1156 | for _, o := range oldList { |
| 1157 | found := false |
| 1158 | changed := false |
| 1159 | for _, n := range newList { |
| 1160 | if o.Id == n.Id { |
| 1161 | changed = !reflect.DeepEqual(o, n) |
| 1162 | found = true |
| 1163 | break |
| 1164 | } |
| 1165 | } |
| 1166 | if !found { |
| 1167 | deletedPorts = append(deletedPorts, o) |
| 1168 | } |
| 1169 | if changed { |
| 1170 | changedPorts = append(changedPorts, o) |
| 1171 | } |
| 1172 | } |
| 1173 | for _, n := range newList { |
| 1174 | found := false |
| 1175 | for _, o := range oldList { |
| 1176 | if o.Id == n.Id { |
| 1177 | found = true |
| 1178 | break |
| 1179 | } |
| 1180 | } |
| 1181 | if !found { |
| 1182 | newPorts = append(newPorts, n) |
| 1183 | } |
| 1184 | } |
| 1185 | return |
| 1186 | } |
| 1187 | |
| 1188 | // portUpdated is invoked when a port is updated on the logical device. Until |
| 1189 | // the POST_ADD notification is fixed, we will use the logical device to |
| 1190 | // update that data. |
| 1191 | func (agent *LogicalDeviceAgent) portUpdated(args ...interface{}) interface{} { |
| 1192 | log.Debugw("portUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 1193 | |
| 1194 | var oldLD *voltha.LogicalDevice |
| 1195 | var newlD *voltha.LogicalDevice |
| 1196 | |
| 1197 | var ok bool |
| 1198 | if oldLD, ok = args[0].(*voltha.LogicalDevice); !ok { |
| 1199 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 1200 | return nil |
| 1201 | } |
| 1202 | if newlD, ok = args[1].(*voltha.LogicalDevice); !ok { |
| 1203 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 1204 | return nil |
| 1205 | } |
| 1206 | |
| 1207 | if reflect.DeepEqual(oldLD.Ports, newlD.Ports) { |
| 1208 | log.Debug("ports-have-not-changed") |
| 1209 | return nil |
| 1210 | } |
| 1211 | |
| 1212 | // Get the difference between the two list |
| 1213 | newPorts, changedPorts, deletedPorts := diff(oldLD.Ports, newlD.Ports) |
| 1214 | |
| 1215 | // Send the port change events to the OF controller |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1216 | for _, newP := range newPorts { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1217 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceId, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1218 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_ADD, Desc: newP.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1219 | } |
| 1220 | for _, change := range changedPorts { |
| 1221 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceId, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1222 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_MODIFY, Desc: change.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1223 | } |
| 1224 | for _, del := range deletedPorts { |
| 1225 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceId, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1226 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_DELETE, Desc: del.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | return nil |
| 1230 | } |
| 1231 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1232 | // addNNILogicalPort adds an NNI port to the logical device. It returns a bool representing whether a port has been |
| 1233 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1234 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1235 | // scenario. This also applies to the case where the port was already added. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1236 | func (agent *LogicalDeviceAgent) addNNILogicalPort(device *voltha.Device, port *voltha.Port) (bool, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1237 | log.Debugw("addNNILogicalPort", log.Fields{"NNI": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1238 | if device.AdminState != voltha.AdminState_ENABLED || device.OperStatus != voltha.OperStatus_ACTIVE { |
| 1239 | log.Infow("device-not-ready", log.Fields{"deviceId": device.Id, "admin": device.AdminState, "oper": device.OperStatus}) |
| 1240 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1241 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1242 | agent.lockLogicalDevice.RLock() |
| 1243 | if agent.portExist(device, port) { |
| 1244 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
| 1245 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1246 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1247 | } |
| 1248 | agent.lockLogicalDevice.RUnlock() |
| 1249 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1250 | var portCap *ic.PortCapability |
| 1251 | var err error |
| 1252 | // First get the port capability |
| 1253 | if portCap, err = agent.deviceMgr.getPortCapability(nil, device.Id, port.PortNo); err != nil { |
| 1254 | log.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1255 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1256 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1257 | |
| 1258 | agent.lockLogicalDevice.Lock() |
| 1259 | defer agent.lockLogicalDevice.Unlock() |
| 1260 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1261 | if agent.portExist(device, port) { |
| 1262 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1263 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1264 | } |
| 1265 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1266 | portCap.Port.RootPort = true |
| 1267 | lp := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
| 1268 | lp.DeviceId = device.Id |
| 1269 | lp.Id = fmt.Sprintf("nni-%d", port.PortNo) |
| 1270 | lp.OfpPort.PortNo = port.PortNo |
| 1271 | lp.OfpPort.Name = lp.Id |
| 1272 | lp.DevicePortNo = port.PortNo |
| 1273 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1274 | var ld *voltha.LogicalDevice |
| 1275 | if ld, err = agent.getLogicalDeviceWithoutLock(); err != nil { |
| 1276 | log.Errorw("error-retrieving-logical-device", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1277 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1278 | } |
| 1279 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 1280 | if cloned.Ports == nil { |
| 1281 | cloned.Ports = make([]*voltha.LogicalPort, 0) |
| 1282 | } |
| 1283 | cloned.Ports = append(cloned.Ports, lp) |
| 1284 | |
| 1285 | if err = agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 1286 | log.Errorw("error-updating-logical-device", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1287 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1288 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1289 | |
| 1290 | // Update the device graph with this new logical port |
| 1291 | clonedLP := (proto.Clone(lp)).(*voltha.LogicalPort) |
| 1292 | go agent.updateDeviceGraph(clonedLP) |
| 1293 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1294 | return true, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1297 | func (agent *LogicalDeviceAgent) portExist(device *voltha.Device, port *voltha.Port) bool { |
khenaidoo | 54544ae | 2019-03-18 13:22:39 -0400 | [diff] [blame] | 1298 | if ldevice, _ := agent.getLogicalDeviceWithoutLock(); ldevice != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1299 | for _, lPort := range ldevice.Ports { |
khenaidoo | 54544ae | 2019-03-18 13:22:39 -0400 | [diff] [blame] | 1300 | if lPort.DeviceId == device.Id && lPort.DevicePortNo == port.PortNo && lPort.Id == port.Label { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1301 | return true |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | return false |
| 1306 | } |
| 1307 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1308 | // addUNILogicalPort adds an UNI port to the logical device. It returns a bool representing whether a port has been |
| 1309 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1310 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1311 | // scenario. This also applies to the case where the port was already added. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1312 | func (agent *LogicalDeviceAgent) addUNILogicalPort(childDevice *voltha.Device, port *voltha.Port) (bool, error) { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1313 | log.Debugw("addUNILogicalPort", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1314 | if childDevice.AdminState != voltha.AdminState_ENABLED || childDevice.OperStatus != voltha.OperStatus_ACTIVE { |
| 1315 | log.Infow("device-not-ready", log.Fields{"deviceId": childDevice.Id, "admin": childDevice.AdminState, "oper": childDevice.OperStatus}) |
| 1316 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1317 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1318 | agent.lockLogicalDevice.RLock() |
| 1319 | if agent.portExist(childDevice, port) { |
| 1320 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
| 1321 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1322 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1323 | } |
| 1324 | agent.lockLogicalDevice.RUnlock() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1325 | var portCap *ic.PortCapability |
| 1326 | var err error |
| 1327 | // First get the port capability |
| 1328 | if portCap, err = agent.deviceMgr.getPortCapability(nil, childDevice.Id, port.PortNo); err != nil { |
| 1329 | log.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1330 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1331 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1332 | agent.lockLogicalDevice.Lock() |
| 1333 | defer agent.lockLogicalDevice.Unlock() |
| 1334 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1335 | if agent.portExist(childDevice, port) { |
| 1336 | log.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1337 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1338 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1339 | // Get stored logical device |
| 1340 | if ldevice, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1341 | return false, status.Error(codes.NotFound, agent.logicalDeviceId) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1342 | } else { |
| 1343 | log.Debugw("adding-uni", log.Fields{"deviceId": childDevice.Id}) |
| 1344 | portCap.Port.RootPort = false |
Matt Jeanneret | 3815e32 | 2019-03-12 19:15:49 -0400 | [diff] [blame] | 1345 | portCap.Port.Id = port.Label |
| 1346 | portCap.Port.OfpPort.PortNo = port.PortNo |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1347 | portCap.Port.DeviceId = childDevice.Id |
| 1348 | portCap.Port.DevicePortNo = port.PortNo |
| 1349 | cloned := (proto.Clone(ldevice)).(*voltha.LogicalDevice) |
| 1350 | if cloned.Ports == nil { |
| 1351 | cloned.Ports = make([]*voltha.LogicalPort, 0) |
| 1352 | } |
| 1353 | cloned.Ports = append(cloned.Ports, portCap.Port) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1354 | if err := agent.updateLogicalDeviceWithoutLock(cloned); err != nil { |
| 1355 | return false, err |
| 1356 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1357 | // Update the device graph with this new logical port |
| 1358 | clonedLP := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
| 1359 | go agent.updateDeviceGraph(clonedLP) |
| 1360 | return true, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1364 | func (agent *LogicalDeviceAgent) packetOut(packet *ofp.OfpPacketOut) { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1365 | log.Debugw("packet-out", log.Fields{"packet": packet.GetInPort()}) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1366 | outPort := fu.GetPacketOutPort(packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1367 | //frame := packet.GetData() |
| 1368 | //TODO: Use a channel between the logical agent and the device agent |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 1369 | if err := agent.deviceMgr.packetOut(agent.rootDeviceId, outPort, packet); err != nil { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1370 | log.Error("packetout-failed", log.Fields{"logicalDeviceID": agent.rootDeviceId}) |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 1371 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1372 | } |
| 1373 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1374 | func (agent *LogicalDeviceAgent) packetIn(port uint32, transactionId string, packet []byte) { |
| 1375 | log.Debugw("packet-in", log.Fields{"port": port, "packet": packet, "transactionId": transactionId}) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1376 | packetIn := fu.MkPacketIn(port, packet) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1377 | agent.ldeviceMgr.grpcNbiHdlr.sendPacketIn(agent.logicalDeviceId, transactionId, packetIn) |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 1378 | log.Debugw("sending-packet-in", log.Fields{"packet-in": packetIn}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1379 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1380 | |
| 1381 | func (agent *LogicalDeviceAgent) addLogicalPortToMap(portNo uint32, nniPort bool) { |
| 1382 | agent.lockLogicalPortsNo.Lock() |
| 1383 | defer agent.lockLogicalPortsNo.Unlock() |
| 1384 | if exist := agent.logicalPortsNo[portNo]; !exist { |
| 1385 | agent.logicalPortsNo[portNo] = nniPort |
| 1386 | } |
| 1387 | } |
| 1388 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1389 | func (agent *LogicalDeviceAgent) addLogicalPortsToMap(lps []*voltha.LogicalPort) { |
| 1390 | agent.lockLogicalPortsNo.Lock() |
| 1391 | defer agent.lockLogicalPortsNo.Unlock() |
| 1392 | for _, lp := range lps { |
| 1393 | if exist := agent.logicalPortsNo[lp.DevicePortNo]; !exist { |
| 1394 | agent.logicalPortsNo[lp.DevicePortNo] = lp.RootPort |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1399 | func (agent *LogicalDeviceAgent) deleteLogicalPortFromMap(portNo uint32) { |
| 1400 | agent.lockLogicalPortsNo.Lock() |
| 1401 | defer agent.lockLogicalPortsNo.Unlock() |
| 1402 | if exist := agent.logicalPortsNo[portNo]; exist { |
| 1403 | delete(agent.logicalPortsNo, portNo) |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | func (agent *LogicalDeviceAgent) isNNIPort(portNo uint32) bool { |
| 1408 | agent.lockLogicalPortsNo.RLock() |
| 1409 | defer agent.lockLogicalPortsNo.RUnlock() |
| 1410 | if exist := agent.logicalPortsNo[portNo]; exist { |
| 1411 | return agent.logicalPortsNo[portNo] |
| 1412 | } |
| 1413 | return false |
| 1414 | } |
| 1415 | |
| 1416 | func (agent *LogicalDeviceAgent) getFirstNNIPort() (uint32, error) { |
| 1417 | agent.lockLogicalPortsNo.RLock() |
| 1418 | defer agent.lockLogicalPortsNo.RUnlock() |
| 1419 | for portNo, nni := range agent.logicalPortsNo { |
| 1420 | if nni { |
| 1421 | return portNo, nil |
| 1422 | } |
| 1423 | } |
| 1424 | return 0, status.Error(codes.NotFound, "No NNI port found") |
| 1425 | } |