blob: 4cc4765d18ec222201f4bbe411333c3da2643e97 [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
manikkaraj k17652a72019-05-06 09:06:36 -040082var controllerPorts []uint32 = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd}
83
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053084func MkUniPortNum(intfId uint32, onuId uint32, uniId uint32) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -040085 /* TODO: Add checks */
86 return ((intfId << 11) | (onuId << 4) | uniId)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +053087}
88
89func MkFlowId(intfId uint32, onuId uint32, idx uint32) uint32 {
90 return (((intfId << 9) | (onuId << 4)) | idx)
91}
92
93func OnuIdFromPortNum(portNum uint32) uint32 {
94 return ((portNum >> 4) & 127)
95}
96
97func IntfIdFromUniPortNum(portNum uint32) uint32 {
98 return ((portNum >> 11) & 15)
99}
100
101func UniIdFromPortNum(portNum uint32) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400102 return ((portNum) & 0xF)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530103}
104
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530105func IntfIdFromPonPortNo(portNo uint32) uint32 {
106 return (portNo & 15)
107}
108
109func IntfIdToPortNo(intfId uint32, intfType voltha.Port_PortType) uint32 {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400110 if (intfType) == voltha.Port_ETHERNET_NNI {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530111 return ((1 << 16) | intfId)
112 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 if (intfType) == voltha.Port_PON_OLT {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530114 return ((2 << 28) | intfId)
115 } else {
116 return 0
117 }
118 }
119}
120
121func IntfIdFromNniPortNum(portNum uint32) uint32 {
122 return (portNum & 0xFFFF)
123}
124
125func IntfIdToPortTypeName(intfId uint32) voltha.Port_PortType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126 if ((2 << 28) ^ intfId) < 16 {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530127 return voltha.Port_PON_OLT
128 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 if (intfId & (1 << 16)) == (1 << 16) {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530130 return voltha.Port_ETHERNET_NNI
131 } else {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400132 return voltha.Port_ETHERNET_UNI
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530133 }
134 }
135}
136
137func PortTypeNameByPortIndex(portIndex int32) string {
138 return voltha.Port_PortType_name[portIndex]
139}
140
141func ExtractAccessFromFlow(inPort uint32, outPort uint32) (uint32, uint32, uint32, uint32) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 if IsUpstream(outPort) {
143 return inPort, IntfIdFromUniPortNum(inPort), OnuIdFromPortNum(inPort), UniIdFromPortNum(inPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530144 } else {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 return outPort, IntfIdFromUniPortNum(outPort), OnuIdFromPortNum(outPort), UniIdFromPortNum(outPort)
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530146 }
147}
148
149func IsUpstream(outPort uint32) bool {
manikkaraj k17652a72019-05-06 09:06:36 -0400150 for _, port := range controllerPorts {
151 if port == outPort {
152 return true
153 }
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530154 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 if (outPort & (1 << 16)) == (1 << 16) {
Manjunath Vanaraj39ecd412019-03-15 20:05:41 +0530156 return true
157 }
158 return false
159}
manikkaraj k17652a72019-05-06 09:06:36 -0400160
161func IsControllerBoundFlow(outPort uint32) bool {
162 for _, port := range controllerPorts {
163 if port == outPort {
164 return true
165 }
166 }
167 return false
168}