blob: 7bdde4cfabcef5773c89f22377d49e68b826a521 [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
22#include <openolt.grpc.pb.h>
23#include "indications.h"
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -040024#include "core.h"
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -040025#include "translation.h"
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040026
27extern "C"
28{
29#include <bcmos_system.h>
30#include <bal_api.h>
31#include <bal_api_end.h>
32#include <flow_fsm.h>
33}
34
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040035//FIXME
36#define FLOWS_COUNT 100
37
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040038bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040039
Shad Ansariedef2132018-08-10 22:14:50 +000040void init_stats() {
41 memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040042}
43
44openolt::PortStatistics* get_default_port_statistics() {
45 openolt::PortStatistics* port_stats = new openolt::PortStatistics;
46 port_stats->set_intf_id(-1);
47 port_stats->set_rx_bytes(-1);
48 port_stats->set_rx_packets(-1);
49 port_stats->set_rx_ucast_packets(-1);
50 port_stats->set_rx_mcast_packets(-1);
51 port_stats->set_rx_bcast_packets(-1);
52 port_stats->set_rx_error_packets(-1);
53 port_stats->set_tx_bytes(-1);
54 port_stats->set_tx_packets(-1);
55 port_stats->set_tx_ucast_packets(-1);
56 port_stats->set_tx_mcast_packets(-1);
57 port_stats->set_tx_bcast_packets(-1);
58 port_stats->set_tx_error_packets(-1);
59 port_stats->set_rx_crc_errors(-1);
60 port_stats->set_bip_errors(-1);
61
62 return port_stats;
63}
64
Shad Ansaricb004c52018-05-30 18:07:23 +000065#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040066openolt::FlowStatistics* get_default_flow_statistics() {
67 openolt::FlowStatistics* flow_stats = new openolt::FlowStatistics;
68 flow_stats->set_flow_id(-1);
69 flow_stats->set_rx_bytes(-1);
70 flow_stats->set_rx_packets(-1);
71 flow_stats->set_tx_bytes(-1);
72 flow_stats->set_tx_packets(-1);
73
74 return flow_stats;
75}
Shad Ansaricb004c52018-05-30 18:07:23 +000076#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040077
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -040078openolt::PortStatistics* collectPortStatistics(bcmbal_interface_key key) {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040079
80 bcmos_errno err;
81 bcmbal_interface_stat stat; /**< declare main API struct */
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040082 bcmos_bool clear_on_read = false;
83
84 openolt::PortStatistics* port_stats = get_default_port_statistics();
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040085
86 /* init the API struct */
87 BCMBAL_STAT_INIT(&stat, interface, key);
88 BCMBAL_STAT_PROP_GET(&stat, interface, all_properties);
89
90 /* call API */
91 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
92 if (err == BCM_ERR_OK)
93 {
Shad Ansari563ea822018-06-28 14:56:27 +000094 //std::cout << "Interface statistics retrieved"
95 // << " intf_id:" << intf_id << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040096
97 port_stats->set_rx_bytes(stat.data.rx_bytes);
98 port_stats->set_rx_packets(stat.data.rx_packets);
99 port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets);
100 port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets);
101 port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets);
102 port_stats->set_rx_error_packets(stat.data.rx_error_packets);
103 port_stats->set_tx_bytes(stat.data.tx_bytes);
104 port_stats->set_tx_packets(stat.data.tx_packets);
105 port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets);
106 port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets);
107 port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets);
108 port_stats->set_tx_error_packets(stat.data.tx_error_packets);
109 port_stats->set_rx_crc_errors(stat.data.rx_crc_errors);
110 port_stats->set_bip_errors(stat.data.bip_errors);
111
112 } else {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400113 BCM_LOG(ERROR, openolt_log_id, "Failed to retrieve port statistics, intf_id %d, intf_type %d\n",
114 (int)key.intf_id, (int)key.intf_type);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400115 }
116
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400117 port_stats->set_intf_id(interface_key_to_port_no(key));
118 time_t now;
119 time(&now);
120 port_stats->set_timestamp((int)now);
121
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400122 return port_stats;
123
124}
125
Shad Ansaricb004c52018-05-30 18:07:23 +0000126#if 0
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400127openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type) {
128
129 bcmos_errno err;
130 bcmbal_flow_stat stat; /**< declare main API struct */
131 bcmbal_flow_key key = { }; /**< declare key */
132 bcmos_bool clear_on_read = false;
133
134 openolt::FlowStatistics* flow_stats = get_default_flow_statistics();
135 //Key
136 key.flow_id = flow_id;
137 key.flow_type = flow_type;
138
139 /* init the API struct */
140 BCMBAL_STAT_INIT(&stat, flow, key);
141 BCMBAL_STAT_PROP_GET(&stat, flow, all_properties);
142
143 err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
144
145 if (err == BCM_ERR_OK)
146 {
147 std::cout << "Flow statistics retrieved"
148 << " flow_id:" << flow_id
149 << " flow_type:" << flow_type << std::endl;
150
151 flow_stats->set_rx_bytes(stat.data.rx_bytes);
152 flow_stats->set_rx_packets(stat.data.rx_packets);
153 flow_stats->set_tx_bytes(stat.data.tx_bytes);
154 flow_stats->set_tx_packets(stat.data.tx_packets);
155
156 } else {
157 std::cout << "ERROR: Failed to retrieve flow statistics"
158 << " flow_id:" << flow_id
159 << " flow_type:" << flow_type << std::endl;
160 }
161
162 return flow_stats;
163}
Shad Ansaricb004c52018-05-30 18:07:23 +0000164#endif
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400165
166
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400167void stats_collection() {
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400168
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400169 if (!state.is_connected()) {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400170 BCM_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400171 return;
172 }
173 if (!state.is_activated()) {
Nicolas Palpacuer967438f2018-09-07 14:41:54 -0400174 BCM_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
Nicolas Palpacuerc09fdb72018-08-22 10:23:22 -0400175 return;
176 }
177
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400178
Craig Lutgend0bae9b2018-10-18 18:02:07 -0500179 BCM_LOG(DEBUG, openolt_log_id, "Collecting statistics\n");
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400180
Shad Ansariedef2132018-08-10 22:14:50 +0000181 //Ports statistics
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400182
Shad Ansariedef2132018-08-10 22:14:50 +0000183 //Uplink ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500184 for (int i = 0; i < NumNniIf_(); i++) {
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400185 bcmbal_interface_key nni_interface;
186 nni_interface.intf_type = BCMBAL_INTF_TYPE_NNI;
187 nni_interface.intf_id = i;
188
189 openolt::PortStatistics* port_stats = collectPortStatistics(nni_interface);
190
Shad Ansariedef2132018-08-10 22:14:50 +0000191 openolt::Indication ind;
192 ind.set_allocated_port_stats(port_stats);
193 oltIndQ.push(ind);
194 }
195 //Pon ports
Craig Lutgen88a22ad2018-10-04 12:30:46 -0500196 for (int i = 0; i < NumPonIf_(); i++) {
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400197 bcmbal_interface_key pon_interface;
198 pon_interface.intf_type = BCMBAL_INTF_TYPE_PON;
199 pon_interface.intf_id = i;
200
201 openolt::PortStatistics* port_stats = collectPortStatistics(pon_interface);
202
Shad Ansariedef2132018-08-10 22:14:50 +0000203 openolt::Indication ind;
204 ind.set_allocated_port_stats(port_stats);
205 oltIndQ.push(ind);
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400206 }
207
Shad Ansariedef2132018-08-10 22:14:50 +0000208 //Flows statistics
209 // flow_inst *current_entry = NULL;
210 //
211 // TAILQ_FOREACH(current_entry,
212 // &FLOW_FSM_FLOW_LIST_CTX_PTR->active_flow_list,
213 // flow_inst_next) {
214 // int flows_measurements = 0;
215 //
216 // for (int i = 0; i < FLOWS_COUNT; i++) {
217 //
218 // // bcmbal_flow_id flow_id = current_entry->api_req_flow_info.key.flow_id;
219 // // bcmbal_flow_type flow_type = current_entry->api_req_flow_info.key.flow_type;
220 //
221 // if (flows_keys[i].flow_id != 0) {
222 // openolt::FlowStatistics* flow_stats = collectFlowStatistics(flows_keys[i].flow_id, flows_keys[i].flow_type);
223 // if (flow_stats->rx_packets() == -1) {
224 // //It Failed
225 // flows_keys[i].flow_id = 0;
226 // } else {
227 // flow_stats->set_flow_id(flows_keys[i].flow_id);
228 // time(&now);
229 // flow_stats->set_timestamp((int)now);
230 // openolt::Indication ind;
231 // ind.set_allocated_flow_stats(flow_stats);
232 // oltIndQ.push(ind);
233 // flows_measurements ++;
234 // }
235 // }
236 //
237 // }
238 // std::cout << "Stats of " << flows_measurements << " flows retrieved" << std::endl;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400239
240}
241
242/* Storing flow keys, temporary */
Nicolas Palpacuer6a63ea92018-09-05 17:21:37 -0400243// void register_new_flow(bcmbal_flow_key key) {
244// for (int i = 0; i < FLOWS_COUNT; i++) {
245// if (flows_keys[i].flow_id == 0) {
246// flows_keys[i] = key;
247// break;
248// }
249// }
250// }