Blame view

net/sched/sch_fifo.c 4.27 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
  /*
   * net/sched/sch_fifo.c	The simplest FIFO queue.
   *
   *		This program is free software; you can redistribute it and/or
   *		modify it under the terms of the GNU General Public License
   *		as published by the Free Software Foundation; either version
   *		2 of the License, or (at your option) any later version.
   *
   * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
12
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #include <linux/skbuff.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
19
  #include <net/pkt_sched.h>
  
  /* 1 band FIFO pseudo-"scheduler" */
520ac30f4   Eric Dumazet   net_sched: drop p...
20
21
  static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  			 struct sk_buff **to_free)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  {
d276055c4   Eric Dumazet   net_sched: reduce...
23
  	if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <= sch->limit))
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
24
  		return qdisc_enqueue_tail(skb, sch);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25

520ac30f4   Eric Dumazet   net_sched: drop p...
26
  	return qdisc_drop(skb, sch, to_free);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  }
520ac30f4   Eric Dumazet   net_sched: drop p...
28
29
  static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  			 struct sk_buff **to_free)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  {
97d0678f9   Florian Westphal   sched: don't use ...
31
  	if (likely(sch->q.qlen < sch->limit))
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
32
  		return qdisc_enqueue_tail(skb, sch);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33

520ac30f4   Eric Dumazet   net_sched: drop p...
34
  	return qdisc_drop(skb, sch, to_free);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
  }
520ac30f4   Eric Dumazet   net_sched: drop p...
36
37
  static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  			      struct sk_buff **to_free)
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
38
  {
6c0d54f18   Eric Dumazet   net_sched: fix pf...
39
  	unsigned int prev_backlog;
97d0678f9   Florian Westphal   sched: don't use ...
40
  	if (likely(sch->q.qlen < sch->limit))
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
41
  		return qdisc_enqueue_tail(skb, sch);
6c0d54f18   Eric Dumazet   net_sched: fix pf...
42
  	prev_backlog = sch->qstats.backlog;
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
43
  	/* queue full, remove one skb to fulfill the limit */
520ac30f4   Eric Dumazet   net_sched: drop p...
44
  	__qdisc_queue_drop_head(sch, &sch->q, to_free);
25331d6ce   John Fastabend   net: sched: imple...
45
  	qdisc_qstats_drop(sch);
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
46
  	qdisc_enqueue_tail(skb, sch);
6c0d54f18   Eric Dumazet   net_sched: fix pf...
47
  	qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
48
49
  	return NET_XMIT_CN;
  }
1e90474c3   Patrick McHardy   [NET_SCHED]: Conv...
50
  static int fifo_init(struct Qdisc *sch, struct nlattr *opt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  {
23624935e   Eric Dumazet   net_sched: TCQ_F_...
52
53
  	bool bypass;
  	bool is_bfifo = sch->ops == &bfifo_qdisc_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  
  	if (opt == NULL) {
348e3435c   Phil Sutter   net: sched: drop ...
56
  		u32 limit = qdisc_dev(sch)->tx_queue_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57

23624935e   Eric Dumazet   net_sched: TCQ_F_...
58
  		if (is_bfifo)
6473990c7   Patrick McHardy   net-sched: fix bf...
59
  			limit *= psched_mtu(qdisc_dev(sch));
6fc8e84f4   Thomas Graf   [PKT_SCHED]: Clea...
60

d276055c4   Eric Dumazet   net_sched: reduce...
61
  		sch->limit = limit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  	} else {
1e90474c3   Patrick McHardy   [NET_SCHED]: Conv...
63
  		struct tc_fifo_qopt *ctl = nla_data(opt);
6fc8e84f4   Thomas Graf   [PKT_SCHED]: Clea...
64

1e90474c3   Patrick McHardy   [NET_SCHED]: Conv...
65
  		if (nla_len(opt) < sizeof(*ctl))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  			return -EINVAL;
6fc8e84f4   Thomas Graf   [PKT_SCHED]: Clea...
67

d276055c4   Eric Dumazet   net_sched: reduce...
68
  		sch->limit = ctl->limit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
  	}
6fc8e84f4   Thomas Graf   [PKT_SCHED]: Clea...
70

23624935e   Eric Dumazet   net_sched: TCQ_F_...
71
  	if (is_bfifo)
d276055c4   Eric Dumazet   net_sched: reduce...
72
  		bypass = sch->limit >= psched_mtu(qdisc_dev(sch));
23624935e   Eric Dumazet   net_sched: TCQ_F_...
73
  	else
d276055c4   Eric Dumazet   net_sched: reduce...
74
  		bypass = sch->limit >= 1;
23624935e   Eric Dumazet   net_sched: TCQ_F_...
75
76
77
78
79
  
  	if (bypass)
  		sch->flags |= TCQ_F_CAN_BYPASS;
  	else
  		sch->flags &= ~TCQ_F_CAN_BYPASS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
  	return 0;
  }
  
  static int fifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  {
d276055c4   Eric Dumazet   net_sched: reduce...
85
  	struct tc_fifo_qopt opt = { .limit = sch->limit };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86

1b34ec43c   David S. Miller   pkt_sched: Stop u...
87
88
  	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  		goto nla_put_failure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  	return skb->len;
1e90474c3   Patrick McHardy   [NET_SCHED]: Conv...
90
  nla_put_failure:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
  	return -1;
  }
20fea08b5   Eric Dumazet   [NET]: Move Qdisc...
93
  struct Qdisc_ops pfifo_qdisc_ops __read_mostly = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  	.id		=	"pfifo",
d276055c4   Eric Dumazet   net_sched: reduce...
95
  	.priv_size	=	0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  	.enqueue	=	pfifo_enqueue,
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
97
  	.dequeue	=	qdisc_dequeue_head,
48a8f519e   Patrick McHardy   pkt_sched: Add ->...
98
  	.peek		=	qdisc_peek_head,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  	.init		=	fifo_init,
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
100
  	.reset		=	qdisc_reset_queue,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
  	.change		=	fifo_init,
  	.dump		=	fifo_dump,
  	.owner		=	THIS_MODULE,
  };
