summaryrefslogtreecommitdiff
path: root/tools/net/ynl/tests/tc.c
blob: 6ff13876578d4b156d3bc92d3dd64602651663e9 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <linux/pkt_sched.h>
#include <linux/tc_act/tc_vlan.h>
#include <linux/tc_act/tc_gact.h>
#include <linux/if_ether.h>
#include <net/if.h>

#include <ynl.h>

#include <kselftest_harness.h>

#include "tc-user.h"

#define TC_HANDLE (0xFFFF << 16)

static bool tc_qdisc_print(struct __test_metadata *_metadata,
			   struct tc_getqdisc_rsp *q)
{
	bool was_fq_codel = false;
	char ifname[IF_NAMESIZE];
	const char *name;

	name = if_indextoname(q->_hdr.tcm_ifindex, ifname);
	EXPECT_TRUE((bool)name);
	ksft_print_msg("%16s: ", name ?: "no-name");

	if (q->_len.kind) {
		printf("%s  ", q->kind);

		if (q->options._present.fq_codel) {
			struct tc_fq_codel_attrs *fq_codel;
			struct tc_fq_codel_xstats *stats;

			fq_codel = &q->options.fq_codel;
			stats = q->stats2.app.fq_codel;

			EXPECT_EQ(true,
				  fq_codel->_present.limit &&
				  fq_codel->_present.target &&
				  q->stats2.app._len.fq_codel);

			if (fq_codel->_present.limit)
				printf("limit: %dp ", fq_codel->limit);
			if (fq_codel->_present.target)
				printf("target: %dms ",
				       (fq_codel->target + 500) / 1000);
			if (q->stats2.app._len.fq_codel)
				printf("new_flow_cnt: %d ",
				       stats->qdisc_stats.new_flow_count);
			was_fq_codel = true;
		}
	}
	printf("\n");

	return was_fq_codel;
}

static const char *vlan_act_name(struct tc_vlan *p)
{
	switch (p->v_action) {
	case TCA_VLAN_ACT_POP:
		return "pop";
	case TCA_VLAN_ACT_PUSH:
		return "push";
	case TCA_VLAN_ACT_MODIFY:
		return "modify";
	default:
		break;
	}

	return "not supported";
}

static const char *gact_act_name(struct tc_gact *p)
{
	switch (p->action) {
	case TC_ACT_SHOT:
		return "drop";
	case TC_ACT_OK:
		return "ok";
	case TC_ACT_PIPE:
		return "pipe";
	default:
		break;
	}

	return "not supported";
}

static void print_vlan(struct tc_act_vlan_attrs *vlan)
{
	printf("%s ", vlan_act_name(vlan->parms));
	if (vlan->_present.push_vlan_id)
		printf("id %u ", vlan->push_vlan_id);
	if (vlan->_present.push_vlan_protocol)
		printf("protocol %#x ", ntohs(vlan->push_vlan_protocol));
	if (vlan->_present.push_vlan_priority)
		printf("priority %u ", vlan->push_vlan_priority);
}

static void print_gact(struct tc_act_gact_attrs *gact)
{
	struct tc_gact *p = gact->parms;

	printf("%s ", gact_act_name(p));
}

static void flower_print(struct tc_flower_attrs *flower, const char *kind)
{
	struct tc_act_attrs *a;
	unsigned int i;

	ksft_print_msg("%s:\n", kind);

	if (flower->_present.key_vlan_id)
		ksft_print_msg("  vlan_id: %u\n", flower->key_vlan_id);
	if (flower->_present.key_vlan_prio)
		ksft_print_msg("  vlan_prio: %u\n", flower->key_vlan_prio);
	if (flower->_present.key_num_of_vlans)
		ksft_print_msg("  num_of_vlans: %u\n",
			       flower->key_num_of_vlans);

	for (i = 0; i < flower->_count.act; i++) {
		a = &flower->act[i];
		ksft_print_msg("action order: %i %s ", i + 1, a->kind);
		if (a->options._present.vlan)
			print_vlan(&a->options.vlan);
		else if (a->options._present.gact)
			print_gact(&a->options.gact);
		printf("\n");
	}
}

static void tc_filter_print(struct __test_metadata *_metadata,
			     struct tc_gettfilter_rsp *f)
{
	struct tc_options_msg *opt = &f->options;

	if (opt->_present.flower) {
		EXPECT_TRUE((bool)f->_len.kind);
		flower_print(&opt->flower, f->kind);
	} else if (f->_len.kind) {
		ksft_print_msg("%s pref %u proto: %#x\n", f->kind,
			       (f->_hdr.tcm_info >> 16),
			       ntohs(TC_H_MIN(f->_hdr.tcm_info)));
	}
}

