blob: d57b30590668f2f52f10f9c183230a8ce72f769a [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{
Florin Coras57b2e4a2021-07-06 08:25:36 -070069 return tcp_connection_get (conn_index, 0);
Florin Coras999840c2020-03-18 20:31:34 +000070}
71
72/**
73 * Our estimate of the number of bytes that have left the network
74 */
75always_inline u32
76tcp_bytes_out (const tcp_connection_t * tc)
77{
78 if (tcp_opts_sack_permitted (&tc->rcv_opts))
79 return tc->sack_sb.sacked_bytes + tc->sack_sb.lost_bytes;
80 else
81 return clib_min (tc->rcv_dupacks * tc->snd_mss,
82 tc->snd_nxt - tc->snd_una);
83}
84
85/**
86 * Our estimate of the number of bytes in flight (pipe size)
87 */
88always_inline u32
89tcp_flight_size (const tcp_connection_t * tc)
90{
91 int flight_size;
92
93 flight_size = (int) (tc->snd_nxt - tc->snd_una) - tcp_bytes_out (tc)
94 + tc->snd_rxt_bytes - tc->rxt_delivered;
95
96 ASSERT (flight_size >= 0);
97
98 return flight_size;
99}
100
101/**
102 * Initial cwnd as per RFC5681
103 */
104always_inline u32
105tcp_initial_cwnd (const tcp_connection_t * tc)
106{
107 if (tcp_cfg.initial_cwnd_multiplier > 0)
108 return tcp_cfg.initial_cwnd_multiplier * tc->snd_mss;
109
110 if (tc->snd_mss > 2190)
111 return 2 * tc->snd_mss;
112 else if (tc->snd_mss > 1095)
113 return 3 * tc->snd_mss;
114 else
115 return 4 * tc->snd_mss;
116}
117
118/*
119 * Accumulate acked bytes for cwnd increase
120 *
121 * Once threshold bytes are accumulated, snd_mss bytes are added
122 * to the cwnd.
123 */
124always_inline void
125tcp_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes)
126{
127 tc->cwnd_acc_bytes += bytes;
128 if (tc->cwnd_acc_bytes >= thresh)
129 {
130 u32 inc = tc->cwnd_acc_bytes / thresh;
131 tc->cwnd_acc_bytes -= inc * thresh;
132 tc->cwnd += inc * tc->snd_mss;
133 tc->cwnd = clib_min (tc->cwnd, tc->tx_fifo_size);
134 }
135}
136
137always_inline u32
138tcp_loss_wnd (const tcp_connection_t * tc)
139{
140 /* Whatever we have in flight + the packet we're about to send */
141 return tcp_flight_size (tc) + tc->snd_mss;
142}
143
144always_inline u32
145tcp_available_snd_wnd (const tcp_connection_t * tc)
146{
147 return clib_min (tc->cwnd, tc->snd_wnd);
148}
149
150always_inline u32
151tcp_available_output_snd_space (const tcp_connection_t * tc)
152{
153 u32 available_wnd = tcp_available_snd_wnd (tc);
154 int flight_size = (int) (tc->snd_nxt - tc->snd_una);
155
156 if (available_wnd <= flight_size)
157 return 0;
158
159 return available_wnd - flight_size;
160}
161
162/**
163 * Estimate of how many bytes we can still push into the network
164 */
165always_inline u32
166tcp_available_cc_snd_space (const tcp_connection_t * tc)
167{
168 u32 available_wnd = tcp_available_snd_wnd (tc);
169 u32 flight_size = tcp_flight_size (tc);
170
171 if (available_wnd <= flight_size)
172 return 0;
173
174 return available_wnd - flight_size;
175}
176
177always_inline u8
178tcp_is_lost_fin (tcp_connection_t * tc)
179{
Florin Coras55e556c2020-10-23 10:45:48 -0700180 if ((tc->flags & TCP_CONN_FINSNT) && (tc->snd_nxt - tc->snd_una == 1))
Florin Coras999840c2020-03-18 20:31:34 +0000181 return 1;
182 return 0;
183}
184
Florin Coras8f10b902021-04-02 18:32:00 -0700185/**
186 * Time used to generate timestamps, not the timestamp
187 */
Florin Coras999840c2020-03-18 20:31:34 +0000188always_inline u32
Florin Coras8f10b902021-04-02 18:32:00 -0700189tcp_time_tstamp (u32 thread_index)
Florin Coras999840c2020-03-18 20:31:34 +0000190{
Florin Coras8f10b902021-04-02 18:32:00 -0700191 return tcp_main.wrk_ctx[thread_index].time_tstamp;
Florin Coras999840c2020-03-18 20:31:34 +0000192}
193
194/**
195 * Generate timestamp for tcp connection
196 */
197always_inline u32
198tcp_tstamp (tcp_connection_t * tc)
199{
Florin Coras8f10b902021-04-02 18:32:00 -0700200 return (tcp_main.wrk_ctx[tc->c_thread_index].time_tstamp -
Florin Coras999840c2020-03-18 20:31:34 +0000201 tc->timestamp_delta);
202}
203
204always_inline f64
205tcp_time_now_us (u32 thread_index)
206{
Florin Coras8f10b902021-04-02 18:32:00 -0700207 return tcp_main.wrk_ctx[thread_index].time_us;
Florin Coras999840c2020-03-18 20:31:34 +0000208}
209
Florin Coras8f10b902021-04-02 18:32:00 -0700210always_inline void
211tcp_set_time_now (tcp_worker_ctx_t *wrk, f64 now)
Florin Coras999840c2020-03-18 20:31:34 +0000212{
Florin Coras8f10b902021-04-02 18:32:00 -0700213 /* TCP internal cache of time reference. Could use @ref transport_time_now
214 * but because @ref tcp_time_now_us is used per packet, caching might
215 * slightly improve efficiency. */
216 wrk->time_us = now;
217 wrk->time_tstamp = (u64) (now * TCP_TSTP_HZ);
218}
219
220always_inline void
221tcp_update_time_now (tcp_worker_ctx_t *wrk)
222{
223 f64 now = vlib_time_now (wrk->vm);
224
225 /* Both pacer and tcp us time need to be updated */
226 transport_update_pacer_time (wrk->vm->thread_index, now);
227 tcp_set_time_now (wrk, now);
Florin Coras999840c2020-03-18 20:31:34 +0000228}
229
230always_inline tcp_connection_t *
231tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
232 u8 is_ip4, u8 is_nolookup)
233{
234 u32 fib_index = vnet_buffer (b)->ip.fib_index;
235 int n_advance_bytes, n_data_bytes;
236 transport_connection_t *tc;
237 tcp_header_t *tcp;
238 u8 result = 0;
239
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +0200240 /* Set the sw_if_index[VLIB_RX] to the interface we received
241 * the connection on (the local interface) */
Florin Coras904638f2021-11-10 07:39:51 -0800242 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->ip.rx_sw_if_index;
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +0200243
Florin Coras999840c2020-03-18 20:31:34 +0000244 if (is_ip4)
245 {
246 ip4_header_t *ip4 = vlib_buffer_get_current (b);
247 int ip_hdr_bytes = ip4_header_bytes (ip4);
248 if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
249 {
250 *error = TCP_ERROR_LENGTH;
251 return 0;
252 }
253 tcp = ip4_next_header (ip4);
254 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
255 n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
256 n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
257
258 /* Length check. Checksum computed by ipx_local no need to compute again */
259 if (PREDICT_FALSE (n_data_bytes < 0))
260 {
261 *error = TCP_ERROR_LENGTH;
262 return 0;
263 }
264
265 if (!is_nolookup)
266 tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
267 &ip4->src_address, tcp->dst_port,
268 tcp->src_port,
269 TRANSPORT_PROTO_TCP, thread_index,
270 &result);
271 }
272 else
273 {
274 ip6_header_t *ip6 = vlib_buffer_get_current (b);
275 if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
276 {
277 *error = TCP_ERROR_LENGTH;
278 return 0;
279 }
280 tcp = ip6_next_header (ip6);
281 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
282 n_advance_bytes = tcp_header_bytes (tcp);
283 n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
284 - n_advance_bytes;
285 n_advance_bytes += sizeof (ip6[0]);
286
287 if (PREDICT_FALSE (n_data_bytes < 0))
288 {
289 *error = TCP_ERROR_LENGTH;
290 return 0;
291 }
292
293 if (!is_nolookup)
294 {
295 if (PREDICT_FALSE
296 (ip6_address_is_link_local_unicast (&ip6->dst_address)))
297 {
Florin Coras1a18d112020-04-21 19:55:39 +0000298 ip6_main_t *im = &ip6_main;
Florin Coras999840c2020-03-18 20:31:34 +0000299 fib_index = vec_elt (im->fib_index_by_sw_if_index,
300 vnet_buffer (b)->sw_if_index[VLIB_RX]);
301 }
302
303 tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
304 &ip6->src_address,
305 tcp->dst_port, tcp->src_port,
306 TRANSPORT_PROTO_TCP,
307 thread_index, &result);
308 }
309 }
310
311 if (is_nolookup)
312 tc =
313 (transport_connection_t *) tcp_connection_get (vnet_buffer (b)->
314 tcp.connection_index,
315 thread_index);
316
317 vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
318 vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
319 vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
320 vnet_buffer (b)->tcp.data_len = n_data_bytes;
321 vnet_buffer (b)->tcp.seq_end = vnet_buffer (b)->tcp.seq_number
322 + n_data_bytes;
Florin Coras999840c2020-03-18 20:31:34 +0000323
324 *error = result ? TCP_ERROR_NONE + result : *error;
325
326 return tcp_get_connection_from_transport (tc);
327}
328
329/**
330 * Initialize connection by gleaning network and rcv params from buffer
331 *
332 * @param tc connection to initialize
333 * @param b buffer whose current data is pointing at ip
334 * @param is_ip4 flag set to 1 if using ip4
335 */
336always_inline void
337tcp_init_w_buffer (tcp_connection_t * tc, vlib_buffer_t * b, u8 is_ip4)
338{
339 tcp_header_t *th = tcp_buffer_hdr (b);
340
341 tc->c_lcl_port = th->dst_port;
342 tc->c_rmt_port = th->src_port;
343 tc->c_is_ip4 = is_ip4;
344
345 if (is_ip4)
346 {
347 ip4_header_t *ip4 = vlib_buffer_get_current (b);
348 tc->c_lcl_ip4.as_u32 = ip4->dst_address.as_u32;
349 tc->c_rmt_ip4.as_u32 = ip4->src_address.as_u32;
350 }
351 else
352 {
353 ip6_header_t *ip6 = vlib_buffer_get_current (b);
354 clib_memcpy_fast (&tc->c_lcl_ip6, &ip6->dst_address,
355 sizeof (ip6_address_t));
356 clib_memcpy_fast (&tc->c_rmt_ip6, &ip6->src_address,
357 sizeof (ip6_address_t));
358 }
359
360 tc->irs = vnet_buffer (b)->tcp.seq_number;
361 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_number + 1;
362 tc->rcv_las = tc->rcv_nxt;
363 tc->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
364 tc->snd_wl1 = vnet_buffer (b)->tcp.seq_number;
365 tc->snd_wl2 = vnet_buffer (b)->tcp.ack_number;
366
367 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
368 * segments are used to initialize PAWS. */
369 if (tcp_opts_tstamp (&tc->rcv_opts))
370 {
371 tc->tsval_recent = tc->rcv_opts.tsval;
Florin Coras8f10b902021-04-02 18:32:00 -0700372 tc->tsval_recent_age = tcp_time_tstamp (tc->c_thread_index);
Florin Coras999840c2020-03-18 20:31:34 +0000373 }
374
375 if (tcp_opts_wscale (&tc->rcv_opts))
376 tc->snd_wscale = tc->rcv_opts.wscale;
377
378 tc->snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
379}
380
381always_inline void
382tcp_update_rto (tcp_connection_t * tc)
383{
384 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
385 tc->rto = clib_max (tc->rto, TCP_RTO_MIN);
386}
387
388always_inline u8
389tcp_is_descheduled (tcp_connection_t * tc)
390{
391 return (transport_connection_is_descheduled (&tc->connection) ? 1 : 0);
392}
393
394/**
395 * Push TCP header to buffer
396 *
397 * @param vm - vlib_main
398 * @param b - buffer to write the header to
399 * @param sp_net - source port net order
400 * @param dp_net - destination port net order
401 * @param seq - sequence number net order
402 * @param ack - ack number net order
403 * @param tcp_hdr_opts_len - header and options length in bytes
404 * @param flags - header flags
405 * @param wnd - window size
406 *
407 * @return - pointer to start of TCP header
408 */
409always_inline void *
410vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
411 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
412 u16 wnd)
413{
414 tcp_header_t *th;
415
416 th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
417
418 th->src_port = sp;
419 th->dst_port = dp;
420 th->seq_number = seq;
421 th->ack_number = ack;
422 th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
423 th->flags = flags;
424 th->window = wnd;
425 th->checksum = 0;
426 th->urgent_pointer = 0;
427 vnet_buffer (b)->l4_hdr_offset = (u8 *) th - b->data;
428 b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
429 return th;
430}
431
432/**
433 * Push TCP header to buffer
434 *
435 * @param b - buffer to write the header to
436 * @param sp_net - source port net order
437 * @param dp_net - destination port net order
438 * @param seq - sequence number host order
439 * @param ack - ack number host order
440 * @param tcp_hdr_opts_len - header and options length in bytes
441 * @param flags - header flags
442 * @param wnd - window size
443 *
444 * @return - pointer to start of TCP header
445 */
446always_inline void *
447vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
448 u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
449{
450 return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
451 clib_host_to_net_u32 (seq),
452 clib_host_to_net_u32 (ack),
453 tcp_hdr_opts_len, flags,
454 clib_host_to_net_u16 (wnd));
455}
456
457#endif /* SRC_VNET_TCP_TCP_INLINES_H_ */
458
459/*
460 * fd.io coding-style-patch-verification: ON
461 *
462 * Local Variables:
463 * eval: (c-set-style "gnu")
464 * End:
465 */