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 = mac80211_open_file_generic,
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         kref_init(&ref->kref);
00163         ref->local = local;
00164         ref->ops = ieee80211_rate_control_ops_get(name);
00165         if (!ref->ops)
00166                 goto fail_ops;
00167 
00168 #ifdef CONFIG_MAC80211_DEBUGFS
00169         debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
00170         local->debugfs.rcdir = debugfsdir;
00171         debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
00172 #endif
00173 
00174         ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
00175         if (!ref->priv)
00176                 goto fail_priv;
00177         return ref;
00178 
00179 fail_priv:
00180         ieee80211_rate_control_ops_put(ref->ops);
00181 fail_ops:
00182         kfree(ref);
00183 fail_ref:
00184         return NULL;
00185 }
00186 
00187 static void rate_control_release(struct kref *kref)
00188 {
00189         struct rate_control_ref *ctrl_ref;
00190 
00191         ctrl_ref = container_of(kref, struct rate_control_ref, kref);
00192         ctrl_ref->ops->free(ctrl_ref->priv);
00193 
00194 #ifdef CONFIG_MAC80211_DEBUGFS
00195         debugfs_remove_recursive(ctrl_ref->local->debugfs.rcdir);
00196         ctrl_ref->local->debugfs.rcdir = NULL;
00197 #endif
00198 
00199         ieee80211_rate_control_ops_put(ctrl_ref->ops);
00200         kfree(ctrl_ref);
00201 }
00202 
00203 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
00204 {
00205         struct sk_buff *skb = txrc->skb;
00206         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
00207         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
00208         __le16 fc;
00209 
00210         fc = hdr->frame_control;
00211 
00212         return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
00213                                IEEE80211_TX_CTL_USE_MINRATE)) ||
00214                 !ieee80211_is_data(fc);
00215 }
00216 
00217 static void rc_send_low_broadcast(s8 *idx, u32 basic_rates,
00218                                   struct ieee80211_supported_band *sband)
00219 {
00220         u8 i;
00221 
00222         if (basic_rates == 0)
00223                 return; /* assume basic rates unknown and accept rate */
00224         if (*idx < 0)
00225                 return;
00226         if (basic_rates & (1 << *idx))
00227                 return; /* selected rate is a basic rate */
00228 
00229         for (i = *idx + 1; i <= sband->n_bitrates; i++) {
00230                 if (basic_rates & (1 << i)) {
00231                         *idx = i;
00232                         return;
00233                 }
00234         }
00235 
00236         /* could not find a basic rate; use original selection */
00237 }
00238 
00239 static inline s8
00240 rate_lowest_non_cck_index(struct ieee80211_supported_band *sband,
00241                           struct ieee80211_sta *sta)
00242 {
00243         int i;
00244 
00245         for (i = 0; i < sband->n_bitrates; i++) {
00246                 struct ieee80211_rate *srate = &sband->bitrates[i];
00247                 if ((srate->bitrate == 10) || (srate->bitrate == 20) ||
00248                     (srate->bitrate == 55) || (srate->bitrate == 110))
00249                         continue;
00250 
00251                 if (rate_supported(sta, sband->band, i))
00252                         return i;
00253         }
00254 
00255         /* No matching rate found */
00256         return 0;
00257 }
00258 
00259 
00260 bool rate_control_send_low(struct ieee80211_sta *sta,
00261                            void *priv_sta,
00262                            struct ieee80211_tx_rate_control *txrc)
00263 {
00264         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
00265         struct ieee80211_supported_band *sband = txrc->sband;
00266         int mcast_rate;
00267 
00268         if (!sta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
00269                 if ((sband->band != IEEE80211_BAND_2GHZ) ||
00270                     !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
00271                         info->control.rates[0].idx =
00272                                 rate_lowest_index(txrc->sband, sta);
00273                 else
00274                         info->control.rates[0].idx =
00275                                 rate_lowest_non_cck_index(txrc->sband, sta);
00276                 info->control.rates[0].count =
00277                         (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
00278                         1 : txrc->hw->max_rate_tries;
00279                 if (!sta && txrc->bss) {
00280                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
00281                         if (mcast_rate > 0) {
00282                                 info->control.rates[0].idx = mcast_rate - 1;
00283                                 return true;
00284                         }
00285 
00286                         rc_send_low_broadcast(&info->control.rates[0].idx,
00287                                               txrc->bss_conf->basic_rates,
00288                                               sband);
00289                 }
00290                 return true;
00291         }
00292         return false;
00293 }
00294 EXPORT_SYMBOL(rate_control_send_low);
00295 
00296 static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
00297                                 int n_bitrates, u32 mask)
00298 {
00299         int j;
00300 
00301         /* See whether the selected rate or anything below it is allowed. */
00302         for (j = rate->idx; j >= 0; j--) {
00303                 if (mask & (1 << j)) {
00304                         /* Okay, found a suitable rate. Use it. */
00305                         rate->idx = j;
00306                         return;
00307                 }
00308         }
00309 
00310         /* Try to find a higher rate that would be allowed */
00311         for (j = rate->idx + 1; j < n_bitrates; j++) {
00312                 if (mask & (1 << j)) {
00313                         /* Okay, found a suitable rate. Use it. */
00314                         rate->idx = j;
00315                         return;
00316                 }
00317         }
00318 
00319         /*
00320          * Uh.. No suitable rate exists. This should not really happen with
00321          * sane TX rate mask configurations. However, should someone manage to
00322          * configure supported rates and TX rate mask in incompatible way,
00323          * allow the frame to be transmitted with whatever the rate control
00324          * selected.
00325          */
00326 }
00327 
00328 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
00329                            struct sta_info *sta,
00330                            struct ieee80211_tx_rate_control *txrc)
00331 {
00332         struct rate_control_ref *ref = sdata->local->rate_ctrl;
00333         void *priv_sta = NULL;
00334         struct ieee80211_sta *ista = NULL;
00335         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
00336         int i;
00337         u32 mask;
00338 
00339         if (sta) {
00340                 ista = &sta->sta;
00341                 priv_sta = sta->rate_ctrl_priv;
00342         }
00343 
00344         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
00345                 info->control.rates[i].idx = -1;
00346                 info->control.rates[i].flags = 0;
00347                 info->control.rates[i].count = 0;
00348         }
00349 
00350         if (sdata->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
00351                 return;
00352 
00353         ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
00354 
00355         /*
00356          * Try to enforce the rateidx mask the user wanted. skip this if the
00357          * default mask (allow all rates) is used to save some processing for
00358          * the common case.
00359          */
00360         mask = sdata->rc_rateidx_mask[info->band];
00361         if (mask != (1 << txrc->sband->n_bitrates) - 1) {
00362                 if (sta) {
00363                         /* Filter out rates that the STA does not support */
00364                         mask &= sta->sta.supp_rates[info->band];
00365                 }
00366                 /*
00367                  * Make sure the rate index selected for each TX rate is
00368                  * included in the configured mask and change the rate indexes
00369                  * if needed.
00370                  */
00371                 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
00372                         /* Skip invalid rates */
00373                         if (info->control.rates[i].idx < 0)
00374                                 break;
00375                         /* Rate masking supports only legacy rates for now */
00376                         if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS)
00377                                 continue;
00378                         rate_idx_match_mask(&info->control.rates[i],
00379                                             txrc->sband->n_bitrates, mask);
00380                 }
00381         }
00382 
00383         BUG_ON(info->control.rates[0].idx < 0);
00384 }
00385 
00386 struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
00387 {
00388         kref_get(&ref->kref);
00389         return ref;
00390 }
00391 
00392 void rate_control_put(struct rate_control_ref *ref)
00393 {
00394         kref_put(&ref->kref, rate_control_release);
00395 }
00396 
00397 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
00398                                  const char *name)
00399 {
00400         struct rate_control_ref *ref, *old;
00401 
00402         ASSERT_RTNL();
00403 
00404         if (local->open_count)
00405                 return -EBUSY;
00406 
00407         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
00408                 if (WARN_ON(!local->ops->set_rts_threshold))
00409                         return -EINVAL;
00410                 return 0;
00411         }
00412 
00413         ref = rate_control_alloc(name, local);
00414         if (!ref) {
00415                 wiphy_warn(local->hw.wiphy,
00416                            "Failed to select rate control algorithm\n");
00417                 return -ENOENT;
00418         }
00419 
00420         old = local->rate_ctrl;
00421         local->rate_ctrl = ref;
00422         if (old) {
00423                 rate_control_put(old);
00424                 sta_info_flush(local, NULL);
00425         }
00426 
00427         wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
00428                     ref->ops->name);
00429 
00430         return 0;
00431 }
00432 
00433 void rate_control_deinitialize(struct ieee80211_local *local)
00434 {
00435         struct rate_control_ref *ref;
00436 
00437         ref = local->rate_ctrl;
00438 
00439         if (!ref)
00440                 return;
00441 
00442         local->rate_ctrl = NULL;
00443         rate_control_put(ref);
00444 }
00445 


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