1
2
3
4
5
6
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
22#define TC_80211_MAX_QUEUES 8
23
24struct 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
33static 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
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
50
51
52
53 if (skb->priority >= 256 && skb->priority <= 263)
54 return skb->priority - 256;
55
56
57 offset = ieee80211_get_hdrlen_from_skb(skb) + 8 ;
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
71static inline int wme_downgrade_ac(struct sk_buff *skb)
72{
73 switch (skb->priority) {
74 case 6:
75 case 7:
76 skb->priority = 5;
77 return 0;
78 case 4:
79 case 5:
80 skb->priority = 3;
81 return 0;
82 case 0:
83 case 3:
84 skb->priority = 2;
85 return 0;
86 default:
87 return -1;
88 }
89}
90
91
92
93
94static 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_hdr *hdr = (struct ieee80211_hdr *) skb->data;
98 unsigned short fc = le16_to_cpu(hdr->frame_control);
99 int qos;
100 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
101
102
103 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
104
105
106 return IEEE80211_TX_QUEUE_DATA0;
107 }
108
109 if (0 ) {
110
111 }
112
113
114 qos = fc & IEEE80211_STYPE_QOS_DATA;
115
116 if (!qos) {
117 skb->priority = 0;
118 return ieee802_1d_to_ac[skb->priority];
119 }
120
121
122
123 skb->priority = classify_1d(skb, qd);
124
125
126 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
127 if (wme_downgrade_ac(skb)) {
128
129 return -1;
130 }
131 }
132
133
134 return ieee802_1d_to_ac[skb->priority];
135}
136
137
138static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
139{
140 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
141 struct ieee80211_sched_data *q = qdisc_priv(qd);
142 struct ieee80211_tx_packet_data *pkt_data =
143 (struct ieee80211_tx_packet_data *) skb->cb;
144 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
145 unsigned short fc = le16_to_cpu(hdr->frame_control);
146 struct Qdisc *qdisc;
147 int err, queue;
148
149 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
150 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
151 qd->q.qlen++;
152 return 0;
153 }
154
155 queue = classify80211(skb, qd);
156
157
158
159 if (WLAN_FC_IS_QOS_DATA(fc)) {
160 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
161 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
162 if (local->wifi_wme_noack_test)
163 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
164 QOS_CONTROL_ACK_POLICY_SHIFT;
165
166 *p = qos_hdr;
167 p++;
168 *p = 0;
169 }
170
171 if (unlikely(queue >= local->hw.queues)) {
172#if 0
173 if (net_ratelimit()) {
174 printk(KERN_DEBUG "%s - queue=%d (hw does not "
175 "support) -> %d\n",
176 __func__, queue, local->hw.queues - 1);
177 }
178#endif
179 queue = local->hw.queues - 1;
180 }
181
182 if (unlikely(queue < 0)) {
183 kfree_skb(skb);
184 err = NET_XMIT_DROP;
185 } else {
186 pkt_data->queue = (unsigned int) queue;
187 qdisc = q->queues[queue];
188 err = qdisc->enqueue(skb, qdisc);
189 if (err == NET_XMIT_SUCCESS) {
190 qd->q.qlen++;
191 qd->bstats.bytes += skb->len;
192 qd->bstats.packets++;
193 return NET_XMIT_SUCCESS;
194 }
195 }
196 qd->qstats.drops++;
197 return err;
198}
199
200
201
202
203
204static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
205{
206 struct ieee80211_sched_data *q = qdisc_priv(qd);
207 struct ieee80211_tx_packet_data *pkt_data =
208 (struct ieee80211_tx_packet_data *) skb->cb;
209 struct Qdisc *qdisc;
210 int err;
211
212
213 qdisc = q->queues[pkt_data->queue];
214
215 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
216 qd->q.qlen++;
217 return 0;
218 }
219 qd->qstats.drops++;
220 return err;
221}
222
223
224static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
225{
226 struct ieee80211_sched_data *q = qdisc_priv(qd);
227 struct net_device *dev = qd->dev;
228 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
229 struct ieee80211_hw *hw = &local->hw;
230 struct sk_buff *skb;
231 struct Qdisc *qdisc;
232 int queue;
233
234
235 for (queue = 0; queue < hw->queues; queue++) {
236
237 if (test_bit(IEEE80211_LINK_STATE_XOFF,
238 &local->state[queue]) ||
239 test_bit(IEEE80211_LINK_STATE_PENDING,
240 &local->state[queue]))
241 continue;
242
243
244 skb = skb_dequeue(&q->requeued[queue]);
245 if (skb) {
246 qd->q.qlen--;
247 return skb;
248 }
249
250 qdisc = q->queues[queue];
251 skb = qdisc->dequeue(qdisc);
252 if (skb) {
253 qd->q.qlen--;
254 return skb;
255 }
256 }
257
258
259 return NULL;
260}
261
262
263static void wme_qdiscop_reset(struct Qdisc* qd)
264{
265 struct ieee80211_sched_data *q = qdisc_priv(qd);
266 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
267 struct ieee80211_hw *hw = &local->hw;
268 int queue;
269
270
271
272 for (queue = 0; queue < hw->queues; queue++) {
273 skb_queue_purge(&q->requeued[queue]);
274 qdisc_reset(q->queues[queue]);
275 }
276 qd->q.qlen = 0;
277}
278
279
280static void wme_qdiscop_destroy(struct Qdisc* qd)
281{
282 struct ieee80211_sched_data *q = qdisc_priv(qd);
283 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
284 struct ieee80211_hw *hw = &local->hw;
285 int queue;
286
287 tcf_destroy_chain(q->filter_list);
288 q->filter_list = NULL;
289
290 for (queue=0; queue < hw->queues; queue++) {
291 skb_queue_purge(&q->requeued[queue]);
292 qdisc_destroy(q->queues[queue]);
293 q->queues[queue] = &noop_qdisc;
294 }
295}
296
297
298
299static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
300{
301
302
303
304
305
306
307
308
309
310
311
312 return 0;
313}
314
315
316
317static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
318{
319 struct ieee80211_sched_data *q = qdisc_priv(qd);
320 struct net_device *dev = qd->dev;
321 struct ieee80211_local *local;
322 int queues;
323 int err = 0, i;
324
325
326 if (!dev->ieee80211_ptr ||
327 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
328 return -EINVAL;
329
330
331 if (dev->type != ARPHRD_IEEE80211)
332 return -EINVAL;
333
334
335
336
337 if (dev->qdisc_sleeping != &noop_qdisc)
338 return -EINVAL;
339
340 if (qd->flags & TCQ_F_INGRESS)
341 return -EINVAL;
342
343 local = wdev_priv(dev->ieee80211_ptr);
344 queues = local->hw.queues;
345
346
347 if (opt) {
348 err = wme_qdiscop_tune(qd, opt);
349 }
350
351
352 for (i = 0; i < queues; i++) {
353 skb_queue_head_init(&q->requeued[i]);
354 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
355 qd->handle);
356 if (!q->queues[i]) {
357 q->queues[i] = &noop_qdisc;
358 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
359 }
360 }
361
362 return err;
363}
364
365static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
366{
367
368
369
370
371
372
373 return skb->len;
374
375
376
377 return -1;
378}
379
380
381static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
382 struct Qdisc *new, struct Qdisc **old)
383{
384 struct ieee80211_sched_data *q = qdisc_priv(qd);
385 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
386 struct ieee80211_hw *hw = &local->hw;
387 unsigned long queue = arg - 1;
388
389 if (queue >= hw->queues)
390 return -EINVAL;
391
392 if (!new)
393 new = &noop_qdisc;
394
395 sch_tree_lock(qd);
396 *old = q->queues[queue];
397 q->queues[queue] = new;
398 qdisc_reset(*old);
399 sch_tree_unlock(qd);
400
401 return 0;
402}
403
404
405static struct Qdisc *
406wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
407{
408 struct ieee80211_sched_data *q = qdisc_priv(qd);
409 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
410 struct ieee80211_hw *hw = &local->hw;
411 unsigned long queue = arg - 1;
412
413 if (queue >= hw->queues)
414 return NULL;
415
416 return q->queues[queue];
417}
418
419
420static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
421{
422 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
423 struct ieee80211_hw *hw = &local->hw;
424 unsigned long queue = TC_H_MIN(classid);
425
426 if (queue - 1 >= hw->queues)
427 return 0;
428
429 return queue;
430}
431
432
433static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
434 u32 classid)
435{
436 return wme_classop_get(qd, classid);
437}
438
439
440static void wme_classop_put(struct Qdisc *q, unsigned long cl)
441{
442}
443
444
445static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
446 struct rtattr **tca, unsigned long *arg)
447{
448 unsigned long cl = *arg;
449 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
450 struct ieee80211_hw *hw = &local->hw;
451
452 if (cl - 1 > hw->queues)
453 return -ENOENT;
454
455
456
457
458 return 0;
459}
460
461
462
463
464static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
465{
466 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
467 struct ieee80211_hw *hw = &local->hw;
468
469 if (cl - 1 > hw->queues)
470 return -ENOENT;
471 return 0;
472}
473
474
475static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
476 struct sk_buff *skb, struct tcmsg *tcm)
477{
478 struct ieee80211_sched_data *q = qdisc_priv(qd);
479 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
480 struct ieee80211_hw *hw = &local->hw;
481
482 if (cl - 1 > hw->queues)
483 return -ENOENT;
484 tcm->tcm_handle = TC_H_MIN(cl);
485 tcm->tcm_parent = qd->handle;
486 tcm->tcm_info = q->queues[cl-1]->handle;
487 return 0;
488}
489
490
491static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
492{
493 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
494 struct ieee80211_hw *hw = &local->hw;
495 int queue;
496
497 if (arg->stop)
498 return;
499
500 for (queue = 0; queue < hw->queues; queue++) {
501 if (arg->count < arg->skip) {
502 arg->count++;
503 continue;
504 }
505
506
507 if (arg->fn(qd, queue+1, arg) < 0) {
508 arg->stop = 1;
509 break;
510 }
511 arg->count++;
512 }
513}
514
515
516static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
517 unsigned long cl)
518{
519 struct ieee80211_sched_data *q = qdisc_priv(qd);
520
521 if (cl)
522 return NULL;
523
524 return &q->filter_list;
525}
526
527
528
529
530static struct Qdisc_class_ops class_ops =
531{
532 .graft = wme_classop_graft,
533 .leaf = wme_classop_leaf,
534
535 .get = wme_classop_get,
536 .put = wme_classop_put,
537 .change = wme_classop_change,
538 .delete = wme_classop_delete,
539 .walk = wme_classop_walk,
540
541 .tcf_chain = wme_classop_find_tcf,
542 .bind_tcf = wme_classop_bind,
543 .unbind_tcf = wme_classop_put,
544
545 .dump = wme_classop_dump_class,
546};
547
548
549
550static struct Qdisc_ops wme_qdisc_ops =
551{
552 .next = NULL,
553 .cl_ops = &class_ops,
554 .id = "ieee80211",
555 .priv_size = sizeof(struct ieee80211_sched_data),
556
557 .enqueue = wme_qdiscop_enqueue,
558 .dequeue = wme_qdiscop_dequeue,
559 .requeue = wme_qdiscop_requeue,
560 .drop = NULL,
561
562 .init = wme_qdiscop_init,
563 .reset = wme_qdiscop_reset,
564 .destroy = wme_qdiscop_destroy,
565 .change = wme_qdiscop_tune,
566
567 .dump = wme_qdiscop_dump,
568};
569
570
571void ieee80211_install_qdisc(struct net_device *dev)
572{
573 struct Qdisc *qdisc;
574
575 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
576 if (!qdisc) {
577 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
578 return;
579 }
580
581
582 qdisc->handle = 0x80010000;
583
584 qdisc_lock_tree(dev);
585 list_add_tail(&qdisc->list, &dev->qdisc_list);
586 dev->qdisc_sleeping = qdisc;
587 qdisc_unlock_tree(dev);
588}
589
590
591int ieee80211_qdisc_installed(struct net_device *dev)
592{
593 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
594}
595
596
597int ieee80211_wme_register(void)
598{
599 return register_qdisc(&wme_qdisc_ops);
600}
601
602
603void ieee80211_wme_unregister(void)
604{
605 unregister_qdisc(&wme_qdisc_ops);
606}
607