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 TC_80211_MAX_QUEUES 16
24 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
26 struct ieee80211_sched_data
28 unsigned long qdisc_pool[BITS_TO_LONGS(TC_80211_MAX_QUEUES)];
29 struct tcf_proto *filter_list;
30 struct Qdisc *queues[TC_80211_MAX_QUEUES];
31 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
34 static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
36 /* given a data frame determine the 802.1p/1d tag to use */
37 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
43 struct ieee80211_sched_data *q = qdisc_priv(qd);
44 struct tcf_result res = { -1, 0 };
46 /* if there is a user set filter list, call out to that */
48 tc_classify(skb, q->filter_list, &res);
53 /* skb->priority values from 256->263 are magic values to
54 * directly indicate a specific 802.1d priority.
55 * This is used to allow 802.1d priority to be passed directly in
56 * from VLAN tags, etc. */
57 if (skb->priority >= 256 && skb->priority <= 263)
58 return skb->priority - 256;
60 /* check there is a valid IP header present */
61 offset = ieee80211_get_hdrlen_from_skb(skb);
62 if (skb->len < offset + sizeof(llc_ip_hdr) + sizeof(*ip) ||
63 memcmp(skb->data + offset, llc_ip_hdr, sizeof(llc_ip_hdr)))
66 ip = (struct iphdr *) (skb->data + offset + sizeof(llc_ip_hdr));
68 dscp = ip->tos & 0xfc;
75 static inline int wme_downgrade_ac(struct sk_buff *skb)
77 switch (skb->priority) {
80 skb->priority = 5; /* VO -> VI */
84 skb->priority = 3; /* VI -> BE */
88 skb->priority = 2; /* BE -> BK */
96 /* positive return value indicates which queue to use
97 * negative return value indicates to drop the frame */
98 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
100 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
101 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
102 unsigned short fc = le16_to_cpu(hdr->frame_control);
105 /* see if frame is data or non data frame */
106 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
107 /* management frames go on AC_VO queue, but are sent
108 * without QoS control fields */
109 return IEEE80211_TX_QUEUE_DATA0;
112 if (0 /* injected */) {
113 /* use AC from radiotap */
116 /* is this a QoS frame? */
117 qos = fc & IEEE80211_STYPE_QOS_DATA;
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_sched_data *q = qdisc_priv(qd);
145 struct ieee80211_tx_packet_data *pkt_data =
146 (struct ieee80211_tx_packet_data *) skb->cb;
147 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
148 unsigned short fc = le16_to_cpu(hdr->frame_control);
151 struct sta_info *sta;
154 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
155 queue = pkt_data->queue;
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 < local->hw.queues) &&
161 test_bit(ampdu_queue, q->qdisc_pool)) {
163 pkt_data->flags |= IEEE80211_TXPD_AMPDU;
165 pkt_data->flags &= ~IEEE80211_TXPD_AMPDU;
169 skb_queue_tail(&q->requeued[queue], skb);
174 queue = classify80211(skb, qd);
176 /* now we know the 1d priority, fill in the QoS header if there is one
178 if (WLAN_FC_IS_QOS_DATA(fc)) {
179 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
181 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
182 if (local->wifi_wme_noack_test)
183 ack_policy |= QOS_CONTROL_ACK_POLICY_NOACK <<
184 QOS_CONTROL_ACK_POLICY_SHIFT;
185 /* qos header is 2 bytes, second reserved */
186 *p = ack_policy | tid;
190 sta = sta_info_get(local, hdr->addr1);
192 int ampdu_queue = sta->tid_to_tx_q[tid];
193 if ((ampdu_queue < local->hw.queues) &&
194 test_bit(ampdu_queue, q->qdisc_pool)) {
196 pkt_data->flags |= IEEE80211_TXPD_AMPDU;
198 pkt_data->flags &= ~IEEE80211_TXPD_AMPDU;
204 if (unlikely(queue >= local->hw.queues)) {
206 if (net_ratelimit()) {
207 printk(KERN_DEBUG "%s - queue=%d (hw does not "
209 __func__, queue, local->hw.queues - 1);
212 queue = local->hw.queues - 1;
215 if (unlikely(queue < 0)) {
219 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
220 pkt_data->queue = (unsigned int) queue;
221 qdisc = q->queues[queue];
222 err = qdisc->enqueue(skb, qdisc);
223 if (err == NET_XMIT_SUCCESS) {
225 qd->bstats.bytes += skb->len;
226 qd->bstats.packets++;
227 return NET_XMIT_SUCCESS;
235 /* TODO: clean up the cases where master_hard_start_xmit
236 * returns non 0 - it shouldn't ever do that. Once done we
237 * can remove this function */
238 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
240 struct ieee80211_sched_data *q = qdisc_priv(qd);
241 struct ieee80211_tx_packet_data *pkt_data =
242 (struct ieee80211_tx_packet_data *) skb->cb;
246 /* we recorded which queue to use earlier! */
247 qdisc = q->queues[pkt_data->queue];
249 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
258 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
260 struct ieee80211_sched_data *q = qdisc_priv(qd);
261 struct net_device *dev = qd->dev;
262 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
263 struct ieee80211_hw *hw = &local->hw;
268 /* check all the h/w queues in numeric/priority order */
269 for (queue = 0; queue < hw->queues; queue++) {
270 /* see if there is room in this hardware queue */
271 if ((test_bit(IEEE80211_LINK_STATE_XOFF,
272 &local->state[queue])) ||
273 (test_bit(IEEE80211_LINK_STATE_PENDING,
274 &local->state[queue])) ||
275 (!test_bit(queue, q->qdisc_pool)))
278 /* there is space - try and get a frame */
279 skb = skb_dequeue(&q->requeued[queue]);
285 qdisc = q->queues[queue];
286 skb = qdisc->dequeue(qdisc);
292 /* returning a NULL here when all the h/w queues are full means we
293 * never need to call netif_stop_queue in the driver */
298 static void wme_qdiscop_reset(struct Qdisc* qd)
300 struct ieee80211_sched_data *q = qdisc_priv(qd);
301 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
302 struct ieee80211_hw *hw = &local->hw;
305 /* QUESTION: should we have some hardware flush functionality here? */
307 for (queue = 0; queue < hw->queues; queue++) {
308 skb_queue_purge(&q->requeued[queue]);
309 qdisc_reset(q->queues[queue]);
315 static void wme_qdiscop_destroy(struct Qdisc* qd)
317 struct ieee80211_sched_data *q = qdisc_priv(qd);
318 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
319 struct ieee80211_hw *hw = &local->hw;
322 tcf_destroy_chain(q->filter_list);
323 q->filter_list = NULL;
325 for (queue=0; queue < hw->queues; queue++) {
326 skb_queue_purge(&q->requeued[queue]);
327 qdisc_destroy(q->queues[queue]);
328 q->queues[queue] = &noop_qdisc;
333 /* called whenever parameters are updated on existing qdisc */
334 static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
336 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
338 /* check our options block is the right size */
339 /* copy any options to our local structure */
340 /* Ignore options block for now - always use static mapping
341 struct tc_ieee80211_qopt *qopt = nla_data(opt);
343 if (opt->nla_len < nla_attr_size(sizeof(*qopt)))
345 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
351 /* called during initial creation of qdisc on device */
352 static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
354 struct ieee80211_sched_data *q = qdisc_priv(qd);
355 struct net_device *dev = qd->dev;
356 struct ieee80211_local *local;
360 /* check that device is a mac80211 device */
361 if (!dev->ieee80211_ptr ||
362 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
365 /* check this device is an ieee80211 master type device */
366 if (dev->type != ARPHRD_IEEE80211)
369 /* check that there is no qdisc currently attached to device
370 * this ensures that we will be the root qdisc. (I can't find a better
371 * way to test this explicitly) */
372 if (dev->qdisc_sleeping != &noop_qdisc)
375 if (qd->flags & TCQ_F_INGRESS)
378 local = wdev_priv(dev->ieee80211_ptr);
379 queues = local->hw.queues;
381 /* if options were passed in, set them */
383 err = wme_qdiscop_tune(qd, opt);
386 /* create child queues */
387 for (i = 0; i < queues; i++) {
388 skb_queue_head_init(&q->requeued[i]);
389 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
392 q->queues[i] = &noop_qdisc;
393 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
397 /* reserve all legacy QoS queues */
398 for (i = 0; i < min(IEEE80211_TX_QUEUE_DATA4, queues); i++)
399 set_bit(i, q->qdisc_pool);
404 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
406 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
407 unsigned char *p = skb->tail;
408 struct tc_ieee80211_qopt opt;
410 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
411 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
415 skb_trim(skb, p - skb->data);*/
420 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
421 struct Qdisc *new, struct Qdisc **old)
423 struct ieee80211_sched_data *q = qdisc_priv(qd);
424 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
425 struct ieee80211_hw *hw = &local->hw;
426 unsigned long queue = arg - 1;
428 if (queue >= hw->queues)
435 *old = q->queues[queue];
436 q->queues[queue] = new;
444 static struct Qdisc *
445 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
447 struct ieee80211_sched_data *q = qdisc_priv(qd);
448 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
449 struct ieee80211_hw *hw = &local->hw;
450 unsigned long queue = arg - 1;
452 if (queue >= hw->queues)
455 return q->queues[queue];
459 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
461 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
462 struct ieee80211_hw *hw = &local->hw;
463 unsigned long queue = TC_H_MIN(classid);
465 if (queue - 1 >= hw->queues)
472 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
475 return wme_classop_get(qd, classid);
479 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
484 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
485 struct nlattr **tca, unsigned long *arg)
487 unsigned long cl = *arg;
488 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
489 struct ieee80211_hw *hw = &local->hw;
491 if (cl - 1 > hw->queues)
494 /* TODO: put code to program hardware queue parameters here,
495 * to allow programming from tc command line */
501 /* we don't support deleting hardware queues
502 * when we add WMM-SA support - TSPECs may be deleted here */
503 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
505 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
506 struct ieee80211_hw *hw = &local->hw;
508 if (cl - 1 > hw->queues)
514 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
515 struct sk_buff *skb, struct tcmsg *tcm)
517 struct ieee80211_sched_data *q = qdisc_priv(qd);
518 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
519 struct ieee80211_hw *hw = &local->hw;
521 if (cl - 1 > hw->queues)
523 tcm->tcm_handle = TC_H_MIN(cl);
524 tcm->tcm_parent = qd->handle;
525 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
530 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
532 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
533 struct ieee80211_hw *hw = &local->hw;
539 for (queue = 0; queue < hw->queues; queue++) {
540 if (arg->count < arg->skip) {
544 /* we should return classids for our internal queues here
545 * as well as the external ones */
546 if (arg->fn(qd, queue+1, arg) < 0) {
555 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
558 struct ieee80211_sched_data *q = qdisc_priv(qd);
563 return &q->filter_list;
567 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
568 * - these are the operations on the classes */
569 static const struct Qdisc_class_ops class_ops =
571 .graft = wme_classop_graft,
572 .leaf = wme_classop_leaf,
574 .get = wme_classop_get,
575 .put = wme_classop_put,
576 .change = wme_classop_change,
577 .delete = wme_classop_delete,
578 .walk = wme_classop_walk,
580 .tcf_chain = wme_classop_find_tcf,
581 .bind_tcf = wme_classop_bind,
582 .unbind_tcf = wme_classop_put,
584 .dump = wme_classop_dump_class,
588 /* queueing discipline operations */
589 static struct Qdisc_ops wme_qdisc_ops __read_mostly =
592 .cl_ops = &class_ops,
594 .priv_size = sizeof(struct ieee80211_sched_data),
596 .enqueue = wme_qdiscop_enqueue,
597 .dequeue = wme_qdiscop_dequeue,
598 .requeue = wme_qdiscop_requeue,
599 .drop = NULL, /* drop not needed since we are always the root qdisc */
601 .init = wme_qdiscop_init,
602 .reset = wme_qdiscop_reset,
603 .destroy = wme_qdiscop_destroy,
604 .change = wme_qdiscop_tune,
606 .dump = wme_qdiscop_dump,
610 void ieee80211_install_qdisc(struct net_device *dev)
614 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
616 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
620 /* same handle as would be allocated by qdisc_alloc_handle() */
621 qdisc->handle = 0x80010000;
623 qdisc_lock_tree(dev);
624 list_add_tail(&qdisc->list, &dev->qdisc_list);
625 dev->qdisc_sleeping = qdisc;
626 qdisc_unlock_tree(dev);
630 int ieee80211_qdisc_installed(struct net_device *dev)
632 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
636 int ieee80211_wme_register(void)
638 return register_qdisc(&wme_qdisc_ops);
642 void ieee80211_wme_unregister(void)
644 unregister_qdisc(&wme_qdisc_ops);
647 int ieee80211_ht_agg_queue_add(struct ieee80211_local *local,
648 struct sta_info *sta, u16 tid)
651 struct ieee80211_sched_data *q =
652 qdisc_priv(local->mdev->qdisc_sleeping);
653 DECLARE_MAC_BUF(mac);
655 /* prepare the filter and save it for the SW queue
656 * matching the recieved HW queue */
658 /* try to get a Qdisc from the pool */
659 for (i = IEEE80211_TX_QUEUE_BEACON; i < local->hw.queues; i++)
660 if (!test_and_set_bit(i, q->qdisc_pool)) {
661 ieee80211_stop_queue(local_to_hw(local), i);
662 sta->tid_to_tx_q[tid] = i;
664 /* IF there are already pending packets
665 * on this tid first we need to drain them
666 * on the previous queue
667 * since HT is strict in order */
668 #ifdef CONFIG_MAC80211_HT_DEBUG
670 printk(KERN_DEBUG "allocated aggregation queue"
671 " %d tid %d addr %s pool=0x%lX",
672 i, tid, print_mac(mac, sta->addr),
674 #endif /* CONFIG_MAC80211_HT_DEBUG */
682 * the caller needs to hold local->mdev->queue_lock
684 void ieee80211_ht_agg_queue_remove(struct ieee80211_local *local,
685 struct sta_info *sta, u16 tid,
688 struct ieee80211_sched_data *q =
689 qdisc_priv(local->mdev->qdisc_sleeping);
690 int agg_queue = sta->tid_to_tx_q[tid];
692 /* return the qdisc to the pool */
693 clear_bit(agg_queue, q->qdisc_pool);
694 sta->tid_to_tx_q[tid] = local->hw.queues;
697 ieee80211_requeue(local, agg_queue);
699 q->queues[agg_queue]->ops->reset(q->queues[agg_queue]);
702 void ieee80211_requeue(struct ieee80211_local *local, int queue)
704 struct Qdisc *root_qd = local->mdev->qdisc_sleeping;
705 struct ieee80211_sched_data *q = qdisc_priv(root_qd);
706 struct Qdisc *qdisc = q->queues[queue];
707 struct sk_buff *skb = NULL;
708 u32 len = qdisc->q.qlen;
710 if (!qdisc || !qdisc->dequeue)
713 printk(KERN_DEBUG "requeue: qlen = %d\n", qdisc->q.qlen);
714 for (len = qdisc->q.qlen; len > 0; len--) {
715 skb = qdisc->dequeue(qdisc);
717 /* packet will be classified again and */
718 /* skb->packet_data->queue will be overridden if needed */
720 wme_qdiscop_enqueue(skb, root_qd);