aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_underlay.c
blob: e09c7ec7af7ce5734b7e6d4232a1e8fff72d9283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
  This file is part of GNUnet.
  Copyright (C) 2008--2013 GNUnet e.V.

  GNUnet is free software: you can redistribute it and/or modify it
  under the terms of the GNU Affero General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Affero General Public License for more details.
*/

/**
 * @file testbed/testbed_api_underlay.c
 * @brief testbed underlay API implementation
 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
 */

#include "testbed_api_peers.h"


/**
 * An underlay link
 */
struct LinkProperty
{
  /**
   * next pointer for list
   */
  struct LinkProperty *next;

  /**
   * the peer whose link is defined by these properties
   */
  struct GNUNET_TESTBED_Peer *peer;

  /**
   * latency of the link in microseconds
   */
  uint32_t latency;

  /**
   * data loss on the link expressed as percentage
   */
  uint32_t loss;

  /**
   * bandwidth of the link in kilobytes per second
   */
  uint32_t bandwidth;
};


/**
 * Container for holding a peer in whitelist/blacklist
 */
struct ListEntry
{
  /**
   * the next pointer
   */
  struct ListEntry *next;

  /**
   * the peer
   */
  struct GNUNET_TESTBED_Peer *peer;
};


/**
 * Model for configuring underlay links of a peer
 * @ingroup underlay
 */
struct GNUNET_TESTBED_UnderlayLinkModel
{
  /**
   * The peer associated with this model
   */
  struct GNUNET_TESTBED_Peer *peer;

  /**
   * List of peers in the list
   */
  struct ListEntry *entries;

  /**
   * list of link properties
   */
  struct LinkProperty *props;

  /**
   * the type of this model
   */
  enum GNUNET_TESTBED_UnderlayLinkModelType type;
}


/**
 * Function to free resources of list entries
 *
 * @param model the model
 */
static void
free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
{
  struct ListEntry *e;

  while (NULL != (e = model->entries))
  {
    model->entries = e->next;
    GNUNET_free (e);
  }
}


/**
 * Function to free resources of link properties added to the given model
 *
 * @param model the model
 */
static void
free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
{
  struct LinkProperty *p;

  while (NULL != (p = model->props))
  {
    model->props = p->next;
    GNUNET_free (p);
  }
}


/**
 * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer.  A peer can
 * have ONLY ONE model and it can be either a blacklist or whitelist based one.
 *
 * @ingroup underlay
 * @param peer the peer for which the model has to be created
 * @param type the type of the model
 * @return the model
 */
struct GNUNET_TESTBED_UnderlayLinkModel *
GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
                                         enum GNUNET_TESTBED_UnderlayLinkModelType type)
{
  struct GNUNET_TESTBED_UnderlayLinkModel *m;

  GNUNET_assert (0 == peer->underlay_model_exists);
  m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
  peer->underlay_model_exists = 1;
  m->type = type;
  return m;
}


/**
 * Add a peer to the given model.  Underlay connections to the given peer will
 * be permitted if the model is whitelist based; otherwise they will not be
 * permitted.
 *
 * @ingroup underlay
 * @param model the model
 * @param peer the peer to add
 */
void
GNUNET_TESTBED_underlaylinkmodel_add_peer (struct GNUNET_TESTBED_UnderlayLinkModel *model,
                                           struct GNUNET_TESTBED_Peer *peer)
{
  struct ListEntry *entry;

  entry = GNUNET_new (struct ListEntry);
  entry->peer = peer;
  entry->next = model->entries;
  model->entries = entry;
}


/**
 * Set the metrics for a link to the given peer in the underlay model.  The link
 * SHOULD be permittable according to the given model.
 *
 * @ingroup underlay
 * @param model the model
 * @param peer the other end peer of the link
 * @param latency latency of the link in microseconds
 * @param loss data loss of the link expressed as a percentage
 * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
 */
void
GNUNET_TESTBED_underlaylinkmodel_set_link (struct GNUNET_TESTBED_UnderlayLinkModel *model,
                                           struct GNUNET_TESTBED_Peer *peer,
                                           uint32_t latency,
                                           uint32_t loss,
                                           uint32_t bandwidth)
{
  struct LinkProperty *prop;

  prop = GNUNET_new (struct LinkProperty);
  prop->peer = peer;
  prop->latency = latency;
  prop->loss = loss;
  prop->bandwidth = bandwidth;
  prop->next = model->props;
  model->props = prop;
}


/**
 * Free the resources of the model.  Use this function only if the model has not
 * be committed and has to be unallocated.  The peer can then have another model
 * created.
 *
 * @ingroup underlay
 * @param model the model to unallocate
 */
void
GNUNET_TESTBED_underlaylinkmodel_free (struct GNUNET_TESTBED_UnderlayLinkModel *model)
{
  model->peer->underlay_model_exists = 0;
  free_entries (model);
  free_link_properties (model);
  gnunet_free (model);
}


/**
 * Commit the model.  The model is freed in this function(!).
 *
 * @ingroup underlay
 * @param model the model to commit
 */
void
GNUNET_TESTBED_underlaylinkmodel_commit (struct GNUNET_TESTBED_UnderlayLinkModel *model)
{
  /* FIXME: Marshal the model into a message */
  GNUNET_break (0);
  /* do not reset the value of model->peer->underlay_model_exists */
  free_entries (model);
  free_link_properties (model);
  GNUNET_free (model);
}