first step to remove plibc
[oweals/gnunet.git] / src / fragmentation / test_fragmentation_parallel.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2004, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file fragmentation/test_fragmentation.c
22  * @brief test for fragmentation.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fragmentation_lib.h"
27
28 #define DETAILS GNUNET_NO
29
30 /**
31  * Number of messages to transmit (note: each uses ~32k memory!)
32  */
33 #define NUM_MSGS 500
34
35 /**
36  * MTU to force on fragmentation (must be > 1k + 12)
37  */
38 #define MTU 1111
39
40 /**
41  * Simulate dropping of 1 out of how many messages? (must be > 1)
42  */
43 #define DROPRATE 5
44
45 static int ret = 1;
46
47 static unsigned int dups;
48
49 static unsigned int fragc;
50
51 static unsigned int frag_drops;
52
53 static unsigned int acks;
54
55 static unsigned int ack_drops;
56
57 static struct GNUNET_DEFRAGMENT_Context *defrag;
58
59 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
60
61 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
62
63 static struct GNUNET_SCHEDULER_Task *shutdown_task;
64
65
66 static void
67 do_shutdown (void *cls)
68 {
69   unsigned int i;
70
71   ret = 0;
72   shutdown_task = NULL;
73   GNUNET_DEFRAGMENT_context_destroy (defrag);
74   defrag = NULL;
75   for (i = 0; i < NUM_MSGS; i++)
76   {
77     if (frags[i] == NULL)
78       continue;
79     GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
80     frags[i] = NULL;
81   }
82 }
83
84
85 static void
86 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
87 {
88   static unsigned int total;
89   unsigned int i;
90   const char *buf;
91
92 #if DETAILS
93   fprintf (stderr, "%s",  "!");        /* message complete, good! */
94 #endif
95   buf = (const char *) hdr;
96   for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
97     GNUNET_assert (buf[i] == (char) i);
98   total++;
99 #if ! DETAILS
100   if (0 == (total % (NUM_MSGS / 100)))
101     fprintf (stderr, "%s",  ".");
102 #endif
103   /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
104   if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
105   {
106     if (NULL == shutdown_task)
107       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
108   }
109 }
110
111
112 /**
113  * Process ACK (by passing to fragmenter)
114  */
115 static void
116 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
117 {
118   unsigned int i;
119   int ret;
120
121   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
122   {
123     ack_drops++;
124     return;                     /* random drop */
125   }
126   for (i = 0; i < NUM_MSGS; i++)
127   {
128     if (frags[i] == NULL)
129       continue;
130     ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
131     if (ret == GNUNET_OK)
132     {
133 #if DETAILS
134       fprintf (stderr, "%s",  "@");    /* good ACK */
135 #endif
136       GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
137       frags[i] = NULL;
138       acks++;
139       return;
140     }
141     if (ret == GNUNET_NO)
142     {
143 #if DETAILS
144       fprintf (stderr, "%s",  "@");    /* good ACK */
145 #endif
146       acks++;
147       return;
148     }
149   }
150 #if DETAILS
151   fprintf (stderr, "%s",  "_");        /* BAD: ack that nobody feels responsible for... */
152 #endif
153 }
154
155
156 /**
157  * Process fragment (by passing to defrag).
158  */
159 static void
160 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
161 {
162   struct GNUNET_FRAGMENT_Context **fc = cls;
163   int ret;
164
165   GNUNET_FRAGMENT_context_transmission_done (*fc);
166   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
167   {
168     frag_drops++;
169     return;                     /* random drop */
170   }
171   if (NULL == defrag)
172   {
173     fprintf (stderr, "%s",  "E");      /* Error: frag after shutdown!? */
174     return;
175   }
176   ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
177   if (ret == GNUNET_NO)
178   {
179 #if DETAILS
180     fprintf (stderr, "%s",  "?");      /* duplicate fragment */
181 #endif
182     dups++;
183   }
184   else if (ret == GNUNET_OK)
185   {
186 #if DETAILS
187     fprintf (stderr, "%s",  ".");      /* good fragment */
188 #endif
189     fragc++;
190   }
191 }
192
193
194 /**
195  * Main function run with scheduler.
196  */
197 static void
198 run (void *cls, char *const *args, const char *cfgfile,
199      const struct GNUNET_CONFIGURATION_Handle *cfg)
200 {
201   unsigned int i;
202   struct GNUNET_MessageHeader *msg;
203   char buf[MTU + 32 * 1024];
204
205   defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS        /* enough space for all */
206                                              , NULL, &proc_msgs, &proc_acks);
207   for (i = 0; i < sizeof (buf); i++)
208     buf[i] = (char) i;
209   msg = (struct GNUNET_MessageHeader *) buf;
210   for (i = 0; i < NUM_MSGS; i++)
211   {
212     msg->type = htons ((uint16_t) i);
213     msg->size =
214         htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
215     frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
216                                                MTU, &trackers[i],
217                                                GNUNET_TIME_UNIT_MILLISECONDS,
218                                                GNUNET_TIME_UNIT_SECONDS,
219                                                msg,
220                                                &proc_frac, &frags[i]);
221   }
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   struct GNUNET_GETOPT_CommandLineOption options[] = {
229     GNUNET_GETOPT_OPTION_END
230   };
231   char *const argv_prog[] = {
232     "test-fragmentation",
233     "-c",
234     "test_fragmentation_data.conf",
235     "-L",
236     "WARNING",
237     NULL
238   };
239   unsigned int i;
240
241   GNUNET_log_setup ("test-fragmentation",
242                     "WARNING",
243                     NULL);
244   for (i = 0; i < NUM_MSGS; i++)
245     GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
246                                    GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
247                                    100);
248   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
249                       &run, NULL);
250   fprintf (stderr,
251            "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
252            fragc, dups, acks, ack_drops);
253   return ret;
254 }