testbed_api_underlay.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008--2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/testbed_api_underlay.c
  18. * @brief testbed underlay API implementation
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "testbed_api_peers.h"
  22. /**
  23. * An underlay link
  24. */
  25. struct LinkProperty
  26. {
  27. /**
  28. * next pointer for list
  29. */
  30. struct LinkProperty *next;
  31. /**
  32. * the peer whose link is defined by these properties
  33. */
  34. struct GNUNET_TESTBED_Peer *peer;
  35. /**
  36. * latency of the link in microseconds
  37. */
  38. uint32_t latency;
  39. /**
  40. * data loss on the link expressed as percentage
  41. */
  42. uint32_t loss;
  43. /**
  44. * bandwidth of the link in kilobytes per second
  45. */
  46. uint32_t bandwidth;
  47. };
  48. /**
  49. * Container for holding a peer in whitelist/blacklist
  50. */
  51. struct ListEntry
  52. {
  53. /**
  54. * the next pointer
  55. */
  56. struct ListEntry *next;
  57. /**
  58. * the peer
  59. */
  60. struct GNUNET_TESTBED_Peer *peer;
  61. };
  62. /**
  63. * Model for configuring underlay links of a peer
  64. * @ingroup underlay
  65. */
  66. struct GNUNET_TESTBED_UnderlayLinkModel
  67. {
  68. /**
  69. * The peer associated with this model
  70. */
  71. struct GNUNET_TESTBED_Peer *peer;
  72. /**
  73. * List of peers in the list
  74. */
  75. struct ListEntry *entries;
  76. /**
  77. * list of link properties
  78. */
  79. struct LinkProperty *props;
  80. /**
  81. * the type of this model
  82. */
  83. enum GNUNET_TESTBED_UnderlayLinkModelType type;
  84. }
  85. /**
  86. * Function to free resources of list entries
  87. *
  88. * @param model the model
  89. */
  90. static void
  91. free_entries (struct GNUNET_TESTBED_UnderlayLinkModel *model)
  92. {
  93. struct ListEntry *e;
  94. while (NULL != (e = model->entries))
  95. {
  96. model->entries = e->next;
  97. GNUNET_free (e);
  98. }
  99. }
  100. /**
  101. * Function to free resources of link properties added to the given model
  102. *
  103. * @param model the model
  104. */
  105. static void
  106. free_link_properties (struct GNUNET_TESTBED_UnderlayLinkModel *model)
  107. {
  108. struct LinkProperty *p;
  109. while (NULL != (p = model->props))
  110. {
  111. model->props = p->next;
  112. GNUNET_free (p);
  113. }
  114. }
  115. /**
  116. * Create a GNUNET_TESTBED_UnderlayLinkModel for the given peer. A peer can
  117. * have ONLY ONE model and it can be either a blacklist or whitelist based one.
  118. *
  119. * @ingroup underlay
  120. * @param peer the peer for which the model has to be created
  121. * @param type the type of the model
  122. * @return the model
  123. */
  124. struct GNUNET_TESTBED_UnderlayLinkModel *
  125. GNUNET_TESTBED_underlaylinkmodel_create (struct GNUNET_TESTBED_Peer *peer,
  126. enum GNUNET_TESTBED_UnderlayLinkModelType type)
  127. {
  128. struct GNUNET_TESTBED_UnderlayLinkModel *m;
  129. GNUNET_assert (0 == peer->underlay_model_exists);
  130. m = GNUNET_new (struct GNUNET_TESTBED_UnderlayLinkModel);
  131. peer->underlay_model_exists = 1;
  132. m->type = type;
  133. return m;
  134. }
  135. /**
  136. * Add a peer to the given model. Underlay connections to the given peer will
  137. * be permitted if the model is whitelist based; otherwise they will not be
  138. * permitted.
  139. *
  140. * @ingroup underlay
  141. * @param model the model
  142. * @param peer the peer to add
  143. */
  144. void
  145. GNUNET_TESTBED_underlaylinkmodel_add_peer (struct GNUNET_TESTBED_UnderlayLinkModel *model,
  146. struct GNUNET_TESTBED_Peer *peer)
  147. {
  148. struct ListEntry *entry;
  149. entry = GNUNET_new (struct ListEntry);
  150. entry->peer = peer;
  151. entry->next = model->entries;
  152. model->entries = entry;
  153. }
  154. /**
  155. * Set the metrics for a link to the given peer in the underlay model. The link
  156. * SHOULD be permittable according to the given model.
  157. *
  158. * @ingroup underlay
  159. * @param model the model
  160. * @param peer the other end peer of the link
  161. * @param latency latency of the link in microseconds
  162. * @param loss data loss of the link expressed as a percentage
  163. * @param bandwidth bandwidth of the link in kilobytes per second [kB/s]
  164. */
  165. void
  166. GNUNET_TESTBED_underlaylinkmodel_set_link (struct GNUNET_TESTBED_UnderlayLinkModel *model,
  167. struct GNUNET_TESTBED_Peer *peer,
  168. uint32_t latency,
  169. uint32_t loss,
  170. uint32_t bandwidth)
  171. {
  172. struct LinkProperty *prop;
  173. prop = GNUNET_new (struct LinkProperty);
  174. prop->peer = peer;
  175. prop->latency = latency;
  176. prop->loss = loss;
  177. prop->bandwidth = bandwidth;
  178. prop->next = model->props;
  179. model->props = prop;
  180. }
  181. /**
  182. * Free the resources of the model. Use this function only if the model has not
  183. * be committed and has to be unallocated. The peer can then have another model
  184. * created.
  185. *
  186. * @ingroup underlay
  187. * @param model the model to unallocate
  188. */
  189. void
  190. GNUNET_TESTBED_underlaylinkmodel_free (struct GNUNET_TESTBED_UnderlayLinkModel *model)
  191. {
  192. model->peer->underlay_model_exists = 0;
  193. free_entries (model);
  194. free_link_properties (model);
  195. gnunet_free (model);
  196. }
  197. /**
  198. * Commit the model. The model is freed in this function(!).
  199. *
  200. * @ingroup underlay
  201. * @param model the model to commit
  202. */
  203. void
  204. GNUNET_TESTBED_underlaylinkmodel_commit (struct GNUNET_TESTBED_UnderlayLinkModel *model)
  205. {
  206. /* FIXME: Marshal the model into a message */
  207. GNUNET_break (0);
  208. /* do not reset the value of model->peer->underlay_model_exists */
  209. free_entries (model);
  210. free_link_properties (model);
  211. GNUNET_free (model);
  212. }