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/d80211.h>
18 #include "ieee80211_i.h"
21 #define CHILD_QDISC_OPS pfifo_qdisc_ops
23 static inline int WLAN_FC_IS_QOS_DATA(u16 fc
)
25 return (fc
& 0x8C) == 0x88;
30 ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data
*rx
)
32 u8
*data
= rx
->skb
->data
;
35 /* does the frame have a qos control field? */
36 if (WLAN_FC_IS_QOS_DATA(rx
->fc
)) {
37 u8
*qc
= data
+ ieee80211_get_hdrlen(rx
->fc
) - QOS_CONTROL_LEN
;
38 /* frame has qos control */
39 tid
= qc
[0] & QOS_CONTROL_TID_MASK
;
41 if (unlikely((rx
->fc
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_MGMT
)) {
42 /* Separate TID for management frames */
43 tid
= NUM_RX_DATA_QUEUES
- 1;
45 /* no qos control present */
46 tid
= 0; /* 802.1d - Best Effort */
49 #ifdef CONFIG_D80211_DEBUG_COUNTERS
50 I802_DEBUG_INC(rx
->local
->wme_rx_queue
[tid
]);
52 I802_DEBUG_INC(rx
->sta
->wme_rx_queue
[tid
]);
54 #endif /* CONFIG_D80211_DEBUG_COUNTERS */
57 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
58 * For now, set skb->priority to 0 for other cases. */
59 rx
->skb
->priority
= (tid
> 7) ? 0 : tid
;
66 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data
*rx
)
69 u8
*data
= rx
->skb
->data
;
70 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) data
;
72 if (!WLAN_FC_IS_QOS_DATA(fc
))
75 /* remove the qos control field, update frame type and meta-data */
76 memmove(data
+ 2, data
, ieee80211_get_hdrlen(fc
) - 2);
77 hdr
= (struct ieee80211_hdr
*) skb_pull(rx
->skb
, 2);
78 /* change frame type to non QOS */
79 rx
->fc
= fc
&= ~IEEE80211_STYPE_QOS_DATA
;
80 hdr
->frame_control
= cpu_to_le16(fc
);
86 /* maximum number of hardware queues we support. */
87 #define TC_80211_MAX_QUEUES 8
89 struct ieee80211_sched_data
91 struct tcf_proto
*filter_list
;
92 struct Qdisc
*queues
[TC_80211_MAX_QUEUES
];
93 struct sk_buff_head requeued
[TC_80211_MAX_QUEUES
];
97 /* given a data frame determine the 802.1p/1d tag to use */
98 static inline unsigned classify_1d(struct sk_buff
*skb
, struct Qdisc
*qd
)
104 #ifdef CONFIG_NET_SCHED
105 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
106 struct tcf_result res
= { -1, 0 };
108 /* if there is a user set filter list, call out to that */
109 if (q
->filter_list
) {
110 tc_classify(skb
, q
->filter_list
, &res
);
114 #endif /* CONFIG_NET_SCHED */
116 /* skb->priority values from 256->263 are magic values to
117 * directly indicate a specific 802.1d priority.
118 * This is used to allow 802.1d priority to be passed directly in
119 * from VLAN tags, etc. */
120 if (skb
->priority
>= 256 && skb
->priority
<= 263)
121 return skb
->priority
- 256;
123 /* check there is a valid IP header present */
124 offset
= ieee80211_get_hdrlen_from_skb(skb
) + 8 /* LLC + proto */;
125 if (skb
->protocol
!= __constant_htons(ETH_P_IP
) ||
126 skb
->len
< offset
+ sizeof(*ip
))
129 ip
= (struct iphdr
*) (skb
->data
+ offset
);
131 dscp
= ip
->tos
& 0xfc;
138 static inline int wme_downgrade_ac(struct sk_buff
*skb
)
140 switch (skb
->priority
) {
143 skb
->priority
= 5; /* VO -> VI */
147 skb
->priority
= 3; /* VI -> BE */
151 skb
->priority
= 2; /* BE -> BK */
159 /* positive return value indicates which queue to use
160 * negative return value indicates to drop the frame */
161 static inline int classify80211(struct sk_buff
*skb
, struct Qdisc
*qd
)
163 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
164 struct ieee80211_tx_packet_data
*pkt_data
=
165 (struct ieee80211_tx_packet_data
*) skb
->cb
;
166 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
167 unsigned short fc
= le16_to_cpu(hdr
->frame_control
);
169 const int ieee802_1d_to_ac
[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
171 /* see if frame is data or non data frame */
172 if (unlikely((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)) {
173 /* management frames go on AC_VO queue, but are sent
174 * without QoS control fields */
175 return IEEE80211_TX_QUEUE_DATA0
;
178 if (unlikely(pkt_data
->mgmt_iface
)) {
179 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
180 * and they will include QoS control fields if
181 * the target STA is using WME. */
183 return ieee802_1d_to_ac
[skb
->priority
];
186 /* is this a QoS frame? */
187 qos
= fc
& IEEE80211_STYPE_QOS_DATA
;
190 skb
->priority
= 0; /* required for correct WPA/11i MIC */
191 return ieee802_1d_to_ac
[skb
->priority
];
194 /* use the data classifier to determine what 802.1d tag the
196 skb
->priority
= classify_1d(skb
, qd
);
198 /* incase we are a client verify acm is not set for this ac */
199 while (unlikely(local
->wmm_acm
& BIT(skb
->priority
))) {
200 if (wme_downgrade_ac(skb
)) {
201 /* No AC with lower priority has acm=0,
207 /* look up which queue to use for frames with this 1d tag */
208 return ieee802_1d_to_ac
[skb
->priority
];
212 static int wme_qdiscop_enqueue(struct sk_buff
*skb
, struct Qdisc
* qd
)
214 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
215 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
216 struct ieee80211_tx_packet_data
*pkt_data
=
217 (struct ieee80211_tx_packet_data
*) skb
->cb
;
218 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
219 unsigned short fc
= le16_to_cpu(hdr
->frame_control
);
223 if (pkt_data
->requeue
) {
224 skb_queue_tail(&q
->requeued
[pkt_data
->queue
], skb
);
228 queue
= classify80211(skb
, qd
);
230 /* now we know the 1d priority, fill in the QoS header if there is one
232 if (WLAN_FC_IS_QOS_DATA(fc
)) {
233 u8
*p
= skb
->data
+ ieee80211_get_hdrlen(fc
) - 2;
234 u8 qos_hdr
= skb
->priority
& QOS_CONTROL_TAG1D_MASK
;
235 if (local
->wifi_wme_noack_test
)
236 qos_hdr
|= QOS_CONTROL_ACK_POLICY_NOACK
<<
237 QOS_CONTROL_ACK_POLICY_SHIFT
;
238 /* qos header is 2 bytes, second reserved */
244 if (unlikely(queue
>= local
->hw
.queues
)) {
246 if (net_ratelimit()) {
247 printk(KERN_DEBUG
"%s - queue=%d (hw does not "
249 __func__
, queue
, local
->hw
.queues
- 1);
252 queue
= local
->hw
.queues
- 1;
255 if (unlikely(queue
< 0)) {
259 pkt_data
->queue
= (unsigned int) queue
;
260 qdisc
= q
->queues
[queue
];
261 err
= qdisc
->enqueue(skb
, qdisc
);
262 if (err
== NET_XMIT_SUCCESS
) {
264 qd
->bstats
.bytes
+= skb
->len
;
265 qd
->bstats
.packets
++;
266 return NET_XMIT_SUCCESS
;
274 /* TODO: clean up the cases where master_hard_start_xmit
275 * returns non 0 - it shouldn't ever do that. Once done we
276 * can remove this function */
277 static int wme_qdiscop_requeue(struct sk_buff
*skb
, struct Qdisc
* qd
)
279 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
280 struct ieee80211_tx_packet_data
*pkt_data
=
281 (struct ieee80211_tx_packet_data
*) skb
->cb
;
285 /* we recorded which queue to use earlier! */
286 qdisc
= q
->queues
[pkt_data
->queue
];
288 if ((err
= qdisc
->ops
->requeue(skb
, qdisc
)) == 0) {
297 static struct sk_buff
*wme_qdiscop_dequeue(struct Qdisc
* qd
)
299 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
300 struct net_device
*dev
= qd
->dev
;
301 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
302 struct ieee80211_hw
*hw
= &local
->hw
;
307 /* check all the h/w queues in numeric/priority order */
308 for (queue
= 0; queue
< hw
->queues
; queue
++) {
309 /* see if there is room in this hardware queue */
310 if (test_bit(IEEE80211_LINK_STATE_XOFF
,
311 &local
->state
[queue
]) ||
312 test_bit(IEEE80211_LINK_STATE_PENDING
,
313 &local
->state
[queue
]))
316 /* there is space - try and get a frame */
317 skb
= skb_dequeue(&q
->requeued
[queue
]);
321 qdisc
= q
->queues
[queue
];
322 skb
= qdisc
->dequeue(qdisc
);
328 /* returning a NULL here when all the h/w queues are full means we
329 * never need to call netif_stop_queue in the driver */
334 static void wme_qdiscop_reset(struct Qdisc
* qd
)
336 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
337 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
338 struct ieee80211_hw
*hw
= &local
->hw
;
341 /* QUESTION: should we have some hardware flush functionality here? */
343 for (queue
= 0; queue
< hw
->queues
; queue
++) {
344 skb_queue_purge(&q
->requeued
[queue
]);
345 qdisc_reset(q
->queues
[queue
]);
351 static void wme_qdiscop_destroy(struct Qdisc
* qd
)
353 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
354 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
355 struct ieee80211_hw
*hw
= &local
->hw
;
356 struct tcf_proto
*tp
;
359 while ((tp
= q
->filter_list
) != NULL
) {
360 q
->filter_list
= tp
->next
;
361 tp
->ops
->destroy(tp
);
364 for (queue
=0; queue
< hw
->queues
; queue
++) {
365 skb_queue_purge(&q
->requeued
[queue
]);
366 qdisc_destroy(q
->queues
[queue
]);
367 q
->queues
[queue
] = &noop_qdisc
;
372 /* called whenever parameters are updated on existing qdisc */
373 static int wme_qdiscop_tune(struct Qdisc
*qd
, struct rtattr
*opt
)
375 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
377 /* check our options block is the right size */
378 /* copy any options to our local structure */
379 /* Ignore options block for now - always use static mapping
380 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
382 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
384 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
390 /* called during initial creation of qdisc on device */
391 static int wme_qdiscop_init(struct Qdisc
*qd
, struct rtattr
*opt
)
393 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
394 struct net_device
*dev
= qd
->dev
;
395 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
396 int queues
= local
->hw
.queues
;
399 /* check this device is an ieee80211 master type device */
400 if (dev
->type
!= ARPHRD_IEEE80211
)
403 /* check that there is no qdisc currently attached to device
404 * this ensures that we will be the root qdisc. (I can't find a better
405 * way to test this explicitly) */
406 if (dev
->qdisc_sleeping
!= &noop_qdisc
)
409 if (qd
->flags
& TCQ_F_INGRESS
)
412 /* if options were passed in, set them */
414 err
= wme_qdiscop_tune(qd
, opt
);
417 /* create child queues */
418 for (i
= 0; i
< queues
; i
++) {
419 skb_queue_head_init(&q
->requeued
[i
]);
420 q
->queues
[i
] = qdisc_create_dflt(qd
->dev
, &CHILD_QDISC_OPS
,
422 if (q
->queues
[i
] == 0) {
423 q
->queues
[i
] = &noop_qdisc
;
424 printk(KERN_ERR
"%s child qdisc %i creation failed", dev
->name
, i
);
431 static int wme_qdiscop_dump(struct Qdisc
*qd
, struct sk_buff
*skb
)
433 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
434 unsigned char *p = skb->tail;
435 struct tc_ieee80211_qopt opt;
437 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
438 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
442 skb_trim(skb, p - skb->data);*/
447 static int wme_classop_graft(struct Qdisc
*qd
, unsigned long arg
,
448 struct Qdisc
*new, struct Qdisc
**old
)
450 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
451 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
452 struct ieee80211_hw
*hw
= &local
->hw
;
453 unsigned long queue
= arg
- 1;
455 if (queue
>= hw
->queues
)
462 *old
= q
->queues
[queue
];
463 q
->queues
[queue
] = new;
471 static struct Qdisc
*
472 wme_classop_leaf(struct Qdisc
*qd
, unsigned long arg
)
474 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
475 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
476 struct ieee80211_hw
*hw
= &local
->hw
;
477 unsigned long queue
= arg
- 1;
479 if (queue
>= hw
->queues
)
482 return q
->queues
[queue
];
486 static unsigned long wme_classop_get(struct Qdisc
*qd
, u32 classid
)
488 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
489 struct ieee80211_hw
*hw
= &local
->hw
;
490 unsigned long queue
= TC_H_MIN(classid
);
492 if (queue
- 1 >= hw
->queues
)
499 static unsigned long wme_classop_bind(struct Qdisc
*qd
, unsigned long parent
,
502 return wme_classop_get(qd
, classid
);
506 static void wme_classop_put(struct Qdisc
*q
, unsigned long cl
)
508 /* printk(KERN_DEBUG "entering %s\n", __func__); */
512 static int wme_classop_change(struct Qdisc
*qd
, u32 handle
, u32 parent
,
513 struct rtattr
**tca
, unsigned long *arg
)
515 unsigned long cl
= *arg
;
516 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
517 struct ieee80211_hw
*hw
= &local
->hw
;
518 /* printk(KERN_DEBUG "entering %s\n", __func__); */
520 if (cl
- 1 > hw
->queues
)
523 /* TODO: put code to program hardware queue parameters here,
524 * to allow programming from tc command line */
530 /* we don't support deleting hardware queues
531 * when we add WMM-SA support - TSPECs may be deleted here */
532 static int wme_classop_delete(struct Qdisc
*qd
, unsigned long cl
)
534 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
535 struct ieee80211_hw
*hw
= &local
->hw
;
536 /* printk(KERN_DEBUG "entering %s\n", __func__); */
538 if (cl
- 1 > hw
->queues
)
544 static int wme_classop_dump_class(struct Qdisc
*qd
, unsigned long cl
,
545 struct sk_buff
*skb
, struct tcmsg
*tcm
)
547 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
548 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
549 struct ieee80211_hw
*hw
= &local
->hw
;
550 /* printk(KERN_DEBUG "entering %s\n", __func__); */
552 if (cl
- 1 > hw
->queues
)
554 tcm
->tcm_handle
= TC_H_MIN(cl
);
555 tcm
->tcm_parent
= qd
->handle
;
556 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
; /* do we need this? */
561 static void wme_classop_walk(struct Qdisc
*qd
, struct qdisc_walker
*arg
)
563 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
564 struct ieee80211_hw
*hw
= &local
->hw
;
566 /* printk(KERN_DEBUG "entering %s\n", __func__); */
571 for (queue
= 0; queue
< hw
->queues
; queue
++) {
572 if (arg
->count
< arg
->skip
) {
576 /* we should return classids for our internal queues here
577 * as well as the external ones */
578 if (arg
->fn(qd
, queue
+1, arg
) < 0) {
587 static struct tcf_proto
** wme_classop_find_tcf(struct Qdisc
*qd
,
590 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
591 /* printk("entering %s\n", __func__); */
596 return &q
->filter_list
;
600 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
601 * - these are the operations on the classes */
602 static struct Qdisc_class_ops class_ops
=
604 .graft
= wme_classop_graft
,
605 .leaf
= wme_classop_leaf
,
607 .get
= wme_classop_get
,
608 .put
= wme_classop_put
,
609 .change
= wme_classop_change
,
610 .delete = wme_classop_delete
,
611 .walk
= wme_classop_walk
,
613 .tcf_chain
= wme_classop_find_tcf
,
614 .bind_tcf
= wme_classop_bind
,
615 .unbind_tcf
= wme_classop_put
,
617 .dump
= wme_classop_dump_class
,
621 /* queueing discipline operations */
622 static struct Qdisc_ops wme_qdisc_ops
=
625 .cl_ops
= &class_ops
,
627 .priv_size
= sizeof(struct ieee80211_sched_data
),
629 .enqueue
= wme_qdiscop_enqueue
,
630 .dequeue
= wme_qdiscop_dequeue
,
631 .requeue
= wme_qdiscop_requeue
,
632 .drop
= NULL
, /* drop not needed since we are always the root qdisc */
634 .init
= wme_qdiscop_init
,
635 .reset
= wme_qdiscop_reset
,
636 .destroy
= wme_qdiscop_destroy
,
637 .change
= wme_qdiscop_tune
,
639 .dump
= wme_qdiscop_dump
,
643 void ieee80211_install_qdisc(struct net_device
*dev
)
647 qdisc
= qdisc_create_dflt(dev
, &wme_qdisc_ops
, TC_H_ROOT
);
649 printk(KERN_ERR
"%s: qdisc installation failed\n", dev
->name
);
653 /* same handle as would be allocated by qdisc_alloc_handle() */
654 qdisc
->handle
= 0x80010000;
656 qdisc_lock_tree(dev
);
657 list_add_tail(&qdisc
->list
, &dev
->qdisc_list
);
658 dev
->qdisc_sleeping
= qdisc
;
659 qdisc_unlock_tree(dev
);
663 int ieee80211_wme_register(void)
667 #ifdef CONFIG_NET_SCHED
668 err
= register_qdisc(&wme_qdisc_ops
);
674 void ieee80211_wme_unregister(void)
676 #ifdef CONFIG_NET_SCHED
677 unregister_qdisc(&wme_qdisc_ops
);