aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs_pr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-service-fs_pr.c')
-rw-r--r--src/fs/gnunet-service-fs_pr.c1927
1 files changed, 0 insertions, 1927 deletions
diff --git a/src/fs/gnunet-service-fs_pr.c b/src/fs/gnunet-service-fs_pr.c
deleted file mode 100644
index 154c454ca..000000000
--- a/src/fs/gnunet-service-fs_pr.c
+++ /dev/null
@@ -1,1927 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 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_pr.c
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_load_lib.h"
29#include "gnunet-service-fs.h"
30#include "gnunet-service-fs_cp.h"
31#include "gnunet-service-fs_indexing.h"
32#include "gnunet-service-fs_pe.h"
33#include "gnunet-service-fs_pr.h"
34#include "gnunet-service-fs_cadet.h"
35
36
37/**
38 * Desired replication level for GETs.
39 */
40#define DHT_GET_REPLICATION 5
41
42/**
43 * Maximum size of the datastore queue for P2P operations. Needs to
44 * be large enough to queue #MAX_QUEUE_PER_PEER operations for roughly
45 * the number of active (connected) peers.
46 */
47#define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
48
49/**
50 * Bandwidth value of a 0-priority content (must be fairly high
51 * compared to query since content is typically significantly larger
52 * -- and more valuable since it can take many queries to get one
53 * piece of content).
54 */
55#define CONTENT_BANDWIDTH_VALUE 800
56
57/**
58 * Hard limit on the number of results we may get from the datastore per query.
59 */
60#define MAX_RESULTS (100 * 1024)
61
62/**
63 * Collect an instance number of statistics? May cause excessive IPC.
64 */
65#define INSANE_STATISTICS GNUNET_NO
66
67/**
68 * If obtaining a block via cadet fails, how often do we retry it before
69 * giving up for good (and sticking to non-anonymous transfer)?
70 */
71#define CADET_RETRY_MAX 3
72
73
74/**
75 * An active request.
76 */
77struct GSF_PendingRequest
78{
79 /**
80 * Public data for the request.
81 */
82 struct GSF_PendingRequestData public_data;
83
84 /**
85 * Function to call if we encounter a reply.
86 */
87 GSF_PendingRequestReplyHandler rh;
88
89 /**
90 * Closure for @e rh
91 */
92 void *rh_cls;
93
94 /**
95 * Array of hash codes of replies we've already seen.
96 */
97 struct GNUNET_HashCode *replies_seen;
98
99 /**
100 * Block group for filtering replies we've already seen.
101 */
102 struct GNUNET_BLOCK_Group *bg;
103
104 /**
105 * Entry for this pending request in the expiration heap, or NULL.
106 */
107 struct GNUNET_CONTAINER_HeapNode *hnode;
108
109 /**
110 * Datastore queue entry for this request (or NULL for none).
111 */
112 struct GNUNET_DATASTORE_QueueEntry *qe;
113
114 /**
115 * DHT request handle for this request (or NULL for none).
116 */
117 struct GNUNET_DHT_GetHandle *gh;
118
119 /**
120 * Cadet request handle for this request (or NULL for none).
121 */
122 struct GSF_CadetRequest *cadet_request;
123
124 /**
125 * Function to call upon completion of the local get
126 * request, or NULL for none.
127 */
128 GSF_LocalLookupContinuation llc_cont;
129
130 /**
131 * Closure for @e llc_cont.
132 */
133 void *llc_cont_cls;
134
135 /**
136 * Last result from the local datastore lookup evaluation.
137 */
138 enum GNUNET_BLOCK_ReplyEvaluationResult local_result;
139
140 /**
141 * Identity of the peer that we should use for the 'sender'
142 * (recipient of the response) when forwarding (0 for none).
143 */
144 GNUNET_PEER_Id sender_pid;
145
146 /**
147 * Identity of the peer that we should never forward this query
148 * to since it originated this query (0 for none).
149 */
150 GNUNET_PEER_Id origin_pid;
151
152 /**
153 * Time we started the last datastore lookup.
154 */
155 struct GNUNET_TIME_Absolute qe_start;
156
157 /**
158 * Task that warns us if the local datastore lookup takes too long.
159 */
160 struct GNUNET_SCHEDULER_Task *warn_task;
161
162 /**
163 * Do we have a first UID yet?
164 */
165 bool have_first_uid;
166
167 /**
168 * Have we seen a NULL result yet?
169 */
170 bool seen_null;
171
172 /**
173 * Unique ID of the first result from the local datastore;
174 * used to terminate the loop.
175 */
176 uint64_t first_uid;
177
178 /**
179 * Result count.
180 */
181 size_t result_count;
182
183 /**
184 * How often have we retried this request via 'cadet'?
185 * (used to bound overall retries).
186 */
187 unsigned int cadet_retry_count;
188
189 /**
190 * Number of valid entries in the 'replies_seen' array.
191 */
192 unsigned int replies_seen_count;
193
194 /**
195 * Length of the 'replies_seen' array.
196 */
197 unsigned int replies_seen_size;
198};
199
200
201/**
202 * All pending requests, ordered by the query. Entries
203 * are of type 'struct GSF_PendingRequest*'.
204 */
205static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
206
207
208/**
209 * Datastore 'PUT' load tracking.
210 */
211static struct GNUNET_LOAD_Value *datastore_put_load;
212
213
214/**
215 * Are we allowed to migrate content to this peer.
216 */
217static int active_to_migration;
218
219
220/**
221 * Heap with the request that will expire next at the top. Contains
222 * pointers of type "struct PendingRequest*"; these will *also* be
223 * aliased from the "requests_by_peer" data structures and the
224 * "requests_by_query" table. Note that requests from our clients
225 * don't expire and are thus NOT in the "requests_by_expiration"
226 * (or the "requests_by_peer" tables).
227 */
228static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
229
230
231/**
232 * Maximum number of requests (from other peers, overall) that we're
233 * willing to have pending at any given point in time. Can be changed
234 * via the configuration file (32k is just the default).
235 */
236static unsigned long long max_pending_requests = (32 * 1024);
237
238
239/**
240 * Recalculate our bloom filter for filtering replies. This function
241 * will create a new bloom filter from scratch, so it should only be
242 * called if we have no bloomfilter at all (and hence can create a
243 * fresh one of minimal size without problems) OR if our peer is the
244 * initiator (in which case we may resize to larger than minimum size).
245 *
246 * @param type type of the request
247 * @param pr request for which the BF is to be recomputed
248 */
249static void
250refresh_bloomfilter (enum GNUNET_BLOCK_Type type,
251 struct GSF_PendingRequest *pr)
252{
253 if (NULL != pr->bg)
254 {
255 GNUNET_BLOCK_group_destroy (pr->bg);
256 pr->bg = NULL;
257 }
258 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
259 return; /* no need */
260 pr->bg =
261 GNUNET_BLOCK_group_create (GSF_block_ctx,
262 type,
263 GNUNET_CRYPTO_random_u32 (
264 GNUNET_CRYPTO_QUALITY_WEAK,
265 UINT32_MAX),
266 NULL,
267 0,
268 "seen-set-size",
269 pr->replies_seen_count,
270 NULL);
271 if (NULL == pr->bg)
272 return;
273 GNUNET_break (GNUNET_OK ==
274 GNUNET_BLOCK_group_set_seen (pr->bg,
275 pr->replies_seen,
276 pr->replies_seen_count));
277}
278
279
280/**
281 * Create a new pending request.
282 *
283 * @param options request options
284 * @param type type of the block that is being requested
285 * @param query key for the lookup
286 * @param target preferred target for the request, NULL for none
287 * @param bf_data raw data for bloom filter for known replies, can be NULL
288 * @param bf_size number of bytes in @a bf_data
289 * @param mingle mingle value for bf
290 * @param anonymity_level desired anonymity level
291 * @param priority maximum outgoing cumulative request priority to use
292 * @param ttl current time-to-live for the request
293 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
294 * @param origin_pid peer ID of origin of query (do not loop back)
295 * @param replies_seen hash codes of known local replies
296 * @param replies_seen_count size of the @a replies_seen array
297 * @param rh handle to call when we get a reply
298 * @param rh_cls closure for @a rh
299 * @return handle for the new pending request
300 */
301struct GSF_PendingRequest *
302GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
303 enum GNUNET_BLOCK_Type type,
304 const struct GNUNET_HashCode *query,
305 const struct GNUNET_PeerIdentity *target,
306 const char *bf_data,
307 size_t bf_size,
308 uint32_t mingle,
309 uint32_t anonymity_level,
310 uint32_t priority,
311 int32_t ttl,
312 GNUNET_PEER_Id sender_pid,
313 GNUNET_PEER_Id origin_pid,
314 const struct GNUNET_HashCode *replies_seen,
315 unsigned int replies_seen_count,
316 GSF_PendingRequestReplyHandler rh,
317 void *rh_cls)
318{
319 struct GSF_PendingRequest *pr;
320 struct GSF_PendingRequest *dpr;
321 size_t extra;
322 struct GNUNET_HashCode *eptr;
323
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Creating request handle for `%s' of type %d\n",
326 GNUNET_h2s (query),
327 type);
328#if INSANE_STATISTICS
329 GNUNET_STATISTICS_update (GSF_stats,
330 gettext_noop ("# Pending requests created"),
331 1,
332 GNUNET_NO);
333#endif
334 extra = 0;
335 if (NULL != target)
336 extra += sizeof(struct GNUNET_PeerIdentity);
337 pr = GNUNET_malloc (sizeof(struct GSF_PendingRequest) + extra);
338 pr->public_data.query = *query;
339 eptr = (struct GNUNET_HashCode *) &pr[1];
340 if (NULL != target)
341 {
342 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
343 GNUNET_memcpy (eptr, target, sizeof(struct GNUNET_PeerIdentity));
344 }
345 pr->public_data.anonymity_level = anonymity_level;
346 pr->public_data.priority = priority;
347 pr->public_data.original_priority = priority;
348 pr->public_data.options = options;
349 pr->public_data.type = type;
350 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
351 pr->sender_pid = sender_pid;
352 pr->origin_pid = origin_pid;
353 pr->rh = rh;
354 pr->rh_cls = rh_cls;
355 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
356 if (ttl >= 0)
357 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (
358 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (uint32_t) ttl));
359 else
360 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (
361 pr->public_data.start_time,
362 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
363 (uint32_t) (-ttl)));
364 if (replies_seen_count > 0)
365 {
366 pr->replies_seen_size = replies_seen_count;
367 pr->replies_seen =
368 GNUNET_new_array (pr->replies_seen_size, struct GNUNET_HashCode);
369 GNUNET_memcpy (pr->replies_seen,
370 replies_seen,
371 replies_seen_count * sizeof(struct GNUNET_HashCode));
372 pr->replies_seen_count = replies_seen_count;
373 }
374 if ((NULL != bf_data) &&
375 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type))
376 {
377 pr->bg = GNUNET_BLOCK_group_create (GSF_block_ctx,
378 pr->public_data.type,
379 mingle,
380 bf_data,
381 bf_size,
382 "seen-set-size",
383 0,
384 NULL);
385 }
386 else if ((replies_seen_count > 0) &&
387 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
388 {
389 refresh_bloomfilter (pr->public_data.type, pr);
390 }
391 GNUNET_CONTAINER_multihashmap_put (pr_map,
392 &pr->public_data.query,
393 pr,
394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
395 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
396 {
397 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
398 pr,
399 pr->public_data.ttl.abs_value_us);
400 /* make sure we don't track too many requests */
401 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
402 max_pending_requests)
403 {
404 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
405 GNUNET_assert (NULL != dpr);
406 if (pr == dpr)
407 break; /* let the request live briefly... */
408 if (NULL != dpr->rh)
409 dpr->rh (dpr->rh_cls,
410 GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED,
411 dpr,
412 UINT32_MAX,
413 GNUNET_TIME_UNIT_FOREVER_ABS,
414 GNUNET_TIME_UNIT_FOREVER_ABS,
415 GNUNET_BLOCK_TYPE_ANY,
416 NULL,
417 0);
418 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
419 }
420 }
421 GNUNET_STATISTICS_update (GSF_stats,
422 gettext_noop ("# Pending requests active"),
423 1,
424 GNUNET_NO);
425 return pr;
426}
427
428
429/**
430 * Obtain the public data associated with a pending request
431 *
432 * @param pr pending request
433 * @return associated public data
434 */
435struct GSF_PendingRequestData *
436GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
437{
438 return &pr->public_data;
439}
440
441
442/**
443 * Test if two pending requests are compatible (would generate
444 * the same query modulo filters and should thus be processed
445 * jointly).
446 *
447 * @param pra a pending request
448 * @param prb another pending request
449 * @return #GNUNET_OK if the requests are compatible
450 */
451int
452GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
453 struct GSF_PendingRequest *prb)
454{
455 if ((pra->public_data.type != prb->public_data.type) ||
456 (0 != memcmp (&pra->public_data.query,
457 &prb->public_data.query,
458 sizeof(struct GNUNET_HashCode))))
459 return GNUNET_NO;
460 return GNUNET_OK;
461}
462
463
464/**
465 * Update a given pending request with additional replies
466 * that have been seen.
467 *
468 * @param pr request to update
469 * @param replies_seen hash codes of replies that we've seen
470 * @param replies_seen_count size of the replies_seen array
471 */
472void
473GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
474 const struct GNUNET_HashCode *replies_seen,
475 unsigned int replies_seen_count)
476{
477 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
478 return; /* integer overflow */
479 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
480 {
481 /* we're responsible for the BF, full refresh */
482 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
483 GNUNET_array_grow (pr->replies_seen,
484 pr->replies_seen_size,
485 replies_seen_count + pr->replies_seen_count);
486 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
487 replies_seen,
488 sizeof(struct GNUNET_HashCode) * replies_seen_count);
489 pr->replies_seen_count += replies_seen_count;
490 refresh_bloomfilter (pr->public_data.type, pr);
491 }
492 else
493 {
494 if (NULL == pr->bg)
495 {
496 /* we're not the initiator, but the initiator did not give us
497 * any bloom-filter, so we need to create one on-the-fly */
498 refresh_bloomfilter (pr->public_data.type, pr);
499 }
500 else
501 {
502 GNUNET_break (GNUNET_OK ==
503 GNUNET_BLOCK_group_set_seen (pr->bg,
504 replies_seen,
505 pr->replies_seen_count));
506 }
507 }
508 if (NULL != pr->gh)
509 GNUNET_DHT_get_filter_known_results (pr->gh,
510 replies_seen_count,
511 replies_seen);
512}
513
514
515/**
516 * Generate the message corresponding to the given pending request for
517 * transmission to other peers.
518 *
519 * @param pr request to generate the message for
520 * @return envelope with the request message
521 */
522struct GNUNET_MQ_Envelope *
523GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
524{
525 struct GNUNET_MQ_Envelope *env;
526 struct GetMessage *gm;
527 struct GNUNET_PeerIdentity *ext;
528 unsigned int k;
529 uint32_t bm;
530 uint32_t prio;
531 size_t bf_size;
532 struct GNUNET_TIME_Absolute now;
533 int64_t ttl;
534 int do_route;
535 void *bf_data;
536 uint32_t bf_nonce;
537
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Building request message for `%s' of type %d\n",
540 GNUNET_h2s (&pr->public_data.query),
541 pr->public_data.type);
542 k = 0;
543 bm = 0;
544 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
545 if ((! do_route) && (pr->sender_pid == 0))
546 {
547 GNUNET_break (0);
548 do_route = GNUNET_YES;
549 }
550 if (! do_route)
551 {
552 bm |= GET_MESSAGE_BIT_RETURN_TO;
553 k++;
554 }
555 if (NULL != pr->public_data.target)
556 {
557 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
558 k++;
559 }
560 if (GNUNET_OK !=
561 GNUNET_BLOCK_group_serialize (pr->bg,
562 &bf_nonce,
563 &bf_data,
564 &bf_size))
565 {
566 bf_size = 0;
567 bf_data = NULL;
568 }
569 env = GNUNET_MQ_msg_extra (gm,
570 bf_size + k * sizeof(struct GNUNET_PeerIdentity),
571 GNUNET_MESSAGE_TYPE_FS_GET);
572 gm->type = htonl (pr->public_data.type);
573 if (do_route)
574 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
575 pr->public_data.priority + 1);
576 else
577 prio = 0;
578 pr->public_data.priority -= prio;
579 pr->public_data.num_transmissions++;
580 pr->public_data.respect_offered += prio;
581 gm->priority = htonl (prio);
582 now = GNUNET_TIME_absolute_get ();
583 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
584 gm->ttl = htonl (ttl / 1000LL / 1000LL);
585 gm->filter_mutator = htonl (bf_nonce);
586 gm->hash_bitmap = htonl (bm);
587 gm->query = pr->public_data.query;
588 ext = (struct GNUNET_PeerIdentity *) &gm[1];
589 k = 0;
590 if (! do_route)
591 GNUNET_PEER_resolve (pr->sender_pid, &ext[k++]);
592 if (NULL != pr->public_data.target)
593 ext[k++] = *pr->public_data.target;
594 GNUNET_memcpy (&ext[k], bf_data, bf_size);
595 GNUNET_free (bf_data);
596 return env;
597}
598
599
600/**
601 * Iterator to free pending requests.
602 *
603 * @param cls closure, unused
604 * @param key current key code
605 * @param value value in the hash map (pending request)
606 * @return #GNUNET_YES (we should continue to iterate)
607 */
608static int
609clean_request (void *cls, const struct GNUNET_HashCode *key, void *value)
610{
611 struct GSF_PendingRequest *pr = value;
612 GSF_LocalLookupContinuation cont;
613
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Cleaning up pending request for `%s'.\n",
616 GNUNET_h2s (key));
617 if (NULL != pr->cadet_request)
618 {
619 pr->cadet_retry_count = CADET_RETRY_MAX;
620 GSF_cadet_query_cancel (pr->cadet_request);
621 pr->cadet_request = NULL;
622 }
623 if (NULL != (cont = pr->llc_cont))
624 {
625 pr->llc_cont = NULL;
626 cont (pr->llc_cont_cls,
627 pr,
628 pr->local_result);
629 }
630 GSF_plan_notify_request_done_ (pr);
631 GNUNET_free (pr->replies_seen);
632 GNUNET_BLOCK_group_destroy (pr->bg);
633 pr->bg = NULL;
634 GNUNET_PEER_change_rc (pr->sender_pid, -1);
635 pr->sender_pid = 0;
636 GNUNET_PEER_change_rc (pr->origin_pid, -1);
637 pr->origin_pid = 0;
638 if (NULL != pr->hnode)
639 {
640 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
641 pr->hnode = NULL;
642 }
643 if (NULL != pr->qe)
644 {
645 GNUNET_DATASTORE_cancel (pr->qe);
646 pr->qe = NULL;
647 }
648 if (NULL != pr->gh)
649 {
650 GNUNET_DHT_get_stop (pr->gh);
651 pr->gh = NULL;
652 }
653 if (NULL != pr->warn_task)
654 {
655 GNUNET_SCHEDULER_cancel (pr->warn_task);
656 pr->warn_task = NULL;
657 }
658 GNUNET_assert (
659 GNUNET_OK ==
660 GNUNET_CONTAINER_multihashmap_remove (pr_map, &pr->public_data.query, pr));
661 GNUNET_STATISTICS_update (GSF_stats,
662 gettext_noop ("# Pending requests active"),
663 -1,
664 GNUNET_NO);
665 GNUNET_free (pr);
666 return GNUNET_YES;
667}
668
669
670/**
671 * Explicitly cancel a pending request.
672 *
673 * @param pr request to cancel
674 * @param full_cleanup fully purge the request
675 */
676void
677GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
678{
679 GSF_LocalLookupContinuation cont;
680
681 if (NULL == pr_map)
682 return; /* already cleaned up! */
683 if (GNUNET_NO == full_cleanup)
684 {
685 /* make request inactive (we're no longer interested in more results),
686 * but do NOT remove from our data-structures, we still need it there
687 * to prevent the request from looping */
688 pr->rh = NULL;
689 if (NULL != pr->cadet_request)
690 {
691 pr->cadet_retry_count = CADET_RETRY_MAX;
692 GSF_cadet_query_cancel (pr->cadet_request);
693 pr->cadet_request = NULL;
694 }
695 if (NULL != (cont = pr->llc_cont))
696 {
697 pr->llc_cont = NULL;
698 cont (pr->llc_cont_cls,
699 pr,
700 pr->local_result);
701 }
702 GSF_plan_notify_request_done_ (pr);
703 if (NULL != pr->qe)
704 {
705 GNUNET_DATASTORE_cancel (pr->qe);
706 pr->qe = NULL;
707 }
708 if (NULL != pr->gh)
709 {
710 GNUNET_DHT_get_stop (pr->gh);
711 pr->gh = NULL;
712 }
713 if (NULL != pr->warn_task)
714 {
715 GNUNET_SCHEDULER_cancel (pr->warn_task);
716 pr->warn_task = NULL;
717 }
718 return;
719 }
720 GNUNET_assert (GNUNET_YES ==
721 clean_request (NULL, &pr->public_data.query, pr));
722}
723
724
725/**
726 * Iterate over all pending requests.
727 *
728 * @param it function to call for each request
729 * @param cls closure for @a it
730 */
731void
732GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
733{
734 GNUNET_CONTAINER_multihashmap_iterate (
735 pr_map,
736 (GNUNET_CONTAINER_MulitHashMapIteratorCallback) it,
737 cls);
738}
739
740
741/**
742 * Closure for process_reply() function.
743 */
744struct ProcessReplyClosure
745{
746 /**
747 * The data for the reply.
748 */
749 const void *data;
750
751 /**
752 * Who gave us this reply? NULL for local host (or DHT)
753 */
754 struct GSF_ConnectedPeer *sender;
755
756 /**
757 * When the reply expires.
758 */
759 struct GNUNET_TIME_Absolute expiration;
760
761 /**
762 * Size of data.
763 */
764 size_t size;
765
766 /**
767 * Type of the block.
768 */
769 enum GNUNET_BLOCK_Type type;
770
771 /**
772 * How much was this reply worth to us?
773 */
774 uint32_t priority;
775
776 /**
777 * Anonymity requirements for this reply.
778 */
779 uint32_t anonymity_level;
780
781 /**
782 * Evaluation result (returned).
783 */
784 enum GNUNET_BLOCK_ReplyEvaluationResult eval;
785
786 /**
787 * Did we find a matching request?
788 */
789 int request_found;
790};
791
792
793/**
794 * Update the performance data for the sender (if any) since
795 * the sender successfully answered one of our queries.
796 *
797 * @param prq information about the sender
798 * @param pr request that was satisfied
799 */
800static void
801update_request_performance_data (struct ProcessReplyClosure *prq,
802 struct GSF_PendingRequest *pr)
803{
804 if (prq->sender == NULL)
805 return;
806 GSF_peer_update_performance_ (prq->sender,
807 pr->public_data.start_time,
808 prq->priority);
809}
810
811
812/**
813 * We have received a reply; handle it!
814 *
815 * @param cls response (a `struct ProcessReplyClosure`)
816 * @param key our query
817 * @param value value in the hash map (info about the query)
818 * @return #GNUNET_YES (we should continue to iterate)
819 */
820static enum GNUNET_GenericReturnValue
821process_reply (void *cls,
822 const struct GNUNET_HashCode *key,
823 void *value)
824{
825 struct ProcessReplyClosure *prq = cls;
826 struct GSF_PendingRequest *pr = value;
827 struct GNUNET_HashCode chash;
828 struct GNUNET_TIME_Absolute last_transmission;
829
830 if (NULL == pr->rh)
831 return GNUNET_YES;
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Matched result (type %u) for query `%s' with pending request\n",
834 (unsigned int) prq->type,
835 GNUNET_h2s (key));
836 GNUNET_STATISTICS_update (GSF_stats,
837 gettext_noop ("# replies received and matched"),
838 1,
839 GNUNET_NO);
840 prq->eval = GNUNET_BLOCK_check_reply (GSF_block_ctx,
841 prq->type,
842 pr->bg,
843 key,
844 NULL, 0,
845 prq->data,
846 prq->size);
847 switch (prq->eval)
848 {
849 case GNUNET_BLOCK_REPLY_OK_MORE:
850 update_request_performance_data (prq, pr);
851 break;
852 case GNUNET_BLOCK_REPLY_OK_LAST:
853 /* short cut: stop processing early, no BF-update, etc. */
854 update_request_performance_data (prq, pr);
855 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
856 GNUNET_TIME_absolute_get_duration (
857 pr->public_data.start_time)
858 .rel_value_us);
859 if (GNUNET_YES !=
860 GSF_request_plan_reference_get_last_transmission_ (pr->public_data
861 .pr_head,
862 prq->sender,
863 &last_transmission))
864 last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
865 /* pass on to other peers / local clients */
866 pr->rh (pr->rh_cls,
867 prq->eval,
868 pr,
869 prq->anonymity_level,
870 prq->expiration,
871 last_transmission,
872 prq->type,
873 prq->data,
874 prq->size);
875 return GNUNET_YES;
876 case GNUNET_BLOCK_REPLY_OK_DUPLICATE:
877#if INSANE_STATISTICS
878 GNUNET_STATISTICS_update (GSF_stats,
879 "# duplicate replies discarded (bloomfilter)",
880 1,
881 GNUNET_NO);
882#endif
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Duplicate response, discarding.\n");
885 return GNUNET_YES; /* duplicate */
886 case GNUNET_BLOCK_REPLY_IRRELEVANT:
887 GNUNET_STATISTICS_update (GSF_stats,
888 "# irrelevant replies discarded",
889 1,
890 GNUNET_NO);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Irrelevant response, ignoring.\n");
893 return GNUNET_YES;
894 case GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED:
895 GNUNET_break (0); /* bad installation? */
896 return GNUNET_NO;
897 }
898 /* update bloomfilter */
899 GNUNET_CRYPTO_hash (prq->data,
900 prq->size,
901 &chash);
902 GSF_pending_request_update_ (pr,
903 &chash,
904 1);
905 if (NULL == prq->sender)
906 {
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Found result for query `%s' in local datastore\n",
909 GNUNET_h2s (key));
910 GNUNET_STATISTICS_update (GSF_stats,
911 gettext_noop ("# results found locally"),
912 1,
913 GNUNET_NO);
914 }
915 else
916 {
917 GSF_dht_lookup_ (pr);
918 }
919 prq->priority += pr->public_data.original_priority;
920 pr->public_data.priority = 0;
921 pr->public_data.original_priority = 0;
922 pr->public_data.results_found++;
923 prq->request_found = GNUNET_YES;
924 /* finally, pass on to other peer / local client */
925 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data
926 .pr_head,
927 prq->sender,
928 &last_transmission))
929 last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
930 pr->rh (pr->rh_cls,
931 prq->eval,
932 pr,
933 prq->anonymity_level,
934 prq->expiration,
935 last_transmission,
936 prq->type,
937 prq->data,
938 prq->size);
939 return GNUNET_YES;
940}
941
942
943/**
944 * Context for put_migration_continuation().
945 */
946struct PutMigrationContext
947{
948 /**
949 * Start time for the operation.
950 */
951 struct GNUNET_TIME_Absolute start;
952
953 /**
954 * Request origin.
955 */
956 struct GNUNET_PeerIdentity origin;
957
958 /**
959 * #GNUNET_YES if we had a matching request for this block,
960 * #GNUNET_NO if not.
961 */
962 int requested;
963};
964
965
966/**
967 * Continuation called to notify client about result of the
968 * operation.
969 *
970 * @param cls closure
971 * @param success #GNUNET_SYSERR on failure
972 * @param min_expiration minimum expiration time required for content to be stored
973 * @param msg NULL on success, otherwise an error message
974 */
975static void
976put_migration_continuation (void *cls,
977 int success,
978 struct GNUNET_TIME_Absolute min_expiration,
979 const char *msg)
980{
981 struct PutMigrationContext *pmc = cls;
982 struct GSF_ConnectedPeer *cp;
983 struct GNUNET_TIME_Relative mig_pause;
984 struct GSF_PeerPerformanceData *ppd;
985
986 if (NULL != datastore_put_load)
987 {
988 if (GNUNET_SYSERR != success)
989 {
990 GNUNET_LOAD_update (datastore_put_load,
991 GNUNET_TIME_absolute_get_duration (pmc->start)
992 .rel_value_us);
993 }
994 else
995 {
996 /* on queue failure / timeout, increase the put load dramatically */
997 GNUNET_LOAD_update (datastore_put_load,
998 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
999 }
1000 }
1001 cp = GSF_peer_get_ (&pmc->origin);
1002 if (GNUNET_OK == success)
1003 {
1004 if (NULL != cp)
1005 {
1006 ppd = GSF_get_peer_performance_data_ (cp);
1007 ppd->migration_delay.rel_value_us /= 2;
1008 }
1009 GNUNET_free (pmc);
1010 return;
1011 }
1012 if ((GNUNET_NO == success) && (GNUNET_NO == pmc->requested) && (NULL != cp))
1013 {
1014 ppd = GSF_get_peer_performance_data_ (cp);
1015 if (min_expiration.abs_value_us > 0)
1016 {
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Asking to stop migration for %s because datastore is full\n",
1019 GNUNET_STRINGS_relative_time_to_string (
1020 GNUNET_TIME_absolute_get_remaining (min_expiration),
1021 GNUNET_YES));
1022 GSF_block_peer_migration_ (cp, min_expiration);
1023 }
1024 else
1025 {
1026 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1027 ppd->migration_delay);
1028 ppd->migration_delay =
1029 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS, ppd->migration_delay);
1030 mig_pause.rel_value_us =
1031 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1032 ppd->migration_delay.rel_value_us);
1033 ppd->migration_delay =
1034 GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1035 GNUNET_log (
1036 GNUNET_ERROR_TYPE_DEBUG,
1037 "Replicated content already exists locally, asking to stop migration for %s\n",
1038 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
1039 GSF_block_peer_migration_ (cp,
1040 GNUNET_TIME_relative_to_absolute (mig_pause));
1041 }
1042 }
1043 GNUNET_free (pmc);
1044 GNUNET_STATISTICS_update (GSF_stats,
1045 gettext_noop ("# Datastore `PUT' failures"),
1046 1,
1047 GNUNET_NO);
1048}
1049
1050
1051/**
1052 * Test if the DATABASE (PUT) load on this peer is too high
1053 * to even consider processing the query at
1054 * all.
1055 *
1056 * @param priority the priority of the item
1057 * @return #GNUNET_YES if the load is too high to do anything (load high)
1058 * #GNUNET_NO to process normally (load normal or low)
1059 */
1060static int
1061test_put_load_too_high (uint32_t priority)
1062{
1063 double ld;
1064
1065 if (NULL == datastore_put_load)
1066 return GNUNET_NO;
1067 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1068 return GNUNET_NO; /* very fast */
1069 ld = GNUNET_LOAD_get_load (datastore_put_load);
1070 if (ld < 2.0 * (1 + priority))
1071 return GNUNET_NO;
1072 GNUNET_STATISTICS_update (GSF_stats,
1073 gettext_noop (
1074 "# storage requests dropped due to high load"),
1075 1,
1076 GNUNET_NO);
1077 return GNUNET_YES;
1078}
1079
1080
1081/**
1082 * Iterator called on each result obtained for a DHT
1083 * operation that expects a reply
1084 *
1085 * @param cls closure
1086 * @param exp when will this value expire
1087 * @param key key of the result
1088 * @param get_path peers on reply path (or NULL if not recorded)
1089 * @param get_path_length number of entries in @a get_path
1090 * @param put_path peers on the PUT path (or NULL if not recorded)
1091 * @param put_path_length number of entries in @a get_path
1092 * @param type type of the result
1093 * @param size number of bytes in @a data
1094 * @param data pointer to the result data
1095 */
1096static void
1097handle_dht_reply (void *cls,
1098 struct GNUNET_TIME_Absolute exp,
1099 const struct GNUNET_HashCode *key,
1100 const struct GNUNET_DHT_PathElement *get_path,
1101 unsigned int get_path_length,
1102 const struct GNUNET_DHT_PathElement *put_path,
1103 unsigned int put_path_length,
1104 enum GNUNET_BLOCK_Type type,
1105 size_t size,
1106 const void *data)
1107{
1108 struct GSF_PendingRequest *pr = cls;
1109 struct ProcessReplyClosure prq;
1110 struct PutMigrationContext *pmc;
1111
1112 GNUNET_STATISTICS_update (GSF_stats,
1113 gettext_noop ("# Replies received from DHT"),
1114 1,
1115 GNUNET_NO);
1116 memset (&prq, 0, sizeof(prq));
1117 prq.data = data;
1118 prq.expiration = exp;
1119 /* do not allow migrated content to live longer than 1 year */
1120 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1121 GNUNET_TIME_UNIT_YEARS),
1122 prq.expiration);
1123 prq.size = size;
1124 prq.type = type;
1125 process_reply (&prq,
1126 key,
1127 pr);
1128 if ((GNUNET_YES == active_to_migration) &&
1129 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1130 {
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Replicating result for query `%s' with priority %u\n",
1133 GNUNET_h2s (key),
1134 prq.priority);
1135 pmc = GNUNET_new (struct PutMigrationContext);
1136 pmc->start = GNUNET_TIME_absolute_get ();
1137 pmc->requested = GNUNET_YES;
1138 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1139 0,
1140 key,
1141 size,
1142 data,
1143 type,
1144 prq.priority,
1145 1 /* anonymity */,
1146 0 /* replication */,
1147 exp,
1148 1 + prq.priority,
1149 MAX_DATASTORE_QUEUE,
1150 &put_migration_continuation,
1151 pmc))
1152 {
1153 put_migration_continuation (pmc,
1154 GNUNET_SYSERR,
1155 GNUNET_TIME_UNIT_ZERO_ABS,
1156 NULL);
1157 }
1158 }
1159}
1160
1161
1162/**
1163 * Consider looking up the data in the DHT (anonymity-level permitting).
1164 *
1165 * @param pr the pending request to process
1166 */
1167void
1168GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1169{
1170 const void *xquery;
1171 size_t xquery_size;
1172 struct GNUNET_PeerIdentity pi;
1173 char buf[sizeof(struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1174
1175 if (0 != pr->public_data.anonymity_level)
1176 return;
1177 if (NULL != pr->gh)
1178 {
1179 GNUNET_DHT_get_stop (pr->gh);
1180 pr->gh = NULL;
1181 }
1182 xquery = NULL;
1183 xquery_size = 0;
1184 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1185 {
1186 GNUNET_assert (0 != pr->sender_pid);
1187 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1188 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof(struct GNUNET_PeerIdentity));
1189 xquery_size += sizeof(struct GNUNET_PeerIdentity);
1190 }
1191 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1192 pr->public_data.type,
1193 &pr->public_data.query,
1194 DHT_GET_REPLICATION,
1195 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1196 xquery,
1197 xquery_size,
1198 &handle_dht_reply,
1199 pr);
1200 if ((NULL != pr->gh) && (0 != pr->replies_seen_count))
1201 GNUNET_DHT_get_filter_known_results (pr->gh,
1202 pr->replies_seen_count,
1203 pr->replies_seen);
1204}
1205
1206
1207/**
1208 * Function called with a reply from the cadet.
1209 *
1210 * @param cls the pending request struct
1211 * @param type type of the block, ANY on error
1212 * @param expiration expiration time for the block
1213 * @param data_size number of bytes in @a data, 0 on error
1214 * @param data reply block data, NULL on error
1215 */
1216static void
1217cadet_reply_proc (void *cls,
1218 enum GNUNET_BLOCK_Type type,
1219 struct GNUNET_TIME_Absolute expiration,
1220 size_t data_size,
1221 const void *data)
1222{
1223 struct GSF_PendingRequest *pr = cls;
1224 struct ProcessReplyClosure prq;
1225 struct GNUNET_HashCode query;
1226
1227 pr->cadet_request = NULL;
1228 if (GNUNET_OK !=
1229 GNUNET_BLOCK_check_block (GSF_block_ctx,
1230 type,
1231 data,
1232 data_size))
1233 {
1234 GNUNET_break_op (0);
1235 return;
1236 }
1237 if (GNUNET_BLOCK_TYPE_ANY == type)
1238 {
1239 GNUNET_break (NULL == data);
1240 GNUNET_break (0 == data_size);
1241 pr->cadet_retry_count++;
1242 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1243 return; /* give up on cadet */
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error retrieiving block via cadet\n");
1245 /* retry -- without delay, as this is non-anonymous
1246 and cadet/cadet connect will take some time anyway */
1247 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1248 &pr->public_data.query,
1249 pr->public_data.type,
1250 &cadet_reply_proc,
1251 pr);
1252 return;
1253 }
1254 if (GNUNET_YES !=
1255 GNUNET_BLOCK_get_key (GSF_block_ctx,
1256 type,
1257 data,
1258 data_size,
1259 &query))
1260 {
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Failed to derive key for block of type %d\n",
1263 (int) type);
1264 GNUNET_break_op (0);
1265 return;
1266 }
1267 GNUNET_STATISTICS_update (GSF_stats,
1268 gettext_noop ("# Replies received from CADET"),
1269 1,
1270 GNUNET_NO);
1271 memset (&prq, 0, sizeof(prq));
1272 prq.data = data;
1273 prq.expiration = expiration;
1274 /* do not allow migrated content to live longer than 1 year */
1275 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1276 GNUNET_TIME_UNIT_YEARS),
1277 prq.expiration);
1278 prq.size = data_size;
1279 prq.type = type;
1280 process_reply (&prq,
1281 &query,
1282 pr);
1283}
1284
1285
1286/**
1287 * Consider downloading via cadet (if possible)
1288 *
1289 * @param pr the pending request to process
1290 */
1291void
1292GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1293{
1294 if (0 != pr->public_data.anonymity_level)
1295 return;
1296 if (0 == pr->public_data.target)
1297 {
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Cannot do cadet-based download, target peer not known\n");
1300 return;
1301 }
1302 if (NULL != pr->cadet_request)
1303 return;
1304 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1305 &pr->public_data.query,
1306 pr->public_data.type,
1307 &cadet_reply_proc,
1308 pr);
1309}
1310
1311
1312/**
1313 * Task that issues a warning if the datastore lookup takes too long.
1314 *
1315 * @param cls the `struct GSF_PendingRequest`
1316 */
1317static void
1318warn_delay_task (void *cls)
1319{
1320 struct GSF_PendingRequest *pr = cls;
1321
1322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1323 _ ("Datastore lookup already took %s!\n"),
1324 GNUNET_STRINGS_relative_time_to_string (
1325 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1326 GNUNET_YES));
1327 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1328 &warn_delay_task,
1329 pr);
1330}
1331
1332
1333/**
1334 * Task that issues a warning if the datastore lookup takes too long.
1335 *
1336 * @param cls the `struct GSF_PendingRequest`
1337 */
1338static void
1339odc_warn_delay_task (void *cls)
1340{
1341 struct GSF_PendingRequest *pr = cls;
1342
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 _ ("On-demand lookup already took %s!\n"),
1345 GNUNET_STRINGS_relative_time_to_string (
1346 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1347 GNUNET_YES));
1348 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1349 &odc_warn_delay_task,
1350 pr);
1351}
1352
1353
1354/* Call our continuation (if we have any) */
1355static void
1356call_continuation (struct GSF_PendingRequest *pr)
1357{
1358 GSF_LocalLookupContinuation cont = pr->llc_cont;
1359
1360 GNUNET_assert (NULL == pr->qe);
1361 if (NULL != pr->warn_task)
1362 {
1363 GNUNET_SCHEDULER_cancel (pr->warn_task);
1364 pr->warn_task = NULL;
1365 }
1366 if (NULL == cont)
1367 return; /* no continuation */
1368 pr->llc_cont = NULL;
1369 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1370 {
1371 if (GNUNET_BLOCK_REPLY_OK_LAST != pr->local_result)
1372 {
1373 /* Signal that we are done and that there won't be any
1374 additional results to allow client to clean up state. */
1375 pr->rh (pr->rh_cls,
1376 GNUNET_BLOCK_REPLY_OK_LAST,
1377 pr,
1378 UINT32_MAX,
1379 GNUNET_TIME_UNIT_ZERO_ABS,
1380 GNUNET_TIME_UNIT_FOREVER_ABS,
1381 GNUNET_BLOCK_TYPE_ANY,
1382 NULL,
1383 0);
1384 }
1385 /* Finally, call our continuation to signal that we are
1386 done with local processing of this request; i.e. to
1387 start reading again from the client. */
1388 cont (pr->llc_cont_cls,
1389 NULL,
1390 GNUNET_BLOCK_REPLY_OK_LAST);
1391 return;
1392 }
1393
1394 cont (pr->llc_cont_cls,
1395 pr,
1396 pr->local_result);
1397}
1398
1399
1400/* Update stats and call continuation */
1401static void
1402no_more_local_results (struct GSF_PendingRequest *pr)
1403{
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1405 "No further local responses available.\n");
1406#if INSANE_STATISTICS
1407 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == pr->public_data.type) ||
1408 (GNUNET_BLOCK_TYPE_FS_IBLOCK == pr->public_data.type))
1409 GNUNET_STATISTICS_update (GSF_stats,
1410 gettext_noop (
1411 "# requested DBLOCK or IBLOCK not found"),
1412 1,
1413 GNUNET_NO);
1414#endif
1415 call_continuation (pr);
1416}
1417
1418
1419/* forward declaration */
1420static void
1421process_local_reply (void *cls,
1422 const struct GNUNET_HashCode *key,
1423 size_t size,
1424 const void *data,
1425 enum GNUNET_BLOCK_Type type,
1426 uint32_t priority,
1427 uint32_t anonymity,
1428 uint32_t replication,
1429 struct GNUNET_TIME_Absolute expiration,
1430 uint64_t uid);
1431
1432
1433/* Start a local query */
1434static void
1435start_local_query (struct GSF_PendingRequest *pr,
1436 uint64_t next_uid,
1437 bool random)
1438{
1439 pr->qe_start = GNUNET_TIME_absolute_get ();
1440 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1441 &warn_delay_task,
1442 pr);
1443 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1444 next_uid,
1445 random,
1446 &pr->public_data.query,
1447 pr->public_data.type ==
1448 GNUNET_BLOCK_TYPE_FS_DBLOCK
1449 ? GNUNET_BLOCK_TYPE_ANY
1450 : pr->public_data.type,
1451 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1452 & pr->public_data.options))
1453 ? UINT_MAX
1454 : 1
1455 /* queue priority */,
1456 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1457 & pr->public_data.options))
1458 ? UINT_MAX
1459 : GSF_datastore_queue_size
1460 /* max queue size */,
1461 &process_local_reply,
1462 pr);
1463 if (NULL != pr->qe)
1464 return;
1465 GNUNET_log (
1466 GNUNET_ERROR_TYPE_DEBUG,
1467 "ERROR Requesting `%s' of type %d with next_uid %llu from datastore.\n",
1468 GNUNET_h2s (&pr->public_data.query),
1469 pr->public_data.type,
1470 (unsigned long long) next_uid);
1471 GNUNET_STATISTICS_update (GSF_stats,
1472 gettext_noop (
1473 "# Datastore lookups concluded (error queueing)"),
1474 1,
1475 GNUNET_NO);
1476 call_continuation (pr);
1477}
1478
1479
1480/**
1481 * We're processing (local) results for a search request
1482 * from another peer. Pass applicable results to the
1483 * peer and if we are done either clean up (operation
1484 * complete) or forward to other peers (more results possible).
1485 *
1486 * @param cls our closure (`struct GSF_PendingRequest *`)
1487 * @param key key for the content
1488 * @param size number of bytes in @a data
1489 * @param data content stored
1490 * @param type type of the content
1491 * @param priority priority of the content
1492 * @param anonymity anonymity-level for the content
1493 * @param replication replication-level for the content
1494 * @param expiration expiration time for the content
1495 * @param uid unique identifier for the datum;
1496 * maybe 0 if no unique identifier is available
1497 */
1498static void
1499process_local_reply (void *cls,
1500 const struct GNUNET_HashCode *key,
1501 size_t size,
1502 const void *data,
1503 enum GNUNET_BLOCK_Type type,
1504 uint32_t priority,
1505 uint32_t anonymity,
1506 uint32_t replication,
1507 struct GNUNET_TIME_Absolute expiration,
1508 uint64_t uid)
1509{
1510 struct GSF_PendingRequest *pr = cls;
1511 struct ProcessReplyClosure prq;
1512 struct GNUNET_HashCode query;
1513 unsigned int old_rf;
1514
1515 GNUNET_SCHEDULER_cancel (pr->warn_task);
1516 pr->warn_task = NULL;
1517 if (NULL == pr->qe)
1518 goto called_from_on_demand;
1519 pr->qe = NULL;
1520 if (
1521 (NULL == key) && pr->seen_null &&
1522 ! pr->have_first_uid) /* We have hit the end for the 2nd time with no results */
1523 {
1524 /* No results */
1525#if INSANE_STATISTICS
1526 GNUNET_STATISTICS_update (GSF_stats,
1527 gettext_noop (
1528 "# Datastore lookups concluded (no results)"),
1529 1,
1530 GNUNET_NO);
1531#endif
1532 no_more_local_results (pr);
1533 return;
1534 }
1535 if (((NULL == key) &&
1536 pr->seen_null) || /* We have hit the end for the 2nd time OR */
1537 (pr->seen_null && pr->have_first_uid &&
1538 (uid >= pr->first_uid))) /* We have hit the end and past first UID */
1539 {
1540 /* Seen all results */
1541 GNUNET_STATISTICS_update (GSF_stats,
1542 gettext_noop (
1543 "# Datastore lookups concluded (seen all)"),
1544 1,
1545 GNUNET_NO);
1546 no_more_local_results (pr);
1547 return;
1548 }
1549 if (NULL == key)
1550 {
1551 GNUNET_assert (! pr->seen_null);
1552 pr->seen_null = true;
1553 start_local_query (pr, 0 /* next_uid */, false /* random */);
1554 return;
1555 }
1556 if (! pr->have_first_uid)
1557 {
1558 pr->first_uid = uid;
1559 pr->have_first_uid = true;
1560 }
1561 pr->result_count++;
1562 if (pr->result_count > MAX_RESULTS)
1563 {
1564 GNUNET_STATISTICS_update (
1565 GSF_stats,
1566 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1567 1,
1568 GNUNET_NO);
1569 no_more_local_results (pr);
1570 return;
1571 }
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1574 GNUNET_h2s (key),
1575 type,
1576 (unsigned long long) uid);
1577 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1578 {
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "Found ONDEMAND block, performing on-demand encoding\n");
1581 GNUNET_STATISTICS_update (GSF_stats,
1582 gettext_noop (
1583 "# on-demand blocks matched requests"),
1584 1,
1585 GNUNET_NO);
1586 pr->qe_start = GNUNET_TIME_absolute_get ();
1587 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1588 &odc_warn_delay_task,
1589 pr);
1590 if (GNUNET_OK == GNUNET_FS_handle_on_demand_block (key,
1591 size,
1592 data,
1593 type,
1594 priority,
1595 anonymity,
1596 replication,
1597 expiration,
1598 uid,
1599 &process_local_reply,
1600 pr))
1601 {
1602 GNUNET_STATISTICS_update (GSF_stats,
1603 gettext_noop (
1604 "# on-demand lookups performed successfully"),
1605 1,
1606 GNUNET_NO);
1607 return; /* we're done */
1608 }
1609 GNUNET_STATISTICS_update (GSF_stats,
1610 gettext_noop ("# on-demand lookups failed"),
1611 1,
1612 GNUNET_NO);
1613 GNUNET_SCHEDULER_cancel (pr->warn_task);
1614 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1615 return;
1616 }
1617called_from_on_demand:
1618 old_rf = pr->public_data.results_found;
1619 memset (&prq, 0, sizeof(prq));
1620 prq.data = data;
1621 prq.expiration = expiration;
1622 prq.size = size;
1623 if (GNUNET_OK !=
1624 GNUNET_BLOCK_get_key (GSF_block_ctx,
1625 type,
1626 data,
1627 size,
1628 &query))
1629 {
1630 GNUNET_break (0);
1631 GNUNET_DATASTORE_remove (GSF_dsh,
1632 key,
1633 size,
1634 data,
1635 UINT_MAX,
1636 UINT_MAX,
1637 NULL,
1638 NULL);
1639 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1640 return;
1641 }
1642 prq.type = type;
1643 prq.priority = priority;
1644 prq.request_found = GNUNET_NO;
1645 prq.anonymity_level = anonymity;
1646 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1647 GSF_update_datastore_delay_ (pr->public_data.start_time);
1648 process_reply (&prq,
1649 key,
1650 pr);
1651 pr->local_result = prq.eval;
1652 if (GNUNET_BLOCK_REPLY_OK_LAST == prq.eval)
1653 {
1654 GNUNET_STATISTICS_update (
1655 GSF_stats,
1656 gettext_noop ("# Datastore lookups concluded (found last result)"),
1657 1,
1658 GNUNET_NO);
1659 call_continuation (pr);
1660 return;
1661 }
1662 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1663 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1664 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1665 {
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1667 GNUNET_STATISTICS_update (GSF_stats,
1668 gettext_noop (
1669 "# Datastore lookups concluded (load too high)"),
1670 1,
1671 GNUNET_NO);
1672 call_continuation (pr);
1673 return;
1674 }
1675 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1676}
1677
1678
1679/**
1680 * Is the given target a legitimate peer for forwarding the given request?
1681 *
1682 * @param pr request
1683 * @param target
1684 * @return #GNUNET_YES if this request could be forwarded to the given peer
1685 */
1686int
1687GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1688 const struct GNUNET_PeerIdentity *target)
1689{
1690 struct GNUNET_PeerIdentity pi;
1691
1692 if (0 == pr->origin_pid)
1693 return GNUNET_YES;
1694 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1695 return (0 == memcmp (&pi, target, sizeof(struct GNUNET_PeerIdentity)))
1696 ? GNUNET_NO
1697 : GNUNET_YES;
1698}
1699
1700
1701/**
1702 * Look up the request in the local datastore.
1703 *
1704 * @param pr the pending request to process
1705 * @param cont function to call at the end
1706 * @param cont_cls closure for @a cont
1707 */
1708void
1709GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1710 GSF_LocalLookupContinuation cont,
1711 void *cont_cls)
1712{
1713 GNUNET_assert (NULL == pr->gh);
1714 GNUNET_assert (NULL == pr->cadet_request);
1715 GNUNET_assert (NULL == pr->llc_cont);
1716 pr->llc_cont = cont;
1717 pr->llc_cont_cls = cont_cls;
1718#if INSANE_STATISTICS
1719 GNUNET_STATISTICS_update (GSF_stats,
1720 gettext_noop ("# Datastore lookups initiated"),
1721 1,
1722 GNUNET_NO);
1723#endif
1724 start_local_query (pr, 0 /* next_uid */, true /* random */);
1725}
1726
1727
1728/**
1729 * Handle P2P "CONTENT" message. Checks that the message is
1730 * well-formed and then checks if there are any pending requests for
1731 * this content and possibly passes it on (to local clients or other
1732 * peers). Does NOT perform migration (content caching at this peer).
1733 *
1734 * @param cls the other peer involved
1735 * @param put the actual message
1736 */
1737void
1738handle_p2p_put (void *cls,
1739 const struct PutMessage *put)
1740{
1741 struct GSF_ConnectedPeer *cp = cls;
1742 uint16_t msize;
1743 size_t dsize;
1744 enum GNUNET_BLOCK_Type type;
1745 struct GNUNET_TIME_Absolute expiration;
1746 struct GNUNET_HashCode query;
1747 struct ProcessReplyClosure prq;
1748 struct GNUNET_TIME_Relative block_time;
1749 double putl;
1750 struct PutMigrationContext *pmc;
1751
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "Received P2P PUT from %s\n",
1754 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1755 GSF_cover_content_count++;
1756 msize = ntohs (put->header.size);
1757 dsize = msize - sizeof(struct PutMessage);
1758 type = ntohl (put->type);
1759 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1760 /* do not allow migrated content to live longer than 1 year */
1761 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1762 GNUNET_TIME_UNIT_YEARS),
1763 expiration);
1764 if (GNUNET_OK !=
1765 GNUNET_BLOCK_check_block (GSF_block_ctx,
1766 type,
1767 &put[1],
1768 dsize))
1769 {
1770 GNUNET_break_op (0);
1771 return;
1772 }
1773 if (GNUNET_OK !=
1774 GNUNET_BLOCK_get_key (GSF_block_ctx,
1775 type,
1776 &put[1],
1777 dsize,
1778 &query))
1779 {
1780 GNUNET_break_op (0);
1781 return;
1782 }
1783 GNUNET_STATISTICS_update (GSF_stats,
1784 gettext_noop ("# GAP PUT messages received"),
1785 1,
1786 GNUNET_NO);
1787 /* now, lookup 'query' */
1788 prq.data = (const void *) &put[1];
1789 prq.sender = cp;
1790 prq.size = dsize;
1791 prq.type = type;
1792 prq.expiration = expiration;
1793 prq.priority = 0;
1794 prq.anonymity_level = UINT32_MAX;
1795 prq.request_found = GNUNET_NO;
1796 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1797 &query,
1798 &process_reply,
1799 &prq);
1800 if (NULL != cp)
1801 {
1802 GSF_connected_peer_change_preference_ (cp,
1803 CONTENT_BANDWIDTH_VALUE
1804 + 1000 * prq.priority);
1805 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1806 }
1807 if ((GNUNET_YES == active_to_migration) && (NULL != cp) &&
1808 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1809 {
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 "Replicating result for query `%s' with priority %u\n",
1812 GNUNET_h2s (&query),
1813 prq.priority);
1814 pmc = GNUNET_new (struct PutMigrationContext);
1815 pmc->start = GNUNET_TIME_absolute_get ();
1816 pmc->requested = prq.request_found;
1817 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1818 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1819 &pmc->origin);
1820 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1821 0,
1822 &query,
1823 dsize,
1824 &put[1],
1825 type,
1826 prq.priority,
1827 1 /* anonymity */,
1828 0 /* replication */,
1829 expiration,
1830 1 + prq.priority,
1831 MAX_DATASTORE_QUEUE,
1832 &put_migration_continuation,
1833 pmc))
1834 {
1835 put_migration_continuation (pmc,
1836 GNUNET_SYSERR,
1837 GNUNET_TIME_UNIT_ZERO_ABS,
1838 NULL);
1839 }
1840 }
1841 else if (NULL != cp)
1842 {
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1844 "Choosing not to keep content `%s' (%d/%d)\n",
1845 GNUNET_h2s (&query),
1846 active_to_migration,
1847 test_put_load_too_high (prq.priority));
1848 }
1849 putl = GNUNET_LOAD_get_load (datastore_put_load);
1850 if ((NULL != cp) && (GNUNET_NO == prq.request_found) &&
1851 ((GNUNET_YES != active_to_migration) ||
1852 (putl > 2.5 * (1 + prq.priority))))
1853 {
1854 if (GNUNET_YES != active_to_migration)
1855 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1856 block_time = GNUNET_TIME_relative_multiply (
1857 GNUNET_TIME_UNIT_MILLISECONDS,
1858 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1859 (unsigned int) (60000 * putl * putl)));
1860 GNUNET_log (
1861 GNUNET_ERROR_TYPE_DEBUG,
1862 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1863 GNUNET_STRINGS_relative_time_to_string (block_time, GNUNET_YES),
1864 putl,
1865 active_to_migration,
1866 (GNUNET_NO == prq.request_found));
1867 GSF_block_peer_migration_ (cp,
1868 GNUNET_TIME_relative_to_absolute (block_time));
1869 }
1870}
1871
1872
1873/**
1874 * Check if the given request is still active.
1875 *
1876 * @param pr pending request
1877 * @return #GNUNET_YES if the request is still active
1878 */
1879int
1880GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1881{
1882 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1883}
1884
1885
1886/**
1887 * Setup the subsystem.
1888 */
1889void
1890GSF_pending_request_init_ ()
1891{
1892 if (GNUNET_OK !=
1893 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1894 "fs",
1895 "MAX_PENDING_REQUESTS",
1896 &max_pending_requests))
1897 {
1898 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1899 "fs",
1900 "MAX_PENDING_REQUESTS");
1901 }
1902 active_to_migration =
1903 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1904 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1905 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1906 requests_by_expiration_heap =
1907 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1908}
1909
1910
1911/**
1912 * Shutdown the subsystem.
1913 */
1914void
1915GSF_pending_request_done_ ()
1916{
1917 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1918 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1919 pr_map = NULL;
1920 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1921 requests_by_expiration_heap = NULL;
1922 GNUNET_LOAD_value_free (datastore_put_load);
1923 datastore_put_load = NULL;
1924}
1925
1926
1927/* end of gnunet-service-fs_pr.c */