2 * Copyright (c) 2016-2019 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.
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/session/session.h>
22 #include <vnet/tcp/tcp_types.h>
23 #include <vnet/tcp/tcp_timer.h>
24 #include <vnet/tcp/tcp_debug.h>
25 #include <vnet/tcp/tcp_sack.h>
26 #include <vnet/tcp/tcp_bt.h>
27 #include <vnet/tcp/tcp_cc.h>
29 typedef void (timer_expiration_handler) (tcp_connection_t * tc);
31 extern timer_expiration_handler tcp_timer_delack_handler;
32 extern timer_expiration_handler tcp_timer_retransmit_handler;
33 extern timer_expiration_handler tcp_timer_persist_handler;
34 extern timer_expiration_handler tcp_timer_retransmit_syn_handler;
36 typedef enum _tcp_error
38 #define tcp_error(n,s) TCP_ERROR_##n,
39 #include <vnet/tcp/tcp_error.def>
44 typedef struct _tcp_lookup_dispatch
47 } tcp_lookup_dispatch_t;
49 #define foreach_tcp_wrk_stat \
50 _(timer_expirations, u64, "timer expirations") \
51 _(rxt_segs, u64, "segments retransmitted") \
52 _(tr_events, u32, "timer retransmit events") \
53 _(to_closewait, u32, "timeout close-wait") \
54 _(to_closewait2, u32, "timeout close-wait w/data") \
55 _(to_finwait1, u32, "timeout fin-wait-1") \
56 _(to_finwait2, u32, "timeout fin-wait-2") \
57 _(to_lastack, u32, "timeout last-ack") \
58 _(to_closing, u32, "timeout closing") \
59 _(tr_abort, u32, "timer retransmit abort") \
60 _(rst_unread, u32, "reset on close due to unread data") \
62 typedef struct tcp_wrk_stats_
64 #define _(name, type, str) type name;
69 typedef struct tcp_free_req_
71 clib_time_type_t free_time;
75 typedef struct tcp_worker_ctx_
77 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
79 /** worker's pool of connections */
80 tcp_connection_t *connections;
82 /** vector of pending ack dequeues */
83 u32 *pending_deq_acked;
85 /** vector of pending disconnect notifications */
86 u32 *pending_disconnects;
88 /** vector of pending reset notifications */
91 /** convenience pointer to this thread's vlib main */
97 /* Max timers to be handled per dispatch loop */
98 u32 max_timers_per_loop;
100 /** Session layer edge indices to tcp output */
101 u32 tco_next_node[2];
103 /* Fifo of pending timer expirations */
106 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
108 /** cached 'on the wire' options for bursts */
111 /** tx buffer free list */
114 /* fifo of pending free requests */
115 tcp_cleanup_req_t *pending_cleanups;
117 /** worker timer wheel */
118 tcp_timer_wheel_t timer_wheel;
120 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
122 tcp_wrk_stats_t stats;
125 #define tcp_worker_stats_inc(_wrk,_stat,_val) \
126 _wrk->stats._stat += _val
128 typedef struct tcp_iss_seed_
134 typedef struct tcp_configuration_
136 /** Max rx fifo size for a session (in bytes). It is used in to compute the
137 * rfc 7323 window scaling factor */
140 /** Min rx fifo for a session (in bytes) */
143 /** Default MTU to be used when establishing connections */
146 /** Initial CWND multiplier, which multiplies MSS to determine initial CWND.
147 * Set 0 to determine the initial CWND by another way */
148 u16 initial_cwnd_multiplier;
150 /** Enable tx pacing for new connections */
153 /** Allow use of TSO whenever available */
156 /** Set if csum offloading is enabled */
159 /** Default congestion control algorithm type */
160 tcp_cc_algorithm_type_e cc_algo;
162 /** Min rwnd, as number of snd_mss segments, for update ack to be sent after
163 * a zero rwnd advertisement */
164 u32 rwnd_min_update_ack;
166 /** Delayed ack time (disabled) */
169 /** Timer ticks to wait for close from app */
172 /** Timer ticks to wait in time-wait. Also known as 2MSL */
175 /** Timer ticks to wait in fin-wait1 to send fin and rcv fin-ack */
178 /** Timer ticks to wait in last ack for ack */
181 /** Timer ticks to wait in fin-wait2 for fin */
184 /** Timer ticks to wait in closing for fin ack */
187 /** Time to wait (sec) before cleaning up the connection */
190 /** Number of preallocated connections */
191 u32 preallocated_connections;
193 /** Number of preallocated half-open connections */
194 u32 preallocated_half_open_connections;
196 /** Vectors of src addresses. Optional unless one needs > 63K active-opens */
197 ip4_address_t *ip4_src_addrs;
198 ip6_address_t *ip6_src_addrs;
200 /** Fault-injection. Debug only */
201 f64 buffer_fail_fraction;
202 } tcp_configuration_t;
204 typedef struct _tcp_main
206 /** per-worker context */
207 tcp_worker_ctx_t *wrk_ctx;
209 /* Pool of listeners. */
210 tcp_connection_t *listener_pool;
212 f64 tstamp_ticks_per_clock;
214 /** vlib buffer size */
215 u32 bytes_per_buffer;
217 /** Session layer edge indices to ip lookup (syns, rst) */
218 u32 ipl_next_node[2];
220 /** Dispatch table by state and flags */
221 tcp_lookup_dispatch_t dispatch_table[TCP_N_STATES][64];
223 clib_spinlock_t half_open_lock;
225 /** Pool of half-open connections on which we've sent a SYN */
226 tcp_connection_t *half_open_connections;
228 /** Seed used to generate random iss */
229 tcp_iss_seed_t iss_seed;
231 /** Congestion control algorithms registered */
232 tcp_cc_algorithm_t *cc_algos;
234 /** Hash table of cc algorithms by name */
235 uword *cc_algo_by_name;
237 /** Last cc algo registered */
238 tcp_cc_algorithm_type_e cc_last_type;
240 /** Flag that indicates if stack is on or off */
243 /** Flag that indicates if v4 punting is enabled */
246 /** Flag that indicates if v6 punting is enabled */
249 /** Rotor for v4 source addresses */
250 u32 last_v4_addr_rotor;
252 /** Rotor for v6 source addresses */
253 u32 last_v6_addr_rotor;
255 /** Protocol configuration */
256 tcp_configuration_t cfg;
259 extern tcp_main_t tcp_main;
260 extern vlib_node_registration_t tcp4_input_node;
261 extern vlib_node_registration_t tcp6_input_node;
262 extern vlib_node_registration_t tcp4_output_node;
263 extern vlib_node_registration_t tcp6_output_node;
264 extern vlib_node_registration_t tcp4_established_node;
265 extern vlib_node_registration_t tcp6_established_node;
266 extern vlib_node_registration_t tcp4_syn_sent_node;
267 extern vlib_node_registration_t tcp6_syn_sent_node;
268 extern vlib_node_registration_t tcp4_rcv_process_node;
269 extern vlib_node_registration_t tcp6_rcv_process_node;
270 extern vlib_node_registration_t tcp4_listen_node;
271 extern vlib_node_registration_t tcp6_listen_node;
273 #define tcp_cfg tcp_main.cfg
274 #define tcp_node_index(node_id, is_ip4) \
275 ((is_ip4) ? tcp4_##node_id##_node.index : tcp6_##node_id##_node.index)
277 always_inline tcp_main_t *
283 always_inline tcp_worker_ctx_t *
284 tcp_get_worker (u32 thread_index)
286 ASSERT (thread_index < vec_len (tcp_main.wrk_ctx));
287 return &tcp_main.wrk_ctx[thread_index];
290 #if (VLIB_BUFFER_TRACE_TRAJECTORY)
291 #define tcp_trajectory_add_start(b, start) \
293 (*vlib_buffer_trace_trajectory_cb) (b, start); \
296 #define tcp_trajectory_add_start(b, start)
299 tcp_connection_t *tcp_connection_alloc (u8 thread_index);
300 tcp_connection_t *tcp_connection_alloc_w_base (u8 thread_index,
301 tcp_connection_t * base);
302 void tcp_connection_free (tcp_connection_t * tc);
303 void tcp_connection_close (tcp_connection_t * tc);
304 void tcp_connection_cleanup (tcp_connection_t * tc);
305 void tcp_connection_del (tcp_connection_t * tc);
306 int tcp_half_open_connection_cleanup (tcp_connection_t * tc);
308 void tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt,
309 u32 thread_index, u8 is_ip4);
310 void tcp_send_reset (tcp_connection_t * tc);
311 void tcp_send_syn (tcp_connection_t * tc);
312 void tcp_send_synack (tcp_connection_t * tc);
313 void tcp_send_fin (tcp_connection_t * tc);
314 void tcp_send_ack (tcp_connection_t * tc);
315 void tcp_send_window_update_ack (tcp_connection_t * tc);
317 void tcp_program_ack (tcp_connection_t * tc);
318 void tcp_program_dupack (tcp_connection_t * tc);
319 void tcp_program_retransmit (tcp_connection_t * tc);
321 void tcp_update_burst_snd_vars (tcp_connection_t * tc);
322 u32 tcp_snd_space (tcp_connection_t * tc);
323 int tcp_fastrecovery_prr_snd_space (tcp_connection_t * tc);
324 void tcp_reschedule (tcp_connection_t * tc);
325 fib_node_index_t tcp_lookup_rmt_in_fib (tcp_connection_t * tc);
326 u32 tcp_session_push_header (transport_connection_t * tconn,
328 int tcp_session_custom_tx (void *conn, u32 max_burst_size);
330 void tcp_connection_timers_init (tcp_connection_t * tc);
331 void tcp_connection_timers_reset (tcp_connection_t * tc);
332 void tcp_init_snd_vars (tcp_connection_t * tc);
333 void tcp_connection_init_vars (tcp_connection_t * tc);
334 void tcp_connection_tx_pacer_update (tcp_connection_t * tc);
335 void tcp_connection_tx_pacer_reset (tcp_connection_t * tc, u32 window,
337 void tcp_program_cleanup (tcp_worker_ctx_t * wrk, tcp_connection_t * tc);
339 void tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
340 int tcp_configure_v4_source_address_range (vlib_main_t * vm,
341 ip4_address_t * start,
342 ip4_address_t * end, u32 table_id);
343 int tcp_configure_v6_source_address_range (vlib_main_t * vm,
344 ip6_address_t * start,
345 ip6_address_t * end, u32 table_id);
347 clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en);
349 format_function_t format_tcp_state;
350 format_function_t format_tcp_flags;
351 format_function_t format_tcp_sacks;
352 format_function_t format_tcp_rcv_sacks;
353 format_function_t format_tcp_connection;
354 format_function_t format_tcp_connection_id;
356 #define tcp_validate_txf_size(_tc, _a) \
357 ASSERT(_tc->state != TCP_STATE_ESTABLISHED \
358 || transport_max_tx_dequeue (&_tc->connection) >= _a)
360 #endif /* _vnet_tcp_h_ */
363 * fd.io coding-style-patch-verification: ON
366 * eval: (c-set-style "gnu")