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.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 PHY-related support.
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #include <net/if_llc.h>
38 #include <net80211/_ieee80211.h>
39 #include <net80211/ieee80211.h>
41 #define IEEE80211_F_SHPREAMBLE 0x00040000 /* STATUS: use short preamble */
50 struct ieee80211_rate_table {
51 int rateCount; /* NB: for proper padding */
52 uint8_t rateCodeToIndex[256]; /* back mapping */
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. */
68 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
69 uint32_t frameLen, uint16_t rate, int isShortPreamble);
71 #define KASSERT(c, msg) do { \
80 panic(const char *fmt, ...)
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)
100 static struct ieee80211_rate_table ieee80211_11b_table = {
101 .rateCount = 4, /* XXX no PBCC */
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 */
113 static struct ieee80211_rate_table ieee80211_11g_table = {
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 }
133 static struct ieee80211_rate_table ieee80211_11a_table = {
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 }
149 static struct ieee80211_rate_table ieee80211_half_table = {
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 }
165 static struct ieee80211_rate_table ieee80211_quarter_table = {
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 }
181 static struct ieee80211_rate_table ieee80211_turbog_table = {
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 }
196 static struct ieee80211_rate_table ieee80211_turboa_table = {
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 }
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.
226 * XXX not reentrant, but shouldn't matter
229 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
231 #define N(a) (sizeof(a)/sizeof(a[0]))
232 #define WLAN_CTRL_FRAME_SIZE \
233 (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
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;
244 rt->rateCodeToIndex[code] = i;
245 if (code & IEEE80211_RATE_BASIC) {
247 * Map w/o basic rate bit too.
249 code &= IEEE80211_RATE_VAL;
250 rt->rateCodeToIndex[code] = i;
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
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.
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);
269 #undef WLAN_CTRL_FRAME_SIZE
273 /* Setup all rate tables */
275 ieee80211_phy_init(void)
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,
292 for (i = 0; i < N(ratetables); ++i)
293 ieee80211_setup_ratetable(ratetables[i]);
297 #define CCK_SIFS_TIME 10
298 #define CCK_PREAMBLE_BITS 144
299 #define CCK_PLCP_BITS 48
301 #define OFDM_SIFS_TIME 16
302 #define OFDM_PREAMBLE_TIME 20
303 #define OFDM_PLCP_BITS 22
304 #define OFDM_SYMBOL_TIME 4
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
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
316 #define TURBO_SIFS_TIME 8
317 #define TURBO_PREAMBLE_TIME 14
318 #define TURBO_PLCP_BITS 22
319 #define TURBO_SYMBOL_TIME 4
326 #define HT_LTF(n) ((n) * 4)
329 * Compute the time to transmit a frame of length frameLen bytes
330 * using the specified rate, phy, and short preamble setting.
334 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
335 uint32_t frameLen, uint16_t rate, int isShortPreamble)
337 uint8_t rix = rt->rateCodeToIndex[rate];
338 uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
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 */
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)
351 numBits = frameLen << 3;
352 txTime = CCK_SIFS_TIME + phyTime
353 + ((numBits * 1000)/kbps);
355 case IEEE80211_T_OFDM:
356 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
357 KASSERT(bitsPerSymbol != 0, ("full rate bps"));
359 numBits = OFDM_PLCP_BITS + (frameLen << 3);
360 numSymbols = howmany(numBits, bitsPerSymbol);
361 txTime = OFDM_SIFS_TIME
363 + (numSymbols * OFDM_SYMBOL_TIME);
365 case IEEE80211_T_OFDM_HALF:
366 bitsPerSymbol = (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
367 KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
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);
375 case IEEE80211_T_OFDM_QUARTER:
376 bitsPerSymbol = (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
377 KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
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);
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"));
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);
396 panic("%s: unknown phy %u (rate %u)\n", __func__,
397 rt->info[rix].phy, rate);
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)
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
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,
416 uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
418 KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
419 KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
422 bitsPerSymbol = ht40_bps[rate & 0xf];
424 bitsPerSymbol = ht20_bps[rate & 0xf];
425 numBits = OFDM_PLCP_BITS + (frameLen << 3);
426 numSymbols = howmany(numBits, bitsPerSymbol);
428 txTime = ((numSymbols * 18) + 4) / 5; /* 3.6us */
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);
435 static const struct ieee80211_rate_table *
436 mode2table(const char *mode)
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;
467 snprintf(buf, sizeof(buf), "%u.5", rate/2);
469 snprintf(buf, sizeof(buf), "%u", rate/2);
474 checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
475 int isShortPreamble, int verbose)
477 if (isShortPreamble) {
478 if (rt->info[rix].phy != IEEE80211_T_CCK) {
480 warnx("short preamble not meaningful, ignored");
482 } else if (!rt->info[rix].shortPreamble) {
484 warnx("short preamble not meaningful with "
486 srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
490 return isShortPreamble;
494 usage(const char *progname)
496 fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
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");
514 main(int argc, char *argv[])
516 const struct ieee80211_rate_table *rt;
522 int ch, allrates, isShortPreamble, isShort;
525 ieee80211_phy_init();
530 + sizeof(struct ieee80211_frame)
536 while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
542 frameLen = strtoul(optarg, NULL, 0);
548 frate = atof(optarg);
549 rate = (int) 2*frate;
559 rt = mode2table(mode);
561 errx(-1, "unknown mode %s", mode);
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);
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");
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,
578 printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
579 time, frameLen, srate(rate),
580 isShort ? "short" : "long");