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/>.
19 * @file ats/gnunet-service-ats_reservations.c
20 * @brief ats service, inbound bandwidth reservation management
21 * @author Christian Grothoff
24 #include "gnunet-service-ats_reservations.h"
25 #include "gnunet-service-ats.h"
29 * Number of seconds that available bandwidth carries over
30 * (can accumulate). Note that the
31 * test_ats_reservation_api test depends on this value!
33 #define MAX_BANDWIDTH_CARRY_S 5
37 * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
39 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
43 * Reserve the given amount of incoming bandwidth (in bytes) from the
44 * given peer. If a reservation is not possible right now, return how
45 * long the client should wait before trying again.
47 * @param peer peer to reserve bandwidth from
48 * @param amount number of bytes to reserve
49 * @return 0 if the reservation was successful, FOREVER if the
50 * peer is not connected, otherwise the time to wait
51 * until the reservation might succeed
53 static struct GNUNET_TIME_Relative
54 reservations_reserve (const struct GNUNET_PeerIdentity *peer,
57 struct GNUNET_BANDWIDTH_Tracker *tracker;
58 struct GNUNET_TIME_Relative ret;
60 tracker = GNUNET_CONTAINER_multipeermap_get (trackers,
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "Not connected, allowing reservation of %d bytes\n",
67 return GNUNET_TIME_UNIT_ZERO; /* not connected, satisfy now */
71 ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker, amount);
72 if (ret.rel_value_us > 0)
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "Delay to satisfy reservation for %d bytes is %s\n",
77 GNUNET_STRINGS_relative_time_to_string (ret,
82 (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Reserved %d bytes\n",
86 return GNUNET_TIME_UNIT_ZERO;
91 * Set the amount of bandwidth the other peer could currently transmit
92 * to us (as far as we know) to the given value.
94 * @param peer identity of the peer
95 * @param bandwidth_in currently available bandwidth from that peer to
96 * this peer (estimate)
99 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
100 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
102 struct GNUNET_BANDWIDTH_Tracker *tracker;
104 tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
105 if (0 == ntohl (bandwidth_in.value__))
109 GNUNET_assert (GNUNET_YES ==
110 GNUNET_CONTAINER_multipeermap_remove (trackers,
113 GNUNET_free (tracker);
118 tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
119 GNUNET_BANDWIDTH_tracker_init (tracker,
123 MAX_BANDWIDTH_CARRY_S);
124 GNUNET_assert (GNUNET_OK ==
125 GNUNET_CONTAINER_multipeermap_put (trackers,
128 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
131 GNUNET_BANDWIDTH_tracker_update_quota (tracker,
137 * Handle 'reservation request' messages from clients.
139 * @param client client that sent the request
140 * @param msg the request message
143 GAS_handle_reservation_request (struct GNUNET_SERVICE_Client *client,
144 const struct ReservationRequestMessage *msg)
146 struct GNUNET_MQ_Envelope *env;
147 struct ReservationResultMessage *result;
149 struct GNUNET_TIME_Relative res_delay;
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Received RESERVATION_REQUEST message\n");
153 amount = (int32_t) ntohl (msg->amount);
154 res_delay = reservations_reserve (&msg->peer, amount);
155 if (res_delay.rel_value_us > 0)
157 env = GNUNET_MQ_msg (result,
158 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
159 result->amount = htonl (amount);
160 result->peer = msg->peer;
161 result->res_delay = GNUNET_TIME_relative_hton (res_delay);
162 GNUNET_STATISTICS_update (GSA_stats,
163 "# reservation requests processed",
166 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
172 * Initialize reservations subsystem.
175 GAS_reservations_init ()
177 trackers = GNUNET_CONTAINER_multipeermap_create (128,
183 * Free memory of bandwidth tracker.
186 * @param key peer identity (unused)
187 * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
188 * @return #GNUNET_OK (continue to iterate)
191 free_tracker (void *cls,
192 const struct GNUNET_PeerIdentity *key,
195 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
197 GNUNET_free (tracker);
203 * Shutdown reservations subsystem.
206 GAS_reservations_done ()
208 GNUNET_CONTAINER_multipeermap_iterate (trackers,
211 GNUNET_CONTAINER_multipeermap_destroy (trackers);
214 /* end of gnunet-service-ats_reservations.c */