wme.c 15 KB

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