4

I am having a FreeBSD 11 system in which I get the following output for ifconfig command

# ifconfig 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)

Thanks in advance !

Edit 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

#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;
}
Gagan93
  • 153
  • 1
  • 9
  • Don't know why it is gone (have you tried verbose `-v`?) but here are alternatives: http://superuser.com/questions/197904/how-to-see-network-traffic-per-interface-in-freebsd – eckes Mar 03 '17 at 11:42
  • Which older FreeBSD ? ... as it is already gone with FreeBSD 10.x – Ouki Mar 03 '17 at 11:57
  • @Ouki : For 7.x / 8.x, it was working @eckes : `-v` didn't help with ifconfig – Gagan93 Mar 03 '17 at 12:09
  • @Ouki : Can you tell what was the change in FreeBSD 1.0x which changed this behavior ? – Gagan93 Mar 03 '17 at 12:14
  • Looks related to https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=213814 – Bradley T. Hughes Mar 06 '17 at 14:42
  • not 100% sure @BradleyT.Hughes – Gagan93 Mar 07 '17 at 05:47
  • Got a FreeBSD 7.4 livefs : `ifconfig` was not displaying RX/TX values either. As mentioned by FarazX, FreeBSD `ifconfig` sole purpose is to config and display the network interfaces ... Your problem is not related to `ifconfig`. – Ouki Mar 09 '17 at 05:45

1 Answers1

6

To answer your question, I should say that as far as I recall, FreeBSD - and also macOS - has never displayed RX/TX Packets as an output of ifconfig command. But if you say it was available in 7.x and 8.x, I should say at least in 10 and 11 it is not, maybe I didn't notice that time for 7.x 8.x . Indeed Linux Distributions including RHEL derivatives (RHEL, Fedora, CentOS, Scientific, etc.), Debian-Based operating systems (Ubuntu, Debian Jessie, etc.) and most of other distros show RX/TX Packets via ifconfig, But FreeBSD and macOS do not so. Many of FreeBSD's commands have nothing to do with commands in Linux, many of them are just called in the same way but in fact, act differently in many cases. In the meanwhile, your struct if_data doesn't look pretty familiar - and correct - to me, I recommend you take a look at <net/if.h>. In other words, delete your if_data and try to include the accurate header.

Anyway, to display number of bytes in/out in FreeBSD, you should use netstat. The following command works fine on both FreeBSD and macOS :

netstat -idb [interface]

-i : interface
-d : show the number of dropped packets
-b : show the number of packets in and out

You can also use -B if you need so. It shows statistics about bpf() peer. This includes information like how many packets have been matched, dropped and received by he bpf device, also information about current buffer size and device stats.

By the way, for monitoring network traffic, you have another option which I highly recommend. You can use systat with the -ifstat option to get Traffic throughput, Peak and Total of your interfaces :

systat -ifstat
  • 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 ? – Gagan93 Mar 17 '17 at 08:28
  • @Gagan93 There is no problem with mine and they are non-zero. –  Mar 19 '17 at 11:53
  • The layout of struct if_data changed on FreeBSD 11, probably due to this commit: https://github.com/freebsd/freebsd/commit/b38edcd355dfe9c2ac4080b8837687b0dba7dd41 Recompile from source and it should work. – damjan Dec 16 '17 at 01:07