blob: 62213c2b79807c26abb949c6bdf839f008b5cf14 [file] [log] [blame]
Zack Williams477ba092018-10-17 10:50:06 -07001/*
2 Copyright (C) 2018 Open Networking Foundation
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040018#include "stats_collection.h"
19
20#include <unistd.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040021
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040022#include "indications.h"
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -040023#include "core.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>
Jason Huangd33b4d82019-05-15 18:22:57 +080029#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
Jason Huangd33b4d82019-05-15 18:22:57 +080036bcmolt_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() {
Jason Huangd33b4d82019-05-15 18:22:57 +080040 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
Jason Huangd33b4d82019-05-15 18:22:57 +080077openolt::PortStatistics* collectPortStatistics(bcmolt_intf_ref intf_ref) {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040078
Girish Gowdrae538dfd2019-09-30 11:07:30 +053079 openolt::PortStatistics* port_stats = get_default_port_statistics();
80#ifndef TEST_MODE
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040081 bcmos_errno err;
Jason Huangd33b4d82019-05-15 18:22:57 +080082 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
Jason Huangd33b4d82019-05-15 18:22:57 +080088 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
Jason Huangd33b4d82019-05-15 18:22:57 +0800108 /* 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 {
Jason Huangb6843dc2019-07-22 17:46:06 +0800129 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d\n",
Jason Huangd33b4d82019-05-15 18:22:57 +0800130 (int)intf_ref.intf_id, (int)intf_ref.intf_type);
131 }
132 break;
133 }
134 case BCMOLT_INTERFACE_TYPE_PON:
135 {
Jason Huangb6843dc2019-07-22 17:46:06 +0800136 bcmolt_pon_interface_key key;
Jason Huangd33b4d82019-05-15 18:22:57 +0800137 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
Jason Huangd33b4d82019-05-15 18:22:57 +0800143 /* 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 {
Jason Huangb6843dc2019-07-22 17:46:06 +0800150 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err %d\n",
151 (int)intf_ref.intf_id, (int)intf_ref.intf_type, err);
Jason Huangd33b4d82019-05-15 18:22:57 +0800152 }
153 {
154 bcmolt_onu_key key;
155 key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
Jason Huangb1fad572019-05-28 19:02:30 +0800156 BCMOLT_STAT_INIT(&pon_stats, onu, itu_pon_stats, key);
Jason Huangd33b4d82019-05-15 18:22:57 +0800157 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
Jason Huangd33b4d82019-05-15 18:22:57 +0800161 /* 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 {
Jason Huangb6843dc2019-07-22 17:46:06 +0800168 OPENOLT_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d, err %d\n",
169 (int)intf_ref.intf_id, (int)intf_ref.intf_type, err);
Jason Huangd33b4d82019-05-15 18:22:57 +0800170 }
171 }
172 break;
173 }
174 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400175
Jason Huangd33b4d82019-05-15 18:22:57 +0800176 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);
Girish Gowdrae538dfd2019-09-30 11:07:30 +0530180#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()) {
Jason Huangb6843dc2019-07-22 17:46:06 +0800229 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()) {
Jason Huangb6843dc2019-07-22 17:46:06 +0800233 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
Jason Huangb6843dc2019-07-22 17:46:06 +0800238 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++) {
Jason Huangd33b4d82019-05-15 18:22:57 +0800244 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
Jason Huangd33b4d82019-05-15 18:22:57 +0800248 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++) {
Jason Huangd33b4d82019-05-15 18:22:57 +0800257 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
Jason Huangd33b4d82019-05-15 18:22:57 +0800261 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// }