test: add bridge creation sample
New test sample file, test-create-bridge.c Create an bridge (testbrige) and attach an already setup interface (testtap1) to it. Signed-off-by: Nicolas PLANEL <nicolas.planel@enovance.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
parent
fdd1ba220d
commit
cb034e12a4
2 changed files with 82 additions and 0 deletions
|
@ -21,6 +21,7 @@ UNIT_TESTS = check-all
|
|||
check_PROGRAMS = \
|
||||
test-create-bond \
|
||||
test-create-vlan \
|
||||
test-create-bridge \
|
||||
test-delete-link \
|
||||
test-socket-creation \
|
||||
test-complex-HTB-with-hash-filters \
|
||||
|
@ -40,6 +41,7 @@ endif
|
|||
test_cache_mngr_SOURCES = test-cache-mngr.c
|
||||
test_create_bond_SOURCES = test-create-bond.c
|
||||
test_create_vlan_SOURCES = test-create-vlan.c
|
||||
test_create_bridge_SOURCES = test-create-bridge.c
|
||||
test_delete_link_SOURCES = test-delete-link.c
|
||||
test_genl_SOURCES = test-genl.c
|
||||
test_nf_cache_mngr_SOURCES = test-nf-cache-mngr.c
|
||||
|
|
80
tests/test-create-bridge.c
Normal file
80
tests/test-create-bridge.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include <netlink/netlink.h>
|
||||
#include <netlink/route/link.h>
|
||||
#include <netlink/route/link/bridge.h>
|
||||
|
||||
#define TEST_BRIDGE_NAME "testbridge"
|
||||
#define TEST_INTERFACE_NAME "testtap1"
|
||||
|
||||
int create_bridge(struct nl_sock *sk, struct nl_cache *link_cache, const char *name) {
|
||||
struct rtnl_link *link;
|
||||
int err;
|
||||
|
||||
link = rtnl_link_alloc();
|
||||
if ((err = rtnl_link_set_type(link, "bridge")) < 0) {
|
||||
rtnl_link_put(link);
|
||||
return err;
|
||||
}
|
||||
rtnl_link_set_name(link, name);
|
||||
|
||||
if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
|
||||
return err;
|
||||
}
|
||||
rtnl_link_put(link);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct rtnl_link *link;
|
||||
struct nl_cache *link_cache;
|
||||
struct nl_sock *sk;
|
||||
int err;
|
||||
|
||||
sk = nl_socket_alloc();
|
||||
if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
|
||||
nl_perror(err, "Unable to connect socket");
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
|
||||
nl_perror(err, "Unable to allocate cache");
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = create_bridge(sk, link_cache, TEST_BRIDGE_NAME)) < 0) {
|
||||
nl_perror(err, "Unable to allocate testbridge");
|
||||
return err;
|
||||
}
|
||||
|
||||
nl_cache_refill(sk, link_cache);
|
||||
|
||||
link = rtnl_link_get_by_name(link_cache, TEST_BRIDGE_NAME);
|
||||
struct rtnl_link *ltap = rtnl_link_get_by_name(link_cache, TEST_INTERFACE_NAME);
|
||||
if (!ltap) {
|
||||
fprintf(stderr, "You should create a tap interface before lunch this test (# tunctl -t %s)\n", TEST_INTERFACE_NAME);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((err = rtnl_link_enslave(sk, link, ltap)) < 0) {
|
||||
nl_perror(err, "Unable to enslave interface to his bridge\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
if(rtnl_link_is_bridge(link) == 0) {
|
||||
fprintf(stderr, "Link is not a bridge\n");
|
||||
return -2;
|
||||
}
|
||||
if(rtnl_link_get_master(ltap) <= 0) {
|
||||
fprintf(stderr, "Interface is not attached to a bridge\n");
|
||||
return -3;
|
||||
}
|
||||
|
||||
rtnl_link_put(ltap);
|
||||
rtnl_link_put(link);
|
||||
|
||||
nl_cache_free(link_cache);
|
||||
nl_socket_free(sk);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue