blob: 25bf7387c69822eccba5c0f1a982524dc07e3968 [file] [log] [blame]
Florin Coras999840c2020-03-18 20:31:34 +00001/*
2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef SRC_VNET_TCP_TCP_INLINES_H_
17#define SRC_VNET_TCP_TCP_INLINES_H_
18
19#include <vnet/tcp/tcp.h>
20
21always_inline tcp_header_t *
22tcp_buffer_hdr (vlib_buffer_t * b)
23{
24 ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE);
25 return (tcp_header_t *) (b->data + b->current_data
26 + vnet_buffer (b)->tcp.hdr_offset);
27}
28
29always_inline tcp_connection_t *
30tcp_connection_get (u32 conn_index, u32 thread_index)
31{
32 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
33 if (PREDICT_FALSE (pool_is_free_index (wrk->connections, conn_index)))
34 return 0;
35 return pool_elt_at_index (wrk->connections, conn_index);
36}
37
38always_inline tcp_connection_t *
39tcp_connection_get_if_valid (u32 conn_index, u32 thread_index)
40{
41 tcp_worker_ctx_t *wrk;
42 if (thread_index >= vec_len (tcp_main.wrk_ctx))
43 return 0;
44 wrk = tcp_get_worker (thread_index);
45 if (pool_is_free_index (wrk->connections, conn_index))
46 return 0;
47 return pool_elt_at_index (wrk->connections, conn_index);
48}
49
50always_inline void
51tcp_connection_set_state (tcp_connection_t * tc, tcp_state_t state)
52{
53 tc->state = state;
54 TCP_EVT (TCP_EVT_STATE_CHANGE, tc);
55}
56
57always_inline tcp_connection_t *
58tcp_listener_get (u32 tli)
59{
60 tcp_connection_t *tc = 0;
61 if (!pool_is_free_index (tcp_main.listener_pool, tli))
62 tc = pool_elt_at_index (tcp_main.listener_pool, tli);
63 return tc;
64}
65
66always_inline tcp_connection_t *
67tcp_half_open_connection_get (u32 conn_index)
68{
69 tcp_connection_t *tc = 0;
70 clib_spinlock_lock_if_init (&tcp_main.half_open_lock);
71 if (!pool_is_free_index (tcp_main.half_open_connections, conn_index))
72 tc = pool_elt_at_index (tcp_main.half_open_connections, conn_index);
73 clib_spinlock_unlock_if_init (&tcp_main.half_open_lock);
74 return tc;
75}
76
77/**
78 * Our estimate of the number of bytes that have left the network
79 */
80always_inline u32
81tcp_bytes_out (const tcp_connection_t * tc)
82{
83 if (tcp_opts_sack_permitted (&tc->rcv_opts))
84 return tc->sack_sb.sacked_bytes + tc->sack_sb.lost_bytes;
85 else
86 return clib_min (tc->rcv_dupacks * tc->snd_mss,
87 tc->snd_nxt - tc->snd_una);
88}
89
90/**
91 * Our estimate of the number of bytes in flight (pipe size)
92 */
93always_inline u32
94tcp_flight_size (const tcp_connection_t * tc)
95{
96 int flight_size;
97
98 flight_size = (int) (tc->snd_nxt - tc->snd_una) - tcp_bytes_out (tc)
99 + tc->snd_rxt_bytes - tc->rxt_delivered;
100
101 ASSERT (flight_size >= 0);
102
103 return flight_size;
104}
105
106/**
107 * Initial cwnd as per RFC5681
108 */
109always_inline u32
110tcp_initial_cwnd (const tcp_connection_t * tc)
111{
112 if (tcp_cfg.initial_cwnd_multiplier > 0)
113 return tcp_cfg.initial_cwnd_multiplier * tc->snd_mss;
114
115 if (tc->snd_mss > 2190)
116 return 2 * tc->snd_mss;
117 else if (tc->snd_mss > 1095)
118 return 3 * tc->snd_mss;
119 else
120 return 4 * tc->snd_mss;
121}
122
123/*
124 * Accumulate acked bytes for cwnd increase
125 *
126 * Once threshold bytes are accumulated, snd_mss bytes are added
127 * to the cwnd.
128 */
129always_inline void
130tcp_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes)
131{
132 tc->cwnd_acc_bytes += bytes;
133 if (tc->cwnd_acc_bytes >= thresh)
134 {
135 u32 inc = tc->cwnd_acc_bytes / thresh;
136 tc->cwnd_acc_bytes -= inc * thresh;
137 tc->cwnd += inc * tc->snd_mss;
138 tc->cwnd = clib_min (tc->cwnd, tc->tx_fifo_size);
139 }
140}
141
142always_inline u32
143tcp_loss_wnd (const tcp_connection_t * tc)
144{
145 /* Whatever we have in flight + the packet we're about to send */
146 return tcp_flight_size (tc) + tc->snd_mss;
147}
148
149always_inline u32
150tcp_available_snd_wnd (const tcp_connection_t * tc)
151{
152 return clib_min (tc->cwnd, tc->snd_wnd);
153}
154
155always_inline u32
156tcp_available_output_snd_space (const tcp_connection_t * tc)
157{
158 u32 available_wnd = tcp_available_snd_wnd (tc);
159 int flight_size = (int) (tc->snd_nxt - tc->snd_una);
160
161 if (available_wnd <= flight_size)
162 return 0;
163
164 return available_wnd - flight_size;
165}
166
167/**
168 * Estimate of how many bytes we can still push into the network
169 */
170always_inline u32
171tcp_available_cc_snd_space (const tcp_connection_t * tc)
172{
173 u32 available_wnd = tcp_available_snd_wnd (tc);
174 u32 flight_size = tcp_flight_size (tc);
175
176 if (available_wnd <= flight_size)
177 return 0;
178
179 return available_wnd - flight_size;
180}
181
182always_inline u8
183tcp_is_lost_fin (tcp_connection_t * tc)
184{
Florin Coras55e556c2020-10-23 10:45:48 -0700185 if ((tc->flags & TCP_CONN_FINSNT) && (tc->snd_nxt - tc->snd_una == 1))
Florin Coras999840c2020-03-18 20:31:34 +0000186 return 1;
187 return 0;
188}
189
190always_inline u32
191tcp_time_now (void)
192{
193 return tcp_main.wrk_ctx[vlib_get_thread_index ()].time_now;
194}
195
196always_inline u32
197tcp_time_now_w_thread (u32 thread_index)
198{
199 return tcp_main.wrk_ctx[thread_index].time_now;
200}
201
202/**
203 * Generate timestamp for tcp connection
204 */
205always_inline u32
206tcp_tstamp (tcp_connection_t * tc)
207{
208 return (tcp_main.wrk_ctx[tc->c_thread_index].time_now -
209 tc->timestamp_delta);
210}
211
212always_inline f64
213tcp_time_now_us (u32 thread_index)
214{
215 return transport_time_now (thread_index);
216}
217
218always_inline u32
219tcp_set_time_now (tcp_worker_ctx_t * wrk)
220{
Florin Coraseedc74b2020-07-31 12:32:40 -0700221 return wrk->time_now = (u64) (vlib_time_now (wrk->vm) * TCP_TSTP_HZ);
Florin Coras999840c2020-03-18 20:31:34 +0000222}
223
224always_inline tcp_connection_t *
225tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
226 u8 is_ip4, u8 is_nolookup)
227{
228 u32 fib_index = vnet_buffer (b)->ip.fib_index;
229 int n_advance_bytes, n_data_bytes;
230 transport_connection_t *tc;
231 tcp_header_t *tcp;
232 u8 result = 0;
233
234 if (is_ip4)
235 {
236 ip4_header_t *ip4 = vlib_buffer_get_current (b);
237 int ip_hdr_bytes = ip4_header_bytes (ip4);
238 if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
239 {
240 *error = TCP_ERROR_LENGTH;
241 return 0;
242 }
243 tcp = ip4_next_header (ip4);
244 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
245 n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
246 n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
247
248 /* Length check. Checksum computed by ipx_local no need to compute again */
249 if (PREDICT_FALSE (n_data_bytes < 0))
250 {
251 *error = TCP_ERROR_LENGTH;
252 return 0;
253 }
254
255 if (!is_nolookup)
256 tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
257 &ip4->src_address, tcp->dst_port,
258 tcp->src_port,
259 TRANSPORT_PROTO_TCP, thread_index,
260 &result);
261 }
262 else
263 {
264 ip6_header_t *ip6 = vlib_buffer_get_current (b);
265 if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
266 {
267 *error = TCP_ERROR_LENGTH;
268 return 0;
269 }
270 tcp = ip6_next_header (ip6);
271 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
272 n_advance_bytes = tcp_header_bytes (tcp);
273 n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
274 - n_advance_bytes;
275 n_advance_bytes += sizeof (ip6[0]);
276
277 if (PREDICT_FALSE (n_data_bytes < 0))
278 {
279 *error = TCP_ERROR_LENGTH;
280 return 0;
281 }
282
283 if (!is_nolookup)
284 {
285 if (PREDICT_FALSE
286 (ip6_address_is_link_local_unicast (&ip6->dst_address)))
287 {
Florin Coras1a18d112020-04-21 19:55:39 +0000288 ip6_main_t *im = &ip6_main;
Florin Coras999840c2020-03-18 20:31:34 +0000289 fib_index = vec_elt (im->fib_index_by_sw_if_index,
290 vnet_buffer (b)->sw_if_index[VLIB_RX]);
291 }
292
293 tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
294 &ip6->src_address,
295 tcp->dst_port, tcp->src_port,
296 TRANSPORT_PROTO_TCP,
297 thread_index, &result);
298 }
299 }
300
301 if (is_nolookup)
302 tc =
303 (transport_connection_t *) tcp_connection_get (vnet_buffer (b)->
304 tcp.connection_index,
305 thread_index);
306
307 vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
308 vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
309 vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
310 vnet_buffer (b)->tcp.data_len = n_data_bytes;
311 vnet_buffer (b)->tcp.seq_end = vnet_buffer (b)->tcp.seq_number
312 + n_data_bytes;
313 vnet_buffer (b)->tcp.flags = 0;
314
315 *error = result ? TCP_ERROR_NONE + result : *error;
316
317 return tcp_get_connection_from_transport (tc);
318}
319
320/**
321 * Initialize connection by gleaning network and rcv params from buffer
322 *
323 * @param tc connection to initialize
324 * @param b buffer whose current data is pointing at ip
325 * @param is_ip4 flag set to 1 if using ip4
326 */
327always_inline void
328tcp_init_w_buffer (tcp_connection_t * tc, vlib_buffer_t * b, u8 is_ip4)
329{
330 tcp_header_t *th = tcp_buffer_hdr (b);
331
332 tc->c_lcl_port = th->dst_port;
333 tc->c_rmt_port = th->src_port;
334 tc->c_is_ip4 = is_ip4;
335
336 if (is_ip4)
337 {
338 ip4_header_t *ip4 = vlib_buffer_get_current (b);
339 tc->c_lcl_ip4.as_u32 = ip4->dst_address.as_u32;
340 tc->c_rmt_ip4.as_u32 = ip4->src_address.as_u32;
341 }
342 else
343 {
344 ip6_header_t *ip6 = vlib_buffer_get_current (b);
345 clib_memcpy_fast (&tc->c_lcl_ip6, &ip6->dst_address,
346 sizeof (ip6_address_t));
347 clib_memcpy_fast (&tc->c_rmt_ip6, &ip6->src_address,
348 sizeof (ip6_address_t));
349 }
350
351 tc->irs = vnet_buffer (b)->tcp.seq_number;
352 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_number + 1;
353 tc->rcv_las = tc->rcv_nxt;
354 tc->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
355 tc->snd_wl1 = vnet_buffer (b)->tcp.seq_number;
356 tc->snd_wl2 = vnet_buffer (b)->tcp.ack_number;
357
358 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
359 * segments are used to initialize PAWS. */
360 if (tcp_opts_tstamp (&tc->rcv_opts))
361 {
362 tc->tsval_recent = tc->rcv_opts.tsval;
363 tc->tsval_recent_age = tcp_time_now ();
364 }
365
366 if (tcp_opts_wscale (&tc->rcv_opts))
367 tc->snd_wscale = tc->rcv_opts.wscale;
368
369 tc->snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
370}
371
372always_inline void
373tcp_update_rto (tcp_connection_t * tc)
374{
375 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
376 tc->rto = clib_max (tc->rto, TCP_RTO_MIN);
377}
378
379always_inline u8
380tcp_is_descheduled (tcp_connection_t * tc)
381{
382 return (transport_connection_is_descheduled (&tc->connection) ? 1 : 0);
383}
384
385/**
386 * Push TCP header to buffer
387 *
388 * @param vm - vlib_main
389 * @param b - buffer to write the header to
390 * @param sp_net - source port net order
391 * @param dp_net - destination port net order
392 * @param seq - sequence number net order
393 * @param ack - ack number net order
394 * @param tcp_hdr_opts_len - header and options length in bytes
395 * @param flags - header flags
396 * @param wnd - window size
397 *
398 * @return - pointer to start of TCP header
399 */
400always_inline void *
401vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
402 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
403 u16 wnd)
404{
405 tcp_header_t *th;
406
407 th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
408
409 th->src_port = sp;
410 th->dst_port = dp;
411 th->seq_number = seq;
412 th->ack_number = ack;
413 th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
414 th->flags = flags;
415 th->window = wnd;
416 th->checksum = 0;
417 th->urgent_pointer = 0;
418 vnet_buffer (b)->l4_hdr_offset = (u8 *) th - b->data;
419 b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
420 return th;
421}
422
423/**
424 * Push TCP header to buffer
425 *
426 * @param b - buffer to write the header to
427 * @param sp_net - source port net order
428 * @param dp_net - destination port net order
429 * @param seq - sequence number host order
430 * @param ack - ack number host order
431 * @param tcp_hdr_opts_len - header and options length in bytes
432 * @param flags - header flags
433 * @param wnd - window size
434 *
435 * @return - pointer to start of TCP header
436 */
437always_inline void *
438vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
439 u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
440{
441 return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
442 clib_host_to_net_u32 (seq),
443 clib_host_to_net_u32 (ack),
444 tcp_hdr_opts_len, flags,
445 clib_host_to_net_u16 (wnd));
446}
447
448#endif /* SRC_VNET_TCP_TCP_INLINES_H_ */
449
450/*
451 * fd.io coding-style-patch-verification: ON
452 *
453 * Local Variables:
454 * eval: (c-set-style "gnu")
455 * End:
456 */