object.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * lib/object.c Generic Cacheable Object
  3. *
  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
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup core_types
  13. * @defgroup object Object (Cacheable)
  14. *
  15. * Generic object data type, for inheritance purposes to implement cacheable
  16. * data types.
  17. *
  18. * Related sections in the development guide:
  19. *
  20. * @{
  21. *
  22. * Header
  23. * ------
  24. * ~~~~{.c}
  25. * #include <netlink/object.h>
  26. * ~~~~
  27. */
  28. #include <netlink-private/netlink.h>
  29. #include <netlink/netlink.h>
  30. #include <netlink/cache.h>
  31. #include <netlink/object.h>
  32. #include <netlink/utils.h>
  33. static inline struct nl_object_ops *obj_ops(struct nl_object *obj)
  34. {
  35. if (!obj->ce_ops)
  36. BUG();
  37. return obj->ce_ops;
  38. }
  39. /**
  40. * @name Object Creation/Deletion
  41. * @{
  42. */
  43. /**
  44. * Allocate a new object of kind specified by the operations handle
  45. * @arg ops cache operations handle
  46. * @return The new object or NULL
  47. */
  48. struct nl_object *nl_object_alloc(struct nl_object_ops *ops)
  49. {
  50. struct nl_object *new;
  51. if (ops->oo_size < sizeof(*new))
  52. BUG();
  53. new = calloc(1, ops->oo_size);
  54. if (!new)
  55. return NULL;
  56. new->ce_refcnt = 1;
  57. nl_init_list_head(&new->ce_list);
  58. new->ce_ops = ops;
  59. if (ops->oo_constructor)
  60. ops->oo_constructor(new);
  61. NL_DBG(4, "Allocated new object %p\n", new);
  62. return new;
  63. }
  64. /**
  65. * Allocate new object of kind specified by the name
  66. * @arg kind name of object type
  67. * @arg result Result pointer
  68. *
  69. * @return 0 on success or a negative error code.
  70. */
  71. int nl_object_alloc_name(const char *kind, struct nl_object **result)
  72. {
  73. struct nl_cache_ops *ops;
  74. ops = nl_cache_ops_lookup_safe(kind);
  75. if (!ops)
  76. return -NLE_OPNOTSUPP;
  77. *result = nl_object_alloc(ops->co_obj_ops);
  78. nl_cache_ops_put(ops);
  79. if (!*result)
  80. return -NLE_NOMEM;
  81. return 0;
  82. }
  83. struct nl_derived_object {
  84. NLHDR_COMMON
  85. char data;
  86. };
  87. /**
  88. * Allocate a new object and copy all data from an existing object
  89. * @arg obj object to inherite data from
  90. * @return The new object or NULL.
  91. */
  92. struct nl_object *nl_object_clone(struct nl_object *obj)
  93. {
  94. struct nl_object *new;
  95. struct nl_object_ops *ops;
  96. int doff = offsetof(struct nl_derived_object, data);
  97. int size;
  98. if (!obj)
  99. return NULL;
  100. ops = obj_ops(obj);
  101. new = nl_object_alloc(ops);
  102. if (!new)
  103. return NULL;
  104. size = ops->oo_size - doff;
  105. if (size < 0)
  106. BUG();
  107. new->ce_ops = obj->ce_ops;
  108. new->ce_msgtype = obj->ce_msgtype;
  109. new->ce_mask = obj->ce_mask;
  110. if (size)
  111. memcpy((void *)new + doff, (void *)obj + doff, size);
  112. if (ops->oo_clone) {
  113. if (ops->oo_clone(new, obj) < 0) {
  114. nl_object_free(new);
  115. return NULL;
  116. }
  117. } else if (size && ops->oo_free_data)
  118. BUG();
  119. return new;
  120. }
  121. /**
  122. * Merge a cacheable object
  123. * @arg dst object to be merged into
  124. * @arg src new object to be merged into dst
  125. *
  126. * @return 0 or a negative error code.
  127. */
  128. int nl_object_update(struct nl_object *dst, struct nl_object *src)
  129. {
  130. struct nl_object_ops *ops = obj_ops(dst);
  131. if (ops->oo_update)
  132. return ops->oo_update(dst, src);
  133. return -NLE_OPNOTSUPP;
  134. }
  135. /**
  136. * Free a cacheable object
  137. * @arg obj object to free
  138. *
  139. * @return 0 or a negative error code.
  140. */
  141. void nl_object_free(struct nl_object *obj)
  142. {
  143. struct nl_object_ops *ops;
  144. if (!obj)
  145. return;
  146. ops = obj_ops(obj);
  147. if (obj->ce_refcnt > 0)
  148. NL_DBG(1, "Warning: Freeing object in use...\n");
  149. if (obj->ce_cache)
  150. nl_cache_remove(obj);
  151. if (ops->oo_free_data)
  152. ops->oo_free_data(obj);
  153. NL_DBG(4, "Freed object %p\n", obj);
  154. free(obj);
  155. }
  156. /** @} */
  157. /**
  158. * @name Reference Management
  159. * @{
  160. */
  161. /**
  162. * Acquire a reference on a object
  163. * @arg obj object to acquire reference from
  164. */
  165. void nl_object_get(struct nl_object *obj)
  166. {
  167. obj->ce_refcnt++;
  168. NL_DBG(4, "New reference to object %p, total %d\n",
  169. obj, obj->ce_refcnt);
  170. }
  171. /**
  172. * Release a reference from an object
  173. * @arg obj object to release reference from
  174. */
  175. void nl_object_put(struct nl_object *obj)
  176. {
  177. if (!obj)
  178. return;
  179. obj->ce_refcnt--;
  180. NL_DBG(4, "Returned object reference %p, %d remaining\n",
  181. obj, obj->ce_refcnt);
  182. if (obj->ce_refcnt < 0)
  183. BUG();
  184. if (obj->ce_refcnt <= 0)
  185. nl_object_free(obj);
  186. }
  187. /**
  188. * Check whether this object is used by multiple users
  189. * @arg obj object to check
  190. * @return true or false
  191. */
  192. int nl_object_shared(struct nl_object *obj)
  193. {
  194. return obj->ce_refcnt > 1;
  195. }
  196. /** @} */
  197. /**
  198. * @name Marks
  199. * @{
  200. */
  201. /**
  202. * Add mark to object
  203. * @arg obj Object to mark
  204. */
  205. void nl_object_mark(struct nl_object *obj)
  206. {
  207. obj->ce_flags |= NL_OBJ_MARK;
  208. }
  209. /**
  210. * Remove mark from object
  211. * @arg obj Object to unmark
  212. */
  213. void nl_object_unmark(struct nl_object *obj)
  214. {
  215. obj->ce_flags &= ~NL_OBJ_MARK;
  216. }
  217. /**
  218. * Return true if object is marked
  219. * @arg obj Object to check
  220. * @return true if object is marked, otherwise false
  221. */
  222. int nl_object_is_marked(struct nl_object *obj)
  223. {
  224. return (obj->ce_flags & NL_OBJ_MARK);
  225. }
  226. /** @} */
  227. /**
  228. * @name Utillities
  229. * @{
  230. */
  231. /**
  232. * Dump this object according to the specified parameters
  233. * @arg obj object to dump
  234. * @arg params dumping parameters
  235. */
  236. void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
  237. {
  238. if (params->dp_buf)
  239. memset(params->dp_buf, 0, params->dp_buflen);
  240. dump_from_ops(obj, params);
  241. }
  242. void nl_object_dump_buf(struct nl_object *obj, char *buf, size_t len)
  243. {
  244. struct nl_dump_params dp = {
  245. .dp_buf = buf,
  246. .dp_buflen = len,
  247. };
  248. return nl_object_dump(obj, &dp);
  249. }
  250. /**
  251. * Check if the identifiers of two objects are identical
  252. * @arg a an object
  253. * @arg b another object of same type
  254. *
  255. * @return true if both objects have equal identifiers, otherwise false.
  256. */
  257. int nl_object_identical(struct nl_object *a, struct nl_object *b)
  258. {
  259. struct nl_object_ops *ops = obj_ops(a);
  260. uint32_t req_attrs;
  261. /* Both objects must be of same type */
  262. if (ops != obj_ops(b))
  263. return 0;
  264. if (ops->oo_id_attrs_get) {
  265. int req_attrs_a = ops->oo_id_attrs_get(a);
  266. int req_attrs_b = ops->oo_id_attrs_get(b);
  267. if (req_attrs_a != req_attrs_b)
  268. return 0;
  269. req_attrs = req_attrs_a;
  270. } else if (ops->oo_id_attrs) {
  271. req_attrs = ops->oo_id_attrs;
  272. } else {
  273. req_attrs = 0xFFFFFFFF;
  274. }
  275. if (req_attrs == 0xFFFFFFFF)
  276. req_attrs = a->ce_mask & b->ce_mask;
  277. /* Both objects must provide all required attributes to uniquely
  278. * identify an object */
  279. if ((a->ce_mask & req_attrs) != req_attrs ||
  280. (b->ce_mask & req_attrs) != req_attrs)
  281. return 0;
  282. /* Can't judge unless we can compare */
  283. if (ops->oo_compare == NULL)
  284. return 0;
  285. return !(ops->oo_compare(a, b, req_attrs, 0));
  286. }
  287. /**
  288. * Compute bitmask representing difference in attribute values
  289. * @arg a an object
  290. * @arg b another object of same type
  291. *
  292. * The bitmask returned is specific to an object type, each bit set represents
  293. * an attribute which mismatches in either of the two objects. Unavailability
  294. * of an attribute in one object and presence in the other is regarded a
  295. * mismatch as well.
  296. *
  297. * @return Bitmask describing differences or 0 if they are completely identical.
  298. */
  299. uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
  300. {
  301. struct nl_object_ops *ops = obj_ops(a);
  302. if (ops != obj_ops(b) || ops->oo_compare == NULL)
  303. return UINT_MAX;
  304. return ops->oo_compare(a, b, ~0, 0);
  305. }
  306. /**
  307. * Match a filter against an object
  308. * @arg obj object to check
  309. * @arg filter object of same type acting as filter
  310. *
  311. * @return 1 if the object matches the filter or 0
  312. * if no filter procedure is available or if the
  313. * filter does not match.
  314. */
  315. int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
  316. {
  317. struct nl_object_ops *ops = obj_ops(obj);
  318. if (ops != obj_ops(filter) || ops->oo_compare == NULL)
  319. return 0;
  320. return !(ops->oo_compare(obj, filter, filter->ce_mask,
  321. LOOSE_COMPARISON));
  322. }
  323. /**
  324. * Convert bitmask of attributes to a character string
  325. * @arg obj object of same type as attribute bitmask
  326. * @arg attrs bitmask of attribute types
  327. * @arg buf destination buffer
  328. * @arg len length of destination buffer
  329. *
  330. * Converts the bitmask of attribute types into a list of attribute
  331. * names separated by comas.
  332. *
  333. * @return destination buffer.
  334. */
  335. char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
  336. char *buf, size_t len)
  337. {
  338. struct nl_object_ops *ops = obj_ops(obj);
  339. if (ops->oo_attrs2str != NULL)
  340. return ops->oo_attrs2str(attrs, buf, len);
  341. else {
  342. memset(buf, 0, len);
  343. return buf;
  344. }
  345. }
  346. /**
  347. * Return list of attributes present in an object
  348. * @arg obj an object
  349. * @arg buf destination buffer
  350. * @arg len length of destination buffer
  351. *
  352. * @return destination buffer.
  353. */
  354. char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
  355. {
  356. return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
  357. }
  358. /**
  359. * Generate object hash key
  360. * @arg obj the object
  361. * @arg hashkey destination buffer to be used for key stream
  362. * @arg hashtbl_sz hash table size
  363. *
  364. * @return hash key in destination buffer
  365. */
  366. void nl_object_keygen(struct nl_object *obj, uint32_t *hashkey,
  367. uint32_t hashtbl_sz)
  368. {
  369. struct nl_object_ops *ops = obj_ops(obj);
  370. if (ops->oo_keygen)
  371. ops->oo_keygen(obj, hashkey, hashtbl_sz);
  372. else
  373. *hashkey = 0;
  374. return;
  375. }
  376. /** @} */
  377. /**
  378. * @name Attributes
  379. * @{
  380. */
  381. /**
  382. * Return number of references held
  383. * @arg obj object
  384. *
  385. * @return The number of references held to this object
  386. */
  387. int nl_object_get_refcnt(struct nl_object *obj)
  388. {
  389. return obj->ce_refcnt;
  390. }
  391. /**
  392. * Return cache the object is associated with
  393. * @arg obj object
  394. *
  395. * @note The returned pointer is not protected with a reference counter,
  396. * it is your responsibility.
  397. *
  398. * @return Pointer to cache or NULL if not associated with a cache.
  399. */
  400. struct nl_cache *nl_object_get_cache(struct nl_object *obj)
  401. {
  402. return obj->ce_cache;
  403. }
  404. /**
  405. * Return the object's type
  406. * @arg obj object
  407. *
  408. * FIXME: link to list of object types
  409. *
  410. * @return Name of the object type
  411. */
  412. const char *nl_object_get_type(const struct nl_object *obj)
  413. {
  414. if (!obj->ce_ops)
  415. BUG();
  416. return obj->ce_ops->oo_name;
  417. }
  418. /**
  419. * Return the netlink message type the object was derived from
  420. * @arg obj object
  421. *
  422. * @return Netlink message type or 0.
  423. */
  424. int nl_object_get_msgtype(const struct nl_object *obj)
  425. {
  426. return obj->ce_msgtype;
  427. }
  428. /**
  429. * Return object operations structure
  430. * @arg obj object
  431. *
  432. * @return Pointer to the object operations structure
  433. */
  434. struct nl_object_ops *nl_object_get_ops(const struct nl_object *obj)
  435. {
  436. return obj->ce_ops;
  437. }
  438. /**
  439. * Return object id attribute mask
  440. * @arg obj object
  441. *
  442. * @return object id attribute mask
  443. */
  444. uint32_t nl_object_get_id_attrs(struct nl_object *obj)
  445. {
  446. struct nl_object_ops *ops = obj_ops(obj);
  447. uint32_t id_attrs;
  448. if (!ops)
  449. return 0;
  450. if (ops->oo_id_attrs_get)
  451. id_attrs = ops->oo_id_attrs_get(obj);
  452. else
  453. id_attrs = ops->oo_id_attrs;
  454. return id_attrs;
  455. }
  456. /** @} */
  457. /** @} */