4 static void* fwp_vres_tx_thread(void *_vres);
9 FWP_VRES_INACTIVE = 1 ,
10 FWP_VRES_UNBOUND = 2 ,
15 * Structure of FWP vres.
16 * Internal representation of vres
20 struct fwp_vres_params params;
21 /* consideration: move tx_queue to endpoint */
22 /**< queue for messages to send */
23 struct fwp_msgq tx_queue;
25 /**< endpoint bounded to this vres */
26 /*fwp_endpoint_t *epoint; */
27 pthread_t tx_thread; /**< tx_thread id*/
28 pthread_attr_t tx_thread_attr;
29 int ac_sockd; /**< ac socket descriptor */
30 fwp_vres_status_t status;
34 struct fwp_vres_table {
35 unsigned int max_vres;
40 /* Global variable - vres table */
41 static fwp_vres_table_t fwp_vres_table = {
44 .lock = PTHREAD_MUTEX_INITIALIZER,
47 /**< mapping priority to ac*/
48 static const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
49 /**< IP tos for AC_VI, AC_VO, AC_BE, AC_BK */
50 static const unsigned int ac_to_tos[4] = {224,160,96,64};
52 static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
56 tos = ac_to_tos[ac_id];
57 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
58 perror("Root permission needed to set AC");
66 static int fwp_vres_ac_open(fwp_ac_t ac_id)
71 if ((ac_id < 0)||(ac_id >= FWP_AC_NUM))
74 if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
75 perror("Unable to open socket for AC");
79 tos = ac_to_tos[ac_id];
81 if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
82 perror("Root permission needed to set AC");
91 static inline int __fwp_vres_send(unsigned int ac_sockd, struct fwp_msgb* msgb)
93 /*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0,
94 msgb->peer->addr, msgb->peer->addrlen);*/
95 _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
99 static inline void fwp_vres_free(fwp_vres_t *vres)
101 vres->status = FWP_VRES_FREE;
104 static inline int fwp_vres_is_valid(fwp_vres_d_t vresd)
106 int id = vresd - fwp_vres_table.entry;
108 if ((id < 0) || (id > fwp_vres_table.max_vres - 1) ||
109 (vresd->status == FWP_VRES_FREE))
115 /*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
117 3if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
119 *vres = &fwp_vres_table.entry[vres_id];
124 int fwp_vres_table_init(unsigned int max_vres)
126 unsigned int table_size = max_vres * sizeof(fwp_vres_t);
128 fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
129 if (!fwp_vres_table.entry)
132 memset((void*) fwp_vres_table.entry, 0, table_size);
133 fwp_vres_table.max_vres = max_vres;
137 fwp_vres_id_t fwp_vres_get_id(fwp_vres_d_t vresd)
139 fwp_vres_t *vres = vresd;
141 return (vres - fwp_vres_table.entry);
144 fwp_vres_d_t fwp_vres_alloc()
147 unsigned int max_vres;
149 /* find free vres id */
150 pthread_mutex_lock(&fwp_vres_table.lock);
152 max_vres = fwp_vres_table.max_vres;
153 while ((i < max_vres) &&
154 (fwp_vres_table.entry[i].status != FWP_VRES_FREE)) {
159 pthread_mutex_unlock(&fwp_vres_table.lock);
163 fwp_vres_table.entry[i].status = FWP_VRES_INACTIVE;
164 pthread_mutex_unlock(&fwp_vres_table.lock);
165 return (&fwp_vres_table.entry[i]);
169 int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
171 fwp_vres_t *vres = vresd;
174 if (!fwp_vres_is_valid(vresd)) {
177 /* copy vres paramters into vres structure */
178 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
180 if (vres->status != FWP_VRES_INACTIVE) {
181 /* Consider: how to change parameters when vres is in running
182 * state - restart thread, set vres_resched flag
184 pthread_cancel(vres->tx_thread);
185 /* or set vres_resched flag and return */
187 /* initialize msg queue */
188 fwp_msgq_init(&vres->tx_queue);
191 if ((vres->status == FWP_VRES_BOUND) &&
192 (rv = fwp_vres_set_ac(vres->ac_sockd, vres->params.ac_id)) < 0) {
196 pthread_attr_init(&vres->tx_thread_attr);
197 if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr,
198 fwp_vres_tx_thread, (void*) vres)) != 0){
202 vres->status = FWP_VRES_UNBOUND;
213 * \param[in] params Vres parameters
214 * \param[out] vresdp Pointer to the descriptor of newly created vres
216 * \return On success returns descriptor of vres.
217 * On error, negative error code is returned.
220 int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
225 /* Check for validity of the contract */
227 vres = fwp_vres_alloc();
231 /* copy vres paramters into vres structure */
232 memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
233 /* initialize msg queue */
234 fwp_msgq_init(&vres->tx_queue);
236 pthread_attr_init(&vres->tx_thread_attr);
237 if ((rv = pthread_create(&vres->tx_thread, &vres->tx_thread_attr,
238 fwp_vres_tx_thread, (void*) vres)) != 0){
242 vres->status = FWP_VRES_UNBOUND;
254 * \param[in] vresd Vres descriptor
256 * \return On success returns 0.
257 * On error, negative error code is returned.
260 int fwp_vres_destroy(fwp_vres_d_t vresd)
262 fwp_vres_t *vres = vresd;
264 if (!fwp_vres_is_valid(vresd))
267 vres->status = FWP_VRES_FREE;
268 pthread_cancel(vres->tx_thread);
270 FWP_DEBUG("Vres vparam_id=%d destroyed.\n", vres->params.id);
274 static void fwp_vres_cleanup(void *_vres)
276 fwp_vres_t *vres = (fwp_vres_t*)_vres;
278 fwp_msgq_dequeue_all(&vres->tx_queue);
282 static void* fwp_vres_tx_thread(void *_vres)
283 {/* TODO: make changes that count with changing of params */
284 struct fwp_vres *vres = (struct fwp_vres*)_vres;
285 struct fwp_msgq *msgq = &vres->tx_queue;
286 struct fwp_msgb *msgb = NULL;
287 unsigned int ac_id = vres->params.ac_id;
288 /*unsigned int ac_sockd = vres->ac_sockd;*/
289 int budget = vres->params.budget;
293 struct timespec start_period, end_period, period;
294 struct timespec current_time, interval;
296 period.tv_nsec = vres->params.period_usec % SEC_TO_USEC;
297 period.tv_sec = vres->params.period_usec / SEC_TO_USEC;
299 fwp_set_rt_prio(90 - ac_id);
301 FWP_DEBUG("Started vres tx thread with budget:%d period_sec=%ld "
302 "period_nsec=%ld.\n",
303 vres->params.budget, period.tv_sec, period.tv_nsec);
305 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
306 pthread_cleanup_push(fwp_vres_cleanup, (void*)vres);
307 clock_gettime(CLOCK_MONOTONIC, &start_period);
309 while (vres->status && (FWP_VRES_UNBOUND || FWP_VRES_BOUND)) {
310 /* wait for next period and then send */
311 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
313 fwp_timespec_add(&end_period, &start_period, &period);
314 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
315 fwp_timespec_sub(&interval, &end_period, ¤t_time);
316 nanosleep(&interval, NULL);
318 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
319 sem_wait(&msgq->empty_lock);
320 clock_gettime(CLOCK_MONOTONIC, &start_period);
322 /*msgb = fwp_msgq_dequeue(msgq);
325 while ((curr_budget < budget)&&
326 (msgb = fwp_msgq_dequeue(msgq))) {
327 rc = __fwp_vres_send(vres->ac_sockd, msgb);
329 FWP_DEBUG("Message sent through AC%d\n",ac_id);
330 /* Switch to this in the future
331 * curr_budget+= msgb->len;
338 pthread_testcancel();
339 /*pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
341 fwp_timespec_add(&end_period, &start_period, &period);
342 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
343 fwp_timespec_sub(&interval, &end_period, ¤t_time);
344 nanosleep(&interval, NULL);
348 /* it should normaly never come here */
349 pthread_cleanup_pop(0);
355 int _fwp_vres_send(fwp_vres_d_t vresd, struct fwp_msgb* msgb)
357 fwp_vres_t *vres = vresd;
359 if (vres->status == FWP_VRES_BOUND) {
360 return fwp_msgq_enqueue(&vres->tx_queue, msgb);
365 /*int _fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
366 int _fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
368 fwp_vres_t *vres = vresd;
371 pthread_mutex_lock(&fwp_vres_table.lock);
372 if (!fwp_vres_is_valid(vresd)) {
377 if (vres->status != FWP_VRES_UNBOUND) { /*if already bounded */
382 vres->ac_sockd = sockd;
383 fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
384 vres->status = FWP_VRES_BOUND;
386 pthread_mutex_unlock(&fwp_vres_table.lock);
390 int _fwp_vres_unbind(fwp_vres_d_t vresd)
392 fwp_vres_t *vres = vresd;
394 if (!fwp_vres_is_valid(vresd)) {
397 vres->status = FWP_VRES_UNBOUND;
398 /* TODO: consider what to do with pending messages */
399 /* fwp_vres_free_msgb(vres->tx_queue); */