2 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
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 * 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.
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.
25 * FreeBSD: src/tools/tools/net80211/wlantxtime/wlantxtime.c,v 1.2 2009/02/19 05:36:07 sam Exp $
29 * IEEE 802.11 PHY-related support.
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <net/if_llc.h>
37 #include <netproto/802_11/_ieee80211.h>
38 #include <netproto/802_11/ieee80211.h>
40 #define IEEE80211_F_SHPREAMBLE 0x00040000 /* STATUS: use short preamble */
49 struct ieee80211_rate_table {
50 int rateCount; /* NB: for proper padding */
51 uint8_t rateCodeToIndex[256]; /* back mapping */
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. */
67 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
68 uint32_t frameLen, uint16_t rate, int isShortPreamble);
70 #define KASSERT(c, msg) do { \
79 panic(const char *fmt, ...)
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)
99 static struct ieee80211_rate_table ieee80211_11b_table = {
100 .rateCount = 4, /* XXX no PBCC */
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 */
112 static struct ieee80211_rate_table ieee80211_11g_table = {
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 }
132 static struct ieee80211_rate_table ieee80211_11a_table = {
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 }
148 static struct ieee80211_rate_table ieee80211_half_table = {
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 }
164 static struct ieee80211_rate_table ieee80211_quarter_table = {
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 }
180 static struct ieee80211_rate_table ieee80211_turbog_table = {
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 }
195 static struct ieee80211_rate_table ieee80211_turboa_table = {
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 }
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.
225 * XXX not reentrant, but shouldn't matter
228 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
230 #define N(a) (sizeof(a)/sizeof(a[0]))
231 #define WLAN_CTRL_FRAME_SIZE \
232 (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
236 for (i = 0; i < N(rt->rateCodeToIndex); i++)
237 rt->rateCodeToIndex[i] = (uint8_t) -1;
238 for (i = 0; i < rt->rateCount; i++) {
239 uint8_t code = rt->info[i].dot11Rate;
240 uint8_t cix = rt->info[i].ctlRateIndex;
241 uint8_t ctl_rate = rt->info[cix].dot11Rate;
243 rt->rateCodeToIndex[code] = i;
244 if (code & IEEE80211_RATE_BASIC) {
246 * Map w/o basic rate bit too.
248 code &= IEEE80211_RATE_VAL;
249 rt->rateCodeToIndex[code] = i;
253 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
254 * depends on whether they are marked as basic rates;
255 * the static tables are setup with an 11b-compatible
256 * 2Mb/s rate which will work but is suboptimal
258 * NB: Control rate is always less than or equal to the
259 * current rate, so control rate's reverse lookup entry
260 * has been installed and following call is safe.
262 rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
263 WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
264 rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
265 WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
268 #undef WLAN_CTRL_FRAME_SIZE
272 /* Setup all rate tables */
274 ieee80211_phy_init(void)
276 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
277 static struct ieee80211_rate_table * const ratetables[] = {
278 &ieee80211_half_table,
279 &ieee80211_quarter_table,
280 &ieee80211_11a_table,
281 &ieee80211_11g_table,
282 &ieee80211_turbog_table,
283 &ieee80211_turboa_table,
284 &ieee80211_turboa_table,
285 &ieee80211_11a_table,
286 &ieee80211_11g_table,
291 for (i = 0; i < N(ratetables); ++i)
292 ieee80211_setup_ratetable(ratetables[i]);
296 #define CCK_SIFS_TIME 10
297 #define CCK_PREAMBLE_BITS 144
298 #define CCK_PLCP_BITS 48
300 #define OFDM_SIFS_TIME 16
301 #define OFDM_PREAMBLE_TIME 20
302 #define OFDM_PLCP_BITS 22
303 #define OFDM_SYMBOL_TIME 4
305 #define OFDM_HALF_SIFS_TIME 32
306 #define OFDM_HALF_PREAMBLE_TIME 40
307 #define OFDM_HALF_PLCP_BITS 22
308 #define OFDM_HALF_SYMBOL_TIME 8
310 #define OFDM_QUARTER_SIFS_TIME 64
311 #define OFDM_QUARTER_PREAMBLE_TIME 80
312 #define OFDM_QUARTER_PLCP_BITS 22
313 #define OFDM_QUARTER_SYMBOL_TIME 16
315 #define TURBO_SIFS_TIME 8
316 #define TURBO_PREAMBLE_TIME 14
317 #define TURBO_PLCP_BITS 22
318 #define TURBO_SYMBOL_TIME 4
325 #define HT_LTF(n) ((n) * 4)
328 * Compute the time to transmit a frame of length frameLen bytes
329 * using the specified rate, phy, and short preamble setting.
333 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
334 uint32_t frameLen, uint16_t rate, int isShortPreamble)
336 uint8_t rix = rt->rateCodeToIndex[rate];
337 uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
340 KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
341 kbps = rt->info[rix].rateKbps;
342 if (kbps == 0) /* XXX bandaid for channel changes */
345 switch (rt->info[rix].phy) {
346 case IEEE80211_T_CCK:
347 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
348 if (isShortPreamble && rt->info[rix].shortPreamble)
350 numBits = frameLen << 3;
351 txTime = CCK_SIFS_TIME + phyTime
352 + ((numBits * 1000)/kbps);
354 case IEEE80211_T_OFDM:
355 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
356 KASSERT(bitsPerSymbol != 0, ("full rate bps"));
358 numBits = OFDM_PLCP_BITS + (frameLen << 3);
359 numSymbols = howmany(numBits, bitsPerSymbol);
360 txTime = OFDM_SIFS_TIME
362 + (numSymbols * OFDM_SYMBOL_TIME);
364 case IEEE80211_T_OFDM_HALF:
365 bitsPerSymbol = (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
366 KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
368 numBits = OFDM_PLCP_BITS + (frameLen << 3);
369 numSymbols = howmany(numBits, bitsPerSymbol);
370 txTime = OFDM_HALF_SIFS_TIME
371 + OFDM_HALF_PREAMBLE_TIME
372 + (numSymbols * OFDM_HALF_SYMBOL_TIME);
374 case IEEE80211_T_OFDM_QUARTER:
375 bitsPerSymbol = (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
376 KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
378 numBits = OFDM_PLCP_BITS + (frameLen << 3);
379 numSymbols = howmany(numBits, bitsPerSymbol);
380 txTime = OFDM_QUARTER_SIFS_TIME
381 + OFDM_QUARTER_PREAMBLE_TIME
382 + (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
384 case IEEE80211_T_TURBO:
385 /* we still save OFDM rates in kbps - so double them */
386 bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
387 KASSERT(bitsPerSymbol != 0, ("turbo bps"));
389 numBits = TURBO_PLCP_BITS + (frameLen << 3);
390 numSymbols = howmany(numBits, bitsPerSymbol);
391 txTime = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
392 + (numSymbols * TURBO_SYMBOL_TIME);
395 panic("%s: unknown phy %u (rate %u)\n", __func__,
396 rt->info[rix].phy, rate);
403 ieee80211_compute_duration_ht(const struct ieee80211_rate_table *rt,
404 uint32_t frameLen, uint16_t rate,
405 int streams, int isht40, int isShortGI)
407 static const uint16_t ht20_bps[16] = {
408 26, 52, 78, 104, 156, 208, 234, 260,
409 52, 104, 156, 208, 312, 416, 468, 520
411 static const uint16_t ht40_bps[16] = {
412 54, 108, 162, 216, 324, 432, 486, 540,
413 108, 216, 324, 432, 648, 864, 972, 1080,
415 uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
417 KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
418 KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
421 bitsPerSymbol = ht40_bps[rate & 0xf];
423 bitsPerSymbol = ht20_bps[rate & 0xf];
424 numBits = OFDM_PLCP_BITS + (frameLen << 3);
425 numSymbols = howmany(numBits, bitsPerSymbol);
427 txTime = ((numSymbols * 18) + 4) / 5; /* 3.6us */
429 txTime = numSymbols * 4; /* 4us */
430 return txTime + HT_L_STF + HT_L_LTF +
431 HT_L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
434 static const struct ieee80211_rate_table *
435 mode2table(const char *mode)
437 if (strcasecmp(mode, "half") == 0)
438 return &ieee80211_half_table;
439 else if (strcasecmp(mode, "quarter") == 0)
440 return &ieee80211_quarter_table;
441 else if (strcasecmp(mode, "hta") == 0)
442 return &ieee80211_11a_table; /* XXX */
443 else if (strcasecmp(mode, "htg") == 0)
444 return &ieee80211_11g_table; /* XXX */
445 else if (strcasecmp(mode, "108g") == 0)
446 return &ieee80211_turbog_table;
447 else if (strcasecmp(mode, "sturbo") == 0)
448 return &ieee80211_turboa_table;
449 else if (strcasecmp(mode, "turbo") == 0)
450 return &ieee80211_turboa_table;
451 else if (strcasecmp(mode, "11a") == 0)
452 return &ieee80211_11a_table;
453 else if (strcasecmp(mode, "11g") == 0)
454 return &ieee80211_11g_table;
455 else if (strcasecmp(mode, "11b") == 0)
456 return &ieee80211_11b_table;
466 snprintf(buf, sizeof(buf), "%u.5", rate/2);
468 snprintf(buf, sizeof(buf), "%u", rate/2);
473 checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
474 int isShortPreamble, int verbose)
476 if (isShortPreamble) {
477 if (rt->info[rix].phy != IEEE80211_T_CCK) {
479 warnx("short preamble not meaningful, ignored");
481 } else if (!rt->info[rix].shortPreamble) {
483 warnx("short preamble not meaningful with "
485 srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
489 return isShortPreamble;
493 usage(const char *progname)
495 fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
497 fprintf(stderr, "-a display calculations for all possible rates\n");
498 fprintf(stderr, "-l framelen length in bytes of 802.11 payload (default 1536)\n");
499 fprintf(stderr, "-m 11a calculate for 11a channel\n");
500 fprintf(stderr, "-m 11b calculate for 11b channel\n");
501 fprintf(stderr, "-m 11g calculate for 11g channel (default)\n");
502 fprintf(stderr, "-m half calculate for 1/2 width channel\n");
503 fprintf(stderr, "-m quarter calculate for 1/4 width channel\n");
504 fprintf(stderr, "-m 108g calculate for dynamic turbo 11g channel\n");
505 fprintf(stderr, "-m sturbo calculate for static turbo channel\n");
506 fprintf(stderr, "-m turbo calculate for dynamic turbo 11a channel\n");
507 fprintf(stderr, "-r rate IEEE rate code (default 54)\n");
508 fprintf(stderr, "-s short preamble (default long)\n");
513 main(int argc, char *argv[])
515 const struct ieee80211_rate_table *rt;
521 int ch, allrates, isShortPreamble, isShort;
524 ieee80211_phy_init();
529 + sizeof(struct ieee80211_frame)
535 while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
541 frameLen = strtoul(optarg, NULL, 0);
547 frate = atof(optarg);
548 rate = (int) 2*frate;
558 rt = mode2table(mode);
560 errx(-1, "unknown mode %s", mode);
562 rix = rt->rateCodeToIndex[rate];
563 if (rix == (uint8_t) -1)
564 errx(-1, "rate %s not valid for mode %s", srate(rate), mode);
565 isShort = checkpreamble(rt, rix, isShortPreamble, 1);
567 time = ieee80211_compute_duration(rt, frameLen, rate, isShort);
568 printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
569 time, frameLen, srate(rate),
570 isShort ? "short" : "long");
572 for (rix = 0; rix < rt->rateCount; rix++) {
573 rate = rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC;
574 isShort = checkpreamble(rt, rix, isShortPreamble, 0);
575 time = ieee80211_compute_duration(rt, frameLen, rate,
577 printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
578 time, frameLen, srate(rate),
579 isShort ? "short" : "long");