sch_atm.c 19.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */

/* Written 1998-2000 by Werner Almesberger, EPFL ICA */

#include <linux/module.h>
6
#include <linux/slab.h>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13
#include <linux/init.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/atmdev.h>
#include <linux/atmclip.h>
#include <linux/rtnetlink.h>
P
Patrick McHardy 已提交
14
#include <linux/file.h>		/* for fput */
15
#include <net/netlink.h>
L
Linus Torvalds 已提交
16 17
#include <net/pkt_sched.h>

P
Patrick McHardy 已提交
18
extern struct socket *sockfd_lookup(int fd, int *err);	/* @@@ fix this */
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/*
 * The ATM queuing discipline provides a framework for invoking classifiers
 * (aka "filters"), which in turn select classes of this queuing discipline.
 * Each class maps the flow(s) it is handling to a given VC. Multiple classes
 * may share the same VC.
 *
 * When creating a class, VCs are specified by passing the number of the open
 * socket descriptor by which the calling process references the VC. The kernel
 * keeps the VC open at least until all classes using it are removed.
 *
 * In this file, most functions are named atm_tc_* to avoid confusion with all
 * the atm_* in net/atm. This naming convention differs from what's used in the
 * rest of net/sched.
 *
 * Known bugs:
 *  - sometimes messes up the IP stack
 *  - any manipulations besides the few operations described in the README, are
 *    untested and likely to crash the system
 *  - should lock the flow while there is data in the queue (?)
 */

#define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))

struct atm_flow_data {
P
Patrick McHardy 已提交
44
	struct Qdisc		*q;	/* FIFO, TBF, etc. */
L
Linus Torvalds 已提交
45
	struct tcf_proto	*filter_list;
P
Patrick McHardy 已提交
46 47
	struct atm_vcc		*vcc;	/* VCC; NULL if VCC is closed */
	void			(*old_pop)(struct atm_vcc *vcc,
48
					   struct sk_buff *skb); /* chaining */
L
Linus Torvalds 已提交
49 50 51 52
	struct atm_qdisc_data	*parent;	/* parent qdisc */
	struct socket		*sock;		/* for closing */
	u32			classid;	/* x:y type ID */
	int			ref;		/* reference count */
53
	struct gnet_stats_basic_packed	bstats;
L
Linus Torvalds 已提交
54
	struct gnet_stats_queue	qstats;
55
	struct list_head	list;
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63
	struct atm_flow_data	*excess;	/* flow for excess traffic;
						   NULL to set CLP instead */
	int			hdr_len;
	unsigned char		hdr[0];		/* header data; MUST BE LAST */
};

struct atm_qdisc_data {
	struct atm_flow_data	link;		/* unclassified skbs go here */
64
	struct list_head	flows;		/* NB: "link" is also on this
L
Linus Torvalds 已提交
65
						   list */
66
	struct tasklet_struct	task;		/* dequeue tasklet */
L
Linus Torvalds 已提交
67 68 69 70
};

/* ------------------------- Class/flow operations ------------------------- */

P
Patrick McHardy 已提交
71
static inline struct atm_flow_data *lookup_flow(struct Qdisc *sch, u32 classid)
L
Linus Torvalds 已提交
72
{
73
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
74 75
	struct atm_flow_data *flow;

76
	list_for_each_entry(flow, &p->flows, list) {
P
Patrick McHardy 已提交
77
		if (flow->classid == classid)
78 79 80
			return flow;
	}
	return NULL;
L
Linus Torvalds 已提交
81 82
}

P
Patrick McHardy 已提交
83 84
static int atm_tc_graft(struct Qdisc *sch, unsigned long arg,
			struct Qdisc *new, struct Qdisc **old)
L
Linus Torvalds 已提交
85
{
86
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
87 88
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;

89
	pr_debug("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",
P
Patrick McHardy 已提交
90
		sch, p, flow, new, old);
91
	if (list_empty(&flow->list))
P
Patrick McHardy 已提交
92 93 94
		return -EINVAL;
	if (!new)
		new = &noop_qdisc;
95 96
	*old = flow->q;
	flow->q = new;
P
Patrick McHardy 已提交
97 98
	if (*old)
		qdisc_reset(*old);
99
	return 0;
L
Linus Torvalds 已提交
100 101
}

