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;
153 static inline int wme_downgrade_ac(struct sk_buff
*skb
)
155 switch (skb
->priority
) {
158 skb
->priority
= 5; /* VO -> VI */
162 skb
->priority
= 3; /* VI -> BE */
166 skb
->priority
= 2; /* BE -> BK */
174 /* positive return value indicates which queue to use
175 * negative return value indicates to drop the frame */
176 static inline int classify80211(struct sk_buff
*skb
, struct Qdisc
*qd
)
178 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
179 struct ieee80211_tx_packet_data
*pkt_data
=
180 (struct ieee80211_tx_packet_data
*) skb
->cb
;
181 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
182 unsigned short fc
= le16_to_cpu(hdr
->frame_control
);
184 const int ieee802_1d_to_ac
[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
186 /* see if frame is data or non data frame */
187 if (unlikely((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)) {
188 /* management frames go on AC_VO queue, but are sent
189 * without QoS control fields */
190 return IEEE80211_TX_QUEUE_DATA0
;
193 if (unlikely(pkt_data
->mgmt_iface
)) {
194 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
195 * and they will include QoS control fields if
196 * the target STA is using WME. */
198 return ieee802_1d_to_ac
[skb
->priority
];
201 /* is this a QoS frame? */
202 qos
= fc
& IEEE80211_STYPE_QOS_DATA
;
205 skb
->priority
= 0; /* required for correct WPA/11i MIC */
206 return ieee802_1d_to_ac
[skb
->priority
];
209 /* use the data classifier to determine what 802.1d tag the
211 skb
->priority
= classify_1d(skb
, qd
);
213 /* incase we are a client verify acm is not set for this ac */
214 while (unlikely(local
->wmm_acm
& BIT(skb
->priority
))) {
215 if (wme_downgrade_ac(skb
)) {
216 /* No AC with lower priority has acm=0,
222 /* look up which queue to use for frames with this 1d tag */
223 return ieee802_1d_to_ac
[skb
->priority
];
227 static int wme_qdiscop_enqueue(struct sk_buff
*skb
, struct Qdisc
* qd
)
229 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
230 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
231 struct ieee80211_tx_packet_data
*pkt_data
=
232 (struct ieee80211_tx_packet_data
*) skb
->cb
;
233 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
234 unsigned short fc
= le16_to_cpu(hdr
->frame_control
);
238 if (pkt_data
->requeue
) {
239 skb_queue_tail(&q
->requeued
[pkt_data
->queue
], skb
);
243 queue
= classify80211(skb
, qd
);
245 /* now we know the 1d priority, fill in the QoS header if there is one
247 if (WLAN_FC_IS_QOS_DATA(fc
)) {
248 u8
*p
= skb
->data
+ ieee80211_get_hdrlen(fc
) - 2;
249 u8 qos_hdr
= skb
->priority
& QOS_CONTROL_TAG1D_MASK
;
250 if (local
->wifi_wme_noack_test
)
251 qos_hdr
|= QOS_CONTROL_ACK_POLICY_NOACK
<<
252 QOS_CONTROL_ACK_POLICY_SHIFT
;
253 /* qos header is 2 bytes, second reserved */
259 if (unlikely(queue
>= local
->hw
.queues
)) {
261 if (net_ratelimit()) {
262 printk(KERN_DEBUG
"%s - queue=%d (hw does not "
264 __func__
, queue
, local
->hw
.queues
- 1);
267 queue
= local
->hw
.queues
- 1;
270 if (unlikely(queue
< 0)) {
274 pkt_data
->queue
= (unsigned int) queue
;
275 qdisc
= q
->queues
[queue
];
276 err
= qdisc
->enqueue(skb
, qdisc
);
277 if (err
== NET_XMIT_SUCCESS
) {
279 qd
->bstats
.bytes
+= skb
->len
;
280 qd
->bstats
.packets
++;
281 return NET_XMIT_SUCCESS
;
289 /* TODO: clean up the cases where master_hard_start_xmit
290 * returns non 0 - it shouldn't ever do that. Once done we
291 * can remove this function */
292 static int wme_qdiscop_requeue(struct sk_buff
*skb
, struct Qdisc
* qd
)
294 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
295 struct ieee80211_tx_packet_data
*pkt_data
=
296 (struct ieee80211_tx_packet_data
*) skb
->cb
;
300 /* we recorded which queue to use earlier! */
301 qdisc
= q
->queues
[pkt_data
->queue
];
303 if ((err
= qdisc
->ops
->requeue(skb
, qdisc
)) == 0) {
312 static struct sk_buff
*wme_qdiscop_dequeue(struct Qdisc
* qd
)
314 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
315 struct net_device
*dev
= qd
->dev
;
316 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
317 struct ieee80211_hw
*hw
= &local
->hw
;
322 /* check all the h/w queues in numeric/priority order */
323 for (queue
= 0; queue
< hw
->queues
; queue
++) {
324 /* see if there is room in this hardware queue */
325 if (test_bit(IEEE80211_LINK_STATE_XOFF
,
326 &local
->state
[queue
]) ||
327 test_bit(IEEE80211_LINK_STATE_PENDING
,
328 &local
->state
[queue
]))
331 /* there is space - try and get a frame */
332 skb
= skb_dequeue(&q
->requeued
[queue
]);
336 qdisc
= q
->queues
[queue
];
337 skb
= qdisc
->dequeue(qdisc
);
343 /* returning a NULL here when all the h/w queues are full means we
344 * never need to call netif_stop_queue in the driver */
349 static void wme_qdiscop_reset(struct Qdisc
* qd
)
351 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
352 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
353 struct ieee80211_hw
*hw
= &local
->hw
;
356 /* QUESTION: should we have some hardware flush functionality here? */
358 for (queue
= 0; queue
< hw
->queues
; queue
++) {
359 skb_queue_purge(&q
->requeued
[queue
]);
360 qdisc_reset(q
->queues
[queue
]);
366 static void wme_qdiscop_destroy(struct Qdisc
* qd
)
368 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
369 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
370 struct ieee80211_hw
*hw
= &local
->hw
;
371 struct tcf_proto
*tp
;
374 while ((tp
= q
->filter_list
) != NULL
) {
375 q
->filter_list
= tp
->next
;
376 tp
->ops
->destroy(tp
);
379 for (queue
=0; queue
< hw
->queues
; queue
++) {
380 skb_queue_purge(&q
->requeued
[queue
]);
381 qdisc_destroy(q
->queues
[queue
]);
382 q
->queues
[queue
] = &noop_qdisc
;
387 /* called whenever parameters are updated on existing qdisc */
388 static int wme_qdiscop_tune(struct Qdisc
*qd
, struct rtattr
*opt
)
390 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
392 /* check our options block is the right size */
393 /* copy any options to our local structure */
394 /* Ignore options block for now - always use static mapping
395 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
397 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
399 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
405 /* called during initial creation of qdisc on device */
406 static int wme_qdiscop_init(struct Qdisc
*qd
, struct rtattr
*opt
)
408 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
409 struct net_device
*dev
= qd
->dev
;
410 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
411 int queues
= local
->hw
.queues
;
414 /* check this device is an ieee80211 master type device */
415 if (dev
->type
!= ARPHRD_IEEE80211
)
418 /* check that there is no qdisc currently attached to device
419 * this ensures that we will be the root qdisc. (I can't find a better
420 * way to test this explicitly) */
421 if (dev
->qdisc_sleeping
!= &noop_qdisc
)
424 if (qd
->flags
& TCQ_F_INGRESS
)
427 /* if options were passed in, set them */
429 err
= wme_qdiscop_tune(qd
, opt
);
432 /* create child queues */
433 for (i
= 0; i
< queues
; i
++) {
434 skb_queue_head_init(&q
->requeued
[i
]);
435 q
->queues
[i
] = qdisc_create_dflt(qd
->dev
, &CHILD_QDISC_OPS
);
436 if (q
->queues
[i
] == 0) {
437 q
->queues
[i
] = &noop_qdisc
;
438 printk(KERN_ERR
"%s child qdisc %i creation failed", dev
->name
, i
);
445 static int wme_qdiscop_dump(struct Qdisc
*qd
, struct sk_buff
*skb
)
447 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
448 unsigned char *p = skb->tail;
449 struct tc_ieee80211_qopt opt;
451 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
452 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
456 skb_trim(skb, p - skb->data);*/
461 static int wme_classop_graft(struct Qdisc
*qd
, unsigned long arg
,
462 struct Qdisc
*new, struct Qdisc
**old
)
464 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
465 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
466 struct ieee80211_hw
*hw
= &local
->hw
;
467 unsigned long queue
= arg
- 1;
469 if (queue
>= hw
->queues
)
476 *old
= q
->queues
[queue
];
477 q
->queues
[queue
] = new;
485 static struct Qdisc
*
486 wme_classop_leaf(struct Qdisc
*qd
, unsigned long arg
)
488 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
489 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
490 struct ieee80211_hw
*hw
= &local
->hw
;
491 unsigned long queue
= arg
- 1;
493 if (queue
>= hw
->queues
)
496 return q
->queues
[queue
];
500 static unsigned long wme_classop_get(struct Qdisc
*qd
, u32 classid
)
502 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
503 struct ieee80211_hw
*hw
= &local
->hw
;
504 unsigned long queue
= TC_H_MIN(classid
);
506 if (queue
- 1 >= hw
->queues
)
513 static unsigned long wme_classop_bind(struct Qdisc
*qd
, unsigned long parent
,
516 return wme_classop_get(qd
, classid
);
520 static void wme_classop_put(struct Qdisc
*q
, unsigned long cl
)
522 /* printk(KERN_DEBUG "entering %s\n", __func__); */
526 static int wme_classop_change(struct Qdisc
*qd
, u32 handle
, u32 parent
,
527 struct rtattr
**tca
, unsigned long *arg
)
529 unsigned long cl
= *arg
;
530 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
531 struct ieee80211_hw
*hw
= &local
->hw
;
532 /* printk(KERN_DEBUG "entering %s\n", __func__); */
534 if (cl
- 1 > hw
->queues
)
537 /* TODO: put code to program hardware queue parameters here,
538 * to allow programming from tc command line */
544 /* we don't support deleting hardware queues
545 * when we add WMM-SA support - TSPECs may be deleted here */
546 static int wme_classop_delete(struct Qdisc
*qd
, unsigned long cl
)
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
)
558 static int wme_classop_dump_class(struct Qdisc
*qd
, unsigned long cl
,
559 struct sk_buff
*skb
, struct tcmsg
*tcm
)
561 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
562 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
563 struct ieee80211_hw
*hw
= &local
->hw
;
564 /* printk(KERN_DEBUG "entering %s\n", __func__); */
566 if (cl
- 1 > hw
->queues
)
568 tcm
->tcm_handle
= TC_H_MIN(cl
);
569 tcm
->tcm_parent
= qd
->handle
;
570 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
; /* do we need this? */
575 static void wme_classop_walk(struct Qdisc
*qd
, struct qdisc_walker
*arg
)
577 struct ieee80211_local
*local
= qd
->dev
->ieee80211_ptr
;
578 struct ieee80211_hw
*hw
= &local
->hw
;
580 /* printk(KERN_DEBUG "entering %s\n", __func__); */
585 for (queue
= 0; queue
< hw
->queues
; queue
++) {
586 if (arg
->count
< arg
->skip
) {
590 /* we should return classids for our internal queues here
591 * as well as the external ones */
592 if (arg
->fn(qd
, queue
+1, arg
) < 0) {
601 static struct tcf_proto
** wme_classop_find_tcf(struct Qdisc
*qd
,
604 struct ieee80211_sched_data
*q
= qdisc_priv(qd
);
605 /* printk("entering %s\n", __func__); */
610 return &q
->filter_list
;
614 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
615 * - these are the operations on the classes */
616 static struct Qdisc_class_ops class_ops
=
618 .graft
= wme_classop_graft
,
619 .leaf
= wme_classop_leaf
,
621 .get
= wme_classop_get
,
622 .put
= wme_classop_put
,
623 .change
= wme_classop_change
,
624 .delete = wme_classop_delete
,
625 .walk
= wme_classop_walk
,
627 .tcf_chain
= wme_classop_find_tcf
,
628 .bind_tcf
= wme_classop_bind
,
629 .unbind_tcf
= wme_classop_put
,
631 .dump
= wme_classop_dump_class
,
635 /* queueing discipline operations */
636 static struct Qdisc_ops wme_qdisc_ops
=
639 .cl_ops
= &class_ops
,
641 .priv_size
= sizeof(struct ieee80211_sched_data
),
643 .enqueue
= wme_qdiscop_enqueue
,
644 .dequeue
= wme_qdiscop_dequeue
,
645 .requeue
= wme_qdiscop_requeue
,
646 .drop
= NULL
, /* drop not needed since we are always the root qdisc */
648 .init
= wme_qdiscop_init
,
649 .reset
= wme_qdiscop_reset
,
650 .destroy
= wme_qdiscop_destroy
,
651 .change
= wme_qdiscop_tune
,
653 .dump
= wme_qdiscop_dump
,
657 void ieee80211_install_qdisc(struct net_device
*dev
)
661 qdisc
= qdisc_create_dflt(dev
, &wme_qdisc_ops
);
663 printk(KERN_ERR
"%s: qdisc installation failed\n", dev
->name
);
667 /* same handle as would be allocated by qdisc_alloc_handle() */
668 qdisc
->handle
= 0x80010000;
670 qdisc_lock_tree(dev
);
671 list_add_tail(&qdisc
->list
, &dev
->qdisc_list
);
672 dev
->qdisc_sleeping
= qdisc
;
673 qdisc_unlock_tree(dev
);
677 int ieee80211_wme_register(void)
681 #ifdef CONFIG_NET_SCHED
682 err
= register_qdisc(&wme_qdisc_ops
);
688 void ieee80211_wme_unregister(void)
690 #ifdef CONFIG_NET_SCHED
691 unregister_qdisc(&wme_qdisc_ops
);