]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/fwp_admctrl.c
Bitrate increased to 22 Mbps, added utilization export for GUI
[frescor/fwp.git] / fwp / mngr / fwp_admctrl.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FWP (Frescor WLAN Protocol)                      */
26 /*                                                                        */
27 /* FWP is free software; you can redistribute it and/or modify it         */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FWP is distributed in the hope that it will be         */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FWP; see file        */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FWP header files in a file,          */
39 /* instantiating FWP generics or templates, or linking other files        */
40 /* with FWP objects to produce an executable application, does not        */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46 #include <fres_sa_scenario.h>
47 #include <fwp.h>
48 #include "fwp_idl.h"
49 #include "fwp_admctrl.h"
50
51 /* TODO: Find out the real value and determine what influences it (MTU
52  *       of the interface minus header sizes?).  */
53 #define MTU 1472
54 #define UDP_HEADER_SIZE 8
55 #define IP_HEADER_SIZE 20
56 #define LLC_HEADER_SIZE 2       /* ??? */
57 #define MAC_HEADER_SIZE 26      /* With QoS field added */
58 #define MAC_FCS_SIZE 4
59
60 #define RTS_FRAME_SIZE 20
61 #define CTS_FRAME_SIZE 14
62 #define ACK_FRAME_SIZE 14
63
64 #define ASLOTTIME_USEC 20       /* 802.11g-2003 p. 46 */
65 #define ASIFSTIME_USEC 10
66 #define ADIFSTIME_USEC (ASIFSTIME_USEC + 2*ASLOTTIME_USEC)
67 #define AAIFSTIME_USEC(n) (ASIFSTIME_USEC + (n)*ASLOTTIME_USEC)
68
69 /* Default values from 802.11e */
70 const int aifsn[FWP_AC_NUM] = { 2, 2, 3, 7 };
71 const int cwmin[FWP_AC_NUM] = { 3, 7, 15, 15 };
72
73 /* Experimental konstants - weight of backoff */
74 const int pisvejc[FWP_AC_NUM] = { 6, 5, 2, 2 };
75
76 int fwp_reserved_utilization;
77
78 /**
79  * Calucaltes frame duration in microseconds. If the real duration is
80  * represented by a fractional number, the value is rounded up.
81  *
82  * @param length Number of bytes in PSDU.
83  * @param rate_bps Transmit rate of PSDU. The rate should correspond
84  *            to other parameters accoring to 19.3.2 (802.11g)
85  * @param erp_ofdm Whether Extended Rate PHY (part of 802.11g) and ERP-OFDM
86  *            modulation is used.
87  * @param short_preamble Whether short preamble (HR/DSSS/short) is
88  *            used (802.11b)
89  *
90  * @return The number of microseconds or a negative number in case of error.
91  */
92 static int frame_duration(uint16_t length, int rate_bps, bool erp_ofdm, bool short_preamble)
93 {
94         uint32_t duration_usec = 0;
95
96         if (!erp_ofdm) {
97                 duration_usec = ((int64_t)(length) * 8 * SEC_TO_USEC / rate_bps);
98                 if (short_preamble) {
99                         /* For HR/DSSS/short (2, 5.5 and 11 Mbit,
100                          * 802.11b-1999), also for DSSS-OFDM rates and
101                          * ERP-PBCC rates.
102                          * Preamble sent at 1 MBit, header at 2 Mbit */
103                         duration_usec += 72/*bits*/+48/*bits*//2;
104                 } else {
105                         /* For DSSS PHY (1 and 2 Mbit rates,
106                          * 802.11-1999) and for DSS-OFDM and ERP-PBCC
107                          * rates. Always sent at 1 MBit */
108                         duration_usec += 144/*bits*/+48/*bits*/;
109                 }
110         } else {
111 #define MBIT_TO_INDEX(rate_Mbps)  ((rate_Mbps)/3 - 2)
112                 const int N_dbps[] = {
113                         [MBIT_TO_INDEX(6)] = 24,
114                         [MBIT_TO_INDEX(9)] = 36,
115                         [MBIT_TO_INDEX(12)] = 48,
116                         [MBIT_TO_INDEX(18)] = 72,
117                         [MBIT_TO_INDEX(24)] = 96,
118                         [MBIT_TO_INDEX(36)] = 144,
119                         [MBIT_TO_INDEX(48)] = 192,
120                         [MBIT_TO_INDEX(54)] = 216
121                 };
122                 int rate_idx = MBIT_TO_INDEX(rate_bps/1000/1000);
123 #undef MBIT_TO_INDEX
124                 if (rate_idx < 0 ||
125                     rate_idx >= sizeof(N_dbps)/sizeof(*N_dbps) ||
126                     N_dbps[rate_idx] == 0)
127                         return -1;
128
129                 duration_usec += 16 + 4; /* Preamble, SIGNAL */
130                 int bits =
131                         16 +        /* SERVICE */
132                         length * 8 +
133                         6;          /* tail bits */
134                 int Nsym = (bits + N_dbps[rate_idx] - 1)/N_dbps[rate_idx];
135
136                 duration_usec += Nsym * 4;
137
138                 duration_usec += 6; /* signal extension */
139         }
140         return duration_usec;
141 }
142
143 int fwp_admctrl_utilization(struct fres_sa_scenario *scenario, void *priv,
144                                 bool *schedulable)
145 {
146         int utilization = 0;
147
148         const int rate = 22*1000*1000;
149         const bool erp_ofdm = false;
150         const bool short_preamble = false;
151         struct fres_sa_contract *c, *c_new = NULL;
152         long int period_usec;
153         fres_block_basic *basic;
154         fres_block_fwp_sched *fwp_sched;
155
156         size_t bytes; 
157         long int duration_usec, tmp_usec;
158         int fragments;
159         int ac = FWP_AC_VO; 
160         
161         fres_sa_scenario_for_each_contract(scenario, c) {
162                 fres_block_basic *basic;
163                 char id[40];
164                 fres_contract_id_to_string(id, &c->contract->id, sizeof(id));
165                 basic = fres_contract_get_basic(c->contract);
166
167                 frsh_network_budget_to_bytes(FRSH_NETPF_FWP,&basic->budget,&bytes);
168                 printf("  processing: id=%s, period=%ld ms, budget=%lu bytes\n",
169                        id,
170                        fosa_rel_time_to_msec(basic->period),
171                        (long unsigned int)bytes);
172
173                 /* Calculate protocol overhead */
174                 fragments = (bytes + MTU - 1) / MTU;
175
176                 if (fragments == 0)
177                         continue;
178
179                 const int data_overhead = UDP_HEADER_SIZE + IP_HEADER_SIZE +
180                         LLC_HEADER_SIZE + MAC_FCS_SIZE;
181
182                 duration_usec = frame_duration(data_overhead + bytes%MTU, rate, 
183                                                 erp_ofdm, short_preamble);
184                 tmp_usec = frame_duration(data_overhead + MTU, rate, erp_ofdm, 
185                                                 short_preamble);
186                 duration_usec += tmp_usec*(fragments-1);
187                 /* Add average backoff - assume there is no collision */
188                 tmp_usec = ASLOTTIME_USEC*fragments*pisvejc[ac];
189                 duration_usec += (aifsn[ac] + cwmin[ac]/2)*tmp_usec;
190                 /* We use ACK and ignore burst */
191                 tmp_usec = frame_duration(ACK_FRAME_SIZE, rate, erp_ofdm, 
192                                         short_preamble) + ASIFSTIME_USEC; 
193                 duration_usec += fragments * tmp_usec; 
194
195                 /* TODO: If STA-to-STA, multiply it by two. Note that
196                  * AP may use different values for backoff. */
197                 duration_usec *= 2; /* For demo, we have always STA-to-STA */
198
199                 basic = fres_contract_get_basic(c->contract);
200                 period_usec = fosa_rel_time_to_msec(basic->period)*1000;
201                 if (c->contract == c->new) {
202                         c_new = c;
203                         if (period_usec == 0) {
204                                 goto not_schedulable;
205                         }
206                 }
207                 utilization += (long long)(duration_usec * 10000) / period_usec;
208         }
209         
210         if (!c_new) {
211                 *schedulable = false;
212                 return -1;      
213         }       
214         if (utilization >= 10000 * 96/100) {
215                 goto not_schedulable;
216         }
217
218         scenario->utilization = utilization/100; /* For GUI */
219         
220         basic = fres_contract_get_basic(c_new->contract);
221         /*int d = c->deadline_usec;*/
222         long int d = fosa_rel_time_to_msec(basic->period)*1000;         
223         if (d <= 0) d = 100*1000*1000;
224         if (d < 50000) {
225                 goto not_schedulable;
226         };
227         
228         *schedulable = true;
229         fwp_sched = malloc(sizeof(*fwp_sched));
230         fwp_sched->ac_id =
231                         d <  100*1000 ? FWP_AC_VO :
232                         d <  500*1000 ? FWP_AC_VI :
233                         d < 1000*1000 ? FWP_AC_BE :
234                         FWP_AC_BK;
235         fres_contract_add_block(c_new->contract, FRES_BLOCK_FWP_SCHED, fwp_sched);      
236         return 0;
237
238 not_schedulable:
239         *schedulable = false;
240         return 0;
241 }