blob: 470d4a9e095243ab2849314a71340d3c297c1ed7 [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
Mahir Gunyel85f61c12021-10-06 11:53:45 -070017package platform
Girish Gowdru0c588b22019-04-23 23:24:56 -040018
19import (
Neha Sharma96b7bf22020-06-15 10:37:32 +000020 "context"
Kent Hagermane6ff1012020-07-14 15:07:53 -040021
khenaidoo106c61a2021-08-11 18:05:46 -040022 "github.com/opencord/voltha-lib-go/v7/pkg/flows"
23 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo106c61a2021-08-11 18:05:46 -040024 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
25 "github.com/opencord/voltha-protos/v5/go/voltha"
Mahir Gunyel85f61c12021-10-06 11:53:45 -070026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
Girish Gowdru0c588b22019-04-23 23:24:56 -040028)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053029
30/*=====================================================================
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053031Logical (OF) UNI port number
32
33 OpenFlow port number corresponding to PON UNI
34
Amit Ghoshd4cbe482019-11-21 12:07:14 +000035 20 12 4 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053036 +--+--------+--------------+------+
Amit Ghoshd4cbe482019-11-21 12:07:14 +000037 |0 | pon id | onu id |uni id|
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053038 +--+--------+--------------+------+
39
Amit Ghoshd4cbe482019-11-21 12:07:14 +000040 pon id = 8 bits = 256 PON ports
41 onu id = 8 bits = 256 ONUs per PON port
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053042
43Logical (OF) NNI port number
44
Amit Ghoshd4cbe482019-11-21 12:07:14 +000045 OpenFlow port number corresponding to PON NNI
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053046
Amit Ghoshd4cbe482019-11-21 12:07:14 +000047 20 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053048 +--+----------------------------+
49 |1 | intf_id |
50 +--+----------------------------+
51
52 No overlap with UNI port number space
53
54
55PON OLT (OF) port number
56
57 OpenFlow port number corresponding to PON OLT ports
58
Amit Ghoshd4cbe482019-11-21 12:07:14 +000059 31 28 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053060 +--------+------------------------~~~------+
61 | 0x2 | pon intf id |
62 +--------+------------------------~~~------+
63*/
64
Amit Ghoshd4cbe482019-11-21 12:07:14 +000065const (
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
Kent Hagermane6ff1012020-07-14 15:07:53 -040072 // MaxOnusPerPon is Max number of ONUs on any PON port
Amit Ghoshd4cbe482019-11-21 12:07:14 +000073 MaxOnusPerPon = (1 << bitsForONUID)
Kent Hagermane6ff1012020-07-14 15:07:53 -040074 // MaxPonsPerOlt is Max number of PON ports on any OLT
Amit Ghoshd4cbe482019-11-21 12:07:14 +000075 MaxPonsPerOlt = (1 << bitsForPONID)
Kent Hagermane6ff1012020-07-14 15:07:53 -040076 // MaxUnisPerOnu is the Max number of UNI ports on any ONU
Amit Ghoshd4cbe482019-11-21 12:07:14 +000077 MaxUnisPerOnu = (1 << bitsForUniID)
Kent Hagermane6ff1012020-07-14 15:07:53 -040078 // Bit position where the differentiation bit is located
Amit Ghoshd4cbe482019-11-21 12:07:14 +000079 nniUniDiffPos = (bitsForUniID + bitsForONUID + bitsForPONID)
Kent Hagermane6ff1012020-07-14 15:07:53 -040080 // Bit position where the marker for PON port type of OF port is present
Amit Ghoshd4cbe482019-11-21 12:07:14 +000081 ponIntfMarkerPos = 28
Kent Hagermane6ff1012020-07-14 15:07:53 -040082 // Value of marker used to distinguish PON port type of OF port
Amit Ghoshd4cbe482019-11-21 12:07:14 +000083 ponIntfMarkerValue = 0x2
David K. Bainbridge794735f2020-02-11 21:01:37 -080084 // 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)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070090 // minPonIntfPortNum stores the minimum pon port number
91 minPonIntfPortNum = ponIntfMarkerValue << ponIntfMarkerPos
92 // maxPonIntfPortNum stores the maximum pon port number
Andrea Campanella890ae922021-06-28 15:25:42 +020093 maxPonIntfPortNum = (ponIntfMarkerValue << ponIntfMarkerPos) | (1 << bitsForPONID)
Mahir Gunyel85f61c12021-10-06 11:53:45 -070094 upstream = "upstream"
95 downstream = "downstream"
Amit Ghoshd4cbe482019-11-21 12:07:14 +000096)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053097
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070098//MinUpstreamPortID value
99var MinUpstreamPortID = 0xfffd
manikkaraj k17652a72019-05-06 09:06:36 -0400100
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700101//MaxUpstreamPortID value
102var MaxUpstreamPortID = 0xfffffffd
103
104var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd}
105
106//MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
Neha Sharma96b7bf22020-06-15 10:37:32 +0000107func MkUniPortNum(ctx context.Context, intfID, onuID, uniID uint32) uint32 {
gerardo.laurenzi72c84382019-07-11 15:03:46 +0000108 var limit = int(onuID)
109 if limit > MaxOnusPerPon {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000110 logger.Warn(ctx, "exceeded-the-max-onus-per-pon")
gerardo.laurenzi72c84382019-07-11 15:03:46 +0000111 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000112 return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530113}
114
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700115//OnuIDFromPortNum returns ONUID derived from portNumber
116func OnuIDFromPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000117 return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530118}
119
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700120//IntfIDFromUniPortNum returns IntfID derived from portNum
121func IntfIDFromUniPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000122 return (portNum >> (bitsForUniID + bitsForONUID)) & (MaxPonsPerOlt - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530123}
124
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700125//UniIDFromPortNum return UniID derived from portNum
126func UniIDFromPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000127 return (portNum) & (MaxUnisPerOnu - 1)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530128}
129
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700130//IntfIDToPortNo returns portId derived from intftype, intfId and portType
131func IntfIDToPortNo(intfID uint32, intfType voltha.Port_PortType) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132 if (intfType) == voltha.Port_ETHERNET_NNI {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000133 return (1 << nniUniDiffPos) | intfID
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530134 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700135 if (intfType) == voltha.Port_PON_OLT {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000136 return (ponIntfMarkerValue << ponIntfMarkerPos) | intfID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 }
138 return 0
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530139}
140
Naga Manjunath7615e552019-10-11 22:35:47 +0530141//PortNoToIntfID returns portnumber derived from interfaceID
142func PortNoToIntfID(portno uint32, intfType voltha.Port_PortType) uint32 {
143 if (intfType) == voltha.Port_ETHERNET_NNI {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000144 return (1 << nniUniDiffPos) ^ portno
Naga Manjunath7615e552019-10-11 22:35:47 +0530145 }
146 if (intfType) == voltha.Port_PON_OLT {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000147 return (ponIntfMarkerValue << ponIntfMarkerPos) ^ portno
Naga Manjunath7615e552019-10-11 22:35:47 +0530148 }
149 return 0
150}
151
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152//IntfIDFromNniPortNum returns Intf ID derived from portNum
Neha Sharma96b7bf22020-06-15 10:37:32 +0000153func IntfIDFromNniPortNum(ctx context.Context, portNum uint32) (uint32, error) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800154 if portNum < minNniIntPortNum || portNum > maxNniPortNum {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000155 logger.Errorw(ctx, "nniportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum})
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700156 return uint32(0), status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", portNum)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800157 }
158 return (portNum & 0xFFFF), nil
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530159}
160
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700161//IntfIDFromPonPortNum returns Intf ID derived from portNum
162func 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})
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700165 return uint32(0), status.Errorf(codes.InvalidArgument, "invalid-pon-port-number:%d", portNum)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700166 }
167 return (portNum & 0x7FFF), nil
168}
169
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700170//IntfIDToPortTypeName returns port type derived from the intfId
171func IntfIDToPortTypeName(intfID uint32) voltha.Port_PortType {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000172 if ((ponIntfMarkerValue << ponIntfMarkerPos) ^ intfID) < MaxPonsPerOlt {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530173 return voltha.Port_PON_OLT
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530174 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000175 if (intfID & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700176 return voltha.Port_ETHERNET_NNI
177 }
178 return voltha.Port_ETHERNET_UNI
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530179}
180
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700181//ExtractAccessFromFlow returns AccessDevice information
182func ExtractAccessFromFlow(inPort, outPort uint32) (uint32, uint32, uint32, uint32) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400183 if IsUpstream(outPort) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700184 return inPort, IntfIDFromUniPortNum(inPort), OnuIDFromPortNum(inPort), UniIDFromPortNum(inPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530185 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 return outPort, IntfIDFromUniPortNum(outPort), OnuIDFromPortNum(outPort), UniIDFromPortNum(outPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530187}
188
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700189//IsUpstream returns true for Upstream and false for downstream
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530190func IsUpstream(outPort uint32) bool {
manikkaraj k17652a72019-05-06 09:06:36 -0400191 for _, port := range controllerPorts {
192 if port == outPort {
193 return true
194 }
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530195 }
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000196 return (outPort & (1 << nniUniDiffPos)) == (1 << nniUniDiffPos)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530197}
manikkaraj k17652a72019-05-06 09:06:36 -0400198
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700199//IsControllerBoundFlow returns true/false
manikkaraj k17652a72019-05-06 09:06:36 -0400200func IsControllerBoundFlow(outPort uint32) bool {
201 for _, port := range controllerPorts {
202 if port == outPort {
203 return true
204 }
205 }
206 return false
207}
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400208
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700209//OnuIDFromUniPortNum returns onuId from give portNum information.
210func OnuIDFromUniPortNum(portNum uint32) uint32 {
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000211 return (portNum >> bitsForUniID) & (MaxOnusPerPon - 1)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400212}
213
Humera Kouser94d7a842019-08-25 19:04:32 -0400214//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 +0000215func FlowExtractInfo(ctx context.Context, flow *ofp.OfpFlowStats, flowDirection string) (uint32, uint32, uint32, uint32, uint32, uint32, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 var uniPortNo uint32
217 var ponIntf uint32
218 var onuID uint32
219 var uniID uint32
Humera Kouser94d7a842019-08-25 19:04:32 -0400220 var inPort uint32
221 var ethType uint32
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400222
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700223 if flowDirection == upstream {
Scott Baker355d1742019-10-24 10:57:52 -0700224 if uniPortNo = flows.GetChildPortFromTunnelId(flow); uniPortNo == 0 {
225 for _, field := range flows.GetOfbFields(flow) {
226 if field.GetType() == flows.IN_PORT {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400227 uniPortNo = field.GetPort()
228 break
229 }
230 }
231 }
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700232 } else if flowDirection == downstream {
Scott Baker355d1742019-10-24 10:57:52 -0700233 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 {
Humera Kouser94d7a842019-08-25 19:04:32 -0400238 if out := action.GetOutput(); out != nil {
239 uniPortNo = out.GetPort()
240 }
241 break
242 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400243 }
Scott Baker355d1742019-10-24 10:57:52 -0700244 } else if field.GetType() == flows.IN_PORT {
Humera Kouser94d7a842019-08-25 19:04:32 -0400245 inPort = field.GetPort()
Scott Baker355d1742019-10-24 10:57:52 -0700246 } else if field.GetType() == flows.ETH_TYPE {
Humera Kouser94d7a842019-08-25 19:04:32 -0400247 ethType = field.GetEthType()
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400248 }
249 }
250 }
251 }
252
253 if uniPortNo == 0 {
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700254 return 0, 0, 0, 0, 0, 0, status.Errorf(codes.NotFound, "uni-not-found-flow-diraction:%s", flowDirection)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400255 }
256
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 ponIntf = IntfIDFromUniPortNum(uniPortNo)
258 onuID = OnuIDFromUniPortNum(uniPortNo)
259 uniID = UniIDFromPortNum(uniPortNo)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400260
Neha Sharma96b7bf22020-06-15 10:37:32 +0000261 logger.Debugw(ctx, "flow-extract-info-result",
Shrey Baid26912972020-04-16 21:02:31 +0530262 log.Fields{
263 "uniportno": uniPortNo,
264 "pon-intf": ponIntf,
265 "onu-id": onuID,
266 "uni-id": uniID,
267 "inport": inPort,
268 "ethtype": ethType})
Girish Gowdra3d633032019-12-10 16:37:05 +0530269
Humera Kouser94d7a842019-08-25 19:04:32 -0400270 return uniPortNo, ponIntf, onuID, uniID, inPort, ethType, nil
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400271}