Solaris Bandwidth Manager 1.5 Developing Guide

Chapter 3 Using the C Statistics API

The C Statistics API is used to retrieve information about the local configuration of Solaris Bandwidth Manager, and statistics for the classes of network traffic that are defined by the configuration.

It provides functions for the following types of operation:

Linking with the Shared Library

The C Statistics API is implemented as the following shared library:

/opt/SUNWconn/lib/libba_stat.so.1

Your source code must include the following line:

#include <netinet/ba_stat.h>

To compile your application, use the cc(1B) command with the following switch:

-I/opt/SUNWconn/include

To link the shared library with your application, use the ld(1) command with the following switches:

-L/opt/SUNWconn/lib -R/opt/SUNWconn/lib -lba_stat

For example:

$ ld -o myexe myobj -L/opt/SUNWconn/lib -R/opt/SUNWconn/lib -lba_stat

For an example of how to use a Makefile to link your application, see the Makefile for the code examples, /opt/SUNWconn/ba/examples/Makefile.

Multithreading

The C Statistics API can be used safely in multithreaded mode, with the following restrictions:

Device Handling

The kernel module is viewed as a streams driver (/dev/ipqos) by the user process and as a streams module (ipqos) by the IP stack. The file /dev/ipqos is opened automatically by the first function call through the C Statistics API and its file descriptor is used to access the ipqos module.

The C Statistics API provides two functions that control the file /dev/ipqos directly:

Interface Handling

The C Statistics API provides the following functions for interface handling:

Class Handling

The C Statistics API provides the following functions for class handling:

Statistics Handling

The C Statistics API provides the following functions for statistics handling:

Event Handling

The C Statistics API can be used to detect when certain events occur at the level of the ipqos module, and to return information about the event that has occurred. There are three ways to detect that an event has occurred:


Note -

As events are buffered, if using poll(2) or select(3c), you must call ba_get_next_event() repeatedly in non-blocking mode to check that there are no outstanding events, to make sure that the next poll(2) works correctly.


Once you have detected that an event has occurred, for example, by using poll(2), call the function ba_get_next_event() to retrieve information about the event.

The C Statistics API provides the following functions for event handling:

Table 3-1 lists the event types reported to the user process.

Table 3-1 Event Types

Event Type 

Description 

BA_EVENT_CONFIG_STARTING

Generated when the Solaris Bandwidth Manager policy agent starts to read a new configuration 

BA_EVENT_CONFIG_ENDING

Generated when the Solaris Bandwidth Manager Policy Agent finishes reading a new configuration 

BA_EVENT_DAEMON_ENDING

Generated when the Solaris Bandwidth Manager policy agent stops or is killed 

BA_EVENT_FLOW_ACCOUNTING

Generated when a flow has timed out, or when the TOS field changes for a flow 

BA_EVENT_STATS_RESET

Generated when a class has been reset or removed 

To limit the number of events held in the buffer, edit the environment variable BA_EVENTBUFSIZE in the /etc/default/ba_info file. To buffer an unlimited number of events, set this value to 0, but note that this will lead to kernel memory exhaustion if all events are not retrieved.

Error Handling

If an error occurs during any call to the C Statistics API, the function returns with a value of -1, and the variable ba_errno is set to an error code that indicates the error condition. The possible error codes for ba_errno are listed in Table 4-2.

The C Statistics API provides the following functions for error handling:


Note -

Solaris Bandwidth Manager does not reset ba_errno following a successful function call. This means that the value of ba_errno is only valid when a function returns --1 to indicate an error condition.


Memory Handling

The C Statistics API uses two methods for allocating memory for arrays.

When using the ba_list_interfaces() and ba_list_class_names() functions, you must always allocate sufficient memory for the buffer, the size of which is dependent on the number of classes. For example, when using ba_list_interfaces():

nintface = ba_get_num_interfaces();
if (nintface > 0) {
	interfaces = (ba_name_t *) calloc(nintface, sizeof ba_name_t);
	if (ba_list_interfaces(interfaces) != -1) {
	/* process interfaces .. */
} else {
	/* error handling */
}
	free(interfaces);
}

When using the ba_list_interface_config(), ba_list_classes() and ba_get_flow_stats() functions, memory is allocated automatically. However, in this case the user must call the ba_free() function afterwards, in order to free this memory.

Examples

Solaris Bandwidth Manager includes the following examples:

The examples are in the directory /opt/SUNWconn/ba/examples. This directory also contains a Makefile for the examples, Makefile.