summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/tcp_ao/lib/proc.c
blob: 2fb6dd8adba6945d0000c19fe90da1002d8c0dd4 (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
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
// SPDX-License-Identifier: GPL-2.0
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include "../../../../../include/linux/compiler.h"
#include "../../../../../include/linux/kernel.h"
#include "aolib.h"

struct netstat_counter {
	uint64_t val;
	char *name;
};

struct netstat {
	char *header_name;
	struct netstat *next;
	size_t counters_nr;
	struct netstat_counter *counters;
};

static struct netstat *lookup_type(struct netstat *ns,
		const char *type, size_t len)
{
	while (ns != NULL) {
		size_t cmp = max(len, strlen(ns->header_name));

		if (!strncmp(ns->header_name, type, cmp))
			return ns;
		ns = ns->next;
	}
	return NULL;
}

static struct netstat *lookup_get(struct netstat *ns,
				  const char *type, const size_t len)
{
	struct netstat *ret;

	ret = lookup_type(ns, type, len);
	if (ret != NULL)
		return ret;

	ret = malloc(sizeof(struct netstat));
	if (!ret)
		test_error("malloc()");

	ret->header_name = strndup(type, len);
	if (ret->header_name == NULL)
		test_error("strndup()");
	ret->next = ns;
	ret->counters_nr = 0;
	ret->counters = NULL;

	return ret;
}

static struct netstat *lookup_get_column(struct netstat *ns, const char *line)
{
	char *column;

	column = strchr(line, ':');
	if (!column)
		test_error("can't parse netstat file");

	return lookup_get(ns, line, column - line);
}

static void netstat_read_type(FILE *fnetstat, struct netstat **dest, char *line)
{
	struct netstat *type = lookup_get_column(*dest, line);
	const char *pos = line;
	size_t i, nr_elems = 0;
	char tmp;

	while ((pos = strchr(pos, ' '))) {
		nr_elems++;
		pos++;
	}

	*dest = type;
	type->counters = reallocarray(type->counters,
				type->counters_nr + nr_elems,
				sizeof(struct netstat_counter));
	if (!type->counters)
		test_error("reallocarray()");

	pos = strchr(line, ' ') + 1;

	if (fscanf(fnetstat, type->header_name) == EOF)
		test_error("fscanf(%s)", type->header_name);
	if (fread(&tmp, 1, 1, fnetstat) != 1 || tmp != ':')
		test_error("Unexpected netstat format (%c)", tmp);

	for (i = type->counters_nr; i < type->counters_nr + nr_elems; i++) {
		struct netstat_counter *nc = &type->counters[i];
		const char *new_pos = strchr(pos, ' ');
		const char *fmt = " %" PRIu64;

		if (new_pos == NULL)
			new_pos = strchr(pos, '\n');

		nc->name = strndup(pos, new_pos - pos);
		if (nc->name == NULL)
			test_error("strndup()");

		if (unlikely(!strcmp(nc->name, "MaxConn")))
			fmt = " %" PRId64; /* MaxConn is signed, RFC 2012 */
		if (fscanf(fnetstat, fmt, &nc->val) != 1)
			test_error("fscanf(%s)", nc->name);
		pos = new_pos + 1;
	}
	type->counters_nr += nr_elems;

	if (fread(&tmp, 1, 1, fnetstat) != 1 || tmp != '\n')
		test_error("Unexpected netstat format");
}

static const char *snmp6_name = "Snmp6";
static void snmp6_read(FILE *fnetstat, struct netstat **dest)
{
	struct netstat *type = lookup_get(*dest, snmp6_name, strlen(snmp6_name));
	char *counter_name;
	size_t i;

	for (i = type->counters_nr;; i++) {
		struct netstat_counter *nc;
		uint64_t counter;

		if (fscanf(fnetstat, "%ms", &counter_name) == EOF)
			break;
		if (fscanf(fnetstat, "%" PRIu64, &counter) == EOF)
			test_error("Unexpected snmp6 format");
		type->counters = reallocarray(type->counters, i + 1,
					sizeof(struct netstat_counter));
		if (!type->counters)
			test_error("reallocarray()");
		nc = &type->counters[i];
		nc->name = counter_name;
		nc->val = counter;
	}
	type->counters_nr = i;
	*dest = type;
}

struct netstat *netstat_read(void)
{
	struct netstat *ret = 0;
	size_t line_sz = 0;
	char *line = NULL;
	FILE *fnetstat;

	/*
	 * Opening thread-self instead of /proc/net/... as the latter
	 * points to /proc/self/net/ which instantiates thread-leader's
	 * net-ns, see:
	 * commit 155134fef2b6 ("Revert "proc: Point /proc/{mounts,net} at..")
	 */
	errno = 0;
	fnetstat = fopen("/proc/thread-self/net/netstat", "r");
	if (fnetstat == NULL)
		test_error("failed to open /proc/net/netstat");

	while (getline(&line, &line_sz, fnetstat) != -1)
		netstat_read_type(fnetstat, &ret, line);
	fclose(fnetstat);

	errno = 0;
	fnetstat = fopen("/proc/thread-self/net/snmp", "r");
	if (fnetstat == NULL)
		test_error("failed to open /proc/net/snmp");

	while (getline(&line, &line_sz, fnetstat) != -1)
		netstat_read_type(fnetstat, &ret, line);
	fclose(fnetstat);

	errno = 0;
	fnetstat = fopen("/proc/thread-self/net/snmp6", "r");
	if (fnetstat == NULL)
		test_error("failed to open /proc/net/snmp6");

	snmp6_read(fnetstat, &ret);
	fclose(fnetstat);

	free(line);
	return ret;
}

void netstat_free(struct netstat *ns)
{
	while (ns != NULL) {
		struct netstat *prev = ns;
		size_t i;

		free(ns->header_name);
		for (i = 0; i < ns->counters_nr; i++)
			free(ns->counters[i].name);
		free(ns->counters);
		ns = ns->next;
		free(prev);
	}
}

static inline void
__netstat_print_diff(uint64_t a, struct netstat *nsb, size_t i)
{
	if (unlikely(!strcmp(nsb->header_name, "MaxConn"))) {
		test_print("%8s %25s: %" PRId64 " => %" PRId64,
				nsb->header_name, nsb->counters[i].name,
				a, nsb->counters[i].val);
		return;
	}

	test_print("%8s %25s: %" PRIu64 " => %" PRIu64, nsb->header_name,
			nsb->counters[i].name, a, nsb->counters[i].val);
}

void netstat_print_diff(struct netstat *nsa, struct netstat *nsb)
{
	size_t i, j;

	while (nsb != NULL) {
		if (unlikely(strcmp(nsb->header_name, nsa->header_name))) {
			for (i = 0; i < nsb->counters_nr; i++)
				__netstat_print_diff(0, nsb, i);
			nsb = nsb->next;
			continue;
		}

		if (nsb->counters_nr < nsa->counters_nr)
			test_error("Unexpected: some counters disappeared!");

		for (j = 0, i = 0; i < nsb->counters_nr; i++) {
			if (strcmp(nsb->counters[i].name, nsa->counters[j].name)) {
				__netstat_print_diff(0, nsb, i);
				continue;
			}

			if (nsa->counters[j].val == nsb->counters[i].val) {
				j++;
				continue;
			}

			__netstat_print_diff(nsa->counters[j].val, nsb, i);
			j++;
		}
		if (j != nsa->counters_nr)
			test_error("Unexpected: some counters disappeared!");

		nsb = nsb->next;
		nsa = nsa->next;
	}
}

uint64_t netstat_get(struct netstat *ns, const char *name, bool *not_found)
{
	if (not_found)
		*not_found = false;

	while (ns != NULL) {
		size_t i;

		for (i = 0; i < ns->counters_nr; i++) {
			if (!strcmp(name, ns->counters[i].name))
				return ns->counters[i].val;
		}

		ns = ns->next;
	}

	if (not_found)
		*not_found = true;
	return 0;
}