diff options
author | Kozlov Dmitry <xeb@mail.ru> | 2012-07-11 18:47:08 +0400 |
---|---|---|
committer | Kozlov Dmitry <xeb@mail.ru> | 2012-07-11 18:47:08 +0400 |
commit | b2e106f8778fd834e0518cc6c0704d7d6b50875e (patch) | |
tree | e6a7dcd7f80b4cb52dd59a2cd087f4c30c357b41 /accel-pppd/libnetlink | |
parent | 07c57d04f2c585f4e3ba37bf61c3d14f9d40cd7d (diff) | |
download | accel-ppp-b2e106f8778fd834e0518cc6c0704d7d6b50875e.tar.gz accel-ppp-b2e106f8778fd834e0518cc6c0704d7d6b50875e.zip |
radius: fetch interface statistics via netlink
Diffstat (limited to 'accel-pppd/libnetlink')
-rw-r--r-- | accel-pppd/libnetlink/iplink.c | 93 | ||||
-rw-r--r-- | accel-pppd/libnetlink/iplink.h | 3 |
2 files changed, 94 insertions, 2 deletions
diff --git a/accel-pppd/libnetlink/iplink.c b/accel-pppd/libnetlink/iplink.c index ba3ada06..df00285b 100644 --- a/accel-pppd/libnetlink/iplink.c +++ b/accel-pppd/libnetlink/iplink.c @@ -3,6 +3,7 @@ #include <unistd.h> #include <syslog.h> #include <fcntl.h> +#include <pthread.h> #include <net/if_arp.h> #include <sys/socket.h> #include <netinet/in.h> @@ -11,17 +12,51 @@ #include <time.h> #include <sys/uio.h> -#include "libnetlink.h" -#include "iplink.h" #include "triton.h" #include "log.h" +#include "libnetlink.h" +#include "iplink.h" + +#include "memdebug.h" + struct arg { iplink_list_func func; void *arg; }; +static pthread_key_t rth_key; +static __thread struct rtnl_handle *rth; + +static void open_rth(void) +{ + rth = _malloc(sizeof(*rth)); + + if (!rth) + return; + + memset(rth, 0, sizeof(*rth)); + + if (rtnl_open(rth, 0)) { + log_ppp_error("radius: cannot open rtnetlink\n"); + _free(rth); + rth = NULL; + return; + } + + pthread_setspecific(rth_key, rth); +} + +static void free_rth(void *arg) +{ + struct rtnl_handle *rth = arg; + + rtnl_close(rth); + + _free(rth); +} + static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { struct ifinfomsg *ifi = NLMSG_DATA(n); @@ -75,3 +110,57 @@ out_err: return -1; } +int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) +{ + struct iplink_req { + struct nlmsghdr n; + struct ifinfomsg i; + char buf[4096]; + } req; + struct ifinfomsg *ifi; + int len; + struct rtattr *tb[IFLA_MAX + 1]; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 1024); + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); + req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; + req.n.nlmsg_type = RTM_GETLINK; + req.i.ifi_family = AF_PACKET; + req.i.ifi_index = ifindex; + + if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0) + return -1; + + if (req.n.nlmsg_type != RTM_NEWLINK) + return -1; + + ifi = NLMSG_DATA(&req.n); + + len = req.n.nlmsg_len; + + len -= NLMSG_LENGTH(sizeof(*ifi)); + if (len < 0) + return -1; + + parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); + if (tb[IFLA_STATS]) + memcpy(stats, RTA_DATA(tb[IFLA_STATS]), sizeof(*stats)); + else + return -1; + + return 0; +} + +static void init(void) +{ + pthread_key_create(&rth_key, free_rth); +} + +DEFINE_INIT(100, init); diff --git a/accel-pppd/libnetlink/iplink.h b/accel-pppd/libnetlink/iplink.h index 70c7c600..e67e8ed5 100644 --- a/accel-pppd/libnetlink/iplink.h +++ b/accel-pppd/libnetlink/iplink.h @@ -1,8 +1,11 @@ #ifndef __IPLINK_H #define __IPLINK_H +#include <linux/if_link.h> + typedef int (*iplink_list_func)(int index, int flags, const char *name, void *arg); int iplink_list(iplink_list_func func, void *arg); +int iplink_get_stats(int ifindex, struct rtnl_link_stats *stats); #endif |