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
00085 #include <netlink-local.h>
00086 #include <netlink-tc.h>
00087 #include <netlink/netlink.h>
00088 #include <netlink/utils.h>
00089 #include <netlink/route/link.h>
00090 #include <netlink/route/tc.h>
00091 #include <netlink/route/qdisc.h>
00092 #include <netlink/route/class.h>
00093 #include <netlink/route/classifier.h>
00094 #include <netlink/route/qdisc-modules.h>
00095
00096 static struct nl_cache_ops rtnl_qdisc_ops;
00097
00098 static int qdisc_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00099 struct nlmsghdr *n, struct nl_parser_param *pp)
00100 {
00101 int err = -ENOMEM;
00102 struct rtnl_qdisc *qdisc;
00103 struct rtnl_qdisc_ops *qops;
00104
00105 qdisc = rtnl_qdisc_alloc();
00106 if (!qdisc) {
00107 err = nl_errno(ENOMEM);
00108 goto errout;
00109 }
00110
00111 qdisc->ce_msgtype = n->nlmsg_type;
00112
00113 err = tca_msg_parser(n, (struct rtnl_tca *) qdisc);
00114 if (err < 0)
00115 goto errout_free;
00116
00117 qops = rtnl_qdisc_lookup_ops(qdisc);
00118 if (qops && qops->qo_msg_parser) {
00119 err = qops->qo_msg_parser(qdisc);
00120 if (err < 0)
00121 goto errout_free;
00122 }
00123
00124 err = pp->pp_cb((struct nl_object *) qdisc, pp);
00125 if (err < 0)
00126 goto errout_free;
00127
00128 err = P_ACCEPT;
00129
00130 errout_free:
00131 rtnl_qdisc_put(qdisc);
00132 errout:
00133 return err;
00134 }
00135
00136 static int qdisc_request_update(struct nl_cache *c, struct nl_handle *h)
00137 {
00138 struct tcmsg tchdr = {
00139 .tcm_family = AF_UNSPEC,
00140 .tcm_ifindex = c->c_iarg1,
00141 };
00142
00143 return nl_send_simple(h, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
00144 sizeof(tchdr));
00145 }
00146
00147
00148
00149
00150
00151
00152 static struct nl_msg *qdisc_build(struct rtnl_qdisc *qdisc, int type, int flags)
00153 {
00154 struct rtnl_qdisc_ops *qops;
00155 struct nl_msg *msg;
00156 int err;
00157
00158 msg = tca_build_msg((struct rtnl_tca *) qdisc, type, flags);
00159 if (!msg)
00160 goto errout;
00161
00162 qops = rtnl_qdisc_lookup_ops(qdisc);
00163 if (qops && qops->qo_get_opts) {
00164 struct nl_msg *opts;
00165
00166 opts = qops->qo_get_opts(qdisc);
00167 if (opts) {
00168 err = nla_put_nested(msg, TCA_OPTIONS, opts);
00169 nlmsg_free(opts);
00170 if (err < 0)
00171 goto errout;
00172 }
00173 }
00174
00175 return msg;
00176 errout:
00177 nlmsg_free(msg);
00178
00179 return NULL;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 struct nl_msg *rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc,
00198 int flags)
00199 {
00200 struct nl_msg *msg;
00201
00202 msg = qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags);
00203 if (!msg)
00204 nl_errno(ENOMEM);
00205
00206 return msg;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 int rtnl_qdisc_add(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
00225 int flags)
00226 {
00227 struct nl_msg *msg;
00228 int err;
00229
00230 msg = rtnl_qdisc_build_add_request(qdisc, flags);
00231 if (!msg)
00232 return nl_errno(ENOMEM);
00233
00234 err = nl_send_auto_complete(handle, msg);
00235 if (err < 0)
00236 return err;
00237
00238 nlmsg_free(msg);
00239 return nl_wait_for_ack(handle);
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 struct nl_msg *rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
00262 struct rtnl_qdisc *new)
00263 {
00264 return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE);
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 int rtnl_qdisc_change(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
00280 struct rtnl_qdisc *new)
00281 {
00282 struct nl_msg *msg;
00283 int err;
00284
00285 msg = rtnl_qdisc_build_change_request(qdisc, new);
00286 if (!msg)
00287 return nl_errno(ENOMEM);
00288
00289 err = nl_send_auto_complete(handle, msg);
00290 if (err < 0)
00291 return err;
00292
00293 nlmsg_free(msg);
00294 return nl_wait_for_ack(handle);
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 struct nl_msg *rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc)
00316 {
00317 struct nl_msg *msg;
00318 struct tcmsg tchdr;
00319 int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
00320
00321 if ((qdisc->ce_mask & required) != required)
00322 BUG();
00323
00324 msg = nlmsg_alloc_simple(RTM_DELQDISC, 0);
00325 if (!msg)
00326 return NULL;
00327
00328 tchdr.tcm_family = AF_UNSPEC,
00329 tchdr.tcm_handle = qdisc->q_handle,
00330 tchdr.tcm_parent = qdisc->q_parent,
00331 tchdr.tcm_ifindex = qdisc->q_ifindex,
00332 nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO);
00333
00334 return msg;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 int rtnl_qdisc_delete(struct nl_handle *handle, struct rtnl_qdisc *qdisc)
00349 {
00350 struct nl_msg *msg;
00351 int err;
00352
00353 msg = rtnl_qdisc_build_delete_request(qdisc);
00354 if (!msg)
00355 return nl_errno(ENOMEM);
00356
00357 err = nl_send_auto_complete(handle, msg);
00358 if (err < 0)
00359 return err;
00360
00361 nlmsg_free(msg);
00362 return nl_wait_for_ack(handle);
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 struct nl_cache * rtnl_qdisc_alloc_cache(struct nl_handle *handle)
00385 {
00386 struct nl_cache * cache;
00387
00388 cache = nl_cache_alloc(&rtnl_qdisc_ops);
00389 if (cache == NULL)
00390 return NULL;
00391
00392 if (handle && nl_cache_refill(handle, cache) < 0) {
00393 nl_cache_free(cache);
00394 return NULL;
00395 }
00396
00397 return cache;
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407 struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache,
00408 int ifindex, uint32_t parent)
00409 {
00410 struct rtnl_qdisc *q;
00411
00412 if (cache->c_ops != &rtnl_qdisc_ops)
00413 return NULL;
00414
00415 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
00416 if (q->q_parent == parent && q->q_ifindex == ifindex) {
00417 nl_object_get((struct nl_object *) q);
00418 return q;
00419 }
00420 }
00421
00422 return NULL;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432 struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache,
00433 int ifindex, uint32_t handle)
00434 {
00435 struct rtnl_qdisc *q;
00436
00437 if (cache->c_ops != &rtnl_qdisc_ops)
00438 return NULL;
00439
00440 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
00441 if (q->q_handle == handle && q->q_ifindex == ifindex) {
00442 nl_object_get((struct nl_object *) q);
00443 return q;
00444 }
00445 }
00446
00447 return NULL;
00448 }
00449
00450
00451
00452 static struct nl_cache_ops rtnl_qdisc_ops = {
00453 .co_name = "route/qdisc",
00454 .co_hdrsize = sizeof(struct tcmsg),
00455 .co_msgtypes = {
00456 { RTM_NEWQDISC, NL_ACT_NEW, "new" },
00457 { RTM_DELQDISC, NL_ACT_DEL, "del" },
00458 { RTM_GETQDISC, NL_ACT_GET, "get" },
00459 END_OF_MSGTYPES_LIST,
00460 },
00461 .co_protocol = NETLINK_ROUTE,
00462 .co_request_update = qdisc_request_update,
00463 .co_msg_parser = qdisc_msg_parser,
00464 .co_obj_ops = &qdisc_obj_ops,
00465 };
00466
00467 static void __init qdisc_init(void)
00468 {
00469 nl_cache_mngt_register(&rtnl_qdisc_ops);
00470 }
00471
00472 static void __exit qdisc_exit(void)
00473 {
00474 nl_cache_mngt_unregister(&rtnl_qdisc_ops);
00475 }
00476
00477