added ATS addresstype information to unix
[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    * Point in time until which this peer does not want us to migrate content
97    * to it.
98    */
99   struct GNUNET_TIME_Absolute migration_blocked_until;
100
101   /**
102    * Transmission times for the last MAX_QUEUE_PER_PEER
103    * requests for this peer.  Used as a ring buffer, current
104    * offset is stored in 'last_request_times_off'.  If the
105    * oldest entry is more recent than the 'avg_delay', we should
106    * not send any more requests right now.
107    */
108   struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
109
110   /**
111    * How long does it typically take for us to transmit a message
112    * to this peer?  (delay between the request being issued and
113    * the callback being invoked).
114    */
115   struct GNUNET_LOAD_Value *transmission_delay;
116
117   /**
118    * Average priority of successful replies.  Calculated
119    * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
120    */
121   double avg_priority;
122
123   /**
124    * The peer's identity.
125    */
126   GNUNET_PEER_Id pid;
127
128   /**
129    * Trust rating for this peer
130    */
131   uint32_t trust;
132
133   /**
134    * Number of pending queries (replies are not counted)
135    */
136   unsigned int pending_queries;
137
138   /**
139    * Number of pending replies (queries are not counted)
140    */
141   unsigned int pending_replies;
142
143   /**
144    * How many of the last blocks from migration were duplicates?
145    */
146   unsigned int migration_duplication;
147 };
148
149
150 /**
151  * Signature of function called on a connected peer.
152  *
153  * @param cls closure
154  * @param peer identity of the peer
155  * @param cp handle to the connected peer record
156  * @param perf peer performance data
157  */
158 typedef void (*GSF_ConnectedPeerIterator) (void *cls,
159                                            const struct GNUNET_PeerIdentity *
160                                            peer, struct GSF_ConnectedPeer * cp,
161                                            const struct GSF_PeerPerformanceData
162                                            * ppd);
163
164
165 /**
166  * Function called to get a message for transmission.
167  *
168  * @param cls closure
169  * @param buf_size number of bytes available in buf
170  * @param buf where to copy the message, NULL on error (peer disconnect)
171  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
172  */
173 typedef size_t (*GSF_GetMessageCallback) (void *cls, 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 (*GSF_PeerReserveCallback) (void *cls,
185                                          struct GSF_ConnectedPeer * cp,
186                                          int success);
187
188
189 /**
190  * Handle to cancel a transmission request.
191  */
192 struct GSF_PeerTransmitHandle;
193
194
195 /**
196  * A peer connected to us.  Setup the connected peer
197  * records.
198  *
199  * @param peer identity of peer that connected
200  * @param atsi performance data for the connection
201  * @param atsi_count number of records in 'atsi'
202  * @return handle to connected peer entry
203  */
204 struct GSF_ConnectedPeer *
205 GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
206                            const struct GNUNET_ATS_Information *atsi,
207                            unsigned int atsi_count);
208
209
210 /**
211  * Get a handle for a connected peer.
212  *
213  * @param peer peer's identity
214  * @return NULL if this peer is not currently connected
215  */
216 struct GSF_ConnectedPeer *
217 GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer);
218
219
220 /**
221  * Transmit a message to the given peer as soon as possible.
222  * If the peer disconnects before the transmission can happen,
223  * the callback is invoked with a 'NULL' buffer.
224  *
225  * @param cp target peer
226  * @param is_query is this a query (GNUNET_YES) or content (GNUNET_NO)
227  * @param priority how important is this request?
228  * @param timeout when does this request timeout (call gmc with error)
229  * @param size number of bytes we would like to send to the peer
230  * @param gmc function to call to get the message
231  * @param gmc_cls closure for gmc
232  * @return handle to cancel request
233  */
234 struct GSF_PeerTransmitHandle *
235 GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp, int is_query,
236                     uint32_t priority, struct GNUNET_TIME_Relative timeout,
237                     size_t size, GSF_GetMessageCallback gmc, void *gmc_cls);
238
239
240 /**
241  * Cancel an earlier request for transmission.
242  *
243  * @param pth request to cancel
244  */
245 void
246 GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth);
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 "MIGRATION_STOP" message.
289  *
290  * @param cls closure, always NULL
291  * @param other the other peer involved (sender or receiver, NULL
292  *        for loopback messages where we are both sender and receiver)
293  * @param message the actual message
294  * @param atsi performance information
295  * @param atsi_count number of records in 'atsi'
296  * @return GNUNET_OK to keep the connection open,
297  *         GNUNET_SYSERR to close it (signal serious error)
298  */
299 int
300 GSF_handle_p2p_migration_stop_ (void *cls,
301                                 const struct GNUNET_PeerIdentity *other,
302                                 const struct GNUNET_MessageHeader *message,
303                                 const struct GNUNET_ATS_Information *atsi,
304                                 unsigned int atsi_count);
305
306
307 /**
308  * Handle P2P "QUERY" message.  Only responsible for creating the
309  * request entry itself and setting up reply callback and cancellation
310  * on peer disconnect.  Does NOT execute the actual request strategy
311  * (planning) or local database operations.
312  *
313  * @param other the other peer involved (sender or receiver, NULL
314  *        for loopback messages where we are both sender and receiver)
315  * @param message the actual message
316  * @return pending request handle, NULL on error
317  */
318 struct GSF_PendingRequest *
319 GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
320                        const struct GNUNET_MessageHeader *message);
321
322
323 /**
324  * Return the performance data record for the given peer
325  *
326  * @param cp peer to query
327  * @return performance data record for the peer
328  */
329 struct GSF_PeerPerformanceData *
330 GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp);
331
332
333 /**
334  * Ask a peer to stop migrating data to us until the given point
335  * in time.
336  *
337  * @param cp peer to ask
338  * @param block_time until when to block
339  */
340 void
341 GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
342                            struct GNUNET_TIME_Relative block_time);
343
344
345 /**
346  * A peer disconnected from us.  Tear down the connected peer
347  * record.
348  *
349  * @param cls unused
350  * @param peer identity of peer that connected
351  */
352 void
353 GSF_peer_disconnect_handler_ (void *cls,
354                               const struct GNUNET_PeerIdentity *peer);
355
356
357 /**
358  * Notification that a local client disconnected.  Clean up all of our
359  * references to the given handle.
360  *
361  * @param lc handle to the local client (henceforth invalid)
362  */
363 void
364 GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc);
365
366
367 /**
368  * Notify core about a preference we have for the given peer
369  * (to allocate more resources towards it).  The change will
370  * be communicated the next time we reserve bandwidth with
371  * core (not instantly).
372  *
373  * @param cp peer to reserve bandwidth from
374  * @param pref preference change
375  */
376 void
377 GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
378                                        uint64_t pref);
379
380
381 /**
382  * Obtain the identity of a connected peer.
383  *
384  * @param cp peer to reserve bandwidth from
385  * @param id identity to set (written to)
386  */
387 void
388 GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
389                                   struct GNUNET_PeerIdentity *id);
390
391
392 /**
393  * Iterate over all connected peers.
394  *
395  * @param it function to call for each peer
396  * @param it_cls closure for it
397  */
398 void
399 GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it, void *it_cls);
400
401
402 /**
403  * Initialize peer management subsystem.
404  */
405 void
406 GSF_connected_peer_init_ (void);
407
408
409 /**
410  * Shutdown peer management subsystem.
411  */
412 void
413 GSF_connected_peer_done_ (void);
414
415
416 #endif
417 /* end of gnunet-service-fs_cp.h */