blob: 9caa98e4248196a886f1374f2918162031d3083e [file] [log] [blame]
Zack Williams477ba092018-10-17 10:50:06 -07001/*
Girish Gowdraa707e7c2019-11-07 11:36:13 +05302 * Copyright 2018-present Open Networking Foundation
Zack Williams477ba092018-10-17 10:50:06 -07003
Girish Gowdraa707e7c2019-11-07 11:36:13 +05304 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Zack Williams477ba092018-10-17 10:50:06 -07007
Girish Gowdraa707e7c2019-11-07 11:36:13 +05308 * http://www.apache.org/licenses/LICENSE-2.0
Zack Williams477ba092018-10-17 10:50:06 -07009
Girish Gowdraa707e7c2019-11-07 11:36:13 +053010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Zack Williams477ba092018-10-17 10:50:06 -070016
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040017#include "stats_collection.h"
18
19#include <unistd.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040020
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040021#include "indications.h"
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -040022#include "core.h"
Girish Gowdraddf9a162020-01-27 12:56:27 +053023#include "core_data.h"
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -040024#include "translation.h"
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040025
26extern "C"
27{
28#include <bcmos_system.h>
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000029#include <bcmolt_api.h>
30#include <bcmolt_api_model_api_structs.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040031}
32
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040033//FIXME
34#define FLOWS_COUNT 100
35
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000036bcmolt_flow_key* flows_keys = new bcmolt_flow_key[FLOWS_COUNT];
37bcmolt_odid device_id = 0;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040038
Shad Ansariedef2132018-08-10 22:14:50 +000039void init_stats() {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000040 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmolt_flow_key));
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040041}
42
43openolt::PortStatistics* get_default_port_statistics() {
44 openolt::PortStatistics* port_stats = new openolt::PortStatistics;
45 port_stats->set_intf_id(-1);
46 port_stats->set_rx_bytes(-1);
47 port_stats->set_rx_packets(-1);
48 port_stats->set_rx_ucast_packets(-1);
49 port_stats->set_rx_mcast_packets(-1);
50 port_stats->set_rx_bcast_packets(-1);
51 port_stats->set_rx_error_packets(-1);
52 port_stats->set_tx_bytes(-1);
53 port_stats->set_tx_packets(-1);
54 port_stats->set_tx_ucast_packets(-1);
55 port_stats->set_tx_mcast_packets(-1);
56 port_stats->set_tx_bcast_packets(-1);
57 port_stats->set_tx_error_packets(-1);
58 port_stats->set_rx_crc_errors(-1);
59 port_stats->set_bip_errors(-1);
60
61 return port_stats;
62}
63
Shad Ansaricb004c52018-05-30 18:07:23 +000064#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040065openolt::FlowStatistics* get_default_flow_statistics() {
66 openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics;
67 flow_stats->set_flow_id(-1);
68 flow_stats->set_rx_bytes(-1);
69 flow_stats->set_rx_packets(-1);
70 flow_stats->set_tx_bytes(-1);
71 flow_stats->set_tx_packets(-1);
72
73 return flow_stats;
74}
Shad Ansaricb004c52018-05-30 18:07:23 +000075#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040076
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000077openolt::PortStatistics* collectPortStatistics(bcmolt_intf_ref intf_ref) {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040078
79 openolt::PortStatistics* port_stats = get_default_port_statistics();
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000080#ifndef TEST_MODE
81 bcmos_errno err;
82 bcmolt_stat_flags clear_on_read = BCMOLT_STAT_FLAGS_NONE;
83 bcmolt_nni_interface_stats nni_stats;
84 bcmolt_onu_itu_pon_stats pon_stats;
85 bcmolt_pon_interface_itu_pon_stats itu_pon_stats;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040086
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040087
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000088 switch (intf_ref.intf_type) {
89 case BCMOLT_INTERFACE_TYPE_NNI:
90 {
91 bcmolt_nni_interface_key nni_intf_key;
92 nni_intf_key.id = intf_ref.intf_id;
93 /* init the API struct */
94 BCMOLT_STAT_INIT(&nni_stats, nni_interface, stats, nni_intf_key);
95 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bytes);
96 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_packets);
97 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_ucast_packets);
98 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_mcast_packets);
99 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bcast_packets);
100 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_error_packets);
101 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bytes);
102 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_packets);
103 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_ucast_packets);
104 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_mcast_packets);
105 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bcast_packets);
106 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_error_packets);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400107
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000108 /* call API */
109 err = bcmolt_stat_get((bcmolt_oltid)device_id, &nni_stats.hdr, clear_on_read);
110 if (err == BCM_ERR_OK)
111 {
112 //std::cout << "Interface statistics retrieved"
113 // << " intf_id:" << intf_id << std::endl;
114
115 port_stats->set_rx_bytes(nni_stats.data.rx_bytes);
116 port_stats->set_rx_packets(nni_stats.data.rx_packets);
117 port_stats->set_rx_ucast_packets(nni_stats.data.rx_ucast_packets);
118 port_stats->set_rx_mcast_packets(nni_stats.data.rx_mcast_packets);
119 port_stats->set_rx_bcast_packets(nni_stats.data.rx_bcast_packets);
120 port_stats->set_rx_error_packets(nni_stats.data.rx_error_packets);
121 port_stats->set_tx_bytes(nni_stats.data.tx_bytes);
122 port_stats->set_tx_packets(nni_stats.data.tx_packets);
123 port_stats->set_tx_ucast_packets(nni_stats.data.tx_ucast_packets);
124 port_stats->set_tx_mcast_packets(nni_stats.data.tx_mcast_packets);
125 port_stats->set_tx_bcast_packets(nni_stats.data.tx_bcast_packets);
126 port_stats->set_tx_error_packets(nni_stats.data.tx_error_packets);
127
128 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500129 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
130 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000131 }
132 break;
133 }
134 case BCMOLT_INTERFACE_TYPE_PON:
135 {
136 bcmolt_pon_interface_key key;
137 key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
138 BCMOLT_STAT_INIT(&itu_pon_stats, pon_interface, itu_pon_stats, key);
139 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, tx_packets);
140 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, bip_errors);
141 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, rx_crc_error);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400142
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000143 /* call API */
144 err = bcmolt_stat_get((bcmolt_oltid)device_id, &itu_pon_stats.hdr, clear_on_read);
145 if (err == BCM_ERR_OK) {
146 port_stats->set_tx_packets(itu_pon_stats.data.tx_packets);
147 port_stats->set_bip_errors(itu_pon_stats.data.bip_errors);
148 port_stats->set_rx_crc_errors(itu_pon_stats.data.rx_crc_error);
149 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500150 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
151 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000152 }
153 {
154 bcmolt_onu_key key;
155 key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
156 BCMOLT_STAT_INIT(&pon_stats, onu, itu_pon_stats, key);
157 BCMOLT_MSG_FIELD_GET(&pon_stats, rx_bytes);
158 BCMOLT_MSG_FIELD_GET(&pon_stats, rx_packets);
159 BCMOLT_MSG_FIELD_GET(&pon_stats, tx_bytes);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400160
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000161 /* call API */
162 err = bcmolt_stat_get((bcmolt_oltid)device_id, &pon_stats.hdr, clear_on_read);
163 if (err == BCM_ERR_OK) {
164 port_stats->set_rx_bytes(pon_stats.data.rx_bytes);
165 port_stats->set_rx_packets(pon_stats.data.rx_packets);
166 port_stats->set_tx_bytes(pon_stats.data.tx_bytes);
167 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500168 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
169 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000170 }
171 }
172 break;
173 }
174 }
175
176 port_stats->set_intf_id(interface_key_to_port_no((bcmolt_interface_id)intf_ref.intf_id, (bcmolt_interface_type)intf_ref.intf_type));
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400177 time_t now;
178 time(&now);
179 port_stats->set_timestamp((int)now);
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000180#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400181 return port_stats;
182
183}
184
Shad Ansaricb004c52018-05-30 18:07:23 +0000185#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400186openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
187
188 bcmos_errno err;
189 bcmbal_flow_stat stat; /**< declare main API struct */
190 bcmbal_flow_key key = { }; /**< declare key */
191 bcmos_bool clear_on_read = false;
192
193 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
194 //Key
195 key.flow_id = flow_id;
196 key.flow_type = flow_type;
197
198 /* init the API struct */
199 BCMBAL_STAT_INIT(&stat, flow, key);
200 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
201
202 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
203
204 if (err == BCM_ERR_OK)
205 {
206 std::cout << "Flow statistics retrieved"
207 << " flow_id:" << flow_id
208 << " flow_type:" << flow_type << std::endl;
209
210 flow_stats->set_rx_bytes(stat.data.rx_bytes);
211 flow_stats->set_rx_packets(stat.data.rx_packets);
212 flow_stats->set_tx_bytes(stat.data.tx_bytes);
213 flow_stats->set_tx_packets(stat.data.tx_packets);
214
215 } else {
216 std::cout << "ERROR: Failed to retrieve flow statistics"
217 << " flow_id:" << flow_id
218 << " flow_type:" << flow_type << std::endl;
219 }
220
221 return flow_stats;
222}
Shad Ansaricb004c52018-05-30 18:07:23 +0000223#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400224
225
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400226void stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400227
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400228 if (!state.is_connected()) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000229 OPENOLT_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400230 return;
231 }
232 if (!state.is_activated()) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000233 OPENOLT_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400234 return;
235 }
236
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400237
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000238 OPENOLT_LOG(DEBUG, openolt_log_id, "Collecting statistics\n");
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400239
Shad Ansariedef2132018-08-10 22:14:50 +0000240 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400241
Shad Ansariedef2132018-08-10 22:14:50 +0000242 //Uplink ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500243 for (int i = 0; i < NumNniIf_(); i++) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000244 bcmolt_intf_ref intf_ref;
245 intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_NNI;
246 intf_ref.intf_id = i;
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400247
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000248 openolt::PortStatistics* port_stats =
249 collectPortStatistics(intf_ref);
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400250
Shad Ansariedef2132018-08-10 22:14:50 +0000251 openolt::Indication ind;
252 ind.set_allocated_port_stats(port_stats);
253 oltIndQ.push(ind);
254 }
255 //Pon ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500256 for (int i = 0; i < NumPonIf_(); i++) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000257 bcmolt_intf_ref intf_ref;
258 intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_PON;
259 intf_ref.intf_id = i;
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400260
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000261 openolt::PortStatistics* port_stats =
262 collectPortStatistics(intf_ref);
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400263
Shad Ansariedef2132018-08-10 22:14:50 +0000264 openolt::Indication ind;
265 ind.set_allocated_port_stats(port_stats);
266 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400267 }
268
Shad Ansariedef2132018-08-10 22:14:50 +0000269 //Flows statistics
270 // flow_inst *current_entry = NULL;
271 //
272 // TAILQ_FOREACH(current_entry,
273 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
274 // flow_inst_next) {
275 // int flows_measurements = 0;
276 //
277 // for (int i = 0; i < FLOWS_COUNT; i++) {
278 //
279 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
280 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
281 //
282 // if (flows_keys[i].flow_id != 0) {
283 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
284 // if (flow_stats->rx_packets() == -1) {
285 // //It Failed
286 // flows_keys[i].flow_id = 0;
287 // } else {
288 // flow_stats->set_flow_id(flows_keys[i].flow_id);
289 // time(&now);
290 // flow_stats->set_timestamp((int)now);
291 // openolt::Indication ind;
292 // ind.set_allocated_flow_stats(flow_stats);
293 // oltIndQ.push(ind);
294 // flows_measurements ++;
295 // }
296 // }
297 //
298 // }
299 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400300
301}
302
303/* Storing flow keys, temporary */
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400304// void register_new_flow(bcmbal_flow_key key) {
305// for (int i = 0; i < FLOWS_COUNT; i++) {
306// if (flows_keys[i].flow_id == 0) {
307// flows_keys[i] = key;
308// break;
309// }
310// }
311// }