blob: a7f69046c444df8099d4d5b01e8cae8d19530f39 [file] [log] [blame]
Shad Ansari22920932018-05-17 00:33:34 +00001#
2# Copyright 2018 the original author or authors.
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#
16
17from voltha.protos.device_pb2 import Port
mzadige4ad1d22018-07-27 15:27:22 -040018import voltha.protos.device_pb2 as dev_pb2
Shad Ansari22920932018-05-17 00:33:34 +000019
20"""
21Encoding of identifiers
22=======================
23
24GEM port ID
25
26 GEM port id is unique per PON port
27
28 10 3 0
29 +--+--------------+------+
30 |1 | onu id | GEM |
31 | | | idx |
32 +--+--------------+------+
33
34 GEM port id range (0, 1023) is reserved
35 onu id = 7 bits = 128 ONUs per PON
36 GEM index = 3 bits = 8 GEM ports per ONU
37
38Alloc ID
39
40 Uniquely identifies a T-CONT
41 Ranges from 0 to 4095
42 Unique per PON interface
43
44 12 6 0
45 +------------+------------+
46 | onu id | Alloc idx |
47 +------------+------------+
48
49 onu id = 7 bits = 128 ONUs per PON
50 Alloc index = 6 bits = 64 GEM ports per ONU
51
52Flow id
53
54 Identifies a flow within a single OLT
55 Flow Id is unique per OLT
56 Multiple GEM ports can map to same flow id
57
58 13 11 4 0
59 +--------+--------------+------+
60 | pon id | onu id | Flow |
61 | | | idx |
62 +--------+--------------+------+
63
64 14 bits = 16384 flows (per OLT).
65
66 pon id = 4 bits = 16 PON ports
67 onu id = 7 bits = 128 ONUss per PON port
68 Flow index = 3 bits = 4 bi-directional flows per ONU
69 = 8 uni-directional flows per ONU
70
71
72Logical (OF) UNI port number
73
74 OpenFlow port number corresponding to PON UNI
75
76 15 11 4 0
77 +--+--------+--------------+------+
78 |0 | pon id | onu id | 0 |
79 +--+--------+--------------+------+
80
81 pon id = 4 bits = 16 PON ports
82 onu id = 7 bits = 128 ONUs per PON port
83
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040084Logical (OF) NNI port number
85
86 OpenFlow port number corresponding to PON UNI
87
88 16 0
89 +--+----------------------------+
90 |1 | intf_id |
91 +--+----------------------------+
92
93 No overlap with UNI port number space
94
Shad Ansari22920932018-05-17 00:33:34 +000095
96PON OLT (OF) port number
97
98 OpenFlow port number corresponding to PON OLT ports
99
100 31 28 0
101 +--------+------------------------~~~------+
102 | 0x2 | pon intf id |
103 +--------+------------------------~~~------+
104
105"""
106
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400107MAX_ONUS_PER_PON = 112
Shad Ansarif9d2d102018-06-13 02:15:26 +0000108
Shad Ansari22920932018-05-17 00:33:34 +0000109def mk_uni_port_num(intf_id, onu_id):
110 return intf_id << 11 | onu_id << 4
111
Shad Ansarif9d2d102018-06-13 02:15:26 +0000112
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400113def mk_alloc_id(intf_id, onu_id, idx=0):
Shad Ansari22920932018-05-17 00:33:34 +0000114 # FIXME - driver should do prefixing 1 << 10 as it is Maple specific
Shad Ansarif9d2d102018-06-13 02:15:26 +0000115 # return 1<<10 | onu_id<<6 | idx
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400116 return 1023 + intf_id * MAX_ONUS_PER_PON + onu_id # FIXME
Shad Ansarif9d2d102018-06-13 02:15:26 +0000117
Shad Ansari22920932018-05-17 00:33:34 +0000118
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400119def mk_gemport_id(intf_id, onu_id, idx=0):
120 return 1024 + (((MAX_ONUS_PER_PON * intf_id + onu_id - 1) * 7) + idx)
Shad Ansari22920932018-05-17 00:33:34 +0000121
122def onu_id_from_gemport_id(gemport_id):
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400123 return (((gemport_id - 1024) // 7) % MAX_ONUS_PER_PON) + 1
Shad Ansarif9d2d102018-06-13 02:15:26 +0000124
Shad Ansari22920932018-05-17 00:33:34 +0000125
126def mk_flow_id(intf_id, onu_id, idx):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000127 return intf_id << 11 | onu_id << 4 | idx
128
Shad Ansari22920932018-05-17 00:33:34 +0000129
130def onu_id_from_port_num(port_num):
131 return (port_num >> 4) & 0x7F
132
Shad Ansarif9d2d102018-06-13 02:15:26 +0000133
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400134def intf_id_from_uni_port_num(port_num):
Shad Ansari22920932018-05-17 00:33:34 +0000135 return (port_num >> 11) & 0xF
136
Shad Ansarif9d2d102018-06-13 02:15:26 +0000137
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400138def intf_id_from_pon_port_no(port_no):
139 return port_no & 0xF
140
Shad Ansarif9d2d102018-06-13 02:15:26 +0000141
Shad Ansari22920932018-05-17 00:33:34 +0000142def intf_id_to_port_no(intf_id, intf_type):
143 if intf_type is Port.ETHERNET_NNI:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400144 return (0x1 << 16) | intf_id
Shad Ansari22920932018-05-17 00:33:34 +0000145 elif intf_type is Port.PON_OLT:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000146 return 0x2 << 28 | intf_id
Shad Ansari22920932018-05-17 00:33:34 +0000147 else:
148 raise Exception('Invalid port type')
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400149
Shad Ansarif9d2d102018-06-13 02:15:26 +0000150
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400151def intf_id_from_nni_port_num(port_num):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400152 return port_num & 0xFFFF
mzadige4ad1d22018-07-27 15:27:22 -0400153
mzadige4ad1d22018-07-27 15:27:22 -0400154
155def intf_id_to_port_type_name(intf_id):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400156 if (2 << 28 ^ intf_id) < 16:
157 return Port.PON_OLT
158 elif intf_id & (0x1 << 16) == (0x1 << 16):
159 return Port.ETHERNET_NNI
160 else:
161 return None
mzadige4ad1d22018-07-27 15:27:22 -0400162
163def port_type_name_by_port_index(port_index):
164 try:
165 return dev_pb2._PORT_PORTTYPE.values_by_number[port_index].name
166 except Exception as err:
167 raise Exception(err)
168
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400169def extract_access_from_flow(in_port, out_port):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400170 if is_upstream(out_port):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400171 return (intf_id_from_uni_port_num(in_port), onu_id_from_port_num(
172 in_port))
173 else:
174 return (intf_id_from_uni_port_num(out_port), onu_id_from_port_num(
175 out_port))
176
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400177def is_upstream(out_port):
178
179 if out_port in [0xfffd, 0xfffffffd]:
180 # To Controller
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400181 return True
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400182 if (out_port & (0x1 << 16)) == (0x1 << 16):
183 # NNI interface
184 return True
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400185
186 return False