1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 FRESCOR consortium partners:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politécnica Valencia, SPAIN
9 // Czech Technical University in Prague, CZECH REPUBLIC
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org for a link to partners' websites
18 // FRESCOR project (FP6/2005/IST/5-034026) is funded
19 // in part by the European Union Sixth Framework Programme
20 // The European Union is not liable of any use that may be
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FRSH API
36 // FRSH API is free software; you can redistribute it and/or modify
37 // it under the terms of the GNU General Public License as published by
38 // the Free Software Foundation; either version 2, or (at your option)
41 // FRSH API is distributed in the hope that it will be useful, but
42 // WITHOUT ANY WARRANTY; without even the implied warranty of
43 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 // General Public License for more details.
46 // You should have received a copy of the GNU General Public License
47 // distributed with FRSH API; see file COPYING. If not, write to the
48 // Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
51 // As a special exception, if you include this header file into source
52 // files to be compiled, this header file does not by itself cause
53 // the resulting executable to be covered by the GNU General Public
54 // License. This exception does not however invalidate any other
55 // reasons why the executable file might be covered by the GNU General
57 // -----------------------------------------------------------------------
59 //==============================================
60 // ******** ******* ******** ** **
61 // **///// /**////** **////// /** /**
62 // ** /** /** /** /** /**
63 // ******* /******* /********* /**********
64 // **//// /**///** ////////** /**//////**
65 // ** /** //** /** /** /**
66 // ** /** //** ******** /** /**
67 // // // // //////// // //
69 // FRSH(FRescor ScHeduler), pronounced "fresh"
70 //==============================================
71 #ifndef _FRSH_DISTRIBUTED_H_
72 #define _FRSH_DISTRIBUTED_H_
75 * @file frsh_distributed.h
79 #include "frsh_distributed_types.h"
80 #include "frsh_core_types.h"
83 * @defgroup distributed Distributed module
85 * This module defines the functions and typedefs for use in
86 * distributed applications.
88 * Each network is identified by its resource_id and FRSH hides its
89 * characteristics completely. The type of network is implied with
90 * its ID via a configuration table defined at compile time.
92 * FRSH uses the "message" as the atomic unit for every exchange.
93 * Queue sizes are measured in number of pending messages.
95 * FRSH provides a function to calculate the transmision time needed
96 * for a certain message size in a network as well as the maximum
97 * message size that can admit.
99 * Note also that package delivery guarantee is protocol dependent.
100 * For protocols in which the order is no guaranteed, the application
101 * needs to add extra info to detect possible package disorder.
103 * Summary of typical steps.
105 * 1. Map (internally in FRSH implementation)
106 * - node--> network_addresses
107 * - network --> resource_id's
108 * - unidirectional communication channel --> stream_id
109 * - other config --> protocol_info.
111 * 2. In a sending node:
112 * 2.1. Negotiates a "network contract" per communication channel
113 * that is used in the application. In the contract it is
115 * - frsh_resource_type = FRSH_RT_NETWORK.
116 * - frsh_resource_id = <network id #>
117 * - budget: Time needed to send the required data per period.
118 * (you can use frsh_netinfo_*() functions for this).
119 * - period: Period of sendings.
120 * - Queueing info: How will sends be queued at sendEndpoint.
121 * - Other protocol dependent function in protocol_contract_info.
122 * 2.2. Create a send_endpoint per any unidirectional stream that will
124 * resource_id --> the network through which the stream will
125 * flow (this is extra info needed for coherency
127 * destinator --> network_address of the destination.
128 * stream_id --> the unidirectional communication channel.
129 * 2.3. Bind the send_endpoint to the network contract negotiated
131 * 2.4. The (processor) sending vres invokes frsh_send_(a)sync() to
132 * send the data through the corresponding stream.
134 * 3. In a receiving node:
135 * 3.1. Create a receive_endpoint per any unidirectional stream
136 * that will be used in receiving.
137 * 3.2. The processor expecting a reception of message invokes
138 * frsh_receive_(a)sync() to read the incoming data.
140 * 4. When all comunication is finished and the channel is no longer
141 * needed the nodes will destroy the send and receive endpoints
142 * and the network contract will be canceled.
146 * frsh_distributed_init(void)
148 * This operation initializes all the installed networks and the structures
149 * that are necessary for the distributed module. Currently it is called by
150 * frsh_init so it is not necessary that the user calls it again.
153 * FRSH_ERR_INTERNAL_ERROR: protocol dependent internal errors \n
156 int frsh_distributed_init(void);
158 //////////////////////////////////////////////////////////////////////
160 //////////////////////////////////////////////////////////////////////
163 * @defgroup distcontract Contract Info for Distributed Systems
164 * @ingroup distributed
166 * These functions help you calculate the needed budget for network
167 * contracts and also to include protocol dependent info in contract
174 * frsh_network_get_max_message_size()
176 * This operation gives the maximum number of bytes that can be sent
177 * at a time through the send function when using the network designated by
178 * 'resource_id' and sending it to 'destination'.
180 * If the application needs to send bigger messages it will have to
183 * Some protocols, like IP, are capable of sending large messages
184 * (and use fragmentation internally) but other protocols don't.
186 * @param[in] resource_id The network we want the tx time from.
187 * @param[in] destination The destination address
188 * @param[out] max_size The maximum number of bytes for each message
192 * FRSH_ERR_INTERNAL_ERROR: protocol dependent internal errors \n
193 * FRSH_ERR_NOT_INITIALIZED: if the protocol is not initialized \n
194 * FRSH_ERR_RESOURCE_ID_INVALID: if resource id does not represent
195 * a network accessible from the current processing node \n
196 * FRSH_ERR_BAD_ARGUMENT: if pointers are NULL or destination is
200 int frsh_network_get_max_message_size
201 (const frsh_resource_id_t resource_id,
202 const frsh_network_address_t destination,
206 * frsh_network_bytes_to_budget()
208 * This operation converts a number of bytes into a temporal budget for
209 * a specific network. Network overheads are not included here but are
210 * considered internally when negotiating a specific contract.
212 * @param[in] resource_id The network
213 * @param[in] nbytes Number of bytes
214 * @param[out] budget The network budget for nbytes
218 * FRSH_ERR_INTERNAL_ERROR: protocol dependent internal errors \n
219 * FRSH_ERR_NOT_INITIALIZED: if the protocol is not initialized \n
220 * FRSH_ERR_RESOURCE_ID_INVALID: if resource id does not represent
221 * a network accessible from the current processing node \n
222 * FRSH_ERR_BAD_ARGUMENT: if pointers are NULL or nbytes is less
226 int frsh_network_bytes_to_budget
227 (const frsh_resource_id_t resource_id,
229 struct timespec *budget);
232 * frsh_network_budget_to_bytes()
234 * This operation converts a temporal budget into a number of bytes for
235 * a specific network. Network overheads are not included.
237 * @param[in] resource_id The network
238 * @param[in] budget The network budget for nbytes
239 * @param[out] nbytes Number of bytes
243 * FRSH_ERR_INTERNAL_ERROR: protocol dependent internal errors \n
244 * FRSH_ERR_NOT_INITIALIZED: if the protocol is not initialized \n
245 * FRSH_ERR_RESOURCE_ID_INVALID: if resource id does not represent
246 * a network accessible from the current processing node \n
247 * FRSH_ERR_BAD_ARGUMENT: if pointers are NULL or budget refers to
248 * an invalid time value \n
251 int frsh_network_budget_to_bytes
252 (const frsh_resource_id_t resource_id,
253 const struct timespec *budget,
257 * frsh_network_get_min_effective_budget()
259 * This operation gets the minimum effective budget for a network. Each message
260 * consumes a contracted budget in "chunks" (i.e: packets) that we call
261 * minimum effective budget.
263 * A negotiated contract, for N bytes in a period T, means that there is a
264 * virtual resource that reserves for the user:
266 * Ceiling ((N bytes) / budget_to_bytes (min_effective_budget)) "CHUNKS"
268 * Note that if the user decides not to send these N bytes at once but, say,
269 * one byte at a time, it will consume one "CHUNK" at a time and the reserved
270 * budget will become exhausted before sending all the bytes.
272 * @param[in] resource_id The network
273 * @param[out] budget The network budget
277 * FRSH_ERR_INTERNAL_ERROR: protocol dependent internal errors \n
278 * FRSH_ERR_NOT_INITIALIZED: if the protocol is not initialized \n
279 * FRSH_ERR_RESOURCE_ID_INVALID: if resource id does not represent
280 * a network accessible from the current processing node \n
281 * FRSH_ERR_BAD_ARGUMENT: if pointers are NULL \n
284 int frsh_network_get_min_effective_budget
285 (const frsh_resource_id_t resource_id,
286 struct timespec *budget);
289 * frsh_contract_set_queueing_info()
291 * This function adds queueing parameters that will be used in the
292 * sendEndpoint when the sendEndpoint is bound to the vres.
294 int frsh_contract_set_queueing_info(frsh_endpoint_queueing_info_t queueing_info,
295 frsh_contract_t *contract);
298 * frsh_contract_get_queueing_info()
300 * This function gets the queueing parameters that were specified in
301 * the network contract.
303 int frsh_contract_get_queueing_info(const frsh_contract_t *contract,
304 frsh_endpoint_queueing_info_t *queueing_info);
307 * frsh_contract_set_protocol_info
309 * We add protocol info to the contract
311 int frsh_contract_set_protocol_info(frsh_protocol_info_t protocol_info,
312 frsh_contract_t *contract);
315 * frsh_contract_get_protocol_info
317 * We get protocol info from the contract
319 int frsh_contract_get_protocol_info(frsh_contract_t *contract,
320 frsh_protocol_info_t *protocol_info);
323 * frsh_contract_marshal
325 * Convert a contract to a sequence of bytes of minimum size so it can
326 * be sent through the network with the minimum amount of bytes.
330 int frsh_contract_marshal(const frsh_contract_t *contract,
331 unsigned char *buffer,
332 const size_t buffer_size,
337 //////////////////////////////////////////////////////////////////////
338 // TRANSMISSION SERVICES
339 //////////////////////////////////////////////////////////////////////
342 * @defgroup txservices Transmission services
343 * @ingroup distributed
345 * These functions allow to create and manage endpoints for sending
346 * and receiving and to perform send and receive operations both
347 * synchronously (blocking) and asynchronously (non-blocking).
354 * frsh_send_endpoint_create()
356 * This operation creates a unidirectional stream input endpoint
357 * through which, after the corresponding binding, it is possible to
358 * send data to a unicast or multicast destination.
360 * @param[in] resource_id Identifier of the network referred in the
361 * network contract as a resource_id.
362 * @param[in] destination FRSH abstraction of the protocol address for the
364 * @param[in] stream_id Identifier of the communication channel between
365 * the nodes. Multiplexing is achieved by using
366 * different streams between the same nodes and the
368 * @param[in] queueing_info Queueing params of the endpoint (size and
370 * @param[in] protocol_info Optional protocol-dependent info.
371 * @param[out] endpoint Placeholder for the endpoint object.
373 int frsh_send_endpoint_create
374 (frsh_resource_id_t resource_id,
375 frsh_network_address_t destination,
376 frsh_stream_id_t stream_id,
377 frsh_send_endpoint_protocol_info_t protocol_info,
378 frsh_send_endpoint_t *endpoint);
381 * frsh_send_endpoint_get_params()
383 * This operation returns in the variables associated to the
384 * endpoint at creation time.
386 int frsh_send_endpoint_get_params
387 (const frsh_send_endpoint_t endpoint,
388 frsh_resource_id_t *resource_id,
389 frsh_network_address_t *destination,
390 frsh_stream_id_t *stream,
391 frsh_send_endpoint_protocol_info_t *protocol_info);
394 * frsh_send_endpoint_destroy()
396 * This operation eliminates any resources reserved for the referenced
397 * endpoint. Pending messages will be discarded and processor-vres
398 * waiting in a synchronous operation will be awoken with an error
401 int frsh_send_endpoint_destroy
402 (frsh_send_endpoint_t endpoint);
406 * frsh_send_endpoint_bind()
408 * This operation associates a send endpoint with a network vres,
409 * which means that messages sent through this endpoint will consume
410 * the vres's reserved bandwidth and its packets will be sent
411 * according to the contract established for that vres.
413 * If the endpoint is already bound to another vres, it is effectively
414 * unbound from it and bound to the specified one. However if a vres
415 * is already bound to another endpoint an error is returned.
417 * A consistency check is done in which the resource_id specified at
418 * endpoint creation must correspond to the resource_id of the vres
421 * @return 0 if successful \n
422 * FRSH_ERR_BAD_ARGUMENT if the endpoint or the vres are not
424 * FRSH_ERR_ALREADY_BOUND if the vres is already bound to some
425 * other send endpoint \n
426 * FRSH_ERR_WRONG_NETWORK if the vres network id is not the same
427 * as the one in the endpoint \n
429 int frsh_send_endpoint_bind
430 (frsh_vres_id_t vres,
431 frsh_send_endpoint_t endpoint);
434 * frsh_send_endpoint_unbind()
436 * This operation unbinds a send endpoint from a vres. Endpoints with
437 * no vres associated cannot be used to send data, and they stay in
438 * that state until they are either eliminated or bound again.
440 * @return 0 if successful \n
441 * FRSH_ERR_NOT_BOUND if the endpoint was not bound \n
443 int frsh_send_endpoint_unbind
444 (frsh_send_endpoint_t endpoint);
447 * frsh_send_endpoint_get_vres_id()
449 * This operation copies the id of the vres that is bound to the
450 * specified send endpoint into the variable pointed to by vres.
452 * @return 0 if successful \n
453 * FRSH_ERR_NOT_BOUND if the endpoint was not bound \n
454 * FRSH_ERR_BAD_ARGUMENT if the endpoint is not valid or vres
457 int frsh_send_endpoint_get_vres_id
458 (const frsh_send_endpoint_t endpoint,
459 frsh_vres_id_t *vres);
464 * This operation sends a message stored in msg and of length size
465 * through the given endpoint. The operation is non-blocking and
466 * returns immediately.
468 * An internal frsh service will schedule the sending of messages and
469 * implement the communications sporadic vres corresponding to the
470 * network vres bound to the given endpoint.
472 * @returns 0 if successful \n
473 * FRSH_ERR_BAD_ARGUMENT if endpoint is not valid \n
474 * FRSH_ERR_NOT_BOUND if endpoint is not bound to a valid vres \n
475 * FRSH_ERR_TOO_LARGE if the message is too large for the
476 * network protocol \n
477 * FRSH_ERR_BUFFER_FULL if the message has been discarded
478 * because the queue is full (and does not
479 * have the policy FRSH_QP_OLDEST \n
482 (const frsh_send_endpoint_t endpoint,
489 * Similar to previous function but now the sending vres gets blocked
490 * until the message is processed.
493 (const frsh_send_endpoint_t endpoint,
498 * frsh_send_endpoint_get_status()
500 * This function tells the number of messages still pending in the
501 * endpoint queue, whether the network is up or down with some
502 * optional information which is protocol_dependent.
504 int frsh_send_endpoint_get_status
505 (const frsh_send_endpoint_t endpoint,
506 int *number_pending_msg,
507 frsh_endpoint_network_status_t *network_status,
508 frsh_protocol_status_t *protocol_status);
511 * frsh_receive_endpoint_create()
513 * This operation creates a receive endpoint associated with a
514 * undirectional stream within a network interface of the node.
516 * Receiving endpoints are not bound to any network vres, this is
517 * because don't originate any traffic.
519 * Note that the protocol address is not needed for reception because
520 * it can be determined internally by FRSH based on the resource_id.
522 * Note also that messages may come from diferent originators.
524 * @param[in] resource_id Id of the network from which we listen.
525 * @param[in] stream_id Id of the stream within the network.
526 * @param[in] queueing_info Buffering information(queue size and
528 * @param[in] protocol_info Extra protocol info opaque for the
530 * @param[in] endpoin Placeholder for the endpoint object.
532 * @return 0 if successful \n
533 * FRSH_ERR_BAD_ARGUMENT if the stream or the network id are not
536 int frsh_receive_endpoint_create
537 (frsh_resource_id_t resource_id,
538 frsh_stream_id_t stream_id,
539 frsh_endpoint_queueing_info_t queueing_info,
540 frsh_receive_endpoint_protocol_info_t protocol_info,
541 frsh_receive_endpoint_t *endpoint);
544 * frsh_receive_endpoint_get_params()
546 * This operation returns in the variables associated to the
547 * endpoint at creation time.
549 int frsh_receive_endpoint_get_params
550 (const frsh_receive_endpoint_t endpoint,
551 frsh_resource_id_t *resource_id,
552 frsh_stream_id_t *stream,
553 frsh_endpoint_queueing_info_t *queueing_info,
554 frsh_receive_endpoint_protocol_info_t *protocol_info);
557 * frsh_receive_endpoint_destroy()
559 * This operation eliminates any resources reserved for the referenced
560 * endpoint. Pending messages will be discarded and processor-vres
561 * waiting in a synchronous operation will be awoken with an error
564 int frsh_receive_endpoint_destroy
565 (frsh_receive_endpoint_t endpoint);
569 * frsh_receive_sync()
571 * If there are no messages available in the specified receive endpoint
572 * this operation blocks the calling thread waiting for a message to be
575 * When a message is available, if its size is less than or
576 * equal to the buffer_size, the function stores it in the variable
577 * pointed to by buffer and puts the number of bytes received in the
578 * variable pointed to by message size.
580 * The function fails with FRSH_ERR_NO_SPACE if the buffersize is
581 * too small for the message received. In this case the message is
584 * Messages arriving at a destination buffer that is full will be
585 * silently discarded (details in the queueing policy of the
586 * endpoint). The application is responsible of reading the receive
587 * endpoints with appropriate regularity, or of using a sequence
588 * number or some other mechanism to detect any lost messages.
590 * @return 0 if successful \n
591 * FRSH_ERR_BAD_ARGUMENT if the endpoint is not valid, or if
592 * buffer or message_size are NULL.\n
593 * FRSH_ERR_NO_SPACE if the message size is bigger than the
596 int frsh_receive_sync
597 (const frsh_receive_endpoint_t endpoint,
600 size_t *message_size,
601 frsh_network_address_t *from);
604 * frsh_receive_async()
606 * This operation is similar to the previous one but it works in a non
607 * blocking (asynchronous) fashion. If no message is available it
608 * returns with error FRSH_NO_MESSAGE.
610 * @return 0 if successful \n
611 * FRSH_ERR_BAD_ARGUMENT if the endpoint is not valid, or if
612 * buffer or message_size are NULL \n
613 * FRSH_NO_MESSAGE if no messages are available in the queue \n
614 * FRSH_ERR_NO_SPACE if the message size is bigger than the
617 int frsh_receive_async
618 (const frsh_receive_endpoint_t endpoint,
621 size_t *message_size,
622 frsh_network_address_t *from);
626 * frsh_receive_endpoint_get_status
628 * This function tells the number of messages still pending in the
629 * endpoint queue, whether the network is up or down and some optional
630 * information which is protocol dependent.
632 int frsh_receive_endpoint_get_status
633 (const frsh_receive_endpoint_t endpoint,
634 int *number_pending_messages,
635 frsh_endpoint_network_status_t *network_status,
636 frsh_protocol_status_t *protocol_status);
640 #endif // _FRSH_DISTRIBUTED_H_