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;
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);
190 if (l >= 1099511627776LL) {
192 return ((
double) l) / 1099511627776LL;
193 }
else if (l >= 1073741824) {
195 return ((
double) l) / 1073741824;
196 }
else if (l >= 1048576) {
198 return ((
double) l) / 1048576;
199 }
else if (l >= 1024) {
201 return ((
double) l) / 1024;
220 if (l >= 1099511627776ULL) {
222 return ((
double) l) / 1099511627776ULL;
223 }
else if (l >= 1073741824) {
225 return ((
double) l) / 1073741824;
226 }
else if (l >= 1048576) {
228 return ((
double) l) / 1048576;
229 }
else if (l >= 1024) {
231 return ((
double) l) / 1024;
253 return ((
double) l) / 1000000;
254 }
else if (l >= 1000) {
256 return ((
double) l) / 1000;
286 long l = strtol(str, &p, 0);
291 if (!strcasecmp(p,
"kb") || !strcasecmp(p,
"k"))
293 else if (!strcasecmp(p,
"gb") || !strcasecmp(p,
"g"))
295 else if (!strcasecmp(p,
"gbit"))
296 l *= 1024*1024*1024/8;
297 else if (!strcasecmp(p,
"mb") || !strcasecmp(p,
"m"))
299 else if (!strcasecmp(p,
"mbit"))
301 else if (!strcasecmp(p,
"kbit"))
303 else if (!strcasecmp(p,
"bit"))
305 else if (strcasecmp(p,
"b") != 0)
328 double d = strtod(str, &p);
336 if (d > 1.0f || d < 0.0f)
339 if (*p && strcmp(p,
"%") != 0)
353 static uint32_t user_hz = USER_HZ;
355 static uint32_t user_hz = 100;
358 static double ticks_per_usec = 1.0f;
369 static void __init get_psched_settings(
void)
371 char name[FILENAME_MAX];
373 int got_hz = 0, got_tick = 0;
376 long hz = strtol(getenv(
"HZ"), NULL, 0);
378 if (LONG_MIN != hz && LONG_MAX != hz) {
385 user_hz = sysconf(_SC_CLK_TCK);
387 if (getenv(
"TICKS_PER_USEC")) {
388 double t = strtod(getenv(
"TICKS_PER_USEC"), NULL);
395 if (getenv(
"PROC_NET_PSCHED"))
396 snprintf(name,
sizeof(name),
"%s", getenv(
"PROC_NET_PSCHED"));
397 else if (getenv(
"PROC_ROOT"))
398 snprintf(name,
sizeof(name),
"%s/net/psched",
399 getenv(
"PROC_ROOT"));
401 strncpy(name,
"/proc/net/psched",
sizeof(name) - 1);
403 if ((fd = fopen(name,
"r"))) {
404 uint32_t tick, us, nom;
405 int r = fscanf(fd,
"%08x%08x%08x%*08x", &tick, &us, &nom);
407 if (4 == r && nom == 1000000 && !got_tick)
408 ticks_per_usec = (double)tick/(
double)us;
431 return us * ticks_per_usec;
442 return ticks / ticks_per_usec;
445 long nl_time2int(
const char *str)
448 long l = strtol(str, &p, 0);
453 if (!strcasecmp(p,
"min") == 0 || !strcasecmp(p,
"m"))
455 else if (!strcasecmp(p,
"hour") || !strcasecmp(p,
"h"))
457 else if (!strcasecmp(p,
"day") || !strcasecmp(p,
"d"))
459 else if (strcasecmp(p,
"s") != 0)
481 char *units[] = {
"d",
"h",
"m",
"s",
"msec"};
483 #define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
493 for (i = 0; i < ARRAY_SIZE(split); i++) {
496 snprintf(t,
sizeof(t),
"%s%d%s",
497 strlen(buf) ?
" " :
"", split[i], units[i]);
498 strncat(buf, t, len - strlen(buf) - 1);
512 static struct trans_tbl llprotos[] = {
514 __ADD(ARPHRD_ETHER,ether)
515 __ADD(ARPHRD_EETHER,eether)
516 __ADD(ARPHRD_AX25,ax25)
517 __ADD(ARPHRD_PRONET,pronet)
518 __ADD(ARPHRD_CHAOS,chaos)
519 __ADD(ARPHRD_IEEE802,ieee802)
520 __ADD(ARPHRD_ARCNET,arcnet)
521 __ADD(ARPHRD_APPLETLK,atalk)
522 __ADD(ARPHRD_DLCI,dlci)
523 __ADD(ARPHRD_ATM,atm)
524 __ADD(ARPHRD_METRICOM,metricom)
525 __ADD(ARPHRD_IEEE1394,ieee1394)
527 __ADD(ARPHRD_EUI64,eui64)
529 __ADD(ARPHRD_INFINIBAND,infiniband)
530 __ADD(ARPHRD_SLIP,slip)
531 __ADD(ARPHRD_CSLIP,cslip)
532 __ADD(ARPHRD_SLIP6,slip6)
533 __ADD(ARPHRD_CSLIP6,cslip6)
534 __ADD(ARPHRD_RSRVD,rsrvd)
535 __ADD(ARPHRD_ADAPT,adapt)
536 __ADD(ARPHRD_ROSE,rose)
537 __ADD(ARPHRD_X25,x25)
539 __ADD(ARPHRD_HWX25,hwx25)
541 __ADD(ARPHRD_PPP,ppp)
542 __ADD(ARPHRD_HDLC,hdlc)
543 __ADD(ARPHRD_LAPB,lapb)
544 __ADD(ARPHRD_DDCMP,ddcmp)
545 __ADD(ARPHRD_RAWHDLC,rawhdlc)
546 __ADD(ARPHRD_TUNNEL,ipip)
547 __ADD(ARPHRD_TUNNEL6,tunnel6)
548 __ADD(ARPHRD_FRAD,frad)
549 __ADD(ARPHRD_SKIP,skip)
550 __ADD(ARPHRD_LOOPBACK,loopback)
551 __ADD(ARPHRD_LOCALTLK,localtlk)
552 __ADD(ARPHRD_FDDI,fddi)
553 __ADD(ARPHRD_BIF,bif)
554 __ADD(ARPHRD_SIT,sit)
555 __ADD(ARPHRD_IPDDP,ip/ddp)
556 __ADD(ARPHRD_IPGRE,gre)
557 __ADD(ARPHRD_PIMREG,pimreg)
558 __ADD(ARPHRD_HIPPI,hippi)
559 __ADD(ARPHRD_ASH,ash)
560 __ADD(ARPHRD_ECONET,econet)
561 __ADD(ARPHRD_IRDA,irda)
562 __ADD(ARPHRD_FCPP,fcpp)
563 __ADD(ARPHRD_FCAL,fcal)
564 __ADD(ARPHRD_FCPL,fcpl)
565 __ADD(ARPHRD_FCFABRIC,fcfb_0)
566 __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
567 __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
568 __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
569 __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
570 __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
571 __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
572 __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
573 __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
574 __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
575 __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
576 __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
577 __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
578 __ADD(ARPHRD_IEEE802_TR,tr)
579 __ADD(ARPHRD_IEEE80211,ieee802.11)
580 #ifdef ARPHRD_IEEE80211_PRISM
581 __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
584 __ADD(ARPHRD_VOID,
void)
588 char * nl_llproto2str(
int llproto,
char *buf,
size_t len)
590 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
593 int nl_str2llproto(
const char *name)
595 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
606 static struct trans_tbl ether_protos[] = {
607 __ADD(ETH_P_LOOP,loop)
609 __ADD(ETH_P_PUPAT,pupat)
614 __ADD(ETH_P_IEEEPUP,ieeepup)
615 __ADD(ETH_P_IEEEPUPAT,ieeepupat)
617 __ADD(ETH_P_DNA_DL,dna_dl)
618 __ADD(ETH_P_DNA_RC,dna_rc)
619 __ADD(ETH_P_DNA_RT,dna_rt)
621 __ADD(ETH_P_DIAG,diag)
622 __ADD(ETH_P_CUST,cust)
624 __ADD(ETH_P_RARP,rarp)
625 __ADD(ETH_P_ATALK,atalk)
626 __ADD(ETH_P_AARP,aarp)
628 __ADD(ETH_P_8021Q,802.1q)
631 __ADD(ETH_P_IPV6,ipv6)
633 __ADD(ETH_P_WCCP,wccp)
635 __ADD(ETH_P_PPP_DISC,ppp_disc)
636 __ADD(ETH_P_PPP_SES,ppp_ses)
637 __ADD(ETH_P_MPLS_UC,mpls_uc)
638 __ADD(ETH_P_MPLS_MC,mpls_mc)
639 __ADD(ETH_P_ATMMPOA,atmmpoa)
640 __ADD(ETH_P_ATMFATE,atmfate)
641 __ADD(ETH_P_EDP2,edp2)
642 __ADD(ETH_P_802_3,802.3)
643 __ADD(ETH_P_AX25,ax25)
645 __ADD(ETH_P_802_2,802.2)
646 __ADD(ETH_P_SNAP,snap)
647 __ADD(ETH_P_DDCMP,ddcmp)
648 __ADD(ETH_P_WAN_PPP,wan_ppp)
649 __ADD(ETH_P_PPP_MP,ppp_mp)
650 __ADD(ETH_P_LOCALTALK,localtalk)
651 __ADD(ETH_P_PPPTALK,ppptalk)
652 __ADD(ETH_P_TR_802_2,tr_802.2)
653 __ADD(ETH_P_MOBITEX,mobitex)
654 __ADD(ETH_P_CONTROL,control)
655 __ADD(ETH_P_IRDA,irda)
656 __ADD(ETH_P_ECONET,econet)
657 __ADD(ETH_P_HDLC,hdlc)
660 char *nl_ether_proto2str(
int eproto,
char *buf,
size_t len)
662 return __type2str(eproto, buf, len, ether_protos,
663 ARRAY_SIZE(ether_protos));
666 int nl_str2ether_proto(
const char *name)
668 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
678 char *nl_ip_proto2str(
int proto,
char *buf,
size_t len)
680 struct protoent *p = getprotobynumber(proto);
683 snprintf(buf, len,
"%s", p->p_name);
687 snprintf(buf, len,
"0x%x", proto);
691 int nl_str2ip_proto(
const char *name)
693 struct protoent *p = getprotobyname(name);
700 l = strtoul(name, &end, 0);
701 if (l == ULONG_MAX || *end !=
'\0')
729 for (i = 0; i < params->
dp_prefix; i++) {
731 fprintf(params->
dp_fd,
" ");
733 strncat(params->
dp_buf,
" ",
735 sizeof(params->
dp_buf) - 1);
757 __dp_dump(params, fmt, args);