Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [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 | */ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 16 | |
| 17 | //Package adaptercore provides the utility for olt devices, flows and statistics |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 18 | package adaptercore |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 19 | |
| 20 | import ( |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 21 | "errors" |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 22 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 24 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 25 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 26 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 27 | ) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 28 | |
| 29 | /*===================================================================== |
| 30 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 31 | @TODO: Looks like this Flow id concept below is not used anywhere |
| 32 | Propose to remove the below documentation of Flow Id on confirmation |
| 33 | of the same |
| 34 | |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 35 | Flow id |
| 36 | |
| 37 | Identifies a flow within a single OLT |
| 38 | Flow Id is unique per OLT |
| 39 | Multiple GEM ports can map to same flow id |
| 40 | |
| 41 | 13 11 4 0 |
| 42 | +--------+--------------+------+ |
| 43 | | pon id | onu id | Flow | |
| 44 | | | | idx | |
| 45 | +--------+--------------+------+ |
| 46 | |
| 47 | 14 bits = 16384 flows (per OLT). |
| 48 | |
| 49 | pon id = 4 bits = 16 PON ports |
| 50 | onu id = 7 bits = 128 ONUss per PON port |
| 51 | Flow index = 3 bits = 4 bi-directional flows per ONU |
| 52 | = 8 uni-directional flows per ONU |
| 53 | |
| 54 | |
| 55 | Logical (OF) UNI port number |
| 56 | |
| 57 | OpenFlow port number corresponding to PON UNI |
| 58 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 59 | 20 12 4 0 |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 60 | +--+--------+--------------+------+ |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 61 | |0 | pon id | onu id |uni id| |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 62 | +--+--------+--------------+------+ |
| 63 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 64 | pon id = 8 bits = 256 PON ports |
| 65 | onu id = 8 bits = 256 ONUs per PON port |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 66 | |
| 67 | Logical (OF) NNI port number |
| 68 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 69 | OpenFlow port number corresponding to PON NNI |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 70 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 71 | 20 0 |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 72 | +--+----------------------------+ |
| 73 | |1 | intf_id | |
| 74 | +--+----------------------------+ |
| 75 | |
| 76 | No overlap with UNI port number space |
| 77 | |
| 78 | |
| 79 | PON OLT (OF) port number |
| 80 | |
| 81 | OpenFlow port number corresponding to PON OLT ports |
| 82 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 83 | 31 28 0 |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 84 | +--------+------------------------~~~------+ |
| 85 | | 0x2 | pon intf id | |
| 86 | +--------+------------------------~~~------+ |
| 87 | */ |
| 88 | |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 89 | const ( |
| 90 | // Number of bits for the physical UNI of the ONUs |
| 91 | bitsForUniID = 4 |
| 92 | // Number of bits for the ONU ID |
| 93 | bitsForONUID = 8 |
| 94 | // Number of bits for PON ID |
| 95 | bitsForPONID = 8 |
| 96 | // Number of bits to differentiate between UNI and NNI Logical Port |
| 97 | bitsForUNINNIDiff = 1 |
| 98 | //MaxOnusPerPon is Max number of ONUs on any PON port |
| 99 | MaxOnusPerPon = (1 << bitsForONUID) |
| 100 | //MaxPonsPerOlt is Max number of PON ports on any OLT |
| 101 | MaxPonsPerOlt = (1 << bitsForPONID) |
| 102 | //MaxUnisPerOnu is the Max number of UNI ports on any ONU |
| 103 | MaxUnisPerOnu = (1 << bitsForUniID) |
| 104 | //Bit position where the differentiation bit is located |
| 105 | nniUniDiffPos = (bitsForUniID + bitsForONUID + bitsForPONID) |
| 106 | //Bit position where the marker for PON port type of OF port is present |
| 107 | ponIntfMarkerPos = 28 |
| 108 | //Value of marker used to distinguish PON port type of OF port |
| 109 | ponIntfMarkerValue = 0x2 |
| 110 | ) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 111 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 112 | //MinUpstreamPortID value |
| 113 | var MinUpstreamPortID = 0xfffd |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 114 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 115 | //MaxUpstreamPortID value |
| 116 | var MaxUpstreamPortID = 0xfffffffd |
| 117 | |
| 118 | var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd} |
| 119 | |
| 120 | //MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID |
| 121 | func MkUniPortNum(intfID, onuID, uniID uint32) uint32 { |
gerardo.laurenzi | 72c8438 | 2019-07-11 15:03:46 +0000 | [diff] [blame] | 122 | var limit = int(onuID) |
| 123 | if limit > MaxOnusPerPon { |
| 124 | log.Warn("Warning: exceeded the MAX ONUS per PON") |
| 125 | } |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 126 | return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 127 | } |
| 128 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 129 | //OnuIDFromPortNum returns ONUID derived from portNumber |
| 130 | func OnuIDFromPortNum(portNum uint32) uint32 { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 131 | return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 132 | } |
| 133 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 134 | //IntfIDFromUniPortNum returns IntfID derived from portNum |
| 135 | func IntfIDFromUniPortNum(portNum uint32) uint32 { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 136 | return (portNum >> (bitsForUniID + bitsForONUID)) & (MaxPonsPerOlt - 1) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 137 | } |
| 138 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 139 | //UniIDFromPortNum return UniID derived from portNum |
| 140 | func UniIDFromPortNum(portNum uint32) uint32 { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 141 | return (portNum) & (MaxUnisPerOnu - 1) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 142 | } |
| 143 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 144 | //IntfIDToPortNo returns portId derived from intftype, intfId and portType |
| 145 | func IntfIDToPortNo(intfID uint32, intfType voltha.Port_PortType) uint32 { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 146 | if (intfType) == voltha.Port_ETHERNET_NNI { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 147 | return (1 << nniUniDiffPos) | intfID |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 148 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 149 | if (intfType) == voltha.Port_PON_OLT { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 150 | return (ponIntfMarkerValue << ponIntfMarkerPos) | intfID |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 151 | } |
| 152 | return 0 |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 153 | } |
| 154 | |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 155 | //PortNoToIntfID returns portnumber derived from interfaceID |
| 156 | func PortNoToIntfID(portno uint32, intfType voltha.Port_PortType) uint32 { |
| 157 | if (intfType) == voltha.Port_ETHERNET_NNI { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 158 | return (1 << nniUniDiffPos) ^ portno |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 159 | } |
| 160 | if (intfType) == voltha.Port_PON_OLT { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 161 | return (ponIntfMarkerValue << ponIntfMarkerPos) ^ portno |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 162 | } |
| 163 | return 0 |
| 164 | } |
| 165 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 166 | //IntfIDFromNniPortNum returns Intf ID derived from portNum |
| 167 | func IntfIDFromNniPortNum(portNum uint32) uint32 { |
| 168 | return portNum & 0xFFFF |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 171 | //IntfIDToPortTypeName returns port type derived from the intfId |
| 172 | func IntfIDToPortTypeName(intfID uint32) voltha.Port_PortType { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 173 | if ((ponIntfMarkerValue << ponIntfMarkerPos) ^ intfID) < MaxPonsPerOlt { |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 174 | return voltha.Port_PON_OLT |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 175 | } |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 176 | if (intfID & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 177 | return voltha.Port_ETHERNET_NNI |
| 178 | } |
| 179 | return voltha.Port_ETHERNET_UNI |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 180 | } |
| 181 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 182 | //ExtractAccessFromFlow returns AccessDevice information |
| 183 | func ExtractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 184 | if IsUpstream(outPort) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 185 | return inPort, IntfIDFromUniPortNum(inPort), OnuIDFromPortNum(inPort), UniIDFromPortNum(inPort) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 186 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 187 | return outPort, IntfIDFromUniPortNum(outPort), OnuIDFromPortNum(outPort), UniIDFromPortNum(outPort) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 190 | //IsUpstream returns true for Upstream and false for downstream |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 191 | func IsUpstream(outPort uint32) bool { |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 192 | for _, port := range controllerPorts { |
| 193 | if port == outPort { |
| 194 | return true |
| 195 | } |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 196 | } |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 197 | return (outPort & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 198 | } |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 199 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 200 | //IsControllerBoundFlow returns true/false |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 201 | func IsControllerBoundFlow(outPort uint32) bool { |
| 202 | for _, port := range controllerPorts { |
| 203 | if port == outPort { |
| 204 | return true |
| 205 | } |
| 206 | } |
| 207 | return false |
| 208 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 209 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 210 | //OnuIDFromUniPortNum returns onuId from give portNum information. |
| 211 | func OnuIDFromUniPortNum(portNum uint32) uint32 { |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 212 | return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1) |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 215 | //FlowExtractInfo fetches uniport from the flow, based on which it gets and returns ponInf, onuID, uniID, inPort and ethType |
| 216 | func FlowExtractInfo(flow *ofp.OfpFlowStats, flowDirection string) (uint32, uint32, uint32, uint32, uint32, uint32, error) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 217 | var uniPortNo uint32 |
| 218 | var ponIntf uint32 |
| 219 | var onuID uint32 |
| 220 | var uniID uint32 |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 221 | var inPort uint32 |
| 222 | var ethType uint32 |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 223 | |
| 224 | if flowDirection == "upstream" { |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 225 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 226 | for _, field := range flows.GetOfbFields(flow) { |
| 227 | if field.GetType() == flows.IN_PORT { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 228 | uniPortNo = field.GetPort() |
| 229 | break |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } else if flowDirection == "downstream" { |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 234 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 235 | for _, field := range flows.GetOfbFields(flow) { |
| 236 | if field.GetType() == flows.METADATA { |
| 237 | for _, action := range flows.GetActions(flow) { |
| 238 | if action.Type == flows.OUTPUT { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 239 | if out := action.GetOutput(); out != nil { |
| 240 | uniPortNo = out.GetPort() |
| 241 | } |
| 242 | break |
| 243 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 244 | } |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 245 | } else if field.GetType() == flows.IN_PORT { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 246 | inPort = field.GetPort() |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 247 | } else if field.GetType() == flows.ETH_TYPE { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 248 | ethType = field.GetEthType() |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if uniPortNo == 0 { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 255 | return 0, 0, 0, 0, 0, 0, errors.New("failed to extract Pon Interface, ONU Id and Uni Id from flow") |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 258 | ponIntf = IntfIDFromUniPortNum(uniPortNo) |
| 259 | onuID = OnuIDFromUniPortNum(uniPortNo) |
| 260 | uniID = UniIDFromPortNum(uniPortNo) |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 261 | |
Girish Gowdra | 3d63303 | 2019-12-10 16:37:05 +0530 | [diff] [blame] | 262 | log.Debugw("flow extract info result", |
| 263 | log.Fields{"uniPortNo": uniPortNo, "ponIntf": ponIntf, |
| 264 | "onuID": onuID, "uniID": uniID, "inPort": inPort, "ethType": ethType}) |
| 265 | |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 266 | return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 267 | } |