]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src_frescan/frescan_servers.c
Do not enter unnecessary subdirectories
[frescor/frsh-forb.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 - 2009 by the FRESCOR consortium:
23 //
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
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
37 //
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
41 //        made of this code.
42 //
43 //
44 //  based on previous work (FSF) done in the FIRST project
45 //
46 //   Copyright (C) 2005  Mälardalen University, SWEDEN
47 //                       Scuola Superiore S.Anna, ITALY
48 //                       Universidad de Cantabria, SPAIN
49 //                       University of York, UK
50 //
51 // This file is part of FNA (Frescor Network Adaptation)
52 //
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.
63 //
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 // -----------------------------------------------------------------------
72  *
73  */
74
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"
80 #include <signal.h>
81 #include <time.h>
82 #include <misc/linux_list.h>
83 #include "fosa_time_timespec.h" // smaller_timespec
84
85 /**
86  * frescan_servers_init() - initialize server structures
87  *
88  * @net: the network instance
89  */
90
91 int frescan_servers_init(frescan_network_t net)
92 {
93         int ret, i;
94
95         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG, "initializing servers\n");
96
97         ret = freelist_init(&frescan_data[net].ss_id_freelist, FRESCAN_MX_IDS);
98         if (ret != 0) return ret;
99
100         for (i=0; i<FRESCAN_MX_NETWORKS; i++) {
101                 INIT_LIST_HEAD(&frescan_data[net].ss_active_head.servers_list);
102         }
103
104         ret = frescan_replenishments_init(net);
105         if (ret != 0) {
106                 FRESCAN_ERROR("could not initialize the replenishments\n");
107                 return -1;
108         }
109
110         return 0;
111 }
112
113 /**
114  * frescan_servers_create() - create a sporadic server
115  *
116  * @net: the network instance
117  * @params: the parameters for the server
118  * @id: the identificator for the server as a return value
119  *
120  */
121
122 int frescan_servers_create(frescan_network_t net,
123                            const frescan_server_params_t *params,
124                            frescan_ss_t *id)
125 {
126         int i, ret, pos;
127         struct sigevent evp;
128         frescan_ss_data_t *server;
129         frescan_repl_op_t *repl;
130
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);
134
135         if (pos == -1) {
136                 FRESCAN_ERROR("could not allocate servers\n");
137                 return -1;
138         }
139
140         *id = (frescan_ss_t)pos;
141
142         server = &frescan_data[net].ss_data[*id];
143
144         server->net = net;
145         server->id  = *id;
146         server->committed_params = *params;
147         server->perceived_params = server->committed_params;
148         server->current_priority = params->prio;
149         server->pending_packets  = 0;
150
151         // the first act_time is set to the server creation time
152         clock_gettime (CLOCK_MONOTONIC, &server->act_time);
153
154         // init the list of packets associated to the server
155         INIT_LIST_HEAD(&server->packet_list.fifo_list);
156
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;
162                 repl->amount = 1;
163                 list_add_tail(&repl->repl_list,
164                               &server->replenishments.repl_list);
165         }
166
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;
171
172         ret = timer_create (CLOCK_MONOTONIC, &evp, &server->repl_timer);
173         if (ret != 0) {
174                 FRESCAN_ERROR("could not create timer\n");
175                 return ret;
176         }
177
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);
182
183         return 0;
184 }
185
186 /**
187  * frescan_servers_set_perceived() - update a sporadic server perceived data
188  *
189  * @net: the network instance
190  * @params: the parameters for the server
191  * @id: the identificator for the server
192  *
193  */
194
195 int frescan_servers_set_perceived(frescan_network_t net,
196                                   const frescan_server_params_t *params,
197                                   frescan_ss_t id)
198 {
199         frescan_data[net].ss_data[id].perceived_params = *params;
200         return 0;
201 }
202
203 /**
204  * frescan_servers_commit_perceived() - commit sporadic server perceived data
205  *
206  * Add or remove repl operations according to the budget change
207  *
208  * @net: the network instance
209  * @id: the identificator for the server
210  *
211  */
212
213 int frescan_servers_commit_perceived(frescan_network_t net,
214                                      frescan_ss_t id)
215 {
216         int i, ret;
217         frescan_ss_data_t *server;
218         int budget_variation;
219         frescan_repl_op_t *repl = NULL;
220         struct list_head *pos;
221
222         server = &frescan_data[net].ss_data[id];
223         budget_variation = server->perceived_params.budget -
224                            server->committed_params.budget;
225
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!
231                         repl->amount = 1;
232                         list_add_tail(&repl->repl_list,
233                                       &server->replenishments.repl_list);
234                 }
235         } else {
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,
241                                                   frescan_repl_op_t,
242                                                   repl_list);
243                                 break;
244                         }
245                         list_del(&repl->repl_list);
246
247                         ret = frescan_repl_op_free(repl);
248                         if (ret != 0) return ret;
249                 }
250         }
251
252         server->committed_params = server->perceived_params;
253
254         return 0;
255 }
256
257 /**
258  * frescan_servers_update() - update a sporadic server data
259  *
260  * @net: the network instance
261  * @params: the parameters for the server
262  * @id: the identificator for the server
263  *
264  */
265
266 int frescan_servers_update(frescan_network_t net,
267                            const frescan_server_params_t *params,
268                            frescan_ss_t id)
269 {
270         int ret;
271
272         ret = frescan_servers_set_perceived(net, params, id);
273         if (ret != 0) return ret;
274
275         ret = frescan_servers_commit_perceived(net, id);
276         if (ret != 0) return ret;
277
278         return 0;
279 }
280
281 /**
282  * frescan_servers_destroy() - delete a sporadic server
283  *
284  * @net: the network instance
285  * @id: the identificator for the server
286  *
287  */
288
289 int frescan_servers_destroy(frescan_network_t net, frescan_ss_t id)
290 {
291         int ret;
292         frescan_repl_op_t *repl;
293         frescan_packet_t *packet;
294         frescan_ss_data_t *server;
295
296         server = &frescan_data[net].ss_data[id];
297
298         ret = timer_delete (server->repl_timer);
299         if (ret != 0) {
300                 FRESCAN_ERROR("could not delete timer\n");
301                 return ret;
302         }
303
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,
309                                     fifo_list) {
310                         ret = frescan_packets_free(packet);
311                         if (ret != 0) return ret;
312                 }
313                 INIT_LIST_HEAD(&server->packet_list.fifo_list);
314         }
315
316         // remove the servers replenishment capacity queue
317         list_for_each_entry(repl,
318                             &server->replenishments.repl_list,
319                             repl_list) {
320                 ret = frescan_repl_op_free(repl);
321                 if (ret != 0) return ret;
322         }
323         INIT_LIST_HEAD(&server->replenishments.repl_list);
324
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);
329
330         if (ret != 0) {
331                 FRESCAN_ERROR("could not free server data from pool\n");
332                 return ret;
333         }
334
335         return 0;
336 }
337
338 /**
339  * frescan_servers_get_data() - get a sporadic server data
340  *
341  * @net: the network instance
342  * @params: the parameters of the server
343  * @id: the identificator for the server
344  *
345  */
346
347 int frescan_servers_get_data(frescan_network_t net,
348                              frescan_server_params_t *params,
349                              frescan_ss_t id)
350 {
351         *params = frescan_data[net].ss_data[id].perceived_params;
352         return 0;
353 }
354
355 /**
356  * frescan_servers_get_current_budget() - get the current ss budget
357  *
358  * @net: the network instance
359  * @id: the identificator for the server
360  * @current_budget: the current budget of the server
361  *
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.
365  *
366  * That budget must be modified if the perceived budget has not been
367  * committed yet.
368  *
369  */
370
371 int frescan_servers_get_current_budget(frescan_network_t net,
372                                        frescan_ss_t id,
373                                        frescan_budget_t *current_budget)
374 {
375         struct timespec now;
376         frescan_repl_op_t *repl;
377         frescan_ss_data_t *server;
378         int count;
379
380         server = &frescan_data[net].ss_data[id];
381
382         // first we get the current real budget in the capacity queue
383         clock_gettime (CLOCK_MONOTONIC, &now);
384
385         count = 0;
386         list_for_each_entry(repl,
387                             &server->replenishments.repl_list,
388                             repl_list) {
389                 if (smaller_timespec(now, repl->when)) break;
390                 count = count + 1;
391         }
392
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;
399         }
400
401         *current_budget = (frescan_budget_t)count;
402
403         return 0;
404 }
405
406 /**
407  * frescan_servers_get_highest_prio() - get the server with highest priority
408  *
409  * @net: the network instance
410  * @id: the identificator for the server
411  * @prio: the priority of that server
412  *
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
418  *
419  */
420
421 int frescan_servers_get_highest_prio(frescan_network_t net,
422                                      frescan_ss_t *id,
423                                      frescan_prio_t *prio)
424 {
425         frescan_ss_data_t *server;
426
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;
430                 return 0;
431         }
432
433         *prio = 0;
434         list_for_each_entry(server,
435                             &frescan_data[net].ss_active_head.servers_list,
436                             servers_list) {
437                 if (server->current_priority >= *prio) {
438                         *id = server->id;
439                         *prio = server->current_priority;
440                 }
441         }
442
443         DEBUG(FRESCAN_SERVERS_ENABLE_DEBUG,
444               "highest prio:%u id:%u\n", *prio, *id);
445
446         return 0;
447 }
448
449 /**
450  * frescan_servers_frame_sent() - hook to control the server budget and prio
451  *
452  * @net: the network instance
453  * @id: the identificator for the server
454  *
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.
459  *
460  * NOTE: the replenishment operation is programmed using the corresponding
461  * function at frescan_servers_replenishments module
462  */
463
464 int frescan_servers_frame_sent(frescan_network_t net,
465                                frescan_ss_t id,
466                                frescan_packet_t *packet)
467 {
468         int ret;
469         struct timespec *repl_time;
470         frescan_ss_data_t *server;
471
472         server = &frescan_data[net].ss_data[id];
473
474         if (server->current_priority != FRESCAN_BACKGROUND_PRIO) {
475                 if (smaller_timespec(packet->timestamp, server->act_time)) {
476                         repl_time = &server->act_time;
477                 } else {
478                         repl_time = &packet->timestamp;
479                 }
480
481                 ret = frescan_replenishment_program(net, id, repl_time);
482                 if (ret != 0) return -1;
483         }
484
485         return 0;
486 }