]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_servers.c
add group negotiations to frescan and change all the requests and messages to map...
[frescor/fna.git] / src_frescan / frescan_servers.c
1 /*!
2  * @file frescan_servers.c
3  *
4  * @brief FRESCAN sporadic servers
5  *
6  * @version 0.01
7  *
8  * @date 27-Feb-2008
9  *
10  * @author
11  *      Daniel Sangorrin
12  *
13  * @comments
14  *
15  * This file contains the FRESCAN sporadic servers that allow to isolate
16  * different streams of data by assigning them a budget and replenishment
17  * period.
18  *
19  * @license
20  *
21  * -----------------------------------------------------------------------
22  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
23  *
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
30  *    ENEA                                   SWEDEN
31  *    Thales Communication S.A.              FRANCE
32  *    Visual Tools S.A.                      SPAIN
33  *    Rapita Systems Ltd                     UK
34  *    Evidence                               ITALY
35  *
36  *    See http://www.frescor.org for a link to partners' websites
37  *
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
41  *        made of this code.
42  *
43  *  This file is part of FRESCAN
44  *
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)
48  *  any later version.
49  *
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.
54  *
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
58  *  02111-1307, USA.
59  *
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  * -----------------------------------------------------------------------
68  *
69  */
70
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"
76 #include <signal.h>
77 #include <time.h>
78 #include <misc/linux_list.h>
79 #include "fosa_time_timespec.h" // smaller_timespec
80
81 /**
82  * frescan_servers_init() - initialize server structures
83  *
84  * @net: the network instance
85  */
86
87 int frescan_servers_init(frescan_network_t net)
88 {
89         int ret, i;
90
91         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
92
93         ret = freelist_init(&frescan_data[net].ss_id_freelist, FRESCAN_MX_IDS);
94         if (ret != 0) return ret;
95
96         for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
97                 INIT_LIST_HEAD(&frescan_data[net].ss_active_head.servers_list);
98         }
99
100         ret = frescan_replenishments_init(net);
101         if (ret != 0) {
102                 FRESCAN_ERROR("could not initialize the replenishments\n");
103                 return -1;
104         }
105
106         return 0;
107 }
108
109 /**
110  * frescan_servers_create() - create a sporadic server
111  *
112  * @net: the network instance
113  * @params: the parameters for the server
114  * @id: the identificator for the server as a return value
115  *
116  */
117
118 int frescan_servers_create(frescan_network_t net,
119                            const frescan_server_params_t *params,
120                            frescan_ss_t *id)
121 {
122         int i, ret, pos;
123         struct sigevent evp;
124         frescan_ss_data_t *server;
125         frescan_repl_op_t *repl;
126
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);
130
131         if (pos == -1) {
132                 FRESCAN_ERROR("could not allocate servers\n");
133                 return -1;
134         }
135
136         *id = (frescan_ss_t)pos;
137
138         server = &frescan_data[net].ss_data[*id];
139
140         server->net = net;
141         server->id  = *id;
142         server->committed_params = *params;
143         server->perceived_params = server->committed_params;
144         server->current_priority = params->prio;
145         server->pending_packets  = 0;
146
147         // the first act_time is set to the server creation time
148         clock_gettime (CLOCK_MONOTONIC, &server->act_time);
149
150         // init the list of packets associated to the server
151         INIT_LIST_HEAD(&server->packet_list.fifo_list);
152
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;
158                 repl->amount = 1;
159                 list_add_tail(&repl->repl_list,
160                               &server->replenishments.repl_list);
161         }
162
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;
167
168         ret = timer_create (CLOCK_MONOTONIC, &evp, &server->repl_timer);
169         if (ret != 0) {
170                 FRESCAN_ERROR("could not create timer\n");
171                 return ret;
172         }
173
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);
178
179         return 0;
180 }
181
182 /**
183  * frescan_servers_set_perceived() - update a sporadic server perceived data
184  *
185  * @net: the network instance
186  * @params: the parameters for the server
187  * @id: the identificator for the server
188  *
189  */
190
191 int frescan_servers_set_perceived(frescan_network_t net,
192                                   const frescan_server_params_t *params,
193                                   frescan_ss_t id)
194 {
195         frescan_data[net].ss_data[id].perceived_params = *params;
196         return 0;
197 }
198
199 /**
200  * frescan_servers_commit_perceived() - commit sporadic server perceived data
201  *
202  * Add or remove repl operations according to the budget change
203  *
204  * @net: the network instance
205  * @id: the identificator for the server
206  *
207  */
208
209 int frescan_servers_commit_perceived(frescan_network_t net,
210                                      frescan_ss_t id)
211 {
212         int i, ret;
213         frescan_ss_data_t *server;
214         int budget_variation;
215         frescan_repl_op_t *repl = NULL;
216         struct list_head *pos;
217
218         server = &frescan_data[net].ss_data[id];
219         budget_variation = server->perceived_params.budget -
220                            server->committed_params.budget;
221
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!
227                         repl->amount = 1;
228                         list_add_tail(&repl->repl_list,
229                                       &server->replenishments.repl_list);
230                 }
231         } else {
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,
237                                                   frescan_repl_op_t,
238                                                   repl_list);
239                                 break;
240                         }
241                         list_del(&repl->repl_list);
242
243                         ret = frescan_repl_op_free(repl);
244                         if (ret != 0) return ret;
245                 }
246         }
247
248         server->committed_params = server->perceived_params;
249
250         return 0;
251 }
252
253 /**
254  * frescan_servers_update() - update a sporadic server data
255  *
256  * @net: the network instance
257  * @params: the parameters for the server
258  * @id: the identificator for the server
259  *
260  */
261
262 int frescan_servers_update(frescan_network_t net,
263                            const frescan_server_params_t *params,
264                            frescan_ss_t id)
265 {
266         int ret;
267
268         ret = frescan_servers_set_perceived(net, params, id);
269         if (ret != 0) return ret;
270
271         ret = frescan_servers_commit_perceived(net, id);
272         if (ret != 0) return ret;
273
274         return 0;
275 }
276
277 /**
278  * frescan_servers_destroy() - delete a sporadic server
279  *
280  * @net: the network instance
281  * @id: the identificator for the server
282  *
283  */
284
285 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
286 {
287         int ret;
288         frescan_repl_op_t *repl;
289         frescan_packet_t *packet;
290         frescan_ss_data_t *server;
291
292         server = &frescan_data[net].ss_data[id];
293
294         ret = timer_delete (server->repl_timer);
295         if (ret != 0) {
296                 FRESCAN_ERROR("could not delete timer\n");
297                 return ret;
298         }
299
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,
305                                     fifo_list) {
306                         ret = frescan_packets_free(packet);
307                         if (ret != 0) return ret;
308                 }
309                 INIT_LIST_HEAD(&server->packet_list.fifo_list);
310         }
311
312         // remove the servers replenishment capacity queue
313         list_for_each_entry(repl,
314                             &server->replenishments.repl_list,
315                             repl_list) {
316                 ret = frescan_repl_op_free(repl);
317                 if (ret != 0) return ret;
318         }
319         INIT_LIST_HEAD(&server->replenishments.repl_list);
320
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);
325
326         if (ret != 0) {
327                 FRESCAN_ERROR("could not free server data from pool\n");
328                 return ret;
329         }
330
331         return 0;
332 }
333
334 /**
335  * frescan_servers_get_data() - get a sporadic server data
336  *
337  * @net: the network instance
338  * @params: the parameters of the server
339  * @id: the identificator for the server
340  *
341  */
342
343 int frescan_servers_get_data(frescan_network_t net,
344                              frescan_server_params_t *params,
345                              frescan_ss_t id)
346 {
347         *params = frescan_data[net].ss_data[id].perceived_params;
348         return 0;
349 }
350
351 /**
352  * frescan_servers_get_current_budget() - get the current ss budget
353  *
354  * @net: the network instance
355  * @id: the identificator for the server
356  * @current_budget: the current budget of the server
357  *
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.
361  *
362  * That budget must be modified if the perceived budget has not been
363  * committed yet.
364  *
365  */
366
367 int frescan_servers_get_current_budget(frescan_network_t net,
368                                        frescan_ss_t id,
369                                        frescan_budget_t *current_budget)
370 {
371         struct timespec now;
372         frescan_repl_op_t *repl;
373         frescan_ss_data_t *server;
374         int count;
375
376         server = &frescan_data[net].ss_data[id];
377
378         // first we get the current real budget in the capacity queue
379         clock_gettime (CLOCK_MONOTONIC, &now);
380
381         count = 0;
382         list_for_each_entry(repl,
383                             &server->replenishments.repl_list,
384                             repl_list) {
385                 if (smaller_timespec(now, repl->when)) break;
386                 count = count + 1;
387         }
388
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;
395         }
396
397         *current_budget = (frescan_budget_t)count;
398
399         return 0;
400 }
401
402 /**
403  * frescan_servers_get_highest_prio() - get the server with highest priority
404  *
405  * @net: the network instance
406  * @id: the identificator for the server
407  * @prio: the priority of that server
408  *
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
414  *
415  */
416
417 int frescan_servers_get_highest_prio(frescan_network_t net,
418                                      frescan_ss_t *id,
419                                      frescan_prio_t *prio)
420 {
421         frescan_ss_data_t *server;
422
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;
426                 return 0;
427         }
428
429         *prio = 0;
430         list_for_each_entry(server,
431                             &frescan_data[net].ss_active_head.servers_list,
432                             servers_list) {
433                 if (server->current_priority >= *prio) {
434                         *id = server->id;
435                         *prio = server->current_priority;
436                 }
437         }
438
439         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
440               "highest prio:%u id:%u\n", *prio, *id);
441
442         return 0;
443 }
444
445 /**
446  * frescan_servers_frame_sent() - hook to control the server budget and prio
447  *
448  * @net: the network instance
449  * @id: the identificator for the server
450  *
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.
455  *
456  * NOTE: the replenishment operation is programmed using the corresponding
457  * function at frescan_servers_replenishments module
458  */
459
460 int frescan_servers_frame_sent(frescan_network_t net,
461                                frescan_ss_t id,
462                                frescan_packet_t *packet)
463 {
464         int ret;
465         struct timespec *repl_time;
466         frescan_ss_data_t *server;
467
468         server = &frescan_data[net].ss_data[id];
469
470         if (server->current_priority != FRESCAN_BACKGROUND_PRIO) {
471                 if (smaller_timespec(packet->timestamp, server->act_time)) {
472                         repl_time = &server->act_time;
473                 } else {
474                         repl_time = &packet->timestamp;
475                 }
476
477                 ret = frescan_replenishment_program(net, id, repl_time);
478                 if (ret != 0) return -1;
479         }
480
481         return 0;
482 }