blob: 60b9bb819c118b2edab1cd3d5aeebb7ece46b4ac [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 */
16package adaptercore
Girish Gowdru0c588b22019-04-23 23:24:56 -040017
18import (
19 voltha "github.com/opencord/voltha-protos/go/voltha"
20)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053021
22/*=====================================================================
23
24Flow id
25
26 Identifies a flow within a single OLT
27 Flow Id is unique per OLT
28 Multiple GEM ports can map to same flow id
29
30 13 11 4 0
31 +--------+--------------+------+
32 | pon id | onu id | Flow |
33 | | | idx |
34 +--------+--------------+------+
35
36 14 bits = 16384 flows (per OLT).
37
38 pon id = 4 bits = 16 PON ports
39 onu id = 7 bits = 128 ONUss per PON port
40 Flow index = 3 bits = 4 bi-directional flows per ONU
41 = 8 uni-directional flows per ONU
42
43
44Logical (OF) UNI port number
45
46 OpenFlow port number corresponding to PON UNI
47
48 15 11 4 0
49 +--+--------+--------------+------+
50 |0 | pon id | onu id | 0 |
51 +--+--------+--------------+------+
52
53 pon id = 4 bits = 16 PON ports
54 onu id = 7 bits = 128 ONUs per PON port
55
56Logical (OF) NNI port number
57
58 OpenFlow port number corresponding to PON UNI
59
60 16 0
61 +--+----------------------------+
62 |1 | intf_id |
63 +--+----------------------------+
64
65 No overlap with UNI port number space
66
67
68PON OLT (OF) port number
69
70 OpenFlow port number corresponding to PON OLT ports
71
72 31 28 0
73 +--------+------------------------~~~------+
74 | 0x2 | pon intf id |
75 +--------+------------------------~~~------+
76*/
77
Girish Gowdru0c588b22019-04-23 23:24:56 -040078var MAX_ONUS_PER_PON = 32
79var MIN_UPSTREAM_PORT_ID = 0xfffd
80var MAX_UPSTREAM_PORT_ID = 0xfffffffd
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053081
82func MkUniPortNum(intfId uint32, onuId uint32, uniId uint32) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -040083 /* TODO: Add checks */
84 return ((intfId << 11) | (onuId << 4) | uniId)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053085}
86
87func MkFlowId(intfId uint32, onuId uint32, idx uint32) uint32 {
88 return (((intfId << 9) | (onuId << 4)) | idx)
89}
90
91func OnuIdFromPortNum(portNum uint32) uint32 {
92 return ((portNum >> 4) & 127)
93}
94
95func IntfIdFromUniPortNum(portNum uint32) uint32 {
96 return ((portNum >> 11) & 15)
97}
98
99func UniIdFromPortNum(portNum uint32) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400100 return ((portNum) & 0xF)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530101}
102
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530103func IntfIdFromPonPortNo(portNo uint32) uint32 {
104 return (portNo & 15)
105}
106
107func IntfIdToPortNo(intfId uint32, intfType voltha.Port_PortType) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400108 if (intfType) == voltha.Port_ETHERNET_NNI {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530109 return ((1 << 16) | intfId)
110 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400111 if (intfType) == voltha.Port_PON_OLT {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530112 return ((2 << 28) | intfId)
113 } else {
114 return 0
115 }
116 }
117}
118
119func IntfIdFromNniPortNum(portNum uint32) uint32 {
120 return (portNum & 0xFFFF)
121}
122
123func IntfIdToPortTypeName(intfId uint32) voltha.Port_PortType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400124 if ((2 << 28) ^ intfId) < 16 {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530125 return voltha.Port_PON_OLT
126 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 if (intfId & (1 << 16)) == (1 << 16) {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530128 return voltha.Port_ETHERNET_NNI
129 } else {
130 return voltha.Port_UNKNOWN
131 }
132 }
133}
134
135func PortTypeNameByPortIndex(portIndex int32) string {
136 return voltha.Port_PortType_name[portIndex]
137}
138
139func ExtractAccessFromFlow(inPort uint32, outPort uint32) (uint32, uint32, uint32, uint32) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 if IsUpstream(outPort) {
141 return inPort, IntfIdFromUniPortNum(inPort), OnuIdFromPortNum(inPort), UniIdFromPortNum(inPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530142 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 return outPort, IntfIdFromUniPortNum(outPort), OnuIdFromPortNum(outPort), UniIdFromPortNum(outPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530144 }
145}
146
147func IsUpstream(outPort uint32) bool {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 if (outPort >= uint32(MIN_UPSTREAM_PORT_ID)) && (outPort <= uint32(MAX_UPSTREAM_PORT_ID)) {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530149 return true
150 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 if (outPort & (1 << 16)) == (1 << 16) {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530152 return true
153 }
154 return false
155}