]> git.meshlink.io Git - meshlink/blob - test/pmtu.c
cb39d4bceb08e0dcd153819e68c543300a5bfabb
[meshlink] / test / pmtu.c
1 #ifndef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14
15 #include "meshlink.h"
16 #include "utils.h"
17
18 #define nnodes 3
19
20 static const struct info {
21         const char *name;
22         const char *confdir;
23         const char *netns;
24         dev_class_t devclass;
25 } nodes[nnodes] = {
26         {"relay", "pmtu_conf.1", "/run/netns/pmtu_r", DEV_CLASS_BACKBONE},
27         {"peer", "pmtu_conf.2", "/run/netns/pmtu_p", DEV_CLASS_STATIONARY},
28         {"nut", "pmtu_conf.3", "/run/netns/pmtu_n", DEV_CLASS_STATIONARY},
29 };
30
31 static struct state {
32         meshlink_handle_t *mesh;
33         int netns;
34         struct sync_flag up_flag;
35         int pmtu;
36         int probe_count;
37         int probe_bytes;
38 } states[nnodes];
39
40 static void relay_up_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
41         struct state *state = mesh->priv;
42
43         // Check that we are connected to another peer besides the relay
44         if(reachable && node != meshlink_get_self(mesh) && strcmp(node->name, "relay")) {
45                 set_sync_flag(&state->up_flag, true);
46                 meshlink_set_node_status_cb(mesh, NULL);
47         }
48 }
49
50 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
51         if(!data && !len) {
52                 meshlink_channel_close(mesh, channel);
53         }
54 }
55
56 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
57         (void)port;
58         (void)data;
59         (void)len;
60         meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
61         return true;
62 }
63
64 static void parse_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
65         if(level >= MESHLINK_INFO) {
66                 log_cb(mesh, level, text);
67         }
68
69         struct state *state = mesh->priv;
70
71         if(state->pmtu != 0) {
72                 return;
73         }
74
75         int len;
76         char name[10] = "";
77
78         if(sscanf(text, "Sending UDP probe length %d to %9s", &len, name) == 2 || sscanf(text, "Got PMTU probe length %d from %s", &len, name) == 2) {
79                 if(!strcmp(name, "nut")) {
80                         state->probe_count++;
81                         state->probe_bytes += len;
82                 }
83         } else if(sscanf(text, "Fixing PMTU of %9s to %d", name, &len) == 2) {
84                 if(!strcmp(name, "nut")) {
85                         state->pmtu = len;
86                 }
87         }
88 }
89
90 static void wait_for_pmtu(void) {
91         // Set up a channel from peer to nut
92         meshlink_set_channel_accept_cb(states[2].mesh, accept_cb);
93         meshlink_channel_t *channel = meshlink_channel_open(states[1].mesh, meshlink_get_node(states[1].mesh, nodes[2].name), 1, NULL, NULL, 0);
94         assert(channel);
95
96         // While sending regular data, wait for PMTU discovery to finish
97         for(int i = 0; i < 30; i++) {
98                 sleep(1);
99
100                 if(states[1].pmtu) {
101                         break;
102                 }
103
104                 assert(meshlink_channel_send(states[1].mesh, channel, "ping", 4) == 4);
105         }
106
107         meshlink_channel_close(states[1].mesh, channel);
108 }
109
110 static void start_peer_nut(void) {
111         // Start peer and nut
112         for(int i = 1; i < nnodes; i++) {
113                 meshlink_set_node_status_cb(states[i].mesh, relay_up_cb);
114                 assert(meshlink_start(states[i].mesh));
115         }
116
117         // Wait for the peer and nut to see each other
118         for(int i = 1; i < nnodes; i++) {
119                 assert(wait_sync_flag(&states[i].up_flag, 5));
120         }
121 }
122
123 static void stop_peer_nut(void) {
124         // Stop peer and nut, reset counters
125         for(int i = 1; i < nnodes; i++) {
126                 meshlink_stop(states[i].mesh);
127                 states[i].up_flag.flag = false;
128                 states[i].pmtu = 0;
129                 states[i].probe_count = 0;
130                 states[i].probe_bytes = 0;
131         }
132 }
133
134 int main(void) {
135         // This test requires root access due to the use of network namespaces
136         if(getuid() != 0) {
137                 return 77;
138         }
139
140         // Set up namespaces
141         assert(system("./pmtu-setup") == 0);
142
143         // Bring up the nodes
144         for(int i = 0; i < nnodes; i++) {
145                 assert(meshlink_destroy(nodes[i].confdir));
146
147                 // Open the network namespace
148                 states[i].netns = open(nodes[i].netns, O_RDONLY);
149                 assert(states[i].netns != -1);
150
151                 // Open the MeshLink instance
152                 meshlink_open_params_t *params;
153                 assert(params = meshlink_open_params_init(nodes[i].confdir, nodes[i].name, "pmtu", nodes[i].devclass));
154                 assert(meshlink_open_params_set_netns(params, states[i].netns));
155                 assert(states[i].mesh = meshlink_open_ex(params));
156                 free(params);
157
158                 states[i].mesh->priv = &states[i];
159                 meshlink_enable_discovery(states[i].mesh, false);
160                 init_sync_flag(&states[i].up_flag);
161
162                 meshlink_set_log_cb(states[i].mesh, MESHLINK_DEBUG, parse_log_cb);
163
164                 // Link the relay node to the other nodes
165                 if(i > 0) {
166                         link_meshlink_pair(states[0].mesh, states[i].mesh);
167                 }
168         }
169
170         // Start the relay
171         assert(meshlink_start(states[0].mesh));
172
173         // Start peers and wait for them to connect
174         start_peer_nut();
175
176         // Wait for PMTU discovery to finish
177         wait_for_pmtu();
178
179         assert(states[1].pmtu >= 1400 && states[1].pmtu <= 1500);
180         assert(states[1].probe_count <= 10);
181         assert(states[1].probe_bytes <= 1500 * 10);
182
183         // Drop the MTU to 800
184         stop_peer_nut();
185
186         assert(system("ip netns exec pmtu_p ip link set eth0 mtu 800") == 0);
187         assert(system("ip netns exec pmtu_n ip link set eth0 mtu 800") == 0);
188
189         // Workaround for autoconnect algorithm throttling reconnects
190         sleep(15);
191
192         start_peer_nut();
193         wait_for_pmtu();
194
195         assert(states[1].pmtu >= 700 && states[1].pmtu <= 800);
196         assert(states[1].probe_count <= 20);
197         assert(states[1].probe_bytes <= 800 * 20);
198
199         // Cleanup
200         for(int i = 0; i < nnodes; i++) {
201                 meshlink_close(states[i].mesh);
202         }
203 }