diff mbox series

[v2] wifi: mac80211: ethtool: use best available link for default stats.

Message ID 20231020152353.3705759-1-greearb@candelatech.com
State New
Headers show
Series [v2] wifi: mac80211: ethtool: use best available link for default stats. | expand

Commit Message

Ben Greear Oct. 20, 2023, 3:23 p.m. UTC
From: Ben Greear <greearb@candelatech.com>

Best link is the link with the highest state (ie, associated)
and if multiple links have same state, the highest
frequency wins.

This makes current ethtool stats more useful.  Per-link
ethtool stats can be added later.

Signed-off-by: Ben Greear <greearb@candelatech.com>
---

v2:  Use best link, not just first one.

 net/mac80211/ethtool.c  |  7 ++++--
 net/mac80211/link.c     | 51 +++++++++++++++++++++++++++++++++++++++++
 net/mac80211/sta_info.h |  4 ++++
 3 files changed, 60 insertions(+), 2 deletions(-)

Comments

kernel test robot Oct. 24, 2023, 4:57 p.m. UTC | #1
Hi,

kernel test robot noticed the following build warnings:

[auto build test WARNING on wireless-next/main]
[also build test WARNING on wireless/main linus/master v6.6-rc7 next-20231024]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/greearb-candelatech-com/wifi-mac80211-ethtool-use-best-available-link-for-default-stats/20231020-232523
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git main
patch link:    https://lore.kernel.org/r/20231020152353.3705759-1-greearb%40candelatech.com
patch subject: [PATCH v2] wifi: mac80211: ethtool: use best available link for default stats.
config: mips-bmips_stb_defconfig (https://download.01.org/0day-ci/archive/20231025/202310250024.SZSqLsNe-lkp@intel.com/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/202310250024.SZSqLsNe-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202310250024.SZSqLsNe-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> net/mac80211/link.c:143:7: warning: variable 'sta1' is uninitialized when used here [-Wuninitialized]
     143 |                 if (sta1->sta_state > best_sta->sta_state) {
         |                     ^~~~
   net/mac80211/link.c:131:24: note: initialize the variable 'sta1' to silence this warning
     131 |                 struct sta_info *sta1;
         |                                      ^
         |                                       = NULL
   1 warning generated.


vim +/sta1 +143 net/mac80211/link.c

   117	
   118	/* For cases where we need a link for stats and such, and just want
   119	 * a 'good' one.
   120	 */
   121	struct sta_info *
   122	ieee80211_find_best_sta_link(struct ieee80211_sub_if_data *sdata,
   123				     struct ieee80211_link_data **link)
   124	{
   125		struct ieee80211_link_data *best_link = NULL;
   126		struct sta_info *best_sta = NULL;
   127		int i;
   128	
   129		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
   130			struct ieee80211_link_data *link1;
   131			struct sta_info *sta1;
   132			bool sta1_better = false;
   133	
   134			link1 = sdata_dereference(sdata->link[i], sdata);
   135			if (!link1)
   136				continue;
   137			if (!best_link) {
   138				best_link = link1;
   139				best_sta = sta_info_get_bss(sdata, best_link->u.mgd.bssid);
   140				continue;
   141			}
   142			/* we have two potential best links, find one we like best. */
 > 143			if (sta1->sta_state > best_sta->sta_state) {
   144				sta1_better = true;
   145			} else if (sta1->sta_state == best_sta->sta_state) {
   146				u32 freq_best = 0;
   147				u32 freq1 = 0;
   148	
   149				if (best_link->conf->chandef.chan)
   150					freq_best = best_link->conf->chandef.chan->center_freq;
   151				if (link1->conf->chandef.chan)
   152					freq1 = link1->conf->chandef.chan->center_freq;
   153				if (freq1 > freq_best)
   154					sta1_better = true;
   155			}
   156	
   157			if (sta1_better) {
   158				best_sta = sta1;
   159				best_link = link1;
   160			}
   161		}
   162	
   163		*link = best_link;
   164		if (best_link)
   165			return sta_info_get_bss(sdata, best_link->u.mgd.bssid);
   166		return sta_info_get_bss(sdata, sdata->deflink.u.mgd.bssid);
   167	}
   168
Ben Greear Oct. 24, 2023, 6:31 p.m. UTC | #2
On 10/20/23 08:23, greearb@candelatech.com wrote:
> From: Ben Greear <greearb@candelatech.com>
> 
> Best link is the link with the highest state (ie, associated)
> and if multiple links have same state, the highest
> frequency wins.
> 
> This makes current ethtool stats more useful.  Per-link
> ethtool stats can be added later.

The kernel robot is correct, this patch is buggy and would crash with a 2+ link
situation.  I'll send v3, though I don't have an AP that actually advertises > 1 link
so difficult to test well...

Thanks,
Ben
diff mbox series

Patch

diff --git a/net/mac80211/ethtool.c b/net/mac80211/ethtool.c
index 99f6174a9d69..f51b0f97a4a4 100644
--- a/net/mac80211/ethtool.c
+++ b/net/mac80211/ethtool.c
@@ -83,6 +83,7 @@  static void ieee80211_get_stats(struct net_device *dev,
 	struct ieee80211_local *local = sdata->local;
 	struct station_info sinfo;
 	struct survey_info survey;
+	struct ieee80211_link_data *link = NULL;
 	int i, q;
 #define STA_STATS_SURVEY_LEN 7
 
@@ -112,7 +113,7 @@  static void ieee80211_get_stats(struct net_device *dev,
 	wiphy_lock(local->hw.wiphy);
 
 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
-		sta = sta_info_get_bss(sdata, sdata->deflink.u.mgd.bssid);
+		sta = ieee80211_find_best_sta_link(sdata, &link);
 
 		if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
 			goto do_survey;
@@ -158,7 +159,9 @@  static void ieee80211_get_stats(struct net_device *dev,
 
 	rcu_read_lock();
 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
-	if (chanctx_conf)
+	if (link)
+		channel = link->conf->chandef.chan;
+	else if (chanctx_conf)
 		channel = chanctx_conf->def.chan;
 	else
 		channel = NULL;
diff --git a/net/mac80211/link.c b/net/mac80211/link.c
index 2a78374f6f04..bf48eec76d93 100644
--- a/net/mac80211/link.c
+++ b/net/mac80211/link.c
@@ -115,6 +115,57 @@  static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
 		kfree(links[link_id]);
 }
 
+/* For cases where we need a link for stats and such, and just want
+ * a 'good' one.
+ */
+struct sta_info *
+ieee80211_find_best_sta_link(struct ieee80211_sub_if_data *sdata,
+			     struct ieee80211_link_data **link)
+{
+	struct ieee80211_link_data *best_link = NULL;
+	struct sta_info *best_sta = NULL;
+	int i;
+
+	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
+		struct ieee80211_link_data *link1;
+		struct sta_info *sta1;
+		bool sta1_better = false;
+
+		link1 = sdata_dereference(sdata->link[i], sdata);
+		if (!link1)
+			continue;
+		if (!best_link) {
+			best_link = link1;
+			best_sta = sta_info_get_bss(sdata, best_link->u.mgd.bssid);
+			continue;
+		}
+		/* we have two potential best links, find one we like best. */
+		if (sta1->sta_state > best_sta->sta_state) {
+			sta1_better = true;
+		} else if (sta1->sta_state == best_sta->sta_state) {
+			u32 freq_best = 0;
+			u32 freq1 = 0;
+
+			if (best_link->conf->chandef.chan)
+				freq_best = best_link->conf->chandef.chan->center_freq;
+			if (link1->conf->chandef.chan)
+				freq1 = link1->conf->chandef.chan->center_freq;
+			if (freq1 > freq_best)
+				sta1_better = true;
+		}
+
+		if (sta1_better) {
+			best_sta = sta1;
+			best_link = link1;
+		}
+	}
+
+	*link = best_link;
+	if (best_link)
+		return sta_info_get_bss(sdata, best_link->u.mgd.bssid);
+	return sta_info_get_bss(sdata, sdata->deflink.u.mgd.bssid);
+}
+
 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
 {
 	unsigned int i, j;
diff --git a/net/mac80211/sta_info.h b/net/mac80211/sta_info.h
index 7acf2223e47a..c57380b622e6 100644
--- a/net/mac80211/sta_info.h
+++ b/net/mac80211/sta_info.h
@@ -929,6 +929,10 @@  void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
 
 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links);
 
+struct sta_info *
+ieee80211_find_best_sta_link(struct ieee80211_sub_if_data *sdata,
+			     struct ieee80211_link_data **link);
+
 enum sta_stats_type {
 	STA_STATS_RATE_TYPE_INVALID = 0,
 	STA_STATS_RATE_TYPE_LEGACY,