blob: 8afc5f25615b6560963655dc0a0bff178966ffd4 [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 {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -040096 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 Palpacuer0f19b1a2018-06-07 17:29:31 -040098 }
99
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400100 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 Palpacuer0f19b1a2018-06-07 17:29:31 -0400105 return port_stats;
106
107}
108
Shad Ansaricb004c52018-05-30 18:07:23 +0000109#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400110openolt::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 Ansaricb004c52018-05-30 18:07:23 +0000147#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400148
149
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400150void stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400151
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400152 if (!state.is_connected()) {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400153 BCM_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400154 return;
155 }
156 if (!state.is_activated()) {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400157 BCM_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400158 return;
159 }
160
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400161
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400162 BCM_LOG(INFO, openolt_log_id, "Collecting statistics\n");
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400163
Shad Ansariedef2132018-08-10 22:14:50 +0000164 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400165
Shad Ansariedef2132018-08-10 22:14:50 +0000166 //Uplink ports
167 for (int i = 0; i < 4; i++) {
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400168 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 Ansariedef2132018-08-10 22:14:50 +0000174 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 Palpacuer6a63ea92018-09-05 17:21:37 -0400180 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 Ansariedef2132018-08-10 22:14:50 +0000186 openolt::Indication ind;
187 ind.set_allocated_port_stats(port_stats);
188 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400189 }
190
Shad Ansariedef2132018-08-10 22:14:50 +0000191 //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 Palpacuer0f19b1a2018-06-07 17:29:31 -0400222
223}
224
225/* Storing flow keys, temporary */
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400226// 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// }