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