summaryrefslogtreecommitdiffstats
path: root/tc/tc_qevent.c
blob: 34568070b5cad6a6f3fe220dee394605f6300951 (plain)
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
39
40
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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause

/*
 * Helpers for handling qevents.
 */

#include <stdio.h>
#include <string.h>

#include "tc_qevent.h"
#include "utils.h"

void qevents_init(struct qevent_util *qevents)
{
	if (!qevents)
		return;

	for (; qevents->id; qevents++)
		memset(qevents->data, 0, qevents->data_size);
}

int qevent_parse(struct qevent_util *qevents, int *p_argc, char ***p_argv)
{
	char **argv = *p_argv;
	int argc = *p_argc;
	const char *name = *argv;
	int err;

	if (!qevents)
		goto out;

	for (; qevents->id; qevents++) {
		if (strcmp(name, qevents->id) == 0) {
			NEXT_ARG();
			err = qevents->parse_qevent(qevents, &argc, &argv);
			if (err)
				return err;

			*p_argc = argc;
			*p_argv = argv;
			return 0;
		}
	}

out:
	fprintf(stderr, "Unknown qevent `%s'\n", name);
	return -1;
}

int qevents_read(struct qevent_util *qevents, struct rtattr **tb)
{
	int err;

	if (!qevents)
		return 0;

	for (; qevents->id; qevents++) {
		if (tb[qevents->attr]) {
			err = qevents->read_qevent(qevents, tb);
			if (err)
				return err;
		}
	}

	return 0;
}

void qevents_print(struct qevent_util *qevents, FILE *f)
{
	int first = true;

	if (!qevents)
		return;

	for (; qevents->id; qevents++) {
		struct qevent_base *qeb = qevents->data;

		if (qeb->block_idx) {
			if (first) {
				open_json_array(PRINT_JSON, "qevents");
				first = false;
			}

			open_json_object(NULL);
			print_string(PRINT_ANY, "kind", "qevent %s", qevents->id);
			qevents->print_qevent(qevents, f);
			print_string(PRINT_FP, NULL, "%s", " ");
			close_json_object();
		}
	}

	if (!first)
		close_json_array(PRINT_ANY, "");
}

bool qevents_have_block(struct qevent_util *qevents, __u32 block_idx)
{
	if (!qevents)
		return false;

	for (; qevents->id; qevents++) {
		struct qevent_base *qeb = qevents->data;

		if (qeb->block_idx == block_idx)
			return true;
	}

	return false;
}

int qevents_dump(struct qevent_util *qevents, struct nlmsghdr *n)
{
	int err;

	if (!qevents)
		return 0;

	for (; qevents->id; qevents++) {
		struct qevent_base *qeb = qevents->data;

		if (qeb->block_idx) {
			err = qevents->dump_qevent(qevents, n);
			if (err)
				return err;
		}
	}

	return 0;
}

static int parse_block_idx(const char *arg, struct qevent_base *qeb)
{
	if (qeb->block_idx) {
		fprintf(stderr, "Qevent block index already specified\n");
		return -1;
	}

	if (get_unsigned(&qeb->block_idx, arg, 10) || !qeb->block_idx) {
		fprintf(stderr, "Illegal qevent block index\n");
		return -1;
	}

	return 0;
}

static int read_block_idx(struct rtattr *attr, struct qevent_base *qeb)
{
	if (qeb->block_idx) {
		fprintf(stderr, "Qevent block index already specified\n");
		return -1;
	}

	qeb->block_idx = rta_getattr_u32(attr);
	if (!qeb->block_idx) {
		fprintf(stderr, "Illegal qevent block index\n");
		return -1;
	}

	return 0;
}

static void print_block_idx(FILE *f, __u32 block_idx)
{
	print_uint(PRINT_ANY, "block", " block %u", block_idx);
}

int qevent_parse_plain(struct qevent_util *qu, int *p_argc, char ***p_argv)
{
	struct qevent_plain *qe = qu->data;
	char **argv = *p_argv;
	int argc = *p_argc;

	if (qe->base.block_idx) {
		fprintf(stderr, "Duplicate qevent\n");
		return -1;
	}

	while (argc > 0) {
		if (strcmp(*argv, "block") == 0) {
			NEXT_ARG();
			if (parse_block_idx(*argv, &qe->base))
				return -1;
		} else {
			break;
		}
		NEXT_ARG_FWD();
	}

	if (!qe->base.block_idx) {
		fprintf(stderr, "Unspecified qevent block index\n");
		return -1;
	}

	*p_argc = argc;
	*p_argv = argv;
	return 0;
}

int qevent_read_plain(struct qevent_util *qu, struct rtattr **tb)
{
	struct qevent_plain *qe = qu->data;

	return read_block_idx(tb[qu->attr], &qe->base);
}

void qevent_print_plain(struct qevent_util *qu, FILE *f)
{
	struct qevent_plain *qe = qu->data;

	print_block_idx(f, qe->base.block_idx);
}

int qevent_dump_plain(struct qevent_util *qu, struct nlmsghdr *n)
{
	struct qevent_plain *qe = qu->data;

	return addattr32(n, 1024, qu->attr, qe->base.block_idx);
}