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