1/* net/sched/sch_dsmark.c - Differentiated Services field marker */ 2 3/* Written 1998-2000 by Werner Almesberger, EPFL ICA */ 4 5 6#include <linux/module.h> 7#include <linux/init.h> 8#include <linux/types.h> 9#include <linux/string.h> 10#include <linux/errno.h> 11#include <linux/skbuff.h> 12#include <linux/netdevice.h> /* for pkt_sched */ 13#include <linux/rtnetlink.h> 14#include <net/pkt_sched.h> 15#include <net/dsfield.h> 16#include <net/inet_ecn.h> 17#include <asm/byteorder.h> 18 19 20#define DPRINTK(format,args...) 21 22#define D2PRINTK(format,args...) 23 24 25#define PRIV(sch) ((struct dsmark_qdisc_data *) qdisc_priv(sch)) 26 27 28/* 29 * classid class marking 30 * ------- ----- ------- 31 * n/a 0 n/a 32 * x:0 1 use entry [0] 33 * ... ... ... 34 * x:y y>0 y+1 use entry [y] 35 * ... ... ... 36 * x:indices-1 indices use entry [indices-1] 37 * ... ... ... 38 * x:y y+1 use entry [y & (indices-1)] 39 * ... ... ... 40 * 0xffff 0x10000 use entry [indices-1] 41 */ 42 43 44#define NO_DEFAULT_INDEX (1 << 16) 45 46struct dsmark_qdisc_data { 47 struct Qdisc *q; 48 struct tcf_proto *filter_list; 49 u8 *mask; /* "owns" the array */ 50 u8 *value; 51 u16 indices; 52 u32 default_index; /* index range is 0...0xffff */ 53 int set_tc_index; 54}; 55 56static inline int dsmark_valid_indices(u16 indices) 57{ 58 while (indices != 1) { 59 if (indices & 1) 60 return 0; 61 indices >>= 1; 62 } 63 64 return 1; 65} 66 67static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index) 68{ 69 return (index <= p->indices && index > 0); 70} 71 72/* ------------------------- Class/flow operations ------------------------- */ 73 74static int dsmark_graft(struct Qdisc *sch, unsigned long arg, 75 struct Qdisc *new, struct Qdisc **old) 76{ 77 struct dsmark_qdisc_data *p = PRIV(sch); 78 79 DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)\n", 80 sch, p, new, old); 81 82 if (new == NULL) { 83 new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, 84 sch->handle); 85 if (new == NULL) 86 new = &noop_qdisc; 87 } 88 89 sch_tree_lock(sch); 90 *old = xchg(&p->q, new); 91 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen); 92 qdisc_reset(*old); 93 sch_tree_unlock(sch); 94 95 return 0; 96} 97 98static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg) 99{ 100 return PRIV(sch)->q; 101} 102 103static unsigned long dsmark_get(struct Qdisc *sch, u32 classid) 104{ 105 DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)\n", 106 sch, PRIV(sch), classid); 107 108 return TC_H_MIN(classid) + 1; 109} 110 111static unsigned long dsmark_bind_filter(struct Qdisc *sch, 112 unsigned long parent, u32 classid) 113{ 114 return dsmark_get(sch, classid); 115} 116 117static void dsmark_put(struct Qdisc *sch, unsigned long cl) 118{ 119} 120 121static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent, 122 struct rtattr **tca, unsigned long *arg) 123{ 124 struct dsmark_qdisc_data *p = PRIV(sch); 125 struct rtattr *opt = tca[TCA_OPTIONS-1]; 126 struct rtattr *tb[TCA_DSMARK_MAX]; 127 int err = -EINVAL; 128 u8 mask = 0; 129 130 DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x)," 131 "arg 0x%lx\n", sch, p, classid, parent, *arg); 132 133 if (!dsmark_valid_index(p, *arg)) { 134 err = -ENOENT; 135 goto rtattr_failure; 136 } 137 138 if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt)) 139 goto rtattr_failure; 140 141 if (tb[TCA_DSMARK_MASK-1]) 142 mask = RTA_GET_U8(tb[TCA_DSMARK_MASK-1]); 143 144 if (tb[TCA_DSMARK_VALUE-1]) 145 p->value[*arg-1] = RTA_GET_U8(tb[TCA_DSMARK_VALUE-1]); 146 147 if (tb[TCA_DSMARK_MASK-1]) 148 p->mask[*arg-1] = mask; 149 150 err = 0; 151 152rtattr_failure: 153 return err; 154} 155 156static int dsmark_delete(struct Qdisc *sch, unsigned long arg) 157{ 158 struct dsmark_qdisc_data *p = PRIV(sch); 159 160 if (!dsmark_valid_index(p, arg)) 161 return -EINVAL; 162 163 p->mask[arg-1] = 0xff; 164 p->value[arg-1] = 0; 165 166 return 0; 167} 168 169static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker) 170{ 171 struct dsmark_qdisc_data *p = PRIV(sch); 172 int i; 173 174 DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker); 175 176 if (walker->stop) 177 return; 178 179 for (i = 0; i < p->indices; i++) { 180 if (p->mask[i] == 0xff && !p->value[i]) 181 goto ignore; 182 if (walker->count >= walker->skip) { 183 if (walker->fn(sch, i+1, walker) < 0) { 184 walker->stop = 1; 185 break; 186 } 187 } 188ignore: 189 walker->count++; 190 } 191} 192 193static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl) 194{ 195 return &PRIV(sch)->filter_list; 196} 197 198/* --------------------------- Qdisc operations ---------------------------- */ 199 200static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch) 201{ 202 struct dsmark_qdisc_data *p = PRIV(sch); 203 int err; 204 205 D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p); 206 207 if (p->set_tc_index) { 208 switch (skb->protocol) { 209 case __constant_htons(ETH_P_IP): 210 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb)) 211 & ~INET_ECN_MASK; 212 break; 213 case __constant_htons(ETH_P_IPV6): 214 skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb)) 215 & ~INET_ECN_MASK; 216 break; 217 default: 218 skb->tc_index = 0; 219 break; 220 } 221 } 222 223 if (TC_H_MAJ(skb->priority) == sch->handle) 224 skb->tc_index = TC_H_MIN(skb->priority); 225 else { 226 struct tcf_result res; 227 int result = tc_classify(skb, p->filter_list, &res); 228 229 D2PRINTK("result %d class 0x%04x\n", result, res.classid); 230 231 switch (result) { 232#ifdef CONFIG_NET_CLS_POLICE 233 case TC_POLICE_SHOT: 234 kfree_skb(skb); 235 sch->qstats.drops++; 236 return NET_XMIT_POLICED; 237#endif 238 case TC_POLICE_OK: 239 skb->tc_index = TC_H_MIN(res.classid); 240 break; 241 case TC_POLICE_UNSPEC: 242 /* fall through */ 243 default: 244 if (p->default_index != NO_DEFAULT_INDEX) 245 skb->tc_index = p->default_index; 246 break; 247 } 248 } 249 250 err = p->q->enqueue(skb,p->q); 251 if (err != NET_XMIT_SUCCESS) { 252 sch->qstats.drops++; 253 return err; 254 } 255 256 sch->bstats.bytes += skb->len; 257 sch->bstats.packets++; 258 sch->q.qlen++; 259 260 return NET_XMIT_SUCCESS; 261} 262 263static struct sk_buff *dsmark_dequeue(struct Qdisc *sch) 264{ 265 struct dsmark_qdisc_data *p = PRIV(sch); 266 struct sk_buff *skb; 267 u32 index; 268 269 D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])\n", sch, p); 270 271 skb = p->q->ops->dequeue(p->q); 272 if (skb == NULL) 273 return NULL; 274 275 sch->q.qlen--; 276 277 index = skb->tc_index & (p->indices - 1); 278 D2PRINTK("index %d->%d\n", skb->tc_index, index); 279 280 switch (skb->protocol) { 281 case __constant_htons(ETH_P_IP): 282 ipv4_change_dsfield(ip_hdr(skb), p->mask[index], 283 p->value[index]); 284 break; 285 case __constant_htons(ETH_P_IPV6): 286 ipv6_change_dsfield(ipv6_hdr(skb), p->mask[index], 287 p->value[index]); 288 break; 289 default: 290 /* 291 * Only complain if a change was actually attempted. 292 * This way, we can send non-IP traffic through dsmark 293 * and don't need yet another qdisc as a bypass. 294 */ 295 if (p->mask[index] != 0xff || p->value[index]) 296 printk(KERN_WARNING "dsmark_dequeue: " 297 "unsupported protocol %d\n", 298 ntohs(skb->protocol)); 299 break; 300 } 301 302 return skb; 303} 304 305static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch) 306{ 307 struct dsmark_qdisc_data *p = PRIV(sch); 308 int err; 309 310 D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p); 311 312 err = p->q->ops->requeue(skb, p->q); 313 if (err != NET_XMIT_SUCCESS) { 314 sch->qstats.drops++; 315 return err; 316 } 317 318 sch->q.qlen++; 319 sch->qstats.requeues++; 320 321 return NET_XMIT_SUCCESS; 322} 323 324static unsigned int dsmark_drop(struct Qdisc *sch) 325{ 326 struct dsmark_qdisc_data *p = PRIV(sch); 327 unsigned int len; 328 329 DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p); 330 331 if (p->q->ops->drop == NULL) 332 return 0; 333 334 len = p->q->ops->drop(p->q); 335 if (len) 336 sch->q.qlen--; 337 338 return len; 339} 340 341static int dsmark_init(struct Qdisc *sch, struct rtattr *opt) 342{ 343 struct dsmark_qdisc_data *p = PRIV(sch); 344 struct rtattr *tb[TCA_DSMARK_MAX]; 345 int err = -EINVAL; 346 u32 default_index = NO_DEFAULT_INDEX; 347 u16 indices; 348 u8 *mask; 349 350 DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt); 351 352 if (!opt || rtattr_parse_nested(tb, TCA_DSMARK_MAX, opt) < 0) 353 goto errout; 354 355 indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]); 356 if (!indices || !dsmark_valid_indices(indices)) 357 goto errout; 358 359 if (tb[TCA_DSMARK_DEFAULT_INDEX-1]) 360 default_index = RTA_GET_U16(tb[TCA_DSMARK_DEFAULT_INDEX-1]); 361 362 mask = kmalloc(indices * 2, GFP_KERNEL); 363 if (mask == NULL) { 364 err = -ENOMEM; 365 goto errout; 366 } 367 368 p->mask = mask; 369 memset(p->mask, 0xff, indices); 370 371 p->value = p->mask + indices; 372 memset(p->value, 0, indices); 373 374 p->indices = indices; 375 p->default_index = default_index; 376 p->set_tc_index = RTA_GET_FLAG(tb[TCA_DSMARK_SET_TC_INDEX-1]); 377 378 p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, sch->handle); 379 if (p->q == NULL) 380 p->q = &noop_qdisc; 381 382 DPRINTK("dsmark_init: qdisc %p\n", p->q); 383 384 err = 0; 385errout: 386rtattr_failure: 387 return err; 388} 389 390static void dsmark_reset(struct Qdisc *sch) 391{ 392 struct dsmark_qdisc_data *p = PRIV(sch); 393 394 DPRINTK("dsmark_reset(sch %p,[qdisc %p])\n", sch, p); 395 qdisc_reset(p->q); 396 sch->q.qlen = 0; 397} 398 399static void dsmark_destroy(struct Qdisc *sch) 400{ 401 struct dsmark_qdisc_data *p = PRIV(sch); 402 403 DPRINTK("dsmark_destroy(sch %p,[qdisc %p])\n", sch, p); 404 405 tcf_destroy_chain(p->filter_list); 406 qdisc_destroy(p->q); 407 kfree(p->mask); 408} 409 410static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl, 411 struct sk_buff *skb, struct tcmsg *tcm) 412{ 413 struct dsmark_qdisc_data *p = PRIV(sch); 414 struct rtattr *opts = NULL; 415 416 DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ld\n", sch, p, cl); 417 418 if (!dsmark_valid_index(p, cl)) 419 return -EINVAL; 420 421 tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl-1); 422 tcm->tcm_info = p->q->handle; 423 424 opts = RTA_NEST(skb, TCA_OPTIONS); 425 RTA_PUT_U8(skb,TCA_DSMARK_MASK, p->mask[cl-1]); 426 RTA_PUT_U8(skb,TCA_DSMARK_VALUE, p->value[cl-1]); 427 428 return RTA_NEST_END(skb, opts); 429 430rtattr_failure: 431 return RTA_NEST_CANCEL(skb, opts); 432} 433 434static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb) 435{ 436 struct dsmark_qdisc_data *p = PRIV(sch); 437 struct rtattr *opts = NULL; 438 439 opts = RTA_NEST(skb, TCA_OPTIONS); 440 RTA_PUT_U16(skb, TCA_DSMARK_INDICES, p->indices); 441 442 if (p->default_index != NO_DEFAULT_INDEX) 443 RTA_PUT_U16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index); 444 445 if (p->set_tc_index) 446 RTA_PUT_FLAG(skb, TCA_DSMARK_SET_TC_INDEX); 447 448 return RTA_NEST_END(skb, opts); 449 450rtattr_failure: 451 return RTA_NEST_CANCEL(skb, opts); 452} 453 454static struct Qdisc_class_ops dsmark_class_ops = { 455 .graft = dsmark_graft, 456 .leaf = dsmark_leaf, 457 .get = dsmark_get, 458 .put = dsmark_put, 459 .change = dsmark_change, 460 .delete = dsmark_delete, 461 .walk = dsmark_walk, 462 .tcf_chain = dsmark_find_tcf, 463 .bind_tcf = dsmark_bind_filter, 464 .unbind_tcf = dsmark_put, 465 .dump = dsmark_dump_class, 466}; 467 468static struct Qdisc_ops dsmark_qdisc_ops = { 469 .next = NULL, 470 .cl_ops = &dsmark_class_ops, 471 .id = "dsmark", 472 .priv_size = sizeof(struct dsmark_qdisc_data), 473 .enqueue = dsmark_enqueue, 474 .dequeue = dsmark_dequeue, 475 .requeue = dsmark_requeue, 476 .drop = dsmark_drop, 477 .init = dsmark_init, 478 .reset = dsmark_reset, 479 .destroy = dsmark_destroy, 480 .change = NULL, 481 .dump = dsmark_dump, 482 .owner = THIS_MODULE, 483}; 484 485static int __init dsmark_module_init(void) 486{ 487 return register_qdisc(&dsmark_qdisc_ops); 488} 489 490static void __exit dsmark_module_exit(void) 491{ 492 unregister_qdisc(&dsmark_qdisc_ops); 493} 494 495module_init(dsmark_module_init) 496module_exit(dsmark_module_exit) 497 498MODULE_LICENSE("GPL"); 499