sch_atm.c 19.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* net/sched/sch_atm.c - ATM VC selection "queueing discipline" */

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

#include <linux/module.h>
#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 已提交
13
#include <linux/file.h>		/* for fput */
14
#include <net/netlink.h>
L
Linus Torvalds 已提交
15 16
#include <net/pkt_sched.h>

P
Patrick McHardy 已提交
17
extern struct socket *sockfd_lookup(int fd, int *err);	/* @@@ fix this */
L
Linus Torvalds 已提交
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 43 44 45 46 47 48 49 50 51 52 53 54 55

#if 0 /* control */
#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
#else
#define DPRINTK(format,args...)
#endif

#if 0 /* data */
#define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
#else
#define D2PRINTK(format,args...)
#endif

/*
 * 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 PRIV(sch) qdisc_priv(sch)
#define VCC2FLOW(vcc) ((struct atm_flow_data *) ((vcc)->user_back))

struct atm_flow_data {
P
Patrick McHardy 已提交
56
	struct Qdisc		*q;	/* FIFO, TBF, etc. */
L
Linus Torvalds 已提交
57
	struct tcf_proto	*filter_list;
P
Patrick McHardy 已提交
58 59 60
	struct atm_vcc		*vcc;	/* VCC; NULL if VCC is closed */
	void			(*old_pop)(struct atm_vcc *vcc,
					   struct sk_buff * skb); /* chaining */
L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	struct atm_qdisc_data	*parent;	/* parent qdisc */
	struct socket		*sock;		/* for closing */
	u32			classid;	/* x:y type ID */
	int			ref;		/* reference count */
	struct gnet_stats_basic	bstats;
	struct gnet_stats_queue	qstats;
	struct atm_flow_data	*next;
	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 */
	struct atm_flow_data	*flows;		/* NB: "link" is also on this
						   list */
	struct tasklet_struct	task;		/* requeue tasklet */
};

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

P
Patrick McHardy 已提交
83
static int find_flow(struct atm_qdisc_data *qdisc, struct atm_flow_data *flow)
L
Linus Torvalds 已提交
84 85 86
{
	struct atm_flow_data *walk;

P
Patrick McHardy 已提交
87
	DPRINTK("find_flow(qdisc %p,flow %p)\n", qdisc, flow);
L
Linus Torvalds 已提交
88
	for (walk = qdisc->flows; walk; walk = walk->next)
P
Patrick McHardy 已提交
89 90
		if (walk == flow)
			return 1;
L
Linus Torvalds 已提交
91 92 93 94
	DPRINTK("find_flow: not found\n");
	return 0;
}

P
Patrick McHardy 已提交
95
static inline struct atm_flow_data *lookup_flow(struct Qdisc *sch, u32 classid)
L
Linus Torvalds 已提交
96 97 98 99
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;

100
	for (flow = p->flows; flow; flow = flow->next)
P
Patrick McHardy 已提交
101 102
		if (flow->classid == classid)
			break;
L
Linus Torvalds 已提交
103 104 105
	return flow;
}

P
Patrick McHardy 已提交
106 107
static int atm_tc_graft(struct Qdisc *sch, unsigned long arg,
			struct Qdisc *new, struct Qdisc **old)
L
Linus Torvalds 已提交
108 109
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
110 111 112 113 114 115 116 117 118 119 120
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;

	DPRINTK("atm_tc_graft(sch %p,[qdisc %p],flow %p,new %p,old %p)\n",
		sch, p, flow, new, old);
	if (!find_flow(p, flow))
		return -EINVAL;
	if (!new)
		new = &noop_qdisc;
	*old = xchg(&flow->q, new);
	if (*old)
		qdisc_reset(*old);
121
	return 0;
L
Linus Torvalds 已提交
122 123
}

