khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame] | 2 | * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 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 route |
| 18 | |
| 19 | import ( |
| 20 | "context" |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 21 | "errors" |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 22 | "fmt" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 23 | "sync" |
| 24 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 31 | var ErrNoRoute = errors.New("no route") |
| 32 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 33 | // Hop represent a route hop |
| 34 | type Hop struct { |
| 35 | DeviceID string |
| 36 | Ingress uint32 |
| 37 | Egress uint32 |
| 38 | } |
| 39 | |
| 40 | // PathID is the identification of a route between two logical ports |
| 41 | type PathID struct { |
| 42 | Ingress uint32 |
| 43 | Egress uint32 |
| 44 | } |
| 45 | |
| 46 | type OFPortLink struct { |
| 47 | Ingress uint32 |
| 48 | Egress uint32 |
| 49 | } |
| 50 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 51 | // listDevicePortsFunc returns device ports |
| 52 | type listDevicePortsFunc func(ctx context.Context, id string) (map[uint32]*voltha.Port, error) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 53 | |
| 54 | // DeviceRoutes represent the set of routes between logical ports of a logical device |
| 55 | type DeviceRoutes struct { |
| 56 | logicalDeviceID string |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 57 | rootDeviceID string |
| 58 | listDevicePorts listDevicePortsFunc |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 59 | logicalPorts map[uint32]*voltha.LogicalPort |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 60 | RootPorts map[uint32]uint32 |
| 61 | rootPortsLock sync.RWMutex |
| 62 | Routes map[PathID][]Hop |
| 63 | routeBuildLock sync.RWMutex |
| 64 | devicesPonPorts map[string][]*voltha.Port |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 65 | childConnectionPort map[string]uint32 |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // NewDeviceRoutes creates device graph instance |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 69 | func NewDeviceRoutes(logicalDeviceID, rootDeviceID string, deviceMgr listDevicePortsFunc) *DeviceRoutes { |
| 70 | return &DeviceRoutes{ |
| 71 | logicalDeviceID: logicalDeviceID, |
| 72 | rootDeviceID: rootDeviceID, |
| 73 | listDevicePorts: deviceMgr, |
| 74 | RootPorts: make(map[uint32]uint32), |
| 75 | Routes: make(map[PathID][]Hop), |
| 76 | devicesPonPorts: make(map[string][]*voltha.Port), |
| 77 | childConnectionPort: make(map[string]uint32), |
| 78 | logicalPorts: make(map[uint32]*voltha.LogicalPort), |
| 79 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | //IsRootPort returns true if the port is a root port on a logical device |
| 83 | func (dr *DeviceRoutes) IsRootPort(port uint32) bool { |
| 84 | dr.rootPortsLock.RLock() |
| 85 | defer dr.rootPortsLock.RUnlock() |
| 86 | _, exist := dr.RootPorts[port] |
| 87 | return exist |
| 88 | } |
| 89 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 90 | func (dr *DeviceRoutes) GetRoute(ctx context.Context, ingress, egress uint32) ([]Hop, error) { |
| 91 | dr.routeBuildLock.Lock() |
| 92 | defer dr.routeBuildLock.Unlock() |
| 93 | |
| 94 | if route, exist := dr.Routes[PathID{Ingress: ingress, Egress: egress}]; exist { |
| 95 | return route, nil |
| 96 | } |
| 97 | |
| 98 | uniPort, nniPort, err := dr.getLogicalPorts(ingress, egress) |
| 99 | if err != nil { |
Andrea Campanella | 832cff6 | 2021-11-05 17:05:18 +0100 | [diff] [blame] | 100 | return nil, fmt.Errorf("no route from:%d to:%d for %s %w", ingress, egress, err.Error(), ErrNoRoute) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | childPonPort, err := dr.getChildPonPort(ctx, uniPort.DeviceId) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 107 | rootDevicePonPort, err := dr.getParentPonPort(ctx, uniPort.DeviceId) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | dr.Routes[PathID{Ingress: nniPort.OfpPort.PortNo, Egress: uniPort.DevicePortNo}] = []Hop{ |
| 113 | {DeviceID: nniPort.DeviceId, Ingress: nniPort.DevicePortNo, Egress: rootDevicePonPort}, |
| 114 | {DeviceID: uniPort.DeviceId, Ingress: childPonPort, Egress: uniPort.DevicePortNo}, |
| 115 | } |
| 116 | dr.Routes[PathID{Ingress: uniPort.DevicePortNo, Egress: nniPort.OfpPort.PortNo}] = getReverseRoute( |
| 117 | dr.Routes[PathID{Ingress: nniPort.OfpPort.PortNo, Egress: uniPort.DevicePortNo}]) |
| 118 | |
| 119 | return dr.Routes[PathID{Ingress: ingress, Egress: egress}], nil |
| 120 | |
| 121 | } |
| 122 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 123 | func (dr *DeviceRoutes) RemoveRoutes() { |
| 124 | dr.reset() |
| 125 | } |
| 126 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 127 | //ComputeRoutes calculates all the routes between the logical ports. This will clear up any existing route |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 128 | func (dr *DeviceRoutes) ComputeRoutes(ctx context.Context, lps map[uint32]*voltha.LogicalPort) error { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 129 | dr.routeBuildLock.Lock() |
| 130 | defer dr.routeBuildLock.Unlock() |
| 131 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 132 | logger.Debugw(ctx, "computing-all-routes", log.Fields{"len-logical-ports": len(lps)}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 133 | var err error |
| 134 | defer func() { |
| 135 | // On error, clear the routes - any flow request or a port add/delete will trigger the rebuild |
| 136 | if err != nil { |
| 137 | dr.reset() |
| 138 | } |
| 139 | }() |
| 140 | |
| 141 | if len(lps) < 2 { |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 142 | return fmt.Errorf("not enough logical port :%w", ErrNoRoute) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | dr.reset() |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 146 | |
| 147 | // Setup the physical ports to logical ports map, the nni ports as well as the root ports map |
| 148 | physPortToLogicalPortMap := make(map[string]uint32) |
| 149 | nniPorts := make([]*voltha.LogicalPort, 0) |
| 150 | for _, lp := range lps { |
| 151 | physPortToLogicalPortMap[concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)] = lp.OfpPort.PortNo |
| 152 | if lp.RootPort { |
| 153 | nniPorts = append(nniPorts, lp) |
| 154 | dr.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo |
| 155 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 156 | dr.logicalPorts[lp.OfpPort.PortNo] = lp |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 157 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 158 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 159 | if len(nniPorts) == 0 { |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 160 | return fmt.Errorf("no nni port :%w", ErrNoRoute) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 161 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 162 | var copyFromNNIPort *voltha.LogicalPort |
| 163 | for idx, nniPort := range nniPorts { |
| 164 | if idx == 0 { |
| 165 | copyFromNNIPort = nniPort |
| 166 | } else if len(dr.Routes) > 0 { |
| 167 | dr.copyFromExistingNNIRoutes(nniPort, copyFromNNIPort) |
| 168 | return nil |
| 169 | } |
| 170 | // Get root device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 171 | rootDeviceID := nniPort.DeviceId |
| 172 | rootDevicePorts, err := dr.getDeviceWithCacheUpdate(ctx, nniPort.DeviceId) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 173 | if err != nil { |
| 174 | return err |
| 175 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 176 | if len(rootDevicePorts) == 0 { |
| 177 | err = status.Errorf(codes.FailedPrecondition, "no-port-%s", rootDeviceID) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 178 | return err |
| 179 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 180 | for _, rootDevicePort := range rootDevicePorts { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 181 | if rootDevicePort.Type == voltha.Port_PON_OLT { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 182 | logger.Debugw(ctx, "peers", log.Fields{"root-device-id": rootDeviceID, "port-no": rootDevicePort.PortNo, "len-peers": len(rootDevicePort.Peers)}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 183 | for _, rootDevicePeer := range rootDevicePort.Peers { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 184 | childDeviceID := rootDevicePeer.DeviceId |
| 185 | childDevicePorts, err := dr.getDeviceWithCacheUpdate(ctx, rootDevicePeer.DeviceId) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 186 | if err != nil { |
| 187 | return err |
| 188 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 189 | childPonPort, err := dr.getChildPonPort(ctx, childDeviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 190 | if err != nil { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 191 | return err |
| 192 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 193 | for _, childDevicePort := range childDevicePorts { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 194 | if childDevicePort.Type == voltha.Port_ETHERNET_UNI { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 195 | childLogicalPort, exist := physPortToLogicalPortMap[concatDeviceIDPortID(childDeviceID, childDevicePort.PortNo)] |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 196 | if !exist { |
| 197 | // This can happen if this logical port has not been created yet for that device |
| 198 | continue |
| 199 | } |
| 200 | dr.Routes[PathID{Ingress: nniPort.OfpPort.PortNo, Egress: childLogicalPort}] = []Hop{ |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 201 | {DeviceID: rootDeviceID, Ingress: nniPort.DevicePortNo, Egress: rootDevicePort.PortNo}, |
| 202 | {DeviceID: childDeviceID, Ingress: childPonPort, Egress: childDevicePort.PortNo}, |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 203 | } |
| 204 | dr.Routes[PathID{Ingress: childLogicalPort, Egress: nniPort.OfpPort.PortNo}] = getReverseRoute( |
| 205 | dr.Routes[PathID{Ingress: nniPort.OfpPort.PortNo, Egress: childLogicalPort}]) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 215 | // AddPort augments the current set of routes with new routes corresponding to the logical port "lp". If the routes have |
| 216 | // not been built yet then use logical port "lps" to compute all current routes (lps includes lp) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 217 | func (dr *DeviceRoutes) AddPort(ctx context.Context, lp *voltha.LogicalPort, deviceID string, devicePorts map[uint32]*voltha.Port, lps map[uint32]*voltha.LogicalPort) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 218 | logger.Debugw(ctx, "add-port-to-routes", log.Fields{"port": lp, "count-logical-ports": len(lps)}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 219 | |
| 220 | // Adding NNI port |
| 221 | if lp.RootPort { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 222 | return dr.AddNNIPort(ctx, lp, deviceID, devicePorts, lps) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 223 | } |
| 224 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 225 | // Adding UNI port |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 226 | return dr.AddUNIPort(ctx, lp, deviceID, devicePorts, lps) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // AddUNIPort setup routes between the logical UNI port lp and all registered NNI ports |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 230 | func (dr *DeviceRoutes) AddUNIPort(ctx context.Context, lp *voltha.LogicalPort, deviceID string, devicePorts map[uint32]*voltha.Port, lps map[uint32]*voltha.LogicalPort) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 231 | logger.Debugw(ctx, "add-uni-port-to-routes", log.Fields{"port": lp, "count-logical-ports": len(lps)}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 232 | |
| 233 | dr.routeBuildLock.Lock() |
| 234 | defer dr.routeBuildLock.Unlock() |
| 235 | |
| 236 | // Add port to logical ports |
| 237 | dr.logicalPorts[lp.OfpPort.PortNo] = lp |
| 238 | |
| 239 | // Update internal structures with device data |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 240 | dr.updateCache(deviceID, devicePorts) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 241 | |
| 242 | // Adding a UNI port |
| 243 | childPonPort, err := dr.getChildPonPort(ctx, lp.DeviceId) |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 247 | rootDevicePonPort, err := dr.getParentPonPort(ctx, deviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 248 | if err != nil { |
| 249 | return err |
| 250 | } |
| 251 | |
| 252 | // Adding a UNI port |
| 253 | for _, lPort := range lps { |
| 254 | if lPort.RootPort { |
| 255 | dr.Routes[PathID{Ingress: lPort.OfpPort.PortNo, Egress: lp.OfpPort.PortNo}] = []Hop{ |
| 256 | {DeviceID: lPort.DeviceId, Ingress: lPort.DevicePortNo, Egress: rootDevicePonPort}, |
| 257 | {DeviceID: lp.DeviceId, Ingress: childPonPort, Egress: lp.DevicePortNo}, |
| 258 | } |
| 259 | dr.Routes[PathID{Ingress: lp.OfpPort.PortNo, Egress: lPort.OfpPort.PortNo}] = getReverseRoute( |
| 260 | dr.Routes[PathID{Ingress: lPort.OfpPort.PortNo, Egress: lp.OfpPort.PortNo}]) |
| 261 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 262 | } |
| 263 | return nil |
| 264 | } |
| 265 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 266 | // AddNNIPort setup routes between the logical NNI port lp and all registered UNI ports |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 267 | func (dr *DeviceRoutes) AddNNIPort(ctx context.Context, lp *voltha.LogicalPort, deviceID string, devicePorts map[uint32]*voltha.Port, lps map[uint32]*voltha.LogicalPort) error { |
| 268 | logger.Debugw(ctx, "add-port-to-routes", log.Fields{"port": lp, "logical-ports-count": len(lps), "device-id": deviceID}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 269 | |
| 270 | dr.routeBuildLock.Lock() |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 271 | defer dr.routeBuildLock.Unlock() |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 272 | |
| 273 | // Update internal structures with device data |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 274 | dr.updateCache(deviceID, devicePorts) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 275 | |
| 276 | // Setup the physical ports to logical ports map, the nni ports as well as the root ports map |
| 277 | physPortToLogicalPortMap := make(map[string]uint32) |
| 278 | for _, lp := range lps { |
| 279 | physPortToLogicalPortMap[concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)] = lp.OfpPort.PortNo |
| 280 | if lp.RootPort { |
| 281 | dr.rootPortsLock.Lock() |
| 282 | dr.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo |
| 283 | dr.rootPortsLock.Unlock() |
| 284 | } |
| 285 | dr.logicalPorts[lp.OfpPort.PortNo] = lp |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 288 | for _, rootDevicePort := range devicePorts { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 289 | if rootDevicePort.Type == voltha.Port_PON_OLT { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 290 | logger.Debugw(ctx, "peers", log.Fields{"root-device-id": deviceID, "port-no": rootDevicePort.PortNo, "len-peers": len(rootDevicePort.Peers)}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 291 | for _, rootDevicePeer := range rootDevicePort.Peers { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 292 | childDeviceID := rootDevicePeer.DeviceId |
| 293 | childDevicePorts, err := dr.getDeviceWithCacheUpdate(ctx, rootDevicePeer.DeviceId) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 294 | if err != nil { |
| 295 | continue |
| 296 | } |
| 297 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 298 | childPonPort, err := dr.getChildPonPort(ctx, childDeviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 299 | if err != nil { |
| 300 | continue |
| 301 | } |
| 302 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 303 | for _, childDevicePort := range childDevicePorts { |
| 304 | childLogicalPort, exist := physPortToLogicalPortMap[concatDeviceIDPortID(childDeviceID, childDevicePort.PortNo)] |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 305 | if !exist { |
| 306 | // This can happen if this logical port has not been created yet for that device |
| 307 | continue |
| 308 | } |
| 309 | |
| 310 | if childDevicePort.Type == voltha.Port_ETHERNET_UNI { |
| 311 | dr.Routes[PathID{Ingress: lp.OfpPort.PortNo, Egress: childLogicalPort}] = []Hop{ |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 312 | {DeviceID: deviceID, Ingress: lp.DevicePortNo, Egress: rootDevicePort.PortNo}, |
| 313 | {DeviceID: childDeviceID, Ingress: childPonPort, Egress: childDevicePort.PortNo}, |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 314 | } |
| 315 | dr.Routes[PathID{Ingress: childLogicalPort, Egress: lp.OfpPort.PortNo}] = getReverseRoute( |
| 316 | dr.Routes[PathID{Ingress: lp.OfpPort.PortNo, Egress: childLogicalPort}]) |
| 317 | } |
| 318 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 322 | return nil |
| 323 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 324 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 325 | // AddAllPorts setups up new routes using all ports on the device. lps includes the device's logical port |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 326 | func (dr *DeviceRoutes) AddAllPorts(ctx context.Context, deviceID string, devicePorts map[uint32]*voltha.Port, lps map[uint32]*voltha.LogicalPort) error { |
| 327 | logger.Debugw(ctx, "add-all-port-to-routes", log.Fields{"logical-ports-count": len(lps), "device-id": deviceID}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 328 | for _, lp := range lps { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 329 | if lp.DeviceId == deviceID { |
| 330 | if err := dr.AddPort(ctx, lp, deviceID, devicePorts, lps); err != nil { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 331 | return err |
| 332 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 333 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 334 | } |
| 335 | return nil |
| 336 | } |
| 337 | |
| 338 | // Print prints routes |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 339 | func (dr *DeviceRoutes) Print(ctx context.Context) error { |
khenaidoo | 292ab52 | 2020-06-05 18:17:59 -0400 | [diff] [blame] | 340 | dr.routeBuildLock.RLock() |
| 341 | defer dr.routeBuildLock.RUnlock() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 342 | logger.Debugw(ctx, "Print", log.Fields{"logical-device-id": dr.logicalDeviceID, "logical-ports": dr.logicalPorts}) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 343 | if logger.V(log.DebugLevel) { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 344 | output := "" |
| 345 | routeNumber := 1 |
| 346 | for k, v := range dr.Routes { |
| 347 | key := fmt.Sprintf("LP:%d->LP:%d", k.Ingress, k.Egress) |
| 348 | val := "" |
| 349 | for _, i := range v { |
| 350 | val += fmt.Sprintf("{%d->%s->%d},", i.Ingress, i.DeviceID, i.Egress) |
| 351 | } |
| 352 | val = val[:len(val)-1] |
| 353 | output += fmt.Sprintf("%d:{%s=>%s} ", routeNumber, key, fmt.Sprintf("[%s]", val)) |
| 354 | routeNumber++ |
| 355 | } |
| 356 | if len(dr.Routes) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 357 | logger.Debugw(ctx, "no-routes-found", log.Fields{"logical-device-id": dr.logicalDeviceID}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 358 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 359 | logger.Debugw(ctx, "graph_routes", log.Fields{"logical-device-id": dr.logicalDeviceID, "Routes": output}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | return nil |
| 363 | } |
| 364 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 365 | // isUpToDate returns true if device is up to date |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 366 | func (dr *DeviceRoutes) isUpToDate(ldPorts map[uint32]*voltha.LogicalPort) bool { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 367 | dr.routeBuildLock.Lock() |
| 368 | defer dr.routeBuildLock.Unlock() |
| 369 | numNNI, numUNI := 0, 0 |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 370 | if ldPorts != nil { |
| 371 | if len(dr.logicalPorts) != len(ldPorts) { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 372 | return false |
| 373 | } |
| 374 | numNNI = len(dr.RootPorts) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 375 | numUNI = len(ldPorts) - numNNI |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 376 | } |
| 377 | return len(dr.Routes) == numNNI*numUNI*2 |
| 378 | } |
| 379 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 380 | // IsRoutesEmpty returns true if there are no routes |
| 381 | func (dr *DeviceRoutes) IsRoutesEmpty() bool { |
| 382 | dr.routeBuildLock.RLock() |
| 383 | defer dr.routeBuildLock.RUnlock() |
| 384 | return len(dr.Routes) == 0 |
| 385 | } |
| 386 | |
| 387 | // GetHalfRoute returns a half route that has only the egress hop set or the ingress hop set |
| 388 | func (dr *DeviceRoutes) GetHalfRoute(nniAsEgress bool, ingress, egress uint32) ([]Hop, error) { |
| 389 | dr.routeBuildLock.RLock() |
| 390 | defer dr.routeBuildLock.RUnlock() |
| 391 | routes := make([]Hop, 0) |
| 392 | for routeLink, path := range dr.Routes { |
| 393 | // If nniAsEgress is set then the half route will only have the egress hop set where the egress port needs to be |
| 394 | // an NNI port |
| 395 | if nniAsEgress { |
| 396 | // Prioritize a specific egress NNI port if set |
| 397 | if egress != 0 && dr.IsRootPort(egress) && routeLink.Egress == egress { |
| 398 | routes = append(routes, Hop{}) |
| 399 | routes = append(routes, path[1]) |
| 400 | return routes, nil |
| 401 | } |
| 402 | if egress == 0 && dr.IsRootPort(routeLink.Egress) { |
| 403 | routes = append(routes, Hop{}) |
| 404 | routes = append(routes, path[1]) |
| 405 | return routes, nil |
| 406 | } |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 407 | } else if ingress != 0 && routeLink.Ingress == ingress { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 408 | // Here we use the first route whose ingress port matches the ingress input parameter |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 409 | routes = append(routes, path[0]) |
| 410 | routes = append(routes, Hop{}) |
| 411 | return routes, nil |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 412 | } |
| 413 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 414 | return routes, fmt.Errorf("no half route found for ingress port %d, egress port %d and nni as egress %t", ingress, egress, nniAsEgress) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 415 | } |
| 416 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 417 | //getDeviceWithCacheUpdate returns the from the model and updates the PON ports map of that device. |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 418 | func (dr *DeviceRoutes) getDeviceWithCacheUpdate(ctx context.Context, deviceID string) (map[uint32]*voltha.Port, error) { |
| 419 | devicePorts, err := dr.listDevicePorts(ctx, deviceID) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 420 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 421 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 422 | return nil, err |
| 423 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 424 | dr.updateCache(deviceID, devicePorts) |
| 425 | return devicePorts, nil |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | //copyFromExistingNNIRoutes copies routes from an existing set of NNI routes |
| 429 | func (dr *DeviceRoutes) copyFromExistingNNIRoutes(newNNIPort *voltha.LogicalPort, copyFromNNIPort *voltha.LogicalPort) { |
| 430 | updatedRoutes := make(map[PathID][]Hop) |
| 431 | for key, val := range dr.Routes { |
| 432 | if key.Ingress == copyFromNNIPort.OfpPort.PortNo { |
| 433 | updatedRoutes[PathID{Ingress: newNNIPort.OfpPort.PortNo, Egress: key.Egress}] = []Hop{ |
| 434 | {DeviceID: newNNIPort.DeviceId, Ingress: newNNIPort.DevicePortNo, Egress: val[0].Egress}, |
| 435 | val[1], |
| 436 | } |
| 437 | } |
| 438 | if key.Egress == copyFromNNIPort.OfpPort.PortNo { |
| 439 | updatedRoutes[PathID{Ingress: key.Ingress, Egress: newNNIPort.OfpPort.PortNo}] = []Hop{ |
| 440 | val[0], |
| 441 | {DeviceID: newNNIPort.DeviceId, Ingress: val[1].Ingress, Egress: newNNIPort.DevicePortNo}, |
| 442 | } |
| 443 | } |
| 444 | updatedRoutes[key] = val |
| 445 | } |
| 446 | dr.Routes = updatedRoutes |
| 447 | } |
| 448 | |
| 449 | // reset cleans up the device graph |
| 450 | func (dr *DeviceRoutes) reset() { |
| 451 | dr.rootPortsLock.Lock() |
| 452 | dr.RootPorts = make(map[uint32]uint32) |
| 453 | dr.rootPortsLock.Unlock() |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 454 | dr.Routes = make(map[PathID][]Hop) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 455 | dr.logicalPorts = make(map[uint32]*voltha.LogicalPort) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 456 | dr.devicesPonPorts = make(map[string][]*voltha.Port) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 457 | dr.childConnectionPort = make(map[string]uint32) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | //concatDeviceIdPortId formats a portid using the device id and the port number |
| 461 | func concatDeviceIDPortID(deviceID string, portNo uint32) string { |
| 462 | return fmt.Sprintf("%s:%d", deviceID, portNo) |
| 463 | } |
| 464 | |
| 465 | //getReverseRoute returns the reverse of the route |
| 466 | func getReverseRoute(route []Hop) []Hop { |
| 467 | reverse := make([]Hop, len(route)) |
| 468 | for i, j := 0, len(route)-1; j >= 0; i, j = i+1, j-1 { |
| 469 | reverse[i].DeviceID, reverse[i].Ingress, reverse[i].Egress = route[j].DeviceID, route[j].Egress, route[j].Ingress |
| 470 | } |
| 471 | return reverse |
| 472 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 473 | |
| 474 | // getChildPonPort returns the child PON port number either from cache or from the model. If it is from the model then |
| 475 | // it updates the PON ports map of that device. |
| 476 | func (dr *DeviceRoutes) getChildPonPort(ctx context.Context, deviceID string) (uint32, error) { |
| 477 | if port, exist := dr.devicesPonPorts[deviceID]; exist { |
| 478 | // Return only the first PON port of that child device |
| 479 | return port[0].PortNo, nil |
| 480 | } |
| 481 | |
| 482 | // Get child device from model |
| 483 | if _, err := dr.getDeviceWithCacheUpdate(ctx, deviceID); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 484 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 485 | return 0, err |
| 486 | } |
| 487 | |
| 488 | // Try again |
| 489 | if port, exist := dr.devicesPonPorts[deviceID]; exist { |
| 490 | // Return only the first PON port of that child device |
| 491 | return port[0].PortNo, nil |
| 492 | } |
| 493 | |
| 494 | return 0, fmt.Errorf("pon port not found %s", deviceID) |
| 495 | } |
| 496 | |
| 497 | // getParentPonPort returns the parent PON port of the child device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 498 | func (dr *DeviceRoutes) getParentPonPort(ctx context.Context, childDeviceID string) (uint32, error) { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 499 | if pNo, exist := dr.childConnectionPort[childDeviceID]; exist { |
| 500 | return pNo, nil |
| 501 | } |
| 502 | |
| 503 | // Get parent device from the model |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 504 | if _, err := dr.getDeviceWithCacheUpdate(ctx, dr.rootDeviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 505 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": dr.rootDeviceID, "error": err}) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 506 | return 0, err |
| 507 | } |
| 508 | // Try again |
| 509 | if pNo, exist := dr.childConnectionPort[childDeviceID]; exist { |
| 510 | return pNo, nil |
| 511 | } |
| 512 | return 0, fmt.Errorf("pon port associated with child device %s not found", childDeviceID) |
| 513 | } |
| 514 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 515 | func (dr *DeviceRoutes) updateCache(deviceID string, devicePorts map[uint32]*voltha.Port) { |
| 516 | for _, port := range devicePorts { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 517 | if port.Type == voltha.Port_PON_ONU || port.Type == voltha.Port_PON_OLT { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 518 | dr.devicesPonPorts[deviceID] = append(dr.devicesPonPorts[deviceID], port) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 519 | for _, peer := range port.Peers { |
| 520 | if port.Type == voltha.Port_PON_ONU { |
| 521 | dr.childConnectionPort[port.DeviceId] = peer.PortNo |
| 522 | } else { |
| 523 | dr.childConnectionPort[peer.DeviceId] = port.PortNo |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | func (dr *DeviceRoutes) getLogicalPorts(ingress, egress uint32) (uniPort, nniPort *voltha.LogicalPort, err error) { |
| 531 | inPort, exist := dr.logicalPorts[ingress] |
| 532 | if !exist { |
| 533 | err = fmt.Errorf("ingress port %d not found", ingress) |
| 534 | return |
| 535 | } |
| 536 | outPort, exist := dr.logicalPorts[egress] |
| 537 | if !exist { |
| 538 | err = fmt.Errorf("egress port %d not found", egress) |
| 539 | return |
| 540 | } |
| 541 | |
| 542 | if inPort.RootPort { |
| 543 | nniPort = inPort |
| 544 | uniPort = outPort |
| 545 | } else { |
| 546 | nniPort = outPort |
| 547 | uniPort = inPort |
| 548 | } |
| 549 | |
| 550 | return |
| 551 | } |