Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [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 | |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 17 | //Package adaptercoreonu provides the utility for onu devices, flows and statistics |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 18 | package adaptercoreonu |
| 19 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 20 | import "context" |
| 21 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 22 | //Attention: this file is more or less a coopy of file olt_platform.go from the voltha-openolt-adapter |
| 23 | // which includes system wide definitions and thus normally should be stored more centrally (within some voltha libs)!! |
| 24 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 25 | /*===================================================================== |
| 26 | |
| 27 | @TODO: Looks like this Flow id concept below is not used anywhere |
| 28 | Propose to remove the below documentation of Flow Id on confirmation |
| 29 | of the same |
| 30 | |
| 31 | Flow id |
| 32 | |
| 33 | Identifies a flow within a single OLT |
| 34 | Flow Id is unique per OLT |
| 35 | Multiple GEM ports can map to same flow id |
| 36 | |
| 37 | 13 11 4 0 |
| 38 | +--------+--------------+------+ |
| 39 | | pon id | onu id | Flow | |
| 40 | | | | idx | |
| 41 | +--------+--------------+------+ |
| 42 | |
| 43 | 14 bits = 16384 flows (per OLT). |
| 44 | |
| 45 | pon id = 4 bits = 16 PON ports |
| 46 | onu id = 7 bits = 128 ONUss per PON port |
| 47 | Flow index = 3 bits = 4 bi-directional flows per ONU |
| 48 | = 8 uni-directional flows per ONU |
| 49 | |
| 50 | |
| 51 | Logical (OF) UNI port number |
| 52 | |
| 53 | OpenFlow port number corresponding to PON UNI |
| 54 | |
| 55 | 20 12 4 0 |
| 56 | +--+--------+--------------+------+ |
| 57 | |0 | pon id | onu id |uni id| |
| 58 | +--+--------+--------------+------+ |
| 59 | |
| 60 | pon id = 8 bits = 256 PON ports |
| 61 | onu id = 8 bits = 256 ONUs per PON port |
| 62 | |
| 63 | Logical (OF) NNI port number |
| 64 | |
| 65 | OpenFlow port number corresponding to PON NNI |
| 66 | |
| 67 | 20 0 |
| 68 | +--+----------------------------+ |
| 69 | |1 | intf_id | |
| 70 | +--+----------------------------+ |
| 71 | |
| 72 | No overlap with UNI port number space |
| 73 | |
| 74 | |
| 75 | PON OLT (OF) port number |
| 76 | |
| 77 | OpenFlow port number corresponding to PON OLT ports |
| 78 | |
| 79 | 31 28 0 |
| 80 | +--------+------------------------~~~------+ |
| 81 | | 0x2 | pon intf id | |
| 82 | +--------+------------------------~~~------+ |
| 83 | */ |
| 84 | |
| 85 | const ( |
| 86 | // Number of bits for the physical UNI of the ONUs |
| 87 | bitsForUniID = 4 |
| 88 | // Number of bits for the ONU ID |
| 89 | bitsForONUID = 8 |
| 90 | // Number of bits for PON ID |
| 91 | bitsForPONID = 8 |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 92 | /* |
| 93 | // Number of bits to differentiate between UNI and NNI Logical Port |
| 94 | bitsForUNINNIDiff = 1 |
| 95 | */ |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 96 | //maxOnusPerPon is Max number of ONUs on any PON port |
| 97 | maxOnusPerPon = (1 << bitsForONUID) |
| 98 | //maxPonsPerOlt is Max number of PON ports on any OLT |
| 99 | maxPonsPerOlt = (1 << bitsForPONID) |
| 100 | //maxUnisPerOnu is the Max number of UNI ports on any ONU |
| 101 | maxUnisPerOnu = (1 << bitsForUniID) |
| 102 | /* |
| 103 | //Bit position where the differentiation bit is located |
| 104 | nniUniDiffPos = (bitsForUniID + bitsForONUID + bitsForPONID) |
| 105 | //Bit position where the marker for PON port type of OF port is present |
| 106 | ponIntfMarkerPos = 28 |
| 107 | //Value of marker used to distinguish PON port type of OF port |
| 108 | ponIntfMarkerValue = 0x2 |
| 109 | // Number of bits for NNI ID |
| 110 | bitsforNNIID = 20 |
| 111 | // minNniIntPortNum is used to store start range of nni port number (1 << 20) 1048576 |
| 112 | minNniIntPortNum = (1 << bitsforNNIID) |
| 113 | // maxNniPortNum is used to store the maximum range of nni port number ((1 << 21)-1) 2097151 |
| 114 | maxNniPortNum = ((1 << (bitsforNNIID + 1)) - 1) |
| 115 | */ |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 116 | ) |
| 117 | |
Holger Hildebrandt | be67442 | 2020-05-05 13:05:30 +0000 | [diff] [blame] | 118 | //Mask to indicate which possibly active ONU UNI state is really reported to the core |
| 119 | // compare python code - at the moment restrict active state to the first ONU UNI port |
| 120 | // check is limited to max 16 uni ports - cmp above UNI limit!!! |
Matteo Scandolo | abfa192 | 2021-06-04 08:57:51 -0700 | [diff] [blame^] | 121 | var activeUniPortStateUpdateMask = 0x0001 |
Holger Hildebrandt | be67442 | 2020-05-05 13:05:30 +0000 | [diff] [blame] | 122 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 123 | /* |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 124 | //MinUpstreamPortID value |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 125 | var minUpstreamPortID = 0xfffd |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 126 | |
| 127 | //MaxUpstreamPortID value |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 128 | var maxUpstreamPortID = 0xfffffffd |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 129 | |
| 130 | var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd} |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 131 | */ |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 132 | |
Holger Hildebrandt | 47555e7 | 2020-09-21 11:07:24 +0000 | [diff] [blame] | 133 | //mkUniPortNum returns new UNIportNum based on intfID, onuID and uniID |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 134 | func mkUniPortNum(ctx context.Context, intfID, onuID, uniID uint32) uint32 { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 135 | //extended for checks available in the python onu adapter:!! |
| 136 | var limit = int(intfID) |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 137 | if limit > maxPonsPerOlt { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 138 | logger.Warn(ctx, "Warning: exceeded the MAX pons per OLT") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 139 | } |
| 140 | limit = int(onuID) |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 141 | if limit > maxOnusPerPon { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 142 | logger.Warn(ctx, "Warning: exceeded the MAX ONUS per PON") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 143 | } |
| 144 | limit = int(uniID) |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 145 | if limit > maxUnisPerOnu { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 146 | logger.Warn(ctx, "Warning: exceeded the MAX UNIS per ONU") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 147 | } |
| 148 | return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID |
| 149 | } |
| 150 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 151 | /* |
| 152 | //onuIDFromPortNum returns ONUID derived from portNumber |
| 153 | func onuIDFromPortNum(portNum uint32) uint32 { |
| 154 | return (portNum >> bitsForUniID) & (maxOnusPerPon - 1) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 157 | //intfIDFromUniPortNum returns IntfID derived from portNum |
| 158 | func intfIDFromUniPortNum(portNum uint32) uint32 { |
| 159 | return (portNum >> (bitsForUniID + bitsForONUID)) & (maxPonsPerOlt - 1) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 162 | //uniIDFromPortNum return UniID derived from portNum |
| 163 | func uniIDFromPortNum(portNum uint32) uint32 { |
| 164 | return (portNum) & (maxUnisPerOnu - 1) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 167 | //intfIDToPortNo returns portId derived from intftype, intfId and portType |
| 168 | func intfIDToPortNo(intfID uint32, intfType voltha.Port_PortType) uint32 { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 169 | if (intfType) == voltha.Port_ETHERNET_NNI { |
| 170 | return (1 << nniUniDiffPos) | intfID |
| 171 | } |
| 172 | if (intfType) == voltha.Port_PON_OLT { |
| 173 | return (ponIntfMarkerValue << ponIntfMarkerPos) | intfID |
| 174 | } |
| 175 | return 0 |
| 176 | } |
| 177 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 178 | //portNoToIntfID returns portnumber derived from interfaceID |
| 179 | func portNoToIntfID(portno uint32, intfType voltha.Port_PortType) uint32 { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 180 | if (intfType) == voltha.Port_ETHERNET_NNI { |
| 181 | return (1 << nniUniDiffPos) ^ portno |
| 182 | } |
| 183 | if (intfType) == voltha.Port_PON_OLT { |
| 184 | return (ponIntfMarkerValue << ponIntfMarkerPos) ^ portno |
| 185 | } |
| 186 | return 0 |
| 187 | } |
| 188 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 189 | //intfIDFromNniPortNum returns Intf ID derived from portNum |
| 190 | func intfIDFromNniPortNum(portNum uint32) (uint32, error) { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 191 | if portNum < minNniIntPortNum || portNum > maxNniPortNum { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 192 | logger.Errorw(ctx,"NNIPortNumber is not in valid range", log.Fields{"portNum": portNum}) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 193 | return uint32(0), errors.New("invalid-port-range") //olterrors.ErrInvalidPortRange |
| 194 | } |
| 195 | return (portNum & 0xFFFF), nil |
| 196 | } |
| 197 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 198 | //intfIDToPortTypeName returns port type derived from the intfId |
| 199 | func intfIDToPortTypeName(intfID uint32) voltha.Port_PortType { |
| 200 | if ((ponIntfMarkerValue << ponIntfMarkerPos) ^ intfID) < maxPonsPerOlt { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 201 | return voltha.Port_PON_OLT |
| 202 | } |
| 203 | if (intfID & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) { |
| 204 | return voltha.Port_ETHERNET_NNI |
| 205 | } |
| 206 | return voltha.Port_ETHERNET_UNI |
| 207 | } |
| 208 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 209 | //extractAccessFromFlow returns AccessDevice information |
| 210 | func extractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) { |
| 211 | if isUpstream(outPort) { |
| 212 | return inPort, intfIDFromUniPortNum(inPort), onuIDFromPortNum(inPort), uniIDFromPortNum(inPort) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 213 | } |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 214 | return outPort, intfIDFromUniPortNum(outPort), onuIDFromPortNum(outPort), uniIDFromPortNum(outPort) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 217 | //isUpstream returns true for Upstream and false for downstream |
| 218 | func isUpstream(outPort uint32) bool { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 219 | for _, port := range controllerPorts { |
| 220 | if port == outPort { |
| 221 | return true |
| 222 | } |
| 223 | } |
| 224 | return (outPort & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) |
| 225 | } |
| 226 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 227 | //isControllerBoundFlow returns true/false |
| 228 | func isControllerBoundFlow(outPort uint32) bool { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 229 | for _, port := range controllerPorts { |
| 230 | if port == outPort { |
| 231 | return true |
| 232 | } |
| 233 | } |
| 234 | return false |
| 235 | } |
| 236 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 237 | //onuIDFromUniPortNum returns onuId from give portNum information. |
| 238 | func onuIDFromUniPortNum(portNum uint32) uint32 { |
| 239 | return (portNum >> bitsForUniID) & (maxOnusPerPon - 1) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 242 | //flowExtractInfo fetches uniport from the flow, based on which it gets and returns ponInf, onuID, uniID, inPort and ethType |
| 243 | func flowExtractInfo(flow *ofp.OfpFlowStats, flowDirection string) (uint32, uint32, uint32, uint32, uint32, uint32, error) { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 244 | var uniPortNo uint32 |
| 245 | var ponIntf uint32 |
| 246 | var onuID uint32 |
| 247 | var uniID uint32 |
| 248 | var inPort uint32 |
| 249 | var ethType uint32 |
| 250 | |
| 251 | if flowDirection == "upstream" { |
| 252 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 253 | for _, field := range flows.GetOfbFields(flow) { |
| 254 | if field.GetType() == flows.IN_PORT { |
| 255 | uniPortNo = field.GetPort() |
| 256 | break |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } else if flowDirection == "downstream" { |
| 261 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 262 | for _, field := range flows.GetOfbFields(flow) { |
| 263 | if field.GetType() == flows.METADATA { |
| 264 | for _, action := range flows.GetActions(flow) { |
| 265 | if action.Type == flows.OUTPUT { |
| 266 | if out := action.GetOutput(); out != nil { |
| 267 | uniPortNo = out.GetPort() |
| 268 | } |
| 269 | break |
| 270 | } |
| 271 | } |
| 272 | } else if field.GetType() == flows.IN_PORT { |
| 273 | inPort = field.GetPort() |
| 274 | } else if field.GetType() == flows.ETH_TYPE { |
| 275 | ethType = field.GetEthType() |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if uniPortNo == 0 { |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 282 | return 0, 0, 0, 0, 0, 0, errors.New("notFound: pon-interface (flowDirection)") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 283 | // olterrors.NewErrNotFound("pon-interface", log.Fields{"flow-direction": flowDirection}, nil) |
| 284 | } |
| 285 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 286 | ponIntf = intfIDFromUniPortNum(uniPortNo) |
| 287 | onuID = onuIDFromUniPortNum(uniPortNo) |
| 288 | uniID = uniIDFromPortNum(uniPortNo) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 289 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 290 | logger.Debugw(ctx,"flow extract info result", |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 291 | log.Fields{"uniPortNo": uniPortNo, "ponIntf": ponIntf, |
| 292 | "onuID": onuID, "uniID": uniID, "inPort": inPort, "ethType": ethType}) |
| 293 | |
| 294 | return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil |
| 295 | } |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 296 | */ |