1 /* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */
3 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/errno.h>
10 #include <linux/skbuff.h>
11 #include <linux/atmdev.h>
12 #include <linux/atmclip.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/file.h> /* for fput */
15 #include <net/netlink.h>
16 #include <net/pkt_sched.h>
19 extern struct socket *sockfd_lookup(int fd, int *err); /* @@@ fix this */
22 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
24 #define DPRINTK(format,args...)
28 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
30 #define D2PRINTK(format,args...)
35 * The ATM queuing discipline provides a framework for invoking classifiers
36 * (aka "filters"), which in turn select classes of this queuing discipline.
37 * Each class maps the flow(s) it is handling to a given VC. Multiple classes
38 * may share the same VC.
40 * When creating a class, VCs are specified by passing the number of the open
41 * socket descriptor by which the calling process references the VC. The kernel
42 * keeps the VC open at least until all classes using it are removed.
44 * In this file, most functions are named atm_tc_* to avoid confusion with all
45 * the atm_* in net/atm. This naming convention differs from what's used in the
49 * - sometimes messes up the IP stack
50 * - any manipulations besides the few operations described in the README, are
51 * untested and likely to crash the system
52 * - should lock the flow while there is data in the queue (?)
56 #define PRIV(sch) qdisc_priv(sch)
57 #define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))
60 struct atm_flow_data {
61 struct Qdisc *q; /* FIFO, TBF, etc. */
62 struct tcf_proto *filter_list;
63 struct atm_vcc *vcc; /* VCC; NULL if VCC is closed */
64 void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); /* chaining */
65 struct atm_qdisc_data *parent; /* parent qdisc */
66 struct socket *sock; /* for closing */
67 u32 classid; /* x:y type ID */
68 int ref; /* reference count */
69 struct gnet_stats_basic bstats;
70 struct gnet_stats_queue qstats;
71 struct atm_flow_data *next;
72 struct atm_flow_data *excess; /* flow for excess traffic;
73 NULL to set CLP instead */
75 unsigned char hdr[0]; /* header data; MUST BE LAST */
78 struct atm_qdisc_data {
79 struct atm_flow_data link; /* unclassified skbs go here */
80 struct atm_flow_data *flows; /* NB: "link" is also on this
82 struct tasklet_struct task; /* requeue tasklet */
86 /* ------------------------- Class/flow operations ------------------------- */
89 static int find_flow(struct atm_qdisc_data *qdisc,struct atm_flow_data *flow)
91 struct atm_flow_data *walk;
93 DPRINTK("find_flow(qdisc %p,flow %p)\n",qdisc,flow);
94 for (walk = qdisc->flows; walk; walk = walk->next)
95 if (walk == flow) return 1;
96 DPRINTK("find_flow: not found\n");
101 static __inline__ struct atm_flow_data *lookup_flow(struct Qdisc *sch,
104 struct atm_qdisc_data *p = PRIV(sch);
105 struct atm_flow_data *flow;
107 for (flow = p->flows; flow; flow = flow->next)
108 if (flow->classid == classid) break;
113 static int atm_tc_graft(struct Qdisc *sch,unsigned long arg,
114 struct Qdisc *new,struct Qdisc **old)
116 struct atm_qdisc_data *p = PRIV(sch);
117 struct atm_flow_data *flow = (struct atm_flow_data *) arg;
119 DPRINTK("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",sch,
121 if (!find_flow(p,flow)) return -EINVAL;
122 if (!new) new = &noop_qdisc;
123 *old = xchg(&flow->q,new);
124 if (*old) qdisc_reset(*old);
129 static struct Qdisc *atm_tc_leaf(struct Qdisc *sch,unsigned long cl)
131 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
133 DPRINTK("atm_tc_leaf(sch %p,flow %p)\n",sch,flow);
134 return flow ? flow->q : NULL;
138 static unsigned long atm_tc_get(struct Qdisc *sch,u32 classid)
140 struct atm_qdisc_data *p __attribute__((unused)) = PRIV(sch);
141 struct atm_flow_data *flow;
143 DPRINTK("atm_tc_get(sch %p,[qdisc %p],classid %x)\n",sch,p,classid);
144 flow = lookup_flow(sch,classid);
145 if (flow) flow->ref++;
146 DPRINTK("atm_tc_get: flow %p\n",flow);
147 return (unsigned long) flow;
151 static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
152 unsigned long parent, u32 classid)
154 return atm_tc_get(sch,classid);
158 * atm_tc_put handles all destructions, including the ones that are explicitly
159 * requested (atm_tc_destroy, etc.). The assumption here is that we never drop
160 * anything that still seems to be in use.
163 static void atm_tc_put(struct Qdisc *sch, unsigned long cl)
165 struct atm_qdisc_data *p = PRIV(sch);
166 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
167 struct atm_flow_data **prev;
169 DPRINTK("atm_tc_put(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
170 if (--flow->ref) return;
171 DPRINTK("atm_tc_put: destroying\n");
172 for (prev = &p->flows; *prev; prev = &(*prev)->next)
173 if (*prev == flow) break;
175 printk(KERN_CRIT "atm_tc_put: class %p not found\n",flow);
179 DPRINTK("atm_tc_put: qdisc %p\n",flow->q);
180 qdisc_destroy(flow->q);
181 tcf_destroy_chain(flow->filter_list);
183 DPRINTK("atm_tc_put: f_count %d\n",
184 file_count(flow->sock->file));
185 flow->vcc->pop = flow->old_pop;
186 sockfd_put(flow->sock);
188 if (flow->excess) atm_tc_put(sch,(unsigned long) flow->excess);
189 if (flow != &p->link) kfree(flow);
191 * If flow == &p->link, the qdisc no longer works at this point and
192 * needs to be removed. (By the caller of atm_tc_put.)
197 static void sch_atm_pop(struct atm_vcc *vcc,struct sk_buff *skb)
199 struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;
201 D2PRINTK("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n",vcc,skb,p);
202 VCC2FLOW(vcc)->old_pop(vcc,skb);
203 tasklet_schedule(&p->task);
206 static const u8 llc_oui_ip[] = {
207 0xaa, /* DSAP: non-ISO */
208 0xaa, /* SSAP: non-ISO */
209 0x03, /* Ctrl: Unnumbered Information Command PDU */
210 0x00, /* OUI: EtherType */
212 0x08, 0x00 }; /* Ethertype IP (0800) */
214 static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
215 struct rtattr **tca, unsigned long *arg)
217 struct atm_qdisc_data *p = PRIV(sch);
218 struct atm_flow_data *flow = (struct atm_flow_data *) *arg;
219 struct atm_flow_data *excess = NULL;
220 struct rtattr *opt = tca[TCA_OPTIONS-1];
221 struct rtattr *tb[TCA_ATM_MAX];
223 int fd,error,hdr_len;
226 DPRINTK("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
227 "flow %p,opt %p)\n",sch,p,classid,parent,flow,opt);
229 * The concept of parents doesn't apply for this qdisc.
231 if (parent && parent != TC_H_ROOT && parent != sch->handle)
234 * ATM classes cannot be changed. In order to change properties of the
235 * ATM connection, that socket needs to be modified directly (via the
236 * native ATM API. In order to send a flow to a different VC, the old
237 * class needs to be removed and a new one added. (This may be changed
240 if (flow) return -EBUSY;
241 if (opt == NULL || rtattr_parse_nested(tb, TCA_ATM_MAX, opt))
243 if (!tb[TCA_ATM_FD-1] || RTA_PAYLOAD(tb[TCA_ATM_FD-1]) < sizeof(fd))
245 fd = *(int *) RTA_DATA(tb[TCA_ATM_FD-1]);
246 DPRINTK("atm_tc_change: fd %d\n",fd);
247 if (tb[TCA_ATM_HDR-1]) {
248 hdr_len = RTA_PAYLOAD(tb[TCA_ATM_HDR-1]);
249 hdr = RTA_DATA(tb[TCA_ATM_HDR-1]);
252 hdr_len = RFC1483LLC_LEN;
253 hdr = NULL; /* default LLC/SNAP for IP */
255 if (!tb[TCA_ATM_EXCESS-1]) excess = NULL;
257 if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS-1]) != sizeof(u32))
259 excess = (struct atm_flow_data *) atm_tc_get(sch,
260 *(u32 *) RTA_DATA(tb[TCA_ATM_EXCESS-1]));
261 if (!excess) return -ENOENT;
263 DPRINTK("atm_tc_change: type %d, payload %d, hdr_len %d\n",
264 opt->rta_type,RTA_PAYLOAD(opt),hdr_len);
265 if (!(sock = sockfd_lookup(fd,&error))) return error; /* f_count++ */
266 DPRINTK("atm_tc_change: f_count %d\n",file_count(sock->file));
267 if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
271 /* @@@ should check if the socket is really operational or we'll crash
274 if (TC_H_MAJ(classid ^ sch->handle)) {
275 DPRINTK("atm_tc_change: classid mismatch\n");
279 if (find_flow(p,flow)) {
288 for (i = 1; i < 0x8000; i++) {
289 classid = TC_H_MAKE(sch->handle,0x8000 | i);
290 if (!(cl = atm_tc_get(sch,classid))) break;
294 DPRINTK("atm_tc_change: new id %x\n",classid);
295 flow = kmalloc(sizeof(struct atm_flow_data)+hdr_len,GFP_KERNEL);
296 DPRINTK("atm_tc_change: flow %p\n",flow);
301 memset(flow,0,sizeof(*flow));
302 flow->filter_list = NULL;
303 if (!(flow->q = qdisc_create_dflt(sch->dev,&pfifo_qdisc_ops,classid)))
304 flow->q = &noop_qdisc;
305 DPRINTK("atm_tc_change: qdisc %p\n",flow->q);
307 flow->vcc = ATM_SD(sock); /* speedup */
308 flow->vcc->user_back = flow;
309 DPRINTK("atm_tc_change: vcc %p\n",flow->vcc);
310 flow->old_pop = flow->vcc->pop;
312 flow->vcc->pop = sch_atm_pop;
313 flow->classid = classid;
315 flow->excess = excess;
316 flow->next = p->link.next;
318 flow->hdr_len = hdr_len;
320 memcpy(flow->hdr,hdr,hdr_len);
322 memcpy(flow->hdr,llc_oui_ip,sizeof(llc_oui_ip));
323 *arg = (unsigned long) flow;
326 if (excess) atm_tc_put(sch,(unsigned long) excess);
332 static int atm_tc_delete(struct Qdisc *sch,unsigned long arg)
334 struct atm_qdisc_data *p = PRIV(sch);
335 struct atm_flow_data *flow = (struct atm_flow_data *) arg;
337 DPRINTK("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
338 if (!find_flow(PRIV(sch),flow)) return -EINVAL;
339 if (flow->filter_list || flow == &p->link) return -EBUSY;
341 * Reference count must be 2: one for "keepalive" (set at class
342 * creation), and one for the reference held when calling delete.
345 printk(KERN_ERR "atm_tc_delete: flow->ref == %d\n",flow->ref);
348 if (flow->ref > 2) return -EBUSY; /* catch references via excess, etc.*/
354 static void atm_tc_walk(struct Qdisc *sch,struct qdisc_walker *walker)
356 struct atm_qdisc_data *p = PRIV(sch);
357 struct atm_flow_data *flow;
359 DPRINTK("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n",sch,p,walker);
360 if (walker->stop) return;
361 for (flow = p->flows; flow; flow = flow->next) {
362 if (walker->count >= walker->skip)
363 if (walker->fn(sch,(unsigned long) flow,walker) < 0) {
372 static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch,unsigned long cl)
374 struct atm_qdisc_data *p = PRIV(sch);
375 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
377 DPRINTK("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n",sch,p,flow);
378 return flow ? &flow->filter_list : &p->link.filter_list;
382 /* --------------------------- Qdisc operations ---------------------------- */
385 static int atm_tc_enqueue(struct sk_buff *skb,struct Qdisc *sch)
387 struct atm_qdisc_data *p = PRIV(sch);
388 struct atm_flow_data *flow = NULL ; /* @@@ */
389 struct tcf_result res;
391 int ret = NET_XMIT_POLICED;
393 D2PRINTK("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
394 result = TC_POLICE_OK; /* be nice to gcc */
395 if (TC_H_MAJ(skb->priority) != sch->handle ||
396 !(flow = (struct atm_flow_data *) atm_tc_get(sch,skb->priority)))
397 for (flow = p->flows; flow; flow = flow->next)
398 if (flow->filter_list) {
399 result = tc_classify(skb,flow->filter_list,
401 if (result < 0) continue;
402 flow = (struct atm_flow_data *) res.class;
403 if (!flow) flow = lookup_flow(sch,res.classid);
406 if (!flow) flow = &p->link;
409 ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
410 /*@@@ looks good ... but it's not supposed to work :-)*/
411 #ifdef CONFIG_NET_CLS_POLICE
416 case TC_POLICE_RECLASSIFY:
417 if (flow->excess) flow = flow->excess;
419 ATM_SKB(skb)->atm_options |=
432 #ifdef CONFIG_NET_CLS_POLICE
433 result == TC_POLICE_SHOT ||
435 (ret = flow->q->enqueue(skb,flow->q)) != 0) {
437 if (flow) flow->qstats.drops++;
440 sch->bstats.bytes += skb->len;
441 sch->bstats.packets++;
442 flow->bstats.bytes += skb->len;
443 flow->bstats.packets++;
445 * Okay, this may seem weird. We pretend we've dropped the packet if
446 * it goes via ATM. The reason for this is that the outer qdisc
447 * expects to be able to q->dequeue the packet later on if we return
448 * success at this place. Also, sch->q.qdisc needs to reflect whether
449 * there is a packet egligible for dequeuing or not. Note that the
450 * statistics of the outer qdisc are necessarily wrong because of all
451 * this. There's currently no correct solution for this.
453 if (flow == &p->link) {
457 tasklet_schedule(&p->task);
458 return NET_XMIT_BYPASS;
463 * Dequeue packets and send them over ATM. Note that we quite deliberately
464 * avoid checking net_device's flow control here, simply because sch_atm
465 * uses its own channels, which have nothing to do with any CLIP/LANE/or
466 * non-ATM interfaces.
470 static void sch_atm_dequeue(unsigned long data)
472 struct Qdisc *sch = (struct Qdisc *) data;
473 struct atm_qdisc_data *p = PRIV(sch);
474 struct atm_flow_data *flow;
477 D2PRINTK("sch_atm_dequeue(sch %p,[qdisc %p])\n",sch,p);
478 for (flow = p->link.next; flow; flow = flow->next)
480 * If traffic is properly shaped, this won't generate nasty
481 * little bursts. Otherwise, it may ... (but that's okay)
483 while ((skb = flow->q->dequeue(flow->q))) {
484 if (!atm_may_send(flow->vcc,skb->truesize)) {
485 (void) flow->q->ops->requeue(skb,flow->q);
488 D2PRINTK("atm_tc_dequeue: sending on class %p\n",flow);
489 /* remove any LL header somebody else has attached */
490 skb_pull(skb, skb_network_offset(skb));
491 if (skb_headroom(skb) < flow->hdr_len) {
494 new = skb_realloc_headroom(skb,flow->hdr_len);
499 D2PRINTK("sch_atm_dequeue: ip %p, data %p\n",
500 skb_network_header(skb), skb->data);
501 ATM_SKB(skb)->vcc = flow->vcc;
502 memcpy(skb_push(skb,flow->hdr_len),flow->hdr,
504 atomic_add(skb->truesize,
505 &sk_atm(flow->vcc)->sk_wmem_alloc);
506 /* atm.atm_options are already set by atm_tc_enqueue */
507 (void) flow->vcc->send(flow->vcc,skb);
512 static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
514 struct atm_qdisc_data *p = PRIV(sch);
517 D2PRINTK("atm_tc_dequeue(sch %p,[qdisc %p])\n",sch,p);
518 tasklet_schedule(&p->task);
519 skb = p->link.q->dequeue(p->link.q);
520 if (skb) sch->q.qlen--;
525 static int atm_tc_requeue(struct sk_buff *skb,struct Qdisc *sch)
527 struct atm_qdisc_data *p = PRIV(sch);
530 D2PRINTK("atm_tc_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,p);
531 ret = p->link.q->ops->requeue(skb,p->link.q);
534 sch->qstats.requeues++;
537 p->link.qstats.drops++;
543 static unsigned int atm_tc_drop(struct Qdisc *sch)
545 struct atm_qdisc_data *p = PRIV(sch);
546 struct atm_flow_data *flow;
549 DPRINTK("atm_tc_drop(sch %p,[qdisc %p])\n",sch,p);
550 for (flow = p->flows; flow; flow = flow->next)
551 if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
557 static int atm_tc_init(struct Qdisc *sch,struct rtattr *opt)
559 struct atm_qdisc_data *p = PRIV(sch);
561 DPRINTK("atm_tc_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
563 if(!(p->link.q = qdisc_create_dflt(sch->dev,&pfifo_qdisc_ops,
565 p->link.q = &noop_qdisc;
566 DPRINTK("atm_tc_init: link (%p) qdisc %p\n",&p->link,p->link.q);
567 p->link.filter_list = NULL;
570 p->link.classid = sch->handle;
573 tasklet_init(&p->task,sch_atm_dequeue,(unsigned long) sch);
578 static void atm_tc_reset(struct Qdisc *sch)
580 struct atm_qdisc_data *p = PRIV(sch);
581 struct atm_flow_data *flow;
583 DPRINTK("atm_tc_reset(sch %p,[qdisc %p])\n",sch,p);
584 for (flow = p->flows; flow; flow = flow->next) qdisc_reset(flow->q);
589 static void atm_tc_destroy(struct Qdisc *sch)
591 struct atm_qdisc_data *p = PRIV(sch);
592 struct atm_flow_data *flow;
594 DPRINTK("atm_tc_destroy(sch %p,[qdisc %p])\n",sch,p);
596 while ((flow = p->flows)) {
597 tcf_destroy_chain(flow->filter_list);
598 flow->filter_list = NULL;
600 printk(KERN_ERR "atm_destroy: %p->ref = %d\n",flow,
602 atm_tc_put(sch,(unsigned long) flow);
603 if (p->flows == flow) {
604 printk(KERN_ERR "atm_destroy: putting flow %p didn't "
606 p->flows = flow->next; /* brute force */
610 tasklet_kill(&p->task);
614 static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
615 struct sk_buff *skb, struct tcmsg *tcm)
617 struct atm_qdisc_data *p = PRIV(sch);
618 struct atm_flow_data *flow = (struct atm_flow_data *) cl;
619 unsigned char *b = skb_tail_pointer(skb);
622 DPRINTK("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
624 if (!find_flow(p,flow)) return -EINVAL;
625 tcm->tcm_handle = flow->classid;
626 tcm->tcm_info = flow->q->handle;
627 rta = (struct rtattr *) b;
628 RTA_PUT(skb,TCA_OPTIONS,0,NULL);
629 RTA_PUT(skb,TCA_ATM_HDR,flow->hdr_len,flow->hdr);
631 struct sockaddr_atmpvc pvc;
634 pvc.sap_family = AF_ATMPVC;
635 pvc.sap_addr.itf = flow->vcc->dev ? flow->vcc->dev->number : -1;
636 pvc.sap_addr.vpi = flow->vcc->vpi;
637 pvc.sap_addr.vci = flow->vcc->vci;
638 RTA_PUT(skb,TCA_ATM_ADDR,sizeof(pvc),&pvc);
639 state = ATM_VF2VS(flow->vcc->flags);
640 RTA_PUT(skb,TCA_ATM_STATE,sizeof(state),&state);
643 RTA_PUT(skb,TCA_ATM_EXCESS,sizeof(u32),&flow->classid);
647 RTA_PUT(skb,TCA_ATM_EXCESS,sizeof(zero),&zero);
649 rta->rta_len = skb_tail_pointer(skb) - b;
657 atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
660 struct atm_flow_data *flow = (struct atm_flow_data *) arg;
662 flow->qstats.qlen = flow->q->q.qlen;
664 if (gnet_stats_copy_basic(d, &flow->bstats) < 0 ||
665 gnet_stats_copy_queue(d, &flow->qstats) < 0)
671 static int atm_tc_dump(struct Qdisc *sch, struct sk_buff *skb)
676 static struct Qdisc_class_ops atm_class_ops = {
677 .graft = atm_tc_graft,
681 .change = atm_tc_change,
682 .delete = atm_tc_delete,
684 .tcf_chain = atm_tc_find_tcf,
685 .bind_tcf = atm_tc_bind_filter,
686 .unbind_tcf = atm_tc_put,
687 .dump = atm_tc_dump_class,
688 .dump_stats = atm_tc_dump_class_stats,
691 static struct Qdisc_ops atm_qdisc_ops = {
693 .cl_ops = &atm_class_ops,
695 .priv_size = sizeof(struct atm_qdisc_data),
696 .enqueue = atm_tc_enqueue,
697 .dequeue = atm_tc_dequeue,
698 .requeue = atm_tc_requeue,
701 .reset = atm_tc_reset,
702 .destroy = atm_tc_destroy,
705 .owner = THIS_MODULE,
709 static int __init atm_init(void)
711 return register_qdisc(&atm_qdisc_ops);
714 static void __exit atm_exit(void)
716 unregister_qdisc(&atm_qdisc_ops);
719 module_init(atm_init)
720 module_exit(atm_exit)
721 MODULE_LICENSE("GPL");