Utilities

Data Structures

struct  nl_dump_params
 Dumping parameters. More...

Modules

 Abstract Address
 Abstract Data

Enumerations

enum  nl_dump_type {
  NL_DUMP_BRIEF, NL_DUMP_FULL, NL_DUMP_STATS, NL_DUMP_XML,
  NL_DUMP_ENV, NL_DUMP_EVENTS, __NL_DUMP_MAX
}
 

Dumping types (dp_type).

More...

Variables

int nl_debug = 0
 Debug level.
struct nl_dump_params nl_debug_dp

Error Code Helpers



int nl_get_errno (void)
char * nl_geterror (void)
 Return error message for an error code.
void nl_perror (const char *s)
 Print a libnl error message.

Time Translations



int nl_get_hz (void)
 Return the value of HZ.
uint32_t nl_us2ticks (uint32_t us)
 Convert micro seconds to ticks.
uint32_t nl_ticks2us (uint32_t ticks)
 Convert ticks to micro seconds.
long nl_time2int (const char *str)
char * nl_msec2str (uint64_t msec, char *buf, size_t len)
 Convert milliseconds to a character string.

Link Layer Protocol Translations



char * nl_llproto2str (int llproto, char *buf, size_t len)
int nl_str2llproto (const char *name)

Ethernet Protocol Translations



char * nl_ether_proto2str (int eproto, char *buf, size_t len)
int nl_str2ether_proto (const char *name)

Unit Pretty-Printing



double nl_cancel_down_bytes (unsigned long long l, char **unit)
 Cancel down a byte counter.
double nl_cancel_down_bits (unsigned long long l, char **unit)
 Cancel down a bit counter.
double nl_cancel_down_us (uint32_t l, char **unit)
 Cancel down a micro second value.

Generic Unit Translations



long nl_size2int (const char *str)
 Convert a character string to a size.
long nl_prob2int (const char *str)
 Convert a character string to a probability.

IP Protocol Translations



char * nl_ip_proto2str (int proto, char *buf, size_t len)
int nl_str2ip_proto (const char *name)

Dumping Helpers



void nl_new_line (struct nl_dump_params *params, int line)
 Handle a new line while dumping.
void nl_dump (struct nl_dump_params *params, const char *fmt,...)
 Dump a formatted character string.

Probability Constants



#define NL_PROB_MIN   0x0
 Lower probability limit.
#define NL_PROB_MAX   0xffffffff
 Upper probability limit.

Enumeration Type Documentation

Enumerator:
NL_DUMP_BRIEF 

Dump object in a brief one-liner.

NL_DUMP_FULL 

Dump all attributes but no statistics.

NL_DUMP_STATS 

Dump all attributes including statistics.

NL_DUMP_XML 

Dump all attribtes in XML format.

NL_DUMP_ENV 

Dump all attribtues as env variables.

NL_DUMP_EVENTS 

Dump event.

Definition at line 21 of file types.h.

00021                   {
00022         NL_DUMP_BRIEF,          /**< Dump object in a brief one-liner */
00023         NL_DUMP_FULL,           /**< Dump all attributes but no statistics */
00024         NL_DUMP_STATS,          /**< Dump all attributes including statistics */
00025         NL_DUMP_XML,            /**< Dump all attribtes in XML format */
00026         NL_DUMP_ENV,            /**< Dump all attribtues as env variables */
00027         NL_DUMP_EVENTS,         /**< Dump event */
00028         __NL_DUMP_MAX,
00029 };


Function Documentation

char* nl_geterror ( void   ) 
Returns:
error message

Definition at line 142 of file utils.c.

Referenced by nl_perror().

00143 {
00144         if (errbuf)
00145                 return errbuf;
00146 
00147         if (nlerrno)
00148                 return strerror(nlerrno);
00149 
00150         return "Sucess\n";
00151 }

void nl_perror ( const char *  s  ) 
Parameters:
s error message prefix

Prints the error message of the call that failed last.

If s is not NULL and *s is not a null byte the argument string is printed, followed by a colon and a blank. Then the error message and a new-line.

Definition at line 163 of file utils.c.

References nl_geterror().

00164 {
00165         if (s && *s)
00166                 fprintf(stderr, "%s: %s\n", s, nl_geterror());
00167         else
00168                 fprintf(stderr, "%s\n", nl_geterror());
00169 }

