18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/utils.h>
21 #include <linux/socket.h>
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 ");
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;
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;
154 int nl_rate2str(
unsigned long long rate,
int type,
char *buf,
size_t len)
172 return snprintf(buf, len,
"%.2f%s/s", frac, unit);
189 return ((
double) l) / 1000000;
190 }
else if (l >= 1000) {
192 return ((
double) l) / 1000;
222 long l = strtol(str, &p, 0);
227 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
229 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
231 else if (!strcasecmp(p,
"gbit"))
232 l *= 1024*1024*1024/8;
233 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
235 else if (!strcasecmp(p,
"mbit"))
237 else if (!strcasecmp(p,
"kbit"))
239 else if (!strcasecmp(p,
"bit"))
241 else if (strcasecmp(p,
"b") != 0)
248 static const struct {
252 { 1024. * 1024. * 1024. * 1024. * 1024.,
"EiB" },
253 { 1024. * 1024. * 1024. * 1024.,
"TiB" },
254 { 1024. * 1024. * 1024.,
"GiB" },
255 { 1024. * 1024.,
"MiB" },
286 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
287 if (size >= size_units[i].limit) {
288 snprintf(buf, len,
"%.2g%s",
289 (
double) size / size_units[i].limit,
314 double d = strtod(str, &p);
322 if (d > 1.0f || d < 0.0f)
325 if (*p && strcmp(p,
"%") != 0)
339 static uint32_t user_hz = USER_HZ;
341 static uint32_t user_hz = 100;
344 static double ticks_per_usec = 1.0f;
355 static void __init get_psched_settings(
void)
357 char name[FILENAME_MAX];
362 long hz = strtol(getenv(
"HZ"), NULL, 0);
364 if (LONG_MIN != hz && LONG_MAX != hz) {
371 user_hz = sysconf(_SC_CLK_TCK);
373 if (getenv(
"TICKS_PER_USEC")) {
374 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
378 if (getenv(
"PROC_NET_PSCHED"))
379 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
380 else if (getenv(
"PROC_ROOT"))
381 snprintf(name,
sizeof(name),
"%s/net/psched",
382 getenv(
"PROC_ROOT"));
384 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
386 if ((fd = fopen(name,
"r"))) {
387 uint32_t ns_per_usec, ns_per_tick;
390 fscanf(fd,
"%08x %08x", &ns_per_usec, &ns_per_tick);
392 ticks_per_usec = (double) ns_per_usec /
393 (
double) ns_per_tick;
418 return us * ticks_per_usec;
429 return ticks / ticks_per_usec;
432 int nl_str2msec(
const char *str, uint64_t *result)
434 uint64_t total = 0, l;
439 l = strtoul(str, &p, 0);
443 plen = strcspn(p,
" \t");
447 else if (!strncasecmp(p,
"sec", plen))
449 else if (!strncasecmp(p,
"min", plen))
450 total += (l * 1000*60);
451 else if (!strncasecmp(p,
"hour", plen))
452 total += (l * 1000*60*60);
453 else if (!strncasecmp(p,
"day", plen))
454 total += (l * 1000*60*60*24);
461 }
while (*str && *p);
483 char *units[] = {
"d",
"h",
"m",
"s",
"msec"};
485 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
495 for (i = 0; i < ARRAY_SIZE(split); i++) {
498 snprintf(t,
sizeof(t),
"%s%d%s",
499 strlen(buf) ?
" " :
"", split[i], units[i]);
500 strncat(buf, t, len - strlen(buf) - 1);
514 static const struct trans_tbl nlfamilies[] = {
515 __ADD(NETLINK_ROUTE,route)
516 __ADD(NETLINK_USERSOCK,usersock)
517 __ADD(NETLINK_FIREWALL,firewall)
518 __ADD(NETLINK_INET_DIAG,inetdiag)
519 __ADD(NETLINK_NFLOG,nflog)
520 __ADD(NETLINK_XFRM,xfrm)
521 __ADD(NETLINK_SELINUX,selinux)
522 __ADD(NETLINK_ISCSI,iscsi)
523 __ADD(NETLINK_AUDIT,audit)
524 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
525 __ADD(NETLINK_CONNECTOR,connector)
526 __ADD(NETLINK_NETFILTER,netfilter)
527 __ADD(NETLINK_IP6_FW,ip6_fw)
528 __ADD(NETLINK_DNRTMSG,dnrtmsg)
529 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
530 __ADD(NETLINK_GENERIC,generic)
531 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
532 __ADD(NETLINK_ECRYPTFS,ecryptfs)
535 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
537 return __type2str(family, buf, size, nlfamilies,
538 ARRAY_SIZE(nlfamilies));
541 int nl_str2nlfamily(
const char *name)
543 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
555 static const struct trans_tbl llprotos[] = {
557 __ADD(ARPHRD_ETHER,ether)
558 __ADD(ARPHRD_EETHER,eether)
559 __ADD(ARPHRD_AX25,ax25)
560 __ADD(ARPHRD_PRONET,pronet)
561 __ADD(ARPHRD_CHAOS,chaos)
562 __ADD(ARPHRD_IEEE802,ieee802)
563 __ADD(ARPHRD_ARCNET,arcnet)
564 __ADD(ARPHRD_APPLETLK,atalk)
565 __ADD(ARPHRD_DLCI,dlci)
566 __ADD(ARPHRD_ATM,atm)
567 __ADD(ARPHRD_METRICOM,metricom)
568 __ADD(ARPHRD_IEEE1394,ieee1394)
570 __ADD(ARPHRD_EUI64,eui64)
572 __ADD(ARPHRD_INFINIBAND,infiniband)
573 __ADD(ARPHRD_SLIP,slip)
574 __ADD(ARPHRD_CSLIP,cslip)
575 __ADD(ARPHRD_SLIP6,slip6)
576 __ADD(ARPHRD_CSLIP6,cslip6)
577 __ADD(ARPHRD_RSRVD,rsrvd)
578 __ADD(ARPHRD_ADAPT,adapt)
579 __ADD(ARPHRD_ROSE,rose)
580 __ADD(ARPHRD_X25,x25)
582 __ADD(ARPHRD_HWX25,hwx25)
584 __ADD(ARPHRD_CAN,can)
585 __ADD(ARPHRD_PPP,ppp)
586 __ADD(ARPHRD_HDLC,hdlc)
587 __ADD(ARPHRD_LAPB,lapb)
588 __ADD(ARPHRD_DDCMP,ddcmp)
589 __ADD(ARPHRD_RAWHDLC,rawhdlc)
590 __ADD(ARPHRD_TUNNEL,ipip)
591 __ADD(ARPHRD_TUNNEL6,tunnel6)
592 __ADD(ARPHRD_FRAD,frad)
593 __ADD(ARPHRD_SKIP,skip)
594 __ADD(ARPHRD_LOOPBACK,loopback)
595 __ADD(ARPHRD_LOCALTLK,localtlk)
596 __ADD(ARPHRD_FDDI,fddi)
597 __ADD(ARPHRD_BIF,bif)
598 __ADD(ARPHRD_SIT,sit)
599 __ADD(ARPHRD_IPDDP,ip/ddp)
600 __ADD(ARPHRD_IPGRE,gre)
601 __ADD(ARPHRD_PIMREG,pimreg)
602 __ADD(ARPHRD_HIPPI,hippi)
603 __ADD(ARPHRD_ASH,ash)
604 __ADD(ARPHRD_ECONET,econet)
605 __ADD(ARPHRD_IRDA,irda)
606 __ADD(ARPHRD_FCPP,fcpp)
607 __ADD(ARPHRD_FCAL,fcal)
608 __ADD(ARPHRD_FCPL,fcpl)
609 __ADD(ARPHRD_FCFABRIC,fcfb_0)
610 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
611 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
612 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
613 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
614 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
615 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
616 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
617 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
618 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
619 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
620 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
621 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
622 __ADD(ARPHRD_IEEE802_TR,tr)
623 __ADD(ARPHRD_IEEE80211,ieee802.11)
624 __ADD(ARPHRD_PHONET,phonet)
625 __ADD(ARPHRD_CAIF, caif)
626 #ifdef ARPHRD_IEEE80211_PRISM
627 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
630 __ADD(ARPHRD_VOID,
void)
633 __ADD(ARPHRD_NONE,nohdr)
637 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
639 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
642 int nl_str2llproto(
const char *name)
644 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
655 static const struct trans_tbl ether_protos[] = {
656 __ADD(ETH_P_LOOP,loop)
658 __ADD(ETH_P_PUPAT,pupat)
663 __ADD(ETH_P_IEEEPUP,ieeepup)
664 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
666 __ADD(ETH_P_DNA_DL,dna_dl)
667 __ADD(ETH_P_DNA_RC,dna_rc)
668 __ADD(ETH_P_DNA_RT,dna_rt)
670 __ADD(ETH_P_DIAG,diag)
671 __ADD(ETH_P_CUST,cust)
674 __ADD(ETH_P_RARP,rarp)
675 __ADD(ETH_P_ATALK,atalk)
676 __ADD(ETH_P_AARP,aarp)
678 __ADD(ETH_P_8021Q,802.1q)
681 __ADD(ETH_P_IPV6,ipv6)
682 __ADD(ETH_P_PAUSE,pause)
683 __ADD(ETH_P_SLOW,slow)
685 __ADD(ETH_P_WCCP,wccp)
687 __ADD(ETH_P_PPP_DISC,ppp_disc)
688 __ADD(ETH_P_PPP_SES,ppp_ses)
689 __ADD(ETH_P_MPLS_UC,mpls_uc)
690 __ADD(ETH_P_MPLS_MC,mpls_mc)
691 __ADD(ETH_P_ATMMPOA,atmmpoa)
692 __ADD(ETH_P_LINK_CTL,link_ctl)
693 __ADD(ETH_P_ATMFATE,atmfate)
696 __ADD(ETH_P_TIPC,tipc)
697 __ADD(ETH_P_1588,ieee1588)
698 __ADD(ETH_P_FCOE,fcoe)
700 __ADD(ETH_P_EDSA,edsa)
701 __ADD(ETH_P_EDP2,edp2)
702 __ADD(ETH_P_802_3,802.3)
703 __ADD(ETH_P_AX25,ax25)
705 __ADD(ETH_P_802_2,802.2)
706 __ADD(ETH_P_SNAP,snap)
707 __ADD(ETH_P_DDCMP,ddcmp)
708 __ADD(ETH_P_WAN_PPP,wan_ppp)
709 __ADD(ETH_P_PPP_MP,ppp_mp)
710 __ADD(ETH_P_LOCALTALK,localtalk)
712 __ADD(ETH_P_PPPTALK,ppptalk)
713 __ADD(ETH_P_TR_802_2,tr_802.2)
714 __ADD(ETH_P_MOBITEX,mobitex)
715 __ADD(ETH_P_CONTROL,control)
716 __ADD(ETH_P_IRDA,irda)
717 __ADD(ETH_P_ECONET,econet)
718 __ADD(ETH_P_HDLC,hdlc)
719 __ADD(ETH_P_ARCNET,arcnet)
721 __ADD(ETH_P_TRAILER,trailer)
722 __ADD(ETH_P_PHONET,phonet)
723 __ADD(ETH_P_IEEE802154,ieee802154)
724 __ADD(ETH_P_CAIF,caif)
727 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
729 return __type2str(eproto, buf, len, ether_protos,
730 ARRAY_SIZE(ether_protos));
733 int nl_str2ether_proto(
const char *name)
735 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
745 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
747 struct protoent *p = getprotobynumber(proto);
750 snprintf(buf, len,
"%s", p->p_name);
754 snprintf(buf, len,
"0x%x", proto);
758 int nl_str2ip_proto(
const char *name)
760 struct protoent *p = getprotobyname(name);
767 l = strtoul(name, &end, 0);
768 if (l == ULONG_MAX || *end !=
'\0')
769 return -NLE_OBJ_NOTFOUND;
797 for (i = 0; i < params->
dp_prefix; i++) {
799 fprintf(params->
dp_fd,
" ");
801 strncat(params->
dp_buf,
" ",
803 sizeof(params->
dp_buf) - 1);
808 params->
dp_nl_cb(params, params->dp_line);
811 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
815 vfprintf(parms->
dp_fd, fmt, args);
818 if (vasprintf(&buf, fmt, args) >= 0) {
820 parms->
dp_cb(parms, buf);
822 strncat(parms->
dp_buf, buf,
844 dump_one(params, fmt, args);
848 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
855 dump_one(parms, fmt, args);
864 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
866 struct trans_list *tl;
868 tl = calloc(1,
sizeof(*tl));
875 nl_list_add_tail(&tl->list, head);
882 struct trans_list *tl, *next;
884 nl_list_for_each_entry_safe(tl, next, head, list) {
889 nl_init_list_head(head);
892 char *__type2str(
int type,
char *buf,
size_t len,
893 const struct trans_tbl *tbl,
size_t tbl_len)
896 for (i = 0; i < tbl_len; i++) {
897 if (tbl[i].i == type) {
898 snprintf(buf, len,
"%s", tbl[i].a);
903 snprintf(buf, len,
"0x%x", type);
907 char *__list_type2str(
int type,
char *buf,
size_t len,
910 struct trans_list *tl;
912 nl_list_for_each_entry(tl, head, list) {
914 snprintf(buf, len,
"%s", tl->a);
919 snprintf(buf, len,
"0x%x", type);
923 char *__flags2str(
int flags,
char *buf,
size_t len,
924 const struct trans_tbl *tbl,
size_t tbl_len)
931 for (i = 0; i < tbl_len; i++) {
932 if (tbl[i].i & tmp) {
934 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
936 strncat(buf,
",", len - strlen(buf) - 1);
943 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
952 for (i = 0; i < tbl_len; i++)
953 if (!strcasecmp(tbl[i].a, buf))
956 l = strtoul(buf, &end, 0);
957 if (l == ULONG_MAX || *end !=
'\0')
958 return -NLE_OBJ_NOTFOUND;
963 int __list_str2type(
const char *buf,
struct nl_list_head *head)
965 struct trans_list *tl;
972 nl_list_for_each_entry(tl, head, list) {
973 if (!strcasecmp(tl->a, buf))
977 l = strtoul(buf, &end, 0);
978 if (l == ULONG_MAX || *end !=
'\0')
979 return -NLE_OBJ_NOTFOUND;
984 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
986 int i, flags = 0, len;
987 char *p = (
char *) buf, *t;
994 len = t ? t - p : strlen(p);
995 for (i = 0; i < tbl_len; i++)
996 if (!strncasecmp(tbl[i].a, p, len))
1012 if (type < 0 || type > NL_DUMP_MAX)
1015 params->dp_line = 0;
1022 dp_dump_line(params, 0,
"%s ",
1023 nl_cache_mngt_type2name(obj->ce_ops,
1024 obj->ce_ops->co_protocol,
1034 if (obj->ce_ops->oo_dump[type])
1035 obj->ce_ops->oo_dump[type](obj, params);