00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <netlink-local.h>
00044 #include <netlink/netlink.h>
00045 #include <netlink/cache.h>
00046 #include <netlink/object.h>
00047 #include <netlink/utils.h>
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 int nl_cache_nitems(struct nl_cache *cache)
00059 {
00060 return cache->c_nitems;
00061 }
00062
00063
00064
00065
00066
00067
00068 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
00069 {
00070 struct nl_object *obj;
00071 int nitems = 0;
00072
00073 if (cache->c_ops == NULL)
00074 BUG();
00075
00076 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00077 if (filter && !nl_object_match_filter(obj, filter))
00078 continue;
00079
00080 nitems++;
00081 }
00082
00083 return nitems;
00084 }
00085
00086
00087
00088
00089
00090
00091 int nl_cache_is_empty(struct nl_cache *cache)
00092 {
00093 return nl_list_empty(&cache->c_items);
00094 }
00095
00096
00097
00098
00099
00100 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
00101 {
00102 return cache->c_ops;
00103 }
00104
00105
00106
00107
00108
00109 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
00110 {
00111 if (nl_list_empty(&cache->c_items))
00112 return NULL;
00113
00114 return nl_list_entry(cache->c_items.next,
00115 struct nl_object, ce_list);
00116 }
00117
00118
00119
00120
00121
00122 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
00123 {
00124 if (nl_list_empty(&cache->c_items))
00125 return NULL;
00126
00127 return nl_list_entry(cache->c_items.prev,
00128 struct nl_object, ce_list);
00129 }
00130
00131
00132
00133
00134
00135 struct nl_object *nl_cache_get_next(struct nl_object *obj)
00136 {
00137 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
00138 return NULL;
00139 else
00140 return nl_list_entry(obj->ce_list.next,
00141 struct nl_object, ce_list);
00142 }
00143
00144
00145
00146
00147
00148 struct nl_object *nl_cache_get_prev(struct nl_object *obj)
00149 {
00150 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
00151 return NULL;
00152 else
00153 return nl_list_entry(obj->ce_list.prev,
00154 struct nl_object, ce_list);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
00171 {
00172 struct nl_cache *cache;
00173
00174 cache = calloc(1, sizeof(*cache));
00175 if (!cache) {
00176 nl_errno(ENOMEM);
00177 return NULL;
00178 }
00179
00180 nl_init_list_head(&cache->c_items);
00181 cache->c_ops = ops;
00182 cache->c_refcnt = 1;
00183
00184 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
00185
00186 return cache;
00187 }
00188
00189
00190
00191
00192
00193
00194 struct nl_cache *nl_cache_alloc_name(const char *kind)
00195 {
00196 struct nl_cache_ops *ops;
00197
00198 ops = nl_cache_ops_lookup(kind);
00199 if (!ops) {
00200 nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
00201 return NULL;
00202 }
00203
00204 return nl_cache_alloc(ops);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 struct nl_cache *nl_cache_subset(struct nl_cache *orig,
00214 struct nl_object *filter)
00215 {
00216 struct nl_cache *cache;
00217 struct nl_object *obj;
00218
00219 if (!filter)
00220 BUG();
00221
00222 cache = nl_cache_alloc(orig->c_ops);
00223 if (!cache)
00224 return NULL;
00225
00226 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
00227 if (!nl_object_match_filter(obj, filter))
00228 continue;
00229
00230 nl_cache_add(cache, obj);
00231 }
00232
00233 return cache;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242 void nl_cache_clear(struct nl_cache *cache)
00243 {
00244 struct nl_object *obj, *tmp;
00245
00246 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
00247
00248 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
00249 nl_cache_remove(obj);
00250 }
00251
00252 static void __nl_cache_free(struct nl_cache *cache)
00253 {
00254 nl_cache_clear(cache);
00255
00256 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
00257 free(cache);
00258 }
00259
00260
00261
00262
00263
00264 void nl_cache_get(struct nl_cache *cache)
00265 {
00266 cache->c_refcnt++;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 void nl_cache_free(struct nl_cache *cache)
00278 {
00279 if (!cache)
00280 return;
00281
00282 cache->c_refcnt--;
00283 NL_DBG(4, "Returned cache reference %p, %d remaining\n",
00284 cache, cache->c_refcnt);
00285
00286 if (cache->c_refcnt <= 0)
00287 __nl_cache_free(cache);
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
00298 {
00299 obj->ce_cache = cache;
00300
00301 nl_list_add_tail(&obj->ce_list, &cache->c_items);
00302 cache->c_nitems++;
00303
00304 NL_DBG(1, "Added %p to cache %p <%s>.\n",
00305 obj, cache, nl_cache_name(cache));
00306
00307 return 0;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
00321 {
00322 struct nl_object *new;
00323
00324 if (cache->c_ops->co_obj_ops != obj->ce_ops)
00325 return nl_error(EINVAL, "Object mismatches cache type");
00326
00327 if (!nl_list_empty(&obj->ce_list)) {
00328 new = nl_object_clone(obj);
00329 if (!new)
00330 return nl_errno(ENOMEM);
00331 } else {
00332 nl_object_get(obj);
00333 new = obj;
00334 }
00335
00336 return __cache_add(cache, new);
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
00350 {
00351 if (cache->c_ops->co_obj_ops != obj->ce_ops)
00352 return nl_error(EINVAL, "Object mismatches cache type");
00353
00354 NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
00355
00356
00357
00358 nl_object_get(obj);
00359
00360 if (!nl_list_empty(&obj->ce_list))
00361 nl_cache_remove(obj);
00362
00363 return __cache_add(cache, obj);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 void nl_cache_remove(struct nl_object *obj)
00375 {
00376 struct nl_cache *cache = obj->ce_cache;
00377
00378 if (cache == NULL)
00379 return;
00380
00381 nl_list_del(&obj->ce_list);
00382 obj->ce_cache = NULL;
00383 nl_object_put(obj);
00384 cache->c_nitems--;
00385
00386 NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
00387 obj, cache, nl_cache_name(cache));
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 struct nl_object *nl_cache_search(struct nl_cache *cache,
00402 struct nl_object *needle)
00403 {
00404 struct nl_object *obj;
00405
00406 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00407 if (nl_object_identical(obj, needle)) {
00408 nl_object_get(obj);
00409 return obj;
00410 }
00411 }
00412
00413 return NULL;
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 int nl_cache_request_full_dump(struct nl_handle *handle, struct nl_cache *cache)
00436 {
00437 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
00438 cache, nl_cache_name(cache));
00439
00440 if (cache->c_ops->co_request_update == NULL)
00441 return nl_error(EOPNOTSUPP, "Operation not supported");
00442
00443 return cache->c_ops->co_request_update(cache, handle);
00444 }
00445
00446
00447 struct update_xdata {
00448 struct nl_cache_ops *ops;
00449 struct nl_parser_param *params;
00450 };
00451
00452 static int update_msg_parser(struct nl_msg *msg, void *arg)
00453 {
00454 struct update_xdata *x = arg;
00455
00456 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
00457 }
00458
00459
00460 int __cache_pickup(struct nl_handle *handle, struct nl_cache *cache,
00461 struct nl_parser_param *param)
00462 {
00463 int err;
00464 struct nl_cb *cb;
00465 struct update_xdata x = {
00466 .ops = cache->c_ops,
00467 .params = param,
00468 };
00469
00470 NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
00471 cache, nl_cache_name(cache));
00472
00473 cb = nl_cb_clone(handle->h_cb);
00474 if (cb == NULL)
00475 return nl_get_errno();
00476
00477 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
00478
00479 err = nl_recvmsgs(handle, cb);
00480 if (err < 0)
00481 NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
00482 "%d: %s", cache, nl_cache_name(cache),
00483 err, nl_geterror());
00484
00485 nl_cb_put(cb);
00486
00487 return err;
00488 }
00489
00490 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
00491 {
00492 return nl_cache_add((struct nl_cache *) p->pp_arg, c);
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505 int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
00506 {
00507 struct nl_parser_param p = {
00508 .pp_cb = pickup_cb,
00509 .pp_arg = cache,
00510 };
00511
00512 return __cache_pickup(handle, cache, &p);
00513 }
00514
00515 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
00516 struct nl_msgtype *type, change_func_t cb)
00517 {
00518 struct nl_object *old;
00519
00520 switch (type->mt_act) {
00521 case NL_ACT_NEW:
00522 case NL_ACT_DEL:
00523 old = nl_cache_search(cache, obj);
00524 if (old) {
00525 nl_cache_remove(old);
00526 if (type->mt_act == NL_ACT_DEL) {
00527 if (cb)
00528 cb(cache, old, NL_ACT_DEL);
00529 nl_object_put(old);
00530 }
00531 }
00532
00533 if (type->mt_act == NL_ACT_NEW) {
00534 nl_cache_move(cache, obj);
00535 if (old == NULL && cb)
00536 cb(cache, obj, NL_ACT_NEW);
00537 else if (old) {
00538 if (nl_object_diff(old, obj) && cb)
00539 cb(cache, obj, NL_ACT_CHANGE);
00540
00541 nl_object_put(old);
00542 }
00543 }
00544 break;
00545 default:
00546 NL_DBG(2, "Unknown action associated to object %p\n", obj);
00547 return 0;
00548 }
00549
00550 return 0;
00551 }
00552
00553 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
00554 change_func_t change_cb)
00555 {
00556 struct nl_cache_ops *ops = cache->c_ops;
00557 int i;
00558
00559 if (ops->co_obj_ops != obj->ce_ops)
00560 return nl_error(EINVAL, "Object mismatches cache type");
00561
00562 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
00563 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
00564 return cache_include(cache, obj, &ops->co_msgtypes[i],
00565 change_cb);
00566
00567 return nl_errno(EINVAL);
00568 }
00569
00570 static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
00571 {
00572 struct nl_cache_assoc *ca = p->pp_arg;
00573
00574 return nl_cache_include(ca->ca_cache, c, ca->ca_change);
00575 }
00576
00577 int nl_cache_resync(struct nl_handle *handle, struct nl_cache *cache,
00578 change_func_t change_cb)
00579 {
00580 struct nl_object *obj, *next;
00581 struct nl_cache_assoc ca = {
00582 .ca_cache = cache,
00583 .ca_change = change_cb,
00584 };
00585 struct nl_parser_param p = {
00586 .pp_cb = resync_cb,
00587 .pp_arg = &ca,
00588 };
00589 int err;
00590
00591 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
00592
00593
00594 nl_cache_mark_all(cache);
00595
00596 err = nl_cache_request_full_dump(handle, cache);
00597 if (err < 0)
00598 goto errout;
00599
00600 err = __cache_pickup(handle, cache, &p);
00601 if (err < 0)
00602 goto errout;
00603
00604 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
00605 if (nl_object_is_marked(obj))
00606 nl_cache_remove(obj);
00607
00608 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
00609
00610 err = 0;
00611 errout:
00612 return err;
00613 }
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00624 struct nlmsghdr *nlh, struct nl_parser_param *params)
00625 {
00626 int i, err;
00627
00628 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) {
00629 err = nl_error(EINVAL, "netlink message too short to be "
00630 "of kind %s", ops->co_name);
00631 goto errout;
00632 }
00633
00634 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
00635 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
00636 err = ops->co_msg_parser(ops, who, nlh, params);
00637 if (err != -ENOENT)
00638 goto errout;
00639 }
00640 }
00641
00642
00643 err = nl_error(EINVAL, "Unsupported netlink message type %d",
00644 nlh->nlmsg_type);
00645 errout:
00646 return err;
00647 }
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
00661 {
00662 struct nl_parser_param p = {
00663 .pp_cb = pickup_cb,
00664 .pp_arg = cache,
00665 };
00666
00667 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
00668 }
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
00681 {
00682 int err;
00683
00684 err = nl_cache_request_full_dump(handle, cache);
00685 if (err < 0)
00686 return err;
00687
00688 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
00689 cache, nl_cache_name(cache));
00690 nl_cache_clear(cache);
00691
00692 return nl_cache_pickup(handle, cache);
00693 }
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706 void nl_cache_mark_all(struct nl_cache *cache)
00707 {
00708 struct nl_object *obj;
00709
00710 NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
00711 cache, nl_cache_name(cache));
00712
00713 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
00714 nl_object_mark(obj);
00715 }
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
00732 {
00733 nl_cache_dump_filter(cache, params, NULL);
00734 }
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 void nl_cache_dump_filter(struct nl_cache *cache,
00746 struct nl_dump_params *params,
00747 struct nl_object *filter)
00748 {
00749 int type = params ? params->dp_type : NL_DUMP_FULL;
00750 struct nl_object_ops *ops;
00751 struct nl_object *obj;
00752
00753 NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
00754 cache, nl_cache_name(cache), filter);
00755
00756 if (type > NL_DUMP_MAX || type < 0)
00757 BUG();
00758
00759 if (cache->c_ops == NULL)
00760 BUG();
00761
00762 ops = cache->c_ops->co_obj_ops;
00763 if (!ops->oo_dump[type])
00764 return;
00765
00766 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
00767 if (filter && !nl_object_match_filter(obj, filter))
00768 continue;
00769
00770 NL_DBG(4, "Dumping object %p...\n", obj);
00771 dump_from_ops(obj, params);
00772 }
00773 }
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 void nl_cache_foreach(struct nl_cache *cache,
00792 void (*cb)(struct nl_object *, void *), void *arg)
00793 {
00794 nl_cache_foreach_filter(cache, NULL, cb, arg);
00795 }
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
00809 void (*cb)(struct nl_object *, void *), void *arg)
00810 {
00811 struct nl_object *obj, *tmp;
00812
00813 if (cache->c_ops == NULL)
00814 BUG();
00815
00816 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
00817 if (filter && !nl_object_match_filter(obj, filter))
00818 continue;
00819
00820 cb(obj, arg);
00821 }
00822 }
00823
00824
00825
00826