TAS
TCP Acceleration as an OS Service
tcp_common.c
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 #include <stdio.h>
26 
27 #include <flextcp_plif.h>
28 #include "../tcp_common.h"
29 
30 static int test_tcp_valid_rxseq_single(uint32_t fs_seq, uint32_t fs_avail,
31  uint32_t pkt_seq, uint32_t pkt_bytes, int process, uint16_t trim_start,
32  uint16_t trim_end)
33 {
34  struct flextcp_pl_flowst fs;
35  uint16_t ts, te;
36  int ret;
37 
38  printf(" fs_seq=%u fs_avail=%u pkt_seq=%u pkt_bytes=%u\n", fs_seq, fs_avail,
39  pkt_seq, pkt_bytes);
40 
41  fs.rx_next_seq = fs_seq;
42  fs.rx_avail = fs_avail;
43 
44  ret = tcp_valid_rxseq(&fs, pkt_seq, pkt_bytes, &ts, &te);
45  if (ret != 0) {
46  if (process) {
47  fprintf(stderr, " pkt unexpectedly rejected\n");
48  return -1;
49  }
50  return 0;
51  } else if (!process) {
52  fprintf(stderr, " pkt unexpectedly accepted\n");
53  return -1;
54  }
55 
56  if (trim_start != ts || trim_end != te) {
57  fprintf(stderr, " trims don't match: got (ts=%u te=%u) expect "
58  "(ts=%u te=%u)\n", ts, te, trim_start, trim_end);
59  return -1;
60  }
61 
62  return 0;
63 }
64 
65 static int test_tcp_valid_rxseq(void)
66 {
67  int ret = 0;
68 
69  printf("testing tcp_valid_rxseq\n");
70 
71  /* fits fully, no wrap-arounds */
72  if (test_tcp_valid_rxseq_single(2048, 8192, 2048, 1400, 1, 0, 0) != 0)
73  ret = -1;
74  /* overlaps already received bytes, no wrap-arounds */
75  if (test_tcp_valid_rxseq_single(2048, 8192, 2000, 1400, 1, 48, 0) != 0)
76  ret = -1;
77  /* overlaps already received bytes, and does not fit fully, no wrap-arounds */
78  if (test_tcp_valid_rxseq_single(2048, 1000, 2000, 1400, 1, 48, 352) != 0)
79  ret = -1;
80  /* starts after beginning, no wrap-arounds */
81  if (test_tcp_valid_rxseq_single(2048, 8192, 2049, 1400, 0, 0, 0) != 0)
82  ret = -1;
83  /* starts after buffer, no wrap-arounds */
84  if (test_tcp_valid_rxseq_single(2048, 8192, 16384, 1400, 0, 0, 0) != 0)
85  ret = -1;
86 
87  /* fits fully, wrap in buffer */
88  if (test_tcp_valid_rxseq_single(4294965248, 8192, 4294965248, 1400, 1, 0, 0) != 0)
89  ret = -1;
90  /* overlaps already received bytes, wrap in buffer */
91  if (test_tcp_valid_rxseq_single(4294965248, 8192, 4294965200, 1400, 1, 48, 0) != 0)
92  ret = -1;
93  /* starts after beginning in upper half, wrap in buffer */
94  if (test_tcp_valid_rxseq_single(4294965248, 8192, 4294965249, 1400, 0, 0, 0) != 0)
95  ret = -1;
96  /* starts after beginning in lower half, wrap in buffer */
97  if (test_tcp_valid_rxseq_single(4294965248, 8192, 0, 1400, 0, 0, 0) != 0)
98  ret = -1;
99  /* starts after buffer, wrap in buffer */
100  if (test_tcp_valid_rxseq_single(4294965248, 8192, 6144, 1400, 0, 0, 0) != 0)
101  ret = -1;
102 
103  /* fits fully, wrap in buffer and packet */
104  if (test_tcp_valid_rxseq_single(4294966784, 8192, 4294966784, 1400, 1, 0, 0) != 0)
105  ret = -1;
106  /* overlaps already received bytes, wrap in buffer and packet */
107  if (test_tcp_valid_rxseq_single(4294966784, 8192, 4294966700, 1400, 1, 84, 0) != 0)
108  ret = -1;
109  /* overlaps already received bytes, and does not fit fully, wrap in buffer and packet */
110  if (test_tcp_valid_rxseq_single(4294966784, 1000, 4294966700, 1400, 1, 84, 316) != 0)
111  ret = -1;
112  /* starts after beginning , wrap in buffer and packet*/
113  if (test_tcp_valid_rxseq_single(4294966784, 8192, 4294966785, 1400, 0, 0, 0) != 0)
114  ret = -1;
115 
116  return ret;
117 }
118 
119 static int test_tcp_trim_rxbuf_single(uint32_t fs_seq, uint32_t fs_avail,
120  uint32_t pkt_seq, uint32_t pkt_bytes, int process, uint16_t trim_start,
121  uint16_t trim_end)
122 {
123  struct flextcp_pl_flowst fs;
124  uint16_t ts, te;
125  int ret;
126 
127  printf(" fs_seq=%u fs_avail=%u pkt_seq=%u pkt_bytes=%u\n", fs_seq, fs_avail,
128  pkt_seq, pkt_bytes);
129 
130  fs.rx_next_seq = fs_seq;
131  fs.rx_avail = fs_avail;
132 
133  ret = tcp_trim_rxbuf(&fs, pkt_seq, pkt_bytes, &ts, &te);
134  if (ret != 0) {
135  if (process) {
136  fprintf(stderr, " pkt unexpectedly rejected\n");
137  return -1;
138  }
139  return 0;
140  } else if (!process) {
141  fprintf(stderr, " pkt unexpectedly accepted\n");
142  return -1;
143  }
144 
145  if (trim_start != ts || trim_end != te) {
146  fprintf(stderr, " trims don't match: got (ts=%u te=%u) expect "
147  "(ts=%u te=%u)\n", ts, te, trim_start, trim_end);
148  return -1;
149  }
150 
151  return 0;
152 }
153 
154 static int test_tcp_trim_rxbuf(void)
155 {
156  int ret = 0;
157 
158  printf("testing tcp_trim_rxbuf\n");
159 
160  /* fits fully, no wrap-arounds */
161  if (test_tcp_trim_rxbuf_single(2048, 8192, 2048, 1400, 1, 0, 0) != 0)
162  ret = -1;
163  /* overlaps already received bytes, no wrap-arounds */
164  if (test_tcp_trim_rxbuf_single(2048, 8192, 2000, 1400, 1, 48, 0) != 0)
165  ret = -1;
166  /* overlaps already received bytes, and does not fit fully, no wrap-arounds */
167  if (test_tcp_trim_rxbuf_single(2048, 1000, 2000, 1400, 1, 48, 352) != 0)
168  ret = -1;
169  /* starts after beginning, no wrap-arounds */
170  if (test_tcp_trim_rxbuf_single(2048, 8192, 2049, 1400, 1, 0, 0) != 0)
171  ret = -1;
172  /* starts after buffer, no wrap-arounds */
173  if (test_tcp_trim_rxbuf_single(2048, 8192, 16384, 1400, 0, 0, 0) != 0)
174  ret = -1;
175 
176  /* fits fully, wrap in buffer */
177  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 4294965248, 1400, 1, 0, 0) != 0)
178  ret = -1;
179  /* overlaps already received bytes, wrap in buffer */
180  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 4294965200, 1400, 1, 48, 0) != 0)
181  ret = -1;
182  /* starts after beginning in upper half, wrap in buffer */
183  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 4294965249, 1400, 1, 0, 0) != 0)
184  ret = -1;
185  /* starts after beginning in lower half, wrap in buffer */
186  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 0, 1400, 1, 0, 0) != 0)
187  ret = -1;
188  /* starts right after buffer, wrap in buffer */
189  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 6144, 1400, 1, 0, 1400) != 0)
190  ret = -1;
191  /* starts further after buffer, wrap in buffer */
192  if (test_tcp_trim_rxbuf_single(4294965248, 8192, 6145, 1400, 0, 0, 0) != 0)
193  ret = -1;
194 
195  /* fits fully, wrap in buffer and packet */
196  if (test_tcp_trim_rxbuf_single(4294966784, 8192, 4294966784, 1400, 1, 0, 0) != 0)
197  ret = -1;
198  /* overlaps already received bytes, wrap in buffer and packet */
199  if (test_tcp_trim_rxbuf_single(4294966784, 8192, 4294966700, 1400, 1, 84, 0) != 0)
200  ret = -1;
201  /* overlaps already received bytes, and does not fit fully, wrap in buffer and packet */
202  if (test_tcp_trim_rxbuf_single(4294966784, 1000, 4294966700, 1400, 1, 84, 316) != 0)
203  ret = -1;
204  /* starts after beginning , wrap in buffer and packet*/
205  if (test_tcp_trim_rxbuf_single(4294966784, 8192, 4294966785, 1400, 1, 0, 0) != 0)
206  ret = -1;
207 
208  return ret;
209 }
210 
211 
212 int main(int argc, char *argv[])
213 {
214  int ret = 0;
215 
216  if (test_tcp_valid_rxseq() != 0)
217  ret = -1;
218 
219  if (test_tcp_trim_rxbuf() != 0)
220  ret = -1;
221 
222  return ret;
223 }