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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #include <netlink-local.h>
00085 #include <netlink/netlink.h>
00086 #include <netlink/cache.h>
00087 #include <netlink/utils.h>
00088
00089 static int include_cb(struct nl_object *obj, struct nl_parser_param *p)
00090 {
00091 struct nl_cache_assoc *ca = p->pp_arg;
00092 struct nl_cache_ops *ops = ca->ca_cache->c_ops;
00093
00094 NL_DBG(2, "Including object %p into cache %p\n", obj, ca->ca_cache);
00095 #ifdef NL_DEBUG
00096 if (nl_debug >= 4)
00097 nl_object_dump(obj, &nl_debug_dp);
00098 #endif
00099
00100 if (ops->co_event_filter)
00101 if (ops->co_event_filter(ca->ca_cache, obj) != NL_OK)
00102 return 0;
00103
00104 return nl_cache_include(ca->ca_cache, obj, ca->ca_change, ca->ca_change_data);
00105 }
00106
00107 static int event_input(struct nl_msg *msg, void *arg)
00108 {
00109 struct nl_cache_mngr *mngr = arg;
00110 int protocol = nlmsg_get_proto(msg);
00111 int type = nlmsg_hdr(msg)->nlmsg_type;
00112 struct nl_cache_ops *ops;
00113 int i, n;
00114 struct nl_parser_param p = {
00115 .pp_cb = include_cb,
00116 };
00117
00118 NL_DBG(2, "Cache manager %p, handling new message %p as event\n",
00119 mngr, msg);
00120 #ifdef NL_DEBUG
00121 if (nl_debug >= 4)
00122 nl_msg_dump(msg, stderr);
00123 #endif
00124
00125 if (mngr->cm_protocol != protocol)
00126 BUG();
00127
00128 for (i = 0; i < mngr->cm_nassocs; i++) {
00129 if (mngr->cm_assocs[i].ca_cache) {
00130 ops = mngr->cm_assocs[i].ca_cache->c_ops;
00131 for (n = 0; ops->co_msgtypes[n].mt_id >= 0; n++)
00132 if (ops->co_msgtypes[n].mt_id == type)
00133 goto found;
00134 }
00135 }
00136
00137 return NL_SKIP;
00138
00139 found:
00140 NL_DBG(2, "Associated message %p to cache %p\n",
00141 msg, mngr->cm_assocs[i].ca_cache);
00142 p.pp_arg = &mngr->cm_assocs[i];
00143
00144 return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags,
00157 struct nl_cache_mngr **result)
00158 {
00159 struct nl_cache_mngr *mngr;
00160 int err = -NLE_NOMEM;
00161
00162 if (sk == NULL)
00163 BUG();
00164
00165 mngr = calloc(1, sizeof(*mngr));
00166 if (!mngr)
00167 goto errout;
00168
00169 mngr->cm_handle = sk;
00170 mngr->cm_nassocs = 32;
00171 mngr->cm_protocol = protocol;
00172 mngr->cm_flags = flags;
00173 mngr->cm_assocs = calloc(mngr->cm_nassocs,
00174 sizeof(struct nl_cache_assoc));
00175 if (!mngr->cm_assocs)
00176 goto errout;
00177
00178 nl_socket_modify_cb(mngr->cm_handle, NL_CB_VALID, NL_CB_CUSTOM,
00179 event_input, mngr);
00180
00181
00182 nl_socket_disable_seq_check(mngr->cm_handle);
00183
00184 if ((err = nl_connect(mngr->cm_handle, protocol) < 0))
00185 goto errout;
00186
00187 if ((err = nl_socket_set_nonblocking(mngr->cm_handle) < 0))
00188 goto errout;
00189
00190 NL_DBG(1, "Allocated cache manager %p, protocol %d, %d caches\n",
00191 mngr, protocol, mngr->cm_nassocs);
00192
00193 *result = mngr;
00194 return 0;
00195
00196 errout:
00197 nl_cache_mngr_free(mngr);
00198 return err;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name,
00218 change_func_t cb, void *data, struct nl_cache **result)
00219 {
00220 struct nl_cache_ops *ops;
00221 struct nl_cache *cache;
00222 struct nl_af_group *grp;
00223 int err, i;
00224
00225 ops = nl_cache_ops_lookup(name);
00226 if (!ops)
00227 return -NLE_NOCACHE;
00228
00229 if (ops->co_protocol != mngr->cm_protocol)
00230 return -NLE_PROTO_MISMATCH;
00231
00232 if (ops->co_groups == NULL)
00233 return -NLE_OPNOTSUPP;
00234
00235 for (i = 0; i < mngr->cm_nassocs; i++)
00236 if (mngr->cm_assocs[i].ca_cache &&
00237 mngr->cm_assocs[i].ca_cache->c_ops == ops)
00238 return -NLE_EXIST;
00239
00240 retry:
00241 for (i = 0; i < mngr->cm_nassocs; i++)
00242 if (!mngr->cm_assocs[i].ca_cache)
00243 break;
00244
00245 if (i >= mngr->cm_nassocs) {
00246 mngr->cm_nassocs += 16;
00247 mngr->cm_assocs = realloc(mngr->cm_assocs,
00248 mngr->cm_nassocs *
00249 sizeof(struct nl_cache_assoc));
00250 if (mngr->cm_assocs == NULL)
00251 return -NLE_NOMEM;
00252 else {
00253 NL_DBG(1, "Increased capacity of cache manager %p " \
00254 "to %d\n", mngr, mngr->cm_nassocs);
00255 goto retry;
00256 }
00257 }
00258
00259 cache = nl_cache_alloc(ops);
00260 if (!cache)
00261 return -NLE_NOMEM;
00262
00263 for (grp = ops->co_groups; grp->ag_group; grp++) {
00264 err = nl_socket_add_membership(mngr->cm_handle, grp->ag_group);
00265 if (err < 0)
00266 goto errout_free_cache;
00267 }
00268
00269 err = nl_cache_refill(mngr->cm_handle, cache);
00270 if (err < 0)
00271 goto errout_drop_membership;
00272
00273 mngr->cm_assocs[i].ca_cache = cache;
00274 mngr->cm_assocs[i].ca_change = cb;
00275 mngr->cm_assocs[i].ca_change_data = data;
00276
00277 if (mngr->cm_flags & NL_AUTO_PROVIDE)
00278 nl_cache_mngt_provide(cache);
00279
00280 NL_DBG(1, "Added cache %p <%s> to cache manager %p\n",
00281 cache, nl_cache_name(cache), mngr);
00282
00283 *result = cache;
00284 return 0;
00285
00286 errout_drop_membership:
00287 for (grp = ops->co_groups; grp->ag_group; grp++)
00288 nl_socket_drop_membership(mngr->cm_handle, grp->ag_group);
00289 errout_free_cache:
00290 nl_cache_free(cache);
00291
00292 return err;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 int nl_cache_mngr_get_fd(struct nl_cache_mngr *mngr)
00304 {
00305 return nl_socket_get_fd(mngr->cm_handle);
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
00324 {
00325 int ret;
00326 struct pollfd fds = {
00327 .fd = nl_socket_get_fd(mngr->cm_handle),
00328 .events = POLLIN,
00329 };
00330
00331 NL_DBG(3, "Cache manager %p, poll() fd %d\n", mngr, fds.fd);
00332 ret = poll(&fds, 1, timeout);
00333 NL_DBG(3, "Cache manager %p, poll() returned %d\n", mngr, ret);
00334 if (ret < 0)
00335 return -nl_syserr2nlerr(errno);
00336
00337 if (ret == 0)
00338 return 0;
00339
00340 return nl_cache_mngr_data_ready(mngr);
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
00355 {
00356 int err;
00357
00358 err = nl_recvmsgs_default(mngr->cm_handle);
00359 if (err < 0)
00360 return err;
00361
00362 return 1;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
00372 {
00373 int i;
00374
00375 if (!mngr)
00376 return;
00377
00378 if (mngr->cm_handle)
00379 nl_close(mngr->cm_handle);
00380
00381 for (i = 0; i < mngr->cm_nassocs; i++) {
00382 if (mngr->cm_assocs[i].ca_cache) {
00383 nl_cache_mngt_unprovide(mngr->cm_assocs[i].ca_cache);
00384 nl_cache_free(mngr->cm_assocs[i].ca_cache);
00385 }
00386 }
00387
00388 free(mngr->cm_assocs);
00389 free(mngr);
00390
00391 NL_DBG(1, "Cache manager %p freed\n", mngr);
00392 }
00393
00394