24#include <netlink-private/netlink.h>
25#include <netlink/netlink.h>
26#include <netlink/cache.h>
27#include <netlink/object.h>
28#include <netlink/utils.h>
30static inline struct nl_object_ops *obj_ops(
struct nl_object *obj)
50 struct nl_object *
new;
52 if (ops->oo_size <
sizeof(*
new))
55 new = calloc(1, ops->oo_size);
60 nl_init_list_head(&new->ce_list);
63 if (ops->oo_constructor)
64 ops->oo_constructor(
new);
66 NL_DBG(4,
"Allocated new object %p\n",
new);
80 struct nl_cache_ops *ops;
84 return -NLE_OPNOTSUPP;
106 struct nl_object *
new;
107 struct nl_object_ops *ops;
119 size = ops->oo_size - doff;
123 new->ce_ops = obj->ce_ops;
124 new->ce_msgtype = obj->ce_msgtype;
125 new->ce_mask = obj->ce_mask;
128 memcpy((
char *)
new + doff, (
char *)obj + doff, size);
136 if (ops->oo_clone(
new, obj) < 0) {
140 }
else if (size && ops->oo_free_data)
155 struct nl_object_ops *ops = obj_ops(dst);
158 return ops->oo_update(dst, src);
160 return -NLE_OPNOTSUPP;
171 struct nl_object_ops *ops;
178 if (obj->ce_refcnt > 0)
179 NL_DBG(1,
"Warning: Freeing object in use...\n");
184 if (ops->oo_free_data)
185 ops->oo_free_data(obj);
187 NL_DBG(4,
"Freed object %p\n", obj);
206 NL_DBG(4,
"New reference to object %p, total %d\n",
207 obj, obj->ce_refcnt);
220 NL_DBG(4,
"Returned object reference %p, %d remaining\n",
221 obj, obj->ce_refcnt);
223 if (obj->ce_refcnt < 0)
226 if (obj->ce_refcnt <= 0)
237 return obj->ce_refcnt > 1;
253 obj->ce_flags |= NL_OBJ_MARK;
262 obj->ce_flags &= ~NL_OBJ_MARK;
272 return (obj->ce_flags & NL_OBJ_MARK);
292 dump_from_ops(obj, params);
295void nl_object_dump_buf(
struct nl_object *obj,
char *buf,
size_t len)
314 struct nl_object_ops *ops;
315 uint64_t req_attrs_a;
316 uint64_t req_attrs_b;
323 if (ops != obj_ops(b))
327 if (ops->oo_compare == NULL)
330 if (ops->oo_id_attrs_get) {
331 req_attrs_a = ops->oo_id_attrs_get(a);
332 req_attrs_b = ops->oo_id_attrs_get(b);
333 }
else if (ops->oo_id_attrs) {
334 req_attrs_a = ops->oo_id_attrs;
335 req_attrs_b = req_attrs_a;
337 req_attrs_a = UINT64_MAX;
338 req_attrs_b = req_attrs_a;
341 req_attrs_a &= a->ce_mask;
342 req_attrs_b &= b->ce_mask;
346 if (req_attrs_a != req_attrs_b)
349 return !(ops->oo_compare(a, b, req_attrs_a, ID_COMPARISON));
366 struct nl_object_ops *ops = obj_ops(a);
368 if (ops != obj_ops(b) || ops->oo_compare == NULL)
371 return ops->oo_compare(a, b, UINT64_MAX, 0);
394 return (diff & ~((uint64_t) 0xFFFFFFFF))
395 ? (uint32_t) diff | (1 << 31)
410 struct nl_object_ops *ops = obj_ops(obj);
412 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
415 return !(ops->oo_compare(obj, filter, filter->ce_mask,
432 char *buf,
size_t len)
434 struct nl_object_ops *ops = obj_ops(obj);
436 if (ops->oo_attrs2str != NULL)
437 return ops->oo_attrs2str(attrs, buf, len);
468 struct nl_object_ops *ops = obj_ops(obj);
471 ops->oo_keygen(obj, hashkey, hashtbl_sz);
493 return obj->ce_refcnt;
507 return obj->ce_cache;
523 return obj->ce_ops->oo_name;
534 return obj->ce_msgtype;
556 struct nl_object_ops *ops = obj_ops(obj);
562 if (ops->oo_id_attrs_get)
563 id_attrs = ops->oo_id_attrs_get(obj);
565 id_attrs = ops->oo_id_attrs;
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
int nl_object_alloc_name(const char *kind, struct nl_object **result)
Allocate new object of kind specified by the name.
struct nl_object_ops * nl_object_get_ops(const struct nl_object *obj)
Return object operations structure.
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute 32-bit bitmask representing difference in attribute values.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
int nl_object_update(struct nl_object *dst, struct nl_object *src)
Merge a cacheable object.
const char * nl_object_get_type(const struct nl_object *obj)
Return the object's type.
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_get_msgtype(const struct nl_object *obj)
Return the netlink message type the object was derived from.
char * nl_object_attrs2str(struct nl_object *obj, uint32_t attrs, char *buf, size_t len)
Convert bitmask of attributes to a character string.
uint64_t nl_object_diff64(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
Return object id attribute mask.
int nl_object_get_refcnt(struct nl_object *obj)
Return number of references held.
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.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
struct nl_cache * nl_object_get_cache(struct nl_object *obj)
Return cache the object is associated with.
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
char * nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
Return list of attributes present in an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
void nl_object_free(struct nl_object *obj)
Free a cacheable object.
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
int nl_object_is_marked(struct nl_object *obj)
Return true if object is marked.
void nl_object_mark(struct nl_object *obj)
Add mark to object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
void nl_object_keygen(struct nl_object *obj, uint32_t *hashkey, uint32_t hashtbl_sz)
Generate object hash key.
size_t dp_buflen
Length of the buffer dp_buf.
char * dp_buf
Alternatively the output may be redirected into a buffer.