blob: 9bcee03b25312328462d4454e4de662df287b583 [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.
86* VOLTHA replays whole configuration on OLT reboot, so caching locally is not a problem
87*
88* @param intf_id NNI or PON intf ID
89* @param onu_id ONU ID
90* @param uni_id UNI ID
91* @param gemport_id GEM Port ID
92* @param direction Upstream or downstream
93*
94* @return tm_sched_id
95*/
96uint32_t get_tm_sched_id(int pon_intf_id, int onu_id, int uni_id, std::string direction) {
97 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction);
98 int sched_id = -1;
99
100 std::map<sched_map_key_tuple, int>::const_iterator it = sched_map.find(key);
101 if (it != sched_map.end()) {
102 sched_id = it->second;
103 }
104 if (sched_id != -1) {
105 return sched_id;
106 }
107
108 bcmos_fastlock_lock(&data_lock);
109 // Complexity of O(n). Is there better way that can avoid linear search?
110 for (sched_id = 0; sched_id < MAX_TM_SCHED_ID; sched_id++) {
111 if (tm_sched_bitset[sched_id] == 0) {
112 tm_sched_bitset[sched_id] = 1;
113 break;
114 }
115 }
116 bcmos_fastlock_unlock(&data_lock, 0);
117
118 if (sched_id < MAX_TM_SCHED_ID) {
119 bcmos_fastlock_lock(&data_lock);
120 sched_map[key] = sched_id;
121 bcmos_fastlock_unlock(&data_lock, 0);
122 return sched_id;
123 } else {
124 return -1;
125 }
126}
127
128/**
129* Free tm_sched_id for a given intf_id, onu_id, uni_id, gemport_id, direction
130*
131* @param intf_id NNI or PON intf ID
132* @param onu_id ONU ID
133* @param uni_id UNI ID
134* @param gemport_id GEM Port ID
135* @param direction Upstream or downstream
136*/
137void free_tm_sched_id(int pon_intf_id, int onu_id, int uni_id, std::string direction) {
138 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction);
139 std::map<sched_map_key_tuple, int>::const_iterator it;
140 bcmos_fastlock_lock(&data_lock);
141 it = sched_map.find(key);
142 if (it != sched_map.end()) {
143 tm_sched_bitset[it->second] = 0;
144 sched_map.erase(it);
145 }
146 bcmos_fastlock_unlock(&data_lock, 0);
147}
148
149bool is_tm_sched_id_present(int pon_intf_id, int onu_id, int uni_id, std::string direction) {
150 sched_map_key_tuple key(pon_intf_id, onu_id, uni_id, direction);
151 std::map<sched_map_key_tuple, int>::const_iterator it = sched_map.find(key);
152 if (it != sched_map.end()) {
153 return true;
154 }
155 return false;
156}
157
158/**
159* Check whether given two tm qmp profiles are equal or not
160*
161* @param tmq_map_profileA <vector> TM QUEUE MAPPING PROFILE
162* @param tmq_map_profileB <vector> TM QUEUE MAPPING PROFILE
163*
164* @return boolean, true if given tmq_map_profiles are equal else false
165*/
166
167bool check_tm_qmp_equality(std::vector<uint32_t> tmq_map_profileA, std::vector<uint32_t> tmq_map_profileB) {
168 for (uint32_t i = 0; i < TMQ_MAP_PROFILE_SIZE; i++) {
169 if (tmq_map_profileA[i] != tmq_map_profileB[i]) {
170 return false;
171 }
172 }
173 return true;
174}
175
176/**
177* Modifies given queues_pbit_map to parsable format
178* e.g: Modifes "0b00000101" to "10100000"
179*
180* @param queues_pbit_map PBIT MAP configured for each GEM in TECH PROFILE
181* @param size Queue count
182*
183* @return string queues_pbit_map
184*/
185std::string* get_valid_queues_pbit_map(std::string *queues_pbit_map, uint32_t size) {
186 for(uint32_t i=0; i < size; i++) {
187 /* Deletes 2 characters from index number 0 */
188 queues_pbit_map[i].erase(0, 2);
189 std::reverse(queues_pbit_map[i].begin(), queues_pbit_map[i].end());
190 }
191 return queues_pbit_map;
192}
193
194/**
195* Creates TM QUEUE MAPPING PROFILE for given queues_pbit_map and queues_priority_q
196*
197* @param queues_pbit_map PBIT MAP configured for each GEM in TECH PROFILE
198* @param queues_priority_q PRIORITY_Q configured for each GEM in TECH PROFILE
199* @param size Queue count
200*
201* @return <vector> TM QUEUE MAPPING PROFILE
202*/
203std::vector<uint32_t> get_tmq_map_profile(std::string *queues_pbit_map, uint32_t *queues_priority_q, uint32_t size) {
204 std::vector<uint32_t> tmq_map_profile(8,0);
205
206 for(uint32_t i=0; i < size; i++) {
207 for (uint32_t j = 0; j < queues_pbit_map[i].size(); j++) {
208 if (queues_pbit_map[i][j]=='1') {
209 tmq_map_profile.at(j) = queue_id_list[queues_priority_q[i]];
210 }
211 }
212 }
213 return tmq_map_profile;
214}
215
216/**
217* Gets corresponding tm_qmp_id for a given tmq_map_profile
218*
219* @param <vector> TM QUEUE MAPPING PROFILE
220*
221* @return tm_qmp_id
222*/
223int get_tm_qmp_id(std::vector<uint32_t> tmq_map_profile) {
224 int tm_qmp_id = -1;
225
226 std::map<int, std::vector < uint32_t > >::const_iterator it = qmp_id_to_qmp_map.begin();
227 while(it != qmp_id_to_qmp_map.end()) {
228 if(check_tm_qmp_equality(tmq_map_profile, it->second)) {
229 tm_qmp_id = it->first;
230 break;
231 }
232 it++;
233 }
234 return tm_qmp_id;
235}
236
237/**
238* Updates sched_qmp_id_map with given sched_id, pon_intf_id, onu_id, uni_id, tm_qmp_id
239*
240* @param upstream/downstream sched_id
241* @param PON intf ID
242* @param onu_id ONU ID
243* @param uni_id UNI ID
244* @param tm_qmp_id TM QUEUE MAPPING PROFILE ID
245*/
246void update_sched_qmp_id_map(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, \
247 uint32_t uni_id, int tm_qmp_id) {
248 bcmos_fastlock_lock(&data_lock);
249 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
250 sched_qmp_id_map.insert(make_pair(key, tm_qmp_id));
251 bcmos_fastlock_unlock(&data_lock, 0);
252}
253
254/**
255* Gets corresponding tm_qmp_id for a given sched_id, pon_intf_id, onu_id, uni_id
256*
257* @param upstream/downstream sched_id
258* @param PON intf ID
259* @param onu_id ONU ID
260* @param uni_id UNI ID
261*
262* @return tm_qmp_id
263*/
264int get_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id) {
265 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
266 int tm_qmp_id = -1;
267
268 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it = sched_qmp_id_map.find(key);
269 if (it != sched_qmp_id_map.end()) {
270 tm_qmp_id = it->second;
271 }
272 return tm_qmp_id;
273}
274
275/**
276* Gets a unique tm_qmp_id for a given tmq_map_profile
277* The tm_qmp_id is locally cached in a map, so that it can be rendered when necessary.
278* VOLTHA replays whole configuration on OLT reboot, so caching locally is not a problem
279*
280* @param upstream/downstream sched_id
281* @param PON intf ID
282* @param onu_id ONU ID
283* @param uni_id UNI ID
284* @param <vector> TM QUEUE MAPPING PROFILE
285*
286* @return tm_qmp_id
287*/
288int get_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id, \
289 std::vector<uint32_t> tmq_map_profile) {
290 int tm_qmp_id;
291
292 bcmos_fastlock_lock(&data_lock);
293 /* Complexity of O(n). Is there better way that can avoid linear search? */
294 for (tm_qmp_id = 0; tm_qmp_id < MAX_TM_QMP_ID; tm_qmp_id++) {
295 if (tm_qmp_bitset[tm_qmp_id] == 0) {
296 tm_qmp_bitset[tm_qmp_id] = 1;
297 break;
298 }
299 }
300 bcmos_fastlock_unlock(&data_lock, 0);
301
302 if (tm_qmp_id < MAX_TM_QMP_ID) {
303 bcmos_fastlock_lock(&data_lock);
304 qmp_id_to_qmp_map.insert(make_pair(tm_qmp_id, tmq_map_profile));
305 bcmos_fastlock_unlock(&data_lock, 0);
306 update_sched_qmp_id_map(sched_id, pon_intf_id, onu_id, uni_id, tm_qmp_id);
307 return tm_qmp_id;
308 } else {
309 return -1;
310 }
311}
312
313/**
314* Free tm_qmp_id for a given sched_id, pon_intf_id, onu_id, uni_id
315*
316* @param upstream/downstream sched_id
317* @param PON intf ID
318* @param onu_id ONU ID
319* @param uni_id UNI ID
320* @param tm_qmp_id TM QUEUE MAPPING PROFILE ID
321*
322* @return boolean, true if no more reference for TM QMP else false
323*/
324bool free_tm_qmp_id(uint32_t sched_id,uint32_t pon_intf_id, uint32_t onu_id, \
325 uint32_t uni_id, int tm_qmp_id) {
326 bool result;
327 sched_qmp_id_map_key_tuple key(sched_id, pon_intf_id, onu_id, uni_id);
328 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it = sched_qmp_id_map.find(key);
329 bcmos_fastlock_lock(&data_lock);
330 if (it != sched_qmp_id_map.end()) {
331 sched_qmp_id_map.erase(it);
332 }
333 bcmos_fastlock_unlock(&data_lock, 0);
334
335 uint32_t tm_qmp_ref_count = 0;
336 std::map<sched_qmp_id_map_key_tuple, int>::const_iterator it2 = sched_qmp_id_map.begin();
337 while(it2 != sched_qmp_id_map.end()) {
338 if(it2->second == tm_qmp_id) {
339 tm_qmp_ref_count++;
340 }
341 it2++;
342 }
343
344 if (tm_qmp_ref_count == 0) {
345 std::map<int, std::vector < uint32_t > >::const_iterator it3 = qmp_id_to_qmp_map.find(tm_qmp_id);
346 if (it3 != qmp_id_to_qmp_map.end()) {
347 bcmos_fastlock_lock(&data_lock);
348 tm_qmp_bitset[tm_qmp_id] = 0;
349 qmp_id_to_qmp_map.erase(it3);
350 bcmos_fastlock_unlock(&data_lock, 0);
351 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 }
360 return result;
361}
362
363// Gets free ACL ID if available, else -1
364int get_acl_id() {
365 int acl_id;
366 bcmos_fastlock_lock(&data_lock);
367 /* Complexity of O(n). Is there better way that can avoid linear search? */
368 for (acl_id = 0; acl_id < MAX_ACL_ID; acl_id++) {
369 if (acl_id_bitset[acl_id] == 0) {
370 acl_id_bitset[acl_id] = 1;
371 break;
372 }
373 }
374 bcmos_fastlock_unlock(&data_lock, 0);
375 if (acl_id < MAX_ACL_ID) {
376 return acl_id ;
377 } else {
378 return -1;
379 }
380}
381
382// Frees up the ACL ID.
383void free_acl_id (int acl_id) {
384 if (acl_id < MAX_ACL_ID) {
385 bcmos_fastlock_lock(&data_lock);
386 acl_id_bitset[acl_id] = 0;
387 bcmos_fastlock_unlock(&data_lock, 0);
388 }
389}
390
391/**
392* Returns qos type as string
393*
394* @param qos_type bcmolt_egress_qos_type enum
395*/
396std::string get_qos_type_as_string(bcmolt_egress_qos_type qos_type) {
397 switch (qos_type)
398 {
399 case BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE: return "FIXED_QUEUE";
400 case BCMOLT_EGRESS_QOS_TYPE_TC_TO_QUEUE: return "TC_TO_QUEUE";
401 case BCMOLT_EGRESS_QOS_TYPE_PBIT_TO_TC: return "PBIT_TO_TC";
402 case BCMOLT_EGRESS_QOS_TYPE_NONE: return "NONE";
403 case BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE: return "PRIORITY_TO_QUEUE";
404 default: OPENOLT_LOG(ERROR, openolt_log_id, "qos-type-not-supported %d\n", qos_type);
405 return "qos-type-not-supported";
406 }
407}
408
409/**
410* Gets/Updates qos type for given pon_intf_id, onu_id, uni_id
411*
412* @param PON intf ID
413* @param onu_id ONU ID
414* @param uni_id UNI ID
415* @param queue_size TrafficQueues Size
416*
417* @return qos_type
418*/
419bcmolt_egress_qos_type get_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id, uint32_t queue_size) {
420 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
421 bcmolt_egress_qos_type egress_qos_type = BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
422 std::string qos_string;
423
424 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
425 if (it != qos_type_map.end()) {
426 egress_qos_type = it->second;
427 qos_string = get_qos_type_as_string(egress_qos_type);
428 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", \
429 pon_intf_id, onu_id, uni_id, qos_string.c_str());
430 }
431 else {
432 /* QOS Type has been pre-defined as Fixed Queue but it will be updated based on number of GEMPORTS
433 associated for a given subscriber. If GEM count = 1 for a given subscriber, qos_type will be Fixed Queue
434 else Priority to Queue */
435 egress_qos_type = (queue_size > 1) ? \
436 BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE : BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
437 bcmos_fastlock_lock(&data_lock);
438 qos_type_map.insert(make_pair(key, egress_qos_type));
439 bcmos_fastlock_unlock(&data_lock, 0);
440 qos_string = get_qos_type_as_string(egress_qos_type);
441 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", \
442 pon_intf_id, onu_id, uni_id, qos_string.c_str());
443 }
444 return egress_qos_type;
445}
446
447/**
448* Clears qos type for given pon_intf_id, onu_id, uni_id
449*
450* @param PON intf ID
451* @param onu_id ONU ID
452* @param uni_id UNI ID
453*/
454void clear_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id) {
455 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
456 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
457 bcmos_fastlock_lock(&data_lock);
458 if (it != qos_type_map.end()) {
459 qos_type_map.erase(it);
460 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", \
461 pon_intf_id, onu_id, uni_id);
462 }
463 bcmos_fastlock_unlock(&data_lock, 0);
464}
465
466/**
467* Returns Scheduler/Queue direction as string
468*
469* @param direction as specified in tech_profile.proto
470*/
471std::string GetDirection(int direction) {
472 switch (direction)
473 {
474 case tech_profile::Direction::UPSTREAM: return upstream;
475 case tech_profile::Direction::DOWNSTREAM: return downstream;
476 default: OPENOLT_LOG(ERROR, openolt_log_id, "direction-not-supported %d\n", direction);
477 return "direction-not-supported";
478 }
479}
480
481// This method handles waiting for AllocObject configuration.
482// Returns error if the AllocObject is not in the appropriate state based on action requested.
483bcmos_errno wait_for_alloc_action(uint32_t intf_id, uint32_t alloc_id, AllocCfgAction action) {
484 Queue<alloc_cfg_complete_result> cfg_result;
485 alloc_cfg_compltd_key k(intf_id, alloc_id);
486 alloc_cfg_compltd_map[k] = &cfg_result;
487 bcmos_errno err = BCM_ERR_OK;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530488
489 // Try to pop the result from BAL with a timeout of ALLOC_CFG_COMPLETE_WAIT_TIMEOUT ms
490 std::pair<alloc_cfg_complete_result, bool> result = cfg_result.pop(ALLOC_CFG_COMPLETE_WAIT_TIMEOUT);
491 if (result.second == false) {
492 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for alloc cfg complete indication intf_id %d, alloc_id %d\n",
493 intf_id, alloc_id);
494 // Invalidate the queue pointer.
495 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
496 alloc_cfg_compltd_map[k] = NULL;
497 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
498 err = BCM_ERR_INTERNAL;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530499 }
500 else if (result.first.status == ALLOC_CFG_STATUS_FAIL) {
501 OPENOLT_LOG(ERROR, openolt_log_id, "error processing alloc cfg request intf_id %d, alloc_id %d\n",
502 intf_id, alloc_id);
503 err = BCM_ERR_INTERNAL;
504 }
505
506 if (err == BCM_ERR_OK) {
507 if (action == ALLOC_OBJECT_CREATE) {
508 if (result.first.state != ALLOC_OBJECT_STATE_ACTIVE) {
509 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object not in active state intf_id %d, alloc_id %d alloc_obj_state %d\n",
510 intf_id, alloc_id, result.first.state);
511 err = BCM_ERR_INTERNAL;
512 } else {
513 OPENOLT_LOG(INFO, openolt_log_id, "Create upstream bandwidth allocation success, intf_id %d, alloc_id %d\n",
514 intf_id, alloc_id);
515 }
516 } else { // ALLOC_OBJECT_DELETE
517 if (result.first.state != ALLOC_OBJECT_STATE_NOT_CONFIGURED) {
518 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object is not reset intf_id %d, alloc_id %d alloc_obj_state %d\n",
519 intf_id, alloc_id, result.first.state);
520 err = BCM_ERR_INTERNAL;
521 } else {
522 OPENOLT_LOG(INFO, openolt_log_id, "Remove alloc object success, intf_id %d, alloc_id %d\n",
523 intf_id, alloc_id);
524 }
525 }
526 }
527
528 // Remove entry from map
529 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
530 alloc_cfg_compltd_map.erase(k);
531 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
Girish Gowdra7a79dae2020-02-10 18:22:11 +0530532 return err;
533}
534
535// This method handles waiting for OnuDeactivate Completed Indication
536bcmos_errno wait_for_onu_deactivate_complete(uint32_t intf_id, uint32_t onu_id) {
537 Queue<onu_deactivate_complete_result> deact_result;
538 onu_deact_compltd_key k(intf_id, onu_id);
539 onu_deact_compltd_map[k] = &deact_result;
540 bcmos_errno err = BCM_ERR_OK;
541
542 // Try to pop the result from BAL with a timeout of ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT ms
543 std::pair<onu_deactivate_complete_result, bool> result = deact_result.pop(ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT);
544 if (result.second == false) {
545 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for onu deactivate complete indication intf_id %d, onu_id %d\n",
546 intf_id, onu_id);
547 // Invalidate the queue pointer.
548 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
549 onu_deact_compltd_map[k] = NULL;
550 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
551 err = BCM_ERR_INTERNAL;
552 }
553 else if (result.first.result == BCMOLT_RESULT_FAIL) {
554 OPENOLT_LOG(ERROR, openolt_log_id, "error processing onu deactivate request intf_id %d, onu_id %d, fail_reason %d\n",
555 intf_id, onu_id, result.first.reason);
556 err = BCM_ERR_INTERNAL;
557 } else if (result.first.result == BCMOLT_RESULT_SUCCESS) {
558 OPENOLT_LOG(INFO, openolt_log_id, "success processing onu deactivate request intf_id %d, onu_id %d\n",
559 intf_id, onu_id);
560 }
561
562 // Remove entry from map
563 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
564 onu_deact_compltd_map.erase(k);
565 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
566
Girish Gowdraddf9a162020-01-27 12:56:27 +0530567 return err;
568}
569
570char* openolt_read_sysinfo(const char* field_name, char* field_val)
571{
572 FILE *fp;
573 /* Prepare the command*/
574 char command[150];
575
576 snprintf(command, sizeof command, "bash -l -c \"onlpdump -s\" | perl -ne 'print $1 if /%s: (\\S+)/'", field_name);
577 /* Open the command for reading. */
578 fp = popen(command, "r");
579 if (fp == NULL) {
580 /*The client has to check for a Null field value in this case*/
581 OPENOLT_LOG(INFO, openolt_log_id, "Failed to query the %s\n", field_name);
582 return field_val;
583 }
584
585 /*Read the field value*/
586 if (fp) {
587 uint8_t ret;
588 ret = fread(field_val, OPENOLT_FIELD_LEN, 1, fp);
589 if (ret >= OPENOLT_FIELD_LEN)
590 OPENOLT_LOG(INFO, openolt_log_id, "Read data length %u\n", ret);
591 pclose(fp);
592 }
593 return field_val;
594}
595
596Status pushOltOperInd(uint32_t intf_id, const char *type, const char *state)
597{
598 openolt::Indication ind;
599 openolt::IntfOperIndication* intf_oper_ind = new openolt::IntfOperIndication;
600
601 intf_oper_ind->set_type(type);
602 intf_oper_ind->set_intf_id(intf_id);
603 intf_oper_ind->set_oper_state(state);
604 ind.set_allocated_intf_oper_ind(intf_oper_ind);
605 oltIndQ.push(ind);
606 return Status::OK;
607}
608
609#define CLI_HOST_PROMPT_FORMAT "BCM.%u> "
610
611/* Build CLI prompt */
612void openolt_cli_get_prompt_cb(bcmcli_session *session, char *buf, uint32_t max_len)
613{
614 snprintf(buf, max_len, CLI_HOST_PROMPT_FORMAT, dev_id);
615}
616
617int _bal_apiend_cli_thread_handler(long data)
618{
619 char init_string[]="\n";
620 bcmcli_session *sess = current_session;
621 bcmos_task_parm bal_cli_task_p_dummy;
622
623 /* Switch to interactive mode if not stopped in the init script */
624 if (!bcmcli_is_stopped(sess)) {
625 /* Force a CLI command prompt
626 * The string passed into the parse function
627 * must be modifiable, so a string constant like
628 * bcmcli_parse(current_session, "\n") will not
629 * work.
630 */
631 bcmcli_parse(sess, init_string);
632
633 /* Process user input until EOF or quit command */
634 bcmcli_driver(sess);
635 }
636 OPENOLT_LOG(INFO, openolt_log_id, "BAL API End CLI terminated\n");
637
638 /* Cleanup */
639 bcmcli_session_close(current_session);
640 bcmcli_token_destroy(NULL);
641 return 0;
642}
643
644/* Init API CLI commands for the current device */
645bcmos_errno bcm_openolt_api_cli_init(bcmcli_entry *parent_dir, bcmcli_session *session)
646{
647 bcmos_errno rc;
648
649 api_parent_dir = parent_dir;
650
651 rc = bcm_api_cli_set_commands(session);
652
653#ifdef BCM_SUBSYSTEM_HOST
654 /* Subscribe for device change indication */
655 rc = rc ? rc : bcmolt_olt_sel_ind_register(_api_cli_olt_change_ind);
656#endif
657
658 return rc;
659}
660
661bcmos_errno bcm_cli_quit(bcmcli_session *session, const bcmcli_cmd_parm parm[], uint16_t n_parms)
662{
663 bcmcli_stop(session);
664 bcmcli_session_print(session, "CLI terminated by 'Quit' command\n");
665 status_bcm_cli_quit = BCMOS_TRUE;
666
667 return BCM_ERR_OK;
668}
669
670int get_status_bcm_cli_quit(void) {
671 return status_bcm_cli_quit;
672}
673
674bcmos_errno bcmolt_apiend_cli_init() {
675 bcmos_errno ret;
676 bcmos_task_parm bal_cli_task_p = {};
677 bcmos_task_parm bal_cli_task_p_dummy;
678
679 /** before creating the task, check if it is already created by the other half of BAL i.e. Core side */
680 if (BCM_ERR_OK != bcmos_task_query(&bal_cli_thread, &bal_cli_task_p_dummy)) {
681 /* Create BAL CLI thread */
682 bal_cli_task_p.name = bal_cli_thread_name;
683 bal_cli_task_p.handler = _bal_apiend_cli_thread_handler;
684 bal_cli_task_p.priority = TASK_PRIORITY_CLI;
685
686 ret = bcmos_task_create(&bal_cli_thread, &bal_cli_task_p);
687 if (BCM_ERR_OK != ret) {
688 bcmos_printf("Couldn't create BAL API end CLI thread\n");
689 return ret;
690 }
691 }
692}
693
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530694bcmos_errno get_onu_status(bcmolt_interface pon_ni, int onu_id, bcmolt_onu_state *onu_state) {
695 bcmos_errno err;
696 bcmolt_onu_cfg onu_cfg;
697 bcmolt_onu_key onu_key;
698 onu_key.pon_ni = pon_ni;
699 onu_key.onu_id = onu_id;
700
701 BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
702 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, onu_state);
703 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, itu);
704 #ifdef TEST_MODE
705 // It is impossible to mock the setting of onu_cfg.data.onu_state because
706 // the actual bcmolt_cfg_get passes the address of onu_cfg.hdr and we cannot
707 // set the onu_cfg.data.onu_state. So a new stub function is created and address
708 // of onu_cfg is passed. This is one-of case where we need to add test specific
709 // code in production code.
710 err = bcmolt_cfg_get__onu_state_stub(dev_id, &onu_cfg);
711 #else
712 err = bcmolt_cfg_get(dev_id, &onu_cfg.hdr);
713 #endif
714 *onu_state = onu_cfg.data.onu_state;
715 return err;
716}
717
718bcmos_errno get_pon_interface_status(bcmolt_interface pon_ni, bcmolt_interface_state *state, bcmolt_status *los_status) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530719 bcmos_errno err;
720 bcmolt_pon_interface_key pon_key;
721 bcmolt_pon_interface_cfg pon_cfg;
722 pon_key.pon_ni = pon_ni;
723
724 BCMOLT_CFG_INIT(&pon_cfg, pon_interface, pon_key);
725 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, state);
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530726 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, los_status);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530727 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, itu);
728 #ifdef TEST_MODE
729 // It is impossible to mock the setting of pon_cfg.data.state because
730 // the actual bcmolt_cfg_get passes the address of pon_cfg.hdr and we cannot
731 // set the pon_cfg.data.state. So a new stub function is created and address
732 // of pon_cfg is passed. This is one-of case where we need to add test specific
733 // code in production code.
734 err = bcmolt_cfg_get__pon_intf_stub(dev_id, &pon_cfg);
735 #else
736 err = bcmolt_cfg_get(dev_id, &pon_cfg.hdr);
737 #endif
738 *state = pon_cfg.data.state;
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530739 *los_status = pon_cfg.data.los_status;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530740 return err;
741}
742
743/* Same as bcmolt_cfg_get but with added logic of retrying the API
744 in case of some specific failures like timeout or object not yet ready
745*/
746bcmos_errno bcmolt_cfg_get_mult_retry(bcmolt_oltid olt, bcmolt_cfg *cfg) {
747 bcmos_errno err;
748 uint32_t current_try = 0;
749
750 while (current_try < MAX_BAL_API_RETRY_COUNT) {
751 err = bcmolt_cfg_get(olt, cfg);
752 current_try++;
753
754 if (err == BCM_ERR_STATE || err == BCM_ERR_TIMEOUT) {
755 OPENOLT_LOG(WARNING, openolt_log_id, "bcmolt_cfg_get: err = %s\n", bcmos_strerror(err));
756 bcmos_usleep(BAL_API_RETRY_TIME_IN_USECS);
757 continue;
758 }
759 else {
760 break;
761 }
762 }
763
764 if (err != BCM_ERR_OK) {
765 OPENOLT_LOG(ERROR, openolt_log_id, "bcmolt_cfg_get tried (%d) times with retry time(%d usecs) err = %s\n",
766 current_try,
767 BAL_API_RETRY_TIME_IN_USECS,
768 bcmos_strerror(err));
769 }
770 return err;
771}
772
773
774unsigned NumNniIf_() {return num_of_nni_ports;}
775unsigned NumPonIf_() {return num_of_pon_ports;}
776
777bcmos_errno get_nni_interface_status(bcmolt_interface id, bcmolt_interface_state *state) {
778 bcmos_errno err;
779 bcmolt_nni_interface_key nni_key;
780 bcmolt_nni_interface_cfg nni_cfg;
781 nni_key.id = id;
782
783 BCMOLT_CFG_INIT(&nni_cfg, nni_interface, nni_key);
784 BCMOLT_FIELD_SET_PRESENT(&nni_cfg.data, nni_interface_cfg_data, state);
785 #ifdef TEST_MODE
786 // It is impossible to mock the setting of nni_cfg.data.state because
787 // the actual bcmolt_cfg_get passes the address of nni_cfg.hdr and we cannot
788 // set the nni_cfg.data.state. So a new stub function is created and address
789 // of nni_cfg is passed. This is one-of case where we need to add test specific
790 // code in production code.
791 err = bcmolt_cfg_get__nni_intf_stub(dev_id, &nni_cfg);
792 #else
793 err = bcmolt_cfg_get(dev_id, &nni_cfg.hdr);
794 #endif
795 *state = nni_cfg.data.state;
796 return err;
797}
798
799Status install_gem_port(int32_t intf_id, int32_t onu_id, int32_t gemport_id) {
800 bcmos_errno err;
801 bcmolt_itupon_gem_cfg cfg; /* declare main API struct */
802 bcmolt_itupon_gem_key key = {}; /* declare key */
803 bcmolt_gem_port_configuration configuration = {};
804
805 key.pon_ni = intf_id;
806 key.gem_port_id = gemport_id;
807
808 BCMOLT_CFG_INIT(&cfg, itupon_gem, key);
809
810 bcmolt_gem_port_direction configuration_direction;
811 configuration_direction = BCMOLT_GEM_PORT_DIRECTION_BIDIRECTIONAL;
812 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, direction, configuration_direction);
813
814 bcmolt_gem_port_type configuration_type;
815 configuration_type = BCMOLT_GEM_PORT_TYPE_UNICAST;
816 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, type, configuration_type);
817
818 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, configuration, configuration);
819
820 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, onu_id, onu_id);
821
822 bcmolt_control_state encryption_mode;
823 encryption_mode = BCMOLT_CONTROL_STATE_DISABLE;
824 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, encryption_mode, encryption_mode);
825
826 bcmolt_us_gem_port_destination upstream_destination_queue;
827 upstream_destination_queue = BCMOLT_US_GEM_PORT_DESTINATION_DATA;
828 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, upstream_destination_queue, upstream_destination_queue);
829
830 bcmolt_control_state control;
831 control = BCMOLT_CONTROL_STATE_ENABLE;
832 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, control, control);
833
834 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
835 if(err != BCM_ERR_OK) {
836 OPENOLT_LOG(ERROR, openolt_log_id, "failed to install gem_port = %d\n", gemport_id);
837 return bcm_to_grpc_err(err, "Access_Control set ITU PON Gem port failed");
838 }
839
840 OPENOLT_LOG(INFO, openolt_log_id, "gem port installed successfully = %d\n", gemport_id);
841
842 return Status::OK;
843}
844
845Status remove_gem_port(int32_t intf_id, int32_t gemport_id) {
846 bcmolt_itupon_gem_cfg gem_cfg;
847 bcmolt_itupon_gem_key key = {
848 .pon_ni = (bcmolt_interface)intf_id,
849 .gem_port_id = (bcmolt_gem_port_id)gemport_id
850 };
851 bcmos_errno err;
852
853 BCMOLT_CFG_INIT(&gem_cfg, itupon_gem, key);
854 err = bcmolt_cfg_clear(dev_id, &gem_cfg.hdr);
855 if (err != BCM_ERR_OK)
856 {
857 OPENOLT_LOG(ERROR, openolt_log_id, "failed to remove gem_port = %d err=%s\n", gemport_id, gem_cfg.hdr.hdr.err_text);
858 return bcm_to_grpc_err(err, "Access_Control clear ITU PON Gem port failed");
859 }
860
861 OPENOLT_LOG(INFO, openolt_log_id, "gem port removed successfully = %d\n", gemport_id);
862
863 return Status::OK;
864}
865
866Status update_acl_interface(int32_t intf_id, bcmolt_interface_type intf_type, uint32_t access_control_id,
867 bcmolt_members_update_command acl_cmd) {
868 bcmos_errno err;
869 bcmolt_access_control_interfaces_update oper; /* declare main API struct */
870 bcmolt_access_control_key acl_key = {}; /* declare key */
871 bcmolt_intf_ref interface_ref_list_elem = {};
872 bcmolt_interface_type interface_ref_list_elem_intf_type;
873 bcmolt_interface_id interface_ref_list_elem_intf_id;
874 bcmolt_intf_ref_list_u8 interface_ref_list = {};
875
876 if (acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_ADD && acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE) {
877 OPENOLT_LOG(ERROR, openolt_log_id, "acl cmd = %d not supported currently\n", acl_cmd);
878 return bcm_to_grpc_err(BCM_ERR_PARM, "unsupported acl cmd");
879 }
880 interface_ref_list.arr = (bcmolt_intf_ref*)bcmos_calloc(sizeof(bcmolt_intf_ref)*1);
881
882 if (interface_ref_list.arr == NULL)
883 return bcm_to_grpc_err(BCM_ERR_PARM, "allocate interface_ref_list failed");
884 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface received for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
885 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
886 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
887
888 acl_key.id = access_control_id;
889
890 /* Initialize the API struct. */
891 BCMOLT_OPER_INIT(&oper, access_control, interfaces_update, acl_key);
892
893 bcmolt_members_update_command command;
894 command = acl_cmd;
895 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, command, command);
896
897 interface_ref_list_elem_intf_type = intf_type;
898 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_type, interface_ref_list_elem_intf_type);
899
900 interface_ref_list_elem_intf_id = intf_id;
901 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_id, interface_ref_list_elem_intf_id);
902
903 interface_ref_list.len = 1;
904 BCMOLT_ARRAY_ELEM_SET(&interface_ref_list, 0, interface_ref_list_elem);
905
906 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, interface_ref_list, interface_ref_list);
907
908 err = bcmolt_oper_submit(dev_id, &oper.hdr);
909 if (err != BCM_ERR_OK) {
910 OPENOLT_LOG(ERROR, openolt_log_id, "update acl interface fail for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
911 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
912 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
913 return bcm_to_grpc_err(err, "Access_Control submit interface failed");
914 }
915
916 bcmos_free(interface_ref_list.arr);
917 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface success for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
918 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
919 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
920
921 return Status::OK;
922}
923
924Status install_acl(const acl_classifier_key acl_key) {
925
926 bcmos_errno err;
927 bcmolt_access_control_cfg cfg;
928 bcmolt_access_control_key key = { };
929 bcmolt_classifier c_val = { };
930 // hardcode the action for now.
931 bcmolt_access_control_fwd_action_type action_type = BCMOLT_ACCESS_CONTROL_FWD_ACTION_TYPE_TRAP_TO_HOST;
932
933 int acl_id = get_acl_id();
934 if (acl_id < 0) {
935 OPENOLT_LOG(ERROR, openolt_log_id, "exhausted acl_id for eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d\n",
936 acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port);
937 bcmos_fastlock_unlock(&data_lock, 0);
938 return bcm_to_grpc_err(BCM_ERR_INTERNAL, "exhausted acl id");
939 }
940
941 key.id = acl_id;
942 /* config access control instance */
943 BCMOLT_CFG_INIT(&cfg, access_control, key);
944
945 if (acl_key.ether_type > 0) {
946 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ether_type 0x%04x\n", acl_key.ether_type);
947 BCMOLT_FIELD_SET(&c_val, classifier, ether_type, acl_key.ether_type);
948 }
949
950 if (acl_key.ip_proto > 0) {
951 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ip_proto %d\n", acl_key.ip_proto);
952 BCMOLT_FIELD_SET(&c_val, classifier, ip_proto, acl_key.ip_proto);
953 }
954
955 if (acl_key.dst_port > 0) {
956 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify dst_port %d\n", acl_key.dst_port);
957 BCMOLT_FIELD_SET(&c_val, classifier, dst_port, acl_key.dst_port);
958 }
959
960 if (acl_key.src_port > 0) {
961 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify src_port %d\n", acl_key.src_port);
962 BCMOLT_FIELD_SET(&c_val, classifier, src_port, acl_key.src_port);
963 }
964
965 BCMOLT_MSG_FIELD_SET(&cfg, classifier, c_val);
966 BCMOLT_MSG_FIELD_SET(&cfg, priority, 10000);
967 BCMOLT_MSG_FIELD_SET(&cfg, statistics_control, BCMOLT_CONTROL_STATE_ENABLE);
968
969 BCMOLT_MSG_FIELD_SET(&cfg, forwarding_action.action, action_type);
970
971 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
972 if (err != BCM_ERR_OK) {
973 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control set configuration failed, Error %d\n", err);
974 // Free the acl_id
975 free_acl_id(acl_id);
976 return bcm_to_grpc_err(err, "Access_Control set configuration failed");
977 }
978
979 ACL_LOG(INFO, "ACL add ok", err);
980
981 // Update the map that we have installed an acl for the given classfier.
982 acl_classifier_to_acl_id_map[acl_key] = acl_id;
983 return Status::OK;
984}
985
986Status remove_acl(int acl_id) {
987 bcmos_errno err;
988 bcmolt_access_control_cfg cfg; /* declare main API struct */
989 bcmolt_access_control_key key = {}; /* declare key */
990
991 key.id = acl_id;
992
993 /* Initialize the API struct. */
994 BCMOLT_CFG_INIT(&cfg, access_control, key);
995 BCMOLT_FIELD_SET_PRESENT(&cfg.data, access_control_cfg_data, state);
996 err = bcmolt_cfg_get(dev_id, &cfg.hdr);
997 if (err != BCM_ERR_OK) {
998 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control get state failed\n");
999 return bcm_to_grpc_err(err, "Access_Control get state failed");
1000 }
1001
1002 if (cfg.data.state == BCMOLT_CONFIG_STATE_CONFIGURED) {
1003 key.id = acl_id;
1004 /* Initialize the API struct. */
1005 BCMOLT_CFG_INIT(&cfg, access_control, key);
1006
1007 err = bcmolt_cfg_clear(dev_id, &cfg.hdr);
1008 if (err != BCM_ERR_OK) {
1009 // Should we free acl_id here ? We should ideally never land here..
1010 OPENOLT_LOG(ERROR, openolt_log_id, "Error %d while removing Access_Control rule ID %d\n",
1011 err, acl_id);
1012 return Status(grpc::StatusCode::INTERNAL, "Failed to remove Access_Control");
1013 }
1014 }
1015
1016 // Free up acl_id
1017 free_acl_id(acl_id);
1018
1019 OPENOLT_LOG(INFO, openolt_log_id, "acl removed successfully %d\n", acl_id);
1020
1021 return Status::OK;
1022}
1023
1024// Formulates ACL Classifier Key based on the following fields
1025// a. ether_type b. ip_proto c. src_port d. dst_port
1026// If any of the field is not available it is populated as -1.
1027void formulate_acl_classifier_key(acl_classifier_key *key, const ::openolt::Classifier& classifier) {
1028
1029 // TODO: Is 0 a valid value for any of the following classifiers?
1030 // because in the that case, the 'if' check would fail and -1 would be filled as value.
1031 //
1032 if (classifier.eth_type()) {
1033 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ether_type 0x%04x\n", classifier.eth_type());
1034 key->ether_type = classifier.eth_type();
1035 } else key->ether_type = -1;
1036
1037 if (classifier.ip_proto()) {
1038 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ip_proto %d\n", classifier.ip_proto());
1039 key->ip_proto = classifier.ip_proto();
1040 } else key->ip_proto = -1;
1041
1042
1043 if (classifier.src_port()) {
1044 OPENOLT_LOG(DEBUG, openolt_log_id, "classify src_port %d\n", classifier.src_port());
1045 key->src_port = classifier.src_port();
1046 } else key->src_port = -1;
1047
1048
1049 if (classifier.dst_port()) {
1050 OPENOLT_LOG(DEBUG, openolt_log_id, "classify dst_port %d\n", classifier.dst_port());
1051 key->dst_port = classifier.dst_port();
1052 } else key->dst_port = -1;
1053}
1054
1055Status handle_acl_rule_install(int32_t onu_id, uint32_t flow_id,
1056 const std::string flow_type, int32_t access_intf_id,
1057 int32_t network_intf_id, int32_t gemport_id,
1058 const ::openolt::Classifier& classifier) {
1059 int acl_id;
1060 int32_t intf_id = flow_type.compare(upstream) == 0? access_intf_id: network_intf_id;
1061 const std::string intf_type = flow_type.compare(upstream) == 0? "pon": "nni";
1062 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1063
1064 Status resp;
1065
1066 // few map keys we are going to use later.
1067 flow_id_flow_direction fl_id_fl_dir(flow_id, flow_type);
1068 gem_id_intf_id gem_intf(gemport_id, access_intf_id);
1069 acl_classifier_key acl_key;
1070 formulate_acl_classifier_key(&acl_key, classifier);
1071 const acl_classifier_key acl_key_const = {.ether_type=acl_key.ether_type, .ip_proto=acl_key.ip_proto,
1072 .src_port=acl_key.src_port, .dst_port=acl_key.dst_port};
1073
1074 bcmos_fastlock_lock(&data_lock);
1075
1076 // Check if the acl is already installed
1077 if (acl_classifier_to_acl_id_map.count(acl_key_const) > 0) {
1078 // retreive the acl_id
1079 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
1080 acl_id_gem_id_intf_id ac_id_gm_id_if_id(acl_id, gemport_id, intf_id);
1081 if (flow_to_acl_map.count(fl_id_fl_dir)) {
1082 // coult happen if same trap flow is received again
1083 OPENOLT_LOG(INFO, openolt_log_id, "flow and related acl already handled, nothing more to do\n");
1084 bcmos_fastlock_unlock(&data_lock, 0);
1085 return Status::OK;
1086 }
1087
1088 OPENOLT_LOG(INFO, openolt_log_id, "Acl for flow_id=%u with eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d already installed with acl id = %u\n",
1089 flow_id, acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port, acl_id);
1090
1091 // The acl_ref_cnt is needed to know how many flows refer an ACL.
1092 // When the flow is removed, we decrement the reference count.
1093 // When the reference count becomes 0, we remove the ACL.
1094 if (acl_ref_cnt.count(acl_id) > 0) {
1095 acl_ref_cnt[acl_id] ++;
1096 } else {
1097 // We should ideally not land here. The acl_ref_cnt should have been
1098 // initialized the first time acl was installed.
1099 acl_ref_cnt[acl_id] = 1;
1100 }
1101
1102 } else {
1103 resp = install_acl(acl_key_const);
1104 if (!resp.ok()) {
1105 OPENOLT_LOG(ERROR, openolt_log_id, "Acl for flow_id=%u with eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d failed\n",
1106 flow_id, acl_key_const.ether_type, acl_key_const.ip_proto, acl_key_const.src_port, acl_key_const.dst_port);
1107 bcmos_fastlock_unlock(&data_lock, 0);
1108 return resp;
1109 }
1110
1111 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
1112
1113 // Initialize the acl reference count
1114 acl_ref_cnt[acl_id] = 1;
1115
1116 OPENOLT_LOG(INFO, openolt_log_id, "acl add success for flow_id=%u with acl_id=%d\n", flow_id, acl_id);
1117 }
1118
1119 // Register the interface for the given acl
1120 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1121 // This is needed to keep a track of which interface (pon/nni) has registered for an ACL.
1122 // If it is registered, how many flows refer to it.
1123 if (intf_acl_registration_ref_cnt.count(ac_id_inf_id_inf_type) > 0) {
1124 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]++;
1125 } else {
1126 // The given interface is not registered for the ACL. We need to do it now.
1127 resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_ADD);
1128 if (!resp.ok()){
1129 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);
1130 // TODO: Ideally we should return error from hear and clean up other other stateful
1131 // counters we creaed earlier. Will leave it out for now.
1132 }
1133 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] = 1;
1134 }
1135
1136
1137 // Install the gem port if needed.
1138 if (gemport_id > 0 && access_intf_id >= 0) {
1139 if (gem_ref_cnt.count(gem_intf) > 0) {
1140 // The gem port is already installed
1141 // Increment the ref counter indicating number of flows referencing this gem port
1142 gem_ref_cnt[gem_intf]++;
1143 OPENOLT_LOG(DEBUG, openolt_log_id, "increment gem_ref_cnt in acl handler, ref_cnt=%d\n", gem_ref_cnt[gem_intf]);
1144
1145 } else {
1146 // We should ideally never land here. The gem port should have been created the
1147 // first time ACL was installed.
1148 // Install the gem port
1149 Status resp = install_gem_port(access_intf_id, onu_id, gemport_id);
1150 if (!resp.ok()) {
1151 // TODO: We might need to reverse all previous data, but leave it out for now.
1152 OPENOLT_LOG(ERROR, openolt_log_id, "failed to install the gemport=%d for acl_id=%d, intf_id=%d\n", gemport_id, acl_id, access_intf_id);
1153 bcmos_fastlock_unlock(&data_lock, 0);
1154 return resp;
1155 }
1156 // Initialize the refence count for the gemport.
1157 gem_ref_cnt[gem_intf] = 1;
1158 OPENOLT_LOG(DEBUG, openolt_log_id, "intialized gem ref count in acl handler\n");
1159 }
1160 } else {
1161 OPENOLT_LOG(DEBUG, openolt_log_id, "not incrementing gem_ref_cnt in acl handler flow_id=%d, gemport_id=%d, intf_id=%d\n", flow_id, gemport_id, access_intf_id);
1162 }
1163
1164 // Update the flow_to_acl_map
1165 // This info is needed during flow remove. We need to which ACL ID and GEM PORT ID
1166 // the flow was referring to.
1167 // After retrieving the ACL ID and GEM PORT ID, we decrement the corresponding
1168 // reference counters for those ACL ID and GEMPORT ID.
1169 acl_id_gem_id_intf_id ac_id_gm_id_if_id(acl_id, gemport_id, intf_id);
1170 flow_to_acl_map[fl_id_fl_dir] = ac_id_gm_id_if_id;
1171
1172 bcmos_fastlock_unlock(&data_lock, 0);
1173
1174 return Status::OK;
1175}
1176
1177void clear_gem_port(int gemport_id, int access_intf_id) {
1178 gem_id_intf_id gem_intf(gemport_id, access_intf_id);
1179 if (gemport_id > 0 && access_intf_id >= 0 && gem_ref_cnt.count(gem_intf) > 0) {
1180 OPENOLT_LOG(DEBUG, openolt_log_id, "decrementing gem_ref_cnt gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1181 gem_ref_cnt[gem_intf]--;
1182 if (gem_ref_cnt[gem_intf] == 0) {
1183 // For datapath flow this may not be necessary (to be verified)
1184 remove_gem_port(access_intf_id, gemport_id);
1185 gem_ref_cnt.erase(gem_intf);
1186 OPENOLT_LOG(DEBUG, openolt_log_id, "removing gem_ref_cnt entry gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1187 } else {
1188 OPENOLT_LOG(DEBUG, openolt_log_id, "gem_ref_cnt not zero yet gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1189 }
1190 } else {
1191 OPENOLT_LOG(DEBUG, openolt_log_id, "not decrementing gem_ref_cnt gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1192 }
1193}
1194
1195Status handle_acl_rule_cleanup(int16_t acl_id, int32_t gemport_id, int32_t intf_id, const std::string flow_type) {
1196 const std::string intf_type= flow_type.compare(upstream) == 0 ? "pon": "nni";
1197 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1198 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]--;
1199 if (intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] == 0) {
1200 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1201 Status resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE);
1202 if (!resp.ok()){
1203 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);
1204 }
1205 intf_acl_registration_ref_cnt.erase(ac_id_inf_id_inf_type);
1206 }
1207
1208 acl_ref_cnt[acl_id]--;
1209 if (acl_ref_cnt[acl_id] == 0) {
1210 remove_acl(acl_id);
1211 acl_ref_cnt.erase(acl_id);
1212 // Iterate acl_classifier_to_acl_id_map and delete classifier the key corresponding to acl_id
1213 std::map<acl_classifier_key, uint16_t>::iterator it;
1214 for (it=acl_classifier_to_acl_id_map.begin(); it!=acl_classifier_to_acl_id_map.end(); ++it) {
1215 if (it->second == acl_id) {
1216 OPENOLT_LOG(INFO, openolt_log_id, "cleared classifier key corresponding to acl_id = %d\n", acl_id);
1217 acl_classifier_to_acl_id_map.erase(it->first);
1218 break;
1219 }
1220 }
1221 }
1222
1223 clear_gem_port(gemport_id, intf_id);
1224
1225 return Status::OK;
1226}
1227
1228Status check_bal_ready() {
1229 bcmos_errno err;
1230 int maxTrials = 30;
1231 bcmolt_olt_cfg olt_cfg = { };
1232 bcmolt_olt_key olt_key = { };
1233
1234 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
1235 BCMOLT_MSG_FIELD_GET(&olt_cfg, bal_state);
1236
1237 while (olt_cfg.data.bal_state != BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY) {
1238 if (--maxTrials == 0)
1239 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check bal ready failed");
1240 sleep(5);
1241 #ifdef TEST_MODE
1242 // It is impossible to mock the setting of olt_cfg.data.bal_state because
1243 // the actual bcmolt_cfg_get passes the address of olt_cfg.hdr and we cannot
1244 // set the olt_cfg.data.bal_state. So a new stub function is created and address
1245 // of olt_cfg is passed. This is one-of case where we need to add test specific
1246 // code in production code.
1247 if (bcmolt_cfg_get__bal_state_stub(dev_id, &olt_cfg)) {
1248 #else
1249 if (bcmolt_cfg_get(dev_id, &olt_cfg.hdr)) {
1250 #endif
1251 continue;
1252 }
1253 else
1254 OPENOLT_LOG(INFO, openolt_log_id, "waiting for BAL ready ...\n");
1255 }
1256
1257 OPENOLT_LOG(INFO, openolt_log_id, "BAL is ready\n");
1258 return Status::OK;
1259}
1260
1261Status check_connection() {
1262 int maxTrials = 60;
1263 while (!bcmolt_api_conn_mgr_is_connected(dev_id)) {
1264 sleep(1);
1265 if (--maxTrials == 0)
1266 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check connection failed");
1267 else
1268 OPENOLT_LOG(INFO, openolt_log_id, "waiting for daemon connection ...\n");
1269 }
1270 OPENOLT_LOG(INFO, openolt_log_id, "daemon is connected\n");
1271 return Status::OK;
1272}
1273