1 // SPDX-License-Identifier: GPL-2.0-only
2 /* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */
4 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/atmdev.h>
14 #include <linux/atmclip.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/file.h> /* for fput */
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <net/pkt_cls.h>
22 * The ATM queuing discipline provides a framework for invoking classifiers
23 * (aka "filters"), which in turn select classes of this queuing discipline.
24 * Each class maps the flow(s) it is handling to a given VC. Multiple classes
25 * may share the same VC.
27 * When creating a class, VCs are specified by passing the number of the open
28 * socket descriptor by which the calling process references the VC. The kernel
29 * keeps the VC open at least until all classes using it are removed.
31 * In this file, most functions are named atm_tc_* to avoid confusion with all
32 * the atm_* in net/atm. This naming convention differs from what's used in the
36 * - sometimes messes up the IP stack
37 * - any manipulations besides the few operations described in the README, are
38 * untested and likely to crash the system
39 * - should lock the flow while there is data in the queue (?)
42 #define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))
44 struct atm_flow_data {
45 struct Qdisc_class_common common;
46 struct Qdisc *q; /* FIFO, TBF, etc. */
47 struct tcf_proto __rcu *filter_list;
48 struct tcf_block *block;
49 struct atm_vcc *vcc; /* VCC; NULL if VCC is closed */
50 void (*old_pop)(struct atm_vcc *vcc,
51 struct sk_buff *skb); /* chaining */
52 struct atm_qdisc_data *parent; /* parent qdisc */
53 struct socket *sock; /* for closing */
54 int ref; /* reference count */
55 struct gnet_stats_basic_packed bstats;
56 struct gnet_stats_queue qstats;
57 struct list_head list;
58 struct atm_flow_data *excess; /* flow for excess traffic;
59 NULL to set CLP instead */
61 unsigned char hdr[]; /* header data; MUST BE LAST */
64 struct atm_qdisc_data {
65 struct atm_flow_data link; /* unclassified skbs go here */
66 struct list_head flows; /* NB: "link" is also on this
68 struct tasklet_struct task; /* dequeue tasklet */
71 /* ------------------------- Class/flow operations ------------------------- */
73 static inline struct atm_flow_data *lookup_flow(struct Qdisc *sch, u32 classid)
75 struct atm_qdisc_data *p = qdisc_priv(sch);
76 struct atm_flow_data *flow;
78 list_for_each_entry(flow, &p->flows, list) {
79 if (flow->common.classid == classid)
85 static int atm_tc_graft(struct Qdisc *sch, unsigned long arg,
86 struct Qdisc *new, struct Qdisc **old,
87 struct netlink_ext_ack *extack)
89 struct atm_qdisc_data *p = qdisc_priv(sch);
90 struct atm_flow_data *flow = (struct atm_flow_data *)arg;
92 pr_debug("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",
93 sch, p, flow, new, old);
94 if (list_empty(&flow->list))
105 static struct Qdisc *atm_tc_leaf(struct Qdisc *sch, unsigned long cl)
107 struct atm_flow_data *flow = (struct atm_flow_data *)cl;
109 pr_debug("atm_tc_leaf(sch %p,flow %p)\n", sch, flow);
110 return flow ? flow->q : NULL;
113 static unsigned long atm_tc_find(struct Qdisc *sch, u32 classid)
115 struct atm_qdisc_data *p __maybe_unused = qdisc_priv(sch);
116 struct atm_flow_data *flow;
118 pr_debug("%s(sch %p,[qdisc %p],classid %x)\n", __func__, sch, p, classid);
119 flow = lookup_flow(sch, classid);
120 pr_debug("%s: flow %p\n", __func__, flow);
121 return (unsigned long)flow;
124 static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
125 unsigned long parent, u32 classid)
127 struct atm_qdisc_data *p __maybe_unused = qdisc_priv(sch);
128 struct atm_flow_data *flow;
130 pr_debug("%s(sch %p,[qdisc %p],classid %x)\n", __func__, sch, p, classid);
131 flow = lookup_flow(sch, classid);
134 pr_debug("%s: flow %p\n", __func__, flow);
135 return (unsigned long)flow;
139 * atm_tc_put handles all destructions, including the ones that are explicitly
140 * requested (atm_tc_destroy, etc.). The assumption here is that we never drop
141 * anything that still seems to be in use.
143 static void atm_tc_put(struct Qdisc *sch, unsigned long cl)
145 struct atm_qdisc_data *p = qdisc_priv(sch);
146 struct atm_flow_data *flow = (struct atm_flow_data *)cl;
148 pr_debug("atm_tc_put(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
151 pr_debug("atm_tc_put: destroying\n");
152 list_del_init(&flow->list);
153 pr_debug("atm_tc_put: qdisc %p\n", flow->q);
155 tcf_block_put(flow->block);
157 pr_debug("atm_tc_put: f_count %ld\n",
158 file_count(flow->sock->file));
159 flow->vcc->pop = flow->old_pop;
160 sockfd_put(flow->sock);
163 atm_tc_put(sch, (unsigned long)flow->excess);
164 if (flow != &p->link)
167 * If flow == &p->link, the qdisc no longer works at this point and
168 * needs to be removed. (By the caller of atm_tc_put.)
172 static void sch_atm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
174 struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;
176 pr_debug("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n", vcc, skb, p);
177 VCC2FLOW(vcc)->old_pop(vcc, skb);
178 tasklet_schedule(&p->task);
181 static const u8 llc_oui_ip[] = {
182 0xaa, /* DSAP: non-ISO */
183 0xaa, /* SSAP: non-ISO */
184 0x03, /* Ctrl: Unnumbered Information Command PDU */
185 0x00, /* OUI: EtherType */
188 }; /* Ethertype IP (0800) */
190 static const struct nla_policy atm_policy[TCA_ATM_MAX + 1] = {
191 [TCA_ATM_FD] = { .type = NLA_U32 },
192 [TCA_ATM_EXCESS] = { .type = NLA_U32 },
195 static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
196 struct nlattr **tca, unsigned long *arg,
197 struct netlink_ext_ack *extack)
199 struct atm_qdisc_data *p = qdisc_priv(sch);
200 struct atm_flow_data *flow = (struct atm_flow_data *)*arg;
201 struct atm_flow_data *excess = NULL;
202 struct nlattr *opt = tca[TCA_OPTIONS];
203 struct nlattr *tb[TCA_ATM_MAX + 1];
205 int fd, error, hdr_len;
208 pr_debug("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
209 "flow %p,opt %p)\n", sch, p, classid, parent, flow, opt);
211 * The concept of parents doesn't apply for this qdisc.
213 if (parent && parent != TC_H_ROOT && parent != sch->handle)
216 * ATM classes cannot be changed. In order to change properties of the
217 * ATM connection, that socket needs to be modified directly (via the
218 * native ATM API. In order to send a flow to a different VC, the old
219 * class needs to be removed and a new one added. (This may be changed
227 error = nla_parse_nested_deprecated(tb, TCA_ATM_MAX, opt, atm_policy,
234 fd = nla_get_u32(tb[TCA_ATM_FD]);
235 pr_debug("atm_tc_change: fd %d\n", fd);
236 if (tb[TCA_ATM_HDR]) {
237 hdr_len = nla_len(tb[TCA_ATM_HDR]);
238 hdr = nla_data(tb[TCA_ATM_HDR]);
240 hdr_len = RFC1483LLC_LEN;
241 hdr = NULL; /* default LLC/SNAP for IP */
243 if (!tb[TCA_ATM_EXCESS])
246 excess = (struct atm_flow_data *)
247 atm_tc_find(sch, nla_get_u32(tb[TCA_ATM_EXCESS]));
251 pr_debug("atm_tc_change: type %d, payload %d, hdr_len %d\n",
252 opt->nla_type, nla_len(opt), hdr_len);
253 sock = sockfd_lookup(fd, &error);
255 return error; /* f_count++ */
256 pr_debug("atm_tc_change: f_count %ld\n", file_count(sock->file));
257 if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
261 /* @@@ should check if the socket is really operational or we'll crash
264 if (TC_H_MAJ(classid ^ sch->handle)) {
265 pr_debug("atm_tc_change: classid mismatch\n");
273 for (i = 1; i < 0x8000; i++) {
274 classid = TC_H_MAKE(sch->handle, 0x8000 | i);
275 cl = atm_tc_find(sch, classid);
280 pr_debug("atm_tc_change: new id %x\n", classid);
281 flow = kzalloc(sizeof(struct atm_flow_data) + hdr_len, GFP_KERNEL);
282 pr_debug("atm_tc_change: flow %p\n", flow);
288 error = tcf_block_get(&flow->block, &flow->filter_list, sch,
295 flow->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, classid,
298 flow->q = &noop_qdisc;
299 pr_debug("atm_tc_change: qdisc %p\n", flow->q);
301 flow->vcc = ATM_SD(sock); /* speedup */
302 flow->vcc->user_back = flow;
303 pr_debug("atm_tc_change: vcc %p\n", flow->vcc);
304 flow->old_pop = flow->vcc->pop;
306 flow->vcc->pop = sch_atm_pop;
307 flow->common.classid = classid;
309 flow->excess = excess;
310 list_add(&flow->list, &p->link.list);
311 flow->hdr_len = hdr_len;
313 memcpy(flow->hdr, hdr, hdr_len);
315 memcpy(flow->hdr, llc_oui_ip, sizeof(llc_oui_ip));
316 *arg = (unsigned long)flow;
323 static int atm_tc_delete(struct Qdisc *sch, unsigned long arg,
324 struct netlink_ext_ack *extack)
326 struct atm_qdisc_data *p = qdisc_priv(sch);
327 struct atm_flow_data *flow = (struct atm_flow_data *)arg;
329 pr_debug("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
330 if (list_empty(&flow->list))
332 if (rcu_access_pointer(flow->filter_list) || flow == &p->link)
335 * Reference count must be 2: one for "keepalive" (set at class
336 * creation), and one for the reference held when calling delete.
339 pr_err("atm_tc_delete: flow->ref == %d\n", flow->ref);
343 return -EBUSY; /* catch references via excess, etc. */
344 atm_tc_put(sch, arg);
348 static void atm_tc_walk(struct Qdisc *sch, struct qdisc_walker *walker)
350 struct atm_qdisc_data *p = qdisc_priv(sch);
351 struct atm_flow_data *flow;
353 pr_debug("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
356 list_for_each_entry(flow, &p->flows, list) {
357 if (walker->count >= walker->skip &&
358 walker->fn(sch, (unsigned long)flow, walker) < 0) {
366 static struct tcf_block *atm_tc_tcf_block(struct Qdisc *sch, unsigned long cl,
367 struct netlink_ext_ack *extack)
369 struct atm_qdisc_data *p = qdisc_priv(sch);
370 struct atm_flow_data *flow = (struct atm_flow_data *)cl;
372 pr_debug("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
373 return flow ? flow->block : p->link.block;
376 /* --------------------------- Qdisc operations ---------------------------- */
378 static int atm_tc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
379 struct sk_buff **to_free)
381 struct atm_qdisc_data *p = qdisc_priv(sch);
382 struct atm_flow_data *flow;
383 struct tcf_result res;
385 int ret = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
387 pr_debug("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
388 result = TC_ACT_OK; /* be nice to gcc */
390 if (TC_H_MAJ(skb->priority) != sch->handle ||
391 !(flow = (struct atm_flow_data *)atm_tc_find(sch, skb->priority))) {
392 struct tcf_proto *fl;
394 list_for_each_entry(flow, &p->flows, list) {
395 fl = rcu_dereference_bh(flow->filter_list);
397 result = tcf_classify(skb, NULL, fl, &res, true);
400 flow = (struct atm_flow_data *)res.class;
402 flow = lookup_flow(sch, res.classid);
414 ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
415 /*@@@ looks good ... but it's not supposed to work :-) */
416 #ifdef CONFIG_NET_CLS_ACT
421 __qdisc_drop(skb, to_free);
422 return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
424 __qdisc_drop(skb, to_free);
426 case TC_ACT_RECLASSIFY:
430 ATM_SKB(skb)->atm_options |= ATM_ATMOPT_CLP;
436 ret = qdisc_enqueue(skb, flow->q, to_free);
437 if (ret != NET_XMIT_SUCCESS) {
439 if (net_xmit_drop_count(ret)) {
440 qdisc_qstats_drop(sch);
442 flow->qstats.drops++;
447 * Okay, this may seem weird. We pretend we've dropped the packet if
448 * it goes via ATM. The reason for this is that the outer qdisc
449 * expects to be able to q->dequeue the packet later on if we return
450 * success at this place. Also, sch->q.qdisc needs to reflect whether
451 * there is a packet egligible for dequeuing or not. Note that the
452 * statistics of the outer qdisc are necessarily wrong because of all
453 * this. There's currently no correct solution for this.
455 if (flow == &p->link) {
457 return NET_XMIT_SUCCESS;
459 tasklet_schedule(&p->task);
460 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
464 * Dequeue packets and send them over ATM. Note that we quite deliberately
465 * avoid checking net_device's flow control here, simply because sch_atm
466 * uses its own channels, which have nothing to do with any CLIP/LANE/or
467 * non-ATM interfaces.
470 static void sch_atm_dequeue(struct tasklet_struct *t)
472 struct atm_qdisc_data *p = from_tasklet(p, t, task);
473 struct Qdisc *sch = qdisc_from_priv(p);
474 struct atm_flow_data *flow;
477 pr_debug("sch_atm_dequeue(sch %p,[qdisc %p])\n", sch, p);
478 list_for_each_entry(flow, &p->flows, list) {
479 if (flow == &p->link)
482 * If traffic is properly shaped, this won't generate nasty
483 * little bursts. Otherwise, it may ... (but that's okay)
485 while ((skb = flow->q->ops->peek(flow->q))) {
486 if (!atm_may_send(flow->vcc, skb->truesize))
489 skb = qdisc_dequeue_peeked(flow->q);
493 qdisc_bstats_update(sch, skb);
494 bstats_update(&flow->bstats, skb);
495 pr_debug("atm_tc_dequeue: sending on class %p\n", flow);
496 /* remove any LL header somebody else has attached */
497 skb_pull(skb, skb_network_offset(skb));
498 if (skb_headroom(skb) < flow->hdr_len) {
501 new = skb_realloc_headroom(skb, flow->hdr_len);
507 pr_debug("sch_atm_dequeue: ip %p, data %p\n",
508 skb_network_header(skb), skb->data);
509 ATM_SKB(skb)->vcc = flow->vcc;
510 memcpy(skb_push(skb, flow->hdr_len), flow->hdr,
512 refcount_add(skb->truesize,
513 &sk_atm(flow->vcc)->sk_wmem_alloc);
514 /* atm.atm_options are already set by atm_tc_enqueue */
515 flow->vcc->send(flow->vcc, skb);
520 static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
522 struct atm_qdisc_data *p = qdisc_priv(sch);
525 pr_debug("atm_tc_dequeue(sch %p,[qdisc %p])\n", sch, p);
526 tasklet_schedule(&p->task);
527 skb = qdisc_dequeue_peeked(p->link.q);
533 static struct sk_buff *atm_tc_peek(struct Qdisc *sch)
535 struct atm_qdisc_data *p = qdisc_priv(sch);
537 pr_debug("atm_tc_peek(sch %p,[qdisc %p])\n", sch, p);
539 return p->link.q->ops->peek(p->link.q);
542 static int atm_tc_init(struct Qdisc *sch, struct nlattr *opt,
543 struct netlink_ext_ack *extack)
545 struct atm_qdisc_data *p = qdisc_priv(sch);
548 pr_debug("atm_tc_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
549 INIT_LIST_HEAD(&p->flows);
550 INIT_LIST_HEAD(&p->link.list);
551 list_add(&p->link.list, &p->flows);
552 p->link.q = qdisc_create_dflt(sch->dev_queue,
553 &pfifo_qdisc_ops, sch->handle, extack);
555 p->link.q = &noop_qdisc;
556 pr_debug("atm_tc_init: link (%p) qdisc %p\n", &p->link, p->link.q);
559 p->link.common.classid = sch->handle;
562 err = tcf_block_get(&p->link.block, &p->link.filter_list, sch,
567 tasklet_setup(&p->task, sch_atm_dequeue);
571 static void atm_tc_reset(struct Qdisc *sch)
573 struct atm_qdisc_data *p = qdisc_priv(sch);
574 struct atm_flow_data *flow;
576 pr_debug("atm_tc_reset(sch %p,[qdisc %p])\n", sch, p);
577 list_for_each_entry(flow, &p->flows, list)
578 qdisc_reset(flow->q);
582 static void atm_tc_destroy(struct Qdisc *sch)
584 struct atm_qdisc_data *p = qdisc_priv(sch);
585 struct atm_flow_data *flow, *tmp;
587 pr_debug("atm_tc_destroy(sch %p,[qdisc %p])\n", sch, p);
588 list_for_each_entry(flow, &p->flows, list) {
589 tcf_block_put(flow->block);
593 list_for_each_entry_safe(flow, tmp, &p->flows, list) {
595 pr_err("atm_destroy: %p->ref = %d\n", flow, flow->ref);
596 atm_tc_put(sch, (unsigned long)flow);
598 tasklet_kill(&p->task);
601 static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
602 struct sk_buff *skb, struct tcmsg *tcm)
604 struct atm_qdisc_data *p = qdisc_priv(sch);
605 struct atm_flow_data *flow = (struct atm_flow_data *)cl;
608 pr_debug("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
609 sch, p, flow, skb, tcm);
610 if (list_empty(&flow->list))
612 tcm->tcm_handle = flow->common.classid;
613 tcm->tcm_info = flow->q->handle;
615 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
617 goto nla_put_failure;
619 if (nla_put(skb, TCA_ATM_HDR, flow->hdr_len, flow->hdr))
620 goto nla_put_failure;
622 struct sockaddr_atmpvc pvc;
625 memset(&pvc, 0, sizeof(pvc));
626 pvc.sap_family = AF_ATMPVC;
627 pvc.sap_addr.itf = flow->vcc->dev ? flow->vcc->dev->number : -1;
628 pvc.sap_addr.vpi = flow->vcc->vpi;
629 pvc.sap_addr.vci = flow->vcc->vci;
630 if (nla_put(skb, TCA_ATM_ADDR, sizeof(pvc), &pvc))
631 goto nla_put_failure;
632 state = ATM_VF2VS(flow->vcc->flags);
633 if (nla_put_u32(skb, TCA_ATM_STATE, state))
634 goto nla_put_failure;
637 if (nla_put_u32(skb, TCA_ATM_EXCESS, flow->common.classid))
638 goto nla_put_failure;
640 if (nla_put_u32(skb, TCA_ATM_EXCESS, 0))
641 goto nla_put_failure;
643 return nla_nest_end(skb, nest);
646 nla_nest_cancel(skb, nest);
650 atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
653 struct atm_flow_data *flow = (struct atm_flow_data *)arg;
655 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
656 d, NULL, &flow->bstats) < 0 ||
657 gnet_stats_copy_queue(d, NULL, &flow->qstats, flow->q->q.qlen) < 0)
663 static int atm_tc_dump(struct Qdisc *sch, struct sk_buff *skb)
668 static const struct Qdisc_class_ops atm_class_ops = {
669 .graft = atm_tc_graft,
672 .change = atm_tc_change,
673 .delete = atm_tc_delete,
675 .tcf_block = atm_tc_tcf_block,
676 .bind_tcf = atm_tc_bind_filter,
677 .unbind_tcf = atm_tc_put,
678 .dump = atm_tc_dump_class,
679 .dump_stats = atm_tc_dump_class_stats,
682 static struct Qdisc_ops atm_qdisc_ops __read_mostly = {
683 .cl_ops = &atm_class_ops,
685 .priv_size = sizeof(struct atm_qdisc_data),
686 .enqueue = atm_tc_enqueue,
687 .dequeue = atm_tc_dequeue,
690 .reset = atm_tc_reset,
691 .destroy = atm_tc_destroy,
693 .owner = THIS_MODULE,
696 static int __init atm_init(void)
698 return register_qdisc(&atm_qdisc_ops);
701 static void __exit atm_exit(void)
703 unregister_qdisc(&atm_qdisc_ops);
706 module_init(atm_init)
707 module_exit(atm_exit)
708 MODULE_LICENSE("GPL");