2f3c1f5f1f37e2482e82f16d9bdca40c8b74da19
[oweals/gnunet.git] / src / ats / gnunet-service-ats_reservations.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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).
33  */
34 #define MAX_BANDWIDTH_CARRY_S 5
35
36
37 /**
38  * Map of peer identities to `struct GNUNET_BANDWIDTH_Tracker *`s
39  */
40 static struct GNUNET_CONTAINER_MultiPeerMap *trackers;
41
42 /**
43  * Context for sending messages to performance clients without PIC.
44  */
45 static struct GNUNET_SERVER_NotificationContext *nc;
46
47
48 /**
49  * Reserve the given amount of incoming bandwidth (in bytes) from the
50  * given peer.  If a reservation is not possible right now, return how
51  * long the client should wait before trying again.
52  *
53  * @param peer peer to reserve bandwidth from
54  * @param amount number of bytes to reserve
55  * @return 0 if the reservation was successful, FOREVER if the
56  *         peer is not connected, otherwise the time to wait
57  *         until the reservation might succeed
58  */
59 struct GNUNET_TIME_Relative
60 GAS_reservations_reserve (const struct GNUNET_PeerIdentity *peer,
61                           int32_t amount)
62 {
63   struct GNUNET_BANDWIDTH_Tracker *tracker;
64   struct GNUNET_TIME_Relative ret;
65
66   tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
67   if (NULL == tracker)
68     return GNUNET_TIME_UNIT_ZERO;       /* not connected, satisfy now */
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, "Reserved %d bytes\n", (int) amount);
84   return GNUNET_TIME_UNIT_ZERO;
85 }
86
87
88 /**
89  * Set the amount of bandwidth the other peer could currently transmit
90  * to us (as far as we know) to the given value.
91  *
92  * @param peer identity of the peer
93  * @param bandwidth_in currently available bandwidth from that peer to
94  *        this peer (estimate)
95  */
96 void
97 GAS_reservations_set_bandwidth (const struct GNUNET_PeerIdentity *peer,
98                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
99 {
100   struct GNUNET_BANDWIDTH_Tracker *tracker;
101
102   tracker = GNUNET_CONTAINER_multipeermap_get (trackers, peer);
103   if (0 == ntohl (bandwidth_in.value__))
104   {
105     if (NULL == tracker)
106       return;
107     GNUNET_assert (GNUNET_YES ==
108                    GNUNET_CONTAINER_multipeermap_remove (trackers,
109                                                          peer,
110                                                          tracker));
111     GNUNET_free (tracker);
112     return;
113   }
114   if (NULL == tracker)
115   {
116     tracker = GNUNET_new (struct GNUNET_BANDWIDTH_Tracker);
117     GNUNET_BANDWIDTH_tracker_init (tracker, NULL, NULL, bandwidth_in,
118                                    MAX_BANDWIDTH_CARRY_S);
119     GNUNET_assert (GNUNET_OK ==
120                    GNUNET_CONTAINER_multipeermap_put (trackers, peer, tracker,
121                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
122     return;
123   }
124   GNUNET_BANDWIDTH_tracker_update_quota (tracker, bandwidth_in);
125 }
126
127
128 /**
129  * Handle 'reservation request' messages from clients.
130  *
131  * @param cls unused, NULL
132  * @param client client that sent the request
133  * @param message the request message
134  */
135 void
136 GAS_handle_reservation_request (void *cls,
137                                 struct GNUNET_SERVER_Client *client,
138                                 const struct GNUNET_MessageHeader *message)
139 {
140   const struct ReservationRequestMessage *msg =
141       (const struct ReservationRequestMessage *) message;
142   struct ReservationResultMessage result;
143   int32_t amount;
144   struct GNUNET_TIME_Relative res_delay;
145
146   GNUNET_SERVER_notification_context_add (nc,
147                                           client);
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Received RESERVATION_REQUEST message\n");
150   amount = (int32_t) ntohl (msg->amount);
151   res_delay = GAS_reservations_reserve (&msg->peer, amount);
152   if (res_delay.rel_value_us > 0)
153     amount = 0;
154   result.header.size = htons (sizeof (struct ReservationResultMessage));
155   result.header.type = htons (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_SERVER_notification_context_unicast (nc,
164                                               client,
165                                               &result.header,
166                                               GNUNET_NO);
167   GNUNET_SERVER_receive_done (client,
168                               GNUNET_OK);
169 }
170
171
172 /**
173  * Initialize reservations subsystem.
174  *
175  * @param server handle to our server
176  */
177 void
178 GAS_reservations_init (struct GNUNET_SERVER_Handle *server)
179 {
180   trackers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
181   nc = GNUNET_SERVER_notification_context_create (server, 128);
182 }
183
184
185 /**
186  * Free memory of bandwidth tracker.
187  *
188  * @param cls NULL
189  * @param key peer identity (unused)
190  * @param value the `struct GNUNET_BANDWIDTH_Tracker` to free
191  * @return #GNUNET_OK (continue to iterate)
192  */
193 static int
194 free_tracker (void *cls,
195               const struct GNUNET_PeerIdentity *key, 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   GNUNET_SERVER_notification_context_destroy (nc);
215   nc = NULL;
216
217 }
218
219 /* end of gnunet-service-ats_reservations.c */