P
Patrick McHardy 已提交
124
static struct Qdisc *atm_tc_leaf(struct Qdisc *sch, unsigned long cl)
L
Linus Torvalds 已提交
125
{
P
Patrick McHardy 已提交
126
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
127

P
Patrick McHardy 已提交
128
	DPRINTK("atm_tc_leaf(sch %p,flow %p)\n", sch, flow);
L
Linus Torvalds 已提交
129 130 131
	return flow ? flow->q : NULL;
}

P
Patrick McHardy 已提交
132
static unsigned long atm_tc_get(struct Qdisc *sch, u32 classid)
L
Linus Torvalds 已提交
133
{
P
Patrick McHardy 已提交
134
	struct atm_qdisc_data *p __maybe_unused = PRIV(sch);
L
Linus Torvalds 已提交
135 136
	struct atm_flow_data *flow;

P
Patrick McHardy 已提交
137 138 139 140 141 142
	DPRINTK("atm_tc_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
	flow = lookup_flow(sch, classid);
	if (flow)
		flow->ref++;
	DPRINTK("atm_tc_get: flow %p\n", flow);
	return (unsigned long)flow;
L
Linus Torvalds 已提交
143 144 145
}

static unsigned long atm_tc_bind_filter(struct Qdisc *sch,
P
Patrick McHardy 已提交
146
					unsigned long parent, u32 classid)
L
Linus Torvalds 已提交
147
{
P
Patrick McHardy 已提交
148
	return atm_tc_get(sch, classid);
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158
}

/*
 * 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)
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
159
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
160 161
	struct atm_flow_data **prev;

P
Patrick McHardy 已提交
162 163 164
	DPRINTK("atm_tc_put(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
	if (--flow->ref)
		return;
L
Linus Torvalds 已提交
165 166
	DPRINTK("atm_tc_put: destroying\n");
	for (prev = &p->flows; *prev; prev = &(*prev)->next)
P
Patrick McHardy 已提交
167 168
		if (*prev == flow)
			break;
L
Linus Torvalds 已提交
169
	if (!*prev) {
P
Patrick McHardy 已提交
170
		printk(KERN_CRIT "atm_tc_put: class %p not found\n", flow);
L
Linus Torvalds 已提交
171 172 173
		return;
	}
	*prev = flow->next;
P
Patrick McHardy 已提交
174
	DPRINTK("atm_tc_put: qdisc %p\n", flow->q);
L
Linus Torvalds 已提交
175
	qdisc_destroy(flow->q);
176
	tcf_destroy_chain(flow->filter_list);
L
Linus Torvalds 已提交
177 178
	if (flow->sock) {
		DPRINTK("atm_tc_put: f_count %d\n",
P
Patrick McHardy 已提交
179
			file_count(flow->sock->file));
L
Linus Torvalds 已提交
180 181 182
		flow->vcc->pop = flow->old_pop;
		sockfd_put(flow->sock);
	}
P
Patrick McHardy 已提交
183 184 185 186
	if (flow->excess)
		atm_tc_put(sch, (unsigned long)flow->excess);
	if (flow != &p->link)
		kfree(flow);
L
Linus Torvalds 已提交
187 188 189 190 191 192
	/*
	 * 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 已提交
193
static void sch_atm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
L
Linus Torvalds 已提交
194 195 196
{
	struct atm_qdisc_data *p = VCC2FLOW(vcc)->parent;

P
Patrick McHardy 已提交
197 198
	D2PRINTK("sch_atm_pop(vcc %p,skb %p,[qdisc %p])\n", vcc, skb, p);
	VCC2FLOW(vcc)->old_pop(vcc, skb);
L
Linus Torvalds 已提交
199 200 201 202
	tasklet_schedule(&p->task);
}

static const u8 llc_oui_ip[] = {
P
Patrick McHardy 已提交
203 204 205 206
	0xaa,			/* DSAP: non-ISO */
	0xaa,			/* SSAP: non-ISO */
	0x03,			/* Ctrl: Unnumbered Information Command PDU */
	0x00,			/* OUI: EtherType */
L
Linus Torvalds 已提交
207
	0x00, 0x00,
P
Patrick McHardy 已提交
208 209
	0x08, 0x00
};				/* Ethertype IP (0800) */
L
Linus Torvalds 已提交
210 211

