5 #include "fwp_endpoint.h"
9 static void* fwp_vres_tx_thread(void *_vres);
17 fwp_vres_params_t fwp_vres_params_default = {
21 .period = {.tv_sec = 2 , .tv_nsec = 111111}
25 * Structure of FWP vres.
26 * Internal representation of vres
30 struct fwp_vres_params params;
31 /* consideration: move tx_queue to endpoint */
32 /**< queue for messages to send */
33 struct fwp_msgq tx_queue;
34 fwp_vres_flag_t flags;
35 /**< endpoint bounded to this vres */
36 /*fwp_endpoint_t *epoint; */
37 pthread_t tx_thread; /**< tx_thread id*/
38 pthread_attr_t tx_thread_attr;
39 int ac_sockd; /**< ac socket descriptor */
43 struct fwp_vres_table {
44 unsigned int max_vres;
49 /* Global variable - vres table */
50 static fwp_vres_table_t fwp_vres_table = {
53 .lock = PTHREAD_MUTEX_INITIALIZER,
56 /**< mapping priority to ac*/
57 static const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
58 /**< IP tos for AC_VI, AC_VO, AC_BE, AC_BK */
59 static const unsigned int ac_to_tos[4] = {224,160,96,64};
62 * Set access category (AC) to socket
64 * \param[in] sockd Socket descriptor
65 * \param[in] ac_id AC identifier
67 * \return On success returns zero.
68 * On error, negative error code is returned.
71 static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
75 tos = ac_to_tos[ac_id];
76 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
77 FWP_ERROR("setsockopt: %s", strerror(errno));
84 static inline void fwp_vres_set_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
86 vres->flags |= (1 << flag);
89 static inline void fwp_vres_clear_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
91 vres->flags &= ~(1 << flag);
94 static inline int fwp_vres_get_flag(fwp_vres_t *vres, fwp_vres_flag_t flag)
96 return !!(vres->flags & (1 << flag));
99 static inline void fwp_vres_clearall_flag(fwp_vres_t *vres)
106 static int fwp_vres_ac_open(fwp_ac_t ac_id)
111 if ((ac_id < 0)||(ac_id >= FWP_AC_NUM)) {
116 if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
117 FWP_ERROR("Unable to open socket for AC: %s", strerror(errno));
121 tos = ac_to_tos[ac_id];
123 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
125 FWP_ERROR("setsockopt(IP_TOS): %s", strerror(errno));
135 static inline int _fwp_vres_send(unsigned int ac_sockd, struct fwp_msgb* msgb)
137 /*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0,
138 msgb->peer->addr, msgb->peer->addrlen);*/
139 return _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
142 static inline void fwp_vres_free(fwp_vres_t *vres)
144 fwp_vres_clearall_flag(vres);
147 static inline int fwp_vres_is_valid(fwp_vres_t *vres)
149 int id = vres - fwp_vres_table.entry;
151 if ((id < 0) || (id > fwp_vres_table.max_vres - 1) ||
152 (!fwp_vres_get_flag(vres, FWP_VF_USED)))
158 /*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
160 if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
162 *vres = &fwp_vres_table.entry[vres_id];
167 int fwp_vres_table_init(unsigned int max_vres)
169 unsigned int table_size = max_vres * sizeof(fwp_vres_t);
171 fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
172 if (!fwp_vres_table.entry)
173 return -1; /* Errno is set by malloc */
175 memset((void*) fwp_vres_table.entry, 0, table_size);
176 fwp_vres_table.max_vres = max_vres;
180 fwp_vres_id_t fwp_vres_get_id(fwp_vres_d_t vresd)
182 fwp_vres_t *vres = vresd;
184 return (vres - fwp_vres_table.entry);
190 * \return On success returns vres descriptor.
192 fwp_vres_d_t fwp_vres_alloc()
195 unsigned int max_vres;
197 /* find free vres id */
198 pthread_mutex_lock(&fwp_vres_table.lock);
200 max_vres = fwp_vres_table.max_vres;
201 while ((i < max_vres) &&
202 (fwp_vres_get_flag(&fwp_vres_table.entry[i], FWP_VF_USED))) {
207 pthread_mutex_unlock(&fwp_vres_table.lock);
212 FWP_DEBUG("Allocated vres id = %d\n",i);
213 fwp_vres_set_flag(&fwp_vres_table.entry[i], FWP_VF_USED);
214 pthread_mutex_unlock(&fwp_vres_table.lock);
215 return (&fwp_vres_table.entry[i]);
218 inline int _fwp_vres_set_params(fwp_vres_t *vres, fwp_vres_params_t *params)
222 /* copy vres paramters into vres structure */
223 rv = fwp_vres_set_ac(vres->ac_sockd, params->ac_id);
226 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
227 fwp_vres_set_flag(vres, FWP_VF_RESCHED);
235 * \param[in] vresdp Vres descriptor
236 * \param[in] params Vres parameters
238 * \return On success returns zero.
239 * On error, negative error code is returned.
242 int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
244 fwp_vres_t *vres = vresd;
246 if (!fwp_vres_is_valid(vres)) {
251 return _fwp_vres_set_params(vres, params);
257 * \param[in] params Vres parameters
258 * \param[out] vresdp Pointer to the descriptor of newly created vres
260 * \return On success returns descriptor of vres.
261 * On error, negative error code is returned.
264 int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
269 vres = fwp_vres_alloc();
274 /* initialize msg queue */
275 fwp_msgq_init(&vres->tx_queue);
277 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
278 fwp_vres_set_flag(vres, FWP_VF_RESCHED);
279 pthread_attr_init(&vres->tx_thread_attr);
280 if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr,
281 fwp_vres_tx_thread, (void*) vres)) != 0){
295 * \param[in] vresd Vres descriptor
297 * \return On success returns 0.
298 * On error, negative error code is returned.
301 int fwp_vres_destroy(fwp_vres_d_t vresd)
303 fwp_vres_t *vres = vresd;
305 if (!fwp_vres_is_valid(vres)) {
310 pthread_cancel(vres->tx_thread);
312 FWP_DEBUG("Vres vparam_id=%d destroyed.\n", vres->params.id);
316 static void fwp_vres_cleanup(void *_vres)
318 fwp_vres_t *vres = (fwp_vres_t*)_vres;
320 fwp_msgq_dequeue_all(&vres->tx_queue);
325 fwp_vres_sched_update(fwp_vres_t *vres, struct timespec *period,
326 fwp_budget_t *budget)
328 if (fwp_vres_get_flag(vres, FWP_VF_RESCHED)) {
329 /*period->tv_nsec = vres->params.period % SEC_TO_USEC;
330 period->tv_sec = vres->params.period / SEC_TO_USEC;*/
331 *period = vres->params.period;
332 *budget = vres->params.budget;
333 FWP_DEBUG("Vres tx thread with budget=%ld period_sec=%ld "
334 "period_nsec=%ld.\n",vres->params.budget,
335 period->tv_sec, period->tv_nsec);
336 fwp_vres_clear_flag(vres, FWP_VF_RESCHED);
341 * Thread that does budgeting
344 static void* fwp_vres_tx_thread(void *_vres)
346 struct fwp_vres *vres = (struct fwp_vres*)_vres;
347 struct fwp_msgq *msgq = &vres->tx_queue;
348 struct fwp_msgb *msgb = NULL;
349 unsigned int ac_id = vres->params.ac_id;
350 fwp_budget_t budget = vres->params.budget;
351 fwp_budget_t curr_budget;
354 struct timespec start_period, end_period, period;
355 struct timespec current_time, interval;
357 fwp_set_rt_prio(90 - ac_id);
360 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
361 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
362 pthread_cleanup_push(fwp_vres_cleanup, (void*)vres);
364 clock_gettime(CLOCK_MONOTONIC, &start_period);
367 /* wait for next period and then send */
368 fwp_timespec_add(&end_period, &start_period, &period);
369 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
370 fwp_timespec_sub(&interval, &end_period, ¤t_time);
371 nanosleep(&interval, NULL);
373 sem_wait(&msgq->empty_lock);
374 fwp_vres_sched_update(vres, &period, &budget);
375 clock_gettime(CLOCK_MONOTONIC, &start_period);
377 /*msgb = fwp_msgq_dequeue(msgq);
380 while ((curr_budget < budget)&&
381 (msgb = fwp_msgq_dequeue(msgq))) {
382 rc = _fwp_vres_send(vres->ac_sockd, msgb);
384 FWP_DEBUG("Message sent through AC%d\n",ac_id);
385 /* Switch to this in the future */
386 //curr_budget+= msgb->len;
393 /*pthread_testcancel(); */
394 /*pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
396 fwp_timespec_add(&end_period, &start_period, &period);
397 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
398 fwp_timespec_sub(&interval, &end_period, ¤t_time);
399 nanosleep(&interval, NULL);
403 /* it should normaly never come here */
404 pthread_cleanup_pop(0);
410 int fwp_vres_send(fwp_vres_d_t vresd, struct fwp_msgb* msgb)
412 fwp_vres_t *vres = vresd;
414 if (fwp_vres_is_valid(vres)) {
415 return fwp_msgq_enqueue(&vres->tx_queue, msgb);
417 errno = EPERM; /* TODO: Use more appropriate error than EPERM. */
422 /*int fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
423 int fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
425 fwp_vres_t *vres = vresd;
428 pthread_mutex_lock(&fwp_vres_table.lock);
429 if (!fwp_vres_is_valid(vres)) {
435 if (fwp_vres_get_flag(vres, FWP_VF_BOUND)) { /*if already bounded */
441 vres->ac_sockd = sockd;
442 rv = fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
445 fwp_vres_set_flag(vres, FWP_VF_BOUND);
447 pthread_mutex_unlock(&fwp_vres_table.lock);
451 int fwp_vres_unbind(fwp_vres_d_t vresd)
453 fwp_vres_t *vres = vresd;
455 if (!fwp_vres_is_valid(vresd)) {
459 fwp_vres_clear_flag(vres, FWP_VF_BOUND);
460 /* TODO: consider what to do with pending messages */
461 /* fwp_vres_free_msgb(vres->tx_queue); */