blob: 554e88d488927e58d6a756d1c4fc67f4478ad174 [file] [log] [blame]
Girish Gowdraddf9a162020-01-27 12:56:27 +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 */
16
17#include "core_utils.h"
18
19std::string serial_number_to_str(bcmolt_serial_number* serial_number) {
20#define SERIAL_NUMBER_SIZE 12
21 char buff[SERIAL_NUMBER_SIZE+1];
22
23 sprintf(buff, "%c%c%c%c%1X%1X%1X%1X%1X%1X%1X%1X",
24 serial_number->vendor_id.arr[0],
25 serial_number->vendor_id.arr[1],
26 serial_number->vendor_id.arr[2],
27 serial_number->vendor_id.arr[3],
28 serial_number->vendor_specific.arr[0]>>4 & 0x0f,
29 serial_number->vendor_specific.arr[0] & 0x0f,
30 serial_number->vendor_specific.arr[1]>>4 & 0x0f,
31 serial_number->vendor_specific.arr[1] & 0x0f,
32 serial_number->vendor_specific.arr[2]>>4 & 0x0f,
33 serial_number->vendor_specific.arr[2] & 0x0f,
34 serial_number->vendor_specific.arr[3]>>4 & 0x0f,
35 serial_number->vendor_specific.arr[3] & 0x0f);
36
37 return buff;
38}
39
40std::string vendor_specific_to_str(char const * const vendor_specific) {
41 char buff[SERIAL_NUMBER_SIZE+1];
42
43 sprintf(buff, "%1X%1X%1X%1X%1X%1X%1X%1X",
44 vendor_specific[0]>>4 & 0x0f,
45 vendor_specific[0] & 0x0f,
46 vendor_specific[1]>>4 & 0x0f,
47 vendor_specific[1] & 0x0f,
48 vendor_specific[2]>>4 & 0x0f,
49 vendor_specific[2] & 0x0f,
50 vendor_specific[3]>>4 & 0x0f,
51 vendor_specific[3] & 0x0f);
52
53 return buff;
54}
55/**
56* Returns the default NNI (Upstream direction) or PON (Downstream direction) scheduler
57* Every NNI port and PON port have default scheduler.
58* The NNI0 default scheduler ID is 18432, and NNI1 is 18433 and so on.
59* Similarly, PON0 default scheduler ID is 16384. PON1 is 16385 and so on.
60*
61* @param intf_id NNI or PON interface ID
62* @param direction "upstream" or "downstream"
63*
64* @return default scheduler ID for the given interface.
65*/
66
67uint16_t get_dev_id(void) {
68 return dev_id;
69}
70
71int get_default_tm_sched_id(int intf_id, std::string direction) {
72 if (direction.compare(upstream) == 0) {
73 return tm_upstream_sched_id_start + intf_id;
74 } else if (direction.compare(downstream) == 0) {
75 return tm_downstream_sched_id_start + intf_id;
76 }
77 else {
78 OPENOLT_LOG(ERROR, openolt_log_id, "invalid direction - %s\n", direction.c_str());
79 return 0;
80 }
81}
82
83/**
84* Gets a unique tm_sched_id for a given intf_id, onu_id, uni_id, gemport_id, direction
85* The tm_sched_id is locally cached in a map, so that it can rendered when necessary.
Burak Gurdag2f2618c2020-04-23 13:20:30 +000086* VOLTHA replays whole configuration on OLT reboot, so caching locally is not a problem.
87* Note that tech_profile_id is used to differentiate service schedulers in downstream direction.
Girish Gowdraddf9a162020-01-27 12:56:27 +053088*
89* @param intf_id NNI or PON intf ID
90* @param onu_id ONU ID
91* @param uni_id UNI ID
92* @param gemport_id GEM Port ID
93* @param direction Upstream or downstream
Burak Gurdag2f2618c2020-04-23 13:20:30 +000094* @param tech_profile_id Technology Profile ID
Girish Gowdraddf9a162020-01-27 12:56:27 +053095*
96* @return tm_sched_id
97*/
Burak Gurdag2f2618c2020-04-23 13:20:30 +000098uint32_t get_tm_sched_id(int pon_intf_id, int onu_id, int uni_id, std::string direction, int tech_profile_id) {
99 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction, tech_profile_id);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530100 int sched_id = -1;
101
Girish Gowdra252f4972020-09-07 21:24:01 -0700102 bcmos_fastlock_lock(&tm_sched_bitset_lock);
103
Girish Gowdraddf9a162020-01-27 12:56:27 +0530104 std::map<sched_map_key_tuple, int>::const_iterator it = sched_map.find(key);
105 if (it != sched_map.end()) {
106 sched_id = it->second;
107 }
108 if (sched_id != -1) {
Girish Gowdra252f4972020-09-07 21:24:01 -0700109 bcmos_fastlock_unlock(&tm_sched_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530110 return sched_id;
111 }
112
Girish Gowdraddf9a162020-01-27 12:56:27 +0530113 // Complexity of O(n). Is there better way that can avoid linear search?
114 for (sched_id = 0; sched_id < MAX_TM_SCHED_ID; sched_id++) {
115 if (tm_sched_bitset[sched_id] == 0) {
116 tm_sched_bitset[sched_id] = 1;
117 break;
118 }
119 }
Girish Gowdraddf9a162020-01-27 12:56:27 +0530120
121 if (sched_id < MAX_TM_SCHED_ID) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530122 sched_map[key] = sched_id;
Girish Gowdra252f4972020-09-07 21:24:01 -0700123 bcmos_fastlock_unlock(&tm_sched_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530124 return sched_id;
125 } else {
Girish Gowdra252f4972020-09-07 21:24:01 -0700126 bcmos_fastlock_unlock(&tm_sched_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530127 return -1;
128 }
129}
130
131/**
Burak Gurdag2f2618c2020-04-23 13:20:30 +0000132* Free tm_sched_id for a given intf_id, onu_id, uni_id, gemport_id, direction, tech_profile_id
Girish Gowdraddf9a162020-01-27 12:56:27 +0530133*
134* @param intf_id NNI or PON intf ID
135* @param onu_id ONU ID
136* @param uni_id UNI ID
137* @param gemport_id GEM Port ID
138* @param direction Upstream or downstream
Burak Gurdag2f2618c2020-04-23 13:20:30 +0000139* @param tech_profile_id Technology Profile ID
Girish Gowdraddf9a162020-01-27 12:56:27 +0530140*/
Burak Gurdag2f2618c2020-04-23 13:20:30 +0000141void free_tm_sched_id(int pon_intf_id, int onu_id, int uni_id, std::string direction, int tech_profile_id) {
142 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction, tech_profile_id);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530143 std::map<sched_map_key_tuple, int>::const_iterator it;
Girish Gowdra252f4972020-09-07 21:24:01 -0700144 bcmos_fastlock_lock(&tm_sched_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530145 it = sched_map.find(key);
146 if (it != sched_map.end()) {
147 tm_sched_bitset[it->second] = 0;
148 sched_map.erase(it);
149 }
Girish Gowdra252f4972020-09-07 21:24:01 -0700150 bcmos_fastlock_unlock(&tm_sched_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530151}
152
Burak Gurdag2f2618c2020-04-23 13:20:30 +0000153bool is_tm_sched_id_present(int pon_intf_id, int onu_id, int uni_id, std::string direction, int tech_profile_id) {
154 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction, tech_profile_id);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530155 std::map<sched_map_key_tuple, int>::const_iterator it = sched_map.find(key);
156 if (it != sched_map.end()) {
157 return true;
158 }
159 return false;
160}
161
162/**
163* Check whether given two tm qmp profiles are equal or not
164*
165* @param tmq_map_profileA <vector> TM QUEUE MAPPING PROFILE
166* @param tmq_map_profileB <vector> TM QUEUE MAPPING PROFILE
167*
168* @return boolean, true if given tmq_map_profiles are equal else false
169*/
170
171bool check_tm_qmp_equality(std::vector<uint32_t> tmq_map_profileA, std::vector<uint32_t> tmq_map_profileB) {
172 for (uint32_t i = 0; i < TMQ_MAP_PROFILE_SIZE; i++) {
173 if (tmq_map_profileA[i] != tmq_map_profileB[i]) {
174 return false;
175 }
176 }
177 return true;
178}
179
180/**
181* Modifies given queues_pbit_map to parsable format
182* e.g: Modifes "0b00000101" to "10100000"
183*
184* @param queues_pbit_map PBIT MAP configured for each GEM in TECH PROFILE
185* @param size Queue count
186*
187* @return string queues_pbit_map
188*/
189std::string* get_valid_queues_pbit_map(std::string *queues_pbit_map, uint32_t size) {
190 for(uint32_t i=0; i < size; i++) {
191 /* Deletes 2 characters from index number 0 */
192 queues_pbit_map[i].erase(0, 2);
193 std::reverse(queues_pbit_map[i].begin(), queues_pbit_map[i].end());
194 }
195 return queues_pbit_map;
196}
197
198/**
199* Creates TM QUEUE MAPPING PROFILE for given queues_pbit_map and queues_priority_q
200*
201* @param queues_pbit_map PBIT MAP configured for each GEM in TECH PROFILE
202* @param queues_priority_q PRIORITY_Q configured for each GEM in TECH PROFILE
203* @param size Queue count
204*
205* @return <vector> TM QUEUE MAPPING PROFILE
206*/
207std::vector<uint32_t> get_tmq_map_profile(std::string *queues_pbit_map, uint32_t *queues_priority_q, uint32_t size) {
208 std::vector<uint32_t> tmq_map_profile(8,0);
209
210 for(uint32_t i=0; i < size; i++) {
211 for (uint32_t j = 0; j < queues_pbit_map[i].size(); j++) {
212 if (queues_pbit_map[i][j]=='1') {
213 tmq_map_profile.at(j) = queue_id_list[queues_priority_q[i]];
214 }
215 }
216 }
217 return tmq_map_profile;
218}
219
220/**
221* Gets corresponding tm_qmp_id for a given tmq_map_profile
222*
223* @param <vector> TM QUEUE MAPPING PROFILE
224*
225* @return tm_qmp_id
226*/
227int get_tm_qmp_id(std::vector<uint32_t> tmq_map_profile) {
228 int tm_qmp_id = -1;
229
230 std::map<int, std::vector < uint32_t > >::const_iterator it = qmp_id_to_qmp_map.begin();
231 while(it != qmp_id_to_qmp_map.end()) {
232 if(check_tm_qmp_equality(tmq_map_profile, it->second)) {
233 tm_qmp_id = it->first;
234 break;
235 }
236 it++;
237 }
238 return tm_qmp_id;
239}
240
241/**
242* Updates sched_qmp_id_map with given sched_id, pon_intf_id, onu_id, uni_id, tm_qmp_id
243*
244* @param upstream/downstream sched_id
245* @param PON intf ID
246* @param onu_id ONU ID
247* @param uni_id UNI ID
248* @param tm_qmp_id TM QUEUE MAPPING PROFILE ID
249*/
250void update_sched_qmp_id_map(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, \
251 uint32_t uni_id, int tm_qmp_id) {
Girish Gowdra252f4972020-09-07 21:24:01 -0700252 bcmos_fastlock_lock(&tm_qmp_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530253 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
254 sched_qmp_id_map.insert(make_pair(key, tm_qmp_id));
Girish Gowdra252f4972020-09-07 21:24:01 -0700255 bcmos_fastlock_unlock(&tm_qmp_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530256}
257
258/**
259* Gets corresponding tm_qmp_id for a given sched_id, pon_intf_id, onu_id, uni_id
260*
261* @param upstream/downstream sched_id
262* @param PON intf ID
263* @param onu_id ONU ID
264* @param uni_id UNI ID
265*
266* @return tm_qmp_id
267*/
268int get_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id) {
269 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
270 int tm_qmp_id = -1;
271
272 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it = sched_qmp_id_map.find(key);
273 if (it != sched_qmp_id_map.end()) {
274 tm_qmp_id = it->second;
275 }
276 return tm_qmp_id;
277}
278
279/**
280* Gets a unique tm_qmp_id for a given tmq_map_profile
281* The tm_qmp_id is locally cached in a map, so that it can be rendered when necessary.
282* VOLTHA replays whole configuration on OLT reboot, so caching locally is not a problem
283*
284* @param upstream/downstream sched_id
285* @param PON intf ID
286* @param onu_id ONU ID
287* @param uni_id UNI ID
288* @param <vector> TM QUEUE MAPPING PROFILE
289*
290* @return tm_qmp_id
291*/
292int get_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id, \
293 std::vector<uint32_t> tmq_map_profile) {
294 int tm_qmp_id;
295
Girish Gowdra252f4972020-09-07 21:24:01 -0700296 bcmos_fastlock_lock(&tm_qmp_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530297 /* Complexity of O(n). Is there better way that can avoid linear search? */
298 for (tm_qmp_id = 0; tm_qmp_id < MAX_TM_QMP_ID; tm_qmp_id++) {
299 if (tm_qmp_bitset[tm_qmp_id] == 0) {
300 tm_qmp_bitset[tm_qmp_id] = 1;
301 break;
302 }
303 }
Girish Gowdraddf9a162020-01-27 12:56:27 +0530304
305 if (tm_qmp_id < MAX_TM_QMP_ID) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530306 qmp_id_to_qmp_map.insert(make_pair(tm_qmp_id, tmq_map_profile));
Girish Gowdra252f4972020-09-07 21:24:01 -0700307 bcmos_fastlock_unlock(&tm_qmp_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530308 update_sched_qmp_id_map(sched_id, pon_intf_id, onu_id, uni_id, tm_qmp_id);
309 return tm_qmp_id;
310 } else {
Girish Gowdra252f4972020-09-07 21:24:01 -0700311 bcmos_fastlock_unlock(&tm_qmp_bitset_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530312 return -1;
313 }
314}
315
316/**
317* Free tm_qmp_id for a given sched_id, pon_intf_id, onu_id, uni_id
318*
319* @param upstream/downstream sched_id
320* @param PON intf ID
321* @param onu_id ONU ID
322* @param uni_id UNI ID
323* @param tm_qmp_id TM QUEUE MAPPING PROFILE ID
324*
325* @return boolean, true if no more reference for TM QMP else false
326*/
327bool free_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, \
328 uint32_t uni_id, int tm_qmp_id) {
329 bool result;
330 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
331 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it = sched_qmp_id_map.find(key);
Girish Gowdra252f4972020-09-07 21:24:01 -0700332 bcmos_fastlock_lock(&tm_qmp_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530333 if (it != sched_qmp_id_map.end()) {
334 sched_qmp_id_map.erase(it);
335 }
Girish Gowdraddf9a162020-01-27 12:56:27 +0530336
337 uint32_t tm_qmp_ref_count = 0;
338 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it2 = sched_qmp_id_map.begin();
339 while(it2 != sched_qmp_id_map.end()) {
340 if(it2->second == tm_qmp_id) {
341 tm_qmp_ref_count++;
342 }
343 it2++;
344 }
345
346 if (tm_qmp_ref_count == 0) {
347 std::map<int, std::vector < uint32_t > >::const_iterator it3 = qmp_id_to_qmp_map.find(tm_qmp_id);
348 if (it3 != qmp_id_to_qmp_map.end()) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530349 tm_qmp_bitset[tm_qmp_id] = 0;
350 qmp_id_to_qmp_map.erase(it3);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530351 OPENOLT_LOG(INFO, openolt_log_id, "Reference count for tm qmp profile id %d is : %d. So clearing it\n", \
352 tm_qmp_id, tm_qmp_ref_count);
353 result = true;
354 }
355 } else {
356 OPENOLT_LOG(INFO, openolt_log_id, "Reference count for tm qmp profile id %d is : %d. So not clearing it\n", \
357 tm_qmp_id, tm_qmp_ref_count);
358 result = false;
359 }
Girish Gowdra252f4972020-09-07 21:24:01 -0700360 bcmos_fastlock_unlock(&tm_qmp_bitset_lock, 0);
361
Girish Gowdraddf9a162020-01-27 12:56:27 +0530362 return result;
363}
364
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530365/* ACL ID is a shared resource, caller of this function has to ensure atomicity using locks
366 Gets free ACL ID if available, else -1. */
Girish Gowdraddf9a162020-01-27 12:56:27 +0530367int get_acl_id() {
368 int acl_id;
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530369
Girish Gowdra252f4972020-09-07 21:24:01 -0700370 bcmos_fastlock_lock(&acl_id_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530371 /* Complexity of O(n). Is there better way that can avoid linear search? */
372 for (acl_id = 0; acl_id < MAX_ACL_ID; acl_id++) {
373 if (acl_id_bitset[acl_id] == 0) {
374 acl_id_bitset[acl_id] = 1;
375 break;
376 }
377 }
Girish Gowdra252f4972020-09-07 21:24:01 -0700378 bcmos_fastlock_unlock(&acl_id_bitset_lock, 0);
379
Girish Gowdraddf9a162020-01-27 12:56:27 +0530380 if (acl_id < MAX_ACL_ID) {
381 return acl_id ;
382 } else {
383 return -1;
384 }
385}
386
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530387/* ACL ID is a shared resource, caller of this function has to ensure atomicity using locks
388 Frees up the ACL ID. */
Girish Gowdraddf9a162020-01-27 12:56:27 +0530389void free_acl_id (int acl_id) {
Girish Gowdra252f4972020-09-07 21:24:01 -0700390 bcmos_fastlock_lock(&acl_id_bitset_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530391 if (acl_id < MAX_ACL_ID) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530392 acl_id_bitset[acl_id] = 0;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530393 }
Girish Gowdra252f4972020-09-07 21:24:01 -0700394 bcmos_fastlock_unlock(&acl_id_bitset_lock, 0);
395}
396
397/* Gets a free Flow ID if available, else INVALID_FLOW_ID */
398uint16_t get_flow_id() {
399 uint16_t flow_id;
400
401 bcmos_fastlock_lock(&flow_id_bitset_lock);
402 /* Complexity of O(n). Is there better way that can avoid linear search? */
403 // start flow_id from 1 as 0 is invalid
404 for (flow_id = FLOW_ID_START; flow_id <= FLOW_ID_END; flow_id++) {
405 if (flow_id_bitset[flow_id] == 0) {
406 flow_id_bitset[flow_id] = 1;
407 break;
408 }
409 }
410 bcmos_fastlock_unlock(&flow_id_bitset_lock, 0);
411
412 if (flow_id <= MAX_FLOW_ID) {
413 return flow_id ;
414 } else {
415 return INVALID_FLOW_ID;
416 }
417}
418
419/* Gets requested number of Flow IDs.
420 'num_of_flow_ids' is number of flow_ids requested. This cannot be more than NUMBER_OF_PBITS
421 'flow_ids' is pointer to array of size NUMBER_OF_PBITS
422 If the operation is successful, returns true else false
423 The operation is successful if we can allocate fully the number of flow_ids requested.
424 */
425bool get_flow_ids(int num_of_flow_ids, uint16_t *flow_ids) {
426 if (num_of_flow_ids > NUMBER_OF_PBITS) {
427 OPENOLT_LOG(ERROR, openolt_log_id, "requested number of flow_ids is more than 8\n");
428 return false;
429 }
430 int cnt = 0;
431
432 bcmos_fastlock_lock(&flow_id_bitset_lock);
433 /* Complexity of O(n). Is there better way that can avoid linear search? */
434 // start flow_id from 1 as 0 is invalid
435 for (uint16_t flow_id = FLOW_ID_START; flow_id <= FLOW_ID_END && cnt < num_of_flow_ids; flow_id++) {
436 if (flow_id_bitset[flow_id] == 0) {
437 flow_id_bitset[flow_id] = 1;
438 flow_ids[cnt] = flow_id;
439 cnt++;
440 }
441 }
442 bcmos_fastlock_unlock(&flow_id_bitset_lock, 0);
443 // If we could not allocate the requested number of flow_ids free the allocated flow_ids
444 // and return false
445 if (cnt != num_of_flow_ids) {
446 OPENOLT_LOG(ERROR, openolt_log_id, "could not allocated the rquested number of flows ids. requested=%d, allocated=%d", num_of_flow_ids, cnt);
447 if (cnt > 0) {
448 for(int i=0; i < cnt; i++) {
449 free_flow_id(flow_ids[i]);
450 }
451 }
452 return false;
453 }
454 return true;
455}
456
457/* Frees up the FLOW ID. */
458void free_flow_id (uint16_t flow_id) {
459 bcmos_fastlock_lock(&flow_id_bitset_lock);
460 if (flow_id <= MAX_FLOW_ID) {
461 flow_id_bitset[flow_id] = 0;
462 }
463 bcmos_fastlock_unlock(&flow_id_bitset_lock, 0);
464}
465
466void free_flow_ids(uint8_t num_flows, uint16_t *flow_ids) {
467 for (uint8_t i = 0; i < num_flows; i++) {
468 bcmos_fastlock_lock(&flow_id_bitset_lock);
469 if (flow_ids[i] <= MAX_FLOW_ID) {
470 flow_id_bitset[flow_ids[i]] = 0;
471 }
472 bcmos_fastlock_unlock(&flow_id_bitset_lock, 0);
473 }
Girish Gowdraddf9a162020-01-27 12:56:27 +0530474}
475
476/**
477* Returns qos type as string
478*
479* @param qos_type bcmolt_egress_qos_type enum
480*/
481std::string get_qos_type_as_string(bcmolt_egress_qos_type qos_type) {
482 switch (qos_type)
483 {
484 case BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE: return "FIXED_QUEUE";
485 case BCMOLT_EGRESS_QOS_TYPE_TC_TO_QUEUE: return "TC_TO_QUEUE";
486 case BCMOLT_EGRESS_QOS_TYPE_PBIT_TO_TC: return "PBIT_TO_TC";
487 case BCMOLT_EGRESS_QOS_TYPE_NONE: return "NONE";
488 case BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE: return "PRIORITY_TO_QUEUE";
489 default: OPENOLT_LOG(ERROR, openolt_log_id, "qos-type-not-supported %d\n", qos_type);
490 return "qos-type-not-supported";
491 }
492}
493
494/**
495* Gets/Updates qos type for given pon_intf_id, onu_id, uni_id
496*
497* @param PON intf ID
498* @param onu_id ONU ID
499* @param uni_id UNI ID
500* @param queue_size TrafficQueues Size
501*
502* @return qos_type
503*/
504bcmolt_egress_qos_type get_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id, uint32_t queue_size) {
505 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
506 bcmolt_egress_qos_type egress_qos_type = BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
507 std::string qos_string;
508
509 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
510 if (it != qos_type_map.end()) {
511 egress_qos_type = it->second;
512 qos_string = get_qos_type_as_string(egress_qos_type);
513 OPENOLT_LOG(INFO, openolt_log_id, "Qos-type for subscriber connected to pon_intf_id %d, onu_id %d and uni_id %d is %s\n", \
514 pon_intf_id, onu_id, uni_id, qos_string.c_str());
515 }
516 else {
517 /* QOS Type has been pre-defined as Fixed Queue but it will be updated based on number of GEMPORTS
518 associated for a given subscriber. If GEM count = 1 for a given subscriber, qos_type will be Fixed Queue
519 else Priority to Queue */
520 egress_qos_type = (queue_size > 1) ? \
521 BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE : BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
522 bcmos_fastlock_lock(&data_lock);
523 qos_type_map.insert(make_pair(key, egress_qos_type));
524 bcmos_fastlock_unlock(&data_lock, 0);
525 qos_string = get_qos_type_as_string(egress_qos_type);
526 OPENOLT_LOG(INFO, openolt_log_id, "Qos-type for subscriber connected to pon_intf_id %d, onu_id %d and uni_id %d is %s\n", \
527 pon_intf_id, onu_id, uni_id, qos_string.c_str());
528 }
529 return egress_qos_type;
530}
531
532/**
533* Clears qos type for given pon_intf_id, onu_id, uni_id
534*
535* @param PON intf ID
536* @param onu_id ONU ID
537* @param uni_id UNI ID
538*/
539void clear_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id) {
540 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
541 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
542 bcmos_fastlock_lock(&data_lock);
543 if (it != qos_type_map.end()) {
544 qos_type_map.erase(it);
545 OPENOLT_LOG(INFO, openolt_log_id, "Cleared Qos-type for subscriber connected to pon_intf_id %d, onu_id %d and uni_id %d\n", \
546 pon_intf_id, onu_id, uni_id);
547 }
548 bcmos_fastlock_unlock(&data_lock, 0);
549}
550
551/**
552* Returns Scheduler/Queue direction as string
553*
554* @param direction as specified in tech_profile.proto
555*/
556std::string GetDirection(int direction) {
557 switch (direction)
558 {
559 case tech_profile::Direction::UPSTREAM: return upstream;
560 case tech_profile::Direction::DOWNSTREAM: return downstream;
561 default: OPENOLT_LOG(ERROR, openolt_log_id, "direction-not-supported %d\n", direction);
562 return "direction-not-supported";
563 }
564}
565
566// This method handles waiting for AllocObject configuration.
567// Returns error if the AllocObject is not in the appropriate state based on action requested.
568bcmos_errno wait_for_alloc_action(uint32_t intf_id, uint32_t alloc_id, AllocCfgAction action) {
569 Queue<alloc_cfg_complete_result> cfg_result;
570 alloc_cfg_compltd_key k(intf_id, alloc_id);
571 alloc_cfg_compltd_map[k] = &cfg_result;
572 bcmos_errno err = BCM_ERR_OK;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530573
574 // Try to pop the result from BAL with a timeout of ALLOC_CFG_COMPLETE_WAIT_TIMEOUT ms
575 std::pair<alloc_cfg_complete_result, bool> result = cfg_result.pop(ALLOC_CFG_COMPLETE_WAIT_TIMEOUT);
576 if (result.second == false) {
577 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for alloc cfg complete indication intf_id %d, alloc_id %d\n",
578 intf_id, alloc_id);
579 // Invalidate the queue pointer.
580 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
581 alloc_cfg_compltd_map[k] = NULL;
582 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
583 err = BCM_ERR_INTERNAL;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530584 }
585 else if (result.first.status == ALLOC_CFG_STATUS_FAIL) {
586 OPENOLT_LOG(ERROR, openolt_log_id, "error processing alloc cfg request intf_id %d, alloc_id %d\n",
587 intf_id, alloc_id);
588 err = BCM_ERR_INTERNAL;
589 }
590
591 if (err == BCM_ERR_OK) {
592 if (action == ALLOC_OBJECT_CREATE) {
593 if (result.first.state != ALLOC_OBJECT_STATE_ACTIVE) {
594 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object not in active state intf_id %d, alloc_id %d alloc_obj_state %d\n",
595 intf_id, alloc_id, result.first.state);
596 err = BCM_ERR_INTERNAL;
597 } else {
598 OPENOLT_LOG(INFO, openolt_log_id, "Create upstream bandwidth allocation success, intf_id %d, alloc_id %d\n",
599 intf_id, alloc_id);
600 }
601 } else { // ALLOC_OBJECT_DELETE
602 if (result.first.state != ALLOC_OBJECT_STATE_NOT_CONFIGURED) {
603 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object is not reset intf_id %d, alloc_id %d alloc_obj_state %d\n",
604 intf_id, alloc_id, result.first.state);
605 err = BCM_ERR_INTERNAL;
606 } else {
607 OPENOLT_LOG(INFO, openolt_log_id, "Remove alloc object success, intf_id %d, alloc_id %d\n",
608 intf_id, alloc_id);
609 }
610 }
611 }
612
613 // Remove entry from map
614 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
615 alloc_cfg_compltd_map.erase(k);
616 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
Girish Gowdra7a79dae2020-02-10 18:22:11 +0530617 return err;
618}
619
620// This method handles waiting for OnuDeactivate Completed Indication
621bcmos_errno wait_for_onu_deactivate_complete(uint32_t intf_id, uint32_t onu_id) {
622 Queue<onu_deactivate_complete_result> deact_result;
623 onu_deact_compltd_key k(intf_id, onu_id);
624 onu_deact_compltd_map[k] = &deact_result;
625 bcmos_errno err = BCM_ERR_OK;
626
627 // Try to pop the result from BAL with a timeout of ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT ms
628 std::pair<onu_deactivate_complete_result, bool> result = deact_result.pop(ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT);
629 if (result.second == false) {
630 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for onu deactivate complete indication intf_id %d, onu_id %d\n",
631 intf_id, onu_id);
632 // Invalidate the queue pointer.
633 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
634 onu_deact_compltd_map[k] = NULL;
635 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
636 err = BCM_ERR_INTERNAL;
637 }
638 else if (result.first.result == BCMOLT_RESULT_FAIL) {
639 OPENOLT_LOG(ERROR, openolt_log_id, "error processing onu deactivate request intf_id %d, onu_id %d, fail_reason %d\n",
640 intf_id, onu_id, result.first.reason);
641 err = BCM_ERR_INTERNAL;
642 } else if (result.first.result == BCMOLT_RESULT_SUCCESS) {
643 OPENOLT_LOG(INFO, openolt_log_id, "success processing onu deactivate request intf_id %d, onu_id %d\n",
644 intf_id, onu_id);
645 }
646
647 // Remove entry from map
648 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
649 onu_deact_compltd_map.erase(k);
650 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
651
Girish Gowdraddf9a162020-01-27 12:56:27 +0530652 return err;
653}
654
655char* openolt_read_sysinfo(const char* field_name, char* field_val)
656{
657 FILE *fp;
658 /* Prepare the command*/
659 char command[150];
660
661 snprintf(command, sizeof command, "bash -l -c \"onlpdump -s\" | perl -ne 'print $1 if /%s: (\\S+)/'", field_name);
662 /* Open the command for reading. */
663 fp = popen(command, "r");
664 if (fp == NULL) {
665 /*The client has to check for a Null field value in this case*/
666 OPENOLT_LOG(INFO, openolt_log_id, "Failed to query the %s\n", field_name);
667 return field_val;
668 }
669
670 /*Read the field value*/
671 if (fp) {
672 uint8_t ret;
673 ret = fread(field_val, OPENOLT_FIELD_LEN, 1, fp);
674 if (ret >= OPENOLT_FIELD_LEN)
675 OPENOLT_LOG(INFO, openolt_log_id, "Read data length %u\n", ret);
676 pclose(fp);
677 }
678 return field_val;
679}
680
681Status pushOltOperInd(uint32_t intf_id, const char *type, const char *state)
682{
Girish Gowdra252f4972020-09-07 21:24:01 -0700683 ::openolt::Indication ind;
684 ::openolt::IntfOperIndication* intf_oper_ind = new ::openolt::IntfOperIndication;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530685
686 intf_oper_ind->set_type(type);
687 intf_oper_ind->set_intf_id(intf_id);
688 intf_oper_ind->set_oper_state(state);
689 ind.set_allocated_intf_oper_ind(intf_oper_ind);
690 oltIndQ.push(ind);
691 return Status::OK;
692}
693
694#define CLI_HOST_PROMPT_FORMAT "BCM.%u> "
695
696/* Build CLI prompt */
697void openolt_cli_get_prompt_cb(bcmcli_session *session, char *buf, uint32_t max_len)
698{
699 snprintf(buf, max_len, CLI_HOST_PROMPT_FORMAT, dev_id);
700}
701
702int _bal_apiend_cli_thread_handler(long data)
703{
704 char init_string[]="\n";
705 bcmcli_session *sess = current_session;
706 bcmos_task_parm bal_cli_task_p_dummy;
707
708 /* Switch to interactive mode if not stopped in the init script */
709 if (!bcmcli_is_stopped(sess)) {
710 /* Force a CLI command prompt
711 * The string passed into the parse function
712 * must be modifiable, so a string constant like
713 * bcmcli_parse(current_session, "\n") will not
714 * work.
715 */
716 bcmcli_parse(sess, init_string);
717
718 /* Process user input until EOF or quit command */
719 bcmcli_driver(sess);
720 }
721 OPENOLT_LOG(INFO, openolt_log_id, "BAL API End CLI terminated\n");
722
723 /* Cleanup */
724 bcmcli_session_close(current_session);
725 bcmcli_token_destroy(NULL);
726 return 0;
727}
728
729/* Init API CLI commands for the current device */
730bcmos_errno bcm_openolt_api_cli_init(bcmcli_entry *parent_dir, bcmcli_session *session)
731{
732 bcmos_errno rc;
733
734 api_parent_dir = parent_dir;
735
736 rc = bcm_api_cli_set_commands(session);
737
738#ifdef BCM_SUBSYSTEM_HOST
739 /* Subscribe for device change indication */
740 rc = rc ? rc : bcmolt_olt_sel_ind_register(_api_cli_olt_change_ind);
741#endif
742
743 return rc;
744}
745
746bcmos_errno bcm_cli_quit(bcmcli_session *session, const bcmcli_cmd_parm parm[], uint16_t n_parms)
747{
748 bcmcli_stop(session);
749 bcmcli_session_print(session, "CLI terminated by 'Quit' command\n");
750 status_bcm_cli_quit = BCMOS_TRUE;
751
752 return BCM_ERR_OK;
753}
754
755int get_status_bcm_cli_quit(void) {
756 return status_bcm_cli_quit;
757}
758
759bcmos_errno bcmolt_apiend_cli_init() {
760 bcmos_errno ret;
761 bcmos_task_parm bal_cli_task_p = {};
762 bcmos_task_parm bal_cli_task_p_dummy;
763
764 /** before creating the task, check if it is already created by the other half of BAL i.e. Core side */
765 if (BCM_ERR_OK != bcmos_task_query(&bal_cli_thread, &bal_cli_task_p_dummy)) {
766 /* Create BAL CLI thread */
767 bal_cli_task_p.name = bal_cli_thread_name;
768 bal_cli_task_p.handler = _bal_apiend_cli_thread_handler;
769 bal_cli_task_p.priority = TASK_PRIORITY_CLI;
770
771 ret = bcmos_task_create(&bal_cli_thread, &bal_cli_task_p);
772 if (BCM_ERR_OK != ret) {
773 bcmos_printf("Couldn't create BAL API end CLI thread\n");
774 return ret;
775 }
776 }
777}
778
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530779bcmos_errno get_onu_status(bcmolt_interface pon_ni, int onu_id, bcmolt_onu_state *onu_state) {
780 bcmos_errno err;
781 bcmolt_onu_cfg onu_cfg;
782 bcmolt_onu_key onu_key;
783 onu_key.pon_ni = pon_ni;
784 onu_key.onu_id = onu_id;
785
786 BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
787 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, onu_state);
788 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, itu);
789 #ifdef TEST_MODE
790 // It is impossible to mock the setting of onu_cfg.data.onu_state because
791 // the actual bcmolt_cfg_get passes the address of onu_cfg.hdr and we cannot
792 // set the onu_cfg.data.onu_state. So a new stub function is created and address
793 // of onu_cfg is passed. This is one-of case where we need to add test specific
794 // code in production code.
795 err = bcmolt_cfg_get__onu_state_stub(dev_id, &onu_cfg);
796 #else
797 err = bcmolt_cfg_get(dev_id, &onu_cfg.hdr);
798 #endif
799 *onu_state = onu_cfg.data.onu_state;
800 return err;
801}
802
803bcmos_errno get_pon_interface_status(bcmolt_interface pon_ni, bcmolt_interface_state *state, bcmolt_status *los_status) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530804 bcmos_errno err;
805 bcmolt_pon_interface_key pon_key;
806 bcmolt_pon_interface_cfg pon_cfg;
807 pon_key.pon_ni = pon_ni;
808
809 BCMOLT_CFG_INIT(&pon_cfg, pon_interface, pon_key);
810 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, state);
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530811 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, los_status);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530812 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, itu);
813 #ifdef TEST_MODE
814 // It is impossible to mock the setting of pon_cfg.data.state because
815 // the actual bcmolt_cfg_get passes the address of pon_cfg.hdr and we cannot
816 // set the pon_cfg.data.state. So a new stub function is created and address
817 // of pon_cfg is passed. This is one-of case where we need to add test specific
818 // code in production code.
819 err = bcmolt_cfg_get__pon_intf_stub(dev_id, &pon_cfg);
820 #else
821 err = bcmolt_cfg_get(dev_id, &pon_cfg.hdr);
822 #endif
823 *state = pon_cfg.data.state;
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530824 *los_status = pon_cfg.data.los_status;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530825 return err;
826}
827
828/* Same as bcmolt_cfg_get but with added logic of retrying the API
829 in case of some specific failures like timeout or object not yet ready
830*/
831bcmos_errno bcmolt_cfg_get_mult_retry(bcmolt_oltid olt, bcmolt_cfg *cfg) {
832 bcmos_errno err;
833 uint32_t current_try = 0;
834
835 while (current_try < MAX_BAL_API_RETRY_COUNT) {
836 err = bcmolt_cfg_get(olt, cfg);
837 current_try++;
838
839 if (err == BCM_ERR_STATE || err == BCM_ERR_TIMEOUT) {
840 OPENOLT_LOG(WARNING, openolt_log_id, "bcmolt_cfg_get: err = %s\n", bcmos_strerror(err));
841 bcmos_usleep(BAL_API_RETRY_TIME_IN_USECS);
842 continue;
843 }
844 else {
845 break;
846 }
847 }
848
849 if (err != BCM_ERR_OK) {
850 OPENOLT_LOG(ERROR, openolt_log_id, "bcmolt_cfg_get tried (%d) times with retry time(%d usecs) err = %s\n",
851 current_try,
852 BAL_API_RETRY_TIME_IN_USECS,
853 bcmos_strerror(err));
854 }
855 return err;
856}
857
858
859unsigned NumNniIf_() {return num_of_nni_ports;}
860unsigned NumPonIf_() {return num_of_pon_ports;}
861
862bcmos_errno get_nni_interface_status(bcmolt_interface id, bcmolt_interface_state *state) {
863 bcmos_errno err;
864 bcmolt_nni_interface_key nni_key;
865 bcmolt_nni_interface_cfg nni_cfg;
866 nni_key.id = id;
867
868 BCMOLT_CFG_INIT(&nni_cfg, nni_interface, nni_key);
869 BCMOLT_FIELD_SET_PRESENT(&nni_cfg.data, nni_interface_cfg_data, state);
870 #ifdef TEST_MODE
871 // It is impossible to mock the setting of nni_cfg.data.state because
872 // the actual bcmolt_cfg_get passes the address of nni_cfg.hdr and we cannot
873 // set the nni_cfg.data.state. So a new stub function is created and address
874 // of nni_cfg is passed. This is one-of case where we need to add test specific
875 // code in production code.
876 err = bcmolt_cfg_get__nni_intf_stub(dev_id, &nni_cfg);
877 #else
878 err = bcmolt_cfg_get(dev_id, &nni_cfg.hdr);
879 #endif
880 *state = nni_cfg.data.state;
881 return err;
882}
883
884Status install_gem_port(int32_t intf_id, int32_t onu_id, int32_t gemport_id) {
885 bcmos_errno err;
886 bcmolt_itupon_gem_cfg cfg; /* declare main API struct */
887 bcmolt_itupon_gem_key key = {}; /* declare key */
888 bcmolt_gem_port_configuration configuration = {};
889
890 key.pon_ni = intf_id;
891 key.gem_port_id = gemport_id;
892
893 BCMOLT_CFG_INIT(&cfg, itupon_gem, key);
894
895 bcmolt_gem_port_direction configuration_direction;
896 configuration_direction = BCMOLT_GEM_PORT_DIRECTION_BIDIRECTIONAL;
897 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, direction, configuration_direction);
898
899 bcmolt_gem_port_type configuration_type;
900 configuration_type = BCMOLT_GEM_PORT_TYPE_UNICAST;
901 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, type, configuration_type);
902
903 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, configuration, configuration);
904
905 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, onu_id, onu_id);
906
907 bcmolt_control_state encryption_mode;
908 encryption_mode = BCMOLT_CONTROL_STATE_DISABLE;
909 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, encryption_mode, encryption_mode);
910
911 bcmolt_us_gem_port_destination upstream_destination_queue;
912 upstream_destination_queue = BCMOLT_US_GEM_PORT_DESTINATION_DATA;
913 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, upstream_destination_queue, upstream_destination_queue);
914
915 bcmolt_control_state control;
916 control = BCMOLT_CONTROL_STATE_ENABLE;
917 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, control, control);
918
919 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
920 if(err != BCM_ERR_OK) {
Girish Gowdra252f4972020-09-07 21:24:01 -0700921 OPENOLT_LOG(ERROR, openolt_log_id, "failed to install gem_port = %d err_text=%s\n", gemport_id, cfg.hdr.hdr.err_text);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530922 return bcm_to_grpc_err(err, "Access_Control set ITU PON Gem port failed");
923 }
924
925 OPENOLT_LOG(INFO, openolt_log_id, "gem port installed successfully = %d\n", gemport_id);
926
927 return Status::OK;
928}
929
930Status remove_gem_port(int32_t intf_id, int32_t gemport_id) {
931 bcmolt_itupon_gem_cfg gem_cfg;
932 bcmolt_itupon_gem_key key = {
933 .pon_ni = (bcmolt_interface)intf_id,
934 .gem_port_id = (bcmolt_gem_port_id)gemport_id
935 };
936 bcmos_errno err;
937
938 BCMOLT_CFG_INIT(&gem_cfg, itupon_gem, key);
939 err = bcmolt_cfg_clear(dev_id, &gem_cfg.hdr);
940 if (err != BCM_ERR_OK)
941 {
942 OPENOLT_LOG(ERROR, openolt_log_id, "failed to remove gem_port = %d err=%s\n", gemport_id, gem_cfg.hdr.hdr.err_text);
943 return bcm_to_grpc_err(err, "Access_Control clear ITU PON Gem port failed");
944 }
945
946 OPENOLT_LOG(INFO, openolt_log_id, "gem port removed successfully = %d\n", gemport_id);
947
948 return Status::OK;
949}
950
951Status update_acl_interface(int32_t intf_id, bcmolt_interface_type intf_type, uint32_t access_control_id,
952 bcmolt_members_update_command acl_cmd) {
953 bcmos_errno err;
954 bcmolt_access_control_interfaces_update oper; /* declare main API struct */
955 bcmolt_access_control_key acl_key = {}; /* declare key */
956 bcmolt_intf_ref interface_ref_list_elem = {};
957 bcmolt_interface_type interface_ref_list_elem_intf_type;
958 bcmolt_interface_id interface_ref_list_elem_intf_id;
959 bcmolt_intf_ref_list_u8 interface_ref_list = {};
960
961 if (acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_ADD && acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE) {
962 OPENOLT_LOG(ERROR, openolt_log_id, "acl cmd = %d not supported currently\n", acl_cmd);
963 return bcm_to_grpc_err(BCM_ERR_PARM, "unsupported acl cmd");
964 }
965 interface_ref_list.arr = (bcmolt_intf_ref*)bcmos_calloc(sizeof(bcmolt_intf_ref)*1);
966
967 if (interface_ref_list.arr == NULL)
968 return bcm_to_grpc_err(BCM_ERR_PARM, "allocate interface_ref_list failed");
969 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface received for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
970 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
971 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
972
973 acl_key.id = access_control_id;
974
975 /* Initialize the API struct. */
976 BCMOLT_OPER_INIT(&oper, access_control, interfaces_update, acl_key);
977
978 bcmolt_members_update_command command;
979 command = acl_cmd;
980 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, command, command);
981
982 interface_ref_list_elem_intf_type = intf_type;
983 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_type, interface_ref_list_elem_intf_type);
984
985 interface_ref_list_elem_intf_id = intf_id;
986 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_id, interface_ref_list_elem_intf_id);
987
988 interface_ref_list.len = 1;
989 BCMOLT_ARRAY_ELEM_SET(&interface_ref_list, 0, interface_ref_list_elem);
990
991 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, interface_ref_list, interface_ref_list);
992
993 err = bcmolt_oper_submit(dev_id, &oper.hdr);
994 if (err != BCM_ERR_OK) {
995 OPENOLT_LOG(ERROR, openolt_log_id, "update acl interface fail for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
996 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
997 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
998 return bcm_to_grpc_err(err, "Access_Control submit interface failed");
999 }
1000
1001 bcmos_free(interface_ref_list.arr);
1002 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface success for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
1003 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
1004 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
1005
1006 return Status::OK;
1007}
1008
1009Status install_acl(const acl_classifier_key acl_key) {
Girish Gowdraddf9a162020-01-27 12:56:27 +05301010 bcmos_errno err;
1011 bcmolt_access_control_cfg cfg;
1012 bcmolt_access_control_key key = { };
1013 bcmolt_classifier c_val = { };
1014 // hardcode the action for now.
1015 bcmolt_access_control_fwd_action_type action_type = BCMOLT_ACCESS_CONTROL_FWD_ACTION_TYPE_TRAP_TO_HOST;
Girish Gowdraddf9a162020-01-27 12:56:27 +05301016 int acl_id = get_acl_id();
1017 if (acl_id < 0) {
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001018 OPENOLT_LOG(ERROR, openolt_log_id, "exhausted acl_id for eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d o_vid = %d, max_acl_hit=%d\n",
1019 acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port, acl_key.o_vid, max_acls_with_vlan_classifiers_hit);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301020 return bcm_to_grpc_err(BCM_ERR_INTERNAL, "exhausted acl id");
1021 }
1022
1023 key.id = acl_id;
1024 /* config access control instance */
1025 BCMOLT_CFG_INIT(&cfg, access_control, key);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301026 if (acl_key.ether_type > 0) {
1027 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ether_type 0x%04x\n", acl_key.ether_type);
1028 BCMOLT_FIELD_SET(&c_val, classifier, ether_type, acl_key.ether_type);
1029 }
1030
1031 if (acl_key.ip_proto > 0) {
1032 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ip_proto %d\n", acl_key.ip_proto);
1033 BCMOLT_FIELD_SET(&c_val, classifier, ip_proto, acl_key.ip_proto);
1034 }
1035
1036 if (acl_key.dst_port > 0) {
1037 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify dst_port %d\n", acl_key.dst_port);
1038 BCMOLT_FIELD_SET(&c_val, classifier, dst_port, acl_key.dst_port);
1039 }
1040
1041 if (acl_key.src_port > 0) {
1042 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify src_port %d\n", acl_key.src_port);
1043 BCMOLT_FIELD_SET(&c_val, classifier, src_port, acl_key.src_port);
1044 }
1045
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001046 // Make sure that max_acls_with_vlan_classifiers_hit is not true to consider o_vid for ACL classification.
1047 if (acl_key.o_vid > 0 && acl_key.o_vid != ANY_VLAN && !max_acls_with_vlan_classifiers_hit) {
1048 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify o_vid %d\n", acl_key.o_vid);
1049 BCMOLT_FIELD_SET(&c_val, classifier, o_vid, acl_key.o_vid);
1050 }
1051
Girish Gowdraddf9a162020-01-27 12:56:27 +05301052 BCMOLT_MSG_FIELD_SET(&cfg, classifier, c_val);
1053 BCMOLT_MSG_FIELD_SET(&cfg, priority, 10000);
1054 BCMOLT_MSG_FIELD_SET(&cfg, statistics_control, BCMOLT_CONTROL_STATE_ENABLE);
1055
1056 BCMOLT_MSG_FIELD_SET(&cfg, forwarding_action.action, action_type);
1057
1058 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
1059 if (err != BCM_ERR_OK) {
1060 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control set configuration failed, Error %d\n", err);
1061 // Free the acl_id
1062 free_acl_id(acl_id);
1063 return bcm_to_grpc_err(err, "Access_Control set configuration failed");
1064 }
1065
1066 ACL_LOG(INFO, "ACL add ok", err);
1067
1068 // Update the map that we have installed an acl for the given classfier.
1069 acl_classifier_to_acl_id_map[acl_key] = acl_id;
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001070 // If there was a valid vlan classifier in the ACL and the ACL ID hit the ceiling, set max_acls_with_vlan_classifiers_hit to true
1071 // After max_acls_with_vlan_classifiers_hit is set to true no more ACLs can have vlan as an ACL classifier.
1072 if (acl_key.o_vid > 0 && acl_key.o_vid != ANY_VLAN && acl_id >= MAX_ACL_WITH_VLAN_CLASSIFIER) {
1073 max_acls_with_vlan_classifiers_hit = true;
1074 }
Girish Gowdraddf9a162020-01-27 12:56:27 +05301075 return Status::OK;
1076}
1077
1078Status remove_acl(int acl_id) {
1079 bcmos_errno err;
1080 bcmolt_access_control_cfg cfg; /* declare main API struct */
1081 bcmolt_access_control_key key = {}; /* declare key */
1082
1083 key.id = acl_id;
1084
1085 /* Initialize the API struct. */
1086 BCMOLT_CFG_INIT(&cfg, access_control, key);
1087 BCMOLT_FIELD_SET_PRESENT(&cfg.data, access_control_cfg_data, state);
1088 err = bcmolt_cfg_get(dev_id, &cfg.hdr);
1089 if (err != BCM_ERR_OK) {
1090 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control get state failed\n");
1091 return bcm_to_grpc_err(err, "Access_Control get state failed");
1092 }
1093
1094 if (cfg.data.state == BCMOLT_CONFIG_STATE_CONFIGURED) {
1095 key.id = acl_id;
1096 /* Initialize the API struct. */
1097 BCMOLT_CFG_INIT(&cfg, access_control, key);
1098
1099 err = bcmolt_cfg_clear(dev_id, &cfg.hdr);
1100 if (err != BCM_ERR_OK) {
1101 // Should we free acl_id here ? We should ideally never land here..
1102 OPENOLT_LOG(ERROR, openolt_log_id, "Error %d while removing Access_Control rule ID %d\n",
1103 err, acl_id);
1104 return Status(grpc::StatusCode::INTERNAL, "Failed to remove Access_Control");
1105 }
1106 }
1107
1108 // Free up acl_id
1109 free_acl_id(acl_id);
1110
1111 OPENOLT_LOG(INFO, openolt_log_id, "acl removed successfully %d\n", acl_id);
1112
1113 return Status::OK;
1114}
1115
1116// Formulates ACL Classifier Key based on the following fields
1117// a. ether_type b. ip_proto c. src_port d. dst_port
1118// If any of the field is not available it is populated as -1.
1119void formulate_acl_classifier_key(acl_classifier_key *key, const ::openolt::Classifier& classifier) {
1120
1121 // TODO: Is 0 a valid value for any of the following classifiers?
1122 // because in the that case, the 'if' check would fail and -1 would be filled as value.
1123 //
1124 if (classifier.eth_type()) {
1125 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ether_type 0x%04x\n", classifier.eth_type());
1126 key->ether_type = classifier.eth_type();
1127 } else key->ether_type = -1;
1128
1129 if (classifier.ip_proto()) {
1130 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ip_proto %d\n", classifier.ip_proto());
1131 key->ip_proto = classifier.ip_proto();
1132 } else key->ip_proto = -1;
1133
1134
1135 if (classifier.src_port()) {
1136 OPENOLT_LOG(DEBUG, openolt_log_id, "classify src_port %d\n", classifier.src_port());
1137 key->src_port = classifier.src_port();
1138 } else key->src_port = -1;
1139
1140
1141 if (classifier.dst_port()) {
1142 OPENOLT_LOG(DEBUG, openolt_log_id, "classify dst_port %d\n", classifier.dst_port());
1143 key->dst_port = classifier.dst_port();
1144 } else key->dst_port = -1;
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001145
1146 // We should also check the max_acls_with_vlan_classifiers_hit flag is not false to consider the vlan for flow classifier key
1147 if (classifier.o_vid() && !max_acls_with_vlan_classifiers_hit) {
1148 OPENOLT_LOG(DEBUG, openolt_log_id, "classify o_vid %d\n", classifier.o_vid());
1149 key->o_vid = classifier.o_vid();
1150 } else key->o_vid = ANY_VLAN;
1151
Girish Gowdraddf9a162020-01-27 12:56:27 +05301152}
1153
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001154Status handle_acl_rule_install(int32_t onu_id, uint64_t flow_id, int32_t gemport_id,
Girish Gowdraddf9a162020-01-27 12:56:27 +05301155 const std::string flow_type, int32_t access_intf_id,
Girish Gowdra252f4972020-09-07 21:24:01 -07001156 int32_t network_intf_id,
Girish Gowdraddf9a162020-01-27 12:56:27 +05301157 const ::openolt::Classifier& classifier) {
1158 int acl_id;
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001159 uint32_t intf_id = flow_type.compare(upstream) == 0? access_intf_id: network_intf_id;
Girish Gowdraddf9a162020-01-27 12:56:27 +05301160 const std::string intf_type = flow_type.compare(upstream) == 0? "pon": "nni";
1161 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1162
1163 Status resp;
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001164 trap_to_host_packet_type pkt_type = get_trap_to_host_packet_type(classifier);
1165 if (pkt_type == unsupported_trap_to_host_pkt_type) {
1166 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported pkt trap type");
1167 return Status(grpc::StatusCode::UNIMPLEMENTED, "unsupported pkt trap type");
1168 }
Girish Gowdraddf9a162020-01-27 12:56:27 +05301169
1170 // few map keys we are going to use later.
1171 flow_id_flow_direction fl_id_fl_dir(flow_id, flow_type);
Girish Gowdra252f4972020-09-07 21:24:01 -07001172
Girish Gowdraddf9a162020-01-27 12:56:27 +05301173 acl_classifier_key acl_key;
1174 formulate_acl_classifier_key(&acl_key, classifier);
1175 const acl_classifier_key acl_key_const = {.ether_type=acl_key.ether_type, .ip_proto=acl_key.ip_proto,
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001176 .src_port=acl_key.src_port, .dst_port=acl_key.dst_port, .o_vid=acl_key.o_vid};
1177 bcmos_fastlock_lock(&acl_packet_trap_handler_lock);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301178
1179 // Check if the acl is already installed
1180 if (acl_classifier_to_acl_id_map.count(acl_key_const) > 0) {
1181 // retreive the acl_id
1182 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
Girish Gowdra252f4972020-09-07 21:24:01 -07001183
1184
Girish Gowdraddf9a162020-01-27 12:56:27 +05301185 if (flow_to_acl_map.count(fl_id_fl_dir)) {
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001186 // could happen if same trap flow is received again
Girish Gowdraddf9a162020-01-27 12:56:27 +05301187 OPENOLT_LOG(INFO, openolt_log_id, "flow and related acl already handled, nothing more to do\n");
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001188 bcmos_fastlock_unlock(&acl_packet_trap_handler_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301189 return Status::OK;
1190 }
1191
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001192 OPENOLT_LOG(INFO, openolt_log_id, "Acl for flow_id=%lu with eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d o_vid = %d already installed with acl id = %u\n",
1193 flow_id, acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port, acl_key.o_vid, acl_id);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301194
1195 // The acl_ref_cnt is needed to know how many flows refer an ACL.
1196 // When the flow is removed, we decrement the reference count.
1197 // When the reference count becomes 0, we remove the ACL.
1198 if (acl_ref_cnt.count(acl_id) > 0) {
1199 acl_ref_cnt[acl_id] ++;
1200 } else {
1201 // We should ideally not land here. The acl_ref_cnt should have been
1202 // initialized the first time acl was installed.
1203 acl_ref_cnt[acl_id] = 1;
1204 }
1205
1206 } else {
1207 resp = install_acl(acl_key_const);
1208 if (!resp.ok()) {
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001209 OPENOLT_LOG(ERROR, openolt_log_id, "Acl for flow_id=%lu with eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d o_vid = %d failed\n",
1210 flow_id, acl_key_const.ether_type, acl_key_const.ip_proto, acl_key_const.src_port, acl_key_const.dst_port, acl_key_const.o_vid);
1211 bcmos_fastlock_unlock(&acl_packet_trap_handler_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301212 return resp;
1213 }
1214
1215 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
1216
1217 // Initialize the acl reference count
1218 acl_ref_cnt[acl_id] = 1;
1219
Girish Gowdra252f4972020-09-07 21:24:01 -07001220 OPENOLT_LOG(INFO, openolt_log_id, "acl add success for flow_id=%lu with acl_id=%d\n", flow_id, acl_id);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301221 }
1222
1223 // Register the interface for the given acl
1224 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1225 // This is needed to keep a track of which interface (pon/nni) has registered for an ACL.
1226 // If it is registered, how many flows refer to it.
1227 if (intf_acl_registration_ref_cnt.count(ac_id_inf_id_inf_type) > 0) {
1228 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]++;
1229 } else {
1230 // The given interface is not registered for the ACL. We need to do it now.
1231 resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_ADD);
1232 if (!resp.ok()){
1233 OPENOLT_LOG(ERROR, openolt_log_id, "failed to update acl interfaces intf_id=%d, intf_type=%s, acl_id=%d", intf_id, intf_type.c_str(), acl_id);
1234 // TODO: Ideally we should return error from hear and clean up other other stateful
1235 // counters we creaed earlier. Will leave it out for now.
Girish Gowdra252f4972020-09-07 21:24:01 -07001236 }
Girish Gowdraddf9a162020-01-27 12:56:27 +05301237 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] = 1;
1238 }
1239
Girish Gowdra252f4972020-09-07 21:24:01 -07001240 acl_id_intf_id ac_id_if_id(acl_id, intf_id);
1241 flow_to_acl_map[fl_id_fl_dir] = ac_id_if_id;
Girish Gowdraddf9a162020-01-27 12:56:27 +05301242
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001243 // Populate the trap_to_host_pkt_info_with_vlan corresponding to the trap-to-host voltha flow_id key.
1244 // When the trap-to-host voltha flow-id is being removed, this entry is cleared too from the map.
1245 trap_to_host_pkt_info_with_vlan pkt_info_with_vlan((int32_t)olt_if_type, intf_id, (int32_t)pkt_type, gemport_id, (short unsigned int)classifier.o_vid());
1246 trap_to_host_pkt_info_with_vlan_for_flow_id[flow_id] = pkt_info_with_vlan;
1247 trap_to_host_pkt_info pkt_info((int32_t)olt_if_type, intf_id, (int32_t)pkt_type, gemport_id);
1248 bool duplicate = false;
1249 // Check if the vlan_id corresponding to the trap_to_host_pkt_info key is found. Set the 'duplicate' flag accordingly.
1250 if (trap_to_host_vlan_ids_for_trap_to_host_pkt_info.count(pkt_info) > 0) {
1251 auto& vlan_id_list = trap_to_host_vlan_ids_for_trap_to_host_pkt_info[pkt_info];
1252 auto it = std::find(vlan_id_list.begin(), vlan_id_list.end(), acl_key.o_vid);
1253 if (it != vlan_id_list.end()) {
1254 OPENOLT_LOG(DEBUG, openolt_log_id, "cvid = %d exists already in list", acl_key.o_vid);
1255 duplicate = true;
1256 }
1257 }
1258 // If the vlan_id is not found corresponding to the trap_to_host_pkt_info key, update it.
1259 // This will be used to validate the vlan_id in the trapped packet. If vlan_id in the
1260 // trapped packet is not match with the stored value, packet is dropped.
1261 if (!duplicate) {
1262 trap_to_host_vlan_ids_for_trap_to_host_pkt_info[pkt_info].push_back(acl_key.o_vid);
1263 }
1264
1265 bcmos_fastlock_unlock(&acl_packet_trap_handler_lock, 0);
Girish Gowdraddf9a162020-01-27 12:56:27 +05301266
1267 return Status::OK;
1268}
1269
Girish Gowdra252f4972020-09-07 21:24:01 -07001270Status handle_acl_rule_cleanup(int16_t acl_id, int32_t intf_id, const std::string flow_type) {
Girish Gowdraddf9a162020-01-27 12:56:27 +05301271 const std::string intf_type= flow_type.compare(upstream) == 0 ? "pon": "nni";
1272 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1273 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]--;
1274 if (intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] == 0) {
1275 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1276 Status resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE);
1277 if (!resp.ok()){
1278 OPENOLT_LOG(ERROR, openolt_log_id, "failed to update acl interfaces intf_id=%d, intf_type=%s, acl_id=%d", intf_id, intf_type.c_str(), acl_id);
1279 }
1280 intf_acl_registration_ref_cnt.erase(ac_id_inf_id_inf_type);
1281 }
1282
1283 acl_ref_cnt[acl_id]--;
1284 if (acl_ref_cnt[acl_id] == 0) {
1285 remove_acl(acl_id);
1286 acl_ref_cnt.erase(acl_id);
1287 // Iterate acl_classifier_to_acl_id_map and delete classifier the key corresponding to acl_id
1288 std::map<acl_classifier_key, uint16_t>::iterator it;
1289 for (it=acl_classifier_to_acl_id_map.begin(); it!=acl_classifier_to_acl_id_map.end(); ++it) {
1290 if (it->second == acl_id) {
1291 OPENOLT_LOG(INFO, openolt_log_id, "cleared classifier key corresponding to acl_id = %d\n", acl_id);
1292 acl_classifier_to_acl_id_map.erase(it->first);
1293 break;
1294 }
1295 }
1296 }
1297
Girish Gowdraddf9a162020-01-27 12:56:27 +05301298 return Status::OK;
1299}
1300
1301Status check_bal_ready() {
1302 bcmos_errno err;
1303 int maxTrials = 30;
1304 bcmolt_olt_cfg olt_cfg = { };
1305 bcmolt_olt_key olt_key = { };
1306
1307 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
1308 BCMOLT_MSG_FIELD_GET(&olt_cfg, bal_state);
1309
1310 while (olt_cfg.data.bal_state != BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY) {
1311 if (--maxTrials == 0)
1312 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check bal ready failed");
1313 sleep(5);
1314 #ifdef TEST_MODE
1315 // It is impossible to mock the setting of olt_cfg.data.bal_state because
1316 // the actual bcmolt_cfg_get passes the address of olt_cfg.hdr and we cannot
1317 // set the olt_cfg.data.bal_state. So a new stub function is created and address
1318 // of olt_cfg is passed. This is one-of case where we need to add test specific
1319 // code in production code.
1320 if (bcmolt_cfg_get__bal_state_stub(dev_id, &olt_cfg)) {
1321 #else
1322 if (bcmolt_cfg_get(dev_id, &olt_cfg.hdr)) {
1323 #endif
1324 continue;
1325 }
1326 else
1327 OPENOLT_LOG(INFO, openolt_log_id, "waiting for BAL ready ...\n");
1328 }
1329
1330 OPENOLT_LOG(INFO, openolt_log_id, "BAL is ready\n");
1331 return Status::OK;
1332}
1333
1334Status check_connection() {
1335 int maxTrials = 60;
1336 while (!bcmolt_api_conn_mgr_is_connected(dev_id)) {
1337 sleep(1);
1338 if (--maxTrials == 0)
1339 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check connection failed");
1340 else
1341 OPENOLT_LOG(INFO, openolt_log_id, "waiting for daemon connection ...\n");
1342 }
1343 OPENOLT_LOG(INFO, openolt_log_id, "daemon is connected\n");
1344 return Status::OK;
1345}
1346
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +05301347std::string get_ip_address(const char* nw_intf){
1348 std::string ipAddress = "0.0.0.0";
1349 struct ifaddrs *interfaces = NULL;
1350 struct ifaddrs *temp_addr = NULL;
1351 int success = 0;
1352 /* retrieve the current interfaces - returns 0 on success */
1353 success = getifaddrs(&interfaces);
1354 if (success == 0) {
1355 /* Loop through linked list of interfaces */
1356 temp_addr = interfaces;
1357 while(temp_addr != NULL) {
1358 if(temp_addr->ifa_addr->sa_family == AF_INET) {
1359 /* Check if interface given present in OLT, if yes return its IP Address */
1360 if(strcmp(temp_addr->ifa_name, nw_intf) == 0){
1361 ipAddress=inet_ntoa(((struct sockaddr_in*)temp_addr->ifa_addr)->sin_addr);
1362 break;
1363 }
1364 }
1365 temp_addr = temp_addr->ifa_next;
1366 }
1367 }
1368 /* Free memory */
1369 freeifaddrs(interfaces);
1370 return ipAddress;
1371}
Jason Huang1d9cfce2020-05-20 22:58:47 +08001372
1373bcmos_errno getOnuMaxLogicalDistance(uint32_t intf_id, uint32_t *mld) {
1374 bcmos_errno err = BCM_ERR_OK;
1375 bcmolt_pon_distance pon_distance = {};
1376 bcmolt_pon_interface_cfg pon_cfg; /* declare main API struct */
1377 bcmolt_pon_interface_key key = {}; /* declare key */
1378
1379 key.pon_ni = intf_id;
1380
1381 if (!state.is_activated()) {
1382 OPENOLT_LOG(ERROR, openolt_log_id, "ONU maximum logical distance is not available since OLT is not activated yet\n");
1383 return BCM_ERR_STATE;
1384 }
1385
1386 /* Initialize the API struct. */
1387 BCMOLT_CFG_INIT(&pon_cfg, pon_interface, key);
1388 BCMOLT_FIELD_SET_PRESENT(&pon_distance, pon_distance, max_log_distance);
1389 BCMOLT_FIELD_SET(&pon_cfg.data, pon_interface_cfg_data, pon_distance, pon_distance);
1390 #ifdef TEST_MODE
1391 // It is impossible to mock the setting of pon_cfg.data.state because
1392 // the actual bcmolt_cfg_get passes the address of pon_cfg.hdr and we cannot
1393 // set the pon_cfg.data.state. So a new stub function is created and address
1394 // of pon_cfg is passed. This is one-of case where we need to add test specific
1395 // code in production code.
1396 err = bcmolt_cfg_get__pon_intf_stub(dev_id, &pon_cfg);
1397 #else
1398 err = bcmolt_cfg_get(dev_id, &pon_cfg.hdr);
1399 #endif
1400 if (err != BCM_ERR_OK) {
1401 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve ONU maximum logical distance for PON %d, err = %s (%d)\n", intf_id, bcmos_strerror(err), err);
1402 return err;
1403 }
1404 *mld = pon_distance.max_log_distance;
1405
1406 return BCM_ERR_OK;
1407}
Humera Kouser6143c9e2020-06-17 22:37:31 +05301408
1409/**
1410* Gets mac address based on interface name.
1411*
1412* @param intf_name interface name
1413* @param mac_address mac address field
1414* @param max_size_of_mac_address max sixe of the mac_address
1415* @return mac_address value in case of success or return NULL in case of failure.
1416*/
1417
1418char* get_intf_mac(const char* intf_name, char* mac_address, unsigned int max_size_of_mac_address){
1419 int fd;
1420 struct ifreq ifr;
1421 char *mac;
1422
1423 fd = socket(AF_INET, SOCK_DGRAM, 0);
1424 if ( fd == -1) {
1425 OPENOLT_LOG(ERROR, openolt_log_id, "failed to get mac, could not create file descriptor");
1426 return NULL;
1427 }
1428
1429 ifr.ifr_addr.sa_family = AF_INET;
1430 strncpy((char *)ifr.ifr_name , (const char *)intf_name , IFNAMSIZ-1);
1431 if( ioctl(fd, SIOCGIFHWADDR, &ifr) == -1)
1432 {
1433 OPENOLT_LOG(ERROR, openolt_log_id, "failed to get mac, ioctl failed and returned err");
1434 close(fd);
1435 return NULL;
1436 }
1437
1438 close(fd);
1439 mac = (char *)ifr.ifr_hwaddr.sa_data;
1440
1441 // formatted mac address
1442 snprintf(mac_address, max_size_of_mac_address, (const char *)"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2], (unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5]);
1443
1444 return mac_address;
1445}
Girish Gowdra252f4972020-09-07 21:24:01 -07001446
1447void update_voltha_flow_to_cache(uint64_t voltha_flow_id, device_flow dev_flow) {
1448 OPENOLT_LOG(DEBUG, openolt_log_id, "updating voltha flow=%lu to cache\n", voltha_flow_id)
1449 bcmos_fastlock_lock(&voltha_flow_to_device_flow_lock);
1450 voltha_flow_to_device_flow[voltha_flow_id] = dev_flow;
1451 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1452}
1453
1454void remove_voltha_flow_from_cache(uint64_t voltha_flow_id) {
1455 bcmos_fastlock_lock(&voltha_flow_to_device_flow_lock);
1456 std::map<uint64_t, device_flow>::const_iterator it = voltha_flow_to_device_flow.find(voltha_flow_id);
1457 if (it != voltha_flow_to_device_flow.end()) {
1458 voltha_flow_to_device_flow.erase(it);
1459 }
1460 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1461}
1462
1463bool is_voltha_flow_installed(uint64_t voltha_flow_id ) {
1464 int count;
1465 bcmos_fastlock_lock(&voltha_flow_to_device_flow_lock);
1466 count = voltha_flow_to_device_flow.count(voltha_flow_id);
1467 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1468
1469 return count > 0 ? true : false;
1470}
1471
1472const device_flow_params* get_device_flow_params(uint64_t voltha_flow_id) {
1473 bcmos_fastlock_lock(&voltha_flow_to_device_flow_lock);
1474 std::map<uint64_t, device_flow>::const_iterator it = voltha_flow_to_device_flow.find(voltha_flow_id);
1475 if (it != voltha_flow_to_device_flow.end()) {
1476 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1477 return it->second.params;
1478 }
1479 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1480
1481 return NULL;
1482}
1483
1484const device_flow* get_device_flow(uint64_t voltha_flow_id) {
1485 bcmos_fastlock_lock(&voltha_flow_to_device_flow_lock);
1486 std::map<uint64_t, device_flow>::const_iterator it = voltha_flow_to_device_flow.find(voltha_flow_id);
1487 if (it != voltha_flow_to_device_flow.end()) {
1488 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1489 return &it->second;
1490 }
1491 bcmos_fastlock_unlock(&voltha_flow_to_device_flow_lock, 0);
1492
1493 return NULL;
1494}
Girish Gowdra1935e6a2020-10-31 21:48:22 -07001495
1496trap_to_host_packet_type get_trap_to_host_packet_type(const ::openolt::Classifier& classifier) {
1497 trap_to_host_packet_type type = unsupported_trap_to_host_pkt_type;
1498 if (classifier.eth_type() == EAP_ETH_TYPE) {
1499 type = eap;
1500 } else if (classifier.src_port() == DHCP_SERVER_SRC_PORT || classifier.src_port() == DHCP_CLIENT_SRC_PORT) {
1501 type = dhcpv4;
1502 } else if (classifier.eth_type() == LLDP_ETH_TYPE) {
1503 type = lldp;
1504 } else if (classifier.ip_proto() == IGMPv4_PROTOCOL) {
1505 type = igmpv4;
1506 }
1507
1508 return type;
1509}
1510
1511// is_packet_allowed extracts the VLAN, packet-type, interface-type, interface-id from incoming trap-to-host packet.
1512// Then it verifies if this packet can be allowed upstream to host. It does this by checking if the vlan in the incoming packet
1513//exists in trap_to_host_vlan_ids_for_trap_to_host_pkt_info map for (interface-type, interface-id, packet-type) key.
1514bool is_packet_allowed(bcmolt_access_control_receive_eth_packet_data *data, int32_t gemport_id) {
1515 bcmolt_interface_type intf_type = data->interface_ref.intf_type;
1516 uint32_t intf_id = data->interface_ref.intf_id;
1517 trap_to_host_packet_type pkt_type = unsupported_trap_to_host_pkt_type;
1518 uint16_t vlan_id = 0;
1519 int ethType;
1520
1521 struct timeval dummy_tv = {0, 0};
1522 bool free_memory_of_raw_packet = false; // This indicates the pcap library to not free the message buffer. It will freed by the caller.
1523
1524 pcpp::RawPacket rawPacket(data->buffer.arr, data->buffer.len, dummy_tv, free_memory_of_raw_packet, pcpp::LINKTYPE_ETHERNET);
1525 pcpp::Packet parsedPacket(&rawPacket);
1526 pcpp::EthLayer* ethernetLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
1527 if (ethernetLayer == NULL)
1528 {
1529 OPENOLT_LOG(ERROR, openolt_log_id, "Something went wrong, couldn't find Ethernet layer\n");
1530 return false;
1531 }
1532
1533 // Getting Vlan layer
1534 pcpp::VlanLayer* vlanLayer = parsedPacket.getLayerOfType<pcpp::VlanLayer>();
1535 if (vlanLayer == NULL)
1536 {
1537 // Allow Untagged LLDP Ether type packet to trap from NNI
1538 if (ntohs(ethernetLayer->getEthHeader()->etherType) == LLDP_ETH_TYPE && intf_type == BCMOLT_INTERFACE_TYPE_NNI) {
1539 return true;
1540 } else {
1541 OPENOLT_LOG(WARNING, openolt_log_id, "untagged packets other than lldp packets are dropped. ethertype=%d, intftype=%d, intf_id=%d\n",
1542 ntohs(ethernetLayer->getEthHeader()->etherType), intf_type, intf_id);
1543 return false;
1544 }
1545 } else {
1546 ethType = ntohs(vlanLayer->getVlanHeader()->etherType);
1547 if (ethType == EAP_ETH_TYPE) { // single tagged packet with EAPoL payload
1548 vlan_id = vlanLayer->getVlanID();
1549 pkt_type = eap;
1550 } else if (ethType == IPV4_ETH_TYPE) { // single tagged packet with IPv4 payload
1551 vlan_id = vlanLayer->getVlanID();
1552 vlanLayer->parseNextLayer();
1553 pcpp::IPv4Layer *ipv4Layer = (pcpp::IPv4Layer*)vlanLayer->getNextLayer();
1554 if(ipv4Layer->getIPv4Header()->protocol == UDP_PROTOCOL) { // UDP payload
1555 // Check the UDP Ports to see if it is a DHCPv4 packet
1556 ipv4Layer->parseNextLayer();
1557 pcpp::UdpLayer *udpLayer = (pcpp::UdpLayer*)ipv4Layer->getNextLayer();
1558 if (ntohs(udpLayer->getUdpHeader()->portSrc) == DHCP_SERVER_SRC_PORT|| ntohs(udpLayer->getUdpHeader()->portSrc) == DHCP_CLIENT_SRC_PORT) {
1559 pkt_type = dhcpv4;
1560 } else {
1561 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported udp source port = %d\n", ntohs(udpLayer->getUdpHeader()->portSrc));
1562 return false;
1563 }
1564 } else if (ipv4Layer->getIPv4Header()->protocol == IGMPv4_PROTOCOL) { // Igmpv4 payload
1565 pkt_type = igmpv4;
1566 } else {
1567 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported ip protocol = %d\n", ipv4Layer->getIPv4Header()->protocol);
1568 return false;
1569 }
1570 } else if (ethType == VLAN_ETH_TYPE) { // double tagged packet
1571
1572 // Trap-to-host from NNI flows do not specify the VLANs, so no vlan validation is necessary.
1573 if (intf_type == BCMOLT_INTERFACE_TYPE_NNI) {
1574 return true;
1575 }
1576
1577 // Here we parse the inner vlan payload and currently support only IPv4 packets
1578
1579 // Extract the vlan_id for trap-to-host packets arriving from the PON
1580 // trap-to-host ACLs from the NNI do not care about VLAN.
1581 if (intf_type == BCMOLT_INTERFACE_TYPE_PON) {
1582 vlan_id = vlanLayer->getVlanID(); // This is the outer vlan id
1583 }
1584 vlanLayer->parseNextLayer();
1585 vlanLayer = (pcpp::VlanLayer*)vlanLayer->getNextLayer(); // Here we extract the inner vlan layer
1586 ethType = ntohs(vlanLayer->getVlanHeader()->etherType);
1587 if (ethType == IPV4_ETH_TYPE) { // IPv4
1588 uint16_t _inner_vlan_id = vlanLayer->getVlanID();
1589 vlanLayer->parseNextLayer();
1590 pcpp::IPv4Layer *ipv4Layer = (pcpp::IPv4Layer*)vlanLayer->getNextLayer(); // here we extract the inner vlan IPv4 payload
1591 if(ipv4Layer->getIPv4Header()->protocol == UDP_PROTOCOL) { // UDP payload
1592 // Check the UDP Ports to see if it is a DHCPv4 packet
1593 ipv4Layer->parseNextLayer();
1594 pcpp::UdpLayer *udpLayer = (pcpp::UdpLayer*)ipv4Layer->getNextLayer();
1595 if (ntohs(udpLayer->getUdpHeader()->portSrc) == DHCP_SERVER_SRC_PORT || ntohs(udpLayer->getUdpHeader()->portSrc) == DHCP_CLIENT_SRC_PORT) {
1596 pkt_type = dhcpv4;
1597 } else {
1598 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported udp source port = %d\n", ntohs(udpLayer->getUdpHeader()->portSrc));
1599 return false;
1600 }
1601 } else if (ipv4Layer->getIPv4Header()->protocol == IGMPv4_PROTOCOL) { // Igmpv4 payload
1602 pkt_type = igmpv4;
1603 } else {
1604 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported ip protocol = %d\n", ipv4Layer->getIPv4Header()->protocol)
1605 return false;
1606 }
1607 }
1608 } else {
1609 OPENOLT_LOG(ERROR, openolt_log_id, "unsupported ether type = 0x%x\n", ntohs((vlanLayer->getVlanHeader()->etherType)));
1610 return false;
1611 }
1612 }
1613
1614#if 0 // Debug logs for test purpose only
1615 std::cout << "vlan of received packet " << vlan_id << " intf_type " << intf_type << " intf_id " <<intf_id << " pkt_type " <<pkt_type << " gem_port_id" << gemport_id << "\n";
1616 for(std::map<trap_to_host_pkt_info, std::list<uint16_t> >::const_iterator it = trap_to_host_vlan_ids_for_trap_to_host_pkt_info.begin();
1617 it != trap_to_host_vlan_ids_for_trap_to_host_pkt_info.end(); ++it)
1618 {
1619 std::cout << "value entries" << " " << std::get<0>(it->first) << " "<< std::get<1>(it->first) << " "<< std::get<2>(it->first) << " "<< std::get<3>(it->first) << "\n\n";
1620 std::cout << "vlans for the above key are => ";
1621 for (std::list<uint16_t>::const_iterator _it=it->second.begin();
1622 _it != it->second.end();
1623 ++_it) {
1624 std::cout << *_it << " ";
1625 }
1626 std::cout << "\n\n";
1627 }
1628#endif
1629
1630 trap_to_host_pkt_info pkt_info(intf_type, intf_id, pkt_type, gemport_id);
1631 // Check for matching vlan only if the trap_to_host_pkt_info exists in the trap_to_host_vlan_ids_for_trap_to_host_pkt_info map
1632 if (trap_to_host_vlan_ids_for_trap_to_host_pkt_info.count(pkt_info) > 0) {
1633 // Iterate throught the vlan list to find matching vlan
1634 auto& vlan_id_list = trap_to_host_vlan_ids_for_trap_to_host_pkt_info[pkt_info];
1635 for (auto allowed_vlan_id : vlan_id_list) {
1636 // Found exact matching vlan in the allowed list of vlans for the trap_to_host_pkt_info key or
1637 // there is generic match ANY_VLAN in the list in the allowed vlan list.
1638 if (allowed_vlan_id == vlan_id || allowed_vlan_id == ANY_VLAN) {
1639 return true;
1640 }
1641 }
1642 }
1643 return false;
1644}