summaryrefslogtreecommitdiffstats
path: root/lib/libxdp/tests/test_xdp_frags.c
blob: d70e802ae0a20f46fd20a61b64aeda2843819e47 (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
/* SPDX-License-Identifier: GPL-2.0 */

#define _GNU_SOURCE

#include <errno.h>
#include <linux/err.h>
#include <net/if.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>

#include "test_utils.h"

#include <xdp/libxdp.h>
#include <bpf/libbpf.h>

# define ARRAY_SIZE(_x) (sizeof(_x) / sizeof((_x)[0]))

static bool kern_compat;


static struct xdp_program *load_prog(void)
{
        DECLARE_LIBXDP_OPTS(xdp_program_opts, opts,
                            .prog_name = "xdp_pass",
                            .find_filename = "xdp-dispatcher.o",
                );
        return xdp_program__create(&opts);
}

static int check_attached_progs(int ifindex, int count, bool frags)
{
        struct xdp_multiprog *mp;
        int ret;

        /* If the kernel does not support frags, we always expect
         * frags support to be disabled on a returned dispatcher
         */
        if (!kern_compat)
                frags = false;

        mp = xdp_multiprog__get_from_ifindex(ifindex);
        ret = libxdp_get_error(mp);
	if (ret) {
		fprintf(stderr, "Couldn't get multiprog on ifindex %d: %s\n",
			ifindex, strerror(-ret));
		return ret;
	}

        ret = -EINVAL;

	if (xdp_multiprog__is_legacy(mp)) {
		fprintf(stderr, "Found legacy prog on ifindex %d\n", ifindex);
		goto out;
	}

	if (xdp_multiprog__program_count(mp) != count) {
		fprintf(stderr, "Expected %d programs loaded on ifindex %d, found %d\n",
                        count, ifindex, xdp_multiprog__program_count(mp));
		goto out;
	}

	if (xdp_multiprog__xdp_frags_support(mp) != frags) {
		fprintf(stderr,
			"Multiprog on ifindex %d %s frags, expected %s\n",
			ifindex,
			xdp_multiprog__xdp_frags_support(mp) ?
				"supports" :
				"does not support",
			frags ? "support" : "no support");
                goto out;
	}

        ret = 0;

out:
        xdp_multiprog__close(mp);
        return ret;
}

static void print_test_result(const char *func, int ret)
{
        fflush(stderr);
	fprintf(stderr, "%s:\t%s\n", func, ret ? "FAILED" : "PASSED");
        fflush(stdout);
}

static int load_attach_prog(struct xdp_program **prog, int ifindex, bool frags)
{
        int ret;

        *prog = load_prog();
	if (!*prog) {
		ret = -errno;
		fprintf(stderr, "Couldn't load program: %s\n", strerror(-ret));
		return ret;
	}

        ret = xdp_program__set_xdp_frags_support(*prog, frags);
        if (ret)
                return ret;

        return xdp_program__attach(*prog, ifindex, XDP_MODE_NATIVE, 0);
}

static int _check_load(int ifindex, bool frags, bool should_succeed)
{
        struct xdp_program *prog = NULL;
        bool attached;
        int ret;

        ret = load_attach_prog(&prog, ifindex, frags);
        attached = !ret;

	if (attached != should_succeed) {
		ret = -EINVAL;
		goto out;
	}

        if (should_succeed)
                ret = check_attached_progs(ifindex, 1, frags);
        else
                ret = 0;

out:
        if (attached)
                xdp_program__detach(prog, ifindex, XDP_MODE_NATIVE, 0);
        xdp_program__close(prog);
        return ret;
}

static int check_load_frags(int ifindex_bigmtu, int ifindex_smallmtu)
{
        int ret = _check_load(ifindex_smallmtu, true, true);
        if (!ret && ifindex_bigmtu)
                _check_load(ifindex_bigmtu, true, true);
        print_test_result(__func__, ret);
        return ret;
}

static int check_load_nofrags_success(int ifindex)
{
        int ret = _check_load(ifindex, false, true);
        print_test_result(__func__, ret);
        return ret;
}

static int check_load_nofrags_fail(int ifindex)
{
        int ret = _check_load(ifindex, false, false);
        print_test_result(__func__, ret);
        return ret;
}
static int check_load_frags_multi(int ifindex)
{
        struct xdp_program *prog1 = NULL, *prog2 = NULL;
        int ret;

        ret = load_attach_prog(&prog1, ifindex, true);
        if (ret)
                goto out;

        ret = load_attach_prog(&prog2, ifindex, true);
        if (ret)
                goto out_prog1;

        ret = check_attached_progs(ifindex, 2, true);

        xdp_program__detach(prog2, ifindex, XDP_MODE_NATIVE, 0);
out_prog1:
        xdp_program__detach(prog1, ifindex, XDP_MODE_NATIVE, 0);
out:
        xdp_program__close(prog2);
        xdp_program__close(prog1);
        print_test_result(__func__, ret);
        return ret;
}

static int check_load_mix_small(int ifindex)
{
        struct xdp_program *prog1 = NULL, *prog2 = NULL;
        int ret;

        ret = load_attach_prog(&prog1, ifindex, true);
        if (ret)
                goto out;

        /* First program attached, dispatcher supports frags */
	ret = check_attached_progs(ifindex, 1, true);
        if (ret)
                goto out;

        ret = load_attach_prog(&prog2, ifindex, false);
	if (ret)
		goto out_prog1;

        /* Mixed program attachment, dispatcher should not support frags */
	ret = check_attached_progs(ifindex, 2, false);

        ret = xdp_program__detach(prog2, ifindex, XDP_MODE_NATIVE, 0) || ret;
        if (ret)
                goto out_prog1;

        /* Second program removed, back to frags-only */
	ret = check_attached_progs(ifindex, 1, true) || ret;

out_prog1:
        xdp_program__detach(prog1, ifindex, XDP_MODE_NATIVE, 0);

out:
        xdp_program__close(prog2);
        xdp_program__close(prog1);
        print_test_result(__func__, ret);
        return ret;
}

static int check_load_mix_big(int ifindex)
{
        struct xdp_program *prog1 = NULL, *prog2 = NULL;
        int ret;

        ret = load_attach_prog(&prog1, ifindex, true);
        if (ret)
                goto out;

        /* First program attached, dispatcher supports frags */
	ret = check_attached_progs(ifindex, 1, true);
        if (ret)
                goto out;

        /* Second non-frags program should fail on big-MTU device */
        ret = load_attach_prog(&prog2, ifindex, false);
	if (!ret) {
		xdp_program__detach(prog2, ifindex, XDP_MODE_NATIVE, 0);
		ret = -EINVAL;
		goto out_prog1;
	}

	/* Still only a single program loaded, with frags support */
	ret = check_attached_progs(ifindex, 1, true);

out_prog1:
        xdp_program__detach(prog1, ifindex, XDP_MODE_NATIVE, 0);

out:
        xdp_program__close(prog2);
        xdp_program__close(prog1);
        print_test_result(__func__, ret);
        return ret;
}


static bool check_frags_compat(void)
{
	struct xdp_program *test_prog;
        struct bpf_program *prog;
        struct bpf_object *obj;
	bool ret = false;
        int err;

	test_prog = load_prog();
	if (!test_prog)
		return false;

        obj = xdp_program__bpf_obj(test_prog);
        if (!obj)
                goto out;

        prog = bpf_object__find_program_by_name(obj, "xdp_pass");
        if (!prog)
                goto out;

	bpf_program__set_flags(prog, BPF_F_XDP_HAS_FRAGS);
        err = bpf_object__load(obj);
	if (!err) {
		printf("Kernel supports XDP programs with frags\n");
                ret = true;
	} else {
		printf("Kernel DOES NOT support XDP programs with frags\n");
	}
        fflush(stdout);

out:
	xdp_program__close(test_prog);
	return ret;
}

static void usage(char *progname)
{
	fprintf(stderr, "Usage: %s <ifname_bigmtu> <ifname_smallmtu>\n", progname);
	exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
	int ifindex_bigmtu, ifindex_smallmtu, ret;
        char *envval;

        envval = secure_getenv("VERBOSE_TESTS");

	silence_libbpf_logging();
        if (envval && envval[0] == '1')
                verbose_libxdp_logging();
        else
                silence_libxdp_logging();

        kern_compat = check_frags_compat();

	if (argc != 3)
                usage(argv[0]);

	ifindex_bigmtu = if_nametoindex(argv[1]);
	ifindex_smallmtu = if_nametoindex(argv[2]);
	if (!ifindex_bigmtu || !ifindex_smallmtu) {
		fprintf(stderr, "Interface '%s' or '%s' not found.\n", argv[1], argv[2]);
                usage(argv[0]);
	}

	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
		fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
			strerror(errno));
		exit(EXIT_FAILURE);
	}

        ret = check_load_frags(kern_compat ? ifindex_bigmtu : 0, ifindex_smallmtu);
        ret = check_load_nofrags_success(ifindex_smallmtu) || ret;
	if (kern_compat) {
		ret = check_load_nofrags_fail(ifindex_bigmtu) || ret;
		ret = check_load_frags_multi(ifindex_bigmtu) || ret;
                ret = check_load_mix_big(ifindex_bigmtu) || ret;
	}
	ret = check_load_mix_small(ifindex_smallmtu) || ret;

	return ret;
}