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-api.h>
00091 #include <netlink/route/qdisc.h>
00092 #include <netlink/route/class.h>
00093 #include <netlink/route/classifier.h>
00094
00095 static struct nl_cache_ops rtnl_qdisc_ops;
00096
00097 static int qdisc_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00098 struct nlmsghdr *n, struct nl_parser_param *pp)
00099 {
00100 struct rtnl_qdisc *qdisc;
00101 int err;
00102
00103 if (!(qdisc = rtnl_qdisc_alloc()))
00104 return -NLE_NOMEM;
00105
00106 if ((err = rtnl_tc_msg_parse(n, TC_CAST(qdisc))) < 0)
00107 goto errout;
00108
00109 err = pp->pp_cb(OBJ_CAST(qdisc), pp);
00110 errout:
00111 rtnl_qdisc_put(qdisc);
00112
00113 return err;
00114 }
00115
00116 static int qdisc_request_update(struct nl_cache *c, struct nl_sock *sk)
00117 {
00118 struct tcmsg tchdr = {
00119 .tcm_family = AF_UNSPEC,
00120 .tcm_ifindex = c->c_iarg1,
00121 };
00122
00123 return nl_send_simple(sk, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
00124 sizeof(tchdr));
00125 }
00126
00127
00128
00129
00130
00131
00132 static int qdisc_build(struct rtnl_qdisc *qdisc, int type, int flags,
00133 struct nl_msg **result)
00134 {
00135 return rtnl_tc_msg_build(TC_CAST(qdisc), type, flags, result);
00136
00137 #if 0
00138
00139
00140
00141 else if (qops && qops->qo_build_msg) {
00142 err = qops->qo_build_msg(qdisc, *result);
00143 if (err < 0)
00144 goto errout;
00145 }
00146 #endif
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 int rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc, int flags,
00166 struct nl_msg **result)
00167 {
00168 return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags, result);
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc,
00187 int flags)
00188 {
00189 struct nl_msg *msg;
00190 int err;
00191
00192 if ((err = rtnl_qdisc_build_add_request(qdisc, flags, &msg)) < 0)
00193 return err;
00194
00195 err = nl_send_auto_complete(sk, msg);
00196 nlmsg_free(msg);
00197 if (err < 0)
00198 return err;
00199
00200 return wait_for_ack(sk);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 int rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
00224 struct rtnl_qdisc *new,
00225 struct nl_msg **result)
00226 {
00227 return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE, result);
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 int rtnl_qdisc_change(struct nl_sock *sk, struct rtnl_qdisc *qdisc,
00243 struct rtnl_qdisc *new)
00244 {
00245 struct nl_msg *msg;
00246 int err;
00247
00248 if ((err = rtnl_qdisc_build_change_request(qdisc, new, &msg)) < 0)
00249 return err;
00250
00251 err = nl_send_auto_complete(sk, msg);
00252 nlmsg_free(msg);
00253 if (err < 0)
00254 return err;
00255
00256 return wait_for_ack(sk);
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 int rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc,
00279 struct nl_msg **result)
00280 {
00281 struct nl_msg *msg;
00282 struct tcmsg tchdr;
00283 int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
00284
00285 if ((qdisc->ce_mask & required) != required)
00286 BUG();
00287
00288 msg = nlmsg_alloc_simple(RTM_DELQDISC, 0);
00289 if (!msg)
00290 return -NLE_NOMEM;
00291
00292 tchdr.tcm_family = AF_UNSPEC;
00293 tchdr.tcm_handle = qdisc->q_handle;
00294 tchdr.tcm_parent = qdisc->q_parent;
00295 tchdr.tcm_ifindex = qdisc->q_ifindex;
00296 if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
00297 nlmsg_free(msg);
00298 return -NLE_MSGSIZE;
00299 }
00300
00301 *result = msg;
00302 return 0;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 int rtnl_qdisc_delete(struct nl_sock *sk, struct rtnl_qdisc *qdisc)
00317 {
00318 struct nl_msg *msg;
00319 int err;
00320
00321 if ((err = rtnl_qdisc_build_delete_request(qdisc, &msg)) < 0)
00322 return err;
00323
00324 err = nl_send_auto_complete(sk, msg);
00325 nlmsg_free(msg);
00326 if (err < 0)
00327 return err;
00328
00329 return wait_for_ack(sk);
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 int rtnl_qdisc_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
00351 {
00352 return nl_cache_alloc_and_fill(&rtnl_qdisc_ops, sk, result);
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362 struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache,
00363 int ifindex, uint32_t parent)
00364 {
00365 struct rtnl_qdisc *q;
00366
00367 if (cache->c_ops != &rtnl_qdisc_ops)
00368 return NULL;
00369
00370 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
00371 if (q->q_parent == parent && q->q_ifindex == ifindex) {
00372 nl_object_get((struct nl_object *) q);
00373 return q;
00374 }
00375 }
00376
00377 return NULL;
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387 struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache,
00388 int ifindex, uint32_t handle)
00389 {
00390 struct rtnl_qdisc *q;
00391
00392 if (cache->c_ops != &rtnl_qdisc_ops)
00393 return NULL;
00394
00395 nl_list_for_each_entry(q, &cache->c_items, ce_list) {
00396 if (q->q_handle == handle && q->q_ifindex == ifindex) {
00397 nl_object_get((struct nl_object *) q);
00398 return q;
00399 }
00400 }
00401
00402 return NULL;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412 struct rtnl_qdisc *rtnl_qdisc_alloc(void)
00413 {
00414 struct rtnl_tc *tc;
00415
00416 tc = TC_CAST(nl_object_alloc(&qdisc_obj_ops));
00417 if (tc)
00418 tc->tc_type = RTNL_TC_TYPE_QDISC;
00419
00420 return (struct rtnl_qdisc *) tc;
00421 }
00422
00423 void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
00424 {
00425 nl_object_put((struct nl_object *) qdisc);
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
00444 void (*cb)(struct nl_object *, void *), void *arg)
00445 {
00446 struct rtnl_class *filter;
00447
00448 filter = rtnl_class_alloc();
00449 if (!filter)
00450 return;
00451
00452 rtnl_tc_set_parent(TC_CAST(filter), qdisc->q_handle);
00453 rtnl_tc_set_ifindex(TC_CAST(filter), qdisc->q_ifindex);
00454 rtnl_tc_set_kind(TC_CAST(filter), qdisc->q_kind);
00455
00456 nl_cache_foreach_filter(cache, OBJ_CAST(filter), cb, arg);
00457
00458 rtnl_class_put(filter);
00459 }
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
00470 void (*cb)(struct nl_object *, void *), void *arg)
00471 {
00472 struct rtnl_cls *filter;
00473
00474 filter = rtnl_cls_alloc();
00475 if (!filter)
00476 return;
00477
00478 rtnl_tc_set_ifindex((struct rtnl_tc *) filter, qdisc->q_ifindex);
00479 rtnl_tc_set_parent((struct rtnl_tc *) filter, qdisc->q_parent);
00480
00481 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
00482 rtnl_cls_put(filter);
00483 }
00484
00485
00486
00487 static void qdisc_dump_details(struct rtnl_tc *tc, struct nl_dump_params *p)
00488 {
00489 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) tc;
00490
00491 nl_dump(p, "refcnt %u ", qdisc->q_info);
00492 }
00493
00494 static struct rtnl_tc_type_ops qdisc_ops = {
00495 .tt_type = RTNL_TC_TYPE_QDISC,
00496 .tt_dump_prefix = "qdisc",
00497 .tt_dump = {
00498 [NL_DUMP_DETAILS] = qdisc_dump_details,
00499 },
00500 };
00501
00502 static struct nl_cache_ops rtnl_qdisc_ops = {
00503 .co_name = "route/qdisc",
00504 .co_hdrsize = sizeof(struct tcmsg),
00505 .co_msgtypes = {
00506 { RTM_NEWQDISC, NL_ACT_NEW, "new" },
00507 { RTM_DELQDISC, NL_ACT_DEL, "del" },
00508 { RTM_GETQDISC, NL_ACT_GET, "get" },
00509 END_OF_MSGTYPES_LIST,
00510 },
00511 .co_protocol = NETLINK_ROUTE,
00512 .co_request_update = qdisc_request_update,
00513 .co_msg_parser = qdisc_msg_parser,
00514 .co_obj_ops = &qdisc_obj_ops,
00515 };
00516
00517 struct nl_object_ops qdisc_obj_ops = {
00518 .oo_name = "route/qdisc",
00519 .oo_size = sizeof(struct rtnl_qdisc),
00520 .oo_free_data = rtnl_tc_free_data,
00521 .oo_clone = rtnl_tc_clone,
00522 .oo_dump = {
00523 [NL_DUMP_LINE] = rtnl_tc_dump_line,
00524 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
00525 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
00526 },
00527 .oo_compare = rtnl_tc_compare,
00528 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
00529 };
00530
00531 static void __init qdisc_init(void)
00532 {
00533 rtnl_tc_type_register(&qdisc_ops);
00534 nl_cache_mngt_register(&rtnl_qdisc_ops);
00535 }
00536
00537 static void __exit qdisc_exit(void)
00538 {
00539 nl_cache_mngt_unregister(&rtnl_qdisc_ops);
00540 rtnl_tc_type_unregister(&qdisc_ops);
00541 }
00542
00543