8 /* TODO: Find out the real value and determine what influences it (MTU
9 * of the interface minus header sizes?). */
11 #define UDP_HEADER_SIZE 8
12 #define IP_HEADER_SIZE 20
13 #define LLC_HEADER_SIZE 2 /* ??? */
14 #define MAC_HEADER_SIZE 26 /* With QoS field added */
15 #define MAC_FCS_SIZE 4
17 #define RTS_FRAME_SIZE 20
18 #define CTS_FRAME_SIZE 14
19 #define ACK_FRAME_SIZE 14
21 #define BITRATE_BPS (1000*1000)
22 #define ASLOTTIME_USEC 20 /* 802.11g-2003 p. 46 */
23 #define ASIFSTIME_USEC 10
24 #define ADIFSTIME_USEC (ASIFSTIME_USEC + 2*ASLOTTIME_USEC)
25 #define AAIFSTIME_USEC(n) (ASIFSTIME_USEC + (n)*ASLOTTIME_USEC)
27 #define TXTIME_USEC(bytes) ((int64_t)(bytes) * 8 * SEC_TO_USEC / BITRATE_BPS)
30 /* Default values from 802.11e */
31 const int aifsn[FWP_AC_NUM] = { 2, 2, 3, 7 };
32 const int cwmin[FWP_AC_NUM] = { 3, 7, 15, 15 };
34 /* Experimental konstants - weight of backoff */
35 const int pisvejc[FWP_AC_NUM] = { 6, 5, 2, 2 };
39 * Calucaltes frame duration in microseconds. If the real duration is
40 * represented by a fractional number, the value is rounded up.
42 * @param length Number of bytes in PSDU.
43 * @param rate_bps Transmit rate of PSDU. The rate should correspond
44 * to other parameters accoring to 19.3.2 (802.11g)
45 * @param erp_ofdm Whether Extended Rate PHY (part of 802.11g) and ERP-OFDM
47 * @param short_preamble Whether short preamble (HR/DSSS/short) is
50 * @return The number of microseconds or a negative number in case of error.
52 static int frame_duration(uint16_t length, int rate_bps, bool erp_ofdm, bool short_preamble)
54 uint32_t duration_usec;
57 duration_usec = ((int64_t)(length) * 8 * SEC_TO_USEC / rate_bps);
59 /* For HR/DSSS/short (2, 5.5 and 11 Mbit,
60 * 802.11b-1999), also for DSSS-OFDM rates and
62 * Preamble sent at 1 MBit, header at 2 Mbit */
63 duration_usec += 72/*bits*/+48/*bits*//2;
65 /* For DSSS PHY (1 and 2 Mbit rates,
66 * 802.11-1999) and for DSS-OFDM and ERP-PBCC
67 * rates. Always sent at 1 MBit */
68 duration_usec += 144/*bits*/+48/*bits*/;
71 #define MBIT_TO_INDEX(rate_Mbps) ((rate_Mbps)/3 - 2)
72 const int N_dbps[] = {
73 [MBIT_TO_INDEX(6)] = 24,
74 [MBIT_TO_INDEX(9)] = 36,
75 [MBIT_TO_INDEX(12)] = 48,
76 [MBIT_TO_INDEX(18)] = 72,
77 [MBIT_TO_INDEX(24)] = 96,
78 [MBIT_TO_INDEX(36)] = 144,
79 [MBIT_TO_INDEX(48)] = 192,
80 [MBIT_TO_INDEX(54)] = 216
82 int rate_idx = MBIT_TO_INDEX(rate_bps/1000/1000);
85 rate_idx >= sizeof(N_dbps)/sizeof(*N_dbps) ||
86 N_dbps[rate_idx] == 0)
89 duration_usec += 16 + 4; /* Preamble, SIGNAL */
94 int Nsym = (bits + N_dbps[rate_idx] - 1)/N_dbps[rate_idx];
96 duration_usec += Nsym * 4;
98 duration_usec += 6; /* signal extension */
100 return duration_usec;
103 int fwp_adm_test(struct fwp_ctable *ct)
108 const int rate = 1*1000*1000;
109 const bool erp_ofdm = false;
110 const bool short_preamble = false;
112 for (i = 0; i < ct->nr_contract; i++) {
113 int bytes, duration_usec, fragments;
114 struct fwp_contract *contract = &ct->entry[i].contract;
115 int ac = FWP_AC_VO; /* FIXME: assign to AC before this
116 * loop based on deadlines */
117 ac = contract->ac_id;
119 /* Calculate protocol overhead */
121 bytes = contract->budget;
122 fragments = (bytes + MTU - 1) / MTU;
127 const int data_overhead = UDP_HEADER_SIZE + IP_HEADER_SIZE +
128 LLC_HEADER_SIZE + MAC_FCS_SIZE;
130 duration_usec = frame_duration(data_overhead + bytes%MTU, rate, erp_ofdm, short_preamble);
131 duration_usec += frame_duration(data_overhead + MTU, rate, erp_ofdm, short_preamble)*(fragments-1);
133 /* Add average backoff - assume there is no collision */
134 duration_usec += (aifsn[ac] + cwmin[ac]/2)*ASLOTTIME_USEC*fragments*pisvejc[ac];
135 /* We use ACK and ignore burst */
136 duration_usec += fragments * (ASIFSTIME_USEC +
137 frame_duration(ACK_FRAME_SIZE, rate, erp_ofdm, short_preamble));
139 /* TODO: If STA-to-STA, multiply it by two. Note that
140 * AP may use different values for backoff. */
141 if (contract->period_usec == 0) return FWP_CNT_REJECTED;
142 utilization += (long long)(duration_usec * 10000) / contract->period_usec;
144 if (utilization >= 10000 * 96/100) {
145 return FWP_CNT_REJECTED;
147 return FWP_CNT_NEGOTIATED;