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