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 { |
| 96 | std::cout << "ERROR: Failed to retrieve port statistics" |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 97 | << " intf_id:" << key.intf_id |
| 98 | << " intf_type:" << key.intf_type << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 101 | port_stats->set_intf_id(interface_key_to_port_no(key)); |
| 102 | time_t now; |
| 103 | time(&now); |
| 104 | port_stats->set_timestamp((int)now); |
| 105 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 106 | return port_stats; |
| 107 | |
| 108 | } |
| 109 | |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 110 | #if 0 |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 111 | openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) { |
| 112 | |
| 113 | bcmos_errno err; |
| 114 | bcmbal_flow_stat stat; /**< declare main API struct */ |
| 115 | bcmbal_flow_key key = { }; /**< declare key */ |
| 116 | bcmos_bool clear_on_read = false; |
| 117 | |
| 118 | openolt::FlowStatistics* flow_stats = get_default_flow_statistics(); |
| 119 | //Key |
| 120 | key.flow_id = flow_id; |
| 121 | key.flow_type = flow_type; |
| 122 | |
| 123 | /* init the API struct */ |
| 124 | BCMBAL_STAT_INIT(&stat, flow, key); |
| 125 | BCMBAL_STAT_PROP_GET(&stat, flow, all_properties); |
| 126 | |
| 127 | err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read); |
| 128 | |
| 129 | if (err == BCM_ERR_OK) |
| 130 | { |
| 131 | std::cout << "Flow statistics retrieved" |
| 132 | << " flow_id:" << flow_id |
| 133 | << " flow_type:" << flow_type << std::endl; |
| 134 | |
| 135 | flow_stats->set_rx_bytes(stat.data.rx_bytes); |
| 136 | flow_stats->set_rx_packets(stat.data.rx_packets); |
| 137 | flow_stats->set_tx_bytes(stat.data.tx_bytes); |
| 138 | flow_stats->set_tx_packets(stat.data.tx_packets); |
| 139 | |
| 140 | } else { |
| 141 | std::cout << "ERROR: Failed to retrieve flow statistics" |
| 142 | << " flow_id:" << flow_id |
| 143 | << " flow_type:" << flow_type << std::endl; |
| 144 | } |
| 145 | |
| 146 | return flow_stats; |
| 147 | } |
Shad Ansari | cb004c5 | 2018-05-30 18:07:23 +0000 | [diff] [blame] | 148 | #endif |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 149 | |
| 150 | |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 151 | void stats_collection() { |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 152 | |
Nicolas Palpacuer | c09fdb7 | 2018-08-22 10:23:22 -0400 | [diff] [blame] | 153 | if (!state.is_connected()) { |
| 154 | std::cout << "Voltha is not connected, do not collect stats" << std::endl; |
| 155 | return; |
| 156 | } |
| 157 | if (!state.is_activated()) { |
| 158 | std::cout << "The OLT is not up, do not collect stats" << std::endl; |
| 159 | return; |
| 160 | } |
| 161 | |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 162 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 163 | std::cout << "Collecting statistics" << std::endl; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 164 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 165 | //Ports statistics |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 166 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 167 | //Uplink ports |
| 168 | for (int i = 0; i < 4; i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 169 | bcmbal_interface_key nni_interface; |
| 170 | nni_interface.intf_type = BCMBAL_INTF_TYPE_NNI; |
| 171 | nni_interface.intf_id = i; |
| 172 | |
| 173 | openolt::PortStatistics* port_stats = collectPortStatistics(nni_interface); |
| 174 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 175 | openolt::Indication ind; |
| 176 | ind.set_allocated_port_stats(port_stats); |
| 177 | oltIndQ.push(ind); |
| 178 | } |
| 179 | //Pon ports |
| 180 | for (int i = 0; i < 16; i++) { |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 181 | bcmbal_interface_key pon_interface; |
| 182 | pon_interface.intf_type = BCMBAL_INTF_TYPE_PON; |
| 183 | pon_interface.intf_id = i; |
| 184 | |
| 185 | openolt::PortStatistics* port_stats = collectPortStatistics(pon_interface); |
| 186 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 187 | openolt::Indication ind; |
| 188 | ind.set_allocated_port_stats(port_stats); |
| 189 | oltIndQ.push(ind); |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 192 | //Flows statistics |
| 193 | // flow_inst *current_entry = NULL; |
| 194 | // |
| 195 | // TAILQ_FOREACH(current_entry, |
| 196 | // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list, |
| 197 | // flow_inst_next) { |
| 198 | // int flows_measurements = 0; |
| 199 | // |
| 200 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 201 | // |
| 202 | // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id; |
| 203 | // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type; |
| 204 | // |
| 205 | // if (flows_keys[i].flow_id != 0) { |
| 206 | // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type); |
| 207 | // if (flow_stats->rx_packets() == -1) { |
| 208 | // //It Failed |
| 209 | // flows_keys[i].flow_id = 0; |
| 210 | // } else { |
| 211 | // flow_stats->set_flow_id(flows_keys[i].flow_id); |
| 212 | // time(&now); |
| 213 | // flow_stats->set_timestamp((int)now); |
| 214 | // openolt::Indication ind; |
| 215 | // ind.set_allocated_flow_stats(flow_stats); |
| 216 | // oltIndQ.push(ind); |
| 217 | // flows_measurements ++; |
| 218 | // } |
| 219 | // } |
| 220 | // |
| 221 | // } |
| 222 | // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl; |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 223 | |
| 224 | } |
| 225 | |
| 226 | /* Storing flow keys, temporary */ |
Nicolas Palpacuer | 6a63ea9 | 2018-09-05 17:21:37 -0400 | [diff] [blame] | 227 | // void register_new_flow(bcmbal_flow_key key) { |
| 228 | // for (int i = 0; i < FLOWS_COUNT; i++) { |
| 229 | // if (flows_keys[i].flow_id == 0) { |
| 230 | // flows_keys[i] = key; |
| 231 | // break; |
| 232 | // } |
| 233 | // } |
| 234 | // } |