rate.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2002-2005, Instant802 Networks, Inc.
00003  * Copyright 2005-2006, Devicescape Software, Inc.
00004  * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  */
00010 
00011 #include <linux/kernel.h>
00012 #include <linux/rtnetlink.h>
00013 #include <linux/slab.h>
00014 #include <linux/module.h>
00015 #include "rate.h"
00016 #include "ieee80211_i.h"
00017 #include "debugfs.h"
00018 
00019 struct rate_control_alg {
00020         struct list_head list;
00021         struct rate_control_ops *ops;
00022 };
00023 
00024 static LIST_HEAD(rate_ctrl_algs);
00025 static DEFINE_MUTEX(rate_ctrl_mutex);
00026 
00027 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
00028 module_param(ieee80211_default_rc_algo, charp, 0644);
00029 MODULE_PARM_DESC(ieee80211_default_rc_algo,
00030                  "Default rate control algorithm for mac80211 to use");
00031 
00032 int ieee80211_rate_control_register(struct rate_control_ops *ops)
00033 {
00034         struct rate_control_alg *alg;
00035 
00036         if (!ops->name)
00037                 return -EINVAL;
00038 
00039         mutex_lock(&rate_ctrl_mutex);
00040         list_for_each_entry(alg, &rate_ctrl_algs, list) {
00041                 if (!strcmp(alg->ops->name, ops->name)) {
00042                         /* don't register an algorithm twice */
00043                         WARN_ON(1);
00044                         mutex_unlock(&rate_ctrl_mutex);
00045                         return -EALREADY;
00046                 }
00047         }
00048 
00049         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
00050         if (alg == NULL) {
00051                 mutex_unlock(&rate_ctrl_mutex);
00052                 return -ENOMEM;
00053         }
00054         alg->ops = ops;
00055 
00056         list_add_tail(&alg->list, &rate_ctrl_algs);
00057         mutex_unlock(&rate_ctrl_mutex);
00058 
00059         return 0;
00060 }
00061 EXPORT_SYMBOL(ieee80211_rate_control_register);
00062 
00063 void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
00064 {
00065         struct rate_control_alg *alg;
00066 
00067         mutex_lock(&rate_ctrl_mutex);
00068         list_for_each_entry(alg, &rate_ctrl_algs, list) {
00069                 if (alg->ops == ops) {
00070                         list_del(&alg->list);
00071                         kfree(alg);
00072                         break;
00073                 }
00074         }
00075         mutex_unlock(&rate_ctrl_mutex);
00076 }
00077 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
00078 
00079 static struct rate_control_ops *
00080 ieee80211_try_rate_control_ops_get(const char *name)
00081 {
00082         struct rate_control_alg *alg;
00083         struct rate_control_ops *ops = NULL;
00084 
00085         if (!name)
00086                 return NULL;
00087 
00088         mutex_lock(&rate_ctrl_mutex);
00089         list_for_each_entry(alg, &rate_ctrl_algs, list) {
00090                 if (!strcmp(alg->ops->name, name))
00091                         if (try_module_get(alg->ops->module)) {
00092                                 ops = alg->ops;
00093                                 break;
00094                         }
00095         }
00096         mutex_unlock(&rate_ctrl_mutex);
00097         return ops;
00098 }
00099 
00100 /* Get the rate control algorithm. */
00101 static struct rate_control_ops *
00102 ieee80211_rate_control_ops_get(const char *name)
00103 {
00104         struct rate_control_ops *ops;
00105         const char *alg_name;
00106 
00107         kparam_block_sysfs_write(ieee80211_default_rc_algo);
00108         if (!name)
00109                 alg_name = ieee80211_default_rc_algo;
00110         else
00111                 alg_name = name;
00112 
00113         ops = ieee80211_try_rate_control_ops_get(alg_name);
00114         if (!ops) {
00115                 request_module("rc80211_%s", alg_name);
00116                 ops = ieee80211_try_rate_control_ops_get(alg_name);
00117         }
00118         if (!ops && name)
00119                 /* try default if specific alg requested but not found */
00120                 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
00121 
00122         /* try built-in one if specific alg requested but not found */
00123         if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
00124                 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
00125         kparam_unblock_sysfs_write(ieee80211_default_rc_algo);
00126 
00127         return ops;
00128 }
00129 
00130 static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
00131 {
00132         module_put(ops->module);
00133 }
00134 
00135 #ifdef CONFIG_MAC80211_DEBUGFS
00136 static ssize_t rcname_read(struct file *file, char __user *userbuf,
00137                            size_t count, loff_t *ppos)
00138 {
00139         struct rate_control_ref *ref = file->private_data;
00140         int len = strlen(ref->ops->name);
00141 
00142         return simple_read_from_buffer(userbuf, count, ppos,
00143                                        ref->ops->name, len);
00144 }
00145 
00146 static const struct file_operations rcname_ops = {
00147         .read = rcname_read,
00148         .open = simple_open,
00149         .llseek = default_llseek,
00150 };
00151 #endif
00152 
00153 static struct rate_control_ref *rate_control_alloc(const char *name,
00154                                             struct ieee80211_local *local)
00155 {
00156         struct dentry *debugfsdir = NULL;
00157         struct rate_control_ref *ref;
00158 
00159         ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
00160         if (!ref)
00161                 goto fail_ref;
00162         ref->local = local;
00163         ref->ops = ieee80211_rate_control_ops_get(name);
00164         if (!ref->ops)
00165                 goto fail_ops;
00166 
00167 #ifdef CONFIG_MAC80211_DEBUGFS
00168         debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
00169         local->debugfs.rcdir = debugfsdir;
00170         debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
00171 #endif
00172 
00173         ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
00174         if (!ref->priv)
00175                 goto fail_priv;
00176         return ref;
00177 
00178 fail_priv:
00179         ieee80211_rate_control_ops_put(ref->ops);
00180 fail_ops:
00181         kfree(ref);
00182 fail_ref:
00183         return NULL;
00184 }
00185 
00186 static void rate_control_free(struct rate_control_ref *ctrl_ref)
00187 {
00188         ctrl_ref->ops->free(ctrl_ref->priv);
00189 
00190 #ifdef CONFIG_MAC80211_DEBUGFS
00191         debugfs_remove_recursive(ctrl_ref->local->debugfs.rcdir);
00192         ctrl_ref->local->debugfs.rcdir = NULL;
00193 #endif
00194 
00195         ieee80211_rate_control_ops_put(ctrl_ref->ops);
00196         kfree(ctrl_ref);
00197 }
00198 
00199 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
00200 {
00201         struct sk_buff *skb = txrc->skb;
00202         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00203         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
00204         __le16 fc;
00205 
00206         fc = hdr->frame_control;
00207 
00208         return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
00209                                IEEE80211_TX_CTL_USE_MINRATE)) ||
00210                 !ieee80211_is_data(fc);
00211 }
00212 
00213 static void rc_send_low_broadcast(s8 *idx, u32 basic_rates,
00214                                   struct ieee80211_supported_band *sband)
00215 {
00216         u8 i;
00217 
00218         if (basic_rates == 0)
00219                 return; /* assume basic rates unknown and accept rate */
00220         if (*idx < 0)
00221                 return;
00222         if (basic_rates & (1 << *idx))
00223                 return; /* selected rate is a basic rate */
00224 
00225         for (i = *idx + 1; i <= sband->n_bitrates; i++) {
00226                 if (basic_rates & (1 << i)) {
00227                         *idx = i;
00228                         return;
00229                 }
00230         }
00231 
00232         /* could not find a basic rate; use original selection */
00233 }
00234 
00235 static inline s8
00236 rate_lowest_non_cck_index(struct ieee80211_supported_band *sband,
00237                           struct ieee80211_sta *sta)
00238 {
00239         int i;
00240 
00241         for (i = 0; i < sband->n_bitrates; i++) {
00242                 struct ieee80211_rate *srate = &sband->bitrates[i];
00243                 if ((srate->bitrate == 10) || (srate->bitrate == 20) ||
00244                     (srate->bitrate == 55) || (srate->bitrate == 110))
00245                         continue;
00246 
00247                 if (rate_supported(sta, sband->band, i))
00248                         return i;
00249         }
00250 
00251         /* No matching rate found */
00252         return 0;
00253 }
00254 
00255 
00256 bool rate_control_send_low(struct ieee80211_sta *sta,
00257                            void *priv_sta,
00258                            struct ieee80211_tx_rate_control *txrc)
00259 {
00260         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
00261         struct ieee80211_supported_band *sband = txrc->sband;
00262         int mcast_rate;
00263 
00264         if (!sta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
00265                 if ((sband->band != IEEE80211_BAND_2GHZ) ||
00266                     !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
00267                         info->control.rates[0].idx =
00268                                 rate_lowest_index(txrc->sband, sta);
00269                 else
00270                         info->control.rates[0].idx =
00271                                 rate_lowest_non_cck_index(txrc->sband, sta);
00272                 info->control.rates[0].count =
00273                         (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
00274                         1 : txrc->hw->max_rate_tries;
00275                 if (!sta && txrc->bss) {
00276                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
00277                         if (mcast_rate > 0) {
00278                                 info->control.rates[0].idx = mcast_rate - 1;
00279                                 return true;
00280                         }
00281 
00282                         rc_send_low_broadcast(&info->control.rates[0].idx,
00283                                               txrc->bss_conf->basic_rates,
00284                                               sband);
00285                 }
00286                 return true;
00287         }
00288         return false;
00289 }
00290 EXPORT_SYMBOL(rate_control_send_low);
00291 
00292 static bool rate_idx_match_legacy_mask(struct ieee80211_tx_rate *rate,
00293                                        int n_bitrates, u32 mask)
00294 {
00295         int j;
00296 
00297         /* See whether the selected rate or anything below it is allowed. */
00298         for (j = rate->idx; j >= 0; j--) {
00299                 if (mask & (1 << j)) {
00300                         /* Okay, found a suitable rate. Use it. */
00301                         rate->idx = j;
00302                         return true;
00303                 }
00304         }
00305 
00306         /* Try to find a higher rate that would be allowed */
00307         for (j = rate->idx + 1; j < n_bitrates; j++) {
00308                 if (mask & (1 << j)) {
00309                         /* Okay, found a suitable rate. Use it. */
00310                         rate->idx = j;
00311                         return true;
00312                 }
00313         }
00314         return false;
00315 }
00316 
00317 static bool rate_idx_match_mcs_mask(struct ieee80211_tx_rate *rate,
00318                                     u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
00319 {
00320         int i, j;
00321         int ridx, rbit;
00322 
00323         ridx = rate->idx / 8;
00324         rbit = rate->idx % 8;
00325 
00326         /* sanity check */
00327         if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
00328                 return false;
00329 
00330         /* See whether the selected rate or anything below it is allowed. */
00331         for (i = ridx; i >= 0; i--) {
00332                 for (j = rbit; j >= 0; j--)
00333                         if (mcs_mask[i] & BIT(j)) {
00334                                 rate->idx = i * 8 + j;
00335                                 return true;
00336                         }
00337                 rbit = 7;
00338         }
00339 
00340         /* Try to find a higher rate that would be allowed */
00341         ridx = (rate->idx + 1) / 8;
00342         rbit = (rate->idx + 1) % 8;
00343 
00344         for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
00345                 for (j = rbit; j < 8; j++)
00346                         if (mcs_mask[i] & BIT(j)) {
00347                                 rate->idx = i * 8 + j;
00348                                 return true;
00349                         }
00350                 rbit = 0;
00351         }
00352         return false;
00353 }
00354 
00355 
00356 
00357 static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
00358                                 struct ieee80211_tx_rate_control *txrc,
00359                                 u32 mask,
00360                                 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
00361 {
00362         struct ieee80211_tx_rate alt_rate;
00363 
00364         /* handle HT rates */
00365         if (rate->flags & IEEE80211_TX_RC_MCS) {
00366                 if (rate_idx_match_mcs_mask(rate, mcs_mask))
00367                         return;
00368 
00369                 /* also try the legacy rates. */
00370                 alt_rate.idx = 0;
00371                 /* keep protection flags */
00372                 alt_rate.flags = rate->flags &
00373                                  (IEEE80211_TX_RC_USE_RTS_CTS |
00374                                   IEEE80211_TX_RC_USE_CTS_PROTECT |
00375                                   IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
00376                 alt_rate.count = rate->count;
00377                 if (rate_idx_match_legacy_mask(&alt_rate,
00378                                                txrc->sband->n_bitrates,
00379                                                mask)) {
00380                         *rate = alt_rate;
00381                         return;
00382                 }
00383         } else {
00384                 struct sk_buff *skb = txrc->skb;
00385                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00386                 __le16 fc;
00387 
00388                 /* handle legacy rates */
00389                 if (rate_idx_match_legacy_mask(rate, txrc->sband->n_bitrates,
00390                                                mask))
00391                         return;
00392 
00393                 /* if HT BSS, and we handle a data frame, also try HT rates */
00394                 if (txrc->bss_conf->channel_type == NL80211_CHAN_NO_HT)
00395                         return;
00396 
00397                 fc = hdr->frame_control;
00398                 if (!ieee80211_is_data(fc))
00399                         return;
00400 
00401                 alt_rate.idx = 0;
00402                 /* keep protection flags */
00403                 alt_rate.flags = rate->flags &
00404                                  (IEEE80211_TX_RC_USE_RTS_CTS |
00405                                   IEEE80211_TX_RC_USE_CTS_PROTECT |
00406                                   IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
00407                 alt_rate.count = rate->count;
00408 
00409                 alt_rate.flags |= IEEE80211_TX_RC_MCS;
00410 
00411                 if ((txrc->bss_conf->channel_type == NL80211_CHAN_HT40MINUS) ||
00412                     (txrc->bss_conf->channel_type == NL80211_CHAN_HT40PLUS))
00413                         alt_rate.flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
00414 
00415                 if (rate_idx_match_mcs_mask(&alt_rate, mcs_mask)) {
00416                         *rate = alt_rate;
00417                         return;
00418                 }
00419         }
00420 
00421         /*
00422          * Uh.. No suitable rate exists. This should not really happen with
00423          * sane TX rate mask configurations. However, should someone manage to
00424          * configure supported rates and TX rate mask in incompatible way,
00425          * allow the frame to be transmitted with whatever the rate control
00426          * selected.
00427          */
00428 }
00429 
00430 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
00431                            struct sta_info *sta,
00432                            struct ieee80211_tx_rate_control *txrc)
00433 {
00434         struct rate_control_ref *ref = sdata->local->rate_ctrl;
00435         void *priv_sta = NULL;
00436         struct ieee80211_sta *ista = NULL;
00437         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
00438         int i;
00439         u32 mask;
00440         u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
00441 
00442         if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
00443                 ista = &sta->sta;
00444                 priv_sta = sta->rate_ctrl_priv;
00445         }
00446 
00447         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
00448                 info->control.rates[i].idx = -1;
00449                 info->control.rates[i].flags = 0;
00450                 info->control.rates[i].count = 0;
00451         }
00452 
00453         if (sdata->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
00454                 return;
00455 
00456         ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
00457 
00458         /*
00459          * Try to enforce the rateidx mask the user wanted. skip this if the
00460          * default mask (allow all rates) is used to save some processing for
00461          * the common case.
00462          */
00463         mask = sdata->rc_rateidx_mask[info->band];
00464         memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
00465                sizeof(mcs_mask));
00466         if (mask != (1 << txrc->sband->n_bitrates) - 1) {
00467                 if (sta) {
00468                         /* Filter out rates that the STA does not support */
00469                         mask &= sta->sta.supp_rates[info->band];
00470                         for (i = 0; i < sizeof(mcs_mask); i++)
00471                                 mcs_mask[i] &= sta->sta.ht_cap.mcs.rx_mask[i];
00472                 }
00473                 /*
00474                  * Make sure the rate index selected for each TX rate is
00475                  * included in the configured mask and change the rate indexes
00476                  * if needed.
00477                  */
00478                 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
00479                         /* Skip invalid rates */
00480                         if (info->control.rates[i].idx < 0)
00481                                 break;
00482                         rate_idx_match_mask(&info->control.rates[i], txrc,
00483                                             mask, mcs_mask);
00484                 }
00485         }
00486 
00487         BUG_ON(info->control.rates[0].idx < 0);
00488 }
00489 
00490 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
00491                                  const char *name)
00492 {
00493         struct rate_control_ref *ref;
00494 
00495         ASSERT_RTNL();
00496 
00497         if (local->open_count)
00498                 return -EBUSY;
00499 
00500         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
00501                 if (WARN_ON(!local->ops->set_rts_threshold))
00502                         return -EINVAL;
00503                 return 0;
00504         }
00505 
00506         ref = rate_control_alloc(name, local);
00507         if (!ref) {
00508                 wiphy_warn(local->hw.wiphy,
00509                            "Failed to select rate control algorithm\n");
00510                 return -ENOENT;
00511         }
00512 
00513         WARN_ON(local->rate_ctrl);
00514         local->rate_ctrl = ref;
00515 
00516         wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
00517                     ref->ops->name);
00518 
00519         return 0;
00520 }
00521 
00522 void rate_control_deinitialize(struct ieee80211_local *local)
00523 {
00524         struct rate_control_ref *ref;
00525 
00526         ref = local->rate_ctrl;
00527 
00528         if (!ref)
00529                 return;
00530 
00531         local->rate_ctrl = NULL;
00532         rate_control_free(ref);
00533 }
00534 


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:55