libnl  1.1
cls_obj.c
1 /*
2  * lib/route/cls_api.c Classifier Object
3  *
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
7  * of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup cls
14  * @defgroup cls_obj Classifier Object
15  * @{
16  */
17 
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>
26 
27 /** @cond SKIP */
28 #define CLS_ATTR_PRIO (TCA_ATTR_MAX << 1)
29 #define CLS_ATTR_PROTOCOL (TCA_ATTR_MAX << 2)
30 /** @endcond */
31 
32 static void cls_free_data(struct nl_object *obj)
33 {
34  struct rtnl_cls *cls = (struct rtnl_cls *) obj;
35  struct rtnl_cls_ops *cops;
36 
37  tca_free_data((struct rtnl_tca *) cls);
38 
39  cops = rtnl_cls_lookup_ops(cls);
40  if (cops && cops->co_free_data)
41  cops->co_free_data(cls);
42 }
43 
44 static int cls_clone(struct nl_object *_dst, struct nl_object *_src)
45 {
46  struct rtnl_cls *dst = nl_object_priv(_dst);
47  struct rtnl_cls *src = nl_object_priv(_src);
48  struct rtnl_cls_ops *cops;
49  int err;
50 
51  err = tca_clone((struct rtnl_tca *) dst, (struct rtnl_tca *) src);
52  if (err < 0)
53  goto errout;
54 
55  cops = rtnl_cls_lookup_ops(src);
56  if (cops && cops->co_clone)
57  err = cops->co_clone(dst, src);
58 errout:
59  return err;
60 }
61 
62 static int cls_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
63 {
64  char buf[32];
65  struct rtnl_cls *cls = (struct rtnl_cls *) obj;
66  struct rtnl_cls_ops *cops;
67  int line;
68 
69  line = tca_dump_brief((struct rtnl_tca *) cls, "cls", p, 0);
70 
71  dp_dump(p, " prio %u protocol %s", cls->c_prio,
72  nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));
73 
74  cops = rtnl_cls_lookup_ops(cls);
75  if (cops && cops->co_dump[NL_DUMP_BRIEF])
76  line = cops->co_dump[NL_DUMP_BRIEF](cls, p, line);
77  dp_dump(p, "\n");
78 
79  return line;
80 }
81 
82 static int cls_dump_full(struct nl_object *obj, struct nl_dump_params *p)
83 {
84  struct rtnl_cls *cls = (struct rtnl_cls *) obj;
85  struct rtnl_cls_ops *cops;
86  int line;
87 
88  line = cls_dump_brief(obj, p);
89  line = tca_dump_full((struct rtnl_tca *) cls, p, line);
90 
91  cops = rtnl_cls_lookup_ops(cls);
92  if (cops && cops->co_dump[NL_DUMP_FULL])
93  line = cops->co_dump[NL_DUMP_FULL](cls, p, line);
94  else
95  dp_dump(p, "no options\n");
96 
97  return line;
98 }
99 
100 static int cls_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
101 {
102  struct rtnl_cls *cls = (struct rtnl_cls *) obj;
103  struct rtnl_cls_ops *cops;
104  int line;
105 
106  line = cls_dump_full(obj, p);
107  line = tca_dump_stats((struct rtnl_tca *) cls, p, line);
108  dp_dump(p, "\n");
109 
110  cops = rtnl_cls_lookup_ops(cls);
111  if (cops && cops->co_dump[NL_DUMP_STATS])
112  line = cops->co_dump[NL_DUMP_STATS](cls, p, line);
113 
114  return line;
115 }
116 
117 /**
118  * @name Allocation/Freeing
119  * @{
120  */
121 
122 struct rtnl_cls *rtnl_cls_alloc(void)
123 {
124  return (struct rtnl_cls *) nl_object_alloc(&cls_obj_ops);
125 }
126 
127 void rtnl_cls_put(struct rtnl_cls *cls)
128 {
129  nl_object_put((struct nl_object *) cls);
130 }
131 
132 /** @} */
133 
134 
135 /**
136  * @name Attributes
137  * @{
138  */
139 
140 void rtnl_cls_set_ifindex(struct rtnl_cls *f, int ifindex)
141 {
142  tca_set_ifindex((struct rtnl_tca *) f, ifindex);
143 }
144 
145 void rtnl_cls_set_handle(struct rtnl_cls *f, uint32_t handle)
146 {
147  tca_set_handle((struct rtnl_tca *) f, handle);
148 }
149 
150 void rtnl_cls_set_parent(struct rtnl_cls *f, uint32_t parent)
151 {
152  tca_set_parent((struct rtnl_tca *) f, parent);
153 }
154 
155 void rtnl_cls_set_kind(struct rtnl_cls *f, const char *kind)
156 {
157  tca_set_kind((struct rtnl_tca *) f, kind);
158  f->c_ops = __rtnl_cls_lookup_ops(kind);
159 }
160 
161 void rtnl_cls_set_prio(struct rtnl_cls *cls, int prio)
162 {
163  cls->c_prio = prio;
164  cls->ce_mask |= CLS_ATTR_PRIO;
165 }
166 
167 int rtnl_cls_get_prio(struct rtnl_cls *cls)
168 {
169  if (cls->ce_mask & CLS_ATTR_PRIO)
170  return cls->c_prio;
171  else
172  return 0;
173 }
174 
175 void rtnl_cls_set_protocol(struct rtnl_cls *cls, int protocol)
176 {
177  cls->c_protocol = protocol;
178  cls->ce_mask |= CLS_ATTR_PROTOCOL;
179 }
180 
181 int rtnl_cls_get_protocol(struct rtnl_cls *cls)
182 {
183  if (cls->ce_mask & CLS_ATTR_PROTOCOL)
184  return cls->c_protocol;
185  else
186  return ETH_P_ALL;
187 }
188 
189 /** @} */
190 
191 struct nl_object_ops cls_obj_ops = {
192  .oo_name = "route/cls",
193  .oo_size = sizeof(struct rtnl_cls),
194  .oo_free_data = cls_free_data,
195  .oo_clone = cls_clone,
196  .oo_dump[NL_DUMP_BRIEF] = cls_dump_brief,
197  .oo_dump[NL_DUMP_FULL] = cls_dump_full,
198  .oo_dump[NL_DUMP_STATS] = cls_dump_stats,
199  .oo_compare = tca_compare,
200  .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
201 };
202 
203 /** @} */
Dump object in a brief one-liner.
Definition: types.h:22
int(* co_clone)(struct rtnl_cls *, struct rtnl_cls *)
Called whenever a classifier object needs to be cloned.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:42
struct rtnl_cls_ops * rtnl_cls_lookup_ops(struct rtnl_cls *cls)
Lookup classifier operations for a classifier object.
Definition: cls_api.c:92
int(* co_dump[NL_DUMP_MAX+1])(struct rtnl_cls *, struct nl_dump_params *, int)
Dump callbacks.
Object Operations.
Definition: object-api.h:254
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:173
Classifier operations.
Dumping parameters.
Definition: types.h:36
void(* co_free_data)(struct rtnl_cls *)
Called before a class object gets destroyed.
char * oo_name
Unique name of object type.
Definition: object-api.h:261
Dump all attributes but no statistics.
Definition: types.h:23
Dump all attributes including statistics.
Definition: types.h:24