P
Patrick McHardy 已提交
102
static struct Qdisc *atm_tc_leaf(struct Qdisc *sch, unsigned long cl)
L
Linus Torvalds 已提交
103
{
P
Patrick McHardy 已提交
104
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
105

106
	pr_debug("atm_tc_leaf(sch %p,flow %p)\n", sch, flow);
L
Linus Torvalds 已提交
107 108 109
	return flow ? flow->q : NULL;
}

P
Patrick McHardy 已提交
110
static unsigned long atm_tc_get(struct Qdisc *sch, u32 classid)
L
Linus Torvalds 已提交
111
{
112
	struct atm_qdisc_data *p __maybe_unused = qdisc_priv(sch);
L
Linus Torvalds 已提交
113 114
	struct atm_flow_data *flow;

115
	pr_debug("atm_tc_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
P
Patrick McHardy 已提交
116 117 118
	flow = lookup_flow(sch, classid);
	if (flow)
		flow->ref++;
119
	pr_debug("atm_tc_get: flow %p\n", flow);
P
Patrick McHardy 已提交
120
	return (unsigned long)flow;
L
Linus Torvalds 已提交
121 122 123
}

static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
P
Patrick McHardy 已提交
124
					unsigned long parent, u32 classid)
L
Linus Torvalds 已提交
125
{
P
Patrick McHardy 已提交
126
	return atm_tc_get(sch, classid);
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135
}

/*
 * atm_tc_put handles all destructions, including the ones that are explicitly
 * requested (atm_tc_destroy, etc.). The assumption here is that we never drop
 * anything that still seems to be in use.
 */
static void atm_tc_put(struct Qdisc *sch, unsigned long cl)
{
136
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
137
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
138

139
	pr_debug("atm_tc_put(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
P
Patrick McHardy 已提交
140 141
	if (--flow->ref)
		return;
142
	pr_debug("atm_tc_put: destroying\n");
143
	list_del_init(&flow->list);
144
	pr_debug("atm_tc_put: qdisc %p\n", flow->q);
L
Linus Torvalds 已提交
145
	qdisc_destroy(flow->q);
146
	tcf_destroy_chain(&flow->filter_list);
L
Linus Torvalds 已提交
147
	if (flow->sock) {
A
Al Viro 已提交
148
		pr_debug("atm_tc_put: f_count %ld\n",
P
Patrick McHardy 已提交
149
			file_count(flow->sock->file));
L
Linus Torvalds 已提交
150 151 152
		flow->vcc->pop = flow->old_pop;
		sockfd_put(flow->sock);
	}
P
Patrick McHardy 已提交
153 154 155 156
	if (flow->excess)
		atm_tc_put(sch, (unsigned long)flow->excess);
	if (flow != &p->link)
		kfree(flow);
L
Linus Torvalds 已提交
157 158 159 160 161 162
	/*
	 * If flow == &p->link, the qdisc no longer works at this point and
	 * needs to be removed. (By the caller of atm_tc_put.)
	 */
}

P
Patrick McHardy 已提交
163
static void sch_atm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
164 165 166
{
	struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;

167
	pr_debug("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n", vcc, skb, p);
P
Patrick McHardy 已提交
168
	VCC2FLOW(vcc)->old_pop(vcc, skb);
L
Linus Torvalds 已提交
169 170 171 172
	tasklet_schedule(&p->task);
}

static const u8 llc_oui_ip[] = {
P
Patrick McHardy 已提交
173 174 175 176
	0xaa,			/* DSAP: non-ISO */
	0xaa,			/* SSAP: non-ISO */
	0x03,			/* Ctrl: Unnumbered Information Command PDU */
	0x00,			/* OUI: EtherType */
L
Linus Torvalds 已提交
177
	0x00, 0x00,
P
Patrick McHardy 已提交
178 179
	0x08, 0x00
};				/* Ethertype IP (0800) */
L
Linus Torvalds 已提交
180

181 182 183 184 185
static const struct nla_policy atm_policy[TCA_ATM_MAX + 1] = {
	[TCA_ATM_FD]		= { .type = NLA_U32 },
	[TCA_ATM_EXCESS]	= { .type = NLA_U32 },
};

L
Linus Torvalds 已提交
186
static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
187
			 struct nlattr **tca, unsigned long *arg)
