wme.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/module.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/types.h>
  13. #include <net/ip.h>
  14. #include <net/pkt_sched.h>
  15. #include <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "wme.h"
  18. /* maximum number of hardware queues we support. */
  19. #define TC_80211_MAX_QUEUES 8
  20. struct ieee80211_sched_data
  21. {
  22. struct tcf_proto *filter_list;
  23. struct Qdisc *queues[TC_80211_MAX_QUEUES];
  24. struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
  25. };
  26. /* given a data frame determine the 802.1p/1d tag to use */
  27. static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
  28. {
  29. struct iphdr *ip;
  30. int dscp;
  31. int offset;
  32. struct ieee80211_sched_data *q = qdisc_priv(qd);
  33. struct tcf_result res = { -1, 0 };
  34. /* if there is a user set filter list, call out to that */
  35. if (q->filter_list) {
  36. tc_classify(skb, q->filter_list, &res);
  37. if (res.class != -1)
  38. return res.class;
  39. }
  40. /* skb->priority values from 256->263 are magic values to
  41. * directly indicate a specific 802.1d priority.
  42. * This is used to allow 802.1d priority to be passed directly in
  43. * from VLAN tags, etc. */
  44. if (skb->priority >= 256 && skb->priority <= 263)
  45. return skb->priority - 256;
  46. /* check there is a valid IP header present */
  47. offset = ieee80211_get_hdrlen_from_skb(skb) + 8 /* LLC + proto */;
  48. if (skb->protocol != __constant_htons(ETH_P_IP) ||
  49. skb->len < offset + sizeof(*ip))
  50. return 0;
  51. ip = (struct iphdr *) (skb->data + offset);
  52. dscp = ip->tos & 0xfc;
  53. if (dscp & 0x1c)
  54. return 0;
  55. return dscp >> 5;
  56. }
  57. static inline int wme_downgrade_ac(struct sk_buff *skb)
  58. {
  59. switch (skb->priority) {
  60. case 6:
  61. case 7:
  62. skb->priority = 5; /* VO -> VI */
  63. return 0;
  64. case 4:
  65. case 5:
  66. skb->priority = 3; /* VI -> BE */
  67. return 0;
  68. case 0:
  69. case 3:
  70. skb->priority = 2; /* BE -> BK */
  71. return 0;
  72. default:
  73. return -1;
  74. }
  75. }
  76. /* positive return value indicates which queue to use
  77. * negative return value indicates to drop the frame */
  78. static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
  79. {
  80. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  81. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  82. unsigned short fc = le16_to_cpu(hdr->frame_control);
  83. int qos;
  84. const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
  85. /* see if frame is data or non data frame */
  86. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
  87. /* management frames go on AC_VO queue, but are sent
  88. * without QoS control fields */
  89. return IEEE80211_TX_QUEUE_DATA0;
  90. }
  91. if (0 /* injected */) {
  92. /* use AC from radiotap */
  93. }
  94. /* is this a QoS frame? */
  95. qos = fc & IEEE80211_STYPE_QOS_DATA;
  96. if (!qos) {
  97. skb->priority = 0; /* required for correct WPA/11i MIC */
  98. return ieee802_1d_to_ac[skb->priority];
  99. }
  100. /* use the data classifier to determine what 802.1d tag the
  101. * data frame has */
  102. skb->priority = classify_1d(skb, qd);
  103. /* in case we are a client verify acm is not set for this ac */
  104. while (unlikely(local->wmm_acm & BIT(skb->priority))) {
  105. if (wme_downgrade_ac(skb)) {
  106. /* No AC with lower priority has acm=0, drop packet. */
  107. return -1;
  108. }
  109. }
  110. /* look up which queue to use for frames with this 1d tag */
  111. return ieee802_1d_to_ac[skb->priority];
  112. }
  113. static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
  114. {
  115. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  116. struct ieee80211_sched_data *q = qdisc_priv(qd);
  117. struct ieee80211_tx_packet_data *pkt_data =
  118. (struct ieee80211_tx_packet_data *) skb->cb;
  119. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  120. unsigned short fc = le16_to_cpu(hdr->frame_control);
  121. struct Qdisc *qdisc;
  122. int err, queue;
  123. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
  124. skb_queue_tail(&q->requeued[pkt_data->queue], skb);
  125. qd->q.qlen++;
  126. return 0;
  127. }
  128. queue = classify80211(skb, qd);
  129. /* now we know the 1d priority, fill in the QoS header if there is one
  130. */
  131. if (WLAN_FC_IS_QOS_DATA(fc)) {
  132. u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
  133. u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
  134. if (local->wifi_wme_noack_test)
  135. qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
  136. QOS_CONTROL_ACK_POLICY_SHIFT;
  137. /* qos header is 2 bytes, second reserved */
  138. *p = qos_hdr;
  139. p++;
  140. *p = 0;
  141. }
  142. if (unlikely(queue >= local->hw.queues)) {
  143. #if 0
  144. if (net_ratelimit()) {
  145. printk(KERN_DEBUG "%s - queue=%d (hw does not "
  146. "support) -> %d\n",
  147. __func__, queue, local->hw.queues - 1);
  148. }
  149. #endif
  150. queue = local->hw.queues - 1;
  151. }
  152. if (unlikely(queue < 0)) {
  153. kfree_skb(skb);
  154. err = NET_XMIT_DROP;
  155. } else {
  156. pkt_data->queue = (unsigned int) queue;
  157. qdisc = q->queues[queue];
  158. err = qdisc->enqueue(skb, qdisc);
  159. if (err == NET_XMIT_SUCCESS) {
  160. qd->q.qlen++;
  161. qd->bstats.bytes += skb->len;
  162. qd->bstats.packets++;
  163. return NET_XMIT_SUCCESS;
  164. }
  165. }
  166. qd->qstats.drops++;
  167. return err;
  168. }
  169. /* TODO: clean up the cases where master_hard_start_xmit
  170. * returns non 0 - it shouldn't ever do that. Once done we
  171. * can remove this function */
  172. static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
  173. {
  174. struct ieee80211_sched_data *q = qdisc_priv(qd);
  175. struct ieee80211_tx_packet_data *pkt_data =
  176. (struct ieee80211_tx_packet_data *) skb->cb;
  177. struct Qdisc *qdisc;
  178. int err;
  179. /* we recorded which queue to use earlier! */
  180. qdisc = q->queues[pkt_data->queue];
  181. if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
  182. qd->q.qlen++;
  183. return 0;
  184. }
  185. qd->qstats.drops++;
  186. return err;
  187. }
  188. static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
  189. {
  190. struct ieee80211_sched_data *q = qdisc_priv(qd);
  191. struct net_device *dev = qd->dev;
  192. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  193. struct ieee80211_hw *hw = &local->hw;
  194. struct sk_buff *skb;
  195. struct Qdisc *qdisc;
  196. int queue;
  197. /* check all the h/w queues in numeric/priority order */
  198. for (queue = 0; queue < hw->queues; queue++) {
  199. /* see if there is room in this hardware queue */
  200. if (test_bit(IEEE80211_LINK_STATE_XOFF,
  201. &local->state[queue]) ||
  202. test_bit(IEEE80211_LINK_STATE_PENDING,
  203. &local->state[queue]))
  204. continue;
  205. /* there is space - try and get a frame */
  206. skb = skb_dequeue(&q->requeued[queue]);
  207. if (skb) {
  208. qd->q.qlen--;
  209. return skb;
  210. }
  211. qdisc = q->queues[queue];
  212. skb = qdisc->dequeue(qdisc);
  213. if (skb) {
  214. qd->q.qlen--;
  215. return skb;
  216. }
  217. }
  218. /* returning a NULL here when all the h/w queues are full means we
  219. * never need to call netif_stop_queue in the driver */
  220. return NULL;
  221. }
  222. static void wme_qdiscop_reset(struct Qdisc* qd)
  223. {
  224. struct ieee80211_sched_data *q = qdisc_priv(qd);
  225. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  226. struct ieee80211_hw *hw = &local->hw;
  227. int queue;
  228. /* QUESTION: should we have some hardware flush functionality here? */
  229. for (queue = 0; queue < hw->queues; queue++) {
  230. skb_queue_purge(&q->requeued[queue]);
  231. qdisc_reset(q->queues[queue]);
  232. }
  233. qd->q.qlen = 0;
  234. }
  235. static void wme_qdiscop_destroy(struct Qdisc* qd)
  236. {
  237. struct ieee80211_sched_data *q = qdisc_priv(qd);
  238. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  239. struct ieee80211_hw *hw = &local->hw;
  240. int queue;
  241. tcf_destroy_chain(q->filter_list);
  242. q->filter_list = NULL;
  243. for (queue=0; queue < hw->queues; queue++) {
  244. skb_queue_purge(&q->requeued[queue]);
  245. qdisc_destroy(q->queues[queue]);
  246. q->queues[queue] = &noop_qdisc;
  247. }
  248. }
  249. /* called whenever parameters are updated on existing qdisc */
  250. static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
  251. {
  252. /* struct ieee80211_sched_data *q = qdisc_priv(qd);
  253. */
  254. /* check our options block is the right size */
  255. /* copy any options to our local structure */
  256. /* Ignore options block for now - always use static mapping
  257. struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
  258. if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
  259. return -EINVAL;
  260. memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
  261. */
  262. return 0;
  263. }
  264. /* called during initial creation of qdisc on device */
  265. static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
  266. {
  267. struct ieee80211_sched_data *q = qdisc_priv(qd);
  268. struct net_device *dev = qd->dev;
  269. struct ieee80211_local *local;
  270. int queues;
  271. int err = 0, i;
  272. /* check that device is a mac80211 device */
  273. if (!dev->ieee80211_ptr ||
  274. dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  275. return -EINVAL;
  276. /* check this device is an ieee80211 master type device */
  277. if (dev->type != ARPHRD_IEEE80211)
  278. return -EINVAL;
  279. /* check that there is no qdisc currently attached to device
  280. * this ensures that we will be the root qdisc. (I can't find a better
  281. * way to test this explicitly) */
  282. if (dev->qdisc_sleeping != &noop_qdisc)
  283. return -EINVAL;
  284. if (qd->flags & TCQ_F_INGRESS)
  285. return -EINVAL;
  286. local = wdev_priv(dev->ieee80211_ptr);
  287. queues = local->hw.queues;
  288. /* if options were passed in, set them */
  289. if (opt) {
  290. err = wme_qdiscop_tune(qd, opt);
  291. }
  292. /* create child queues */
  293. for (i = 0; i < queues; i++) {
  294. skb_queue_head_init(&q->requeued[i]);
  295. q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
  296. qd->handle);
  297. if (!q->queues[i]) {
  298. q->queues[i] = &noop_qdisc;
  299. printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
  300. }
  301. }
  302. return err;
  303. }
  304. static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
  305. {
  306. /* struct ieee80211_sched_data *q = qdisc_priv(qd);
  307. unsigned char *p = skb->tail;
  308. struct tc_ieee80211_qopt opt;
  309. memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
  310. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  311. */ return skb->len;
  312. /*
  313. rtattr_failure:
  314. skb_trim(skb, p - skb->data);*/
  315. return -1;
  316. }
  317. static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
  318. struct Qdisc *new, struct Qdisc **old)
  319. {
  320. struct ieee80211_sched_data *q = qdisc_priv(qd);
  321. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  322. struct ieee80211_hw *hw = &local->hw;
  323. unsigned long queue = arg - 1;
  324. if (queue >= hw->queues)
  325. return -EINVAL;
  326. if (!new)
  327. new = &noop_qdisc;
  328. sch_tree_lock(qd);
  329. *old = q->queues[queue];
  330. q->queues[queue] = new;
  331. qdisc_reset(*old);
  332. sch_tree_unlock(qd);
  333. return 0;
  334. }
  335. static struct Qdisc *
  336. wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
  337. {
  338. struct ieee80211_sched_data *q = qdisc_priv(qd);
  339. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  340. struct ieee80211_hw *hw = &local->hw;
  341. unsigned long queue = arg - 1;
  342. if (queue >= hw->queues)
  343. return NULL;
  344. return q->queues[queue];
  345. }
  346. static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
  347. {
  348. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  349. struct ieee80211_hw *hw = &local->hw;
  350. unsigned long queue = TC_H_MIN(classid);
  351. if (queue - 1 >= hw->queues)
  352. return 0;
  353. return queue;
  354. }
  355. static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
  356. u32 classid)
  357. {
  358. return wme_classop_get(qd, classid);
  359. }
  360. static void wme_classop_put(struct Qdisc *q, unsigned long cl)
  361. {
  362. }
  363. static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
  364. struct rtattr **tca, unsigned long *arg)
  365. {
  366. unsigned long cl = *arg;
  367. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  368. struct ieee80211_hw *hw = &local->hw;
  369. if (cl - 1 > hw->queues)
  370. return -ENOENT;
  371. /* TODO: put code to program hardware queue parameters here,
  372. * to allow programming from tc command line */
  373. return 0;
  374. }
  375. /* we don't support deleting hardware queues
  376. * when we add WMM-SA support - TSPECs may be deleted here */
  377. static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
  378. {
  379. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  380. struct ieee80211_hw *hw = &local->hw;
  381. if (cl - 1 > hw->queues)
  382. return -ENOENT;
  383. return 0;
  384. }
  385. static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
  386. struct sk_buff *skb, struct tcmsg *tcm)
  387. {
  388. struct ieee80211_sched_data *q = qdisc_priv(qd);
  389. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  390. struct ieee80211_hw *hw = &local->hw;
  391. if (cl - 1 > hw->queues)
  392. return -ENOENT;
  393. tcm->tcm_handle = TC_H_MIN(cl);
  394. tcm->tcm_parent = qd->handle;
  395. tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
  396. return 0;
  397. }
  398. static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
  399. {
  400. struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
  401. struct ieee80211_hw *hw = &local->hw;
  402. int queue;
  403. if (arg->stop)
  404. return;
  405. for (queue = 0; queue < hw->queues; queue++) {
  406. if (arg->count < arg->skip) {
  407. arg->count++;
  408. continue;
  409. }
  410. /* we should return classids for our internal queues here
  411. * as well as the external ones */
  412. if (arg->fn(qd, queue+1, arg) < 0) {
  413. arg->stop = 1;
  414. break;
  415. }
  416. arg->count++;
  417. }
  418. }
  419. static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
  420. unsigned long cl)
  421. {
  422. struct ieee80211_sched_data *q = qdisc_priv(qd);
  423. if (cl)
  424. return NULL;
  425. return &q->filter_list;
  426. }
  427. /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
  428. * - these are the operations on the classes */
  429. static struct Qdisc_class_ops class_ops =
  430. {
  431. .graft = wme_classop_graft,
  432. .leaf = wme_classop_leaf,
  433. .get = wme_classop_get,
  434. .put = wme_classop_put,
  435. .change = wme_classop_change,
  436. .delete = wme_classop_delete,
  437. .walk = wme_classop_walk,
  438. .tcf_chain = wme_classop_find_tcf,
  439. .bind_tcf = wme_classop_bind,
  440. .unbind_tcf = wme_classop_put,
  441. .dump = wme_classop_dump_class,
  442. };
  443. /* queueing discipline operations */
  444. static struct Qdisc_ops wme_qdisc_ops =
  445. {
  446. .next = NULL,
  447. .cl_ops = &class_ops,
  448. .id = "ieee80211",
  449. .priv_size = sizeof(struct ieee80211_sched_data),
  450. .enqueue = wme_qdiscop_enqueue,
  451. .dequeue = wme_qdiscop_dequeue,
  452. .requeue = wme_qdiscop_requeue,
  453. .drop = NULL, /* drop not needed since we are always the root qdisc */
  454. .init = wme_qdiscop_init,
  455. .reset = wme_qdiscop_reset,
  456. .destroy = wme_qdiscop_destroy,
  457. .change = wme_qdiscop_tune,
  458. .dump = wme_qdiscop_dump,
  459. };
  460. void ieee80211_install_qdisc(struct net_device *dev)
  461. {
  462. struct Qdisc *qdisc;
  463. qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
  464. if (!qdisc) {
  465. printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
  466. return;
  467. }
  468. /* same handle as would be allocated by qdisc_alloc_handle() */
  469. qdisc->handle = 0x80010000;
  470. qdisc_lock_tree(dev);
  471. list_add_tail(&qdisc->list, &dev->qdisc_list);
  472. dev->qdisc_sleeping = qdisc;
  473. qdisc_unlock_tree(dev);
  474. }
  475. int ieee80211_qdisc_installed(struct net_device *dev)
  476. {
  477. return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
  478. }
  479. int ieee80211_wme_register(void)
  480. {
  481. return register_qdisc(&wme_qdisc_ops);
  482. }
  483. void ieee80211_wme_unregister(void)
  484. {
  485. unregister_qdisc(&wme_qdisc_ops);
  486. }