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];
32 /* given a data frame determine the 802.1p/1d tag to use */
33 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
39 struct ieee80211_sched_data *q = qdisc_priv(qd);
40 struct tcf_result res = { -1, 0 };
42 /* if there is a user set filter list, call out to that */
44 tc_classify(skb, q->filter_list, &res);
49 /* skb->priority values from 256->263 are magic values to
50 * directly indicate a specific 802.1d priority.
51 * This is used to allow 802.1d priority to be passed directly in
52 * from VLAN tags, etc. */
53 if (skb->priority >= 256 && skb->priority <= 263)
54 return skb->priority - 256;
56 /* check there is a valid IP header present */
57 offset = ieee80211_get_hdrlen_from_skb(skb) + 8 /* LLC + proto */;
58 if (skb->protocol != __constant_htons(ETH_P_IP) ||
59 skb->len < offset + sizeof(*ip))
62 ip = (struct iphdr *) (skb->data + offset);
64 dscp = ip->tos & 0xfc;
71 static inline int wme_downgrade_ac(struct sk_buff *skb)
73 switch (skb->priority) {
76 skb->priority = 5; /* VO -> VI */
80 skb->priority = 3; /* VI -> BE */
84 skb->priority = 2; /* BE -> BK */
92 /* positive return value indicates which queue to use
93 * negative return value indicates to drop the frame */
94 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
96 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
97 struct ieee80211_tx_packet_data *pkt_data =
98 (struct ieee80211_tx_packet_data *) skb->cb;
99 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
100 unsigned short fc = le16_to_cpu(hdr->frame_control);
102 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
104 /* see if frame is data or non data frame */
105 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
106 /* management frames go on AC_VO queue, but are sent
107 * without QoS control fields */
108 return IEEE80211_TX_QUEUE_DATA0;
111 if (unlikely(pkt_data->flags & IEEE80211_TXPD_MGMT_IFACE)) {
112 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
113 * and they will include QoS control fields if
114 * the target STA is using WME. */
116 return ieee802_1d_to_ac[skb->priority];
119 /* is this a QoS frame? */
120 qos = fc & IEEE80211_STYPE_QOS_DATA;
123 skb->priority = 0; /* required for correct WPA/11i MIC */
124 return ieee802_1d_to_ac[skb->priority];
127 /* use the data classifier to determine what 802.1d tag the
129 skb->priority = classify_1d(skb, qd);
131 /* incase we are a client verify acm is not set for this ac */
132 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
133 if (wme_downgrade_ac(skb)) {
134 /* No AC with lower priority has acm=0,
140 /* look up which queue to use for frames with this 1d tag */
141 return ieee802_1d_to_ac[skb->priority];
145 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
147 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
148 struct ieee80211_sched_data *q = qdisc_priv(qd);
149 struct ieee80211_tx_packet_data *pkt_data =
150 (struct ieee80211_tx_packet_data *) skb->cb;
151 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
152 unsigned short fc = le16_to_cpu(hdr->frame_control);
156 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
157 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
162 queue = classify80211(skb, qd);
164 /* now we know the 1d priority, fill in the QoS header if there is one
166 if (WLAN_FC_IS_QOS_DATA(fc)) {
167 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
168 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
169 if (local->wifi_wme_noack_test)
170 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
171 QOS_CONTROL_ACK_POLICY_SHIFT;
172 /* qos header is 2 bytes, second reserved */
178 if (unlikely(queue >= local->hw.queues)) {
180 if (net_ratelimit()) {
181 printk(KERN_DEBUG "%s - queue=%d (hw does not "
183 __func__, queue, local->hw.queues - 1);
186 queue = local->hw.queues - 1;
189 if (unlikely(queue < 0)) {
193 pkt_data->queue = (unsigned int) queue;
194 qdisc = q->queues[queue];
195 err = qdisc->enqueue(skb, qdisc);
196 if (err == NET_XMIT_SUCCESS) {
198 qd->bstats.bytes += skb->len;
199 qd->bstats.packets++;
200 return NET_XMIT_SUCCESS;
208 /* TODO: clean up the cases where master_hard_start_xmit
209 * returns non 0 - it shouldn't ever do that. Once done we
210 * can remove this function */
211 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
213 struct ieee80211_sched_data *q = qdisc_priv(qd);
214 struct ieee80211_tx_packet_data *pkt_data =
215 (struct ieee80211_tx_packet_data *) skb->cb;
219 /* we recorded which queue to use earlier! */
220 qdisc = q->queues[pkt_data->queue];
222 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
231 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
233 struct ieee80211_sched_data *q = qdisc_priv(qd);
234 struct net_device *dev = qd->dev;
235 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
236 struct ieee80211_hw *hw = &local->hw;
241 /* check all the h/w queues in numeric/priority order */
242 for (queue = 0; queue < hw->queues; queue++) {
243 /* see if there is room in this hardware queue */
244 if (test_bit(IEEE80211_LINK_STATE_XOFF,
245 &local->state[queue]) ||
246 test_bit(IEEE80211_LINK_STATE_PENDING,
247 &local->state[queue]))
250 /* there is space - try and get a frame */
251 skb = skb_dequeue(&q->requeued[queue]);
257 qdisc = q->queues[queue];
258 skb = qdisc->dequeue(qdisc);
264 /* returning a NULL here when all the h/w queues are full means we
265 * never need to call netif_stop_queue in the driver */
270 static void wme_qdiscop_reset(struct Qdisc* qd)
272 struct ieee80211_sched_data *q = qdisc_priv(qd);
273 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
274 struct ieee80211_hw *hw = &local->hw;
277 /* QUESTION: should we have some hardware flush functionality here? */
279 for (queue = 0; queue < hw->queues; queue++) {
280 skb_queue_purge(&q->requeued[queue]);
281 qdisc_reset(q->queues[queue]);
287 static void wme_qdiscop_destroy(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 tcf_destroy_chain(q->filter_list);
295 q->filter_list = NULL;
297 for (queue=0; queue < hw->queues; queue++) {
298 skb_queue_purge(&q->requeued[queue]);
299 qdisc_destroy(q->queues[queue]);
300 q->queues[queue] = &noop_qdisc;
305 /* called whenever parameters are updated on existing qdisc */
306 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
308 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
310 /* check our options block is the right size */
311 /* copy any options to our local structure */
312 /* Ignore options block for now - always use static mapping
313 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
315 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
317 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
323 /* called during initial creation of qdisc on device */
324 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
326 struct ieee80211_sched_data *q = qdisc_priv(qd);
327 struct net_device *dev = qd->dev;
328 struct ieee80211_local *local;
332 /* check that device is a mac80211 device */
333 if (!dev->ieee80211_ptr ||
334 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
337 /* check this device is an ieee80211 master type device */
338 if (dev->type != ARPHRD_IEEE80211)
341 /* check that there is no qdisc currently attached to device
342 * this ensures that we will be the root qdisc. (I can't find a better
343 * way to test this explicitly) */
344 if (dev->qdisc_sleeping != &noop_qdisc)
347 if (qd->flags & TCQ_F_INGRESS)
350 local = wdev_priv(dev->ieee80211_ptr);
351 queues = local->hw.queues;
353 /* if options were passed in, set them */
355 err = wme_qdiscop_tune(qd, opt);
358 /* create child queues */
359 for (i = 0; i < queues; i++) {
360 skb_queue_head_init(&q->requeued[i]);
361 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
364 q->queues[i] = &noop_qdisc;
365 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
372 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
374 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
375 unsigned char *p = skb->tail;
376 struct tc_ieee80211_qopt opt;
378 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
379 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
383 skb_trim(skb, p - skb->data);*/
388 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
389 struct Qdisc *new, struct Qdisc **old)
391 struct ieee80211_sched_data *q = qdisc_priv(qd);
392 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
393 struct ieee80211_hw *hw = &local->hw;
394 unsigned long queue = arg - 1;
396 if (queue >= hw->queues)
403 *old = q->queues[queue];
404 q->queues[queue] = new;
412 static struct Qdisc *
413 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
415 struct ieee80211_sched_data *q = qdisc_priv(qd);
416 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
417 struct ieee80211_hw *hw = &local->hw;
418 unsigned long queue = arg - 1;
420 if (queue >= hw->queues)
423 return q->queues[queue];
427 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
429 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
430 struct ieee80211_hw *hw = &local->hw;
431 unsigned long queue = TC_H_MIN(classid);
433 if (queue - 1 >= hw->queues)
440 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
443 return wme_classop_get(qd, classid);
447 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
452 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
453 struct rtattr **tca, unsigned long *arg)
455 unsigned long cl = *arg;
456 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
457 struct ieee80211_hw *hw = &local->hw;
459 if (cl - 1 > hw->queues)
462 /* TODO: put code to program hardware queue parameters here,
463 * to allow programming from tc command line */
469 /* we don't support deleting hardware queues
470 * when we add WMM-SA support - TSPECs may be deleted here */
471 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
473 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
474 struct ieee80211_hw *hw = &local->hw;
476 if (cl - 1 > hw->queues)
482 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
483 struct sk_buff *skb, struct tcmsg *tcm)
485 struct ieee80211_sched_data *q = qdisc_priv(qd);
486 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
487 struct ieee80211_hw *hw = &local->hw;
489 if (cl - 1 > hw->queues)
491 tcm->tcm_handle = TC_H_MIN(cl);
492 tcm->tcm_parent = qd->handle;
493 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
498 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
500 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
501 struct ieee80211_hw *hw = &local->hw;
507 for (queue = 0; queue < hw->queues; queue++) {
508 if (arg->count < arg->skip) {
512 /* we should return classids for our internal queues here
513 * as well as the external ones */
514 if (arg->fn(qd, queue+1, arg) < 0) {
523 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
526 struct ieee80211_sched_data *q = qdisc_priv(qd);
531 return &q->filter_list;
535 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
536 * - these are the operations on the classes */
537 static struct Qdisc_class_ops class_ops =
539 .graft = wme_classop_graft,
540 .leaf = wme_classop_leaf,
542 .get = wme_classop_get,
543 .put = wme_classop_put,
544 .change = wme_classop_change,
545 .delete = wme_classop_delete,
546 .walk = wme_classop_walk,
548 .tcf_chain = wme_classop_find_tcf,
549 .bind_tcf = wme_classop_bind,
550 .unbind_tcf = wme_classop_put,
552 .dump = wme_classop_dump_class,
556 /* queueing discipline operations */
557 static struct Qdisc_ops wme_qdisc_ops =
560 .cl_ops = &class_ops,
562 .priv_size = sizeof(struct ieee80211_sched_data),
564 .enqueue = wme_qdiscop_enqueue,
565 .dequeue = wme_qdiscop_dequeue,
566 .requeue = wme_qdiscop_requeue,
567 .drop = NULL, /* drop not needed since we are always the root qdisc */
569 .init = wme_qdiscop_init,
570 .reset = wme_qdiscop_reset,
571 .destroy = wme_qdiscop_destroy,
572 .change = wme_qdiscop_tune,
574 .dump = wme_qdiscop_dump,
578 void ieee80211_install_qdisc(struct net_device *dev)
582 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
584 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
588 /* same handle as would be allocated by qdisc_alloc_handle() */
589 qdisc->handle = 0x80010000;
591 qdisc_lock_tree(dev);
592 list_add_tail(&qdisc->list, &dev->qdisc_list);
593 dev->qdisc_sleeping = qdisc;
594 qdisc_unlock_tree(dev);
598 int ieee80211_qdisc_installed(struct net_device *dev)
600 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
604 int ieee80211_wme_register(void)
606 return register_qdisc(&wme_qdisc_ops);
610 void ieee80211_wme_unregister(void)
612 unregister_qdisc(&wme_qdisc_ops);