blob: a9dc3fd16c7d518b0eff0d858ed9a9dadb9b7cd0 [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=======================
Girish Gowdru1a3b7042018-09-19 07:08:48 -070023GEM port ID
24
25 GEM port id is unique per PON port
26
27 10 3 0
28 +--+--------------+------+
29 |1 | onu id | GEM |
30 | | | idx |
31 +--+--------------+------+
32
33 GEM port id range (0, 1023) is reserved
34 onu id = 7 bits = 128 ONUs per PON
35 GEM index = 3 bits = 8 GEM ports per ONU
36
37Alloc ID
38
39 Uniquely identifies a T-CONT
40 Ranges from 0 to 4095
41 Unique per PON interface
42
43 12 6 0
44 +------------+------------+
45 | onu id | Alloc idx |
46 +------------+------------+
47
48 onu id = 7 bits = 128 ONUs per PON
49 Alloc index = 6 bits = 64 GEM ports per ONU
Shad Ansari22920932018-05-17 00:33:34 +000050
Shad Ansari22920932018-05-17 00:33:34 +000051Flow id
52
53 Identifies a flow within a single OLT
54 Flow Id is unique per OLT
55 Multiple GEM ports can map to same flow id
56
57 13 11 4 0
58 +--------+--------------+------+
59 | pon id | onu id | Flow |
60 | | | idx |
61 +--------+--------------+------+
62
63 14 bits = 16384 flows (per OLT).
64
65 pon id = 4 bits = 16 PON ports
66 onu id = 7 bits = 128 ONUss per PON port
67 Flow index = 3 bits = 4 bi-directional flows per ONU
68 = 8 uni-directional flows per ONU
69
70
71Logical (OF) UNI port number
72
73 OpenFlow port number corresponding to PON UNI
74
75 15 11 4 0
76 +--+--------+--------------+------+
77 |0 | pon id | onu id | 0 |
78 +--+--------+--------------+------+
79
80 pon id = 4 bits = 16 PON ports
81 onu id = 7 bits = 128 ONUs per PON port
82
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040083Logical (OF) NNI port number
84
85 OpenFlow port number corresponding to PON UNI
86
87 16 0
88 +--+----------------------------+
89 |1 | intf_id |
90 +--+----------------------------+
91
92 No overlap with UNI port number space
93
Shad Ansari22920932018-05-17 00:33:34 +000094
95PON OLT (OF) port number
96
97 OpenFlow port number corresponding to PON OLT ports
98
99 31 28 0
100 +--------+------------------------~~~------+
101 | 0x2 | pon intf id |
102 +--------+------------------------~~~------+
103
104"""
105
Shad Ansari9712dc92018-09-26 17:46:00 +0000106# MAX_ONUS_PER_PON = 112
107MAX_ONUS_PER_PON = 32
Shad Ansarif9d2d102018-06-13 02:15:26 +0000108
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700109def mk_alloc_id(intf_id, onu_id, idx=0):
110 # FIXME - driver should do prefixing 1 << 10 as it is Maple specific
111 # return 1<<10 | onu_id<<6 | idx
112 return 1023 + intf_id * MAX_ONUS_PER_PON + onu_id # FIXME
113
114
115def mk_gemport_id(intf_id, onu_id, idx=0):
116 return 1024 + (((MAX_ONUS_PER_PON * intf_id + onu_id - 1) * 7) + idx)
117
118def onu_id_from_gemport_id(gemport_id):
119 return (((gemport_id - 1024) // 7) % MAX_ONUS_PER_PON) + 1
120
Shad Ansari22920932018-05-17 00:33:34 +0000121def mk_uni_port_num(intf_id, onu_id):
122 return intf_id << 11 | onu_id << 4
123
Shad Ansari22920932018-05-17 00:33:34 +0000124def mk_flow_id(intf_id, onu_id, idx):
Shad Ansari9712dc92018-09-26 17:46:00 +0000125 return intf_id << 9 | onu_id << 4 | idx
Shad Ansarif9d2d102018-06-13 02:15:26 +0000126
Shad Ansari22920932018-05-17 00:33:34 +0000127
128def onu_id_from_port_num(port_num):
129 return (port_num >> 4) & 0x7F
130
Shad Ansarif9d2d102018-06-13 02:15:26 +0000131
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400132def intf_id_from_uni_port_num(port_num):
Shad Ansari22920932018-05-17 00:33:34 +0000133 return (port_num >> 11) & 0xF
134
Shad Ansarif9d2d102018-06-13 02:15:26 +0000135
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400136def intf_id_from_pon_port_no(port_no):
137 return port_no & 0xF
138
Shad Ansarif9d2d102018-06-13 02:15:26 +0000139
Shad Ansari22920932018-05-17 00:33:34 +0000140def intf_id_to_port_no(intf_id, intf_type):
141 if intf_type is Port.ETHERNET_NNI:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400142 return (0x1 << 16) | intf_id
Shad Ansari22920932018-05-17 00:33:34 +0000143 elif intf_type is Port.PON_OLT:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000144 return 0x2 << 28 | intf_id
Shad Ansari22920932018-05-17 00:33:34 +0000145 else:
146 raise Exception('Invalid port type')
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400147
Shad Ansarif9d2d102018-06-13 02:15:26 +0000148
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400149def intf_id_from_nni_port_num(port_num):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400150 return port_num & 0xFFFF
mzadige4ad1d22018-07-27 15:27:22 -0400151
mzadige4ad1d22018-07-27 15:27:22 -0400152
153def intf_id_to_port_type_name(intf_id):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400154 if (2 << 28 ^ intf_id) < 16:
155 return Port.PON_OLT
156 elif intf_id & (0x1 << 16) == (0x1 << 16):
157 return Port.ETHERNET_NNI
158 else:
159 return None
mzadige4ad1d22018-07-27 15:27:22 -0400160
161def port_type_name_by_port_index(port_index):
162 try:
163 return dev_pb2._PORT_PORTTYPE.values_by_number[port_index].name
164 except Exception as err:
165 raise Exception(err)
166
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400167def extract_access_from_flow(in_port, out_port):
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400168 if is_upstream(out_port):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400169 return (intf_id_from_uni_port_num(in_port), onu_id_from_port_num(
170 in_port))
171 else:
172 return (intf_id_from_uni_port_num(out_port), onu_id_from_port_num(
173 out_port))
174
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400175def is_upstream(out_port):
176
177 if out_port in [0xfffd, 0xfffffffd]:
178 # To Controller
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400179 return True
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400180 if (out_port & (0x1 << 16)) == (0x1 << 16):
181 # NNI interface
182 return True
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400183
184 return False
Girish Gowdru141ced82018-09-17 20:19:14 -0700185