sch_atm.c 19 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
7
#include <linux/init.h>
8
#include <linux/interrupt.h>
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/atmdev.h>
#include <linux/atmclip.h>
#include <linux/rtnetlink.h>
15
#include <linux/file.h>		/* for fput */
16
#include <net/netlink.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <net/pkt_sched.h>

/*
 * 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 {
43
	struct Qdisc		*q;	/* FIFO, TBF, etc. */
Linus Torvalds's avatar
Linus Torvalds committed
44
	struct tcf_proto	*filter_list;
45 46
	struct atm_vcc		*vcc;	/* VCC; NULL if VCC is closed */
	void			(*old_pop)(struct atm_vcc *vcc,
47
					   struct sk_buff *skb); /* chaining */
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51
	struct atm_qdisc_data	*parent;	/* parent qdisc */
	struct socket		*sock;		/* for closing */
	u32			classid;	/* x:y type ID */
	int			ref;		/* reference count */
52
	struct gnet_stats_basic_packed	bstats;
Linus Torvalds's avatar
Linus Torvalds committed
53
	struct gnet_stats_queue	qstats;
54
	struct list_head	list;
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61 62
	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 */
63
	struct list_head	flows;		/* NB: "link" is also on this
Linus Torvalds's avatar
Linus Torvalds committed
64
						   list */
65
	struct tasklet_struct	task;		/* dequeue tasklet */
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69
};

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

70
static inline struct atm_flow_data *lookup_flow(struct Qdisc *sch, u32 classid)
Linus Torvalds's avatar
Linus Torvalds committed
71
{
72
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
73 74
	struct atm_flow_data *flow;

75
	list_for_each_entry(flow, &p->flows, list) {
76
		if (flow->classid == classid)
77 78 79
			return flow;
	}
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
80 81
}

82 83
static int atm_tc_graft(struct Qdisc *sch, unsigned long arg,
			struct Qdisc *new, struct Qdisc **old)
Linus Torvalds's avatar
Linus Torvalds committed
84
{
85
	struct atm_qdisc_data *p = qdisc_priv(sch);
86 87
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;

88
	pr_debug("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",
89
		sch, p, flow, new, old);
90
	if (list_empty(&flow->list))
91 92 93
		return -EINVAL;
	if (!new)
		new = &noop_qdisc;
94 95
	*old = flow->q;
	flow->q = new;
96 97
	if (*old)
		qdisc_reset(*old);
98
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
99 100
}

101
static struct Qdisc *atm_tc_leaf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds's avatar
Linus Torvalds committed
102
{
103
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
Linus Torvalds's avatar
Linus Torvalds committed
104

105
	pr_debug("atm_tc_leaf(sch %p,flow %p)\n", sch, flow);
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108
	return flow ? flow->q : NULL;
}

109
static unsigned long atm_tc_get(struct Qdisc *sch, u32 classid)
Linus Torvalds's avatar
Linus Torvalds committed
110
{
111
	struct atm_qdisc_data *p __maybe_unused = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
112 113
	struct atm_flow_data *flow;

114
	pr_debug("atm_tc_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
115 116 117
	flow = lookup_flow(sch, classid);
	if (flow)
		flow->ref++;
118
	pr_debug("atm_tc_get: flow %p\n", flow);
119
	return (unsigned long)flow;
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122
}

static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
123
					unsigned long parent, u32 classid)
Linus Torvalds's avatar
Linus Torvalds committed
124
{
125
	return atm_tc_get(sch, classid);
Linus Torvalds's avatar
Linus Torvalds committed
126 127 128 129 130 131 132 133 134
}

/*
 * 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)
{
135
	struct atm_qdisc_data *p = qdisc_priv(sch);
136
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
Linus Torvalds's avatar
Linus Torvalds committed
137

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

162
static void sch_atm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165
{
	struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;

166
	pr_debug("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n", vcc, skb, p);
167
	VCC2FLOW(vcc)->old_pop(vcc, skb);
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171
	tasklet_schedule(&p->task);
}

static const u8 llc_oui_ip[] = {
172 173 174 175
	0xaa,			/* DSAP: non-ISO */
	0xaa,			/* SSAP: non-ISO */
	0x03,			/* Ctrl: Unnumbered Information Command PDU */
	0x00,			/* OUI: EtherType */
