Zack Williams | 477ba09 | 2018-10-17 10:50:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2018 Open Networking Foundation |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 18 | #include "stats_collection.h" |
| 19 | |
| 20 | #include <unistd.h> |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 21 | |
| 22 | #include <openolt.grpc.pb.h> |
| 23 | #include "indications.h" |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 24 | #include "core.h" |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 25 | #include "translation.h" |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 26 | |
| 27 | extern "C" |
| 28 | { |
| 29 | #include <bcmos_system.h> |
| 30 | #include <bal_api.h> |
| 31 | #include <bal_api_end.h> |
| 32 | #include <flow_fsm.h> |
| 33 | } |
| 34 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 35 | //FIXME |
| 36 | #define FLOWS_COUNT 100 |
| 37 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 38 | bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT]; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 39 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 40 | void init_stats() { |
| 41 | memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key)); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | openolt::PortStatistics* get_default_port_statistics() { |
| 45 | openolt::PortStatistics* port_stats = new openolt::PortStatistics; |
| 46 | port_stats->set_intf_id(-1); |
| 47 | port_stats->set_rx_bytes(-1); |
| 48 | port_stats->set_rx_packets(-1); |
| 49 | port_stats->set_rx_ucast_packets(-1); |
| 50 | port_stats->set_rx_mcast_packets(-1); |
| 51 | port_stats->set_rx_bcast_packets(-1); |
| 52 | port_stats->set_rx_error_packets(-1); |
| 53 | port_stats->set_tx_bytes(-1); |
| 54 | port_stats->set_tx_packets(-1); |
| 55 | port_stats->set_tx_ucast_packets(-1); |
| 56 | port_stats->set_tx_mcast_packets(-1); |
| 57 | port_stats->set_tx_bcast_packets(-1); |
| 58 | port_stats->set_tx_error_packets(-1); |
| 59 | port_stats->set_rx_crc_errors(-1); |
| 60 | port_stats->set_bip_errors(-1); |
| 61 | |
| 62 | return port_stats; |
| 63 | } |
| 64 | |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 65 | #if 0 |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 66 | openolt::FlowStatistics* get_default_flow_statistics() { |
| 67 | openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics; |
| 68 | flow_stats->set_flow_id(-1); |
| 69 | flow_stats->set_rx_bytes(-1); |
| 70 | flow_stats->set_rx_packets(-1); |
| 71 | flow_stats->set_tx_bytes(-1); |
| 72 | flow_stats->set_tx_packets(-1); |
| 73 | |
| 74 | return flow_stats; |
| 75 | } |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 76 | #endif |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 77 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 78 | openolt::PortStatistics* collectPortStatistics(bcmbal_interface_key key) { |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 79 | |
| 80 | bcmos_errno err; |
| 81 | bcmbal_interface_stat stat; /**< declare main API struct */ |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 82 | bcmos_bool clear_on_read = false; |
| 83 | |
| 84 | openolt::PortStatistics* port_stats = get_default_port_statistics(); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 85 | |
| 86 | /* init the API struct */ |
| 87 | BCMBAL_STAT_INIT(&stat, interface, key); |
| 88 | BCMBAL_STAT_PROP_GET(&stat, interface, all_properties); |
| 89 | |
| 90 | /* call API */ |
| 91 | err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read); |
| 92 | if (err == BCM_ERR_OK) |
| 93 | { |
Shad Ansari | 563ea82 | 2018-06-28 14:56:27 +0000 | [diff] [blame] | 94 | //std::cout << "Interface statistics retrieved" |
| 95 | // << " intf_id:" << intf_id << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 96 | |
| 97 | port_stats->set_rx_bytes(stat.data.rx_bytes); |
| 98 | port_stats->set_rx_packets(stat.data.rx_packets); |
| 99 | port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets); |
| 100 | port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets); |
| 101 | port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets); |
| 102 | port_stats->set_rx_error_packets(stat.data.rx_error_packets); |
| 103 | port_stats->set_tx_bytes(stat.data.tx_bytes); |
| 104 | port_stats->set_tx_packets(stat.data.tx_packets); |
| 105 | port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets); |
| 106 | port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets); |
| 107 | port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets); |
| 108 | port_stats->set_tx_error_packets(stat.data.tx_error_packets); |
| 109 | port_stats->set_rx_crc_errors(stat.data.rx_crc_errors); |
| 110 | port_stats->set_bip_errors(stat.data.bip_errors); |
| 111 | |
| 112 | } else { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 113 | BCM_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d\n", |
| 114 | (int)key.intf_id, (int)key.intf_type); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 117 | port_stats->set_intf_id(interface_key_to_port_no(key)); |
| 118 | time_t now; |
| 119 | time(&now); |
| 120 | port_stats->set_timestamp((int)now); |
| 121 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 122 | return port_stats; |
| 123 | |
| 124 | } |
| 125 | |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 126 | #if 0 |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 127 | openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) { |
| 128 | |
| 129 | bcmos_errno err; |
| 130 | bcmbal_flow_stat stat; /**< declare main API struct */ |
| 131 | bcmbal_flow_key key = { }; /**< declare key */ |
| 132 | bcmos_bool clear_on_read = false; |
| 133 | |
| 134 | openolt::FlowStatistics* flow_stats = get_default_flow_statistics(); |
| 135 | //Key |
| 136 | key.flow_id = flow_id; |
| 137 | key.flow_type = flow_type; |
| 138 | |
| 139 | /* init the API struct */ |
| 140 | BCMBAL_STAT_INIT(&stat, flow, key); |
| 141 | BCMBAL_STAT_PROP_GET(&stat, flow, all_properties); |
| 142 | |
| 143 | err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read); |
| 144 | |
| 145 | if (err == BCM_ERR_OK) |
| 146 | { |
| 147 | std::cout << "Flow statistics retrieved" |
| 148 | << " flow_id:" << flow_id |
| 149 | << " flow_type:" << flow_type << std::endl; |
| 150 | |
| 151 | flow_stats->set_rx_bytes(stat.data.rx_bytes); |
| 152 | flow_stats->set_rx_packets(stat.data.rx_packets); |
| 153 | flow_stats->set_tx_bytes(stat.data.tx_bytes); |
| 154 | flow_stats->set_tx_packets(stat.data.tx_packets); |
| 155 | |
| 156 | } else { |
| 157 | std::cout << "ERROR: Failed to retrieve flow statistics" |
| 158 | << " flow_id:" << flow_id |
| 159 | << " flow_type:" << flow_type << std::endl; |
| 160 | } |
| 161 | |
| 162 | return flow_stats; |
| 163 | } |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 164 | #endif |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 165 | |
| 166 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 167 | void stats_collection() { |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 168 | |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 169 | if (!state.is_connected()) { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 170 | BCM_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n"); |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | if (!state.is_activated()) { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 174 | BCM_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n"); |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 178 | |
Craig Lutgen | d0bae9b | 2018-10-18 18:02:07 -0500 | [diff] [blame] | 179 | BCM_LOG(DEBUG, openolt_log_id, "Collecting statistics\n"); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 180 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 181 | //Ports statistics |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 182 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 183 | //Uplink ports |
Craig Lutgen | 88a22ad | 2018-10-04 12:30:46 -0500 | [diff] [blame] | 184 | for (int i = 0; i < NumNniIf_(); i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 185 | bcmbal_interface_key nni_interface; |
| 186 | nni_interface.intf_type = BCMBAL_INTF_TYPE_NNI; |
| 187 | nni_interface.intf_id = i; |
| 188 | |
| 189 | openolt::PortStatistics* port_stats = collectPortStatistics(nni_interface); |
| 190 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 191 | openolt::Indication ind; |
| 192 | ind.set_allocated_port_stats(port_stats); |
| 193 | oltIndQ.push(ind); |
| 194 | } |
| 195 | //Pon ports |
Craig Lutgen | 88a22ad | 2018-10-04 12:30:46 -0500 | [diff] [blame] | 196 | for (int i = 0; i < NumPonIf_(); i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 197 | bcmbal_interface_key pon_interface; |
| 198 | pon_interface.intf_type = BCMBAL_INTF_TYPE_PON; |
| 199 | pon_interface.intf_id = i; |
| 200 | |
| 201 | openolt::PortStatistics* port_stats = collectPortStatistics(pon_interface); |
| 202 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 203 | openolt::Indication ind; |
| 204 | ind.set_allocated_port_stats(port_stats); |
| 205 | oltIndQ.push(ind); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 208 | //Flows statistics |
| 209 | // flow_inst *current_entry = NULL; |
| 210 | // |
| 211 | // TAILQ_FOREACH(current_entry, |
| 212 | // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list, |
| 213 | // flow_inst_next) { |
| 214 | // int flows_measurements = 0; |
| 215 | // |
| 216 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 217 | // |
| 218 | // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id; |
| 219 | // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type; |
| 220 | // |
| 221 | // if (flows_keys[i].flow_id != 0) { |
| 222 | // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type); |
| 223 | // if (flow_stats->rx_packets() == -1) { |
| 224 | // //It Failed |
| 225 | // flows_keys[i].flow_id = 0; |
| 226 | // } else { |
| 227 | // flow_stats->set_flow_id(flows_keys[i].flow_id); |
| 228 | // time(&now); |
| 229 | // flow_stats->set_timestamp((int)now); |
| 230 | // openolt::Indication ind; |
| 231 | // ind.set_allocated_flow_stats(flow_stats); |
| 232 | // oltIndQ.push(ind); |
| 233 | // flows_measurements ++; |
| 234 | // } |
| 235 | // } |
| 236 | // |
| 237 | // } |
| 238 | // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 239 | |
| 240 | } |
| 241 | |
| 242 | /* Storing flow keys, temporary */ |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 243 | // void register_new_flow(bcmbal_flow_key key) { |
| 244 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 245 | // if (flows_keys[i].flow_id == 0) { |
| 246 | // flows_keys[i] = key; |
| 247 | // break; |
| 248 | // } |
| 249 | // } |
| 250 | // } |