libnl  1.1.4
Modules | Data Structures | Enumerations | Variables
Utilities

Modules

 Abstract Address
 
 Abstract Data
 

Data Structures

struct  nl_dump_params
 Dumping parameters. More...
 

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. More...
 
void nl_perror (const char *s)
 Print a libnl error message. More...
 

Unit Pretty-Printing

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

Generic Unit Translations

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

Time Translations

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

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)
 

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. More...
 
void nl_dump (struct nl_dump_params *params, const char *fmt,...)
 Dump a formatted character string. More...
 

Probability Constants

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

Detailed Description

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.

21  {
22  NL_DUMP_BRIEF, /**< Dump object in a brief one-liner */
23  NL_DUMP_FULL, /**< Dump all attributes but no statistics */
24  NL_DUMP_STATS, /**< Dump all attributes including statistics */
25  NL_DUMP_XML, /**< Dump all attribtes in XML format */
26  NL_DUMP_ENV, /**< Dump all attribtues as env variables */
27  NL_DUMP_EVENTS, /**< Dump event */
28  __NL_DUMP_MAX,
29 };

Function Documentation

char* nl_geterror ( void  )
Returns
error message

Definition at line 142 of file utils.c.

Referenced by nl_perror().

143 {
144  if (errbuf)
145  return errbuf;
146 
147  if (nlerrno)
148  return strerror(nlerrno);
149 
150  return "Sucess\n";
151 }
void nl_perror ( const char *  s)
Parameters
serror 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().

164 {
165  if (s && *s)
166  fprintf(stderr, "%s: %s\n", s, nl_geterror());
167  else
168  fprintf(stderr, "%s\n", nl_geterror());
169 }
double nl_cancel_down_bytes ( unsigned long long  l,
char **  unit 
)
Parameters
lbyte counter
unitdestination 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.

190 {
191  if (l >= 1099511627776LL) {
192  *unit = "TiB";
193  return ((double) l) / 1099511627776LL;
194  } else if (l >= 1073741824) {
195  *unit = "GiB";
196  return ((double) l) / 1073741824;
197  } else if (l >= 1048576) {
198  *unit = "MiB";
199  return ((double) l) / 1048576;
200  } else if (l >= 1024) {
201  *unit = "KiB";
202  return ((double) l) / 1024;
203  } else {
204  *unit = "B";
205  return (double) l;
206  }
207 }
double nl_cancel_down_bits ( unsigned long long  l,
char **  unit 
)
Parameters
lbit counter
unitdestination 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.

221 {
222  if (l >= 1000000000000ULL) {
223  *unit = "Tbit";
224  return ((double) l) / 1000000000000ULL;
225  }
226 
227  if (l >= 1000000000) {
228  *unit = "Gbit";
229  return ((double) l) / 1000000000;
230  }
231 
232  if (l >= 1000000) {
233  *unit = "Mbit";
234  return ((double) l) / 1000000;
235  }
236 
237  if (l >= 1000) {
238  *unit = "Kbit";
239  return ((double) l) / 1000;
240  }
241 
242  *unit = "bit";
243  return (double) l;
244 }
double nl_cancel_down_us ( uint32_t  l,
char **  unit 
)
Parameters
lmicro seconds
unitdestination 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.

257 {
258  if (l >= 1000000) {
259  *unit = "s";
260  return ((double) l) / 1000000;
261  } else if (l >= 1000) {
262  *unit = "ms";
263  return ((double) l) / 1000;
264  } else {
265  *unit = "us";
266  return (double) l;
267  }
268 }
long nl_size2int ( const char *  str)
Parameters
strsize 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.

294 {
295  char *p;
296  long l = strtol(str, &p, 0);
297  if (p == str)
298  return -1;
299 
300  if (*p) {
301  if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
302  l *= 1024;
303  else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
304  l *= 1024*1024*1024;
305  else if (!strcasecmp(p, "gbit"))
306  l *= 1000000000L/8;
307  else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
308  l *= 1024*1024;
309  else if (!strcasecmp(p, "mbit"))
310  l *= 1000000/8;
311  else if (!strcasecmp(p, "kbit"))
312  l *= 1000/8;
313  else if (!strcasecmp(p, "bit"))
314  l /= 8;
315  else if (strcasecmp(p, "b") != 0)
316  return -1;
317  }
318 
319  return l;
320 }
long nl_prob2int ( const char *  str)
Parameters
strprobability 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.

336 {
337  char *p;
338  double d = strtod(str, &p);
339 
340  if (p == str)
341  return -1;
342 
343  if (d > 1.0)
344  d /= 100.0f;
345 
346  if (d > 1.0f || d < 0.0f)
347  return -1;
348 
349  if (*p && strcmp(p, "%") != 0)
350  return -1;
351 
352  return rint(d * NL_PROB_MAX);
353 }
uint32_t nl_us2ticks ( uint32_t  us)
Parameters
usmicro seconds
Returns
number of ticks

Definition at line 439 of file utils.c.

Referenced by rtnl_netem_set_delay(), and rtnl_netem_set_jitter().

440 {
441  return us * ticks_per_usec;
442 }
uint32_t nl_ticks2us ( uint32_t  ticks)
Parameters
ticksnumber of ticks
Returns
microseconds

Definition at line 450 of file utils.c.

Referenced by rtnl_netem_get_delay(), and rtnl_netem_get_jitter().

451 {
452  return ticks / ticks_per_usec;
453 }
char* nl_msec2str ( uint64_t  msec,
char *  buf,
size_t  len 
)
Parameters
msecnumber of milliseconds
bufdestination buffer
lenbuffer 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.

489 {
490  int i, split[5];
491  char *units[] = {"d", "h", "m", "s", "msec"};
492 
493 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
494  _SPLIT(0, 86400000); /* days */
495  _SPLIT(1, 3600000); /* hours */
496  _SPLIT(2, 60000); /* minutes */
497  _SPLIT(3, 1000); /* seconds */
498 #undef _SPLIT
499  split[4] = msec;
500 
501  memset(buf, 0, len);
502 
503  for (i = 0; i < ARRAY_SIZE(split); i++) {
504  if (split[i] > 0) {
505  char t[64];
506  snprintf(t, sizeof(t), "%s%d%s",
507  strlen(buf) ? " " : "", split[i], units[i]);
508  strncat(buf, t, len - strlen(buf) - 1);
509  }
510  }
511 
512  return buf;
513 }
void nl_new_line ( struct nl_dump_params params,
int  line 
)
Parameters
paramsDumping parameters
lineNumber 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.

736 {
737  if (params->dp_prefix) {
738  int i;
739  for (i = 0; i < params->dp_prefix; i++) {
740  if (params->dp_fd)
741  fprintf(params->dp_fd, " ");
742  else if (params->dp_buf)
743  strncat(params->dp_buf, " ",
744  params->dp_buflen -
745  sizeof(params->dp_buf) - 1);
746  }
747  }
748 
749  if (params->dp_nl_cb)
750  params->dp_nl_cb(params, line);
751 }
void nl_dump ( struct nl_dump_params params,
const char *  fmt,
  ... 
)
Parameters
paramsDumping parameters
fmtprintf 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.

763 {
764  va_list args;
765 
766  va_start(args, fmt);
767  __dp_dump(params, fmt, args);
768  va_end(args);
769 }

Variable Documentation

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

Definition at line 27 of file utils.c.