static int atm_tc_change(struct Qdisc *sch, u32 classid, u32 parent,
P
Patrick McHardy 已提交
212
			 struct rtattr **tca, unsigned long *arg)
L
Linus Torvalds 已提交
213 214
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
215
	struct atm_flow_data *flow = (struct atm_flow_data *)*arg;
L
Linus Torvalds 已提交
216
	struct atm_flow_data *excess = NULL;
P
Patrick McHardy 已提交
217
	struct rtattr *opt = tca[TCA_OPTIONS - 1];
L
Linus Torvalds 已提交
218 219
	struct rtattr *tb[TCA_ATM_MAX];
	struct socket *sock;
P
Patrick McHardy 已提交
220
	int fd, error, hdr_len;
L
Linus Torvalds 已提交
221 222 223
	void *hdr;

	DPRINTK("atm_tc_change(sch %p,[qdisc %p],classid %x,parent %x,"
P
Patrick McHardy 已提交
224
		"flow %p,opt %p)\n", sch, p, classid, parent, flow, opt);
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236
	/*
	 * 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 已提交
237 238
	if (flow)
		return -EBUSY;
L
Linus Torvalds 已提交
239 240
	if (opt == NULL || rtattr_parse_nested(tb, TCA_ATM_MAX, opt))
		return -EINVAL;
P
Patrick McHardy 已提交
241
	if (!tb[TCA_ATM_FD - 1] || RTA_PAYLOAD(tb[TCA_ATM_FD - 1]) < sizeof(fd))
L
Linus Torvalds 已提交
242
		return -EINVAL;
P
Patrick McHardy 已提交
243 244 245 246 247 248
	fd = *(int *)RTA_DATA(tb[TCA_ATM_FD - 1]);
	DPRINTK("atm_tc_change: fd %d\n", fd);
	if (tb[TCA_ATM_HDR - 1]) {
		hdr_len = RTA_PAYLOAD(tb[TCA_ATM_HDR - 1]);
		hdr = RTA_DATA(tb[TCA_ATM_HDR - 1]);
	} else {
L
Linus Torvalds 已提交
249
		hdr_len = RFC1483LLC_LEN;
P
Patrick McHardy 已提交
250
		hdr = NULL;	/* default LLC/SNAP for IP */
L
Linus Torvalds 已提交
251
	}
P
Patrick McHardy 已提交
252 253
	if (!tb[TCA_ATM_EXCESS - 1])
		excess = NULL;
L
Linus Torvalds 已提交
254
	else {
P
Patrick McHardy 已提交
255
		if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS - 1]) != sizeof(u32))
L
Linus Torvalds 已提交
256
			return -EINVAL;
P
Patrick McHardy 已提交
257 258 259 260
		excess = (struct atm_flow_data *)
			atm_tc_get(sch, *(u32 *)RTA_DATA(tb[TCA_ATM_EXCESS - 1]));
		if (!excess)
			return -ENOENT;
L
Linus Torvalds 已提交
261 262
	}
	DPRINTK("atm_tc_change: type %d, payload %d, hdr_len %d\n",
P
Patrick McHardy 已提交
263 264 265 266
		opt->rta_type, RTA_PAYLOAD(opt), hdr_len);
	if (!(sock = sockfd_lookup(fd, &error)))
		return error;	/* f_count++ */
	DPRINTK("atm_tc_change: f_count %d\n", file_count(sock->file));
