]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[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
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/network_namespace_framework.h"
36 #include "../../utils.h"
37 #include "../run_blackbox_tests/test_optimal_pmtu.h"
38
39 extern bool test_pmtu_nut_running;
40 extern bool test_pmtu_peer_running;
41 extern bool test_pmtu_relay_running;
42 extern struct sync_flag test_pmtu_nut_closed;
43 extern bool ping_channel_enable_07;
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
52 pmtu_attr_t node_pmtu[2];
53 static time_t node_shutdown_time = 0;
54 static bool mtu_set = true;
55
56 static void print_mtu_calc(pmtu_attr_t node_pmtu) {
57         fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
58         fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
59         fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
60         fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
61         fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
62         fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
63         float avg = 0;
64
65         if(node_pmtu.mtu_ping.probes) {
66                 avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
67         }
68
69         fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
70         fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
71         fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
72 }
73
74 // Node status callback
75 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
76         (void)mesh;
77
78         // Signal pthread_cond_wait if peer is reachable
79         if(!strcasecmp(node->name, "peer") && reachable) {
80                 set_sync_flag(&peer_reachable, true);
81         }
82
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 /* channel receive callback */
97 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
98         if(len == 0) {
99                 fail();
100                 return;
101         }
102
103         if(!strcmp(channel->node->name, "peer")) {
104                 if(!memcmp(dat, "reply", 5)) {
105                         set_sync_flag(&channel_opened, true);
106                 } else if(!memcmp(dat, "test", 5)) {
107                         assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
108                 }
109         }
110
111         return;
112 }
113
114 // Meshlink log handler
115 static void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
116                             const char *text) {
117         (void)mesh;
118         (void)level;
119         int probe_len;
120         int mtu_len;
121         int probes;
122         char node_name[100];
123         int i = -1;
124
125         time_t cur_time;
126         time_t probe_interval;
127
128         cur_time = time(NULL);
129         assert(cur_time != -1);
130
131         if(node_shutdown_time && cur_time >= node_shutdown_time) {
132                 test_pmtu_nut_running = false;
133         }
134
135         if(level == MESHLINK_INFO) {
136                 fprintf(stderr, "\x1b[32m nut:\x1b[0m %s\n", text);
137         }
138
139         /* Calculate the MTU parameter values from the meshlink logs */
140         if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
141                 find_node_index(i, node_name);
142                 node_pmtu[i].mtu_sent_probes.probes += 1;
143                 node_pmtu[i].mtu_sent_probes.probes_total_len += probe_len;
144
145                 if(node_pmtu[i].mtu_size) {
146                         if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
147                                 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
148                         } else {
149                                 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
150                         }
151
152                         node_pmtu[i].mtu_ping.probes += 1;
153                         node_pmtu[i].mtu_ping.time += probe_interval;
154                         node_pmtu[i].mtu_ping.probes_total_len += probe_len;
155                 }
156
157                 node_pmtu[i].mtu_sent_probes.time = cur_time;
158
159         } else if(sscanf(text, "Got MTU probe length %d from %s", &probe_len, node_name) == 2) {
160                 find_node_index(i, node_name);
161                 node_pmtu[i].mtu_recv_probes.probes += 1;
162                 node_pmtu[i].mtu_recv_probes.probes_total_len += probe_len;
163
164                 if(node_pmtu[i].mtu_size) {
165                         if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
166                                 probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
167                         } else {
168                                 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
169                         }
170
171                         node_pmtu[i].mtu_ping.probes += 1;
172                         node_pmtu[i].mtu_ping.time += probe_interval;
173                         node_pmtu[i].mtu_ping.probes_total_len += probe_len;
174                 }
175
176                 node_pmtu[i].mtu_recv_probes.time = cur_time;
177
178         } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
179
180                 if(!node_shutdown_time && !strcasecmp("peer", node_name) && mtu_set) {
181                         node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
182                         mtu_set = false;
183                 }
184
185                 find_node_index(i, node_name);
186                 node_pmtu[i].mtu_discovery.probes = node_pmtu[i].mtu_recv_probes.probes + node_pmtu[i].mtu_sent_probes.probes;
187                 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;
188                 node_pmtu[i].mtu_discovery.time = cur_time - node_pmtu[i].mtu_start.time;
189                 node_pmtu[i].mtu_discovery.count += 1;
190                 node_pmtu[i].mtu_size = mtu_len;
191
192         } else if(sscanf(text, "SPTPS key exchange with %s successful", node_name) == 1) {
193                 find_node_index(i, node_name);
194                 node_pmtu[i].mtu_start.time = cur_time;
195                 node_pmtu[i].mtu_start.count += 1;
196                 memset(&node_pmtu[i].mtu_discovery, 0, sizeof(struct pmtu_attr_para));
197                 memset(&node_pmtu[i].mtu_ping, 0, sizeof(struct pmtu_attr_para));
198                 memset(&node_pmtu[i].mtu_increase, 0, sizeof(struct pmtu_attr_para));
199
200         } else if(sscanf(text, "Increase in PMTU to %s detected, restarting PMTU discovery", node_name) == 1) {
201                 find_node_index(i, node_name);
202                 node_pmtu[i].mtu_increase.time = cur_time - node_pmtu[i].mtu_start.time;
203                 node_pmtu[i].mtu_increase.count += 1;
204
205         } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
206
207         } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
208
209         } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
210
211         } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
212
213         } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
214         }
215 }
216
217 void *node_sim_pmtu_nut_01(void *arg) {
218         mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
219         struct timeval main_loop_wait = { 5, 0 };
220
221         set_sync_flag(&peer_reachable, false);
222         set_sync_flag(&channel_opened, false);
223         node_shutdown_time = 0;
224         mtu_set = true;
225
226         // Run relay node instance
227
228         meshlink_handle_t *mesh;
229         mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
230         assert(mesh);
231         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
232         meshlink_set_node_status_cb(mesh, node_status_cb);
233         sleep(1);
234
235         // Join relay node and if fails to join then try few more attempts
236
237         if(mesh_arg->join_invitation) {
238                 int attempts;
239                 bool join_ret;
240
241                 for(attempts = 0; attempts < 10; attempts++) {
242                         join_ret = meshlink_join(mesh, mesh_arg->join_invitation);
243
244                         if(join_ret) {
245                                 break;
246                         }
247
248                         sleep(1);
249                 }
250
251                 if(attempts == 10) {
252                         fail();
253                 }
254         }
255
256         assert(meshlink_start(mesh));
257
258         // Wait for peer node to join
259
260         assert(wait_sync_flag(&peer_reachable, 10));
261
262         // Open a channel to peer node
263
264         meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
265         assert(peer_node);
266         meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
267                                       channel_receive_cb, NULL, 0);
268         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
269
270         assert(wait_sync_flag(&channel_opened, 30));
271
272         // All test steps executed - wait for signals to stop/start or close the mesh
273
274         time_t time_stamp, send_time;
275
276         time_stamp = time(NULL);
277         send_time = time_stamp + 10;
278
279         while(test_pmtu_nut_running) {
280                 select(1, NULL, NULL, NULL, &main_loop_wait);
281
282                 // Ping the channel for every 10 seconds if ping_channel_enable_07 is enabled
283
284                 if(ping_channel_enable_07) {
285                         time_stamp = time(NULL);
286
287                         if(time_stamp >= send_time) {
288                                 send_time = time_stamp + 10;
289                                 assert(meshlink_channel_send(mesh, channel, "ping", 5) == 5);
290                         }
291                 }
292         }
293
294         // Send MTU probe parameters data to the test driver
295         meshlink_close(mesh);
296
297         set_sync_flag(&test_pmtu_nut_closed, true);
298         fprintf(stderr, "NODE_PMTU_PEER :\n");
299         print_mtu_calc(node_pmtu[NODE_PMTU_PEER]);
300         fprintf(stderr, "\nNODE_PMTU_RELAY :\n");
301         print_mtu_calc(node_pmtu[NODE_PMTU_RELAY]);
302
303         return NULL;
304 }