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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 cls unused, NULL
142 * @param client client that sent the request
143 * @param message the request message
146 GAS_handle_reservation_request (void *cls,
147 struct GNUNET_SERVER_Client *client,
148 const struct GNUNET_MessageHeader *message)
150 const struct ReservationRequestMessage *msg =
151 (const struct ReservationRequestMessage *) message;
152 struct ReservationResultMessage result;
154 struct GNUNET_TIME_Relative res_delay;
155 struct GNUNET_SERVER_NotificationContext **uc;
156 struct GNUNET_SERVER_NotificationContext *nc;
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Received RESERVATION_REQUEST message\n");
160 uc = GNUNET_SERVER_client_get_user_context (client,
161 struct GNUNET_SERVER_NotificationContext *);
168 amount = (int32_t) ntohl (msg->amount);
169 res_delay = reservations_reserve (&msg->peer, amount);
170 if (res_delay.rel_value_us > 0)
172 result.header.size = htons (sizeof (struct ReservationResultMessage));
173 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
174 result.amount = htonl (amount);
175 result.peer = msg->peer;
176 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
177 GNUNET_STATISTICS_update (GSA_stats,
178 "# reservation requests processed",
182 GNUNET_SERVER_notification_context_unicast (nc,
186 GNUNET_SERVER_receive_done (client,
192 * Initialize reservations subsystem.
194 * @param server handle to our server
197 GAS_reservations_init (struct GNUNET_SERVER_Handle *server)
199 trackers = GNUNET_CONTAINER_multipeermap_create (128,
205 * Free memory of bandwidth tracker.
208 * @param key peer identity (unused)
209 * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
210 * @return #GNUNET_OK (continue to iterate)
213 free_tracker (void *cls,
214 const struct GNUNET_PeerIdentity *key, void *value)
216 struct GNUNET_BANDWIDTH_Tracker *tracker = value;
218 GNUNET_free (tracker);
224 * Shutdown reservations subsystem.
227 GAS_reservations_done ()
229 GNUNET_CONTAINER_multipeermap_iterate (trackers,
232 GNUNET_CONTAINER_multipeermap_destroy (trackers);
235 /* end of gnunet-service-ats_reservations.c */