Linus Torvalds's avatar
Linus Torvalds committed
176
	0x00, 0x00,
177 178
	0x08, 0x00
};				/* Ethertype IP (0800) */
Linus Torvalds's avatar
Linus Torvalds committed
179

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

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

197
	pr_debug("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
198
		"flow %p,opt %p)\n", sch, p, classid, parent, flow, opt);
Linus Torvalds's avatar
Linus Torvalds committed
199 200 201 202 203 204 205 206 207 208 209 210
	/*
	 * 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.)
	 */
211 212
	if (flow)
		return -EBUSY;
213
	if (opt == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
214
		return -EINVAL;
215 216

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

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

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

306
static int atm_tc_delete(struct Qdisc *sch, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308
	struct atm_qdisc_data *p = qdisc_priv(sch);
309
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
Linus Torvalds's avatar
Linus Torvalds committed
310

311
	pr_debug("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
312
	if (list_empty(&flow->list))
313 314 315
		return -EINVAL;
	if (flow->filter_list || flow == &p->link)
		return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320
	/*
	 * 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) {
Eric Dumazet's avatar
Eric Dumazet committed
321
		pr_err("atm_tc_delete: flow->ref == %d\n", flow->ref);
Linus Torvalds's avatar
Linus Torvalds committed
322 323
		return -EINVAL;
	}
324 325 326
	if (flow->ref > 2)
		return -EBUSY;	/* catch references via excess, etc. */
	atm_tc_put(sch, arg);
Linus Torvalds's avatar
Linus Torvalds committed
327 328 329
	return 0;
}

330
static void atm_tc_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds's avatar
Linus Torvalds committed
331
{
332
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
333 334
	struct atm_flow_data *flow;

335
	pr_debug("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
336 337
	if (walker->stop)
		return;
338 339 340 341 342 343
	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;
		}
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347
		walker->count++;
	}
}

348
static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds's avatar
Linus Torvalds committed
349
{
350
	struct atm_qdisc_data *p = qdisc_priv(sch);
351
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
Linus Torvalds's avatar
Linus Torvalds committed
352

353
	pr_debug("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
354
	return flow ? &flow->filter_list : &p->link.filter_list;
Linus Torvalds's avatar
Linus Torvalds committed
355 356 357 358
}

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

359
static int atm_tc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds's avatar
Linus Torvalds committed
360
{
361
	struct atm_qdisc_data *p = qdisc_priv(sch);
362
	struct atm_flow_data *flow;
Linus Torvalds's avatar
Linus Torvalds committed
363 364 365 366
	struct tcf_result res;
	int result;
	int ret = NET_XMIT_POLICED;

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

414
	ret = qdisc_enqueue(skb, flow->q);
415
	if (ret != NET_XMIT_SUCCESS) {
416
drop: __maybe_unused
417 418 419 420 421
		if (net_xmit_drop_count(ret)) {
			sch->qstats.drops++;
			if (flow)
				flow->qstats.drops++;
		}
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425 426 427 428 429 430 431 432 433 434
		return ret;
	}
	/*
	 * 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++;
435
		return NET_XMIT_SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
436 437
	}
	tasklet_schedule(&p->task);
438
	return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441 442 443 444 445 446 447 448 449
}

/*
 * 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)
{
450
	struct Qdisc *sch = (struct Qdisc *)data;
451
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
452 453 454
	struct atm_flow_data *flow;
	struct sk_buff *skb;

455
	pr_debug("sch_atm_dequeue(sch %p,[qdisc %p])\n", sch, p);
456 457 458
	list_for_each_entry(flow, &p->flows, list) {
		if (flow == &p->link)
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462
		/*
		 * If traffic is properly shaped, this won't generate nasty
		 * little bursts. Otherwise, it may ... (but that's okay)
		 */
463 464
		while ((skb = flow->q->ops->peek(flow->q))) {
			if (!atm_may_send(flow->vcc, skb->truesize))
Linus Torvalds's avatar
Linus Torvalds committed
465
				break;
466

467
			skb = qdisc_dequeue_peeked(flow->q);
468 469 470
			if (unlikely(!skb))
				break;

471 472
			qdisc_bstats_update(sch, skb);
			bstats_update(&flow->bstats, skb);
473
			pr_debug("atm_tc_dequeue: sending on class %p\n", flow);
Linus Torvalds's avatar
Linus Torvalds committed
474
			/* remove any LL header somebody else has attached */
475
			skb_pull(skb, skb_network_offset(skb));
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478
			if (skb_headroom(skb) < flow->hdr_len) {
				struct sk_buff *new;

479
				new = skb_realloc_headroom(skb, flow->hdr_len);
Linus Torvalds's avatar
Linus Torvalds committed
480
				dev_kfree_skb(skb);
481 482
				if (!new)
					continue;
Linus Torvalds's avatar
Linus Torvalds committed
483 484
				skb = new;
			}
485
			pr_debug("sch_atm_dequeue: ip %p, data %p\n",
486
				 skb_network_header(skb), skb->data);
Linus Torvalds's avatar
Linus Torvalds committed
487
			ATM_SKB(skb)->vcc = flow->vcc;
488 489
			memcpy(skb_push(skb, flow->hdr_len), flow->hdr,
			       flow->hdr_len);
Linus Torvalds's avatar
Linus Torvalds committed
490 491 492
			atomic_add(skb->truesize,
				   &sk_atm(flow->vcc)->sk_wmem_alloc);
			/* atm.atm_options are already set by atm_tc_enqueue */
493
			flow->vcc->send(flow->vcc, skb);
Linus Torvalds's avatar
Linus Torvalds committed
494
		}
495
	}
Linus Torvalds's avatar
Linus Torvalds committed
496 497 498 499
}

static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
{
500
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
501 502
	struct sk_buff *skb;

503
	pr_debug("atm_tc_dequeue(sch %p,[qdisc %p])\n", sch, p);
Linus Torvalds's avatar
Linus Torvalds committed
504
	tasklet_schedule(&p->task);
505
	skb = qdisc_dequeue_peeked(p->link.q);
506 507
	if (skb)
		sch->q.qlen--;
Linus Torvalds's avatar
Linus Torvalds committed
508 509 510
	return skb;
}

511 512 513 514 515 516 517 518 519
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);
}