L
Linus Torvalds 已提交
188
{
189
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
190
	struct atm_flow_data *flow = (struct atm_flow_data *)*arg;
L
Linus Torvalds 已提交
191
	struct atm_flow_data *excess = NULL;
192 193
	struct nlattr *opt = tca[TCA_OPTIONS];
	struct nlattr *tb[TCA_ATM_MAX + 1];
L
Linus Torvalds 已提交
194
	struct socket *sock;
P
Patrick McHardy 已提交
195
	int fd, error, hdr_len;
L
Linus Torvalds 已提交
196 197
	void *hdr;

198
	pr_debug("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
P
Patrick McHardy 已提交
199
		"flow %p,opt %p)\n", sch, p, classid, parent, flow, opt);
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210 211
	/*
	 * The concept of parents doesn't apply for this qdisc.
	 */
	if (parent && parent != TC_H_ROOT && parent != sch->handle)
		return -EINVAL;
	/*
	 * ATM classes cannot be changed. In order to change properties of the
	 * ATM connection, that socket needs to be modified directly (via the
	 * native ATM API. In order to send a flow to a different VC, the old
	 * class needs to be removed and a new one added. (This may be changed
	 * later.)
	 */
P
Patrick McHardy 已提交
212 213
	if (flow)
		return -EBUSY;
214
	if (opt == NULL)
L
Linus Torvalds 已提交
215
		return -EINVAL;
216 217

	error = nla_parse_nested(tb, TCA_ATM_MAX, opt, atm_policy);
218 219 220
	if (error < 0)
		return error;

221
	if (!tb[TCA_ATM_FD])
L
Linus Torvalds 已提交
222
		return -EINVAL;
223
	fd = nla_get_u32(tb[TCA_ATM_FD]);
224
	pr_debug("atm_tc_change: fd %d\n", fd);
225 226 227
	if (tb[TCA_ATM_HDR]) {
		hdr_len = nla_len(tb[TCA_ATM_HDR]);
		hdr = nla_data(tb[TCA_ATM_HDR]);
P
Patrick McHardy 已提交
228
	} else {
L
Linus Torvalds 已提交
229
		hdr_len = RFC1483LLC_LEN;
P
Patrick McHardy 已提交
230
		hdr = NULL;	/* default LLC/SNAP for IP */
L
Linus Torvalds 已提交
231
	}
232
	if (!tb[TCA_ATM_EXCESS])
P
Patrick McHardy 已提交
233
		excess = NULL;
L
Linus Torvalds 已提交
234
	else {
P
Patrick McHardy 已提交
235
		excess = (struct atm_flow_data *)
236
			atm_tc_get(sch, nla_get_u32(tb[TCA_ATM_EXCESS]));
P
Patrick McHardy 已提交
237 238
		if (!excess)
			return -ENOENT;
L
Linus Torvalds 已提交
239
	}
240
	pr_debug("atm_tc_change: type %d, payload %d, hdr_len %d\n",
241
		 opt->nla_type, nla_len(opt), hdr_len);
242 243
	sock = sockfd_lookup(fd, &error);
	if (!sock)
P
Patrick McHardy 已提交
244
		return error;	/* f_count++ */
A
Al Viro 已提交
245
	pr_debug("atm_tc_change: f_count %ld\n", file_count(sock->file));
246
	if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
L
Linus Torvalds 已提交
247
		error = -EPROTOTYPE;
248
		goto err_out;
L
Linus Torvalds 已提交
249 250 251 252 253
	}
	/* @@@ should check if the socket is really operational or we'll crash
	   on vcc->send */
	if (classid) {
		if (TC_H_MAJ(classid ^ sch->handle)) {
254
			pr_debug("atm_tc_change: classid mismatch\n");
L
Linus Torvalds 已提交
255 256 257
			error = -EINVAL;
			goto err_out;
		}
P
Patrick McHardy 已提交
258
	} else {
L
Linus Torvalds 已提交
259 260 261 262
		int i;
		unsigned long cl;

		for (i = 1; i < 0x8000; i++) {
P
Patrick McHardy 已提交
263
			classid = TC_H_MAKE(sch->handle, 0x8000 | i);
264 265
			cl = atm_tc_get(sch, classid);
			if (!cl)
P
Patrick McHardy 已提交
266 267
				break;
			atm_tc_put(sch, cl);
L
Linus Torvalds 已提交
268 269
		}
	}
