blob: 49793660ca756bd138cfffd5c0ac9f35b64f57d5 [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];
Jason Huang5d9ab1a2020-04-15 16:53:49 +080037bcmolt_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
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000087 switch (intf_ref.intf_type) {
88 case BCMOLT_INTERFACE_TYPE_NNI:
89 {
90 bcmolt_nni_interface_key nni_intf_key;
91 nni_intf_key.id = intf_ref.intf_id;
92 /* init the API struct */
93 BCMOLT_STAT_INIT(&nni_stats, nni_interface, stats, nni_intf_key);
94 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bytes);
95 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_packets);
96 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_ucast_packets);
97 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_mcast_packets);
98 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bcast_packets);
99 BCMOLT_MSG_FIELD_GET(&nni_stats, rx_error_packets);
100 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bytes);
101 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_packets);
102 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_ucast_packets);
103 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_mcast_packets);
104 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bcast_packets);
105 BCMOLT_MSG_FIELD_GET(&nni_stats, tx_error_packets);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400106
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000107 /* call API */
108 err = bcmolt_stat_get((bcmolt_oltid)device_id, &nni_stats.hdr, clear_on_read);
109 if (err == BCM_ERR_OK)
110 {
111 //std::cout << "Interface statistics retrieved"
112 // << " intf_id:" << intf_id << std::endl;
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800113
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000114 port_stats->set_rx_bytes(nni_stats.data.rx_bytes);
115 port_stats->set_rx_packets(nni_stats.data.rx_packets);
116 port_stats->set_rx_ucast_packets(nni_stats.data.rx_ucast_packets);
117 port_stats->set_rx_mcast_packets(nni_stats.data.rx_mcast_packets);
118 port_stats->set_rx_bcast_packets(nni_stats.data.rx_bcast_packets);
119 port_stats->set_rx_error_packets(nni_stats.data.rx_error_packets);
120 port_stats->set_tx_bytes(nni_stats.data.tx_bytes);
121 port_stats->set_tx_packets(nni_stats.data.tx_packets);
122 port_stats->set_tx_ucast_packets(nni_stats.data.tx_ucast_packets);
123 port_stats->set_tx_mcast_packets(nni_stats.data.tx_mcast_packets);
124 port_stats->set_tx_bcast_packets(nni_stats.data.tx_bcast_packets);
125 port_stats->set_tx_error_packets(nni_stats.data.tx_error_packets);
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800126
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000127 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500128 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
129 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000130 }
131 break;
132 }
133 case BCMOLT_INTERFACE_TYPE_PON:
134 {
135 bcmolt_pon_interface_key key;
136 key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
137 BCMOLT_STAT_INIT(&itu_pon_stats, pon_interface, itu_pon_stats, key);
138 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, tx_packets);
139 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, bip_errors);
140 BCMOLT_MSG_FIELD_GET(&itu_pon_stats, rx_crc_error);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400141
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000142 /* call API */
143 err = bcmolt_stat_get((bcmolt_oltid)device_id, &itu_pon_stats.hdr, clear_on_read);
144 if (err == BCM_ERR_OK) {
145 port_stats->set_tx_packets(itu_pon_stats.data.tx_packets);
146 port_stats->set_bip_errors(itu_pon_stats.data.bip_errors);
147 port_stats->set_rx_crc_errors(itu_pon_stats.data.rx_crc_error);
148 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500149 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
150 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000151 }
152 {
153 bcmolt_onu_key key;
154 key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
155 BCMOLT_STAT_INIT(&pon_stats, onu, itu_pon_stats, key);
156 BCMOLT_MSG_FIELD_GET(&pon_stats, rx_bytes);
157 BCMOLT_MSG_FIELD_GET(&pon_stats, rx_packets);
158 BCMOLT_MSG_FIELD_GET(&pon_stats, tx_bytes);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400159
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000160 /* call API */
161 err = bcmolt_stat_get((bcmolt_oltid)device_id, &pon_stats.hdr, clear_on_read);
162 if (err == BCM_ERR_OK) {
163 port_stats->set_rx_bytes(pon_stats.data.rx_bytes);
164 port_stats->set_rx_packets(pon_stats.data.rx_packets);
165 port_stats->set_tx_bytes(pon_stats.data.tx_bytes);
166 } else {
Thiyagarajan Subramani0890b1f2019-11-22 07:52:47 -0500167 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err = %s\n",
168 (int)intf_ref.intf_id, (int)intf_ref.intf_type, bcmos_strerror(err));
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000169 }
170 }
171 break;
172 }
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800173 }
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000174
175 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 -0400176 time_t now;
177 time(&now);
178 port_stats->set_timestamp((int)now);
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000179#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400180 return port_stats;
181
182}
183
Shad Ansaricb004c52018-05-30 18:07:23 +0000184#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400185openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
186
187 bcmos_errno err;
188 bcmbal_flow_stat stat; /**< declare main API struct */
189 bcmbal_flow_key key = { }; /**< declare key */
190 bcmos_bool clear_on_read = false;
191
192 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
193 //Key
194 key.flow_id = flow_id;
195 key.flow_type = flow_type;
196
197 /* init the API struct */
198 BCMBAL_STAT_INIT(&stat, flow, key);
199 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
200
201 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
202
203 if (err == BCM_ERR_OK)
204 {
205 std::cout << "Flow statistics retrieved"
206 << " flow_id:" << flow_id
207 << " flow_type:" << flow_type << std::endl;
208
209 flow_stats->set_rx_bytes(stat.data.rx_bytes);
210 flow_stats->set_rx_packets(stat.data.rx_packets);
211 flow_stats->set_tx_bytes(stat.data.tx_bytes);
212 flow_stats->set_tx_packets(stat.data.tx_packets);
213
214 } else {
215 std::cout << "ERROR: Failed to retrieve flow statistics"
216 << " flow_id:" << flow_id
217 << " flow_type:" << flow_type << std::endl;
218 }
219
220 return flow_stats;
221}
Shad Ansaricb004c52018-05-30 18:07:23 +0000222#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400223
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400224void stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400225
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400226 if (!state.is_connected()) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000227 OPENOLT_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400228 return;
229 }
230 if (!state.is_activated()) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000231 OPENOLT_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400232 return;
233 }
234
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000235 OPENOLT_LOG(DEBUG, openolt_log_id, "Collecting statistics\n");
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400236
Shad Ansariedef2132018-08-10 22:14:50 +0000237 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400238
Shad Ansariedef2132018-08-10 22:14:50 +0000239 //Uplink ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500240 for (int i = 0; i < NumNniIf_(); i++) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000241 bcmolt_intf_ref intf_ref;
242 intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_NNI;
243 intf_ref.intf_id = i;
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400244
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000245 openolt::PortStatistics* port_stats =
246 collectPortStatistics(intf_ref);
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400247
Shad Ansariedef2132018-08-10 22:14:50 +0000248 openolt::Indication ind;
249 ind.set_allocated_port_stats(port_stats);
250 oltIndQ.push(ind);
251 }
252 //Pon ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500253 for (int i = 0; i < NumPonIf_(); i++) {
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000254 bcmolt_intf_ref intf_ref;
255 intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_PON;
256 intf_ref.intf_id = i;
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400257
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800258 openolt::PortStatistics* port_stats =
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000259 collectPortStatistics(intf_ref);
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400260
Shad Ansariedef2132018-08-10 22:14:50 +0000261 openolt::Indication ind;
262 ind.set_allocated_port_stats(port_stats);
263 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400264 }
265
Shad Ansariedef2132018-08-10 22:14:50 +0000266 //Flows statistics
267 // flow_inst *current_entry = NULL;
268 //
269 // TAILQ_FOREACH(current_entry,
270 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
271 // flow_inst_next) {
272 // int flows_measurements = 0;
273 //
274 // for (int i = 0; i < FLOWS_COUNT; i++) {
275 //
276 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
277 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
278 //
279 // if (flows_keys[i].flow_id != 0) {
280 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
281 // if (flow_stats->rx_packets() == -1) {
282 // //It Failed
283 // flows_keys[i].flow_id = 0;
284 // } else {
285 // flow_stats->set_flow_id(flows_keys[i].flow_id);
286 // time(&now);
287 // flow_stats->set_timestamp((int)now);
288 // openolt::Indication ind;
289 // ind.set_allocated_flow_stats(flow_stats);
290 // oltIndQ.push(ind);
291 // flows_measurements ++;
292 // }
293 // }
294 //
295 // }
296 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400297
298}
299
300/* Storing flow keys, temporary */
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400301// void register_new_flow(bcmbal_flow_key key) {
302// for (int i = 0; i < FLOWS_COUNT; i++) {
303// if (flows_keys[i].flow_id == 0) {
304// flows_keys[i] = key;
305// break;
306// }
307// }
308// }