aboutsummaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats_reservations.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ats/gnunet-service-ats_reservations.c')
-rw-r--r--src/ats/gnunet-service-ats_reservations.c217
1 files changed, 0 insertions, 217 deletions
diff --git a/src/ats/gnunet-service-ats_reservations.c b/src/ats/gnunet-service-ats_reservations.c
deleted file mode 100644
index a50e9d5b7..000000000
--- a/src/ats/gnunet-service-ats_reservations.c
+++ /dev/null
@@ -1,217 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
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 */
41static 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 */
55static struct GNUNET_TIME_Relative
56reservations_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 */
100void
101GAS_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 */
144void
145GAS_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 */
176void
177GAS_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 */
192static int
193free_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 */
207void
208GAS_reservations_done ()
209{
210 GNUNET_CONTAINER_multipeermap_iterate (trackers,
211 &free_tracker,
212 NULL);
213 GNUNET_CONTAINER_multipeermap_destroy (trackers);
214}
215
216
217/* end of gnunet-service-ats_reservations.c */