blob: 559b2c15a6cf75c49cfe3c62b2aa70690f55dfa5 [file] [log] [blame]
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -04001#include "stats_collection.h"
2
3#include <unistd.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -04004
5#include <openolt.grpc.pb.h>
6#include "indications.h"
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -04007#include "core.h"
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -04008#include "translation.h"
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -04009
10extern "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 Palpacuer0f19b1a2018-06-07 17:29:31 -040018//FIXME
19#define FLOWS_COUNT 100
20
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040021bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040022
Shad Ansariedef2132018-08-10 22:14:50 +000023void init_stats() {
24 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040025}
26
27openolt::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 Ansaricb004c52018-05-30 18:07:23 +000048#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040049openolt::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 Ansaricb004c52018-05-30 18:07:23 +000059#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040060
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -040061openolt::PortStatistics* collectPortStatistics(bcmbal_interface_key key) {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040062
63 bcmos_errno err;
64 bcmbal_interface_stat stat; /**< declare main API struct */
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040065 bcmos_bool clear_on_read = false;
66
67 openolt::PortStatistics* port_stats = get_default_port_statistics();
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040068
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 Ansari563ea822018-06-28 14:56:27 +000077 //std::cout << "Interface statistics retrieved"
78 // << " intf_id:" << intf_id << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040079
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 Palpacuer6a63ea92018-09-05 17:21:37 -040097 << " intf_id:" << key.intf_id
98 << " intf_type:" << key.intf_type << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040099 }
100
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400101 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 Palpacuer0f19b1a2018-06-07 17:29:31 -0400106 return port_stats;
107
108}
109
Shad Ansaricb004c52018-05-30 18:07:23 +0000110#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400111openolt::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 Ansaricb004c52018-05-30 18:07:23 +0000148#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400149
150
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400151void stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400152
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400153 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 Palpacuer0f19b1a2018-06-07 17:29:31 -0400162
Shad Ansariedef2132018-08-10 22:14:50 +0000163 std::cout << "Collecting statistics" << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400164
Shad Ansariedef2132018-08-10 22:14:50 +0000165 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400166
Shad Ansariedef2132018-08-10 22:14:50 +0000167 //Uplink ports
168 for (int i = 0; i < 4; i++) {
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400169 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 Ansariedef2132018-08-10 22:14:50 +0000175 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 Palpacuer6a63ea92018-09-05 17:21:37 -0400181 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 Ansariedef2132018-08-10 22:14:50 +0000187 openolt::Indication ind;
188 ind.set_allocated_port_stats(port_stats);
189 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400190 }
191
Shad Ansariedef2132018-08-10 22:14:50 +0000192 //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 Palpacuer0f19b1a2018-06-07 17:29:31 -0400223
224}
225
226/* Storing flow keys, temporary */
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400227// 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// }