27 #include <netlink-private/netlink.h>
28 #include <netlink/netlink.h>
29 #include <netlink/utils.h>
30 #include <linux/socket.h>
57 static void __init nl_debug_init(
void)
61 if ((nldbg = getenv(
"NLDBG"))) {
62 long level = strtol(nldbg, &end, 0);
67 nl_debug_dp.
dp_fd = stderr;
71 int __nl_read_num_str_file(
const char *path,
int (*cb)(
long,
const char *))
76 fd = fopen(path,
"r");
78 return -nl_syserr2nlerr(errno);
80 while (fgets(buf,
sizeof(buf), fd)) {
85 if (*buf ==
'#' || *buf ==
'\n' || *buf ==
'\r')
88 num = strtol(buf, &end, 0);
94 if (num == LONG_MIN || num == LONG_MAX) {
99 while (*end ==
' ' || *end ==
'\t')
102 goodlen = strcspn(end,
"#\r\n\t ");
141 if (l >= 1099511627776LL) {
143 return ((
double) l) / 1099511627776LL;
144 }
else if (l >= 1073741824) {
146 return ((
double) l) / 1073741824;
147 }
else if (l >= 1048576) {
149 return ((
double) l) / 1048576;
150 }
else if (l >= 1024) {
152 return ((
double) l) / 1024;
172 if (l >= 1000000000000ULL) {
174 return ((
double) l) / 1000000000000ULL;
177 if (l >= 1000000000) {
179 return ((
double) l) / 1000000000;
184 return ((
double) l) / 1000000;
189 return ((
double) l) / 1000;
196 int nl_rate2str(
unsigned long long rate,
int type,
char *buf,
size_t len)
214 return snprintf(buf, len,
"%.2f%s/s", frac, unit);
231 return ((
double) l) / 1000000;
232 }
else if (l >= 1000) {
234 return ((
double) l) / 1000;
267 long l = strtol(str, &p, 0);
272 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
274 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
276 else if (!strcasecmp(p,
"gbit"))
278 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
280 else if (!strcasecmp(p,
"mbit"))
282 else if (!strcasecmp(p,
"kbit"))
284 else if (!strcasecmp(p,
"bit"))
286 else if (strcasecmp(p,
"b") != 0)
293 static const struct {
297 { 1024. * 1024. * 1024. * 1024. * 1024.,
"EiB" },
298 { 1024. * 1024. * 1024. * 1024.,
"TiB" },
299 { 1024. * 1024. * 1024.,
"GiB" },
300 { 1024. * 1024.,
"MiB" },
331 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
332 if (size >= size_units[i].limit) {
333 snprintf(buf, len,
"%.2g%s",
334 (
double) size / size_units[i].limit,
359 double d = strtod(str, &p);
367 if (d > 1.0f || d < 0.0f)
370 if (*p && strcmp(p,
"%") != 0)
387 static uint32_t user_hz = USER_HZ;
388 static uint32_t psched_hz = USER_HZ;
390 static double ticks_per_usec = 1.0f;
401 static void __init get_psched_settings(
void)
403 char name[FILENAME_MAX];
408 long hz = strtol(getenv(
"HZ"), NULL, 0);
410 if (LONG_MIN != hz && LONG_MAX != hz) {
417 user_hz = sysconf(_SC_CLK_TCK);
421 if (getenv(
"TICKS_PER_USEC")) {
422 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
426 if (getenv(
"PROC_NET_PSCHED"))
427 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
428 else if (getenv(
"PROC_ROOT"))
429 snprintf(name,
sizeof(name),
"%s/net/psched",
430 getenv(
"PROC_ROOT"));
432 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
434 if ((fd = fopen(name,
"r"))) {
435 unsigned int ns_per_usec, ns_per_tick, nom, denom;
437 if (fscanf(fd,
"%08x %08x %08x %08x",
438 &ns_per_usec, &ns_per_tick, &nom, &denom) != 4) {
439 NL_DBG(1,
"Fatal error: can not read psched settings from \"%s\". " \
440 "Try to set TICKS_PER_USEC, PROC_NET_PSCHED or PROC_ROOT " \
441 "environment variables\n", name);
445 ticks_per_usec = (double) ns_per_usec /
446 (
double) ns_per_tick;
480 return us * ticks_per_usec;
491 return ticks / ticks_per_usec;
494 int nl_str2msec(
const char *str, uint64_t *result)
496 uint64_t total = 0, l;
501 l = strtoul(str, &p, 0);
505 plen = strcspn(p,
" \t");
509 else if (!strncasecmp(p,
"sec", plen))
511 else if (!strncasecmp(p,
"min", plen))
512 total += (l * 1000*60);
513 else if (!strncasecmp(p,
"hour", plen))
514 total += (l * 1000*60*60);
515 else if (!strncasecmp(p,
"day", plen))
516 total += (l * 1000*60*60*24);
523 }
while (*str && *p);
546 static const char *units[5] = {
"d",
"h",
"m",
"s",
"msec"};
547 char *
const buf_orig = buf;
549 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
557 for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
561 l = snprintf(buf, len,
"%s%" PRIu64
"%s",
562 (buf==buf_orig) ?
"" :
" ", split[i], units[i]);
577 static const struct trans_tbl nlfamilies[] = {
578 __ADD(NETLINK_ROUTE,route)
579 __ADD(NETLINK_USERSOCK,usersock)
580 __ADD(NETLINK_FIREWALL,firewall)
581 __ADD(NETLINK_INET_DIAG,inetdiag)
582 __ADD(NETLINK_NFLOG,nflog)
583 __ADD(NETLINK_XFRM,xfrm)
584 __ADD(NETLINK_SELINUX,selinux)
585 __ADD(NETLINK_ISCSI,iscsi)
586 __ADD(NETLINK_AUDIT,audit)
587 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
588 __ADD(NETLINK_CONNECTOR,connector)
589 __ADD(NETLINK_NETFILTER,netfilter)
590 __ADD(NETLINK_IP6_FW,ip6_fw)
591 __ADD(NETLINK_DNRTMSG,dnrtmsg)
592 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
593 __ADD(NETLINK_GENERIC,generic)
594 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
595 __ADD(NETLINK_ECRYPTFS,ecryptfs)
598 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
600 return __type2str(family, buf, size, nlfamilies,
601 ARRAY_SIZE(nlfamilies));
604 int nl_str2nlfamily(
const char *name)
606 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
618 static const struct trans_tbl llprotos[] = {
620 __ADD(ARPHRD_ETHER,ether)
621 __ADD(ARPHRD_EETHER,eether)
622 __ADD(ARPHRD_AX25,ax25)
623 __ADD(ARPHRD_PRONET,pronet)
624 __ADD(ARPHRD_CHAOS,chaos)
625 __ADD(ARPHRD_IEEE802,ieee802)
626 __ADD(ARPHRD_ARCNET,arcnet)
627 __ADD(ARPHRD_APPLETLK,atalk)
628 __ADD(ARPHRD_DLCI,dlci)
629 __ADD(ARPHRD_ATM,atm)
630 __ADD(ARPHRD_METRICOM,metricom)
631 __ADD(ARPHRD_IEEE1394,ieee1394)
633 __ADD(ARPHRD_EUI64,eui64)
635 __ADD(ARPHRD_INFINIBAND,infiniband)
636 __ADD(ARPHRD_SLIP,slip)
637 __ADD(ARPHRD_CSLIP,cslip)
638 __ADD(ARPHRD_SLIP6,slip6)
639 __ADD(ARPHRD_CSLIP6,cslip6)
640 __ADD(ARPHRD_RSRVD,rsrvd)
641 __ADD(ARPHRD_ADAPT,adapt)
642 __ADD(ARPHRD_ROSE,rose)
643 __ADD(ARPHRD_X25,x25)
645 __ADD(ARPHRD_HWX25,hwx25)
647 __ADD(ARPHRD_CAN,can)
648 __ADD(ARPHRD_PPP,ppp)
649 __ADD(ARPHRD_HDLC,hdlc)
650 __ADD(ARPHRD_LAPB,lapb)
651 __ADD(ARPHRD_DDCMP,ddcmp)
652 __ADD(ARPHRD_RAWHDLC,rawhdlc)
653 __ADD(ARPHRD_TUNNEL,ipip)
654 __ADD(ARPHRD_TUNNEL6,tunnel6)
655 __ADD(ARPHRD_FRAD,frad)
656 __ADD(ARPHRD_SKIP,skip)
657 __ADD(ARPHRD_LOOPBACK,loopback)
658 __ADD(ARPHRD_LOCALTLK,localtlk)
659 __ADD(ARPHRD_FDDI,fddi)
660 __ADD(ARPHRD_BIF,bif)
661 __ADD(ARPHRD_SIT,sit)
662 __ADD(ARPHRD_IPDDP,ip/ddp)
663 __ADD(ARPHRD_IPGRE,gre)
664 __ADD(ARPHRD_PIMREG,pimreg)
665 __ADD(ARPHRD_HIPPI,hippi)
666 __ADD(ARPHRD_ASH,ash)
667 __ADD(ARPHRD_ECONET,econet)
668 __ADD(ARPHRD_IRDA,irda)
669 __ADD(ARPHRD_FCPP,fcpp)
670 __ADD(ARPHRD_FCAL,fcal)
671 __ADD(ARPHRD_FCPL,fcpl)
672 __ADD(ARPHRD_FCFABRIC,fcfb_0)
673 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
674 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
675 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
676 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
677 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
678 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
679 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
680 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
681 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
682 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
683 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
684 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
685 __ADD(ARPHRD_IEEE802_TR,tr)
686 __ADD(ARPHRD_IEEE80211,ieee802.11)
687 __ADD(ARPHRD_PHONET,phonet)
688 __ADD(ARPHRD_CAIF, caif)
689 #ifdef ARPHRD_IEEE80211_PRISM
690 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
693 __ADD(ARPHRD_VOID,
void)
696 __ADD(ARPHRD_NONE,nohdr)
700 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
702 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
705 int nl_str2llproto(
const char *name)
707 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
718 static const struct trans_tbl ether_protos[] = {
719 __ADD(ETH_P_LOOP,loop)
721 __ADD(ETH_P_PUPAT,pupat)
726 __ADD(ETH_P_IEEEPUP,ieeepup)
727 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
729 __ADD(ETH_P_DNA_DL,dna_dl)
730 __ADD(ETH_P_DNA_RC,dna_rc)
731 __ADD(ETH_P_DNA_RT,dna_rt)
733 __ADD(ETH_P_DIAG,diag)
734 __ADD(ETH_P_CUST,cust)
737 __ADD(ETH_P_RARP,rarp)
738 __ADD(ETH_P_ATALK,atalk)
739 __ADD(ETH_P_AARP,aarp)
741 __ADD(ETH_P_8021Q,802.1q)
744 __ADD(ETH_P_IPV6,ipv6)
745 __ADD(ETH_P_PAUSE,pause)
746 __ADD(ETH_P_SLOW,slow)
748 __ADD(ETH_P_WCCP,wccp)
750 __ADD(ETH_P_PPP_DISC,ppp_disc)
751 __ADD(ETH_P_PPP_SES,ppp_ses)
752 __ADD(ETH_P_MPLS_UC,mpls_uc)
753 __ADD(ETH_P_MPLS_MC,mpls_mc)
754 __ADD(ETH_P_ATMMPOA,atmmpoa)
755 __ADD(ETH_P_LINK_CTL,link_ctl)
756 __ADD(ETH_P_ATMFATE,atmfate)
759 __ADD(ETH_P_TIPC,tipc)
760 __ADD(ETH_P_1588,ieee1588)
761 __ADD(ETH_P_FCOE,fcoe)
763 __ADD(ETH_P_EDSA,edsa)
764 __ADD(ETH_P_EDP2,edp2)
765 __ADD(ETH_P_802_3,802.3)
766 __ADD(ETH_P_AX25,ax25)
768 __ADD(ETH_P_802_2,802.2)
769 __ADD(ETH_P_SNAP,snap)
770 __ADD(ETH_P_DDCMP,ddcmp)
771 __ADD(ETH_P_WAN_PPP,wan_ppp)
772 __ADD(ETH_P_PPP_MP,ppp_mp)
773 __ADD(ETH_P_LOCALTALK,localtalk)
775 __ADD(ETH_P_PPPTALK,ppptalk)
776 __ADD(ETH_P_TR_802_2,tr_802.2)
777 __ADD(ETH_P_MOBITEX,mobitex)
778 __ADD(ETH_P_CONTROL,control)
779 __ADD(ETH_P_IRDA,irda)
780 __ADD(ETH_P_ECONET,econet)
781 __ADD(ETH_P_HDLC,hdlc)
782 __ADD(ETH_P_ARCNET,arcnet)
784 __ADD(ETH_P_TRAILER,trailer)
785 __ADD(ETH_P_PHONET,phonet)
786 __ADD(ETH_P_IEEE802154,ieee802154)
787 __ADD(ETH_P_CAIF,caif)
790 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
792 return __type2str(eproto, buf, len, ether_protos,
793 ARRAY_SIZE(ether_protos));
796 int nl_str2ether_proto(
const char *name)
798 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
808 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
810 struct protoent *p = getprotobynumber(proto);
813 snprintf(buf, len,
"%s", p->p_name);
817 snprintf(buf, len,
"0x%x", proto);
821 int nl_str2ip_proto(
const char *name)
823 struct protoent *p = getprotobyname(name);
830 l = strtoul(name, &end, 0);
831 if (l == ULONG_MAX || *end !=
'\0')
832 return -NLE_OBJ_NOTFOUND;
860 for (i = 0; i < params->
dp_prefix; i++) {
862 fprintf(params->
dp_fd,
" ");
864 strncat(params->
dp_buf,
" ",
866 strlen(params->
dp_buf) - 1);
871 params->
dp_nl_cb(params, params->dp_line);
874 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
878 vfprintf(parms->
dp_fd, fmt, args);
881 if (vasprintf(&buf, fmt, args) >= 0) {
883 parms->
dp_cb(parms, buf);
885 strncat(parms->
dp_buf, buf,
887 strlen(parms->
dp_buf) - 1);
908 dump_one(params, fmt, args);
912 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
919 dump_one(parms, fmt, args);
928 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
930 struct trans_list *tl;
932 tl = calloc(1,
sizeof(*tl));
939 nl_list_add_tail(&tl->list, head);
946 struct trans_list *tl, *next;
948 nl_list_for_each_entry_safe(tl, next, head, list) {
953 nl_init_list_head(head);
956 char *__type2str(
int type,
char *buf,
size_t len,
957 const struct trans_tbl *tbl,
size_t tbl_len)
960 for (i = 0; i < tbl_len; i++) {
961 if (tbl[i].i == type) {
962 snprintf(buf, len,
"%s", tbl[i].a);
967 snprintf(buf, len,
"0x%x", type);
971 char *__list_type2str(
int type,
char *buf,
size_t len,
974 struct trans_list *tl;
976 nl_list_for_each_entry(tl, head, list) {
978 snprintf(buf, len,
"%s", tl->a);
983 snprintf(buf, len,
"0x%x", type);
987 char *__flags2str(
int flags,
char *buf,
size_t len,
988 const struct trans_tbl *tbl,
size_t tbl_len)
995 for (i = 0; i < tbl_len; i++) {
996 if (tbl[i].i & tmp) {
998 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
1000 strncat(buf,
",", len - strlen(buf) - 1);
1007 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1016 for (i = 0; i < tbl_len; i++)
1017 if (!strcasecmp(tbl[i].a, buf))
1020 l = strtoul(buf, &end, 0);
1021 if (l == ULONG_MAX || *end !=
'\0')
1022 return -NLE_OBJ_NOTFOUND;
1027 int __list_str2type(
const char *buf,
struct nl_list_head *head)
1029 struct trans_list *tl;
1036 nl_list_for_each_entry(tl, head, list) {
1037 if (!strcasecmp(tl->a, buf))
1041 l = strtoul(buf, &end, 0);
1042 if (l == ULONG_MAX || *end !=
'\0')
1043 return -NLE_OBJ_NOTFOUND;
1048 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1053 char *p = (
char *) buf, *t;
1060 len = t ? t - p : strlen(p);
1061 for (i = 0; i < tbl_len; i++)
1062 if (len == strlen(tbl[i].a) &&
1063 !strncasecmp(tbl[i].a, p, len))
1075 void dump_from_ops(
struct nl_object *obj,
struct nl_dump_params *params)
1079 if (type < 0 || type > NL_DUMP_MAX)
1082 params->dp_line = 0;
1089 dp_dump_line(params, 0,
"%s ",
1090 nl_cache_mngt_type2name(obj->ce_ops,
1091 obj->ce_ops->co_protocol,
1098 if (obj->ce_ops->oo_dump[type])
1099 obj->ce_ops->oo_dump[type](obj, params);