wme.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /* Default mapping in classifier to work with default
  19. * queue setup.
  20. */
  21. const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
  22. static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
  23. /* Given a data frame determine the 802.1p/1d tag to use. */
  24. static unsigned int classify_1d(struct sk_buff *skb)
  25. {
  26. unsigned int dscp;
  27. /* skb->priority values from 256->263 are magic values to
  28. * directly indicate a specific 802.1d priority. This is used
  29. * to allow 802.1d priority to be passed directly in from VLAN
  30. * tags, etc.
  31. */
  32. if (skb->priority >= 256 && skb->priority <= 263)
  33. return skb->priority - 256;
  34. switch (skb->protocol) {
  35. case htons(ETH_P_IP):
  36. dscp = ip_hdr(skb)->tos & 0xfc;
  37. break;
  38. default:
  39. return 0;
  40. }
  41. return dscp >> 5;
  42. }
  43. static int wme_downgrade_ac(struct sk_buff *skb)
  44. {
  45. switch (skb->priority) {
  46. case 6:
  47. case 7:
  48. skb->priority = 5; /* VO -> VI */
  49. return 0;
  50. case 4:
  51. case 5:
  52. skb->priority = 3; /* VI -> BE */
  53. return 0;
  54. case 0:
  55. case 3:
  56. skb->priority = 2; /* BE -> BK */
  57. return 0;
  58. default:
  59. return -1;
  60. }
  61. }
  62. /* Indicate which queue to use. */
  63. static u16 classify80211(struct ieee80211_local *local, struct sk_buff *skb)
  64. {
  65. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  66. if (!ieee80211_is_data(hdr->frame_control)) {
  67. /* management frames go on AC_VO queue, but are sent
  68. * without QoS control fields */
  69. return 0;
  70. }
  71. if (0 /* injected */) {
  72. /* use AC from radiotap */
  73. }
  74. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  75. skb->priority = 0; /* required for correct WPA/11i MIC */
  76. return ieee802_1d_to_ac[skb->priority];
  77. }
  78. /* use the data classifier to determine what 802.1d tag the
  79. * data frame has */
  80. skb->priority = classify_1d(skb);
  81. /* in case we are a client verify acm is not set for this ac */
  82. while (unlikely(local->wmm_acm & BIT(skb->priority))) {
  83. if (wme_downgrade_ac(skb)) {
  84. /* The old code would drop the packet in this
  85. * case.
  86. */
  87. return 0;
  88. }
  89. }
  90. /* look up which queue to use for frames with this 1d tag */
  91. return ieee802_1d_to_ac[skb->priority];
  92. }
  93. u16 ieee80211_select_queue(struct net_device *dev, struct sk_buff *skb)
  94. {
  95. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  96. struct ieee80211_local *local = mpriv->local;
  97. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  98. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  99. struct sta_info *sta;
  100. u16 queue;
  101. u8 tid;
  102. queue = classify80211(local, skb);
  103. if (unlikely(queue >= local->hw.queues))
  104. queue = local->hw.queues - 1;
  105. if (info->flags & IEEE80211_TX_CTL_REQUEUE) {
  106. rcu_read_lock();
  107. sta = sta_info_get(local, hdr->addr1);
  108. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  109. if (sta) {
  110. struct ieee80211_hw *hw = &local->hw;
  111. int ampdu_queue = sta->tid_to_tx_q[tid];
  112. if ((ampdu_queue < ieee80211_num_queues(hw)) &&
  113. test_bit(ampdu_queue, local->queue_pool)) {
  114. queue = ampdu_queue;
  115. info->flags |= IEEE80211_TX_CTL_AMPDU;
  116. } else {
  117. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  118. }
  119. }
  120. rcu_read_unlock();
  121. return queue;
  122. }
  123. /* Now we know the 1d priority, fill in the QoS header if
  124. * there is one.
  125. */
  126. if (ieee80211_is_data_qos(hdr->frame_control)) {
  127. u8 *p = ieee80211_get_qos_ctl(hdr);
  128. u8 ack_policy = 0;
  129. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  130. if (local->wifi_wme_noack_test)
  131. ack_policy |= QOS_CONTROL_ACK_POLICY_NOACK <<
  132. QOS_CONTROL_ACK_POLICY_SHIFT;
  133. /* qos header is 2 bytes, second reserved */
  134. *p++ = ack_policy | tid;
  135. *p = 0;
  136. rcu_read_lock();
  137. sta = sta_info_get(local, hdr->addr1);
  138. if (sta) {
  139. int ampdu_queue = sta->tid_to_tx_q[tid];
  140. struct ieee80211_hw *hw = &local->hw;
  141. if ((ampdu_queue < ieee80211_num_queues(hw)) &&
  142. test_bit(ampdu_queue, local->queue_pool)) {
  143. queue = ampdu_queue;
  144. info->flags |= IEEE80211_TX_CTL_AMPDU;
  145. } else {
  146. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  147. }
  148. }
  149. rcu_read_unlock();
  150. }
  151. return queue;
  152. }
  153. int ieee80211_ht_agg_queue_add(struct ieee80211_local *local,
  154. struct sta_info *sta, u16 tid)
  155. {
  156. int i;
  157. /* XXX: currently broken due to cb/requeue use */
  158. return -EPERM;
  159. /* prepare the filter and save it for the SW queue
  160. * matching the received HW queue */
  161. if (!local->hw.ampdu_queues)
  162. return -EPERM;
  163. /* try to get a Qdisc from the pool */
  164. for (i = local->hw.queues; i < ieee80211_num_queues(&local->hw); i++)
  165. if (!test_and_set_bit(i, local->queue_pool)) {
  166. ieee80211_stop_queue(local_to_hw(local), i);
  167. sta->tid_to_tx_q[tid] = i;
  168. /* IF there are already pending packets
  169. * on this tid first we need to drain them
  170. * on the previous queue
  171. * since HT is strict in order */
  172. #ifdef CONFIG_MAC80211_HT_DEBUG
  173. if (net_ratelimit()) {
  174. DECLARE_MAC_BUF(mac);
  175. printk(KERN_DEBUG "allocated aggregation queue"
  176. " %d tid %d addr %s pool=0x%lX\n",
  177. i, tid, print_mac(mac, sta->sta.addr),
  178. local->queue_pool[0]);
  179. }
  180. #endif /* CONFIG_MAC80211_HT_DEBUG */
  181. return 0;
  182. }
  183. return -EAGAIN;
  184. }
  185. /**
  186. * the caller needs to hold netdev_get_tx_queue(local->mdev, X)->lock
  187. */
  188. void ieee80211_ht_agg_queue_remove(struct ieee80211_local *local,
  189. struct sta_info *sta, u16 tid,
  190. u8 requeue)
  191. {
  192. int agg_queue = sta->tid_to_tx_q[tid];
  193. struct ieee80211_hw *hw = &local->hw;
  194. /* return the qdisc to the pool */
  195. clear_bit(agg_queue, local->queue_pool);
  196. sta->tid_to_tx_q[tid] = ieee80211_num_queues(hw);
  197. if (requeue) {
  198. ieee80211_requeue(local, agg_queue);
  199. } else {
  200. struct netdev_queue *txq;
  201. spinlock_t *root_lock;
  202. struct Qdisc *q;
  203. txq = netdev_get_tx_queue(local->mdev, agg_queue);
  204. q = rcu_dereference(txq->qdisc);
  205. root_lock = qdisc_lock(q);
  206. spin_lock_bh(root_lock);
  207. qdisc_reset(q);
  208. spin_unlock_bh(root_lock);
  209. }
  210. }
  211. void ieee80211_requeue(struct ieee80211_local *local, int queue)
  212. {
  213. struct netdev_queue *txq = netdev_get_tx_queue(local->mdev, queue);
  214. struct sk_buff_head list;
  215. spinlock_t *root_lock;
  216. struct Qdisc *qdisc;
  217. u32 len;
  218. rcu_read_lock_bh();
  219. qdisc = rcu_dereference(txq->qdisc);
  220. if (!qdisc || !qdisc->dequeue)
  221. goto out_unlock;
  222. skb_queue_head_init(&list);
  223. root_lock = qdisc_root_lock(qdisc);
  224. spin_lock(root_lock);
  225. for (len = qdisc->q.qlen; len > 0; len--) {
  226. struct sk_buff *skb = qdisc->dequeue(qdisc);
  227. if (skb)
  228. __skb_queue_tail(&list, skb);
  229. }
  230. spin_unlock(root_lock);
  231. for (len = list.qlen; len > 0; len--) {
  232. struct sk_buff *skb = __skb_dequeue(&list);
  233. u16 new_queue;
  234. BUG_ON(!skb);
  235. new_queue = ieee80211_select_queue(local->mdev, skb);
  236. skb_set_queue_mapping(skb, new_queue);
  237. txq = netdev_get_tx_queue(local->mdev, new_queue);
  238. qdisc = rcu_dereference(txq->qdisc);
  239. root_lock = qdisc_root_lock(qdisc);
  240. spin_lock(root_lock);
  241. qdisc_enqueue_root(skb, qdisc);
  242. spin_unlock(root_lock);
  243. }
  244. out_unlock:
  245. rcu_read_unlock_bh();
  246. }