270
	pr_debug("atm_tc_change: new id %x\n", classid);
271
	flow = kzalloc(sizeof(struct atm_flow_data) + hdr_len, GFP_KERNEL);
272
	pr_debug("atm_tc_change: flow %p\n", flow);
L
Linus Torvalds 已提交
273 274 275 276 277
	if (!flow) {
		error = -ENOBUFS;
		goto err_out;
	}
	flow->filter_list = NULL;
278
	flow->q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
279
				    &pfifo_qdisc_ops, classid);
280
	if (!flow->q)
L
Linus Torvalds 已提交
281
		flow->q = &noop_qdisc;
282
	pr_debug("atm_tc_change: qdisc %p\n", flow->q);
L
Linus Torvalds 已提交
283
	flow->sock = sock;
P
Patrick McHardy 已提交
284
	flow->vcc = ATM_SD(sock);	/* speedup */
L
Linus Torvalds 已提交
285
	flow->vcc->user_back = flow;
286
	pr_debug("atm_tc_change: vcc %p\n", flow->vcc);
L
Linus Torvalds 已提交
287 288 289 290 291 292
	flow->old_pop = flow->vcc->pop;
	flow->parent = p;
	flow->vcc->pop = sch_atm_pop;
	flow->classid = classid;
	flow->ref = 1;
	flow->excess = excess;
293
	list_add(&flow->list, &p->link.list);
L
Linus Torvalds 已提交
294 295
	flow->hdr_len = hdr_len;
	if (hdr)
P
Patrick McHardy 已提交
296
		memcpy(flow->hdr, hdr, hdr_len);
L
Linus Torvalds 已提交
297
	else
P
Patrick McHardy 已提交
298 299
		memcpy(flow->hdr, llc_oui_ip, sizeof(llc_oui_ip));
	*arg = (unsigned long)flow;
L
Linus Torvalds 已提交
300 301
	return 0;
err_out:
P
Patrick McHardy 已提交
302 303
	if (excess)
		atm_tc_put(sch, (unsigned long)excess);
L
Linus Torvalds 已提交
304 305 306 307
	sockfd_put(sock);
	return error;
}

