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;
108 unsigned short fc = le16_to_cpu(hdr->frame_control);
111 /* see if frame is data or non data frame */
112 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
113 /* management frames go on AC_VO queue, but are sent
114 * without QoS control fields */
118 if (0 /* injected */) {
119 /* use AC from radiotap */
122 /* is this a QoS frame? */
123 qos = fc & IEEE80211_STYPE_QOS_DATA;
126 skb->priority = 0; /* required for correct WPA/11i MIC */
127 return ieee802_1d_to_ac[skb->priority];
130 /* use the data classifier to determine what 802.1d tag the
132 skb->priority = classify_1d(skb, qd);
134 /* in case we are a client verify acm is not set for this ac */
135 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
136 if (wme_downgrade_ac(skb)) {
137 /* No AC with lower priority has acm=0, drop packet. */
142 /* look up which queue to use for frames with this 1d tag */
143 return ieee802_1d_to_ac[skb->priority];
147 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
149 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
150 struct ieee80211_hw *hw = &local->hw;
151 struct ieee80211_sched_data *q = qdisc_priv(qd);
152 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
153 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
154 unsigned short fc = le16_to_cpu(hdr->frame_control);
156 struct sta_info *sta;
160 if (info->flags & IEEE80211_TX_CTL_REQUEUE) {
161 queue = skb_get_queue_mapping(skb);
163 sta = sta_info_get(local, hdr->addr1);
164 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
166 int ampdu_queue = sta->tid_to_tx_q[tid];
167 if ((ampdu_queue < QD_NUM(hw)) &&
168 test_bit(ampdu_queue, q->qdisc_pool)) {
170 info->flags |= IEEE80211_TX_CTL_AMPDU;
172 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
176 skb_queue_tail(&q->requeued[queue], skb);
181 queue = classify80211(skb, qd);
183 if (unlikely(queue >= local->hw.queues))
184 queue = local->hw.queues - 1;
186 /* now we know the 1d priority, fill in the QoS header if there is one
188 if (WLAN_FC_IS_QOS_DATA(fc)) {
189 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
191 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
192 if (local->wifi_wme_noack_test)
193 ack_policy |= QOS_CONTROL_ACK_POLICY_NOACK <<
194 QOS_CONTROL_ACK_POLICY_SHIFT;
195 /* qos header is 2 bytes, second reserved */
196 *p = ack_policy | tid;
202 sta = sta_info_get(local, hdr->addr1);
204 int ampdu_queue = sta->tid_to_tx_q[tid];
205 if ((ampdu_queue < QD_NUM(hw)) &&
206 test_bit(ampdu_queue, q->qdisc_pool)) {
208 info->flags |= IEEE80211_TX_CTL_AMPDU;
210 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
217 if (unlikely(queue < 0)) {
221 tid = skb->priority & QOS_CONTROL_TAG1D_MASK;
222 skb_set_queue_mapping(skb, queue);
223 qdisc = q->queues[queue];
224 err = qdisc->enqueue(skb, qdisc);
225 if (err == NET_XMIT_SUCCESS) {
227 qd->bstats.bytes += skb->len;
228 qd->bstats.packets++;
229 return NET_XMIT_SUCCESS;
237 /* TODO: clean up the cases where master_hard_start_xmit
238 * returns non 0 - it shouldn't ever do that. Once done we
239 * can remove this function */
240 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
242 struct ieee80211_sched_data *q = qdisc_priv(qd);
246 /* we recorded which queue to use earlier! */
247 qdisc = q->queues[skb_get_queue_mapping(skb)];
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 < QD_NUM(hw); queue++) {
270 /* see if there is room in this hardware queue */
271 if (__netif_subqueue_stopped(local->mdev, queue) ||
272 !test_bit(queue, q->qdisc_pool))
275 /* there is space - try and get a frame */
276 skb = skb_dequeue(&q->requeued[queue]);
282 qdisc = q->queues[queue];
283 skb = qdisc->dequeue(qdisc);
289 /* returning a NULL here when all the h/w queues are full means we
290 * never need to call netif_stop_queue in the driver */
295 static void wme_qdiscop_reset(struct Qdisc* qd)
297 struct ieee80211_sched_data *q = qdisc_priv(qd);
298 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
299 struct ieee80211_hw *hw = &local->hw;
302 /* QUESTION: should we have some hardware flush functionality here? */
304 for (queue = 0; queue < QD_NUM(hw); queue++) {
305 skb_queue_purge(&q->requeued[queue]);
306 qdisc_reset(q->queues[queue]);
312 static void wme_qdiscop_destroy(struct Qdisc* qd)
314 struct ieee80211_sched_data *q = qdisc_priv(qd);
315 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
316 struct ieee80211_hw *hw = &local->hw;
319 tcf_destroy_chain(q->filter_list);
320 q->filter_list = NULL;
322 for (queue = 0; queue < QD_NUM(hw); queue++) {
323 skb_queue_purge(&q->requeued[queue]);
324 qdisc_destroy(q->queues[queue]);
325 q->queues[queue] = &noop_qdisc;
330 /* called whenever parameters are updated on existing qdisc */
331 static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
337 /* called during initial creation of qdisc on device */
338 static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
340 struct ieee80211_sched_data *q = qdisc_priv(qd);
341 struct net_device *dev = qd->dev;
342 struct ieee80211_local *local;
343 struct ieee80211_hw *hw;
346 /* check that device is a mac80211 device */
347 if (!dev->ieee80211_ptr ||
348 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
351 local = wdev_priv(dev->ieee80211_ptr);
354 /* only allow on master dev */
355 if (dev != local->mdev)
358 /* ensure that we are root qdisc */
359 if (qd->parent != TC_H_ROOT)
362 if (qd->flags & TCQ_F_INGRESS)
365 /* if options were passed in, set them */
367 err = wme_qdiscop_tune(qd, opt);
369 /* create child queues */
370 for (i = 0; i < QD_NUM(hw); i++) {
371 skb_queue_head_init(&q->requeued[i]);
372 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
375 q->queues[i] = &noop_qdisc;
376 printk(KERN_ERR "%s child qdisc %i creation failed\n",
381 /* non-aggregation queues: reserve/mark as used */
382 for (i = 0; i < local->hw.queues; i++)
383 set_bit(i, q->qdisc_pool);
388 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
394 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
395 struct Qdisc *new, struct Qdisc **old)
397 struct ieee80211_sched_data *q = qdisc_priv(qd);
398 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
399 struct ieee80211_hw *hw = &local->hw;
400 unsigned long queue = arg - 1;
402 if (queue >= QD_NUM(hw))
409 *old = q->queues[queue];
410 q->queues[queue] = new;
418 static struct Qdisc *
419 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
421 struct ieee80211_sched_data *q = qdisc_priv(qd);
422 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
423 struct ieee80211_hw *hw = &local->hw;
424 unsigned long queue = arg - 1;
426 if (queue >= QD_NUM(hw))
429 return q->queues[queue];
433 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
435 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
436 struct ieee80211_hw *hw = &local->hw;
437 unsigned long queue = TC_H_MIN(classid);
439 if (queue - 1 >= QD_NUM(hw))
446 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
449 return wme_classop_get(qd, classid);
453 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
458 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
459 struct nlattr **tca, unsigned long *arg)
461 unsigned long cl = *arg;
462 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
463 struct ieee80211_hw *hw = &local->hw;
465 if (cl - 1 > QD_NUM(hw))
468 /* TODO: put code to program hardware queue parameters here,
469 * to allow programming from tc command line */
475 /* we don't support deleting hardware queues
476 * when we add WMM-SA support - TSPECs may be deleted here */
477 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
479 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
480 struct ieee80211_hw *hw = &local->hw;
482 if (cl - 1 > QD_NUM(hw))
488 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
489 struct sk_buff *skb, struct tcmsg *tcm)
491 struct ieee80211_sched_data *q = qdisc_priv(qd);
492 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
493 struct ieee80211_hw *hw = &local->hw;
495 if (cl - 1 > QD_NUM(hw))
497 tcm->tcm_handle = TC_H_MIN(cl);
498 tcm->tcm_parent = qd->handle;
499 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
504 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
506 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
507 struct ieee80211_hw *hw = &local->hw;
513 for (queue = 0; queue < QD_NUM(hw); queue++) {
514 if (arg->count < arg->skip) {
518 /* we should return classids for our internal queues here
519 * as well as the external ones */
520 if (arg->fn(qd, queue+1, arg) < 0) {
529 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
532 struct ieee80211_sched_data *q = qdisc_priv(qd);
537 return &q->filter_list;
541 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
542 * - these are the operations on the classes */
543 static const struct Qdisc_class_ops class_ops =
545 .graft = wme_classop_graft,
546 .leaf = wme_classop_leaf,
548 .get = wme_classop_get,
549 .put = wme_classop_put,
550 .change = wme_classop_change,
551 .delete = wme_classop_delete,
552 .walk = wme_classop_walk,
554 .tcf_chain = wme_classop_find_tcf,
555 .bind_tcf = wme_classop_bind,
556 .unbind_tcf = wme_classop_put,
558 .dump = wme_classop_dump_class,
562 /* queueing discipline operations */
563 static struct Qdisc_ops wme_qdisc_ops __read_mostly =
566 .cl_ops = &class_ops,
568 .priv_size = sizeof(struct ieee80211_sched_data),
570 .enqueue = wme_qdiscop_enqueue,
571 .dequeue = wme_qdiscop_dequeue,
572 .requeue = wme_qdiscop_requeue,
573 .drop = NULL, /* drop not needed since we are always the root qdisc */
575 .init = wme_qdiscop_init,
576 .reset = wme_qdiscop_reset,
577 .destroy = wme_qdiscop_destroy,
578 .change = wme_qdiscop_tune,
580 .dump = wme_qdiscop_dump,
584 void ieee80211_install_qdisc(struct net_device *dev)
588 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
590 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
594 /* same handle as would be allocated by qdisc_alloc_handle() */
595 qdisc->handle = 0x80010000;
597 qdisc_lock_tree(dev);
598 list_add_tail(&qdisc->list, &dev->qdisc_list);
599 dev->qdisc_sleeping = qdisc;
600 qdisc_unlock_tree(dev);
604 int ieee80211_qdisc_installed(struct net_device *dev)
606 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
610 int ieee80211_wme_register(void)
612 return register_qdisc(&wme_qdisc_ops);
616 void ieee80211_wme_unregister(void)
618 unregister_qdisc(&wme_qdisc_ops);
621 int ieee80211_ht_agg_queue_add(struct ieee80211_local *local,
622 struct sta_info *sta, u16 tid)
625 struct ieee80211_sched_data *q =
626 qdisc_priv(local->mdev->qdisc_sleeping);
627 DECLARE_MAC_BUF(mac);
629 /* prepare the filter and save it for the SW queue
630 * matching the received HW queue */
632 if (!local->hw.ampdu_queues)
635 /* try to get a Qdisc from the pool */
636 for (i = local->hw.queues; i < QD_NUM(&local->hw); i++)
637 if (!test_and_set_bit(i, q->qdisc_pool)) {
638 ieee80211_stop_queue(local_to_hw(local), i);
639 sta->tid_to_tx_q[tid] = i;
641 /* IF there are already pending packets
642 * on this tid first we need to drain them
643 * on the previous queue
644 * since HT is strict in order */
645 #ifdef CONFIG_MAC80211_HT_DEBUG
647 printk(KERN_DEBUG "allocated aggregation queue"
648 " %d tid %d addr %s pool=0x%lX",
649 i, tid, print_mac(mac, sta->addr),
651 #endif /* CONFIG_MAC80211_HT_DEBUG */
659 * the caller needs to hold local->mdev->queue_lock
661 void ieee80211_ht_agg_queue_remove(struct ieee80211_local *local,
662 struct sta_info *sta, u16 tid,
665 struct ieee80211_hw *hw = &local->hw;
666 struct ieee80211_sched_data *q =
667 qdisc_priv(local->mdev->qdisc_sleeping);
668 int agg_queue = sta->tid_to_tx_q[tid];
670 /* return the qdisc to the pool */
671 clear_bit(agg_queue, q->qdisc_pool);
672 sta->tid_to_tx_q[tid] = QD_NUM(hw);
675 ieee80211_requeue(local, agg_queue);
677 q->queues[agg_queue]->ops->reset(q->queues[agg_queue]);
680 void ieee80211_requeue(struct ieee80211_local *local, int queue)
682 struct Qdisc *root_qd = local->mdev->qdisc_sleeping;
683 struct ieee80211_sched_data *q = qdisc_priv(root_qd);
684 struct Qdisc *qdisc = q->queues[queue];
685 struct sk_buff *skb = NULL;
688 if (!qdisc || !qdisc->dequeue)
691 printk(KERN_DEBUG "requeue: qlen = %d\n", qdisc->q.qlen);
692 for (len = qdisc->q.qlen; len > 0; len--) {
693 skb = qdisc->dequeue(qdisc);
695 /* packet will be classified again and */
696 /* skb->packet_data->queue will be overridden if needed */
698 wme_qdiscop_enqueue(skb, root_qd);