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