【问题标题】:Getting CAN interface packet statistics using getifaddrs使用 getifaddrs 获取 CAN 接口数据包统计信息
【发布时间】:2021-10-30 00:07:08
【问题描述】:

我正在使用getifaddrs Linux 函数来获取有关运行 Debian Jessie 内核 3.16.0 的 Linux 机器上的网络接口的信息。

我想知道的信息中有网络统计信息(丢弃、发送的数据包等),正如 getifaddrs 的手册页所述,它包含在 ifaddrs::ifa_addr::ifa_data 指向的 rtnl_link_stats 结构中,而 ifaddrs::if_addr::sa_family 是设置为AF_PACKET

这适用于以太网接口,但不适用于机器上的 CAN 接口,因为 ifaddrs::ifa_addr 为 NULL,因此永远不会为任何 CAN 接口返回 AF_PACKET

以下 C++ 代码...

#include <ifaddrs.h>
#include <linux/if_link.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <cstdio>
#include <cstdlib>

int main() {
  ifaddrs *addrs;
  if (getifaddrs(&addrs) == -1) {
    perror("getifaddrs");
    return EXIT_FAILURE;
  }

  printf("AF_PACKET: %d\n\n", (int)AF_PACKET);

  for (auto addr = addrs; addr != NULL; addr = addr->ifa_next) {
    if (addr->ifa_addr != nullptr) {
      printf("%s: family: %d\n", addr->ifa_name, (int)addr->ifa_addr->sa_family);
    } else {
      printf("%s: family: none\n", addr->ifa_name);
    }
  }

  freeifaddrs(addrs);

  return EXIT_SUCCESS;
}

...打印出来

AF_PACKET: 17

lo: family: 17
eth0: family: 17
eth1: family: 17
can0: family: none
can1: family: none
lo: family: 2
eth1: family: 2
lo: family: 10
eth1: family: 10

这意味着这两个 CAN 接口都没有任何系列集。

ifconfig 命令显示信息没有问题,但是:

can0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:16  Metric:1
          RX packets:28481588 errors:0 dropped:8729866 overruns:0 frame:0
          TX packets:8168599 errors:2292404 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:10 
          RX bytes:211108099 (201.3 MiB)  TX bytes:64828340 (61.8 MiB)
          Interrupt:17 

can1      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:16  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:10 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:17

(注意我只包括了 CAN 接口的输出,而不包括其余部分)

我做错了什么?检索 CAN 接口数据包统计信息的正确方法是什么?

【问题讨论】:

  • 我对CAN一无所知,也没有设备-但我怀疑如果您使用netlink套接字调用RTM_GETLINK,信息可能可用,并将rta_type 设置为 IFLA_STATS。设置起来有点麻烦,但起点是man7.org/linux/man-pages/man7/rtnetlink.7.html
  • 数据包概念是否适用于 CAN? CAN 报文由一个具有优先级的报头组成。如果标头与其他设备发生冲突,则整个电报将停止。这是在 MCU 上的硬件 CAN 协议处理程序中完成的。它是纯硬件!

标签: c++ linux networking can-bus


【解决方案1】:

在“ifconfig”命令中使用“address_family”选项;检查您的“man ifconfig”页面,这是我的相关文本(我的系统上没有 CAN intfs):

...
     address_family
             Specify the address family which affects interpretation of the remaining parameters.  Since an interface
             can receive transmissions in differing protocols with different naming schemes, specifying the address
             family is recommended.  The address or protocol families currently supported are ``inet'', ``inet6'', and
             ``link''.  The default is ``inet''.  ``ether'' and ``lladdr'' are synonyms for ``link''.

【讨论】:

    【解决方案2】:

    我在我的 C 项目 nagios-plugins-linux 中遇到了类似的问题,不得不从非常简单的界面 getifaddrs 切换到 linux rtnetlink,这管理起来要复杂得多,但功能也更强大(getifaddrs 提供如果我没记错的话,仅限AF_PACKET 家庭的网络统计信息)。

    您可以在库文件中找到相关代码:

    我无法检查can0 接口,但我的代码检测到vcan0 接口,无论如何都应该适用于任何网络接口。

    【讨论】:

      【解决方案3】:

      给定 net-tools source code 它会读取 proc 文件系统中的文件 /proc/net/dev 以获取此信息。

      查看lib/interface.c中的以下函数:

      • if_readlist()

      • if_readlist_proc()

      祝你的项目好运!

      EDIT> 连接 CAN 适配器

      文件/proc/net/dev

      Inter-|   Receive                                                |  Transmit
       face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
      slcan0:       0       0    0    0    0     0          0         0      344      43    0    0    0     0       0          0
      

      ifconfig的输出:

      slcan0: flags=193<UP,RUNNING,NOARP>  mtu 16
              unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
              RX packets 0  bytes 0 (0.0 B)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 43  bytes 344 (344.0 B)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      

      【讨论】:

        猜你喜欢
        • 2023-02-08
        • 1970-01-01
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多