P
Patrick McHardy 已提交
308
static int atm_tc_delete(struct Qdisc *sch, unsigned long arg)
L
Linus Torvalds 已提交
309
{
310
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
311
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
L
Linus Torvalds 已提交
312

313
	pr_debug("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
314
	if (list_empty(&flow->list))
P
Patrick McHardy 已提交
315 316 317
		return -EINVAL;
	if (flow->filter_list || flow == &p->link)
		return -EBUSY;
L
Linus Torvalds 已提交
318 319 320 321 322
	/*
	 * Reference count must be 2: one for "keepalive" (set at class
	 * creation), and one for the reference held when calling delete.
	 */
	if (flow->ref < 2) {
P
Patrick McHardy 已提交
323
		printk(KERN_ERR "atm_tc_delete: flow->ref == %d\n", flow->ref);
L
Linus Torvalds 已提交
324 325
		return -EINVAL;
	}
P
Patrick McHardy 已提交
326 327 328
	if (flow->ref > 2)
		return -EBUSY;	/* catch references via excess, etc. */
	atm_tc_put(sch, arg);
L
Linus Torvalds 已提交
329 330 331
	return 0;
}

P
Patrick McHardy 已提交
332
static void atm_tc_walk(struct Qdisc *sch, struct qdisc_walker *walker)
L
Linus Torvalds 已提交
333
{
334
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
335 336
	struct atm_flow_data *flow;

337
	pr_debug("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
P
Patrick McHardy 已提交
338 339
	if (walker->stop)
		return;
340 341 342 343 344 345
	list_for_each_entry(flow, &p->flows, list) {
		if (walker->count >= walker->skip &&
		    walker->fn(sch, (unsigned long)flow, walker) < 0) {
			walker->stop = 1;
			break;
		}
L
Linus Torvalds 已提交
346 347 348 349
		walker->count++;
	}
}

P
Patrick McHardy 已提交
350
static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch, unsigned long cl)
L
Linus Torvalds 已提交
351
{
352
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
353
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
354

355
	pr_debug("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
356
	return flow ? &flow->filter_list : &p->link.filter_list;
L
Linus Torvalds 已提交
357 358 359 360
}

/* --------------------------- Qdisc operations ---------------------------- */

P
Patrick McHardy 已提交
361
static int atm_tc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
L
Linus Torvalds 已提交
362
{
363
	struct atm_qdisc_data *p = qdisc_priv(sch);
364
	struct atm_flow_data *flow;
L
Linus Torvalds 已提交
365 366 367 368
	struct tcf_result res;
	int result;
	int ret = NET_XMIT_POLICED;

369
	pr_debug("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
P
Patrick McHardy 已提交
370
	result = TC_POLICE_OK;	/* be nice to gcc */
371
	flow = NULL;
L
Linus Torvalds 已提交
372
	if (TC_H_MAJ(skb->priority) != sch->handle ||
373 374
	    !(flow = (struct atm_flow_data *)atm_tc_get(sch, skb->priority))) {
		list_for_each_entry(flow, &p->flows, list) {
L
Linus Torvalds 已提交
375
			if (flow->filter_list) {
376 377 378
				result = tc_classify_compat(skb,
							    flow->filter_list,
							    &res);
P
Patrick McHardy 已提交
379 380 381 382 383
				if (result < 0)
					continue;
				flow = (struct atm_flow_data *)res.class;
				if (!flow)
					flow = lookup_flow(sch, res.classid);
384
				goto done;
L
Linus Torvalds 已提交
385
			}
386 387 388 389 390
		}
		flow = NULL;
	done:
		;		
	}
P
Patrick McHardy 已提交
391 392
	if (!flow)
		flow = &p->link;
L
Linus Torvalds 已提交
393 394 395
	else {
		if (flow->vcc)
			ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
P
Patrick McHardy 已提交
396
		/*@@@ looks good ... but it's not supposed to work :-) */
397 398 399 400 401
#ifdef CONFIG_NET_CLS_ACT
		switch (result) {
		case TC_ACT_QUEUED:
		case TC_ACT_STOLEN:
			kfree_skb(skb);
402
			return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
403 404 405
		case TC_ACT_SHOT:
			kfree_skb(skb);
			goto drop;
406 407 408 409 410 411
		case TC_POLICE_RECLASSIFY:
			if (flow->excess)
				flow = flow->excess;
			else
				ATM_SKB(skb)->atm_options |= ATM_ATMOPT_CLP;
			break;
412
		}
L
Linus Torvalds 已提交
413 414
#endif
	}
415

416
	ret = qdisc_enqueue(skb, flow->q);
417
	if (ret != NET_XMIT_SUCCESS) {
418
drop: __maybe_unused
419 420 421 422 423
		if (net_xmit_drop_count(ret)) {
			sch->qstats.drops++;
			if (flow)
				flow->qstats.drops++;
		}
L
Linus Torvalds 已提交
424 425
		return ret;
	}
426
	sch->bstats.bytes += qdisc_pkt_len(skb);
L
Linus Torvalds 已提交
427
	sch->bstats.packets++;
428
	flow->bstats.bytes += qdisc_pkt_len(skb);
L
Linus Torvalds 已提交
429 430 431 432 433 434 435 436 437 438 439 440
	flow->bstats.packets++;
	/*
	 * Okay, this may seem weird. We pretend we've dropped the packet if
	 * it goes via ATM. The reason for this is that the outer qdisc
	 * expects to be able to q->dequeue the packet later on if we return
	 * success at this place. Also, sch->q.qdisc needs to reflect whether
	 * there is a packet egligible for dequeuing or not. Note that the
	 * statistics of the outer qdisc are necessarily wrong because of all
	 * this. There's currently no correct solution for this.
	 */
	if (flow == &p->link) {
		sch->q.qlen++;
441
		return NET_XMIT_SUCCESS;
L
Linus Torvalds 已提交
442 443
	}
	tasklet_schedule(&p->task);
444
	return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455
}

/*
 * Dequeue packets and send them over ATM. Note that we quite deliberately
 * avoid checking net_device's flow control here, simply because sch_atm
 * uses its own channels, which have nothing to do with any CLIP/LANE/or
 * non-ATM interfaces.
 */

static void sch_atm_dequeue(unsigned long data)
{
P
Patrick McHardy 已提交
456
	struct Qdisc *sch = (struct Qdisc *)data;
457
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
458 459 460
	struct atm_flow_data *flow;
	struct sk_buff *skb;

461
	pr_debug("sch_atm_dequeue(sch %p,[qdisc %p])\n", sch, p);
462 463 464
	list_for_each_entry(flow, &p->flows, list) {
		if (flow == &p->link)
			continue;
L
Linus Torvalds 已提交
465 466 467 468
		/*
		 * If traffic is properly shaped, this won't generate nasty
		 * little bursts. Otherwise, it may ... (but that's okay)
		 */
469 470
		while ((skb = flow->q->ops->peek(flow->q))) {
			if (!atm_may_send(flow->vcc, skb->truesize))
L
Linus Torvalds 已提交
471
				break;
472

473
			skb = qdisc_dequeue_peeked(flow->q);
474 475 476
			if (unlikely(!skb))
				break;

477
			pr_debug("atm_tc_dequeue: sending on class %p\n", flow);
L
Linus Torvalds 已提交
478
			/* remove any LL header somebody else has attached */
479
			skb_pull(skb, skb_network_offset(skb));
L
Linus Torvalds 已提交
480 481 482
			if (skb_headroom(skb) < flow->hdr_len) {
				struct sk_buff *new;

P
Patrick McHardy 已提交
483
				new = skb_realloc_headroom(skb, flow->hdr_len);
L
Linus Torvalds 已提交
484
				dev_kfree_skb(skb);
P
Patrick McHardy 已提交
485 486
				if (!new)
					continue;
L
Linus Torvalds 已提交
487 488
				skb = new;
			}
489
			pr_debug("sch_atm_dequeue: ip %p, data %p\n",
490
				 skb_network_header(skb), skb->data);
L
Linus Torvalds 已提交
491
			ATM_SKB(skb)->vcc = flow->vcc;
P
Patrick McHardy 已提交
492 493
			memcpy(skb_push(skb, flow->hdr_len), flow->hdr,
			       flow->hdr_len);
L
Linus Torvalds 已提交
494 495 496
			atomic_add(skb->truesize,
				   &sk_atm(flow->vcc)->sk_wmem_alloc);
			/* atm.atm_options are already set by atm_tc_enqueue */
P
Patrick McHardy 已提交
497
			flow->vcc->send(flow->vcc, skb);
L
Linus Torvalds 已提交
498
		}
499
	}
L
Linus Torvalds 已提交
500 501 502 503
}

static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
{
504
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
505 506
	struct sk_buff *skb;

507
	pr_debug("atm_tc_dequeue(sch %p,[qdisc %p])\n", sch, p);
L
Linus Torvalds 已提交
508
	tasklet_schedule(&p->task);
509
	skb = qdisc_dequeue_peeked(p->link.q);
P
Patrick McHardy 已提交
510 511
	if (skb)
		sch->q.qlen--;
L
Linus Torvalds 已提交
512 513 514
	return skb;
}

515 516 517 518 519 520 521 522 523
static struct sk_buff *atm_tc_peek(struct Qdisc *sch)
{
	struct atm_qdisc_data *p = qdisc_priv(sch);

	pr_debug("atm_tc_peek(sch %p,[qdisc %p])\n", sch, p);

	return p->link.q->ops->peek(p->link.q);
}

L
Linus Torvalds 已提交
524 525
static unsigned int atm_tc_drop(struct Qdisc *sch)
{
526
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
527 528 529
	struct atm_flow_data *flow;
	unsigned int len;

530
	pr_debug("atm_tc_drop(sch %p,[qdisc %p])\n", sch, p);
531
	list_for_each_entry(flow, &p->flows, list) {
L
Linus Torvalds 已提交
532 533
		if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
			return len;
534
	}
L
Linus Torvalds 已提交
535 536 537
	return 0;
}

538
static int atm_tc_init(struct Qdisc *sch, struct nlattr *opt)
L
Linus Torvalds 已提交
539
{
540
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
541

542
	pr_debug("atm_tc_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
543 544 545
	INIT_LIST_HEAD(&p->flows);
	INIT_LIST_HEAD(&p->link.list);
	list_add(&p->link.list, &p->flows);
546
	p->link.q = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
547
				      &pfifo_qdisc_ops, sch->handle);
548
	if (!p->link.q)
L
Linus Torvalds 已提交
549
		p->link.q = &noop_qdisc;
550
	pr_debug("atm_tc_init: link (%p) qdisc %p\n", &p->link, p->link.q);
L
Linus Torvalds 已提交
551 552 553 554 555
	p->link.filter_list = NULL;
	p->link.vcc = NULL;
	p->link.sock = NULL;
	p->link.classid = sch->handle;
	p->link.ref = 1;
P
Patrick McHardy 已提交
556
	tasklet_init(&p->task, sch_atm_dequeue, (unsigned long)sch);
L
Linus Torvalds 已提交
557 558 559 560 561
	return 0;
}

static void atm_tc_reset(struct Qdisc *sch)
{
562
	struct atm_qdisc_data *p = qdisc_priv(sch);
L
Linus Torvalds 已提交
563 564
	struct atm_flow_data *flow;

565
	pr_debug("atm_tc_reset(sch %p,[qdisc %p])\n", sch, p);
566
	list_for_each_entry(flow, &p->flows, list)
P
Patrick McHardy 已提交
567
		qdisc_reset(flow->q);
L
Linus Torvalds 已提交
568 569 570 571 572
	sch->q.qlen = 0;
}

static void atm_tc_destroy(struct Qdisc *sch)
{
573
	struct atm_qdisc_data *p = qdisc_priv(sch);
574
	struct atm_flow_data *flow, *tmp;
L
Linus Torvalds 已提交
575

576
	pr_debug("atm_tc_destroy(sch %p,[qdisc %p])\n", sch, p);
577
	list_for_each_entry(flow, &p->flows, list)
578 579
		tcf_destroy_chain(&flow->filter_list);

580
	list_for_each_entry_safe(flow, tmp, &p->flows, list) {
L
Linus Torvalds 已提交
581
		if (flow->ref > 1)
P
Patrick McHardy 已提交
582 583 584
			printk(KERN_ERR "atm_destroy: %p->ref = %d\n", flow,
			       flow->ref);
		atm_tc_put(sch, (unsigned long)flow);
L
Linus Torvalds 已提交
585 586 587 588 589
	}
	tasklet_kill(&p->task);
}

static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
P
Patrick McHardy 已提交
590
			     struct sk_buff *skb, struct tcmsg *tcm)
L
Linus Torvalds 已提交
591
{
592
	struct atm_qdisc_data *p = qdisc_priv(sch);
P
Patrick McHardy 已提交
593
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
594
	struct nlattr *nest;
L
Linus Torvalds 已提交
595

596
	pr_debug("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
P
Patrick McHardy 已提交
597
		sch, p, flow, skb, tcm);
598
	if (list_empty(&flow->list))
P
Patrick McHardy 已提交
599
		return -EINVAL;
L
Linus Torvalds 已提交
600
	tcm->tcm_handle = flow->classid;
601
	tcm->tcm_info = flow->q->handle;
602 603 604 605 606

	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;

607
	NLA_PUT(skb, TCA_ATM_HDR, flow->hdr_len, flow->hdr);
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615
	if (flow->vcc) {
		struct sockaddr_atmpvc pvc;
		int state;

		pvc.sap_family = AF_ATMPVC;
		pvc.sap_addr.itf = flow->vcc->dev ? flow->vcc->dev->number : -1;
		pvc.sap_addr.vpi = flow->vcc->vpi;
		pvc.sap_addr.vci = flow->vcc->vci;
616
		NLA_PUT(skb, TCA_ATM_ADDR, sizeof(pvc), &pvc);
L
Linus Torvalds 已提交
617
		state = ATM_VF2VS(flow->vcc->flags);
618
		NLA_PUT_U32(skb, TCA_ATM_STATE, state);
L
Linus Torvalds 已提交
619 620
	}
	if (flow->excess)
621
		NLA_PUT_U32(skb, TCA_ATM_EXCESS, flow->classid);
L
Linus Torvalds 已提交
622
	else {
623
		NLA_PUT_U32(skb, TCA_ATM_EXCESS, 0);
L
Linus Torvalds 已提交
624
	}
625 626

	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
627 628
	return skb->len;

629
nla_put_failure:
630
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
631 632 633 634
	return -1;
}
static int
atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
P
Patrick McHardy 已提交
635
			struct gnet_dump *d)
L
Linus Torvalds 已提交
636
{
P
Patrick McHardy 已提交
637
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

	flow->qstats.qlen = flow->q->q.qlen;

	if (gnet_stats_copy_basic(d, &flow->bstats) < 0 ||
	    gnet_stats_copy_queue(d, &flow->qstats) < 0)
		return -1;

	return 0;
}

static int atm_tc_dump(struct Qdisc *sch, struct sk_buff *skb)
{
	return 0;
}

653
static const struct Qdisc_class_ops atm_class_ops = {
P
Patrick McHardy 已提交
654 655 656 657 658 659 660 661 662 663 664 665
	.graft		= atm_tc_graft,
	.leaf		= atm_tc_leaf,
	.get		= atm_tc_get,
	.put		= atm_tc_put,
	.change		= atm_tc_change,
	.delete		= atm_tc_delete,
	.walk		= atm_tc_walk,
	.tcf_chain	= atm_tc_find_tcf,
	.bind_tcf	= atm_tc_bind_filter,
	.unbind_tcf	= atm_tc_put,
	.dump		= atm_tc_dump_class,
	.dump_stats	= atm_tc_dump_class_stats,
L
Linus Torvalds 已提交
666 667
};

668
static struct Qdisc_ops atm_qdisc_ops __read_mostly = {
P
Patrick McHardy 已提交
669 670 671 672 673
	.cl_ops		= &atm_class_ops,
	.id		= "atm",
	.priv_size	= sizeof(struct atm_qdisc_data),
	.enqueue	= atm_tc_enqueue,
	.dequeue	= atm_tc_dequeue,
674
	.peek		= atm_tc_peek,
P
Patrick McHardy 已提交
675 676 677 678 679 680
	.drop		= atm_tc_drop,
	.init		= atm_tc_init,
	.reset		= atm_tc_reset,
	.destroy	= atm_tc_destroy,
	.dump		= atm_tc_dump,
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
681 682 683 684 685 686 687
};

static int __init atm_init(void)
{
	return register_qdisc(&atm_qdisc_ops);
}

688
static void __exit atm_exit(void)
L
Linus Torvalds 已提交
689 690 691 692 693 694 695
{
	unregister_qdisc(&atm_qdisc_ops);
}

module_init(atm_init)
module_exit(atm_exit)
MODULE_LICENSE("GPL");