13 #include "netns_utils.h"
16 static struct sync_flag peer_reachable;
17 static struct sync_flag aio_done;
18 static const size_t size = 10000000;
21 static void nut_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
24 if(reachable && !strcmp(node->name, "peer")) {
25 set_sync_flag(&peer_reachable, true);
29 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
36 set_sync_flag(&aio_done, true);
39 static void peer_aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
46 meshlink_channel_close(mesh, channel);
49 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
55 assert(meshlink_channel_aio_receive(mesh, channel, buffer, size, peer_aio_cb, NULL));
59 static void print_counters(peer_config_t *peers, const char *description) {
60 printf("%s:\n", description);
61 printf(" %9s %9s %9s %9s %9s %9s\n",
69 for(int i = 0; i < 3; i++) {
70 meshlink_node_t *node = meshlink_get_node(peers[0].mesh, peers[i].name);
72 struct devtool_node_status status;
73 devtool_reset_node_counters(peers[0].mesh, node, &status);
74 printf(" %5s: %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 "\n",
87 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
89 init_sync_flag(&peer_reachable);
90 init_sync_flag(&aio_done);
92 // Set up relay, peer and NUT
93 peer_config_t *peers = setup_relay_peer_nut("metering-tcponly");
94 set_peers_tcponly(peers, 3);
96 meshlink_set_node_status_cb(peers[2].mesh, nut_status_cb);
98 for(int i = 0; i < 3; i++) {
99 assert(meshlink_start(peers[i].mesh));
102 // Measure traffic after 1 minute of PMTU probing
104 print_counters(peers, "PMTU probing (1 min)");
106 // Measure traffic after 1 minute of idle
107 for(int i = 0; i < 10; i++) {
109 print_counters(peers, "Idle (1 min)");
112 // Measure channel traffic between relay and peer
113 buffer = calloc(1, size);
115 meshlink_node_t *peer = meshlink_get_node(peers[0].mesh, peers[1].name);
117 meshlink_set_channel_accept_cb(peers[1].mesh, accept_cb);
118 meshlink_channel_t *channel = meshlink_channel_open(peers[0].mesh, peer, 1, NULL, NULL, 0);
120 assert(meshlink_channel_aio_send(peers[0].mesh, channel, buffer, size, aio_cb, NULL));
121 assert(wait_sync_flag(&aio_done, 15));
122 meshlink_channel_close(peers[0].mesh, channel);
124 print_counters(peers, "relay->peer channel traffic");
126 // Measure channel traffic between NUT and peer
127 assert(wait_sync_flag(&peer_reachable, 5));
128 meshlink_node_t *nut = meshlink_get_node(peers[0].mesh, peers[2].name);
130 peer = meshlink_get_node(peers[2].mesh, peers[1].name);
132 channel = meshlink_channel_open(peers[2].mesh, peer, 1, NULL, NULL, 0);
134 init_sync_flag(&aio_done);
135 assert(meshlink_channel_aio_send(peers[2].mesh, channel, buffer, size, aio_cb, NULL));
136 assert(wait_sync_flag(&aio_done, 15));
137 meshlink_channel_close(peers[2].mesh, channel);
139 print_counters(peers, "NUT->peer channel traffic");
141 close_relay_peer_nut(peers);