gnunet-service-fs_cp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (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. * List of the last clients for which this peer successfully
  59. * answered a query.
  60. */
  61. struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
  62. /**
  63. * List of the last PIDs for which
  64. * this peer successfully answered a query;
  65. * We use 0 to indicate no successful reply.
  66. */
  67. GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
  68. /**
  69. * Average delay between sending the peer a request and
  70. * getting a reply (only calculated over the requests for
  71. * which we actually got a reply). Calculated
  72. * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
  73. */
  74. struct GNUNET_TIME_Relative avg_reply_delay;
  75. /**
  76. * If we get content we already have from this peer, for how
  77. * long do we block him? Adjusted based on the fraction of
  78. * redundant data we receive, between 1s and 1h.
  79. */
  80. struct GNUNET_TIME_Relative migration_delay;
  81. /**
  82. * Point in time until which this peer does not want us to migrate content
  83. * to it.
  84. */
  85. struct GNUNET_TIME_Absolute migration_blocked_until;
  86. /**
  87. * Transmission times for the last MAX_QUEUE_PER_PEER
  88. * requests for this peer. Used as a ring buffer, current
  89. * offset is stored in 'last_request_times_off'. If the
  90. * oldest entry is more recent than the 'avg_delay', we should
  91. * not send any more requests right now.
  92. */
  93. struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
  94. /**
  95. * How long does it typically take for us to transmit a message
  96. * to this peer? (delay between the request being issued and
  97. * the callback being invoked).
  98. */
  99. struct GNUNET_LOAD_Value *transmission_delay;
  100. /**
  101. * Average priority of successful replies. Calculated
  102. * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
  103. */
  104. double avg_priority;
  105. /**
  106. * The peer's identity.
  107. */
  108. GNUNET_PEER_Id pid;
  109. /**
  110. * Respect rating for this peer
  111. */
  112. uint32_t respect;
  113. /**
  114. * Number of pending queries (replies are not counted)
  115. */
  116. unsigned int pending_queries;
  117. /**
  118. * Number of pending replies (queries are not counted)
  119. */
  120. unsigned int pending_replies;
  121. };
  122. /**
  123. * Signature of function called on a connected peer.
  124. *
  125. * @param cls closure
  126. * @param peer identity of the peer
  127. * @param cp handle to the connected peer record
  128. * @param perf peer performance data
  129. */
  130. typedef void
  131. (*GSF_ConnectedPeerIterator) (void *cls,
  132. const struct GNUNET_PeerIdentity *peer,
  133. struct GSF_ConnectedPeer *cp,
  134. const struct GSF_PeerPerformanceData *ppd);
  135. /**
  136. * Function called to get a message for transmission.
  137. *
  138. * @param cls closure
  139. * @param buf_size number of bytes available in @a buf
  140. * @param buf where to copy the message, NULL on error (peer disconnect)
  141. * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
  142. */
  143. typedef size_t
  144. (*GSF_GetMessageCallback) (void *cls,
  145. size_t buf_size,
  146. void *buf);
  147. /**
  148. * Signature of function called on a reservation success or failure.
  149. *
  150. * @param cls closure
  151. * @param cp handle to the connected peer record
  152. * @param success #GNUNET_YES on success, #GNUNET_NO on failure
  153. */
  154. typedef void
  155. (*GSF_PeerReserveCallback) (void *cls,
  156. struct GSF_ConnectedPeer *cp,
  157. int success);
  158. /**
  159. * Function called after the creation of a connected peer record is complete.
  160. *
  161. * @param cls closure
  162. * @param cp handle to the newly created connected peer record
  163. */
  164. typedef void
  165. (*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,
  216. int is_query,
  217. uint32_t priority,
  218. struct GNUNET_TIME_Relative timeout,
  219. size_t size, GSF_GetMessageCallback gmc,
  220. void *gmc_cls);
  221. /**
  222. * Cancel an earlier request for transmission.
  223. *
  224. * @param pth request to cancel
  225. */
  226. void
  227. GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
  228. /**
  229. * Report on receiving a reply; update the performance record of the given peer.
  230. *
  231. * @param cp responding peer (will be updated)
  232. * @param request_time time at which the original query was transmitted
  233. * @param request_priority priority of the original request
  234. */
  235. void
  236. GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
  237. struct GNUNET_TIME_Absolute request_time,
  238. uint32_t request_priority);
  239. /**
  240. * Report on receiving a reply in response to an initiating client.
  241. * Remember that this peer is good for this client.
  242. *
  243. * @param cp responding peer (will be updated)
  244. * @param initiator_client local client on responsible for query
  245. */
  246. void
  247. GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
  248. struct GSF_LocalClient *initiator_client);
  249. /**
  250. * Report on receiving a reply in response to an initiating peer.
  251. * Remember that this peer is good for this initiating peer.
  252. *
  253. * @param cp responding peer (will be updated)
  254. * @param initiator_peer other peer responsible for query
  255. */
  256. void
  257. GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
  258. const struct GSF_ConnectedPeer
  259. *initiator_peer);
  260. /**
  261. * Handle P2P "MIGRATION_STOP" message.
  262. *
  263. * @param cls closure, always NULL
  264. * @param other the other peer involved (sender or receiver, NULL
  265. * for loopback messages where we are both sender and receiver)
  266. * @param message the actual message
  267. * @return #GNUNET_OK to keep the connection open,
  268. * #GNUNET_SYSERR to close it (signal serious error)
  269. */
  270. int
  271. GSF_handle_p2p_migration_stop_ (void *cls,
  272. const struct GNUNET_PeerIdentity *other,
  273. const struct GNUNET_MessageHeader *message);
  274. /**
  275. * Handle P2P "QUERY" message. Only responsible for creating the
  276. * request entry itself and setting up reply callback and cancellation
  277. * on peer disconnect. Does NOT execute the actual request strategy
  278. * (planning) or local database operations.
  279. *
  280. * @param other the other peer involved (sender or receiver, NULL
  281. * for loopback messages where we are both sender and receiver)
  282. * @param message the actual message
  283. * @return pending request handle, NULL on error
  284. */
  285. struct GSF_PendingRequest *
  286. GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
  287. const struct GNUNET_MessageHeader *message);
  288. /**
  289. * Return the performance data record for the given peer
  290. *
  291. * @param cp peer to query
  292. * @return performance data record for the peer
  293. */
  294. struct GSF_PeerPerformanceData *
  295. GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
  296. /**
  297. * Ask a peer to stop migrating data to us until the given point
  298. * in time.
  299. *
  300. * @param cp peer to ask
  301. * @param block_time until when to block
  302. */
  303. void
  304. GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
  305. struct GNUNET_TIME_Absolute block_time);
  306. /**
  307. * A peer disconnected from us. Tear down the connected peer
  308. * record.
  309. *
  310. * @param cls unused
  311. * @param peer identity of peer that connected
  312. */
  313. void
  314. GSF_peer_disconnect_handler_ (void *cls,
  315. const struct GNUNET_PeerIdentity *peer);
  316. /**
  317. * Notification that a local client disconnected. Clean up all of our
  318. * references to the given handle.
  319. *
  320. * @param lc handle to the local client (henceforth invalid)
  321. */
  322. void
  323. GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
  324. /**
  325. * Notify core about a preference we have for the given peer
  326. * (to allocate more resources towards it). The change will
  327. * be communicated the next time we reserve bandwidth with
  328. * core (not instantly).
  329. *
  330. * @param cp peer to reserve bandwidth from
  331. * @param pref preference change
  332. */
  333. void
  334. GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
  335. uint64_t pref);
  336. /**
  337. * Obtain the identity of a connected peer.
  338. *
  339. * @param cp peer to get identity of
  340. * @param id identity to set (written to)
  341. */
  342. void
  343. GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
  344. struct GNUNET_PeerIdentity *id);
  345. /**
  346. * Obtain the identity of a connected peer.
  347. *
  348. * @param cp peer to get identity of
  349. * @return reference to peer identity, valid until peer disconnects (!)
  350. */
  351. const struct GNUNET_PeerIdentity *
  352. GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
  353. /**
  354. * Iterate over all connected peers.
  355. *
  356. * @param it function to call for each peer
  357. * @param it_cls closure for it
  358. */
  359. void
  360. GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
  361. /**
  362. * Initialize peer management subsystem.
  363. */
  364. void
  365. GSF_connected_peer_init_ (void);
  366. /**
  367. * Shutdown peer management subsystem.
  368. */
  369. void
  370. GSF_connected_peer_done_ (void);
  371. #endif
  372. /* end of gnunet-service-fs_cp.h */