tcp: move features to separate files
[vpp.git] / src / vnet / tcp / tcp_cli.c
1 /*
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
21 const char *tcp_fsm_states[] = {
22 #define _(sym, str) str,
23   foreach_tcp_fsm_state
24 #undef _
25 };
26
27 u8 *
28 format_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
39 const char *tcp_cfg_flags_str[] = {
40 #define _(sym, str) str,
41   foreach_tcp_cfg_flag
42 #undef _
43 };
44
45 static u8 *
46 format_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
64 const char *tcp_connection_flags_str[] = {
65 #define _(sym, str) str,
66   foreach_tcp_connection_flag
67 #undef _
68 };
69
70 static u8 *
71 format_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
89 const char *tcp_conn_timers[] = {
90 #define _(sym, str) str,
91   foreach_tcp_timer
92 #undef _
93 };
94
95 static u8 *
96 format_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
117 static u8 *
118 format_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
130 static i32
131 tcp_rcv_wnd_available (tcp_connection_t * tc)
132 {
133   return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
134 }
135
136 static u8 *
137 format_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",
152               format_white_space, indent, tc->snd_rxt_bytes,
153               tc->rxt_delivered, tc->rxt_head - tc->iss,
154               tcp_time_now_w_thread (tc->c_thread_index) - tc->snd_rxt_ts);
155   if (tcp_in_fastrecovery (tc))
156     prr_space = tcp_fastrecovery_prr_snd_space (tc);
157   s = format (s, "%Uprr_start %u prr_delivered %u prr space %u\n",
158               format_white_space, indent, tc->prr_start - tc->iss,
159               tc->prr_delivered, prr_space);
160   return s;
161 }
162
163 static u8 *
164 format_tcp_stats (u8 * s, va_list * args)
165 {
166   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
167   u32 indent = format_get_indent (s);
168   s = format (s, "in segs %lu dsegs %lu bytes %lu dupacks %u\n",
169               tc->segs_in, tc->data_segs_in, tc->bytes_in, tc->dupacks_in);
170   s = format (s, "%Uout segs %lu dsegs %lu bytes %lu dupacks %u\n",
171               format_white_space, indent, tc->segs_out,
172               tc->data_segs_out, tc->bytes_out, tc->dupacks_out);
173   s = format (s, "%Ufr %u tr %u rxt segs %lu bytes %lu duration %.3f\n",
174               format_white_space, indent, tc->fr_occurences,
175               tc->tr_occurences, tc->segs_retrans, tc->bytes_retrans,
176               tcp_time_now_us (tc->c_thread_index) - tc->start_ts);
177   s = format (s, "%Uerr wnd data below %u above %u ack below %u above %u",
178               format_white_space, indent, tc->errors.below_data_wnd,
179               tc->errors.above_data_wnd, tc->errors.below_ack_wnd,
180               tc->errors.above_ack_wnd);
181   return s;
182 }
183
184 static u8 *
185 format_tcp_vars (u8 * s, va_list * args)
186 {
187   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
188   s = format (s, " index: %u cfg: %U flags: %U timers: %U\n", tc->c_c_index,
189               format_tcp_cfg_flags, tc, format_tcp_connection_flags, tc,
190               format_tcp_timers, tc);
191   s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
192               tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
193               tc->snd_una_max - tc->iss);
194   s = format (s, " rcv_nxt %u rcv_las %u\n",
195               tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
196   s = format (s, " snd_wnd %u rcv_wnd %u rcv_wscale %u ",
197               tc->snd_wnd, tc->rcv_wnd, tc->rcv_wscale);
198   s = format (s, "snd_wl1 %u snd_wl2 %u\n", tc->snd_wl1 - tc->irs,
199               tc->snd_wl2 - tc->iss);
200   s = format (s, " flight size %u out space %u rcv_wnd_av %u",
201               tcp_flight_size (tc), tcp_available_output_snd_space (tc),
202               tcp_rcv_wnd_available (tc));
203   s = format (s, " tsval_recent %u\n", tc->tsval_recent);
204   s = format (s, " tsecr %u tsecr_last_ack %u tsval_recent_age %u",
205               tc->rcv_opts.tsecr, tc->tsecr_last_ack,
206               tcp_time_now () - tc->tsval_recent_age);
207   s = format (s, " snd_mss %u\n", tc->snd_mss);
208   s = format (s, " rto %u rto_boff %u srtt %u us %.3f rttvar %u rtt_ts %.4f",
209               tc->rto, tc->rto_boff, tc->srtt, tc->mrtt_us * 1000, tc->rttvar,
210               tc->rtt_ts);
211   s = format (s, " rtt_seq %u\n", tc->rtt_seq - tc->iss);
212   s = format (s, " next_node %u opaque 0x%x fib_index %u\n",
213               tc->next_node_index, tc->next_node_opaque, tc->c_fib_index);
214   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
228 u8 *
229 format_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
252 u8 *
253 format_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;
260   s = format (s, "%-50U", format_tcp_connection_id, tc);
261   if (verbose)
262     {
263       s = format (s, "%-15U", format_tcp_state, tc->state);
264       if (verbose > 1)
265         s = format (s, "\n%U", format_tcp_vars, tc);
266     }
267
268   return s;
269 }
270
271 u8 *
272 format_tcp_sacks (u8 * s, va_list * args)
273 {
274   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
275   sack_block_t *sacks = tc->snd_sacks;
276   sack_block_t *block;
277   int i, len = 0;
278
279   len = vec_len (sacks);
280   for (i = 0; i < len - 1; i++)
281     {
282       block = &sacks[i];
283       s = format (s, " start %u end %u\n", block->start - tc->irs,
284                   block->end - tc->irs);
285     }
286   if (len)
287     {
288       block = &sacks[len - 1];
289       s = format (s, " start %u end %u", block->start - tc->irs,
290                   block->end - tc->irs);
291     }
292   return s;
293 }
294
295 u8 *
296 format_tcp_rcv_sacks (u8 * s, va_list * args)
297 {
298   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
299   sack_block_t *sacks = tc->rcv_opts.sacks;
300   sack_block_t *block;
301   int i, len = 0;
302
303   len = vec_len (sacks);
304   for (i = 0; i < len - 1; i++)
305     {
306       block = &sacks[i];
307       s = format (s, " start %u end %u\n", block->start - tc->iss,
308                   block->end - tc->iss);
309     }
310   if (len)
311     {
312       block = &sacks[len - 1];
313       s = format (s, " start %u end %u", block->start - tc->iss,
314                   block->end - tc->iss);
315     }
316   return s;
317 }
318
319 static u8 *
320 format_tcp_sack_hole (u8 * s, va_list * args)
321 {
322   sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
323   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
324   if (tc)
325     s = format (s, "  [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
326   else
327     s = format (s, "  [%u, %u]", hole->start, hole->end);
328   return s;
329 }
330
331 u8 *
332 format_tcp_scoreboard (u8 * s, va_list * args)
333 {
334   sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
335   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
336   sack_scoreboard_hole_t *hole;
337   u32 indent = format_get_indent (s);
338
339   s = format (s, "sacked %u last_sacked %u lost %u last_lost %u"
340               " rxt_sacked %u\n",
341               sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes,
342               sb->last_lost_bytes, sb->rxt_sacked);
343   s = format (s, "%Ulast_delivered %u high_sacked %u is_reneging %u\n",
344               format_white_space, indent, sb->last_bytes_delivered,
345               sb->high_sacked - tc->iss, sb->is_reneging);
346   s = format (s, "%Ucur_rxt_hole %u high_rxt %u rescue_rxt %u",
347               format_white_space, indent, sb->cur_rxt_hole,
348               sb->high_rxt - tc->iss, sb->rescue_rxt - tc->iss);
349
350   hole = scoreboard_first_hole (sb);
351   if (hole)
352     s = format (s, "\n%Uhead %u tail %u %u holes:\n%U", format_white_space,
353                 indent, sb->head, sb->tail, pool_elts (sb->holes),
354                 format_white_space, indent);
355
356   while (hole)
357     {
358       s = format (s, "%U", format_tcp_sack_hole, hole, tc);
359       hole = scoreboard_next_hole (sb, hole);
360     }
361
362   return s;
363 }
364
365 /**
366  * \brief Configure an ipv4 source address range
367  * @param vm vlib_main_t pointer
368  * @param start first ipv4 address in the source address range
369  * @param end last ipv4 address in the source address range
370  * @param table_id VRF / table ID, 0 for the default FIB
371  * @return 0 if all OK, else an error indication from api_errno.h
372  */
373
374 int
375 tcp_configure_v4_source_address_range (vlib_main_t * vm,
376                                        ip4_address_t * start,
377                                        ip4_address_t * end, u32 table_id)
378 {
379   u32 start_host_byte_order, end_host_byte_order;
380   fib_prefix_t prefix;
381   fib_node_index_t fei;
382   u32 fib_index = 0;
383   u32 sw_if_index;
384   int rv;
385
386   clib_memset (&prefix, 0, sizeof (prefix));
387
388   fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
389
390   if (fib_index == ~0)
391     return VNET_API_ERROR_NO_SUCH_FIB;
392
393   start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
394   end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
395
396   /* sanity check for reversed args or some such */
397   if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
398     return VNET_API_ERROR_INVALID_ARGUMENT;
399
400   /* Lookup the last address, to identify the interface involved */
401   prefix.fp_len = 32;
402   prefix.fp_proto = FIB_PROTOCOL_IP4;
403   memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
404
405   fei = fib_table_lookup (fib_index, &prefix);
406
407   /* Couldn't find route to destination. Bail out. */
408   if (fei == FIB_NODE_INDEX_INVALID)
409     return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
410
411   sw_if_index = fib_entry_get_resolving_interface (fei);
412
413   /* Configure proxy arp across the range */
414   rv = ip4_neighbor_proxy_add (fib_index, start, end);
415
416   if (rv)
417     return rv;
418
419   rv = ip4_neighbor_proxy_enable (sw_if_index);
420
421   if (rv)
422     return rv;
423
424   do
425     {
426       dpo_id_t dpo = DPO_INVALID;
427
428       vec_add1 (tcp_cfg.ip4_src_addrs, start[0]);
429
430       /* Add local adjacencies for the range */
431
432       receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
433                                NULL, &dpo);
434       prefix.fp_len = 32;
435       prefix.fp_proto = FIB_PROTOCOL_IP4;
436       prefix.fp_addr.ip4.as_u32 = start->as_u32;
437
438       fib_table_entry_special_dpo_update (fib_index,
439                                           &prefix,
440                                           FIB_SOURCE_API,
441                                           FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
442       dpo_reset (&dpo);
443
444       start_host_byte_order++;
445       start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
446     }
447   while (start_host_byte_order <= end_host_byte_order);
448
449   return 0;
450 }
451
452 /**
453  * \brief Configure an ipv6 source address range
454  * @param vm vlib_main_t pointer
455  * @param start first ipv6 address in the source address range
456  * @param end last ipv6 address in the source address range
457  * @param table_id VRF / table ID, 0 for the default FIB
458  * @return 0 if all OK, else an error indication from api_errno.h
459  */
460
461 int
462 tcp_configure_v6_source_address_range (vlib_main_t * vm,
463                                        ip6_address_t * start,
464                                        ip6_address_t * end, u32 table_id)
465 {
466   fib_prefix_t prefix;
467   u32 fib_index = 0;
468   fib_node_index_t fei;
469   u32 sw_if_index;
470
471   clib_memset (&prefix, 0, sizeof (prefix));
472
473   fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
474
475   if (fib_index == ~0)
476     return VNET_API_ERROR_NO_SUCH_FIB;
477
478   while (1)
479     {
480       int i;
481       ip6_address_t tmp;
482       dpo_id_t dpo = DPO_INVALID;
483
484       /* Remember this address */
485       vec_add1 (tcp_cfg.ip6_src_addrs, start[0]);
486
487       /* Lookup the prefix, to identify the interface involved */
488       prefix.fp_len = 128;
489       prefix.fp_proto = FIB_PROTOCOL_IP6;
490       memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
491
492       fei = fib_table_lookup (fib_index, &prefix);
493
494       /* Couldn't find route to destination. Bail out. */
495       if (fei == FIB_NODE_INDEX_INVALID)
496         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
497
498       sw_if_index = fib_entry_get_resolving_interface (fei);
499
500       if (sw_if_index == (u32) ~ 0)
501         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
502
503       /* Add a proxy neighbor discovery entry for this address */
504       ip6_neighbor_proxy_add (sw_if_index, start);
505
506       /* Add a receive adjacency for this address */
507       receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
508                                NULL, &dpo);
509
510       fib_table_entry_special_dpo_update (fib_index,
511                                           &prefix,
512                                           FIB_SOURCE_API,
513                                           FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
514       dpo_reset (&dpo);
515
516       /* Done with the entire range? */
517       if (!memcmp (start, end, sizeof (start[0])))
518         break;
519
520       /* Increment the address. DGMS. */
521       tmp = start[0];
522       for (i = 15; i >= 0; i--)
523         {
524           tmp.as_u8[i] += 1;
525           if (tmp.as_u8[i] != 0)
526             break;
527         }
528       start[0] = tmp;
529     }
530   return 0;
531 }
532
533 static clib_error_t *
534 tcp_src_address_fn (vlib_main_t * vm,
535                     unformat_input_t * input, vlib_cli_command_t * cmd_arg)
536 {
537   ip4_address_t v4start, v4end;
538   ip6_address_t v6start, v6end;
539   u32 table_id = 0;
540   int v4set = 0;
541   int v6set = 0;
542   int rv;
543
544   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
545     {
546       if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
547                     unformat_ip4_address, &v4end))
548         v4set = 1;
549       else if (unformat (input, "%U", unformat_ip4_address, &v4start))
550         {
551           memcpy (&v4end, &v4start, sizeof (v4start));
552           v4set = 1;
553         }
554       else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
555                          unformat_ip6_address, &v6end))
556         v6set = 1;
557       else if (unformat (input, "%U", unformat_ip6_address, &v6start))
558         {
559           memcpy (&v6end, &v6start, sizeof (v6start));
560           v6set = 1;
561         }
562       else if (unformat (input, "fib-table %d", &table_id))
563         ;
564       else
565         break;
566     }
567
568   if (!v4set && !v6set)
569     return clib_error_return (0, "at least one v4 or v6 address required");
570
571   if (v4set)
572     {
573       rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
574                                                   table_id);
575       switch (rv)
576         {
577         case 0:
578           break;
579
580         case VNET_API_ERROR_NO_SUCH_FIB:
581           return clib_error_return (0, "Invalid table-id %d", table_id);
582
583         case VNET_API_ERROR_INVALID_ARGUMENT:
584           return clib_error_return (0, "Invalid address range %U - %U",
585                                     format_ip4_address, &v4start,
586                                     format_ip4_address, &v4end);
587         default:
588           return clib_error_return (0, "error %d", rv);
589           break;
590         }
591     }
592   if (v6set)
593     {
594       rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
595                                                   table_id);
596       switch (rv)
597         {
598         case 0:
599           break;
600
601         case VNET_API_ERROR_NO_SUCH_FIB:
602           return clib_error_return (0, "Invalid table-id %d", table_id);
603
604         default:
605           return clib_error_return (0, "error %d", rv);
606           break;
607         }
608     }
609   return 0;
610 }
611
612 /* *INDENT-OFF* */
613 VLIB_CLI_COMMAND (tcp_src_address_command, static) =
614 {
615   .path = "tcp src-address",
616   .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
617   .function = tcp_src_address_fn,
618 };
619 /* *INDENT-ON* */
620
621 static u8 *
622 tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
623 {
624 #if TCP_SCOREBOARD_TRACE
625
626   scoreboard_trace_elt_t *block;
627   int i = 0;
628
629   if (!sb->trace)
630     return s;
631
632   s = format (s, "scoreboard trace:");
633   vec_foreach (block, sb->trace)
634   {
635     s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
636                 block->ack, block->snd_una_max, block->group);
637     if ((++i % 3) == 0)
638       s = format (s, "\n");
639   }
640   return s;
641 #else
642   return 0;
643 #endif
644 }
645
646 static clib_error_t *
647 tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
648                               vlib_cli_command_t * cmd_arg)
649 {
650   transport_connection_t *tconn = 0;
651   tcp_connection_t *tc;
652   u8 *s = 0;
653   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
654     {
655       if (unformat (input, "%U", unformat_transport_connection, &tconn,
656                     TRANSPORT_PROTO_TCP))
657         ;
658       else
659         return clib_error_return (0, "unknown input `%U'",
660                                   format_unformat_error, input);
661     }
662
663   if (!TCP_SCOREBOARD_TRACE)
664     {
665       vlib_cli_output (vm, "scoreboard tracing not enabled");
666       return 0;
667     }
668
669   tc = tcp_get_connection_from_transport (tconn);
670   s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
671   vlib_cli_output (vm, "%v", s);
672   return 0;
673 }
674
675 /* *INDENT-OFF* */
676 VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
677 {
678   .path = "show tcp scoreboard trace",
679   .short_help = "show tcp scoreboard trace <connection>",
680   .function = tcp_show_scoreboard_trace_fn,
681 };
682 /* *INDENT-ON* */
683
684 u8 *
685 tcp_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;
690   tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
691   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
702   clib_memset (dummy_tc, 0, sizeof (*dummy_tc));
703   tcp_connection_timers_init (dummy_tc);
704   scoreboard_init (&dummy_tc->sack_sb);
705   dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
706
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         {
716           dummy_tc->snd_una = trace[i].ack - 1448;
717           dummy_tc->snd_una_max = trace[i].ack;
718         }
719     }
720
721   left = 0;
722   while (left < trace_len)
723     {
724       group = trace[left].group;
725       vec_reset_length (dummy_tc->rcv_opts.sacks);
726       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: ",
733                             trace[left].ack, trace[left].snd_una_max);
734               dummy_tc->snd_una_max = trace[left].snd_una_max;
735               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);
743               vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
744               block->start = trace[left].start;
745               block->end = trace[left].end;
746             }
747           left++;
748         }
749
750       /* Push segments */
751       tcp_rcv_sacks (dummy_tc, next_ack);
752       if (has_new_ack)
753         dummy_tc->snd_una = next_ack;
754
755       if (verbose)
756         s = format (s, "result: %U", format_tcp_scoreboard,
757                     &dummy_tc->sack_sb);
758
759     }
760   s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
761
762   return s;
763 }
764
765 static clib_error_t *
766 tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
767                          vlib_cli_command_t * cmd_arg)
768 {
769   transport_connection_t *tconn = 0;
770   tcp_connection_t *tc = 0;
771   u8 *str = 0;
772   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
773     {
774       if (unformat (input, "%U", unformat_transport_connection, &tconn,
775                     TRANSPORT_PROTO_TCP))
776         ;
777       else
778         return clib_error_return (0, "unknown input `%U'",
779                                   format_unformat_error, input);
780     }
781
782   if (!TCP_SCOREBOARD_TRACE)
783     {
784       vlib_cli_output (vm, "scoreboard tracing not enabled");
785       return 0;
786     }
787
788   tc = tcp_get_connection_from_transport (tconn);
789   if (!tc)
790     {
791       vlib_cli_output (vm, "connection not found");
792       return 0;
793     }
794   str = tcp_scoreboard_replay (str, tc, 1);
795   vlib_cli_output (vm, "%v", str);
796   return 0;
797 }
798
799 /* *INDENT-OFF* */
800 VLIB_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 };
806 /* *INDENT-ON* */
807
808 static clib_error_t *
809 show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
810                   vlib_cli_command_t * cmd_arg)
811 {
812   tcp_main_t *tm = vnet_get_tcp_main ();
813   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
814     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
815                               input);
816   vlib_cli_output (vm, "IPv4 TCP punt: %s",
817                    tm->punt_unknown4 ? "enabled" : "disabled");
818   vlib_cli_output (vm, "IPv6 TCP punt: %s",
819                    tm->punt_unknown6 ? "enabled" : "disabled");
820   return 0;
821 }
822 /* *INDENT-OFF* */
823 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
824 {
825   .path = "show tcp punt",
826   .short_help = "show tcp punt",
827   .function = show_tcp_punt_fn,
828 };
829 /* *INDENT-ON* */
830
831 static clib_error_t *
832 show_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
833                    vlib_cli_command_t * cmd)
834 {
835   tcp_main_t *tm = vnet_get_tcp_main ();
836   tcp_worker_ctx_t *wrk;
837   u32 thread;
838
839   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
840     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
841                               input);
842   for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
843     {
844       wrk = tcp_get_worker (thread);
845       vlib_cli_output (vm, "Thread %u:\n", thread);
846
847       if (clib_fifo_elts (wrk->pending_timers))
848         vlib_cli_output (vm, " %lu pending timers",
849                          clib_fifo_elts (wrk->pending_timers));
850
851 #define _(name,type,str)                                        \
852   if (wrk->stats.name)                                          \
853     vlib_cli_output (vm, " %lu %s", wrk->stats.name, str);
854       foreach_tcp_wrk_stat
855 #undef _
856     }
857
858   return 0;
859 }
860
861 /* *INDENT-OFF* */
862 VLIB_CLI_COMMAND (show_tcp_stats_command, static) =
863 {
864   .path = "show tcp stats",
865   .short_help = "show tcp stats",
866   .function = show_tcp_stats_fn,
867 };
868 /* *INDENT-ON* */
869
870 static clib_error_t *
871 clear_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
872                     vlib_cli_command_t * cmd)
873 {
874   tcp_main_t *tm = vnet_get_tcp_main ();
875   tcp_worker_ctx_t *wrk;
876   u32 thread;
877
878   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
879     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
880                               input);
881
882   for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
883     {
884       wrk = tcp_get_worker (thread);
885       clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
886     }
887
888   return 0;
889 }
890
891 /* *INDENT-OFF* */
892 VLIB_CLI_COMMAND (clear_tcp_stats_command, static) =
893 {
894   .path = "clear tcp stats",
895   .short_help = "clear tcp stats",
896   .function = clear_tcp_stats_fn,
897 };
898 /* *INDENT-ON* */
899
900 uword
901 unformat_tcp_cc_algo (unformat_input_t * input, va_list * va)
902 {
903   tcp_cc_algorithm_type_e *result = va_arg (*va, tcp_cc_algorithm_type_e *);
904   tcp_main_t *tm = &tcp_main;
905   char *cc_algo_name;
906   u8 found = 0;
907   uword *p;
908
909   if (unformat (input, "%s", &cc_algo_name)
910       && ((p = hash_get_mem (tm->cc_algo_by_name, cc_algo_name))))
911     {
912       *result = *p;
913       found = 1;
914     }
915
916   vec_free (cc_algo_name);
917   return found;
918 }
919
920 uword
921 unformat_tcp_cc_algo_cfg (unformat_input_t * input, va_list * va)
922 {
923   tcp_main_t *tm = vnet_get_tcp_main ();
924   tcp_cc_algorithm_t *cc_alg;
925   unformat_input_t sub_input;
926   int found = 0;
927
928   vec_foreach (cc_alg, tm->cc_algos)
929   {
930     if (!unformat (input, cc_alg->name))
931       continue;
932
933     if (cc_alg->unformat_cfg
934         && unformat (input, "%U", unformat_vlib_cli_sub_input, &sub_input))
935       {
936         if (cc_alg->unformat_cfg (&sub_input))
937           found = 1;
938       }
939   }
940   return found;
941 }
942
943 static clib_error_t *
944 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
945 {
946   u32 cwnd_multiplier, tmp_time;
947   uword memory_size;
948
949   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
950     {
951       if (unformat (input, "preallocated-connections %d",
952                     &tcp_cfg.preallocated_connections))
953         ;
954       else if (unformat (input, "preallocated-half-open-connections %d",
955                          &tcp_cfg.preallocated_half_open_connections))
956         ;
957       else if (unformat (input, "buffer-fail-fraction %f",
958                          &tcp_cfg.buffer_fail_fraction))
959         ;
960       else if (unformat (input, "max-rx-fifo %U", unformat_memory_size,
961                          &memory_size))
962         {
963           if (memory_size >= 0x100000000)
964             {
965               return clib_error_return
966                 (0, "max-rx-fifo %llu (0x%llx) too large", memory_size,
967                  memory_size);
968             }
969           tcp_cfg.max_rx_fifo = memory_size;
970         }
971       else if (unformat (input, "min-rx-fifo %U", unformat_memory_size,
972                          &memory_size))
973         {
974           if (memory_size >= 0x100000000)
975             {
976               return clib_error_return
977                 (0, "min-rx-fifo %llu (0x%llx) too large", memory_size,
978                  memory_size);
979             }
980           tcp_cfg.min_rx_fifo = memory_size;
981         }
982       else if (unformat (input, "mtu %u", &tcp_cfg.default_mtu))
983         ;
984       else if (unformat (input, "rwnd-min-update-ack %d",
985                          &tcp_cfg.rwnd_min_update_ack))
986         ;
987       else if (unformat (input, "initial-cwnd-multiplier %u",
988                          &cwnd_multiplier))
989         tcp_cfg.initial_cwnd_multiplier = cwnd_multiplier;
990       else if (unformat (input, "no-tx-pacing"))
991         tcp_cfg.enable_tx_pacing = 0;
992       else if (unformat (input, "tso"))
993         tcp_cfg.allow_tso = 1;
994       else if (unformat (input, "no-csum-offload"))
995         tcp_cfg.csum_offload = 0;
996       else if (unformat (input, "cc-algo %U", unformat_tcp_cc_algo,
997                          &tcp_cfg.cc_algo))
998         ;
999       else if (unformat (input, "%U", unformat_tcp_cc_algo_cfg))
1000         ;
1001       else if (unformat (input, "closewait-time %u", &tmp_time))
1002         tcp_cfg.closewait_time = tmp_time / TCP_TIMER_TICK;
1003       else if (unformat (input, "timewait-time %u", &tmp_time))
1004         tcp_cfg.timewait_time = tmp_time / TCP_TIMER_TICK;
1005       else if (unformat (input, "finwait1-time %u", &tmp_time))
1006         tcp_cfg.finwait1_time = tmp_time / TCP_TIMER_TICK;
1007       else if (unformat (input, "finwait2-time %u", &tmp_time))
1008         tcp_cfg.finwait2_time = tmp_time / TCP_TIMER_TICK;
1009       else if (unformat (input, "lastack-time %u", &tmp_time))
1010         tcp_cfg.lastack_time = tmp_time / TCP_TIMER_TICK;
1011       else if (unformat (input, "closing-time %u", &tmp_time))
1012         tcp_cfg.closing_time = tmp_time / TCP_TIMER_TICK;
1013       else if (unformat (input, "cleanup-time %u", &tmp_time))
1014         tcp_cfg.cleanup_time = tmp_time / 1000.0;
1015       else
1016         return clib_error_return (0, "unknown input `%U'",
1017                                   format_unformat_error, input);
1018     }
1019   return 0;
1020 }
1021
1022 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1023
1024 /*
1025  * fd.io coding-style-patch-verification: ON
1026  *
1027  * Local Variables:
1028  * eval: (c-set-style "gnu")
1029  * End:
1030  */