1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2009 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 /**************************************************************************/
47 #include "fwp_utils.h"
51 #include "fwp_endpoint.h"
52 #include "fwp_debug.h"
58 static void* fwp_vres_tx_thread(void *_vres);
67 fwp_vres_params_t fwp_vres_params_default = {
70 .period = {.tv_sec = 2 , .tv_nsec = 111111},
75 * Structure of FWP vres.
76 * Internal representation of vres
80 struct fwp_vres_params params;
81 fwp_vres_flag_t flags;
82 pthread_mutex_t mutex;
83 pthread_cond_t cond; /**< Signalizes budget replenishment */
84 fwp_budget_t budget; /**< Current remaining budget */
85 fwp_period_t period; /**< Period for this "activation" */
86 struct timespec replenish_at; /**< Time of next replenishment */
88 /**< endpoint bounded to this vres */
89 struct fwp_endpoint *epoint;
90 pthread_t tx_thread; /**< tx_thread id*/
91 pthread_attr_t tx_thread_attr;
92 /** Copy of bound enpoint's socket - used for future changes
93 * of vres parameters. */
95 /** Queue for messages to send */
96 struct fwp_msgq msg_queue;
100 struct fwp_vres_table {
101 unsigned int max_vres;
103 pthread_mutex_t lock;
106 /* Global variable - vres table */
107 static fwp_vres_table_t fwp_vres_table = {
110 .lock = PTHREAD_MUTEX_INITIALIZER,
113 /**< mapping priority to ac*/
114 static const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
115 /**< IP tos for AC_VI, AC_VO, AC_BE, AC_BK */
116 static const unsigned int ac_to_tos[4] = {224,160,96,64};
119 * Set access category (AC) to socket
121 * \param[in] sockd Socket descriptor
122 * \param[in] ac_id AC identifier
124 * \return On success returns zero.
125 * On error, negative error code is returned.
128 static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
132 tos = ac_to_tos[ac_id];
133 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
134 FWP_ERROR("setsockopt: %s", strerror(errno));
141 static inline void set_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
143 vres->flags |= (1 << flag);
146 static inline void clear_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
148 vres->flags &= ~(1 << flag);
151 static inline int get_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
153 return !!(vres->flags & (1 << flag));
156 static inline void clear_all_flags(fwp_vres_t *vres)
161 static inline void fwp_vres_free(fwp_vres_t *vres)
163 /* Clear USED flag */
164 clear_all_flags(vres);
167 static inline int fwp_vres_is_valid(fwp_vres_t *vres)
169 int id = vres - fwp_vres_table.entry;
171 if ((id < 0) || (id > fwp_vres_table.max_vres - 1) ||
172 (!get_flag(vres, USED)))
178 /*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
180 if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
182 *vres = &fwp_vres_table.entry[vres_id];
187 int fwp_vres_table_init(unsigned int max_vres)
189 unsigned int table_size = max_vres * sizeof(fwp_vres_t);
191 fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
192 if (!fwp_vres_table.entry)
193 return -1; /* Errno is set by malloc */
195 memset((void*) fwp_vres_table.entry, 0, table_size);
196 fwp_vres_table.max_vres = max_vres;
203 * \return On success returns vres descriptor.
205 fwp_vres_t *fwp_vres_alloc()
208 unsigned int max_vres;
210 /* find free vres id */
211 pthread_mutex_lock(&fwp_vres_table.lock);
213 max_vres = fwp_vres_table.max_vres;
214 while ((i < max_vres) &&
215 (get_flag(&fwp_vres_table.entry[i], USED))) {
220 pthread_mutex_unlock(&fwp_vres_table.lock);
225 FWP_DEBUG("Allocated vres id = %d\n",i);
226 set_flag(&fwp_vres_table.entry[i], USED);
227 pthread_mutex_unlock(&fwp_vres_table.lock);
228 return (&fwp_vres_table.entry[i]);
231 static int apply_params(fwp_vres_t *vres)
234 vres->period = vres->params.period;
235 vres->budget = vres->params.budget;
236 set_flag(vres, UNTOUCHED);
237 if (get_flag(vres, CHANGED)) {
238 clear_flag(vres, CHANGED);
239 rv = fwp_vres_set_ac(vres->ac_sockd, vres->params.ac_id);
247 * \param[in] vresp Vres descriptor
248 * \param[in] params Vres parameters
250 * \return On success returns zero.
251 * On error, negative error code is returned.
254 int fwp_vres_set_params(fwp_vres_t *vres, fwp_vres_params_t *params)
258 if (!fwp_vres_is_valid(vres)) {
263 pthread_mutex_lock(&vres->mutex);
266 params->src.s_addr != vres->params.src.s_addr) {
271 vres->params = *params;
273 set_flag(vres, CHANGED);
274 if (get_flag(vres, UNTOUCHED))
275 rv = apply_params(vres);
278 pthread_mutex_unlock(&vres->mutex);
285 * \param[in] params Vres parameters
286 * \param[out] vresp Pointer to the descriptor of newly created vres
288 * \return On success returns descriptor of vres.
289 * On error, negative error code is returned.
292 int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_t **vresp)
297 vres = fwp_vres_alloc();
303 pthread_mutexattr_t ma;
304 rv = pthread_mutexattr_init(&ma);
305 rv = pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT);
307 pthread_mutex_init(&vres->mutex, &ma);
308 pthread_cond_init(&vres->cond, NULL);
310 vres->params = *params;
312 fwp_msgq_init(&vres->msg_queue);
314 pthread_attr_init(&vres->tx_thread_attr);
315 if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr,
316 fwp_vres_tx_thread, (void*) vres)) != 0){
324 fwp_msgq_dequeue_all(&vres->msg_queue);
332 * \param[in] vres Vres descriptor
334 * \return On success returns 0.
335 * On error, negative error code is returned.
338 int fwp_vres_destroy(fwp_vres_t *vres)
340 if (!fwp_vres_is_valid(vres)) {
345 pthread_cancel(vres->tx_thread);
346 pthread_cond_destroy(&vres->cond);
347 pthread_mutex_destroy(&vres->mutex);
349 fwp_msgq_dequeue_all(&vres->msg_queue);
352 FWP_DEBUG("Vres destroyed.\n");
356 static void do_consume_budget(struct fwp_vres *vres, size_t size)
358 if (get_flag(vres, UNTOUCHED)) {
359 /* Setup next replenish time */
361 clear_flag(vres, UNTOUCHED);
362 if (get_flag(vres, QUEUED))
363 now = vres->replenish_at;
365 clock_gettime(CLOCK_MONOTONIC, &now);
366 fwp_timespec_add(&vres->replenish_at, &now, &vres->period);
367 sem_post(&vres->consumed);
369 vres->budget -= size;
372 int __consume_budget(struct fwp_vres *vres, size_t size, bool can_block)
375 if (vres->params.budget < size) {
379 while (can_block && vres->budget < size) {
380 ret = pthread_cond_wait(&vres->cond, &vres->mutex);
381 /* The budget might have been changed while we were
382 * waiting, so check it again. */
383 if (vres->params.budget < size) {
389 if (vres->budget >= size) {
390 do_consume_budget(vres, size);
393 set_flag(vres, QUEUED);
401 * Tries to consume (a part of) budget
403 * @param vres VRES whose budget is conumed.
404 * @param size How much to consume (in bytes).
405 * @param can_block True, indicates that the function can block to
406 * wait for budget replenishment. False causes no blocking and if 1 is
407 * returned, the calles must call fwp_vres_enqueue() to enqueue the
408 * packet to be sent later after replenishing.
410 * @return Zero if budget was consumed, 1 if there is not enough
411 * budget available and blocking was not allowed, -1 in case of error.
413 int fwp_vres_consume_budget(struct fwp_vres *vres, size_t size, bool can_block)
416 pthread_mutex_lock(&vres->mutex);
417 ret = __consume_budget(vres, size, can_block);
418 pthread_mutex_unlock(&vres->mutex);
422 int fwp_vres_enqueue(struct fwp_vres *vres, struct fwp_endpoint *ep,
423 const void *msg, size_t size)
425 struct fwp_msgb *msgb;
428 if (!(msgb = fwp_msgb_alloc(size)))
430 memcpy(msgb->data, msg, size);
431 fwp_msgb_put(msgb, size);
432 ret = fwp_msgq_enqueue(&vres->msg_queue, msgb);
440 static void wait_for_replenish(struct fwp_vres *vres)
442 sem_wait(&vres->consumed);
443 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
444 &vres->replenish_at, NULL);
447 static void send_queue(struct fwp_vres *vres)
449 struct fwp_msgb *msgb;
451 msgb = fwp_msgq_dequeue(&vres->msg_queue);
452 can_send = (0 == __consume_budget(vres, msgb->len, false));
455 FWP_ERROR("Cannot send queued packet (budget decreased?)\n");
458 /* If we cannot send the whole queue, the flag will be set
459 * later by __consume_budget(). */
460 clear_flag(vres, QUEUED);
463 fwp_endpoint_do_send(vres->epoint, msgb->data, msgb->len);
465 msgb = fwp_msgq_peek(&vres->msg_queue);
467 can_send = (0 == __consume_budget(vres, msgb->len, false));
469 msgb = fwp_msgq_dequeue(&vres->msg_queue);
478 static void replenish(struct fwp_vres *vres)
480 pthread_mutex_lock(&vres->mutex);
482 if (get_flag(vres, QUEUED))
484 pthread_cond_broadcast(&vres->cond);
485 pthread_mutex_unlock(&vres->mutex);
489 * Thread that does budgeting
492 static void* fwp_vres_tx_thread(void *_vres)
494 struct fwp_vres *vres = (struct fwp_vres*)_vres;
495 unsigned int ac_id = vres->params.ac_id;
497 fwp_set_rt_prio(90 - ac_id);
498 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
499 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
502 wait_for_replenish(vres);
503 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
505 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
511 /*int fwp_vres_bind(fwp_vres_t *vres, struct fwp_endpoint *epoint)*/
512 int fwp_vres_bind(fwp_vres_t *vres, struct fwp_endpoint *ep, int sockd, struct in_addr *src)
516 if (!fwp_vres_is_valid(vres)) {
522 if (vres->epoint) { /*if already bounded */
528 vres->ac_sockd = sockd;
529 *src = vres->params.src;
530 rv = fwp_vres_set_ac(vres->ac_sockd, vres->params.ac_id);
538 int fwp_vres_unbind(fwp_vres_t *vres)
540 if (!fwp_vres_is_valid(vres)) {
544 pthread_mutex_lock(&vres->mutex);
546 pthread_mutex_unlock(&vres->mutex);
547 /* TODO: consider what to do with pending messages */
548 fwp_msgq_dequeue_all(&vres->msg_queue);