bool TNlCgFilter::Exists(const TNlLink &link) { int ret; struct nl_cache *clsCache; ret = rtnl_cls_alloc_cache(link.GetSock(), link.GetIndex(), Parent, &clsCache); if (ret < 0) { L_ERR() << "Can't allocate filter cache: " << nl_geterror(ret) << std::endl; return false; } link.LogCache(clsCache); struct CgFilterIter { uint32_t parent; uint32_t handle; bool exists; } data = { Parent, Handle, false }; nl_cache_foreach(clsCache, [](struct nl_object *obj, void *data) { CgFilterIter *p = (CgFilterIter *)data; if (rtnl_tc_get_handle(TC_CAST(obj)) == p->handle && rtnl_tc_get_parent(TC_CAST(obj)) == p->parent) p->exists = true; }, &data); nl_cache_free(clsCache); return data.exists; }
bool TNlHtb::Valid(const TNlLink &link, uint32_t defaultClass) { int ret; struct nl_cache *qdiscCache; bool valid = true; ret = rtnl_qdisc_alloc_cache(link.GetSock(), &qdiscCache); if (ret < 0) { L_ERR() << "can't alloc qdisc cache" << std::endl; return false; } struct rtnl_qdisc *qdisc = rtnl_qdisc_get(qdiscCache, link.GetIndex(), Handle); if (qdisc) { link.Dump("found", qdisc); if (rtnl_tc_get_ifindex(TC_CAST(qdisc)) != link.GetIndex()) valid = false; else if (rtnl_tc_get_parent(TC_CAST(qdisc)) != Parent) valid = false; else if (rtnl_tc_get_handle(TC_CAST(qdisc)) != Handle) valid = false; else if (rtnl_tc_get_kind(TC_CAST(qdisc)) != std::string("htb")) valid = false; else if (rtnl_htb_get_defcls(qdisc) != TC_H_MIN(defaultClass)) valid = false; } else { valid = false; } rtnl_qdisc_put(qdisc); nl_cache_free(qdiscCache); return valid; }
static void __dump_link(int ifindex, struct rtnl_cls *filter) { struct nl_cache *cache; uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter); cache = nl_cli_cls_alloc_cache(sock, ifindex, parent); nl_cache_dump_filter(cache, ¶ms, OBJ_CAST(filter)); nl_cache_free(cache); }
static void __delete_link(int ifindex, struct rtnl_cls *filter) { struct nl_cache *cache; uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter); cache = nl_cli_cls_alloc_cache(sock, ifindex, parent); nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL); nl_cache_free(cache); }
Result<ingress::Discipline> decode<ingress::Discipline>( const Netlink<struct rtnl_qdisc>& qdisc) { if (rtnl_tc_get_kind(TC_CAST(qdisc.get())) != string("ingress") || rtnl_tc_get_parent(TC_CAST(qdisc.get())) != INGRESS_ROOT.get() || rtnl_tc_get_handle(TC_CAST(qdisc.get())) != ingress::HANDLE.get()) { return None(); } return ingress::Discipline(); }
static void list_qdisc(struct nl_object *obj, void *arg) { struct rtnl_qdisc *qdisc = nl_object_priv(obj); struct rtnl_tc *tc = (struct rtnl_tc *) qdisc; nl_object_dump(obj, ¶ms); list_cls(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc)); if (rtnl_tc_get_parent(tc) == TC_H_ROOT) { list_cls(rtnl_tc_get_ifindex(tc), TC_H_ROOT); list_classes(rtnl_tc_get_ifindex(tc), TC_H_ROOT); } list_classes(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc)); }
void nl_cli_tc_parse_handle(struct rtnl_tc *tc, char *arg, int create) { uint32_t handle, parent; int err; parent = rtnl_tc_get_parent(tc); if ((err = rtnl_tc_str2handle(arg, &handle)) < 0) { if (err == -NLE_OBJ_NOTFOUND && create) err = rtnl_classid_generate(arg, &handle, parent); if (err < 0) nl_cli_fatal(err, "Unable to parse handle \"%s\": %s", arg, nl_geterror(err)); } rtnl_tc_set_handle(tc, handle); }
bool TNlQdisc::Check(const TNl &nl) { struct nl_cache *qdiscCache; bool result = false; int ret; ret = rtnl_qdisc_alloc_cache(nl.GetSock(), &qdiscCache); if (ret < 0) { L_ERR() << nl.Error(ret, "cannot alloc qdisc cache") << std::endl; return false; } struct rtnl_qdisc *qdisc = rtnl_qdisc_get(qdiscCache, Index, Handle); if (!qdisc) { result = Kind == ""; goto out; } nl.Dump("found", qdisc); if (rtnl_tc_get_ifindex(TC_CAST(qdisc)) != Index) goto out; if (rtnl_tc_get_parent(TC_CAST(qdisc)) != Parent) goto out; if (rtnl_tc_get_handle(TC_CAST(qdisc)) != Handle) goto out; if (rtnl_tc_get_kind(TC_CAST(qdisc)) != Kind) goto out; if (Kind == "htb") { if (rtnl_htb_get_defcls(qdisc) != Default) goto out; } result = true; out: rtnl_qdisc_put(qdisc); nl_cache_free(qdiscCache); return result; }
static void update_tc_infos(struct element *e, struct rtnl_tc *tc) { char buf[64]; snprintf(buf, sizeof(buf), "%u", rtnl_tc_get_mtu(tc)); element_update_info(e, "MTU", buf); snprintf(buf, sizeof(buf), "%u", rtnl_tc_get_mpu(tc)); element_update_info(e, "MPU", buf); snprintf(buf, sizeof(buf), "%u", rtnl_tc_get_overhead(tc)); element_update_info(e, "Overhead", buf); snprintf(buf, sizeof(buf), "%#x", rtnl_tc_get_handle(tc)); element_update_info(e, "Id", buf); snprintf(buf, sizeof(buf), "%#x", rtnl_tc_get_parent(tc)); element_update_info(e, "Parent", buf); }
int main(int argc, char *argv[]) { struct nl_sock *sock; struct rtnl_cls *cls; struct rtnl_tc *tc; struct nl_cache *link_cache; struct nl_dump_params dp = { .dp_type = NL_DUMP_DETAILS, .dp_fd = stdout, }; struct nl_cli_tc_module *tm; struct rtnl_tc_ops *ops; int err, flags = NLM_F_CREATE | NLM_F_EXCL; char *kind, *id = NULL; sock = nl_cli_alloc_socket(); nl_cli_connect(sock, NETLINK_ROUTE); link_cache = nl_cli_link_alloc_cache(sock); cls = nl_cli_cls_alloc(); tc = (struct rtnl_tc *) cls; for (;;) { int c, optidx = 0; enum { ARG_UPDATE = 257, ARG_UPDATE_ONLY = 258, ARG_MTU, ARG_MPU, ARG_OVERHEAD, ARG_LINKTYPE, ARG_PROTO, ARG_PRIO, }; static struct option long_opts[] = { { "quiet", 0, 0, 'q' }, { "help", 0, 0, 'h' }, { "version", 0, 0, 'v' }, { "dev", 1, 0, 'd' }, { "parent", 1, 0, 'p' }, { "id", 1, 0, 'i' }, { "proto", 1, 0, ARG_PROTO }, { "prio", 1, 0, ARG_PRIO }, { "update", 0, 0, ARG_UPDATE }, { "update-only", 0, 0, ARG_UPDATE_ONLY }, { "mtu", 1, 0, ARG_MTU }, { "mpu", 1, 0, ARG_MPU }, { "overhead", 1, 0, ARG_OVERHEAD }, { "linktype", 1, 0, ARG_LINKTYPE }, { 0, 0, 0, 0 } }; c = getopt_long(argc, argv, "+qhvd:p:i:", long_opts, &optidx); if (c == -1) break; switch (c) { case 'q': quiet = 1; break; case 'h': print_usage(); break; case 'v': nl_cli_print_version(); break; case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break; case 'p': nl_cli_tc_parse_parent(tc, optarg); break; case 'i': id = strdup(optarg); break; case ARG_UPDATE: flags = NLM_F_CREATE; break; case ARG_UPDATE_ONLY: flags = 0; break; case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break; case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break; case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break; case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break; case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break; case ARG_PRIO: rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg)); break; } } if (optind >= argc) print_usage(); if (!rtnl_tc_get_ifindex(tc)) nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)"); if (!rtnl_tc_get_parent(tc)) nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)"); if (id) { nl_cli_tc_parse_handle(tc, id, 1); free(id); } kind = argv[optind++]; rtnl_tc_set_kind(tc, kind); if (!(ops = rtnl_tc_get_ops(tc))) nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind); if (!(tm = nl_cli_tc_lookup(ops))) nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind); tm->tm_parse_argv(tc, argc, argv); if (!quiet) { printf("Adding "); nl_object_dump(OBJ_CAST(cls), &dp); } if ((err = rtnl_cls_add(sock, cls, flags)) < 0) nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err)); return 0; }
static void handle_class(struct nl_object *obj, void *arg) { struct rtnl_tc *tc = (struct rtnl_tc *) obj; struct element *e; struct rdata *rdata = arg; struct rdata ndata = { .level = rdata->level + 1, }; if (!(e = handle_tc_obj(tc, "class", rdata))) return; ndata.parent = e; if (!strcmp(rtnl_tc_get_kind(tc), "htb")) element_set_txmax(e, rtnl_htb_get_rate((struct rtnl_class *) tc)); find_classes(rtnl_tc_get_handle(tc), &ndata); find_qdiscs(rtnl_tc_get_handle(tc), &ndata); } static void find_qdiscs(uint32_t parent, struct rdata *rdata) { struct rtnl_qdisc *filter; if (!(filter = rtnl_qdisc_alloc())) return; rtnl_tc_set_parent((struct rtnl_tc *) filter, parent); nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(filter), handle_qdisc, rdata); rtnl_qdisc_put(filter); } static void find_cls(int ifindex, uint32_t parent, struct rdata *rdata) { struct nl_cache *cls_cache; if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0) return; nl_cache_foreach(cls_cache, handle_cls, rdata); nl_cache_free(cls_cache); } static void find_classes(uint32_t parent, struct rdata *rdata) { struct rtnl_class *filter; if (!(filter = rtnl_class_alloc())) return; rtnl_tc_set_parent((struct rtnl_tc *) filter, parent); nl_cache_foreach_filter(class_cache, OBJ_CAST(filter), handle_class, rdata); rtnl_class_put(filter); } static void handle_qdisc(struct nl_object *obj, void *arg) { struct rtnl_tc *tc = (struct rtnl_tc *) obj; struct element *e; struct rdata *rdata = arg; struct rdata ndata = { .level = rdata->level + 1, }; if (!(e = handle_tc_obj(tc, "qdisc", rdata))) return; ndata.parent = e; find_cls(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc), &ndata); if (rtnl_tc_get_parent(tc) == TC_H_ROOT) { find_cls(rtnl_tc_get_ifindex(tc), TC_H_ROOT, &ndata); find_classes(TC_H_ROOT, &ndata); } find_classes(rtnl_tc_get_handle(tc), &ndata); } static void handle_tc(struct element *e, struct rtnl_link *link) { struct rtnl_qdisc *qdisc; int ifindex = rtnl_link_get_ifindex(link); struct rdata rdata = { .level = 1, .parent = e, }; if (rtnl_class_alloc_cache(sock, ifindex, &class_cache) < 0) return; qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } nl_cache_free(class_cache); } static void update_link_infos(struct element *e, struct rtnl_link *link) { char buf[64]; snprintf(buf, sizeof(buf), "%u", rtnl_link_get_mtu(link)); element_update_info(e, "MTU", buf); rtnl_link_flags2str(rtnl_link_get_flags(link), buf, sizeof(buf)); element_update_info(e, "Flags", buf); rtnl_link_operstate2str(rtnl_link_get_operstate(link), buf, sizeof(buf)); element_update_info(e, "Operstate", buf); snprintf(buf, sizeof(buf), "%u", rtnl_link_get_ifindex(link)); element_update_info(e, "IfIndex", buf); nl_addr2str(rtnl_link_get_addr(link), buf, sizeof(buf)); element_update_info(e, "Address", buf); nl_addr2str(rtnl_link_get_broadcast(link), buf, sizeof(buf)); element_update_info(e, "Broadcast", buf); rtnl_link_mode2str(rtnl_link_get_linkmode(link), buf, sizeof(buf)); element_update_info(e, "Mode", buf); snprintf(buf, sizeof(buf), "%u", rtnl_link_get_txqlen(link)); element_update_info(e, "TXQlen", buf); nl_af2str(rtnl_link_get_family(link), buf, sizeof(buf)); element_update_info(e, "Family", buf); element_update_info(e, "Alias", rtnl_link_get_ifalias(link) ? : ""); element_update_info(e, "Qdisc", rtnl_link_get_qdisc(link) ? : ""); if (rtnl_link_get_link(link)) { snprintf(buf, sizeof(buf), "%u", rtnl_link_get_link(link)); element_update_info(e, "SlaveOfIndex", buf); } } static void do_link(struct nl_object *obj, void *arg) { struct rtnl_link *link = (struct rtnl_link *) obj; struct element *e, *e_parent = NULL; int i, master_ifindex; if (!cfg_show_all && !(rtnl_link_get_flags(link) & IFF_UP)) { /* FIXME: delete element */ return; } /* Check if the interface is a slave of another interface */ if ((master_ifindex = rtnl_link_get_link(link))) { char parent[IFNAMSIZ+1]; rtnl_link_i2name(link_cache, master_ifindex, parent, sizeof(parent)); e_parent = element_lookup(grp, parent, master_ifindex, NULL, 0); } if (!(e = element_lookup(grp, rtnl_link_get_name(link), rtnl_link_get_ifindex(link), e_parent, ELEMENT_CREAT))) return; if (e->e_flags & ELEMENT_FLAG_CREATED) { if (e->e_parent) e->e_level = e->e_parent->e_level + 1; if (element_set_key_attr(e, "bytes", "packets") || element_set_usage_attr(e, "bytes")) BUG(); /* FIXME: Update link infos every 1s or so */ update_link_infos(e, link); e->e_flags &= ~ELEMENT_FLAG_CREATED; } for (i = 0; i < ARRAY_SIZE(link_attrs); i++) { struct attr_map *m = &link_attrs[i]; uint64_t c_rx = 0, c_tx = 0; int flags = 0; if (m->rxid >= 0) { c_rx = rtnl_link_get_stat(link, m->rxid); flags |= UPDATE_FLAG_RX; } if (m->txid >= 0) { c_tx = rtnl_link_get_stat(link, m->txid); flags |= UPDATE_FLAG_TX; } attr_update(e, m->attrid, c_rx, c_tx, flags); } if (!c_notc) handle_tc(e, link); element_notify_update(e, NULL); element_lifesign(e, 1); } static void netlink_read(void) { int err; if ((err = nl_cache_resync(sock, link_cache, NULL, NULL)) < 0) { fprintf(stderr, "Unable to resync link cache: %s\n", nl_geterror(err)); goto disable; } if ((err = nl_cache_resync(sock, qdisc_cache, NULL, NULL)) < 0) { fprintf(stderr, "Unable to resync qdisc cache: %s\n", nl_geterror(err)); goto disable; } nl_cache_foreach(link_cache, do_link, NULL); return; disable: netlink_ops.m_flags &= ~BMON_MODULE_ENABLED; } static void netlink_shutdown(void) { nl_cache_free(link_cache); nl_cache_free(qdisc_cache); nl_socket_free(sock); }