1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FWP (Frescor WLAN Protocol) */
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. */
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"
50 #include "fwp_endpoint.h"
54 static void* fwp_vres_tx_thread(void *_vres);
62 fwp_vres_params_t fwp_vres_params_default = {
66 .period = {.tv_sec = 2 , .tv_nsec = 111111}
70 * Structure of FWP vres.
71 * Internal representation of vres
75 struct fwp_vres_params params;
76 /* consideration: move tx_queue to endpoint */
77 /**< queue for messages to send */
78 struct fwp_msgq tx_queue;
79 fwp_vres_flag_t flags;
80 /**< endpoint bounded to this vres */
81 /*fwp_endpoint_t *epoint; */
82 pthread_t tx_thread; /**< tx_thread id*/
83 pthread_attr_t tx_thread_attr;
84 int ac_sockd; /**< ac socket descriptor */
88 struct fwp_vres_table {
89 unsigned int max_vres;
94 /* Global variable - vres table */
95 static fwp_vres_table_t fwp_vres_table = {
98 .lock = PTHREAD_MUTEX_INITIALIZER,
101 /**< mapping priority to ac*/
102 static const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
103 /**< IP tos for AC_VI, AC_VO, AC_BE, AC_BK */
104 static const unsigned int ac_to_tos[4] = {224,160,96,64};
107 * Set access category (AC) to socket
109 * \param[in] sockd Socket descriptor
110 * \param[in] ac_id AC identifier
112 * \return On success returns zero.
113 * On error, negative error code is returned.
116 static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
120 tos = ac_to_tos[ac_id];
121 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
122 FWP_ERROR("setsockopt: %s", strerror(errno));
129 static inline void fwp_vres_set_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
131 vres->flags |= (1 << flag);
134 static inline void fwp_vres_clear_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
136 vres->flags &= ~(1 << flag);
139 static inline int fwp_vres_get_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
141 return !!(vres->flags & (1 << flag));
144 static inline void fwp_vres_clearall_flag(fwp_vres_t *vres)
151 static int fwp_vres_ac_open(fwp_ac_t ac_id)
156 if ((ac_id < 0)||(ac_id >= FWP_AC_NUM)) {
161 if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
162 FWP_ERROR("Unable to open socket for AC: %s", strerror(errno));
166 tos = ac_to_tos[ac_id];
168 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
170 FWP_ERROR("setsockopt(IP_TOS): %s", strerror(errno));
180 static inline int _fwp_vres_send(unsigned int ac_sockd, struct fwp_msgb* msgb)
182 /*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0,
183 msgb->peer->addr, msgb->peer->addrlen);*/
184 return _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
187 static inline void fwp_vres_free(fwp_vres_t *vres)
189 fwp_vres_clearall_flag(vres);
192 static inline int fwp_vres_is_valid(fwp_vres_t *vres)
194 int id = vres - fwp_vres_table.entry;
196 if ((id < 0) || (id > fwp_vres_table.max_vres - 1) ||
197 (!fwp_vres_get_flag(vres, FWP_VF_USED)))
203 /*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
205 if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
207 *vres = &fwp_vres_table.entry[vres_id];
212 int fwp_vres_table_init(unsigned int max_vres)
214 unsigned int table_size = max_vres * sizeof(fwp_vres_t);
216 fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
217 if (!fwp_vres_table.entry)
218 return -1; /* Errno is set by malloc */
220 memset((void*) fwp_vres_table.entry, 0, table_size);
221 fwp_vres_table.max_vres = max_vres;
225 fwp_vres_id_t fwp_vres_get_id(fwp_vres_d_t vresd)
227 fwp_vres_t *vres = vresd;
229 return (vres - fwp_vres_table.entry);
235 * \return On success returns vres descriptor.
237 fwp_vres_d_t fwp_vres_alloc()
240 unsigned int max_vres;
242 /* find free vres id */
243 pthread_mutex_lock(&fwp_vres_table.lock);
245 max_vres = fwp_vres_table.max_vres;
246 while ((i < max_vres) &&
247 (fwp_vres_get_flag(&fwp_vres_table.entry[i], FWP_VF_USED))) {
252 pthread_mutex_unlock(&fwp_vres_table.lock);
257 FWP_DEBUG("Allocated vres id = %d\n",i);
258 fwp_vres_set_flag(&fwp_vres_table.entry[i], FWP_VF_USED);
259 pthread_mutex_unlock(&fwp_vres_table.lock);
260 return (&fwp_vres_table.entry[i]);
263 inline int _fwp_vres_set_params(fwp_vres_t *vres, fwp_vres_params_t *params)
267 /* copy vres paramters into vres structure */
268 rv = fwp_vres_set_ac(vres->ac_sockd, params->ac_id);
271 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
272 fwp_vres_set_flag(vres, FWP_VF_RESCHED);
280 * \param[in] vresdp Vres descriptor
281 * \param[in] params Vres parameters
283 * \return On success returns zero.
284 * On error, negative error code is returned.
287 int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
289 fwp_vres_t *vres = vresd;
291 if (!fwp_vres_is_valid(vres)) {
296 return _fwp_vres_set_params(vres, params);
302 * \param[in] params Vres parameters
303 * \param[out] vresdp Pointer to the descriptor of newly created vres
305 * \return On success returns descriptor of vres.
306 * On error, negative error code is returned.
309 int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
314 vres = fwp_vres_alloc();
319 /* initialize msg queue */
320 fwp_msgq_init(&vres->tx_queue);
322 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
323 fwp_vres_set_flag(vres, FWP_VF_RESCHED);
324 pthread_attr_init(&vres->tx_thread_attr);
325 if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr,
326 fwp_vres_tx_thread, (void*) vres)) != 0){
340 * \param[in] vresd Vres descriptor
342 * \return On success returns 0.
343 * On error, negative error code is returned.
346 int fwp_vres_destroy(fwp_vres_d_t vresd)
348 fwp_vres_t *vres = vresd;
350 if (!fwp_vres_is_valid(vres)) {
355 pthread_cancel(vres->tx_thread);
357 FWP_DEBUG("Vres vparam_id=%d destroyed.\n", vres->params.id);
361 static void fwp_vres_cleanup(void *_vres)
363 fwp_vres_t *vres = (fwp_vres_t*)_vres;
365 fwp_msgq_dequeue_all(&vres->tx_queue);
370 fwp_vres_sched_update(fwp_vres_t *vres, struct timespec *period,
371 fwp_budget_t *budget)
373 if (fwp_vres_get_flag(vres, FWP_VF_RESCHED)) {
374 /*period->tv_nsec = vres->params.period % SEC_TO_USEC;
375 period->tv_sec = vres->params.period / SEC_TO_USEC;*/
376 *period = vres->params.period;
377 *budget = vres->params.budget;
378 FWP_DEBUG("Vres tx thread with budget=%ld period_sec=%ld "
379 "period_nsec=%ld.\n",vres->params.budget,
380 period->tv_sec, period->tv_nsec);
381 fwp_vres_clear_flag(vres, FWP_VF_RESCHED);
386 * Thread that does budgeting
389 static void* fwp_vres_tx_thread(void *_vres)
391 struct fwp_vres *vres = (struct fwp_vres*)_vres;
392 struct fwp_msgq *msgq = &vres->tx_queue;
393 struct fwp_msgb *msgb = NULL;
394 unsigned int ac_id = vres->params.ac_id;
395 fwp_budget_t budget = vres->params.budget;
396 fwp_budget_t curr_budget;
398 struct timespec start_period, period, interval, current_time;
400 fwp_set_rt_prio(90 - ac_id);
401 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
402 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
403 pthread_cleanup_push(fwp_vres_cleanup, (void*)vres);
406 fwp_vres_sched_update(vres, &period, &budget);
407 clock_gettime(CLOCK_MONOTONIC, &start_period);
411 /* wait for next period and then send */
412 /*clock_gettime(CLOCK_MONOTONIC, ¤t_time);
413 fwp_timespec_sub(&interval, &end_period, ¤t_time);
414 nanosleep(&interval, NULL);*/
416 /*while((rc = sem_wait(&msgq->empty_lock, &end_period))==-1
421 msgb = fwp_msgq_dequeue(msgq);
422 fwp_vres_sched_update(vres, &period, &budget);
423 if ((curr_budget + msgb->len) > budget) {
424 /* need to recharge */
425 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
426 fwp_timespec_sub(&interval, ¤t_time, &start_period);
427 fwp_timespec_modulo(&interval, &interval, &period);
428 fwp_timespec_sub(&interval, &period, &interval);
429 nanosleep(&interval, NULL);
431 clock_gettime(CLOCK_MONOTONIC, &start_period);
434 rc = _fwp_vres_send(vres->ac_sockd, msgb);
436 FWP_DEBUG("Message sent through AC%d\n",ac_id);
437 curr_budget+= msgb->len;
439 FWP_DEBUG("Message sent error %d\n",rc);
444 /*pthread_testcancel(); */
445 /*pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
447 fwp_timespec_add(&end_period, &start_period, &period);
448 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
449 fwp_timespec_sub(&interval, &end_period, ¤t_time);
450 nanosleep(&interval, NULL);
454 /* it should normaly never come here */
455 pthread_cleanup_pop(0);
461 int fwp_vres_send(fwp_vres_d_t vresd, struct fwp_msgb* msgb)
463 fwp_vres_t *vres = vresd;
465 if (fwp_vres_is_valid(vres)) {
466 return fwp_msgq_enqueue(&vres->tx_queue, msgb);
468 errno = EPERM; /* TODO: Use more appropriate error than EPERM. */
473 /*int fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
474 int fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
476 fwp_vres_t *vres = vresd;
479 pthread_mutex_lock(&fwp_vres_table.lock);
480 if (!fwp_vres_is_valid(vres)) {
486 if (fwp_vres_get_flag(vres, FWP_VF_BOUND)) { /*if already bounded */
492 vres->ac_sockd = sockd;
493 rv = fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
496 fwp_vres_set_flag(vres, FWP_VF_BOUND);
498 pthread_mutex_unlock(&fwp_vres_table.lock);
502 int fwp_vres_unbind(fwp_vres_d_t vresd)
504 fwp_vres_t *vres = vresd;
506 if (!fwp_vres_is_valid(vresd)) {
510 fwp_vres_clear_flag(vres, FWP_VF_BOUND);
511 /* TODO: consider what to do with pending messages */
512 /* fwp_vres_free_msgb(vres->tx_queue); */