xref: /openbmc/linux/net/sched/sch_generic.c (revision 876d48aa)
1 /*
2  * net/sched/sch_generic.c	Generic packet scheduler routines.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *              Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11  *              - Ingress support
12  */
13 
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <linux/bitops.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/in.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
32 #include <linux/rcupdate.h>
33 #include <linux/list.h>
34 #include <net/sock.h>
35 #include <net/pkt_sched.h>
36 
37 /* Main transmission queue. */
38 
39 /* Modifications to data participating in scheduling must be protected with
40  * dev->queue_lock spinlock.
41  *
42  * The idea is the following:
43  * - enqueue, dequeue are serialized via top level device
44  *   spinlock dev->queue_lock.
45  * - ingress filtering is serialized via top level device
46  *   spinlock dev->ingress_lock.
47  * - updates to tree and tree walking are only done under the rtnl mutex.
48  */
49 
50 void qdisc_lock_tree(struct net_device *dev)
51 {
52 	spin_lock_bh(&dev->queue_lock);
53 	spin_lock(&dev->ingress_lock);
54 }
55 
56 void qdisc_unlock_tree(struct net_device *dev)
57 {
58 	spin_unlock(&dev->ingress_lock);
59 	spin_unlock_bh(&dev->queue_lock);
60 }
61 
62 static inline int qdisc_qlen(struct Qdisc *q)
63 {
64 	return q->q.qlen;
65 }
66 
67 static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
68 				  struct Qdisc *q)
69 {
70 	if (unlikely(skb->next))
71 		dev->gso_skb = skb;
72 	else
73 		q->ops->requeue(skb, q);
74 
75 	netif_schedule(dev);
76 	return 0;
77 }
78 
79 static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
80 					      struct Qdisc *q)
81 {
82 	struct sk_buff *skb;
83 
84 	if ((skb = dev->gso_skb))
85 		dev->gso_skb = NULL;
86 	else
87 		skb = q->dequeue(q);
88 
89 	return skb;
90 }
91 
92 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
93 					   struct net_device *dev,
94 					   struct Qdisc *q)
95 {
96 	int ret;
97 
98 	if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
99 		/*
100 		 * Same CPU holding the lock. It may be a transient
101 		 * configuration error, when hard_start_xmit() recurses. We
102 		 * detect it by checking xmit owner and drop the packet when
103 		 * deadloop is detected. Return OK to try the next skb.
104 		 */
105 		kfree_skb(skb);
106 		if (net_ratelimit())
107 			printk(KERN_WARNING "Dead loop on netdevice %s, "
108 			       "fix it urgently!\n", dev->name);
109 		ret = qdisc_qlen(q);
110 	} else {
111 		/*
112 		 * Another cpu is holding lock, requeue & delay xmits for
113 		 * some time.
114 		 */
115 		__get_cpu_var(netdev_rx_stat).cpu_collision++;
116 		ret = dev_requeue_skb(skb, dev, q);
117 	}
118 
119 	return ret;
120 }
121 
122 /*
123  * NOTE: Called under dev->queue_lock with locally disabled BH.
124  *
125  * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
126  * device at a time. dev->queue_lock serializes queue accesses for
127  * this device AND dev->qdisc pointer itself.
128  *
129  *  netif_tx_lock serializes accesses to device driver.
130  *
131  *  dev->queue_lock and netif_tx_lock are mutually exclusive,
132  *  if one is grabbed, another must be free.
133  *
134  * Note, that this procedure can be called by a watchdog timer
135  *
136  * Returns to the caller:
137  *				0  - queue is empty or throttled.
138  *				>0 - queue is not empty.
139  *
140  */
141 static inline int qdisc_restart(struct net_device *dev)
142 {
143 	struct Qdisc *q = dev->qdisc;
144 	struct sk_buff *skb;
145 	unsigned lockless;
146 	int ret;
147 
148 	/* Dequeue packet */
149 	if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
150 		return 0;
151 
152 	/*
153 	 * When the driver has LLTX set, it does its own locking in
154 	 * start_xmit. These checks are worth it because even uncongested
155 	 * locks can be quite expensive. The driver can do a trylock, as
156 	 * is being done here; in case of lock contention it should return
157 	 * NETDEV_TX_LOCKED and the packet will be requeued.
158 	 */
159 	lockless = (dev->features & NETIF_F_LLTX);
160 
161 	if (!lockless && !netif_tx_trylock(dev)) {
162 		/* Another CPU grabbed the driver tx lock */
163 		return handle_dev_cpu_collision(skb, dev, q);
164 	}
165 
166 	/* And release queue */
167 	spin_unlock(&dev->queue_lock);
168 
169 	ret = dev_hard_start_xmit(skb, dev);
170 
171 	if (!lockless)
172 		netif_tx_unlock(dev);
173 
174 	spin_lock(&dev->queue_lock);
175 	q = dev->qdisc;
176 
177 	switch (ret) {
178 	case NETDEV_TX_OK:
179 		/* Driver sent out skb successfully */
180 		ret = qdisc_qlen(q);
181 		break;
182 
183 	case NETDEV_TX_LOCKED:
184 		/* Driver try lock failed */
185 		ret = handle_dev_cpu_collision(skb, dev, q);
186 		break;
187 
188 	default:
189 		/* Driver returned NETDEV_TX_BUSY - requeue skb */
190 		if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
191 			printk(KERN_WARNING "BUG %s code %d qlen %d\n",
192 			       dev->name, ret, q->q.qlen);
193 
194 		ret = dev_requeue_skb(skb, dev, q);
195 		break;
196 	}
197 
198 	return ret;
199 }
200 
201 void __qdisc_run(struct net_device *dev)
202 {
203 	do {
204 		if (!qdisc_restart(dev))
205 			break;
206 	} while (!netif_queue_stopped(dev));
207 
208 	clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
209 }
210 
211 static void dev_watchdog(unsigned long arg)
212 {
213 	struct net_device *dev = (struct net_device *)arg;
214 
215 	netif_tx_lock(dev);
216 	if (dev->qdisc != &noop_qdisc) {
217 		if (netif_device_present(dev) &&
218 		    netif_running(dev) &&
219 		    netif_carrier_ok(dev)) {
220 			if (netif_queue_stopped(dev) &&
221 			    time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
222 
223 				printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
224 				       dev->name);
225 				dev->tx_timeout(dev);
226 			}
227 			if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
228 				dev_hold(dev);
229 		}
230 	}
231 	netif_tx_unlock(dev);
232 
233 	dev_put(dev);
234 }
235 
236 static void dev_watchdog_init(struct net_device *dev)
237 {
238 	init_timer(&dev->watchdog_timer);
239 	dev->watchdog_timer.data = (unsigned long)dev;
240 	dev->watchdog_timer.function = dev_watchdog;
241 }
242 
243 void __netdev_watchdog_up(struct net_device *dev)
244 {
245 	if (dev->tx_timeout) {
246 		if (dev->watchdog_timeo <= 0)
247 			dev->watchdog_timeo = 5*HZ;
248 		if (!mod_timer(&dev->watchdog_timer,
249 			       round_jiffies(jiffies + dev->watchdog_timeo)))
250 			dev_hold(dev);
251 	}
252 }
253 
254 static void dev_watchdog_up(struct net_device *dev)
255 {
256 	__netdev_watchdog_up(dev);
257 }
258 
259 static void dev_watchdog_down(struct net_device *dev)
260 {
261 	netif_tx_lock_bh(dev);
262 	if (del_timer(&dev->watchdog_timer))
263 		dev_put(dev);
264 	netif_tx_unlock_bh(dev);
265 }
266 
267 void netif_carrier_on(struct net_device *dev)
268 {
269 	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state))
270 		linkwatch_fire_event(dev);
271 	if (netif_running(dev))
272 		__netdev_watchdog_up(dev);
273 }
274 
275 void netif_carrier_off(struct net_device *dev)
276 {
277 	if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
278 		linkwatch_fire_event(dev);
279 }
280 
281 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
282    under all circumstances. It is difficult to invent anything faster or
283    cheaper.
284  */
285 
286 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
287 {
288 	kfree_skb(skb);
289 	return NET_XMIT_CN;
290 }
291 
292 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
293 {
294 	return NULL;
295 }
296 
297 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
298 {
299 	if (net_ratelimit())
300 		printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
301 		       skb->dev->name);
302 	kfree_skb(skb);
303 	return NET_XMIT_CN;
304 }
305 
306 struct Qdisc_ops noop_qdisc_ops = {
307 	.id		=	"noop",
308 	.priv_size	=	0,
309 	.enqueue	=	noop_enqueue,
310 	.dequeue	=	noop_dequeue,
311 	.requeue	=	noop_requeue,
312 	.owner		=	THIS_MODULE,
313 };
314 
315 struct Qdisc noop_qdisc = {
316 	.enqueue	=	noop_enqueue,
317 	.dequeue	=	noop_dequeue,
318 	.flags		=	TCQ_F_BUILTIN,
319 	.ops		=	&noop_qdisc_ops,
320 	.list		=	LIST_HEAD_INIT(noop_qdisc.list),
321 };
322 
323 static struct Qdisc_ops noqueue_qdisc_ops = {
324 	.id		=	"noqueue",
325 	.priv_size	=	0,
326 	.enqueue	=	noop_enqueue,
327 	.dequeue	=	noop_dequeue,
328 	.requeue	=	noop_requeue,
329 	.owner		=	THIS_MODULE,
330 };
331 
332 static struct Qdisc noqueue_qdisc = {
333 	.enqueue	=	NULL,
334 	.dequeue	=	noop_dequeue,
335 	.flags		=	TCQ_F_BUILTIN,
336 	.ops		=	&noqueue_qdisc_ops,
337 	.list		=	LIST_HEAD_INIT(noqueue_qdisc.list),
338 };
339 
340 
341 static const u8 prio2band[TC_PRIO_MAX+1] =
342 	{ 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
343 
344 /* 3-band FIFO queue: old style, but should be a bit faster than
345    generic prio+fifo combination.
346  */
347 
348 #define PFIFO_FAST_BANDS 3
349 
350 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
351 					     struct Qdisc *qdisc)
352 {
353 	struct sk_buff_head *list = qdisc_priv(qdisc);
354 	return list + prio2band[skb->priority & TC_PRIO_MAX];
355 }
356 
357 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
358 {
359 	struct sk_buff_head *list = prio2list(skb, qdisc);
360 
361 	if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
362 		qdisc->q.qlen++;
363 		return __qdisc_enqueue_tail(skb, qdisc, list);
364 	}
365 
366 	return qdisc_drop(skb, qdisc);
367 }
368 
369 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
370 {
371 	int prio;
372 	struct sk_buff_head *list = qdisc_priv(qdisc);
373 
374 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
375 		if (!skb_queue_empty(list + prio)) {
376 			qdisc->q.qlen--;
377 			return __qdisc_dequeue_head(qdisc, list + prio);
378 		}
379 	}
380 
381 	return NULL;
382 }
383 
384 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
385 {
386 	qdisc->q.qlen++;
387 	return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
388 }
389 
390 static void pfifo_fast_reset(struct Qdisc* qdisc)
391 {
392 	int prio;
393 	struct sk_buff_head *list = qdisc_priv(qdisc);
394 
395 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
396 		__qdisc_reset_queue(qdisc, list + prio);
397 
398 	qdisc->qstats.backlog = 0;
399 	qdisc->q.qlen = 0;
400 }
401 
402 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
403 {
404 	struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
405 
406 	memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
407 	RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
408 	return skb->len;
409 
410 rtattr_failure:
411 	return -1;
412 }
413 
414 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
415 {
416 	int prio;
417 	struct sk_buff_head *list = qdisc_priv(qdisc);
418 
419 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
420 		skb_queue_head_init(list + prio);
421 
422 	return 0;
423 }
424 
425 static struct Qdisc_ops pfifo_fast_ops = {
426 	.id		=	"pfifo_fast",
427 	.priv_size	=	PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
428 	.enqueue	=	pfifo_fast_enqueue,
429 	.dequeue	=	pfifo_fast_dequeue,
430 	.requeue	=	pfifo_fast_requeue,
431 	.init		=	pfifo_fast_init,
432 	.reset		=	pfifo_fast_reset,
433 	.dump		=	pfifo_fast_dump,
434 	.owner		=	THIS_MODULE,
435 };
436 
437 struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
438 {
439 	void *p;
440 	struct Qdisc *sch;
441 	unsigned int size;
442 	int err = -ENOBUFS;
443 
444 	/* ensure that the Qdisc and the private data are 32-byte aligned */
445 	size = QDISC_ALIGN(sizeof(*sch));
446 	size += ops->priv_size + (QDISC_ALIGNTO - 1);
447 
448 	p = kzalloc(size, GFP_KERNEL);
449 	if (!p)
450 		goto errout;
451 	sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
452 	sch->padded = (char *) sch - (char *) p;
453 
454 	INIT_LIST_HEAD(&sch->list);
455 	skb_queue_head_init(&sch->q);
456 	sch->ops = ops;
457 	sch->enqueue = ops->enqueue;
458 	sch->dequeue = ops->dequeue;
459 	sch->dev = dev;
460 	dev_hold(dev);
461 	atomic_set(&sch->refcnt, 1);
462 
463 	return sch;
464 errout:
465 	return ERR_PTR(-err);
466 }
467 
468 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
469 				 unsigned int parentid)
470 {
471 	struct Qdisc *sch;
472 
473 	sch = qdisc_alloc(dev, ops);
474 	if (IS_ERR(sch))
475 		goto errout;
476 	sch->stats_lock = &dev->queue_lock;
477 	sch->parent = parentid;
478 
479 	if (!ops->init || ops->init(sch, NULL) == 0)
480 		return sch;
481 
482 	qdisc_destroy(sch);
483 errout:
484 	return NULL;
485 }
486 
487 /* Under dev->queue_lock and BH! */
488 
489 void qdisc_reset(struct Qdisc *qdisc)
490 {
491 	struct Qdisc_ops *ops = qdisc->ops;
492 
493 	if (ops->reset)
494 		ops->reset(qdisc);
495 }
496 
497 /* this is the rcu callback function to clean up a qdisc when there
498  * are no further references to it */
499 
500 static void __qdisc_destroy(struct rcu_head *head)
501 {
502 	struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
503 	kfree((char *) qdisc - qdisc->padded);
504 }
505 
506 /* Under dev->queue_lock and BH! */
507 
508 void qdisc_destroy(struct Qdisc *qdisc)
509 {
510 	struct Qdisc_ops  *ops = qdisc->ops;
511 
512 	if (qdisc->flags & TCQ_F_BUILTIN ||
513 	    !atomic_dec_and_test(&qdisc->refcnt))
514 		return;
515 
516 	list_del(&qdisc->list);
517 	gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
518 	if (ops->reset)
519 		ops->reset(qdisc);
520 	if (ops->destroy)
521 		ops->destroy(qdisc);
522 
523 	module_put(ops->owner);
524 	dev_put(qdisc->dev);
525 	call_rcu(&qdisc->q_rcu, __qdisc_destroy);
526 }
527 
528 void dev_activate(struct net_device *dev)
529 {
530 	/* No queueing discipline is attached to device;
531 	   create default one i.e. pfifo_fast for devices,
532 	   which need queueing and noqueue_qdisc for
533 	   virtual interfaces
534 	 */
535 
536 	if (dev->qdisc_sleeping == &noop_qdisc) {
537 		struct Qdisc *qdisc;
538 		if (dev->tx_queue_len) {
539 			qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
540 						  TC_H_ROOT);
541 			if (qdisc == NULL) {
542 				printk(KERN_INFO "%s: activation failed\n", dev->name);
543 				return;
544 			}
545 			list_add_tail(&qdisc->list, &dev->qdisc_list);
546 		} else {
547 			qdisc =  &noqueue_qdisc;
548 		}
549 		dev->qdisc_sleeping = qdisc;
550 	}
551 
552 	if (!netif_carrier_ok(dev))
553 		/* Delay activation until next carrier-on event */
554 		return;
555 
556 	spin_lock_bh(&dev->queue_lock);
557 	rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
558 	if (dev->qdisc != &noqueue_qdisc) {
559 		dev->trans_start = jiffies;
560 		dev_watchdog_up(dev);
561 	}
562 	spin_unlock_bh(&dev->queue_lock);
563 }
564 
565 void dev_deactivate(struct net_device *dev)
566 {
567 	struct Qdisc *qdisc;
568 	struct sk_buff *skb;
569 
570 	spin_lock_bh(&dev->queue_lock);
571 	qdisc = dev->qdisc;
572 	dev->qdisc = &noop_qdisc;
573 
574 	qdisc_reset(qdisc);
575 
576 	skb = dev->gso_skb;
577 	dev->gso_skb = NULL;
578 	spin_unlock_bh(&dev->queue_lock);
579 
580 	kfree_skb(skb);
581 
582 	dev_watchdog_down(dev);
583 
584 	/* Wait for outstanding dev_queue_xmit calls. */
585 	synchronize_rcu();
586 
587 	/* Wait for outstanding qdisc_run calls. */
588 	while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
589 		yield();
590 }
591 
592 void dev_init_scheduler(struct net_device *dev)
593 {
594 	qdisc_lock_tree(dev);
595 	dev->qdisc = &noop_qdisc;
596 	dev->qdisc_sleeping = &noop_qdisc;
597 	INIT_LIST_HEAD(&dev->qdisc_list);
598 	qdisc_unlock_tree(dev);
599 
600 	dev_watchdog_init(dev);
601 }
602 
603 void dev_shutdown(struct net_device *dev)
604 {
605 	struct Qdisc *qdisc;
606 
607 	qdisc_lock_tree(dev);
608 	qdisc = dev->qdisc_sleeping;
609 	dev->qdisc = &noop_qdisc;
610 	dev->qdisc_sleeping = &noop_qdisc;
611 	qdisc_destroy(qdisc);
612 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
613 	if ((qdisc = dev->qdisc_ingress) != NULL) {
614 		dev->qdisc_ingress = NULL;
615 		qdisc_destroy(qdisc);
616 	}
617 #endif
618 	BUG_TRAP(!timer_pending(&dev->watchdog_timer));
619 	qdisc_unlock_tree(dev);
620 }
621 
622 EXPORT_SYMBOL(netif_carrier_on);
623 EXPORT_SYMBOL(netif_carrier_off);
624 EXPORT_SYMBOL(noop_qdisc);
625 EXPORT_SYMBOL(qdisc_create_dflt);
626 EXPORT_SYMBOL(qdisc_destroy);
627 EXPORT_SYMBOL(qdisc_reset);
628 EXPORT_SYMBOL(qdisc_lock_tree);
629 EXPORT_SYMBOL(qdisc_unlock_tree);
630