267
	if (sock->ops->family != PF_ATMSVC && sock->ops->family != PF_ATMPVC) {
L
Linus Torvalds 已提交
268
		error = -EPROTOTYPE;
269
		goto err_out;
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278
	}
	/* @@@ should check if the socket is really operational or we'll crash
	   on vcc->send */
	if (classid) {
		if (TC_H_MAJ(classid ^ sch->handle)) {
			DPRINTK("atm_tc_change: classid mismatch\n");
			error = -EINVAL;
			goto err_out;
		}
P
Patrick McHardy 已提交
279
		if (find_flow(p, flow)) {
L
Linus Torvalds 已提交
280 281 282
			error = -EEXIST;
			goto err_out;
		}
P
Patrick McHardy 已提交
283
	} else {
L
Linus Torvalds 已提交
284 285 286 287
		int i;
		unsigned long cl;

		for (i = 1; i < 0x8000; i++) {
P
Patrick McHardy 已提交
288 289 290 291
			classid = TC_H_MAKE(sch->handle, 0x8000 | i);
			if (!(cl = atm_tc_get(sch, classid)))
				break;
			atm_tc_put(sch, cl);
L
Linus Torvalds 已提交
292 293
		}
	}
P
Patrick McHardy 已提交
294 295 296
	DPRINTK("atm_tc_change: new id %x\n", classid);
	flow = kmalloc(sizeof(struct atm_flow_data) + hdr_len, GFP_KERNEL);
	DPRINTK("atm_tc_change: flow %p\n", flow);
L
Linus Torvalds 已提交
297 298 299 300
	if (!flow) {
		error = -ENOBUFS;
		goto err_out;
	}
P
Patrick McHardy 已提交
301
	memset(flow, 0, sizeof(*flow));
L
Linus Torvalds 已提交
302
	flow->filter_list = NULL;
P
Patrick McHardy 已提交
303
	if (!(flow->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid)))
L
Linus Torvalds 已提交
304
		flow->q = &noop_qdisc;
P
Patrick McHardy 已提交
305
	DPRINTK("atm_tc_change: qdisc %p\n", flow->q);
L
Linus Torvalds 已提交
306
	flow->sock = sock;
P
Patrick McHardy 已提交
307
	flow->vcc = ATM_SD(sock);	/* speedup */
L
Linus Torvalds 已提交
308
	flow->vcc->user_back = flow;
P
Patrick McHardy 已提交
309
	DPRINTK("atm_tc_change: vcc %p\n", flow->vcc);
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319
	flow->old_pop = flow->vcc->pop;
	flow->parent = p;
	flow->vcc->pop = sch_atm_pop;
	flow->classid = classid;
	flow->ref = 1;
	flow->excess = excess;
	flow->next = p->link.next;
	p->link.next = flow;
	flow->hdr_len = hdr_len;
	if (hdr)
P
Patrick McHardy 已提交
320
		memcpy(flow->hdr, hdr, hdr_len);
L
Linus Torvalds 已提交
321
	else
P
Patrick McHardy 已提交
322 323
		memcpy(flow->hdr, llc_oui_ip, sizeof(llc_oui_ip));
	*arg = (unsigned long)flow;
L
Linus Torvalds 已提交
324 325
	return 0;
err_out:
P
Patrick McHardy 已提交
326 327
	if (excess)
		atm_tc_put(sch, (unsigned long)excess);
L
Linus Torvalds 已提交
328 329 330 331
	sockfd_put(sock);
	return error;
}

P
Patrick McHardy 已提交
332
static int atm_tc_delete(struct Qdisc *sch, unsigned long arg)
L
Linus Torvalds 已提交
333 334
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
335
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
L
Linus Torvalds 已提交
336

