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 8
24 struct ieee80211_sched_data
26 struct tcf_proto *filter_list;
27 struct Qdisc *queues[TC_80211_MAX_QUEUES];
28 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
31 static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
33 /* given a data frame determine the 802.1p/1d tag to use */
34 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
40 struct ieee80211_sched_data *q = qdisc_priv(qd);
41 struct tcf_result res = { -1, 0 };
43 /* if there is a user set filter list, call out to that */
45 tc_classify(skb, q->filter_list, &res);
50 /* skb->priority values from 256->263 are magic values to
51 * directly indicate a specific 802.1d priority.
52 * This is used to allow 802.1d priority to be passed directly in
53 * from VLAN tags, etc. */
54 if (skb->priority >= 256 && skb->priority <= 263)
55 return skb->priority - 256;
57 /* check there is a valid IP header present */
58 offset = ieee80211_get_hdrlen_from_skb(skb);
59 if (skb->len < offset + sizeof(llc_ip_hdr) + sizeof(*ip) ||
60 memcmp(skb->data + offset, llc_ip_hdr, sizeof(llc_ip_hdr)))
63 ip = (struct iphdr *) (skb->data + offset + sizeof(llc_ip_hdr));
65 dscp = ip->tos & 0xfc;
72 static inline int wme_downgrade_ac(struct sk_buff *skb)
74 switch (skb->priority) {
77 skb->priority = 5; /* VO -> VI */
81 skb->priority = 3; /* VI -> BE */
85 skb->priority = 2; /* BE -> BK */
93 /* positive return value indicates which queue to use
94 * negative return value indicates to drop the frame */
95 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
97 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
98 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
99 unsigned short fc = le16_to_cpu(hdr->frame_control);
101 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
103 /* see if frame is data or non data frame */
104 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
105 /* management frames go on AC_VO queue, but are sent
106 * without QoS control fields */
107 return IEEE80211_TX_QUEUE_DATA0;
110 if (0 /* injected */) {
111 /* use AC from radiotap */
114 /* is this a QoS frame? */
115 qos = fc & IEEE80211_STYPE_QOS_DATA;
118 skb->priority = 0; /* required for correct WPA/11i MIC */
119 return ieee802_1d_to_ac[skb->priority];
122 /* use the data classifier to determine what 802.1d tag the
124 skb->priority = classify_1d(skb, qd);
126 /* in case we are a client verify acm is not set for this ac */
127 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
128 if (wme_downgrade_ac(skb)) {
129 /* No AC with lower priority has acm=0, drop packet. */
134 /* look up which queue to use for frames with this 1d tag */
135 return ieee802_1d_to_ac[skb->priority];
139 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
141 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
142 struct ieee80211_sched_data *q = qdisc_priv(qd);
143 struct ieee80211_tx_packet_data *pkt_data =
144 (struct ieee80211_tx_packet_data *) skb->cb;
145 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
146 unsigned short fc = le16_to_cpu(hdr->frame_control);
150 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
151 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
156 queue = classify80211(skb, qd);
158 /* now we know the 1d priority, fill in the QoS header if there is one
160 if (WLAN_FC_IS_QOS_DATA(fc)) {
161 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
162 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
163 if (local->wifi_wme_noack_test)
164 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
165 QOS_CONTROL_ACK_POLICY_SHIFT;
166 /* qos header is 2 bytes, second reserved */
172 if (unlikely(queue >= local->hw.queues)) {
174 if (net_ratelimit()) {
175 printk(KERN_DEBUG "%s - queue=%d (hw does not "
177 __func__, queue, local->hw.queues - 1);
180 queue = local->hw.queues - 1;
183 if (unlikely(queue < 0)) {
187 pkt_data->queue = (unsigned int) queue;
188 qdisc = q->queues[queue];
189 err = qdisc->enqueue(skb, qdisc);
190 if (err == NET_XMIT_SUCCESS) {
192 qd->bstats.bytes += skb->len;
193 qd->bstats.packets++;
194 return NET_XMIT_SUCCESS;
202 /* TODO: clean up the cases where master_hard_start_xmit
203 * returns non 0 - it shouldn't ever do that. Once done we
204 * can remove this function */
205 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
207 struct ieee80211_sched_data *q = qdisc_priv(qd);
208 struct ieee80211_tx_packet_data *pkt_data =
209 (struct ieee80211_tx_packet_data *) skb->cb;
213 /* we recorded which queue to use earlier! */
214 qdisc = q->queues[pkt_data->queue];
216 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
225 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
227 struct ieee80211_sched_data *q = qdisc_priv(qd);
228 struct net_device *dev = qd->dev;
229 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
230 struct ieee80211_hw *hw = &local->hw;
235 /* check all the h/w queues in numeric/priority order */
236 for (queue = 0; queue < hw->queues; queue++) {
237 /* see if there is room in this hardware queue */
238 if (test_bit(IEEE80211_LINK_STATE_XOFF,
239 &local->state[queue]) ||
240 test_bit(IEEE80211_LINK_STATE_PENDING,
241 &local->state[queue]))
244 /* there is space - try and get a frame */
245 skb = skb_dequeue(&q->requeued[queue]);
251 qdisc = q->queues[queue];
252 skb = qdisc->dequeue(qdisc);
258 /* returning a NULL here when all the h/w queues are full means we
259 * never need to call netif_stop_queue in the driver */
264 static void wme_qdiscop_reset(struct Qdisc* qd)
266 struct ieee80211_sched_data *q = qdisc_priv(qd);
267 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
268 struct ieee80211_hw *hw = &local->hw;
271 /* QUESTION: should we have some hardware flush functionality here? */
273 for (queue = 0; queue < hw->queues; queue++) {
274 skb_queue_purge(&q->requeued[queue]);
275 qdisc_reset(q->queues[queue]);
281 static void wme_qdiscop_destroy(struct Qdisc* qd)
283 struct ieee80211_sched_data *q = qdisc_priv(qd);
284 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
285 struct ieee80211_hw *hw = &local->hw;
288 tcf_destroy_chain(q->filter_list);
289 q->filter_list = NULL;
291 for (queue=0; queue < hw->queues; queue++) {
292 skb_queue_purge(&q->requeued[queue]);
293 qdisc_destroy(q->queues[queue]);
294 q->queues[queue] = &noop_qdisc;
299 /* called whenever parameters are updated on existing qdisc */
300 static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
302 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
304 /* check our options block is the right size */
305 /* copy any options to our local structure */
306 /* Ignore options block for now - always use static mapping
307 struct tc_ieee80211_qopt *qopt = nla_data(opt);
309 if (opt->nla_len < nla_attr_size(sizeof(*qopt)))
311 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
317 /* called during initial creation of qdisc on device */
318 static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
320 struct ieee80211_sched_data *q = qdisc_priv(qd);
321 struct net_device *dev = qd->dev;
322 struct ieee80211_local *local;
326 /* check that device is a mac80211 device */
327 if (!dev->ieee80211_ptr ||
328 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
331 /* check this device is an ieee80211 master type device */
332 if (dev->type != ARPHRD_IEEE80211)
335 /* check that there is no qdisc currently attached to device
336 * this ensures that we will be the root qdisc. (I can't find a better
337 * way to test this explicitly) */
338 if (dev->qdisc_sleeping != &noop_qdisc)
341 if (qd->flags & TCQ_F_INGRESS)
344 local = wdev_priv(dev->ieee80211_ptr);
345 queues = local->hw.queues;
347 /* if options were passed in, set them */
349 err = wme_qdiscop_tune(qd, opt);
352 /* create child queues */
353 for (i = 0; i < queues; i++) {
354 skb_queue_head_init(&q->requeued[i]);
355 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
358 q->queues[i] = &noop_qdisc;
359 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
366 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
368 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
369 unsigned char *p = skb->tail;
370 struct tc_ieee80211_qopt opt;
372 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
373 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
377 skb_trim(skb, p - skb->data);*/
382 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
383 struct Qdisc *new, struct Qdisc **old)
385 struct ieee80211_sched_data *q = qdisc_priv(qd);
386 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
387 struct ieee80211_hw *hw = &local->hw;
388 unsigned long queue = arg - 1;
390 if (queue >= hw->queues)
397 *old = q->queues[queue];
398 q->queues[queue] = new;
406 static struct Qdisc *
407 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
409 struct ieee80211_sched_data *q = qdisc_priv(qd);
410 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
411 struct ieee80211_hw *hw = &local->hw;
412 unsigned long queue = arg - 1;
414 if (queue >= hw->queues)
417 return q->queues[queue];
421 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
423 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
424 struct ieee80211_hw *hw = &local->hw;
425 unsigned long queue = TC_H_MIN(classid);
427 if (queue - 1 >= hw->queues)
434 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
437 return wme_classop_get(qd, classid);
441 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
446 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
447 struct nlattr **tca, unsigned long *arg)
449 unsigned long cl = *arg;
450 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
451 struct ieee80211_hw *hw = &local->hw;
453 if (cl - 1 > hw->queues)
456 /* TODO: put code to program hardware queue parameters here,
457 * to allow programming from tc command line */
463 /* we don't support deleting hardware queues
464 * when we add WMM-SA support - TSPECs may be deleted here */
465 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
467 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
468 struct ieee80211_hw *hw = &local->hw;
470 if (cl - 1 > hw->queues)
476 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
477 struct sk_buff *skb, struct tcmsg *tcm)
479 struct ieee80211_sched_data *q = qdisc_priv(qd);
480 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
481 struct ieee80211_hw *hw = &local->hw;
483 if (cl - 1 > hw->queues)
485 tcm->tcm_handle = TC_H_MIN(cl);
486 tcm->tcm_parent = qd->handle;
487 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
492 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
494 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
495 struct ieee80211_hw *hw = &local->hw;
501 for (queue = 0; queue < hw->queues; queue++) {
502 if (arg->count < arg->skip) {
506 /* we should return classids for our internal queues here
507 * as well as the external ones */
508 if (arg->fn(qd, queue+1, arg) < 0) {
517 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
520 struct ieee80211_sched_data *q = qdisc_priv(qd);
525 return &q->filter_list;
529 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
530 * - these are the operations on the classes */
531 static const struct Qdisc_class_ops class_ops =
533 .graft = wme_classop_graft,
534 .leaf = wme_classop_leaf,
536 .get = wme_classop_get,
537 .put = wme_classop_put,
538 .change = wme_classop_change,
539 .delete = wme_classop_delete,
540 .walk = wme_classop_walk,
542 .tcf_chain = wme_classop_find_tcf,
543 .bind_tcf = wme_classop_bind,
544 .unbind_tcf = wme_classop_put,
546 .dump = wme_classop_dump_class,
550 /* queueing discipline operations */
551 static struct Qdisc_ops wme_qdisc_ops __read_mostly =
554 .cl_ops = &class_ops,
556 .priv_size = sizeof(struct ieee80211_sched_data),
558 .enqueue = wme_qdiscop_enqueue,
559 .dequeue = wme_qdiscop_dequeue,
560 .requeue = wme_qdiscop_requeue,
561 .drop = NULL, /* drop not needed since we are always the root qdisc */
563 .init = wme_qdiscop_init,
564 .reset = wme_qdiscop_reset,
565 .destroy = wme_qdiscop_destroy,
566 .change = wme_qdiscop_tune,
568 .dump = wme_qdiscop_dump,
572 void ieee80211_install_qdisc(struct net_device *dev)
576 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
578 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
582 /* same handle as would be allocated by qdisc_alloc_handle() */
583 qdisc->handle = 0x80010000;
585 qdisc_lock_tree(dev);
586 list_add_tail(&qdisc->list, &dev->qdisc_list);
587 dev->qdisc_sleeping = qdisc;
588 qdisc_unlock_tree(dev);
592 int ieee80211_qdisc_installed(struct net_device *dev)
594 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
598 int ieee80211_wme_register(void)
600 return register_qdisc(&wme_qdisc_ops);
604 void ieee80211_wme_unregister(void)
606 unregister_qdisc(&wme_qdisc_ops);