TAS
TCP Acceleration as an OS Service
internal.h
1 /*
2  * Copyright 2019 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef INTERNAL_H_
26 #define INTERNAL_H_
27 
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/socket.h>
32 #include <sys/epoll.h>
33 #include <time.h>
34 #include <poll.h>
35 #include <netinet/in.h>
36 
37 #include <tas_ll.h>
38 #include <utils_sync.h>
39 
40 enum filehandle_type {
41  SOCK_UNUSED = 0,
42  SOCK_SOCKET = 1,
43  SOCK_CONNECTION = 2,
44  SOCK_LISTENER = 3,
45 };
46 
47 enum socket_flags {
48  SOF_NONBLOCK = 1,
49  SOF_BOUND = 2,
50  SOF_REUSEPORT = 4,
51  SOF_CLOEXEC = 8,
52 };
53 
54 enum conn_status {
55  SOC_CONNECTING = 0,
56  SOC_CONNECTED = 1,
57  SOC_FAILED = 2,
58  SOC_CLOSED = 3,
59 };
60 
61 enum listen_status {
62  SOL_OPENING = 0,
63  SOL_OPEN = 1,
64  SOL_FAILED = 2,
65 };
66 
67 enum conn_stflags {
68  CSTF_RXCLOSED = 1,
69  CSTF_TXCLOSED = 2,
70  CSTF_TXCLOSED_ACK = 4,
71 };
72 
74  struct socket *s;
75  struct flextcp_context *ctx;
76  struct socket_pending *next;
77  int fd;
78 };
79 
80 struct socket_conn {
81  struct flextcp_connection c;
82  uint8_t status;
83  uint8_t st_flags;
84  struct socket *listener;
85 
86  void *rx_buf_1;
87  void *rx_buf_2;
88  size_t rx_len_1;
89  size_t rx_len_2;
90  struct flextcp_context *ctx;
91  int move_status;
92 };
93 
94 struct socket_listen {
95  struct flextcp_listener l;
96  struct socket_pending *pending;
97  int backlog;
98  uint8_t status;
99 };
100 
101 struct socket {
102  union {
103  struct socket_conn connection;
104  struct socket_listen listener;
105  } data;
106  struct sockaddr_in addr;
107  uint8_t flags;
108  uint8_t type;
109  int refcnt;
110  volatile uint32_t sp_lock;
111 
113  uint32_t ep_events;
115  struct epoll_socket *eps;
116 #if 0
117 
118  struct epoll_socket *eps_exc_first;
120  struct epoll_socket *eps_exc_last;
121 #endif
122 };
123 
124 struct epoll {
129  struct epoll_socket *active_last;
130 
131  int refcnt;
132  volatile uint32_t sp_lock;
133 
134  uint32_t num_linux;
135  uint32_t num_tas;
136  uint32_t num_active;
137  uint8_t linux_next;
138 };
139 
140 struct epoll_socket {
141  struct epoll *ep;
142  struct socket *s;
143 
144  struct epoll_socket *ep_next;
145  struct epoll_socket *ep_prev;
146 
147  struct epoll_socket *so_prev;
148  struct epoll_socket *so_next;
149 
150  epoll_data_t data;
151  uint32_t mask;
152  uint8_t active;
153 };
154 
156  struct flextcp_context ctx;
157 
158  struct pollfd *pollfds_cache;
159  size_t pollfds_cache_size;
160 
161  struct pollfd *selectfds_cache;
162  size_t selectfds_cache_size;
163 };
164 
165 int flextcp_fd_init(void);
166 int flextcp_fd_salloc(struct socket **ps);
167 int flextcp_fd_ealloc(struct epoll **pe, int fd);
168 int flextcp_fd_slookup(int fd, struct socket **ps);
169 int flextcp_fd_elookup(int fd, struct epoll **pe);
170 void flextcp_fd_srelease(int fd, struct socket *s);
171 void flextcp_fd_erelease(int fd, struct epoll *ep);
172 void flextcp_fd_close(int fd);
173 
174 struct sockets_context *flextcp_sockctx_getfull(void);
175 struct flextcp_context *flextcp_sockctx_get(void);
176 int flextcp_sockctx_poll(struct flextcp_context *ctx);
177 int flextcp_sockctx_poll_n(struct flextcp_context *ctx, unsigned n);
178 
179 void flextcp_sockclose_finish(struct flextcp_context *ctx, struct socket *s);
180 
181 void flextcp_epoll_sockinit(struct socket *s);
182 void flextcp_epoll_sockclose(struct socket *s);
183 void flextcp_epoll_set(struct socket *s, uint32_t evts);
184 void flextcp_epoll_clear(struct socket *s, uint32_t evts);
185 void flextcp_epoll_destroy(struct epoll *ep);
186 
187 int tas_sock_close(struct socket *sock);
188 int tas_sock_move(struct socket *s);
189 
190 int tas_libc_epoll_create1(int flags);
191 int tas_libc_epoll_ctl(int epfd, int op, int fd,
192  struct epoll_event *event);
193 int tas_libc_epoll_wait(int epfd, struct epoll_event *events,
194  int maxevents, int timeout);
195 int tas_libc_poll(struct pollfd *fds, nfds_t nfds, int timeout);
196 int tas_libc_close(int fd);
197 int tas_libc_dup(int oldfd);
198 int tas_libc_dup2(int oldfd, int newfd);
199 int tas_libc_dup3(int oldfd, int newfd, int flags);
200 
201 static inline void socket_lock(struct socket *s)
202 {
203  util_spin_lock(&s->sp_lock);
204 }
205 
206 static inline void socket_unlock(struct socket *s)
207 {
208  util_spin_unlock(&s->sp_lock);
209 }
210 
211 static inline void epoll_lock(struct epoll *ep)
212 {
213  util_spin_lock(&ep->sp_lock);
214 }
215 
216 static inline void epoll_unlock(struct epoll *ep)
217 {
218  util_spin_unlock(&ep->sp_lock);
219 }
220 
221 static inline uint64_t get_msecs(void)
222 {
223  int ret;
224  struct timespec ts;
225 
226  ret = clock_gettime(CLOCK_MONOTONIC, &ts);
227  if (ret != 0) {
228  perror("flextcp get_msecs: clock_gettime failed\n");
229  abort();
230  }
231 
232  return ts.tv_sec * 1000ULL + (ts.tv_nsec / 1000000ULL);
233 }
234 
235 
236 #endif /* ndef INTERNAL_H_ */
struct epoll_socket * inactive
Definition: internal.h:126
Public low-level application interface for TAS.
struct epoll_socket * active_first
Definition: internal.h:128
struct epoll_socket * eps
Definition: internal.h:115
uint32_t ep_events
Definition: internal.h:113
static int epfd
Definition: appif.c:80