bb956bcda57f38013c2973e7b002ee0dfad6433c
[samplevnf.git] / VNFs / DPPD-PROX / input.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <rte_cycles.h>
18 #include <rte_common.h>
19
20 #include "clock.h"
21 #include "input.h"
22
23 static struct input *inputs[32];
24 static int n_inputs;
25 static int max_input_fd;
26
27 int reg_input(struct input *in)
28 {
29         if (n_inputs == sizeof(inputs)/sizeof(inputs[0]))
30                 return -1;
31
32         for (int i = 0; i < n_inputs; ++i) {
33                 if (inputs[i] == in)
34                         return -1;
35         }
36         inputs[n_inputs++] = in;
37         max_input_fd = RTE_MAX(in->fd, max_input_fd);
38
39         return 0;
40 }
41
42 void unreg_input(struct input *in)
43 {
44         int rm, i;
45
46         for (rm = 0; rm < n_inputs; ++rm) {
47                 if (inputs[rm] == in) {
48                         break;
49                 }
50         }
51
52         if (rm == n_inputs)
53                 return ;
54
55         for (i = rm + 1; i < n_inputs; ++i) {
56                 inputs[i - 1] = inputs[i];
57         }
58
59         n_inputs--;
60         max_input_fd = 0;
61         for (i = 0; i < n_inputs; ++i) {
62                 max_input_fd = RTE_MAX(inputs[i]->fd, max_input_fd);
63         }
64 }
65
66 static int tsc_diff_to_tv(uint64_t beg, uint64_t end, struct timeval *tv)
67 {
68         if (end < beg) {
69                 return -1;
70         }
71
72         uint64_t diff = end - beg;
73         tsc_to_tv(tv, diff);
74         return 0;
75 }
76
77 void input_proc_until(uint64_t deadline)
78 {
79         struct timeval tv;
80         fd_set in_fd;
81         int ret = 1;
82
83         /* Keep checking for input until select() returned 0 (timeout
84            occurred before input was read) or current time has passed
85            the deadline (which occurs when time progresses past the
86            deadline between return of select() and the next
87            iteration). */
88         while (ret != 0 && tsc_diff_to_tv(rte_rdtsc(), deadline, &tv) == 0) {
89                 FD_ZERO(&in_fd);
90
91                 for (int i = 0; i < n_inputs; ++i) {
92                         FD_SET(inputs[i]->fd, &in_fd);
93                 }
94
95                 ret = select(max_input_fd + 1, &in_fd, NULL, NULL, &tv);
96
97                 if (ret > 0) {
98                         for (int i = 0; i < n_inputs; ++i) {
99                                 if (FD_ISSET(inputs[i]->fd, &in_fd)) {
100                                         inputs[i]->proc_input(inputs[i]);
101                                 }
102                         }
103                 }
104         }
105 }