-
Notifications
You must be signed in to change notification settings - Fork 25
/
ct_net_host.c
84 lines (66 loc) · 1.75 KB
/
ct_net_host.c
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
/*
* Test how host nic assignment works
*/
#include <sys/types.h>
#include <libct.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sched.h>
#include <stdlib.h>
#include "test.h"
static int check_ct_net(void *a)
{
int *ct_status = a;
ct_status[0] = 1;
if (!system("ip link l dm0"))
ct_status[1] = 1;
return 0;
}
int main(int argc, char **argv)
{
int *ct_status;
libct_session_t s;
ct_handler_t ct;
ct_process_desc_t p;
ct_process_t pr;
ct_net_t nd;
test_init(argc, argv);
ct_status = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
if (ct_status == MAP_FAILED)
return tst_perr("Unable to allocate memory");
ct_status[0] = 0;
ct_status[1] = 0;
system("ip link add name dm0 type dummy");
if (system("ip link l dm0"))
return tst_err("Can't create dummy device");
s = libct_session_open_local();
if (libct_handle_is_err(s))
return tst_err("Unable to create a session");
ct = libct_container_create(s, "test");
p = libct_process_desc_create(s);
if (libct_handle_is_err(ct) ||
libct_handle_is_err(p))
return tst_err("Unable tot create handle");
if (libct_container_set_nsmask(ct, CLONE_NEWNET))
return tst_err("Unable to set nsmask");
nd = libct_net_add(ct, CT_NET_HOSTNIC, "dm0");
if (libct_handle_is_err(nd)) {
system("ip link del dm0");
return tst_err("Can't add hostnic");
}
pr = libct_container_spawn_cb(ct, p, check_ct_net, ct_status);
if (libct_handle_is_err(pr)) {
system("ip link del dm0");
return tst_err("Can't spawn CT");
}
libct_container_wait(ct);
libct_container_destroy(ct);
libct_session_close(s);
system("ip link del dm0");
if (!ct_status[0])
return fail("CT is not alive");
if (!ct_status[1])
return fail("Netdevice not assigned");
return pass("HostNic works OK");
}