gnunet-service-fs_cp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011 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 fs/gnunet-service-fs_cp.h
  19. * @brief API to handle 'connected peers'
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_SERVICE_FS_CP_H
  23. #define GNUNET_SERVICE_FS_CP_H
  24. #include "fs.h"
  25. #include "gnunet-service-fs.h"
  26. /**
  27. * Maximum number of outgoing messages we queue per peer.
  28. *
  29. * Performance measurements for 2 peer setup for 50 MB file
  30. * (with MAX_DATASTORE_QUEUE = 1 and RETRY_PROBABILITY_INV = 1):
  31. *
  32. * 2: 1700 kb/s, 1372 kb/s
  33. * 8: 2117 kb/s, 1284 kb/s, 1112 kb/s
  34. * 16: 3500 kb/s, 3200 kb/s, 3388 kb/s
  35. * 32: 3441 kb/s, 3163 kb/s, 3277 kb/s
  36. * 128: 1700 kb/s; 2010 kb/s, 3383 kb/s, 1156 kb/s
  37. *
  38. * Conclusion: 16 seems to be a pretty good value (stable
  39. * and high performance, no excessive memory use).
  40. */
  41. #define MAX_QUEUE_PER_PEER 16
  42. /**
  43. * Length of the P2P success tracker. Note that having a very long
  44. * list can also hurt performance.
  45. */
  46. #define P2P_SUCCESS_LIST_SIZE 8
  47. /**
  48. * Length of the CS-2-P success tracker. Note that
  49. * having a very long list can also hurt performance.
  50. */
  51. #define CS2P_SUCCESS_LIST_SIZE 8
  52. /**
  53. * Performance data kept for a peer.
  54. */
  55. struct GSF_PeerPerformanceData
  56. {
  57. /**
  58. * Transport performance data.
  59. */
  60. struct GNUNET_ATS_Information *atsi;
  61. /**
  62. * List of the last clients for which this peer successfully
  63. * answered a query.
  64. */
  65. struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
  66. /**
  67. * List of the last PIDs for which
  68. * this peer successfully answered a query;
  69. * We use 0 to indicate no successful reply.
  70. */
  71. GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
  72. /**
  73. * Average delay between sending the peer a request and
  74. * getting a reply (only calculated over the requests for
  75. * which we actually got a reply). Calculated
  76. * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
  77. */
  78. struct GNUNET_TIME_Relative avg_reply_delay;
  79. /**
  80. * If we get content we already have from this peer, for how
  81. * long do we block him? Adjusted based on the fraction of
  82. * redundant data we receive, between 1s and 1h.
  83. */
  84. struct GNUNET_TIME_Relative migration_delay;
  85. /**
  86. * Point in time until which this peer does not want us to migrate content
  87. * to it.
  88. */
  89. struct GNUNET_TIME_Absolute migration_blocked_until;
  90. /**
  91. * Transmission times for the last MAX_QUEUE_PER_PEER
  92. * requests for this peer. Used as a ring buffer, current
  93. * offset is stored in 'last_request_times_off'. If the
  94. * oldest entry is more recent than the 'avg_delay', we should
  95. * not send any more requests right now.
  96. */
  97. struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
  98. /**
  99. * How long does it typically take for us to transmit a message
  100. * to this peer? (delay between the request being issued and
  101. * the callback being invoked).
  102. */
  103. struct GNUNET_LOAD_Value *transmission_delay;
  104. /**
  105. * Average priority of successful replies. Calculated
  106. * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
  107. */
  108. double avg_priority;
  109. /**
  110. * The peer's identity.
  111. */
  112. GNUNET_PEER_Id pid;
  113. /**
  114. * Respect rating for this peer
  115. */
  116. uint32_t respect;
  117. /**
  118. * Number of pending queries (replies are not counted)
  119. */
  120. unsigned int pending_queries;
  121. /**
  122. * Number of pending replies (queries are not counted)
  123. */
  124. unsigned int pending_replies;
  125. };
  126. /**
  127. * Signature of function called on a connected peer.
  128. *
  129. * @param cls closure
  130. * @param peer identity of the peer
  131. * @param cp handle to the connected peer record
  132. * @param perf peer performance data
  133. */
  134. typedef void (*GSF_ConnectedPeerIterator) (void *cls,
  135. const struct GNUNET_PeerIdentity *
  136. peer, struct GSF_ConnectedPeer * cp,
  137. const struct GSF_PeerPerformanceData
  138. * ppd);
  139. /**
  140. * Function called to get a message for transmission.
  141. *
  142. * @param cls closure
  143. * @param buf_size number of bytes available in buf
  144. * @param buf where to copy the message, NULL on error (peer disconnect)
  145. * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
  146. */
  147. typedef size_t (*GSF_GetMessageCallback) (void *cls, size_t buf_size,
  148. void *buf);
  149. /**
  150. * Signature of function called on a reservation success or failure.
  151. *
  152. * @param cls closure
  153. * @param cp handle to the connected peer record
  154. * @param success GNUNET_YES on success, GNUNET_NO on failure
  155. */
  156. typedef void (*GSF_PeerReserveCallback) (void *cls,
  157. struct GSF_ConnectedPeer * cp,
  158. int success);
  159. /**
  160. * Function called after the creation of a connected peer record is complete.
  161. *
  162. * @param cls closure
  163. * @param cp handle to the newly created connected peer record
  164. */
  165. typedef void (*GSF_ConnectedPeerCreationCallback) (void *cls,
  166. struct GSF_ConnectedPeer *cp);
  167. /**
  168. * Handle to cancel a transmission request.
  169. */
  170. struct GSF_PeerTransmitHandle;
  171. /**
  172. * A peer connected to us. Setup the connected peer
  173. * records.
  174. *
  175. * @param peer identity of peer that connected
  176. * @param creation_cb callback function when the record is created.
  177. * @param creation_cb_cls closure for @creation_cb
  178. */
  179. void
  180. GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
  181. GSF_ConnectedPeerCreationCallback creation_cb,
  182. void *creation_cb_cls);
  183. /**
  184. * Get a handle for a connected peer.
  185. *
  186. * @param peer peer's identity
  187. * @return NULL if this peer is not currently connected
  188. */
  189. struct GSF_ConnectedPeer *
  190. GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
  191. /**
  192. * Update the latency information kept for the given peer.
  193. *
  194. * @param id peer record to update
  195. * @param latency current latency value
  196. */
  197. void
  198. GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
  199. struct GNUNET_TIME_Relative latency);
  200. /**
  201. * Transmit a message to the given peer as soon as possible.
  202. * If the peer disconnects before the transmission can happen,
  203. * the callback is invoked with a 'NULL' buffer.
  204. *
  205. * @param cp target peer
  206. * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
  207. * @param priority how important is this request?
  208. * @param timeout when does this request timeout (call gmc with error)
  209. * @param size number of bytes we would like to send to the peer
  210. * @param gmc function to call to get the message
  211. * @param gmc_cls closure for gmc
  212. * @return handle to cancel request
  213. */
  214. struct GSF_PeerTransmitHandle *
  215. GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
  216. uint32_t priority, struct GNUNET_TIME_Relative timeout,
  217. size_t size, GSF_GetMessageCallback gmc, void *gmc_cls);
  218. /**
  219. * Cancel an earlier request for transmission.
  220. *
  221. * @param pth request to cancel
  222. */
  223. void
  224. GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
  225. /**
  226. * Report on receiving a reply; update the performance record of the given peer.
  227. *
  228. * @param cp responding peer (will be updated)
  229. * @param request_time time at which the original query was transmitted
  230. * @param request_priority priority of the original request
  231. */
  232. void
  233. GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
  234. struct GNUNET_TIME_Absolute request_time,
  235. uint32_t request_priority);
  236. /**
  237. * Report on receiving a reply in response to an initiating client.
  238. * Remember that this peer is good for this client.
  239. *
  240. * @param cp responding peer (will be updated)
  241. * @param initiator_client local client on responsible for query
  242. */
  243. void
  244. GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
  245. struct GSF_LocalClient *initiator_client);
  246. /**
  247. * Report on receiving a reply in response to an initiating peer.
  248. * Remember that this peer is good for this initiating peer.
  249. *
  250. * @param cp responding peer (will be updated)
  251. * @param initiator_peer other peer responsible for query
  252. */
  253. void
  254. GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
  255. const struct GSF_ConnectedPeer
  256. *initiator_peer);
  257. /**
  258. * Handle P2P "MIGRATION_STOP" message.
  259. *
  260. * @param cls closure, always NULL
  261. * @param other the other peer involved (sender or receiver, NULL
  262. * for loopback messages where we are both sender and receiver)
  263. * @param message the actual message
  264. * @return GNUNET_OK to keep the connection open,
  265. * GNUNET_SYSERR to close it (signal serious error)
  266. */
  267. int
  268. GSF_handle_p2p_migration_stop_ (void *cls,
  269. const struct GNUNET_PeerIdentity *other,
  270. const struct GNUNET_MessageHeader *message);
  271. /**
  272. * Handle P2P "QUERY" message. Only responsible for creating the
  273. * request entry itself and setting up reply callback and cancellation
  274. * on peer disconnect. Does NOT execute the actual request strategy
  275. * (planning) or local database operations.
  276. *
  277. * @param other the other peer involved (sender or receiver, NULL
  278. * for loopback messages where we are both sender and receiver)
  279. * @param message the actual message
  280. * @return pending request handle, NULL on error
  281. */
  282. struct GSF_PendingRequest *
  283. GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
  284. const struct GNUNET_MessageHeader *message);
  285. /**
  286. * Return the performance data record for the given peer
  287. *
  288. * @param cp peer to query
  289. * @return performance data record for the peer
  290. */
  291. struct GSF_PeerPerformanceData *
  292. GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
  293. /**
  294. * Ask a peer to stop migrating data to us until the given point
  295. * in time.
  296. *
  297. * @param cp peer to ask
  298. * @param block_time until when to block
  299. */
  300. void
  301. GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
  302. struct GNUNET_TIME_Absolute block_time);
  303. /**
  304. * A peer disconnected from us. Tear down the connected peer
  305. * record.
  306. *
  307. * @param cls unused
  308. * @param peer identity of peer that connected
  309. */
  310. void
  311. GSF_peer_disconnect_handler_ (void *cls,
  312. const struct GNUNET_PeerIdentity *peer);
  313. /**
  314. * Notification that a local client disconnected. Clean up all of our
  315. * references to the given handle.
  316. *
  317. * @param lc handle to the local client (henceforth invalid)
  318. */
  319. void
  320. GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
  321. /**
  322. * Notify core about a preference we have for the given peer
  323. * (to allocate more resources towards it). The change will
  324. * be communicated the next time we reserve bandwidth with
  325. * core (not instantly).
  326. *
  327. * @param cp peer to reserve bandwidth from
  328. * @param pref preference change
  329. */
  330. void
  331. GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
  332. uint64_t pref);
  333. /**
  334. * Obtain the identity of a connected peer.
  335. *
  336. * @param cp peer to get identity of
  337. * @param id identity to set (written to)
  338. */
  339. void
  340. GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
  341. struct GNUNET_PeerIdentity *id);
  342. /**
  343. * Obtain the identity of a connected peer.
  344. *
  345. * @param cp peer to get identity of
  346. * @return reference to peer identity, valid until peer disconnects (!)
  347. */
  348. const struct GNUNET_PeerIdentity *
  349. GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
  350. /**
  351. * Iterate over all connected peers.
  352. *
  353. * @param it function to call for each peer
  354. * @param it_cls closure for it
  355. */
  356. void
  357. GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
  358. /**
  359. * Initialize peer management subsystem.
  360. */
  361. void
  362. GSF_connected_peer_init_ (void);
  363. /**
  364. * Shutdown peer management subsystem.
  365. */
  366. void
  367. GSF_connected_peer_done_ (void);
  368. #endif
  369. /* end of gnunet-service-fs_cp.h */