Mahir Gunyel | 2c4b96b | 2021-09-13 17:01:05 -0700 | [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 platform |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | |
| 22 | "github.com/opencord/voltha-lib-go/v7/pkg/flows" |
| 23 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 24 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 25 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
| 28 | ) |
| 29 | |
| 30 | /*===================================================================== |
| 31 | Logical (OF) UNI port number |
| 32 | |
| 33 | OpenFlow port number corresponding to PON UNI |
| 34 | |
| 35 | 20 12 4 0 |
| 36 | +--+--------+--------------+------+ |
| 37 | |0 | pon id | onu id |uni id| |
| 38 | +--+--------+--------------+------+ |
| 39 | |
| 40 | pon id = 8 bits = 256 PON ports |
| 41 | onu id = 8 bits = 256 ONUs per PON port |
| 42 | |
| 43 | Logical (OF) NNI port number |
| 44 | |
| 45 | OpenFlow port number corresponding to PON NNI |
| 46 | |
| 47 | 20 0 |
| 48 | +--+----------------------------+ |
| 49 | |1 | intf_id | |
| 50 | +--+----------------------------+ |
| 51 | |
| 52 | No overlap with UNI port number space |
| 53 | |
| 54 | |
| 55 | PON OLT (OF) port number |
| 56 | |
| 57 | OpenFlow port number corresponding to PON OLT ports |
| 58 | |
| 59 | 31 28 0 |
| 60 | +--------+------------------------~~~------+ |
| 61 | | 0x2 | pon intf id | |
| 62 | +--------+------------------------~~~------+ |
| 63 | */ |
| 64 | |
| 65 | const ( |
| 66 | // Number of bits for the physical UNI of the ONUs |
| 67 | bitsForUniID = 4 |
| 68 | // Number of bits for the ONU ID |
| 69 | bitsForONUID = 8 |
| 70 | // Number of bits for PON ID |
| 71 | bitsForPONID = 8 |
| 72 | // MaxOnusPerPon is Max number of ONUs on any PON port |
| 73 | MaxOnusPerPon = (1 << bitsForONUID) |
| 74 | // MaxPonsPerOlt is Max number of PON ports on any OLT |
| 75 | MaxPonsPerOlt = (1 << bitsForPONID) |
| 76 | // MaxUnisPerOnu is the Max number of UNI ports on any ONU |
| 77 | MaxUnisPerOnu = (1 << bitsForUniID) |
| 78 | // Bit position where the differentiation bit is located |
| 79 | nniUniDiffPos = (bitsForUniID + bitsForONUID + bitsForPONID) |
| 80 | // Bit position where the marker for PON port type of OF port is present |
| 81 | ponIntfMarkerPos = 28 |
| 82 | // Value of marker used to distinguish PON port type of OF port |
| 83 | ponIntfMarkerValue = 0x2 |
| 84 | // Number of bits for NNI ID |
| 85 | bitsforNNIID = 20 |
| 86 | // minNniIntPortNum is used to store start range of nni port number (1 << 20) 1048576 |
| 87 | minNniIntPortNum = (1 << bitsforNNIID) |
| 88 | // maxNniPortNum is used to store the maximum range of nni port number ((1 << 21)-1) 2097151 |
| 89 | maxNniPortNum = ((1 << (bitsforNNIID + 1)) - 1) |
| 90 | // minPonIntfPortNum stores the minimum pon port number |
| 91 | minPonIntfPortNum = ponIntfMarkerValue << ponIntfMarkerPos |
| 92 | // maxPonIntfPortNum stores the maximum pon port number |
| 93 | maxPonIntfPortNum = (ponIntfMarkerValue << ponIntfMarkerPos) | (1 << bitsForPONID) |
| 94 | upstream = "upstream" |
| 95 | downstream = "downstream" |
| 96 | ) |
| 97 | |
| 98 | //MinUpstreamPortID value |
| 99 | var MinUpstreamPortID = 0xfffd |
| 100 | |
| 101 | //MaxUpstreamPortID value |
| 102 | var MaxUpstreamPortID = 0xfffffffd |
| 103 | |
| 104 | var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd} |
| 105 | |
| 106 | //MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID |
| 107 | func MkUniPortNum(ctx context.Context, intfID, onuID, uniID uint32) uint32 { |
| 108 | var limit = int(onuID) |
| 109 | if limit > MaxOnusPerPon { |
| 110 | logger.Warn(ctx, "exceeded-the-max-onus-per-pon") |
| 111 | } |
| 112 | return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID |
| 113 | } |
| 114 | |
| 115 | //OnuIDFromPortNum returns ONUID derived from portNumber |
| 116 | func OnuIDFromPortNum(portNum uint32) uint32 { |
| 117 | return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1) |
| 118 | } |
| 119 | |
| 120 | //IntfIDFromUniPortNum returns IntfID derived from portNum |
| 121 | func IntfIDFromUniPortNum(portNum uint32) uint32 { |
| 122 | return (portNum >> (bitsForUniID + bitsForONUID)) & (MaxPonsPerOlt - 1) |
| 123 | } |
| 124 | |
| 125 | //UniIDFromPortNum return UniID derived from portNum |
| 126 | func UniIDFromPortNum(portNum uint32) uint32 { |
| 127 | return (portNum) & (MaxUnisPerOnu - 1) |
| 128 | } |
| 129 | |
| 130 | //IntfIDToPortNo returns portId derived from intftype, intfId and portType |
| 131 | func IntfIDToPortNo(intfID uint32, intfType voltha.Port_PortType) uint32 { |
| 132 | if (intfType) == voltha.Port_ETHERNET_NNI { |
| 133 | return (1 << nniUniDiffPos) | intfID |
| 134 | } |
| 135 | if (intfType) == voltha.Port_PON_OLT { |
| 136 | return (ponIntfMarkerValue << ponIntfMarkerPos) | intfID |
| 137 | } |
| 138 | return 0 |
| 139 | } |
| 140 | |
| 141 | //PortNoToIntfID returns portnumber derived from interfaceID |
| 142 | func PortNoToIntfID(portno uint32, intfType voltha.Port_PortType) uint32 { |
| 143 | if (intfType) == voltha.Port_ETHERNET_NNI { |
| 144 | return (1 << nniUniDiffPos) ^ portno |
| 145 | } |
| 146 | if (intfType) == voltha.Port_PON_OLT { |
| 147 | return (ponIntfMarkerValue << ponIntfMarkerPos) ^ portno |
| 148 | } |
| 149 | return 0 |
| 150 | } |
| 151 | |
| 152 | //IntfIDFromNniPortNum returns Intf ID derived from portNum |
| 153 | func IntfIDFromNniPortNum(ctx context.Context, portNum uint32) (uint32, error) { |
| 154 | if portNum < minNniIntPortNum || portNum > maxNniPortNum { |
| 155 | logger.Errorw(ctx, "nniportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum}) |
| 156 | return uint32(0), status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", portNum) |
| 157 | } |
| 158 | return (portNum & 0xFFFF), nil |
| 159 | } |
| 160 | |
| 161 | //IntfIDFromPonPortNum returns Intf ID derived from portNum |
| 162 | func IntfIDFromPonPortNum(ctx context.Context, portNum uint32) (uint32, error) { |
| 163 | if portNum < minPonIntfPortNum || portNum > maxPonIntfPortNum { |
| 164 | logger.Errorw(ctx, "ponportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum}) |
| 165 | return uint32(0), status.Errorf(codes.InvalidArgument, "invalid-pon-port-number:%d", portNum) |
| 166 | } |
| 167 | return (portNum & 0x7FFF), nil |
| 168 | } |
| 169 | |
| 170 | //IntfIDToPortTypeName returns port type derived from the intfId |
| 171 | func IntfIDToPortTypeName(intfID uint32) voltha.Port_PortType { |
| 172 | if ((ponIntfMarkerValue << ponIntfMarkerPos) ^ intfID) < MaxPonsPerOlt { |
| 173 | return voltha.Port_PON_OLT |
| 174 | } |
| 175 | if (intfID & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) { |
| 176 | return voltha.Port_ETHERNET_NNI |
| 177 | } |
| 178 | return voltha.Port_ETHERNET_UNI |
| 179 | } |
| 180 | |
| 181 | //ExtractAccessFromFlow returns AccessDevice information |
| 182 | func ExtractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) { |
| 183 | if IsUpstream(outPort) { |
| 184 | return inPort, IntfIDFromUniPortNum(inPort), OnuIDFromPortNum(inPort), UniIDFromPortNum(inPort) |
| 185 | } |
| 186 | return outPort, IntfIDFromUniPortNum(outPort), OnuIDFromPortNum(outPort), UniIDFromPortNum(outPort) |
| 187 | } |
| 188 | |
| 189 | //IsUpstream returns true for Upstream and false for downstream |
| 190 | func IsUpstream(outPort uint32) bool { |
| 191 | for _, port := range controllerPorts { |
| 192 | if port == outPort { |
| 193 | return true |
| 194 | } |
| 195 | } |
| 196 | return (outPort & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) |
| 197 | } |
| 198 | |
| 199 | //IsControllerBoundFlow returns true/false |
| 200 | func IsControllerBoundFlow(outPort uint32) bool { |
| 201 | for _, port := range controllerPorts { |
| 202 | if port == outPort { |
| 203 | return true |
| 204 | } |
| 205 | } |
| 206 | return false |
| 207 | } |
| 208 | |
| 209 | //OnuIDFromUniPortNum returns onuId from give portNum information. |
| 210 | func OnuIDFromUniPortNum(portNum uint32) uint32 { |
| 211 | return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1) |
| 212 | } |
| 213 | |
| 214 | //FlowExtractInfo fetches uniport from the flow, based on which it gets and returns ponInf, onuID, uniID, inPort and ethType |
| 215 | func FlowExtractInfo(ctx context.Context, flow *ofp.OfpFlowStats, flowDirection string) (uint32, uint32, uint32, uint32, uint32, uint32, error) { |
| 216 | var uniPortNo uint32 |
| 217 | var ponIntf uint32 |
| 218 | var onuID uint32 |
| 219 | var uniID uint32 |
| 220 | var inPort uint32 |
| 221 | var ethType uint32 |
| 222 | |
| 223 | if flowDirection == upstream { |
| 224 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 225 | for _, field := range flows.GetOfbFields(flow) { |
| 226 | if field.GetType() == flows.IN_PORT { |
| 227 | uniPortNo = field.GetPort() |
| 228 | break |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } else if flowDirection == downstream { |
| 233 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 234 | for _, field := range flows.GetOfbFields(flow) { |
| 235 | if field.GetType() == flows.METADATA { |
| 236 | for _, action := range flows.GetActions(flow) { |
| 237 | if action.Type == flows.OUTPUT { |
| 238 | if out := action.GetOutput(); out != nil { |
| 239 | uniPortNo = out.GetPort() |
| 240 | } |
| 241 | break |
| 242 | } |
| 243 | } |
| 244 | } else if field.GetType() == flows.IN_PORT { |
| 245 | inPort = field.GetPort() |
| 246 | } else if field.GetType() == flows.ETH_TYPE { |
| 247 | ethType = field.GetEthType() |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if uniPortNo == 0 { |
| 254 | return 0, 0, 0, 0, 0, 0, status.Errorf(codes.NotFound, "uni-not-found-flow-diraction:%s", flowDirection) |
| 255 | } |
| 256 | |
| 257 | ponIntf = IntfIDFromUniPortNum(uniPortNo) |
| 258 | onuID = OnuIDFromUniPortNum(uniPortNo) |
| 259 | uniID = UniIDFromPortNum(uniPortNo) |
| 260 | |
| 261 | logger.Debugw(ctx, "flow-extract-info-result", |
| 262 | log.Fields{ |
| 263 | "uniportno": uniPortNo, |
| 264 | "pon-intf": ponIntf, |
| 265 | "onu-id": onuID, |
| 266 | "uni-id": uniID, |
| 267 | "inport": inPort, |
| 268 | "ethtype": ethType}) |
| 269 | |
| 270 | return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil |
| 271 | } |