title: Capturing berr-counter tx/rx from ip link show
body: <p>I would like to be able to capture the berr-counter values in a shell script. I can view the values with:
  <code>ip -det link show can0</code> which gives:</p>
  
  <pre><code>2: can0: &lt;NOARP,ECHO&gt; mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
      link/can  promiscuity 0
      can state STOPPED (berr-counter tx 144 rx 128) restart-ms 100
            bitrate 125000 sample-point 0.866
            tq 133 prop-seg 6 phase-seg1 6 phase-seg2 2 sjw 1
            flexcan: tseg1 4..16 tseg2 2..8 sjw 1..4 brp 1..256 brp-inc 1
            clock 30000000
  </code></pre>
  
  <p>I could just parse this output and capture the tx/rx berr-counter, but I would rather capture these values directly. So, I have been trying find where to access these values. I dug into <a href="https://github.com/shemminger/iproute2" rel="nofollow noreferrer">https://github.com/shemminger/iproute2</a> 's code and found where these values are being printed in <code>ip/iplink_can.c</code> in the function: </p>
  
  <pre><code>static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
  </code></pre>
  
  <p>There is the code:</p>
  
  <pre><code>if (tb[IFLA_CAN_BERR_COUNTER]) {
      struct can_berr_counter *bc =
          RTA_DATA(tb[IFLA_CAN_BERR_COUNTER]);
  
      fprintf(f, "(berr-counter tx %d rx %d) ", bc-&gt;txerr, bc-&gt;rxerr);
  }
  </code></pre>
  
  <p>And at the bottom of the same file there is a struct:</p>
  
  <pre><code>struct link_util can_link_util = {
      .id     = "can",
      .maxattr    = IFLA_CAN_MAX,
      .parse_opt  = can_parse_opt,
      .print_opt  = can_print_opt,
      .print_xstats   = can_print_xstats,
      .print_help = can_print_help,
  };
  </code></pre>
  
  <p>But I can't find anywhere where <code>can_print_opt</code>, or <code>can_link_util.print_opt</code> are called, and I haven't found any success sifting through all of the <code>struct rtattr</code> in the repo.</p>
  
  <p>I'm not sure where to go from here to get these values other than just grabbing them from the output of <code>ip -det link show can0</code></p>