gnunet-service-fs_cp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 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 fs/gnunet-service-fs_cp.h
  18. * @brief API to handle 'connected peers'
  19. * @author Christian Grothoff
  20. */
  21. #ifndef GNUNET_SERVICE_FS_CP_H
  22. #define GNUNET_SERVICE_FS_CP_H
  23. #include "fs.h"
  24. #include "gnunet-service-fs.h"
  25. /**
  26. * Maximum number of outgoing messages we queue per peer.
  27. *
  28. * Performance measurements for 2 peer setup for 50 MB file
  29. * (using perf_gnunet_service_fs_p2p):
  30. *
  31. * 24: 2-3 MB/s # ~ 24 MB RAM
  32. * 256: 8 MB/s # ~256 MB RAM
  33. *
  34. * Conclusion: 24 should suffice (reasonable
  35. * performance, no excessive memory use).
  36. */
  37. #define MAX_QUEUE_PER_PEER 24
  38. /**
  39. * Length of the P2P success tracker. Note that having a very long
  40. * list can also hurt performance.
  41. */
  42. #define P2P_SUCCESS_LIST_SIZE 8
  43. /**
  44. * Length of the CS-2-P success tracker. Note that
  45. * having a very long list can also hurt performance.
  46. */
  47. #define CS2P_SUCCESS_LIST_SIZE 8
  48. /**
  49. * Performance data kept for a peer.
  50. */
  51. struct GSF_PeerPerformanceData
  52. {
  53. /**
  54. * List of the last clients for which this peer successfully
  55. * answered a query.
  56. */
  57. struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
  58. /**
  59. * List of the last PIDs for which
  60. * this peer successfully answered a query;
  61. * We use 0 to indicate no successful reply.
  62. */
  63. GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
  64. /**
  65. * Average delay between sending the peer a request and
  66. * getting a reply (only calculated over the requests for
  67. * which we actually got a reply). Calculated
  68. * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
  69. */
  70. struct GNUNET_TIME_Relative avg_reply_delay;
  71. /**
  72. * If we get content we already have from this peer, for how
  73. * long do we block it? Adjusted based on the fraction of
  74. * redundant data we receive, between 1s and 1h.
  75. */
  76. struct GNUNET_TIME_Relative migration_delay;
  77. /**
  78. * Point in time until which this peer does not want us to migrate content
  79. * to it.
  80. */
  81. struct GNUNET_TIME_Absolute migration_blocked_until;
  82. /**
  83. * Transmission times for the last MAX_QUEUE_PER_PEER
  84. * requests for this peer. Used as a ring buffer, current
  85. * offset is stored in 'last_request_times_off'. If the
  86. * oldest entry is more recent than the 'avg_delay', we should
  87. * not send any more requests right now.
  88. */
  89. struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
  90. /**
  91. * How long does it typically take for us to transmit a message
  92. * to this peer? (delay between the request being issued and
  93. * the callback being invoked).
  94. */
  95. struct GNUNET_LOAD_Value *transmission_delay;
  96. /**
  97. * Average priority of successful replies. Calculated
  98. * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
  99. */
  100. double avg_priority;
  101. /**
  102. * The peer's identity (interned version).
  103. */
  104. GNUNET_PEER_Id pid;
  105. /**
  106. * The peer's identity (pointer).
  107. */
  108. const struct GNUNET_PeerIdentity *peer;
  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. * Handle to cancel a transmission request.
  160. */
  161. struct GSF_PeerTransmitHandle;
  162. /**
  163. * A peer connected to us. Setup the connected peer
  164. * records.
  165. *
  166. * @param cls NULL
  167. * @param peer identity of peer that connected
  168. * @param mq queue for sending messages to @a peer
  169. * @return internal handle for the peer
  170. */
  171. void *
  172. GSF_peer_connect_handler (void *cls,
  173. const struct GNUNET_PeerIdentity *peer,
  174. struct GNUNET_MQ_Handle *mq);
  175. /**
  176. * Get a handle for a connected peer.
  177. *
  178. * @param peer peer's identity
  179. * @return NULL if this peer is not currently connected
  180. */
  181. struct GSF_ConnectedPeer *
  182. GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
  183. /**
  184. * Update the latency information kept for the given peer.
  185. *
  186. * @param id peer record to update
  187. * @param latency current latency value
  188. */
  189. void
  190. GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
  191. struct GNUNET_TIME_Relative latency);
  192. /**
  193. * Transmit a message to the given peer as soon as possible.
  194. * If the peer disconnects before the transmission can happen,
  195. * the callback is invoked with a 'NULL' buffer.
  196. *
  197. * @param cp target peer
  198. * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO)
  199. * @param priority how important is this request?
  200. * @param env envelope of message to send
  201. */
  202. void
  203. GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
  204. int is_query,
  205. uint32_t priority,
  206. struct GNUNET_MQ_Envelope *env);
  207. /**
  208. * Report on receiving a reply; update the performance record of the given peer.
  209. *
  210. * @param cp responding peer (will be updated)
  211. * @param request_time time at which the original query was transmitted
  212. * @param request_priority priority of the original request
  213. */
  214. void
  215. GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
  216. struct GNUNET_TIME_Absolute request_time,
  217. uint32_t request_priority);
  218. /**
  219. * Report on receiving a reply in response to an initiating client.
  220. * Remember that this peer is good for this client.
  221. *
  222. * @param cp responding peer (will be updated)
  223. * @param initiator_client local client on responsible for query
  224. */
  225. void
  226. GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
  227. struct GSF_LocalClient *initiator_client);
  228. /**
  229. * Report on receiving a reply in response to an initiating peer.
  230. * Remember that this peer is good for this initiating peer.
  231. *
  232. * @param cp responding peer (will be updated)
  233. * @param initiator_peer other peer responsible for query
  234. */
  235. void
  236. GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
  237. const struct GSF_ConnectedPeer
  238. *initiator_peer);
  239. /**
  240. * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
  241. *
  242. * @param cls closure, the `struct GSF_ConnectedPeer`
  243. * @param msm the actual message
  244. */
  245. void
  246. handle_p2p_migration_stop (void *cls,
  247. const struct MigrationStopMessage *message);
  248. /**
  249. * Handle P2P "QUERY" message.
  250. *
  251. * @param cls the `struct GSF_ConnectedPeer` of the other sender
  252. * @param gm the actual message
  253. */
  254. void
  255. handle_p2p_get (void *cls,
  256. const struct GetMessage *gm);
  257. /**
  258. * Return the performance data record for the given peer
  259. *
  260. * @param cp peer to query
  261. * @return performance data record for the peer
  262. */
  263. struct GSF_PeerPerformanceData *
  264. GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
  265. /**
  266. * Ask a peer to stop migrating data to us until the given point
  267. * in time.
  268. *
  269. * @param cp peer to ask
  270. * @param block_time until when to block
  271. */
  272. void
  273. GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
  274. struct GNUNET_TIME_Absolute block_time);
  275. /**
  276. * A peer disconnected from us. Tear down the connected peer
  277. * record.
  278. *
  279. * @param cls unused
  280. * @param peer identity of peer that connected
  281. * @param internal_cls our `struct GSF_ConnectedPeer` for @a peer
  282. */
  283. void
  284. GSF_peer_disconnect_handler (void *cls,
  285. const struct GNUNET_PeerIdentity *peer,
  286. void *internal_cls);
  287. /**
  288. * Notification that a local client disconnected. Clean up all of our
  289. * references to the given handle.
  290. *
  291. * @param lc handle to the local client (henceforth invalid)
  292. */
  293. void
  294. GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
  295. /**
  296. * Notify core about a preference we have for the given peer
  297. * (to allocate more resources towards it). The change will
  298. * be communicated the next time we reserve bandwidth with
  299. * core (not instantly).
  300. *
  301. * @param cp peer to reserve bandwidth from
  302. * @param pref preference change
  303. */
  304. void
  305. GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
  306. uint64_t pref);
  307. /**
  308. * Obtain the identity of a connected peer.
  309. *
  310. * @param cp peer to get identity of
  311. * @param id identity to set (written to)
  312. */
  313. void
  314. GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
  315. struct GNUNET_PeerIdentity *id);
  316. /**
  317. * Obtain the identity of a connected peer.
  318. *
  319. * @param cp peer to get identity of
  320. * @return reference to peer identity, valid until peer disconnects (!)
  321. */
  322. const struct GNUNET_PeerIdentity *
  323. GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
  324. /**
  325. * Iterate over all connected peers.
  326. *
  327. * @param it function to call for each peer
  328. * @param it_cls closure for it
  329. */
  330. void
  331. GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
  332. /**
  333. * Initialize peer management subsystem.
  334. */
  335. void
  336. GSF_connected_peer_init_ (void);
  337. /**
  338. * Shutdown peer management subsystem.
  339. */
  340. void
  341. GSF_connected_peer_done_ (void);
  342. #endif
  343. /* end of gnunet-service-fs_cp.h */