static int tc_clsact_add(struct ynl_sock *ys, int ifi)
{
	struct tc_newqdisc_req *req;
	int ret;

	req = tc_newqdisc_req_alloc();
	if (!req)
		return -1;
	memset(req, 0, sizeof(*req));

	req->_hdr.tcm_ifindex = ifi;
	req->_hdr.tcm_parent = TC_H_CLSACT;
	req->_hdr.tcm_handle = TC_HANDLE;
	tc_newqdisc_req_set_nlflags(req,
				    NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE);
	tc_newqdisc_req_set_kind(req, "clsact");

	ret = tc_newqdisc(ys, req);
	tc_newqdisc_req_free(req);

	return ret;
}

static int tc_clsact_del(struct ynl_sock *ys, int ifi)
{
	struct tc_delqdisc_req *req;
	int ret;

	req = tc_delqdisc_req_alloc();
	if (!req)
		return -1;
	memset(req, 0, sizeof(*req));

	req->_hdr.tcm_ifindex = ifi;
	req->_hdr.tcm_parent = TC_H_CLSACT;
	req->_hdr.tcm_handle = TC_HANDLE;
	tc_delqdisc_req_set_nlflags(req, NLM_F_REQUEST);

	ret = tc_delqdisc(ys, req);
	tc_delqdisc_req_free(req);

	return ret;
}

static int tc_filter_add(struct ynl_sock *ys, int ifi)
{
	struct tc_newtfilter_req *req;
	struct tc_act_attrs *acts;
	struct tc_vlan p = {
		.action = TC_ACT_PIPE,
		.v_action = TCA_VLAN_ACT_PUSH
	};
	int ret;

	req = tc_newtfilter_req_alloc();
	if (!req)
		return -1;
	memset(req, 0, sizeof(*req));

	acts = tc_act_attrs_alloc(3);
	if (!acts) {
		tc_newtfilter_req_free(req);
		return -1;
	}
	memset(acts, 0, sizeof(*acts) * 3);

	req->_hdr.tcm_ifindex = ifi;
	req->_hdr.tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
	req->_hdr.tcm_info = TC_H_MAKE(1 << 16, htons(ETH_P_8021Q));
	req->chain = 0;

	tc_newtfilter_req_set_nlflags(req, NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE);
	tc_newtfilter_req_set_kind(req, "flower");
	tc_newtfilter_req_set_options_flower_key_vlan_id(req, 100);
	tc_newtfilter_req_set_options_flower_key_vlan_prio(req, 5);
	tc_newtfilter_req_set_options_flower_key_num_of_vlans(req, 3);

	__tc_newtfilter_req_set_options_flower_act(req, acts, 3);

	/* Skip action at index 0 because in TC, the action array
	 * index starts at 1, with each index defining the action's
	 * order. In contrast, in YNL indexed arrays start at index 0.
	 */
	tc_act_attrs_set_kind(&acts[1], "vlan");
	tc_act_attrs_set_options_vlan_parms(&acts[1], &p, sizeof(p));
	tc_act_attrs_set_options_vlan_push_vlan_id(&acts[1], 200);
	tc_act_attrs_set_kind(&acts[2], "vlan");
	tc_act_attrs_set_options_vlan_parms(&acts[2], &p, sizeof(p));
	tc_act_attrs_set_options_vlan_push_vlan_id(&acts[2], 300);

	tc_newtfilter_req_set_options_flower_flags(req, 0);
	tc_newtfilter_req_set_options_flower_key_eth_type(req, htons(0x8100));

	ret = tc_newtfilter(ys, req);
	tc_newtfilter_req_free(req);

	return ret;
}

static int tc_filter_del(struct ynl_sock *ys, int ifi)
{
	struct tc_deltfilter_req *req;
	int ret;

	req = tc_deltfilter_req_alloc();
	if (!req)
		return -1;
	memset(req, 0, sizeof(*req));

	req->_hdr.tcm_ifindex = ifi;
	req->_hdr.tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
	req->_hdr.tcm_info = TC_H_MAKE(1 << 16, htons(ETH_P_8021Q));
	tc_deltfilter_req_set_nlflags(req, NLM_F_REQUEST);

	ret = tc_deltfilter(ys, req);
	tc_deltfilter_req_free(req);

	return ret;
}

FIXTURE(tc)
{
	struct ynl_sock *ys;
	int ifindex;
};

