27 #include <netlink-private/netlink.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);
92 if (num == LONG_MIN || num == LONG_MAX) {
97 while (*end ==
' ' || *end ==
'\t')
100 goodlen = strcspn(end,
"#\r\n\t ");
139 if (l >= 1099511627776LL) {
141 return ((
double) l) / 1099511627776LL;
142 }
else if (l >= 1073741824) {
144 return ((
double) l) / 1073741824;
145 }
else if (l >= 1048576) {
147 return ((
double) l) / 1048576;
148 }
else if (l >= 1024) {
150 return ((
double) l) / 1024;
170 if (l >= 1000000000000ULL) {
172 return ((
double) l) / 1000000000000ULL;
175 if (l >= 1000000000) {
177 return ((
double) l) / 1000000000;
182 return ((
double) l) / 1000000;
187 return ((
double) l) / 1000;
194 int nl_rate2str(
unsigned long long rate,
int type,
char *buf,
size_t len)
212 return snprintf(buf, len,
"%.2f%s/s", frac, unit);
229 return ((
double) l) / 1000000;
230 }
else if (l >= 1000) {
232 return ((
double) l) / 1000;
265 long l = strtol(str, &p, 0);
270 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
272 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
274 else if (!strcasecmp(p,
"gbit"))
276 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
278 else if (!strcasecmp(p,
"mbit"))
280 else if (!strcasecmp(p,
"kbit"))
282 else if (!strcasecmp(p,
"bit"))
284 else if (strcasecmp(p,
"b") != 0)
291 static const struct {
295 { 1024. * 1024. * 1024. * 1024. * 1024.,
"EiB" },
296 { 1024. * 1024. * 1024. * 1024.,
"TiB" },
297 { 1024. * 1024. * 1024.,
"GiB" },
298 { 1024. * 1024.,
"MiB" },
329 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
330 if (size >= size_units[i].limit) {
331 snprintf(buf, len,
"%.2g%s",
332 (
double) size / size_units[i].limit,
357 double d = strtod(str, &p);
365 if (d > 1.0f || d < 0.0f)
368 if (*p && strcmp(p,
"%") != 0)
385 static uint32_t user_hz = USER_HZ;
386 static uint32_t psched_hz = USER_HZ;
388 static double ticks_per_usec = 1.0f;
399 static void __init get_psched_settings(
void)
401 char name[FILENAME_MAX];
406 long hz = strtol(getenv(
"HZ"), NULL, 0);
408 if (LONG_MIN != hz && LONG_MAX != hz) {
415 user_hz = sysconf(_SC_CLK_TCK);
419 if (getenv(
"TICKS_PER_USEC")) {
420 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
424 if (getenv(
"PROC_NET_PSCHED"))
425 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
426 else if (getenv(
"PROC_ROOT"))
427 snprintf(name,
sizeof(name),
"%s/net/psched",
428 getenv(
"PROC_ROOT"));
430 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
432 if ((fd = fopen(name,
"r"))) {
433 unsigned int ns_per_usec, ns_per_tick, nom, denom;
435 if (fscanf(fd,
"%08x %08x %08x %08x",
436 &ns_per_usec, &ns_per_tick, &nom, &denom) != 4) {
437 NL_DBG(1,
"Fatal error: can not read psched settings from \"%s\". " \
438 "Try to set TICKS_PER_USEC, PROC_NET_PSCHED or PROC_ROOT " \
439 "environment variables\n", name);
443 ticks_per_usec = (double) ns_per_usec /
444 (
double) ns_per_tick;
478 return us * ticks_per_usec;
489 return ticks / ticks_per_usec;
492 int nl_str2msec(
const char *str, uint64_t *result)
494 uint64_t total = 0, l;
499 l = strtoul(str, &p, 0);
503 plen = strcspn(p,
" \t");
507 else if (!strncasecmp(p,
"sec", plen))
509 else if (!strncasecmp(p,
"min", plen))
510 total += (l * 1000*60);
511 else if (!strncasecmp(p,
"hour", plen))
512 total += (l * 1000*60*60);
513 else if (!strncasecmp(p,
"day", plen))
514 total += (l * 1000*60*60*24);
521 }
while (*str && *p);
544 static const char *units[5] = {
"d",
"h",
"m",
"s",
"msec"};
545 char *
const buf_orig = buf;
547 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
555 for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
559 l = snprintf(buf, len,
"%s%" PRIu64
"%s",
560 (buf==buf_orig) ?
"" :
" ", split[i], units[i]);
575 static const struct trans_tbl nlfamilies[] = {
576 __ADD(NETLINK_ROUTE,route)
577 __ADD(NETLINK_USERSOCK,usersock)
578 __ADD(NETLINK_FIREWALL,firewall)
579 __ADD(NETLINK_INET_DIAG,inetdiag)
580 __ADD(NETLINK_NFLOG,nflog)
581 __ADD(NETLINK_XFRM,xfrm)
582 __ADD(NETLINK_SELINUX,selinux)
583 __ADD(NETLINK_ISCSI,iscsi)
584 __ADD(NETLINK_AUDIT,audit)
585 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
586 __ADD(NETLINK_CONNECTOR,connector)
587 __ADD(NETLINK_NETFILTER,netfilter)
588 __ADD(NETLINK_IP6_FW,ip6_fw)
589 __ADD(NETLINK_DNRTMSG,dnrtmsg)
590 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
591 __ADD(NETLINK_GENERIC,generic)
592 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
593 __ADD(NETLINK_ECRYPTFS,ecryptfs)
596 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
598 return __type2str(family, buf, size, nlfamilies,
599 ARRAY_SIZE(nlfamilies));
602 int nl_str2nlfamily(
const char *name)
604 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
616 static const struct trans_tbl llprotos[] = {
618 __ADD(ARPHRD_ETHER,ether)
619 __ADD(ARPHRD_EETHER,eether)
620 __ADD(ARPHRD_AX25,ax25)
621 __ADD(ARPHRD_PRONET,pronet)
622 __ADD(ARPHRD_CHAOS,chaos)
623 __ADD(ARPHRD_IEEE802,ieee802)
624 __ADD(ARPHRD_ARCNET,arcnet)
625 __ADD(ARPHRD_APPLETLK,atalk)
626 __ADD(ARPHRD_DLCI,dlci)
627 __ADD(ARPHRD_ATM,atm)
628 __ADD(ARPHRD_METRICOM,metricom)
629 __ADD(ARPHRD_IEEE1394,ieee1394)
631 __ADD(ARPHRD_EUI64,eui64)
633 __ADD(ARPHRD_INFINIBAND,infiniband)
634 __ADD(ARPHRD_SLIP,slip)
635 __ADD(ARPHRD_CSLIP,cslip)
636 __ADD(ARPHRD_SLIP6,slip6)
637 __ADD(ARPHRD_CSLIP6,cslip6)
638 __ADD(ARPHRD_RSRVD,rsrvd)
639 __ADD(ARPHRD_ADAPT,adapt)
640 __ADD(ARPHRD_ROSE,rose)
641 __ADD(ARPHRD_X25,x25)
643 __ADD(ARPHRD_HWX25,hwx25)
645 __ADD(ARPHRD_CAN,can)
646 __ADD(ARPHRD_PPP,ppp)
647 __ADD(ARPHRD_HDLC,hdlc)
648 __ADD(ARPHRD_LAPB,lapb)
649 __ADD(ARPHRD_DDCMP,ddcmp)
650 __ADD(ARPHRD_RAWHDLC,rawhdlc)
651 __ADD(ARPHRD_TUNNEL,ipip)
652 __ADD(ARPHRD_TUNNEL6,tunnel6)
653 __ADD(ARPHRD_FRAD,frad)
654 __ADD(ARPHRD_SKIP,skip)
655 __ADD(ARPHRD_LOOPBACK,loopback)
656 __ADD(ARPHRD_LOCALTLK,localtlk)
657 __ADD(ARPHRD_FDDI,fddi)
658 __ADD(ARPHRD_BIF,bif)
659 __ADD(ARPHRD_SIT,sit)
660 __ADD(ARPHRD_IPDDP,ip/ddp)
661 __ADD(ARPHRD_IPGRE,gre)
662 __ADD(ARPHRD_PIMREG,pimreg)
663 __ADD(ARPHRD_HIPPI,hippi)
664 __ADD(ARPHRD_ASH,ash)
665 __ADD(ARPHRD_ECONET,econet)
666 __ADD(ARPHRD_IRDA,irda)
667 __ADD(ARPHRD_FCPP,fcpp)
668 __ADD(ARPHRD_FCAL,fcal)
669 __ADD(ARPHRD_FCPL,fcpl)
670 __ADD(ARPHRD_FCFABRIC,fcfb_0)
671 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
672 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
673 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
674 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
675 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
676 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
677 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
678 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
679 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
680 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
681 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
682 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
683 __ADD(ARPHRD_IEEE802_TR,tr)
684 __ADD(ARPHRD_IEEE80211,ieee802.11)
685 __ADD(ARPHRD_PHONET,phonet)
686 __ADD(ARPHRD_CAIF, caif)
687 #ifdef ARPHRD_IEEE80211_PRISM
688 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
691 __ADD(ARPHRD_VOID,
void)
694 __ADD(ARPHRD_NONE,nohdr)
698 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
700 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
703 int nl_str2llproto(
const char *name)
705 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
716 static const struct trans_tbl ether_protos[] = {
717 __ADD(ETH_P_LOOP,loop)
719 __ADD(ETH_P_PUPAT,pupat)
724 __ADD(ETH_P_IEEEPUP,ieeepup)
725 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
727 __ADD(ETH_P_DNA_DL,dna_dl)
728 __ADD(ETH_P_DNA_RC,dna_rc)
729 __ADD(ETH_P_DNA_RT,dna_rt)
731 __ADD(ETH_P_DIAG,diag)
732 __ADD(ETH_P_CUST,cust)
735 __ADD(ETH_P_RARP,rarp)
736 __ADD(ETH_P_ATALK,atalk)
737 __ADD(ETH_P_AARP,aarp)
739 __ADD(ETH_P_8021Q,802.1q)
742 __ADD(ETH_P_IPV6,ipv6)
743 __ADD(ETH_P_PAUSE,pause)
744 __ADD(ETH_P_SLOW,slow)
746 __ADD(ETH_P_WCCP,wccp)
748 __ADD(ETH_P_PPP_DISC,ppp_disc)
749 __ADD(ETH_P_PPP_SES,ppp_ses)
750 __ADD(ETH_P_MPLS_UC,mpls_uc)
751 __ADD(ETH_P_MPLS_MC,mpls_mc)
752 __ADD(ETH_P_ATMMPOA,atmmpoa)
753 __ADD(ETH_P_LINK_CTL,link_ctl)
754 __ADD(ETH_P_ATMFATE,atmfate)
757 __ADD(ETH_P_TIPC,tipc)
758 __ADD(ETH_P_1588,ieee1588)
759 __ADD(ETH_P_FCOE,fcoe)
761 __ADD(ETH_P_EDSA,edsa)
762 __ADD(ETH_P_EDP2,edp2)
763 __ADD(ETH_P_802_3,802.3)
764 __ADD(ETH_P_AX25,ax25)
766 __ADD(ETH_P_802_2,802.2)
767 __ADD(ETH_P_SNAP,snap)
768 __ADD(ETH_P_DDCMP,ddcmp)
769 __ADD(ETH_P_WAN_PPP,wan_ppp)
770 __ADD(ETH_P_PPP_MP,ppp_mp)
771 __ADD(ETH_P_LOCALTALK,localtalk)
773 __ADD(ETH_P_PPPTALK,ppptalk)
774 __ADD(ETH_P_TR_802_2,tr_802.2)
775 __ADD(ETH_P_MOBITEX,mobitex)
776 __ADD(ETH_P_CONTROL,control)
777 __ADD(ETH_P_IRDA,irda)
778 __ADD(ETH_P_ECONET,econet)
779 __ADD(ETH_P_HDLC,hdlc)
780 __ADD(ETH_P_ARCNET,arcnet)
782 __ADD(ETH_P_TRAILER,trailer)
783 __ADD(ETH_P_PHONET,phonet)
784 __ADD(ETH_P_IEEE802154,ieee802154)
785 __ADD(ETH_P_CAIF,caif)
788 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
790 return __type2str(eproto, buf, len, ether_protos,
791 ARRAY_SIZE(ether_protos));
794 int nl_str2ether_proto(
const char *name)
796 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
806 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
808 struct protoent *p = getprotobynumber(proto);
811 snprintf(buf, len,
"%s", p->p_name);
815 snprintf(buf, len,
"0x%x", proto);
819 int nl_str2ip_proto(
const char *name)
821 struct protoent *p = getprotobyname(name);
828 l = strtoul(name, &end, 0);
829 if (l == ULONG_MAX || *end !=
'\0')
830 return -NLE_OBJ_NOTFOUND;
858 for (i = 0; i < params->
dp_prefix; i++) {
860 fprintf(params->
dp_fd,
" ");
862 strncat(params->
dp_buf,
" ",
864 strlen(params->
dp_buf) - 1);
869 params->
dp_nl_cb(params, params->dp_line);
872 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
876 vfprintf(parms->
dp_fd, fmt, args);
879 if (vasprintf(&buf, fmt, args) >= 0) {
881 parms->
dp_cb(parms, buf);
883 strncat(parms->
dp_buf, buf,
885 strlen(parms->
dp_buf) - 1);
906 dump_one(params, fmt, args);
910 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
917 dump_one(parms, fmt, args);
926 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
928 struct trans_list *tl;
930 tl = calloc(1,
sizeof(*tl));
937 nl_list_add_tail(&tl->list, head);
944 struct trans_list *tl, *next;
946 nl_list_for_each_entry_safe(tl, next, head, list) {
951 nl_init_list_head(head);
954 char *__type2str(
int type,
char *buf,
size_t len,
955 const struct trans_tbl *tbl,
size_t tbl_len)
958 for (i = 0; i < tbl_len; i++) {
959 if (tbl[i].i == type) {
960 snprintf(buf, len,
"%s", tbl[i].a);
965 snprintf(buf, len,
"0x%x", type);
969 char *__list_type2str(
int type,
char *buf,
size_t len,
972 struct trans_list *tl;
974 nl_list_for_each_entry(tl, head, list) {
976 snprintf(buf, len,
"%s", tl->a);
981 snprintf(buf, len,
"0x%x", type);
985 char *__flags2str(
int flags,
char *buf,
size_t len,
986 const struct trans_tbl *tbl,
size_t tbl_len)
993 for (i = 0; i < tbl_len; i++) {
994 if (tbl[i].i & tmp) {
996 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
998 strncat(buf,
",", len - strlen(buf) - 1);
1005 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1014 for (i = 0; i < tbl_len; i++)
1015 if (!strcasecmp(tbl[i].a, buf))
1018 l = strtoul(buf, &end, 0);
1019 if (l == ULONG_MAX || *end !=
'\0')
1020 return -NLE_OBJ_NOTFOUND;
1025 int __list_str2type(
const char *buf,
struct nl_list_head *head)
1027 struct trans_list *tl;
1034 nl_list_for_each_entry(tl, head, list) {
1035 if (!strcasecmp(tl->a, buf))
1039 l = strtoul(buf, &end, 0);
1040 if (l == ULONG_MAX || *end !=
'\0')
1041 return -NLE_OBJ_NOTFOUND;
1046 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1051 char *p = (
char *) buf, *t;
1058 len = t ? t - p : strlen(p);
1059 for (i = 0; i < tbl_len; i++)
1060 if (len == strlen(tbl[i].a) &&
1061 !strncasecmp(tbl[i].a, p, len))
1073 void dump_from_ops(
struct nl_object *obj,
struct nl_dump_params *params)
1077 if (type < 0 || type > NL_DUMP_MAX)
1080 params->dp_line = 0;
1087 dp_dump_line(params, 0,
"%s ",
1088 nl_cache_mngt_type2name(obj->ce_ops,
1089 obj->ce_ops->co_protocol,
1096 if (obj->ce_ops->oo_dump[type])
1097 obj->ce_ops->oo_dump[type](obj, params);