30 #include <netlink-private/netlink.h>
31 #include <netlink/netlink.h>
32 #include <netlink/cache.h>
33 #include <netlink/object.h>
34 #include <netlink/utils.h>
36 static inline struct nl_object_ops *obj_ops(
struct nl_object *obj)
56 struct nl_object *
new;
58 if (ops->oo_size <
sizeof(*
new))
61 new = calloc(1, ops->oo_size);
66 nl_init_list_head(&new->ce_list);
69 if (ops->oo_constructor)
70 ops->oo_constructor(
new);
72 NL_DBG(4,
"Allocated new object %p\n",
new);
86 struct nl_cache_ops *ops;
90 return -NLE_OPNOTSUPP;
112 struct nl_object *
new;
113 struct nl_object_ops *ops;
125 size = ops->oo_size - doff;
129 new->ce_ops = obj->ce_ops;
130 new->ce_msgtype = obj->ce_msgtype;
131 new->ce_mask = obj->ce_mask;
134 memcpy((
void *)
new + doff, (
void *)obj + doff, size);
137 if (ops->oo_clone(
new, obj) < 0) {
141 }
else if (size && ops->oo_free_data)
156 struct nl_object_ops *ops = obj_ops(dst);
159 return ops->oo_update(dst, src);
161 return -NLE_OPNOTSUPP;
172 struct nl_object_ops *ops;
179 if (obj->ce_refcnt > 0)
180 NL_DBG(1,
"Warning: Freeing object in use...\n");
185 if (ops->oo_free_data)
186 ops->oo_free_data(obj);
188 NL_DBG(4,
"Freed object %p\n", obj);
207 NL_DBG(4,
"New reference to object %p, total %d\n",
208 obj, obj->ce_refcnt);
221 NL_DBG(4,
"Returned object reference %p, %d remaining\n",
222 obj, obj->ce_refcnt);
224 if (obj->ce_refcnt < 0)
227 if (obj->ce_refcnt <= 0)
238 return obj->ce_refcnt > 1;
254 obj->ce_flags |= NL_OBJ_MARK;
263 obj->ce_flags &= ~NL_OBJ_MARK;
273 return (obj->ce_flags & NL_OBJ_MARK);
293 dump_from_ops(obj, params);
296 void nl_object_dump_buf(
struct nl_object *obj,
char *buf,
size_t len)
315 struct nl_object_ops *ops = obj_ops(a);
319 if (ops != obj_ops(b))
322 if (ops->oo_id_attrs_get) {
323 int req_attrs_a = ops->oo_id_attrs_get(a);
324 int req_attrs_b = ops->oo_id_attrs_get(b);
325 if (req_attrs_a != req_attrs_b)
327 req_attrs = req_attrs_a;
328 }
else if (ops->oo_id_attrs) {
329 req_attrs = ops->oo_id_attrs;
331 req_attrs = 0xFFFFFFFF;
333 if (req_attrs == 0xFFFFFFFF)
334 req_attrs = a->ce_mask & b->ce_mask;
338 if ((a->ce_mask & req_attrs) != req_attrs ||
339 (b->ce_mask & req_attrs) != req_attrs)
343 if (ops->oo_compare == NULL)
346 return !(ops->oo_compare(a, b, req_attrs, 0));
363 struct nl_object_ops *ops = obj_ops(a);
365 if (ops != obj_ops(b) || ops->oo_compare == NULL)
368 return ops->oo_compare(a, b, ~0, 0);
382 struct nl_object_ops *ops = obj_ops(obj);
384 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
387 return !(ops->oo_compare(obj, filter, filter->ce_mask,
404 char *buf,
size_t len)
406 struct nl_object_ops *ops = obj_ops(obj);
408 if (ops->oo_attrs2str != NULL)
409 return ops->oo_attrs2str(attrs, buf, len);
440 struct nl_object_ops *ops = obj_ops(obj);
443 ops->oo_keygen(obj, hashkey, hashtbl_sz);
465 return obj->ce_refcnt;
479 return obj->ce_cache;
495 return obj->ce_ops->oo_name;
506 return obj->ce_msgtype;
528 struct nl_object_ops *ops = obj_ops(obj);
534 if (ops->oo_id_attrs_get)
535 id_attrs = ops->oo_id_attrs_get(obj);
537 id_attrs = ops->oo_id_attrs;
char * nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
Return list of attributes present in an object.
char * dp_buf
Alternatively the output may be redirected into a buffer.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_object_alloc_name(const char *kind, struct nl_object **result)
Allocate new object of kind specified by the name.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
int nl_object_shared(struct nl_object *obj)
Check whether this object is used by multiple users.
void nl_object_unmark(struct nl_object *obj)
Remove mark from object.
struct nl_cache * nl_object_get_cache(struct nl_object *obj)
Return cache the object is associated with.
const char * nl_object_get_type(const struct nl_object *obj)
Return the object's type.
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
Return object id attribute mask.
void nl_object_free(struct nl_object *obj)
Free a cacheable object.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
int nl_object_get_msgtype(const struct nl_object *obj)
Return the netlink message type the object was derived from.
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
struct nl_object_ops * nl_object_get_ops(const struct nl_object *obj)
Return object operations structure.
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
size_t dp_buflen
Length of the buffer dp_buf.
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
void nl_object_mark(struct nl_object *obj)
Add mark to object.
int nl_object_update(struct nl_object *dst, struct nl_object *src)
Merge a cacheable object.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
int nl_object_is_marked(struct nl_object *obj)
Return true if object is marked.
int nl_object_get_refcnt(struct nl_object *obj)
Return number of references held.
void nl_object_keygen(struct nl_object *obj, uint32_t *hashkey, uint32_t hashtbl_sz)
Generate object hash key.
char * nl_object_attrs2str(struct nl_object *obj, uint32_t attrs, char *buf, size_t len)
Convert bitmask of attributes to a character string.
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.