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