X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=test%2Fnetns_utils.c;fp=test%2Fnetns_utils.c;h=d957d5b63386640e622821ef673ec45cb1b3e41b;hp=0000000000000000000000000000000000000000;hb=e38737be28c742b241a6b0ffdbad541c3c0b13b2;hpb=d78e24a1f77084f2395b7f344533fc1819b0b3b0 diff --git a/test/netns_utils.c b/test/netns_utils.c new file mode 100644 index 00000000..d957d5b6 --- /dev/null +++ b/test/netns_utils.c @@ -0,0 +1,148 @@ +#define _GNU_SOURCE 1 + +#ifndef NDEBUG +#undef NDEBUG +#endif + +#include +#include +#include +#include +#include + +#include "../src/meshlink.h" +#include "netns_utils.h" +#include "utils.h" + +static int ip = 1; + +/// Create meshlink instances and network namespaces for a list of peers +static void create_peers(peer_config_t *peers, int npeers, const char *prefix) { + // We require root for network namespaces + if(getuid() != 0) { + exit(77); + } + + for(int i = 0; i < npeers; i++) { + assert(asprintf(&peers[i].netns_name, "%s%d", prefix, i) > 0); + char *command = NULL; + assert(asprintf(&command, + "/bin/ip netns delete %1$s 2>/dev/null || true;" + "/bin/ip netns add %1$s;" + "/bin/ip netns exec %1$s ip link set dev lo up;", + peers[i].netns_name)); + assert(command); + assert(system(command) == 0); + free(command); + + char *netns_path = NULL; + assert(asprintf(&netns_path, "/run/netns/%s", peers[i].netns_name)); + assert(netns_path); + peers[i].netns = open(netns_path, O_RDONLY); + assert(peers[i].netns != -1); + free(netns_path); + + char *conf_path = NULL; + assert(asprintf(&conf_path, "%s_conf.%d", prefix, i + 1) > 0); + assert(conf_path); + assert(meshlink_destroy(conf_path)); + + meshlink_open_params_t *params = meshlink_open_params_init(conf_path, peers[i].name, prefix, peers[i].devclass); + assert(params); + assert(meshlink_open_params_set_netns(params, peers[i].netns)); + + peers[i].mesh = meshlink_open_ex(params); + assert(peers[i].mesh); + free(params); + free(conf_path); + + meshlink_enable_discovery(peers[i].mesh, false); + } +} + +/// Set up a LAN topology where all peers can see each other directly +static void setup_lan_topology(peer_config_t *peers, int npeers) { + // Set up the LAN bridge + { + char *command = NULL; + assert(asprintf(&command, + "/bin/ip netns exec %1$s /bin/ip link add eth0 type bridge;" + "/bin/ip netns exec %1$s /bin/ip link set eth0 up;", + peers[0].netns_name)); + assert(command); + assert(system(command) == 0); + } + + // Add an interface to each peer that is connected to the bridge + for(int i = 1; i < npeers; i++) { + char *command = NULL; + assert(asprintf(&command, + "/bin/ip netns exec %1$s /bin/ip link add eth0 type veth peer eth%3$d netns %2$s;" + "/bin/ip netns exec %1$s /bin/ip link set dev eth0 up;" + "/bin/ip netns exec %2$s /bin/ip link set dev eth%3$d master eth0 up;", + peers[i].netns_name, peers[0].netns_name, i)); + assert(command); + assert(system(command) == 0); + free(command); + } + + // Configure addresses + for(int i = 0; i < npeers; i++) { + change_peer_ip(&peers[i]); + } +} + +/// Give a peer a unique IP address +void change_peer_ip(peer_config_t *peer) { + char *command = NULL; + assert(asprintf(&command, + "/bin/ip netns exec %1$s ip addr flush dev eth0;" + "/bin/ip netns exec %1$s ip addr add 203.0.113.%2$d/24 dev eth0;", + peer->netns_name, ip)); + ip++; + assert(command); + assert(system(command) == 0); + free(command); +} + +/// Let the first peer in a list invite all the subsequent peers +static void invite_peers(peer_config_t *peers, int npeers) { + assert(meshlink_start(peers[0].mesh)); + + for(int i = 1; i < npeers; i++) { + char *invitation = meshlink_invite_ex(peers[0].mesh, NULL, peers[i].name, MESHLINK_INVITE_LOCAL | MESHLINK_INVITE_NUMERIC); + assert(invitation); + assert(meshlink_join(peers[i].mesh, invitation)); + free(invitation); + } + + meshlink_stop(peers[0].mesh); +} + +/// Close meshlink instances and clean up +static void close_peers(peer_config_t *peers, int npeers) { + for(int i = 0; i < npeers; i++) { + meshlink_close(peers[i].mesh); + close(peers[i].netns); + free(peers[i].netns_name); + } +} + +/// Set up relay, peer and NUT that are directly connected +peer_config_t *setup_relay_peer_nut(const char *prefix) { + static peer_config_t peers[] = { + {"relay", DEV_CLASS_BACKBONE}, + {"peer", DEV_CLASS_STATIONARY}, + {"nut", DEV_CLASS_STATIONARY}, + }; + + create_peers(peers, 3, prefix); + setup_lan_topology(peers, 3); + invite_peers(peers, 3); + + return peers; +} + +void close_relay_peer_nut(peer_config_t *peers) { + close_peers(peers, 3); +}