blob: 4b36102847bbaf6d8b5d2f955cf87abc3d0927c6 [file] [log] [blame]
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +05301/*
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 Gowdru6a80bbd2019-07-02 07:36:09 -070016
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
Girish Gowdru0c588b22019-04-23 23:24:56 -040019
20import (
Neha Sharma96b7bf22020-06-15 10:37:32 +000021 "context"
Esin Karamanccb714b2019-11-29 15:02:06 +000022 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
23 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Thomas Lee S94109f12020-03-03 16:39:29 +053024 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Esin Karamanccb714b2019-11-29 15:02:06 +000025 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
26 "github.com/opencord/voltha-protos/v3/go/voltha"
Girish Gowdru0c588b22019-04-23 23:24:56 -040027)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053028
29/*=====================================================================
30
Amit Ghoshd4cbe482019-11-21 12:07:14 +000031@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 Vanaraj39ecd412019-03-15 20:05:41 +053035Flow 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
55Logical (OF) UNI port number
56
57 OpenFlow port number corresponding to PON UNI
58
Amit Ghoshd4cbe482019-11-21 12:07:14 +000059 20 12 4 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053060 +--+--------+--------------+------+
Amit Ghoshd4cbe482019-11-21 12:07:14 +000061 |0 | pon id | onu id |uni id|
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053062 +--+--------+--------------+------+
63
Amit Ghoshd4cbe482019-11-21 12:07:14 +000064 pon id = 8 bits = 256 PON ports
65 onu id = 8 bits = 256 ONUs per PON port
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053066
67Logical (OF) NNI port number
68
Amit Ghoshd4cbe482019-11-21 12:07:14 +000069 OpenFlow port number corresponding to PON NNI
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053070
Amit Ghoshd4cbe482019-11-21 12:07:14 +000071 20 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053072 +--+----------------------------+
73 |1 | intf_id |
74 +--+----------------------------+
75
76 No overlap with UNI port number space
77
78
79PON OLT (OF) port number
80
81 OpenFlow port number corresponding to PON OLT ports
82
Amit Ghoshd4cbe482019-11-21 12:07:14 +000083 31 28 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053084 +--------+------------------------~~~------+
85 | 0x2 | pon intf id |
86 +--------+------------------------~~~------+
87*/
88
Amit Ghoshd4cbe482019-11-21 12:07:14 +000089const (
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
David K. Bainbridge794735f2020-02-11 21:01:37 -0800110 // Number of bits for NNI ID
111 bitsforNNIID = 20
112 // minNniIntPortNum is used to store start range of nni port number (1 << 20) 1048576
113 minNniIntPortNum = (1 << bitsforNNIID)
114 // maxNniPortNum is used to store the maximum range of nni port number ((1 << 21)-1) 2097151
115 maxNniPortNum = ((1 << (bitsforNNIID + 1)) - 1)
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000116)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530117
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700118//MinUpstreamPortID value
119var MinUpstreamPortID = 0xfffd
manikkaraj k17652a72019-05-06 09:06:36 -0400120
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700121//MaxUpstreamPortID value
122var MaxUpstreamPortID = 0xfffffffd
123
124var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd}
125
126//MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
Neha Sharma96b7bf22020-06-15 10:37:32 +0000127func MkUniPortNum(ctx context.Context, intfID, onuID, uniID uint32) uint32 {
gerardo.laurenzi72c84382019-07-11 15:03:46 +0000128 var limit = int(onuID)
129 if limit > MaxOnusPerPon {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000130 logger.Warn(ctx, "exceeded-the-max-onus-per-pon")
gerardo.laurenzi72c84382019-07-11 15:03:46 +0000131 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000132 return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530133}
134
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700135//OnuIDFromPortNum returns ONUID derived from portNumber
136func OnuIDFromPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000137 return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530138}
139
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140//IntfIDFromUniPortNum returns IntfID derived from portNum
141func IntfIDFromUniPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000142 return (portNum >> (bitsForUniID + bitsForONUID)) & (MaxPonsPerOlt - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530143}
144
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700145//UniIDFromPortNum return UniID derived from portNum
146func UniIDFromPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000147 return (portNum) & (MaxUnisPerOnu - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530148}
149
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700150//IntfIDToPortNo returns portId derived from intftype, intfId and portType
151func IntfIDToPortNo(intfID uint32, intfType voltha.Port_PortType) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 if (intfType) == voltha.Port_ETHERNET_NNI {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000153 return (1 << nniUniDiffPos) | intfID
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530154 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 if (intfType) == voltha.Port_PON_OLT {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000156 return (ponIntfMarkerValue << ponIntfMarkerPos) | intfID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157 }
158 return 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530159}
160
Naga Manjunath7615e552019-10-11 22:35:47 +0530161//PortNoToIntfID returns portnumber derived from interfaceID
162func PortNoToIntfID(portno uint32, intfType voltha.Port_PortType) uint32 {
163 if (intfType) == voltha.Port_ETHERNET_NNI {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000164 return (1 << nniUniDiffPos) ^ portno
Naga Manjunath7615e552019-10-11 22:35:47 +0530165 }
166 if (intfType) == voltha.Port_PON_OLT {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000167 return (ponIntfMarkerValue << ponIntfMarkerPos) ^ portno
Naga Manjunath7615e552019-10-11 22:35:47 +0530168 }
169 return 0
170}
171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172//IntfIDFromNniPortNum returns Intf ID derived from portNum
Neha Sharma96b7bf22020-06-15 10:37:32 +0000173func IntfIDFromNniPortNum(ctx context.Context, portNum uint32) (uint32, error) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800174 if portNum < minNniIntPortNum || portNum > maxNniPortNum {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Errorw(ctx, "nniportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum})
Thomas Lee S94109f12020-03-03 16:39:29 +0530176 return uint32(0), olterrors.ErrInvalidPortRange
David K. Bainbridge794735f2020-02-11 21:01:37 -0800177 }
178 return (portNum & 0xFFFF), nil
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530179}
180
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700181//IntfIDToPortTypeName returns port type derived from the intfId
182func IntfIDToPortTypeName(intfID uint32) voltha.Port_PortType {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000183 if ((ponIntfMarkerValue << ponIntfMarkerPos) ^ intfID) < MaxPonsPerOlt {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530184 return voltha.Port_PON_OLT
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530185 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000186 if (intfID & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 return voltha.Port_ETHERNET_NNI
188 }
189 return voltha.Port_ETHERNET_UNI
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530190}
191
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192//ExtractAccessFromFlow returns AccessDevice information
193func ExtractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 if IsUpstream(outPort) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 return inPort, IntfIDFromUniPortNum(inPort), OnuIDFromPortNum(inPort), UniIDFromPortNum(inPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530196 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700197 return outPort, IntfIDFromUniPortNum(outPort), OnuIDFromPortNum(outPort), UniIDFromPortNum(outPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530198}
199
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200//IsUpstream returns true for Upstream and false for downstream
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530201func IsUpstream(outPort uint32) bool {
manikkaraj k17652a72019-05-06 09:06:36 -0400202 for _, port := range controllerPorts {
203 if port == outPort {
204 return true
205 }
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530206 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000207 return (outPort & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530208}
manikkaraj k17652a72019-05-06 09:06:36 -0400209
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210//IsControllerBoundFlow returns true/false
manikkaraj k17652a72019-05-06 09:06:36 -0400211func IsControllerBoundFlow(outPort uint32) bool {
212 for _, port := range controllerPorts {
213 if port == outPort {
214 return true
215 }
216 }
217 return false
218}
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400219
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700220//OnuIDFromUniPortNum returns onuId from give portNum information.
221func OnuIDFromUniPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000222 return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400223}
224
Humera Kouser94d7a842019-08-25 19:04:32 -0400225//FlowExtractInfo fetches uniport from the flow, based on which it gets and returns ponInf, onuID, uniID, inPort and ethType
Neha Sharma96b7bf22020-06-15 10:37:32 +0000226func FlowExtractInfo(ctx context.Context, flow *ofp.OfpFlowStats, flowDirection string) (uint32, uint32, uint32, uint32, uint32, uint32, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 var uniPortNo uint32
228 var ponIntf uint32
229 var onuID uint32
230 var uniID uint32
Humera Kouser94d7a842019-08-25 19:04:32 -0400231 var inPort uint32
232 var ethType uint32
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400233
234 if flowDirection == "upstream" {
Scott Baker355d1742019-10-24 10:57:52 -0700235 if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 {
236 for _, field := range flows.GetOfbFields(flow) {
237 if field.GetType() == flows.IN_PORT {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400238 uniPortNo = field.GetPort()
239 break
240 }
241 }
242 }
243 } else if flowDirection == "downstream" {
Scott Baker355d1742019-10-24 10:57:52 -0700244 if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 {
245 for _, field := range flows.GetOfbFields(flow) {
246 if field.GetType() == flows.METADATA {
247 for _, action := range flows.GetActions(flow) {
248 if action.Type == flows.OUTPUT {
Humera Kouser94d7a842019-08-25 19:04:32 -0400249 if out := action.GetOutput(); out != nil {
250 uniPortNo = out.GetPort()
251 }
252 break
253 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400254 }
Scott Baker355d1742019-10-24 10:57:52 -0700255 } else if field.GetType() == flows.IN_PORT {
Humera Kouser94d7a842019-08-25 19:04:32 -0400256 inPort = field.GetPort()
Scott Baker355d1742019-10-24 10:57:52 -0700257 } else if field.GetType() == flows.ETH_TYPE {
Humera Kouser94d7a842019-08-25 19:04:32 -0400258 ethType = field.GetEthType()
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400259 }
260 }
261 }
262 }
263
264 if uniPortNo == 0 {
Shrey Baid26912972020-04-16 21:02:31 +0530265 return 0, 0, 0, 0, 0, 0, olterrors.NewErrNotFound("pon-interface", log.Fields{"flow-direction": flowDirection}, nil)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400266 }
267
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700268 ponIntf = IntfIDFromUniPortNum(uniPortNo)
269 onuID = OnuIDFromUniPortNum(uniPortNo)
270 uniID = UniIDFromPortNum(uniPortNo)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400271
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 logger.Debugw(ctx, "flow-extract-info-result",
Shrey Baid26912972020-04-16 21:02:31 +0530273 log.Fields{
274 "uniportno": uniPortNo,
275 "pon-intf": ponIntf,
276 "onu-id": onuID,
277 "uni-id": uniID,
278 "inport": inPort,
279 "ethtype": ethType})
Girish Gowdra3d633032019-12-10 16:37:05 +0530280
Humera Kouser94d7a842019-08-25 19:04:32 -0400281 return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400282}