27 #include <netlink-local.h>
28 #include <netlink/netlink.h>
29 #include <netlink/utils.h>
30 #include <linux/socket.h>
55 static void __init nl_debug_init(
void)
59 if ((nldbg = getenv(
"NLDBG"))) {
60 long level = strtol(nldbg, &end, 0);
65 nl_debug_dp.
dp_fd = stderr;
68 int __nl_read_num_str_file(
const char *path,
int (*cb)(
long,
const char *))
73 fd = fopen(path,
"r");
75 return -nl_syserr2nlerr(errno);
77 while (fgets(buf,
sizeof(buf), fd)) {
82 if (*buf ==
'#' || *buf ==
'\n' || *buf ==
'\r')
85 num = strtol(buf, &end, 0);
89 if (num == LONG_MIN || num == LONG_MAX)
92 while (*end ==
' ' || *end ==
'\t')
95 goodlen = strcspn(end,
"#\r\n\t ");
129 if (l >= 1099511627776LL) {
131 return ((
double) l) / 1099511627776LL;
132 }
else if (l >= 1073741824) {
134 return ((
double) l) / 1073741824;
135 }
else if (l >= 1048576) {
137 return ((
double) l) / 1048576;
138 }
else if (l >= 1024) {
140 return ((
double) l) / 1024;
159 if (l >= 1099511627776ULL) {
161 return ((
double) l) / 1099511627776ULL;
162 }
else if (l >= 1073741824) {
164 return ((
double) l) / 1073741824;
165 }
else if (l >= 1048576) {
167 return ((
double) l) / 1048576;
168 }
else if (l >= 1024) {
170 return ((
double) l) / 1024;
178 int nl_rate2str(
unsigned long long rate,
int type,
char *buf,
size_t len)
196 return snprintf(buf, len,
"%.2f%s/s", frac, unit);
213 return ((
double) l) / 1000000;
214 }
else if (l >= 1000) {
216 return ((
double) l) / 1000;
246 long l = strtol(str, &p, 0);
251 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
253 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
255 else if (!strcasecmp(p,
"gbit"))
256 l *= 1024*1024*1024/8;
257 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
259 else if (!strcasecmp(p,
"mbit"))
261 else if (!strcasecmp(p,
"kbit"))
263 else if (!strcasecmp(p,
"bit"))
265 else if (strcasecmp(p,
"b") != 0)
272 static const struct {
276 { 1024. * 1024. * 1024. * 1024. * 1024.,
"EiB" },
277 { 1024. * 1024. * 1024. * 1024.,
"TiB" },
278 { 1024. * 1024. * 1024.,
"GiB" },
279 { 1024. * 1024.,
"MiB" },
310 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
311 if (size >= size_units[i].limit) {
312 snprintf(buf, len,
"%.2g%s",
313 (
double) size / size_units[i].limit,
338 double d = strtod(str, &p);
346 if (d > 1.0f || d < 0.0f)
349 if (*p && strcmp(p,
"%") != 0)
366 static uint32_t user_hz = USER_HZ;
367 static uint32_t psched_hz = USER_HZ;
369 static double ticks_per_usec = 1.0f;
380 static void __init get_psched_settings(
void)
382 char name[FILENAME_MAX];
387 long hz = strtol(getenv(
"HZ"), NULL, 0);
389 if (LONG_MIN != hz && LONG_MAX != hz) {
396 user_hz = sysconf(_SC_CLK_TCK);
400 if (getenv(
"TICKS_PER_USEC")) {
401 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
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"));
411 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
413 if ((fd = fopen(name,
"r"))) {
414 uint32_t ns_per_usec, ns_per_tick, nom, denom;
416 fscanf(fd,
"%08x %08x %08x %08x",
417 &ns_per_usec, &ns_per_tick, &nom, &denom);
419 ticks_per_usec = (double) ns_per_usec /
420 (
double) ns_per_tick;
454 return us * ticks_per_usec;
465 return ticks / ticks_per_usec;
468 int nl_str2msec(
const char *str, uint64_t *result)
470 uint64_t total = 0, l;
475 l = strtoul(str, &p, 0);
479 plen = strcspn(p,
" \t");
483 else if (!strncasecmp(p,
"sec", plen))
485 else if (!strncasecmp(p,
"min", plen))
486 total += (l * 1000*60);
487 else if (!strncasecmp(p,
"hour", plen))
488 total += (l * 1000*60*60);
489 else if (!strncasecmp(p,
"day", plen))
490 total += (l * 1000*60*60*24);
497 }
while (*str && *p);
520 static const char *units[5] = {
"d",
"h",
"m",
"s",
"msec"};
521 char *
const buf_orig = buf;
523 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
531 for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
535 l = snprintf(buf, len,
"%s%" PRIu64
"%s",
536 (buf==buf_orig) ?
"" :
" ", split[i], units[i]);
551 static const struct trans_tbl nlfamilies[] = {
552 __ADD(NETLINK_ROUTE,route)
553 __ADD(NETLINK_USERSOCK,usersock)
554 __ADD(NETLINK_FIREWALL,firewall)
555 __ADD(NETLINK_INET_DIAG,inetdiag)
556 __ADD(NETLINK_NFLOG,nflog)
557 __ADD(NETLINK_XFRM,xfrm)
558 __ADD(NETLINK_SELINUX,selinux)
559 __ADD(NETLINK_ISCSI,iscsi)
560 __ADD(NETLINK_AUDIT,audit)
561 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
562 __ADD(NETLINK_CONNECTOR,connector)
563 __ADD(NETLINK_NETFILTER,netfilter)
564 __ADD(NETLINK_IP6_FW,ip6_fw)
565 __ADD(NETLINK_DNRTMSG,dnrtmsg)
566 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
567 __ADD(NETLINK_GENERIC,generic)
568 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
569 __ADD(NETLINK_ECRYPTFS,ecryptfs)
572 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
574 return __type2str(family, buf, size, nlfamilies,
575 ARRAY_SIZE(nlfamilies));
578 int nl_str2nlfamily(
const char *name)
580 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
592 static const struct trans_tbl llprotos[] = {
594 __ADD(ARPHRD_ETHER,ether)
595 __ADD(ARPHRD_EETHER,eether)
596 __ADD(ARPHRD_AX25,ax25)
597 __ADD(ARPHRD_PRONET,pronet)
598 __ADD(ARPHRD_CHAOS,chaos)
599 __ADD(ARPHRD_IEEE802,ieee802)
600 __ADD(ARPHRD_ARCNET,arcnet)
601 __ADD(ARPHRD_APPLETLK,atalk)
602 __ADD(ARPHRD_DLCI,dlci)
603 __ADD(ARPHRD_ATM,atm)
604 __ADD(ARPHRD_METRICOM,metricom)
605 __ADD(ARPHRD_IEEE1394,ieee1394)
607 __ADD(ARPHRD_EUI64,eui64)
609 __ADD(ARPHRD_INFINIBAND,infiniband)
610 __ADD(ARPHRD_SLIP,slip)
611 __ADD(ARPHRD_CSLIP,cslip)
612 __ADD(ARPHRD_SLIP6,slip6)
613 __ADD(ARPHRD_CSLIP6,cslip6)
614 __ADD(ARPHRD_RSRVD,rsrvd)
615 __ADD(ARPHRD_ADAPT,adapt)
616 __ADD(ARPHRD_ROSE,rose)
617 __ADD(ARPHRD_X25,x25)
619 __ADD(ARPHRD_HWX25,hwx25)
621 __ADD(ARPHRD_CAN,can)
622 __ADD(ARPHRD_PPP,ppp)
623 __ADD(ARPHRD_HDLC,hdlc)
624 __ADD(ARPHRD_LAPB,lapb)
625 __ADD(ARPHRD_DDCMP,ddcmp)
626 __ADD(ARPHRD_RAWHDLC,rawhdlc)
627 __ADD(ARPHRD_TUNNEL,ipip)
628 __ADD(ARPHRD_TUNNEL6,tunnel6)
629 __ADD(ARPHRD_FRAD,frad)
630 __ADD(ARPHRD_SKIP,skip)
631 __ADD(ARPHRD_LOOPBACK,loopback)
632 __ADD(ARPHRD_LOCALTLK,localtlk)
633 __ADD(ARPHRD_FDDI,fddi)
634 __ADD(ARPHRD_BIF,bif)
635 __ADD(ARPHRD_SIT,sit)
636 __ADD(ARPHRD_IPDDP,ip/ddp)
637 __ADD(ARPHRD_IPGRE,gre)
638 __ADD(ARPHRD_PIMREG,pimreg)
639 __ADD(ARPHRD_HIPPI,hippi)
640 __ADD(ARPHRD_ASH,ash)
641 __ADD(ARPHRD_ECONET,econet)
642 __ADD(ARPHRD_IRDA,irda)
643 __ADD(ARPHRD_FCPP,fcpp)
644 __ADD(ARPHRD_FCAL,fcal)
645 __ADD(ARPHRD_FCPL,fcpl)
646 __ADD(ARPHRD_FCFABRIC,fcfb_0)
647 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
648 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
649 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
650 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
651 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
652 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
653 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
654 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
655 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
656 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
657 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
658 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
659 __ADD(ARPHRD_IEEE802_TR,tr)
660 __ADD(ARPHRD_IEEE80211,ieee802.11)
661 __ADD(ARPHRD_PHONET,phonet)
662 __ADD(ARPHRD_CAIF, caif)
663 #ifdef ARPHRD_IEEE80211_PRISM
664 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
667 __ADD(ARPHRD_VOID,
void)
670 __ADD(ARPHRD_NONE,nohdr)
674 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
676 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
679 int nl_str2llproto(
const char *name)
681 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
692 static const struct trans_tbl ether_protos[] = {
693 __ADD(ETH_P_LOOP,loop)
695 __ADD(ETH_P_PUPAT,pupat)
700 __ADD(ETH_P_IEEEPUP,ieeepup)
701 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
703 __ADD(ETH_P_DNA_DL,dna_dl)
704 __ADD(ETH_P_DNA_RC,dna_rc)
705 __ADD(ETH_P_DNA_RT,dna_rt)
707 __ADD(ETH_P_DIAG,diag)
708 __ADD(ETH_P_CUST,cust)
711 __ADD(ETH_P_RARP,rarp)
712 __ADD(ETH_P_ATALK,atalk)
713 __ADD(ETH_P_AARP,aarp)
715 __ADD(ETH_P_8021Q,802.1q)
718 __ADD(ETH_P_IPV6,ipv6)
719 __ADD(ETH_P_PAUSE,pause)
720 __ADD(ETH_P_SLOW,slow)
722 __ADD(ETH_P_WCCP,wccp)
724 __ADD(ETH_P_PPP_DISC,ppp_disc)
725 __ADD(ETH_P_PPP_SES,ppp_ses)
726 __ADD(ETH_P_MPLS_UC,mpls_uc)
727 __ADD(ETH_P_MPLS_MC,mpls_mc)
728 __ADD(ETH_P_ATMMPOA,atmmpoa)
729 __ADD(ETH_P_LINK_CTL,link_ctl)
730 __ADD(ETH_P_ATMFATE,atmfate)
733 __ADD(ETH_P_TIPC,tipc)
734 __ADD(ETH_P_1588,ieee1588)
735 __ADD(ETH_P_FCOE,fcoe)
737 __ADD(ETH_P_EDSA,edsa)
738 __ADD(ETH_P_EDP2,edp2)
739 __ADD(ETH_P_802_3,802.3)
740 __ADD(ETH_P_AX25,ax25)
742 __ADD(ETH_P_802_2,802.2)
743 __ADD(ETH_P_SNAP,snap)
744 __ADD(ETH_P_DDCMP,ddcmp)
745 __ADD(ETH_P_WAN_PPP,wan_ppp)
746 __ADD(ETH_P_PPP_MP,ppp_mp)
747 __ADD(ETH_P_LOCALTALK,localtalk)
749 __ADD(ETH_P_PPPTALK,ppptalk)
750 __ADD(ETH_P_TR_802_2,tr_802.2)
751 __ADD(ETH_P_MOBITEX,mobitex)
752 __ADD(ETH_P_CONTROL,control)
753 __ADD(ETH_P_IRDA,irda)
754 __ADD(ETH_P_ECONET,econet)
755 __ADD(ETH_P_HDLC,hdlc)
756 __ADD(ETH_P_ARCNET,arcnet)
758 __ADD(ETH_P_TRAILER,trailer)
759 __ADD(ETH_P_PHONET,phonet)
760 __ADD(ETH_P_IEEE802154,ieee802154)
761 __ADD(ETH_P_CAIF,caif)
764 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
766 return __type2str(eproto, buf, len, ether_protos,
767 ARRAY_SIZE(ether_protos));
770 int nl_str2ether_proto(
const char *name)
772 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
782 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
784 struct protoent *p = getprotobynumber(proto);
787 snprintf(buf, len,
"%s", p->p_name);
791 snprintf(buf, len,
"0x%x", proto);
795 int nl_str2ip_proto(
const char *name)
797 struct protoent *p = getprotobyname(name);
804 l = strtoul(name, &end, 0);
805 if (l == ULONG_MAX || *end !=
'\0')
806 return -NLE_OBJ_NOTFOUND;
834 for (i = 0; i < params->
dp_prefix; i++) {
836 fprintf(params->
dp_fd,
" ");
838 strncat(params->
dp_buf,
" ",
840 strlen(params->
dp_buf) - 1);
845 params->
dp_nl_cb(params, params->dp_line);
848 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
852 vfprintf(parms->
dp_fd, fmt, args);
855 if (vasprintf(&buf, fmt, args) >= 0) {
857 parms->
dp_cb(parms, buf);
859 strncat(parms->
dp_buf, buf,
861 strlen(parms->
dp_buf) - 1);
882 dump_one(params, fmt, args);
886 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
893 dump_one(parms, fmt, args);
902 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
904 struct trans_list *tl;
906 tl = calloc(1,
sizeof(*tl));
913 nl_list_add_tail(&tl->list, head);
920 struct trans_list *tl, *next;
922 nl_list_for_each_entry_safe(tl, next, head, list) {
927 nl_init_list_head(head);
930 char *__type2str(
int type,
char *buf,
size_t len,
931 const struct trans_tbl *tbl,
size_t tbl_len)
934 for (i = 0; i < tbl_len; i++) {
935 if (tbl[i].i == type) {
936 snprintf(buf, len,
"%s", tbl[i].a);
941 snprintf(buf, len,
"0x%x", type);
945 char *__list_type2str(
int type,
char *buf,
size_t len,
948 struct trans_list *tl;
950 nl_list_for_each_entry(tl, head, list) {
952 snprintf(buf, len,
"%s", tl->a);
957 snprintf(buf, len,
"0x%x", type);
961 char *__flags2str(
int flags,
char *buf,
size_t len,
962 const struct trans_tbl *tbl,
size_t tbl_len)
969 for (i = 0; i < tbl_len; i++) {
970 if (tbl[i].i & tmp) {
972 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
974 strncat(buf,
",", len - strlen(buf) - 1);
981 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
990 for (i = 0; i < tbl_len; i++)
991 if (!strcasecmp(tbl[i].a, buf))
994 l = strtoul(buf, &end, 0);
995 if (l == ULONG_MAX || *end !=
'\0')
996 return -NLE_OBJ_NOTFOUND;
1001 int __list_str2type(
const char *buf,
struct nl_list_head *head)
1003 struct trans_list *tl;
1010 nl_list_for_each_entry(tl, head, list) {
1011 if (!strcasecmp(tl->a, buf))
1015 l = strtoul(buf, &end, 0);
1016 if (l == ULONG_MAX || *end !=
'\0')
1017 return -NLE_OBJ_NOTFOUND;
1022 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1027 char *p = (
char *) buf, *t;
1034 len = t ? t - p : strlen(p);
1035 for (i = 0; i < tbl_len; i++)
1036 if (len == strlen(tbl[i].a) &&
1037 !strncasecmp(tbl[i].a, p, len))
1053 if (type < 0 || type > NL_DUMP_MAX)
1056 params->dp_line = 0;
1063 dp_dump_line(params, 0,
"%s ",
1064 nl_cache_mngt_type2name(obj->ce_ops,
1065 obj->ce_ops->co_protocol,
1072 if (obj->ce_ops->oo_dump[type])
1073 obj->ce_ops->oo_dump[type](obj, params);