diff options
author | Nicolas PLANEL <nicolas.planel@enovance.com> | 2013-10-10 21:43:12 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2013-10-22 12:20:41 (GMT) |
commit | cb034e12a42d95c25b9029e577e4581c3a1e4399 (patch) | |
tree | 01a2a861e238c4e361f2816c7c4e69eeddfe7329 /tests | |
parent | fdd1ba220dd7b780400e9d0652cde80e59f63572 (diff) | |
download | libnl-cb034e12a42d95c25b9029e577e4581c3a1e4399.zip libnl-cb034e12a42d95c25b9029e577e4581c3a1e4399.tar.gz libnl-cb034e12a42d95c25b9029e577e4581c3a1e4399.tar.bz2 |
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>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/test-create-bridge.c | 80 |
2 files changed, 82 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 4b63a7c..a37de96 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 diff --git a/tests/test-create-bridge.c b/tests/test-create-bridge.c new file mode 100644 index 0000000..7202cd7 --- /dev/null +++ b/tests/test-create-bridge.c @@ -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; +} |