[MAC80211]: clean up whitespace
[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->flags & IEEE80211_TXPD_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         /* in case 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, drop packet. */
135                         return -1;
136                 }
137         }
138
139         /* look up which queue to use for frames with this 1d tag */
140         return ieee802_1d_to_ac[skb->priority];
141 }
142
143
144 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
145 {
146         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
147         struct ieee80211_sched_data *q = qdisc_priv(qd);
148         struct ieee80211_tx_packet_data *pkt_data =
149                 (struct ieee80211_tx_packet_data *) skb->cb;
150         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
151         unsigned short fc = le16_to_cpu(hdr->frame_control);
152         struct Qdisc *qdisc;
153         int err, queue;
154
155         if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
156                 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
157                 qd->q.qlen++;
158                 return 0;
159         }
160
161         queue = classify80211(skb, qd);
162
163         /* now we know the 1d priority, fill in the QoS header if there is one
164          */
165         if (WLAN_FC_IS_QOS_DATA(fc)) {
166                 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
167                 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
168                 if (local->wifi_wme_noack_test)
169                         qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
170                                         QOS_CONTROL_ACK_POLICY_SHIFT;
171                 /* qos header is 2 bytes, second reserved */
172                 *p = qos_hdr;
173                 p++;
174                 *p = 0;
175         }
176
177         if (unlikely(queue >= local->hw.queues)) {
178 #if 0
179                 if (net_ratelimit()) {
180                         printk(KERN_DEBUG "%s - queue=%d (hw does not "
181                                "support) -> %d\n",
182                                __func__, queue, local->hw.queues - 1);
183                 }
184 #endif
185                 queue = local->hw.queues - 1;
186         }
187
188         if (unlikely(queue < 0)) {
189                         kfree_skb(skb);
190                         err = NET_XMIT_DROP;
191         } else {
192                 pkt_data->queue = (unsigned int) queue;
193                 qdisc = q->queues[queue];
194                 err = qdisc->enqueue(skb, qdisc);
195                 if (err == NET_XMIT_SUCCESS) {
196                         qd->q.qlen++;
197                         qd->bstats.bytes += skb->len;
198                         qd->bstats.packets++;
199                         return NET_XMIT_SUCCESS;
200                 }
201         }
202         qd->qstats.drops++;
203         return err;
204 }
205
206
207 /* TODO: clean up the cases where master_hard_start_xmit
208  * returns non 0 - it shouldn't ever do that. Once done we
209  * can remove this function */
210 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
211 {
212         struct ieee80211_sched_data *q = qdisc_priv(qd);
213         struct ieee80211_tx_packet_data *pkt_data =
214                 (struct ieee80211_tx_packet_data *) skb->cb;
215         struct Qdisc *qdisc;
216         int err;
217
218         /* we recorded which queue to use earlier! */
219         qdisc = q->queues[pkt_data->queue];
220
221         if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
222                 qd->q.qlen++;
223                 return 0;
224         }
225         qd->qstats.drops++;
226         return err;
227 }
228
229
230 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
231 {
232         struct ieee80211_sched_data *q = qdisc_priv(qd);
233         struct net_device *dev = qd->dev;
234         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
235         struct ieee80211_hw *hw = &local->hw;
236         struct sk_buff *skb;
237         struct Qdisc *qdisc;
238         int queue;
239
240         /* check all the h/w queues in numeric/priority order */
241         for (queue = 0; queue < hw->queues; queue++) {
242                 /* see if there is room in this hardware queue */
243                 if (test_bit(IEEE80211_LINK_STATE_XOFF,
244                              &local->state[queue]) ||
245                     test_bit(IEEE80211_LINK_STATE_PENDING,
246                              &local->state[queue]))
247                         continue;
248
249                 /* there is space - try and get a frame */
250                 skb = skb_dequeue(&q->requeued[queue]);
251                 if (skb) {
252                         qd->q.qlen--;
253                         return skb;
254                 }
255
256                 qdisc = q->queues[queue];
257                 skb = qdisc->dequeue(qdisc);
258                 if (skb) {
259                         qd->q.qlen--;
260                         return skb;
261                 }
262         }
263         /* returning a NULL here when all the h/w queues are full means we
264          * never need to call netif_stop_queue in the driver */
265         return NULL;
266 }
267
268
269 static void wme_qdiscop_reset(struct Qdisc* qd)
270 {
271         struct ieee80211_sched_data *q = qdisc_priv(qd);
272         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
273         struct ieee80211_hw *hw = &local->hw;
274         int queue;
275
276         /* QUESTION: should we have some hardware flush functionality here? */
277
278         for (queue = 0; queue < hw->queues; queue++) {
279                 skb_queue_purge(&q->requeued[queue]);
280                 qdisc_reset(q->queues[queue]);
281         }
282         qd->q.qlen = 0;
283 }
284
285
286 static void wme_qdiscop_destroy(struct Qdisc* qd)
287 {
288         struct ieee80211_sched_data *q = qdisc_priv(qd);
289         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
290         struct ieee80211_hw *hw = &local->hw;
291         int queue;
292
293         tcf_destroy_chain(q->filter_list);
294         q->filter_list = NULL;
295
296         for (queue=0; queue < hw->queues; queue++) {
297                 skb_queue_purge(&q->requeued[queue]);
298                 qdisc_destroy(q->queues[queue]);
299                 q->queues[queue] = &noop_qdisc;
300         }
301 }
302
303
304 /* called whenever parameters are updated on existing qdisc */
305 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
306 {
307 /*      struct ieee80211_sched_data *q = qdisc_priv(qd);
308 */
309         /* check our options block is the right size */
310         /* copy any options to our local structure */
311 /*      Ignore options block for now - always use static mapping
312         struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
313
314         if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
315                 return -EINVAL;
316         memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
317 */
318         return 0;
319 }
320
321
322 /* called during initial creation of qdisc on device */
323 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
324 {
325         struct ieee80211_sched_data *q = qdisc_priv(qd);
326         struct net_device *dev = qd->dev;
327         struct ieee80211_local *local;
328         int queues;
329         int err = 0, i;
330
331         /* check that device is a mac80211 device */
332         if (!dev->ieee80211_ptr ||
333             dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
334                 return -EINVAL;
335
336         /* check this device is an ieee80211 master type device */
337         if (dev->type != ARPHRD_IEEE80211)
338                 return -EINVAL;
339
340         /* check that there is no qdisc currently attached to device
341          * this ensures that we will be the root qdisc. (I can't find a better
342          * way to test this explicitly) */
343         if (dev->qdisc_sleeping != &noop_qdisc)
344                 return -EINVAL;
345
346         if (qd->flags & TCQ_F_INGRESS)
347                 return -EINVAL;
348
349         local = wdev_priv(dev->ieee80211_ptr);
350         queues = local->hw.queues;
351
352         /* if options were passed in, set them */
353         if (opt) {
354                 err = wme_qdiscop_tune(qd, opt);
355         }
356
357         /* create child queues */
358         for (i = 0; i < queues; i++) {
359                 skb_queue_head_init(&q->requeued[i]);
360                 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
361                                                  qd->handle);
362                 if (!q->queues[i]) {
363                         q->queues[i] = &noop_qdisc;
364                         printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
365                 }
366         }
367
368         return err;
369 }
370
371 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
372 {
373 /*      struct ieee80211_sched_data *q = qdisc_priv(qd);
374         unsigned char *p = skb->tail;
375         struct tc_ieee80211_qopt opt;
376
377         memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
378         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
379 */      return skb->len;
380 /*
381 rtattr_failure:
382         skb_trim(skb, p - skb->data);*/
383         return -1;
384 }
385
386
387 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
388                              struct Qdisc *new, struct Qdisc **old)
389 {
390         struct ieee80211_sched_data *q = qdisc_priv(qd);
391         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
392         struct ieee80211_hw *hw = &local->hw;
393         unsigned long queue = arg - 1;
394
395         if (queue >= hw->queues)
396                 return -EINVAL;
397
398         if (!new)
399                 new = &noop_qdisc;
400
401         sch_tree_lock(qd);
402         *old = q->queues[queue];
403         q->queues[queue] = new;
404         qdisc_reset(*old);
405         sch_tree_unlock(qd);
406
407         return 0;
408 }
409
410
411 static struct Qdisc *
412 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
413 {
414         struct ieee80211_sched_data *q = qdisc_priv(qd);
415         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
416         struct ieee80211_hw *hw = &local->hw;
417         unsigned long queue = arg - 1;
418
419         if (queue >= hw->queues)
420                 return NULL;
421
422         return q->queues[queue];
423 }
424
425
426 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
427 {
428         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
429         struct ieee80211_hw *hw = &local->hw;
430         unsigned long queue = TC_H_MIN(classid);
431
432         if (queue - 1 >= hw->queues)
433                 return 0;
434
435         return queue;
436 }
437
438
439 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
440                                       u32 classid)
441 {
442         return wme_classop_get(qd, classid);
443 }
444
445
446 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
447 {
448 }
449
450
451 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
452                               struct rtattr **tca, unsigned long *arg)
453 {
454         unsigned long cl = *arg;
455         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
456         struct ieee80211_hw *hw = &local->hw;
457
458         if (cl - 1 > hw->queues)
459                 return -ENOENT;
460
461         /* TODO: put code to program hardware queue parameters here,
462          * to allow programming from tc command line */
463
464         return 0;
465 }
466
467
468 /* we don't support deleting hardware queues
469  * when we add WMM-SA support - TSPECs may be deleted here */
470 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
471 {
472         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
473         struct ieee80211_hw *hw = &local->hw;
474
475         if (cl - 1 > hw->queues)
476                 return -ENOENT;
477         return 0;
478 }
479
480
481 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
482                                   struct sk_buff *skb, struct tcmsg *tcm)
483 {
484         struct ieee80211_sched_data *q = qdisc_priv(qd);
485         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
486         struct ieee80211_hw *hw = &local->hw;
487
488         if (cl - 1 > hw->queues)
489                 return -ENOENT;
490         tcm->tcm_handle = TC_H_MIN(cl);
491         tcm->tcm_parent = qd->handle;
492         tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
493         return 0;
494 }
495
496
497 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
498 {
499         struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
500         struct ieee80211_hw *hw = &local->hw;
501         int queue;
502
503         if (arg->stop)
504                 return;
505
506         for (queue = 0; queue < hw->queues; queue++) {
507                 if (arg->count < arg->skip) {
508                         arg->count++;
509                         continue;
510                 }
511                 /* we should return classids for our internal queues here
512                  * as well as the external ones */
513                 if (arg->fn(qd, queue+1, arg) < 0) {
514                         arg->stop = 1;
515                         break;
516                 }
517                 arg->count++;
518         }
519 }
520
521
522 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
523                                                 unsigned long cl)
524 {
525         struct ieee80211_sched_data *q = qdisc_priv(qd);
526
527         if (cl)
528                 return NULL;
529
530         return &q->filter_list;
531 }
532
533
534 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
535  * - these are the operations on the classes */
536 static struct Qdisc_class_ops class_ops =
537 {
538         .graft = wme_classop_graft,
539         .leaf = wme_classop_leaf,
540
541         .get = wme_classop_get,
542         .put = wme_classop_put,
543         .change = wme_classop_change,
544         .delete = wme_classop_delete,
545         .walk = wme_classop_walk,
546
547         .tcf_chain = wme_classop_find_tcf,
548         .bind_tcf = wme_classop_bind,
549         .unbind_tcf = wme_classop_put,
550
551         .dump = wme_classop_dump_class,
552 };
553
554
555 /* queueing discipline operations */
556 static struct Qdisc_ops wme_qdisc_ops =
557 {
558         .next = NULL,
559         .cl_ops = &class_ops,
560         .id = "ieee80211",
561         .priv_size = sizeof(struct ieee80211_sched_data),
562
563         .enqueue = wme_qdiscop_enqueue,
564         .dequeue = wme_qdiscop_dequeue,
565         .requeue = wme_qdiscop_requeue,
566         .drop = NULL, /* drop not needed since we are always the root qdisc */
567
568         .init = wme_qdiscop_init,
569         .reset = wme_qdiscop_reset,
570         .destroy = wme_qdiscop_destroy,
571         .change = wme_qdiscop_tune,
572
573         .dump = wme_qdiscop_dump,
574 };
575
576
577 void ieee80211_install_qdisc(struct net_device *dev)
578 {
579         struct Qdisc *qdisc;
580
581         qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
582         if (!qdisc) {
583                 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
584                 return;
585         }
586
587         /* same handle as would be allocated by qdisc_alloc_handle() */
588         qdisc->handle = 0x80010000;
589
590         qdisc_lock_tree(dev);
591         list_add_tail(&qdisc->list, &dev->qdisc_list);
592         dev->qdisc_sleeping = qdisc;
593         qdisc_unlock_tree(dev);
594 }
595
596
597 int ieee80211_qdisc_installed(struct net_device *dev)
598 {
599         return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
600 }
601
602
603 int ieee80211_wme_register(void)
604 {
605         return register_qdisc(&wme_qdisc_ops);
606 }
607
608
609 void ieee80211_wme_unregister(void)
610 {
611         unregister_qdisc(&wme_qdisc_ops);
612 }