31 #include <netlink-private/netlink.h> 32 #include <netlink/netlink.h> 33 #include <netlink/cache.h> 34 #include <netlink/object.h> 35 #include <netlink/utils.h> 37 static 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);
138 if (ops->oo_clone(
new, obj) < 0) {
142 }
else if (size && ops->oo_free_data)
157 struct nl_object_ops *ops = obj_ops(dst);
160 return ops->oo_update(dst, src);
162 return -NLE_OPNOTSUPP;
173 struct nl_object_ops *ops;
180 if (obj->ce_refcnt > 0)
181 NL_DBG(1,
"Warning: Freeing object in use...\n");
186 if (ops->oo_free_data)
187 ops->oo_free_data(obj);
189 NL_DBG(4,
"Freed object %p\n", obj);
208 NL_DBG(4,
"New reference to object %p, total %d\n",
209 obj, obj->ce_refcnt);
222 NL_DBG(4,
"Returned object reference %p, %d remaining\n",
223 obj, obj->ce_refcnt);
225 if (obj->ce_refcnt < 0)
228 if (obj->ce_refcnt <= 0)
239 return obj->ce_refcnt > 1;
255 obj->ce_flags |= NL_OBJ_MARK;
264 obj->ce_flags &= ~NL_OBJ_MARK;
274 return (obj->ce_flags & NL_OBJ_MARK);
294 dump_from_ops(obj, params);
297 void nl_object_dump_buf(
struct nl_object *obj,
char *buf,
size_t len)
316 struct nl_object_ops *ops = obj_ops(a);
320 if (ops != obj_ops(b))
323 if (ops->oo_id_attrs_get) {
324 int req_attrs_a = ops->oo_id_attrs_get(a);
325 int req_attrs_b = ops->oo_id_attrs_get(b);
326 if (req_attrs_a != req_attrs_b)
328 req_attrs = req_attrs_a;
329 }
else if (ops->oo_id_attrs) {
330 req_attrs = ops->oo_id_attrs;
332 req_attrs = 0xFFFFFFFF;
334 if (req_attrs == 0xFFFFFFFF)
335 req_attrs = a->ce_mask & b->ce_mask;
339 if ((a->ce_mask & req_attrs) != req_attrs ||
340 (b->ce_mask & req_attrs) != req_attrs)
344 if (ops->oo_compare == NULL)
347 return !(ops->oo_compare(a, b, req_attrs, ID_COMPARISON));
364 struct nl_object_ops *ops = obj_ops(a);
366 if (ops != obj_ops(b) || ops->oo_compare == NULL)
369 return ops->oo_compare(a, b, ~0, 0);
392 return (diff & ~((uint64_t) 0xFFFFFFFF))
393 ? (uint32_t) diff | (1 << 31)
408 struct nl_object_ops *ops = obj_ops(obj);
410 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
413 return !(ops->oo_compare(obj, filter, filter->ce_mask,
430 char *buf,
size_t len)
432 struct nl_object_ops *ops = obj_ops(obj);
434 if (ops->oo_attrs2str != NULL)
435 return ops->oo_attrs2str(attrs, buf, len);
466 struct nl_object_ops *ops = obj_ops(obj);
469 ops->oo_keygen(obj, hashkey, hashtbl_sz);
491 return obj->ce_refcnt;
505 return obj->ce_cache;
521 return obj->ce_ops->oo_name;
532 return obj->ce_msgtype;
554 struct nl_object_ops *ops = obj_ops(obj);
560 if (ops->oo_id_attrs_get)
561 id_attrs = ops->oo_id_attrs_get(obj);
563 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.
uint64_t nl_object_diff64(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
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 32-bit bitmask representing difference in attribute values.