testbed_api_underlay.c 5.8 KB

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