Newer
Older
Shubham Sharma
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-License-Identifier: GPL-2.0 */
/*
* qosmngr.c - main qosmngr's code
*
* Copyright (C) 2020 iopsys Software Solutions AB. All rights reserved.
*
* Author: Oskar Viljasaar <oskar.viljasaar@iopsys.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <libubus.h>
#include <uci.h>
/* Needed to get the IFNAMSIZ define */
#include <net/if.h>
#include "qosmngr.h"
/* Used as an internal value to query all the queues */
#define QOS_QUEUE_ANY -1
/* Used for getting interface indexes for global q_stat array */
#define MIN_INDEX 48
#define MAX_INDEX 57
Shubham Sharma
committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
static int init_flag = 1;
static struct qos_stats **q_stat = {0};
/**
* get_no_queues function for getting number of queues on device by calling uci qos config
* @param ifname input parameter pointer to char for which number of queues needed
* retrun integer value 0 on success and -1 on failure
*/
static int get_no_queues(const char *ifname)
{
int queues = 0;
struct uci_package *uci_pkg = NULL;
struct uci_element *uci_elmnt = NULL;
struct uci_context *uci_ctx = uci_alloc_context();
uci_load(uci_ctx, "qos", &uci_pkg);
if (!uci_pkg) {
syslog(LOG_ERR, "Failed to load configuration\n");
queues = -1;
goto done;
}
uci_foreach_element(&uci_pkg->sections, uci_elmnt) {
struct uci_section *uci_sec = uci_to_section(uci_elmnt);
if (uci_sec && !strcmp(uci_sec->type, "queue")) {
struct uci_element *e = NULL;
uci_foreach_element(&uci_sec->options, e) {
struct uci_option *uci_opn = uci_to_option(e);
if (uci_opn && !strcmp(uci_opn->v.string, ifname))
queues++;
}
}
}
uci_unload(uci_ctx, uci_pkg);
done:
uci_free_context(uci_ctx);
return queues;
}
/**
* get_interface_index function for getting interface index i.e., eth0 having index 0
* @param ifname input parameter pointer to char for which index needs to be fetched
* retrun integer value 0 on success and -1 on failure
*/
static int get_interface_index(const char *ifname)
{
int i;
for (i = 0; i < strlen(ifname); i++) {
if ((ifname[i] >= MIN_INDEX) && (ifname[i] <= MAX_INDEX)) {
return atoi(ifname + i);
}
}
return -1;
}
/**
* get_no_ports function for getting number of ports on device by calling uci port config
* @param none
* retrun integer value 0 on success and -1 on failure
*/
static int get_no_ports()
{
int ports = 0;
struct uci_package *uci_pkg = NULL;
struct uci_element *uci_elmnt = NULL;
struct uci_context *uci_ctx = uci_alloc_context();
uci_load(uci_ctx, "ports", &uci_pkg);
if (!uci_pkg) {
syslog(LOG_ERR, "Failed to load configuration\n");
ports = -1;
goto done;
}
uci_foreach_element(&uci_pkg->sections, uci_elmnt) {
struct uci_section *uci_sec = uci_to_section(uci_elmnt);
if (uci_sec)
ports++;
}
done:
uci_unload(uci_ctx, uci_pkg);
uci_free_context(uci_ctx);
return ports;
}
/**
* init_qstat function for initializing global q_stat structure for sustainable stats
* @param none
* retrun integer value 0 on success and -1 on failure
*/
static int init_qstat()
{
int ret = 0;
int index = 0;
syslog(LOG_ERR, "Initializing global stat structure.\n");
int ports = get_no_ports();
q_stat = (struct qos_stats **)calloc(ports, sizeof(struct qos_stats *));
if (q_stat == NULL) {
syslog(LOG_ERR, "Initialization failed during memory allocation.\n");
ret = -1;
return ret;
}
struct uci_package *uci_pkg = NULL;
struct uci_element *uci_elmnt = NULL;
struct uci_context *uci_ctx = uci_alloc_context();
uci_load(uci_ctx, "ports", &uci_pkg);
if (!uci_pkg) {
syslog(LOG_ERR, "Failed to load configuration\n");
ret = -1;
goto done;
}
uci_foreach_element(&uci_pkg->sections, uci_elmnt) {
struct uci_section *uci_sec = uci_to_section(uci_elmnt);
if (uci_sec) {
struct uci_option *uci_opn = uci_lookup_option(uci_ctx, uci_sec, "ifname");
index = get_interface_index(uci_opn->v.string);
int queues = get_no_queues(uci_opn->v.string);
q_stat[index] = (struct qos_stats *)calloc(queues, sizeof(struct qos_stats));
if (q_stat[index] == NULL) {
syslog(LOG_ERR, "Initialization failed during memory allocation.\n");
ret = -1;
uci_unload(uci_ctx, uci_pkg);
goto done;
}
}
}
uci_unload(uci_ctx, uci_pkg);
done:
uci_free_context(uci_ctx);
return ret;
}
/**
* prapare_stats_blob function for getting stats by calling libqos function prepare_stats_blob
* @param b output parameter pointer to blob_buf
* @param stats output parameter pointer to qos_stats actually queue stats container
* @param dd output parameter pointer to void used for blob buffer array elements
* @param ifname input parameter pointer to char for which to get stats
* @param qid input parameter integer queue identifier for which to get stats
* retrun integer value 0 on success and -1 on failure
*/
static int prepare_stats_blob(struct blob_buf *b, struct qos_stats *stats, void *dd,
char *ifname, int qid)
{
int index;
int ret = 0;
// Initialize global q_stat global struct
if (init_flag) {
ret = init_qstat();
if (ret) {
syslog(LOG_ERR, "Failed to initialize gobal q_stat.\n");
}
syslog(LOG_ERR, "Initialized gobal q_stat successfully.\n");
init_flag = 0;
}
ret = qos_get_stats(ifname, qid, stats);
if (ret != 0) {
syslog(LOG_ERR, "blob_get_status: ret %d\n", ret);
return ret;
}
index = get_interface_index(ifname);
q_stat[index][qid].tx_packets += stats->tx_packets;
q_stat[index][qid].tx_bytes += stats->tx_bytes;
q_stat[index][qid].tx_dropped_packets += stats->tx_dropped_packets;
q_stat[index][qid].tx_dropped_bytes += stats->tx_dropped_bytes;
dd = blobmsg_open_table(b, "");
blobmsg_add_string(b, "iface", ifname);
blobmsg_add_u32(b, "qid", qid);
blobmsg_add_u32(b, "tx_packets", q_stat[index][qid].tx_packets);
blobmsg_add_u32(b, "tx_bytes", q_stat[index][qid].tx_bytes);
blobmsg_add_u32(b, "tx_dropped_packets", q_stat[index][qid].tx_dropped_packets);
blobmsg_add_u32(b, "tx_dropped_bytes", q_stat[index][qid].tx_dropped_bytes);
blobmsg_close_table(b, dd);
return ret;
}
/**
* get_stats_by_ifname function for getting specific interface stats
* @param b output parameter pointer to blob_buf
* @param stats output parameter pointer to qos_stats actually queue stats container
* @param dd output parameter pointer to void used for blob buffer array elements
* @param ifname input parameter pointer to char for which to get stats
* @param qid input parameter integer queue identifier for which to get stats
* retrun integer value 0 on success and -1 on failure
*/
static int get_stats_by_ifname(struct blob_buf *b, struct qos_stats *stats, void *dd,
char *ifname, int qid)
{
int queues = 0;
int i, ret = 0;
if (qid >= 0) {
ret = prepare_stats_blob(b, stats, dd, ifname, qid);
} else {
queues = get_no_queues(ifname);
for (i = 0; i < queues; i++) {
ret = prepare_stats_blob(b, stats, dd, ifname, i);
if (ret != 0)
return ret;
}
}
return ret;
}
/**
* get_stats_for_all_intf function for getting all interface stats
* @param b output parameter pointer to blob_buf
* @param stats output parameter pointer to qos_stats actually queue stats container
* @param dd output parameter pointer to void used for blob buffer array elements
* retrun integer value 0 on success and -1 on failure
*/
static int get_stats_for_all_intf(struct blob_buf *b, struct qos_stats *stats, void *dd)
{
int ret = 0;
char ifname[IFNAMSIZ] = {0};
struct uci_package *uci_pkg = NULL;
struct uci_element *uci_elmnt = NULL;
struct uci_context *uci_ctx = uci_alloc_context();
uci_load(uci_ctx, "ports", &uci_pkg);
if (!uci_pkg) {
syslog(LOG_ERR, "Failed to load configuration\n");
ret = -1;
return ret;
}
uci_foreach_element(&uci_pkg->sections, uci_elmnt) {
struct uci_section *uci_sec = uci_to_section(uci_elmnt);
if (uci_sec) {
struct uci_option *uci_opn = uci_lookup_option(uci_ctx, uci_sec, "ifname");
strncpy(ifname, uci_opn->v.string, sizeof(ifname) - 1);
Shubham Sharma
committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
ret = get_stats_by_ifname(b, stats, dd, ifname, QOS_QUEUE_ANY);
if (ret != 0) {
syslog(LOG_ERR, "get_stats_by_ifname : ret %d\n", ret);
return ret;
}
}
}
return ret;
}
/**
* qosmngr_get_stats function callback on ubus method queue_stats
* @param ctx input parameter pointer to ubus context
* @param obj input parameter pointer to ubus object in out case qos
* @param req input parameter pointer to ubus requested data
* @param method input parameter pointer to char method i.e., queue_stats
* @param msg input parameter pointer containing qid and ifname
* retrun integer value 0 on success and -1 on failure
*/
int qosmngr_get_stats(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
int ret;
int qid = QOS_QUEUE_ANY;
char ifname[IFNAMSIZ] = {0};
struct blob_attr *tb[NUM_QOS_POLICY];
struct blob_buf b = {0};
struct qos_stats stats = {0};
/* These are for the blobbuf array elements */
void *d = NULL, *dd = NULL;
blobmsg_parse(get_status_policy, QOS_POLICY_MAX, tb, blob_data(msg),
blob_len(msg));
if (tb[QOS_POLICY_IFNAME])
strncpy(ifname, blobmsg_data(tb[QOS_POLICY_IFNAME]), sizeof(ifname)-1);
/* Parse optional arguments */
if (tb[QOS_POLICY_QID])
qid = blobmsg_get_u32(tb[QOS_POLICY_QID]);
/* Can't have a queue id specified without an interface */
if (tb[QOS_POLICY_QID] && !tb[QOS_POLICY_IFNAME])
return UBUS_STATUS_INVALID_ARGUMENT;
blob_buf_init(&b, 0);
d = blobmsg_open_array(&b, "queues");
if (tb[QOS_POLICY_IFNAME]) {
ret = get_stats_by_ifname(&b, &stats, &dd, ifname, qid);
if (ret != 0) {
syslog(LOG_ERR, "get_stats_by_ifname : ret %d\n", ret);
goto fail_get_status;
}
} else {
ret = get_stats_for_all_intf(&b, &stats, &dd);
if (ret != 0) {
syslog(LOG_ERR, "get_stats_for_all_intf : ret %d\n", ret);
goto fail_get_status;
}
}
blobmsg_close_array(&b, d);
ubus_send_reply(ctx, req, b.head);
fail_get_status:
blob_buf_free(&b);
return ret;
}