2 * lib/route/cls/basic.c Basic Classifier
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
14 * @defgroup basic Basic Classifier
17 * The basic classifier is the simplest form of a classifier. It does
18 * not have any special classification capabilities, instead it can be
19 * used to classify exclusively based on extended matches or to
20 * create a "catch-all" filter.
25 #include <netlink-local.h>
26 #include <netlink-tc.h>
27 #include <netlink/netlink.h>
28 #include <netlink/route/classifier.h>
29 #include <netlink/route/classifier-modules.h>
30 #include <netlink/route/cls/basic.h>
31 #include <netlink/route/cls/ematch.h>
36 struct rtnl_ematch_tree * b_ematch;
41 #define BASIC_ATTR_CLASSID 0x001
42 #define BASIC_ATTR_EMATCH 0x002
45 static struct nla_policy basic_policy[TCA_FW_MAX+1] = {
46 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
47 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
48 [TCA_BASIC_ACT] = { .type = NLA_NESTED },
49 [TCA_BASIC_POLICE] = { .type = NLA_NESTED },
52 static int basic_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
54 return -NLE_OPNOTSUPP;
57 static void basic_free_data(struct rtnl_cls *cls)
59 struct rtnl_basic *basic = rtnl_cls_data(cls);
61 rtnl_ematch_tree_free(basic->b_ematch);
64 static int basic_msg_parser(struct rtnl_cls *cls)
66 struct nlattr *tb[TCA_BASIC_MAX + 1];
67 struct rtnl_basic *basic = rtnl_cls_data(cls);
70 err = tca_parse(tb, TCA_BASIC_MAX, (struct rtnl_tca *) cls, basic_policy);
74 if (tb[TCA_BASIC_CLASSID]) {
75 basic->b_classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
76 basic->b_mask |= BASIC_ATTR_CLASSID;
79 if (tb[TCA_BASIC_EMATCHES]) {
80 if ((err = rtnl_ematch_parse(tb[TCA_BASIC_EMATCHES],
81 &basic->b_ematch)) < 0)
85 basic->b_mask |= BASIC_ATTR_EMATCH;
88 if (tb[TCA_BASIC_ACT]) {
92 if (tb[TCA_BASIC_POLICE]) {
99 static void basic_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
101 struct rtnl_basic *b = rtnl_cls_data(cls);
104 if (b->b_mask & BASIC_ATTR_EMATCH)
105 nl_dump(p, " ematch");
107 nl_dump(p, " match-all");
109 if (b->b_mask & BASIC_ATTR_CLASSID)
110 nl_dump(p, " classify-to %s",
111 rtnl_tc_handle2str(b->b_classid, buf, sizeof(buf)));
114 static void basic_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
116 struct rtnl_basic *b = rtnl_cls_data(cls);
118 if (b->b_mask & BASIC_ATTR_EMATCH) {
120 nl_dump_line(p, " ematch ");
121 rtnl_ematch_tree_dump(b->b_ematch, p);
123 nl_dump(p, "no options.\n");
126 static int basic_get_opts(struct rtnl_cls *cls, struct nl_msg *msg)
128 struct rtnl_basic *b = rtnl_cls_data(cls);
130 if (!(b->b_mask & BASIC_ATTR_CLASSID))
131 return -NLE_MISSING_ATTR;
133 NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_classid);
142 * @name Attribute Modifications
146 int rtnl_basic_set_classid(struct rtnl_cls *cls, uint32_t classid)
148 struct rtnl_basic *b = rtnl_cls_data(cls);
150 b->b_classid = classid;
151 b->b_mask |= BASIC_ATTR_CLASSID;
156 uint32_t rtnl_basic_get_classid(struct rtnl_cls *cls)
158 struct rtnl_basic *b = rtnl_cls_data(cls);
163 int rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
165 struct rtnl_basic *b = rtnl_cls_data(cls);
168 rtnl_ematch_tree_free(b->b_ematch);
169 b->b_mask &= ~BASIC_ATTR_EMATCH;
175 b->b_mask |= BASIC_ATTR_EMATCH;
180 struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
182 struct rtnl_basic *b = rtnl_cls_data(cls);
188 static struct rtnl_cls_ops basic_ops = {
190 .co_size = sizeof(struct rtnl_basic),
191 .co_msg_parser = basic_msg_parser,
192 .co_clone = basic_clone,
193 .co_free_data = basic_free_data,
194 .co_get_opts = basic_get_opts,
196 [NL_DUMP_LINE] = basic_dump_line,
197 [NL_DUMP_DETAILS] = basic_dump_details,
201 static void __init basic_init(void)
203 rtnl_cls_register(&basic_ops);
206 static void __exit basic_exit(void)
208 rtnl_cls_unregister(&basic_ops);