27 #include <netlink-local.h>
28 #include <netlink/netlink.h>
29 #include <netlink/utils.h>
30 #include <linux/socket.h>
56 static void __init nl_debug_init(
void)
60 if ((nldbg = getenv(
"NLDBG"))) {
61 long level = strtol(nldbg, &end, 0);
66 nl_debug_dp.
dp_fd = stderr;
69 int __nl_read_num_str_file(
const char *path,
int (*cb)(
long,
const char *))
74 fd = fopen(path,
"r");
76 return -nl_syserr2nlerr(errno);
78 while (fgets(buf,
sizeof(buf), fd)) {
83 if (*buf ==
'#' || *buf ==
'\n' || *buf ==
'\r')
86 num = strtol(buf, &end, 0);
90 if (num == LONG_MIN || num == LONG_MAX)
93 while (*end ==
' ' || *end ==
'\t')
96 goodlen = strcspn(end,
"#\r\n\t ");
131 if (l >= 1099511627776LL) {
133 return ((
double) l) / 1099511627776LL;
134 }
else if (l >= 1073741824) {
136 return ((
double) l) / 1073741824;
137 }
else if (l >= 1048576) {
139 return ((
double) l) / 1048576;
140 }
else if (l >= 1024) {
142 return ((
double) l) / 1024;
162 if (l >= 1000000000000ULL) {
164 return ((
double) l) / 1000000000000ULL;
167 if (l >= 1000000000) {
169 return ((
double) l) / 1000000000;
174 return ((
double) l) / 1000000;
179 return ((
double) l) / 1000;
186 int nl_rate2str(
unsigned long long rate,
int type,
char *buf,
size_t len)
204 return snprintf(buf, len,
"%.2f%s/s", frac, unit);
221 return ((
double) l) / 1000000;
222 }
else if (l >= 1000) {
224 return ((
double) l) / 1000;
257 long l = strtol(str, &p, 0);
262 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
264 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
266 else if (!strcasecmp(p,
"gbit"))
268 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
270 else if (!strcasecmp(p,
"mbit"))
272 else if (!strcasecmp(p,
"kbit"))
274 else if (!strcasecmp(p,
"bit"))
276 else if (strcasecmp(p,
"b") != 0)
283 static const struct {
287 { 1024. * 1024. * 1024. * 1024. * 1024.,
"EiB" },
288 { 1024. * 1024. * 1024. * 1024.,
"TiB" },
289 { 1024. * 1024. * 1024.,
"GiB" },
290 { 1024. * 1024.,
"MiB" },
321 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
322 if (size >= size_units[i].limit) {
323 snprintf(buf, len,
"%.2g%s",
324 (
double) size / size_units[i].limit,
349 double d = strtod(str, &p);
357 if (d > 1.0f || d < 0.0f)
360 if (*p && strcmp(p,
"%") != 0)
377 static uint32_t user_hz = USER_HZ;
378 static uint32_t psched_hz = USER_HZ;
380 static double ticks_per_usec = 1.0f;
391 static void __init get_psched_settings(
void)
393 char name[FILENAME_MAX];
398 long hz = strtol(getenv(
"HZ"), NULL, 0);
400 if (LONG_MIN != hz && LONG_MAX != hz) {
407 user_hz = sysconf(_SC_CLK_TCK);
411 if (getenv(
"TICKS_PER_USEC")) {
412 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
416 if (getenv(
"PROC_NET_PSCHED"))
417 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
418 else if (getenv(
"PROC_ROOT"))
419 snprintf(name,
sizeof(name),
"%s/net/psched",
420 getenv(
"PROC_ROOT"));
422 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
424 if ((fd = fopen(name,
"r"))) {
425 unsigned int ns_per_usec, ns_per_tick, nom, denom;
427 if (fscanf(fd,
"%08x %08x %08x %08x",
428 &ns_per_usec, &ns_per_tick, &nom, &denom) != 4) {
429 fprintf(stderr,
"Fatal error: can not read psched settings from \"%s\". " \
430 "Try to set TICKS_PER_USEC, PROC_NET_PSCHED or PROC_ROOT " \
431 "environment variables\n", name);
435 ticks_per_usec = (double) ns_per_usec /
436 (
double) ns_per_tick;
470 return us * ticks_per_usec;
481 return ticks / ticks_per_usec;
484 int nl_str2msec(
const char *str, uint64_t *result)
486 uint64_t total = 0, l;
491 l = strtoul(str, &p, 0);
495 plen = strcspn(p,
" \t");
499 else if (!strncasecmp(p,
"sec", plen))
501 else if (!strncasecmp(p,
"min", plen))
502 total += (l * 1000*60);
503 else if (!strncasecmp(p,
"hour", plen))
504 total += (l * 1000*60*60);
505 else if (!strncasecmp(p,
"day", plen))
506 total += (l * 1000*60*60*24);
513 }
while (*str && *p);
536 static const char *units[5] = {
"d",
"h",
"m",
"s",
"msec"};
537 char *
const buf_orig = buf;
539 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
547 for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
551 l = snprintf(buf, len,
"%s%" PRIu64
"%s",
552 (buf==buf_orig) ?
"" :
" ", split[i], units[i]);
567 static const struct trans_tbl nlfamilies[] = {
568 __ADD(NETLINK_ROUTE,route)
569 __ADD(NETLINK_USERSOCK,usersock)
570 __ADD(NETLINK_FIREWALL,firewall)
571 __ADD(NETLINK_INET_DIAG,inetdiag)
572 __ADD(NETLINK_NFLOG,nflog)
573 __ADD(NETLINK_XFRM,xfrm)
574 __ADD(NETLINK_SELINUX,selinux)
575 __ADD(NETLINK_ISCSI,iscsi)
576 __ADD(NETLINK_AUDIT,audit)
577 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
578 __ADD(NETLINK_CONNECTOR,connector)
579 __ADD(NETLINK_NETFILTER,netfilter)
580 __ADD(NETLINK_IP6_FW,ip6_fw)
581 __ADD(NETLINK_DNRTMSG,dnrtmsg)
582 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
583 __ADD(NETLINK_GENERIC,generic)
584 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
585 __ADD(NETLINK_ECRYPTFS,ecryptfs)
588 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
590 return __type2str(family, buf, size, nlfamilies,
591 ARRAY_SIZE(nlfamilies));
594 int nl_str2nlfamily(
const char *name)
596 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
608 static const struct trans_tbl llprotos[] = {
610 __ADD(ARPHRD_ETHER,ether)
611 __ADD(ARPHRD_EETHER,eether)
612 __ADD(ARPHRD_AX25,ax25)
613 __ADD(ARPHRD_PRONET,pronet)
614 __ADD(ARPHRD_CHAOS,chaos)
615 __ADD(ARPHRD_IEEE802,ieee802)
616 __ADD(ARPHRD_ARCNET,arcnet)
617 __ADD(ARPHRD_APPLETLK,atalk)
618 __ADD(ARPHRD_DLCI,dlci)
619 __ADD(ARPHRD_ATM,atm)
620 __ADD(ARPHRD_METRICOM,metricom)
621 __ADD(ARPHRD_IEEE1394,ieee1394)
623 __ADD(ARPHRD_EUI64,eui64)
625 __ADD(ARPHRD_INFINIBAND,infiniband)
626 __ADD(ARPHRD_SLIP,slip)
627 __ADD(ARPHRD_CSLIP,cslip)
628 __ADD(ARPHRD_SLIP6,slip6)
629 __ADD(ARPHRD_CSLIP6,cslip6)
630 __ADD(ARPHRD_RSRVD,rsrvd)
631 __ADD(ARPHRD_ADAPT,adapt)
632 __ADD(ARPHRD_ROSE,rose)
633 __ADD(ARPHRD_X25,x25)
635 __ADD(ARPHRD_HWX25,hwx25)
637 __ADD(ARPHRD_CAN,can)
638 __ADD(ARPHRD_PPP,ppp)
639 __ADD(ARPHRD_HDLC,hdlc)
640 __ADD(ARPHRD_LAPB,lapb)
641 __ADD(ARPHRD_DDCMP,ddcmp)
642 __ADD(ARPHRD_RAWHDLC,rawhdlc)
643 __ADD(ARPHRD_TUNNEL,ipip)
644 __ADD(ARPHRD_TUNNEL6,tunnel6)
645 __ADD(ARPHRD_FRAD,frad)
646 __ADD(ARPHRD_SKIP,skip)
647 __ADD(ARPHRD_LOOPBACK,loopback)
648 __ADD(ARPHRD_LOCALTLK,localtlk)
649 __ADD(ARPHRD_FDDI,fddi)
650 __ADD(ARPHRD_BIF,bif)
651 __ADD(ARPHRD_SIT,sit)
652 __ADD(ARPHRD_IPDDP,ip/ddp)
653 __ADD(ARPHRD_IPGRE,gre)
654 __ADD(ARPHRD_PIMREG,pimreg)
655 __ADD(ARPHRD_HIPPI,hippi)
656 __ADD(ARPHRD_ASH,ash)
657 __ADD(ARPHRD_ECONET,econet)
658 __ADD(ARPHRD_IRDA,irda)
659 __ADD(ARPHRD_FCPP,fcpp)
660 __ADD(ARPHRD_FCAL,fcal)
661 __ADD(ARPHRD_FCPL,fcpl)
662 __ADD(ARPHRD_FCFABRIC,fcfb_0)
663 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
664 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
665 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
666 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
667 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
668 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
669 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
670 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
671 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
672 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
673 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
674 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
675 __ADD(ARPHRD_IEEE802_TR,tr)
676 __ADD(ARPHRD_IEEE80211,ieee802.11)
677 __ADD(ARPHRD_PHONET,phonet)
678 __ADD(ARPHRD_CAIF, caif)
679 #ifdef ARPHRD_IEEE80211_PRISM
680 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
683 __ADD(ARPHRD_VOID,
void)
686 __ADD(ARPHRD_NONE,nohdr)
690 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
692 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
695 int nl_str2llproto(
const char *name)
697 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
708 static const struct trans_tbl ether_protos[] = {
709 __ADD(ETH_P_LOOP,loop)
711 __ADD(ETH_P_PUPAT,pupat)
716 __ADD(ETH_P_IEEEPUP,ieeepup)
717 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
719 __ADD(ETH_P_DNA_DL,dna_dl)
720 __ADD(ETH_P_DNA_RC,dna_rc)
721 __ADD(ETH_P_DNA_RT,dna_rt)
723 __ADD(ETH_P_DIAG,diag)
724 __ADD(ETH_P_CUST,cust)
727 __ADD(ETH_P_RARP,rarp)
728 __ADD(ETH_P_ATALK,atalk)
729 __ADD(ETH_P_AARP,aarp)
731 __ADD(ETH_P_8021Q,802.1q)
734 __ADD(ETH_P_IPV6,ipv6)
735 __ADD(ETH_P_PAUSE,pause)
736 __ADD(ETH_P_SLOW,slow)
738 __ADD(ETH_P_WCCP,wccp)
740 __ADD(ETH_P_PPP_DISC,ppp_disc)
741 __ADD(ETH_P_PPP_SES,ppp_ses)
742 __ADD(ETH_P_MPLS_UC,mpls_uc)
743 __ADD(ETH_P_MPLS_MC,mpls_mc)
744 __ADD(ETH_P_ATMMPOA,atmmpoa)
745 __ADD(ETH_P_LINK_CTL,link_ctl)
746 __ADD(ETH_P_ATMFATE,atmfate)
749 __ADD(ETH_P_TIPC,tipc)
750 __ADD(ETH_P_1588,ieee1588)
751 __ADD(ETH_P_FCOE,fcoe)
753 __ADD(ETH_P_EDSA,edsa)
754 __ADD(ETH_P_EDP2,edp2)
755 __ADD(ETH_P_802_3,802.3)
756 __ADD(ETH_P_AX25,ax25)
758 __ADD(ETH_P_802_2,802.2)
759 __ADD(ETH_P_SNAP,snap)
760 __ADD(ETH_P_DDCMP,ddcmp)
761 __ADD(ETH_P_WAN_PPP,wan_ppp)
762 __ADD(ETH_P_PPP_MP,ppp_mp)
763 __ADD(ETH_P_LOCALTALK,localtalk)
765 __ADD(ETH_P_PPPTALK,ppptalk)
766 __ADD(ETH_P_TR_802_2,tr_802.2)
767 __ADD(ETH_P_MOBITEX,mobitex)
768 __ADD(ETH_P_CONTROL,control)
769 __ADD(ETH_P_IRDA,irda)
770 __ADD(ETH_P_ECONET,econet)
771 __ADD(ETH_P_HDLC,hdlc)
772 __ADD(ETH_P_ARCNET,arcnet)
774 __ADD(ETH_P_TRAILER,trailer)
775 __ADD(ETH_P_PHONET,phonet)
776 __ADD(ETH_P_IEEE802154,ieee802154)
777 __ADD(ETH_P_CAIF,caif)
780 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
782 return __type2str(eproto, buf, len, ether_protos,
783 ARRAY_SIZE(ether_protos));
786 int nl_str2ether_proto(
const char *name)
788 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
798 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
800 struct protoent *p = getprotobynumber(proto);
803 snprintf(buf, len,
"%s", p->p_name);
807 snprintf(buf, len,
"0x%x", proto);
811 int nl_str2ip_proto(
const char *name)
813 struct protoent *p = getprotobyname(name);
820 l = strtoul(name, &end, 0);
821 if (l == ULONG_MAX || *end !=
'\0')
822 return -NLE_OBJ_NOTFOUND;
850 for (i = 0; i < params->
dp_prefix; i++) {
852 fprintf(params->
dp_fd,
" ");
854 strncat(params->
dp_buf,
" ",
856 strlen(params->
dp_buf) - 1);
861 params->
dp_nl_cb(params, params->dp_line);
864 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
868 vfprintf(parms->
dp_fd, fmt, args);
871 if (vasprintf(&buf, fmt, args) >= 0) {
873 parms->
dp_cb(parms, buf);
875 strncat(parms->
dp_buf, buf,
877 strlen(parms->
dp_buf) - 1);
898 dump_one(params, fmt, args);
902 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
909 dump_one(parms, fmt, args);
918 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
920 struct trans_list *tl;
922 tl = calloc(1,
sizeof(*tl));
929 nl_list_add_tail(&tl->list, head);
936 struct trans_list *tl, *next;
938 nl_list_for_each_entry_safe(tl, next, head, list) {
943 nl_init_list_head(head);
946 char *__type2str(
int type,
char *buf,
size_t len,
947 const struct trans_tbl *tbl,
size_t tbl_len)
950 for (i = 0; i < tbl_len; i++) {
951 if (tbl[i].i == type) {
952 snprintf(buf, len,
"%s", tbl[i].a);
957 snprintf(buf, len,
"0x%x", type);
961 char *__list_type2str(
int type,
char *buf,
size_t len,
964 struct trans_list *tl;
966 nl_list_for_each_entry(tl, head, list) {
968 snprintf(buf, len,
"%s", tl->a);
973 snprintf(buf, len,
"0x%x", type);
977 char *__flags2str(
int flags,
char *buf,
size_t len,
978 const struct trans_tbl *tbl,
size_t tbl_len)
985 for (i = 0; i < tbl_len; i++) {
986 if (tbl[i].i & tmp) {
988 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
990 strncat(buf,
",", len - strlen(buf) - 1);
997 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1006 for (i = 0; i < tbl_len; i++)
1007 if (!strcasecmp(tbl[i].a, buf))
1010 l = strtoul(buf, &end, 0);
1011 if (l == ULONG_MAX || *end !=
'\0')
1012 return -NLE_OBJ_NOTFOUND;
1017 int __list_str2type(
const char *buf,
struct nl_list_head *head)
1019 struct trans_list *tl;
1026 nl_list_for_each_entry(tl, head, list) {
1027 if (!strcasecmp(tl->a, buf))
1031 l = strtoul(buf, &end, 0);
1032 if (l == ULONG_MAX || *end !=
'\0')
1033 return -NLE_OBJ_NOTFOUND;
1038 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1043 char *p = (
char *) buf, *t;
1050 len = t ? t - p : strlen(p);
1051 for (i = 0; i < tbl_len; i++)
1052 if (len == strlen(tbl[i].a) &&
1053 !strncasecmp(tbl[i].a, p, len))
1069 if (type < 0 || type > NL_DUMP_MAX)
1072 params->dp_line = 0;
1079 dp_dump_line(params, 0,
"%s ",
1080 nl_cache_mngt_type2name(obj->ce_ops,
1081 obj->ce_ops->co_protocol,
1088 if (obj->ce_ops->oo_dump[type])
1089 obj->ce_ops->oo_dump[type](obj, params);