perfstat_protocol Interface

The perfstat_protocol interface returns a set of structures of type perfstat_protocol_t, which consists of a set of unions to accommodate the different sets of fields needed for each protocol, as defined in the libperfstat.h file.

Selected fields from the perfstat_protocol_t structure include:
Item Descriptor
name Protocol name, which can be any of the following values: ip, ip6, icmp, icmp6, udp, tcp, rpc, nfs, nfsv2, or nfsv3.
ipackets Number of input packets received using this protocol. This field exists only for protocols ip, ipv6, udp, and tcp.
opackets Number of output packets sent using this protocol. This field exists only for protocols ip, ipv6, udp, and tcp.
received Number of packets received using this protocol. This field exists only for protocols icmp and icmpv6.
calls Number of calls made to this protocol. This field exists only for protocols rpc, nfs, nfsv2, and nfsv3.
Many other network-protocol related metrics are also returned. For a complete list of network-protocol related metrics, see the perfstat_protocol_t section in the libperfstat.h header file.
The following code shows an example of how the perfstat_protocol interface is used:
#include <stdio.h>
#include <string.h>
#include <libperfstat.h>

int main(int argc, char* argv[]) {
    int ret, tot, retrieved = 0;
    perfstat_protocol_t pinfo;
    perfstat_id_t protid;

    /* check how many perfstat_protocol_t structures are available */
    tot = perfstat_protocol(NULL, NULL, sizeof(perfstat_protocol_t), 0);

    /* check for error */
    if (tot <= 0)
    {
     perror("perfstat_protocol");
	  exit(-1);
    } 
   
    printf("number of protocol usage structures available : %d\n", tot);

    /* set name to first protocol */
    strcpy(protid.name, FIRST_PROTOCOL);

    /* retrieve first protocol usage information */
    ret = perfstat_protocol(&protid, &pinfo, sizeof(perfstat_protocol_t), 1);
    
    if (ret < 0)
    {
	  perror("perfstat_protocol");
	  exit(-1);
    }
   retrieved += ret;
    do {
       printf("\nStatistics for protocol : %s\n", pinfo.name);
       printf("-----------------------\n");

       if (!strcmp(pinfo.name,"ip")) {
           printf("number of input packets  : %llu\n", pinfo.u.ip.ipackets);
           printf("number of input errors   : %llu\n", pinfo.u.ip.ierrors);
           printf("number of output packets : %llu\n", pinfo.u.ip.opackets);
           printf("number of output errors  : %llu\n", pinfo.u.ip.oerrors);
       } else if (!strcmp(pinfo.name,"ipv6")) {
           printf("number of input packets  : %llu\n", pinfo.u.ipv6.ipackets);
           printf("number of input errors   : %llu\n", pinfo.u.ipv6.ierrors);
           printf("number of output packets : %llu\n", pinfo.u.ipv6.opackets);
           printf("number of output errors  : %llu\n", pinfo.u.ipv6.oerrors);
       } else if (!strcmp(pinfo.name,"icmp")) {
           printf("number of packets received : %llu\n", pinfo.u.icmp.received);
           printf("number of packets sent     : %llu\n", pinfo.u.icmp.sent);
           printf("number of errors           : %llu\n", pinfo.u.icmp.errors);
       } else if (!strcmp(pinfo.name,"icmpv6")) {
           printf("number of packets received : %llu\n", pinfo.u.icmpv6.received);
           printf("number of packets sent     : %llu\n", pinfo.u.icmpv6.sent);
           printf("number of errors           : %llu\n", pinfo.u.icmpv6.errors);
       } else if (!strcmp(pinfo.name,"udp")) {
           printf("number of input packets  : %llu\n", pinfo.u.udp.ipackets);
           printf("number of input errors   : %llu\n", pinfo.u.udp.ierrors);
           printf("number of output packets : %llu\n", pinfo.u.udp.opackets);
       } else if (!strcmp(pinfo.name,"tcp")) {
           printf("number of input packets  : %llu\n", pinfo.u.tcp.ipackets);
           printf("number of input errors   : %llu\n", pinfo.u.tcp.ierrors);
           printf("number of output packets : %llu\n", pinfo.u.tcp.opackets);
       } else if (!strcmp(pinfo.name,"rpc")) {
           printf("client statistics:\n");
           printf("number of connection-oriented RPC requests  : %llu\n",
                  pinfo.u.rpc.client.stream.calls);
           printf("number of rejected connection-oriented RPCs : %llu\n",
                  pinfo.u.rpc.client.stream.badcalls);
           printf("number of connectionless RPC requests       : %llu\n",
                  pinfo.u.rpc.client.dgram.calls);
           printf("number of rejected connectionless RPCs      : %llu\n",
                  pinfo.u.rpc.client.dgram.badcalls);
           printf("\nserver statistics:\n");
           printf("number of connection-oriented RPC requests  : %llu\n",
                  pinfo.u.rpc.server.stream.calls);
           printf("number of rejected connection-oriented RPCs : %llu\n",
                  pinfo.u.rpc.server.stream.badcalls);
           printf("number of connectionless RPC requests       : %llu\n",
                  pinfo.u.rpc.server.dgram.calls);
           printf("number of rejected connectionless RPCs      : %llu\n",
                  pinfo.u.rpc.server.dgram.badcalls);
       } else if (!strcmp(pinfo.name,"nfs")) {
           printf("total number of NFS client requests        : %llu\n",
                  pinfo.u.nfs.client.calls);
           printf("total number of NFS client failed calls    : %llu\n",
                  pinfo.u.nfs.client.badcalls);
           printf("total number of NFS server requests        : %llu\n",
                  pinfo.u.nfs.server.calls);
           printf("total number of NFS server failed calls    : %llu\n",
                  pinfo.u.nfs.server.badcalls);
           printf("total number of NFS version 2 server calls : %llu\n",
                  pinfo.u.nfs.server.public_v2);
           printf("total number of NFS version 3 server calls : %llu\n",
                  pinfo.u.nfs.server.public_v3);
       } else if (!strcmp(pinfo.name,"nfsv2")) {
           printf("number of NFS V2 client requests : %llu\n",
                  pinfo.u.nfsv2.client.calls);
           printf("number of NFS V2 server requests : %llu\n",
                  pinfo.u.nfsv2.server.calls);
       } else if (!strcmp(pinfo.name,"nfsv3")) {
           printf("number of NFS V3 client requests : %llu\n",
                  pinfo.u.nfsv3.client.calls);
           printf("number of NFS V3 server requests : %llu\n",
                  pinfo.u.nfsv3.server.calls);
       }

       /* make sure we stop after the last protocol */
       if (ret = strcmp(protid.name, "")) {
           printf("\nnext protocol name : %s\n", protid.name);

           /* retrieve information for next protocol */
           ret = perfstat_protocol(&protid, &pinfo, sizeof(perfstat_protocol_t), 1);
	   if (ret < 0)
           {
		      perror("perfstat_protocol");
		      exit(-1);
           }
           retrieved += ret;
       }
    } while (ret == 1);

   printf("\nnumber of protocol usage structures retrieved : %d\n", retrieved);
}
The program displays an output that is similar to the following example output:
number of protocol usage structures available : 11