double nl_cancel_down_bytes ( unsigned long long  l,
char **  unit 
)
Parameters:
l byte counter
unit destination unit pointer

Cancels down a byte counter until it reaches a reasonable unit. The chosen unit is assigned to unit. This function assume 1024 bytes in one kilobyte

Returns:
The cancelled down byte counter in the new unit.

Definition at line 189 of file utils.c.

00190 {
00191         if (l >= 1099511627776LL) {
00192                 *unit = "TiB";
00193                 return ((double) l) / 1099511627776LL;
00194         } else if (l >= 1073741824) {
00195                 *unit = "GiB";
00196                 return ((double) l) / 1073741824;
00197         } else if (l >= 1048576) {
00198                 *unit = "MiB";
00199                 return ((double) l) / 1048576;
00200         } else if (l >= 1024) {
00201                 *unit = "KiB";
00202                 return ((double) l) / 1024;
00203         } else {
00204                 *unit = "B";
00205                 return (double) l;
00206         }
00207 }

double nl_cancel_down_bits ( unsigned long long  l,
char **  unit 
)
Parameters:
l bit counter
unit destination unit pointer

Cancels down bit counter until it reaches a reasonable unit. The chosen unit is assigned to unit. This function assume 1000 bits in one kilobit

Returns:
The cancelled down bit counter in the new unit.

Definition at line 220 of file utils.c.

00221 {
00222         if (l >= 1000000000000ULL) {
00223                 *unit = "Tbit";
00224                 return ((double) l) / 1000000000000ULL;
00225         }
00226 
00227         if (l >= 1000000000) {
00228                 *unit = "Gbit";
00229                 return ((double) l) / 1000000000;
00230         }
00231 
00232         if (l >= 1000000) {
00233                 *unit = "Mbit";
00234                 return ((double) l) / 1000000;
00235         }
00236 
00237         if (l >= 1000) {
00238                 *unit = "Kbit";
00239                 return ((double) l) / 1000;
00240         }
00241 
00242         *unit = "bit";
00243         return (double) l;
00244 }

double nl_cancel_down_us ( uint32_t  l,
char **  unit 
)
Parameters:
l micro seconds
unit destination unit pointer

Cancels down a microsecond counter until it reaches a reasonable unit. The chosen unit is assigned to unit.

Returns:
The cancelled down microsecond in the new unit

Definition at line 256 of file utils.c.

00257 {
00258         if (l >= 1000000) {
00259                 *unit = "s";
00260                 return ((double) l) / 1000000;
00261         } else if (l >= 1000) {
00262                 *unit = "ms";
00263                 return ((double) l) / 1000;
00264         } else {
00265                 *unit = "us";
00266                 return (double) l;
00267         }
00268 }

long nl_size2int ( const char *  str  ) 
Parameters:
str size encoded as character string

Converts the specified size as character to the corresponding number of bytes.

Supported formats are:

  • b,kb/k,m/mb,gb/g for bytes
  • bit,kbit/mbit/gbit

This function assume 1000 bits in one kilobit and 1024 bytes in one kilobyte

Returns:
The number of bytes or -1 if the string is unparseable

Definition at line 293 of file utils.c.

00294 {
00295         char *p;
00296         long l = strtol(str, &p, 0);
00297         if (p == str)
00298                 return -1;
00299 
00300         if (*p) {
00301                 if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
00302                         l *= 1024;
00303                 else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
00304                         l *= 1024*1024*1024;
00305                 else if (!strcasecmp(p, "gbit"))
00306                         l *= 1000000000L/8;
00307                 else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
00308                         l *= 1024*1024;
00309                 else if (!strcasecmp(p, "mbit"))
00310                         l *= 1000000/8;
00311                 else if (!strcasecmp(p, "kbit"))
00312                         l *= 1000/8;
00313                 else if (!strcasecmp(p, "bit"))
00314                         l /= 8;
00315                 else if (strcasecmp(p, "b") != 0)
00316                         return -1;
00317         }
00318 
00319         return l;
00320 }

long nl_prob2int ( const char *  str  ) 
Parameters:
str probability encoded as character string

Converts the specified probability as character to the corresponding probability number.

Supported formats are:

  • 0.0-1.0
  • 0-100%
Returns:
The probability relative to NL_PROB_MIN and NL_PROB_MAX