Linus Torvalds's avatar
Linus Torvalds committed
520 521
static unsigned int atm_tc_drop(struct Qdisc *sch)
{
522
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
523 524 525
	struct atm_flow_data *flow;
	unsigned int len;

526
	pr_debug("atm_tc_drop(sch %p,[qdisc %p])\n", sch, p);
527
	list_for_each_entry(flow, &p->flows, list) {
Linus Torvalds's avatar
Linus Torvalds committed
528 529
		if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
			return len;
530
	}
Linus Torvalds's avatar
Linus Torvalds committed
531 532 533
	return 0;
}

534
static int atm_tc_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds's avatar
Linus Torvalds committed
535
{
536
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
537

538
	pr_debug("atm_tc_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
539 540 541
	INIT_LIST_HEAD(&p->flows);
	INIT_LIST_HEAD(&p->link.list);
	list_add(&p->link.list, &p->flows);
542
	p->link.q = qdisc_create_dflt(sch->dev_queue,
543
				      &pfifo_qdisc_ops, sch->handle);
544
	if (!p->link.q)
Linus Torvalds's avatar
Linus Torvalds committed
545
		p->link.q = &noop_qdisc;
546
	pr_debug("atm_tc_init: link (%p) qdisc %p\n", &p->link, p->link.q);
Linus Torvalds's avatar
Linus Torvalds committed
547 548 549 550 551
	p->link.filter_list = NULL;
	p->link.vcc = NULL;
	p->link.sock = NULL;
	p->link.classid = sch->handle;
	p->link.ref = 1;
552
	tasklet_init(&p->task, sch_atm_dequeue, (unsigned long)sch);
Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557
	return 0;
}

static void atm_tc_reset(struct Qdisc *sch)
{
558
	struct atm_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds's avatar
Linus Torvalds committed
559 560
	struct atm_flow_data *flow;

561
	pr_debug("atm_tc_reset(sch %p,[qdisc %p])\n", sch, p);
562
	list_for_each_entry(flow, &p->flows, list)
563
		qdisc_reset(flow->q);
Linus Torvalds's avatar
Linus Torvalds committed
564 565 566 567 568
	sch->q.qlen = 0;
}

static void atm_tc_destroy(struct Qdisc *sch)
{
569
	struct atm_qdisc_data *p = qdisc_priv(sch);
570
	struct atm_flow_data *flow, *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
571

572
	pr_debug("atm_tc_destroy(sch %p,[qdisc %p])\n", sch, p);
573
	list_for_each_entry(flow, &p->flows, list)
574 575
		tcf_destroy_chain(&flow->filter_list);

576
	list_for_each_entry_safe(flow, tmp, &p->flows, list) {
Linus Torvalds's avatar
Linus Torvalds committed
577
		if (flow->ref > 1)
Eric Dumazet's avatar
Eric Dumazet committed
578
			pr_err("atm_destroy: %p->ref = %d\n", flow, flow->ref);
579
		atm_tc_put(sch, (unsigned long)flow);
Linus Torvalds's avatar
Linus Torvalds committed
580 581 582 583 584
	}
	tasklet_kill(&p->task);
}

static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
585
			     struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds's avatar
Linus Torvalds committed
586
{
587
	struct atm_qdisc_data *p = qdisc_priv(sch);
588
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
589
	struct nlattr *nest;
Linus Torvalds's avatar
Linus Torvalds committed
590

591
	pr_debug("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
592
		sch, p, flow, skb, tcm);
593
	if (list_empty(&flow->list))
594
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
595
	tcm->tcm_handle = flow->classid;
596
	tcm->tcm_info = flow->q->handle;
597 598 599 600 601

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

602 603
	if (nla_put(skb, TCA_ATM_HDR, flow->hdr_len, flow->hdr))
		goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607
	if (flow->vcc) {
		struct sockaddr_atmpvc pvc;
		int state;

608
		memset(&pvc, 0, sizeof(pvc));
Linus Torvalds's avatar
Linus Torvalds committed
609 610 611 612
		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;
613 614
		if (nla_put(skb, TCA_ATM_ADDR, sizeof(pvc), &pvc))
			goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
615
		state = ATM_VF2VS(flow->vcc->flags);
616 617 618 619 620 621 622 623 624
		if (nla_put_u32(skb, TCA_ATM_STATE, state))
			goto nla_put_failure;
	}
	if (flow->excess) {
		if (nla_put_u32(skb, TCA_ATM_EXCESS, flow->classid))
			goto nla_put_failure;
	} else {
		if (nla_put_u32(skb, TCA_ATM_EXCESS, 0))
			goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
625
	}
626
	nla_nest_end(skb, nest);
Linus Torvalds's avatar
Linus Torvalds committed
627 628
	return skb->len;

629
nla_put_failure:
630
	nla_nest_cancel(skb, nest);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634
	return -1;
}
static int
atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
635
			struct gnet_dump *d)
Linus Torvalds's avatar
Linus Torvalds committed
636
{
637
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
Linus Torvalds's avatar
Linus Torvalds committed
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 = {
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,
Linus Torvalds's avatar
Linus Torvalds committed
666 667
};

668
static struct Qdisc_ops atm_qdisc_ops __read_mostly = {
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,
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,
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691 692 693 694 695
{
	unregister_qdisc(&atm_qdisc_ops);
}

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