FreeBSD 11 not showing RX,TX packets and counts using ifconfig/getifaddrs

I am having a FreeBSD 11 system in which I get the following output for ifconfig command
Code:
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
    options=600003<RXCSUM,TXCSUM,RXCSUM_IPV6,TXCSUM_IPV6>
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 127.0.0.1 netmask 0xff000000 
    nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
    groups: lo 
xn0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=503<RXCSUM,TXCSUM,TSO4,LRO>
    ether 0e:c2:a2:36:c1:b4
    inet 10.0.0.71 netmask 0xffffff00 broadcast 10.0.0.255 
    nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    media: Ethernet manual
    status: active


Two doubts here :

  1. Why I am not getting RX, TX bytes and packet count like I get for older FreeBSD / ubuntu systems ? I am using a utility which parses this response to get network usage and it is failing (can't modify, it's a third party binary).
  2. Any changes in FreeBSD 11 because it worked fine in older versions ? I am more interested to fix this or do a config change (if this is being controlled by some .conf file) rather than changing my method of monitoring (eg. parsing response from iftop or some other command)

    Specifically, a C library is being used in that binary to get stats which are coming out to be zero. I am attaching a sample code which is also returning zero values for rx/tx bytes because that information is not available. It uses getifaddrs function from sys/sockets library

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <ifaddrs.h>
    
     struct if_data {
      unsigned char  ifi_type;
      unsigned char  ifi_physical;
      unsigned char  ifi_addrlen;
      unsigned char  ifi_hdrlen;
      unsigned char  ifi_recvquota;
      unsigned char  ifi_xmitquota;
      unsigned long  ifi_mtu;
      unsigned long  ifi_metric;
      unsigned long  ifi_baudrate;
    
      unsigned long  ifi_ipackets;
      unsigned long  ifi_ierrors;
      unsigned long  ifi_opackets;
      unsigned long  ifi_oerrors;
      unsigned long  ifi_collisions;
      unsigned long  ifi_ibytes;
      unsigned long  ifi_obytes;
      unsigned long  ifi_imcasts;
      unsigned long  ifi_omcasts;
      unsigned long  ifi_iqdrops;
      unsigned long  ifi_noproto;
      unsigned long  ifi_recvtiming;
      unsigned long  ifi_xmittiming;
      struct  timeval ifi_lastchange;
    };
    
    
    int main()
    {
      struct ifaddrs    *ifap, *ifa;
      struct if_data    *ifadata = NULL;
      char *dev_name;
    
      if (getifaddrs(&ifap) < 0) {
        printf ("returning  for null");
        return 1;
      }
    
      for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    
        if (ifa->ifa_flags & 0x01) {
          if ( ifa->ifa_addr->sa_family == AF_LINK) {
            if (ifa->ifa_data) {
              ifadata = (struct if_data *)ifa->ifa_data;
              dev_name = ifa->ifa_name;
    
              if (ifadata->ifi_ipackets == 0 && ifadata->ifi_opackets == 0)
              {
                printf("returning as zero for %s", dev_name);
                continue;
              }
    
              printf("name=%s ipkts=%ld opkts=%ld\n", dev_name,
                ifadata->ifi_ipackets, ifadata->ifi_opackets);
    
              printf("%lu", ifadata->ifi_ibytes);
              printf("%lu", ifadata->ifi_ipackets);
              printf("%lu",  ifadata->ifi_ierrors);
              printf("%lu", ifadata->ifi_iqdrops);
              printf("%lu", ifadata->ifi_imcasts);
              printf("%lu", ifadata->ifi_obytes);
              printf("%lu", ifadata->ifi_opackets);
              printf("%lu",  ifadata->ifi_oerrors);
            }
          }
        }
      }
      freeifaddrs(ifap);
      return 0;
    }

    These values are coming out to be zero (printf values) in FreeBSD 11 but returns expected results for FreeBSD 9
 
It never did? The ifconfig(8) command never showed this information.
This is response from my local ubuntu 14 system. Notice RX/TX packets and bytes information.
Code:
          wlan2     Link encap:Ethernet  HWaddr 34:02:86:bb:61:65  
          inet addr:192.168.1.228  Bcast:192.168.3.255  Mask:255.255.252.0
          inet6 addr: fe80::3602:86ff:febb:6165/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3920211 errors:0 dropped:2 overruns:0 frame:0
          TX packets:2842777 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2606010158 (2.6 GB)  TX bytes:743731183 (743.7 MB)

Also, I am more interested to fix behavior of getifaddrs function
 
This is response from my local ubuntu 14 system.
FreeBSD's ifconfig(8) has nothing to do with Linux' ifconfig.

struct if_data {
Where did you get that definition of struct if_data from? FreeBSD 11.0's version looks different and that is likely the source of your problem. It's defined in <net/if.h>. Your program works fine if I delete your if_data definition and just include the correct header.
 
FreeBSD's ifconfig(8) has nothing to do with Linux' ifconfig.


Where did you get that definition of struct if_data from? FreeBSD 11.0's version looks different and that is likely the source of your problem. It's defined in <net/if.h>. Your program works fine if I delete your if_data definition and just include the correct header.

Thanks, that solved the issue. On using struct from net/if.h, there's another issue. I am getting input bytes and input packets fine. But for output (tx), output bytes are coming out to be zero while output packets is non zero. My calculations based on tx bytes are going wrong as this is coming out to be zero even when I create some tx traffic. Can you check once if this is a FreeBSD 11 issue ?
 
My calculations based on tx bytes are going wrong as this is coming out to be zero even when I create some tx traffic. Can you check once if this is a FreeBSD 11 issue ?
This works fine for me (i.e. ifi_obytes and ifi_opkts are both non-zero when I generate some outgoing traffic) on FreeBSD 11.0 and 12.0-CURRENT.
 
Back
Top