2 * lib/route/cls_api.c Classifier Object
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) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cls_obj Classifier Object
18 #include <netlink-local.h>
19 #include <netlink-tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/tc.h>
23 #include <netlink/route/classifier.h>
24 #include <netlink/route/classifier-modules.h>
25 #include <netlink/route/link.h>
28 #define CLS_ATTR_PRIO (TCA_ATTR_MAX << 1)
29 #define CLS_ATTR_PROTOCOL (TCA_ATTR_MAX << 2)
32 static void cls_free_data(struct nl_object *obj)
34 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
35 struct rtnl_cls_ops *cops;
37 tca_free_data((struct rtnl_tca *) cls);
39 cops = rtnl_cls_lookup_ops(cls);
40 if (cops && cops->co_free_data)
41 cops->co_free_data(cls);
43 nl_data_free(cls->c_subdata);
46 static int cls_clone(struct nl_object *_dst, struct nl_object *_src)
48 struct rtnl_cls *dst = nl_object_priv(_dst);
49 struct rtnl_cls *src = nl_object_priv(_src);
50 struct rtnl_cls_ops *cops;
53 err = tca_clone((struct rtnl_tca *) dst, (struct rtnl_tca *) src);
58 if (!(dst->c_subdata = nl_data_clone(src->c_subdata))) {
64 cops = rtnl_cls_lookup_ops(src);
65 if (cops && cops->co_clone)
66 err = cops->co_clone(dst, src);
71 static void cls_dump_line(struct nl_object *obj, struct nl_dump_params *p)
74 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
75 struct rtnl_cls_ops *cops;
77 tca_dump_line((struct rtnl_tca *) cls, "cls", p);
79 nl_dump(p, " prio %u protocol %s", cls->c_prio,
80 nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));
82 cops = rtnl_cls_lookup_ops(cls);
83 if (cops && cops->co_dump[NL_DUMP_LINE])
84 cops->co_dump[NL_DUMP_LINE](cls, p);
88 static void cls_dump_details(struct nl_object *obj, struct nl_dump_params *p)
90 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
91 struct rtnl_cls_ops *cops;
93 cls_dump_line(obj, p);
94 tca_dump_details((struct rtnl_tca *) cls, p);
96 cops = rtnl_cls_lookup_ops(cls);
97 if (cops && cops->co_dump[NL_DUMP_DETAILS])
98 cops->co_dump[NL_DUMP_DETAILS](cls, p);
100 nl_dump(p, "no options\n");
103 static void cls_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
105 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
106 struct rtnl_cls_ops *cops;
108 cls_dump_details(obj, p);
109 tca_dump_stats((struct rtnl_tca *) cls, p);
112 cops = rtnl_cls_lookup_ops(cls);
113 if (cops && cops->co_dump[NL_DUMP_STATS])
114 cops->co_dump[NL_DUMP_STATS](cls, p);
118 * @name Allocation/Freeing
122 struct rtnl_cls *rtnl_cls_alloc(void)
124 return (struct rtnl_cls *) nl_object_alloc(&cls_obj_ops);
127 void rtnl_cls_put(struct rtnl_cls *cls)
129 nl_object_put((struct nl_object *) cls);
140 void rtnl_cls_set_ifindex(struct rtnl_cls *f, int ifindex)
142 tca_set_ifindex((struct rtnl_tca *) f, ifindex);
145 int rtnl_cls_get_ifindex(struct rtnl_cls *cls)
147 return cls->c_ifindex;
150 void rtnl_cls_set_handle(struct rtnl_cls *f, uint32_t handle)
152 tca_set_handle((struct rtnl_tca *) f, handle);
155 void rtnl_cls_set_parent(struct rtnl_cls *f, uint32_t parent)
157 tca_set_parent((struct rtnl_tca *) f, parent);
160 uint32_t rtnl_cls_get_parent(struct rtnl_cls *cls)
162 return cls->c_parent;
165 int rtnl_cls_set_kind(struct rtnl_cls *cls, const char *kind)
167 if (cls->ce_mask & TCA_ATTR_KIND)
170 tca_set_kind((struct rtnl_tca *) cls, kind);
172 /* Force allocation of data */
178 struct rtnl_cls_ops *rtnl_cls_get_ops(struct rtnl_cls *cls)
183 void rtnl_cls_set_prio(struct rtnl_cls *cls, uint16_t prio)
186 cls->ce_mask |= CLS_ATTR_PRIO;
189 uint16_t rtnl_cls_get_prio(struct rtnl_cls *cls)
191 if (cls->ce_mask & CLS_ATTR_PRIO)
197 void rtnl_cls_set_protocol(struct rtnl_cls *cls, uint16_t protocol)
199 cls->c_protocol = protocol;
200 cls->ce_mask |= CLS_ATTR_PROTOCOL;
203 uint16_t rtnl_cls_get_protocol(struct rtnl_cls *cls)
205 if (cls->ce_mask & CLS_ATTR_PROTOCOL)
206 return cls->c_protocol;
211 void *rtnl_cls_data(struct rtnl_cls *cls)
213 if (!cls->c_subdata) {
214 struct rtnl_cls_ops *ops = cls->c_ops;
220 ops = __rtnl_cls_lookup_ops(cls->c_kind);
230 if (!(cls->c_subdata = nl_data_alloc(NULL, ops->co_size)))
234 return nl_data_get(cls->c_subdata);
239 struct nl_object_ops cls_obj_ops = {
240 .oo_name = "route/cls",
241 .oo_size = sizeof(struct rtnl_cls),
242 .oo_free_data = cls_free_data,
243 .oo_clone = cls_clone,
245 [NL_DUMP_LINE] = cls_dump_line,
246 [NL_DUMP_DETAILS] = cls_dump_details,
247 [NL_DUMP_STATS] = cls_dump_stats,
249 .oo_compare = tca_compare,
250 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),