wme.c 15 KB

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