session tcp udp tls quic: improve cli formatting
[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 %.1f us %.3f rttvar %.1f",
209               tc->rto / 1000, tc->rto_boff, tc->srtt / 1000.0,
210               tc->mrtt_us * 1e3, tc->rttvar / 1000.0);
211   s = format (s, " rtt_ts %.4f rtt_seq %u\n", tc->rtt_ts,
212               tc->rtt_seq - tc->iss);
213   s = format (s, " next_node %u opaque 0x%x fib_index %u\n",
214               tc->next_node_index, tc->next_node_opaque, tc->c_fib_index);
215   s = format (s, " cong:   %U", format_tcp_congestion, tc);
216
217   if (tc->state >= TCP_STATE_ESTABLISHED)
218     {
219       s = format (s, " sboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
220                   tc);
221       s = format (s, " stats: %U\n", format_tcp_stats, tc);
222     }
223   if (vec_len (tc->snd_sacks))
224     s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
225
226   return s;
227 }
228
229 u8 *
230 format_tcp_connection_id (u8 * s, va_list * args)
231 {
232   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
233   if (!tc)
234     return s;
235   if (tc->c_is_ip4)
236     {
237       s = format (s, "[%d:%d][%s] %U:%d->%U:%d", tc->c_thread_index,
238                   tc->c_s_index, "T", format_ip4_address, &tc->c_lcl_ip4,
239                   clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
240                   &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
241     }
242   else
243     {
244       s = format (s, "[%d:%d][%s] %U:%d->%U:%d", tc->c_thread_index,
245                   tc->c_s_index, "T", format_ip6_address, &tc->c_lcl_ip6,
246                   clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
247                   &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
248     }
249
250   return s;
251 }
252
253 u8 *
254 format_tcp_connection (u8 * s, va_list * args)
255 {
256   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
257   u32 verbose = va_arg (*args, u32);
258
259   if (!tc)
260     return s;
261   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tcp_connection_id, tc);
262   if (verbose)
263     {
264       s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tcp_state,
265                   tc->state);
266       if (verbose > 1)
267         s = format (s, "\n%U", format_tcp_vars, tc);
268     }
269
270   return s;
271 }
272
273 u8 *
274 format_tcp_sacks (u8 * s, va_list * args)
275 {
276   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
277   sack_block_t *sacks = tc->snd_sacks;
278   sack_block_t *block;
279   int i, len = 0;
280
281   len = vec_len (sacks);
282   for (i = 0; i < len - 1; i++)
283     {
284       block = &sacks[i];
285       s = format (s, " start %u end %u\n", block->start - tc->irs,
286                   block->end - tc->irs);
287     }
288   if (len)
289     {
290       block = &sacks[len - 1];
291       s = format (s, " start %u end %u", block->start - tc->irs,
292                   block->end - tc->irs);
293     }
294   return s;
295 }
296
297 u8 *
298 format_tcp_rcv_sacks (u8 * s, va_list * args)
299 {
300   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
301   sack_block_t *sacks = tc->rcv_opts.sacks;
302   sack_block_t *block;
303   int i, len = 0;
304
305   len = vec_len (sacks);
306   for (i = 0; i < len - 1; i++)
307     {
308       block = &sacks[i];
309       s = format (s, " start %u end %u\n", block->start - tc->iss,
310                   block->end - tc->iss);
311     }
312   if (len)
313     {
314       block = &sacks[len - 1];
315       s = format (s, " start %u end %u", block->start - tc->iss,
316                   block->end - tc->iss);
317     }
318   return s;
319 }
320
321 static u8 *
322 format_tcp_sack_hole (u8 * s, va_list * args)
323 {
324   sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
325   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
326   if (tc)
327     s = format (s, "  [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
328   else
329     s = format (s, "  [%u, %u]", hole->start, hole->end);
330   return s;
331 }
332
333 u8 *
334 format_tcp_scoreboard (u8 * s, va_list * args)
335 {
336   sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
337   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
338   sack_scoreboard_hole_t *hole;
339   u32 indent = format_get_indent (s);
340
341   s = format (s, "sacked %u last_sacked %u lost %u last_lost %u"
342               " rxt_sacked %u\n",
343               sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes,
344               sb->last_lost_bytes, sb->rxt_sacked);
345   s = format (s, "%Ulast_delivered %u high_sacked %u is_reneging %u",
346               format_white_space, indent, sb->last_bytes_delivered,
347               sb->high_sacked - tc->iss, sb->is_reneging);
348   s = format (s, " reorder %u\n", sb->reorder);
349   s = format (s, "%Ucur_rxt_hole %u high_rxt %u rescue_rxt %u",
350               format_white_space, indent, sb->cur_rxt_hole,
351               sb->high_rxt - tc->iss, sb->rescue_rxt - tc->iss);
352
353   hole = scoreboard_first_hole (sb);
354   if (hole)
355     s = format (s, "\n%Uhead %u tail %u %u holes:\n%U", format_white_space,
356                 indent, sb->head, sb->tail, pool_elts (sb->holes),
357                 format_white_space, indent);
358
359   while (hole)
360     {
361       s = format (s, "%U", format_tcp_sack_hole, hole, tc);
362       hole = scoreboard_next_hole (sb, hole);
363     }
364
365   return s;
366 }
367
368 /**
369  * \brief Configure an ipv4 source address range
370  * @param vm vlib_main_t pointer
371  * @param start first ipv4 address in the source address range
372  * @param end last ipv4 address in the source address range
373  * @param table_id VRF / table ID, 0 for the default FIB
374  * @return 0 if all OK, else an error indication from api_errno.h
375  */
376
377 int
378 tcp_configure_v4_source_address_range (vlib_main_t * vm,
379                                        ip4_address_t * start,
380                                        ip4_address_t * end, u32 table_id)
381 {
382   u32 start_host_byte_order, end_host_byte_order;
383   fib_prefix_t prefix;
384   fib_node_index_t fei;
385   u32 fib_index = 0;
386   u32 sw_if_index;
387   int rv;
388
389   clib_memset (&prefix, 0, sizeof (prefix));
390
391   fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
392
393   if (fib_index == ~0)
394     return VNET_API_ERROR_NO_SUCH_FIB;
395
396   start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
397   end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
398
399   /* sanity check for reversed args or some such */
400   if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
401     return VNET_API_ERROR_INVALID_ARGUMENT;
402
403   /* Lookup the last address, to identify the interface involved */
404   prefix.fp_len = 32;
405   prefix.fp_proto = FIB_PROTOCOL_IP4;
406   memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
407
408   fei = fib_table_lookup (fib_index, &prefix);
409
410   /* Couldn't find route to destination. Bail out. */
411   if (fei == FIB_NODE_INDEX_INVALID)
412     return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
413
414   sw_if_index = fib_entry_get_resolving_interface (fei);
415
416   /* Configure proxy arp across the range */
417   rv = ip4_neighbor_proxy_add (fib_index, start, end);
418
419   if (rv)
420     return rv;
421
422   rv = ip4_neighbor_proxy_enable (sw_if_index);
423
424   if (rv)
425     return rv;
426
427   do
428     {
429       dpo_id_t dpo = DPO_INVALID;
430
431       vec_add1 (tcp_cfg.ip4_src_addrs, start[0]);
432
433       /* Add local adjacencies for the range */
434
435       receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
436                                NULL, &dpo);
437       prefix.fp_len = 32;
438       prefix.fp_proto = FIB_PROTOCOL_IP4;
439       prefix.fp_addr.ip4.as_u32 = start->as_u32;
440
441       fib_table_entry_special_dpo_update (fib_index,
442                                           &prefix,
443                                           FIB_SOURCE_API,
444                                           FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
445       dpo_reset (&dpo);
446
447       start_host_byte_order++;
448       start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
449     }
450   while (start_host_byte_order <= end_host_byte_order);
451
452   return 0;
453 }
454
455 /**
456  * \brief Configure an ipv6 source address range
457  * @param vm vlib_main_t pointer
458  * @param start first ipv6 address in the source address range
459  * @param end last ipv6 address in the source address range
460  * @param table_id VRF / table ID, 0 for the default FIB
461  * @return 0 if all OK, else an error indication from api_errno.h
462  */
463
464 int
465 tcp_configure_v6_source_address_range (vlib_main_t * vm,
466                                        ip6_address_t * start,
467                                        ip6_address_t * end, u32 table_id)
468 {
469   fib_prefix_t prefix;
470   u32 fib_index = 0;
471   fib_node_index_t fei;
472   u32 sw_if_index;
473
474   clib_memset (&prefix, 0, sizeof (prefix));
475
476   fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
477
478   if (fib_index == ~0)
479     return VNET_API_ERROR_NO_SUCH_FIB;
480
481   while (1)
482     {
483       int i;
484       ip6_address_t tmp;
485       dpo_id_t dpo = DPO_INVALID;
486
487       /* Remember this address */
488       vec_add1 (tcp_cfg.ip6_src_addrs, start[0]);
489
490       /* Lookup the prefix, to identify the interface involved */
491       prefix.fp_len = 128;
492       prefix.fp_proto = FIB_PROTOCOL_IP6;
493       memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
494
495       fei = fib_table_lookup (fib_index, &prefix);
496
497       /* Couldn't find route to destination. Bail out. */
498       if (fei == FIB_NODE_INDEX_INVALID)
499         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
500
501       sw_if_index = fib_entry_get_resolving_interface (fei);
502
503       if (sw_if_index == (u32) ~ 0)
504         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
505
506       /* Add a proxy neighbor discovery entry for this address */
507       ip6_neighbor_proxy_add (sw_if_index, start);
508
509       /* Add a receive adjacency for this address */
510       receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
511                                NULL, &dpo);
512
513       fib_table_entry_special_dpo_update (fib_index,
514                                           &prefix,
515                                           FIB_SOURCE_API,
516                                           FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
517       dpo_reset (&dpo);
518
519       /* Done with the entire range? */
520       if (!memcmp (start, end, sizeof (start[0])))
521         break;
522
523       /* Increment the address. DGMS. */
524       tmp = start[0];
525       for (i = 15; i >= 0; i--)
526         {
527           tmp.as_u8[i] += 1;
528           if (tmp.as_u8[i] != 0)
529             break;
530         }
531       start[0] = tmp;
532     }
533   return 0;
534 }
535
536 static clib_error_t *
537 tcp_src_address_fn (vlib_main_t * vm,
538                     unformat_input_t * input, vlib_cli_command_t * cmd_arg)
539 {
540   ip4_address_t v4start, v4end;
541   ip6_address_t v6start, v6end;
542   u32 table_id = 0;
543   int v4set = 0;
544   int v6set = 0;
545   int rv;
546
547   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
548     {
549       if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
550                     unformat_ip4_address, &v4end))
551         v4set = 1;
552       else if (unformat (input, "%U", unformat_ip4_address, &v4start))
553         {
554           memcpy (&v4end, &v4start, sizeof (v4start));
555           v4set = 1;
556         }
557       else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
558                          unformat_ip6_address, &v6end))
559         v6set = 1;
560       else if (unformat (input, "%U", unformat_ip6_address, &v6start))
561         {
562           memcpy (&v6end, &v6start, sizeof (v6start));
563           v6set = 1;
564         }
565       else if (unformat (input, "fib-table %d", &table_id))
566         ;
567       else
568         break;
569     }
570
571   if (!v4set && !v6set)
572     return clib_error_return (0, "at least one v4 or v6 address required");
573
574   if (v4set)
575     {
576       rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
577                                                   table_id);
578       switch (rv)
579         {
580         case 0:
581           break;
582
583         case VNET_API_ERROR_NO_SUCH_FIB:
584           return clib_error_return (0, "Invalid table-id %d", table_id);
585
586         case VNET_API_ERROR_INVALID_ARGUMENT:
587           return clib_error_return (0, "Invalid address range %U - %U",
588                                     format_ip4_address, &v4start,
589                                     format_ip4_address, &v4end);
590         default:
591           return clib_error_return (0, "error %d", rv);
592           break;
593         }
594     }
595   if (v6set)
596     {
597       rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
598                                                   table_id);
599       switch (rv)
600         {
601         case 0:
602           break;
603
604         case VNET_API_ERROR_NO_SUCH_FIB:
605           return clib_error_return (0, "Invalid table-id %d", table_id);
606
607         default:
608           return clib_error_return (0, "error %d", rv);
609           break;
610         }
611     }
612   return 0;
613 }
614
615 /* *INDENT-OFF* */
616 VLIB_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 };
622 /* *INDENT-ON* */
623
624 static u8 *
625 tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
626 {
627 #if TCP_SCOREBOARD_TRACE
628
629   scoreboard_trace_elt_t *block;
630   int i = 0;
631
632   if (!sb->trace)
633     return s;
634
635   s = format (s, "scoreboard trace:");
636   vec_foreach (block, sb->trace)
637   {
638     s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
639                 block->ack, block->snd_una_max, block->group);
640     if ((++i % 3) == 0)
641       s = format (s, "\n");
642   }
643   return s;
644 #else
645   return 0;
646 #endif
647 }
648
649 static clib_error_t *
650 tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
651                               vlib_cli_command_t * cmd_arg)
652 {
653   transport_connection_t *tconn = 0;
654   tcp_connection_t *tc;
655   u8 *s = 0;
656   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
657     {
658       if (unformat (input, "%U", unformat_transport_connection, &tconn,
659                     TRANSPORT_PROTO_TCP))
660         ;
661       else
662         return clib_error_return (0, "unknown input `%U'",
663                                   format_unformat_error, input);
664     }
665
666   if (!TCP_SCOREBOARD_TRACE)
667     {
668       vlib_cli_output (vm, "scoreboard tracing not enabled");
669       return 0;
670     }
671
672   tc = tcp_get_connection_from_transport (tconn);
673   s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
674   vlib_cli_output (vm, "%v", s);
675   return 0;
676 }
677
678 /* *INDENT-OFF* */
679 VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
680 {
681   .path = "show tcp scoreboard trace",
682   .short_help = "show tcp scoreboard trace <connection>",
683   .function = tcp_show_scoreboard_trace_fn,
684 };
685 /* *INDENT-ON* */
686
687 u8 *
688 tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
689 {
690   int i, trace_len;
691   scoreboard_trace_elt_t *trace;
692   u32 next_ack, left, group, has_new_ack = 0;
693   tcp_connection_t _placeholder_tc, *placeholder_tc = &_placeholder_tc;
694   sack_block_t *block;
695
696   if (!TCP_SCOREBOARD_TRACE)
697     {
698       s = format (s, "scoreboard tracing not enabled");
699       return s;
700     }
701
702   if (!tc)
703     return s;
704
705   clib_memset (placeholder_tc, 0, sizeof (*placeholder_tc));
706   tcp_connection_timers_init (placeholder_tc);
707   scoreboard_init (&placeholder_tc->sack_sb);
708   placeholder_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
709
710 #if TCP_SCOREBOARD_TRACE
711   trace = tc->sack_sb.trace;
712   trace_len = vec_len (tc->sack_sb.trace);
713 #endif
714
715   for (i = 0; i < trace_len; i++)
716     {
717       if (trace[i].ack != 0)
718         {
719           placeholder_tc->snd_una = trace[i].ack - 1448;
720           placeholder_tc->snd_una_max = trace[i].ack;
721         }
722     }
723
724   left = 0;
725   while (left < trace_len)
726     {
727       group = trace[left].group;
728       vec_reset_length (placeholder_tc->rcv_opts.sacks);
729       has_new_ack = 0;
730       while (trace[left].group == group)
731         {
732           if (trace[left].ack != 0)
733             {
734               if (verbose)
735                 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
736                             trace[left].ack, trace[left].snd_una_max);
737               placeholder_tc->snd_una_max = trace[left].snd_una_max;
738               next_ack = trace[left].ack;
739               has_new_ack = 1;
740             }
741           else
742             {
743               if (verbose)
744                 s = format (s, "[%u, %u], ", trace[left].start,
745                             trace[left].end);
746               vec_add2 (placeholder_tc->rcv_opts.sacks, block, 1);
747               block->start = trace[left].start;
748               block->end = trace[left].end;
749             }
750           left++;
751         }
752
753       /* Push segments */
754       tcp_rcv_sacks (placeholder_tc, next_ack);
755       if (has_new_ack)
756         placeholder_tc->snd_una = next_ack;
757
758       if (verbose)
759         s = format (s, "result: %U", format_tcp_scoreboard,
760                     &placeholder_tc->sack_sb);
761
762     }
763   s =
764     format (s, "result: %U", format_tcp_scoreboard, &placeholder_tc->sack_sb);
765
766   return s;
767 }
768
769 static clib_error_t *
770 tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
771                          vlib_cli_command_t * cmd_arg)
772 {
773   transport_connection_t *tconn = 0;
774   tcp_connection_t *tc = 0;
775   u8 *str = 0;
776   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
777     {
778       if (unformat (input, "%U", unformat_transport_connection, &tconn,
779                     TRANSPORT_PROTO_TCP))
780         ;
781       else
782         return clib_error_return (0, "unknown input `%U'",
783                                   format_unformat_error, input);
784     }
785
786   if (!TCP_SCOREBOARD_TRACE)
787     {
788       vlib_cli_output (vm, "scoreboard tracing not enabled");
789       return 0;
790     }
791
792   tc = tcp_get_connection_from_transport (tconn);
793   if (!tc)
794     {
795       vlib_cli_output (vm, "connection not found");
796       return 0;
797     }
798   str = tcp_scoreboard_replay (str, tc, 1);
799   vlib_cli_output (vm, "%v", str);
800   return 0;
801 }
802
803 /* *INDENT-OFF* */
804 VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
805 {
806   .path = "tcp replay scoreboard",
807   .short_help = "tcp replay scoreboard <connection>",
808   .function = tcp_scoreboard_trace_fn,
809 };
810 /* *INDENT-ON* */
811
812 static clib_error_t *
813 show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
814                   vlib_cli_command_t * cmd_arg)
815 {
816   tcp_main_t *tm = vnet_get_tcp_main ();
817   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
818     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
819                               input);
820   vlib_cli_output (vm, "IPv4 TCP punt: %s",
821                    tm->punt_unknown4 ? "enabled" : "disabled");
822   vlib_cli_output (vm, "IPv6 TCP punt: %s",
823                    tm->punt_unknown6 ? "enabled" : "disabled");
824   return 0;
825 }
826 /* *INDENT-OFF* */
827 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
828 {
829   .path = "show tcp punt",
830   .short_help = "show tcp punt",
831   .function = show_tcp_punt_fn,
832 };
833 /* *INDENT-ON* */
834
835 static clib_error_t *
836 show_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
837                    vlib_cli_command_t * cmd)
838 {
839   tcp_main_t *tm = vnet_get_tcp_main ();
840   tcp_worker_ctx_t *wrk;
841   u32 thread;
842
843   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
844     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
845                               input);
846   for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
847     {
848       wrk = tcp_get_worker (thread);
849       vlib_cli_output (vm, "Thread %u:\n", thread);
850
851       if (clib_fifo_elts (wrk->pending_timers))
852         vlib_cli_output (vm, " %lu pending timers",
853                          clib_fifo_elts (wrk->pending_timers));
854
855 #define _(name,type,str)                                        \
856   if (wrk->stats.name)                                          \
857     vlib_cli_output (vm, " %lu %s", wrk->stats.name, str);
858       foreach_tcp_wrk_stat
859 #undef _
860     }
861
862   return 0;
863 }
864
865 /* *INDENT-OFF* */
866 VLIB_CLI_COMMAND (show_tcp_stats_command, static) =
867 {
868   .path = "show tcp stats",
869   .short_help = "show tcp stats",
870   .function = show_tcp_stats_fn,
871 };
872 /* *INDENT-ON* */
873
874 static clib_error_t *
875 clear_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
876                     vlib_cli_command_t * cmd)
877 {
878   tcp_main_t *tm = vnet_get_tcp_main ();
879   tcp_worker_ctx_t *wrk;
880   u32 thread;
881
882   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
883     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
884                               input);
885
886   for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
887     {
888       wrk = tcp_get_worker (thread);
889       clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
890     }
891
892   return 0;
893 }
894
895 /* *INDENT-OFF* */
896 VLIB_CLI_COMMAND (clear_tcp_stats_command, static) =
897 {
898   .path = "clear tcp stats",
899   .short_help = "clear tcp stats",
900   .function = clear_tcp_stats_fn,
901 };
902 /* *INDENT-ON* */
903
904 static void
905 tcp_show_half_open (vlib_main_t * vm, u32 start, u32 end, u8 verbose)
906 {
907   tcp_main_t *tm = &tcp_main;
908   u8 output_suppressed = 0;
909   u32 n_elts, count = 0;
910   tcp_connection_t *tc;
911   int max_index, i;
912
913   n_elts = pool_elts (tm->half_open_connections);
914   max_index = clib_max (pool_len (tm->half_open_connections), 1) - 1;
915   if (verbose && end == ~0 && n_elts > 50)
916     {
917       vlib_cli_output (vm, "Too many connections, use range <start> <end>");
918       return;
919     }
920
921   if (!verbose)
922     {
923       vlib_cli_output (vm, "%u tcp half-open connections", n_elts);
924       return;
925     }
926
927   for (i = start; i <= clib_min (end, max_index); i++)
928     {
929       if (pool_is_free_index (tm->half_open_connections, i))
930         continue;
931
932       tc = pool_elt_at_index (tm->half_open_connections, i);
933
934       count += 1;
935       if (verbose)
936         {
937           if (count > 50 || (verbose > 1 && count > 10))
938             {
939               output_suppressed = 1;
940               continue;
941             }
942         }
943       vlib_cli_output (vm, "%U", format_tcp_connection, tc, verbose);
944     }
945   if (!output_suppressed)
946     vlib_cli_output (vm, "%u tcp half-open connections", n_elts);
947   else
948     vlib_cli_output (vm, "%u tcp half-open connections matched. Output "
949                      "suppressed. Use finer grained filter.", count);
950
951 }
952
953 static clib_error_t *
954 show_tcp_half_open_fn (vlib_main_t * vm, unformat_input_t * input,
955                        vlib_cli_command_t * cmd)
956 {
957   unformat_input_t _line_input, *line_input = &_line_input;
958   u32 start, end = ~0, verbose = 0;
959   clib_error_t *error = 0;
960
961   session_cli_return_if_not_enabled ();
962
963   if (!unformat_user (input, unformat_line_input, line_input))
964     {
965       tcp_show_half_open (vm, 0, ~0, 0);
966       return 0;
967     }
968
969   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
970     {
971       if (unformat (line_input, "range %u %u", &start, &end))
972         ;
973       else if (unformat (line_input, "verbose %d", &verbose))
974         ;
975       else if (unformat (line_input, "verbose"))
976         verbose = 1;
977       else
978         {
979           error = clib_error_return (0, "unknown input `%U'",
980                                      format_unformat_error, input);
981           goto done;
982         }
983     }
984
985   if (start > end)
986     {
987       error = clib_error_return (0, "invalid range start: %u end: %u", start,
988                                  end);
989       goto done;
990     }
991
992   tcp_show_half_open (vm, start, end, verbose);
993
994 done:
995   unformat_free (line_input);
996   return error;
997 }
998
999 /* *INDENT-OFF* */
1000 VLIB_CLI_COMMAND (show_tcp_half_open_command, static) =
1001 {
1002   .path = "show tcp half-open",
1003   .short_help = "show tcp half-open [verbose <n>] [range <start> <end>]",
1004   .function = show_tcp_half_open_fn,
1005 };
1006 /* *INDENT-ON* */
1007
1008 uword
1009 unformat_tcp_cc_algo (unformat_input_t * input, va_list * va)
1010 {
1011   tcp_cc_algorithm_type_e *result = va_arg (*va, tcp_cc_algorithm_type_e *);
1012   tcp_main_t *tm = &tcp_main;
1013   char *cc_algo_name;
1014   u8 found = 0;
1015   uword *p;
1016
1017   if (unformat (input, "%s", &cc_algo_name)
1018       && ((p = hash_get_mem (tm->cc_algo_by_name, cc_algo_name))))
1019     {
1020       *result = *p;
1021       found = 1;
1022     }
1023
1024   vec_free (cc_algo_name);
1025   return found;
1026 }
1027
1028 uword
1029 unformat_tcp_cc_algo_cfg (unformat_input_t * input, va_list * va)
1030 {
1031   tcp_main_t *tm = vnet_get_tcp_main ();
1032   tcp_cc_algorithm_t *cc_alg;
1033   unformat_input_t sub_input;
1034   int found = 0;
1035
1036   vec_foreach (cc_alg, tm->cc_algos)
1037   {
1038     if (!unformat (input, cc_alg->name))
1039       continue;
1040
1041     if (cc_alg->unformat_cfg
1042         && unformat (input, "%U", unformat_vlib_cli_sub_input, &sub_input))
1043       {
1044         if (cc_alg->unformat_cfg (&sub_input))
1045           found = 1;
1046       }
1047   }
1048   return found;
1049 }
1050
1051 static clib_error_t *
1052 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1053 {
1054   u32 cwnd_multiplier, tmp_time, mtu, max_gso_size;
1055   uword memory_size;
1056
1057   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1058     {
1059       if (unformat (input, "preallocated-connections %d",
1060                     &tcp_cfg.preallocated_connections))
1061         ;
1062       else if (unformat (input, "preallocated-half-open-connections %d",
1063                          &tcp_cfg.preallocated_half_open_connections))
1064         ;
1065       else if (unformat (input, "buffer-fail-fraction %f",
1066                          &tcp_cfg.buffer_fail_fraction))
1067         ;
1068       else if (unformat (input, "max-rx-fifo %U", unformat_memory_size,
1069                          &memory_size))
1070         {
1071           if (memory_size >= 0x100000000)
1072             {
1073               return clib_error_return
1074                 (0, "max-rx-fifo %llu (0x%llx) too large", memory_size,
1075                  memory_size);
1076             }
1077           tcp_cfg.max_rx_fifo = memory_size;
1078         }
1079       else if (unformat (input, "min-rx-fifo %U", unformat_memory_size,
1080                          &memory_size))
1081         {
1082           if (memory_size >= 0x100000000)
1083             {
1084               return clib_error_return
1085                 (0, "min-rx-fifo %llu (0x%llx) too large", memory_size,
1086                  memory_size);
1087             }
1088           tcp_cfg.min_rx_fifo = memory_size;
1089         }
1090       else if (unformat (input, "mtu %u", &mtu))
1091         tcp_cfg.default_mtu = mtu;
1092       else if (unformat (input, "rwnd-min-update-ack %d",
1093                          &tcp_cfg.rwnd_min_update_ack))
1094         ;
1095       else if (unformat (input, "initial-cwnd-multiplier %u",
1096                          &cwnd_multiplier))
1097         tcp_cfg.initial_cwnd_multiplier = cwnd_multiplier;
1098       else if (unformat (input, "no-tx-pacing"))
1099         tcp_cfg.enable_tx_pacing = 0;
1100       else if (unformat (input, "tso"))
1101         tcp_cfg.allow_tso = 1;
1102       else if (unformat (input, "no-csum-offload"))
1103         tcp_cfg.csum_offload = 0;
1104       else if (unformat (input, "max-gso-size %u", &max_gso_size))
1105         tcp_cfg.max_gso_size = clib_min (max_gso_size, TCP_MAX_GSO_SZ);
1106       else if (unformat (input, "cc-algo %U", unformat_tcp_cc_algo,
1107                          &tcp_cfg.cc_algo))
1108         ;
1109       else if (unformat (input, "%U", unformat_tcp_cc_algo_cfg))
1110         ;
1111       else if (unformat (input, "closewait-time %u", &tmp_time))
1112         tcp_cfg.closewait_time = tmp_time / TCP_TIMER_TICK;
1113       else if (unformat (input, "timewait-time %u", &tmp_time))
1114         tcp_cfg.timewait_time = tmp_time / TCP_TIMER_TICK;
1115       else if (unformat (input, "finwait1-time %u", &tmp_time))
1116         tcp_cfg.finwait1_time = tmp_time / TCP_TIMER_TICK;
1117       else if (unformat (input, "finwait2-time %u", &tmp_time))
1118         tcp_cfg.finwait2_time = tmp_time / TCP_TIMER_TICK;
1119       else if (unformat (input, "lastack-time %u", &tmp_time))
1120         tcp_cfg.lastack_time = tmp_time / TCP_TIMER_TICK;
1121       else if (unformat (input, "closing-time %u", &tmp_time))
1122         tcp_cfg.closing_time = tmp_time / TCP_TIMER_TICK;
1123       else if (unformat (input, "cleanup-time %u", &tmp_time))
1124         tcp_cfg.cleanup_time = tmp_time / 1000.0;
1125       else
1126         return clib_error_return (0, "unknown input `%U'",
1127                                   format_unformat_error, input);
1128     }
1129   return 0;
1130 }
1131
1132 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1133
1134 /*
1135  * fd.io coding-style-patch-verification: ON
1136  *
1137  * Local Variables:
1138  * eval: (c-set-style "gnu")
1139  * End:
1140  */