summary refs log tree commit diff
path: root/host/start-vmm/tests/bridge_add_if.c
blob: f65151c972ff3a2b4c8d491fc4549f142757f027 (plain) (blame)
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
// SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>

#include "../net-util.h"

#include <assert.h>
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/mount.h>
#include <sys/stat.h>

int main(void)
{
	char bridge_name[IFNAMSIZ], tap_name[IFNAMSIZ] = "tap%d";
	char brif_path[32 + IFNAMSIZ * 2];
	int r, tap;
	struct stat statbuf;

	if (!unshare(CLONE_NEWUSER|CLONE_NEWNET|CLONE_NEWNS)) {
		if (mount("sysfs", "/sys", "sysfs", 0, NULL) == -1)
			return errno == ENOENT ? 77 : 1;
	}

	tap = tap_open(tap_name, 0);
	if (tap == -1)
		return errno == EPERM || errno == ENOENT ? 77 : 1;

	r = snprintf(bridge_name, sizeof bridge_name, "br%d", rand());
	assert(r > 0 && (size_t)r < sizeof bridge_name);
	assert(bridge_add(bridge_name) != -1);

	assert(bridge_add_if(bridge_name, tap_name) != -1);

	r = snprintf(brif_path, sizeof brif_path,
		     "/sys/devices/virtual/net/%s/brif/%s",
		     bridge_name, tap_name);
	assert(r > 0 && (size_t)r < sizeof brif_path);

	assert(!lstat(brif_path, &statbuf));
}