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