VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 1 | /* |
| 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" |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 18 | #include "asfvolt16_driver.h" |
| 19 | |
| 20 | long unsigned int indication_drop_count = 0; |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 21 | |
| 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 | |
| 30 | list_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 | |
| 52 | void 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*/ |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 56 | if (num_of_nodes == 0 && |
| 57 | bal_ind_queue_head == NULL && |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 58 | 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 | } |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 66 | else if (num_of_nodes == 1 && |
| 67 | bal_ind_queue_head != NULL && |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 68 | 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 | } |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 74 | else if (num_of_nodes > 1 && |
| 75 | num_of_nodes < MAX_QUEUE_SIZE && |
| 76 | bal_ind_queue_head != NULL && |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 77 | 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 | } |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 83 | else if (num_of_nodes == MAX_QUEUE_SIZE && |
| 84 | bal_ind_queue_head != NULL && |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 85 | bal_ind_queue_tail != NULL ) { |
| 86 | list_node *head_ref = bal_ind_queue_head; |
root | 2ca2cc0 | 2017-11-03 19:51:57 +0530 | [diff] [blame^] | 87 | ASFVOLT_LOG(ASFVOLT_DEBUG, "Queue full, deleting a node. Drop cnt = %lu\n",\ |
| 88 | ++indication_drop_count); |
VoLTHA | 753536e | 2017-11-02 20:15:09 +0530 | [diff] [blame] | 89 | 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 | } |