P
Patrick McHardy 已提交
337 338 339 340 341
	DPRINTK("atm_tc_delete(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
	if (!find_flow(PRIV(sch), flow))
		return -EINVAL;
	if (flow->filter_list || flow == &p->link)
		return -EBUSY;
L
Linus Torvalds 已提交
342 343 344 345 346
	/*
	 * 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 已提交
347
		printk(KERN_ERR "atm_tc_delete: flow->ref == %d\n", flow->ref);
L
Linus Torvalds 已提交
348 349
		return -EINVAL;
	}
P
Patrick McHardy 已提交
350 351 352
	if (flow->ref > 2)
		return -EBUSY;	/* catch references via excess, etc. */
	atm_tc_put(sch, arg);
L
Linus Torvalds 已提交
353 354 355
	return 0;
}

P
Patrick McHardy 已提交
356
static void atm_tc_walk(struct Qdisc *sch, struct qdisc_walker *walker)
L
Linus Torvalds 已提交
357 358 359 360
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;

P
Patrick McHardy 已提交
361 362 363
	DPRINTK("atm_tc_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
	if (walker->stop)
		return;
L
Linus Torvalds 已提交
364 365
	for (flow = p->flows; flow; flow = flow->next) {
		if (walker->count >= walker->skip)
P
Patrick McHardy 已提交
366
			if (walker->fn(sch, (unsigned long)flow, walker) < 0) {
L
Linus Torvalds 已提交
367 368 369 370 371 372 373
				walker->stop = 1;
				break;
			}
		walker->count++;
	}
}

P
Patrick McHardy 已提交
374
static struct tcf_proto **atm_tc_find_tcf(struct Qdisc *sch, unsigned long cl)
L
Linus Torvalds 已提交
375 376
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
377
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
L
Linus Torvalds 已提交
378

P
Patrick McHardy 已提交
379
	DPRINTK("atm_tc_find_tcf(sch %p,[qdisc %p],flow %p)\n", sch, p, flow);
380
	return flow ? &flow->filter_list : &p->link.filter_list;
L
Linus Torvalds 已提交
381 382 383 384
}

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

P
Patrick McHardy 已提交
385
static int atm_tc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
L
Linus Torvalds 已提交
386 387
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
388
	struct atm_flow_data *flow = NULL;	/* @@@ */
L
Linus Torvalds 已提交
389 390 391 392
	struct tcf_result res;
	int result;
	int ret = NET_XMIT_POLICED;

P
Patrick McHardy 已提交
393 394
	D2PRINTK("atm_tc_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
	result = TC_POLICE_OK;	/* be nice to gcc */
L
Linus Torvalds 已提交
395
	if (TC_H_MAJ(skb->priority) != sch->handle ||
P
Patrick McHardy 已提交
396
	    !(flow = (struct atm_flow_data *)atm_tc_get(sch, skb->priority)))
L
Linus Torvalds 已提交
397 398
		for (flow = p->flows; flow; flow = flow->next)
			if (flow->filter_list) {
P
Patrick McHardy 已提交
399 400 401 402 403 404 405
				result = tc_classify(skb, flow->filter_list,
						     &res);
				if (result < 0)
					continue;
				flow = (struct atm_flow_data *)res.class;
				if (!flow)
					flow = lookup_flow(sch, res.classid);
L
Linus Torvalds 已提交
406 407
				break;
			}
P
Patrick McHardy 已提交
408 409
	if (!flow)
		flow = &p->link;
L
Linus Torvalds 已提交
410 411 412
	else {
		if (flow->vcc)
			ATM_SKB(skb)->atm_options = flow->vcc->atm_options;
P
Patrick McHardy 已提交
413
		/*@@@ looks good ... but it's not supposed to work :-) */
414 415 416 417 418 419 420 421 422 423 424
#ifdef CONFIG_NET_CLS_ACT
		switch (result) {
		case TC_ACT_QUEUED:
		case TC_ACT_STOLEN:
			kfree_skb(skb);
			return NET_XMIT_SUCCESS;
		case TC_ACT_SHOT:
			kfree_skb(skb);
			goto drop;
		}
#elif defined(CONFIG_NET_CLS_POLICE)
L
Linus Torvalds 已提交
425
		switch (result) {
P
Patrick McHardy 已提交
426 427
		case TC_POLICE_SHOT:
			kfree_skb(skb);
428
			goto drop;
P
Patrick McHardy 已提交
429 430 431 432 433
		case TC_POLICE_RECLASSIFY:
			if (flow->excess)
				flow = flow->excess;
			else {
				ATM_SKB(skb)->atm_options |= ATM_ATMOPT_CLP;
L
Linus Torvalds 已提交
434
				break;
P
Patrick McHardy 已提交
435 436 437 438 439 440
			}
			/* fall through */
		case TC_POLICE_OK:
			/* fall through */
		default:
			break;
L
Linus Torvalds 已提交
441 442 443
		}
#endif
	}
444 445
	if ((ret = flow->q->enqueue(skb, flow->q)) != 0) {
drop: __maybe_unused
L
Linus Torvalds 已提交
446
		sch->qstats.drops++;
P
Patrick McHardy 已提交
447 448
		if (flow)
			flow->qstats.drops++;
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		return ret;
	}
	sch->bstats.bytes += skb->len;
	sch->bstats.packets++;
	flow->bstats.bytes += skb->len;
	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++;
		return 0;
	}
	tasklet_schedule(&p->task);
	return NET_XMIT_BYPASS;
}

