11 #include <sys/types.h>
20 static const struct info {
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},
32 meshlink_handle_t *mesh;
34 struct sync_flag up_flag;
40 static void relay_up_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
41 struct state *state = mesh->priv;
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);
50 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
52 meshlink_channel_close(mesh, channel);
56 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
60 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
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);
69 struct state *state = mesh->priv;
71 if(state->pmtu != 0) {
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")) {
81 state->probe_bytes += len;
83 } else if(sscanf(text, "Fixing PMTU of %9s to %d", name, &len) == 2) {
84 if(!strcmp(name, "nut")) {
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);
96 // While sending regular data, wait for PMTU discovery to finish
97 for(int i = 0; i < 30; i++) {
104 assert(meshlink_channel_send(states[1].mesh, channel, "ping", 4) == 4);
107 meshlink_channel_close(states[1].mesh, channel);
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));
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));
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;
129 states[i].probe_count = 0;
130 states[i].probe_bytes = 0;
135 // This test requires root access due to the use of network namespaces
141 assert(system("./pmtu-setup") == 0);
143 // Bring up the nodes
144 for(int i = 0; i < nnodes; i++) {
145 assert(meshlink_destroy(nodes[i].confdir));
147 // Open the network namespace
148 states[i].netns = open(nodes[i].netns, O_RDONLY);
149 assert(states[i].netns != -1);
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));
158 states[i].mesh->priv = &states[i];
159 meshlink_enable_discovery(states[i].mesh, false);
160 init_sync_flag(&states[i].up_flag);
162 meshlink_set_log_cb(states[i].mesh, MESHLINK_DEBUG, parse_log_cb);
164 // Link the relay node to the other nodes
166 link_meshlink_pair(states[0].mesh, states[i].mesh);
171 assert(meshlink_start(states[0].mesh));
173 // Start peers and wait for them to connect
176 // Wait for PMTU discovery to finish
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);
183 // Drop the MTU to 800
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);
189 // Workaround for autoconnect algorithm throttling reconnects
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);
200 for(int i = 0; i < nnodes; i++) {
201 meshlink_close(states[i].mesh);