Support packets in flight
[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(void)
78 {
79         struct timeval tv;
80         fd_set in_fd;
81         int ret = 1;
82
83         tv.tv_sec = 0;
84         tv.tv_usec = 0;
85         while (ret != 0) {
86                 FD_ZERO(&in_fd);
87
88                 for (int i = 0; i < n_inputs; ++i) {
89                         FD_SET(inputs[i]->fd, &in_fd);
90                 }
91
92                 ret = select(max_input_fd + 1, &in_fd, NULL, NULL, &tv);
93
94                 if (ret > 0) {
95                         for (int i = 0; i < n_inputs; ++i) {
96                                 if (FD_ISSET(inputs[i]->fd, &in_fd)) {
97                                         inputs[i]->proc_input(inputs[i]);
98                                 }
99                         }
100                 }
101         }
102 }
103
104 void input_proc_until(uint64_t deadline)
105 {
106         struct timeval tv;
107         fd_set in_fd;
108         int ret = 1;
109
110         /* Keep checking for input until select() returned 0 (timeout
111            occurred before input was read) or current time has passed
112            the deadline (which occurs when time progresses past the
113            deadline between return of select() and the next
114            iteration). */
115         while (ret != 0 && tsc_diff_to_tv(rte_rdtsc(), deadline, &tv) == 0) {
116                 FD_ZERO(&in_fd);
117
118                 for (int i = 0; i < n_inputs; ++i) {
119                         FD_SET(inputs[i]->fd, &in_fd);
120                 }
121
122                 ret = select(max_input_fd + 1, &in_fd, NULL, NULL, &tv);
123
124                 if (ret > 0) {
125                         for (int i = 0; i < n_inputs; ++i) {
126                                 if (FD_ISSET(inputs[i]->fd, &in_fd)) {
127                                         inputs[i]->proc_input(inputs[i]);
128                                 }
129                         }
130                 }
131         }
132 }