tighten formatting rules
[oweals/gnunet.git] / src / fs / gnunet-service-fs_cp.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file fs/gnunet-service-fs_cp.h
23  * @brief API to handle 'connected peers'
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_FS_CP_H
27 #define GNUNET_SERVICE_FS_CP_H
28
29 #include "fs.h"
30 #include "gnunet-service-fs.h"
31
32
33 /**
34  * Maximum number of outgoing messages we queue per peer.
35  *
36  * Performance measurements for 2 peer setup for 50 MB file
37  * (using perf_gnunet_service_fs_p2p):
38  *
39  *  24: 2-3 MB/s # ~ 24 MB RAM
40  * 256:   8 MB/s # ~256 MB RAM
41  *
42  * Conclusion: 24 should suffice (reasonable
43  * performance, no excessive memory use).
44  */
45 #define MAX_QUEUE_PER_PEER 24
46
47 /**
48  * Length of the P2P success tracker.  Note that having a very long
49  * list can also hurt performance.
50  */
51 #define P2P_SUCCESS_LIST_SIZE 8
52
53 /**
54  * Length of the CS-2-P success tracker.  Note that
55  * having a very long list can also hurt performance.
56  */
57 #define CS2P_SUCCESS_LIST_SIZE 8
58
59
60 /**
61  * Performance data kept for a peer.
62  */
63 struct GSF_PeerPerformanceData
64 {
65   /**
66    * List of the last clients for which this peer successfully
67    * answered a query.
68    */
69   struct GSF_LocalClient *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
70
71   /**
72    * List of the last PIDs for which
73    * this peer successfully answered a query;
74    * We use 0 to indicate no successful reply.
75    */
76   GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
77
78   /**
79    * Average delay between sending the peer a request and
80    * getting a reply (only calculated over the requests for
81    * which we actually got a reply).   Calculated
82    * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
83    */
84   struct GNUNET_TIME_Relative avg_reply_delay;
85
86   /**
87    * If we get content we already have from this peer, for how
88    * long do we block it?  Adjusted based on the fraction of
89    * redundant data we receive, between 1s and 1h.
90    */
91   struct GNUNET_TIME_Relative migration_delay;
92
93   /**
94    * Point in time until which this peer does not want us to migrate content
95    * to it.
96    */
97   struct GNUNET_TIME_Absolute migration_blocked_until;
98
99   /**
100    * Transmission times for the last MAX_QUEUE_PER_PEER
101    * requests for this peer.  Used as a ring buffer, current
102    * offset is stored in 'last_request_times_off'.  If the
103    * oldest entry is more recent than the 'avg_delay', we should
104    * not send any more requests right now.
105    */
106   struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
107
108   /**
109    * How long does it typically take for us to transmit a message
110    * to this peer?  (delay between the request being issued and
111    * the callback being invoked).
112    */
113   struct GNUNET_LOAD_Value *transmission_delay;
114
115   /**
116    * Average priority of successful replies.  Calculated
117    * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
118    */
119   double avg_priority;
120
121   /**
122    * The peer's identity (interned version).
123    */
124   GNUNET_PEER_Id pid;
125
126   /**
127    * The peer's identity (pointer).
128    */
129   const struct GNUNET_PeerIdentity *peer;
130
131   /**
132    * Respect rating for this peer
133    */
134   uint32_t respect;
135
136   /**
137    * Number of pending queries (replies are not counted)
138    */
139   unsigned int pending_queries;
140
141   /**
142    * Number of pending replies (queries are not counted)
143    */
144   unsigned int pending_replies;
145 };
146
147
148 /**
149  * Signature of function called on a connected peer.
150  *
151  * @param cls closure
152  * @param peer identity of the peer
153  * @param cp handle to the connected peer record
154  * @param perf peer performance data
155  */
156 typedef void
157 (*GSF_ConnectedPeerIterator) (void *cls,
158                               const struct GNUNET_PeerIdentity *peer,
159                               struct GSF_ConnectedPeer *cp,
160                               const struct GSF_PeerPerformanceData *ppd);
161
162
163 /**
164  * Function called to get a message for transmission.
165  *
166  * @param cls closure
167  * @param buf_size number of bytes available in @a buf
168  * @param buf where to copy the message, NULL on error (peer disconnect)
169  * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
170  */
171 typedef size_t
172 (*GSF_GetMessageCallback) (void *cls,
173                            size_t buf_size,
174                            void *buf);
175
176
177 /**
178  * Signature of function called on a reservation success or failure.
179  *
180  * @param cls closure
181  * @param cp handle to the connected peer record
182  * @param success #GNUNET_YES on success, #GNUNET_NO on failure
183  */
184 typedef void
185 (*GSF_PeerReserveCallback) (void *cls,
186                             struct GSF_ConnectedPeer *cp,
187                             int success);
188
189
190 /**
191  * Handle to cancel a transmission request.
192  */
193 struct GSF_PeerTransmitHandle;
194
195
196 /**
197  * A peer connected to us.  Setup the connected peer
198  * records.
199  *
200  * @param cls NULL
201  * @param peer identity of peer that connected
202  * @param mq queue for sending messages to @a peer
203  * @return internal handle for the peer
204  */
205 void *
206 GSF_peer_connect_handler (void *cls,
207                           const struct GNUNET_PeerIdentity *peer,
208                           struct GNUNET_MQ_Handle *mq);
209
210
211 /**
212  * Get a handle for a connected peer.
213  *
214  * @param peer peer's identity
215  * @return NULL if this peer is not currently connected
216  */
217 struct GSF_ConnectedPeer *
218 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
219
220
221 /**
222  * Update the latency information kept for the given peer.
223  *
224  * @param id peer record to update
225  * @param latency current latency value
226  */
227 void
228 GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
229                           struct GNUNET_TIME_Relative latency);
230
231
232 /**
233  * Transmit a message to the given peer as soon as possible.
234  * If the peer disconnects before the transmission can happen,
235  * the callback is invoked with a 'NULL' buffer.
236  *
237  * @param cp target peer
238  * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO)
239  * @param priority how important is this request?
240  * @param env envelope of message to send
241  */
242 void
243 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
244                     int is_query,
245                     uint32_t priority,
246                     struct GNUNET_MQ_Envelope *env);
247
248
249 /**
250  * Report on receiving a reply; update the performance record of the given peer.
251  *
252  * @param cp responding peer (will be updated)
253  * @param request_time time at which the original query was transmitted
254  * @param request_priority priority of the original request
255  */
256 void
257 GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
258                               struct GNUNET_TIME_Absolute request_time,
259                               uint32_t request_priority);
260
261
262 /**
263  * Report on receiving a reply in response to an initiating client.
264  * Remember that this peer is good for this client.
265  *
266  * @param cp responding peer (will be updated)
267  * @param initiator_client local client on responsible for query
268  */
269 void
270 GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
271                                    struct GSF_LocalClient *initiator_client);
272
273
274 /**
275  * Report on receiving a reply in response to an initiating peer.
276  * Remember that this peer is good for this initiating peer.
277  *
278  * @param cp responding peer (will be updated)
279  * @param initiator_peer other peer responsible for query
280  */
281 void
282 GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
283                                  const struct GSF_ConnectedPeer
284                                  *initiator_peer);
285
286
287 /**
288  * Handle P2P #GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP message.
289  *
290  * @param cls closure, the `struct GSF_ConnectedPeer`
291  * @param msm the actual message
292  */
293 void
294 handle_p2p_migration_stop (void *cls,
295                            const struct MigrationStopMessage *message);
296
297
298 /**
299  * Handle P2P "QUERY" message.
300  *
301  * @param cls the `struct GSF_ConnectedPeer` of the other sender
302  * @param gm the actual message
303  */
304 void
305 handle_p2p_get (void *cls,
306                 const struct GetMessage *gm);
307
308
309 /**
310  * Return the performance data record for the given peer
311  *
312  * @param cp peer to query
313  * @return performance data record for the peer
314  */
315 struct GSF_PeerPerformanceData *
316 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
317
318
319 /**
320  * Ask a peer to stop migrating data to us until the given point
321  * in time.
322  *
323  * @param cp peer to ask
324  * @param block_time until when to block
325  */
326 void
327 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
328                            struct GNUNET_TIME_Absolute block_time);
329
330
331 /**
332  * A peer disconnected from us.  Tear down the connected peer
333  * record.
334  *
335  * @param cls unused
336  * @param peer identity of peer that connected
337  * @param internal_cls our `struct GSF_ConnectedPeer` for @a peer
338  */
339 void
340 GSF_peer_disconnect_handler (void *cls,
341                              const struct GNUNET_PeerIdentity *peer,
342                              void *internal_cls);
343
344
345 /**
346  * Notification that a local client disconnected.  Clean up all of our
347  * references to the given handle.
348  *
349  * @param lc handle to the local client (henceforth invalid)
350  */
351 void
352 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
353
354
355 /**
356  * Notify core about a preference we have for the given peer
357  * (to allocate more resources towards it).  The change will
358  * be communicated the next time we reserve bandwidth with
359  * core (not instantly).
360  *
361  * @param cp peer to reserve bandwidth from
362  * @param pref preference change
363  */
364 void
365 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
366                                        uint64_t pref);
367
368
369 /**
370  * Obtain the identity of a connected peer.
371  *
372  * @param cp peer to get identity of
373  * @param id identity to set (written to)
374  */
375 void
376 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
377                                   struct GNUNET_PeerIdentity *id);
378
379
380 /**
381  * Obtain the identity of a connected peer.
382  *
383  * @param cp peer to get identity of
384  * @return reference to peer identity, valid until peer disconnects (!)
385  */
386 const struct GNUNET_PeerIdentity *
387 GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp);
388
389
390 /**
391  * Iterate over all connected peers.
392  *
393  * @param it function to call for each peer
394  * @param it_cls closure for it
395  */
396 void
397 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
398
399
400 /**
401  * Initialize peer management subsystem.
402  */
403 void
404 GSF_connected_peer_init_ (void);
405
406
407 /**
408  * Shutdown peer management subsystem.
409  */
410 void
411 GSF_connected_peer_done_ (void);
412
413
414 #endif
415 /* end of gnunet-service-fs_cp.h */