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