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_hdr *hdr = (struct ieee80211_hdr *) skb->data;
98 unsigned short fc = le16_to_cpu(hdr->frame_control);
100 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
102 /* see if frame is data or non data frame */
103 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
104 /* management frames go on AC_VO queue, but are sent
105 * without QoS control fields */
106 return IEEE80211_TX_QUEUE_DATA0;
109 if (0 /* injected */) {
110 /* use AC from radiotap */
113 /* is this a QoS frame? */
114 qos = fc & IEEE80211_STYPE_QOS_DATA;
117 skb->priority = 0; /* required for correct WPA/11i MIC */
118 return ieee802_1d_to_ac[skb->priority];
121 /* use the data classifier to determine what 802.1d tag the
123 skb->priority = classify_1d(skb, qd);
125 /* in case we are a client verify acm is not set for this ac */
126 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
127 if (wme_downgrade_ac(skb)) {
128 /* No AC with lower priority has acm=0, drop packet. */
133 /* look up which queue to use for frames with this 1d tag */
134 return ieee802_1d_to_ac[skb->priority];
138 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
140 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
141 struct ieee80211_sched_data *q = qdisc_priv(qd);
142 struct ieee80211_tx_packet_data *pkt_data =
143 (struct ieee80211_tx_packet_data *) skb->cb;
144 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
145 unsigned short fc = le16_to_cpu(hdr->frame_control);
149 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
150 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
155 queue = classify80211(skb, qd);
157 /* now we know the 1d priority, fill in the QoS header if there is one
159 if (WLAN_FC_IS_QOS_DATA(fc)) {
160 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
161 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
162 if (local->wifi_wme_noack_test)
163 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
164 QOS_CONTROL_ACK_POLICY_SHIFT;
165 /* qos header is 2 bytes, second reserved */
171 if (unlikely(queue >= local->hw.queues)) {
173 if (net_ratelimit()) {
174 printk(KERN_DEBUG "%s - queue=%d (hw does not "
176 __func__, queue, local->hw.queues - 1);
179 queue = local->hw.queues - 1;
182 if (unlikely(queue < 0)) {
186 pkt_data->queue = (unsigned int) queue;
187 qdisc = q->queues[queue];
188 err = qdisc->enqueue(skb, qdisc);
189 if (err == NET_XMIT_SUCCESS) {
191 qd->bstats.bytes += skb->len;
192 qd->bstats.packets++;
193 return NET_XMIT_SUCCESS;
201 /* TODO: clean up the cases where master_hard_start_xmit
202 * returns non 0 - it shouldn't ever do that. Once done we
203 * can remove this function */
204 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
206 struct ieee80211_sched_data *q = qdisc_priv(qd);
207 struct ieee80211_tx_packet_data *pkt_data =
208 (struct ieee80211_tx_packet_data *) skb->cb;
212 /* we recorded which queue to use earlier! */
213 qdisc = q->queues[pkt_data->queue];
215 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
224 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
226 struct ieee80211_sched_data *q = qdisc_priv(qd);
227 struct net_device *dev = qd->dev;
228 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
229 struct ieee80211_hw *hw = &local->hw;
234 /* check all the h/w queues in numeric/priority order */
235 for (queue = 0; queue < hw->queues; queue++) {
236 /* see if there is room in this hardware queue */
237 if (test_bit(IEEE80211_LINK_STATE_XOFF,
238 &local->state[queue]) ||
239 test_bit(IEEE80211_LINK_STATE_PENDING,
240 &local->state[queue]))
243 /* there is space - try and get a frame */
244 skb = skb_dequeue(&q->requeued[queue]);
250 qdisc = q->queues[queue];
251 skb = qdisc->dequeue(qdisc);
257 /* returning a NULL here when all the h/w queues are full means we
258 * never need to call netif_stop_queue in the driver */
263 static void wme_qdiscop_reset(struct Qdisc* qd)
265 struct ieee80211_sched_data *q = qdisc_priv(qd);
266 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
267 struct ieee80211_hw *hw = &local->hw;
270 /* QUESTION: should we have some hardware flush functionality here? */
272 for (queue = 0; queue < hw->queues; queue++) {
273 skb_queue_purge(&q->requeued[queue]);
274 qdisc_reset(q->queues[queue]);
280 static void wme_qdiscop_destroy(struct Qdisc* qd)
282 struct ieee80211_sched_data *q = qdisc_priv(qd);
283 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
284 struct ieee80211_hw *hw = &local->hw;
287 tcf_destroy_chain(q->filter_list);
288 q->filter_list = NULL;
290 for (queue=0; queue < hw->queues; queue++) {
291 skb_queue_purge(&q->requeued[queue]);
292 qdisc_destroy(q->queues[queue]);
293 q->queues[queue] = &noop_qdisc;
298 /* called whenever parameters are updated on existing qdisc */
299 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
301 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
303 /* check our options block is the right size */
304 /* copy any options to our local structure */
305 /* Ignore options block for now - always use static mapping
306 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
308 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
310 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
316 /* called during initial creation of qdisc on device */
317 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
319 struct ieee80211_sched_data *q = qdisc_priv(qd);
320 struct net_device *dev = qd->dev;
321 struct ieee80211_local *local;
325 /* check that device is a mac80211 device */
326 if (!dev->ieee80211_ptr ||
327 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
330 /* check this device is an ieee80211 master type device */
331 if (dev->type != ARPHRD_IEEE80211)
334 /* check that there is no qdisc currently attached to device
335 * this ensures that we will be the root qdisc. (I can't find a better
336 * way to test this explicitly) */
337 if (dev->qdisc_sleeping != &noop_qdisc)
340 if (qd->flags & TCQ_F_INGRESS)
343 local = wdev_priv(dev->ieee80211_ptr);
344 queues = local->hw.queues;
346 /* if options were passed in, set them */
348 err = wme_qdiscop_tune(qd, opt);
351 /* create child queues */
352 for (i = 0; i < queues; i++) {
353 skb_queue_head_init(&q->requeued[i]);
354 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
357 q->queues[i] = &noop_qdisc;
358 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
365 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
367 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
368 unsigned char *p = skb->tail;
369 struct tc_ieee80211_qopt opt;
371 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
372 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
376 skb_trim(skb, p - skb->data);*/
381 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
382 struct Qdisc *new, struct Qdisc **old)
384 struct ieee80211_sched_data *q = qdisc_priv(qd);
385 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
386 struct ieee80211_hw *hw = &local->hw;
387 unsigned long queue = arg - 1;
389 if (queue >= hw->queues)
396 *old = q->queues[queue];
397 q->queues[queue] = new;
405 static struct Qdisc *
406 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
408 struct ieee80211_sched_data *q = qdisc_priv(qd);
409 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
410 struct ieee80211_hw *hw = &local->hw;
411 unsigned long queue = arg - 1;
413 if (queue >= hw->queues)
416 return q->queues[queue];
420 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
422 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
423 struct ieee80211_hw *hw = &local->hw;
424 unsigned long queue = TC_H_MIN(classid);
426 if (queue - 1 >= hw->queues)
433 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
436 return wme_classop_get(qd, classid);
440 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
445 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
446 struct rtattr **tca, unsigned long *arg)
448 unsigned long cl = *arg;
449 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
450 struct ieee80211_hw *hw = &local->hw;
452 if (cl - 1 > hw->queues)
455 /* TODO: put code to program hardware queue parameters here,
456 * to allow programming from tc command line */
462 /* we don't support deleting hardware queues
463 * when we add WMM-SA support - TSPECs may be deleted here */
464 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
466 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
467 struct ieee80211_hw *hw = &local->hw;
469 if (cl - 1 > hw->queues)
475 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
476 struct sk_buff *skb, struct tcmsg *tcm)
478 struct ieee80211_sched_data *q = qdisc_priv(qd);
479 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
480 struct ieee80211_hw *hw = &local->hw;
482 if (cl - 1 > hw->queues)
484 tcm->tcm_handle = TC_H_MIN(cl);
485 tcm->tcm_parent = qd->handle;
486 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
491 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
493 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
494 struct ieee80211_hw *hw = &local->hw;
500 for (queue = 0; queue < hw->queues; queue++) {
501 if (arg->count < arg->skip) {
505 /* we should return classids for our internal queues here
506 * as well as the external ones */
507 if (arg->fn(qd, queue+1, arg) < 0) {
516 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
519 struct ieee80211_sched_data *q = qdisc_priv(qd);
524 return &q->filter_list;
528 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
529 * - these are the operations on the classes */
530 static struct Qdisc_class_ops class_ops =
532 .graft = wme_classop_graft,
533 .leaf = wme_classop_leaf,
535 .get = wme_classop_get,
536 .put = wme_classop_put,
537 .change = wme_classop_change,
538 .delete = wme_classop_delete,
539 .walk = wme_classop_walk,
541 .tcf_chain = wme_classop_find_tcf,
542 .bind_tcf = wme_classop_bind,
543 .unbind_tcf = wme_classop_put,
545 .dump = wme_classop_dump_class,
549 /* queueing discipline operations */
550 static struct Qdisc_ops wme_qdisc_ops =
553 .cl_ops = &class_ops,
555 .priv_size = sizeof(struct ieee80211_sched_data),
557 .enqueue = wme_qdiscop_enqueue,
558 .dequeue = wme_qdiscop_dequeue,
559 .requeue = wme_qdiscop_requeue,
560 .drop = NULL, /* drop not needed since we are always the root qdisc */
562 .init = wme_qdiscop_init,
563 .reset = wme_qdiscop_reset,
564 .destroy = wme_qdiscop_destroy,
565 .change = wme_qdiscop_tune,
567 .dump = wme_qdiscop_dump,
571 void ieee80211_install_qdisc(struct net_device *dev)
575 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
577 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
581 /* same handle as would be allocated by qdisc_alloc_handle() */
582 qdisc->handle = 0x80010000;
584 qdisc_lock_tree(dev);
585 list_add_tail(&qdisc->list, &dev->qdisc_list);
586 dev->qdisc_sleeping = qdisc;
587 qdisc_unlock_tree(dev);
591 int ieee80211_qdisc_installed(struct net_device *dev)
593 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
597 int ieee80211_wme_register(void)
599 return register_qdisc(&wme_qdisc_ops);
603 void ieee80211_wme_unregister(void)
605 unregister_qdisc(&wme_qdisc_ops);