blob: 5ec33bc1e636765980e5989f3cf8521c862bfb47 [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 {
96 std::cout << "Interface statistics retrieved"
97 << " intf_id:" << intf_id << std::endl;
98
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) {
170 //Ports statistics
171
172 //Uplink ports
173 for (int i = 0; i < 4; i++) {
174 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_NNI);
175 //FIXME Use clean port translation
176 port_stats->set_intf_id(128 + i);
177 time(&now);
178 port_stats->set_timestamp((int)now);
179 openolt::Indication ind;
180 ind.set_allocated_port_stats(port_stats);
181 oltIndQ.push(ind);
182 }
183 //Pon ports
184 for (int i = 0; i < 16; i++) {
185 openolt::PortStatistics* port_stats = collectPortStatistics(i, BCMBAL_INTF_TYPE_PON);
186 //FIXME Use clean port translation
187 port_stats->set_intf_id((0x2 << 28) + i);
188 time(&now);
189 port_stats->set_timestamp((int)now);
190 openolt::Indication ind;
191 ind.set_allocated_port_stats(port_stats);
192 oltIndQ.push(ind);
193 }
194
195 //Flows statistics
196 // flow_inst *current_entry = NULL;
197 //
198 // TAILQ_FOREACH(current_entry,
199 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
200 // flow_inst_next) {
201 // int flows_measurements = 0;
202 //
203 // for (int i = 0; i < FLOWS_COUNT; i++) {
204 //
205 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
206 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
207 //
208 // if (flows_keys[i].flow_id != 0) {
209 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
210 // if (flow_stats->rx_packets() == -1) {
211 // //It Failed
212 // flows_keys[i].flow_id = 0;
213 // } else {
214 // flow_stats->set_flow_id(flows_keys[i].flow_id);
215 // time(&now);
216 // flow_stats->set_timestamp((int)now);
217 // openolt::Indication ind;
218 // ind.set_allocated_flow_stats(flow_stats);
219 // oltIndQ.push(ind);
220 // flows_measurements ++;
221 // }
222 // }
223 //
224 // }
225 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
226
227 sleep(COLLECTION_PERIOD);
228
229 }
230
231 std::cout << "Statistics collection thread terminated" << std::endl;
232
233
234}
235
236/* Storing flow keys, temporary */
237void register_new_flow(bcmbal_flow_key key) {
238 for (int i = 0; i < FLOWS_COUNT; i++) {
239 if (flows_keys[i].flow_id == 0) {
240 flows_keys[i] = key;
241 break;
242 }
243 }
244}