Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002-2005, Instant802 Networks, Inc. |
| 3 | * Copyright 2005-2006, Devicescape Software, Inc. |
| 4 | * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 12 | #include <linux/rtnetlink.h> |
Johannes Berg | 2c8dccc | 2008-04-08 15:14:40 -0400 | [diff] [blame] | 13 | #include "rate.h" |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 14 | #include "ieee80211_i.h" |
| 15 | |
| 16 | struct rate_control_alg { |
| 17 | struct list_head list; |
| 18 | struct rate_control_ops *ops; |
| 19 | }; |
| 20 | |
| 21 | static LIST_HEAD(rate_ctrl_algs); |
| 22 | static DEFINE_MUTEX(rate_ctrl_mutex); |
| 23 | |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 24 | static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT; |
| 25 | module_param(ieee80211_default_rc_algo, charp, 0644); |
| 26 | MODULE_PARM_DESC(ieee80211_default_rc_algo, |
| 27 | "Default rate control algorithm for mac80211 to use"); |
| 28 | |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 29 | int ieee80211_rate_control_register(struct rate_control_ops *ops) |
| 30 | { |
| 31 | struct rate_control_alg *alg; |
| 32 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 33 | if (!ops->name) |
| 34 | return -EINVAL; |
| 35 | |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 36 | mutex_lock(&rate_ctrl_mutex); |
| 37 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
| 38 | if (!strcmp(alg->ops->name, ops->name)) { |
| 39 | /* don't register an algorithm twice */ |
| 40 | WARN_ON(1); |
Cyrill Gorcunov | b808ab1 | 2007-12-13 15:52:11 -0800 | [diff] [blame] | 41 | mutex_unlock(&rate_ctrl_mutex); |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 42 | return -EALREADY; |
| 43 | } |
| 44 | } |
| 45 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 46 | alg = kzalloc(sizeof(*alg), GFP_KERNEL); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 47 | if (alg == NULL) { |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 48 | mutex_unlock(&rate_ctrl_mutex); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 49 | return -ENOMEM; |
| 50 | } |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 51 | alg->ops = ops; |
| 52 | |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 53 | list_add_tail(&alg->list, &rate_ctrl_algs); |
| 54 | mutex_unlock(&rate_ctrl_mutex); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | EXPORT_SYMBOL(ieee80211_rate_control_register); |
| 59 | |
| 60 | void ieee80211_rate_control_unregister(struct rate_control_ops *ops) |
| 61 | { |
| 62 | struct rate_control_alg *alg; |
| 63 | |
| 64 | mutex_lock(&rate_ctrl_mutex); |
| 65 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
| 66 | if (alg->ops == ops) { |
| 67 | list_del(&alg->list); |
Cyrill Gorcunov | 20880e8 | 2007-12-13 16:17:03 -0800 | [diff] [blame] | 68 | kfree(alg); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 69 | break; |
| 70 | } |
| 71 | } |
| 72 | mutex_unlock(&rate_ctrl_mutex); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL(ieee80211_rate_control_unregister); |
| 75 | |
| 76 | static struct rate_control_ops * |
| 77 | ieee80211_try_rate_control_ops_get(const char *name) |
| 78 | { |
| 79 | struct rate_control_alg *alg; |
| 80 | struct rate_control_ops *ops = NULL; |
| 81 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 82 | if (!name) |
| 83 | return NULL; |
| 84 | |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 85 | mutex_lock(&rate_ctrl_mutex); |
| 86 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 87 | if (!strcmp(alg->ops->name, name)) |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 88 | if (try_module_get(alg->ops->module)) { |
| 89 | ops = alg->ops; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | mutex_unlock(&rate_ctrl_mutex); |
| 94 | return ops; |
| 95 | } |
| 96 | |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 97 | /* Get the rate control algorithm. */ |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 98 | static struct rate_control_ops * |
| 99 | ieee80211_rate_control_ops_get(const char *name) |
| 100 | { |
| 101 | struct rate_control_ops *ops; |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 102 | const char *alg_name; |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 103 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 104 | if (!name) |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 105 | alg_name = ieee80211_default_rc_algo; |
| 106 | else |
| 107 | alg_name = name; |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 108 | |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 109 | ops = ieee80211_try_rate_control_ops_get(alg_name); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 110 | if (!ops) { |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 111 | request_module("rc80211_%s", alg_name); |
| 112 | ops = ieee80211_try_rate_control_ops_get(alg_name); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 113 | } |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 114 | if (!ops && name) |
| 115 | /* try default if specific alg requested but not found */ |
| 116 | ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo); |
| 117 | |
Johannes Berg | 4b47589 | 2008-01-02 15:17:03 +0100 | [diff] [blame] | 118 | /* try built-in one if specific alg requested but not found */ |
| 119 | if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT)) |
| 120 | ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT); |
| 121 | |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 122 | return ops; |
| 123 | } |
| 124 | |
| 125 | static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops) |
| 126 | { |
| 127 | module_put(ops->module); |
| 128 | } |
| 129 | |
| 130 | struct rate_control_ref *rate_control_alloc(const char *name, |
| 131 | struct ieee80211_local *local) |
| 132 | { |
| 133 | struct rate_control_ref *ref; |
| 134 | |
| 135 | ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL); |
| 136 | if (!ref) |
| 137 | goto fail_ref; |
| 138 | kref_init(&ref->kref); |
| 139 | ref->ops = ieee80211_rate_control_ops_get(name); |
| 140 | if (!ref->ops) |
| 141 | goto fail_ops; |
| 142 | ref->priv = ref->ops->alloc(local); |
| 143 | if (!ref->priv) |
| 144 | goto fail_priv; |
| 145 | return ref; |
| 146 | |
| 147 | fail_priv: |
| 148 | ieee80211_rate_control_ops_put(ref->ops); |
| 149 | fail_ops: |
| 150 | kfree(ref); |
| 151 | fail_ref: |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | static void rate_control_release(struct kref *kref) |
| 156 | { |
| 157 | struct rate_control_ref *ctrl_ref; |
| 158 | |
| 159 | ctrl_ref = container_of(kref, struct rate_control_ref, kref); |
| 160 | ctrl_ref->ops->free(ctrl_ref->priv); |
| 161 | ieee80211_rate_control_ops_put(ctrl_ref->ops); |
| 162 | kfree(ctrl_ref); |
| 163 | } |
| 164 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 165 | void rate_control_get_rate(struct net_device *dev, |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 166 | struct ieee80211_supported_band *sband, |
| 167 | struct sk_buff *skb, |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 168 | struct rate_selection *sel) |
| 169 | { |
| 170 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 171 | struct rate_control_ref *ref = local->rate_ctrl; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 172 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 173 | struct sta_info *sta; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 174 | int i; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 175 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 176 | rcu_read_lock(); |
| 177 | sta = sta_info_get(local, hdr->addr1); |
| 178 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 179 | memset(sel, 0, sizeof(struct rate_selection)); |
| 180 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 181 | ref->ops->get_rate(ref->priv, dev, sband, skb, sel); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 182 | |
| 183 | /* Select a non-ERP backup rate. */ |
| 184 | if (!sel->nonerp) { |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 185 | for (i = 0; i < sband->n_bitrates; i++) { |
| 186 | struct ieee80211_rate *rate = &sband->bitrates[i]; |
| 187 | if (sel->rate->bitrate < rate->bitrate) |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 188 | break; |
| 189 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 190 | if (rate_supported(sta, sband->band, i) && |
| 191 | !(rate->flags & IEEE80211_RATE_ERP_G)) |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 192 | sel->nonerp = rate; |
| 193 | } |
| 194 | } |
| 195 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 196 | rcu_read_unlock(); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 199 | struct rate_control_ref *rate_control_get(struct rate_control_ref *ref) |
| 200 | { |
| 201 | kref_get(&ref->kref); |
| 202 | return ref; |
| 203 | } |
| 204 | |
| 205 | void rate_control_put(struct rate_control_ref *ref) |
| 206 | { |
| 207 | kref_put(&ref->kref, rate_control_release); |
| 208 | } |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 209 | |
| 210 | int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, |
| 211 | const char *name) |
| 212 | { |
| 213 | struct rate_control_ref *ref, *old; |
| 214 | |
| 215 | ASSERT_RTNL(); |
Johannes Berg | f9d540e | 2007-09-28 14:02:09 +0200 | [diff] [blame] | 216 | if (local->open_count || netif_running(local->mdev)) |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 217 | return -EBUSY; |
| 218 | |
| 219 | ref = rate_control_alloc(name, local); |
| 220 | if (!ref) { |
| 221 | printk(KERN_WARNING "%s: Failed to select rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 222 | "algorithm\n", wiphy_name(local->hw.wiphy)); |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 223 | return -ENOENT; |
| 224 | } |
| 225 | |
| 226 | old = local->rate_ctrl; |
| 227 | local->rate_ctrl = ref; |
| 228 | if (old) { |
| 229 | rate_control_put(old); |
| 230 | sta_info_flush(local, NULL); |
| 231 | } |
| 232 | |
| 233 | printk(KERN_DEBUG "%s: Selected rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 234 | "algorithm '%s'\n", wiphy_name(local->hw.wiphy), |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 235 | ref->ops->name); |
| 236 | |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | void rate_control_deinitialize(struct ieee80211_local *local) |
| 242 | { |
| 243 | struct rate_control_ref *ref; |
| 244 | |
| 245 | ref = local->rate_ctrl; |
| 246 | local->rate_ctrl = NULL; |
| 247 | rate_control_put(ref); |
| 248 | } |
Stefano Brivio | c21b39a | 2007-12-19 01:26:16 +0100 | [diff] [blame] | 249 | |