[MAC80211]: move QoS rx handlers into rx.c
[safe/jmp/linux-2.6] / net / mac80211 / 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/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "wme.h"
20
21 /* maximum number of hardware queues we support. */
22 #define TC_80211_MAX_QUEUES 8
23
24 struct ieee80211_sched_data
25 {
26         struct tcf_proto *filter_list;
27         struct Qdisc *queues[TC_80211_MAX_QUEUES];
28         struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
29 };
30
31
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)
34 {
35         struct iphdr *ip;
36         int dscp;
37         int offset;
38
39         struct ieee80211_sched_data *q = qdisc_priv(qd);
40         struct tcf_result res = { -1, 0 };
41
42         /* if there is a user set filter list, call out to that */
43         if (q->filter_list) {
44                 tc_classify(skb, q->filter_list, &res);
45                 if (res.class != -1)
46                         return res.class;
47         }
48
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;
55
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))
60                 return 0;
61
62         ip = (struct iphdr *) (skb->data + offset);
63
64         dscp = ip->tos & 0xfc;
65         if (dscp & 0x1c)
66                 return 0;
67         return dscp >> 5;
68 }
69
70
71 static inline int wme_downgrade_ac(struct sk_buff *skb)
72 {
73         switch (skb->priority) {
74         case 6:
75         case 7:
76                 skb->priority = 5; /* VO -> VI */
77                 return 0;
78         case 4:
79         case 5:
80                 skb->priority = 3; /* VI -> BE */
81                 return 0;
82         case 0:
83         case 3:
84                 skb->priority = 2; /* BE -> BK */
85                 return 0;
86         default:
87                 return -1;
88         }
89 }
90
91
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)
95 {
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);
101         int qos;
102         const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
103
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;
109         }
110
111         if (unlikely(pkt_data->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. */
115                 skb->priority = 7;
116                 return ieee802_1d_to_ac[skb->priority];
117         }
118
119         /* is this a QoS frame? */
120         qos = fc & IEEE80211_STYPE_QOS_DATA;
121
122         if (!qos) {
123                 skb->priority = 0; /* required for correct WPA/11i MIC */
124                 return ieee802_1d_to_ac[skb->priority];
125         }
126
127         /* use the data classifier to determine what 802.1d tag the
128         * data frame has */
129         skb->priority = classify_1d(skb, qd);
130
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,
135                         * drop packet. */
136                         return -1;
137                 }
138         }
139
140         /* look up which queue to use for frames with this 1d tag */
141         return ieee802_1d_to_ac[skb->priority];
142 }
143
144
145 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
146 {
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);
153         struct Qdisc *qdisc;
154         int err, queue;
155
156         if (pkt_data->requeue) {
157                 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
158                 qd->q.qlen++;
159                 return 0;
160         }
161
162         queue = classify80211(skb, qd);
163
164         /* now we know the 1d priority, fill in the QoS header if there is one
165          */
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 */
173                 *p = qos_hdr;
174                 p++;
175                 *p = 0;
176         }
177
178         if (unlikely(queue >= local->hw.queues)) {
179 #if 0
180                 if (net_ratelimit()) {
181                         printk(KERN_DEBUG "%s - queue=%d (hw does not "
182                                "support) -> %d\n",
183                                __func__, queue, local->hw.queues - 1);
184                 }
185 #endif
186                 queue = local->hw.queues - 1;
187         }
188
189         if (unlikely(queue < 0)) {
190                         kfree_skb(skb);
191                         err = NET_XMIT_DROP;
192         } else {
193                 pkt_data->queue = (unsigned int) queue;
194                 qdisc = q->queues[queue];
195                 err = qdisc->enqueue(skb, qdisc);
196                 if (err == NET_XMIT_SUCCESS) {
197                         qd->q.qlen++;
198                         qd->bstats.bytes += skb->len;
199                         qd->bstats.packets++;
200                         return NET_XMIT_SUCCESS;
201                 }
202         }
203         qd->qstats.drops++;
204         return err;
205 }
206
207
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)
212 {
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;
216         struct Qdisc *qdisc;
217         int err;
218
219         /* we recorded which queue to use earlier! */
220         qdisc = q->queues[pkt_data->queue];
221
222         if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
223                 qd->q.qlen++;
224                 return 0;
225         }
226         qd->qstats.drops++;
227         return err;
228 }
229
230
231 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
232 {
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;
237         struct sk_buff *skb;
238         struct Qdisc *qdisc;
239         int queue;
240
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]))
248                         continue;
249
250                 /* there is space - try and get a frame */
251                 skb = skb_dequeue(&q->requeued[queue]);
252                 if (skb) {
253                         qd->q.qlen--;
254                         return skb;
255                 }
256
257                 qdisc = q->queues[queue];
258                 skb = qdisc->dequeue(qdisc);
259                 if (skb) {
260                         qd->q.qlen--;
261                         return skb;
262                 }
263         }
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 */
266         return NULL;
267 }
268
269
270 static void wme_qdiscop_reset(struct Qdisc* qd)
271 {
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;
275         int queue;
276
277         /* QUESTION: should we have some hardware flush functionality here? */
278
279         for (queue = 0; queue < hw->queues; queue++) {
280                 skb_queue_purge(&q->requeued[queue]);
281                 qdisc_reset(q->queues[queue]);
282         }
283         qd->q.qlen = 0;
284 }
285
286
287 static void wme_qdiscop_destroy(struct Qdisc* qd)
288 {
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;
292         int queue;
293
294         tcf_destroy_chain(q->filter_list);
295         q->filter_list = NULL;
296
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;
301         }
302 }
303
304
305 /* called whenever parameters are updated on existing qdisc */
306 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
307 {
308 /*      struct ieee80211_sched_data *q = qdisc_priv(qd);
309 */
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);
314
315         if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
316                 return -EINVAL;
317         memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
318 */
319         return 0;
320 }
321
322
323 /* called during initial creation of qdisc on device */
324 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
325 {
326         struct ieee80211_sched_data *q = qdisc_priv(qd);
327         struct net_device *dev = qd->dev;
328         struct ieee80211_local *local;
329         int queues;
330         int err = 0, i;
331
332         /* check that device is a mac80211 device */
333         if (!dev->ieee80211_ptr ||
334             dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
335                 return -EINVAL;
336
337         /* check this device is an ieee80211 master type device */
338         if (dev->type != ARPHRD_IEEE80211)
339                 return -EINVAL;
340
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)
345                 return -EINVAL;
346
347         if (qd->flags & TCQ_F_INGRESS)
348                 return -EINVAL;
349
350         local = wdev_priv(dev->ieee80211_ptr);
351         queues = local->hw.queues;
352
353         /* if options were passed in, set them */
354         if (opt) {
355                 err = wme_qdiscop_tune(qd, opt);
356         }
357
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,
362                                                  qd->handle);
363                 if (!q->queues[i]) {
364                         q->queues[i] = &noop_qdisc;
365                         printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
366                 }
367         }
368
369         return err;
370 }
371
372 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
373 {
374 /*      struct ieee80211_sched_data *q = qdisc_priv(qd);
375         unsigned char *p = skb->tail;
376         struct tc_ieee80211_qopt opt;
377
378         memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
379         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
380 */      return skb->len;
381 /*
382 rtattr_failure:
383         skb_trim(skb, p - skb->data);*/
384         return -1;
385 }
386
387
388 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
389                              struct Qdisc *new, struct Qdisc **old)
390 {
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;
395
396         if (queue >= hw->queues)
397                 return -EINVAL;
398
399         if (!new)
400                 new = &noop_qdisc;
401
402         sch_tree_lock(qd);
403         *old = q->queues[queue];
404         q->queues[queue] = new;
405         qdisc_reset(*old);
406         sch_tree_unlock(qd);
407
408         return 0;
409 }
410
411
412 static struct Qdisc *
413 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
414 {
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;
419
420         if (queue >= hw->queues)
421                 return NULL;
422
423         return q->queues[queue];
424 }
425
426
427 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
428 {
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);
432
433         if (queue - 1 >= hw->queues)
434                 return 0;
435
436         return queue;
437 }
438
439
440 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
441                                       u32 classid)
442 {
443         return wme_classop_get(qd, classid);
444 }
445
446
447 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
448 {
449 }
450
451
452 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
453                               struct rtattr **tca, unsigned long *arg)
454 {
455         unsigned long cl = *arg;
456         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
457         struct ieee80211_hw *hw = &local->hw;
458
459         if (cl - 1 > hw->queues)
460                 return -ENOENT;
461
462         /* TODO: put code to program hardware queue parameters here,
463          * to allow programming from tc command line */
464
465         return 0;
466 }
467
468
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)
472 {
473         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
474         struct ieee80211_hw *hw = &local->hw;
475
476         if (cl - 1 > hw->queues)
477                 return -ENOENT;
478         return 0;
479 }
480
481
482 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
483                                   struct sk_buff *skb, struct tcmsg *tcm)
484 {
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;
488
489         if (cl - 1 > hw->queues)
490                 return -ENOENT;
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? */
494         return 0;
495 }
496
497
498 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
499 {
500         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
501         struct ieee80211_hw *hw = &local->hw;
502         int queue;
503
504         if (arg->stop)
505                 return;
506
507         for (queue = 0; queue < hw->queues; queue++) {
508                 if (arg->count < arg->skip) {
509                         arg->count++;
510                         continue;
511                 }
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) {
515                         arg->stop = 1;
516                         break;
517                 }
518                 arg->count++;
519         }
520 }
521
522
523 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
524                                                 unsigned long cl)
525 {
526         struct ieee80211_sched_data *q = qdisc_priv(qd);
527
528         if (cl)
529                 return NULL;
530
531         return &q->filter_list;
532 }
533
534
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 =
538 {
539         .graft = wme_classop_graft,
540         .leaf = wme_classop_leaf,
541
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,
547
548         .tcf_chain = wme_classop_find_tcf,
549         .bind_tcf = wme_classop_bind,
550         .unbind_tcf = wme_classop_put,
551
552         .dump = wme_classop_dump_class,
553 };
554
555
556 /* queueing discipline operations */
557 static struct Qdisc_ops wme_qdisc_ops =
558 {
559         .next = NULL,
560         .cl_ops = &class_ops,
561         .id = "ieee80211",
562         .priv_size = sizeof(struct ieee80211_sched_data),
563
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 */
568
569         .init = wme_qdiscop_init,
570         .reset = wme_qdiscop_reset,
571         .destroy = wme_qdiscop_destroy,
572         .change = wme_qdiscop_tune,
573
574         .dump = wme_qdiscop_dump,
575 };
576
577
578 void ieee80211_install_qdisc(struct net_device *dev)
579 {
580         struct Qdisc *qdisc;
581
582         qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
583         if (!qdisc) {
584                 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
585                 return;
586         }
587
588         /* same handle as would be allocated by qdisc_alloc_handle() */
589         qdisc->handle = 0x80010000;
590
591         qdisc_lock_tree(dev);
592         list_add_tail(&qdisc->list, &dev->qdisc_list);
593         dev->qdisc_sleeping = qdisc;
594         qdisc_unlock_tree(dev);
595 }
596
597
598 int ieee80211_qdisc_installed(struct net_device *dev)
599 {
600         return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
601 }
602
603
604 int ieee80211_wme_register(void)
605 {
606         return register_qdisc(&wme_qdisc_ops);
607 }
608
609
610 void ieee80211_wme_unregister(void)
611 {
612         unregister_qdisc(&wme_qdisc_ops);
613 }