62e3ba1b5   Patrick McHardy   [NET_SCHED]: Move...
105
  EXPORT_SYMBOL(pfifo_qdisc_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

20fea08b5   Eric Dumazet   [NET]: Move Qdisc...
107
  struct Qdisc_ops bfifo_qdisc_ops __read_mostly = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  	.id		=	"bfifo",
d276055c4   Eric Dumazet   net_sched: reduce...
109
  	.priv_size	=	0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  	.enqueue	=	bfifo_enqueue,
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
111
  	.dequeue	=	qdisc_dequeue_head,
48a8f519e   Patrick McHardy   pkt_sched: Add ->...
112
  	.peek		=	qdisc_peek_head,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  	.init		=	fifo_init,
aaae3013d   Thomas Graf   [PKT_SCHED]: Tran...
114
  	.reset		=	qdisc_reset_queue,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
  	.change		=	fifo_init,
  	.dump		=	fifo_dump,
  	.owner		=	THIS_MODULE,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
  EXPORT_SYMBOL(bfifo_qdisc_ops);
fb0305ce1   Patrick McHardy   net-sched: consol...
120

57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
121
122
  struct Qdisc_ops pfifo_head_drop_qdisc_ops __read_mostly = {
  	.id		=	"pfifo_head_drop",
d276055c4   Eric Dumazet   net_sched: reduce...
123
  	.priv_size	=	0,
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
124
125
126
  	.enqueue	=	pfifo_tail_enqueue,
  	.dequeue	=	qdisc_dequeue_head,
  	.peek		=	qdisc_peek_head,
57dbb2d83   Hagen Paul Pfeifer   sched: add head d...
127
128
129
130
131
132
  	.init		=	fifo_init,
  	.reset		=	qdisc_reset_queue,
  	.change		=	fifo_init,
  	.dump		=	fifo_dump,
  	.owner		=	THIS_MODULE,
  };
fb0305ce1   Patrick McHardy   net-sched: consol...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  /* Pass size change message down to embedded FIFO */
  int fifo_set_limit(struct Qdisc *q, unsigned int limit)
  {
  	struct nlattr *nla;
  	int ret = -ENOMEM;
  
  	/* Hack to avoid sending change message to non-FIFO */
  	if (strncmp(q->ops->id + 1, "fifo", 4) != 0)
  		return 0;
  
  	nla = kmalloc(nla_attr_size(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
  	if (nla) {
  		nla->nla_type = RTM_NEWQDISC;
  		nla->nla_len = nla_attr_size(sizeof(struct tc_fifo_qopt));
  		((struct tc_fifo_qopt *)nla_data(nla))->limit = limit;
  
  		ret = q->ops->change(q, nla);
  		kfree(nla);
  	}
  	return ret;
  }
  EXPORT_SYMBOL(fifo_set_limit);
  
  struct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
  			       unsigned int limit)
  {
  	struct Qdisc *q;
  	int err = -ENOMEM;
3511c9132   Changli Gao   net_sched: remove...
161
  	q = qdisc_create_dflt(sch->dev_queue, ops, TC_H_MAKE(sch->handle, 1));
fb0305ce1   Patrick McHardy   net-sched: consol...
162
163
164
165
166
167
168
169
170
171
172
  	if (q) {
  		err = fifo_set_limit(q, limit);
  		if (err < 0) {
  			qdisc_destroy(q);
  			q = NULL;
  		}
  	}
  
  	return q ? : ERR_PTR(err);
  }
  EXPORT_SYMBOL(fifo_create_dflt);