1 /* 2 * lib/route/qdisc_obj.c Queueing Discipline 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-2006 Thomas Graf <tgraf (at) suug.ch> 10 */ 11 12 /** 13 * @ingroup qdisc 14 * @defgroup qdisc_obj Queueing Discipline Object 15 * @{ 16 */ 17 18 #include <netlink-local.h> 19 #include <netlink-tc.h> 20 #include <netlink/netlink.h> 21 #include <netlink/utils.h> 22 #include <netlink/route/link.h> 23 #include <netlink/route/tc.h> 24 #include <netlink/route/qdisc.h> 25 #include <netlink/route/class.h> 26 #include <netlink/route/classifier.h> 27 #include <netlink/route/qdisc-modules.h> 28 29 static void qdisc_free_data(struct nl_object *obj) 30 { 31 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj; 32 struct rtnl_qdisc_ops *qops; 33 34 tca_free_data((struct rtnl_tca *) qdisc); 35 36 qops = rtnl_qdisc_lookup_ops(qdisc); 37 if (qops && qops->qo_free_data) 38 qops->qo_free_data(qdisc); 39 } 40 41 static int qdisc_clone(struct nl_object *_dst, struct nl_object *_src) 42 { 43 struct rtnl_qdisc *dst = (struct rtnl_qdisc *) _dst; 44 struct rtnl_qdisc *src = (struct rtnl_qdisc *) _src; 45 struct rtnl_qdisc_ops *qops; 46 int err; 47 48 err = tca_clone((struct rtnl_tca *) dst, (struct rtnl_tca *) src); 49 if (err < 0) 50 goto errout; 51 52 qops = rtnl_qdisc_lookup_ops(src); 53 if (qops && qops->qo_clone) 54 err = qops->qo_clone(dst, src); 55 errout: 56 return err; 57 } 58 59 static void qdisc_dump_line(struct nl_object *obj, struct nl_dump_params *p) 60 { 61 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj; 62 struct rtnl_qdisc_ops *qops; 63 64 tca_dump_line((struct rtnl_tca *) qdisc, "qdisc", p); 65 66 qops = rtnl_qdisc_lookup_ops(qdisc); 67 if (qops && qops->qo_dump[NL_DUMP_LINE]) 68 qops->qo_dump[NL_DUMP_LINE](qdisc, p); 69 70 nl_dump(p, "\n"); 71 } 72 73 static void qdisc_dump_details(struct nl_object *arg, struct nl_dump_params *p) 74 { 75 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg; 76 struct rtnl_qdisc_ops *qops; 77 78 qdisc_dump_line(arg, p); 79 80 tca_dump_details((struct rtnl_tca *) qdisc, p); 81 nl_dump(p, "refcnt %u ", qdisc->q_info); 82 83 qops = rtnl_qdisc_lookup_ops(qdisc); 84 if (qops && qops->qo_dump[NL_DUMP_DETAILS]) 85 qops->qo_dump[NL_DUMP_DETAILS](qdisc, p); 86 87 nl_dump(p, "\n"); 88 } 89 90 static void qdisc_dump_stats(struct nl_object *arg, struct nl_dump_params *p) 91 { 92 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg; 93 struct rtnl_qdisc_ops *qops; 94 95 qdisc_dump_details(arg, p); 96 tca_dump_stats((struct rtnl_tca *) qdisc, p); 97 nl_dump(p, "\n"); 98 99 qops = rtnl_qdisc_lookup_ops(qdisc); 100 if (qops && qops->qo_dump[NL_DUMP_STATS]) 101 qops->qo_dump[NL_DUMP_STATS](qdisc, p); 102 } 103 104 /** 105 * @name Allocation/Freeing 106 * @{ 107 */ 108 109 struct rtnl_qdisc *rtnl_qdisc_alloc(void) 110 { 111 return (struct rtnl_qdisc *) nl_object_alloc(&qdisc_obj_ops); 112 } 113 114 void rtnl_qdisc_put(struct rtnl_qdisc *qdisc) 115 { 116 nl_object_put((struct nl_object *) qdisc); 117 } 118 119 /** @} */ 120 121 /** 122 * @name Iterators 123 * @{ 124 */ 125 126 /** 127 * Call a callback for each child class of a qdisc 128 * @arg qdisc the parent qdisc 129 * @arg cache a class cache including all classes of the interface 130 * the specified qdisc is attached to 131 * @arg cb callback function 132 * @arg arg argument to be passed to callback function 133 */ 134 void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache, 135 void (*cb)(struct nl_object *, void *), void *arg) 136 { 137 struct rtnl_class *filter; 138 139 filter = rtnl_class_alloc(); 140 if (!filter) 141 return; 142 143 rtnl_class_set_parent(filter, qdisc->q_handle); 144 rtnl_class_set_ifindex(filter, qdisc->q_ifindex); 145 rtnl_class_set_kind(filter, qdisc->q_kind); 146 147 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg); 148 149 rtnl_class_put(filter); 150 } 151 152 /** 153 * Call a callback for each filter attached to the qdisc 154 * @arg qdisc the parent qdisc 155 * @arg cache a filter cache including at least all the filters 156 * attached to the specified qdisc 157 * @arg cb callback function 158 * @arg arg argument to be passed to callback function 159 */ 160 void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache, 161 void (*cb)(struct nl_object *, void *), void *arg) 162 { 163 struct rtnl_cls *filter; 164 165 filter = rtnl_cls_alloc(); 166 if (!filter) 167 return; 168 169 rtnl_cls_set_ifindex(filter, qdisc->q_ifindex); 170 rtnl_cls_set_parent(filter, qdisc->q_parent); 171 172 nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg); 173 rtnl_cls_put(filter); 174 } 175 176 /** @} */ 177 178 /** 179 * @name Attributes 180 * @{ 181 */ 182 183 void rtnl_qdisc_set_ifindex(struct rtnl_qdisc *qdisc, int ifindex) 184 { 185 tca_set_ifindex((struct rtnl_tca *) qdisc, ifindex); 186 } 187 188 int rtnl_qdisc_get_ifindex(struct rtnl_qdisc *qdisc) 189 { 190 return tca_get_ifindex((struct rtnl_tca *) qdisc); 191 } 192 193 void rtnl_qdisc_set_handle(struct rtnl_qdisc *qdisc, uint32_t handle) 194 { 195 tca_set_handle((struct rtnl_tca *) qdisc, handle); 196 } 197 198 uint32_t rtnl_qdisc_get_handle(struct rtnl_qdisc *qdisc) 199 { 200 return tca_get_handle((struct rtnl_tca *) qdisc); 201 } 202 203 void rtnl_qdisc_set_parent(struct rtnl_qdisc *qdisc, uint32_t parent) 204 { 205 tca_set_parent((struct rtnl_tca *) qdisc, parent); 206 } 207 208 uint32_t rtnl_qdisc_get_parent(struct rtnl_qdisc *qdisc) 209 { 210 return tca_get_parent((struct rtnl_tca *) qdisc); 211 } 212 213 void rtnl_qdisc_set_kind(struct rtnl_qdisc *qdisc, const char *name) 214 { 215 tca_set_kind((struct rtnl_tca *) qdisc, name); 216 qdisc->q_ops = __rtnl_qdisc_lookup_ops(name); 217 } 218 219 char *rtnl_qdisc_get_kind(struct rtnl_qdisc *qdisc) 220 { 221 return tca_get_kind((struct rtnl_tca *) qdisc); 222 } 223 224 uint64_t rtnl_qdisc_get_stat(struct rtnl_qdisc *qdisc, 225 enum rtnl_tc_stats_id id) 226 { 227 return tca_get_stat((struct rtnl_tca *) qdisc, id); 228 } 229 230 /** @} */ 231 232 /** 233 * @name Qdisc Specific Options 234 * @{ 235 */ 236 237 /** 238 * Return qdisc specific options for use in TCA_OPTIONS 239 * @arg qdisc qdisc carrying the optiosn 240 * 241 * @return new headerless netlink message carrying the options as payload 242 */ 243 struct nl_msg *rtnl_qdisc_get_opts(struct rtnl_qdisc *qdisc) 244 { 245 struct rtnl_qdisc_ops *ops; 246 247 ops = rtnl_qdisc_lookup_ops(qdisc); 248 if (ops && ops->qo_get_opts) 249 return ops->qo_get_opts(qdisc); 250 251 return NULL; 252 } 253 254 /** @} */ 255 256 struct nl_object_ops qdisc_obj_ops = { 257 .oo_name = "route/qdisc", 258 .oo_size = sizeof(struct rtnl_qdisc), 259 .oo_free_data = qdisc_free_data, 260 .oo_clone = qdisc_clone, 261 .oo_dump = { 262 [NL_DUMP_LINE] = qdisc_dump_line, 263 [NL_DUMP_DETAILS] = qdisc_dump_details, 264 [NL_DUMP_STATS] = qdisc_dump_stats, 265 }, 266 .oo_compare = tca_compare, 267 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE), 268 }; 269 270 /** @} */ 271