-remove debug message
[oweals/gnunet.git] / src / testbed / testbed_api_underlay.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2008--2013 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 /**
22  * @file testbed/testbed_api_underlay.c
23  * @brief testbed underlay API implementation
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27 #include "testbed_api_peers.h"
28
29
30 /**
31  * An underlay link
32  */
33 struct LinkProperty
34 {
35   /**
36    * next pointer for list
37    */
38   struct LinkProperty *next;
39
40   /**
41    * the peer whose link is defined by these properties
42    */
43   struct GNUNET_TESTBED_Peer *peer;
44
45   /**
46    * latency of the link in microseconds
47    */
48   uint32_t latency;
49
50   /**
51    * data loss on the link expressed as percentage
52    */
53   uint32_t loss;
54
55   /**
56    * bandwidth of the link in kilobytes per second
57    */
58   uint32_t bandwidth;
59 };
60
61
62 /**
63  * Container for holding a peer in whitelist/blacklist
64  */
65 struct ListEntry
66 {
67   /**
68    * the next pointer
69    */
70   struct ListEntry *next;
71
72   /**
73    * the peer
74    */
75   struct GNUNET_TESTBED_Peer *peer;
76 };
77
78
79 /**
80  * Model for configuring underlay links of a peer
81  * @ingroup underlay
82  */
83 struct GNUNET_TESTBED_UnderlayLinkModel
84 {
85   /**
86    * The peer associated with this model
87    */
88   struct GNUNET_TESTBED_Peer *peer;
89
90   /**
91    * List of peers in the list
92    */
93   struct ListEntry *entries;
94
95   /**
96    * list of link properties
97    */
98   struct LinkProperty *props;
99
100   /**
101    * the type of this model
102    */
103   enum GNUNET_TESTBED_UnderlayLinkModelType type;
104 }
105
106
107 /**
108  * Function to free resources of list entries
109  *
110  * @param model the model
111  */
112 static void
113 free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
114 {
115   struct ListEntry *e;
116
117   while (NULL != (e = model->entries))
118   {
119     model->entries = e->next;
120     GNUNET_free (e);
121   }
122 }
123
124
125 /**
126  * Function to free resources of link properties added to the given model
127  *
128  * @param model the model
129  */
130 static void
131 free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
132 {
133   struct LinkProperty *p;
134
135   while (NULL != (p = model->props))
136   {
137     model->props = p->next;
138     GNUNET_free (p);
139   }
140 }
141
142
143 /**
144  * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer.  A peer can
145  * have ONLY ONE model and it can be either a blacklist or whitelist based one.
146  *
147  * @ingroup underlay
148  * @param peer the peer for which the model has to be created
149  * @param type the type of the model
150  * @return the model
151  */
152 struct GNUNET_TESTBED_UnderlayLinkModel *
153 GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
154                                          enum
155                                          GNUNET_TESTBED_UnderlayLinkModelType
156                                          type)
157 {
158   struct GNUNET_TESTBED_UnderlayLinkModel *m;
159
160   GNUNET_assert (0 == peer->underlay_model_exists);
161   m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
162   peer->underlay_model_exists = 1;
163   m->type = type;
164   return m;
165 }
166
167
168 /**
169  * Add a peer to the given model.  Underlay connections to the given peer will
170  * be permitted if the model is whitelist based; otherwise they will not be
171  * permitted.
172  *
173  * @ingroup underlay
174  * @param model the model
175  * @param peer the peer to add
176  */
177 void
178 GNUNET_TESTBED_underlaylinkmodel_add_peer (struct
179                                            GNUNET_TESTBED_UnderlayLinkModel *
180                                            model,
181                                            struct GNUNET_TESTBED_Peer *peer)
182 {
183   struct ListEntry *entry;
184
185   entry = GNUNET_new (struct ListEntry);
186   entry->peer = peer;
187   entry->next = model->entries;
188   model->entries = entry;
189 }
190
191
192 /**
193  * Set the metrics for a link to the given peer in the underlay model.  The link
194  * SHOULD be permittable according to the given model.
195  *
196  * @ingroup underlay
197  * @param model the model
198  * @param peer the other end peer of the link
199  * @param latency latency of the link in microseconds
200  * @param loss data loss of the link expressed as a percentage
201  * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
202  */
203 void
204 GNUNET_TESTBED_underlaylinkmodel_set_link (struct
205                                            GNUNET_TESTBED_UnderlayLinkModel *
206                                            model,
207                                            struct GNUNET_TESTBED_Peer *peer,
208                                            uint32_t latency,
209                                            uint32_t loss,
210                                            uint32_t bandwidth)
211 {
212   struct LinkProperty *prop;
213
214   prop = GNUNET_new (struct LinkProperty);
215   prop->peer = peer;
216   prop->latency = latency;
217   prop->loss = loss;
218   prop->bandwidth = bandwidth;
219   prop->next = model->props;
220   model->props = prop;
221 }
222
223
224 /**
225  * Free the resources of the model.  Use this function only if the model has not
226  * be committed and has to be unallocated.  The peer can then have another model
227  * created.
228  *
229  * @ingroup underlay
230  * @param model the model to unallocate
231  */
232 void
233 GNUNET_TESTBED_underlaylinkmodel_free (struct
234                                        GNUNET_TESTBED_UnderlayLinkModel *model)
235 {
236   model->peer->underlay_model_exists = 0;
237   free_entries (model);
238   free_link_properties (model);
239   gnunet_free (model);
240 }
241
242
243 /**
244  * Commit the model.  The model is freed in this function(!).
245  *
246  * @ingroup underlay
247  * @param model the model to commit
248  */
249 void
250 GNUNET_TESTBED_underlaylinkmodel_commit (struct
251                                          GNUNET_TESTBED_UnderlayLinkModel *model)
252 {
253   /* FIXME: Marshal the model into a message */
254   GNUNET_break (0);
255   /* do not reset the value of model->peer->underlay_model_exists */
256   free_entries (model);
257   free_link_properties (model);
258   GNUNET_free (model);
259 }