diff options
author | Thomas Graf <tgraf@suug.ch> | 2010-10-26 20:30:02 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2010-10-26 20:30:02 (GMT) |
commit | b57a697ef1053a70264e9bedea3d2b98b8b24019 (patch) | |
tree | cc5b8f508e85ab3d8a5a44002eb381766df58503 /src/lib/cls.c | |
parent | 4c6d1c5dfb4f7e4a9392895f3da709b55c970e02 (diff) | |
download | libnl-b57a697ef1053a70264e9bedea3d2b98b8b24019.zip libnl-b57a697ef1053a70264e9bedea3d2b98b8b24019.tar.gz libnl-b57a697ef1053a70264e9bedea3d2b98b8b24019.tar.bz2 |
nl-cls-* tools
cli based tools to add/update/list/delete classifiers
Diffstat (limited to 'src/lib/cls.c')
-rw-r--r-- | src/lib/cls.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/lib/cls.c b/src/lib/cls.c new file mode 100644 index 0000000..95997f5 --- /dev/null +++ b/src/lib/cls.c @@ -0,0 +1,115 @@ +/* + * src/lib/cls.c CLI Classifier Helpers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch> + */ + +/** + * @ingroup cli + * @defgroup cli_cls Classifiers + * @{ + */ + +#include <netlink/cli/utils.h> +#include <netlink/cli/cls.h> + +struct rtnl_cls *nl_cli_cls_alloc(void) +{ + struct rtnl_cls *cls; + + cls = rtnl_cls_alloc(); + if (!cls) + nl_cli_fatal(ENOMEM, "Unable to allocate classifier object"); + + return cls; +} + +struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex, + uint32_t parent) +{ + struct nl_cache *cache; + int err; + + if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0) + nl_cli_fatal(err, "Unable to allocate classifier cache: %s", + nl_geterror(err)); + + return cache; +} + +void nl_cli_cls_parse_kind(struct rtnl_cls *cls, char *arg) +{ + rtnl_cls_set_kind(cls, arg); +} + +void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg) +{ + int proto; + + if ((proto = nl_str2ether_proto(arg)) < 0) + nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg); + + rtnl_cls_set_protocol(cls, proto); +} + +static NL_LIST_HEAD(cls_modules); + +struct nl_cli_cls_module *__nl_cli_cls_lookup(struct rtnl_cls_ops *ops) +{ + struct nl_cli_cls_module *cm; + + nl_list_for_each_entry(cm, &cls_modules, cm_list) + if (cm->cm_ops == ops) + return cm; + + return NULL; +} + +struct nl_cli_cls_module *nl_cli_cls_lookup(struct rtnl_cls_ops *ops) +{ + struct nl_cli_cls_module *cm; + + if ((cm = __nl_cli_cls_lookup(ops))) + return cm; + + nl_cli_load_module("cli/cls", ops->co_kind); + + if (!(cm = __nl_cli_cls_lookup(ops))) { + nl_cli_fatal(EINVAL, "Application bug: The shared library for " + "the classifier \"%s\" was successfully loaded but it " + "seems that module did not register itself"); + } + + return cm; +} + +void nl_cli_cls_register(struct nl_cli_cls_module *cm) +{ + struct rtnl_cls_ops *ops; + + if (!(ops = __rtnl_cls_lookup_ops(cm->cm_name))) { + nl_cli_fatal(ENOENT, "Unable to register CLI classifier module " + "\"%s\": No matching libnl cls module found.", cm->cm_name); + } + + if (__nl_cli_cls_lookup(ops)) { + nl_cli_fatal(EEXIST, "Unable to register CLI classifier module " + "\"%s\": Module already registered.", cm->cm_name); + } + + cm->cm_ops = ops; + + nl_list_add_tail(&cm->cm_list, &cls_modules); +} + +void nl_cli_cls_unregister(struct nl_cli_cls_module *cm) +{ + nl_list_del(&cm->cm_list); +} + +/** @} */ |