2 node_sim_nut.c -- Implementation of Node Simulation for Meshlink Testing
3 for meta connection test case 01 - re-connection of
4 two nodes when relay node goes down
5 Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "../common/common_handlers.h"
29 #include "../common/test_step.h"
30 #include "../common/network_namespace_framework.h"
31 #include "../../utils.h"
32 #include "../run_blackbox_tests/test_optimal_pmtu.h"
34 extern bool test_pmtu_nut_running;
35 extern bool test_pmtu_peer_running;
36 extern bool test_pmtu_relay_running;
37 extern struct sync_flag test_pmtu_nut_closed;
38 extern bool ping_channel_enable_07;
40 static struct sync_flag peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
41 static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
43 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
45 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
47 pmtu_attr_t node_pmtu[2];
48 static time_t node_shutdown_time = 0;
49 static bool mtu_set = true;
51 static void print_mtu_calc(pmtu_attr_t node_pmtu) {
52 fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
53 fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
54 fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
55 fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
56 fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
57 fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
60 if(node_pmtu.mtu_ping.probes) {
61 avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
64 fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
65 fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
66 fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
69 // Node status callback
70 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
73 // Signal pthread_cond_wait if peer is reachable
74 if(!strcasecmp(node->name, "peer") && reachable) {
75 set_sync_flag(&peer_reachable, true);
81 // Channel poll callback
82 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
84 meshlink_set_channel_poll_cb(mesh, channel, NULL);
86 // Send data via channel to trigger UDP peer to peer hole punching
87 assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
91 /* channel receive callback */
92 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
98 if(!strcmp(channel->node->name, "peer")) {
99 if(!memcmp(dat, "reply", 5)) {
100 set_sync_flag(&channel_opened, true);
101 } else if(!memcmp(dat, "test", 5)) {
102 assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
109 // Meshlink log handler
110 static void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
121 time_t probe_interval;
123 cur_time = time(NULL);
124 assert(cur_time != -1);
126 if(node_shutdown_time && cur_time >= node_shutdown_time) {
127 test_pmtu_nut_running = false;
130 if(level == MESHLINK_INFO) {
131 fprintf(stderr, "\x1b[32m nut:\x1b[0m %s\n", text);
134 /* Calculate the MTU parameter values from the meshlink logs */
135 if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
136 find_node_index(i, node_name);
137 node_pmtu[i].mtu_sent_probes.probes += 1;
138 node_pmtu[i].mtu_sent_probes.probes_total_len += probe_len;
140 if(node_pmtu[i].mtu_size) {
141 if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
142 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
144 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
147 node_pmtu[i].mtu_ping.probes += 1;
148 node_pmtu[i].mtu_ping.time += probe_interval;
149 node_pmtu[i].mtu_ping.probes_total_len += probe_len;
152 node_pmtu[i].mtu_sent_probes.time = cur_time;
154 } else if(sscanf(text, "Got MTU probe length %d from %s", &probe_len, node_name) == 2) {
155 find_node_index(i, node_name);
156 node_pmtu[i].mtu_recv_probes.probes += 1;
157 node_pmtu[i].mtu_recv_probes.probes_total_len += probe_len;
159 if(node_pmtu[i].mtu_size) {
160 if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
161 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
163 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
166 node_pmtu[i].mtu_ping.probes += 1;
167 node_pmtu[i].mtu_ping.time += probe_interval;
168 node_pmtu[i].mtu_ping.probes_total_len += probe_len;
171 node_pmtu[i].mtu_recv_probes.time = cur_time;
173 } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
175 if(!node_shutdown_time && !strcasecmp("peer", node_name) && mtu_set) {
176 node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
180 find_node_index(i, node_name);
181 node_pmtu[i].mtu_discovery.probes = node_pmtu[i].mtu_recv_probes.probes + node_pmtu[i].mtu_sent_probes.probes;
182 node_pmtu[i].mtu_discovery.probes_total_len = node_pmtu[i].mtu_sent_probes.probes_total_len + node_pmtu[i].mtu_recv_probes.probes_total_len;
183 node_pmtu[i].mtu_discovery.time = cur_time - node_pmtu[i].mtu_start.time;
184 node_pmtu[i].mtu_discovery.count += 1;
185 node_pmtu[i].mtu_size = mtu_len;
187 } else if(sscanf(text, "SPTPS key exchange with %s successful", node_name) == 1) {
188 find_node_index(i, node_name);
189 node_pmtu[i].mtu_start.time = cur_time;
190 node_pmtu[i].mtu_start.count += 1;
191 memset(&node_pmtu[i].mtu_discovery, 0, sizeof(struct pmtu_attr_para));
192 memset(&node_pmtu[i].mtu_ping, 0, sizeof(struct pmtu_attr_para));
193 memset(&node_pmtu[i].mtu_increase, 0, sizeof(struct pmtu_attr_para));
195 } else if(sscanf(text, "Increase in PMTU to %s detected, restarting PMTU discovery", node_name) == 1) {
196 find_node_index(i, node_name);
197 node_pmtu[i].mtu_increase.time = cur_time - node_pmtu[i].mtu_start.time;
198 node_pmtu[i].mtu_increase.count += 1;
200 } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
202 } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
204 } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
206 } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
208 } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
212 void *node_sim_pmtu_nut_01(void *arg) {
213 mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
214 struct timeval main_loop_wait = { 5, 0 };
216 set_sync_flag(&peer_reachable, false);
217 set_sync_flag(&channel_opened, false);
218 node_shutdown_time = 0;
221 // Run relay node instance
223 meshlink_handle_t *mesh;
224 mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
226 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
227 meshlink_set_node_status_cb(mesh, node_status_cb);
230 // Join relay node and if fails to join then try few more attempts
232 if(mesh_arg->join_invitation) {
236 for(attempts = 0; attempts < 10; attempts++) {
237 join_ret = meshlink_join(mesh, mesh_arg->join_invitation);
251 assert(meshlink_start(mesh));
253 // Wait for peer node to join
255 assert(wait_sync_flag(&peer_reachable, 10));
257 // Open a channel to peer node
259 meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
261 meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
262 channel_receive_cb, NULL, 0);
263 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
265 assert(wait_sync_flag(&channel_opened, 30));
267 // All test steps executed - wait for signals to stop/start or close the mesh
269 time_t time_stamp, send_time;
271 time_stamp = time(NULL);
272 send_time = time_stamp + 10;
274 while(test_pmtu_nut_running) {
275 select(1, NULL, NULL, NULL, &main_loop_wait);
277 // Ping the channel for every 10 seconds if ping_channel_enable_07 is enabled
279 if(ping_channel_enable_07) {
280 time_stamp = time(NULL);
282 if(time_stamp >= send_time) {
283 send_time = time_stamp + 10;
284 meshlink_channel_send(mesh, channel, "ping", 5);
289 // Send MTU probe parameters data to the test driver
290 meshlink_close(mesh);
292 set_sync_flag(&test_pmtu_nut_closed, true);
293 fprintf(stderr, "NODE_PMTU_PEER :\n");
294 print_mtu_calc(node_pmtu[NODE_PMTU_PEER]);
295 fprintf(stderr, "\nNODE_PMTU_RELAY :\n");
296 print_mtu_calc(node_pmtu[NODE_PMTU_RELAY]);