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.
16 * @file ats/gnunet-service-ats_reservations.c
17 * @brief ats service, inbound bandwidth reservation management
18 * @author Christian Grothoff
21 #include "gnunet-service-ats_reservations.h"
22 #include "gnunet-service-ats.h"
26 * Number of seconds that available bandwidth carries over
27 * (can accumulate). Note that the
28 * test_ats_reservation_api test depends on this value!
30 #define MAX_BANDWIDTH_CARRY_S 5
34 * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
36 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
40 * Reserve the given amount of incoming bandwidth (in bytes) from the
41 * given peer. If a reservation is not possible right now, return how
42 * long the client should wait before trying again.
44 * @param peer peer to reserve bandwidth from
45 * @param amount number of bytes to reserve
46 * @return 0 if the reservation was successful, FOREVER if the
47 * peer is not connected, otherwise the time to wait
48 * until the reservation might succeed
50 static struct GNUNET_TIME_Relative
51 reservations_reserve (const struct GNUNET_PeerIdentity *peer,
54 struct GNUNET_BANDWIDTH_Tracker *tracker;
55 struct GNUNET_TIME_Relative ret;
57 tracker = GNUNET_CONTAINER_multipeermap_get (trackers,
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Not connected, allowing reservation of %d bytes\n",
64 return GNUNET_TIME_UNIT_ZERO; /* not connected, satisfy now */
68 ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker, amount);
69 if (ret.rel_value_us > 0)
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72 "Delay to satisfy reservation for %d bytes is %s\n",
74 GNUNET_STRINGS_relative_time_to_string (ret,
79 (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Reserved %d bytes\n",
83 return GNUNET_TIME_UNIT_ZERO;
88 * Set the amount of bandwidth the other peer could currently transmit
89 * to us (as far as we know) to the given value.
91 * @param peer identity of the peer
92 * @param bandwidth_in currently available bandwidth from that peer to
93 * this peer (estimate)
96 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
97 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
99 struct GNUNET_BANDWIDTH_Tracker *tracker;
101 tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
102 if (0 == ntohl (bandwidth_in.value__))
106 GNUNET_assert (GNUNET_YES ==
107 GNUNET_CONTAINER_multipeermap_remove (trackers,
110 GNUNET_free (tracker);
115 tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
116 GNUNET_BANDWIDTH_tracker_init (tracker,
120 MAX_BANDWIDTH_CARRY_S);
121 GNUNET_assert (GNUNET_OK ==
122 GNUNET_CONTAINER_multipeermap_put (trackers,
125 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
128 GNUNET_BANDWIDTH_tracker_update_quota (tracker,
134 * Handle 'reservation request' messages from clients.
136 * @param client client that sent the request
137 * @param msg the request message
140 GAS_handle_reservation_request (struct GNUNET_SERVICE_Client *client,
141 const struct ReservationRequestMessage *msg)
143 struct GNUNET_MQ_Envelope *env;
144 struct ReservationResultMessage *result;
146 struct GNUNET_TIME_Relative res_delay;
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Received RESERVATION_REQUEST message\n");
150 amount = (int32_t) ntohl (msg->amount);
151 res_delay = reservations_reserve (&msg->peer, amount);
152 if (res_delay.rel_value_us > 0)
154 env = GNUNET_MQ_msg (result,
155 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
156 result->amount = htonl (amount);
157 result->peer = msg->peer;
158 result->res_delay = GNUNET_TIME_relative_hton (res_delay);
159 GNUNET_STATISTICS_update (GSA_stats,
160 "# reservation requests processed",
163 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
169 * Initialize reservations subsystem.
172 GAS_reservations_init ()
174 trackers = GNUNET_CONTAINER_multipeermap_create (128,
180 * Free memory of bandwidth tracker.
183 * @param key peer identity (unused)
184 * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
185 * @return #GNUNET_OK (continue to iterate)
188 free_tracker (void *cls,
189 const struct GNUNET_PeerIdentity *key,
192 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
194 GNUNET_free (tracker);
200 * Shutdown reservations subsystem.
203 GAS_reservations_done ()
205 GNUNET_CONTAINER_multipeermap_iterate (trackers,
208 GNUNET_CONTAINER_multipeermap_destroy (trackers);
211 /* end of gnunet-service-ats_reservations.c */