2 * @file frescan_servers.c
4 * @brief FRESCAN sporadic servers
15 * This file contains the FRESCAN sporadic servers that allow to isolate
16 * different streams of data by assigning them a budget and replenishment
21 //----------------------------------------------------------------------
22 // Copyright (C) 2006 - 2009 by the FRESCOR consortium:
24 // Universidad de Cantabria, SPAIN
25 // University of York, UK
26 // Scuola Superiore Sant'Anna, ITALY
27 // Kaiserslautern University, GERMANY
28 // Univ. Politecnica Valencia, SPAIN
29 // Czech Technical University in Prague, CZECH REPUBLIC
31 // Thales Communication S.A. FRANCE
32 // Visual Tools S.A. SPAIN
33 // Rapita Systems Ltd UK
36 // See http://www.frescor.org
38 // The FRESCOR project (FP6/2005/IST/5-034026) is funded
39 // in part by the European Union Sixth Framework Programme
40 // The European Union is not liable of any use that may be
44 // based on previous work (FSF) done in the FIRST project
46 // Copyright (C) 2005 Mälardalen University, SWEDEN
47 // Scuola Superiore S.Anna, ITALY
48 // Universidad de Cantabria, SPAIN
49 // University of York, UK
51 // This file is part of FNA (Frescor Network Adaptation)
53 // FNA is free software; you can redistribute it and/or modify it
54 // under terms of the GNU General Public License as published by the
55 // Free Software Foundation; either version 2, or (at your option) any
56 // later version. FNA is distributed in the hope that it will be
57 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
58 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
59 // General Public License for more details. You should have received a
60 // copy of the GNU General Public License along with FNA; see file
61 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
62 // Cambridge, MA 02139, USA.
64 // As a special exception, including FNA header files in a file,
65 // instantiating FNA generics or templates, or linking other files
66 // with FNA objects to produce an executable application, does not
67 // by itself cause the resulting executable application to be covered
68 // by the GNU General Public License. This exception does not
69 // however invalidate any other reasons why the executable file might be
70 // covered by the GNU Public License.
71 // -----------------------------------------------------------------------
75 #include "frescan_servers.h"
76 #include "frescan_servers_replenishments.h" // frescan_replenishments_xxx
77 #include "frescan_debug.h"
78 #include "frescan_packets.h"
79 #include "frescan_data.h"
82 #include <misc/linux_list.h>
83 #include "fosa_time_timespec.h" // smaller_timespec
86 * frescan_servers_init() - initialize server structures
88 * @net: the network instance
91 int frescan_servers_init(frescan_network_t net)
95 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
97 ret = freelist_init(&frescan_data[net].ss_id_freelist, FRESCAN_MX_IDS);
98 if (ret != 0) return ret;
100 for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
101 INIT_LIST_HEAD(&frescan_data[net].ss_active_head.servers_list);
104 ret = frescan_replenishments_init(net);
106 FRESCAN_ERROR("could not initialize the replenishments\n");
114 * frescan_servers_create() - create a sporadic server
116 * @net: the network instance
117 * @params: the parameters for the server
118 * @id: the identificator for the server as a return value
122 int frescan_servers_create(frescan_network_t net,
123 const frescan_server_params_t *params,
128 frescan_ss_data_t *server;
129 frescan_repl_op_t *repl;
131 FRESCAN_ACQUIRE_LOCK(&frescan_data[net].lock);
132 pos = freelist_alloc(&frescan_data[net].ss_id_freelist);
133 FRESCAN_RELEASE_LOCK(&frescan_data[net].lock);
136 FRESCAN_ERROR("could not allocate servers\n");
140 *id = (frescan_ss_t)pos;
142 server = &frescan_data[net].ss_data[*id];
146 server->committed_params = *params;
147 server->perceived_params = server->committed_params;
148 server->current_priority = params->prio;
149 server->pending_packets = 0;
151 // the first act_time is set to the server creation time
152 clock_gettime (CLOCK_MONOTONIC, &server->act_time);
154 // init the list of packets associated to the server
155 INIT_LIST_HEAD(&server->packet_list.fifo_list);
157 // allocate the replenishment capacity queue
158 INIT_LIST_HEAD(&server->replenishments.repl_list);
159 for (i=0; i < params->budget; i++) {
160 repl = frescan_repl_op_alloc();
161 repl->when = server->act_time;
163 list_add_tail(&repl->repl_list,
164 &server->replenishments.repl_list);
167 // the repl timer sends a signal when it expires with the server id
168 evp.sigev_notify = SIGEV_SIGNAL;
169 evp.sigev_signo = FRESCAN_REPL_SIGNAL_NUM;
170 evp.sigev_value.sival_int = (int)*id;
172 ret = timer_create (CLOCK_MONOTONIC, &evp, &server->repl_timer);
174 FRESCAN_ERROR("could not create timer\n");
178 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
179 "server created, id:%u budget:%u prio:%u\n",
180 *id, server->committed_params.budget,
181 server->committed_params.prio);
187 * frescan_servers_set_perceived() - update a sporadic server perceived data
189 * @net: the network instance
190 * @params: the parameters for the server
191 * @id: the identificator for the server
195 int frescan_servers_set_perceived(frescan_network_t net,
196 const frescan_server_params_t *params,
199 frescan_data[net].ss_data[id].perceived_params = *params;
204 * frescan_servers_commit_perceived() - commit sporadic server perceived data
206 * Add or remove repl operations according to the budget change
208 * @net: the network instance
209 * @id: the identificator for the server
213 int frescan_servers_commit_perceived(frescan_network_t net,
217 frescan_ss_data_t *server;
218 int budget_variation;
219 frescan_repl_op_t *repl = NULL;
220 struct list_head *pos;
222 server = &frescan_data[net].ss_data[id];
223 budget_variation = server->perceived_params.budget -
224 server->committed_params.budget;
226 if (budget_variation > 0) {
227 // we have more budget: add repl ops to the tail
228 for (i=0; i < budget_variation; i++) {
229 repl = frescan_repl_op_alloc();
230 repl->when = server->act_time; // TODO: check when!
232 list_add_tail(&repl->repl_list,
233 &server->replenishments.repl_list);
236 // we have less budget: remove repl ops from the tail
237 for (i=0; i > budget_variation; i--) {
238 list_for_each_prev(pos,
239 &server->replenishments.repl_list) {
240 repl = list_entry(pos,
245 list_del(&repl->repl_list);
247 ret = frescan_repl_op_free(repl);
248 if (ret != 0) return ret;
252 server->committed_params = server->perceived_params;
258 * frescan_servers_update() - update a sporadic server data
260 * @net: the network instance
261 * @params: the parameters for the server
262 * @id: the identificator for the server
266 int frescan_servers_update(frescan_network_t net,
267 const frescan_server_params_t *params,
272 ret = frescan_servers_set_perceived(net, params, id);
273 if (ret != 0) return ret;
275 ret = frescan_servers_commit_perceived(net, id);
276 if (ret != 0) return ret;
282 * frescan_servers_destroy() - delete a sporadic server
284 * @net: the network instance
285 * @id: the identificator for the server
289 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
292 frescan_repl_op_t *repl;
293 frescan_packet_t *packet;
294 frescan_ss_data_t *server;
296 server = &frescan_data[net].ss_data[id];
298 ret = timer_delete (server->repl_timer);
300 FRESCAN_ERROR("could not delete timer\n");
304 // remove packets associated to the server
305 if (!list_empty(&server->packet_list.fifo_list)) {
306 FRESCAN_WARNING("destroying a server with packets enqueued\n");
307 list_for_each_entry(packet,
308 &server->packet_list.fifo_list,
310 ret = frescan_packets_free(packet);
311 if (ret != 0) return ret;
313 INIT_LIST_HEAD(&server->packet_list.fifo_list);
316 // remove the servers replenishment capacity queue
317 list_for_each_entry(repl,
318 &server->replenishments.repl_list,
320 ret = frescan_repl_op_free(repl);
321 if (ret != 0) return ret;
323 INIT_LIST_HEAD(&server->replenishments.repl_list);
325 FRESCAN_ACQUIRE_LOCK(&frescan_data[net].lock);
326 list_del(&server->servers_list);
327 ret = freelist_free(&frescan_data[net].ss_id_freelist, id);
328 FRESCAN_RELEASE_LOCK(&frescan_data[net].lock);
331 FRESCAN_ERROR("could not free server data from pool\n");
339 * frescan_servers_get_data() - get a sporadic server data
341 * @net: the network instance
342 * @params: the parameters of the server
343 * @id: the identificator for the server
347 int frescan_servers_get_data(frescan_network_t net,
348 frescan_server_params_t *params,
351 *params = frescan_data[net].ss_data[id].perceived_params;
356 * frescan_servers_get_current_budget() - get the current ss budget
358 * @net: the network instance
359 * @id: the identificator for the server
360 * @current_budget: the current budget of the server
362 * Traverse the capacity queue until we find a replenishment operation
363 * that was programmed for a time later than now. The number of iterations
364 * is the budget in the capacity queue.
366 * That budget must be modified if the perceived budget has not been
371 int frescan_servers_get_current_budget(frescan_network_t net,
373 frescan_budget_t *current_budget)
376 frescan_repl_op_t *repl;
377 frescan_ss_data_t *server;
380 server = &frescan_data[net].ss_data[id];
382 // first we get the current real budget in the capacity queue
383 clock_gettime (CLOCK_MONOTONIC, &now);
386 list_for_each_entry(repl,
387 &server->replenishments.repl_list,
389 if (smaller_timespec(now, repl->when)) break;
393 // if the perceived budget is less than the real budget
394 // we have to give a _perceived_ current budget
395 if (server->perceived_params.budget < server->committed_params.budget) {
396 count = server->perceived_params.budget -
397 (server->committed_params.budget - count);
398 if (count < 0) count = 0;
401 *current_budget = (frescan_budget_t)count;
407 * frescan_servers_get_highest_prio() - get the server with highest priority
409 * @net: the network instance
410 * @id: the identificator for the server
411 * @prio: the priority of that server
413 * For each active server, check the priority.
414 * If "id" is returned with a value of FRESCAN_MX_IDS,
415 * there are no active servers.
416 * NOTE: id=FRESCAN_MX_IDS is the identifier for fixed priority messages
417 * TODO: use a priority queue of active servers
421 int frescan_servers_get_highest_prio(frescan_network_t net,
423 frescan_prio_t *prio)
425 frescan_ss_data_t *server;
427 if (list_empty(&frescan_data[net].ss_active_head.servers_list)) {
428 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server list is empty\n");
429 *id = FRESCAN_MX_IDS;
434 list_for_each_entry(server,
435 &frescan_data[net].ss_active_head.servers_list,
437 if (server->current_priority >= *prio) {
439 *prio = server->current_priority;
443 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
444 "highest prio:%u id:%u\n", *prio, *id);
450 * frescan_servers_frame_sent() - hook to control the server budget and prio
452 * @net: the network instance
453 * @id: the identificator for the server
455 * This function is called when a frame has been effectively sent through the
456 * CAN bus and that frame is associated to a certain server. The function
457 * decreases the capacity of the server and sets the priority to background
458 * in case the budget is exhausted.
460 * NOTE: the replenishment operation is programmed using the corresponding
461 * function at frescan_servers_replenishments module
464 int frescan_servers_frame_sent(frescan_network_t net,
466 frescan_packet_t *packet)
469 struct timespec *repl_time;
470 frescan_ss_data_t *server;
472 server = &frescan_data[net].ss_data[id];
474 if (server->current_priority != FRESCAN_BACKGROUND_PRIO) {
475 if (smaller_timespec(packet->timestamp, server->act_time)) {
476 repl_time = &server->act_time;
478 repl_time = &packet->timestamp;
481 ret = frescan_replenishment_program(net, id, repl_time);
482 if (ret != 0) return -1;