24#include "nl-default.h"
26#include <netlink/netlink.h>
27#include <netlink/cache.h>
28#include <netlink/object.h>
29#include <netlink/utils.h>
32#include "nl-priv-dynamic-core/nl-core.h"
33#include "nl-priv-dynamic-core/object-api.h"
34#include "nl-priv-dynamic-core/cache-api.h"
35#include "nl-aux-core/nl-core.h"
37static inline struct nl_object_ops *obj_ops(
struct nl_object *obj)
57 struct nl_object *
new;
59 if (ops->oo_size <
sizeof(*
new))
62 new = calloc(1, ops->oo_size);
67 nl_init_list_head(&new->ce_list);
70 if (ops->oo_constructor)
71 ops->oo_constructor(
new);
73 NL_DBG(4,
"Allocated new object %p\n",
new);
87 struct nl_cache_ops *ops;
91 return -NLE_OPNOTSUPP;
113 struct nl_object *
new;
114 struct nl_object_ops *ops;
126 size = ops->oo_size - doff;
130 new->ce_ops = obj->ce_ops;
131 new->ce_msgtype = obj->ce_msgtype;
132 new->ce_mask = obj->ce_mask;
135 memcpy((
char *)
new + doff, (
char *)obj + doff, size);
143 if (ops->oo_clone(
new, obj) < 0) {
147 }
else if (size && ops->oo_free_data)
162 struct nl_object_ops *ops = obj_ops(dst);
165 return ops->oo_update(dst, src);
167 return -NLE_OPNOTSUPP;
178 struct nl_object_ops *ops;
185 if (obj->ce_refcnt > 0)
186 NL_DBG(1,
"Warning: Freeing object in use...\n");
191 if (ops->oo_free_data)
192 ops->oo_free_data(obj);
194 NL_DBG(4,
"Freed object %p\n", obj);
213 NL_DBG(4,
"New reference to object %p, total %d\n",
214 obj, obj->ce_refcnt);
227 NL_DBG(4,
"Returned object reference %p, %d remaining\n",
228 obj, obj->ce_refcnt);
230 if (obj->ce_refcnt < 0)
233 if (obj->ce_refcnt <= 0)
244 return obj->ce_refcnt > 1;
260 obj->ce_flags |= NL_OBJ_MARK;
269 obj->ce_flags &= ~NL_OBJ_MARK;
279 return (obj->ce_flags & NL_OBJ_MARK);
299 dump_from_ops(obj, params);
302void nl_object_dump_buf(
struct nl_object *obj,
char *buf,
size_t len)
321 struct nl_object_ops *ops;
322 uint64_t req_attrs_a;
323 uint64_t req_attrs_b;
330 if (ops != obj_ops(b))
334 if (ops->oo_compare == NULL)
337 if (ops->oo_id_attrs_get) {
338 req_attrs_a = ops->oo_id_attrs_get(a);
339 req_attrs_b = ops->oo_id_attrs_get(b);
340 }
else if (ops->oo_id_attrs) {
341 req_attrs_a = ops->oo_id_attrs;
342 req_attrs_b = req_attrs_a;
344 req_attrs_a = UINT64_MAX;
345 req_attrs_b = req_attrs_a;
348 req_attrs_a &= a->ce_mask;
349 req_attrs_b &= b->ce_mask;
353 if (req_attrs_a != req_attrs_b)
356 return !(ops->oo_compare(a, b, req_attrs_a, ID_COMPARISON));
373 struct nl_object_ops *ops = obj_ops(a);
375 if (ops != obj_ops(b) || ops->oo_compare == NULL)
378 return ops->oo_compare(a, b, UINT64_MAX, 0);
401 return (diff & ~((uint64_t) 0xFFFFFFFF))
402 ? (uint32_t) diff | (((uint32_t ) 1u) << 31)
417 struct nl_object_ops *ops = obj_ops(obj);
419 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
422 return !(ops->oo_compare(obj, filter, filter->ce_mask,
439 char *buf,
size_t len)
441 struct nl_object_ops *ops = obj_ops(obj);
443 if (ops->oo_attrs2str != NULL)
444 return ops->oo_attrs2str(attrs, buf, len);
475 struct nl_object_ops *ops = obj_ops(obj);
478 ops->oo_keygen(obj, hashkey, hashtbl_sz);
500 return obj->ce_refcnt;
514 return obj->ce_cache;
530 return obj->ce_ops->oo_name;
541 return obj->ce_msgtype;
563 struct nl_object_ops *ops = obj_ops(obj);
569 if (ops->oo_id_attrs_get)
570 id_attrs = ops->oo_id_attrs_get(obj);
572 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.