blob: 0416ef44d1cf332270334187c54c9ce2494a8d7b [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"
7
8extern "C"
9{
10#include <bcmos_system.h>
11#include <bal_api.h>
12#include <bal_api_end.h>
13#include <flow_fsm.h>
14}
15
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040016//FIXME
17#define FLOWS_COUNT 100
18
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040019bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040020
Shad Ansariedef2132018-08-10 22:14:50 +000021void init_stats() {
22 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040023}
24
25openolt::PortStatistics* get_default_port_statistics() {
26 openolt::PortStatistics* port_stats = new openolt::PortStatistics;
27 port_stats->set_intf_id(-1);
28 port_stats->set_rx_bytes(-1);
29 port_stats->set_rx_packets(-1);
30 port_stats->set_rx_ucast_packets(-1);
31 port_stats->set_rx_mcast_packets(-1);
32 port_stats->set_rx_bcast_packets(-1);
33 port_stats->set_rx_error_packets(-1);
34 port_stats->set_tx_bytes(-1);
35 port_stats->set_tx_packets(-1);
36 port_stats->set_tx_ucast_packets(-1);
37 port_stats->set_tx_mcast_packets(-1);
38 port_stats->set_tx_bcast_packets(-1);
39 port_stats->set_tx_error_packets(-1);
40 port_stats->set_rx_crc_errors(-1);
41 port_stats->set_bip_errors(-1);
42
43 return port_stats;
44}
45
Shad Ansaricb004c52018-05-30 18:07:23 +000046#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040047openolt::FlowStatistics* get_default_flow_statistics() {
48 openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics;
49 flow_stats->set_flow_id(-1);
50 flow_stats->set_rx_bytes(-1);
51 flow_stats->set_rx_packets(-1);
52 flow_stats->set_tx_bytes(-1);
53 flow_stats->set_tx_packets(-1);
54
55 return flow_stats;
56}
Shad Ansaricb004c52018-05-30 18:07:23 +000057#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040058
59openolt::PortStatistics* collectPortStatistics(int intf_id, bcmbal_intf_type intf_type) {
60
61 bcmos_errno err;
62 bcmbal_interface_stat stat; /**< declare main API struct */
63 bcmbal_interface_key key = { }; /**< declare key */
64 bcmos_bool clear_on_read = false;
65
66 openolt::PortStatistics* port_stats = get_default_port_statistics();
67 // build key
68 key.intf_id = (bcmbal_intf_id) intf_id;
69 key.intf_type = intf_type;
70
71 /* init the API struct */
72 BCMBAL_STAT_INIT(&stat, interface, key);
73 BCMBAL_STAT_PROP_GET(&stat, interface, all_properties);
74
75 /* call API */
76 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
77 if (err == BCM_ERR_OK)
78 {
Shad Ansari563ea822018-06-28 14:56:27 +000079 //std::cout << "Interface statistics retrieved"
80 // << " intf_id:" << intf_id << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040081
82 port_stats->set_rx_bytes(stat.data.rx_bytes);
83 port_stats->set_rx_packets(stat.data.rx_packets);
84 port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets);
85 port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets);
86 port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets);
87 port_stats->set_rx_error_packets(stat.data.rx_error_packets);
88 port_stats->set_tx_bytes(stat.data.tx_bytes);
89 port_stats->set_tx_packets(stat.data.tx_packets);
90 port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets);
91 port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets);
92 port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets);
93 port_stats->set_tx_error_packets(stat.data.tx_error_packets);
94 port_stats->set_rx_crc_errors(stat.data.rx_crc_errors);
95 port_stats->set_bip_errors(stat.data.bip_errors);
96
97 } else {
98 std::cout << "ERROR: Failed to retrieve port statistics"
99 << " intf_id:" << intf_id
100 << " intf_type:" << intf_type << std::endl;
101 }
102
103 return port_stats;
104
105}
106
Shad Ansaricb004c52018-05-30 18:07:23 +0000107#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400108openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
109
110 bcmos_errno err;
111 bcmbal_flow_stat stat; /**< declare main API struct */
112 bcmbal_flow_key key = { }; /**< declare key */
113 bcmos_bool clear_on_read = false;
114
115 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
116 //Key
117 key.flow_id = flow_id;
118 key.flow_type = flow_type;
119
120 /* init the API struct */
121 BCMBAL_STAT_INIT(&stat, flow, key);
122 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
123
124 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
125
126 if (err == BCM_ERR_OK)
127 {
128 std::cout << "Flow statistics retrieved"
129 << " flow_id:" << flow_id
130 << " flow_type:" << flow_type << std::endl;
131
132 flow_stats->set_rx_bytes(stat.data.rx_bytes);
133 flow_stats->set_rx_packets(stat.data.rx_packets);
134 flow_stats->set_tx_bytes(stat.data.tx_bytes);
135 flow_stats->set_tx_packets(stat.data.tx_packets);
136
137 } else {
138 std::cout << "ERROR: Failed to retrieve flow statistics"
139 << " flow_id:" << flow_id
140 << " flow_type:" << flow_type << std::endl;
141 }
142
143 return flow_stats;
144}
Shad Ansaricb004c52018-05-30 18:07:23 +0000145#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400146
147
Shad Ansariedef2132018-08-10 22:14:50 +0000148void* stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400149
150 time_t now;
151
Shad Ansariedef2132018-08-10 22:14:50 +0000152 std::cout << "Collecting statistics" << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400153
Shad Ansariedef2132018-08-10 22:14:50 +0000154 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400155
Shad Ansariedef2132018-08-10 22:14:50 +0000156 //Uplink ports
157 for (int i = 0; i < 4; i++) {
158 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_NNI);
159 //FIXME Use clean port translation
160 port_stats->set_intf_id(128 + i);
161 time(&now);
162 port_stats->set_timestamp((int)now);
163 openolt::Indication ind;
164 ind.set_allocated_port_stats(port_stats);
165 oltIndQ.push(ind);
166 }
167 //Pon ports
168 for (int i = 0; i < 16; i++) {
169 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_PON);
170 //FIXME Use clean port translation
171 port_stats->set_intf_id((0x2 << 28) + i);
172 time(&now);
173 port_stats->set_timestamp((int)now);
174 openolt::Indication ind;
175 ind.set_allocated_port_stats(port_stats);
176 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400177 }
178
Shad Ansariedef2132018-08-10 22:14:50 +0000179 //Flows statistics
180 // flow_inst *current_entry = NULL;
181 //
182 // TAILQ_FOREACH(current_entry,
183 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
184 // flow_inst_next) {
185 // int flows_measurements = 0;
186 //
187 // for (int i = 0; i < FLOWS_COUNT; i++) {
188 //
189 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
190 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
191 //
192 // if (flows_keys[i].flow_id != 0) {
193 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
194 // if (flow_stats->rx_packets() == -1) {
195 // //It Failed
196 // flows_keys[i].flow_id = 0;
197 // } else {
198 // flow_stats->set_flow_id(flows_keys[i].flow_id);
199 // time(&now);
200 // flow_stats->set_timestamp((int)now);
201 // openolt::Indication ind;
202 // ind.set_allocated_flow_stats(flow_stats);
203 // oltIndQ.push(ind);
204 // flows_measurements ++;
205 // }
206 // }
207 //
208 // }
209 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400210
211}
212
213/* Storing flow keys, temporary */
214void register_new_flow(bcmbal_flow_key key) {
215 for (int i = 0; i < FLOWS_COUNT; i++) {
216 if (flows_keys[i].flow_id == 0) {
217 flows_keys[i] = key;
218 break;
219 }
220 }
221}