blob: d364384f70c8e83d98d56a924b64ff7cfdb27aa9 [file] [log] [blame]
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -04001#include "stats_collection.h"
2
3#include <unistd.h>
4#include <pthread.h>
5
6#include <openolt.grpc.pb.h>
7#include "indications.h"
8
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
17#define COLLECTION_PERIOD 15
18//FIXME
19#define FLOWS_COUNT 100
20
21bool isCollectingStatistics;
22bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
23bool init_done = false;
24
25
26void start_collecting_statistics() {
27 if (!init_done) {
28 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
29 init_done = true;
30 }
31 pthread_t statisticsCollectionThread;
32 isCollectingStatistics = true;
33 pthread_create(&statisticsCollectionThread, NULL, stats_collection, NULL);
34
35 std::cout << "Statistics collection thread started" << std::endl;
36}
37
38void stop_collecting_statistics() {
39 isCollectingStatistics = false;
40}
41
42openolt::PortStatistics* get_default_port_statistics() {
43 openolt::PortStatistics* port_stats = new openolt::PortStatistics;
44 port_stats->set_intf_id(-1);
45 port_stats->set_rx_bytes(-1);
46 port_stats->set_rx_packets(-1);
47 port_stats->set_rx_ucast_packets(-1);
48 port_stats->set_rx_mcast_packets(-1);
49 port_stats->set_rx_bcast_packets(-1);
50 port_stats->set_rx_error_packets(-1);
51 port_stats->set_tx_bytes(-1);
52 port_stats->set_tx_packets(-1);
53 port_stats->set_tx_ucast_packets(-1);
54 port_stats->set_tx_mcast_packets(-1);
55 port_stats->set_tx_bcast_packets(-1);
56 port_stats->set_tx_error_packets(-1);
57 port_stats->set_rx_crc_errors(-1);
58 port_stats->set_bip_errors(-1);
59
60 return port_stats;
61}
62
Shad Ansaricb004c52018-05-30 18:07:23 +000063#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040064openolt::FlowStatistics* get_default_flow_statistics() {
65 openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics;
66 flow_stats->set_flow_id(-1);
67 flow_stats->set_rx_bytes(-1);
68 flow_stats->set_rx_packets(-1);
69 flow_stats->set_tx_bytes(-1);
70 flow_stats->set_tx_packets(-1);
71
72 return flow_stats;
73}
Shad Ansaricb004c52018-05-30 18:07:23 +000074#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040075
76openolt::PortStatistics* collectPortStatistics(int intf_id, bcmbal_intf_type intf_type) {
77
78 bcmos_errno err;
79 bcmbal_interface_stat stat; /**< declare main API struct */
80 bcmbal_interface_key key = { }; /**< declare key */
81 bcmos_bool clear_on_read = false;
82
83 openolt::PortStatistics* port_stats = get_default_port_statistics();
84 // build key
85 key.intf_id = (bcmbal_intf_id) intf_id;
86 key.intf_type = intf_type;
87
88 /* init the API struct */
89 BCMBAL_STAT_INIT(&stat, interface, key);
90 BCMBAL_STAT_PROP_GET(&stat, interface, all_properties);
91
92 /* call API */
93 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
94 if (err == BCM_ERR_OK)
95 {
Shad Ansari563ea822018-06-28 14:56:27 +000096 //std::cout << "Interface statistics retrieved"
97 // << " intf_id:" << intf_id << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040098
99 port_stats->set_rx_bytes(stat.data.rx_bytes);
100 port_stats->set_rx_packets(stat.data.rx_packets);
101 port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets);
102 port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets);
103 port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets);
104 port_stats->set_rx_error_packets(stat.data.rx_error_packets);
105 port_stats->set_tx_bytes(stat.data.tx_bytes);
106 port_stats->set_tx_packets(stat.data.tx_packets);
107 port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets);
108 port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets);
109 port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets);
110 port_stats->set_tx_error_packets(stat.data.tx_error_packets);
111 port_stats->set_rx_crc_errors(stat.data.rx_crc_errors);
112 port_stats->set_bip_errors(stat.data.bip_errors);
113
114 } else {
115 std::cout << "ERROR: Failed to retrieve port statistics"
116 << " intf_id:" << intf_id
117 << " intf_type:" << intf_type << std::endl;
118 }
119
120 return port_stats;
121
122}
123
Shad Ansaricb004c52018-05-30 18:07:23 +0000124#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400125openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
126
127 bcmos_errno err;
128 bcmbal_flow_stat stat; /**< declare main API struct */
129 bcmbal_flow_key key = { }; /**< declare key */
130 bcmos_bool clear_on_read = false;
131
132 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
133 //Key
134 key.flow_id = flow_id;
135 key.flow_type = flow_type;
136
137 /* init the API struct */
138 BCMBAL_STAT_INIT(&stat, flow, key);
139 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
140
141 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
142
143 if (err == BCM_ERR_OK)
144 {
145 std::cout << "Flow statistics retrieved"
146 << " flow_id:" << flow_id
147 << " flow_type:" << flow_type << std::endl;
148
149 flow_stats->set_rx_bytes(stat.data.rx_bytes);
150 flow_stats->set_rx_packets(stat.data.rx_packets);
151 flow_stats->set_tx_bytes(stat.data.tx_bytes);
152 flow_stats->set_tx_packets(stat.data.tx_packets);
153
154 } else {
155 std::cout << "ERROR: Failed to retrieve flow statistics"
156 << " flow_id:" << flow_id
157 << " flow_type:" << flow_type << std::endl;
158 }
159
160 return flow_stats;
161}
Shad Ansaricb004c52018-05-30 18:07:23 +0000162#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400163
164
165void* stats_collection(void* x) {
166
167 time_t now;
168
169 while(isCollectingStatistics) {
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400170
171 std::cout << "Collecting statistics" << std::endl;
172
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400173 //Ports statistics
174
175 //Uplink ports
176 for (int i = 0; i < 4; i++) {
177 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_NNI);
178 //FIXME Use clean port translation
179 port_stats->set_intf_id(128 + i);
180 time(&now);
181 port_stats->set_timestamp((int)now);
182 openolt::Indication ind;
183 ind.set_allocated_port_stats(port_stats);
184 oltIndQ.push(ind);
185 }
186 //Pon ports
187 for (int i = 0; i < 16; i++) {
188 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_PON);
189 //FIXME Use clean port translation
190 port_stats->set_intf_id((0x2 << 28) + i);
191 time(&now);
192 port_stats->set_timestamp((int)now);
193 openolt::Indication ind;
194 ind.set_allocated_port_stats(port_stats);
195 oltIndQ.push(ind);
196 }
197
198 //Flows statistics
199 // flow_inst *current_entry = NULL;
200 //
201 // TAILQ_FOREACH(current_entry,
202 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
203 // flow_inst_next) {
204 // int flows_measurements = 0;
205 //
206 // for (int i = 0; i < FLOWS_COUNT; i++) {
207 //
208 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
209 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
210 //
211 // if (flows_keys[i].flow_id != 0) {
212 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
213 // if (flow_stats->rx_packets() == -1) {
214 // //It Failed
215 // flows_keys[i].flow_id = 0;
216 // } else {
217 // flow_stats->set_flow_id(flows_keys[i].flow_id);
218 // time(&now);
219 // flow_stats->set_timestamp((int)now);
220 // openolt::Indication ind;
221 // ind.set_allocated_flow_stats(flow_stats);
222 // oltIndQ.push(ind);
223 // flows_measurements ++;
224 // }
225 // }
226 //
227 // }
228 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
229
230 sleep(COLLECTION_PERIOD);
231
232 }
233
234 std::cout << "Statistics collection thread terminated" << std::endl;
235
236
237}
238
239/* Storing flow keys, temporary */
240void register_new_flow(bcmbal_flow_key key) {
241 for (int i = 0; i < FLOWS_COUNT; i++) {
242 if (flows_keys[i].flow_id == 0) {
243 flows_keys[i] = key;
244 break;
245 }
246 }
247}