2 test_optimal_pmtu.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "network_namespace_framework.h"
21 #define DEFAULT_PUB_NET_ADDR "203.0.113.0/24"
22 #define DEFAULT_GATEWAY_NET_ADDR "203.0.113.254"
23 #define NS_PEER0 " ns_peer0 "
24 #define NS_ETH0 " ns_eth0 "
25 #define PEER_INDEX i ? 0 : 1
26 #define get_namespace_handle_by_index(state_ptr, index) index < state_ptr->num_namespaces ? &(state_ptr->namespaces[index]) : NULL
27 #define get_interface_handle_by_index(namespace, index) index < namespace->interfaces_no ? &((namespace->interfaces)[index]) : NULL
29 static int ipv4_str_check_cidr(const char *ip_addr) {
31 sscanf(ip_addr, "%*d.%*d.%*d.%*d/%d", &cidr);
35 static char *ipv4_str_remove_cidr(const char *ipv4_addr) {
36 char *ptr = strdup(ipv4_addr);
39 if(ipv4_str_check_cidr(ptr)) {
40 char *end = strchr(ptr, '/');
47 namespace_t *find_namespace(netns_state_t *state, const char *namespace_name) {
50 for(i = 0; i < state->num_namespaces; i++) {
51 if(!strcmp((state->namespaces[i]).name, namespace_name)) {
52 return &(state->namespaces[i]);
59 static int netns_delete_namespace(namespace_t *namespace_handle) {
62 if(namespace_handle->type != BRIDGE) {
63 assert(sprintf(cmd, "ip netns del %s 2>/dev/null", namespace_handle->name) >= 0);
65 assert(sprintf(cmd, "ip link del %s 2>/dev/null", namespace_handle->name) >= 0);
71 /* Create new network namespace using namespace handle */
72 static void netns_create_namespace(netns_state_t *test_state, namespace_t *namespace_handle) {
76 // Add the network namespace
78 sprintf(cmd, "ip netns add %s", namespace_handle->name);
79 assert(system(cmd) == 0);
81 sprintf(cmd, "ip netns exec %s ip link set dev lo up", namespace_handle->name);
82 assert(system(cmd) == 0);
85 static void netns_create_bridge(netns_state_t *test_state, namespace_t *namespace_handle) {
89 sprintf(cmd, "ip link add name %s type bridge", namespace_handle->name);
90 assert(system(cmd) == 0);
92 sprintf(cmd, "ip link set %s up", namespace_handle->name);
93 assert(system(cmd) == 0);
96 interface_t *get_peer_interface_handle(netns_state_t *test_state, namespace_t *namespace, namespace_t *peer_namespace) {
98 interface_t *interfaces = namespace->interfaces;
99 int if_no = namespace->interfaces_no;
100 char *peer_name = peer_namespace->name;
102 for(i = 0; i < if_no; i++) {
103 if(!strcasecmp(interfaces[i].if_peer, peer_name)) {
104 return &interfaces[i];
111 interface_t *get_interface_handle_by_name(netns_state_t *test_state, namespace_t *namespace, const char *peer_name) {
112 namespace_t *peer_ns;
113 peer_ns = find_namespace(test_state, peer_name);
116 return get_peer_interface_handle(test_state, namespace, peer_ns);
119 bool check_interfaces_visited(netns_state_t *test_state, namespace_t *ns1, namespace_t *ns2) {
120 interface_t *iface, *peer_iface;
121 iface = get_peer_interface_handle(test_state, ns1, ns2);
122 peer_iface = get_peer_interface_handle(test_state, ns2, ns1);
123 assert(iface && peer_iface);
125 return iface->priv || peer_iface->priv;
128 void netns_connect_namespaces(netns_state_t *test_state, namespace_t *ns1, namespace_t *ns2) {
129 char buff[20], cmd[200], ns_eth0[20], ns_peer0[20];
130 int cmd_ret, if_no, i;
131 char eth_pairs[2][20];
132 namespace_t *ns[2] = { ns1, ns2 };
133 interface_t *interface;
136 // Check if visited already
137 if(check_interfaces_visited(test_state, ns1, ns2)) {
141 assert(sprintf(eth_pairs[0], "%.9s_eth0", ns2->name) >= 0);
142 assert(sprintf(eth_pairs[1], "%.9s_peer0", ns1->name) >= 0);
144 // Delete veth pair if already exists
145 for(i = 0; i < 2; i++) {
146 assert(sprintf(cmd, "ip link del %s 2>/dev/null", eth_pairs[i]) >= 0);
147 cmd_ret = system(cmd);
151 assert(sprintf(cmd, "ip link add %s type veth peer name %s", eth_pairs[0], eth_pairs[1]) >= 0);
152 assert(system(cmd) == 0);
154 for(i = 0; i < 2; i++) {
156 // Find interface handle that with it's peer interface
157 interface = get_peer_interface_handle(test_state, ns[i], ns[PEER_INDEX]);
160 if(ns[i]->type != BRIDGE) {
162 // Define interface name
165 if(interface->if_name) {
166 if_name = interface->if_name;
168 assert(sprintf(buff, "eth_%s", interface->if_peer) >= 0);
172 interface->if_name = strdup(if_name);
174 assert(interface->if_name);
176 // Connect one end of the the veth pair to the namespace's interface
177 assert(sprintf(cmd, "ip link set %s netns %s name %s", eth_pairs[i], ns[i]->name, interface->if_name) >= 0);
179 assert(system(cmd) == 0);
182 // Connect one end of the the veth pair to the bridge
183 assert(sprintf(cmd, "ip link set %s master %s up\n", eth_pairs[i], ns[i]->name) >= 0);
184 assert(system(cmd) == 0);
187 // Mark interfaces as connected
188 interface->priv = set;
189 interface = get_peer_interface_handle(test_state, ns[PEER_INDEX], ns[i]);
191 interface->priv = set;
195 void netns_configure_ip_address(netns_state_t *test_state) {
196 int i, if_no, cmd_ret;
197 namespace_t *namespace;
198 interface_t *if_handle;
201 for(i = 0; i < test_state->num_namespaces; i++) {
202 namespace = get_namespace_handle_by_index(test_state, i);
204 for(if_no = 0; if_no < namespace->interfaces_no; if_no++) {
205 if_handle = get_interface_handle_by_index(namespace, if_no);
208 if(if_handle->if_addr && namespace->type != BRIDGE) {
209 assert(sprintf(cmd, "ip netns exec %s ip addr add %s dev %s", namespace->name, if_handle->if_addr, if_handle->if_name) >= 0);
210 assert(system(cmd) == 0);
211 assert(sprintf(cmd, "ip netns exec %s ip link set dev %s up", namespace->name, if_handle->if_name) >= 0);
212 assert(system(cmd) == 0);
214 if(if_handle->if_default_route_ip) {
215 char *route_ip = ipv4_str_remove_cidr(if_handle->if_default_route_ip);
216 assert(sprintf(cmd, "ip netns exec %s ip route add default via %s", namespace->name, route_ip) >= 0);
217 assert(system(cmd) == 0);
225 void netns_enable_all_nats(netns_state_t *test_state) {
227 namespace_t *namespace, *peer_namespace;
228 interface_t *interface_handle;
232 for(i = 0; i < test_state->num_namespaces; i++) {
233 namespace = get_namespace_handle_by_index(test_state, i);
235 if(namespace->type == FULL_CONE) {
236 assert(namespace->nat_arg);
237 netns_fullcone_handle_t **nat_rules = namespace->nat_arg;
240 for(j = 0; nat_rules[j]; j++) {
241 assert(nat_rules[j]->snat_to_source && nat_rules[j]->dnat_to_destination);
243 interface_handle = get_interface_handle_by_name(test_state, namespace, nat_rules[j]->snat_to_source);
244 assert(interface_handle);
245 eth0 = interface_handle->if_name;
246 ip_addr = ipv4_str_remove_cidr(interface_handle->if_addr);
247 assert(sprintf(cmd, "ip netns exec %s iptables -t nat -A POSTROUTING -o %s -j SNAT --to-source %s", namespace->name, eth0, ip_addr) >= 0);
248 assert(system(cmd) == 0);
251 peer_namespace = find_namespace(test_state, nat_rules[j]->dnat_to_destination);
252 interface_handle = get_interface_handle_by_name(test_state, peer_namespace, namespace->name);
253 assert(interface_handle);
255 ip_addr = ipv4_str_remove_cidr(interface_handle->if_addr);
256 assert(sprintf(cmd, "ip netns exec %s iptables -t nat -A PREROUTING -i %s -j DNAT --to-destination %s", namespace->name, eth0, ip_addr) >= 0);
257 assert(system(cmd) == 0);
264 void netns_create_all_namespaces(netns_state_t *test_state) {
266 namespace_t *namespace, *peer_namespace;
267 interface_t *interfaces;
269 for(i = 0; i < test_state->num_namespaces; i++) {
270 namespace = get_namespace_handle_by_index(test_state, i);
272 // Delete the namespace if already exists
273 netns_delete_namespace(namespace);
277 if(namespace->type != BRIDGE) {
278 netns_create_namespace(test_state, namespace);
280 netns_create_bridge(test_state, namespace);
285 void netns_connect_all_namespaces(netns_state_t *test_state) {
287 namespace_t *namespace, *peer_namespace;
288 interface_t *interfaces;
289 interface_t *interface_handle;
291 for(i = 0; i < test_state->num_namespaces; i++) {
292 namespace = get_namespace_handle_by_index(test_state, i);
293 assert(namespace->interfaces);
294 interfaces = namespace->interfaces;
296 for(j = 0; j < namespace->interfaces_no; j++) {
297 peer_namespace = find_namespace(test_state, interfaces[j].if_peer);
298 assert(peer_namespace);
299 netns_connect_namespaces(test_state, namespace, peer_namespace);
303 // Reset all priv members of the interfaces
305 for(i = 0; i < test_state->num_namespaces; i++) {
306 namespace = get_namespace_handle_by_index(test_state, i);
307 assert(namespace->interfaces);
309 for(j = 0; j < namespace->interfaces_no; j++) {
310 interface_handle = get_interface_handle_by_index(namespace, j);
311 assert(interface_handle);
312 interface_handle->priv = NULL;
317 void increment_ipv4_str(char *ip_addr, int ip_addr_size) {
318 uint32_t addr_int_n, addr_int_h;
320 assert(inet_pton(AF_INET, ip_addr, &addr_int_n) > 0);
321 addr_int_h = ntohl(addr_int_n);
322 addr_int_h = addr_int_h + 1;
323 addr_int_n = htonl(addr_int_h);
324 assert(inet_ntop(AF_INET, &addr_int_n, ip_addr, ip_addr_size));
327 void increment_ipv4_cidr_str(char *ip) {
329 assert(sscanf(ip, "%*d.%*d.%*d.%*d/%d", &subnet) >= 0);
330 char *ptr = strchr(ip, '/');
332 increment_ipv4_str(ip, INET6_ADDRSTRLEN);
333 sprintf(ip, "%s/%d", ip, subnet);
336 interface_t *netns_get_priv_addr(netns_state_t *test_state, const char *namespace_name) {
337 namespace_t *namespace_handle;
338 interface_t *interface_handle;
341 namespace_handle = find_namespace(test_state, namespace_name);
342 assert(namespace_handle);
344 for(if_no = 0; if_no < namespace_handle->interfaces_no; if_no++) {
345 interface_handle = get_interface_handle_by_index(namespace_handle, if_no);
347 if(!strcmp(namespace_handle->name, interface_handle->fetch_ip_netns_name)) {
348 return interface_handle;
355 void netns_add_default_route_addr(netns_state_t *test_state) {
357 namespace_t *namespace_handle;
358 interface_t *interface_handle, *peer_interface_handle;
360 for(ns = 0; ns < test_state->num_namespaces; ns++) {
361 namespace_handle = get_namespace_handle_by_index(test_state, ns);
362 assert(namespace_handle);
364 if(namespace_handle->type != HOST) {
368 for(if_no = 0; if_no < namespace_handle->interfaces_no; if_no++) {
369 interface_handle = get_interface_handle_by_index(namespace_handle, if_no);
371 if(interface_handle->if_default_route_ip == NULL) {
372 peer_interface_handle = netns_get_priv_addr(test_state, interface_handle->fetch_ip_netns_name);
373 assert(peer_interface_handle);
374 interface_handle->if_default_route_ip = ipv4_str_remove_cidr(peer_interface_handle->if_addr);
376 char *dup = strdup(interface_handle->if_default_route_ip);
378 interface_handle->if_default_route_ip = dup;
384 void netns_assign_ip_addresses(netns_state_t *test_state) {
386 namespace_t *namespace_handle;
387 interface_t *interface_handle, *peer_interface_handle;
391 char *addr = malloc(INET6_ADDRSTRLEN);
394 if(test_state->public_net_addr) {
395 assert(strncpy(addr, test_state->public_net_addr, INET6_ADDRSTRLEN));
397 assert(strncpy(addr, DEFAULT_PUB_NET_ADDR, INET6_ADDRSTRLEN));
400 test_state->public_net_addr = addr;
402 for(ns = 0; ns < test_state->num_namespaces; ns++) {
403 namespace_handle = get_namespace_handle_by_index(test_state, ns);
404 assert(namespace_handle);
406 if(namespace_handle->type == BRIDGE) {
410 for(j = 0; j < namespace_handle->interfaces_no; j++) {
411 interface_handle = get_interface_handle_by_index(namespace_handle, j);
412 assert(interface_handle);
414 if(interface_handle->if_addr) {
418 // If fetch ip net namespace name is given get IP address from it, else get a public IP address
420 if(interface_handle->fetch_ip_netns_name) {
421 namespace_t *gw_netns_handle = find_namespace(test_state, interface_handle->fetch_ip_netns_name);
422 assert(gw_netns_handle);
423 assert(gw_netns_handle->static_config_net_addr);
425 increment_ipv4_cidr_str(gw_netns_handle->static_config_net_addr);
426 interface_handle->if_addr = strdup(gw_netns_handle->static_config_net_addr);
428 increment_ipv4_cidr_str(test_state->public_net_addr);
429 interface_handle->if_addr = strdup(test_state->public_net_addr);
431 if(namespace_handle->type == HOST) {
432 if(interface_handle->if_default_route_ip) {
433 char *dup = strdup(interface_handle->if_default_route_ip);
435 interface_handle->if_default_route_ip = dup;
437 interface_handle->if_default_route_ip = strdup(DEFAULT_GATEWAY_NET_ADDR);
444 netns_add_default_route_addr(test_state);
447 static void netns_namespace_init_pids(netns_state_t *test_state) {
449 namespace_t *namespace_handle;
451 for(if_no = 0; if_no < test_state->num_namespaces; if_no++) {
452 namespace_handle = get_namespace_handle_by_index(test_state, if_no);
453 assert(namespace_handle);
454 namespace_handle->pid_nos = 0;
455 namespace_handle->pids = NULL;
459 pid_t run_cmd_in_netns(netns_state_t *test_state, char *namespace_name, char *cmd_str) {
461 namespace_t *namespace_handle;
464 assert(namespace_name && cmd_str);
465 namespace_handle = find_namespace(test_state, namespace_name);
466 assert(namespace_handle);
468 if((pid = fork()) == 0) {
469 assert(daemon(1, 0) != -1);
470 assert(sprintf(cmd, "ip netns exec %s %s", namespace_name, cmd_str) >= 0);
471 assert(system(cmd) == 0);
476 pid_ptr = realloc(namespace_handle->pids, (namespace_handle->pid_nos + 1) * sizeof(pid_t));
478 namespace_handle->pids = pid_ptr;
479 (namespace_handle->pids)[namespace_handle->pid_nos] = pid;
480 namespace_handle->pid_nos = namespace_handle->pid_nos + 1;
485 static void *pthread_fun(void *arg) {
486 netns_thread_t *netns_arg = (netns_thread_t *)arg;
487 char namespace_path[100];
489 assert(sprintf(namespace_path, "/var/run/netns/%s", netns_arg->namespace_name) >= 0);
490 int fd = open(namespace_path, O_RDONLY);
492 assert(setns(fd, CLONE_NEWNET) != -1);
494 ret = (netns_arg->netns_thread)(netns_arg->arg);
495 pthread_detach(netns_arg->thread_handle);
499 void run_node_in_namespace_thread(netns_thread_t *netns_arg) {
500 assert(netns_arg->namespace_name && netns_arg->netns_thread);
501 assert(!pthread_create(&(netns_arg->thread_handle), NULL, pthread_fun, netns_arg));
504 void netns_destroy_topology(netns_state_t *test_state) {
505 namespace_t *namespace_handle;
506 interface_t *interface_handle;
510 for(if_no = 0; if_no < test_state->num_namespaces; if_no++) {
511 namespace_handle = get_namespace_handle_by_index(test_state, if_no);
512 assert(namespace_handle->interfaces);
514 for(i = 0; i < namespace_handle->pid_nos; i++) {
515 pid = (namespace_handle->pids)[i];
516 assert(kill(pid, SIGINT) != -1);
517 pid_ret = waitpid(pid, NULL, WNOHANG);
518 assert(pid_ret != -1);
521 fprintf(stderr, "pid: %d, is still running\n", pid);
525 // Free interface name, interface address, interface default address etc.,
526 // which are dynamically allocated and set the values to NULL
528 for(j = 0; j < namespace_handle->interfaces_no; j++) {
529 interface_handle = get_interface_handle_by_index(namespace_handle, j);
530 assert(interface_handle);
532 free(interface_handle->if_name);
533 interface_handle->if_name = NULL;
534 free(interface_handle->if_addr);
535 interface_handle->if_addr = NULL;
536 free(interface_handle->if_default_route_ip);
537 interface_handle->if_default_route_ip = NULL;
541 assert(netns_delete_namespace(namespace_handle) == 0);
544 free(test_state->public_net_addr);
545 test_state->public_net_addr = NULL;
548 bool netns_create_topology(netns_state_t *test_state) {
550 // (Re)create name-spaces and bridges
551 netns_create_all_namespaces(test_state);
553 // Connect namespaces and bridges(if any) with their interfaces
554 netns_connect_all_namespaces(test_state);
556 // Assign IP addresses for the interfaces in namespaces
557 netns_assign_ip_addresses(test_state);
559 // Configure assigned IP addresses with the interfaces in netns
560 netns_configure_ip_address(test_state);
563 netns_enable_all_nats(test_state);
565 netns_namespace_init_pids(test_state);