blob: 36ec466c8e007e77a9a246763d9226d1873a7d7f [file] [log] [blame]
VoLTHA753536e2017-11-02 20:15:09 +05301/*
2** Copyright 2017-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 "bal_indications_queue.h"
root2ca2cc02017-11-03 19:51:57 +053018#include "asfvolt16_driver.h"
19
20long unsigned int indication_drop_count = 0;
VoLTHA753536e2017-11-02 20:15:09 +053021
22/*
23 * Implements a circular queue whose size can grow upto MAX_QUEUE_SIZE defined.
24 * Once MAX_QUEUE_SIZE is reached, newest nodes start replacing oldest nodes
25 * in the circular queue.
26 * We define two APIs below, one to get the node from the queue, another to
27 * add node to the queue.
28 */
29
30list_node* get_bal_indication_node(void) {
31 /*'bal_ind_queue_head' should be thread safe there could be simultaneous
32 write from BAL and adapter trying to read from the 'bal_ind_queue_head'.
33 So, protect it under a mutex(bal_ind_queue_lock), before using this MACRO*/
34 list_node *node = NULL;
35 if (num_of_nodes == 1) {
36 node = bal_ind_queue_head;
37 bal_ind_queue_head = NULL;
38 bal_ind_queue_tail = NULL;
39 num_of_nodes = 0;
40 }
41 else if(num_of_nodes > 1) {
42 /*Process if there is at least one node in the queue*/
43 node = bal_ind_queue_head;
44 bal_ind_queue_head = bal_ind_queue_head->next;
45 bal_ind_queue_tail->next = bal_ind_queue_head;
46 --num_of_nodes;
47 }
48
49 return node;
50}
51
52void add_bal_indication_node(list_node *node) {
53 /*'bal_ind_queue_head' should be thread safe there could be simultaneous
54 write from BAL and adapter trying to read from the 'bal_ind_queue_head'.
55 So, protect it under a mutex(bal_ind_queue_lock), before using this MACRO*/
root2ca2cc02017-11-03 19:51:57 +053056 if (num_of_nodes == 0 &&
57 bal_ind_queue_head == NULL &&
VoLTHA753536e2017-11-02 20:15:09 +053058 bal_ind_queue_tail == NULL ) {
59 /*Case where the queue is empty*/
60 bal_ind_queue_head = node;
61 bal_ind_queue_tail = node;
62 bal_ind_queue_head->next = node;
63 bal_ind_queue_tail->next = node;
64 ++num_of_nodes;
65 }
root2ca2cc02017-11-03 19:51:57 +053066 else if (num_of_nodes == 1 &&
67 bal_ind_queue_head != NULL &&
VoLTHA753536e2017-11-02 20:15:09 +053068 bal_ind_queue_tail != NULL ) {
69 bal_ind_queue_head->next = node;
70 bal_ind_queue_tail = node;
71 bal_ind_queue_tail->next = bal_ind_queue_head;
72 ++num_of_nodes;
73 }
root2ca2cc02017-11-03 19:51:57 +053074 else if (num_of_nodes > 1 &&
75 num_of_nodes < MAX_QUEUE_SIZE &&
76 bal_ind_queue_head != NULL &&
VoLTHA753536e2017-11-02 20:15:09 +053077 bal_ind_queue_tail != NULL ) {
78 bal_ind_queue_tail->next = node;
79 bal_ind_queue_tail = node;
80 bal_ind_queue_tail->next = bal_ind_queue_head;
81 ++num_of_nodes;
82 }
root2ca2cc02017-11-03 19:51:57 +053083 else if (num_of_nodes == MAX_QUEUE_SIZE &&
84 bal_ind_queue_head != NULL &&
VoLTHA753536e2017-11-02 20:15:09 +053085 bal_ind_queue_tail != NULL ) {
86 list_node *head_ref = bal_ind_queue_head;
root2ca2cc02017-11-03 19:51:57 +053087 ASFVOLT_LOG(ASFVOLT_DEBUG, "Queue full, deleting a node. Drop cnt = %lu\n",\
88 ++indication_drop_count);
VoLTHA753536e2017-11-02 20:15:09 +053089 bal_ind_queue_tail->next = node;
90 bal_ind_queue_tail = node;
91 bal_ind_queue_head = bal_ind_queue_head->next;
92 bal_ind_queue_tail->next = bal_ind_queue_head;
93 bal_get_ind__free_mem(head_ref->bal_indication);
94 free(head_ref);
95 }
96}