/*
 * 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 已提交
481
	struct Qdisc *sch = (struct Qdisc *)data;
L
Linus Torvalds 已提交
482 483 484 485
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;
	struct sk_buff *skb;

P
Patrick McHardy 已提交
486
	D2PRINTK("sch_atm_dequeue(sch %p,[qdisc %p])\n", sch, p);
L
Linus Torvalds 已提交
487 488 489 490 491 492
	for (flow = p->link.next; flow; flow = flow->next)
		/*
		 * If traffic is properly shaped, this won't generate nasty
		 * little bursts. Otherwise, it may ... (but that's okay)
		 */
		while ((skb = flow->q->dequeue(flow->q))) {
P
Patrick McHardy 已提交
493 494
			if (!atm_may_send(flow->vcc, skb->truesize)) {
				(void)flow->q->ops->requeue(skb, flow->q);
L
Linus Torvalds 已提交
495 496
				break;
			}
P
Patrick McHardy 已提交
497
			D2PRINTK("atm_tc_dequeue: sending on class %p\n", flow);
L
Linus Torvalds 已提交
498
			/* remove any LL header somebody else has attached */
499
			skb_pull(skb, skb_network_offset(skb));
L
Linus Torvalds 已提交
500 501 502
			if (skb_headroom(skb) < flow->hdr_len) {
				struct sk_buff *new;

P
Patrick McHardy 已提交
503
				new = skb_realloc_headroom(skb, flow->hdr_len);
L
Linus Torvalds 已提交
504
				dev_kfree_skb(skb);
P
Patrick McHardy 已提交
505 506
				if (!new)
					continue;
L
Linus Torvalds 已提交
507 508 509
				skb = new;
			}
			D2PRINTK("sch_atm_dequeue: ip %p, data %p\n",
510
				 skb_network_header(skb), skb->data);
L
Linus Torvalds 已提交
511
			ATM_SKB(skb)->vcc = flow->vcc;
P
Patrick McHardy 已提交
512 513
			memcpy(skb_push(skb, flow->hdr_len), flow->hdr,
			       flow->hdr_len);
L
Linus Torvalds 已提交
514 515 516
			atomic_add(skb->truesize,
				   &sk_atm(flow->vcc)->sk_wmem_alloc);
			/* atm.atm_options are already set by atm_tc_enqueue */
P
Patrick McHardy 已提交
517
			flow->vcc->send(flow->vcc, skb);
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525
		}
}

static struct sk_buff *atm_tc_dequeue(struct Qdisc *sch)
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct sk_buff *skb;

P
Patrick McHardy 已提交
526
	D2PRINTK("atm_tc_dequeue(sch %p,[qdisc %p])\n", sch, p);
