2 * netlink/object-api.c Object API
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-2007 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_OBJECT_API_H_
13 #define NETLINK_OBJECT_API_H_
15 #include <netlink/netlink.h>
16 #include <netlink/utils.h>
24 * @defgroup object_api Object API
27 * @par 1) Object Definition
29 * // Define your object starting with the common object header
35 * // Fill out the object operations structure
36 * struct nl_object_ops my_ops = {
37 * .oo_name = "my_obj",
38 * .oo_size = sizeof(struct my_obj),
41 * // At this point the object can be allocated, you may want to provide a
42 * // separate _alloc() function to ease allocting objects of this kind.
43 * struct nl_object *obj = nl_object_alloc(&my_ops);
45 * // And release it again...
49 * @par 2) Allocating additional data
51 * // You may require to allocate additional data and store it inside
52 * // object, f.e. assuming there is a field `ptr'.
58 * // And at some point you may assign allocated data to this field:
59 * my_obj->ptr = calloc(1, ...);
61 * // In order to not introduce any memory leaks you have to release
62 * // this data again when the last reference is given back.
63 * static void my_obj_free_data(struct nl_object *obj)
65 * struct my_obj *my_obj = nl_object_priv(obj);
70 * // Also when the object is cloned, you must ensure for your pointer
71 * // stay valid even if one of the clones is freed by either making
72 * // a clone as well or increase the reference count.
73 * static int my_obj_clone(struct nl_object *src, struct nl_object *dst)
75 * struct my_obj *my_src = nl_object_priv(src);
76 * struct my_obj *my_dst = nl_object_priv(dst);
79 * dst->ptr = calloc(1, ...);
80 * memcpy(dst->ptr, src->ptr, ...);
84 * struct nl_object_ops my_ops = {
86 * .oo_free_data = my_obj_free_data,
87 * .oo_clone = my_obj_clone,
91 * @par 3) Object Dumping
93 * static int my_obj_dump_detailed(struct nl_object *obj,
94 * struct nl_dump_params *params)
96 * struct my_obj *my_obj = nl_object_priv(obj);
97 * int line = 1; // We will print at least one line for sure
99 * // It is absolutely essential to use nl_dump() when printing
100 * // any text to make sure the dumping parameters are respected.
101 * nl_dump(params, "Obj Integer: %d\n", my_obj->my_int);
103 * // Before we can dump the next line, make sure to prefix
104 * // this line correctly.
105 * nl_new_line(params, line++);
107 * // You may also split a line into multiple nl_dump() calls.
108 * nl_dump(params, "String: %s ", my_obj->my_string);
109 * nl_dump(params, "String-2: %s\n", my_obj->another_string);
111 * // Return the number of lines dumped
115 * struct nl_object_ops my_ops = {
117 * .oo_dump[NL_DUMP_FULL] = my_obj_dump_detailed,
121 * @par 4) Object Attributes
123 * // The concept of object attributes is optional but can ease the typical
124 * // case of objects that have optional attributes, e.g. a route may have a
125 * // nexthop assigned but it is not required to.
127 * // The first step to define your object specific bitmask listing all
129 * #define MY_ATTR_FOO (1<<0)
130 * #define MY_ATTR_BAR (1<<1)
132 * // When assigning an optional attribute to the object, make sure
133 * // to mark its availability.
134 * my_obj->foo = 123123;
135 * my_obj->ce_mask |= MY_ATTR_FOO;
137 * // At any time you may use this mask to check for the availability
138 * // of the attribute, e.g. while dumping
139 * if (my_obj->ce_mask & MY_ATTR_FOO)
140 * nl_dump(params, "foo %d ", my_obj->foo);
142 * // One of the big advantages of this concept is that it allows for
143 * // standardized comparisons which make it trivial for caches to
144 * // identify unique objects by use of unified comparison functions.
145 * // In order for it to work, your object implementation must provide
146 * // a comparison function and define a list of attributes which
147 * // combined together make an object unique.
149 * static int my_obj_compare(struct nl_object *_a, struct nl_object *_b,
150 * uint32_t attrs, int flags)
152 * struct my_obj *a = nl_object_priv(_a):
153 * struct my_obj *b = nl_object_priv(_b):
156 * // We help ourselves in defining our own DIFF macro which will
157 * // call ATTR_DIFF() on both objects which will make sure to only
158 * // compare the attributes if required.
159 * #define MY_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, MY_ATTR_##ATTR, a, b, EXPR)
161 * // Call our own diff macro for each attribute to build a bitmask
162 * // representing the attributes which mismatch.
163 * diff |= MY_DIFF(FOO, a->foo != b->foo)
164 * diff |= MY_DIFF(BAR, strcmp(a->bar, b->bar))
169 * // In order to identify identical objects with differing attributes
170 * // you must specify the attributes required to uniquely identify
171 * // your object. Make sure to not include too many attributes, this
172 * // list is used when caches look for an old version of an object.
173 * struct nl_object_ops my_ops = {
175 * .oo_id_attrs = MY_ATTR_FOO,
176 * .oo_compare = my_obj_compare,
183 * Common Object Header
185 * This macro must be included as first member in every object
186 * definition to allow objects to be cached.
188 #define NLHDR_COMMON \
190 struct nl_object_ops * ce_ops; \
191 struct nl_cache * ce_cache; \
192 struct nl_list_head ce_list; \
198 * Return true if attribute is available in both objects
200 * @arg B another object
201 * @arg ATTR attribute bit
203 * @return True if the attribute is available, otherwise false is returned.
205 #define AVAILABLE(A, B, ATTR) (((A)->ce_mask & (B)->ce_mask) & (ATTR))
208 * Return true if attributes mismatch
210 * @arg B another object
211 * @arg ATTR attribute bit
212 * @arg EXPR Comparison expression
214 * This function will check if the attribute in question is available
215 * in both objects, if not this will count as a mismatch.
217 * If available the function will execute the expression which must
218 * return true if the attributes mismatch.
220 * @return True if the attribute mismatch, or false if they match.
222 #define ATTR_MISMATCH(A, B, ATTR, EXPR) (!AVAILABLE(A, B, ATTR) || (EXPR))
225 * Return attribute bit if attribute does not match
226 * @arg LIST list of attributes to be compared
227 * @arg ATTR attribute bit
229 * @arg B another object
230 * @arg EXPR Comparison expression
232 * This function will check if the attribute in question is available
233 * in both objects, if not this will count as a mismatch.
235 * If available the function will execute the expression which must
236 * return true if the attributes mismatch.
238 * In case the attributes mismatch, the attribute is returned, otherwise
242 * diff |= ATTR_DIFF(attrs, MY_ATTR_FOO, a, b, a->foo != b->foo);
245 #define ATTR_DIFF(LIST, ATTR, A, B, EXPR) \
247 if (((LIST) & (ATTR)) && ATTR_MISMATCH(A, B, ATTR, EXPR)) \
257 * Unique name of object type
259 * Must be in the form family/name, e.g. "route/addr"
263 /** Size of object including its header */
266 /* List of attributes needed to uniquely identify the object */
267 uint32_t oo_id_attrs;
270 * Constructor function
272 * Will be called when a new object of this type is allocated.
273 * Can be used to initialize members such as lists etc.
275 void (*oo_constructor)(struct nl_object *);
278 * Destructor function
280 * Will be called when an object is freed. Must free all
281 * resources which may have been allocated as part of this
284 void (*oo_free_data)(struct nl_object *);
289 * Will be called when an object needs to be cloned. Please
290 * note that the generic object code will make an exact
291 * copy of the object first, therefore you only need to take
292 * care of members which require reference counting etc.
294 * May return a negative error code to abort cloning.
296 int (*oo_clone)(struct nl_object *, struct nl_object *);
301 * Will be called when an object is dumped. The implementations
302 * have to use nl_dump(), nl_dump_line(), and nl_new_line() to
305 * The functions must return the number of lines printed.
307 int (*oo_dump[NL_DUMP_MAX+1])(struct nl_object *,
308 struct nl_dump_params *);
311 * Comparison function
313 * Will be called when two objects of the same type are
314 * compared. It takes the two objects in question, an object
315 * specific bitmask defining which attributes should be
316 * compared and flags to control the behaviour.
318 * The function must return a bitmask with the relevant bit
319 * set for each attribute that mismatches.
321 int (*oo_compare)(struct nl_object *, struct nl_object *,
325 char *(*oo_attrs2str)(int, char *, size_t);