@@ -57,7 +57,19 @@ enum NetCommand {
5757 /// Print the link status
5858 Status ,
5959 /// Print the counters
60- Counters ,
60+ ///
61+ /// This is a destructive operation that clears counter values, resetting
62+ /// them to zero.
63+ ///
64+ /// If no flags are provided, both the table and diagram are shown.
65+ Counters {
66+ /// Show a table of raw counter values
67+ #[ clap( short, long) ]
68+ table : bool ,
69+ /// Show a diagram of good packet counts in context
70+ #[ clap( short, long) ]
71+ diagram : bool ,
72+ } ,
6173}
6274
6375#[ derive( Parser , Debug ) ]
@@ -342,7 +354,11 @@ fn net_status(context: &mut ExecutionContext) -> Result<()> {
342354 Ok ( ( ) )
343355}
344356
345- fn net_counters ( context : & mut ExecutionContext ) -> Result < ( ) > {
357+ fn net_counters (
358+ context : & mut ExecutionContext ,
359+ table : bool ,
360+ diagram : bool ,
361+ ) -> Result < ( ) > {
346362 let Subcommand :: Other ( subargs) = context. cli . cmd . as_ref ( ) . unwrap ( ) ;
347363 let subargs = NetArgs :: try_parse_from ( subargs) ?;
348364
@@ -369,6 +385,22 @@ fn net_counters(context: &mut ExecutionContext) -> Result<()> {
369385 let s = v. as_struct ( ) ?;
370386 assert_eq ! ( s. name( ) , "ManagementCounters" ) ;
371387
388+ if !table && !diagram {
389+ net_counters_table ( s) ?;
390+ println ! ( ) ;
391+ net_counters_diagram ( s) ?;
392+ } else {
393+ if table {
394+ net_counters_table ( s) ?;
395+ }
396+ if diagram {
397+ net_counters_diagram ( s) ?;
398+ }
399+ }
400+ Ok ( ( ) )
401+ }
402+
403+ fn net_counters_table ( s : & Struct ) -> Result < ( ) > {
372404 let k_tx = s[ "ksz8463_tx" ] . as_array ( ) ?;
373405 let k_rx = s[ "ksz8463_rx" ] . as_array ( ) ?;
374406 let value = |k : & Struct , s : & str | {
@@ -473,6 +505,89 @@ fn net_counters(context: &mut ExecutionContext) -> Result<()> {
473505 Ok ( ( ) )
474506}
475507
508+ fn net_counters_diagram ( s : & Struct ) -> Result < ( ) > {
509+ let k_tx = s[ "ksz8463_tx" ] . as_array ( ) ?;
510+ let k_rx = s[ "ksz8463_rx" ] . as_array ( ) ?;
511+ let value = |k : & Struct , s : & str | k[ s] . as_base ( ) . unwrap ( ) . as_u32 ( ) . unwrap ( ) ;
512+
513+ let mut ksz_tx = [ 0 ; 3 ] ;
514+ let mut ksz_rx = [ 0 ; 3 ] ;
515+ for port in 0 ..3 {
516+ let k_tx = k_tx[ port] . as_struct ( ) ?;
517+ let k_rx = k_rx[ port] . as_struct ( ) ?;
518+ for t in [ "unicast" , "broadcast" , "multicast" ] {
519+ ksz_tx[ port] += value ( k_tx, t) ;
520+ ksz_rx[ port] += value ( k_rx, t) ;
521+ }
522+ }
523+
524+ let v_tx = s[ "vsc85x2_tx" ] . as_array ( ) ?;
525+ let v_rx = s[ "vsc85x2_rx" ] . as_array ( ) ?;
526+ let v_mac_valid = s[ "vsc85x2_mac_valid" ] . as_base ( ) ?. as_bool ( ) . unwrap ( ) ;
527+ let value = |v : & Struct , s : & str | v[ s] . as_base ( ) . unwrap ( ) . as_u16 ( ) . unwrap ( ) ;
528+
529+ let mut v_mac_tx = [ 0 ; 2 ] ;
530+ let mut v_mac_rx = [ 0 ; 2 ] ;
531+ let mut v_media_tx = [ 0 ; 2 ] ;
532+ let mut v_media_rx = [ 0 ; 2 ] ;
533+ for port in 0 ..2 {
534+ let v_tx = v_tx[ port] . as_struct ( ) ?;
535+ let v_rx = v_rx[ port] . as_struct ( ) ?;
536+ if v_mac_valid {
537+ v_mac_tx[ port] = value ( v_tx, "mac_good" ) ;
538+ v_mac_rx[ port] = value ( v_rx, "mac_good" ) ;
539+ }
540+ v_media_tx[ port] = value ( v_tx, "media_good" ) ;
541+ v_media_rx[ port] = value ( v_rx, "media_good" ) ;
542+ }
543+
544+ let mac = |i : u16 | {
545+ if v_mac_valid {
546+ i. to_string ( ) . green ( )
547+ } else {
548+ "--" . to_string ( ) . dimmed ( )
549+ }
550+ } ;
551+
552+ println ! (
553+ " ┌──────────────────┐ ┌───────────────────┐
554+ │ {} │ │ {} │
555+ │ │tx rx│ │tx
556+ │ {:>6}├───────────►│{:<6} {:>6}├───────►
557+ │ │1 0│ │0
558+ ┌────┐ rx│ {:>6}│◄───────────┤{:<6} {:>6}│◄───────
559+ │ ├─────►│{:<6} │rx tx│ │rx
560+ │ SP │ 3│ │ │ │
561+ │ │◄─────┤{:<6} │tx rx│ │tx
562+ └────┘ tx│ {:>6}├───────────►│{:<6} {:>6}├───────►
563+ │ │2 1│ │1
564+ │ {:>6}│◄───────────┤{:<6} {:>6}│◄───────
565+ │ │rx tx│ │rx
566+ └──────────────────┘ └───────────────────┘
567+ {} {}
568+ " ,
569+ "KSZ8463" . bold( ) ,
570+ "VSC85x2" . bold( ) ,
571+ ksz_tx[ 0 ] . to_string( ) . green( ) ,
572+ v_media_rx[ 0 ] . to_string( ) . green( ) ,
573+ mac( v_mac_tx[ 0 ] ) ,
574+ ksz_rx[ 0 ] . to_string( ) . green( ) ,
575+ v_media_tx[ 0 ] . to_string( ) . green( ) ,
576+ mac( v_mac_rx[ 0 ] ) ,
577+ ksz_rx[ 2 ] . to_string( ) . green( ) ,
578+ ksz_tx[ 2 ] . to_string( ) . green( ) ,
579+ ksz_tx[ 1 ] . to_string( ) . green( ) ,
580+ v_media_rx[ 1 ] . to_string( ) . green( ) ,
581+ mac( v_mac_tx[ 1 ] ) ,
582+ ksz_rx[ 1 ] . to_string( ) . green( ) ,
583+ v_media_tx[ 1 ] . to_string( ) . green( ) ,
584+ mac( v_mac_rx[ 1 ] ) ,
585+ "MEDIA" . dimmed( ) ,
586+ "MAC" . dimmed( )
587+ ) ;
588+ Ok ( ( ) )
589+ }
590+
476591fn net ( context : & mut ExecutionContext ) -> Result < ( ) > {
477592 let Subcommand :: Other ( subargs) = context. cli . cmd . as_ref ( ) . unwrap ( ) ;
478593 let subargs = NetArgs :: try_parse_from ( subargs) ?;
@@ -481,7 +596,9 @@ fn net(context: &mut ExecutionContext) -> Result<()> {
481596 NetCommand :: Mac => net_mac_table ( context) ?,
482597 NetCommand :: Ip => net_ip ( context) ?,
483598 NetCommand :: Status => net_status ( context) ?,
484- NetCommand :: Counters => net_counters ( context) ?,
599+ NetCommand :: Counters { table, diagram } => {
600+ net_counters ( context, table, diagram) ?
601+ }
485602 }
486603 Ok ( ( ) )
487604}
0 commit comments