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