Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -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 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 22 | "sync" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 23 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 24 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
| 25 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 27 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 28 | "github.com/opencord/voltha-protos/v3/go/voltha" |
| 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
| 31 | ) |
| 32 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 33 | // listLogicalDevicePorts returns logical device ports |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 34 | func (agent *LogicalAgent) listLogicalDevicePorts(ctx context.Context) map[uint32]*voltha.LogicalPort { |
| 35 | logger.Debug(ctx, "listLogicalDevicePorts") |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 36 | portIDs := agent.portLoader.ListIDs() |
| 37 | ret := make(map[uint32]*voltha.LogicalPort, len(portIDs)) |
| 38 | for portID := range portIDs { |
| 39 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 40 | ret[portID] = portHandle.GetReadOnly() |
| 41 | portHandle.Unlock() |
| 42 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 43 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 44 | return ret |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 47 | func (agent *LogicalAgent) updateLogicalPort(ctx context.Context, device *voltha.Device, devicePorts map[uint32]*voltha.Port, port *voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 48 | logger.Debugw(ctx, "updateLogicalPort", log.Fields{"device-id": device.Id, "port": port}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 49 | switch port.Type { |
| 50 | case voltha.Port_ETHERNET_NNI: |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 51 | if err := agent.addNNILogicalPort(ctx, device.Id, devicePorts, port); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 52 | return err |
| 53 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 54 | case voltha.Port_ETHERNET_UNI: |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 55 | if err := agent.addUNILogicalPort(ctx, device.Id, device.AdminState, device.OperStatus, devicePorts, port); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 56 | return err |
| 57 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 58 | case voltha.Port_PON_OLT: |
| 59 | // Rebuilt the routes on Parent PON port addition |
| 60 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 61 | if err := agent.buildRoutes(log.WithSpanFromContext(context.Background(), ctx)); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 62 | // Not an error - temporary state |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 63 | logger.Infow(ctx, "failed-to-update-routes-after-adding-parent-pon-port", log.Fields{"device-id": device.Id, "port": port, "ports-count": len(devicePorts), "error": err}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 64 | } |
| 65 | }() |
| 66 | //fallthrough |
| 67 | case voltha.Port_PON_ONU: |
| 68 | // Add the routes corresponding to that child device |
| 69 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 70 | if err := agent.updateAllRoutes(log.WithSpanFromContext(context.Background(), ctx), device.Id, devicePorts); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 71 | // Not an error - temporary state |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 72 | logger.Infow(ctx, "failed-to-update-routes-after-adding-child-pon-port", log.Fields{"device-id": device.Id, "port": port, "ports-count": len(devicePorts), "error": err}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 73 | } |
| 74 | }() |
| 75 | default: |
| 76 | return fmt.Errorf("invalid port type %v", port) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // setupLogicalPorts is invoked once the logical device has been created and is ready to get ports |
| 82 | // added to it. While the logical device was being created we could have received requests to add |
| 83 | // NNI and UNI ports which were discarded. Now is the time to add them if needed |
| 84 | func (agent *LogicalAgent) setupLogicalPorts(ctx context.Context) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 85 | logger.Infow(ctx, "setupLogicalPorts", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 86 | // First add any NNI ports which could have been missing |
| 87 | if err := agent.setupNNILogicalPorts(ctx, agent.rootDeviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 88 | logger.Errorw(ctx, "error-setting-up-NNI-ports", log.Fields{"error": err, "device-id": agent.rootDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 89 | return err |
| 90 | } |
| 91 | |
| 92 | // Now, set up the UNI ports if needed. |
| 93 | children, err := agent.deviceMgr.GetAllChildDevices(ctx, agent.rootDeviceID) |
| 94 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 95 | logger.Errorw(ctx, "error-getting-child-devices", log.Fields{"error": err, "device-id": agent.rootDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 96 | return err |
| 97 | } |
| 98 | responses := make([]coreutils.Response, 0) |
| 99 | for _, child := range children.Items { |
| 100 | response := coreutils.NewResponse() |
| 101 | responses = append(responses, response) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 102 | go func(ctx context.Context, child *voltha.Device) { |
| 103 | defer response.Done() |
| 104 | |
| 105 | childPorts, err := agent.deviceMgr.listDevicePorts(ctx, child.Id) |
| 106 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 107 | logger.Error(ctx, "setting-up-UNI-ports-failed", log.Fields{"device-id": child.Id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 108 | response.Error(status.Errorf(codes.Internal, "UNI-ports-setup-failed: %s", child.Id)) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | if err = agent.setupUNILogicalPorts(ctx, child, childPorts); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 113 | logger.Error(ctx, "setting-up-UNI-ports-failed", log.Fields{"device-id": child.Id}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 114 | response.Error(status.Errorf(codes.Internal, "UNI-ports-setup-failed: %s", child.Id)) |
| 115 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 116 | }(log.WithSpanFromContext(context.Background(), ctx), child) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 117 | } |
| 118 | // Wait for completion |
| 119 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
| 120 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | // setupNNILogicalPorts creates an NNI port on the logical device that represents an NNI interface on a root device |
| 126 | func (agent *LogicalAgent) setupNNILogicalPorts(ctx context.Context, deviceID string) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 127 | logger.Infow(ctx, "setupNNILogicalPorts-start", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 128 | // Build the logical device based on information retrieved from the device adapter |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 129 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 130 | devicePorts, err := agent.deviceMgr.listDevicePorts(ctx, deviceID) |
| 131 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 132 | logger.Errorw(ctx, "error-retrieving-device-ports", log.Fields{"error": err, "device-id": deviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 133 | return err |
| 134 | } |
| 135 | |
| 136 | //Get UNI port number |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 137 | for _, port := range devicePorts { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 138 | if port.Type == voltha.Port_ETHERNET_NNI { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 139 | if err = agent.addNNILogicalPort(ctx, deviceID, devicePorts, port); err != nil { |
| 140 | logger.Errorw(ctx, "error-adding-NNI-port", log.Fields{"error": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 141 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | return err |
| 145 | } |
| 146 | |
| 147 | // updatePortState updates the port state of the device |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 148 | func (agent *LogicalAgent) updatePortState(ctx context.Context, portNo uint32, operStatus voltha.OperStatus_Types) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 149 | logger.Infow(ctx, "updatePortState-start", log.Fields{"logical-device-id": agent.logicalDeviceID, "portNo": portNo, "state": operStatus}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 150 | |
| 151 | portHandle, have := agent.portLoader.Lock(portNo) |
| 152 | if !have { |
| 153 | return status.Errorf(codes.NotFound, "port-%d-not-exist", portNo) |
| 154 | } |
| 155 | defer portHandle.Unlock() |
| 156 | |
| 157 | newPort := clonePortSetState(portHandle.GetReadOnly(), operStatus) |
| 158 | if err := portHandle.Update(ctx, newPort); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 159 | return err |
| 160 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 161 | agent.orderedEvents.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_MODIFY, newPort.OfpPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 162 | return nil |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 165 | func clonePortSetState(oldPort *voltha.LogicalPort, state voltha.OperStatus_Types) *voltha.LogicalPort { |
| 166 | newPort := *oldPort // only clone the struct(s) that will be changed |
| 167 | newOfpPort := *oldPort.OfpPort |
| 168 | newPort.OfpPort = &newOfpPort |
| 169 | |
| 170 | if state == voltha.OperStatus_ACTIVE { |
| 171 | newOfpPort.Config = newOfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 172 | newOfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
| 173 | } else { |
| 174 | newOfpPort.Config = newOfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 175 | newOfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 176 | } |
| 177 | return &newPort |
| 178 | } |
| 179 | |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 180 | // setupUNILogicalPorts creates a UNI port on the logical device that represents a child UNI interface |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 181 | func (agent *LogicalAgent) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device, childDevicePorts map[uint32]*voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 182 | logger.Infow(ctx, "setupUNILogicalPorts", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 183 | // Build the logical device based on information retrieved from the device adapter |
| 184 | var err error |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 185 | //Get UNI port number |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 186 | for _, port := range childDevicePorts { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 187 | if port.Type == voltha.Port_ETHERNET_UNI { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 188 | if err = agent.addUNILogicalPort(ctx, childDevice.Id, childDevice.AdminState, childDevice.OperStatus, childDevicePorts, port); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 189 | logger.Errorw(ctx, "error-adding-UNI-port", log.Fields{"error": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 190 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | return err |
| 194 | } |
| 195 | |
| 196 | // deleteAllLogicalPorts deletes all logical ports associated with this logical device |
| 197 | func (agent *LogicalAgent) deleteAllLogicalPorts(ctx context.Context) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 198 | logger.Infow(ctx, "updatePortsState-start", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 199 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 200 | // for each port |
| 201 | for portID := range agent.portLoader.ListIDs() { |
| 202 | // TODO: can just call agent.deleteLogicalPort()? |
| 203 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 204 | oldPort := portHandle.GetReadOnly() |
| 205 | // delete |
| 206 | err := portHandle.Delete(ctx) |
| 207 | portHandle.Unlock() |
| 208 | if err != nil { |
| 209 | return err |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 210 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 211 | // and send event |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 212 | agent.orderedEvents.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_DELETE, oldPort.OfpPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 213 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 214 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 215 | |
| 216 | // Reset the logical device routes |
| 217 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 218 | if err := agent.buildRoutes(log.WithSpanFromContext(context.Background(), ctx)); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 219 | logger.Warnw(ctx, "device-routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 220 | } |
| 221 | }() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 222 | return nil |
| 223 | } |
| 224 | |
| 225 | // deleteLogicalPorts removes the logical ports associated with that deviceId |
| 226 | func (agent *LogicalAgent) deleteLogicalPorts(ctx context.Context, deviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 227 | logger.Debugw(ctx, "deleting-logical-ports", log.Fields{"device-id": deviceID}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 228 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 229 | // for each port |
| 230 | for portNo := range agent.portLoader.ListIDsForDevice(deviceID) { |
| 231 | if portHandle, have := agent.portLoader.Lock(portNo); have { |
| 232 | // if belongs to this device |
| 233 | if oldPort := portHandle.GetReadOnly(); oldPort.DeviceId == deviceID { |
| 234 | // delete |
| 235 | if err := portHandle.Delete(ctx); err != nil { |
| 236 | portHandle.Unlock() |
| 237 | return err |
| 238 | } |
| 239 | // and send event |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 240 | agent.orderedEvents.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_DELETE, oldPort.OfpPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 241 | } |
| 242 | portHandle.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 243 | } |
| 244 | } |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 245 | |
| 246 | // Reset the logical device routes |
| 247 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 248 | if err := agent.buildRoutes(log.WithSpanFromContext(context.Background(), ctx)); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 249 | logger.Warnw(ctx, "routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err}) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 250 | } |
| 251 | }() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 252 | return nil |
| 253 | } |
| 254 | |
| 255 | // enableLogicalPort enables the logical port |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 256 | func (agent *LogicalAgent) enableLogicalPort(ctx context.Context, lPortNo uint32) error { |
| 257 | portHandle, have := agent.portLoader.Lock(lPortNo) |
| 258 | if !have { |
| 259 | return status.Errorf(codes.NotFound, "port-%d-not-exist", lPortNo) |
| 260 | } |
| 261 | defer portHandle.Unlock() |
| 262 | |
| 263 | oldPort := portHandle.GetReadOnly() |
| 264 | |
| 265 | newPort := *oldPort // only clone the struct(s) that will be changed |
| 266 | newOfpPort := *oldPort.OfpPort |
| 267 | newPort.OfpPort = &newOfpPort |
| 268 | |
| 269 | newOfpPort.Config = newOfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 270 | if err := portHandle.Update(ctx, &newPort); err != nil { |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 271 | return err |
| 272 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 273 | agent.orderedEvents.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_MODIFY, newPort.OfpPort) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 274 | return nil |
| 275 | } |
| 276 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 277 | // disableLogicalPort disabled the logical port |
| 278 | func (agent *LogicalAgent) disableLogicalPort(ctx context.Context, lPortNo uint32) error { |
| 279 | portHandle, have := agent.portLoader.Lock(lPortNo) |
| 280 | if !have { |
| 281 | return status.Errorf(codes.NotFound, "port-%d-not-exist", lPortNo) |
| 282 | } |
| 283 | defer portHandle.Unlock() |
| 284 | |
| 285 | oldPort := portHandle.GetReadOnly() |
| 286 | |
| 287 | newPort := *oldPort // only clone the struct(s) that will be changed |
| 288 | newOfpPort := *oldPort.OfpPort |
| 289 | newPort.OfpPort = &newOfpPort |
| 290 | |
| 291 | newOfpPort.Config = (newOfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN)) | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 292 | if err := portHandle.Update(ctx, &newPort); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 293 | return err |
| 294 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 295 | agent.orderedEvents.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_MODIFY, newPort.OfpPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 296 | return nil |
| 297 | } |
| 298 | |
| 299 | // addNNILogicalPort adds an NNI port to the logical device. It returns a bool representing whether a port has been |
| 300 | // added and an error in case a valid error is encountered. If the port was successfully added it will return |
| 301 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 302 | // scenario. This also applies to the case where the port was already added. |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 303 | func (agent *LogicalAgent) addNNILogicalPort(ctx context.Context, deviceID string, devicePorts map[uint32]*voltha.Port, port *voltha.Port) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 304 | logger.Debugw(ctx, "addNNILogicalPort", log.Fields{"logical-device-id": agent.logicalDeviceID, "nni-port": port}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 305 | |
| 306 | label := fmt.Sprintf("nni-%d", port.PortNo) |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 307 | ofpPort := *port.OfpPort |
| 308 | ofpPort.HwAddr = append([]uint32{}, port.OfpPort.HwAddr...) |
| 309 | ofpPort.PortNo = port.PortNo |
| 310 | ofpPort.Name = label |
| 311 | nniPort := &voltha.LogicalPort{ |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 312 | RootPort: true, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 313 | DeviceId: deviceID, |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 314 | Id: label, |
| 315 | DevicePortNo: port.PortNo, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 316 | OfpPort: &ofpPort, |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 317 | OfpPortStats: &ofp.OfpPortStats{}, |
| 318 | } |
| 319 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 320 | portHandle, created, err := agent.portLoader.LockOrCreate(ctx, nniPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 321 | if err != nil { |
| 322 | return err |
| 323 | } |
| 324 | defer portHandle.Unlock() |
| 325 | |
| 326 | if !created { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 327 | logger.Debugw(ctx, "port-already-exist", log.Fields{"port": port}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 328 | return nil |
| 329 | } |
| 330 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 331 | // ensure that no events will be sent until this one is |
| 332 | queuePosition := agent.orderedEvents.assignQueuePosition() |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 333 | |
| 334 | // Setup the routes for this device and then send the port update event to the OF Controller |
| 335 | go func() { |
| 336 | // First setup the routes |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 337 | if err := agent.updateRoutes(log.WithSpanFromContext(context.Background(), ctx), deviceID, devicePorts, nniPort, agent.listLogicalDevicePorts(ctx)); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 338 | // This is not an error as we may not have enough logical ports to set up routes or some PON ports have not been |
| 339 | // created yet. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 340 | logger.Infow(ctx, "routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "logical-port": nniPort.OfpPort.PortNo, "error": err}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 343 | // send event, and allow any queued events to be sent as well |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 344 | queuePosition.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_ADD, nniPort.OfpPort) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 345 | }() |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 346 | return nil |
| 347 | } |
| 348 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 349 | // addUNILogicalPort adds an UNI port to the logical device. It returns a bool representing whether a port has been |
| 350 | // added and an error in case a valid error is encountered. If the port was successfully added it will return |
| 351 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 352 | // scenario. This also applies to the case where the port was already added. |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 353 | func (agent *LogicalAgent) addUNILogicalPort(ctx context.Context, deviceID string, deviceAdminState voltha.AdminState_Types, deviceOperStatus voltha.OperStatus_Types, devicePorts map[uint32]*voltha.Port, port *voltha.Port) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 354 | logger.Debugw(ctx, "addUNILogicalPort", log.Fields{"port": port}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 355 | if deviceAdminState != voltha.AdminState_ENABLED || deviceOperStatus != voltha.OperStatus_ACTIVE { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 356 | logger.Infow(ctx, "device-not-ready", log.Fields{"device-id": deviceID, "admin": deviceAdminState, "oper": deviceOperStatus}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 357 | return nil |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 358 | } |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 359 | ofpPort := *port.OfpPort |
| 360 | ofpPort.HwAddr = append([]uint32{}, port.OfpPort.HwAddr...) |
| 361 | ofpPort.PortNo = port.PortNo |
| 362 | uniPort := &voltha.LogicalPort{ |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 363 | RootPort: false, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 364 | DeviceId: deviceID, |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 365 | Id: port.Label, |
| 366 | DevicePortNo: port.PortNo, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 367 | OfpPort: &ofpPort, |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 368 | OfpPortStats: &ofp.OfpPortStats{}, |
| 369 | } |
| 370 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 371 | portHandle, created, err := agent.portLoader.LockOrCreate(ctx, uniPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 372 | if err != nil { |
| 373 | return err |
| 374 | } |
| 375 | defer portHandle.Unlock() |
| 376 | |
| 377 | if !created { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 378 | logger.Debugw(ctx, "port-already-exist", log.Fields{"port": port}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 379 | return nil |
| 380 | } |
| 381 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 382 | // ensure that no events will be sent until this one is |
| 383 | queuePosition := agent.orderedEvents.assignQueuePosition() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 384 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 385 | // Setup the routes for this device and then send the port update event to the OF Controller |
| 386 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 387 | ctx = log.WithSpanFromContext(context.Background(), ctx) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 388 | // First setup the routes |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 389 | if err := agent.updateRoutes(ctx, deviceID, devicePorts, uniPort, agent.listLogicalDevicePorts(ctx)); err != nil { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 390 | // This is not an error as we may not have enough logical ports to set up routes or some PON ports have not been |
| 391 | // created yet. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 392 | logger.Infow(ctx, "routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "logical-port": uniPort.OfpPort.PortNo, "error": err}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 393 | } |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 394 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 395 | // send event, and allow any queued events to be sent as well |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 396 | queuePosition.send(ctx, agent, agent.logicalDeviceID, ofp.OfpPortReason_OFPPR_ADD, uniPort.OfpPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 397 | }() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 398 | return nil |
| 399 | } |
| 400 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 401 | // send is a convenience to avoid calling both assignQueuePosition and qp.send |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 402 | func (e *orderedEvents) send(ctx context.Context, agent *LogicalAgent, deviceID string, reason ofp.OfpPortReason, desc *ofp.OfpPort) { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 403 | qp := e.assignQueuePosition() |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 404 | go qp.send(log.WithSpanFromContext(context.Background(), ctx), agent, deviceID, reason, desc) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 407 | // TODO: shouldn't need to guarantee event ordering like this |
| 408 | // event ordering should really be protected by per-LogicalPort lock |
| 409 | // once routing uses on-demand calculation only, this should be changed |
| 410 | // assignQueuePosition ensures that no events will be sent until this thread calls send() on the returned queuePosition |
| 411 | func (e *orderedEvents) assignQueuePosition() queuePosition { |
| 412 | e.mutex.Lock() |
| 413 | defer e.mutex.Unlock() |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 414 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 415 | prev := e.last |
| 416 | next := make(chan struct{}) |
| 417 | e.last = next |
| 418 | return queuePosition{ |
| 419 | prev: prev, |
| 420 | next: next, |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 424 | // orderedEvents guarantees the order that events are sent, while allowing events to back up. |
| 425 | type orderedEvents struct { |
| 426 | mutex sync.Mutex |
| 427 | last <-chan struct{} |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 430 | type queuePosition struct { |
| 431 | prev <-chan struct{} |
| 432 | next chan<- struct{} |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 435 | // send waits for its turn, then sends the event, then notifies the next in line |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 436 | func (qp queuePosition) send(ctx context.Context, agent *LogicalAgent, deviceID string, reason ofp.OfpPortReason, desc *ofp.OfpPort) { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 437 | if qp.prev != nil { |
| 438 | <-qp.prev // wait for turn |
| 439 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 440 | agent.ldeviceMgr.SendChangeEvent(ctx, deviceID, reason, desc) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 441 | close(qp.next) // notify next |
| 442 | } |
| 443 | |
| 444 | // GetWildcardInputPorts filters out the logical port number from the set of logical ports on the device and |
| 445 | // returns their port numbers. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 446 | func (agent *LogicalAgent) GetWildcardInputPorts(ctx context.Context, excludePort uint32) map[uint32]struct{} { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 447 | portIDs := agent.portLoader.ListIDs() |
| 448 | delete(portIDs, excludePort) |
| 449 | return portIDs |
| 450 | } |
| 451 | |
| 452 | // isNNIPort return true iff the specified port belongs to the parent (OLT) device |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 453 | func (agent *LogicalAgent) isNNIPort(portNo uint32) bool { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 454 | portHandle, have := agent.portLoader.Lock(portNo) |
| 455 | if !have { |
| 456 | return false |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 457 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 458 | defer portHandle.Unlock() |
| 459 | |
| 460 | // any root-device logical port is an NNI port |
| 461 | return portHandle.GetReadOnly().RootPort |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 462 | } |
| 463 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 464 | // getAnyNNIPort returns an NNI port |
| 465 | func (agent *LogicalAgent) getAnyNNIPort() (uint32, error) { |
| 466 | for portID := range agent.portLoader.ListIDsForDevice(agent.rootDeviceID) { |
| 467 | return portID, nil |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 468 | } |
| 469 | return 0, status.Error(codes.NotFound, "No NNI port found") |
| 470 | } |
| 471 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 472 | //GetNNIPorts returns all NNI ports |
| 473 | func (agent *LogicalAgent) GetNNIPorts() map[uint32]struct{} { |
| 474 | return agent.portLoader.ListIDsForDevice(agent.rootDeviceID) |
Kent Hagerman | 3136fbd | 2020-05-14 10:30:45 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // getUNILogicalPortNo returns the UNI logical port number specified in the flow |
| 478 | func (agent *LogicalAgent) getUNILogicalPortNo(flow *ofp.OfpFlowStats) (uint32, error) { |
| 479 | var uniPort uint32 |
| 480 | inPortNo := fu.GetInPort(flow) |
| 481 | outPortNo := fu.GetOutPort(flow) |
| 482 | if agent.isNNIPort(inPortNo) { |
| 483 | uniPort = outPortNo |
| 484 | } else if agent.isNNIPort(outPortNo) { |
| 485 | uniPort = inPortNo |
| 486 | } |
| 487 | if uniPort != 0 { |
| 488 | return uniPort, nil |
| 489 | } |
| 490 | return 0, status.Errorf(codes.NotFound, "no-uni-port: %v", flow) |
| 491 | } |