Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 1 | #include "stats_collection.h" |
| 2 | |
| 3 | #include <unistd.h> |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 4 | |
| 5 | #include <openolt.grpc.pb.h> |
| 6 | #include "indications.h" |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 7 | #include "core.h" |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 8 | #include "translation.h" |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 9 | |
| 10 | extern "C" |
| 11 | { |
| 12 | #include <bcmos_system.h> |
| 13 | #include <bal_api.h> |
| 14 | #include <bal_api_end.h> |
| 15 | #include <flow_fsm.h> |
| 16 | } |
| 17 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 18 | //FIXME |
| 19 | #define FLOWS_COUNT 100 |
| 20 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 21 | bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT]; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 22 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 23 | void init_stats() { |
| 24 | memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key)); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | openolt::PortStatistics* get_default_port_statistics() { |
| 28 | openolt::PortStatistics* port_stats = new openolt::PortStatistics; |
| 29 | port_stats->set_intf_id(-1); |
| 30 | port_stats->set_rx_bytes(-1); |
| 31 | port_stats->set_rx_packets(-1); |
| 32 | port_stats->set_rx_ucast_packets(-1); |
| 33 | port_stats->set_rx_mcast_packets(-1); |
| 34 | port_stats->set_rx_bcast_packets(-1); |
| 35 | port_stats->set_rx_error_packets(-1); |
| 36 | port_stats->set_tx_bytes(-1); |
| 37 | port_stats->set_tx_packets(-1); |
| 38 | port_stats->set_tx_ucast_packets(-1); |
| 39 | port_stats->set_tx_mcast_packets(-1); |
| 40 | port_stats->set_tx_bcast_packets(-1); |
| 41 | port_stats->set_tx_error_packets(-1); |
| 42 | port_stats->set_rx_crc_errors(-1); |
| 43 | port_stats->set_bip_errors(-1); |
| 44 | |
| 45 | return port_stats; |
| 46 | } |
| 47 | |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 48 | #if 0 |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 49 | openolt::FlowStatistics* get_default_flow_statistics() { |
| 50 | openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics; |
| 51 | flow_stats->set_flow_id(-1); |
| 52 | flow_stats->set_rx_bytes(-1); |
| 53 | flow_stats->set_rx_packets(-1); |
| 54 | flow_stats->set_tx_bytes(-1); |
| 55 | flow_stats->set_tx_packets(-1); |
| 56 | |
| 57 | return flow_stats; |
| 58 | } |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 59 | #endif |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 60 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 61 | openolt::PortStatistics* collectPortStatistics(bcmbal_interface_key key) { |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 62 | |
| 63 | bcmos_errno err; |
| 64 | bcmbal_interface_stat stat; /**< declare main API struct */ |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 65 | bcmos_bool clear_on_read = false; |
| 66 | |
| 67 | openolt::PortStatistics* port_stats = get_default_port_statistics(); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 68 | |
| 69 | /* init the API struct */ |
| 70 | BCMBAL_STAT_INIT(&stat, interface, key); |
| 71 | BCMBAL_STAT_PROP_GET(&stat, interface, all_properties); |
| 72 | |
| 73 | /* call API */ |
| 74 | err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read); |
| 75 | if (err == BCM_ERR_OK) |
| 76 | { |
Shad Ansari | 563ea82 | 2018-06-28 14:56:27 +0000 | [diff] [blame] | 77 | //std::cout << "Interface statistics retrieved" |
| 78 | // << " intf_id:" << intf_id << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 79 | |
| 80 | port_stats->set_rx_bytes(stat.data.rx_bytes); |
| 81 | port_stats->set_rx_packets(stat.data.rx_packets); |
| 82 | port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets); |
| 83 | port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets); |
| 84 | port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets); |
| 85 | port_stats->set_rx_error_packets(stat.data.rx_error_packets); |
| 86 | port_stats->set_tx_bytes(stat.data.tx_bytes); |
| 87 | port_stats->set_tx_packets(stat.data.tx_packets); |
| 88 | port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets); |
| 89 | port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets); |
| 90 | port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets); |
| 91 | port_stats->set_tx_error_packets(stat.data.tx_error_packets); |
| 92 | port_stats->set_rx_crc_errors(stat.data.rx_crc_errors); |
| 93 | port_stats->set_bip_errors(stat.data.bip_errors); |
| 94 | |
| 95 | } else { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 96 | BCM_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d\n", |
| 97 | (int)key.intf_id, (int)key.intf_type); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 100 | port_stats->set_intf_id(interface_key_to_port_no(key)); |
| 101 | time_t now; |
| 102 | time(&now); |
| 103 | port_stats->set_timestamp((int)now); |
| 104 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 105 | return port_stats; |
| 106 | |
| 107 | } |
| 108 | |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 109 | #if 0 |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 110 | openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) { |
| 111 | |
| 112 | bcmos_errno err; |
| 113 | bcmbal_flow_stat stat; /**< declare main API struct */ |
| 114 | bcmbal_flow_key key = { }; /**< declare key */ |
| 115 | bcmos_bool clear_on_read = false; |
| 116 | |
| 117 | openolt::FlowStatistics* flow_stats = get_default_flow_statistics(); |
| 118 | //Key |
| 119 | key.flow_id = flow_id; |
| 120 | key.flow_type = flow_type; |
| 121 | |
| 122 | /* init the API struct */ |
| 123 | BCMBAL_STAT_INIT(&stat, flow, key); |
| 124 | BCMBAL_STAT_PROP_GET(&stat, flow, all_properties); |
| 125 | |
| 126 | err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read); |
| 127 | |
| 128 | if (err == BCM_ERR_OK) |
| 129 | { |
| 130 | std::cout << "Flow statistics retrieved" |
| 131 | << " flow_id:" << flow_id |
| 132 | << " flow_type:" << flow_type << std::endl; |
| 133 | |
| 134 | flow_stats->set_rx_bytes(stat.data.rx_bytes); |
| 135 | flow_stats->set_rx_packets(stat.data.rx_packets); |
| 136 | flow_stats->set_tx_bytes(stat.data.tx_bytes); |
| 137 | flow_stats->set_tx_packets(stat.data.tx_packets); |
| 138 | |
| 139 | } else { |
| 140 | std::cout << "ERROR: Failed to retrieve flow statistics" |
| 141 | << " flow_id:" << flow_id |
| 142 | << " flow_type:" << flow_type << std::endl; |
| 143 | } |
| 144 | |
| 145 | return flow_stats; |
| 146 | } |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 147 | #endif |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 148 | |
| 149 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 150 | void stats_collection() { |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 151 | |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 152 | if (!state.is_connected()) { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 153 | 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] | 154 | return; |
| 155 | } |
| 156 | if (!state.is_activated()) { |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 157 | 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] | 158 | return; |
| 159 | } |
| 160 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 161 | |
Nicolas Palpacuer | 967438f | 2018-09-07 14:41:54 -0400 | [diff] [blame] | 162 | BCM_LOG(INFO, openolt_log_id, "Collecting statistics\n"); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 163 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 164 | //Ports statistics |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 165 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 166 | //Uplink ports |
| 167 | for (int i = 0; i < 4; i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 168 | bcmbal_interface_key nni_interface; |
| 169 | nni_interface.intf_type = BCMBAL_INTF_TYPE_NNI; |
| 170 | nni_interface.intf_id = i; |
| 171 | |
| 172 | openolt::PortStatistics* port_stats = collectPortStatistics(nni_interface); |
| 173 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 174 | openolt::Indication ind; |
| 175 | ind.set_allocated_port_stats(port_stats); |
| 176 | oltIndQ.push(ind); |
| 177 | } |
| 178 | //Pon ports |
| 179 | for (int i = 0; i < 16; i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 180 | bcmbal_interface_key pon_interface; |
| 181 | pon_interface.intf_type = BCMBAL_INTF_TYPE_PON; |
| 182 | pon_interface.intf_id = i; |
| 183 | |
| 184 | openolt::PortStatistics* port_stats = collectPortStatistics(pon_interface); |
| 185 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 186 | openolt::Indication ind; |
| 187 | ind.set_allocated_port_stats(port_stats); |
| 188 | oltIndQ.push(ind); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 191 | //Flows statistics |
| 192 | // flow_inst *current_entry = NULL; |
| 193 | // |
| 194 | // TAILQ_FOREACH(current_entry, |
| 195 | // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list, |
| 196 | // flow_inst_next) { |
| 197 | // int flows_measurements = 0; |
| 198 | // |
| 199 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 200 | // |
| 201 | // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id; |
| 202 | // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type; |
| 203 | // |
| 204 | // if (flows_keys[i].flow_id != 0) { |
| 205 | // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type); |
| 206 | // if (flow_stats->rx_packets() == -1) { |
| 207 | // //It Failed |
| 208 | // flows_keys[i].flow_id = 0; |
| 209 | // } else { |
| 210 | // flow_stats->set_flow_id(flows_keys[i].flow_id); |
| 211 | // time(&now); |
| 212 | // flow_stats->set_timestamp((int)now); |
| 213 | // openolt::Indication ind; |
| 214 | // ind.set_allocated_flow_stats(flow_stats); |
| 215 | // oltIndQ.push(ind); |
| 216 | // flows_measurements ++; |
| 217 | // } |
| 218 | // } |
| 219 | // |
| 220 | // } |
| 221 | // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 222 | |
| 223 | } |
| 224 | |
| 225 | /* Storing flow keys, temporary */ |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 226 | // void register_new_flow(bcmbal_flow_key key) { |
| 227 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 228 | // if (flows_keys[i].flow_id == 0) { |
| 229 | // flows_keys[i] = key; |
| 230 | // break; |
| 231 | // } |
| 232 | // } |
| 233 | // } |