Definition at line 335 of file utils.c.

References NL_PROB_MAX.

00336 {
00337         char *p;
00338         double d = strtod(str, &p);
00339 
00340         if (p == str)
00341                 return -1;
00342 
00343         if (d > 1.0)
00344                 d /= 100.0f;
00345 
00346         if (d > 1.0f || d < 0.0f)
00347                 return -1;
00348 
00349         if (*p && strcmp(p, "%") != 0)
00350                 return -1;
00351 
00352         return rint(d * NL_PROB_MAX);
00353 }

uint32_t nl_us2ticks ( uint32_t  us  ) 
Parameters:
us micro seconds
Returns:
number of ticks

Definition at line 439 of file utils.c.

Referenced by rtnl_netem_set_delay(), and rtnl_netem_set_jitter().

00440 {
00441         return us * ticks_per_usec;
00442 }

uint32_t nl_ticks2us ( uint32_t  ticks  ) 
Parameters:
ticks number of ticks
Returns:
microseconds

Definition at line 450 of file utils.c.

Referenced by rtnl_netem_get_delay(), and rtnl_netem_get_jitter().

00451 {
00452         return ticks / ticks_per_usec;
00453 }

char* nl_msec2str ( uint64_t  msec,
char *  buf,
size_t  len 
)
Parameters:
msec number of milliseconds
buf destination buffer
len buffer length

Converts milliseconds to a character string split up in days, hours, minutes, seconds, and milliseconds and stores it in the specified destination buffer.

Returns:
The destination buffer.

Definition at line 488 of file utils.c.

00489 {
00490         int i, split[5];
00491         char *units[] = {"d", "h", "m", "s", "msec"};
00492 
00493 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
00494         _SPLIT(0, 86400000);    /* days */
00495         _SPLIT(1, 3600000);     /* hours */
00496         _SPLIT(2, 60000);       /* minutes */
00497         _SPLIT(3, 1000);        /* seconds */
00498 #undef  _SPLIT
00499         split[4] = msec;
00500 
00501         memset(buf, 0, len);
00502 
00503         for (i = 0; i < ARRAY_SIZE(split); i++) {
00504                 if (split[i] > 0) {
00505                         char t[64];
00506                         snprintf(t, sizeof(t), "%s%d%s",
00507                                  strlen(buf) ? " " : "", split[i], units[i]);
00508                         strncat(buf, t, len - strlen(buf) - 1);
00509                 }
00510         }
00511 
00512         return buf;
00513 }

void nl_new_line ( struct nl_dump_params params,
int  line 
)
Parameters:
params Dumping parameters
line Number of lines dumped already.

This function must be called before dumping any onto a new line. It will ensure proper prefixing as specified by the dumping parameters.

Note:
This function will NOT dump any newlines itself

Definition at line 735 of file utils.c.

References nl_dump_params::dp_buf, nl_dump_params::dp_buflen, nl_dump_params::dp_fd, nl_dump_params::dp_nl_cb, and nl_dump_params::dp_prefix.

00736 {
00737         if (params->dp_prefix) {
00738                 int i;
00739                 for (i = 0; i < params->dp_prefix; i++) {
00740                         if (params->dp_fd)
00741                                 fprintf(params->dp_fd, " ");
00742                         else if (params->dp_buf)
00743                                 strncat(params->dp_buf, " ",
00744                                         params->dp_buflen -
00745                                         sizeof(params->dp_buf) - 1);
00746                 }
00747         }
00748 
00749         if (params->dp_nl_cb)
00750                 params->dp_nl_cb(params, line);
00751 }

void nl_dump ( struct nl_dump_params params,
const char *  fmt,
  ... 
)
Parameters:
params Dumping parameters
fmt printf style formatting string
... Arguments to formatting string

Dumps a printf style formatting string to the output device as specified by the dumping parameters.

Definition at line 762 of file utils.c.

00763 {
00764         va_list args;
00765 
00766         va_start(args, fmt);
00767         __dp_dump(params, fmt, args);
00768         va_end(args);
00769 }


Variable Documentation

struct nl_dump_params nl_debug_dp
Initial value:
 {
        .dp_type = NL_DUMP_FULL,
}

Definition at line 27 of file utils.c.


Generated on 22 Nov 2013 for libnl by  doxygen 1.6.1