update d80211 to latest wireless-dev version
[openwrt/openwrt.git] / package / d80211 / src / wme.c
1 /*
2 * Copyright 2004, Instant802 Networks, Inc.
3 *
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.
7 */
8
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>
14 #include <net/ip.h>
15 #include <net/pkt_sched.h>
16
17 #include <net/d80211.h>
18 #include "ieee80211_i.h"
19 #include "wme.h"
20
21 #define CHILD_QDISC_OPS pfifo_qdisc_ops
22
23 static inline int WLAN_FC_IS_QOS_DATA(u16 fc)
24 {
25 return (fc & 0x8C) == 0x88;
26 }
27
28
29 ieee80211_txrx_result
30 ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
31 {
32 u8 *data = rx->skb->data;
33 int tid;
34
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;
40 } else {
41 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
42 /* Separate TID for management frames */
43 tid = NUM_RX_DATA_QUEUES - 1;
44 } else {
45 /* no qos control present */
46 tid = 0; /* 802.1d - Best Effort */
47 }
48 }
49 #ifdef CONFIG_D80211_DEBUG_COUNTERS
50 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
51 if (rx->sta) {
52 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
53 }
54 #endif /* CONFIG_D80211_DEBUG_COUNTERS */
55
56 rx->u.rx.queue = tid;
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;
60
61 return TXRX_CONTINUE;
62 }
63
64
65 ieee80211_txrx_result
66 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
67 {
68 u16 fc = rx->fc;
69 u8 *data = rx->skb->data;
70 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
71
72 if (!WLAN_FC_IS_QOS_DATA(fc))
73 return TXRX_CONTINUE;
74
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);
81
82 return TXRX_CONTINUE;
83 }
84
85
86 /* maximum number of hardware queues we support. */
87 #define TC_80211_MAX_QUEUES 8
88
89 struct ieee80211_sched_data
90 {
91 struct tcf_proto *filter_list;
92 struct Qdisc *queues[TC_80211_MAX_QUEUES];
93 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
94 };
95
96
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)
99 {
100 struct iphdr *ip;
101 int dscp;
102 int offset;
103
104 #ifdef CONFIG_NET_SCHED
105 struct ieee80211_sched_data *q = qdisc_priv(qd);
106 struct tcf_result res = { -1, 0 };
107
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);
111 if (res.class != -1)
112 return res.class;
113 }
114 #endif /* CONFIG_NET_SCHED */
115
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;
122
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))
127 return 0;
128
129 ip = (struct iphdr *) (skb->data + offset);
130
131 dscp = ip->tos & 0xfc;
132 if (dscp & 0x1c)
133 return 0;
134 return dscp >> 5;
135 }
136
137
138 static inline int wme_downgrade_ac(struct sk_buff *skb)
139 {
140 switch (skb->priority) {
141 case 6:
142 case 7:
143 skb->priority = 5; /* VO -> VI */
144 return 0;
145 case 4:
146 case 5:
147 skb->priority = 3; /* VI -> BE */
148 return 0;
149 case 0:
150 case 3:
151 skb->priority = 2; /* BE -> BK */
152 return 0;
153 default:
154 return -1;
155 }
156 }
157
158
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)
162 {
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);
168 int qos;
169 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
170
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;
176 }
177
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. */
182 skb->priority = 7;
183 return ieee802_1d_to_ac[skb->priority];
184 }
185
186 /* is this a QoS frame? */
187 qos = fc & IEEE80211_STYPE_QOS_DATA;
188
189 if (!qos) {
190 skb->priority = 0; /* required for correct WPA/11i MIC */
191 return ieee802_1d_to_ac[skb->priority];
192 }
193
194 /* use the data classifier to determine what 802.1d tag the
195 * data frame has */
196 skb->priority = classify_1d(skb, qd);
197
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,
202 * drop packet. */
203 return -1;
204 }
205 }
206
207 /* look up which queue to use for frames with this 1d tag */
208 return ieee802_1d_to_ac[skb->priority];
209 }
210
211
212 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
213 {
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);
220 struct Qdisc *qdisc;
221 int err, queue;
222
223 if (pkt_data->requeue) {
224 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
225 return 0;
226 }
227
228 queue = classify80211(skb, qd);
229
230 /* now we know the 1d priority, fill in the QoS header if there is one
231 */
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 */
239 *p = qos_hdr;
240 p++;
241 *p = 0;
242 }
243
244 if (unlikely(queue >= local->hw.queues)) {
245 #if 0
246 if (net_ratelimit()) {
247 printk(KERN_DEBUG "%s - queue=%d (hw does not "
248 "support) -> %d\n",
249 __func__, queue, local->hw.queues - 1);
250 }
251 #endif
252 queue = local->hw.queues - 1;
253 }
254
255 if (unlikely(queue < 0)) {
256 kfree_skb(skb);
257 err = NET_XMIT_DROP;
258 } else {
259 pkt_data->queue = (unsigned int) queue;
260 qdisc = q->queues[queue];
261 err = qdisc->enqueue(skb, qdisc);
262 if (err == NET_XMIT_SUCCESS) {
263 qd->q.qlen++;
264 qd->bstats.bytes += skb->len;
265 qd->bstats.packets++;
266 return NET_XMIT_SUCCESS;
267 }
268 }
269 qd->qstats.drops++;
270 return err;
271 }
272
273
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)
278 {
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;
282 struct Qdisc *qdisc;
283 int err;
284
285 /* we recorded which queue to use earlier! */
286 qdisc = q->queues[pkt_data->queue];
287
288 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
289 qd->q.qlen++;
290 return 0;
291 }
292 qd->qstats.drops++;
293 return err;
294 }
295
296
297 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
298 {
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;
303 struct sk_buff *skb;
304 struct Qdisc *qdisc;
305 int queue;
306
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]))
314 continue;
315
316 /* there is space - try and get a frame */
317 skb = skb_dequeue(&q->requeued[queue]);
318 if (skb)
319 return skb;
320
321 qdisc = q->queues[queue];
322 skb = qdisc->dequeue(qdisc);
323 if (skb) {
324 qd->q.qlen--;
325 return skb;
326 }
327 }
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 */
330 return NULL;
331 }
332
333
334 static void wme_qdiscop_reset(struct Qdisc* qd)
335 {
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;
339 int queue;
340
341 /* QUESTION: should we have some hardware flush functionality here? */
342
343 for (queue = 0; queue < hw->queues; queue++) {
344 skb_queue_purge(&q->requeued[queue]);
345 qdisc_reset(q->queues[queue]);
346 }
347 qd->q.qlen = 0;
348 }
349
350
351 static void wme_qdiscop_destroy(struct Qdisc* qd)
352 {
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;
357 int queue;
358
359 while ((tp = q->filter_list) != NULL) {
360 q->filter_list = tp->next;
361 tp->ops->destroy(tp);
362 }
363
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;
368 }
369 }
370
371
372 /* called whenever parameters are updated on existing qdisc */
373 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
374 {
375 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
376 */
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);
381
382 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
383 return -EINVAL;
384 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
385 */
386 return 0;
387 }
388
389
390 /* called during initial creation of qdisc on device */
391 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
392 {
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;
397 int err = 0, i;
398
399 /* check this device is an ieee80211 master type device */
400 if (dev->type != ARPHRD_IEEE80211)
401 return -EINVAL;
402
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)
407 return -EINVAL;
408
409 if (qd->flags & TCQ_F_INGRESS)
410 return -EINVAL;
411
412 /* if options were passed in, set them */
413 if (opt) {
414 err = wme_qdiscop_tune(qd, opt);
415 }
416
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,
421 qd->handle);
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);
425 }
426 }
427
428 return err;
429 }
430
431 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
432 {
433 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
434 unsigned char *p = skb->tail;
435 struct tc_ieee80211_qopt opt;
436
437 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
438 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
439 */ return skb->len;
440 /*
441 rtattr_failure:
442 skb_trim(skb, p - skb->data);*/
443 return -1;
444 }
445
446
447 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
448 struct Qdisc *new, struct Qdisc **old)
449 {
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;
454
455 if (queue >= hw->queues)
456 return -EINVAL;
457
458 if (!new)
459 new = &noop_qdisc;
460
461 sch_tree_lock(qd);
462 *old = q->queues[queue];
463 q->queues[queue] = new;
464 qdisc_reset(*old);
465 sch_tree_unlock(qd);
466
467 return 0;
468 }
469
470
471 static struct Qdisc *
472 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
473 {
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;
478
479 if (queue >= hw->queues)
480 return NULL;
481
482 return q->queues[queue];
483 }
484
485
486 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
487 {
488 struct ieee80211_local *local = qd->dev->ieee80211_ptr;
489 struct ieee80211_hw *hw = &local->hw;
490 unsigned long queue = TC_H_MIN(classid);
491
492 if (queue - 1 >= hw->queues)
493 return 0;
494
495 return queue;
496 }
497
498
499 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
500 u32 classid)
501 {
502 return wme_classop_get(qd, classid);
503 }
504
505
506 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
507 {
508 /* printk(KERN_DEBUG "entering %s\n", __func__); */
509 }
510
511
512 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
513 struct rtattr **tca, unsigned long *arg)
514 {
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__); */
519
520 if (cl - 1 > hw->queues)
521 return -ENOENT;
522
523 /* TODO: put code to program hardware queue parameters here,
524 * to allow programming from tc command line */
525
526 return 0;
527 }
528
529
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)
533 {
534 struct ieee80211_local *local = qd->dev->ieee80211_ptr;
535 struct ieee80211_hw *hw = &local->hw;
536 /* printk(KERN_DEBUG "entering %s\n", __func__); */
537
538 if (cl - 1 > hw->queues)
539 return -ENOENT;
540 return 0;
541 }
542
543
544 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
545 struct sk_buff *skb, struct tcmsg *tcm)
546 {
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__); */
551
552 if (cl - 1 > hw->queues)
553 return -ENOENT;
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? */
557 return 0;
558 }
559
560
561 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
562 {
563 struct ieee80211_local *local = qd->dev->ieee80211_ptr;
564 struct ieee80211_hw *hw = &local->hw;
565 int queue;
566 /* printk(KERN_DEBUG "entering %s\n", __func__); */
567
568 if (arg->stop)
569 return;
570
571 for (queue = 0; queue < hw->queues; queue++) {
572 if (arg->count < arg->skip) {
573 arg->count++;
574 continue;
575 }
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) {
579 arg->stop = 1;
580 break;
581 }
582 arg->count++;
583 }
584 }
585
586
587 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
588 unsigned long cl)
589 {
590 struct ieee80211_sched_data *q = qdisc_priv(qd);
591 /* printk("entering %s\n", __func__); */
592
593 if (cl)
594 return NULL;
595
596 return &q->filter_list;
597 }
598
599
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 =
603 {
604 .graft = wme_classop_graft,
605 .leaf = wme_classop_leaf,
606
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,
612
613 .tcf_chain = wme_classop_find_tcf,
614 .bind_tcf = wme_classop_bind,
615 .unbind_tcf = wme_classop_put,
616
617 .dump = wme_classop_dump_class,
618 };
619
620
621 /* queueing discipline operations */
622 static struct Qdisc_ops wme_qdisc_ops =
623 {
624 .next = NULL,
625 .cl_ops = &class_ops,
626 .id = "ieee80211",
627 .priv_size = sizeof(struct ieee80211_sched_data),
628
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 */
633
634 .init = wme_qdiscop_init,
635 .reset = wme_qdiscop_reset,
636 .destroy = wme_qdiscop_destroy,
637 .change = wme_qdiscop_tune,
638
639 .dump = wme_qdiscop_dump,
640 };
641
642
643 void ieee80211_install_qdisc(struct net_device *dev)
644 {
645 struct Qdisc *qdisc;
646
647 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
648 if (!qdisc) {
649 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
650 return;
651 }
652
653 /* same handle as would be allocated by qdisc_alloc_handle() */
654 qdisc->handle = 0x80010000;
655
656 qdisc_lock_tree(dev);
657 list_add_tail(&qdisc->list, &dev->qdisc_list);
658 dev->qdisc_sleeping = qdisc;
659 qdisc_unlock_tree(dev);
660 }
661
662
663 int ieee80211_wme_register(void)
664 {
665 int err = 0;
666
667 #ifdef CONFIG_NET_SCHED
668 err = register_qdisc(&wme_qdisc_ops);
669 #endif
670 return err;
671 }
672
673
674 void ieee80211_wme_unregister(void)
675 {
676 #ifdef CONFIG_NET_SCHED
677 unregister_qdisc(&wme_qdisc_ops);
678 #endif
679 }