2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/ats_api_scheduling.c
22 * @brief automatic transport selection and outbound bandwidth determination
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
32 * Message in linked list we should send to the ATS service. The
33 * actual binary message follows this struct.
41 struct PendingMessage *next;
46 struct PendingMessage *prev;
49 * Size of the message.
54 * Is this the 'ATS_START' message?
61 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
63 struct GNUNET_ATS_SchedulingHandle
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Callback to invoke on suggestions.
74 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
77 * Closure for 'suggest_cb'.
82 * Connection to ATS service.
84 struct GNUNET_CLIENT_Connection *client;
87 * Head of list of messages for the ATS service.
89 struct PendingMessage *pending_head;
92 * Tail of list of messages for the ATS service
94 struct PendingMessage *pending_tail;
97 * Current request for transmission to ATS.
99 struct GNUNET_CLIENT_TransmitHandle *th;
102 * Array of session objects (we need to translate them to numbers and back
103 * for the protocol; the offset in the array is the session number on the
104 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
105 * Unused entries are also NULL.
107 struct Session **session_array;
110 * Size of the session array.
112 unsigned int session_array_size;
118 * Re-establish the connection to the ATS service.
120 * @param sh handle to use to re-connect.
123 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
127 * Transmit messages from the message queue to the service
128 * (if there are any, and if we are not already trying).
130 * @param sh handle to use
133 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
137 * We can now transmit a message to ATS. Do it.
139 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
140 * @param size number of bytes we can transmit to ATS
141 * @param buf where to copy the messages
142 * @return number of bytes copied into buf
145 transmit_message_to_ats (void *cls,
149 struct GNUNET_ATS_SchedulingHandle *sh = cls;
150 struct PendingMessage *p;
157 while ( (NULL != (p = sh->pending_head)) &&
160 memcpy (&cbuf[ret], &p[1], p->size);
162 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
173 * Transmit messages from the message queue to the service
174 * (if there are any, and if we are not already trying).
176 * @param sh handle to use
179 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
181 struct PendingMessage *p;
185 if (NULL == (p = sh->pending_head))
187 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
189 GNUNET_TIME_UNIT_FOREVER_REL,
191 &transmit_message_to_ats, sh);
196 * Find the session object corresponding to the given session ID.
198 * @param sh our handle
199 * @param session_id current session ID
200 * @return the session object (or NULL)
202 static struct Session*
203 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
206 if (session_id >= sh->session_array_size)
211 return sh->session_array[session_id];
216 * Get the ID for the given session object. If we do not have an ID for
217 * the given session object, allocate one.
219 * @param sh our handle
220 * @param session session object
221 * @return the session id
224 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
225 struct Session *session)
231 for (i=1;i<sh->session_array_size;i++)
233 if (session == sh->session_array[i])
236 (sh->session_array[i] == NULL) )
241 f = sh->session_array_size;
242 GNUNET_array_grow (sh->session_array,
243 sh->session_array_size,
244 sh->session_array_size * 2);
246 sh->session_array[f] = session;
252 * Remove the session of the given session ID from the session
253 * table (it is no longer valid).
255 * @param sh our handle
256 * @param session_id identifies session that is no longer valid
259 remove_session (struct GNUNET_ATS_SchedulingHandle *sh,
262 GNUNET_assert (session_id < sh->session_array_size);
263 sh->session_array[session_id] = NULL;
268 * Type of a function to call when we receive a message
271 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
272 * @param msg message received, NULL on timeout or fatal error
275 process_ats_message (void *cls,
276 const struct GNUNET_MessageHeader *msg)
278 struct GNUNET_ATS_SchedulingHandle *sh = cls;
279 const struct AddressSuggestionMessage *m;
281 const char *plugin_name;
282 uint16_t address_length;
286 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
291 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
292 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)) )
295 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
300 m = (const struct AddressSuggestionMessage*) msg;
301 address = (const char*) &m[1];
302 address_length = ntohs (m->address_length);
303 plugin_name = &address[address_length];
304 GNUNET_break (0 == ntohl (m->reserved));
305 if ( (ntohs (m->address_length) +
306 ntohs (m->plugin_name_length) +
307 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
308 (plugin_name[ntohs (m->plugin_name_length) - 1] != '\0') )
311 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
316 sh->suggest_cb (sh->suggest_cb_cls,
319 address, address_length,
320 find_session (sh, ntohl (m->session_id)),
323 GNUNET_CLIENT_receive (sh->client,
324 &process_ats_message, sh,
325 GNUNET_TIME_UNIT_FOREVER_REL);
330 * Re-establish the connection to the ATS service.
332 * @param sh handle to use to re-connect.
335 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
337 struct PendingMessage *p;
338 struct ClientStartMessage *init;
340 GNUNET_assert (NULL == sh->client);
341 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
342 GNUNET_assert (NULL != sh->client);
343 GNUNET_CLIENT_receive (sh->client,
344 &process_ats_message, sh,
345 GNUNET_TIME_UNIT_FOREVER_REL);
346 if ( (NULL == (p = sh->pending_head)) ||
347 (GNUNET_YES != p->is_init) )
349 p = GNUNET_malloc (sizeof (struct PendingMessage) +
350 sizeof (struct ClientStartMessage));
351 p->size = sizeof (struct ClientStartMessage);
352 p->is_init = GNUNET_YES;
353 init = (struct ClientStartMessage *) &p[1];
354 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
355 init->header.size = htons (sizeof (struct ClientStartMessage));
356 init->start_flag = htonl (START_FLAG_SCHEDULING);
357 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
366 * Initialize the ATS subsystem.
368 * @param cfg configuration to use
369 * @param suggest_cb notification to call whenever the suggestation changed
370 * @param suggest_cb_cls closure for 'suggest_cb'
371 * @return ats context
373 struct GNUNET_ATS_SchedulingHandle *
374 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
375 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
376 void *suggest_cb_cls)
378 struct GNUNET_ATS_SchedulingHandle *sh;
380 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
382 sh->suggest_cb = suggest_cb;
383 sh->suggest_cb_cls = suggest_cb_cls;
384 GNUNET_array_grow (sh->session_array,
385 sh->session_array_size,
393 * Client is done with ATS scheduling, release resources.
395 * @param sh handle to release
398 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
400 struct PendingMessage *p;
402 while (NULL != (p = sh->pending_head))
404 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
409 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
410 GNUNET_array_grow (sh->session_array,
411 sh->session_array_size,
418 * We would like to establish a new connection with a peer. ATS
419 * should suggest a good address to begin with.
422 * @param peer identity of the peer we need an address for
425 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
426 const struct GNUNET_PeerIdentity *peer)
428 struct PendingMessage *p;
429 struct RequestAddressMessage *m;
431 p = GNUNET_malloc (sizeof (struct PendingMessage) +
432 sizeof (struct RequestAddressMessage));
433 p->size = sizeof (struct RequestAddressMessage);
434 p->is_init = GNUNET_NO;
435 m = (struct RequestAddressMessage*) &p[1];
436 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
437 m->header.size = htons (sizeof (struct RequestAddressMessage));
438 m->reserved = htonl (0);
440 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
448 * We have updated performance statistics for a given address. Note
449 * that this function can be called for addresses that are currently
450 * in use as well as addresses that are valid but not actively in use.
451 * Furthermore, the peer may not even be connected to us right now (in
452 * which case the call may be ignored or the information may be stored
453 * for later use). Update bandwidth assignments.
456 * @param peer identity of the new peer
457 * @param plugin_name name of the transport plugin
458 * @param plugin_addr address (if available)
459 * @param plugin_addr_len number of bytes in plugin_addr
460 * @param session session handle (if available)
461 * @param ats performance data for the address
462 * @param ats_count number of performance records in 'ats'
465 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
466 const struct GNUNET_PeerIdentity *peer,
467 const char *plugin_name,
468 const void *plugin_addr, size_t plugin_addr_len,
469 struct Session *session,
470 const struct GNUNET_TRANSPORT_ATS_Information *ats,
473 struct PendingMessage *p;
474 struct AddressUpdateMessage *m;
475 struct GNUNET_TRANSPORT_ATS_Information *am;
480 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
481 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
482 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + namelen;
483 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
484 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
485 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
486 (ats_count >= GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
491 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
493 p->is_init = GNUNET_NO;
494 m = (struct AddressUpdateMessage*) &p[1];
495 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
496 m->header.size = htons (msize);
497 m->ats_count = htonl (ats_count);
499 m->address_length = htons (plugin_addr_len);
500 m->plugin_name_length = htons (namelen);
501 m->session_id = htonl (get_session_id (sh, session));
502 am = (struct GNUNET_TRANSPORT_ATS_Information*) &m[1];
503 memcpy (am, ats, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
504 pm = (char *) &am[ats_count];
505 memcpy (pm, plugin_addr, plugin_addr_len);
506 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
507 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
515 * A session got destroyed, stop including it as a valid address.
518 * @param peer identity of the peer
519 * @param plugin_name name of the transport plugin
520 * @param plugin_addr address (if available)
521 * @param plugin_addr_len number of bytes in plugin_addr
522 * @param session session handle that is no longer valid
525 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
526 const struct GNUNET_PeerIdentity *peer,
527 const char *plugin_name,
528 const void *plugin_addr,
529 size_t plugin_addr_len,
530 struct Session *session)
532 struct PendingMessage *p;
533 struct AddressDestroyedMessage *m;
539 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
540 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
542 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
543 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
544 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
549 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
551 p->is_init = GNUNET_NO;
552 m = (struct AddressDestroyedMessage*) &p[1];
553 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
554 m->header.size = htons (msize);
555 m->reserved = htonl (0);
557 m->address_length = htons (plugin_addr_len);
558 m->plugin_name_length = htons (namelen);
559 m->session_id = htonl (session_id = get_session_id (sh, session));
561 memcpy (pm, plugin_addr, plugin_addr_len);
562 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
563 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
567 remove_session (sh, session_id);
570 /* end of ats_api_scheduling.c */