2 * Copyright 2004, Instant802 Networks, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/module.h>
12 #include <linux/if_arp.h>
13 #include <linux/types.h>
15 #include <net/pkt_sched.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
21 /* maximum number of hardware queues we support. */
22 #define QD_MAX_QUEUES (IEEE80211_MAX_AMPDU_QUEUES + IEEE80211_MAX_QUEUES)
23 /* current number of hardware queues we support. */
24 #define QD_NUM(hw) ((hw)->queues + (hw)->ampdu_queues)
27 * Default mapping in classifier to work with default
30 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
32 struct ieee80211_sched_data
34 unsigned long qdisc_pool[BITS_TO_LONGS(QD_MAX_QUEUES)];
35 struct tcf_proto *filter_list;
36 struct Qdisc *queues[QD_MAX_QUEUES];
37 struct sk_buff_head requeued[QD_MAX_QUEUES];
40 static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
42 /* given a data frame determine the 802.1p/1d tag to use */
43 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
49 struct ieee80211_sched_data *q = qdisc_priv(qd);
50 struct tcf_result res = { -1, 0 };
52 /* if there is a user set filter list, call out to that */
54 tc_classify(skb, q->filter_list, &res);
59 /* skb->priority values from 256->263 are magic values to
60 * directly indicate a specific 802.1d priority.
61 * This is used to allow 802.1d priority to be passed directly in
62 * from VLAN tags, etc. */
63 if (skb->priority >= 256 && skb->priority <= 263)
64 return skb->priority - 256;
66 /* check there is a valid IP header present */
67 offset = ieee80211_get_hdrlen_from_skb(skb);
68 if (skb->len < offset + sizeof(llc_ip_hdr) + sizeof(*ip) ||
69 memcmp(skb->data + offset, llc_ip_hdr, sizeof(llc_ip_hdr)))
72 ip = (struct iphdr *) (skb->data + offset + sizeof(llc_ip_hdr));
74 dscp = ip->tos & 0xfc;
81 static inline int wme_downgrade_ac(struct sk_buff *skb)
83 switch (skb->priority) {
86 skb->priority = 5; /* VO -> VI */
90 skb->priority = 3; /* VI -> BE */
94 skb->priority = 2; /* BE -> BK */
102 /* positive return value indicates which queue to use
103 * negative return value indicates to drop the frame */
104 static int classify80211(struct sk_buff *skb, struct Qdisc *qd)
106 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
107 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
109 if (!ieee80211_is_data(hdr->frame_control)) {
110 /* management frames go on AC_VO queue, but are sent
111 * without QoS control fields */
115 if (0 /* injected */) {
116 /* use AC from radiotap */
119 if (!ieee80211_is_data_qos(hdr->frame_control)) {
120 skb->priority = 0; /* required for correct WPA/11i MIC */
121 return ieee802_1d_to_ac[skb->priority];
124 /* use the data classifier to determine what 802.1d tag the
126 skb->priority = classify_1d(skb, qd);
128 /* in case we are a client verify acm is not set for this ac */
129 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
130 if (wme_downgrade_ac(skb)) {
131 /* No AC with lower priority has acm=0, drop packet. */
136 /* look up which queue to use for frames with this 1d tag */
137 return ieee802_1d_to_ac[skb->priority];
141 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
143 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
144 struct ieee80211_hw *hw = &local->hw;
145 struct ieee80211_sched_data *q = qdisc_priv(qd);
146 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
147 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
149 struct sta_info *sta;
153 if (info->flags & IEEE80211_TX_CTL_REQUEUE) {
154 queue = skb_get_queue_mapping(skb);
156 sta = sta_info_get(local, hdr->addr1);
157 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
159 int ampdu_queue = sta->tid_to_tx_q[tid];
160 if ((ampdu_queue < QD_NUM(hw)) &&
161 test_bit(ampdu_queue, q->qdisc_pool)) {
163 info->flags |= IEEE80211_TX_CTL_AMPDU;
165 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
169 skb_queue_tail(&q->requeued[queue], skb);
174 queue = classify80211(skb, qd);
176 if (unlikely(queue >= local->hw.queues))
177 queue = local->hw.queues - 1;
179 /* now we know the 1d priority, fill in the QoS header if there is one
181 if (ieee80211_is_data_qos(hdr->frame_control)) {
182 u8 *p = ieee80211_get_qos_ctl(hdr);
184 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
185 if (local->wifi_wme_noack_test)
186 ack_policy |= QOS_CONTROL_ACK_POLICY_NOACK <<
187 QOS_CONTROL_ACK_POLICY_SHIFT;
188 /* qos header is 2 bytes, second reserved */
189 *p++ = ack_policy | tid;
194 sta = sta_info_get(local, hdr->addr1);
196 int ampdu_queue = sta->tid_to_tx_q[tid];
197 if ((ampdu_queue < QD_NUM(hw)) &&
198 test_bit(ampdu_queue, q->qdisc_pool)) {
200 info->flags |= IEEE80211_TX_CTL_AMPDU;
202 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
209 if (unlikely(queue < 0)) {
213 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
214 skb_set_queue_mapping(skb, queue);
215 qdisc = q->queues[queue];
216 err = qdisc->enqueue(skb, qdisc);
217 if (err == NET_XMIT_SUCCESS) {
219 qd->bstats.bytes += skb->len;
220 qd->bstats.packets++;
221 return NET_XMIT_SUCCESS;
229 /* TODO: clean up the cases where master_hard_start_xmit
230 * returns non 0 - it shouldn't ever do that. Once done we
231 * can remove this function */
232 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
234 struct ieee80211_sched_data *q = qdisc_priv(qd);
238 /* we recorded which queue to use earlier! */
239 qdisc = q->queues[skb_get_queue_mapping(skb)];
241 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
250 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
252 struct ieee80211_sched_data *q = qdisc_priv(qd);
253 struct net_device *dev = qd->dev;
254 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
255 struct ieee80211_hw *hw = &local->hw;
260 /* check all the h/w queues in numeric/priority order */
261 for (queue = 0; queue < QD_NUM(hw); queue++) {
262 /* see if there is room in this hardware queue */
263 if (__netif_subqueue_stopped(local->mdev, queue) ||
264 !test_bit(queue, q->qdisc_pool))
267 /* there is space - try and get a frame */
268 skb = skb_dequeue(&q->requeued[queue]);
274 qdisc = q->queues[queue];
275 skb = qdisc->dequeue(qdisc);
281 /* returning a NULL here when all the h/w queues are full means we
282 * never need to call netif_stop_queue in the driver */
287 static void wme_qdiscop_reset(struct Qdisc* qd)
289 struct ieee80211_sched_data *q = qdisc_priv(qd);
290 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
291 struct ieee80211_hw *hw = &local->hw;
294 /* QUESTION: should we have some hardware flush functionality here? */
296 for (queue = 0; queue < QD_NUM(hw); queue++) {
297 skb_queue_purge(&q->requeued[queue]);
298 qdisc_reset(q->queues[queue]);
304 static void wme_qdiscop_destroy(struct Qdisc* qd)
306 struct ieee80211_sched_data *q = qdisc_priv(qd);
307 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
308 struct ieee80211_hw *hw = &local->hw;
311 tcf_destroy_chain(&q->filter_list);
313 for (queue = 0; queue < QD_NUM(hw); queue++) {
314 skb_queue_purge(&q->requeued[queue]);
315 qdisc_destroy(q->queues[queue]);
316 q->queues[queue] = &noop_qdisc;
321 /* called whenever parameters are updated on existing qdisc */
322 static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
328 /* called during initial creation of qdisc on device */
329 static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
331 struct ieee80211_sched_data *q = qdisc_priv(qd);
332 struct net_device *dev = qd->dev;
333 struct ieee80211_local *local;
334 struct ieee80211_hw *hw;
337 /* check that device is a mac80211 device */
338 if (!dev->ieee80211_ptr ||
339 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
342 local = wdev_priv(dev->ieee80211_ptr);
345 /* only allow on master dev */
346 if (dev != local->mdev)
349 /* ensure that we are root qdisc */
350 if (qd->parent != TC_H_ROOT)
353 if (qd->flags & TCQ_F_INGRESS)
356 /* if options were passed in, set them */
358 err = wme_qdiscop_tune(qd, opt);
360 /* create child queues */
361 for (i = 0; i < QD_NUM(hw); i++) {
362 skb_queue_head_init(&q->requeued[i]);
363 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
366 q->queues[i] = &noop_qdisc;
367 printk(KERN_ERR "%s child qdisc %i creation failed\n",
372 /* non-aggregation queues: reserve/mark as used */
373 for (i = 0; i < local->hw.queues; i++)
374 set_bit(i, q->qdisc_pool);
379 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
385 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
386 struct Qdisc *new, struct Qdisc **old)
388 struct ieee80211_sched_data *q = qdisc_priv(qd);
389 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
390 struct ieee80211_hw *hw = &local->hw;
391 unsigned long queue = arg - 1;
393 if (queue >= QD_NUM(hw))
400 *old = q->queues[queue];
401 q->queues[queue] = new;
409 static struct Qdisc *
410 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
412 struct ieee80211_sched_data *q = qdisc_priv(qd);
413 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
414 struct ieee80211_hw *hw = &local->hw;
415 unsigned long queue = arg - 1;
417 if (queue >= QD_NUM(hw))
420 return q->queues[queue];
424 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
426 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
427 struct ieee80211_hw *hw = &local->hw;
428 unsigned long queue = TC_H_MIN(classid);
430 if (queue - 1 >= QD_NUM(hw))
437 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
440 return wme_classop_get(qd, classid);
444 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
449 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
450 struct nlattr **tca, unsigned long *arg)
452 unsigned long cl = *arg;
453 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
454 struct ieee80211_hw *hw = &local->hw;
456 if (cl - 1 > QD_NUM(hw))
459 /* TODO: put code to program hardware queue parameters here,
460 * to allow programming from tc command line */
466 /* we don't support deleting hardware queues
467 * when we add WMM-SA support - TSPECs may be deleted here */
468 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
470 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
471 struct ieee80211_hw *hw = &local->hw;
473 if (cl - 1 > QD_NUM(hw))
479 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
480 struct sk_buff *skb, struct tcmsg *tcm)
482 struct ieee80211_sched_data *q = qdisc_priv(qd);
483 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
484 struct ieee80211_hw *hw = &local->hw;
486 if (cl - 1 > QD_NUM(hw))
488 tcm->tcm_handle = TC_H_MIN(cl);
489 tcm->tcm_parent = qd->handle;
490 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
495 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
497 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
498 struct ieee80211_hw *hw = &local->hw;
504 for (queue = 0; queue < QD_NUM(hw); queue++) {
505 if (arg->count < arg->skip) {
509 /* we should return classids for our internal queues here
510 * as well as the external ones */
511 if (arg->fn(qd, queue+1, arg) < 0) {
520 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
523 struct ieee80211_sched_data *q = qdisc_priv(qd);
528 return &q->filter_list;
532 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
533 * - these are the operations on the classes */
534 static const struct Qdisc_class_ops class_ops =
536 .graft = wme_classop_graft,
537 .leaf = wme_classop_leaf,
539 .get = wme_classop_get,
540 .put = wme_classop_put,
541 .change = wme_classop_change,
542 .delete = wme_classop_delete,
543 .walk = wme_classop_walk,
545 .tcf_chain = wme_classop_find_tcf,
546 .bind_tcf = wme_classop_bind,
547 .unbind_tcf = wme_classop_put,
549 .dump = wme_classop_dump_class,
553 /* queueing discipline operations */
554 static struct Qdisc_ops wme_qdisc_ops __read_mostly =
557 .cl_ops = &class_ops,
559 .priv_size = sizeof(struct ieee80211_sched_data),
561 .enqueue = wme_qdiscop_enqueue,
562 .dequeue = wme_qdiscop_dequeue,
563 .requeue = wme_qdiscop_requeue,
564 .drop = NULL, /* drop not needed since we are always the root qdisc */
566 .init = wme_qdiscop_init,
567 .reset = wme_qdiscop_reset,
568 .destroy = wme_qdiscop_destroy,
569 .change = wme_qdiscop_tune,
571 .dump = wme_qdiscop_dump,
575 void ieee80211_install_qdisc(struct net_device *dev)
579 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
581 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
585 /* same handle as would be allocated by qdisc_alloc_handle() */
586 qdisc->handle = 0x80010000;
588 qdisc_lock_tree(dev);
589 list_add_tail(&qdisc->list, &dev->qdisc_list);
590 dev->qdisc_sleeping = qdisc;
591 qdisc_unlock_tree(dev);
595 int ieee80211_qdisc_installed(struct net_device *dev)
597 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
601 int ieee80211_wme_register(void)
603 return register_qdisc(&wme_qdisc_ops);
607 void ieee80211_wme_unregister(void)
609 unregister_qdisc(&wme_qdisc_ops);
612 int ieee80211_ht_agg_queue_add(struct ieee80211_local *local,
613 struct sta_info *sta, u16 tid)
616 struct ieee80211_sched_data *q =
617 qdisc_priv(local->mdev->qdisc_sleeping);
618 DECLARE_MAC_BUF(mac);
620 /* prepare the filter and save it for the SW queue
621 * matching the received HW queue */
623 if (!local->hw.ampdu_queues)
626 /* try to get a Qdisc from the pool */
627 for (i = local->hw.queues; i < QD_NUM(&local->hw); i++)
628 if (!test_and_set_bit(i, q->qdisc_pool)) {
629 ieee80211_stop_queue(local_to_hw(local), i);
630 sta->tid_to_tx_q[tid] = i;
632 /* IF there are already pending packets
633 * on this tid first we need to drain them
634 * on the previous queue
635 * since HT is strict in order */
636 #ifdef CONFIG_MAC80211_HT_DEBUG
638 printk(KERN_DEBUG "allocated aggregation queue"
639 " %d tid %d addr %s pool=0x%lX\n",
640 i, tid, print_mac(mac, sta->addr),
642 #endif /* CONFIG_MAC80211_HT_DEBUG */
650 * the caller needs to hold local->mdev->queue_lock
652 void ieee80211_ht_agg_queue_remove(struct ieee80211_local *local,
653 struct sta_info *sta, u16 tid,
656 struct ieee80211_hw *hw = &local->hw;
657 struct ieee80211_sched_data *q =
658 qdisc_priv(local->mdev->qdisc_sleeping);
659 int agg_queue = sta->tid_to_tx_q[tid];
661 /* return the qdisc to the pool */
662 clear_bit(agg_queue, q->qdisc_pool);
663 sta->tid_to_tx_q[tid] = QD_NUM(hw);
666 ieee80211_requeue(local, agg_queue);
668 q->queues[agg_queue]->ops->reset(q->queues[agg_queue]);
671 void ieee80211_requeue(struct ieee80211_local *local, int queue)
673 struct Qdisc *root_qd = local->mdev->qdisc_sleeping;
674 struct ieee80211_sched_data *q = qdisc_priv(root_qd);
675 struct Qdisc *qdisc = q->queues[queue];
676 struct sk_buff *skb = NULL;
679 if (!qdisc || !qdisc->dequeue)
682 for (len = qdisc->q.qlen; len > 0; len--) {
683 skb = qdisc->dequeue(qdisc);
685 /* packet will be classified again and */
686 /* skb->packet_data->queue will be overridden if needed */
688 wme_qdiscop_enqueue(skb, root_qd);