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 - 2008 FRESCOR consortium partners:
24 * Universidad de Cantabria, SPAIN
25 * University of York, UK
26 * Scuola Superiore Sant'Anna, ITALY
27 * Kaiserslautern University, GERMANY
28 * Univ. Politécnica 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 for a link to partners' websites
38 * 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
43 * This file is part of FRESCAN
45 * FRESCAN is free software; you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation; either version 2, or (at your option)
50 * FRESCAN is distributed in the hope that it will be useful, but
51 * WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
53 * General Public License for more details.
55 * You should have received a copy of the GNU General Public License
56 * distributed with FRESCAN; see file COPYING. If not, write to the
57 * Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
60 * As a special exception, including FRESCAN header files in a file,
61 * instantiating FRESCAN generics or templates, or linking other files
62 * with FRESCAN objects to produce an executable application, does not
63 * by itself cause the resulting executable application to be covered
64 * by the GNU General Public License. This exception does not
65 * however invalidate any other reasons why the executable file might be
66 * covered by the GNU Public License.
67 * -----------------------------------------------------------------------
71 #include "frescan_servers.h"
72 #include "frescan_servers_replenishments.h" // frescan_replenishments_xxx
73 #include "frescan_debug.h"
74 #include "frescan_packets.h"
75 #include "frescan_data.h"
78 #include <misc/linux_list.h>
79 #include "fosa_time_timespec.h" // smaller_timespec
82 * frescan_servers_init() - initialize server structures
84 * @net: the network instance
87 int frescan_servers_init(frescan_network_t net)
91 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
93 ret = freelist_init(&frescan_data[net].ss_id_freelist, FRESCAN_MX_IDS);
94 if (ret != 0) return ret;
96 for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
97 INIT_LIST_HEAD(&frescan_data[net].ss_active_head.servers_list);
100 ret = frescan_replenishments_init(net);
102 FRESCAN_ERROR("could not initialize the replenishments\n");
110 * frescan_servers_create() - create a sporadic server
112 * @net: the network instance
113 * @params: the parameters for the server
114 * @id: the identificator for the server as a return value
118 int frescan_servers_create(frescan_network_t net,
119 const frescan_server_params_t *params,
124 frescan_ss_data_t *server;
125 frescan_repl_op_t *repl;
127 FRESCAN_ACQUIRE_LOCK(&frescan_data[net].lock);
128 pos = freelist_alloc(&frescan_data[net].ss_id_freelist);
129 FRESCAN_RELEASE_LOCK(&frescan_data[net].lock);
132 FRESCAN_ERROR("could not allocate servers\n");
136 *id = (frescan_ss_t)pos;
138 server = &frescan_data[net].ss_data[*id];
142 server->committed_params = *params;
143 server->perceived_params = server->committed_params;
144 server->current_priority = params->prio;
145 server->pending_packets = 0;
147 // the first act_time is set to the server creation time
148 clock_gettime (CLOCK_MONOTONIC, &server->act_time);
150 // init the list of packets associated to the server
151 INIT_LIST_HEAD(&server->packet_list.fifo_list);
153 // allocate the replenishment capacity queue
154 INIT_LIST_HEAD(&server->replenishments.repl_list);
155 for (i=0; i < params->budget; i++) {
156 repl = frescan_repl_op_alloc();
157 repl->when = server->act_time;
159 list_add_tail(&repl->repl_list,
160 &server->replenishments.repl_list);
163 // the repl timer sends a signal when it expires with the server id
164 evp.sigev_notify = SIGEV_SIGNAL;
165 evp.sigev_signo = FRESCAN_REPL_SIGNAL_NUM;
166 evp.sigev_value.sival_int = (int)*id;
168 ret = timer_create (CLOCK_MONOTONIC, &evp, &server->repl_timer);
170 FRESCAN_ERROR("could not create timer\n");
174 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
175 "server created, id:%u budget:%u prio:%u\n",
176 *id, server->committed_params.budget,
177 server->committed_params.prio);
183 * frescan_servers_set_perceived() - update a sporadic server perceived data
185 * @net: the network instance
186 * @params: the parameters for the server
187 * @id: the identificator for the server
191 int frescan_servers_set_perceived(frescan_network_t net,
192 const frescan_server_params_t *params,
195 frescan_data[net].ss_data[id].perceived_params = *params;
200 * frescan_servers_commit_perceived() - commit sporadic server perceived data
202 * Add or remove repl operations according to the budget change
204 * @net: the network instance
205 * @id: the identificator for the server
209 int frescan_servers_commit_perceived(frescan_network_t net,
213 frescan_ss_data_t *server;
214 int budget_variation;
215 frescan_repl_op_t *repl = NULL;
216 struct list_head *pos;
218 server = &frescan_data[net].ss_data[id];
219 budget_variation = server->perceived_params.budget -
220 server->committed_params.budget;
222 if (budget_variation > 0) {
223 // we have more budget: add repl ops to the tail
224 for (i=0; i < budget_variation; i++) {
225 repl = frescan_repl_op_alloc();
226 repl->when = server->act_time; // TODO: check when!
228 list_add_tail(&repl->repl_list,
229 &server->replenishments.repl_list);
232 // we have less budget: remove repl ops from the tail
233 for (i=0; i > budget_variation; i--) {
234 list_for_each_prev(pos,
235 &server->replenishments.repl_list) {
236 repl = list_entry(pos,
241 list_del(&repl->repl_list);
243 ret = frescan_repl_op_free(repl);
244 if (ret != 0) return ret;
248 server->committed_params = server->perceived_params;
254 * frescan_servers_update() - update a sporadic server data
256 * @net: the network instance
257 * @params: the parameters for the server
258 * @id: the identificator for the server
262 int frescan_servers_update(frescan_network_t net,
263 const frescan_server_params_t *params,
268 ret = frescan_servers_set_perceived(net, params, id);
269 if (ret != 0) return ret;
271 ret = frescan_servers_commit_perceived(net, id);
272 if (ret != 0) return ret;
278 * frescan_servers_destroy() - delete a sporadic server
280 * @net: the network instance
281 * @id: the identificator for the server
285 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
288 frescan_repl_op_t *repl;
289 frescan_packet_t *packet;
290 frescan_ss_data_t *server;
292 server = &frescan_data[net].ss_data[id];
294 ret = timer_delete (server->repl_timer);
296 FRESCAN_ERROR("could not delete timer\n");
300 // remove packets associated to the server
301 if (!list_empty(&server->packet_list.fifo_list)) {
302 FRESCAN_WARNING("destroying a server with packets enqueued\n");
303 list_for_each_entry(packet,
304 &server->packet_list.fifo_list,
306 ret = frescan_packets_free(packet);
307 if (ret != 0) return ret;
309 INIT_LIST_HEAD(&server->packet_list.fifo_list);
312 // remove the servers replenishment capacity queue
313 list_for_each_entry(repl,
314 &server->replenishments.repl_list,
316 ret = frescan_repl_op_free(repl);
317 if (ret != 0) return ret;
319 INIT_LIST_HEAD(&server->replenishments.repl_list);
321 FRESCAN_ACQUIRE_LOCK(&frescan_data[net].lock);
322 list_del(&server->servers_list);
323 ret = freelist_free(&frescan_data[net].ss_id_freelist, id);
324 FRESCAN_RELEASE_LOCK(&frescan_data[net].lock);
327 FRESCAN_ERROR("could not free server data from pool\n");
335 * frescan_servers_get_data() - get a sporadic server data
337 * @net: the network instance
338 * @params: the parameters of the server
339 * @id: the identificator for the server
343 int frescan_servers_get_data(frescan_network_t net,
344 frescan_server_params_t *params,
347 *params = frescan_data[net].ss_data[id].perceived_params;
352 * frescan_servers_get_current_budget() - get the current ss budget
354 * @net: the network instance
355 * @id: the identificator for the server
356 * @current_budget: the current budget of the server
358 * Traverse the capacity queue until we find a replenishment operation
359 * that was programmed for a time later than now. The number of iterations
360 * is the budget in the capacity queue.
362 * That budget must be modified if the perceived budget has not been
367 int frescan_servers_get_current_budget(frescan_network_t net,
369 frescan_budget_t *current_budget)
372 frescan_repl_op_t *repl;
373 frescan_ss_data_t *server;
376 server = &frescan_data[net].ss_data[id];
378 // first we get the current real budget in the capacity queue
379 clock_gettime (CLOCK_MONOTONIC, &now);
382 list_for_each_entry(repl,
383 &server->replenishments.repl_list,
385 if (smaller_timespec(now, repl->when)) break;
389 // if the perceived budget is less than the real budget
390 // we have to give a _perceived_ current budget
391 if (server->perceived_params.budget < server->committed_params.budget) {
392 count = server->perceived_params.budget -
393 (server->committed_params.budget - count);
394 if (count < 0) count = 0;
397 *current_budget = (frescan_budget_t)count;
403 * frescan_servers_get_highest_prio() - get the server with highest priority
405 * @net: the network instance
406 * @id: the identificator for the server
407 * @prio: the priority of that server
409 * For each active server, check the priority.
410 * If "id" is returned with a value of FRESCAN_MX_IDS,
411 * there are no active servers.
412 * NOTE: id=FRESCAN_MX_IDS is the identifier for fixed priority messages
413 * TODO: use a priority queue of active servers
417 int frescan_servers_get_highest_prio(frescan_network_t net,
419 frescan_prio_t *prio)
421 frescan_ss_data_t *server;
423 if (list_empty(&frescan_data[net].ss_active_head.servers_list)) {
424 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "server list is empty\n");
425 *id = FRESCAN_MX_IDS;
430 list_for_each_entry(server,
431 &frescan_data[net].ss_active_head.servers_list,
433 if (server->current_priority >= *prio) {
435 *prio = server->current_priority;
439 DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
440 "highest prio:%u id:%u\n", *prio, *id);
446 * frescan_servers_frame_sent() - hook to control the server budget and prio
448 * @net: the network instance
449 * @id: the identificator for the server
451 * This function is called when a frame has been effectively sent through the
452 * CAN bus and that frame is associated to a certain server. The function
453 * decreases the capacity of the server and sets the priority to background
454 * in case the budget is exhausted.
456 * NOTE: the replenishment operation is programmed using the corresponding
457 * function at frescan_servers_replenishments module
460 int frescan_servers_frame_sent(frescan_network_t net,
462 frescan_packet_t *packet)
465 struct timespec *repl_time;
466 frescan_ss_data_t *server;
468 server = &frescan_data[net].ss_data[id];
470 if (server->current_priority != FRESCAN_BACKGROUND_PRIO) {
471 if (smaller_timespec(packet->timestamp, server->act_time)) {
472 repl_time = &server->act_time;
474 repl_time = &packet->timestamp;
477 ret = frescan_replenishment_program(net, id, repl_time);
478 if (ret != 0) return -1;