2 * lib/utils.c Utility Functions
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
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup utils Utilities
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/utils.h>
21 #include <linux/socket.h>
28 struct nl_dump_params nl_debug_dp = {
29 .dp_type = NL_DUMP_DETAILS,
32 static void __init nl_debug_init(void)
36 if ((nldbg = getenv("NLDBG"))) {
37 long level = strtol(nldbg, &end, 0);
42 nl_debug_dp.dp_fd = stderr;
45 int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
50 fd = fopen(path, "r");
52 return -nl_syserr2nlerr(errno);
54 while (fgets(buf, sizeof(buf), fd)) {
59 if (*buf == '#' || *buf == '\n' || *buf == '\r')
62 num = strtol(buf, &end, 0);
66 if (num == LONG_MIN || num == LONG_MAX)
69 while (*end == ' ' || *end == '\t')
72 goodlen = strcspn(end, "#\r\n\t ");
89 * @name Unit Pretty-Printing
94 * Cancel down a byte counter
96 * @arg unit destination unit pointer
98 * Cancels down a byte counter until it reaches a reasonable
99 * unit. The chosen unit is assigned to \a unit.
101 * @return The cancelled down byte counter in the new unit.
103 double nl_cancel_down_bytes(unsigned long long l, char **unit)
105 if (l >= 1099511627776LL) {
107 return ((double) l) / 1099511627776LL;
108 } else if (l >= 1073741824) {
110 return ((double) l) / 1073741824;
111 } else if (l >= 1048576) {
113 return ((double) l) / 1048576;
114 } else if (l >= 1024) {
116 return ((double) l) / 1024;
124 * Cancel down a bit counter
126 * @arg unit destination unit pointer
128 * Cancels downa bit counter until it reaches a reasonable
129 * unit. The chosen unit is assigned to \a unit.
131 * @return The cancelled down bit counter in the new unit.
133 double nl_cancel_down_bits(unsigned long long l, char **unit)
135 if (l >= 1099511627776ULL) {
137 return ((double) l) / 1099511627776ULL;
138 } else if (l >= 1073741824) {
140 return ((double) l) / 1073741824;
141 } else if (l >= 1048576) {
143 return ((double) l) / 1048576;
144 } else if (l >= 1024) {
146 return ((double) l) / 1024;
155 * Cancel down a micro second value
156 * @arg l micro seconds
157 * @arg unit destination unit pointer
159 * Cancels down a microsecond counter until it reaches a
160 * reasonable unit. The chosen unit is assigned to \a unit.
162 * @return The cancelled down microsecond in the new unit
164 double nl_cancel_down_us(uint32_t l, char **unit)
168 return ((double) l) / 1000000;
169 } else if (l >= 1000) {
171 return ((double) l) / 1000;
181 * @name Generic Unit Translations
186 * Convert a character string to a size
187 * @arg str size encoded as character string
189 * Converts the specified size as character to the corresponding
192 * Supported formats are:
193 * - b,kb/k,m/mb,gb/g for bytes
194 * - bit,kbit/mbit/gbit
196 * @return The number of bytes or -1 if the string is unparseable
198 long nl_size2int(const char *str)
201 long l = strtol(str, &p, 0);
206 if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
208 else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
210 else if (!strcasecmp(p, "gbit"))
211 l *= 1024*1024*1024/8;
212 else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
214 else if (!strcasecmp(p, "mbit"))
216 else if (!strcasecmp(p, "kbit"))
218 else if (!strcasecmp(p, "bit"))
220 else if (strcasecmp(p, "b") != 0)
228 * Convert a character string to a probability
229 * @arg str probability encoded as character string
231 * Converts the specified probability as character to the
232 * corresponding probability number.
234 * Supported formats are:
238 * @return The probability relative to NL_PROB_MIN and NL_PROB_MAX
240 long nl_prob2int(const char *str)
243 double d = strtod(str, &p);
251 if (d > 1.0f || d < 0.0f)
254 if (*p && strcmp(p, "%") != 0)
257 return rint(d * NL_PROB_MAX);
263 * @name Time Translations
268 static uint32_t user_hz = USER_HZ;
270 static uint32_t user_hz = 100;
273 static double ticks_per_usec = 1.0f;
275 /* Retrieves the configured HZ and ticks/us value in the kernel.
276 * The value is cached. Supported ways of getting it:
278 * 1) environment variable
279 * 2) /proc/net/psched and sysconf
281 * Supports the environment variables:
282 * PROC_NET_PSCHED - may point to psched file in /proc
283 * PROC_ROOT - may point to /proc fs */
284 static void __init get_psched_settings(void)
286 char name[FILENAME_MAX];
291 long hz = strtol(getenv("HZ"), NULL, 0);
293 if (LONG_MIN != hz && LONG_MAX != hz) {
300 user_hz = sysconf(_SC_CLK_TCK);
302 if (getenv("TICKS_PER_USEC")) {
303 double t = strtod(getenv("TICKS_PER_USEC"), NULL);
307 if (getenv("PROC_NET_PSCHED"))
308 snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
309 else if (getenv("PROC_ROOT"))
310 snprintf(name, sizeof(name), "%s/net/psched",
311 getenv("PROC_ROOT"));
313 strncpy(name, "/proc/net/psched", sizeof(name) - 1);
315 if ((fd = fopen(name, "r"))) {
317 /* the file contains 4 hexadecimals, but we just use
318 the first two of them */
319 fscanf(fd, "%08x %08x", &tick, &us);
320 ticks_per_usec = (double)tick/(double)us;
328 * Return the value of HZ
337 * Convert micro seconds to ticks
338 * @arg us micro seconds
339 * @return number of ticks
341 uint32_t nl_us2ticks(uint32_t us)
343 return us * ticks_per_usec;
348 * Convert ticks to micro seconds
349 * @arg ticks number of ticks
350 * @return microseconds
352 uint32_t nl_ticks2us(uint32_t ticks)
354 return ticks / ticks_per_usec;
357 int nl_str2msec(const char *str, uint64_t *result)
359 uint64_t total = 0, l;
364 l = strtoul(str, &p, 0);
368 plen = strcspn(p, " \t");
372 else if (!strncasecmp(p, "sec", plen))
374 else if (!strncasecmp(p, "min", plen))
375 total += (l * 1000*60);
376 else if (!strncasecmp(p, "hour", plen))
377 total += (l * 1000*60*60);
378 else if (!strncasecmp(p, "day", plen))
379 total += (l * 1000*60*60*24);
386 } while (*str && *p);
394 * Convert milliseconds to a character string
395 * @arg msec number of milliseconds
396 * @arg buf destination buffer
397 * @arg len buffer length
399 * Converts milliseconds to a character string split up in days, hours,
400 * minutes, seconds, and milliseconds and stores it in the specified
401 * destination buffer.
403 * @return The destination buffer.
405 char * nl_msec2str(uint64_t msec, char *buf, size_t len)
408 char *units[] = {"d", "h", "m", "s", "msec"};
410 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
411 _SPLIT(0, 86400000); /* days */
412 _SPLIT(1, 3600000); /* hours */
413 _SPLIT(2, 60000); /* minutes */
414 _SPLIT(3, 1000); /* seconds */
420 for (i = 0; i < ARRAY_SIZE(split); i++) {
423 snprintf(t, sizeof(t), "%s%d%s",
424 strlen(buf) ? " " : "", split[i], units[i]);
425 strncat(buf, t, len - strlen(buf) - 1);
435 * @name Netlink Family Translations
439 static struct trans_tbl nlfamilies[] = {
440 __ADD(NETLINK_ROUTE,route)
441 __ADD(NETLINK_USERSOCK,usersock)
442 __ADD(NETLINK_FIREWALL,firewall)
443 __ADD(NETLINK_INET_DIAG,inetdiag)
444 __ADD(NETLINK_NFLOG,nflog)
445 __ADD(NETLINK_XFRM,xfrm)
446 __ADD(NETLINK_SELINUX,selinux)
447 __ADD(NETLINK_ISCSI,iscsi)
448 __ADD(NETLINK_AUDIT,audit)
449 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
450 __ADD(NETLINK_CONNECTOR,connector)
451 __ADD(NETLINK_NETFILTER,netfilter)
452 __ADD(NETLINK_IP6_FW,ip6_fw)
453 __ADD(NETLINK_DNRTMSG,dnrtmsg)
454 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
455 __ADD(NETLINK_GENERIC,generic)
456 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
457 __ADD(NETLINK_ECRYPTFS,ecryptfs)
460 char * nl_nlfamily2str(int family, char *buf, size_t size)
462 return __type2str(family, buf, size, nlfamilies,
463 ARRAY_SIZE(nlfamilies));
466 int nl_str2nlfamily(const char *name)
468 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
476 * @name Link Layer Protocol Translations
480 static struct trans_tbl llprotos[] = {
482 __ADD(ARPHRD_ETHER,ether)
483 __ADD(ARPHRD_EETHER,eether)
484 __ADD(ARPHRD_AX25,ax25)
485 __ADD(ARPHRD_PRONET,pronet)
486 __ADD(ARPHRD_CHAOS,chaos)
487 __ADD(ARPHRD_IEEE802,ieee802)
488 __ADD(ARPHRD_ARCNET,arcnet)
489 __ADD(ARPHRD_APPLETLK,atalk)
490 __ADD(ARPHRD_DLCI,dlci)
491 __ADD(ARPHRD_ATM,atm)
492 __ADD(ARPHRD_METRICOM,metricom)
493 __ADD(ARPHRD_IEEE1394,ieee1394)
495 __ADD(ARPHRD_EUI64,eui64)
497 __ADD(ARPHRD_INFINIBAND,infiniband)
498 __ADD(ARPHRD_SLIP,slip)
499 __ADD(ARPHRD_CSLIP,cslip)
500 __ADD(ARPHRD_SLIP6,slip6)
501 __ADD(ARPHRD_CSLIP6,cslip6)
502 __ADD(ARPHRD_RSRVD,rsrvd)
503 __ADD(ARPHRD_ADAPT,adapt)
504 __ADD(ARPHRD_ROSE,rose)
505 __ADD(ARPHRD_X25,x25)
507 __ADD(ARPHRD_HWX25,hwx25)
509 __ADD(ARPHRD_PPP,ppp)
510 __ADD(ARPHRD_HDLC,hdlc)
511 __ADD(ARPHRD_LAPB,lapb)
512 __ADD(ARPHRD_DDCMP,ddcmp)
513 __ADD(ARPHRD_RAWHDLC,rawhdlc)
514 __ADD(ARPHRD_TUNNEL,ipip)
515 __ADD(ARPHRD_TUNNEL6,tunnel6)
516 __ADD(ARPHRD_FRAD,frad)
517 __ADD(ARPHRD_SKIP,skip)
518 __ADD(ARPHRD_LOOPBACK,loopback)
519 __ADD(ARPHRD_LOCALTLK,localtlk)
520 __ADD(ARPHRD_FDDI,fddi)
521 __ADD(ARPHRD_BIF,bif)
522 __ADD(ARPHRD_SIT,sit)
523 __ADD(ARPHRD_IPDDP,ip/ddp)
524 __ADD(ARPHRD_IPGRE,gre)
525 __ADD(ARPHRD_PIMREG,pimreg)
526 __ADD(ARPHRD_HIPPI,hippi)
527 __ADD(ARPHRD_ASH,ash)
528 __ADD(ARPHRD_ECONET,econet)
529 __ADD(ARPHRD_IRDA,irda)
530 __ADD(ARPHRD_FCPP,fcpp)
531 __ADD(ARPHRD_FCAL,fcal)
532 __ADD(ARPHRD_FCPL,fcpl)
533 __ADD(ARPHRD_FCFABRIC,fcfb_0)
534 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
535 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
536 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
537 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
538 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
539 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
540 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
541 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
542 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
543 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
544 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
545 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
546 __ADD(ARPHRD_IEEE802_TR,tr)
547 __ADD(ARPHRD_IEEE80211,ieee802.11)
548 #ifdef ARPHRD_IEEE80211_PRISM
549 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
552 __ADD(ARPHRD_VOID,void)
556 char * nl_llproto2str(int llproto, char *buf, size_t len)
558 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
561 int nl_str2llproto(const char *name)
563 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
570 * @name Ethernet Protocol Translations
574 static struct trans_tbl ether_protos[] = {
575 __ADD(ETH_P_LOOP,loop)
577 __ADD(ETH_P_PUPAT,pupat)
582 __ADD(ETH_P_IEEEPUP,ieeepup)
583 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
585 __ADD(ETH_P_DNA_DL,dna_dl)
586 __ADD(ETH_P_DNA_RC,dna_rc)
587 __ADD(ETH_P_DNA_RT,dna_rt)
589 __ADD(ETH_P_DIAG,diag)
590 __ADD(ETH_P_CUST,cust)
592 __ADD(ETH_P_RARP,rarp)
593 __ADD(ETH_P_ATALK,atalk)
594 __ADD(ETH_P_AARP,aarp)
596 __ADD(ETH_P_8021Q,802.1q)
599 __ADD(ETH_P_IPV6,ipv6)
601 __ADD(ETH_P_WCCP,wccp)
603 __ADD(ETH_P_PPP_DISC,ppp_disc)
604 __ADD(ETH_P_PPP_SES,ppp_ses)
605 __ADD(ETH_P_MPLS_UC,mpls_uc)
606 __ADD(ETH_P_MPLS_MC,mpls_mc)
607 __ADD(ETH_P_ATMMPOA,atmmpoa)
608 __ADD(ETH_P_ATMFATE,atmfate)
609 __ADD(ETH_P_EDP2,edp2)
610 __ADD(ETH_P_802_3,802.3)
611 __ADD(ETH_P_AX25,ax25)
613 __ADD(ETH_P_802_2,802.2)
614 __ADD(ETH_P_SNAP,snap)
615 __ADD(ETH_P_DDCMP,ddcmp)
616 __ADD(ETH_P_WAN_PPP,wan_ppp)
617 __ADD(ETH_P_PPP_MP,ppp_mp)
618 __ADD(ETH_P_LOCALTALK,localtalk)
619 __ADD(ETH_P_PPPTALK,ppptalk)
620 __ADD(ETH_P_TR_802_2,tr_802.2)
621 __ADD(ETH_P_MOBITEX,mobitex)
622 __ADD(ETH_P_CONTROL,control)
623 __ADD(ETH_P_IRDA,irda)
624 __ADD(ETH_P_ECONET,econet)
625 __ADD(ETH_P_HDLC,hdlc)
628 char *nl_ether_proto2str(int eproto, char *buf, size_t len)
630 return __type2str(eproto, buf, len, ether_protos,
631 ARRAY_SIZE(ether_protos));
634 int nl_str2ether_proto(const char *name)
636 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
642 * @name IP Protocol Translations
646 char *nl_ip_proto2str(int proto, char *buf, size_t len)
648 struct protoent *p = getprotobynumber(proto);
651 snprintf(buf, len, "%s", p->p_name);
655 snprintf(buf, len, "0x%x", proto);
659 int nl_str2ip_proto(const char *name)
661 struct protoent *p = getprotobyname(name);
668 l = strtoul(name, &end, 0);
669 if (l == ULONG_MAX || *end != '\0')
670 return -NLE_OBJ_NOTFOUND;
678 * @name Dumping Helpers
683 * Handle a new line while dumping
684 * @arg params Dumping parameters
686 * This function must be called before dumping any onto a
687 * new line. It will ensure proper prefixing as specified
688 * by the dumping parameters.
690 * @note This function will NOT dump any newlines itself
692 void nl_new_line(struct nl_dump_params *params)
696 if (params->dp_prefix) {
698 for (i = 0; i < params->dp_prefix; i++) {
700 fprintf(params->dp_fd, " ");
701 else if (params->dp_buf)
702 strncat(params->dp_buf, " ",
704 sizeof(params->dp_buf) - 1);
708 if (params->dp_nl_cb)
709 params->dp_nl_cb(params, params->dp_line);
712 static void dump_one(struct nl_dump_params *parms, const char *fmt,
716 vfprintf(parms->dp_fd, fmt, args);
717 else if (parms->dp_buf || parms->dp_cb) {
719 vasprintf(&buf, fmt, args);
721 parms->dp_cb(parms, buf);
723 strncat(parms->dp_buf, buf,
724 parms->dp_buflen - strlen(parms->dp_buf) - 1);
731 * Dump a formatted character string
732 * @arg params Dumping parameters
733 * @arg fmt printf style formatting string
734 * @arg ... Arguments to formatting string
736 * Dumps a printf style formatting string to the output device
737 * as specified by the dumping parameters.
739 void nl_dump(struct nl_dump_params *params, const char *fmt, ...)
744 dump_one(params, fmt, args);
748 void nl_dump_line(struct nl_dump_params *parms, const char *fmt, ...)
755 dump_one(parms, fmt, args);
764 int __trans_list_add(int i, const char *a, struct nl_list_head *head)
766 struct trans_list *tl;
768 tl = calloc(1, sizeof(*tl));
775 nl_list_add_tail(&tl->list, head);
780 void __trans_list_clear(struct nl_list_head *head)
782 struct trans_list *tl, *next;
784 nl_list_for_each_entry_safe(tl, next, head, list) {
790 char *__type2str(int type, char *buf, size_t len, struct trans_tbl *tbl,
794 for (i = 0; i < tbl_len; i++) {
795 if (tbl[i].i == type) {
796 snprintf(buf, len, "%s", tbl[i].a);
801 snprintf(buf, len, "0x%x", type);
805 char *__list_type2str(int type, char *buf, size_t len,
806 struct nl_list_head *head)
808 struct trans_list *tl;
810 nl_list_for_each_entry(tl, head, list) {
812 snprintf(buf, len, "%s", tl->a);
817 snprintf(buf, len, "0x%x", type);
821 char *__flags2str(int flags, char *buf, size_t len,
822 struct trans_tbl *tbl, size_t tbl_len)
829 for (i = 0; i < tbl_len; i++) {
830 if (tbl[i].i & tmp) {
832 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
834 strncat(buf, ",", len - strlen(buf) - 1);
841 int __str2type(const char *buf, struct trans_tbl *tbl, size_t tbl_len)
850 for (i = 0; i < tbl_len; i++)
851 if (!strcasecmp(tbl[i].a, buf))
854 l = strtoul(buf, &end, 0);
855 if (l == ULONG_MAX || *end != '\0')
856 return -NLE_OBJ_NOTFOUND;
861 int __list_str2type(const char *buf, struct nl_list_head *head)
863 struct trans_list *tl;
870 nl_list_for_each_entry(tl, head, list) {
871 if (!strcasecmp(tl->a, buf))
875 l = strtoul(buf, &end, 0);
876 if (l == ULONG_MAX || *end != '\0')
877 return -NLE_OBJ_NOTFOUND;
882 int __str2flags(const char *buf, struct trans_tbl *tbl, size_t tbl_len)
884 int i, flags = 0, len;
885 char *p = (char *) buf, *t;
892 len = t ? t - p : strlen(p);
893 for (i = 0; i < tbl_len; i++)
894 if (!strncasecmp(tbl[i].a, p, len))
906 void dump_from_ops(struct nl_object *obj, struct nl_dump_params *params)
908 int type = params->dp_type;
910 if (type < 0 || type > NL_DUMP_MAX)
915 if (params->dp_dump_msgtype) {
920 dp_dump_line(params, 0, "%s ",
921 nl_cache_mngt_type2name(obj->ce_ops,
922 obj->ce_ops->co_protocol,
926 params->dp_pre_dump = 1;
929 if (obj->ce_ops->oo_dump[type])
930 obj->ce_ops->oo_dump[type](obj, params);