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