1 /* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */
3 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
6 #include <linux/config.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/string.h>
10 #include <linux/errno.h>
11 #include <linux/skbuff.h>
12 #include <linux/interrupt.h>
13 #include <linux/atmdev.h>
14 #include <linux/atmclip.h>
15 #include <linux/netdevice.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/file.h> /* for fput */
18 #include <net/pkt_sched.h>
22 extern struct socket *sockfd_lookup(int fd, int *err); /* @@@ fix this */
25 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
27 #define DPRINTK(format,args...)
31 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
33 #define D2PRINTK(format,args...)
38 * The ATM queuing discipline provides a framework for invoking classifiers
39 * (aka "filters"), which in turn select classes of this queuing discipline.
40 * Each class maps the flow(s) it is handling to a given VC. Multiple classes
41 * may share the same VC.
43 * When creating a class, VCs are specified by passing the number of the open
44 * socket descriptor by which the calling process references the VC. The kernel
45 * keeps the VC open at least until all classes using it are removed.
47 * In this file, most functions are named atm_tc_* to avoid confusion with all
48 * the atm_* in net/atm. This naming convention differs from what's used in the
52 * - sometimes messes up the IP stack
53 * - any manipulations besides the few operations described in the README, are
54 * untested and likely to crash the system
55 * - should lock the flow while there is data in the queue (?)
59 #define PRIV(sch) ((struct atm_qdisc_data *) (sch)->data)
60 #define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))
63 struct atm_flow_data {
64 struct Qdisc *q; /* FIFO, TBF, etc. */
65 struct tcf_proto *filter_list;
66 struct atm_vcc *vcc; /* VCC; NULL if VCC is closed */
67 void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); /* chaining */
68 struct atm_qdisc_data *parent; /* parent qdisc */
69 struct socket *sock; /* for closing */
70 u32 classid; /* x:y type ID */
71 int ref; /* reference count */
72 struct tc_stats stats;
73 spinlock_t *stats_lock;
74 struct atm_flow_data *next;
75 struct atm_flow_data *excess; /* flow for excess traffic;
76 NULL to set CLP instead */
78 unsigned char hdr[0]; /* header data; MUST BE LAST */
81 struct atm_qdisc_data {
82 struct atm_flow_data link; /* unclassified skbs go here */
83 struct atm_flow_data *flows; /* NB: "link" is also on this
85 struct tasklet_struct task; /* requeue tasklet */
89 /* ------------------------- Class/flow operations ------------------------- */
92 static int find_flow(struct atm_qdisc_data *qdisc,struct atm_flow_data *flow)
94 struct atm_flow_data *walk;
96 DPRINTK("find_flow(qdisc %p,flow %p)\n",qdisc,flow);
97 for (walk = qdisc->flows; walk; walk = walk->next)
98 if (walk == flow) return 1;
99 DPRINTK("find_flow: not found\n");
104 static __inline__ struct atm_flow_data *lookup_flow(struct Qdisc *sch,
107 struct atm_flow_data *flow;
109 for (flow = PRIV(sch)->flows; flow; flow = flow->next)
110 if (flow->classid == classid) break;
115 static int atm_tc_graft(struct Qdisc *sch,unsigned long arg,
116 struct Qdisc *new,struct Qdisc **old)
118 struct atm_qdisc_data *p = PRIV(sch);
119 struct atm_flow_data *flow = (struct atm_flow_data *) arg;
121 DPRINTK("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",sch,
123 if (!find_flow(p,flow)) return -EINVAL;
124 if (!new) new = &noop_qdisc;
125 *old = xchg(&flow->q,new);
126 if (*old) qdisc_reset(*old);
131 static struct Qdisc *atm_tc_leaf(struct Qdisc *sch,unsigned long cl)
133 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
135 DPRINTK("atm_tc_leaf(sch %p,flow %p)\n",sch,flow);
136 return flow ? flow->q : NULL;
140 static unsigned long atm_tc_get(struct Qdisc *sch,u32 classid)
142 struct atm_qdisc_data *p __attribute__((unused)) = PRIV(sch);
143 struct atm_flow_data *flow;
145 DPRINTK("atm_tc_get(sch %p,[qdisc %p],classid %x)\n",sch,p,classid);
146 flow = lookup_flow(sch,classid);
147 if (flow) flow->ref++;
148 DPRINTK("atm_tc_get: flow %p\n",flow);
149 return (unsigned long) flow;
153 static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
154 unsigned long parent, u32 classid)
156 return atm_tc_get(sch,classid);
160 static void destroy_filters(struct atm_flow_data *flow)
162 struct tcf_proto *filter;
164 while ((filter = flow->filter_list)) {
165 DPRINTK("destroy_filters: destroying filter %p\n",filter);
166 flow->filter_list = filter->next;
173 * atm_tc_put handles all destructions, including the ones that are explicitly
174 * requested (atm_tc_destroy, etc.). The assumption here is that we never drop
175 * anything that still seems to be in use.
178 static void atm_tc_put(struct Qdisc *sch, unsigned long cl)
180 struct atm_qdisc_data *p = PRIV(sch);
181 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
182 struct atm_flow_data **prev;
184 DPRINTK("atm_tc_put(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
185 if (--flow->ref) return;
186 DPRINTK("atm_tc_put: destroying\n");
187 for (prev = &p->flows; *prev; prev = &(*prev)->next)
188 if (*prev == flow) break;
190 printk(KERN_CRIT "atm_tc_put: class %p not found\n",flow);
194 DPRINTK("atm_tc_put: qdisc %p\n",flow->q);
195 qdisc_destroy(flow->q);
196 destroy_filters(flow);
198 DPRINTK("atm_tc_put: f_count %d\n",
199 file_count(flow->sock->file));
200 flow->vcc->pop = flow->old_pop;
201 sockfd_put(flow->sock);
203 if (flow->excess) atm_tc_put(sch,(unsigned long) flow->excess);
204 if (flow != &p->link) kfree(flow);
206 * If flow == &p->link, the qdisc no longer works at this point and
207 * needs to be removed. (By the caller of atm_tc_put.)
212 static void sch_atm_pop(struct atm_vcc *vcc,struct sk_buff *skb)
214 struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;
216 D2PRINTK("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n",vcc,skb,p);
217 VCC2FLOW(vcc)->old_pop(vcc,skb);
218 tasklet_schedule(&p->task);
221 static const u8 llc_oui_ip[] = {
222 0xaa, /* DSAP: non-ISO */
223 0xaa, /* SSAP: non-ISO */
224 0x03, /* Ctrl: Unnumbered Information Command PDU */
225 0x00, /* OUI: EtherType */
227 0x08, 0x00 }; /* Ethertype IP (0800) */
229 static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
230 struct rtattr **tca, unsigned long *arg)
232 struct atm_qdisc_data *p = PRIV(sch);
233 struct atm_flow_data *flow = (struct atm_flow_data *) *arg;
234 struct atm_flow_data *excess = NULL;
235 struct rtattr *opt = tca[TCA_OPTIONS-1];
236 struct rtattr *tb[TCA_ATM_MAX];
238 int fd,error,hdr_len;
241 DPRINTK("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
242 "flow %p,opt %p)\n",sch,p,classid,parent,flow,opt);
244 * The concept of parents doesn't apply for this qdisc.
246 if (parent && parent != TC_H_ROOT && parent != sch->handle)
249 * ATM classes cannot be changed. In order to change properties of the
250 * ATM connection, that socket needs to be modified directly (via the
251 * native ATM API. In order to send a flow to a different VC, the old
252 * class needs to be removed and a new one added. (This may be changed
255 if (flow) return -EBUSY;
256 if (opt == NULL || rtattr_parse(tb,TCA_ATM_MAX,RTA_DATA(opt),
257 RTA_PAYLOAD(opt))) return -EINVAL;
258 if (!tb[TCA_ATM_FD-1] || RTA_PAYLOAD(tb[TCA_ATM_FD-1]) < sizeof(fd))
260 fd = *(int *) RTA_DATA(tb[TCA_ATM_FD-1]);
261 DPRINTK("atm_tc_change: fd %d\n",fd);
262 if (tb[TCA_ATM_HDR-1]) {
263 hdr_len = RTA_PAYLOAD(tb[TCA_ATM_HDR-1]);
264 hdr = RTA_DATA(tb[TCA_ATM_HDR-1]);
267 hdr_len = RFC1483LLC_LEN;
268 hdr = NULL; /* default LLC/SNAP for IP */
270 if (!tb[TCA_ATM_EXCESS-1]) excess = NULL;
272 if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS-1]) != sizeof(u32))
274 excess = (struct atm_flow_data *) atm_tc_get(sch,
275 *(u32 *) RTA_DATA(tb[TCA_ATM_EXCESS-1]));
276 if (!excess) return -ENOENT;
278 DPRINTK("atm_tc_change: type %d, payload %d, hdr_len %d\n",
279 opt->rta_type,RTA_PAYLOAD(opt),hdr_len);
280 if (!(sock = sockfd_lookup(fd,&error))) return error; /* f_count++ */
281 DPRINTK("atm_tc_change: f_count %d\n",file_count(sock->file));
282 if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
286 /* @@@ should check if the socket is really operational or we'll crash
289 if (TC_H_MAJ(classid ^ sch->handle)) {
290 DPRINTK("atm_tc_change: classid mismatch\n");
294 if (find_flow(p,flow)) {
303 for (i = 1; i < 0x8000; i++) {
304 classid = TC_H_MAKE(sch->handle,0x8000 | i);
305 if (!(cl = atm_tc_get(sch,classid))) break;
309 DPRINTK("atm_tc_change: new id %x\n",classid);
310 flow = kmalloc(sizeof(struct atm_flow_data)+hdr_len,GFP_KERNEL);
311 DPRINTK("atm_tc_change: flow %p\n",flow);
316 memset(flow,0,sizeof(*flow));
317 flow->filter_list = NULL;
318 if (!(flow->q = qdisc_create_dflt(sch->dev,&pfifo_qdisc_ops)))
319 flow->q = &noop_qdisc;
320 DPRINTK("atm_tc_change: qdisc %p\n",flow->q);
322 flow->vcc = ATM_SD(sock); /* speedup */
323 flow->vcc->user_back = flow;
324 DPRINTK("atm_tc_change: vcc %p\n",flow->vcc);
325 flow->old_pop = flow->vcc->pop;
327 flow->vcc->pop = sch_atm_pop;
328 flow->classid = classid;
330 flow->excess = excess;
331 flow->next = p->link.next;
333 flow->hdr_len = hdr_len;
335 memcpy(flow->hdr,hdr,hdr_len);
337 memcpy(flow->hdr,llc_oui_ip,sizeof(llc_oui_ip));
338 *arg = (unsigned long) flow;
341 if (excess) atm_tc_put(sch,(unsigned long) excess);
347 static int atm_tc_delete(struct Qdisc *sch,unsigned long arg)
349 struct atm_qdisc_data *p = PRIV(sch);
350 struct atm_flow_data *flow = (struct atm_flow_data *) arg;
352 DPRINTK("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
353 if (!find_flow(PRIV(sch),flow)) return -EINVAL;
354 if (flow->filter_list || flow == &p->link) return -EBUSY;
356 * Reference count must be 2: one for "keepalive" (set at class
357 * creation), and one for the reference held when calling delete.
360 printk(KERN_ERR "atm_tc_delete: flow->ref == %d\n",flow->ref);
363 if (flow->ref > 2) return -EBUSY; /* catch references via excess, etc.*/
369 static void atm_tc_walk(struct Qdisc *sch,struct qdisc_walker *walker)
371 struct atm_qdisc_data *p = PRIV(sch);
372 struct atm_flow_data *flow;
374 DPRINTK("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n",sch,p,walker);
375 if (walker->stop) return;
376 for (flow = p->flows; flow; flow = flow->next) {
377 if (walker->count >= walker->skip)
378 if (walker->fn(sch,(unsigned long) flow,walker) < 0) {
387 static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch,unsigned long cl)
389 struct atm_qdisc_data *p = PRIV(sch);
390 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
392 DPRINTK("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
393 return flow ? &flow->filter_list : &p->link.filter_list;
397 /* --------------------------- Qdisc operations ---------------------------- */
400 static int atm_tc_enqueue(struct sk_buff *skb,struct Qdisc *sch)
402 struct atm_qdisc_data *p = PRIV(sch);
403 struct atm_flow_data *flow = NULL ; /* @@@ */
404 struct tcf_result res;
406 int ret = NET_XMIT_POLICED;
408 D2PRINTK("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
409 result = TC_POLICE_OK; /* be nice to gcc */
410 if (TC_H_MAJ(skb->priority) != sch->handle ||
411 !(flow = (struct atm_flow_data *) atm_tc_get(sch,skb->priority)))
412 for (flow = p->flows; flow; flow = flow->next)
413 if (flow->filter_list) {
414 result = tc_classify(skb,flow->filter_list,
416 if (result < 0) continue;
417 flow = (struct atm_flow_data *) res.class;
418 if (!flow) flow = lookup_flow(sch,res.classid);
421 if (!flow) flow = &p->link;
424 ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
425 /*@@@ looks good ... but it's not supposed to work :-)*/
426 #ifdef CONFIG_NET_CLS_POLICE
431 case TC_POLICE_RECLASSIFY:
432 if (flow->excess) flow = flow->excess;
434 ATM_SKB(skb)->atm_options |=
447 #ifdef CONFIG_NET_CLS_POLICE
448 result == TC_POLICE_SHOT ||
450 (ret = flow->q->enqueue(skb,flow->q)) != 0) {
452 if (flow) flow->stats.drops++;
455 sch->stats.bytes += skb->len;
456 sch->stats.packets++;
457 flow->stats.bytes += skb->len;
458 flow->stats.packets++;
460 * Okay, this may seem weird. We pretend we've dropped the packet if
461 * it goes via ATM. The reason for this is that the outer qdisc
462 * expects to be able to q->dequeue the packet later on if we return
463 * success at this place. Also, sch->q.qdisc needs to reflect whether
464 * there is a packet egligible for dequeuing or not. Note that the
465 * statistics of the outer qdisc are necessarily wrong because of all
466 * this. There's currently no correct solution for this.
468 if (flow == &p->link) {
472 tasklet_schedule(&p->task);
473 return NET_XMIT_BYPASS;
478 * Dequeue packets and send them over ATM. Note that we quite deliberately
479 * avoid checking net_device's flow control here, simply because sch_atm
480 * uses its own channels, which have nothing to do with any CLIP/LANE/or
481 * non-ATM interfaces.
485 static void sch_atm_dequeue(unsigned long data)
487 struct Qdisc *sch = (struct Qdisc *) data;
488 struct atm_qdisc_data *p = PRIV(sch);
489 struct atm_flow_data *flow;
492 D2PRINTK("sch_atm_dequeue(sch %p,[qdisc %p])\n",sch,p);
493 for (flow = p->link.next; flow; flow = flow->next)
495 * If traffic is properly shaped, this won't generate nasty
496 * little bursts. Otherwise, it may ... (but that's okay)
498 while ((skb = flow->q->dequeue(flow->q))) {
499 if (!atm_may_send(flow->vcc,skb->truesize)) {
500 (void) flow->q->ops->requeue(skb,flow->q);
503 D2PRINTK("atm_tc_dequeue: sending on class %p\n",flow);
504 /* remove any LL header somebody else has attached */
505 skb_pull(skb,(char *) skb->nh.iph-(char *) skb->data);
506 if (skb_headroom(skb) < flow->hdr_len) {
509 new = skb_realloc_headroom(skb,flow->hdr_len);
514 D2PRINTK("sch_atm_dequeue: ip %p, data %p\n",
515 skb->nh.iph,skb->data);
516 ATM_SKB(skb)->vcc = flow->vcc;
517 memcpy(skb_push(skb,flow->hdr_len),flow->hdr,
519 atomic_add(skb->truesize,
520 &flow->vcc->sk->sk_wmem_alloc);
521 /* atm.atm_options are already set by atm_tc_enqueue */
522 (void) flow->vcc->send(flow->vcc,skb);
527 static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
529 struct atm_qdisc_data *p = PRIV(sch);
532 D2PRINTK("atm_tc_dequeue(sch %p,[qdisc %p])\n",sch,p);
533 tasklet_schedule(&p->task);
534 skb = p->link.q->dequeue(p->link.q);
535 if (skb) sch->q.qlen--;
540 static int atm_tc_requeue(struct sk_buff *skb,struct Qdisc *sch)
542 struct atm_qdisc_data *p = PRIV(sch);
545 D2PRINTK("atm_tc_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
546 ret = p->link.q->ops->requeue(skb,p->link.q);
547 if (!ret) sch->q.qlen++;
550 p->link.stats.drops++;
556 static unsigned int atm_tc_drop(struct Qdisc *sch)
558 struct atm_qdisc_data *p = PRIV(sch);
559 struct atm_flow_data *flow;
562 DPRINTK("atm_tc_drop(sch %p,[qdisc %p])\n",sch,p);
563 for (flow = p->flows; flow; flow = flow->next)
564 if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
570 static int atm_tc_init(struct Qdisc *sch,struct rtattr *opt)
572 struct atm_qdisc_data *p = PRIV(sch);
574 DPRINTK("atm_tc_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
575 memset(p,0,sizeof(*p));
577 if(!(p->link.q = qdisc_create_dflt(sch->dev,&pfifo_qdisc_ops)))
578 p->link.q = &noop_qdisc;
579 DPRINTK("atm_tc_init: link (%p) qdisc %p\n",&p->link,p->link.q);
580 p->link.filter_list = NULL;
583 p->link.classid = sch->handle;
586 tasklet_init(&p->task,sch_atm_dequeue,(unsigned long) sch);
591 static void atm_tc_reset(struct Qdisc *sch)
593 struct atm_qdisc_data *p = PRIV(sch);
594 struct atm_flow_data *flow;
596 DPRINTK("atm_tc_reset(sch %p,[qdisc %p])\n",sch,p);
597 for (flow = p->flows; flow; flow = flow->next) qdisc_reset(flow->q);
602 static void atm_tc_destroy(struct Qdisc *sch)
604 struct atm_qdisc_data *p = PRIV(sch);
605 struct atm_flow_data *flow;
607 DPRINTK("atm_tc_destroy(sch %p,[qdisc %p])\n",sch,p);
609 while ((flow = p->flows)) {
610 destroy_filters(flow);
612 printk(KERN_ERR "atm_destroy: %p->ref = %d\n",flow,
614 atm_tc_put(sch,(unsigned long) flow);
615 if (p->flows == flow) {
616 printk(KERN_ERR "atm_destroy: putting flow %p didn't "
618 p->flows = flow->next; /* brute force */
622 tasklet_kill(&p->task);
626 static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
627 struct sk_buff *skb, struct tcmsg *tcm)
629 struct atm_qdisc_data *p = PRIV(sch);
630 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
631 unsigned char *b = skb->tail;
634 DPRINTK("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
636 if (!find_flow(p,flow)) return -EINVAL;
637 tcm->tcm_handle = flow->classid;
638 rta = (struct rtattr *) b;
639 RTA_PUT(skb,TCA_OPTIONS,0,NULL);
640 RTA_PUT(skb,TCA_ATM_HDR,flow->hdr_len,flow->hdr);
642 struct sockaddr_atmpvc pvc;
645 pvc.sap_family = AF_ATMPVC;
646 pvc.sap_addr.itf = flow->vcc->dev ? flow->vcc->dev->number : -1;
647 pvc.sap_addr.vpi = flow->vcc->vpi;
648 pvc.sap_addr.vci = flow->vcc->vci;
649 RTA_PUT(skb,TCA_ATM_ADDR,sizeof(pvc),&pvc);
650 state = ATM_VF2VS(flow->vcc->flags);
651 RTA_PUT(skb,TCA_ATM_STATE,sizeof(state),&state);
654 RTA_PUT(skb,TCA_ATM_EXCESS,sizeof(u32),&flow->classid);
658 RTA_PUT(skb,TCA_ATM_EXCESS,sizeof(zero),&zero);
660 rta->rta_len = skb->tail-b;
664 skb_trim(skb,b-skb->data);
668 static int atm_tc_dump(struct Qdisc *sch, struct sk_buff *skb)
673 static struct Qdisc_class_ops atm_class_ops = {
674 .graft = atm_tc_graft,
678 .change = atm_tc_change,
679 .delete = atm_tc_delete,
681 .tcf_chain = atm_tc_find_tcf,
682 .bind_tcf = atm_tc_bind_filter,
683 .unbind_tcf = atm_tc_put,
684 .dump = atm_tc_dump_class,
687 static struct Qdisc_ops atm_qdisc_ops = {
689 .cl_ops = &atm_class_ops,
691 .priv_size = sizeof(struct atm_qdisc_data),
692 .enqueue = atm_tc_enqueue,
693 .dequeue = atm_tc_dequeue,
694 .requeue = atm_tc_requeue,
697 .reset = atm_tc_reset,
698 .destroy = atm_tc_destroy,
701 .owner = THIS_MODULE,
705 static int __init atm_init(void)
707 return register_qdisc(&atm_qdisc_ops);
710 static void __exit atm_exit(void)
712 unregister_qdisc(&atm_qdisc_ops);
715 module_init(atm_init)
716 module_exit(atm_exit)