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 | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame^] | 22 | "github.com/opencord/voltha-lib-go/pkg/flows" |
Scott Baker | f8424cc | 2019-10-18 11:26:50 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/pkg/log" |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 24 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/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 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 130 | //IntfIDFromNniPortNum returns Intf ID derived from portNum |
| 131 | func IntfIDFromNniPortNum(portNum uint32) uint32 { |
| 132 | return portNum & 0xFFFF |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 133 | } |
| 134 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 135 | //IntfIDToPortTypeName returns port type derived from the intfId |
| 136 | func IntfIDToPortTypeName(intfID uint32) voltha.Port_PortType { |
| 137 | if ((2 << 28) ^ intfID) < 16 { |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 138 | return voltha.Port_PON_OLT |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 139 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 140 | if (intfID & (1 << 16)) == (1 << 16) { |
| 141 | return voltha.Port_ETHERNET_NNI |
| 142 | } |
| 143 | return voltha.Port_ETHERNET_UNI |
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 | //ExtractAccessFromFlow returns AccessDevice information |
| 147 | func ExtractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 148 | if IsUpstream(outPort) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 149 | return inPort, IntfIDFromUniPortNum(inPort), OnuIDFromPortNum(inPort), UniIDFromPortNum(inPort) |
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 | return outPort, IntfIDFromUniPortNum(outPort), OnuIDFromPortNum(outPort), UniIDFromPortNum(outPort) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 152 | } |
| 153 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 154 | //IsUpstream returns true for Upstream and false for downstream |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 155 | func IsUpstream(outPort uint32) bool { |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 156 | for _, port := range controllerPorts { |
| 157 | if port == outPort { |
| 158 | return true |
| 159 | } |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 160 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 161 | return (outPort & (1 << 16)) == (1 << 16) |
Manjunath Vanaraj | 39ecd41 | 2019-03-15 20:05:41 +0530 | [diff] [blame] | 162 | } |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 163 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 164 | //IsControllerBoundFlow returns true/false |
manikkaraj k | 17652a7 | 2019-05-06 09:06:36 -0400 | [diff] [blame] | 165 | func IsControllerBoundFlow(outPort uint32) bool { |
| 166 | for _, port := range controllerPorts { |
| 167 | if port == outPort { |
| 168 | return true |
| 169 | } |
| 170 | } |
| 171 | return false |
| 172 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 173 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 174 | //OnuIDFromUniPortNum returns onuId from give portNum information. |
| 175 | func OnuIDFromUniPortNum(portNum uint32) uint32 { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 176 | return (portNum >> 4) & 0x7F |
| 177 | } |
| 178 | |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 179 | //FlowExtractInfo fetches uniport from the flow, based on which it gets and returns ponInf, onuID, uniID, inPort and ethType |
| 180 | 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] | 181 | var uniPortNo uint32 |
| 182 | var ponIntf uint32 |
| 183 | var onuID uint32 |
| 184 | var uniID uint32 |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 185 | var inPort uint32 |
| 186 | var ethType uint32 |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 187 | |
| 188 | if flowDirection == "upstream" { |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame^] | 189 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 190 | for _, field := range flows.GetOfbFields(flow) { |
| 191 | if field.GetType() == flows.IN_PORT { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 192 | uniPortNo = field.GetPort() |
| 193 | break |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } else if flowDirection == "downstream" { |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame^] | 198 | if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 { |
| 199 | for _, field := range flows.GetOfbFields(flow) { |
| 200 | if field.GetType() == flows.METADATA { |
| 201 | for _, action := range flows.GetActions(flow) { |
| 202 | if action.Type == flows.OUTPUT { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 203 | if out := action.GetOutput(); out != nil { |
| 204 | uniPortNo = out.GetPort() |
| 205 | } |
| 206 | break |
| 207 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 208 | } |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame^] | 209 | } else if field.GetType() == flows.IN_PORT { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 210 | inPort = field.GetPort() |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame^] | 211 | } else if field.GetType() == flows.ETH_TYPE { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 212 | ethType = field.GetEthType() |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if uniPortNo == 0 { |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 219 | 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] | 220 | } |
| 221 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 222 | ponIntf = IntfIDFromUniPortNum(uniPortNo) |
| 223 | onuID = OnuIDFromUniPortNum(uniPortNo) |
| 224 | uniID = UniIDFromPortNum(uniPortNo) |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 225 | |
Humera Kouser | 94d7a84 | 2019-08-25 19:04:32 -0400 | [diff] [blame] | 226 | return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 227 | } |