FIXTURE_SETUP(tc)
{
	struct ynl_error yerr;
	int ret;

	ret = unshare(CLONE_NEWNET);
	ASSERT_EQ(0, ret);

	self->ifindex = 1; /* loopback */

	self->ys = ynl_sock_create(&ynl_tc_family, &yerr);
	ASSERT_NE(NULL, self->ys) {
		TH_LOG("failed to create tc socket: %s", yerr.msg);
	}
}

FIXTURE_TEARDOWN(tc)
{
	ynl_sock_destroy(self->ys);
}

TEST_F(tc, qdisc)
{
	struct tc_getqdisc_req_dump *dreq;
	struct tc_newqdisc_req *add_req;
	struct tc_delqdisc_req *del_req;
	struct tc_getqdisc_list *rsp;
	bool found = false;
	int ret;

	add_req = tc_newqdisc_req_alloc();
	ASSERT_NE(NULL, add_req);
	memset(add_req, 0, sizeof(*add_req));

	add_req->_hdr.tcm_ifindex = self->ifindex;
	add_req->_hdr.tcm_parent = TC_H_ROOT;
	tc_newqdisc_req_set_nlflags(add_req,
				    NLM_F_REQUEST | NLM_F_CREATE);
	tc_newqdisc_req_set_kind(add_req, "fq_codel");

	ret = tc_newqdisc(self->ys, add_req);
	tc_newqdisc_req_free(add_req);
	ASSERT_EQ(0, ret) {
		TH_LOG("qdisc add failed: %s", self->ys->err.msg);
	}

	dreq = tc_getqdisc_req_dump_alloc();
	ASSERT_NE(NULL, dreq);
	rsp = tc_getqdisc_dump(self->ys, dreq);
	tc_getqdisc_req_dump_free(dreq);
	ASSERT_NE(NULL, rsp) {
		TH_LOG("dump failed: %s", self->ys->err.msg);
	}
	ASSERT_FALSE(ynl_dump_empty(rsp));

	ynl_dump_foreach(rsp, qdisc) {
		found |= tc_qdisc_print(_metadata, qdisc);
	}
	tc_getqdisc_list_free(rsp);
	EXPECT_TRUE(found);

	del_req = tc_delqdisc_req_alloc();
	ASSERT_NE(NULL, del_req);
	memset(del_req, 0, sizeof(*del_req));

	del_req->_hdr.tcm_ifindex = self->ifindex;
	del_req->_hdr.tcm_parent = TC_H_ROOT;
	tc_delqdisc_req_set_nlflags(del_req, NLM_F_REQUEST);

	ret = tc_delqdisc(self->ys, del_req);
	tc_delqdisc_req_free(del_req);
	EXPECT_EQ(0, ret) {
		TH_LOG("qdisc del failed: %s", self->ys->err.msg);
	}
}

TEST_F(tc, flower)
{
	struct tc_gettfilter_req_dump *dreq;
	struct tc_gettfilter_list *rsp;
	bool found = false;
	int ret;

	ret = tc_clsact_add(self->ys, self->ifindex);
	if (ret)
		SKIP(return, "clsact not supported: %s", self->ys->err.msg);

	ret = tc_filter_add(self->ys, self->ifindex);
	ASSERT_EQ(0, ret) {
		TH_LOG("filter add failed: %s", self->ys->err.msg);
	}

	dreq = tc_gettfilter_req_dump_alloc();
	ASSERT_NE(NULL, dreq);
	memset(dreq, 0, sizeof(*dreq));
	dreq->_hdr.tcm_ifindex = self->ifindex;
	dreq->_hdr.tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
	dreq->_present.chain = 1;
	dreq->chain = 0;

	rsp = tc_gettfilter_dump(self->ys, dreq);
	tc_gettfilter_req_dump_free(dreq);
	ASSERT_NE(NULL, rsp) {
		TH_LOG("filter dump failed: %s", self->ys->err.msg);
	}

	ynl_dump_foreach(rsp, flt) {
		tc_filter_print(_metadata, flt);
		if (flt->options._present.flower) {
			EXPECT_EQ(100, flt->options.flower.key_vlan_id);
			EXPECT_EQ(5, flt->options.flower.key_vlan_prio);
			found = true;
		}
	}
	tc_gettfilter_list_free(rsp);
	EXPECT_TRUE(found);

	ret = tc_filter_del(self->ys, self->ifindex);
	EXPECT_EQ(0, ret) {
		TH_LOG("filter del failed: %s", self->ys->err.msg);
	}

	ret = tc_clsact_del(self->ys, self->ifindex);
	EXPECT_EQ(0, ret) {
		TH_LOG("clsact del failed: %s", self->ys->err.msg);
	}
}

TEST_HARNESS_MAIN