blob: e26488342d2bff4d49ea119d5f5f7e150f8dfd27 [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#include <vnet/tcp/tcp.h>
17#include <vnet/tcp/tcp_inlines.h>
18#include <vnet/dpo/receive_dpo.h>
19#include <vnet/ip-neighbor/ip_neighbor.h>
20
21const char *tcp_fsm_states[] = {
22#define _(sym, str) str,
23 foreach_tcp_fsm_state
24#undef _
25};
26
27u8 *
28format_tcp_state (u8 * s, va_list * args)
29{
30 u32 state = va_arg (*args, u32);
31
32 if (state < TCP_N_STATES)
33 s = format (s, "%s", tcp_fsm_states[state]);
34 else
35 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
36 return s;
37}
38
39const char *tcp_cfg_flags_str[] = {
40#define _(sym, str) str,
41 foreach_tcp_cfg_flag
42#undef _
43};
44
45static u8 *
46format_tcp_cfg_flags (u8 * s, va_list * args)
47{
48 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
49 int i, last = -1;
50
51 for (i = 0; i < TCP_CFG_N_FLAG_BITS; i++)
52 if (tc->cfg_flags & (1 << i))
53 last = i;
54 for (i = 0; i < last; i++)
55 {
56 if (tc->cfg_flags & (1 << i))
57 s = format (s, "%s, ", tcp_cfg_flags_str[i]);
58 }
59 if (last >= 0)
60 s = format (s, "%s", tcp_cfg_flags_str[last]);
61 return s;
62}
63
64const char *tcp_connection_flags_str[] = {
65#define _(sym, str) str,
66 foreach_tcp_connection_flag
67#undef _
68};
69
70static u8 *
71format_tcp_connection_flags (u8 * s, va_list * args)
72{
73 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
74 int i, last = -1;
75
76 for (i = 0; i < TCP_CONN_N_FLAG_BITS; i++)
77 if (tc->flags & (1 << i))
78 last = i;
79 for (i = 0; i < last; i++)
80 {
81 if (tc->flags & (1 << i))
82 s = format (s, "%s, ", tcp_connection_flags_str[i]);
83 }
84 if (last >= 0)
85 s = format (s, "%s", tcp_connection_flags_str[last]);
86 return s;
87}
88
89const char *tcp_conn_timers[] = {
90#define _(sym, str) str,
91 foreach_tcp_timer
92#undef _
93};
94
95static u8 *
96format_tcp_timers (u8 * s, va_list * args)
97{
98 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
99 int i, last = -1;
100
101 for (i = 0; i < TCP_N_TIMERS; i++)
102 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
103 last = i;
104
105 for (i = 0; i < last; i++)
106 {
107 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
108 s = format (s, "%s,", tcp_conn_timers[i]);
109 }
110
111 if (last >= 0)
112 s = format (s, "%s", tcp_conn_timers[i]);
113
114 return s;
115}
116
117static u8 *
118format_tcp_congestion_status (u8 * s, va_list * args)
119{
120 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
121 if (tcp_in_recovery (tc))
122 s = format (s, "recovery");
123 else if (tcp_in_fastrecovery (tc))
124 s = format (s, "fastrecovery");
125 else
126 s = format (s, "none");
127 return s;
128}
129
130static i32
131tcp_rcv_wnd_available (tcp_connection_t * tc)
132{
133 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
134}
135
136static u8 *
137format_tcp_congestion (u8 * s, va_list * args)
138{
139 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
140 u32 indent = format_get_indent (s), prr_space = 0;
141
142 s = format (s, "%U ", format_tcp_congestion_status, tc);
143 s = format (s, "algo %s cwnd %u ssthresh %u bytes_acked %u\n",
144 tc->cc_algo->name, tc->cwnd, tc->ssthresh, tc->bytes_acked);
145 s = format (s, "%Ucc space %u prev_cwnd %u prev_ssthresh %u\n",
146 format_white_space, indent, tcp_available_cc_snd_space (tc),
147 tc->prev_cwnd, tc->prev_ssthresh);
148 s = format (s, "%Usnd_cong %u dupack %u limited_tx %u\n",
149 format_white_space, indent, tc->snd_congestion - tc->iss,
150 tc->rcv_dupacks, tc->limited_transmit - tc->iss);
151 s = format (s, "%Urxt_bytes %u rxt_delivered %u rxt_head %u rxt_ts %u\n",
Florin Coras8f10b902021-04-02 18:32:00 -0700152 format_white_space, indent, tc->snd_rxt_bytes, tc->rxt_delivered,
153 tc->rxt_head - tc->iss, tcp_tstamp (tc) - tc->snd_rxt_ts);
Florin Coras999840c2020-03-18 20:31:34 +0000154 if (tcp_in_fastrecovery (tc))
155 prr_space = tcp_fastrecovery_prr_snd_space (tc);
156 s = format (s, "%Uprr_start %u prr_delivered %u prr space %u\n",
157 format_white_space, indent, tc->prr_start - tc->iss,
158 tc->prr_delivered, prr_space);
159 return s;
160}
161
162static u8 *
163format_tcp_stats (u8 * s, va_list * args)
164{
165 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
166 u32 indent = format_get_indent (s);
167 s = format (s, "in segs %lu dsegs %lu bytes %lu dupacks %u\n",
168 tc->segs_in, tc->data_segs_in, tc->bytes_in, tc->dupacks_in);
169 s = format (s, "%Uout segs %lu dsegs %lu bytes %lu dupacks %u\n",
170 format_white_space, indent, tc->segs_out,
171 tc->data_segs_out, tc->bytes_out, tc->dupacks_out);
172 s = format (s, "%Ufr %u tr %u rxt segs %lu bytes %lu duration %.3f\n",
173 format_white_space, indent, tc->fr_occurences,
174 tc->tr_occurences, tc->segs_retrans, tc->bytes_retrans,
175 tcp_time_now_us (tc->c_thread_index) - tc->start_ts);
176 s = format (s, "%Uerr wnd data below %u above %u ack below %u above %u",
177 format_white_space, indent, tc->errors.below_data_wnd,
178 tc->errors.above_data_wnd, tc->errors.below_ack_wnd,
179 tc->errors.above_ack_wnd);
180 return s;
181}
182
183static u8 *
184format_tcp_vars (u8 * s, va_list * args)
185{
186 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
187 s = format (s, " index: %u cfg: %U flags: %U timers: %U\n", tc->c_c_index,
188 format_tcp_cfg_flags, tc, format_tcp_connection_flags, tc,
189 format_tcp_timers, tc);
Florin Coras55e556c2020-10-23 10:45:48 -0700190 s = format (s, " snd_una %u snd_nxt %u", tc->snd_una - tc->iss,
191 tc->snd_nxt - tc->iss);
Florin Coras999840c2020-03-18 20:31:34 +0000192 s = format (s, " rcv_nxt %u rcv_las %u\n",
193 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
194 s = format (s, " snd_wnd %u rcv_wnd %u rcv_wscale %u ",
195 tc->snd_wnd, tc->rcv_wnd, tc->rcv_wscale);
196 s = format (s, "snd_wl1 %u snd_wl2 %u\n", tc->snd_wl1 - tc->irs,
197 tc->snd_wl2 - tc->iss);
198 s = format (s, " flight size %u out space %u rcv_wnd_av %u",
199 tcp_flight_size (tc), tcp_available_output_snd_space (tc),
200 tcp_rcv_wnd_available (tc));
201 s = format (s, " tsval_recent %u\n", tc->tsval_recent);
202 s = format (s, " tsecr %u tsecr_last_ack %u tsval_recent_age %u",
203 tc->rcv_opts.tsecr, tc->tsecr_last_ack,
Florin Coras8f10b902021-04-02 18:32:00 -0700204 tcp_time_tstamp (tc->c_thread_index) - tc->tsval_recent_age);
Florin Coras999840c2020-03-18 20:31:34 +0000205 s = format (s, " snd_mss %u\n", tc->snd_mss);
Florin Coraseedc74b2020-07-31 12:32:40 -0700206 s = format (s, " rto %u rto_boff %u srtt %.1f us %.3f rttvar %.1f",
207 tc->rto / 1000, tc->rto_boff, tc->srtt / 1000.0,
208 tc->mrtt_us * 1e3, tc->rttvar / 1000.0);
209 s = format (s, " rtt_ts %.4f rtt_seq %u\n", tc->rtt_ts,
210 tc->rtt_seq - tc->iss);
Florin Coras596c45b2021-09-09 12:04:17 -0700211 s = format (s, " next_node %u opaque 0x%x fib_index %u sw_if_index %d\n",
212 tc->next_node_index, tc->next_node_opaque, tc->c_fib_index,
213 tc->sw_if_index);
Florin Coras999840c2020-03-18 20:31:34 +0000214 s = format (s, " cong: %U", format_tcp_congestion, tc);
215
216 if (tc->state >= TCP_STATE_ESTABLISHED)
217 {
218 s = format (s, " sboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
219 tc);
220 s = format (s, " stats: %U\n", format_tcp_stats, tc);
221 }
222 if (vec_len (tc->snd_sacks))
223 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
224
225 return s;
226}
227
228u8 *
229format_tcp_connection_id (u8 * s, va_list * args)
230{
231 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
232 if (!tc)
233 return s;
234 if (tc->c_is_ip4)
235 {
236 s = format (s, "[%d:%d][%s] %U:%d->%U:%d", tc->c_thread_index,
237 tc->c_s_index, "T", format_ip4_address, &tc->c_lcl_ip4,
238 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
239 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
240 }
241 else
242 {
243 s = format (s, "[%d:%d][%s] %U:%d->%U:%d", tc->c_thread_index,
244 tc->c_s_index, "T", format_ip6_address, &tc->c_lcl_ip6,
245 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
246 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
247 }
248
249 return s;
250}
251
252u8 *
253format_tcp_connection (u8 * s, va_list * args)
254{
255 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
256 u32 verbose = va_arg (*args, u32);
257
258 if (!tc)
259 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -0700260 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tcp_connection_id, tc);
Florin Coras999840c2020-03-18 20:31:34 +0000261 if (verbose)
262 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700263 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tcp_state,
264 tc->state);
Florin Coras999840c2020-03-18 20:31:34 +0000265 if (verbose > 1)
266 s = format (s, "\n%U", format_tcp_vars, tc);
267 }
268
269 return s;
270}
271
272u8 *
273format_tcp_sacks (u8 * s, va_list * args)
274{
275 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
276 sack_block_t *sacks = tc->snd_sacks;
277 sack_block_t *block;
278 int i, len = 0;
279
280 len = vec_len (sacks);
281 for (i = 0; i < len - 1; i++)
282 {
283 block = &sacks[i];
284 s = format (s, " start %u end %u\n", block->start - tc->irs,
285 block->end - tc->irs);
286 }
287 if (len)
288 {
289 block = &sacks[len - 1];
290 s = format (s, " start %u end %u", block->start - tc->irs,
291 block->end - tc->irs);
292 }
293 return s;
294}
295
296u8 *
297format_tcp_rcv_sacks (u8 * s, va_list * args)
298{
299 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
300 sack_block_t *sacks = tc->rcv_opts.sacks;
301 sack_block_t *block;
302 int i, len = 0;
303
304 len = vec_len (sacks);
305 for (i = 0; i < len - 1; i++)
306 {
307 block = &sacks[i];
308 s = format (s, " start %u end %u\n", block->start - tc->iss,
309 block->end - tc->iss);
310 }
311 if (len)
312 {
313 block = &sacks[len - 1];
314 s = format (s, " start %u end %u", block->start - tc->iss,
315 block->end - tc->iss);
316 }
317 return s;
318}
319
320static u8 *
321format_tcp_sack_hole (u8 * s, va_list * args)
322{
323 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
324 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
325 if (tc)
326 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
327 else
328 s = format (s, " [%u, %u]", hole->start, hole->end);
329 return s;
330}
331
332u8 *
333format_tcp_scoreboard (u8 * s, va_list * args)
334{
335 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
336 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
337 sack_scoreboard_hole_t *hole;
338 u32 indent = format_get_indent (s);
339
340 s = format (s, "sacked %u last_sacked %u lost %u last_lost %u"
341 " rxt_sacked %u\n",
342 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes,
343 sb->last_lost_bytes, sb->rxt_sacked);
Florin Corascc4d6d02020-07-29 23:03:39 -0700344 s = format (s, "%Ulast_delivered %u high_sacked %u is_reneging %u",
Florin Coras999840c2020-03-18 20:31:34 +0000345 format_white_space, indent, sb->last_bytes_delivered,
346 sb->high_sacked - tc->iss, sb->is_reneging);
Florin Corascc4d6d02020-07-29 23:03:39 -0700347 s = format (s, " reorder %u\n", sb->reorder);
Florin Coras999840c2020-03-18 20:31:34 +0000348 s = format (s, "%Ucur_rxt_hole %u high_rxt %u rescue_rxt %u",
349 format_white_space, indent, sb->cur_rxt_hole,
350 sb->high_rxt - tc->iss, sb->rescue_rxt - tc->iss);
351
352 hole = scoreboard_first_hole (sb);
353 if (hole)
354 s = format (s, "\n%Uhead %u tail %u %u holes:\n%U", format_white_space,
355 indent, sb->head, sb->tail, pool_elts (sb->holes),
356 format_white_space, indent);
357
358 while (hole)
359 {
360 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
361 hole = scoreboard_next_hole (sb, hole);
362 }
363
364 return s;
365}
366
367/**
368 * \brief Configure an ipv4 source address range
369 * @param vm vlib_main_t pointer
370 * @param start first ipv4 address in the source address range
371 * @param end last ipv4 address in the source address range
372 * @param table_id VRF / table ID, 0 for the default FIB
373 * @return 0 if all OK, else an error indication from api_errno.h
374 */
375
376int
377tcp_configure_v4_source_address_range (vlib_main_t * vm,
378 ip4_address_t * start,
379 ip4_address_t * end, u32 table_id)
380{
381 u32 start_host_byte_order, end_host_byte_order;
382 fib_prefix_t prefix;
383 fib_node_index_t fei;
384 u32 fib_index = 0;
385 u32 sw_if_index;
386 int rv;
387
388 clib_memset (&prefix, 0, sizeof (prefix));
389
390 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
391
392 if (fib_index == ~0)
393 return VNET_API_ERROR_NO_SUCH_FIB;
394
395 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
396 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
397
398 /* sanity check for reversed args or some such */
399 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
400 return VNET_API_ERROR_INVALID_ARGUMENT;
401
402 /* Lookup the last address, to identify the interface involved */
403 prefix.fp_len = 32;
404 prefix.fp_proto = FIB_PROTOCOL_IP4;
405 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
406
407 fei = fib_table_lookup (fib_index, &prefix);
408
409 /* Couldn't find route to destination. Bail out. */
410 if (fei == FIB_NODE_INDEX_INVALID)
411 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
412
413 sw_if_index = fib_entry_get_resolving_interface (fei);
Mercury8ade4a52021-12-11 15:58:32 +0800414 if (sw_if_index == (u32) ~0)
415 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
Florin Coras999840c2020-03-18 20:31:34 +0000416
417 /* Configure proxy arp across the range */
418 rv = ip4_neighbor_proxy_add (fib_index, start, end);
419
420 if (rv)
421 return rv;
422
423 rv = ip4_neighbor_proxy_enable (sw_if_index);
424
425 if (rv)
426 return rv;
427
428 do
429 {
430 dpo_id_t dpo = DPO_INVALID;
431
432 vec_add1 (tcp_cfg.ip4_src_addrs, start[0]);
433
434 /* Add local adjacencies for the range */
435
Mercury89618da2021-12-24 11:58:27 +0800436 receive_dpo_add_or_lock (DPO_PROTO_IP4, sw_if_index /* sw_if_index */,
Florin Coras999840c2020-03-18 20:31:34 +0000437 NULL, &dpo);
438 prefix.fp_len = 32;
439 prefix.fp_proto = FIB_PROTOCOL_IP4;
440 prefix.fp_addr.ip4.as_u32 = start->as_u32;
441
442 fib_table_entry_special_dpo_update (fib_index,
443 &prefix,
444 FIB_SOURCE_API,
445 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
446 dpo_reset (&dpo);
447
448 start_host_byte_order++;
449 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
450 }
451 while (start_host_byte_order <= end_host_byte_order);
452
453 return 0;
454}
455
456/**
457 * \brief Configure an ipv6 source address range
458 * @param vm vlib_main_t pointer
459 * @param start first ipv6 address in the source address range
460 * @param end last ipv6 address in the source address range
461 * @param table_id VRF / table ID, 0 for the default FIB
462 * @return 0 if all OK, else an error indication from api_errno.h
463 */
464
465int
466tcp_configure_v6_source_address_range (vlib_main_t * vm,
467 ip6_address_t * start,
468 ip6_address_t * end, u32 table_id)
469{
470 fib_prefix_t prefix;
471 u32 fib_index = 0;
472 fib_node_index_t fei;
473 u32 sw_if_index;
474
475 clib_memset (&prefix, 0, sizeof (prefix));
476
477 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
478
479 if (fib_index == ~0)
480 return VNET_API_ERROR_NO_SUCH_FIB;
481
482 while (1)
483 {
484 int i;
485 ip6_address_t tmp;
486 dpo_id_t dpo = DPO_INVALID;
487
488 /* Remember this address */
489 vec_add1 (tcp_cfg.ip6_src_addrs, start[0]);
490
491 /* Lookup the prefix, to identify the interface involved */
492 prefix.fp_len = 128;
493 prefix.fp_proto = FIB_PROTOCOL_IP6;
494 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
495
496 fei = fib_table_lookup (fib_index, &prefix);
497
498 /* Couldn't find route to destination. Bail out. */
499 if (fei == FIB_NODE_INDEX_INVALID)
500 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
501
502 sw_if_index = fib_entry_get_resolving_interface (fei);
503
504 if (sw_if_index == (u32) ~ 0)
505 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
506
507 /* Add a proxy neighbor discovery entry for this address */
508 ip6_neighbor_proxy_add (sw_if_index, start);
509
510 /* Add a receive adjacency for this address */
Mercury89618da2021-12-24 11:58:27 +0800511 receive_dpo_add_or_lock (DPO_PROTO_IP6, sw_if_index /* sw_if_index */,
Florin Coras999840c2020-03-18 20:31:34 +0000512 NULL, &dpo);
513
514 fib_table_entry_special_dpo_update (fib_index,
515 &prefix,
516 FIB_SOURCE_API,
517 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
518 dpo_reset (&dpo);
519
520 /* Done with the entire range? */
521 if (!memcmp (start, end, sizeof (start[0])))
522 break;
523
524 /* Increment the address. DGMS. */
525 tmp = start[0];
526 for (i = 15; i >= 0; i--)
527 {
528 tmp.as_u8[i] += 1;
529 if (tmp.as_u8[i] != 0)
530 break;
531 }
532 start[0] = tmp;
533 }
534 return 0;
535}
536
537static clib_error_t *
538tcp_src_address_fn (vlib_main_t * vm,
539 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
540{
541 ip4_address_t v4start, v4end;
542 ip6_address_t v6start, v6end;
543 u32 table_id = 0;
544 int v4set = 0;
545 int v6set = 0;
546 int rv;
547
548 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
549 {
550 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
551 unformat_ip4_address, &v4end))
552 v4set = 1;
553 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
554 {
555 memcpy (&v4end, &v4start, sizeof (v4start));
556 v4set = 1;
557 }
558 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
559 unformat_ip6_address, &v6end))
560 v6set = 1;
561 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
562 {
563 memcpy (&v6end, &v6start, sizeof (v6start));
564 v6set = 1;
565 }
566 else if (unformat (input, "fib-table %d", &table_id))
567 ;
568 else
569 break;
570 }
571
572 if (!v4set && !v6set)
573 return clib_error_return (0, "at least one v4 or v6 address required");
574
575 if (v4set)
576 {
577 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
578 table_id);
579 switch (rv)
580 {
581 case 0:
582 break;
583
584 case VNET_API_ERROR_NO_SUCH_FIB:
585 return clib_error_return (0, "Invalid table-id %d", table_id);
586
587 case VNET_API_ERROR_INVALID_ARGUMENT:
588 return clib_error_return (0, "Invalid address range %U - %U",
589 format_ip4_address, &v4start,
590 format_ip4_address, &v4end);
591 default:
592 return clib_error_return (0, "error %d", rv);
593 break;
594 }
595 }
596 if (v6set)
597 {
598 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
599 table_id);
600 switch (rv)
601 {
602 case 0:
603 break;
604
605 case VNET_API_ERROR_NO_SUCH_FIB:
606 return clib_error_return (0, "Invalid table-id %d", table_id);
607
608 default:
609 return clib_error_return (0, "error %d", rv);
610 break;
611 }
612 }
613 return 0;
614}
615
Florin Coras999840c2020-03-18 20:31:34 +0000616VLIB_CLI_COMMAND (tcp_src_address_command, static) =
617{
618 .path = "tcp src-address",
619 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
620 .function = tcp_src_address_fn,
621};
Florin Coras999840c2020-03-18 20:31:34 +0000622
623static u8 *
624tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
625{
626#if TCP_SCOREBOARD_TRACE
627
628 scoreboard_trace_elt_t *block;
629 int i = 0;
630
631 if (!sb->trace)
632 return s;
633
634 s = format (s, "scoreboard trace:");
635 vec_foreach (block, sb->trace)
636 {
637 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
638 block->ack, block->snd_una_max, block->group);
639 if ((++i % 3) == 0)
640 s = format (s, "\n");
641 }
642 return s;
643#else
644 return 0;
645#endif
646}
647
648static clib_error_t *
649tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
650 vlib_cli_command_t * cmd_arg)
651{
652 transport_connection_t *tconn = 0;
653 tcp_connection_t *tc;
654 u8 *s = 0;
655 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
656 {
657 if (unformat (input, "%U", unformat_transport_connection, &tconn,
658 TRANSPORT_PROTO_TCP))
659 ;
660 else
661 return clib_error_return (0, "unknown input `%U'",
662 format_unformat_error, input);
663 }
664
665 if (!TCP_SCOREBOARD_TRACE)
666 {
667 vlib_cli_output (vm, "scoreboard tracing not enabled");
668 return 0;
669 }
670
671 tc = tcp_get_connection_from_transport (tconn);
672 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
673 vlib_cli_output (vm, "%v", s);
674 return 0;
675}
676
Florin Coras999840c2020-03-18 20:31:34 +0000677VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
678{
679 .path = "show tcp scoreboard trace",
680 .short_help = "show tcp scoreboard trace <connection>",
681 .function = tcp_show_scoreboard_trace_fn,
682};
Florin Coras999840c2020-03-18 20:31:34 +0000683
684u8 *
685tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
686{
687 int i, trace_len;
688 scoreboard_trace_elt_t *trace;
689 u32 next_ack, left, group, has_new_ack = 0;
Dave Barach11fb09e2020-08-06 12:10:09 -0400690 tcp_connection_t _placeholder_tc, *placeholder_tc = &_placeholder_tc;
Florin Coras999840c2020-03-18 20:31:34 +0000691 sack_block_t *block;
692
693 if (!TCP_SCOREBOARD_TRACE)
694 {
695 s = format (s, "scoreboard tracing not enabled");
696 return s;
697 }
698
699 if (!tc)
700 return s;
701
Dave Barach11fb09e2020-08-06 12:10:09 -0400702 clib_memset (placeholder_tc, 0, sizeof (*placeholder_tc));
703 tcp_connection_timers_init (placeholder_tc);
704 scoreboard_init (&placeholder_tc->sack_sb);
705 placeholder_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
Florin Coras999840c2020-03-18 20:31:34 +0000706
707#if TCP_SCOREBOARD_TRACE
708 trace = tc->sack_sb.trace;
709 trace_len = vec_len (tc->sack_sb.trace);
710#endif
711
712 for (i = 0; i < trace_len; i++)
713 {
714 if (trace[i].ack != 0)
715 {
Dave Barach11fb09e2020-08-06 12:10:09 -0400716 placeholder_tc->snd_una = trace[i].ack - 1448;
Florin Coras55e556c2020-10-23 10:45:48 -0700717 placeholder_tc->snd_nxt = trace[i].ack;
Florin Coras999840c2020-03-18 20:31:34 +0000718 }
719 }
720
721 left = 0;
722 while (left < trace_len)
723 {
724 group = trace[left].group;
Dave Barach11fb09e2020-08-06 12:10:09 -0400725 vec_reset_length (placeholder_tc->rcv_opts.sacks);
Florin Coras999840c2020-03-18 20:31:34 +0000726 has_new_ack = 0;
727 while (trace[left].group == group)
728 {
729 if (trace[left].ack != 0)
730 {
731 if (verbose)
732 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
Florin Coras55e556c2020-10-23 10:45:48 -0700733 trace[left].ack, trace[left].snd_nxt);
734 placeholder_tc->snd_nxt = trace[left].snd_nxt;
Florin Coras999840c2020-03-18 20:31:34 +0000735 next_ack = trace[left].ack;
736 has_new_ack = 1;
737 }
738 else
739 {
740 if (verbose)
741 s = format (s, "[%u, %u], ", trace[left].start,
742 trace[left].end);
Dave Barach11fb09e2020-08-06 12:10:09 -0400743 vec_add2 (placeholder_tc->rcv_opts.sacks, block, 1);
Florin Coras999840c2020-03-18 20:31:34 +0000744 block->start = trace[left].start;
745 block->end = trace[left].end;
746 }
747 left++;
748 }
749
750 /* Push segments */
Dave Barach11fb09e2020-08-06 12:10:09 -0400751 tcp_rcv_sacks (placeholder_tc, next_ack);
Florin Coras999840c2020-03-18 20:31:34 +0000752 if (has_new_ack)
Dave Barach11fb09e2020-08-06 12:10:09 -0400753 placeholder_tc->snd_una = next_ack;
Florin Coras999840c2020-03-18 20:31:34 +0000754
755 if (verbose)
756 s = format (s, "result: %U", format_tcp_scoreboard,
Dave Barach11fb09e2020-08-06 12:10:09 -0400757 &placeholder_tc->sack_sb);
Florin Coras999840c2020-03-18 20:31:34 +0000758
759 }
Dave Barach11fb09e2020-08-06 12:10:09 -0400760 s =
761 format (s, "result: %U", format_tcp_scoreboard, &placeholder_tc->sack_sb);
Florin Coras999840c2020-03-18 20:31:34 +0000762
763 return s;
764}
765
766static clib_error_t *
767tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
768 vlib_cli_command_t * cmd_arg)
769{
770 transport_connection_t *tconn = 0;
771 tcp_connection_t *tc = 0;
772 u8 *str = 0;
773 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
774 {
775 if (unformat (input, "%U", unformat_transport_connection, &tconn,
776 TRANSPORT_PROTO_TCP))
777 ;
778 else
779 return clib_error_return (0, "unknown input `%U'",
780 format_unformat_error, input);
781 }
782
783 if (!TCP_SCOREBOARD_TRACE)
784 {
785 vlib_cli_output (vm, "scoreboard tracing not enabled");
786 return 0;
787 }
788
789 tc = tcp_get_connection_from_transport (tconn);
790 if (!tc)
791 {
792 vlib_cli_output (vm, "connection not found");
793 return 0;
794 }
795 str = tcp_scoreboard_replay (str, tc, 1);
796 vlib_cli_output (vm, "%v", str);
797 return 0;
798}
799
Florin Coras999840c2020-03-18 20:31:34 +0000800VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
801{
802 .path = "tcp replay scoreboard",
803 .short_help = "tcp replay scoreboard <connection>",
804 .function = tcp_scoreboard_trace_fn,
805};
Florin Coras999840c2020-03-18 20:31:34 +0000806
807static clib_error_t *
808show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
809 vlib_cli_command_t * cmd_arg)
810{
811 tcp_main_t *tm = vnet_get_tcp_main ();
812 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
813 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
814 input);
815 vlib_cli_output (vm, "IPv4 TCP punt: %s",
816 tm->punt_unknown4 ? "enabled" : "disabled");
817 vlib_cli_output (vm, "IPv6 TCP punt: %s",
818 tm->punt_unknown6 ? "enabled" : "disabled");
819 return 0;
820}
Florin Coras999840c2020-03-18 20:31:34 +0000821VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
822{
823 .path = "show tcp punt",
824 .short_help = "show tcp punt",
825 .function = show_tcp_punt_fn,
826};
Florin Coras999840c2020-03-18 20:31:34 +0000827
828static clib_error_t *
829show_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
830 vlib_cli_command_t * cmd)
831{
832 tcp_main_t *tm = vnet_get_tcp_main ();
833 tcp_worker_ctx_t *wrk;
834 u32 thread;
835
836 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
837 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
838 input);
839 for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
840 {
841 wrk = tcp_get_worker (thread);
842 vlib_cli_output (vm, "Thread %u:\n", thread);
843
844 if (clib_fifo_elts (wrk->pending_timers))
845 vlib_cli_output (vm, " %lu pending timers",
846 clib_fifo_elts (wrk->pending_timers));
847
848#define _(name,type,str) \
849 if (wrk->stats.name) \
850 vlib_cli_output (vm, " %lu %s", wrk->stats.name, str);
851 foreach_tcp_wrk_stat
852#undef _
853 }
854
855 return 0;
856}
857
Florin Coras999840c2020-03-18 20:31:34 +0000858VLIB_CLI_COMMAND (show_tcp_stats_command, static) =
859{
860 .path = "show tcp stats",
861 .short_help = "show tcp stats",
862 .function = show_tcp_stats_fn,
863};
Florin Coras999840c2020-03-18 20:31:34 +0000864
865static clib_error_t *
866clear_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
867 vlib_cli_command_t * cmd)
868{
869 tcp_main_t *tm = vnet_get_tcp_main ();
870 tcp_worker_ctx_t *wrk;
871 u32 thread;
872
873 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
874 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
875 input);
876
877 for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
878 {
879 wrk = tcp_get_worker (thread);
880 clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
881 }
882
883 return 0;
884}
885
Florin Coras999840c2020-03-18 20:31:34 +0000886VLIB_CLI_COMMAND (clear_tcp_stats_command, static) =
887{
888 .path = "clear tcp stats",
889 .short_help = "clear tcp stats",
890 .function = clear_tcp_stats_fn,
891};
Florin Coras999840c2020-03-18 20:31:34 +0000892
893uword
894unformat_tcp_cc_algo (unformat_input_t * input, va_list * va)
895{
896 tcp_cc_algorithm_type_e *result = va_arg (*va, tcp_cc_algorithm_type_e *);
897 tcp_main_t *tm = &tcp_main;
898 char *cc_algo_name;
899 u8 found = 0;
900 uword *p;
901
902 if (unformat (input, "%s", &cc_algo_name)
903 && ((p = hash_get_mem (tm->cc_algo_by_name, cc_algo_name))))
904 {
905 *result = *p;
906 found = 1;
907 }
908
909 vec_free (cc_algo_name);
910 return found;
911}
912
913uword
914unformat_tcp_cc_algo_cfg (unformat_input_t * input, va_list * va)
915{
916 tcp_main_t *tm = vnet_get_tcp_main ();
917 tcp_cc_algorithm_t *cc_alg;
918 unformat_input_t sub_input;
919 int found = 0;
920
921 vec_foreach (cc_alg, tm->cc_algos)
922 {
923 if (!unformat (input, cc_alg->name))
924 continue;
925
926 if (cc_alg->unformat_cfg
927 && unformat (input, "%U", unformat_vlib_cli_sub_input, &sub_input))
928 {
929 if (cc_alg->unformat_cfg (&sub_input))
930 found = 1;
931 }
932 }
933 return found;
934}
935
936static clib_error_t *
937tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
938{
Florin Coras57b2e4a2021-07-06 08:25:36 -0700939 u32 cwnd_multiplier, tmp_time, mtu, max_gso_size, tmp;
Florin Coras999840c2020-03-18 20:31:34 +0000940 uword memory_size;
941
942 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
943 {
944 if (unformat (input, "preallocated-connections %d",
945 &tcp_cfg.preallocated_connections))
946 ;
Florin Coras57b2e4a2021-07-06 08:25:36 -0700947 /* Config deprecated. Will be removed in a later release */
948 else if (unformat (input, "preallocated-half-open-connections %d", &tmp))
Florin Coras999840c2020-03-18 20:31:34 +0000949 ;
950 else if (unformat (input, "buffer-fail-fraction %f",
951 &tcp_cfg.buffer_fail_fraction))
952 ;
953 else if (unformat (input, "max-rx-fifo %U", unformat_memory_size,
954 &memory_size))
955 {
956 if (memory_size >= 0x100000000)
957 {
958 return clib_error_return
959 (0, "max-rx-fifo %llu (0x%llx) too large", memory_size,
960 memory_size);
961 }
962 tcp_cfg.max_rx_fifo = memory_size;
963 }
964 else if (unformat (input, "min-rx-fifo %U", unformat_memory_size,
965 &memory_size))
966 {
967 if (memory_size >= 0x100000000)
968 {
969 return clib_error_return
970 (0, "min-rx-fifo %llu (0x%llx) too large", memory_size,
971 memory_size);
972 }
973 tcp_cfg.min_rx_fifo = memory_size;
974 }
Florin Corase10d1672020-04-07 04:14:45 +0000975 else if (unformat (input, "mtu %u", &mtu))
976 tcp_cfg.default_mtu = mtu;
Florin Coras999840c2020-03-18 20:31:34 +0000977 else if (unformat (input, "rwnd-min-update-ack %d",
978 &tcp_cfg.rwnd_min_update_ack))
979 ;
980 else if (unformat (input, "initial-cwnd-multiplier %u",
981 &cwnd_multiplier))
982 tcp_cfg.initial_cwnd_multiplier = cwnd_multiplier;
983 else if (unformat (input, "no-tx-pacing"))
984 tcp_cfg.enable_tx_pacing = 0;
985 else if (unformat (input, "tso"))
986 tcp_cfg.allow_tso = 1;
987 else if (unformat (input, "no-csum-offload"))
988 tcp_cfg.csum_offload = 0;
Simon Zhang23c3d342020-09-15 23:40:28 +0800989 else if (unformat (input, "max-gso-size %u", &max_gso_size))
990 tcp_cfg.max_gso_size = clib_min (max_gso_size, TCP_MAX_GSO_SZ);
Florin Coras999840c2020-03-18 20:31:34 +0000991 else if (unformat (input, "cc-algo %U", unformat_tcp_cc_algo,
992 &tcp_cfg.cc_algo))
993 ;
994 else if (unformat (input, "%U", unformat_tcp_cc_algo_cfg))
995 ;
996 else if (unformat (input, "closewait-time %u", &tmp_time))
997 tcp_cfg.closewait_time = tmp_time / TCP_TIMER_TICK;
998 else if (unformat (input, "timewait-time %u", &tmp_time))
999 tcp_cfg.timewait_time = tmp_time / TCP_TIMER_TICK;
1000 else if (unformat (input, "finwait1-time %u", &tmp_time))
1001 tcp_cfg.finwait1_time = tmp_time / TCP_TIMER_TICK;
1002 else if (unformat (input, "finwait2-time %u", &tmp_time))
1003 tcp_cfg.finwait2_time = tmp_time / TCP_TIMER_TICK;
1004 else if (unformat (input, "lastack-time %u", &tmp_time))
1005 tcp_cfg.lastack_time = tmp_time / TCP_TIMER_TICK;
1006 else if (unformat (input, "closing-time %u", &tmp_time))
1007 tcp_cfg.closing_time = tmp_time / TCP_TIMER_TICK;
liuyacan7e781192021-06-14 18:09:01 +08001008 else if (unformat (input, "alloc-err-timeout %u", &tmp_time))
1009 tcp_cfg.alloc_err_timeout = tmp_time / TCP_TIMER_TICK;
Florin Coras999840c2020-03-18 20:31:34 +00001010 else if (unformat (input, "cleanup-time %u", &tmp_time))
1011 tcp_cfg.cleanup_time = tmp_time / 1000.0;
Florin Coras1c30d2d2024-06-10 13:12:40 -07001012 else if (unformat (input, "syn-rcvd-time %u", &tmp_time))
1013 tcp_cfg.syn_rcvd_time = tmp_time * THZ;
Florin Coras999840c2020-03-18 20:31:34 +00001014 else
1015 return clib_error_return (0, "unknown input `%U'",
1016 format_unformat_error, input);
1017 }
1018 return 0;
1019}
1020
1021VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1022
1023/*
1024 * fd.io coding-style-patch-verification: ON
1025 *
1026 * Local Variables:
1027 * eval: (c-set-style "gnu")
1028 * End:
1029 */