Statistics for protocol : ip
-----------------------
number of input packets  : 155855
number of input errors   : 32911
number of output packets : 25635
number of output errors  : 32909

next protocol name : ipv6

Statistics for protocol : ipv6
-----------------------
number of input packets  : 0
number of input errors   : 0
number of output packets : 0
number of output errors  : 0

next protocol name : icmp

Statistics for protocol : icmp
-----------------------
number of packets received : 2
number of packets sent     : 1
number of errors           : 1

next protocol name : icmpv6

Statistics for protocol : icmpv6
-----------------------
number of packets received : 0
number of packets sent     : 0
number of errors           : 0

next protocol name : udp

Statistics for protocol : udp
-----------------------
number of input packets  : 106630
number of input errors   : 91625
number of output packets : 14435

next protocol name : tcp

Statistics for protocol : tcp
-----------------------
number of input packets  : 16313
number of input errors   : 0
number of output packets : 11196

next protocol name : rpc

Statistics for protocol : rpc
-----------------------
client statistics:
number of connection-oriented RPC requests  : 41
number of rejected connection-oriented RPCs : 0
number of connectionless RPC requests       : 24
number of rejected connectionless RPCs      : 0

server statistics:
number of connection-oriented RPC requests  : 0
number of rejected connection-oriented RPCs : 0
number of connectionless RPC requests       : 0
number of rejected connectionless RPCs      : 0

next protocol name : nfs

Statistics for protocol : nfs
-----------------------
total number of NFS client requests        : 41
total number of NFS client failed calls    : 0
total number of NFS server requests        : 0
total number of NFS server failed calls    : 0
total number of NFS version 2 server calls : 0
total number of NFS version 3 server calls : 0

next protocol name : nfsv2

Statistics for protocol : nfsv2
-----------------------
number of NFS V2 client requests : 0
number of NFS V2 server requests : 0

next protocol name : nfsv3

Statistics for protocol : nfsv3
-----------------------
number of NFS V3 client requests : 41
number of NFS V3 server requests : 0

next protocol name : nfsv4

Statistics for protocol : nfsv4
-----------------------

number of protocol usage structures retrieved : 11

The preceding program emulates protocolstat behavior and also shows how perfstat_protocol is used.