L
Linus Torvalds 已提交
527 528
	tasklet_schedule(&p->task);
	skb = p->link.q->dequeue(p->link.q);
P
Patrick McHardy 已提交
529 530
	if (skb)
		sch->q.qlen--;
L
Linus Torvalds 已提交
531 532 533
	return skb;
}

P
Patrick McHardy 已提交
534
static int atm_tc_requeue(struct sk_buff *skb, struct Qdisc *sch)
L
Linus Torvalds 已提交
535 536 537 538
{
	struct atm_qdisc_data *p = PRIV(sch);
	int ret;

P
Patrick McHardy 已提交
539 540
	D2PRINTK("atm_tc_requeue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
	ret = p->link.q->ops->requeue(skb, p->link.q);
L
Linus Torvalds 已提交
541
	if (!ret) {
P
Patrick McHardy 已提交
542 543 544
		sch->q.qlen++;
		sch->qstats.requeues++;
	} else {
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556
		sch->qstats.drops++;
		p->link.qstats.drops++;
	}
	return ret;
}

static unsigned int atm_tc_drop(struct Qdisc *sch)
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;
	unsigned int len;

P
Patrick McHardy 已提交
557
	DPRINTK("atm_tc_drop(sch %p,[qdisc %p])\n", sch, p);
L
Linus Torvalds 已提交
558 559 560 561 562 563
	for (flow = p->flows; flow; flow = flow->next)
		if (flow->q->ops->drop && (len = flow->q->ops->drop(flow->q)))
			return len;
	return 0;
}

P
Patrick McHardy 已提交
564
static int atm_tc_init(struct Qdisc *sch, struct rtattr *opt)
L
Linus Torvalds 已提交
565 566 567
{
	struct atm_qdisc_data *p = PRIV(sch);

P
Patrick McHardy 已提交
568
	DPRINTK("atm_tc_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
L
Linus Torvalds 已提交
569
	p->flows = &p->link;
P
Patrick McHardy 已提交
570 571
	if (!(p->link.q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
					    sch->handle)))
L
Linus Torvalds 已提交
572
		p->link.q = &noop_qdisc;
P
Patrick McHardy 已提交
573
	DPRINTK("atm_tc_init: link (%p) qdisc %p\n", &p->link, p->link.q);
L
Linus Torvalds 已提交
574 575 576 577 578 579
	p->link.filter_list = NULL;
	p->link.vcc = NULL;
	p->link.sock = NULL;
	p->link.classid = sch->handle;
	p->link.ref = 1;
	p->link.next = NULL;
P
Patrick McHardy 已提交
580
	tasklet_init(&p->task, sch_atm_dequeue, (unsigned long)sch);
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588
	return 0;
}

static void atm_tc_reset(struct Qdisc *sch)
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;

P
Patrick McHardy 已提交
589 590 591
	DPRINTK("atm_tc_reset(sch %p,[qdisc %p])\n", sch, p);
	for (flow = p->flows; flow; flow = flow->next)
		qdisc_reset(flow->q);
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599
	sch->q.qlen = 0;
}

static void atm_tc_destroy(struct Qdisc *sch)
{
	struct atm_qdisc_data *p = PRIV(sch);
	struct atm_flow_data *flow;

P
Patrick McHardy 已提交
600
	DPRINTK("atm_tc_destroy(sch %p,[qdisc %p])\n", sch, p);
L
Linus Torvalds 已提交
601 602
	/* races ? */
	while ((flow = p->flows)) {
603
		tcf_destroy_chain(flow->filter_list);
604
		flow->filter_list = NULL;
L
Linus Torvalds 已提交
605
		if (flow->ref > 1)
P
Patrick McHardy 已提交
606 607 608
			printk(KERN_ERR "atm_destroy: %p->ref = %d\n", flow,
			       flow->ref);
		atm_tc_put(sch, (unsigned long)flow);
L
Linus Torvalds 已提交
609 610
		if (p->flows == flow) {
			printk(KERN_ERR "atm_destroy: putting flow %p didn't "
P
Patrick McHardy 已提交
611 612
			       "kill it\n", flow);
			p->flows = flow->next;	/* brute force */
L
Linus Torvalds 已提交
613 614 615 616 617 618 619
			break;
		}
	}
	tasklet_kill(&p->task);
}

