glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / ats / gnunet-service-ats_reservations.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 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 /**
16  * @file ats/gnunet-service-ats_reservations.c
17  * @brief ats service, inbound bandwidth reservation management
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet-service-ats_reservations.h"
22 #include "gnunet-service-ats.h"
23 #include "ats.h"
24
25 /**
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!
29  */
30 #define MAX_BANDWIDTH_CARRY_S 5
31
32
33 /**
34  * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
35  */
36 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
37
38
39 /**
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.
43  *
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
49  */
50 static struct GNUNET_TIME_Relative
51 reservations_reserve (const struct GNUNET_PeerIdentity *peer,
52                       int32_t amount)
53 {
54   struct GNUNET_BANDWIDTH_Tracker *tracker;
55   struct GNUNET_TIME_Relative ret;
56
57   tracker = GNUNET_CONTAINER_multipeermap_get (trackers,
58                                                peer);
59   if (NULL == tracker)
60   {
61     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62                 "Not connected, allowing reservation of %d bytes\n",
63                 (int) amount);
64     return GNUNET_TIME_UNIT_ZERO;       /* not connected, satisfy now */
65   }
66   if (amount >= 0)
67   {
68     ret = GNUNET_BANDWIDTH_tracker_get_delay (tracker, amount);
69     if (ret.rel_value_us > 0)
70     {
71       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72                   "Delay to satisfy reservation for %d bytes is %s\n",
73                   (int) amount,
74                   GNUNET_STRINGS_relative_time_to_string (ret,
75                                                           GNUNET_YES));
76       return ret;
77     }
78   }
79   (void) GNUNET_BANDWIDTH_tracker_consume (tracker, amount);
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81               "Reserved %d bytes\n",
82               (int) amount);
83   return GNUNET_TIME_UNIT_ZERO;
84 }
85
86
87 /**
88  * Set the amount of bandwidth the other peer could currently transmit
89  * to us (as far as we know) to the given value.
90  *
91  * @param peer identity of the peer
92  * @param bandwidth_in currently available bandwidth from that peer to
93  *        this peer (estimate)
94  */
95 void
96 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
97                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
98 {
99   struct GNUNET_BANDWIDTH_Tracker *tracker;
100
101   tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
102   if (0 == ntohl (bandwidth_in.value__))
103   {
104     if (NULL == tracker)
105       return;
106     GNUNET_assert (GNUNET_YES ==
107                    GNUNET_CONTAINER_multipeermap_remove (trackers,
108                                                          peer,
109                                                          tracker));
110     GNUNET_free (tracker);
111     return;
112   }
113   if (NULL == tracker)
114   {
115     tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
116     GNUNET_BANDWIDTH_tracker_init (tracker,
117                                    NULL,
118                                    NULL,
119                                    bandwidth_in,
120                                    MAX_BANDWIDTH_CARRY_S);
121     GNUNET_assert (GNUNET_OK ==
122                    GNUNET_CONTAINER_multipeermap_put (trackers,
123                                                       peer,
124                                                       tracker,
125                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
126     return;
127   }
128   GNUNET_BANDWIDTH_tracker_update_quota (tracker,
129                                          bandwidth_in);
130 }
131
132
133 /**
134  * Handle 'reservation request' messages from clients.
135  *
136  * @param client client that sent the request
137  * @param msg the request message
138  */
139 void
140 GAS_handle_reservation_request (struct GNUNET_SERVICE_Client *client,
141                                 const struct ReservationRequestMessage *msg)
142 {
143   struct GNUNET_MQ_Envelope *env;
144   struct ReservationResultMessage *result;
145   int32_t amount;
146   struct GNUNET_TIME_Relative res_delay;
147
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)
153     amount = 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",
161                             1,
162                             GNUNET_NO);
163   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
164                   env);
165 }
166
167
168 /**
169  * Initialize reservations subsystem.
170  */
171 void
172 GAS_reservations_init ()
173 {
174   trackers = GNUNET_CONTAINER_multipeermap_create (128,
175                                                    GNUNET_NO);
176 }
177
178
179 /**
180  * Free memory of bandwidth tracker.
181  *
182  * @param cls NULL
183  * @param key peer identity (unused)
184  * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
185  * @return #GNUNET_OK (continue to iterate)
186  */
187 static int
188 free_tracker (void *cls,
189               const struct GNUNET_PeerIdentity *key,
190               void *value)
191 {
192   struct GNUNET_BANDWIDTH_Tracker *tracker = value;
193
194   GNUNET_free (tracker);
195   return GNUNET_OK;
196 }
197
198
199 /**
200  * Shutdown reservations subsystem.
201  */
202 void
203 GAS_reservations_done ()
204 {
205   GNUNET_CONTAINER_multipeermap_iterate (trackers,
206                                          &free_tracker,
207                                          NULL);
208   GNUNET_CONTAINER_multipeermap_destroy (trackers);
209 }
210
211 /* end of gnunet-service-ats_reservations.c */