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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef SRC_VNET_TCP_TCP_INLINES_H_
17 #define SRC_VNET_TCP_TCP_INLINES_H_
19 #include <vnet/tcp/tcp.h>
21 always_inline tcp_header_t *
22 tcp_buffer_hdr (vlib_buffer_t * b)
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);
29 always_inline tcp_connection_t *
30 tcp_connection_get (u32 conn_index, u32 thread_index)
32 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
33 if (PREDICT_FALSE (pool_is_free_index (wrk->connections, conn_index)))
35 return pool_elt_at_index (wrk->connections, conn_index);
38 always_inline tcp_connection_t *
39 tcp_connection_get_if_valid (u32 conn_index, u32 thread_index)
41 tcp_worker_ctx_t *wrk;
42 if (thread_index >= vec_len (tcp_main.wrk_ctx))
44 wrk = tcp_get_worker (thread_index);
45 if (pool_is_free_index (wrk->connections, conn_index))
47 return pool_elt_at_index (wrk->connections, conn_index);
51 tcp_connection_set_state (tcp_connection_t * tc, tcp_state_t state)
54 TCP_EVT (TCP_EVT_STATE_CHANGE, tc);
57 always_inline tcp_connection_t *
58 tcp_listener_get (u32 tli)
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);
66 always_inline tcp_connection_t *
67 tcp_half_open_connection_get (u32 conn_index)
69 tcp_connection_t *tc = 0;
70 if (!pool_is_free_index (tcp_main.half_open_connections, conn_index))
71 tc = pool_elt_at_index (tcp_main.half_open_connections, conn_index);
76 * Our estimate of the number of bytes that have left the network
79 tcp_bytes_out (const tcp_connection_t * tc)
81 if (tcp_opts_sack_permitted (&tc->rcv_opts))
82 return tc->sack_sb.sacked_bytes + tc->sack_sb.lost_bytes;
84 return clib_min (tc->rcv_dupacks * tc->snd_mss,
85 tc->snd_nxt - tc->snd_una);
89 * Our estimate of the number of bytes in flight (pipe size)
92 tcp_flight_size (const tcp_connection_t * tc)
96 flight_size = (int) (tc->snd_nxt - tc->snd_una) - tcp_bytes_out (tc)
97 + tc->snd_rxt_bytes - tc->rxt_delivered;
99 ASSERT (flight_size >= 0);
105 * Initial cwnd as per RFC5681
108 tcp_initial_cwnd (const tcp_connection_t * tc)
110 if (tcp_cfg.initial_cwnd_multiplier > 0)
111 return tcp_cfg.initial_cwnd_multiplier * tc->snd_mss;
113 if (tc->snd_mss > 2190)
114 return 2 * tc->snd_mss;
115 else if (tc->snd_mss > 1095)
116 return 3 * tc->snd_mss;
118 return 4 * tc->snd_mss;
122 * Accumulate acked bytes for cwnd increase
124 * Once threshold bytes are accumulated, snd_mss bytes are added
128 tcp_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes)
130 tc->cwnd_acc_bytes += bytes;
131 if (tc->cwnd_acc_bytes >= thresh)
133 u32 inc = tc->cwnd_acc_bytes / thresh;
134 tc->cwnd_acc_bytes -= inc * thresh;
135 tc->cwnd += inc * tc->snd_mss;
136 tc->cwnd = clib_min (tc->cwnd, tc->tx_fifo_size);
141 tcp_loss_wnd (const tcp_connection_t * tc)
143 /* Whatever we have in flight + the packet we're about to send */
144 return tcp_flight_size (tc) + tc->snd_mss;
148 tcp_available_snd_wnd (const tcp_connection_t * tc)
150 return clib_min (tc->cwnd, tc->snd_wnd);
154 tcp_available_output_snd_space (const tcp_connection_t * tc)
156 u32 available_wnd = tcp_available_snd_wnd (tc);
157 int flight_size = (int) (tc->snd_nxt - tc->snd_una);
159 if (available_wnd <= flight_size)
162 return available_wnd - flight_size;
166 * Estimate of how many bytes we can still push into the network
169 tcp_available_cc_snd_space (const tcp_connection_t * tc)
171 u32 available_wnd = tcp_available_snd_wnd (tc);
172 u32 flight_size = tcp_flight_size (tc);
174 if (available_wnd <= flight_size)
177 return available_wnd - flight_size;
181 tcp_is_lost_fin (tcp_connection_t * tc)
183 if ((tc->flags & TCP_CONN_FINSNT) && (tc->snd_nxt - tc->snd_una == 1))
189 * Time used to generate timestamps, not the timestamp
192 tcp_time_tstamp (u32 thread_index)
194 return tcp_main.wrk_ctx[thread_index].time_tstamp;
198 * Generate timestamp for tcp connection
201 tcp_tstamp (tcp_connection_t * tc)
203 return (tcp_main.wrk_ctx[tc->c_thread_index].time_tstamp -
204 tc->timestamp_delta);
208 tcp_time_now_us (u32 thread_index)
210 return tcp_main.wrk_ctx[thread_index].time_us;
214 tcp_set_time_now (tcp_worker_ctx_t *wrk, f64 now)
216 /* TCP internal cache of time reference. Could use @ref transport_time_now
217 * but because @ref tcp_time_now_us is used per packet, caching might
218 * slightly improve efficiency. */
220 wrk->time_tstamp = (u64) (now * TCP_TSTP_HZ);
224 tcp_update_time_now (tcp_worker_ctx_t *wrk)
226 f64 now = vlib_time_now (wrk->vm);
228 /* Both pacer and tcp us time need to be updated */
229 transport_update_pacer_time (wrk->vm->thread_index, now);
230 tcp_set_time_now (wrk, now);
233 always_inline tcp_connection_t *
234 tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
235 u8 is_ip4, u8 is_nolookup)
237 u32 fib_index = vnet_buffer (b)->ip.fib_index;
238 int n_advance_bytes, n_data_bytes;
239 transport_connection_t *tc;
245 ip4_header_t *ip4 = vlib_buffer_get_current (b);
246 int ip_hdr_bytes = ip4_header_bytes (ip4);
247 if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
249 *error = TCP_ERROR_LENGTH;
252 tcp = ip4_next_header (ip4);
253 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
254 n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
255 n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
257 /* Length check. Checksum computed by ipx_local no need to compute again */
258 if (PREDICT_FALSE (n_data_bytes < 0))
260 *error = TCP_ERROR_LENGTH;
265 tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
266 &ip4->src_address, tcp->dst_port,
268 TRANSPORT_PROTO_TCP, thread_index,
273 ip6_header_t *ip6 = vlib_buffer_get_current (b);
274 if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
276 *error = TCP_ERROR_LENGTH;
279 tcp = ip6_next_header (ip6);
280 vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
281 n_advance_bytes = tcp_header_bytes (tcp);
282 n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
284 n_advance_bytes += sizeof (ip6[0]);
286 if (PREDICT_FALSE (n_data_bytes < 0))
288 *error = TCP_ERROR_LENGTH;
295 (ip6_address_is_link_local_unicast (&ip6->dst_address)))
297 ip6_main_t *im = &ip6_main;
298 fib_index = vec_elt (im->fib_index_by_sw_if_index,
299 vnet_buffer (b)->sw_if_index[VLIB_RX]);
302 tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
304 tcp->dst_port, tcp->src_port,
306 thread_index, &result);
312 (transport_connection_t *) tcp_connection_get (vnet_buffer (b)->
313 tcp.connection_index,
316 vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
317 vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
318 vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
319 vnet_buffer (b)->tcp.data_len = n_data_bytes;
320 vnet_buffer (b)->tcp.seq_end = vnet_buffer (b)->tcp.seq_number
323 *error = result ? TCP_ERROR_NONE + result : *error;
325 return tcp_get_connection_from_transport (tc);
329 * Initialize connection by gleaning network and rcv params from buffer
331 * @param tc connection to initialize
332 * @param b buffer whose current data is pointing at ip
333 * @param is_ip4 flag set to 1 if using ip4
336 tcp_init_w_buffer (tcp_connection_t * tc, vlib_buffer_t * b, u8 is_ip4)
338 tcp_header_t *th = tcp_buffer_hdr (b);
340 tc->c_lcl_port = th->dst_port;
341 tc->c_rmt_port = th->src_port;
342 tc->c_is_ip4 = is_ip4;
346 ip4_header_t *ip4 = vlib_buffer_get_current (b);
347 tc->c_lcl_ip4.as_u32 = ip4->dst_address.as_u32;
348 tc->c_rmt_ip4.as_u32 = ip4->src_address.as_u32;
352 ip6_header_t *ip6 = vlib_buffer_get_current (b);
353 clib_memcpy_fast (&tc->c_lcl_ip6, &ip6->dst_address,
354 sizeof (ip6_address_t));
355 clib_memcpy_fast (&tc->c_rmt_ip6, &ip6->src_address,
356 sizeof (ip6_address_t));
359 tc->irs = vnet_buffer (b)->tcp.seq_number;
360 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_number + 1;
361 tc->rcv_las = tc->rcv_nxt;
362 tc->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
363 tc->snd_wl1 = vnet_buffer (b)->tcp.seq_number;
364 tc->snd_wl2 = vnet_buffer (b)->tcp.ack_number;
366 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
367 * segments are used to initialize PAWS. */
368 if (tcp_opts_tstamp (&tc->rcv_opts))
370 tc->tsval_recent = tc->rcv_opts.tsval;
371 tc->tsval_recent_age = tcp_time_tstamp (tc->c_thread_index);
374 if (tcp_opts_wscale (&tc->rcv_opts))
375 tc->snd_wscale = tc->rcv_opts.wscale;
377 tc->snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
381 tcp_update_rto (tcp_connection_t * tc)
383 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
384 tc->rto = clib_max (tc->rto, TCP_RTO_MIN);
388 tcp_is_descheduled (tcp_connection_t * tc)
390 return (transport_connection_is_descheduled (&tc->connection) ? 1 : 0);
394 * Push TCP header to buffer
396 * @param vm - vlib_main
397 * @param b - buffer to write the header to
398 * @param sp_net - source port net order
399 * @param dp_net - destination port net order
400 * @param seq - sequence number net order
401 * @param ack - ack number net order
402 * @param tcp_hdr_opts_len - header and options length in bytes
403 * @param flags - header flags
404 * @param wnd - window size
406 * @return - pointer to start of TCP header
409 vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
410 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
415 th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
419 th->seq_number = seq;
420 th->ack_number = ack;
421 th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
425 th->urgent_pointer = 0;
426 vnet_buffer (b)->l4_hdr_offset = (u8 *) th - b->data;
427 b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
432 * Push TCP header to buffer
434 * @param b - buffer to write the header to
435 * @param sp_net - source port net order
436 * @param dp_net - destination port net order
437 * @param seq - sequence number host order
438 * @param ack - ack number host order
439 * @param tcp_hdr_opts_len - header and options length in bytes
440 * @param flags - header flags
441 * @param wnd - window size
443 * @return - pointer to start of TCP header
446 vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
447 u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
449 return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
450 clib_host_to_net_u32 (seq),
451 clib_host_to_net_u32 (ack),
452 tcp_hdr_opts_len, flags,
453 clib_host_to_net_u16 (wnd));
456 #endif /* SRC_VNET_TCP_TCP_INLINES_H_ */
459 * fd.io coding-style-patch-verification: ON
462 * eval: (c-set-style "gnu")