2 * Copyright (c) 2012, Adrian Chadd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/types.h>
36 #include <sys/sockio.h>
37 #include <sys/socket.h>
38 #include <net/ethernet.h>
40 #include <net/if_media.h>
54 #include "net80211/ieee80211_ioctl.h"
55 #include "net80211/ieee80211_radiotap.h"
56 #include "if_athioctl.h"
57 #include "if_athrate.h"
59 #include "ath_rate/sample/sample.h"
61 static int do_loop = 0;
64 * This needs to be big enough to fit the two TLVs, the rate table
65 * and the rate statistics table for a single node.
67 #define STATS_BUF_SIZE 8192
69 #define PRINTMSG(...) do { \
71 printf(__VA_ARGS__); \
73 printw(__VA_ARGS__); \
76 #define PRINTATTR_ON(_x) do { \
82 #define PRINTATTR_OFF(_x) do { \
87 struct ath_ratestats {
89 struct ath_rateioctl re;
93 dot11rate(struct ath_rateioctl_rt *rt, int rix)
96 if (rt->ratecode[rix] & IEEE80211_RATE_MCS)
97 return rt->ratecode[rix] & ~(IEEE80211_RATE_MCS);
99 return (rt->ratecode[rix] / 2);
103 dot11str(struct ath_rateioctl_rt *rt, int rix)
107 else if (rt->ratecode[rix] & IEEE80211_RATE_MCS)
114 ath_sample_stats(struct ath_ratestats *r, struct ath_rateioctl_rt *rt,
115 struct sample_node *sn)
120 PRINTMSG("static_rix (%d) ratemask 0x%llx\n",
122 (long long) sn->ratemask);
124 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
125 PRINTATTR_ON(COLOR_PAIR(y+4) | A_BOLD);
126 PRINTMSG("[%4u] cur rate %d %s since switch: "
127 "packets %d ticks %u\n",
129 dot11rate(rt, sn->current_rix[y]),
130 dot11str(rt, sn->current_rix[y]),
131 sn->packets_since_switch[y],
132 sn->ticks_since_switch[y]);
134 PRINTMSG("[%4u] last sample (%d %s) cur sample (%d %s) "
137 dot11rate(rt, sn->last_sample_rix[y]),
138 dot11str(rt, sn->last_sample_rix[y]),
139 dot11rate(rt, sn->current_sample_rix[y]),
140 dot11str(rt, sn->current_sample_rix[y]),
141 sn->packets_sent[y]);
142 PRINTATTR_OFF(COLOR_PAIR(y+4) | A_BOLD);
144 PRINTATTR_ON(COLOR_PAIR(3) | A_BOLD);
145 PRINTMSG("[%4u] packets since sample %d sample tt %u\n",
147 sn->packets_since_sample[y],
149 PRINTATTR_OFF(COLOR_PAIR(3) | A_BOLD);
152 PRINTMSG(" TX Rate TXTOTAL:TXOK EWMA T/ F"
154 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
157 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
158 if (sn->stats[y][rix].total_packets == 0)
160 if (rix == sn->current_rix[y])
161 PRINTATTR_ON(COLOR_PAIR(y+4) | A_BOLD);
162 else if (rix == sn->last_sample_rix[y])
163 PRINTATTR_ON(COLOR_PAIR(3) | A_BOLD);
165 else if (sn->stats[y][rix].ewma_pct / 10 < 50)
166 PRINTATTR_ON(COLOR_PAIR(2) | A_BOLD);
167 else if (sn->stats[y][rix].ewma_pct / 10 < 75)
168 PRINTATTR_ON(COLOR_PAIR(1) | A_BOLD);
170 PRINTMSG("[%2u %s:%4u] %8ju:%-8ju "
171 "(%3d.%1d%%) %8ju/%4d %5uuS %u\n",
175 (uintmax_t) sn->stats[y][rix].total_packets,
176 (uintmax_t) sn->stats[y][rix].packets_acked,
177 sn->stats[y][rix].ewma_pct / 10,
178 sn->stats[y][rix].ewma_pct % 10,
179 (uintmax_t) sn->stats[y][rix].tries,
180 sn->stats[y][rix].successive_failures,
181 sn->stats[y][rix].average_tx_time,
182 sn->stats[y][rix].last_tx);
183 if (rix == sn->current_rix[y])
184 PRINTATTR_OFF(COLOR_PAIR(y+4) | A_BOLD);
185 else if (rix == sn->last_sample_rix[y])
186 PRINTATTR_OFF(COLOR_PAIR(3) | A_BOLD);
188 else if (sn->stats[y][rix].ewma_pct / 10 < 50)
189 PRINTATTR_OFF(COLOR_PAIR(2) | A_BOLD);
190 else if (sn->stats[y][rix].ewma_pct / 10 < 75)
191 PRINTATTR_OFF(COLOR_PAIR(1) | A_BOLD);
198 ath_setifname(struct ath_ratestats *r, const char *ifname)
201 strncpy(r->re.if_name, ifname, sizeof (r->re.if_name));
205 ath_setsta(struct ath_ratestats *r, uint8_t *mac)
208 memcpy(&r->re.is_u.macaddr, mac, sizeof(r->re.is_u.macaddr));
212 ath_rate_ioctl(struct ath_ratestats *r)
215 if (ioctl(r->s, SIOCGATHNODERATESTATS, &r->re) < 0)
220 rate_node_stats(struct ath_ratestats *r, struct ether_addr *e)
222 struct ath_rateioctl_tlv *av;
223 struct sample_node *sn = NULL;
224 struct ath_rateioctl_rt *rt = NULL;
226 uint8_t *buf = (uint8_t *) r->re.buf;
229 * For now, hard-code the TLV order and contents. Ew!
231 av = (struct ath_rateioctl_tlv *) buf;
232 if (av->tlv_id != ATH_RATE_TLV_RATETABLE) {
233 fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
236 ATH_RATE_TLV_RATETABLE);
239 if (av->tlv_len != sizeof(struct ath_rateioctl_rt)) {
240 fprintf(stderr, "unexpected TLV len (got %d bytes, "
241 "expected %d bytes\n",
243 (int) sizeof(struct ath_rateioctl_rt));
246 rt = (void *) (buf + sizeof(struct ath_rateioctl_tlv));
249 av = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
250 sizeof(struct ath_rateioctl_rt));
251 if (av->tlv_id != ATH_RATE_TLV_SAMPLENODE) {
252 fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
255 ATH_RATE_TLV_SAMPLENODE);
258 if (av->tlv_len != sizeof(struct sample_node)) {
259 fprintf(stderr, "unexpected TLV len (got %d bytes, "
260 "expected %d bytes\n",
262 (int) sizeof(struct sample_node));
265 sn = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
266 sizeof(struct ath_rateioctl_rt) +
267 sizeof(struct ath_rateioctl_tlv));
269 ath_sample_stats(r, rt, sn);
275 fetch_and_print_stats(struct ath_ratestats *r, struct ether_addr *e,
279 /* Zero the buffer before it's passed in */
280 memset(buf, '\0', STATS_BUF_SIZE);
283 * Set the station address for this lookup.
285 ath_setsta(r, e->octet);
288 * Fetch the data from the driver.
293 * Decode and parse statistics.
295 rate_node_stats(r, e);
299 main(int argc, char *argv[])
301 char const *ifname = NULL, *macaddr = NULL;
304 struct ether_addr *e;
305 struct ath_ratestats r;
307 useconds_t sleep_period;
311 ifname = getenv("ATH");
313 ifname = ATH_DEFAULT;
315 while ((c = getopt(argc, argv, "ahi:m:s:")) != -1) {
327 sscanf(optarg, "%f", &f);
329 sleep_period = (useconds_t) (f * 1000000.0);
333 "usage: %s [-h] [-i ifname] [-a] [-m macaddr] [-s sleep period]\n",
339 if (macaddr == NULL) {
340 errx(1, "%s: macaddress wasn't supplied and no -a given\n",
344 e = ether_aton(macaddr);
346 err(1, "ether_aton");
348 bzero(&r, sizeof(r));
351 * Persistent buffer for each lookup
353 buf = malloc(STATS_BUF_SIZE);
357 r.re.buf = (char *) buf;
358 r.re.len = STATS_BUF_SIZE;
360 r.s = socket(AF_INET, SOCK_DGRAM, 0);
365 /* XXX error check */
366 ath_setifname(&r, ifname);
371 use_default_colors();
372 pair_content(0, &cf, &cb);
374 init_pair(1, COLOR_YELLOW, cb);
376 init_pair(2, COLOR_RED, cb);
378 init_pair(3, COLOR_CYAN, cb);
379 /* 250 byte frames */
380 init_pair(4, COLOR_BLUE, cb);
381 /* 1600 byte frames */
382 init_pair(5, COLOR_MAGENTA, cb);
387 intrflush(stdscr, FALSE);
388 keypad(stdscr, TRUE);
393 fetch_and_print_stats(&r, e, buf);
395 usleep(sleep_period);
398 fetch_and_print_stats(&r, e, buf);