xref: /freebsd/tools/tools/net80211/wlantxtime/wlantxtime.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 /*
30  * IEEE 802.11 PHY-related support.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 
36 #include <net/if_llc.h>
37 
38 #include <net80211/_ieee80211.h>
39 #include <net80211/ieee80211.h>
40 
41 #define	IEEE80211_F_SHPREAMBLE	0x00040000	/* STATUS: use short preamble */
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include <unistd.h>
49 
50 struct ieee80211_rate_table {
51 	int		rateCount;		/* NB: for proper padding */
52 	uint8_t		rateCodeToIndex[256];	/* back mapping */
53 	struct {
54 		uint8_t		phy;		/* CCK/OFDM/TURBO */
55 		uint32_t	rateKbps;	/* transfer rate in kbs */
56 		uint8_t		shortPreamble;	/* mask for enabling short
57 						 * preamble in CCK rate code */
58 		uint8_t		dot11Rate;	/* value for supported rates
59 						 * info element of MLME */
60 		uint8_t		ctlRateIndex;	/* index of next lower basic
61 						 * rate; used for dur. calcs */
62 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
63 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
64 	} info[32];
65 };
66 
67 uint16_t
68 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
69 	uint32_t frameLen, uint16_t rate, int isShortPreamble);
70 
71 #define	KASSERT(c, msg) do {			\
72 	if (!(c)) {				\
73 		printf msg;			\
74 		putchar('\n');			\
75 		exit(-1);			\
76 	}					\
77 } while (0)
78 
79 static void
80 panic(const char *fmt, ...)
81 {
82 	va_list ap;
83 
84 	va_start(ap, fmt);
85 	vprintf(fmt, ap);
86 	va_end(ap);
87 	exit(-1);
88 }
89 
90 /* shorthands to compact tables for readability */
91 #define	OFDM	IEEE80211_T_OFDM
92 #define	CCK	IEEE80211_T_CCK
93 #define	TURBO	IEEE80211_T_TURBO
94 #define	HALF	IEEE80211_T_OFDM_HALF
95 #define	QUART	IEEE80211_T_OFDM_QUARTER
96 #define	PBCC	(IEEE80211_T_OFDM_QUARTER+1)		/* XXX */
97 #define	B(r)	(0x80 | r)
98 #define	Mb(x)	(x*1000)
99 
100 static struct ieee80211_rate_table ieee80211_11b_table = {
101     .rateCount = 4,		/* XXX no PBCC */
102     .info = {
103 /*                                   short            ctrl  */
104 /*                                Preamble  dot11Rate Rate */
105      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },/*   1 Mb */
106      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },/*   2 Mb */
107      [2] = { .phy = CCK,     5500,    0x04,     B(11),   1 },/* 5.5 Mb */
108      [3] = { .phy = CCK,    11000,    0x04,     B(22),   1 },/*  11 Mb */
109      [4] = { .phy = PBCC,   22000,    0x04,        44,   3 } /*  22 Mb */
110     },
111 };
112 
113 static struct ieee80211_rate_table ieee80211_11g_table = {
114     .rateCount = 12,
115     .info = {
116 /*                                   short            ctrl  */
117 /*                                Preamble  dot11Rate Rate */
118      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },
119      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },
120      [2] = { .phy = CCK,     5500,    0x04,     B(11),   2 },
121      [3] = { .phy = CCK,    11000,    0x04,     B(22),   3 },
122      [4] = { .phy = OFDM,    6000,    0x00,        12,   4 },
123      [5] = { .phy = OFDM,    9000,    0x00,        18,   4 },
124      [6] = { .phy = OFDM,   12000,    0x00,        24,   6 },
125      [7] = { .phy = OFDM,   18000,    0x00,        36,   6 },
126      [8] = { .phy = OFDM,   24000,    0x00,        48,   8 },
127      [9] = { .phy = OFDM,   36000,    0x00,        72,   8 },
128     [10] = { .phy = OFDM,   48000,    0x00,        96,   8 },
129     [11] = { .phy = OFDM,   54000,    0x00,       108,   8 }
130     },
131 };
132 
133 static struct ieee80211_rate_table ieee80211_11a_table = {
134     .rateCount = 8,
135     .info = {
136 /*                                   short            ctrl  */
137 /*                                Preamble  dot11Rate Rate */
138      [0] = { .phy = OFDM,    6000,    0x00,     B(12),   0 },
139      [1] = { .phy = OFDM,    9000,    0x00,        18,   0 },
140      [2] = { .phy = OFDM,   12000,    0x00,     B(24),   2 },
141      [3] = { .phy = OFDM,   18000,    0x00,        36,   2 },
142      [4] = { .phy = OFDM,   24000,    0x00,     B(48),   4 },
143      [5] = { .phy = OFDM,   36000,    0x00,        72,   4 },
144      [6] = { .phy = OFDM,   48000,    0x00,        96,   4 },
145      [7] = { .phy = OFDM,   54000,    0x00,       108,   4 }
146     },
147 };
148 
149 static struct ieee80211_rate_table ieee80211_half_table = {
150     .rateCount = 8,
151     .info = {
152 /*                                   short            ctrl  */
153 /*                                Preamble  dot11Rate Rate */
154      [0] = { .phy = HALF,    3000,    0x00,      B(6),   0 },
155      [1] = { .phy = HALF,    4500,    0x00,         9,   0 },
156      [2] = { .phy = HALF,    6000,    0x00,     B(12),   2 },
157      [3] = { .phy = HALF,    9000,    0x00,        18,   2 },
158      [4] = { .phy = HALF,   12000,    0x00,     B(24),   4 },
159      [5] = { .phy = HALF,   18000,    0x00,        36,   4 },
160      [6] = { .phy = HALF,   24000,    0x00,        48,   4 },
161      [7] = { .phy = HALF,   27000,    0x00,        54,   4 }
162     },
163 };
164 
165 static struct ieee80211_rate_table ieee80211_quarter_table = {
166     .rateCount = 8,
167     .info = {
168 /*                                   short            ctrl  */
169 /*                                Preamble  dot11Rate Rate */
170      [0] = { .phy = QUART,   1500,    0x00,      B(3),   0 },
171      [1] = { .phy = QUART,   2250,    0x00,         4,   0 },
172      [2] = { .phy = QUART,   3000,    0x00,      B(9),   2 },
173      [3] = { .phy = QUART,   4500,    0x00,         9,   2 },
174      [4] = { .phy = QUART,   6000,    0x00,     B(12),   4 },
175      [5] = { .phy = QUART,   9000,    0x00,        18,   4 },
176      [6] = { .phy = QUART,  12000,    0x00,        24,   4 },
177      [7] = { .phy = QUART,  13500,    0x00,        27,   4 }
178     },
179 };
180 
181 static struct ieee80211_rate_table ieee80211_turbog_table = {
182     .rateCount = 7,
183     .info = {
184 /*                                   short            ctrl  */
185 /*                                Preamble  dot11Rate Rate */
186      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
187      [1] = { .phy = TURBO,   24000,   0x00,     B(24),   1 },
188      [2] = { .phy = TURBO,   36000,   0x00,        36,   1 },
189      [3] = { .phy = TURBO,   48000,   0x00,     B(48),   3 },
190      [4] = { .phy = TURBO,   72000,   0x00,        72,   3 },
191      [5] = { .phy = TURBO,   96000,   0x00,        96,   3 },
192      [6] = { .phy = TURBO,  108000,   0x00,       108,   3 }
193     },
194 };
195 
196 static struct ieee80211_rate_table ieee80211_turboa_table = {
197     .rateCount = 8,
198     .info = {
199 /*                                   short            ctrl  */
200 /*                                Preamble  dot11Rate Rate */
201      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
202      [1] = { .phy = TURBO,   18000,   0x00,        18,   0 },
203      [2] = { .phy = TURBO,   24000,   0x00,     B(24),   2 },
204      [3] = { .phy = TURBO,   36000,   0x00,        36,   2 },
205      [4] = { .phy = TURBO,   48000,   0x00,     B(48),   4 },
206      [5] = { .phy = TURBO,   72000,   0x00,        72,   4 },
207      [6] = { .phy = TURBO,   96000,   0x00,        96,   4 },
208      [7] = { .phy = TURBO,  108000,   0x00,       108,   4 }
209     },
210 };
211 
212 #undef	Mb
213 #undef	B
214 #undef	OFDM
215 #undef	CCK
216 #undef	TURBO
217 #undef	XR
218 
219 /*
220  * Setup a rate table's reverse lookup table and fill in
221  * ack durations.  The reverse lookup tables are assumed
222  * to be initialized to zero (or at least the first entry).
223  * We use this as a key that indicates whether or not
224  * we've previously setup the reverse lookup table.
225  *
226  * XXX not reentrant, but shouldn't matter
227  */
228 static void
229 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
230 {
231 #define	N(a)	(sizeof(a)/sizeof(a[0]))
232 #define	WLAN_CTRL_FRAME_SIZE \
233 	(sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
234 
235 	int i;
236 
237 	for (i = 0; i < N(rt->rateCodeToIndex); i++)
238 		rt->rateCodeToIndex[i] = (uint8_t) -1;
239 	for (i = 0; i < rt->rateCount; i++) {
240 		uint8_t code = rt->info[i].dot11Rate;
241 		uint8_t cix = rt->info[i].ctlRateIndex;
242 		uint8_t ctl_rate = rt->info[cix].dot11Rate;
243 
244 		rt->rateCodeToIndex[code] = i;
245 		if (code & IEEE80211_RATE_BASIC) {
246 			/*
247 			 * Map w/o basic rate bit too.
248 			 */
249 			code &= IEEE80211_RATE_VAL;
250 			rt->rateCodeToIndex[code] = i;
251 		}
252 
253 		/*
254 		 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
255 		 *     depends on whether they are marked as basic rates;
256 		 *     the static tables are setup with an 11b-compatible
257 		 *     2Mb/s rate which will work but is suboptimal
258 		 *
259 		 * NB: Control rate is always less than or equal to the
260 		 *     current rate, so control rate's reverse lookup entry
261 		 *     has been installed and following call is safe.
262 		 */
263 		rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
264 			WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
265 		rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
266 			WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
267 	}
268 
269 #undef WLAN_CTRL_FRAME_SIZE
270 #undef N
271 }
272 
273 /* Setup all rate tables */
274 static void
275 ieee80211_phy_init(void)
276 {
277 #define N(arr)	(int)(sizeof(arr) / sizeof(arr[0]))
278 	static struct ieee80211_rate_table * const ratetables[] = {
279 		&ieee80211_half_table,
280 		&ieee80211_quarter_table,
281 		&ieee80211_11a_table,
282 		&ieee80211_11g_table,
283 		&ieee80211_turbog_table,
284 		&ieee80211_turboa_table,
285 		&ieee80211_turboa_table,
286 		&ieee80211_11a_table,
287 		&ieee80211_11g_table,
288 		&ieee80211_11b_table
289 	};
290 	int i;
291 
292 	for (i = 0; i < N(ratetables); ++i)
293 		ieee80211_setup_ratetable(ratetables[i]);
294 
295 #undef N
296 }
297 #define CCK_SIFS_TIME		10
298 #define CCK_PREAMBLE_BITS	144
299 #define CCK_PLCP_BITS		48
300 
301 #define OFDM_SIFS_TIME		16
302 #define OFDM_PREAMBLE_TIME	20
303 #define OFDM_PLCP_BITS		22
304 #define OFDM_SYMBOL_TIME	4
305 
306 #define OFDM_HALF_SIFS_TIME	32
307 #define OFDM_HALF_PREAMBLE_TIME	40
308 #define OFDM_HALF_PLCP_BITS	22
309 #define OFDM_HALF_SYMBOL_TIME	8
310 
311 #define OFDM_QUARTER_SIFS_TIME 		64
312 #define OFDM_QUARTER_PREAMBLE_TIME	80
313 #define OFDM_QUARTER_PLCP_BITS		22
314 #define OFDM_QUARTER_SYMBOL_TIME	16
315 
316 #define TURBO_SIFS_TIME		8
317 #define TURBO_PREAMBLE_TIME	14
318 #define TURBO_PLCP_BITS		22
319 #define TURBO_SYMBOL_TIME	4
320 
321 #define	HT_L_STF	8
322 #define	HT_L_LTF	8
323 #define	HT_L_SIG	4
324 #define	HT_SIG		8
325 #define	HT_STF		4
326 #define	HT_LTF(n)	((n) * 4)
327 
328 /*
329  * Compute the time to transmit a frame of length frameLen bytes
330  * using the specified rate, phy, and short preamble setting.
331  * SIFS is included.
332  */
333 uint16_t
334 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
335 	uint32_t frameLen, uint16_t rate, int isShortPreamble)
336 {
337 	uint8_t rix = rt->rateCodeToIndex[rate];
338 	uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
339 	uint32_t kbps;
340 
341 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
342 	kbps = rt->info[rix].rateKbps;
343 	if (kbps == 0)			/* XXX bandaid for channel changes */
344 		return 0;
345 
346 	switch (rt->info[rix].phy) {
347 	case IEEE80211_T_CCK:
348 		phyTime		= CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
349 		if (isShortPreamble && rt->info[rix].shortPreamble)
350 			phyTime >>= 1;
351 		numBits		= frameLen << 3;
352 		txTime		= CCK_SIFS_TIME + phyTime
353 				+ ((numBits * 1000)/kbps);
354 		break;
355 	case IEEE80211_T_OFDM:
356 		bitsPerSymbol	= (kbps * OFDM_SYMBOL_TIME) / 1000;
357 		KASSERT(bitsPerSymbol != 0, ("full rate bps"));
358 
359 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
360 		numSymbols	= howmany(numBits, bitsPerSymbol);
361 		txTime		= OFDM_SIFS_TIME
362 				+ OFDM_PREAMBLE_TIME
363 				+ (numSymbols * OFDM_SYMBOL_TIME);
364 		break;
365 	case IEEE80211_T_OFDM_HALF:
366 		bitsPerSymbol	= (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
367 		KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
368 
369 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
370 		numSymbols	= howmany(numBits, bitsPerSymbol);
371 		txTime		= OFDM_HALF_SIFS_TIME
372 				+ OFDM_HALF_PREAMBLE_TIME
373 				+ (numSymbols * OFDM_HALF_SYMBOL_TIME);
374 		break;
375 	case IEEE80211_T_OFDM_QUARTER:
376 		bitsPerSymbol	= (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
377 		KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
378 
379 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
380 		numSymbols	= howmany(numBits, bitsPerSymbol);
381 		txTime		= OFDM_QUARTER_SIFS_TIME
382 				+ OFDM_QUARTER_PREAMBLE_TIME
383 				+ (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
384 		break;
385 	case IEEE80211_T_TURBO:
386 		/* we still save OFDM rates in kbps - so double them */
387 		bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
388 		KASSERT(bitsPerSymbol != 0, ("turbo bps"));
389 
390 		numBits       = TURBO_PLCP_BITS + (frameLen << 3);
391 		numSymbols    = howmany(numBits, bitsPerSymbol);
392 		txTime        = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
393 			      + (numSymbols * TURBO_SYMBOL_TIME);
394 		break;
395 	default:
396 		panic("%s: unknown phy %u (rate %u)\n", __func__,
397 		      rt->info[rix].phy, rate);
398 		break;
399 	}
400 	return txTime;
401 }
402 
403 uint32_t
404 ieee80211_compute_duration_ht(const struct ieee80211_rate_table *rt,
405 	uint32_t frameLen, uint16_t rate,
406 	int streams, int isht40, int isShortGI)
407 {
408 	static const uint16_t ht20_bps[16] = {
409 	    26, 52, 78, 104, 156, 208, 234, 260,
410 	    52, 104, 156, 208, 312, 416, 468, 520
411 	};
412 	static const uint16_t ht40_bps[16] = {
413 	    54, 108, 162, 216, 324, 432, 486, 540,
414 	    108, 216, 324, 432, 648, 864, 972, 1080,
415 	};
416 	uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
417 
418 	KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
419 	KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
420 
421 	if (isht40)
422 		bitsPerSymbol = ht40_bps[rate & 0xf];
423 	else
424 		bitsPerSymbol = ht20_bps[rate & 0xf];
425 	numBits = OFDM_PLCP_BITS + (frameLen << 3);
426 	numSymbols = howmany(numBits, bitsPerSymbol);
427 	if (isShortGI)
428 		txTime = ((numSymbols * 18) + 4) / 5;	/* 3.6us */
429 	else
430 		txTime = numSymbols * 4;		/* 4us */
431 	return txTime + HT_L_STF + HT_L_LTF +
432 	    HT_L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
433 }
434 
435 static const struct ieee80211_rate_table *
436 mode2table(const char *mode)
437 {
438 	if (strcasecmp(mode, "half") == 0)
439 		return &ieee80211_half_table;
440 	else if (strcasecmp(mode, "quarter") == 0)
441 		return &ieee80211_quarter_table;
442 	else if (strcasecmp(mode, "hta") == 0)
443 		return &ieee80211_11a_table;	/* XXX */
444 	else if (strcasecmp(mode, "htg") == 0)
445 		return &ieee80211_11g_table;	/* XXX */
446 	else if (strcasecmp(mode, "108g") == 0)
447 		return &ieee80211_turbog_table;
448 	else if (strcasecmp(mode, "sturbo") == 0)
449 		return &ieee80211_turboa_table;
450 	else if (strcasecmp(mode, "turbo") == 0)
451 		return &ieee80211_turboa_table;
452 	else if (strcasecmp(mode, "11a") == 0)
453 		return &ieee80211_11a_table;
454 	else if (strcasecmp(mode, "11g") == 0)
455 		return &ieee80211_11g_table;
456 	else if (strcasecmp(mode, "11b") == 0)
457 		return &ieee80211_11b_table;
458 	else
459 		return NULL;
460 }
461 
462 const char *
463 srate(int rate)
464 {
465 	static char buf[32];
466 	if (rate & 1)
467 		snprintf(buf, sizeof(buf), "%u.5", rate/2);
468 	else
469 		snprintf(buf, sizeof(buf), "%u", rate/2);
470 	return buf;
471 }
472 
473 static int
474 checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
475 	int isShortPreamble, int verbose)
476 {
477 	if (isShortPreamble) {
478 		if (rt->info[rix].phy != IEEE80211_T_CCK) {
479 			if (verbose)
480 				warnx("short preamble not meaningful, ignored");
481 			isShortPreamble = 0;
482 		} else if (!rt->info[rix].shortPreamble) {
483 			if (verbose)
484 				warnx("short preamble not meaningful with "
485 				    "rate %s, ignored",
486 				    srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
487 			isShortPreamble = 0;
488 		}
489 	}
490 	return isShortPreamble;
491 }
492 
493 static void
494 usage(const char *progname)
495 {
496 	fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
497 	    progname);
498 	fprintf(stderr, "-a             display calculations for all possible rates\n");
499 	fprintf(stderr, "-l framelen    length in bytes of 802.11 payload (default 1536)\n");
500 	fprintf(stderr, "-m 11a         calculate for 11a channel\n");
501 	fprintf(stderr, "-m 11b         calculate for 11b channel\n");
502 	fprintf(stderr, "-m 11g         calculate for 11g channel (default)\n");
503 	fprintf(stderr, "-m half        calculate for 1/2 width channel\n");
504 	fprintf(stderr, "-m quarter     calculate for 1/4 width channel\n");
505 	fprintf(stderr, "-m 108g        calculate for dynamic turbo 11g channel\n");
506 	fprintf(stderr, "-m sturbo      calculate for static turbo channel\n");
507 	fprintf(stderr, "-m turbo       calculate for dynamic turbo 11a channel\n");
508 	fprintf(stderr, "-r rate        IEEE rate code (default 54)\n");
509 	fprintf(stderr, "-s             short preamble (default long)\n");
510 	exit(0);
511 }
512 
513 int
514 main(int argc, char *argv[])
515 {
516 	const struct ieee80211_rate_table *rt;
517 	const char *mode;
518 	uint32_t frameLen;
519 	uint16_t rate;
520 	uint16_t time;
521 	uint8_t rix;
522 	int ch, allrates, isShortPreamble, isShort;
523 	float frate;
524 
525 	ieee80211_phy_init();
526 
527 	mode = "11g";
528 	isShortPreamble = 0;
529 	frameLen = 1500
530 		 + sizeof(struct ieee80211_frame)
531 		 + LLC_SNAPFRAMELEN
532 		 + IEEE80211_CRC_LEN
533 		 ;
534 	rate = 2*54;
535 	allrates = 0;
536 	while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
537 		switch (ch) {
538 		case 'a':
539 			allrates = 1;
540 			break;
541 		case 'l':
542 			frameLen = strtoul(optarg, NULL, 0);
543 			break;
544 		case 'm':
545 			mode = optarg;
546 			break;
547 		case 'r':
548 			frate = atof(optarg);
549 			rate = (int) 2*frate;
550 			break;
551 		case 's':
552 			isShortPreamble = 1;
553 			break;
554 		default:
555 			usage(argv[0]);
556 			break;
557 		}
558 	}
559 	rt = mode2table(mode);
560 	if (rt == NULL)
561 		errx(-1, "unknown mode %s", mode);
562 	if (!allrates) {
563 		rix = rt->rateCodeToIndex[rate];
564 		if (rix == (uint8_t) -1)
565 			errx(-1, "rate %s not valid for mode %s", srate(rate), mode);
566 		isShort = checkpreamble(rt, rix, isShortPreamble, 1);
567 
568 		time = ieee80211_compute_duration(rt, frameLen, rate, isShort);
569 		printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
570 		    time, frameLen, srate(rate),
571 		    isShort ? "short" : "long");
572 	} else {
573 		for (rix = 0; rix < rt->rateCount; rix++) {
574 			rate = rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC;
575 			isShort = checkpreamble(rt, rix, isShortPreamble, 0);
576 			time = ieee80211_compute_duration(rt, frameLen, rate,
577 			    isShort);
578 			printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
579 			    time, frameLen, srate(rate),
580 			    isShort ? "short" : "long");
581 		}
582 	}
583 	return 0;
584 }
585