libnl  1.1.4
utils.c
1 /*
2  * lib/utils.c Utility Functions
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @defgroup utils Utilities
14  * @{
15  */
16 
17 #include <netlink-local.h>
18 #include <netlink/netlink.h>
19 #include <netlink/utils.h>
20 #include <linux/socket.h>
21 
22 /**
23  * Debug level
24  */
25 int nl_debug = 0;
26 
27 struct nl_dump_params nl_debug_dp = {
29 };
30 
31 static void __init nl_debug_init(void)
32 {
33  char *nldbg, *end;
34 
35  if ((nldbg = getenv("NLDBG"))) {
36  long level = strtol(nldbg, &end, 0);
37  if (nldbg != end)
38  nl_debug = level;
39  }
40 
41  nl_debug_dp.dp_fd = stderr;
42 }
43 
44 /**
45  * @name Error Code Helpers
46  * @{
47  */
48 
49 static __thread char *errbuf;
50 static __thread int nlerrno;
51 
52 /** @cond SKIP */
53 int __nl_error(int err, const char *file, unsigned int line, const char *func,
54  const char *fmt, ...)
55 {
56  char *user_err;
57  va_list args;
58 
59  if (errbuf) {
60  free(errbuf);
61  errbuf = NULL;
62  }
63 
64  nlerrno = err;
65 
66  if (fmt) {
67  va_start(args, fmt);
68  vasprintf(&user_err, fmt, args);
69  va_end(args);
70  }
71 
72 #ifdef VERBOSE_ERRORS
73  asprintf(&errbuf, "%s:%u:%s: %s (errno = %s)",
74  file, line, func, fmt ? user_err : "", strerror(err));
75 #else
76  asprintf(&errbuf, "%s (errno = %s)",
77  fmt ? user_err : "", strerror(err));
78 #endif
79 
80  if (fmt)
81  free(user_err);
82 
83  return -err;
84 }
85 
86 int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
87 {
88  FILE *fd;
89  char buf[128];
90 
91  fd = fopen(path, "r");
92  if (fd == NULL)
93  return nl_error(errno, "Unable to open file %s for reading",
94  path);
95 
96  while (fgets(buf, sizeof(buf), fd)) {
97  int goodlen, err;
98  long num;
99  char *end;
100 
101  if (*buf == '#' || *buf == '\n' || *buf == '\r')
102  continue;
103 
104  num = strtol(buf, &end, 0);
105  if (end == buf)
106  return nl_error(EINVAL, "Parsing error");
107 
108  if (num == LONG_MIN || num == LONG_MAX)
109  return nl_error(errno, "Number of out range");
110 
111  while (*end == ' ' || *end == '\t')
112  end++;
113 
114  goodlen = strcspn(end, "#\r\n\t ");
115  if (goodlen == 0)
116  return nl_error(EINVAL, "Empty string");
117 
118  end[goodlen] = '\0';
119 
120  err = cb(num, end);
121  if (err < 0)
122  return err;
123  }
124 
125  fclose(fd);
126 
127  return 0;
128 }
129 
130 /** @endcond */
131 
132 int nl_get_errno(void)
133 {
134  return nlerrno;
135 }
136 
137 
138 /**
139  * Return error message for an error code
140  * @return error message
141  */
142 char *nl_geterror(void)
143 {
144  if (errbuf)
145  return errbuf;
146 
147  if (nlerrno)
148  return strerror(nlerrno);
149 
150  return "Sucess\n";
151 }
152 
153 /**
154  * Print a libnl error message
155  * @arg s error message prefix
156  *
157  * Prints the error message of the call that failed last.
158  *
159  * If s is not NULL and *s is not a null byte the argument
160  * string is printed, followed by a colon and a blank. Then
161  * the error message and a new-line.
162  */
163 void nl_perror(const char *s)
164 {
165  if (s && *s)
166  fprintf(stderr, "%s: %s\n", s, nl_geterror());
167  else
168  fprintf(stderr, "%s\n", nl_geterror());
169 }
170 
171 /** @} */
172 
173 /**
174  * @name Unit Pretty-Printing
175  * @{
176  */
177 
178 /**
179  * Cancel down a byte counter
180  * @arg l byte counter
181  * @arg unit destination unit pointer
182  *
183  * Cancels down a byte counter until it reaches a reasonable
184  * unit. The chosen unit is assigned to \a unit.
185  * This function assume 1024 bytes in one kilobyte
186  *
187  * @return The cancelled down byte counter in the new unit.
188  */
189 double nl_cancel_down_bytes(unsigned long long l, char **unit)
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 }
208 
209 /**
210  * Cancel down a bit counter
211  * @arg l bit counter
212  * @arg unit destination unit pointer
213  *
214  * Cancels down bit counter until it reaches a reasonable
215  * unit. The chosen unit is assigned to \a unit.
216  * This function assume 1000 bits in one kilobit
217  *
218  * @return The cancelled down bit counter in the new unit.
219  */
220 double nl_cancel_down_bits(unsigned long long l, char **unit)
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 }
245 
246 /**
247  * Cancel down a micro second value
248  * @arg l micro seconds
249  * @arg unit destination unit pointer
250  *
251  * Cancels down a microsecond counter until it reaches a
252  * reasonable unit. The chosen unit is assigned to \a unit.
253  *
254  * @return The cancelled down microsecond in the new unit
255  */
256 double nl_cancel_down_us(uint32_t l, char **unit)
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 }
269 
270 /** @} */
271 
272 /**
273  * @name Generic Unit Translations
274  * @{
275  */
276 
277 /**
278  * Convert a character string to a size
279  * @arg str size encoded as character string
280  *
281  * Converts the specified size as character to the corresponding
282  * number of bytes.
283  *
284  * Supported formats are:
285  * - b,kb/k,m/mb,gb/g for bytes
286  * - bit,kbit/mbit/gbit
287  *
288  * This function assume 1000 bits in one kilobit and
289  * 1024 bytes in one kilobyte
290  *
291  * @return The number of bytes or -1 if the string is unparseable
292  */
293 long nl_size2int(const char *str)
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 }
321 
322 /**
323  * Convert a character string to a probability
324  * @arg str probability encoded as character string
325  *
326  * Converts the specified probability as character to the
327  * corresponding probability number.
328  *
329  * Supported formats are:
330  * - 0.0-1.0
331  * - 0%-100%
332  *
333  * @return The probability relative to NL_PROB_MIN and NL_PROB_MAX
334  */
335 long nl_prob2int(const char *str)
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 }
354 
355 /** @} */
356 
357 /**
358  * @name Time Translations
359  * @{
360  */
361 
362 #ifdef USER_HZ
363 static uint32_t user_hz = USER_HZ;
364 #else
365 static uint32_t user_hz = 100;
366 #endif
367 
368 static double ticks_per_usec = 1.0f;
369 
370 /* Retrieves the configured HZ and ticks/us value in the kernel.
371  * The value is cached. Supported ways of getting it:
372  *
373  * 1) environment variable
374  * 2) /proc/net/psched and sysconf
375  *
376  * Supports the environment variables:
377  * PROC_NET_PSCHED - may point to psched file in /proc
378  * PROC_ROOT - may point to /proc fs */
379 static void __init get_psched_settings(void)
380 {
381  char name[FILENAME_MAX];
382  FILE *fd;
383  int got_hz = 0, got_tick = 0;
384 
385  if (getenv("HZ")) {
386  long hz = strtol(getenv("HZ"), NULL, 0);
387 
388  if (LONG_MIN != hz && LONG_MAX != hz) {
389  user_hz = hz;
390  got_hz = 1;
391  }
392  }
393 
394  if (!got_hz)
395  user_hz = sysconf(_SC_CLK_TCK);
396 
397  if (getenv("TICKS_PER_USEC")) {
398  double t = strtod(getenv("TICKS_PER_USEC"), NULL);
399 
400  ticks_per_usec = t;
401  got_tick = 1;
402  }
403 
404 
405  if (getenv("PROC_NET_PSCHED"))
406  snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
407  else if (getenv("PROC_ROOT"))
408  snprintf(name, sizeof(name), "%s/net/psched",
409  getenv("PROC_ROOT"));
410  else
411  strncpy(name, "/proc/net/psched", sizeof(name) - 1);
412 
413  if ((fd = fopen(name, "r"))) {
414  uint32_t tick, us, nom;
415  int r = fscanf(fd, "%08x%08x%08x%*08x", &tick, &us, &nom);
416 
417  if (4 == r && nom == 1000000 && !got_tick)
418  ticks_per_usec = (double)tick/(double)us;
419 
420  fclose(fd);
421  }
422 }
423 
424 
425 /**
426  * Return the value of HZ
427  */
428 int nl_get_hz(void)
429 {
430  return user_hz;
431 }
432 
433 
434 /**
435  * Convert micro seconds to ticks
436  * @arg us micro seconds
437  * @return number of ticks
438  */
439 uint32_t nl_us2ticks(uint32_t us)
440 {
441  return us * ticks_per_usec;
442 }
443 
444 
445 /**
446  * Convert ticks to micro seconds
447  * @arg ticks number of ticks
448  * @return microseconds
449  */
450 uint32_t nl_ticks2us(uint32_t ticks)
451 {
452  return ticks / ticks_per_usec;
453 }
454 
455 long nl_time2int(const char *str)
456 {
457  char *p;
458  long l = strtol(str, &p, 0);
459  if (p == str)
460  return -1;
461 
462  if (*p) {
463  if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
464  l *= 60;
465  else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
466  l *= 60*60;
467  else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
468  l *= 60*60*24;
469  else if (strcasecmp(p, "s") != 0)
470  return -1;
471  }
472 
473  return l;
474 }
475 
476 /**
477  * Convert milliseconds to a character string
478  * @arg msec number of milliseconds
479  * @arg buf destination buffer
480  * @arg len buffer length
481  *
482  * Converts milliseconds to a character string split up in days, hours,
483  * minutes, seconds, and milliseconds and stores it in the specified
484  * destination buffer.
485  *
486  * @return The destination buffer.
487  */
488 char * nl_msec2str(uint64_t msec, char *buf, size_t len)
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 }
514 
515 /** @} */
516 
517 /**
518  * @name Link Layer Protocol Translations
519  * @{
520  */
521 
522 static struct trans_tbl llprotos[] = {
523  {0, "generic"},
524  __ADD(ARPHRD_ETHER,ether)
525  __ADD(ARPHRD_EETHER,eether)
526  __ADD(ARPHRD_AX25,ax25)
527  __ADD(ARPHRD_PRONET,pronet)
528  __ADD(ARPHRD_CHAOS,chaos)
529  __ADD(ARPHRD_IEEE802,ieee802)
530  __ADD(ARPHRD_ARCNET,arcnet)
531  __ADD(ARPHRD_APPLETLK,atalk)
532  __ADD(ARPHRD_DLCI,dlci)
533  __ADD(ARPHRD_ATM,atm)
534  __ADD(ARPHRD_METRICOM,metricom)
535  __ADD(ARPHRD_IEEE1394,ieee1394)
536 #ifdef ARPHRD_EUI64
537  __ADD(ARPHRD_EUI64,eui64)
538 #endif
539  __ADD(ARPHRD_INFINIBAND,infiniband)
540  __ADD(ARPHRD_SLIP,slip)
541  __ADD(ARPHRD_CSLIP,cslip)
542  __ADD(ARPHRD_SLIP6,slip6)
543  __ADD(ARPHRD_CSLIP6,cslip6)
544  __ADD(ARPHRD_RSRVD,rsrvd)
545  __ADD(ARPHRD_ADAPT,adapt)
546  __ADD(ARPHRD_ROSE,rose)
547  __ADD(ARPHRD_X25,x25)
548 #ifdef ARPHRD_HWX25
549  __ADD(ARPHRD_HWX25,hwx25)
550 #endif
551  __ADD(ARPHRD_PPP,ppp)
552  __ADD(ARPHRD_HDLC,hdlc)
553  __ADD(ARPHRD_LAPB,lapb)
554  __ADD(ARPHRD_DDCMP,ddcmp)
555  __ADD(ARPHRD_RAWHDLC,rawhdlc)
556  __ADD(ARPHRD_TUNNEL,ipip)
557  __ADD(ARPHRD_TUNNEL6,tunnel6)
558  __ADD(ARPHRD_FRAD,frad)
559  __ADD(ARPHRD_SKIP,skip)
560  __ADD(ARPHRD_LOOPBACK,loopback)
561  __ADD(ARPHRD_LOCALTLK,localtlk)
562  __ADD(ARPHRD_FDDI,fddi)
563  __ADD(ARPHRD_BIF,bif)
564  __ADD(ARPHRD_SIT,sit)
565  __ADD(ARPHRD_IPDDP,ip/ddp)
566  __ADD(ARPHRD_IPGRE,gre)
567  __ADD(ARPHRD_PIMREG,pimreg)
568  __ADD(ARPHRD_HIPPI,hippi)
569  __ADD(ARPHRD_ASH,ash)
570  __ADD(ARPHRD_ECONET,econet)
571  __ADD(ARPHRD_IRDA,irda)
572  __ADD(ARPHRD_FCPP,fcpp)
573  __ADD(ARPHRD_FCAL,fcal)
574  __ADD(ARPHRD_FCPL,fcpl)
575  __ADD(ARPHRD_FCFABRIC,fcfb_0)
576  __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
577  __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
578  __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
579  __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
580  __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
581  __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
582  __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
583  __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
584  __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
585  __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
586  __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
587  __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
588  __ADD(ARPHRD_IEEE802_TR,tr)
589  __ADD(ARPHRD_IEEE80211,ieee802.11)
590 #ifdef ARPHRD_IEEE80211_PRISM
591  __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
592 #endif
593 #ifdef ARPHRD_VOID
594  __ADD(ARPHRD_VOID,void)
595 #endif
596 };
597 
598 char * nl_llproto2str(int llproto, char *buf, size_t len)
599 {
600  return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
601 }
602 
603 int nl_str2llproto(const char *name)
604 {
605  return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
606 }
607 
608 /** @} */
609 
610 
611 /**
612  * @name Ethernet Protocol Translations
613  * @{
614  */
615 
616 static struct trans_tbl ether_protos[] = {
617  __ADD(ETH_P_LOOP,loop)
618  __ADD(ETH_P_PUP,pup)
619  __ADD(ETH_P_PUPAT,pupat)
620  __ADD(ETH_P_IP,ip)
621  __ADD(ETH_P_X25,x25)
622  __ADD(ETH_P_ARP,arp)
623  __ADD(ETH_P_BPQ,bpq)
624  __ADD(ETH_P_IEEEPUP,ieeepup)
625  __ADD(ETH_P_IEEEPUPAT,ieeepupat)
626  __ADD(ETH_P_DEC,dec)
627  __ADD(ETH_P_DNA_DL,dna_dl)
628  __ADD(ETH_P_DNA_RC,dna_rc)
629  __ADD(ETH_P_DNA_RT,dna_rt)
630  __ADD(ETH_P_LAT,lat)
631  __ADD(ETH_P_DIAG,diag)
632  __ADD(ETH_P_CUST,cust)
633  __ADD(ETH_P_SCA,sca)
634  __ADD(ETH_P_RARP,rarp)
635  __ADD(ETH_P_ATALK,atalk)
636  __ADD(ETH_P_AARP,aarp)
637 #ifdef ETH_P_8021Q
638  __ADD(ETH_P_8021Q,802.1q)
639 #endif
640  __ADD(ETH_P_IPX,ipx)
641  __ADD(ETH_P_IPV6,ipv6)
642 #ifdef ETH_P_WCCP
643  __ADD(ETH_P_WCCP,wccp)
644 #endif
645  __ADD(ETH_P_PPP_DISC,ppp_disc)
646  __ADD(ETH_P_PPP_SES,ppp_ses)
647  __ADD(ETH_P_MPLS_UC,mpls_uc)
648  __ADD(ETH_P_MPLS_MC,mpls_mc)
649  __ADD(ETH_P_ATMMPOA,atmmpoa)
650  __ADD(ETH_P_ATMFATE,atmfate)
651  __ADD(ETH_P_EDP2,edp2)
652  __ADD(ETH_P_802_3,802.3)
653  __ADD(ETH_P_AX25,ax25)
654  __ADD(ETH_P_ALL,all)
655  __ADD(ETH_P_802_2,802.2)
656  __ADD(ETH_P_SNAP,snap)
657  __ADD(ETH_P_DDCMP,ddcmp)
658  __ADD(ETH_P_WAN_PPP,wan_ppp)
659  __ADD(ETH_P_PPP_MP,ppp_mp)
660  __ADD(ETH_P_LOCALTALK,localtalk)
661  __ADD(ETH_P_PPPTALK,ppptalk)
662  __ADD(ETH_P_TR_802_2,tr_802.2)
663  __ADD(ETH_P_MOBITEX,mobitex)
664  __ADD(ETH_P_CONTROL,control)
665  __ADD(ETH_P_IRDA,irda)
666  __ADD(ETH_P_ECONET,econet)
667  __ADD(ETH_P_HDLC,hdlc)
668 };
669 
670 char *nl_ether_proto2str(int eproto, char *buf, size_t len)
671 {
672  return __type2str(eproto, buf, len, ether_protos,
673  ARRAY_SIZE(ether_protos));
674 }
675 
676 int nl_str2ether_proto(const char *name)
677 {
678  return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
679 }
680 
681 /** @} */
682 
683 /**
684  * @name IP Protocol Translations
685  * @{
686  */
687 
688 char *nl_ip_proto2str(int proto, char *buf, size_t len)
689 {
690  struct protoent *p = getprotobynumber(proto);
691 
692  if (p) {
693  snprintf(buf, len, "%s", p->p_name);
694  return buf;
695  }
696 
697  snprintf(buf, len, "0x%x", proto);
698  return buf;
699 }
700 
701 int nl_str2ip_proto(const char *name)
702 {
703  struct protoent *p = getprotobyname(name);
704  unsigned long l;
705  char *end;
706 
707  if (p)
708  return p->p_proto;
709 
710  l = strtoul(name, &end, 0);
711  if (l == ULONG_MAX || *end != '\0')
712  return -1;
713 
714  return (int) l;
715 }
716 
717 /** @} */
718 
719 /**
720  * @name Dumping Helpers
721  * @{
722  */
723 
724 /**
725  * Handle a new line while dumping
726  * @arg params Dumping parameters
727  * @arg line Number of lines dumped already.
728  *
729  * This function must be called before dumping any onto a
730  * new line. It will ensure proper prefixing as specified
731  * by the dumping parameters.
732  *
733  * @note This function will NOT dump any newlines itself
734  */
735 void nl_new_line(struct nl_dump_params *params, int line)
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 }
752 
753 /**
754  * Dump a formatted character string
755  * @arg params Dumping parameters
756  * @arg fmt printf style formatting string
757  * @arg ... Arguments to formatting string
758  *
759  * Dumps a printf style formatting string to the output device
760  * as specified by the dumping parameters.
761  */
762 void nl_dump(struct nl_dump_params *params, const char *fmt, ...)
763 {
764  va_list args;
765 
766  va_start(args, fmt);
767  __dp_dump(params, fmt, args);
768  va_end(args);
769 }
770 
771 /** @} */
772 
773 /** @} */
char * dp_buf
Alternatively the output may be redirected into a buffer.
Definition: types.h:91
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:86
long nl_size2int(const char *str)
Convert a character string to a size.
Definition: utils.c:293
double nl_cancel_down_bits(unsigned long long l, char **unit)
Cancel down a bit counter.
Definition: utils.c:220
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:41
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
Definition: utils.c:488
#define NL_PROB_MAX
Upper probability limit.
Definition: utils.h:37
void(* dp_nl_cb)(struct nl_dump_params *, int)
A callback invoked for every new line, can be used to customize the indentation.
Definition: types.h:76
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
Definition: utils.c:189
double nl_cancel_down_us(uint32_t l, char **unit)
Cancel down a micro second value.
Definition: utils.c:256
uint32_t nl_ticks2us(uint32_t ticks)
Convert ticks to micro seconds.
Definition: utils.c:450
void nl_new_line(struct nl_dump_params *params, int line)
Handle a new line while dumping.
Definition: utils.c:735
long nl_prob2int(const char *str)
Convert a character string to a probability.
Definition: utils.c:335
char * nl_geterror(void)
Return error message for an error code.
Definition: utils.c:142
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).
Definition: types.h:47
uint32_t nl_us2ticks(uint32_t us)
Convert micro seconds to ticks.
Definition: utils.c:439
Dumping parameters.
Definition: types.h:36
size_t dp_buflen
Length of the buffer dp_buf.
Definition: types.h:96
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:762
void nl_perror(const char *s)
Print a libnl error message.
Definition: utils.c:163
int nl_debug
Debug level.
Definition: utils.c:25
int nl_get_hz(void)
Return the value of HZ.
Definition: utils.c:428
Dump all attributes but no statistics.
Definition: types.h:23