]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c
Add test cases for the PMTU discovery mechanism.
[meshlink] / test / blackbox / test_case_optimal_pmtu_01 / node_sim_nut.c
1 /*
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>
6
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.
11
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.
16
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.
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25 #include <assert.h>
26 #include <signal.h>
27 #include <time.h>
28 #include "../common/common_handlers.h"
29 #include "../common/test_step.h"
30 #include "../common/mesh_event_handler.h"
31 #include "../../utils.h"
32 #include "../run_blackbox_tests/test_optimal_pmtu.h"
33
34 #define CMD_LINE_ARG_NODENAME   1
35 #define CMD_LINE_ARG_DEVCLASS   2
36 #define CMD_LINE_ARG_CLIENTID   3
37 #define CMD_LINE_ARG_IMPORTSTR  4
38 #define CMD_LINE_ARG_INVITEURL  5
39 #define CHANNEL_PORT 1234
40
41 #pragma pack(1)
42
43 static int client_id = -1;
44
45 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
46 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
47
48 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
49                            bool reachable);
50 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
51 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
52
53 static pmtu_attr_t node_pmtu[2];
54
55 static void print_mtu_calc(pmtu_attr_t node_pmtu) {
56         fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
57         fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
58         fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
59         fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
60         fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
61         fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
62         float avg = 0;
63
64         if(node_pmtu.mtu_ping.probes) {
65                 avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
66         }
67
68         fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
69         fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
70         fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
71 }
72
73 // Node status callback
74 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
75                            bool reachable) {
76         // Signal pthread_cond_wait if peer is reachable
77         if(!strcasecmp(node->name, "peer") && reachable) {
78                 set_sync_flag(&peer_reachable, true);
79         }
80
81         // Notify the JOIN or LEFT event of a node to the test driver
82         mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
83         return;
84 }
85
86 // Channel poll callback
87 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
88         (void)len;
89         meshlink_set_channel_poll_cb(mesh, channel, NULL);
90
91         // Send data via channel to trigger UDP peer to peer hole punching
92         assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
93         return;
94 }
95
96 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
97         (void)dat;
98         (void)len;
99
100         assert(port == CHANNEL_PORT);
101
102         // If the channel is from peer node set receive callback for it else reject the channel
103         if(!strcmp(channel->node->name, "peer")) {
104                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
105                 mesh->priv = channel;
106
107                 return true;
108         }
109
110         return false;
111 }
112
113 /* channel receive callback */
114 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
115         if(len == 0) {
116                 mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
117                 return;
118         }
119
120         if(!strcmp(channel->node->name, "peer")) {
121                 if(!memcmp(dat, "reply", 5)) {
122                         set_sync_flag(&channel_opened, true);
123                         fprintf(stderr, "GOT REPLY FROM PEER\n");
124                 } else if(!memcmp(dat, "test", 5)) {
125                         assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
126                 }
127         }
128
129         return;
130 }
131
132 // Meshlink log handler
133 void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
134                      const char *text) {
135         (void)mesh;
136         (void)level;
137         int probe_len;
138         int mtu_len;
139         int probes;
140         char node_name[100];
141         int i = -1;
142
143         time_t cur_time;
144         time_t probe_interval;
145
146         cur_time = time(NULL);
147         assert(cur_time != -1);
148
149         static time_t node_shutdown_time = 0;
150         bool mtu_probe = false;
151
152         if(node_shutdown_time && cur_time >= node_shutdown_time) {
153                 test_running = false;
154         }
155
156         static const char *levelstr[] = {
157                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
158                 [MESHLINK_INFO] = "\x1b[32mINFO",
159                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
160                 [MESHLINK_ERROR] = "\x1b[31mERROR",
161                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
162         };
163
164         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
165
166         /* Calculate the MTU parameter values from the meshlink logs */
167         if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
168                 find_node_index(i, node_name);
169                 node_pmtu[i].mtu_sent_probes.probes += 1;
170                 node_pmtu[i].mtu_sent_probes.probes_total_len += probe_len;
171
172                 if(node_pmtu[i].mtu_size) {
173                         if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
174                                 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
175                         } else {
176                                 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
177                         }
178
179                         node_pmtu[i].mtu_ping.probes += 1;
180                         node_pmtu[i].mtu_ping.time += probe_interval;
181                         node_pmtu[i].mtu_ping.probes_total_len += probe_len;
182                 }
183
184                 node_pmtu[i].mtu_sent_probes.time = cur_time;
185
186         } else if(sscanf(text, "Got MTU probe length %d from %s", &probe_len, node_name) == 2) {
187                 find_node_index(i, node_name);
188                 node_pmtu[i].mtu_recv_probes.probes += 1;
189                 node_pmtu[i].mtu_recv_probes.probes_total_len += probe_len;
190
191                 if(node_pmtu[i].mtu_size) {
192                         if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
193                                 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
194                         } else {
195                                 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
196                         }
197
198                         node_pmtu[i].mtu_ping.probes += 1;
199                         node_pmtu[i].mtu_ping.time += probe_interval;
200                         node_pmtu[i].mtu_ping.probes_total_len += probe_len;
201                 }
202
203                 node_pmtu[i].mtu_recv_probes.time = cur_time;
204
205         } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
206                 static bool mtu_set = true;
207
208                 if(!node_shutdown_time && !strcasecmp("relay", node_name) && mtu_set) {
209                         node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
210                         mtu_set = false;
211                 }
212
213                 find_node_index(i, node_name);
214                 node_pmtu[i].mtu_discovery.probes = node_pmtu[i].mtu_recv_probes.probes + node_pmtu[i].mtu_sent_probes.probes;
215                 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;
216                 node_pmtu[i].mtu_discovery.time = cur_time - node_pmtu[i].mtu_start.time;
217                 node_pmtu[i].mtu_discovery.count += 1;
218                 node_pmtu[i].mtu_size = mtu_len;
219
220         } else if(sscanf(text, "SPTPS key exchange with %s succesful", node_name) == 1) {
221                 find_node_index(i, node_name);
222                 node_pmtu[i].mtu_start.time = cur_time;
223                 node_pmtu[i].mtu_start.count += 1;
224                 memset(&node_pmtu[i].mtu_discovery, 0, sizeof(struct pmtu_attr_para));
225                 memset(&node_pmtu[i].mtu_ping, 0, sizeof(struct pmtu_attr_para));
226                 memset(&node_pmtu[i].mtu_increase, 0, sizeof(struct pmtu_attr_para));
227
228         } else if(sscanf(text, "Increase in PMTU to %s detected, restarting PMTU discovery", node_name) == 1) {
229                 find_node_index(i, node_name);
230                 node_pmtu[i].mtu_increase.time = cur_time - node_pmtu[i].mtu_start.time;
231                 node_pmtu[i].mtu_increase.count += 1;
232
233         } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
234
235         } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
236
237         } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
238
239         } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
240                 mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, node_name, sizeof(node_name));
241
242         } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
243                 mesh_event_sock_send(client_id, META_CONN_CLOSED, node_name, sizeof(node_name));
244
245         }
246 }
247
248 int main(int argc, char *argv[]) {
249         struct timeval main_loop_wait = { 5, 0 };
250         int i;
251
252         // Import mesh event handler
253
254         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
255                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
256                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
257         }
258
259         setup_signals();
260
261         // Execute test steps
262
263         meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
264                                                 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
265         assert(mesh);
266         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
267         meshlink_set_node_status_cb(mesh, node_status_cb);
268         meshlink_enable_discovery(mesh, false);
269         sleep(1);
270
271         // Join relay node and if fails to join then try few more attempts
272
273         if(argv[CMD_LINE_ARG_INVITEURL]) {
274                 int attempts;
275                 bool join_ret;
276
277                 for(attempts = 0; attempts < 10; attempts++) {
278                         join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
279
280                         if(join_ret) {
281                                 break;
282                         }
283
284                         sleep(1);
285                 }
286
287                 if(attempts == 10) {
288                         abort();
289                 }
290         }
291
292         assert(meshlink_start(mesh));
293
294         // Wait for peer node to join
295
296         assert(wait_sync_flag(&peer_reachable, 10));
297
298         // Open a channel to peer node
299
300         meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
301         assert(peer_node);
302         meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
303                                       channel_receive_cb, NULL, 0);
304         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
305
306         assert(wait_sync_flag(&channel_opened, 30));
307         assert(mesh_event_sock_send(client_id, CHANNEL_OPENED, NULL, 0));
308
309         // All test steps executed - wait for signals to stop/start or close the mesh
310
311         while(test_running) {
312                 select(1, NULL, NULL, NULL, &main_loop_wait);
313         }
314
315         // Send MTU probe parameters data to the test driver
316
317         pmtu_attr_t send_mtu_data;
318         send_mtu_data = node_pmtu[NODE_PMTU_PEER];
319         print_mtu_calc(send_mtu_data);
320         assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_PEER, &send_mtu_data, sizeof(send_mtu_data)));
321         send_mtu_data = node_pmtu[NODE_PMTU_RELAY];
322         print_mtu_calc(send_mtu_data);
323         assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_RELAY, &send_mtu_data, sizeof(send_mtu_data)));
324
325         meshlink_close(mesh);
326 }