]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/fwp_vres.c
RESCHED flag renamed to CHANGED
[frescor/fwp.git] / fwp / lib / fwp / fwp_vres.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FWP (Frescor WLAN Protocol)                      */
26 /*                                                                        */
27 /* FWP is free software; you can redistribute it and/or modify it         */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FWP is distributed in the hope that it will be         */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FWP; see file        */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FWP header files in a file,          */
39 /* instantiating FWP generics or templates, or linking other files        */
40 /* with FWP objects to produce an executable application, does not        */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46 #include "fwp_utils.h"
47 #include "fwp_vres.h"
48
49 #include "fwp_msgq.h"
50 #include "fwp_endpoint.h"
51 #include "fwp_debug.h"
52
53 #include <string.h>
54 #include <errno.h>
55 #include <stdlib.h>
56
57 static void* fwp_vres_tx_thread(void *_vres);
58
59 typedef enum {
60         FWP_VF_USED             = 0,
61         FWP_VF_BOUND            = 1,
62         FWP_VF_CHANGED          = 2,
63 } fwp_vres_flag_t;
64
65 fwp_vres_params_t fwp_vres_params_default = {
66         .id = 0,        
67         .ac_id = FWP_AC_VO,
68         .budget = 100,
69         .period = {.tv_sec = 2 , .tv_nsec = 111111}
70 };
71
72 /**
73  * Structure of FWP vres.
74  * Internal representation of vres
75  * 
76  */
77 struct fwp_vres{
78         struct fwp_vres_params          params;
79         /* consideration: move tx_queue to endpoint */
80         /**< queue for messages to send */
81         struct fwp_msgq                 tx_queue;   
82         fwp_vres_flag_t                 flags;
83         /**< endpoint bounded to this vres */
84         /*fwp_endpoint_t                *epoint; */
85         pthread_t                       tx_thread; /**< tx_thread id*/
86         pthread_attr_t                  tx_thread_attr;
87         int                             ac_sockd;  /**< ac socket descriptor */
88 };
89
90 typedef
91 struct fwp_vres_table {
92         unsigned int                    max_vres; 
93         fwp_vres_t                      *entry;
94         pthread_mutex_t                 lock;
95 } fwp_vres_table_t;
96
97 /* Global variable - vres table */
98 static fwp_vres_table_t  fwp_vres_table = {
99         .max_vres = 0,
100         .entry = NULL,
101         .lock = PTHREAD_MUTEX_INITIALIZER,
102 };
103
104 /**< mapping priority to ac*/
105 static const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
106 /**< IP tos for AC_VI, AC_VO, AC_BE, AC_BK */ 
107 static const unsigned int ac_to_tos[4] = {224,160,96,64};
108
109 /**
110  * Set access category (AC) to socket
111  *
112  * \param[in] sockd Socket descriptor
113  * \param[in] ac_id AC identifier
114  * 
115  * \return On success returns zero. 
116  * On error, negative error code is returned. 
117  *
118  */
119 static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id) 
120 {
121         unsigned int tos;
122         
123         tos = ac_to_tos[ac_id];
124         if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
125                 FWP_ERROR("setsockopt: %s", strerror(errno));
126                 return (-1);
127         }
128         
129         return 0;
130 }
131
132 static inline void fwp_vres_set_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
133 {
134         vres->flags |= (1 << flag);
135 }
136
137 static inline void fwp_vres_clear_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
138 {
139         vres->flags &= ~(1 << flag);
140 }
141
142 static inline int fwp_vres_get_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
143 {
144         return !!(vres->flags & (1 << flag));
145 }
146
147 static inline void fwp_vres_clearall_flag(fwp_vres_t *vres)
148 {
149         vres->flags = 0;
150 }
151
152 #if 0
153 /* Deprecated */
154 static int fwp_vres_ac_open(fwp_ac_t ac_id) 
155 {
156         int sockd;
157         unsigned int tos;
158         
159         if ((ac_id < 0)||(ac_id >= FWP_AC_NUM)) {
160                 errno = EINVAL;
161                 return -1;
162         }
163
164         if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
165                 FWP_ERROR("Unable to open socket for AC: %s", strerror(errno));
166                 return (-1);
167         }
168
169         tos = ac_to_tos[ac_id];
170         
171         if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
172                 int e = errno;
173                 FWP_ERROR("setsockopt(IP_TOS): %s", strerror(errno));
174                 close(sockfd);
175                 errno = e;
176                 return -1;
177         }
178         
179         return sockd;
180 }
181 #endif
182
183 static inline int _fwp_vres_send(unsigned int ac_sockd, struct fwp_msgb* msgb)
184 {
185         /*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0, 
186                         msgb->peer->addr, msgb->peer->addrlen);*/
187         return _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
188 }
189
190 static inline void fwp_vres_free(fwp_vres_t *vres)
191 {
192         fwp_vres_clearall_flag(vres);
193 }
194
195 static inline int fwp_vres_is_valid(fwp_vres_t *vres)
196 {
197         int id  = vres - fwp_vres_table.entry;
198
199         if ((id < 0) || (id > fwp_vres_table.max_vres - 1) || 
200                 (!fwp_vres_get_flag(vres, FWP_VF_USED))) 
201                 return 0;
202         
203         return 1; 
204 }
205
206 /*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
207 {
208         if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
209                 return -EINVAL;
210         *vres = &fwp_vres_table.entry[vres_id];
211         return 0;
212 }
213 */
214
215 int fwp_vres_table_init(unsigned int max_vres)
216 {
217         unsigned int table_size = max_vres * sizeof(fwp_vres_t);
218
219         fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
220         if (!fwp_vres_table.entry) 
221                 return -1;      /* Errno is set by malloc */
222
223         memset((void*) fwp_vres_table.entry, 0, table_size);
224         fwp_vres_table.max_vres = max_vres;
225         return 0;
226 }
227
228 fwp_vres_id_t fwp_vres_get_id(fwp_vres_d_t vresd)
229 {
230         fwp_vres_t *vres = vresd;
231         
232         return (vres - fwp_vres_table.entry);
233 }
234
235 /**
236  * Allocate vres
237  *
238  * \return On success returns vres descriptor. 
239  */
240 fwp_vres_d_t fwp_vres_alloc()
241 {
242         int i;
243         unsigned int max_vres;
244
245         /* find free vres id */
246         pthread_mutex_lock(&fwp_vres_table.lock);
247         i = 0;
248         max_vres = fwp_vres_table.max_vres;
249         while ((i < max_vres) && 
250                 (fwp_vres_get_flag(&fwp_vres_table.entry[i], FWP_VF_USED))) {
251                 i++;
252         }
253         
254         if (i == max_vres) {
255                 pthread_mutex_unlock(&fwp_vres_table.lock);
256                 errno = ENOBUFS;
257                 return NULL;
258         }
259
260         FWP_DEBUG("Allocated vres id = %d\n",i);
261         fwp_vres_set_flag(&fwp_vres_table.entry[i], FWP_VF_USED);
262         pthread_mutex_unlock(&fwp_vres_table.lock);
263         return (&fwp_vres_table.entry[i]);
264 }
265
266 inline int _fwp_vres_set_params(fwp_vres_t *vres, fwp_vres_params_t *params)
267 {
268         int rv;
269
270         /* copy vres paramters into vres structure */
271         rv = fwp_vres_set_ac(vres->ac_sockd, params->ac_id);
272         if (!rv)
273                 return rv;
274         memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
275         fwp_vres_set_flag(vres, FWP_VF_CHANGED);
276
277         return 0;
278 }
279
280 /**
281  * Set vres params
282  *
283  * \param[in] vresdp Vres descriptor
284  * \param[in] params Vres parameters
285  *
286  * \return On success returns zero. 
287  * On error, negative error code is returned. 
288  *
289  */
290 int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
291 {
292         fwp_vres_t *vres = vresd;
293         
294         if (!fwp_vres_is_valid(vres)) {
295                 errno = EINVAL;
296                 return -1;
297         }
298
299         return _fwp_vres_set_params(vres, params);
300 }
301
302 /**
303  * Creates new vres
304  *
305  * \param[in] params Vres parameters
306  * \param[out] vresdp Pointer to the descriptor of newly created vres
307  *
308  * \return On success returns descriptor of vres. 
309  * On error, negative error code is returned. 
310  *
311  */
312 int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
313 {
314         int rv;
315         fwp_vres_t *vres;
316         
317         vres = fwp_vres_alloc();
318         if (!vres) {
319                 errno = ENOMEM;
320                 return -1;
321         }
322         /* initialize msg queue */
323         fwp_msgq_init(&vres->tx_queue);
324         
325         memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
326         fwp_vres_set_flag(vres, FWP_VF_CHANGED);
327         pthread_attr_init(&vres->tx_thread_attr);
328         if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr, 
329                             fwp_vres_tx_thread, (void*) vres)) != 0){
330                 goto err;
331         }
332
333         *vresdp = vres;
334         return 0;
335 err:    
336         fwp_vres_free(vres);
337         return -1; 
338 }
339
340 /**
341  * Destroys vres
342  *
343  * \param[in] vresd Vres descriptor
344  *
345  * \return On success returns 0. 
346  * On error, negative error code is returned. 
347  *
348  */
349 int fwp_vres_destroy(fwp_vres_d_t vresd)
350 {       
351         fwp_vres_t *vres = vresd;
352
353         if (!fwp_vres_is_valid(vres)) {
354                 errno = EINVAL;
355                 return -1;
356         }
357         
358         pthread_cancel(vres->tx_thread);
359                 
360         FWP_DEBUG("Vres vparam_id=%d destroyed.\n", vres->params.id);   
361         return  0;
362 }
363
364 static void fwp_vres_cleanup(void *_vres)
365 {
366         fwp_vres_t *vres = (fwp_vres_t*)_vres;
367
368         fwp_msgq_dequeue_all(&vres->tx_queue);
369         fwp_vres_free(vres);
370 }
371
372 static inline void 
373 fwp_vres_sched_update(fwp_vres_t *vres, struct timespec *period, 
374                         fwp_budget_t  *budget)
375 {
376         if (fwp_vres_get_flag(vres, FWP_VF_CHANGED)) {  
377                 /*period->tv_nsec = vres->params.period % SEC_TO_USEC;
378                 period->tv_sec = vres->params.period / SEC_TO_USEC;*/
379                 *period = vres->params.period;
380                 *budget = vres->params.budget; 
381                 FWP_DEBUG("Vres tx thread with budget=%ld period_sec=%ld "
382                                 "period_nsec=%ld.\n",vres->params.budget, 
383                                 period->tv_sec, period->tv_nsec);
384                 fwp_vres_clear_flag(vres, FWP_VF_CHANGED);
385         }
386 }
387
388 /**
389  * Thread that does budgeting
390  *
391  */
392 static void* fwp_vres_tx_thread(void *_vres)
393 {
394         struct fwp_vres *vres = (struct fwp_vres*)_vres;
395         struct fwp_msgq *msgq = &vres->tx_queue;
396         struct fwp_msgb *msgb = NULL;
397         unsigned int    ac_id = vres->params.ac_id;
398         fwp_budget_t    budget = vres->params.budget;
399         fwp_budget_t    curr_budget;
400         int             rc;
401         struct timespec start_period, period, interval, current_time;
402
403         fwp_set_rt_prio(90 - ac_id);
404         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
405         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);       
406         pthread_cleanup_push(fwp_vres_cleanup, (void*)vres);
407         
408         /* just for sure */
409         fwp_vres_sched_update(vres, &period, &budget);
410         clock_gettime(CLOCK_MONOTONIC, &start_period);
411         curr_budget = 0;
412
413         while (1) {
414                 /* wait for next period and then send */
415                 /*clock_gettime(CLOCK_MONOTONIC, &current_time);
416                 fwp_timespec_sub(&interval, &end_period, &current_time);
417                 nanosleep(&interval, NULL);*/
418         
419                 /*while((rc = sem_wait(&msgq->empty_lock, &end_period))==-1 
420                         && errno == EINTR) {
421                         continue;
422                 }*/
423                 
424                 msgb = fwp_msgq_dequeue(msgq);
425                 fwp_vres_sched_update(vres, &period, &budget);  
426                 if ((curr_budget + msgb->len) > budget) {
427                         /* need to recharge */
428                         clock_gettime(CLOCK_MONOTONIC, &current_time);
429                         fwp_timespec_sub(&interval, &current_time, &start_period);
430                         fwp_timespec_modulo(&interval, &interval, &period);
431                         fwp_timespec_sub(&interval, &period, &interval);
432                         nanosleep(&interval, NULL);
433                         curr_budget = 0;
434                         clock_gettime(CLOCK_MONOTONIC, &start_period);
435                 }
436
437                 rc = _fwp_vres_send(vres->ac_sockd, msgb);
438                 if (!(rc < 0)) {
439                         FWP_DEBUG("Message sent through AC%d\n",ac_id);
440                         curr_budget+= msgb->len;
441                 } else {
442                         FWP_DEBUG("Message sent error %d\n",rc);
443                 }
444                         
445                 fwp_msgb_free(msgb);
446
447                 /*pthread_testcancel(); */
448                 /*pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);     
449                 
450                 fwp_timespec_add(&end_period, &start_period, &period);
451                 clock_gettime(CLOCK_MONOTONIC, &current_time);
452                 fwp_timespec_sub(&interval, &end_period, &current_time);
453                 nanosleep(&interval, NULL);
454                 */
455         }
456         
457         /* it should normaly never come here */ 
458         pthread_cleanup_pop(0); 
459         fwp_vres_free(vres);
460         
461         return NULL;
462 }
463
464 int fwp_vres_send(fwp_vres_d_t vresd, struct fwp_msgb* msgb)
465 {
466         fwp_vres_t *vres = vresd;
467
468         if (fwp_vres_is_valid(vres)) {
469                 return fwp_msgq_enqueue(&vres->tx_queue, msgb);
470         } else {
471                 errno = EINVAL;
472                 return -1;
473         }
474 }
475
476 /*int fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
477 int fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
478 {
479         fwp_vres_t *vres = vresd;
480         int rv = 0;
481
482         pthread_mutex_lock(&fwp_vres_table.lock);
483         if (!fwp_vres_is_valid(vres)) {
484                 errno = EINVAL;
485                 rv = -1;
486                 goto err;
487         }
488         
489         if (fwp_vres_get_flag(vres, FWP_VF_BOUND)) { /*if already bounded */
490                 errno = EBUSY;
491                 rv = -1;
492                 goto err;
493         }
494
495         vres->ac_sockd = sockd;
496         rv = fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
497         /*if (rv)
498                 goto err;*/
499         fwp_vres_set_flag(vres, FWP_VF_BOUND);
500 err:
501         pthread_mutex_unlock(&fwp_vres_table.lock);
502         return rv;
503 }
504
505 int fwp_vres_unbind(fwp_vres_d_t vresd)
506 {
507         fwp_vres_t *vres = vresd;
508         
509         if (!fwp_vres_is_valid(vresd)) {
510                 errno = EINVAL;
511                 return -1;
512         }
513         fwp_vres_clear_flag(vres, FWP_VF_BOUND);
514         /* TODO: consider what to do with pending messages */
515         /* fwp_vres_free_msgb(vres->tx_queue); */
516         return 0;
517 }