17 #include <netlink-local.h> 18 #include <netlink/netlink.h> 19 #include <netlink/utils.h> 20 #include <linux/socket.h> 31 static void __init nl_debug_init(
void)
35 if ((nldbg = getenv(
"NLDBG"))) {
36 long level = strtol(nldbg, &end, 0);
41 nl_debug_dp.
dp_fd = stderr;
49 static __thread
char *errbuf;
50 static __thread
int nlerrno;
53 int __nl_error(
int err,
const char *file,
unsigned int line,
const char *func,
68 vasprintf(&user_err, fmt, args);
73 asprintf(&errbuf,
"%s:%u:%s: %s (errno = %s)",
74 file, line, func, fmt ? user_err :
"", strerror(err));
76 asprintf(&errbuf,
"%s (errno = %s)",
77 fmt ? user_err :
"", strerror(err));
86 int __nl_read_num_str_file(
const char *path,
int (*cb)(
long,
const char *))
91 fd = fopen(path,
"r");
93 return nl_error(errno,
"Unable to open file %s for reading",
96 while (fgets(buf,
sizeof(buf), fd)) {
101 if (*buf ==
'#' || *buf ==
'\n' || *buf ==
'\r')
104 num = strtol(buf, &end, 0);
106 return nl_error(EINVAL,
"Parsing error");
108 if (num == LONG_MIN || num == LONG_MAX)
109 return nl_error(errno,
"Number of out range");
111 while (*end ==
' ' || *end ==
'\t')
114 goodlen = strcspn(end,
"#\r\n\t ");
116 return nl_error(EINVAL,
"Empty string");
132 int nl_get_errno(
void)
148 return strerror(nlerrno);
191 if (l >= 1099511627776LL) {
193 return ((
double) l) / 1099511627776LL;
194 }
else if (l >= 1073741824) {
196 return ((
double) l) / 1073741824;
197 }
else if (l >= 1048576) {
199 return ((
double) l) / 1048576;
200 }
else if (l >= 1024) {
202 return ((
double) l) / 1024;
222 if (l >= 1000000000000ULL) {
224 return ((
double) l) / 1000000000000ULL;
227 if (l >= 1000000000) {
229 return ((
double) l) / 1000000000;
234 return ((
double) l) / 1000000;
239 return ((
double) l) / 1000;
260 return ((
double) l) / 1000000;
261 }
else if (l >= 1000) {
263 return ((
double) l) / 1000;
296 long l = strtol(str, &p, 0);
301 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
303 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
305 else if (!strcasecmp(p,
"gbit"))
307 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
309 else if (!strcasecmp(p,
"mbit"))
311 else if (!strcasecmp(p,
"kbit"))
313 else if (!strcasecmp(p,
"bit"))
315 else if (strcasecmp(p,
"b") != 0)
338 double d = strtod(str, &p);
346 if (d > 1.0f || d < 0.0f)
349 if (*p && strcmp(p,
"%") != 0)
363 static uint32_t user_hz = USER_HZ;
365 static uint32_t user_hz = 100;
368 static double ticks_per_usec = 1.0f;
379 static void __init get_psched_settings(
void)
381 char name[FILENAME_MAX];
383 int got_hz = 0, got_tick = 0;
386 long hz = strtol(getenv(
"HZ"), NULL, 0);
388 if (LONG_MIN != hz && LONG_MAX != hz) {
395 user_hz = sysconf(_SC_CLK_TCK);
397 if (getenv(
"TICKS_PER_USEC")) {
398 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 tick, us, nom;
415 int r = fscanf(fd,
"%08x%08x%08x%*08x", &tick, &us, &nom);
417 if (4 == r && nom == 1000000 && !got_tick)
418 ticks_per_usec = (double)tick/(
double)us;
441 return us * ticks_per_usec;
452 return ticks / ticks_per_usec;
455 long nl_time2int(
const char *str)
458 long l = strtol(str, &p, 0);
463 if (!strcasecmp(p,
"min") == 0 || !strcasecmp(p,
"m"))
465 else if (!strcasecmp(p,
"hour") || !strcasecmp(p,
"h"))
467 else if (!strcasecmp(p,
"day") || !strcasecmp(p,
"d"))
469 else if (strcasecmp(p,
"s") != 0)
491 char *units[] = {
"d",
"h",
"m",
"s",
"msec"};
493 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit 503 for (i = 0; i < ARRAY_SIZE(split); i++) {
506 snprintf(t,
sizeof(t),
"%s%d%s",
507 strlen(buf) ?
" " :
"", split[i], units[i]);
508 strncat(buf, t, len - strlen(buf) - 1);
522 static struct trans_tbl llprotos[] = {
524 __ADD(ARPHRD_ETHER,ether)
525 __ADD(ARPHRD_EETHER,eether)
526 __ADD(ARPHRD_AX25,ax25)
527 __ADD(ARPHRD_PRONET,pronet)
528 __ADD(ARPHRD_CHAOS,chaos)
529 __ADD(ARPHRD_IEEE802,ieee802)
530 __ADD(ARPHRD_ARCNET,arcnet)
531 __ADD(ARPHRD_APPLETLK,atalk)
532 __ADD(ARPHRD_DLCI,dlci)
533 __ADD(ARPHRD_ATM,atm)
534 __ADD(ARPHRD_METRICOM,metricom)
535 __ADD(ARPHRD_IEEE1394,ieee1394)
537 __ADD(ARPHRD_EUI64,eui64)
539 __ADD(ARPHRD_INFINIBAND,infiniband)
540 __ADD(ARPHRD_SLIP,slip)
541 __ADD(ARPHRD_CSLIP,cslip)
542 __ADD(ARPHRD_SLIP6,slip6)
543 __ADD(ARPHRD_CSLIP6,cslip6)
544 __ADD(ARPHRD_RSRVD,rsrvd)
545 __ADD(ARPHRD_ADAPT,adapt)
546 __ADD(ARPHRD_ROSE,rose)
547 __ADD(ARPHRD_X25,x25)
549 __ADD(ARPHRD_HWX25,hwx25)
551 __ADD(ARPHRD_PPP,ppp)
552 __ADD(ARPHRD_HDLC,hdlc)
553 __ADD(ARPHRD_LAPB,lapb)
554 __ADD(ARPHRD_DDCMP,ddcmp)
555 __ADD(ARPHRD_RAWHDLC,rawhdlc)
556 __ADD(ARPHRD_TUNNEL,ipip)
557 __ADD(ARPHRD_TUNNEL6,tunnel6)
558 __ADD(ARPHRD_FRAD,frad)
559 __ADD(ARPHRD_SKIP,skip)
560 __ADD(ARPHRD_LOOPBACK,loopback)
561 __ADD(ARPHRD_LOCALTLK,localtlk)
562 __ADD(ARPHRD_FDDI,fddi)
563 __ADD(ARPHRD_BIF,bif)
564 __ADD(ARPHRD_SIT,sit)
565 __ADD(ARPHRD_IPDDP,ip/ddp)
566 __ADD(ARPHRD_IPGRE,gre)
567 __ADD(ARPHRD_PIMREG,pimreg)
568 __ADD(ARPHRD_HIPPI,hippi)
569 __ADD(ARPHRD_ASH,ash)
570 __ADD(ARPHRD_ECONET,econet)
571 __ADD(ARPHRD_IRDA,irda)
572 __ADD(ARPHRD_FCPP,fcpp)
573 __ADD(ARPHRD_FCAL,fcal)
574 __ADD(ARPHRD_FCPL,fcpl)
575 __ADD(ARPHRD_FCFABRIC,fcfb_0)
576 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
577 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
578 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
579 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
580 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
581 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
582 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
583 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
584 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
585 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
586 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
587 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
588 __ADD(ARPHRD_IEEE802_TR,tr)
589 __ADD(ARPHRD_IEEE80211,ieee802.11)
590 #ifdef ARPHRD_IEEE80211_PRISM 591 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
594 __ADD(ARPHRD_VOID,
void)
598 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
600 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
603 int nl_str2llproto(
const char *name)
605 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
616 static struct trans_tbl ether_protos[] = {
617 __ADD(ETH_P_LOOP,loop)
619 __ADD(ETH_P_PUPAT,pupat)
624 __ADD(ETH_P_IEEEPUP,ieeepup)
625 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
627 __ADD(ETH_P_DNA_DL,dna_dl)
628 __ADD(ETH_P_DNA_RC,dna_rc)
629 __ADD(ETH_P_DNA_RT,dna_rt)
631 __ADD(ETH_P_DIAG,diag)
632 __ADD(ETH_P_CUST,cust)
634 __ADD(ETH_P_RARP,rarp)
635 __ADD(ETH_P_ATALK,atalk)
636 __ADD(ETH_P_AARP,aarp)
638 __ADD(ETH_P_8021Q,802.1q)
641 __ADD(ETH_P_IPV6,ipv6)
643 __ADD(ETH_P_WCCP,wccp)
645 __ADD(ETH_P_PPP_DISC,ppp_disc)
646 __ADD(ETH_P_PPP_SES,ppp_ses)
647 __ADD(ETH_P_MPLS_UC,mpls_uc)
648 __ADD(ETH_P_MPLS_MC,mpls_mc)
649 __ADD(ETH_P_ATMMPOA,atmmpoa)
650 __ADD(ETH_P_ATMFATE,atmfate)
651 __ADD(ETH_P_EDP2,edp2)
652 __ADD(ETH_P_802_3,802.3)
653 __ADD(ETH_P_AX25,ax25)
655 __ADD(ETH_P_802_2,802.2)
656 __ADD(ETH_P_SNAP,snap)
657 __ADD(ETH_P_DDCMP,ddcmp)
658 __ADD(ETH_P_WAN_PPP,wan_ppp)
659 __ADD(ETH_P_PPP_MP,ppp_mp)
660 __ADD(ETH_P_LOCALTALK,localtalk)
661 __ADD(ETH_P_PPPTALK,ppptalk)
662 __ADD(ETH_P_TR_802_2,tr_802.2)
663 __ADD(ETH_P_MOBITEX,mobitex)
664 __ADD(ETH_P_CONTROL,control)
665 __ADD(ETH_P_IRDA,irda)
666 __ADD(ETH_P_ECONET,econet)
667 __ADD(ETH_P_HDLC,hdlc)
670 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
672 return __type2str(eproto, buf, len, ether_protos,
673 ARRAY_SIZE(ether_protos));
676 int nl_str2ether_proto(
const char *name)
678 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
688 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
690 struct protoent *p = getprotobynumber(proto);
693 snprintf(buf, len,
"%s", p->p_name);
697 snprintf(buf, len,
"0x%x", proto);
701 int nl_str2ip_proto(
const char *name)
703 struct protoent *p = getprotobyname(name);
710 l = strtoul(name, &end, 0);
711 if (l == ULONG_MAX || *end !=
'\0')
739 for (i = 0; i < params->
dp_prefix; i++) {
741 fprintf(params->
dp_fd,
" ");
743 strncat(params->
dp_buf,
" ",
745 sizeof(params->
dp_buf) - 1);
767 __dp_dump(params, fmt, args);
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.
#define NL_PROB_MAX
Upper probability limit.
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.
double nl_cancel_down_us(uint32_t l, char **unit)
Cancel down a micro second value.
uint32_t nl_ticks2us(uint32_t ticks)
Convert ticks to micro seconds.
void nl_new_line(struct nl_dump_params *params, int line)
Handle a new line while dumping.
long nl_prob2int(const char *str)
Convert a character string to a probability.
char * nl_geterror(void)
Return error message for an error code.
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.
void nl_perror(const char *s)
Print a libnl error message.
int nl_get_hz(void)
Return the value of HZ.
Dump all attributes but no statistics.