2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file ats/gnunet-service-ats_reservations.c
22 * @brief ats service, inbound bandwidth reservation management
23 * @author Christian Grothoff
26 #include "gnunet-service-ats_reservations.h"
27 #include "gnunet-service-ats.h"
31 * Number of seconds that available bandwidth carries over
32 * (can accumulate). Note that the
33 * test_ats_reservation_api test depends on this value!
35 #define MAX_BANDWIDTH_CARRY_S 5
39 * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
41 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
45 * Reserve the given amount of incoming bandwidth (in bytes) from the
46 * given peer. If a reservation is not possible right now, return how
47 * long the client should wait before trying again.
49 * @param peer peer to reserve bandwidth from
50 * @param amount number of bytes to reserve
51 * @return 0 if the reservation was successful, FOREVER if the
52 * peer is not connected, otherwise the time to wait
53 * until the reservation might succeed
55 static struct GNUNET_TIME_Relative
56 reservations_reserve (const struct GNUNET_PeerIdentity *peer,
59 struct GNUNET_BANDWIDTH_Tracker *tracker;
60 struct GNUNET_TIME_Relative ret;
62 tracker = GNUNET_CONTAINER_multipeermap_get (trackers,
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "Not connected, allowing reservation of %d bytes\n",
69 return GNUNET_TIME_UNIT_ZERO; /* not connected, satisfy now */
73 ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker, amount);
74 if (ret.rel_value_us > 0)
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 "Delay to satisfy reservation for %d bytes is %s\n",
79 GNUNET_STRINGS_relative_time_to_string (ret,
84 (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "Reserved %d bytes\n",
88 return GNUNET_TIME_UNIT_ZERO;
93 * Set the amount of bandwidth the other peer could currently transmit
94 * to us (as far as we know) to the given value.
96 * @param peer identity of the peer
97 * @param bandwidth_in currently available bandwidth from that peer to
98 * this peer (estimate)
101 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
102 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
104 struct GNUNET_BANDWIDTH_Tracker *tracker;
106 tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
107 if (0 == ntohl (bandwidth_in.value__))
111 GNUNET_assert (GNUNET_YES ==
112 GNUNET_CONTAINER_multipeermap_remove (trackers,
115 GNUNET_free (tracker);
120 tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
121 GNUNET_BANDWIDTH_tracker_init (tracker,
125 MAX_BANDWIDTH_CARRY_S);
126 GNUNET_assert (GNUNET_OK ==
127 GNUNET_CONTAINER_multipeermap_put (trackers,
130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
133 GNUNET_BANDWIDTH_tracker_update_quota (tracker,
139 * Handle 'reservation request' messages from clients.
141 * @param client client that sent the request
142 * @param msg the request message
145 GAS_handle_reservation_request (struct GNUNET_SERVICE_Client *client,
146 const struct ReservationRequestMessage *msg)
148 struct GNUNET_MQ_Envelope *env;
149 struct ReservationResultMessage *result;
151 struct GNUNET_TIME_Relative res_delay;
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Received RESERVATION_REQUEST message\n");
155 amount = (int32_t) ntohl (msg->amount);
156 res_delay = reservations_reserve (&msg->peer, amount);
157 if (res_delay.rel_value_us > 0)
159 env = GNUNET_MQ_msg (result,
160 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
161 result->amount = htonl (amount);
162 result->peer = msg->peer;
163 result->res_delay = GNUNET_TIME_relative_hton (res_delay);
164 GNUNET_STATISTICS_update (GSA_stats,
165 "# reservation requests processed",
168 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
174 * Initialize reservations subsystem.
177 GAS_reservations_init ()
179 trackers = GNUNET_CONTAINER_multipeermap_create (128,
185 * Free memory of bandwidth tracker.
188 * @param key peer identity (unused)
189 * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
190 * @return #GNUNET_OK (continue to iterate)
193 free_tracker (void *cls,
194 const struct GNUNET_PeerIdentity *key,
197 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
199 GNUNET_free (tracker);
205 * Shutdown reservations subsystem.
208 GAS_reservations_done ()
210 GNUNET_CONTAINER_multipeermap_iterate (trackers,
213 GNUNET_CONTAINER_multipeermap_destroy (trackers);
216 /* end of gnunet-service-ats_reservations.c */