blob: 5e23d11214cb8bbbcca75c7b3300e678e7fa11c9 [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 Palpacuer0f19b1a2018-06-07 17:29:31 -04008
9extern "C"
10{
11#include <bcmos_system.h>
12#include <bal_api.h>
13#include <bal_api_end.h>
14#include <flow_fsm.h>
15}
16
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040017//FIXME
18#define FLOWS_COUNT 100
19
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040020bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040021
Shad Ansariedef2132018-08-10 22:14:50 +000022void init_stats() {
23 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040024}
25
26openolt::PortStatistics* get_default_port_statistics() {
27 openolt::PortStatistics* port_stats = new openolt::PortStatistics;
28 port_stats->set_intf_id(-1);
29 port_stats->set_rx_bytes(-1);
30 port_stats->set_rx_packets(-1);
31 port_stats->set_rx_ucast_packets(-1);
32 port_stats->set_rx_mcast_packets(-1);
33 port_stats->set_rx_bcast_packets(-1);
34 port_stats->set_rx_error_packets(-1);
35 port_stats->set_tx_bytes(-1);
36 port_stats->set_tx_packets(-1);
37 port_stats->set_tx_ucast_packets(-1);
38 port_stats->set_tx_mcast_packets(-1);
39 port_stats->set_tx_bcast_packets(-1);
40 port_stats->set_tx_error_packets(-1);
41 port_stats->set_rx_crc_errors(-1);
42 port_stats->set_bip_errors(-1);
43
44 return port_stats;
45}
46
Shad Ansaricb004c52018-05-30 18:07:23 +000047#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040048openolt::FlowStatistics* get_default_flow_statistics() {
49 openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics;
50 flow_stats->set_flow_id(-1);
51 flow_stats->set_rx_bytes(-1);
52 flow_stats->set_rx_packets(-1);
53 flow_stats->set_tx_bytes(-1);
54 flow_stats->set_tx_packets(-1);
55
56 return flow_stats;
57}
Shad Ansaricb004c52018-05-30 18:07:23 +000058#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040059
60openolt::PortStatistics* collectPortStatistics(int intf_id, bcmbal_intf_type intf_type) {
61
62 bcmos_errno err;
63 bcmbal_interface_stat stat; /**< declare main API struct */
64 bcmbal_interface_key key = { }; /**< declare key */
65 bcmos_bool clear_on_read = false;
66
67 openolt::PortStatistics* port_stats = get_default_port_statistics();
68 // build key
69 key.intf_id = (bcmbal_intf_id) intf_id;
70 key.intf_type = intf_type;
71
72 /* init the API struct */
73 BCMBAL_STAT_INIT(&stat, interface, key);
74 BCMBAL_STAT_PROP_GET(&stat, interface, all_properties);
75
76 /* call API */
77 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
78 if (err == BCM_ERR_OK)
79 {
Shad Ansari563ea822018-06-28 14:56:27 +000080 //std::cout << "Interface statistics retrieved"
81 // << " intf_id:" << intf_id << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040082
83 port_stats->set_rx_bytes(stat.data.rx_bytes);
84 port_stats->set_rx_packets(stat.data.rx_packets);
85 port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets);
86 port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets);
87 port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets);
88 port_stats->set_rx_error_packets(stat.data.rx_error_packets);
89 port_stats->set_tx_bytes(stat.data.tx_bytes);
90 port_stats->set_tx_packets(stat.data.tx_packets);
91 port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets);
92 port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets);
93 port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets);
94 port_stats->set_tx_error_packets(stat.data.tx_error_packets);
95 port_stats->set_rx_crc_errors(stat.data.rx_crc_errors);
96 port_stats->set_bip_errors(stat.data.bip_errors);
97
98 } else {
99 std::cout << "ERROR: Failed to retrieve port statistics"
100 << " intf_id:" << intf_id
101 << " intf_type:" << intf_type << std::endl;
102 }
103
104 return port_stats;
105
106}
107
Shad Ansaricb004c52018-05-30 18:07:23 +0000108#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400109openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
110
111 bcmos_errno err;
112 bcmbal_flow_stat stat; /**< declare main API struct */
113 bcmbal_flow_key key = { }; /**< declare key */
114 bcmos_bool clear_on_read = false;
115
116 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
117 //Key
118 key.flow_id = flow_id;
119 key.flow_type = flow_type;
120
121 /* init the API struct */
122 BCMBAL_STAT_INIT(&stat, flow, key);
123 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
124
125 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
126
127 if (err == BCM_ERR_OK)
128 {
129 std::cout << "Flow statistics retrieved"
130 << " flow_id:" << flow_id
131 << " flow_type:" << flow_type << std::endl;
132
133 flow_stats->set_rx_bytes(stat.data.rx_bytes);
134 flow_stats->set_rx_packets(stat.data.rx_packets);
135 flow_stats->set_tx_bytes(stat.data.tx_bytes);
136 flow_stats->set_tx_packets(stat.data.tx_packets);
137
138 } else {
139 std::cout << "ERROR: Failed to retrieve flow statistics"
140 << " flow_id:" << flow_id
141 << " flow_type:" << flow_type << std::endl;
142 }
143
144 return flow_stats;
145}
Shad Ansaricb004c52018-05-30 18:07:23 +0000146#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400147
148
Shad Ansariedef2132018-08-10 22:14:50 +0000149void* stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400150
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400151 if (!state.is_connected()) {
152 std::cout << "Voltha is not connected, do not collect stats" << std::endl;
153 return;
154 }
155 if (!state.is_activated()) {
156 std::cout << "The OLT is not up, do not collect stats" << std::endl;
157 return;
158 }
159
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400160 time_t now;
161
Shad Ansariedef2132018-08-10 22:14:50 +0000162 std::cout << "Collecting statistics" << std::endl;
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++) {
168 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_NNI);
169 //FIXME Use clean port translation
170 port_stats->set_intf_id(128 + i);
171 time(&now);
172 port_stats->set_timestamp((int)now);
173 openolt::Indication ind;
174 ind.set_allocated_port_stats(port_stats);
175 oltIndQ.push(ind);
176 }
177 //Pon ports
178 for (int i = 0; i < 16; i++) {
179 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_PON);
180 //FIXME Use clean port translation
181 port_stats->set_intf_id((0x2 << 28) + i);
182 time(&now);
183 port_stats->set_timestamp((int)now);
184 openolt::Indication ind;
185 ind.set_allocated_port_stats(port_stats);
186 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400187 }
188
Shad Ansariedef2132018-08-10 22:14:50 +0000189 //Flows statistics
190 // flow_inst *current_entry = NULL;
191 //
192 // TAILQ_FOREACH(current_entry,
193 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
194 // flow_inst_next) {
195 // int flows_measurements = 0;
196 //
197 // for (int i = 0; i < FLOWS_COUNT; i++) {
198 //
199 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
200 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
201 //
202 // if (flows_keys[i].flow_id != 0) {
203 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
204 // if (flow_stats->rx_packets() == -1) {
205 // //It Failed
206 // flows_keys[i].flow_id = 0;
207 // } else {
208 // flow_stats->set_flow_id(flows_keys[i].flow_id);
209 // time(&now);
210 // flow_stats->set_timestamp((int)now);
211 // openolt::Indication ind;
212 // ind.set_allocated_flow_stats(flow_stats);
213 // oltIndQ.push(ind);
214 // flows_measurements ++;
215 // }
216 // }
217 //
218 // }
219 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400220
221}
222
223/* Storing flow keys, temporary */
224void register_new_flow(bcmbal_flow_key key) {
225 for (int i = 0; i < FLOWS_COUNT; i++) {
226 if (flows_keys[i].flow_id == 0) {
227 flows_keys[i] = key;
228 break;
229 }
230 }
231}