blob: 32de09d796a1feee6b14eb3a17010723fbfcccdc [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
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530363/* ACL ID is a shared resource, caller of this function has to ensure atomicity using locks
364 Gets free ACL ID if available, else -1. */
Girish Gowdraddf9a162020-01-27 12:56:27 +0530365int get_acl_id() {
366 int acl_id;
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530367
Girish Gowdraddf9a162020-01-27 12:56:27 +0530368 /* Complexity of O(n). Is there better way that can avoid linear search? */
369 for (acl_id = 0; acl_id < MAX_ACL_ID; acl_id++) {
370 if (acl_id_bitset[acl_id] == 0) {
371 acl_id_bitset[acl_id] = 1;
372 break;
373 }
374 }
Girish Gowdraddf9a162020-01-27 12:56:27 +0530375 if (acl_id < MAX_ACL_ID) {
376 return acl_id ;
377 } else {
378 return -1;
379 }
380}
381
Thiyagarajan Subramani1744c922020-02-16 18:55:02 +0530382/* ACL ID is a shared resource, caller of this function has to ensure atomicity using locks
383 Frees up the ACL ID. */
Girish Gowdraddf9a162020-01-27 12:56:27 +0530384void free_acl_id (int acl_id) {
385 if (acl_id < MAX_ACL_ID) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530386 acl_id_bitset[acl_id] = 0;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530387 }
388}
389
390/**
391* Returns qos type as string
392*
393* @param qos_type bcmolt_egress_qos_type enum
394*/
395std::string get_qos_type_as_string(bcmolt_egress_qos_type qos_type) {
396 switch (qos_type)
397 {
398 case BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE: return "FIXED_QUEUE";
399 case BCMOLT_EGRESS_QOS_TYPE_TC_TO_QUEUE: return "TC_TO_QUEUE";
400 case BCMOLT_EGRESS_QOS_TYPE_PBIT_TO_TC: return "PBIT_TO_TC";
401 case BCMOLT_EGRESS_QOS_TYPE_NONE: return "NONE";
402 case BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE: return "PRIORITY_TO_QUEUE";
403 default: OPENOLT_LOG(ERROR, openolt_log_id, "qos-type-not-supported %d\n", qos_type);
404 return "qos-type-not-supported";
405 }
406}
407
408/**
409* Gets/Updates qos type for given pon_intf_id, onu_id, uni_id
410*
411* @param PON intf ID
412* @param onu_id ONU ID
413* @param uni_id UNI ID
414* @param queue_size TrafficQueues Size
415*
416* @return qos_type
417*/
418bcmolt_egress_qos_type get_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id, uint32_t queue_size) {
419 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
420 bcmolt_egress_qos_type egress_qos_type = BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
421 std::string qos_string;
422
423 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
424 if (it != qos_type_map.end()) {
425 egress_qos_type = it->second;
426 qos_string = get_qos_type_as_string(egress_qos_type);
427 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", \
428 pon_intf_id, onu_id, uni_id, qos_string.c_str());
429 }
430 else {
431 /* QOS Type has been pre-defined as Fixed Queue but it will be updated based on number of GEMPORTS
432 associated for a given subscriber. If GEM count = 1 for a given subscriber, qos_type will be Fixed Queue
433 else Priority to Queue */
434 egress_qos_type = (queue_size > 1) ? \
435 BCMOLT_EGRESS_QOS_TYPE_PRIORITY_TO_QUEUE : BCMOLT_EGRESS_QOS_TYPE_FIXED_QUEUE;
436 bcmos_fastlock_lock(&data_lock);
437 qos_type_map.insert(make_pair(key, egress_qos_type));
438 bcmos_fastlock_unlock(&data_lock, 0);
439 qos_string = get_qos_type_as_string(egress_qos_type);
440 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", \
441 pon_intf_id, onu_id, uni_id, qos_string.c_str());
442 }
443 return egress_qos_type;
444}
445
446/**
447* Clears qos type for given pon_intf_id, onu_id, uni_id
448*
449* @param PON intf ID
450* @param onu_id ONU ID
451* @param uni_id UNI ID
452*/
453void clear_qos_type(uint32_t pon_intf_id, uint32_t onu_id, uint32_t uni_id) {
454 qos_type_map_key_tuple key(pon_intf_id, onu_id, uni_id);
455 std::map<qos_type_map_key_tuple, bcmolt_egress_qos_type>::const_iterator it = qos_type_map.find(key);
456 bcmos_fastlock_lock(&data_lock);
457 if (it != qos_type_map.end()) {
458 qos_type_map.erase(it);
459 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", \
460 pon_intf_id, onu_id, uni_id);
461 }
462 bcmos_fastlock_unlock(&data_lock, 0);
463}
464
465/**
466* Returns Scheduler/Queue direction as string
467*
468* @param direction as specified in tech_profile.proto
469*/
470std::string GetDirection(int direction) {
471 switch (direction)
472 {
473 case tech_profile::Direction::UPSTREAM: return upstream;
474 case tech_profile::Direction::DOWNSTREAM: return downstream;
475 default: OPENOLT_LOG(ERROR, openolt_log_id, "direction-not-supported %d\n", direction);
476 return "direction-not-supported";
477 }
478}
479
480// This method handles waiting for AllocObject configuration.
481// Returns error if the AllocObject is not in the appropriate state based on action requested.
482bcmos_errno wait_for_alloc_action(uint32_t intf_id, uint32_t alloc_id, AllocCfgAction action) {
483 Queue<alloc_cfg_complete_result> cfg_result;
484 alloc_cfg_compltd_key k(intf_id, alloc_id);
485 alloc_cfg_compltd_map[k] = &cfg_result;
486 bcmos_errno err = BCM_ERR_OK;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530487
488 // Try to pop the result from BAL with a timeout of ALLOC_CFG_COMPLETE_WAIT_TIMEOUT ms
489 std::pair<alloc_cfg_complete_result, bool> result = cfg_result.pop(ALLOC_CFG_COMPLETE_WAIT_TIMEOUT);
490 if (result.second == false) {
491 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for alloc cfg complete indication intf_id %d, alloc_id %d\n",
492 intf_id, alloc_id);
493 // Invalidate the queue pointer.
494 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
495 alloc_cfg_compltd_map[k] = NULL;
496 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
497 err = BCM_ERR_INTERNAL;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530498 }
499 else if (result.first.status == ALLOC_CFG_STATUS_FAIL) {
500 OPENOLT_LOG(ERROR, openolt_log_id, "error processing alloc cfg request intf_id %d, alloc_id %d\n",
501 intf_id, alloc_id);
502 err = BCM_ERR_INTERNAL;
503 }
504
505 if (err == BCM_ERR_OK) {
506 if (action == ALLOC_OBJECT_CREATE) {
507 if (result.first.state != ALLOC_OBJECT_STATE_ACTIVE) {
508 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object not in active state intf_id %d, alloc_id %d alloc_obj_state %d\n",
509 intf_id, alloc_id, result.first.state);
510 err = BCM_ERR_INTERNAL;
511 } else {
512 OPENOLT_LOG(INFO, openolt_log_id, "Create upstream bandwidth allocation success, intf_id %d, alloc_id %d\n",
513 intf_id, alloc_id);
514 }
515 } else { // ALLOC_OBJECT_DELETE
516 if (result.first.state != ALLOC_OBJECT_STATE_NOT_CONFIGURED) {
517 OPENOLT_LOG(ERROR, openolt_log_id, "alloc object is not reset intf_id %d, alloc_id %d alloc_obj_state %d\n",
518 intf_id, alloc_id, result.first.state);
519 err = BCM_ERR_INTERNAL;
520 } else {
521 OPENOLT_LOG(INFO, openolt_log_id, "Remove alloc object success, intf_id %d, alloc_id %d\n",
522 intf_id, alloc_id);
523 }
524 }
525 }
526
527 // Remove entry from map
528 bcmos_fastlock_lock(&alloc_cfg_wait_lock);
529 alloc_cfg_compltd_map.erase(k);
530 bcmos_fastlock_unlock(&alloc_cfg_wait_lock, 0);
Girish Gowdra7a79dae2020-02-10 18:22:11 +0530531 return err;
532}
533
534// This method handles waiting for OnuDeactivate Completed Indication
535bcmos_errno wait_for_onu_deactivate_complete(uint32_t intf_id, uint32_t onu_id) {
536 Queue<onu_deactivate_complete_result> deact_result;
537 onu_deact_compltd_key k(intf_id, onu_id);
538 onu_deact_compltd_map[k] = &deact_result;
539 bcmos_errno err = BCM_ERR_OK;
540
541 // Try to pop the result from BAL with a timeout of ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT ms
542 std::pair<onu_deactivate_complete_result, bool> result = deact_result.pop(ONU_DEACTIVATE_COMPLETE_WAIT_TIMEOUT);
543 if (result.second == false) {
544 OPENOLT_LOG(ERROR, openolt_log_id, "timeout waiting for onu deactivate complete indication intf_id %d, onu_id %d\n",
545 intf_id, onu_id);
546 // Invalidate the queue pointer.
547 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
548 onu_deact_compltd_map[k] = NULL;
549 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
550 err = BCM_ERR_INTERNAL;
551 }
552 else if (result.first.result == BCMOLT_RESULT_FAIL) {
553 OPENOLT_LOG(ERROR, openolt_log_id, "error processing onu deactivate request intf_id %d, onu_id %d, fail_reason %d\n",
554 intf_id, onu_id, result.first.reason);
555 err = BCM_ERR_INTERNAL;
556 } else if (result.first.result == BCMOLT_RESULT_SUCCESS) {
557 OPENOLT_LOG(INFO, openolt_log_id, "success processing onu deactivate request intf_id %d, onu_id %d\n",
558 intf_id, onu_id);
559 }
560
561 // Remove entry from map
562 bcmos_fastlock_lock(&onu_deactivate_wait_lock);
563 onu_deact_compltd_map.erase(k);
564 bcmos_fastlock_unlock(&onu_deactivate_wait_lock, 0);
565
Girish Gowdraddf9a162020-01-27 12:56:27 +0530566 return err;
567}
568
569char* openolt_read_sysinfo(const char* field_name, char* field_val)
570{
571 FILE *fp;
572 /* Prepare the command*/
573 char command[150];
574
575 snprintf(command, sizeof command, "bash -l -c \"onlpdump -s\" | perl -ne 'print $1 if /%s: (\\S+)/'", field_name);
576 /* Open the command for reading. */
577 fp = popen(command, "r");
578 if (fp == NULL) {
579 /*The client has to check for a Null field value in this case*/
580 OPENOLT_LOG(INFO, openolt_log_id, "Failed to query the %s\n", field_name);
581 return field_val;
582 }
583
584 /*Read the field value*/
585 if (fp) {
586 uint8_t ret;
587 ret = fread(field_val, OPENOLT_FIELD_LEN, 1, fp);
588 if (ret >= OPENOLT_FIELD_LEN)
589 OPENOLT_LOG(INFO, openolt_log_id, "Read data length %u\n", ret);
590 pclose(fp);
591 }
592 return field_val;
593}
594
595Status pushOltOperInd(uint32_t intf_id, const char *type, const char *state)
596{
597 openolt::Indication ind;
598 openolt::IntfOperIndication* intf_oper_ind = new openolt::IntfOperIndication;
599
600 intf_oper_ind->set_type(type);
601 intf_oper_ind->set_intf_id(intf_id);
602 intf_oper_ind->set_oper_state(state);
603 ind.set_allocated_intf_oper_ind(intf_oper_ind);
604 oltIndQ.push(ind);
605 return Status::OK;
606}
607
608#define CLI_HOST_PROMPT_FORMAT "BCM.%u> "
609
610/* Build CLI prompt */
611void openolt_cli_get_prompt_cb(bcmcli_session *session, char *buf, uint32_t max_len)
612{
613 snprintf(buf, max_len, CLI_HOST_PROMPT_FORMAT, dev_id);
614}
615
616int _bal_apiend_cli_thread_handler(long data)
617{
618 char init_string[]="\n";
619 bcmcli_session *sess = current_session;
620 bcmos_task_parm bal_cli_task_p_dummy;
621
622 /* Switch to interactive mode if not stopped in the init script */
623 if (!bcmcli_is_stopped(sess)) {
624 /* Force a CLI command prompt
625 * The string passed into the parse function
626 * must be modifiable, so a string constant like
627 * bcmcli_parse(current_session, "\n") will not
628 * work.
629 */
630 bcmcli_parse(sess, init_string);
631
632 /* Process user input until EOF or quit command */
633 bcmcli_driver(sess);
634 }
635 OPENOLT_LOG(INFO, openolt_log_id, "BAL API End CLI terminated\n");
636
637 /* Cleanup */
638 bcmcli_session_close(current_session);
639 bcmcli_token_destroy(NULL);
640 return 0;
641}
642
643/* Init API CLI commands for the current device */
644bcmos_errno bcm_openolt_api_cli_init(bcmcli_entry *parent_dir, bcmcli_session *session)
645{
646 bcmos_errno rc;
647
648 api_parent_dir = parent_dir;
649
650 rc = bcm_api_cli_set_commands(session);
651
652#ifdef BCM_SUBSYSTEM_HOST
653 /* Subscribe for device change indication */
654 rc = rc ? rc : bcmolt_olt_sel_ind_register(_api_cli_olt_change_ind);
655#endif
656
657 return rc;
658}
659
660bcmos_errno bcm_cli_quit(bcmcli_session *session, const bcmcli_cmd_parm parm[], uint16_t n_parms)
661{
662 bcmcli_stop(session);
663 bcmcli_session_print(session, "CLI terminated by 'Quit' command\n");
664 status_bcm_cli_quit = BCMOS_TRUE;
665
666 return BCM_ERR_OK;
667}
668
669int get_status_bcm_cli_quit(void) {
670 return status_bcm_cli_quit;
671}
672
673bcmos_errno bcmolt_apiend_cli_init() {
674 bcmos_errno ret;
675 bcmos_task_parm bal_cli_task_p = {};
676 bcmos_task_parm bal_cli_task_p_dummy;
677
678 /** before creating the task, check if it is already created by the other half of BAL i.e. Core side */
679 if (BCM_ERR_OK != bcmos_task_query(&bal_cli_thread, &bal_cli_task_p_dummy)) {
680 /* Create BAL CLI thread */
681 bal_cli_task_p.name = bal_cli_thread_name;
682 bal_cli_task_p.handler = _bal_apiend_cli_thread_handler;
683 bal_cli_task_p.priority = TASK_PRIORITY_CLI;
684
685 ret = bcmos_task_create(&bal_cli_thread, &bal_cli_task_p);
686 if (BCM_ERR_OK != ret) {
687 bcmos_printf("Couldn't create BAL API end CLI thread\n");
688 return ret;
689 }
690 }
691}
692
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530693bcmos_errno get_onu_status(bcmolt_interface pon_ni, int onu_id, bcmolt_onu_state *onu_state) {
694 bcmos_errno err;
695 bcmolt_onu_cfg onu_cfg;
696 bcmolt_onu_key onu_key;
697 onu_key.pon_ni = pon_ni;
698 onu_key.onu_id = onu_id;
699
700 BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
701 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, onu_state);
702 BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, itu);
703 #ifdef TEST_MODE
704 // It is impossible to mock the setting of onu_cfg.data.onu_state because
705 // the actual bcmolt_cfg_get passes the address of onu_cfg.hdr and we cannot
706 // set the onu_cfg.data.onu_state. So a new stub function is created and address
707 // of onu_cfg is passed. This is one-of case where we need to add test specific
708 // code in production code.
709 err = bcmolt_cfg_get__onu_state_stub(dev_id, &onu_cfg);
710 #else
711 err = bcmolt_cfg_get(dev_id, &onu_cfg.hdr);
712 #endif
713 *onu_state = onu_cfg.data.onu_state;
714 return err;
715}
716
717bcmos_errno get_pon_interface_status(bcmolt_interface pon_ni, bcmolt_interface_state *state, bcmolt_status *los_status) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530718 bcmos_errno err;
719 bcmolt_pon_interface_key pon_key;
720 bcmolt_pon_interface_cfg pon_cfg;
721 pon_key.pon_ni = pon_ni;
722
723 BCMOLT_CFG_INIT(&pon_cfg, pon_interface, pon_key);
724 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, state);
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530725 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, los_status);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530726 BCMOLT_FIELD_SET_PRESENT(&pon_cfg.data, pon_interface_cfg_data, itu);
727 #ifdef TEST_MODE
728 // It is impossible to mock the setting of pon_cfg.data.state because
729 // the actual bcmolt_cfg_get passes the address of pon_cfg.hdr and we cannot
730 // set the pon_cfg.data.state. So a new stub function is created and address
731 // of pon_cfg is passed. This is one-of case where we need to add test specific
732 // code in production code.
733 err = bcmolt_cfg_get__pon_intf_stub(dev_id, &pon_cfg);
734 #else
735 err = bcmolt_cfg_get(dev_id, &pon_cfg.hdr);
736 #endif
737 *state = pon_cfg.data.state;
Thiyagarajan Subramaniad463232020-02-28 19:10:43 +0530738 *los_status = pon_cfg.data.los_status;
Girish Gowdraddf9a162020-01-27 12:56:27 +0530739 return err;
740}
741
742/* Same as bcmolt_cfg_get but with added logic of retrying the API
743 in case of some specific failures like timeout or object not yet ready
744*/
745bcmos_errno bcmolt_cfg_get_mult_retry(bcmolt_oltid olt, bcmolt_cfg *cfg) {
746 bcmos_errno err;
747 uint32_t current_try = 0;
748
749 while (current_try < MAX_BAL_API_RETRY_COUNT) {
750 err = bcmolt_cfg_get(olt, cfg);
751 current_try++;
752
753 if (err == BCM_ERR_STATE || err == BCM_ERR_TIMEOUT) {
754 OPENOLT_LOG(WARNING, openolt_log_id, "bcmolt_cfg_get: err = %s\n", bcmos_strerror(err));
755 bcmos_usleep(BAL_API_RETRY_TIME_IN_USECS);
756 continue;
757 }
758 else {
759 break;
760 }
761 }
762
763 if (err != BCM_ERR_OK) {
764 OPENOLT_LOG(ERROR, openolt_log_id, "bcmolt_cfg_get tried (%d) times with retry time(%d usecs) err = %s\n",
765 current_try,
766 BAL_API_RETRY_TIME_IN_USECS,
767 bcmos_strerror(err));
768 }
769 return err;
770}
771
772
773unsigned NumNniIf_() {return num_of_nni_ports;}
774unsigned NumPonIf_() {return num_of_pon_ports;}
775
776bcmos_errno get_nni_interface_status(bcmolt_interface id, bcmolt_interface_state *state) {
777 bcmos_errno err;
778 bcmolt_nni_interface_key nni_key;
779 bcmolt_nni_interface_cfg nni_cfg;
780 nni_key.id = id;
781
782 BCMOLT_CFG_INIT(&nni_cfg, nni_interface, nni_key);
783 BCMOLT_FIELD_SET_PRESENT(&nni_cfg.data, nni_interface_cfg_data, state);
784 #ifdef TEST_MODE
785 // It is impossible to mock the setting of nni_cfg.data.state because
786 // the actual bcmolt_cfg_get passes the address of nni_cfg.hdr and we cannot
787 // set the nni_cfg.data.state. So a new stub function is created and address
788 // of nni_cfg is passed. This is one-of case where we need to add test specific
789 // code in production code.
790 err = bcmolt_cfg_get__nni_intf_stub(dev_id, &nni_cfg);
791 #else
792 err = bcmolt_cfg_get(dev_id, &nni_cfg.hdr);
793 #endif
794 *state = nni_cfg.data.state;
795 return err;
796}
797
798Status install_gem_port(int32_t intf_id, int32_t onu_id, int32_t gemport_id) {
799 bcmos_errno err;
800 bcmolt_itupon_gem_cfg cfg; /* declare main API struct */
801 bcmolt_itupon_gem_key key = {}; /* declare key */
802 bcmolt_gem_port_configuration configuration = {};
803
804 key.pon_ni = intf_id;
805 key.gem_port_id = gemport_id;
806
807 BCMOLT_CFG_INIT(&cfg, itupon_gem, key);
808
809 bcmolt_gem_port_direction configuration_direction;
810 configuration_direction = BCMOLT_GEM_PORT_DIRECTION_BIDIRECTIONAL;
811 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, direction, configuration_direction);
812
813 bcmolt_gem_port_type configuration_type;
814 configuration_type = BCMOLT_GEM_PORT_TYPE_UNICAST;
815 BCMOLT_FIELD_SET(&configuration, gem_port_configuration, type, configuration_type);
816
817 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, configuration, configuration);
818
819 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, onu_id, onu_id);
820
821 bcmolt_control_state encryption_mode;
822 encryption_mode = BCMOLT_CONTROL_STATE_DISABLE;
823 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, encryption_mode, encryption_mode);
824
825 bcmolt_us_gem_port_destination upstream_destination_queue;
826 upstream_destination_queue = BCMOLT_US_GEM_PORT_DESTINATION_DATA;
827 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, upstream_destination_queue, upstream_destination_queue);
828
829 bcmolt_control_state control;
830 control = BCMOLT_CONTROL_STATE_ENABLE;
831 BCMOLT_FIELD_SET(&cfg.data, itupon_gem_cfg_data, control, control);
832
833 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
834 if(err != BCM_ERR_OK) {
835 OPENOLT_LOG(ERROR, openolt_log_id, "failed to install gem_port = %d\n", gemport_id);
836 return bcm_to_grpc_err(err, "Access_Control set ITU PON Gem port failed");
837 }
838
839 OPENOLT_LOG(INFO, openolt_log_id, "gem port installed successfully = %d\n", gemport_id);
840
841 return Status::OK;
842}
843
844Status remove_gem_port(int32_t intf_id, int32_t gemport_id) {
845 bcmolt_itupon_gem_cfg gem_cfg;
846 bcmolt_itupon_gem_key key = {
847 .pon_ni = (bcmolt_interface)intf_id,
848 .gem_port_id = (bcmolt_gem_port_id)gemport_id
849 };
850 bcmos_errno err;
851
852 BCMOLT_CFG_INIT(&gem_cfg, itupon_gem, key);
853 err = bcmolt_cfg_clear(dev_id, &gem_cfg.hdr);
854 if (err != BCM_ERR_OK)
855 {
856 OPENOLT_LOG(ERROR, openolt_log_id, "failed to remove gem_port = %d err=%s\n", gemport_id, gem_cfg.hdr.hdr.err_text);
857 return bcm_to_grpc_err(err, "Access_Control clear ITU PON Gem port failed");
858 }
859
860 OPENOLT_LOG(INFO, openolt_log_id, "gem port removed successfully = %d\n", gemport_id);
861
862 return Status::OK;
863}
864
865Status update_acl_interface(int32_t intf_id, bcmolt_interface_type intf_type, uint32_t access_control_id,
866 bcmolt_members_update_command acl_cmd) {
867 bcmos_errno err;
868 bcmolt_access_control_interfaces_update oper; /* declare main API struct */
869 bcmolt_access_control_key acl_key = {}; /* declare key */
870 bcmolt_intf_ref interface_ref_list_elem = {};
871 bcmolt_interface_type interface_ref_list_elem_intf_type;
872 bcmolt_interface_id interface_ref_list_elem_intf_id;
873 bcmolt_intf_ref_list_u8 interface_ref_list = {};
874
875 if (acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_ADD && acl_cmd != BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE) {
876 OPENOLT_LOG(ERROR, openolt_log_id, "acl cmd = %d not supported currently\n", acl_cmd);
877 return bcm_to_grpc_err(BCM_ERR_PARM, "unsupported acl cmd");
878 }
879 interface_ref_list.arr = (bcmolt_intf_ref*)bcmos_calloc(sizeof(bcmolt_intf_ref)*1);
880
881 if (interface_ref_list.arr == NULL)
882 return bcm_to_grpc_err(BCM_ERR_PARM, "allocate interface_ref_list failed");
883 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface received for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
884 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
885 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
886
887 acl_key.id = access_control_id;
888
889 /* Initialize the API struct. */
890 BCMOLT_OPER_INIT(&oper, access_control, interfaces_update, acl_key);
891
892 bcmolt_members_update_command command;
893 command = acl_cmd;
894 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, command, command);
895
896 interface_ref_list_elem_intf_type = intf_type;
897 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_type, interface_ref_list_elem_intf_type);
898
899 interface_ref_list_elem_intf_id = intf_id;
900 BCMOLT_FIELD_SET(&interface_ref_list_elem, intf_ref, intf_id, interface_ref_list_elem_intf_id);
901
902 interface_ref_list.len = 1;
903 BCMOLT_ARRAY_ELEM_SET(&interface_ref_list, 0, interface_ref_list_elem);
904
905 BCMOLT_FIELD_SET(&oper.data, access_control_interfaces_update_data, interface_ref_list, interface_ref_list);
906
907 err = bcmolt_oper_submit(dev_id, &oper.hdr);
908 if (err != BCM_ERR_OK) {
909 OPENOLT_LOG(ERROR, openolt_log_id, "update acl interface fail for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
910 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
911 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
912 return bcm_to_grpc_err(err, "Access_Control submit interface failed");
913 }
914
915 bcmos_free(interface_ref_list.arr);
916 OPENOLT_LOG(INFO, openolt_log_id, "update acl interface success for intf_id = %d, intf_type = %s, acl_id = %d, acl_cmd = %s\n",
917 intf_id, intf_type == BCMOLT_INTERFACE_TYPE_PON? "pon": "nni", access_control_id,
918 acl_cmd == BCMOLT_MEMBERS_UPDATE_COMMAND_ADD? "add": "remove");
919
920 return Status::OK;
921}
922
923Status install_acl(const acl_classifier_key acl_key) {
Girish Gowdraddf9a162020-01-27 12:56:27 +0530924 bcmos_errno err;
925 bcmolt_access_control_cfg cfg;
926 bcmolt_access_control_key key = { };
927 bcmolt_classifier c_val = { };
928 // hardcode the action for now.
929 bcmolt_access_control_fwd_action_type action_type = BCMOLT_ACCESS_CONTROL_FWD_ACTION_TYPE_TRAP_TO_HOST;
930
931 int acl_id = get_acl_id();
932 if (acl_id < 0) {
933 OPENOLT_LOG(ERROR, openolt_log_id, "exhausted acl_id for eth_type = %d, ip_proto = %d, src_port = %d, dst_port = %d\n",
934 acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530935 return bcm_to_grpc_err(BCM_ERR_INTERNAL, "exhausted acl id");
936 }
937
938 key.id = acl_id;
939 /* config access control instance */
940 BCMOLT_CFG_INIT(&cfg, access_control, key);
Girish Gowdraddf9a162020-01-27 12:56:27 +0530941 if (acl_key.ether_type > 0) {
942 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ether_type 0x%04x\n", acl_key.ether_type);
943 BCMOLT_FIELD_SET(&c_val, classifier, ether_type, acl_key.ether_type);
944 }
945
946 if (acl_key.ip_proto > 0) {
947 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify ip_proto %d\n", acl_key.ip_proto);
948 BCMOLT_FIELD_SET(&c_val, classifier, ip_proto, acl_key.ip_proto);
949 }
950
951 if (acl_key.dst_port > 0) {
952 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify dst_port %d\n", acl_key.dst_port);
953 BCMOLT_FIELD_SET(&c_val, classifier, dst_port, acl_key.dst_port);
954 }
955
956 if (acl_key.src_port > 0) {
957 OPENOLT_LOG(DEBUG, openolt_log_id, "Access_Control classify src_port %d\n", acl_key.src_port);
958 BCMOLT_FIELD_SET(&c_val, classifier, src_port, acl_key.src_port);
959 }
960
961 BCMOLT_MSG_FIELD_SET(&cfg, classifier, c_val);
962 BCMOLT_MSG_FIELD_SET(&cfg, priority, 10000);
963 BCMOLT_MSG_FIELD_SET(&cfg, statistics_control, BCMOLT_CONTROL_STATE_ENABLE);
964
965 BCMOLT_MSG_FIELD_SET(&cfg, forwarding_action.action, action_type);
966
967 err = bcmolt_cfg_set(dev_id, &cfg.hdr);
968 if (err != BCM_ERR_OK) {
969 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control set configuration failed, Error %d\n", err);
970 // Free the acl_id
971 free_acl_id(acl_id);
972 return bcm_to_grpc_err(err, "Access_Control set configuration failed");
973 }
974
975 ACL_LOG(INFO, "ACL add ok", err);
976
977 // Update the map that we have installed an acl for the given classfier.
978 acl_classifier_to_acl_id_map[acl_key] = acl_id;
979 return Status::OK;
980}
981
982Status remove_acl(int acl_id) {
983 bcmos_errno err;
984 bcmolt_access_control_cfg cfg; /* declare main API struct */
985 bcmolt_access_control_key key = {}; /* declare key */
986
987 key.id = acl_id;
988
989 /* Initialize the API struct. */
990 BCMOLT_CFG_INIT(&cfg, access_control, key);
991 BCMOLT_FIELD_SET_PRESENT(&cfg.data, access_control_cfg_data, state);
992 err = bcmolt_cfg_get(dev_id, &cfg.hdr);
993 if (err != BCM_ERR_OK) {
994 OPENOLT_LOG(ERROR, openolt_log_id, "Access_Control get state failed\n");
995 return bcm_to_grpc_err(err, "Access_Control get state failed");
996 }
997
998 if (cfg.data.state == BCMOLT_CONFIG_STATE_CONFIGURED) {
999 key.id = acl_id;
1000 /* Initialize the API struct. */
1001 BCMOLT_CFG_INIT(&cfg, access_control, key);
1002
1003 err = bcmolt_cfg_clear(dev_id, &cfg.hdr);
1004 if (err != BCM_ERR_OK) {
1005 // Should we free acl_id here ? We should ideally never land here..
1006 OPENOLT_LOG(ERROR, openolt_log_id, "Error %d while removing Access_Control rule ID %d\n",
1007 err, acl_id);
1008 return Status(grpc::StatusCode::INTERNAL, "Failed to remove Access_Control");
1009 }
1010 }
1011
1012 // Free up acl_id
1013 free_acl_id(acl_id);
1014
1015 OPENOLT_LOG(INFO, openolt_log_id, "acl removed successfully %d\n", acl_id);
1016
1017 return Status::OK;
1018}
1019
1020// Formulates ACL Classifier Key based on the following fields
1021// a. ether_type b. ip_proto c. src_port d. dst_port
1022// If any of the field is not available it is populated as -1.
1023void formulate_acl_classifier_key(acl_classifier_key *key, const ::openolt::Classifier& classifier) {
1024
1025 // TODO: Is 0 a valid value for any of the following classifiers?
1026 // because in the that case, the 'if' check would fail and -1 would be filled as value.
1027 //
1028 if (classifier.eth_type()) {
1029 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ether_type 0x%04x\n", classifier.eth_type());
1030 key->ether_type = classifier.eth_type();
1031 } else key->ether_type = -1;
1032
1033 if (classifier.ip_proto()) {
1034 OPENOLT_LOG(DEBUG, openolt_log_id, "classify ip_proto %d\n", classifier.ip_proto());
1035 key->ip_proto = classifier.ip_proto();
1036 } else key->ip_proto = -1;
1037
1038
1039 if (classifier.src_port()) {
1040 OPENOLT_LOG(DEBUG, openolt_log_id, "classify src_port %d\n", classifier.src_port());
1041 key->src_port = classifier.src_port();
1042 } else key->src_port = -1;
1043
1044
1045 if (classifier.dst_port()) {
1046 OPENOLT_LOG(DEBUG, openolt_log_id, "classify dst_port %d\n", classifier.dst_port());
1047 key->dst_port = classifier.dst_port();
1048 } else key->dst_port = -1;
1049}
1050
1051Status handle_acl_rule_install(int32_t onu_id, uint32_t flow_id,
1052 const std::string flow_type, int32_t access_intf_id,
1053 int32_t network_intf_id, int32_t gemport_id,
1054 const ::openolt::Classifier& classifier) {
1055 int acl_id;
1056 int32_t intf_id = flow_type.compare(upstream) == 0? access_intf_id: network_intf_id;
1057 const std::string intf_type = flow_type.compare(upstream) == 0? "pon": "nni";
1058 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1059
1060 Status resp;
1061
1062 // few map keys we are going to use later.
1063 flow_id_flow_direction fl_id_fl_dir(flow_id, flow_type);
1064 gem_id_intf_id gem_intf(gemport_id, access_intf_id);
1065 acl_classifier_key acl_key;
1066 formulate_acl_classifier_key(&acl_key, classifier);
1067 const acl_classifier_key acl_key_const = {.ether_type=acl_key.ether_type, .ip_proto=acl_key.ip_proto,
1068 .src_port=acl_key.src_port, .dst_port=acl_key.dst_port};
1069
1070 bcmos_fastlock_lock(&data_lock);
1071
1072 // Check if the acl is already installed
1073 if (acl_classifier_to_acl_id_map.count(acl_key_const) > 0) {
1074 // retreive the acl_id
1075 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
1076 acl_id_gem_id_intf_id ac_id_gm_id_if_id(acl_id, gemport_id, intf_id);
1077 if (flow_to_acl_map.count(fl_id_fl_dir)) {
1078 // coult happen if same trap flow is received again
1079 OPENOLT_LOG(INFO, openolt_log_id, "flow and related acl already handled, nothing more to do\n");
1080 bcmos_fastlock_unlock(&data_lock, 0);
1081 return Status::OK;
1082 }
1083
1084 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",
1085 flow_id, acl_key.ether_type, acl_key.ip_proto, acl_key.src_port, acl_key.dst_port, acl_id);
1086
1087 // The acl_ref_cnt is needed to know how many flows refer an ACL.
1088 // When the flow is removed, we decrement the reference count.
1089 // When the reference count becomes 0, we remove the ACL.
1090 if (acl_ref_cnt.count(acl_id) > 0) {
1091 acl_ref_cnt[acl_id] ++;
1092 } else {
1093 // We should ideally not land here. The acl_ref_cnt should have been
1094 // initialized the first time acl was installed.
1095 acl_ref_cnt[acl_id] = 1;
1096 }
1097
1098 } else {
1099 resp = install_acl(acl_key_const);
1100 if (!resp.ok()) {
1101 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",
1102 flow_id, acl_key_const.ether_type, acl_key_const.ip_proto, acl_key_const.src_port, acl_key_const.dst_port);
1103 bcmos_fastlock_unlock(&data_lock, 0);
1104 return resp;
1105 }
1106
1107 acl_id = acl_classifier_to_acl_id_map[acl_key_const];
1108
1109 // Initialize the acl reference count
1110 acl_ref_cnt[acl_id] = 1;
1111
1112 OPENOLT_LOG(INFO, openolt_log_id, "acl add success for flow_id=%u with acl_id=%d\n", flow_id, acl_id);
1113 }
1114
1115 // Register the interface for the given acl
1116 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1117 // This is needed to keep a track of which interface (pon/nni) has registered for an ACL.
1118 // If it is registered, how many flows refer to it.
1119 if (intf_acl_registration_ref_cnt.count(ac_id_inf_id_inf_type) > 0) {
1120 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]++;
1121 } else {
1122 // The given interface is not registered for the ACL. We need to do it now.
1123 resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_ADD);
1124 if (!resp.ok()){
1125 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);
1126 // TODO: Ideally we should return error from hear and clean up other other stateful
1127 // counters we creaed earlier. Will leave it out for now.
1128 }
1129 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] = 1;
1130 }
1131
1132
1133 // Install the gem port if needed.
1134 if (gemport_id > 0 && access_intf_id >= 0) {
1135 if (gem_ref_cnt.count(gem_intf) > 0) {
1136 // The gem port is already installed
1137 // Increment the ref counter indicating number of flows referencing this gem port
1138 gem_ref_cnt[gem_intf]++;
1139 OPENOLT_LOG(DEBUG, openolt_log_id, "increment gem_ref_cnt in acl handler, ref_cnt=%d\n", gem_ref_cnt[gem_intf]);
1140
1141 } else {
1142 // We should ideally never land here. The gem port should have been created the
1143 // first time ACL was installed.
1144 // Install the gem port
1145 Status resp = install_gem_port(access_intf_id, onu_id, gemport_id);
1146 if (!resp.ok()) {
1147 // TODO: We might need to reverse all previous data, but leave it out for now.
1148 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);
1149 bcmos_fastlock_unlock(&data_lock, 0);
1150 return resp;
1151 }
1152 // Initialize the refence count for the gemport.
1153 gem_ref_cnt[gem_intf] = 1;
1154 OPENOLT_LOG(DEBUG, openolt_log_id, "intialized gem ref count in acl handler\n");
1155 }
1156 } else {
1157 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);
1158 }
1159
1160 // Update the flow_to_acl_map
1161 // This info is needed during flow remove. We need to which ACL ID and GEM PORT ID
1162 // the flow was referring to.
1163 // After retrieving the ACL ID and GEM PORT ID, we decrement the corresponding
1164 // reference counters for those ACL ID and GEMPORT ID.
1165 acl_id_gem_id_intf_id ac_id_gm_id_if_id(acl_id, gemport_id, intf_id);
1166 flow_to_acl_map[fl_id_fl_dir] = ac_id_gm_id_if_id;
1167
1168 bcmos_fastlock_unlock(&data_lock, 0);
1169
1170 return Status::OK;
1171}
1172
1173void clear_gem_port(int gemport_id, int access_intf_id) {
1174 gem_id_intf_id gem_intf(gemport_id, access_intf_id);
1175 if (gemport_id > 0 && access_intf_id >= 0 && gem_ref_cnt.count(gem_intf) > 0) {
1176 OPENOLT_LOG(DEBUG, openolt_log_id, "decrementing gem_ref_cnt gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1177 gem_ref_cnt[gem_intf]--;
1178 if (gem_ref_cnt[gem_intf] == 0) {
1179 // For datapath flow this may not be necessary (to be verified)
1180 remove_gem_port(access_intf_id, gemport_id);
1181 gem_ref_cnt.erase(gem_intf);
1182 OPENOLT_LOG(DEBUG, openolt_log_id, "removing gem_ref_cnt entry gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1183 } else {
1184 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);
1185 }
1186 } else {
1187 OPENOLT_LOG(DEBUG, openolt_log_id, "not decrementing gem_ref_cnt gemport_id=%d access_intf_id=%d\n", gemport_id, access_intf_id);
1188 }
1189}
1190
1191Status handle_acl_rule_cleanup(int16_t acl_id, int32_t gemport_id, int32_t intf_id, const std::string flow_type) {
1192 const std::string intf_type= flow_type.compare(upstream) == 0 ? "pon": "nni";
1193 acl_id_intf_id_intf_type ac_id_inf_id_inf_type(acl_id, intf_id, intf_type);
1194 intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type]--;
1195 if (intf_acl_registration_ref_cnt[ac_id_inf_id_inf_type] == 0) {
1196 bcmolt_interface_type olt_if_type = intf_type == "pon"? BCMOLT_INTERFACE_TYPE_PON: BCMOLT_INTERFACE_TYPE_NNI;
1197 Status resp = update_acl_interface(intf_id, olt_if_type, acl_id, BCMOLT_MEMBERS_UPDATE_COMMAND_REMOVE);
1198 if (!resp.ok()){
1199 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);
1200 }
1201 intf_acl_registration_ref_cnt.erase(ac_id_inf_id_inf_type);
1202 }
1203
1204 acl_ref_cnt[acl_id]--;
1205 if (acl_ref_cnt[acl_id] == 0) {
1206 remove_acl(acl_id);
1207 acl_ref_cnt.erase(acl_id);
1208 // Iterate acl_classifier_to_acl_id_map and delete classifier the key corresponding to acl_id
1209 std::map<acl_classifier_key, uint16_t>::iterator it;
1210 for (it=acl_classifier_to_acl_id_map.begin(); it!=acl_classifier_to_acl_id_map.end(); ++it) {
1211 if (it->second == acl_id) {
1212 OPENOLT_LOG(INFO, openolt_log_id, "cleared classifier key corresponding to acl_id = %d\n", acl_id);
1213 acl_classifier_to_acl_id_map.erase(it->first);
1214 break;
1215 }
1216 }
1217 }
1218
1219 clear_gem_port(gemport_id, intf_id);
1220
1221 return Status::OK;
1222}
1223
1224Status check_bal_ready() {
1225 bcmos_errno err;
1226 int maxTrials = 30;
1227 bcmolt_olt_cfg olt_cfg = { };
1228 bcmolt_olt_key olt_key = { };
1229
1230 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
1231 BCMOLT_MSG_FIELD_GET(&olt_cfg, bal_state);
1232
1233 while (olt_cfg.data.bal_state != BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY) {
1234 if (--maxTrials == 0)
1235 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check bal ready failed");
1236 sleep(5);
1237 #ifdef TEST_MODE
1238 // It is impossible to mock the setting of olt_cfg.data.bal_state because
1239 // the actual bcmolt_cfg_get passes the address of olt_cfg.hdr and we cannot
1240 // set the olt_cfg.data.bal_state. So a new stub function is created and address
1241 // of olt_cfg is passed. This is one-of case where we need to add test specific
1242 // code in production code.
1243 if (bcmolt_cfg_get__bal_state_stub(dev_id, &olt_cfg)) {
1244 #else
1245 if (bcmolt_cfg_get(dev_id, &olt_cfg.hdr)) {
1246 #endif
1247 continue;
1248 }
1249 else
1250 OPENOLT_LOG(INFO, openolt_log_id, "waiting for BAL ready ...\n");
1251 }
1252
1253 OPENOLT_LOG(INFO, openolt_log_id, "BAL is ready\n");
1254 return Status::OK;
1255}
1256
1257Status check_connection() {
1258 int maxTrials = 60;
1259 while (!bcmolt_api_conn_mgr_is_connected(dev_id)) {
1260 sleep(1);
1261 if (--maxTrials == 0)
1262 return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check connection failed");
1263 else
1264 OPENOLT_LOG(INFO, openolt_log_id, "waiting for daemon connection ...\n");
1265 }
1266 OPENOLT_LOG(INFO, openolt_log_id, "daemon is connected\n");
1267 return Status::OK;
1268}
1269