libnl  1.1
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.

Returns
The cancelled down byte counter in the new unit.

Definition at line 188 of file utils.c.

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

Cancels downa bit counter until it reaches a reasonable unit. The chosen unit is assigned to unit.

Returns
The cancelled down bit counter in the new unit.

Definition at line 218 of file utils.c.

219 {
220  if (l >= 1099511627776ULL) {
221  *unit = "Tbit";
222  return ((double) l) / 1099511627776ULL;
223  } else if (l >= 1073741824) {
224  *unit = "Gbit";
225  return ((double) l) / 1073741824;
226  } else if (l >= 1048576) {
227  *unit = "Mbit";
228  return ((double) l) / 1048576;
229  } else if (l >= 1024) {
230  *unit = "Kbit";
231  return ((double) l) / 1024;
232  } else {
233  *unit = "bit";
234  return (double) l;
235  }
236 
237 }
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 249 of file utils.c.

250 {
251  if (l >= 1000000) {
252  *unit = "s";
253  return ((double) l) / 1000000;
254  } else if (l >= 1000) {
255  *unit = "ms";
256  return ((double) l) / 1000;
257  } else {
258  *unit = "us";
259  return (double) l;
260  }
261 }
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
Returns
The number of bytes or -1 if the string is unparseable

Definition at line 283 of file utils.c.

284 {
285  char *p;
286  long l = strtol(str, &p, 0);
287  if (p == str)
288  return -1;
289 
290  if (*p) {
291  if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
292  l *= 1024;
293  else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
294  l *= 1024*1024*1024;
295  else if (!strcasecmp(p, "gbit"))
296  l *= 1024*1024*1024/8;
297  else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
298  l *= 1024*1024;
299  else if (!strcasecmp(p, "mbit"))
300  l *= 1024*1024/8;
301  else if (!strcasecmp(p, "kbit"))
302  l *= 1024/8;
303  else if (!strcasecmp(p, "bit"))
304  l /= 8;
305  else if (strcasecmp(p, "b") != 0)
306  return -1;
307  }
308 
309  return l;
310 }
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 325 of file utils.c.

References NL_PROB_MAX.

326 {
327  char *p;
328  double d = strtod(str, &p);
329 
330  if (p == str)
331  return -1;
332 
333  if (d > 1.0)
334  d /= 100.0f;
335 
336  if (d > 1.0f || d < 0.0f)
337  return -1;
338 
339  if (*p && strcmp(p, "%") != 0)
340  return -1;
341 
342  return rint(d * NL_PROB_MAX);
343 }
uint32_t nl_us2ticks ( uint32_t  us)
Parameters
usmicro seconds
Returns
number of ticks

Definition at line 429 of file utils.c.

Referenced by rtnl_netem_set_delay(), and rtnl_netem_set_jitter().

430 {
431  return us * ticks_per_usec;
432 }
uint32_t nl_ticks2us ( uint32_t  ticks)
Parameters
ticksnumber of ticks
Returns
microseconds

Definition at line 440 of file utils.c.

Referenced by rtnl_netem_get_delay(), and rtnl_netem_get_jitter().

441 {
442  return ticks / ticks_per_usec;
443 }
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 478 of file utils.c.

479 {
480  int i, split[5];
481  char *units[] = {"d", "h", "m", "s", "msec"};
482 
483 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
484  _SPLIT(0, 86400000); /* days */
485  _SPLIT(1, 3600000); /* hours */
486  _SPLIT(2, 60000); /* minutes */
487  _SPLIT(3, 1000); /* seconds */
488 #undef _SPLIT
489  split[4] = msec;
490 
491  memset(buf, 0, len);
492 
493  for (i = 0; i < ARRAY_SIZE(split); i++) {
494  if (split[i] > 0) {
495  char t[64];
496  snprintf(t, sizeof(t), "%s%d%s",
497  strlen(buf) ? " " : "", split[i], units[i]);
498  strncat(buf, t, len - strlen(buf) - 1);
499  }
500  }
501 
502  return buf;
503 }
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 725 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.

726 {
727  if (params->dp_prefix) {
728  int i;
729  for (i = 0; i < params->dp_prefix; i++) {
730  if (params->dp_fd)
731  fprintf(params->dp_fd, " ");
732  else if (params->dp_buf)
733  strncat(params->dp_buf, " ",
734  params->dp_buflen -
735  sizeof(params->dp_buf) - 1);
736  }
737  }
738 
739  if (params->dp_nl_cb)
740  params->dp_nl_cb(params, line);
741 }
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 752 of file utils.c.

753 {
754  va_list args;
755 
756  va_start(args, fmt);
757  __dp_dump(params, fmt, args);
758  va_end(args);
759 }

Variable Documentation

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

Definition at line 27 of file utils.c.