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" },
332 snprintf(buf, len,
"0B");
336 for (i = 0; i < ARRAY_SIZE(size_units); i++) {
337 if (size >= size_units[i].limit) {
338 snprintf(buf, len,
"%.2g%s",
339 (
double) size / size_units[i].limit,
364 double d = strtod(str, &p);
372 if (d > 1.0f || d < 0.0f)
375 if (*p && strcmp(p,
"%") != 0)
392 static uint32_t user_hz = USER_HZ;
393 static uint32_t psched_hz = USER_HZ;
395 static double ticks_per_usec = 1.0f;
406 static void __init get_psched_settings(
void)
408 char name[FILENAME_MAX];
413 long hz = strtol(getenv(
"HZ"), NULL, 0);
415 if (LONG_MIN != hz && LONG_MAX != hz) {
422 user_hz = sysconf(_SC_CLK_TCK);
426 if (getenv(
"TICKS_PER_USEC")) {
427 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
431 if (getenv(
"PROC_NET_PSCHED"))
432 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
433 else if (getenv(
"PROC_ROOT"))
434 snprintf(name,
sizeof(name),
"%s/net/psched",
435 getenv(
"PROC_ROOT"));
437 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
439 if ((fd = fopen(name,
"r"))) {
440 unsigned int ns_per_usec, ns_per_tick, nom, denom;
442 if (fscanf(fd,
"%08x %08x %08x %08x",
443 &ns_per_usec, &ns_per_tick, &nom, &denom) != 4) {
444 NL_DBG(1,
"Fatal error: can not read psched settings from \"%s\". " \
445 "Try to set TICKS_PER_USEC, PROC_NET_PSCHED or PROC_ROOT " \
446 "environment variables\n", name);
450 ticks_per_usec = (double) ns_per_usec /
451 (
double) ns_per_tick;
485 return us * ticks_per_usec;
496 return ticks / ticks_per_usec;
499 int nl_str2msec(
const char *str, uint64_t *result)
501 uint64_t total = 0, l;
506 l = strtoul(str, &p, 0);
510 plen = strcspn(p,
" \t");
514 else if (!strncasecmp(p,
"sec", plen))
516 else if (!strncasecmp(p,
"min", plen))
517 total += (l * 1000*60);
518 else if (!strncasecmp(p,
"hour", plen))
519 total += (l * 1000*60*60);
520 else if (!strncasecmp(p,
"day", plen))
521 total += (l * 1000*60*60*24);
528 }
while (*str && *p);
551 static const char *units[5] = {
"d",
"h",
"m",
"s",
"msec"};
552 char *
const buf_orig = buf;
555 snprintf(buf, len,
"0msec");
559 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit)) msec %= unit
567 for (i = 0; i < ARRAY_SIZE(split) && len; i++) {
571 l = snprintf(buf, len,
"%s%" PRIu64
"%s",
572 (buf==buf_orig) ?
"" :
" ", split[i], units[i]);
587 static const struct trans_tbl nlfamilies[] = {
588 __ADD(NETLINK_ROUTE,route)
589 __ADD(NETLINK_USERSOCK,usersock)
590 __ADD(NETLINK_FIREWALL,firewall)
591 __ADD(NETLINK_INET_DIAG,inetdiag)
592 __ADD(NETLINK_NFLOG,nflog)
593 __ADD(NETLINK_XFRM,xfrm)
594 __ADD(NETLINK_SELINUX,selinux)
595 __ADD(NETLINK_ISCSI,iscsi)
596 __ADD(NETLINK_AUDIT,audit)
597 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
598 __ADD(NETLINK_CONNECTOR,connector)
599 __ADD(NETLINK_NETFILTER,netfilter)
600 __ADD(NETLINK_IP6_FW,ip6_fw)
601 __ADD(NETLINK_DNRTMSG,dnrtmsg)
602 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
603 __ADD(NETLINK_GENERIC,generic)
604 __ADD(NETLINK_SCSITRANSPORT,scsitransport)
605 __ADD(NETLINK_ECRYPTFS,ecryptfs)
608 char * nl_nlfamily2str(
int family,
char *buf,
size_t size)
610 return __type2str(family, buf, size, nlfamilies,
611 ARRAY_SIZE(nlfamilies));
614 int nl_str2nlfamily(
const char *name)
616 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
628 static const struct trans_tbl llprotos[] = {
630 __ADD(ARPHRD_ETHER,ether)
631 __ADD(ARPHRD_EETHER,eether)
632 __ADD(ARPHRD_AX25,ax25)
633 __ADD(ARPHRD_PRONET,pronet)
634 __ADD(ARPHRD_CHAOS,chaos)
635 __ADD(ARPHRD_IEEE802,ieee802)
636 __ADD(ARPHRD_ARCNET,arcnet)
637 __ADD(ARPHRD_APPLETLK,atalk)
638 __ADD(ARPHRD_DLCI,dlci)
639 __ADD(ARPHRD_ATM,atm)
640 __ADD(ARPHRD_METRICOM,metricom)
641 __ADD(ARPHRD_IEEE1394,ieee1394)
643 __ADD(ARPHRD_EUI64,eui64)
645 __ADD(ARPHRD_INFINIBAND,infiniband)
646 __ADD(ARPHRD_SLIP,slip)
647 __ADD(ARPHRD_CSLIP,cslip)
648 __ADD(ARPHRD_SLIP6,slip6)
649 __ADD(ARPHRD_CSLIP6,cslip6)
650 __ADD(ARPHRD_RSRVD,rsrvd)
651 __ADD(ARPHRD_ADAPT,adapt)
652 __ADD(ARPHRD_ROSE,rose)
653 __ADD(ARPHRD_X25,x25)
655 __ADD(ARPHRD_HWX25,hwx25)
657 __ADD(ARPHRD_CAN,can)
658 __ADD(ARPHRD_PPP,ppp)
659 __ADD(ARPHRD_HDLC,hdlc)
660 __ADD(ARPHRD_LAPB,lapb)
661 __ADD(ARPHRD_DDCMP,ddcmp)
662 __ADD(ARPHRD_RAWHDLC,rawhdlc)
663 __ADD(ARPHRD_TUNNEL,ipip)
664 __ADD(ARPHRD_TUNNEL6,tunnel6)
665 __ADD(ARPHRD_FRAD,frad)
666 __ADD(ARPHRD_SKIP,skip)
667 __ADD(ARPHRD_LOOPBACK,loopback)
668 __ADD(ARPHRD_LOCALTLK,localtlk)
669 __ADD(ARPHRD_FDDI,fddi)
670 __ADD(ARPHRD_BIF,bif)
671 __ADD(ARPHRD_SIT,sit)
672 __ADD(ARPHRD_IPDDP,ip/ddp)
673 __ADD(ARPHRD_IPGRE,gre)
674 __ADD(ARPHRD_PIMREG,pimreg)
675 __ADD(ARPHRD_HIPPI,hippi)
676 __ADD(ARPHRD_ASH,ash)
677 __ADD(ARPHRD_ECONET,econet)
678 __ADD(ARPHRD_IRDA,irda)
679 __ADD(ARPHRD_FCPP,fcpp)
680 __ADD(ARPHRD_FCAL,fcal)
681 __ADD(ARPHRD_FCPL,fcpl)
682 __ADD(ARPHRD_FCFABRIC,fcfb_0)
683 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
684 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
685 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
686 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
687 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
688 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
689 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
690 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
691 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
692 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
693 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
694 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
695 __ADD(ARPHRD_IEEE802_TR,tr)
696 __ADD(ARPHRD_IEEE80211,ieee802.11)
697 __ADD(ARPHRD_PHONET,phonet)
699 __ADD(ARPHRD_CAIF, caif)
701 #ifdef ARPHRD_IEEE80211_PRISM
702 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
705 __ADD(ARPHRD_VOID,
void)
708 __ADD(ARPHRD_NONE,nohdr)
712 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
714 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
717 int nl_str2llproto(
const char *name)
719 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
730 static const struct trans_tbl ether_protos[] = {
731 __ADD(ETH_P_LOOP,loop)
733 __ADD(ETH_P_PUPAT,pupat)
738 __ADD(ETH_P_IEEEPUP,ieeepup)
739 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
741 __ADD(ETH_P_DNA_DL,dna_dl)
742 __ADD(ETH_P_DNA_RC,dna_rc)
743 __ADD(ETH_P_DNA_RT,dna_rt)
745 __ADD(ETH_P_DIAG,diag)
746 __ADD(ETH_P_CUST,cust)
749 __ADD(ETH_P_RARP,rarp)
750 __ADD(ETH_P_ATALK,atalk)
751 __ADD(ETH_P_AARP,aarp)
753 __ADD(ETH_P_8021Q,802.1q)
756 __ADD(ETH_P_IPV6,ipv6)
757 __ADD(ETH_P_PAUSE,pause)
758 __ADD(ETH_P_SLOW,slow)
760 __ADD(ETH_P_WCCP,wccp)
762 __ADD(ETH_P_PPP_DISC,ppp_disc)
763 __ADD(ETH_P_PPP_SES,ppp_ses)
764 __ADD(ETH_P_MPLS_UC,mpls_uc)
765 __ADD(ETH_P_MPLS_MC,mpls_mc)
766 __ADD(ETH_P_ATMMPOA,atmmpoa)
767 __ADD(ETH_P_LINK_CTL,link_ctl)
768 __ADD(ETH_P_ATMFATE,atmfate)
771 __ADD(ETH_P_TIPC,tipc)
772 __ADD(ETH_P_1588,ieee1588)
773 __ADD(ETH_P_FCOE,fcoe)
775 __ADD(ETH_P_EDSA,edsa)
776 __ADD(ETH_P_EDP2,edp2)
777 __ADD(ETH_P_802_3,802.3)
778 __ADD(ETH_P_AX25,ax25)
780 __ADD(ETH_P_802_2,802.2)
781 __ADD(ETH_P_SNAP,snap)
782 __ADD(ETH_P_DDCMP,ddcmp)
783 __ADD(ETH_P_WAN_PPP,wan_ppp)
784 __ADD(ETH_P_PPP_MP,ppp_mp)
785 __ADD(ETH_P_LOCALTALK,localtalk)
787 __ADD(ETH_P_PPPTALK,ppptalk)
788 __ADD(ETH_P_TR_802_2,tr_802.2)
789 __ADD(ETH_P_MOBITEX,mobitex)
790 __ADD(ETH_P_CONTROL,control)
791 __ADD(ETH_P_IRDA,irda)
792 __ADD(ETH_P_ECONET,econet)
793 __ADD(ETH_P_HDLC,hdlc)
794 __ADD(ETH_P_ARCNET,arcnet)
796 __ADD(ETH_P_TRAILER,trailer)
797 __ADD(ETH_P_PHONET,phonet)
798 __ADD(ETH_P_IEEE802154,ieee802154)
799 __ADD(ETH_P_CAIF,caif)
802 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
804 return __type2str(eproto, buf, len, ether_protos,
805 ARRAY_SIZE(ether_protos));
808 int nl_str2ether_proto(
const char *name)
810 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
820 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
822 struct protoent *p = getprotobynumber(proto);
825 snprintf(buf, len,
"%s", p->p_name);
829 snprintf(buf, len,
"0x%x", proto);
833 int nl_str2ip_proto(
const char *name)
835 struct protoent *p = getprotobyname(name);
842 l = strtoul(name, &end, 0);
843 if (l == ULONG_MAX || *end !=
'\0')
844 return -NLE_OBJ_NOTFOUND;
872 for (i = 0; i < params->
dp_prefix; i++) {
874 fprintf(params->
dp_fd,
" ");
876 strncat(params->
dp_buf,
" ",
878 strlen(params->
dp_buf) - 1);
883 params->
dp_nl_cb(params, params->dp_line);
886 static void dump_one(
struct nl_dump_params *parms,
const char *fmt,
890 vfprintf(parms->
dp_fd, fmt, args);
893 if (vasprintf(&buf, fmt, args) >= 0) {
895 parms->
dp_cb(parms, buf);
897 strncat(parms->
dp_buf, buf,
899 strlen(parms->
dp_buf) - 1);
920 dump_one(params, fmt, args);
924 void nl_dump_line(
struct nl_dump_params *parms,
const char *fmt, ...)
931 dump_one(parms, fmt, args);
940 int __trans_list_add(
int i,
const char *a,
struct nl_list_head *head)
942 struct trans_list *tl;
944 tl = calloc(1,
sizeof(*tl));
951 nl_list_add_tail(&tl->list, head);
958 struct trans_list *tl, *next;
960 nl_list_for_each_entry_safe(tl, next, head, list) {
965 nl_init_list_head(head);
968 char *__type2str(
int type,
char *buf,
size_t len,
969 const struct trans_tbl *tbl,
size_t tbl_len)
972 for (i = 0; i < tbl_len; i++) {
973 if (tbl[i].i == type) {
974 snprintf(buf, len,
"%s", tbl[i].a);
979 snprintf(buf, len,
"0x%x", type);
983 char *__list_type2str(
int type,
char *buf,
size_t len,
986 struct trans_list *tl;
988 nl_list_for_each_entry(tl, head, list) {
990 snprintf(buf, len,
"%s", tl->a);
995 snprintf(buf, len,
"0x%x", type);
999 char *__flags2str(
int flags,
char *buf,
size_t len,
1000 const struct trans_tbl *tbl,
size_t tbl_len)
1005 memset(buf, 0, len);
1007 for (i = 0; i < tbl_len; i++) {
1008 if (tbl[i].i & tmp) {
1010 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
1012 strncat(buf,
",", len - strlen(buf) - 1);
1019 int __str2type(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1028 for (i = 0; i < tbl_len; i++)
1029 if (!strcasecmp(tbl[i].a, buf))
1032 l = strtoul(buf, &end, 0);
1033 if (l == ULONG_MAX || *end !=
'\0')
1034 return -NLE_OBJ_NOTFOUND;
1039 int __list_str2type(
const char *buf,
struct nl_list_head *head)
1041 struct trans_list *tl;
1048 nl_list_for_each_entry(tl, head, list) {
1049 if (!strcasecmp(tl->a, buf))
1053 l = strtoul(buf, &end, 0);
1054 if (l == ULONG_MAX || *end !=
'\0')
1055 return -NLE_OBJ_NOTFOUND;
1060 int __str2flags(
const char *buf,
const struct trans_tbl *tbl,
size_t tbl_len)
1065 char *p = (
char *) buf, *t;
1072 len = t ? t - p : strlen(p);
1073 for (i = 0; i < tbl_len; i++)
1074 if (len == strlen(tbl[i].a) &&
1075 !strncasecmp(tbl[i].a, p, len))
1087 void dump_from_ops(
struct nl_object *obj,
struct nl_dump_params *params)
1091 if (type < 0 || type > NL_DUMP_MAX)
1094 params->dp_line = 0;
1101 dp_dump_line(params, 0,
"%s ",
1102 nl_cache_mngt_type2name(obj->ce_ops,
1103 obj->ce_ops->co_protocol,
1110 if (obj->ce_ops->oo_dump[type])
1111 obj->ce_ops->oo_dump[type](obj, params);
1133 int nl_has_capability (
int capability)
1135 static const uint8_t caps[ ( NL_CAPABILITY_MAX + 7 ) / 8 ] = {
1136 #define _NL_ASSERT(expr) ( 0 * sizeof(struct { unsigned int x: ( (!!(expr)) ? 1 : -1 ); }) )
1137 #define _NL_SETV(i, r, v) \
1138 ( _NL_ASSERT( (v) == 0 || (i) * 8 + (r) == (v) - 1 ) + \
1139 ( (v) == 0 ? 0 : (1 << (r)) ) )
1140 #define _NL_SET(i, v0, v1, v2, v3, v4, v5, v6, v7) \
1142 _NL_SETV((i), 0, (v0)) | _NL_SETV((i), 4, (v4)) | \
1143 _NL_SETV((i), 1, (v1)) | _NL_SETV((i), 5, (v5)) | \
1144 _NL_SETV((i), 2, (v2)) | _NL_SETV((i), 6, (v6)) | \
1145 _NL_SETV((i), 3, (v3)) | _NL_SETV((i), 7, (v7)) )
1148 NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE,
1149 NL_CAPABILITY_ROUTE_LINK_CLS_ADD_ACT_OWN_REFERENCE,
1150 NL_CAPABILITY_NL_CONNECT_RETRY_GENERATE_PORT_ON_ADDRINUSE,
1160 if (capability <= 0 || capability > NL_CAPABILITY_MAX)
1163 return (caps[capability / 8] & (1 << (capability % 8))) != 0;
int nl_get_user_hz(void)
Return the value of HZ.
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
char * dp_buf
Alternatively the output may be redirected into a buffer.
FILE * dp_fd
File descriptor the dumping output should go to.
long nl_size2int(const char *str)
Convert a character string to a size.
double nl_cancel_down_bits(unsigned long long l, char **unit)
Cancel down a bit counter.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
void(* dp_cb)(struct nl_dump_params *, char *)
A callback invoked for output.
#define NL_PROB_MAX
Upper probability limit nl_dump_type.
Dump all attributes but no statistics.
char * nl_size2str(const size_t size, char *buf, const size_t len)
Convert a size toa character string.
rtnl_route_build_msg() no longer guesses the route scope if explicitly set to RT_SCOPE_NOWHERE.
void(* dp_nl_cb)(struct nl_dump_params *, int)
A callback invoked for every new line, can be used to customize the indentation.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
int dp_pre_dump
PRIVATE Set if a dump was performed prior to the actual dump handler.
double nl_cancel_down_us(uint32_t l, char **unit)
Cancel down a micro second value.
int nl_get_psched_hz(void)
Return the value of packet scheduler HZ.
uint32_t nl_ticks2us(uint32_t ticks)
Convert ticks to micro seconds.
long nl_prob2int(const char *str)
Convert a character string to a probability.
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).
uint32_t nl_us2ticks(uint32_t us)
Convert micro seconds to ticks.
size_t dp_buflen
Length of the buffer dp_buf.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
int nl_debug
Global variable indicating the desired level of debugging output.
int dp_dump_msgtype
Causes each element to be prefixed with the message type.