Home | History | Annotate | Download | only in cls
      1 /*
      2  * src/cls-utils.c     Classifier Helpers
      3  *
      4  *	This library is free software; you can redistribute it and/or
      5  *	modify it under the terms of the GNU General Public License as
      6  *	published by the Free Software Foundation version 2 of the License.
      7  *
      8  * Copyright (c) 2008-2009 Thomas Graf <tgraf (at) suug.ch>
      9  */
     10 
     11 #include "utils.h"
     12 
     13 struct rtnl_cls *nlt_alloc_cls(void)
     14 {
     15 	struct rtnl_cls *cls;
     16 
     17 	cls = rtnl_cls_alloc();
     18 	if (!cls)
     19 		fatal(ENOMEM, "Unable to allocate classifier object");
     20 
     21 	return cls;
     22 }
     23 
     24 void parse_dev(struct rtnl_cls *cls, struct nl_cache *link_cache, char *arg)
     25 {
     26 	int ival;
     27 
     28 	if (!(ival = rtnl_link_name2i(link_cache, arg)))
     29 		fatal(ENOENT, "Link \"%s\" does not exist", arg);
     30 
     31 	rtnl_cls_set_ifindex(cls, ival);
     32 }
     33 
     34 void parse_prio(struct rtnl_cls *cls, char *arg)
     35 {
     36 	uint32_t prio = parse_u32(arg);
     37 	rtnl_cls_set_prio(cls, prio);
     38 }
     39 
     40 void parse_parent(struct rtnl_cls *cls, char *arg)
     41 {
     42 	uint32_t parent;
     43 	int err;
     44 
     45 	if ((err = rtnl_tc_str2handle(arg, &parent)) < 0)
     46 		fatal(err, "Unable to parse handle \"%s\": %s",
     47 		      arg, nl_geterror(err));
     48 
     49 	rtnl_cls_set_parent(cls, parent);
     50 }
     51 
     52 void parse_handle(struct rtnl_cls *cls, char *arg)
     53 {
     54 	uint32_t handle;
     55 	int err;
     56 
     57 	if ((err = rtnl_tc_str2handle(arg, &handle)) < 0)
     58 		fatal(err, "Unable to parse handle \"%s\": %s",
     59 		      arg, nl_geterror(err));
     60 
     61 	rtnl_cls_set_handle(cls, handle);
     62 }
     63 
     64 void parse_proto(struct rtnl_cls *cls, char *arg)
     65 {
     66 	int proto = nl_str2ether_proto(arg);
     67 	if (proto < 0)
     68 		fatal(proto, "Unable to parse protocol \"%s\": %s",
     69 		      arg, nl_geterror(proto));
     70 	rtnl_cls_set_protocol(cls, proto);
     71 }
     72 
     73 static NL_LIST_HEAD(cls_modules);
     74 
     75 struct cls_module *lookup_cls_mod(struct rtnl_cls_ops *ops)
     76 {
     77 	struct cls_module *mod;
     78 
     79 	nl_list_for_each_entry(mod, &cls_modules, list) {
     80 		if (mod->ops == ops)
     81 			return mod;
     82 	}
     83 
     84 	return NULL;
     85 }
     86 
     87 void register_cls_module(struct cls_module *mod)
     88 {
     89 	struct rtnl_cls_ops *ops;
     90 
     91 	if (!(ops = __rtnl_cls_lookup_ops(mod->name)))
     92 		fatal(ENOENT, "Could not locate classifier module \"%s\"",
     93 			mod->name);
     94 
     95 	if (lookup_cls_mod(ops) != NULL)
     96 		fatal(EEXIST, "Duplicate classifier module registration.");
     97 
     98 	mod->ops = ops;
     99 	nl_list_add_tail(&mod->list, &cls_modules);
    100 }
    101 
    102 void unregister_cls_module(struct cls_module *mod)
    103 {
    104 	nl_list_del(&mod->list);
    105 }
    106