wme.c 15 KB

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