static int atm_tc_dump_class(struct Qdisc *sch, unsigned long cl,
P
Patrick McHardy 已提交
620
			     struct sk_buff *skb, struct tcmsg *tcm)
L
Linus Torvalds 已提交
621 622
{
	struct atm_qdisc_data *p = PRIV(sch);
P
Patrick McHardy 已提交
623
	struct atm_flow_data *flow = (struct atm_flow_data *)cl;
624
	unsigned char *b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
625 626 627
	struct rtattr *rta;

	DPRINTK("atm_tc_dump_class(sch %p,[qdisc %p],flow %p,skb %p,tcm %p)\n",
P
Patrick McHardy 已提交
628 629 630
		sch, p, flow, skb, tcm);
	if (!find_flow(p, flow))
		return -EINVAL;
L
Linus Torvalds 已提交
631
	tcm->tcm_handle = flow->classid;
632
	tcm->tcm_info = flow->q->handle;
P
Patrick McHardy 已提交
633 634 635
	rta = (struct rtattr *)b;
	RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
	RTA_PUT(skb, TCA_ATM_HDR, flow->hdr_len, flow->hdr);
L
Linus Torvalds 已提交
636 637 638 639 640 641 642 643
	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;
P
Patrick McHardy 已提交
644
		RTA_PUT(skb, TCA_ATM_ADDR, sizeof(pvc), &pvc);
L
Linus Torvalds 已提交
645
		state = ATM_VF2VS(flow->vcc->flags);
P
Patrick McHardy 已提交
646
		RTA_PUT(skb, TCA_ATM_STATE, sizeof(state), &state);
L
Linus Torvalds 已提交
647 648
	}
	if (flow->excess)
P
Patrick McHardy 已提交
649
		RTA_PUT(skb, TCA_ATM_EXCESS, sizeof(u32), &flow->classid);
L
Linus Torvalds 已提交
650 651 652
	else {
		static u32 zero;

P
Patrick McHardy 已提交
653
		RTA_PUT(skb, TCA_ATM_EXCESS, sizeof(zero), &zero);
L
Linus Torvalds 已提交
654
	}
655
	rta->rta_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
656 657 658
	return skb->len;

rtattr_failure:
659
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
660 661 662 663
	return -1;
}
static int
atm_tc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
P
Patrick McHardy 已提交
664
			struct gnet_dump *d)
L
Linus Torvalds 已提交
665
{
P
Patrick McHardy 已提交
666
	struct atm_flow_data *flow = (struct atm_flow_data *)arg;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

	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;
}

static struct Qdisc_class_ops atm_class_ops = {
P
Patrick McHardy 已提交
683 684 685 686 687 688 689 690 691 692 693 694
	.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 已提交
695 696 697
};

static struct Qdisc_ops atm_qdisc_ops = {
P
Patrick McHardy 已提交
698 699 700 701 702 703 704 705 706 707 708 709
	.cl_ops		= &atm_class_ops,
	.id		= "atm",
	.priv_size	= sizeof(struct atm_qdisc_data),
	.enqueue	= atm_tc_enqueue,
	.dequeue	= atm_tc_dequeue,
	.requeue	= atm_tc_requeue,
	.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 已提交
710 711 712 713 714 715 716
};

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

717
static void __exit atm_exit(void)
L
Linus Torvalds 已提交
718 719 720 721 722 723 724
{
	unregister_qdisc(&atm_qdisc_ops);
}

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