]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c
Fix compiler warnings in the test suites.
[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
47 pmtu_attr_t node_pmtu[2];
48 static time_t node_shutdown_time = 0;
49 static bool mtu_set = true;
50
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);
58         float avg = 0;
59
60         if(node_pmtu.mtu_ping.probes) {
61                 avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
62         }
63
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);
67 }
68
69 // Node status callback
70 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
71         (void)mesh;
72
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 /* channel receive callback */
92 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
93         if(len == 0) {
94                 fail();
95                 return;
96         }
97
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);
103                 }
104         }
105
106         return;
107 }
108
109 // Meshlink log handler
110 static void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
111                             const char *text) {
112         (void)mesh;
113         (void)level;
114         int probe_len;
115         int mtu_len;
116         int probes;
117         char node_name[100];
118         int i = -1;
119
120         time_t cur_time;
121         time_t probe_interval;
122
123         cur_time = time(NULL);
124         assert(cur_time != -1);
125
126         if(node_shutdown_time && cur_time >= node_shutdown_time) {
127                 test_pmtu_nut_running = false;
128         }
129
130         if(level == MESHLINK_INFO) {
131                 fprintf(stderr, "\x1b[32m nut:\x1b[0m %s\n", text);
132         }
133
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;
139
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;
143                         } else {
144                                 probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
145                         }
146
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;
150                 }
151
152                 node_pmtu[i].mtu_sent_probes.time = cur_time;
153
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;
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_recv_probes.time = cur_time;
172
173         } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
174
175                 if(!node_shutdown_time && !strcasecmp("peer", node_name) && mtu_set) {
176                         node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
177                         mtu_set = false;
178                 }
179
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;
186
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));
194
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;
199
200         } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
201
202         } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
203
204         } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
205
206         } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
207
208         } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
209         }
210 }
211
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 };
215
216         set_sync_flag(&peer_reachable, false);
217         set_sync_flag(&channel_opened, false);
218         node_shutdown_time = 0;
219         mtu_set = true;
220
221         // Run relay node instance
222
223         meshlink_handle_t *mesh;
224         mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
225         assert(mesh);
226         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
227         meshlink_set_node_status_cb(mesh, node_status_cb);
228         sleep(1);
229
230         // Join relay node and if fails to join then try few more attempts
231
232         if(mesh_arg->join_invitation) {
233                 int attempts;
234                 bool join_ret;
235
236                 for(attempts = 0; attempts < 10; attempts++) {
237                         join_ret = meshlink_join(mesh, mesh_arg->join_invitation);
238
239                         if(join_ret) {
240                                 break;
241                         }
242
243                         sleep(1);
244                 }
245
246                 if(attempts == 10) {
247                         fail();
248                 }
249         }
250
251         assert(meshlink_start(mesh));
252
253         // Wait for peer node to join
254
255         assert(wait_sync_flag(&peer_reachable, 10));
256
257         // Open a channel to peer node
258
259         meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
260         assert(peer_node);
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);
264
265         assert(wait_sync_flag(&channel_opened, 30));
266
267         // All test steps executed - wait for signals to stop/start or close the mesh
268
269         time_t time_stamp, send_time;
270
271         time_stamp = time(NULL);
272         send_time = time_stamp + 10;
273
274         while(test_pmtu_nut_running) {
275                 select(1, NULL, NULL, NULL, &main_loop_wait);
276
277                 // Ping the channel for every 10 seconds if ping_channel_enable_07 is enabled
278
279                 if(ping_channel_enable_07) {
280                         time_stamp = time(NULL);
281
282                         if(time_stamp >= send_time) {
283                                 send_time = time_stamp + 10;
284                                 meshlink_channel_send(mesh, channel, "ping", 5);
285                         }
286                 }
287         }
288
289         // Send MTU probe parameters data to the test driver
290         meshlink_close(mesh);
291
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]);
297
298         return NULL;
299 }