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>
41 #include <net/if_var.h>
55 #include "net80211/ieee80211_ioctl.h"
56 #include "net80211/ieee80211_radiotap.h"
57 #include "if_athioctl.h"
58 #include "if_athrate.h"
60 #include "ath_rate/sample/sample.h"
62 static int do_loop = 0;
65 * This needs to be big enough to fit the two TLVs, the rate table
66 * and the rate statistics table for a single node.
68 #define STATS_BUF_SIZE 8192
70 #define PRINTMSG(...) do { \
72 printf(__VA_ARGS__); \
74 printw(__VA_ARGS__); \
77 #define PRINTATTR_ON(_x) do { \
83 #define PRINTATTR_OFF(_x) do { \
88 struct ath_ratestats {
90 struct ath_rateioctl re;
94 dot11rate(struct ath_rateioctl_rt *rt, int rix)
97 if (rt->ratecode[rix] & IEEE80211_RATE_MCS)
98 return rt->ratecode[rix] & ~(IEEE80211_RATE_MCS);
100 return (rt->ratecode[rix] / 2);
104 dot11str(struct ath_rateioctl_rt *rt, int rix)
108 else if (rt->ratecode[rix] & IEEE80211_RATE_MCS)
115 ath_sample_stats(struct ath_ratestats *r, struct ath_rateioctl_rt *rt,
116 struct sample_node *sn)
121 PRINTMSG("static_rix (%d) ratemask 0x%llx\n",
123 (long long) sn->ratemask);
125 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
126 PRINTATTR_ON(COLOR_PAIR(y+4) | A_BOLD);
127 PRINTMSG("[%4u] cur rate %d %s since switch: "
128 "packets %d ticks %u\n",
130 dot11rate(rt, sn->current_rix[y]),
131 dot11str(rt, sn->current_rix[y]),
132 sn->packets_since_switch[y],
133 sn->ticks_since_switch[y]);
135 PRINTMSG("[%4u] last sample (%d %s) cur sample (%d %s) "
138 dot11rate(rt, sn->last_sample_rix[y]),
139 dot11str(rt, sn->last_sample_rix[y]),
140 dot11rate(rt, sn->current_sample_rix[y]),
141 dot11str(rt, sn->current_sample_rix[y]),
142 sn->packets_sent[y]);
143 PRINTATTR_OFF(COLOR_PAIR(y+4) | A_BOLD);
145 PRINTATTR_ON(COLOR_PAIR(3) | A_BOLD);
146 PRINTMSG("[%4u] packets since sample %d sample tt %u\n",
148 sn->packets_since_sample[y],
150 PRINTATTR_OFF(COLOR_PAIR(3) | A_BOLD);
153 PRINTMSG(" TX Rate TXTOTAL:TXOK EWMA T/ F"
155 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
158 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
159 if (sn->stats[y][rix].total_packets == 0)
161 if (rix == sn->current_rix[y])
162 PRINTATTR_ON(COLOR_PAIR(y+4) | A_BOLD);
163 else if (rix == sn->last_sample_rix[y])
164 PRINTATTR_ON(COLOR_PAIR(3) | A_BOLD);
166 else if (sn->stats[y][rix].ewma_pct / 10 < 50)
167 PRINTATTR_ON(COLOR_PAIR(2) | A_BOLD);
168 else if (sn->stats[y][rix].ewma_pct / 10 < 75)
169 PRINTATTR_ON(COLOR_PAIR(1) | A_BOLD);
171 PRINTMSG("[%2u %s:%4u] %8ju:%-8ju "
172 "(%3d.%1d%%) %8ju/%4d %5uuS %u\n",
176 (uintmax_t) sn->stats[y][rix].total_packets,
177 (uintmax_t) sn->stats[y][rix].packets_acked,
178 sn->stats[y][rix].ewma_pct / 10,
179 sn->stats[y][rix].ewma_pct % 10,
180 (uintmax_t) sn->stats[y][rix].tries,
181 sn->stats[y][rix].successive_failures,
182 sn->stats[y][rix].average_tx_time,
183 sn->stats[y][rix].last_tx);
184 if (rix == sn->current_rix[y])
185 PRINTATTR_OFF(COLOR_PAIR(y+4) | A_BOLD);
186 else if (rix == sn->last_sample_rix[y])
187 PRINTATTR_OFF(COLOR_PAIR(3) | A_BOLD);
189 else if (sn->stats[y][rix].ewma_pct / 10 < 50)
190 PRINTATTR_OFF(COLOR_PAIR(2) | A_BOLD);
191 else if (sn->stats[y][rix].ewma_pct / 10 < 75)
192 PRINTATTR_OFF(COLOR_PAIR(1) | A_BOLD);
199 ath_setifname(struct ath_ratestats *r, const char *ifname)
202 strncpy(r->re.if_name, ifname, sizeof (r->re.if_name));
206 ath_setsta(struct ath_ratestats *r, uint8_t *mac)
209 memcpy(&r->re.is_u.macaddr, mac, sizeof(r->re.is_u.macaddr));
213 ath_rate_ioctl(struct ath_ratestats *r)
216 if (ioctl(r->s, SIOCGATHNODERATESTATS, &r->re) < 0)
221 rate_node_stats(struct ath_ratestats *r, struct ether_addr *e)
223 struct ath_rateioctl_tlv *av;
224 struct sample_node *sn = NULL;
225 struct ath_rateioctl_rt *rt = NULL;
227 uint8_t *buf = (uint8_t *) r->re.buf;
230 * For now, hard-code the TLV order and contents. Ew!
232 av = (struct ath_rateioctl_tlv *) buf;
233 if (av->tlv_id != ATH_RATE_TLV_RATETABLE) {
234 fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
237 ATH_RATE_TLV_RATETABLE);
240 if (av->tlv_len != sizeof(struct ath_rateioctl_rt)) {
241 fprintf(stderr, "unexpected TLV len (got %d bytes, "
242 "expected %d bytes\n",
244 (int) sizeof(struct ath_rateioctl_rt));
247 rt = (void *) (buf + sizeof(struct ath_rateioctl_tlv));
250 av = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
251 sizeof(struct ath_rateioctl_rt));
252 if (av->tlv_id != ATH_RATE_TLV_SAMPLENODE) {
253 fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
256 ATH_RATE_TLV_SAMPLENODE);
259 if (av->tlv_len != sizeof(struct sample_node)) {
260 fprintf(stderr, "unexpected TLV len (got %d bytes, "
261 "expected %d bytes\n",
263 (int) sizeof(struct sample_node));
266 sn = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
267 sizeof(struct ath_rateioctl_rt) +
268 sizeof(struct ath_rateioctl_tlv));
270 ath_sample_stats(r, rt, sn);
276 fetch_and_print_stats(struct ath_ratestats *r, struct ether_addr *e,
280 /* Zero the buffer before it's passed in */
281 memset(buf, '\0', STATS_BUF_SIZE);
284 * Set the station address for this lookup.
286 ath_setsta(r, e->octet);
289 * Fetch the data from the driver.
294 * Decode and parse statistics.
296 rate_node_stats(r, e);
300 main(int argc, char *argv[])
302 char const *ifname = NULL, *macaddr = NULL;
305 struct ether_addr *e;
306 struct ath_ratestats r;
308 useconds_t sleep_period;
312 ifname = getenv("ATH");
316 while ((c = getopt(argc, argv, "ahi:m:s:")) != -1) {
328 sscanf(optarg, "%f", &f);
330 sleep_period = (useconds_t) (f * 1000000.0);
334 "usage: %s [-h] [-i ifname] [-a] [-m macaddr] [-s sleep period]\n",
340 if (macaddr == NULL) {
341 errx(1, "%s: macaddress wasn't supplied and no -a given\n",
345 e = ether_aton(macaddr);
347 err(1, "ether_aton");
349 bzero(&r, sizeof(r));
352 * Persistent buffer for each lookup
354 buf = malloc(STATS_BUF_SIZE);
358 r.re.buf = (char *) buf;
359 r.re.len = STATS_BUF_SIZE;
361 r.s = socket(AF_INET, SOCK_DGRAM, 0);
366 /* XXX error check */
367 ath_setifname(&r, ifname);
372 use_default_colors();
373 pair_content(0, &cf, &cb);
375 init_pair(1, COLOR_YELLOW, cb);
377 init_pair(2, COLOR_RED, cb);
379 init_pair(3, COLOR_CYAN, cb);
380 /* 250 byte frames */
381 init_pair(4, COLOR_BLUE, cb);
382 /* 1600 byte frames */
383 init_pair(5, COLOR_MAGENTA, cb);
388 intrflush(stdscr, FALSE);
389 keypad(stdscr, TRUE);
394 fetch_and_print_stats(&r, e, buf);
396 usleep(sleep_period);
399 fetch_and_print_stats(&r, e, buf);