7 #include <netlink/route/class.h>
8 #include <netlink/route/link.h>
9 #include <netlink/cache-api.h>
10 #include <netlink/object.h>
11 #include "mirror-nl-glue.h"
13 static struct nl_cache *link_cache, *class_cache;
14 static struct rtnl_link *eth;
17 struct class_info cogent_class = { "cogent", "01:02", };
18 struct class_info orion_class = { "orion", "01:03", };
19 struct class_info campus_class = { "campus", "01:04", };
21 static struct nl_handle *nl_handle;
23 void die(const char *message) {
24 fprintf(stderr, "fatal: %s\n", message);
28 static void match_obj(struct nl_object *obj, void *arg) {
29 struct nl_object *needle = *(struct nl_object **)arg;
30 struct nl_object **ret = (struct nl_object **)arg + 1;
32 if (!*ret && nl_object_identical(obj, needle)) {
38 static struct rtnl_class *get_class_by_id(char *id, int ifindex) {
40 struct rtnl_class *needle;
41 struct nl_object *magic[2];
43 if (rtnl_tc_str2handle(id, &handle))
46 needle = rtnl_class_alloc();
47 rtnl_class_set_ifindex(needle, ifindex);
48 rtnl_class_set_handle(needle, handle);
50 magic[0] = (struct nl_object *)needle;
51 magic[1] = (struct nl_object *)NULL;
53 nl_cache_foreach(class_cache, match_obj, magic);
55 rtnl_class_put(needle);
56 return (struct rtnl_class *)magic[1];
59 uint64_t get_class_byte_count(struct class_info *info) {
60 struct rtnl_class *class = get_class_by_id(info->id, ifindex);
63 die("class not found");
64 bytes = rtnl_class_get_stat(class, RTNL_TC_BYTES);
65 rtnl_class_put(class);
69 void mirror_stats_refresh(void) {
70 nl_cache_refill(nl_handle, class_cache);
73 void mirror_stats_initialize(void) {
74 nl_handle = nl_handle_alloc();
76 die("unable to allocate handle");
78 if (nl_connect(nl_handle, NETLINK_ROUTE) < 0)
79 die("unable to connect to netlink");
81 link_cache = rtnl_link_alloc_cache(nl_handle);
83 die("unable to allocate link cache");
85 eth = rtnl_link_get_by_name(link_cache, "eth0");
87 die("unable to acquire eth0");
88 ifindex = rtnl_link_get_ifindex(eth);
90 class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
92 die("unable to allocate class cache");
95 void mirror_stats_cleanup(void) {
97 nl_cache_free(class_cache);
98 nl_cache_free(link_cache);
100 nl_handle_destroy(nl_handle);