]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/fwp_admctrl.c
Allow bypassing FWP by setting FWP_BYPASS environment variable
[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 #include <ul_log.h>
51 #include <ul_logreg.h>
52 #include "fwp_utils.h"
53
54 UL_LOG_CUST(ulogd_fwp_admctrl);
55 ul_log_domain_t ulogd_fwp_admctrl = {UL_LOGL_MSG, "fwp_admctrl"};
56 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(fwp_admctrl_logreg_domains, ulogd_fwp_admctrl);
57
58 /* TODO: Find out the real value and determine what influences it (MTU
59  *       of the interface minus header sizes?).  */
60 #define MTU 1472
61 #define UDP_HEADER_SIZE 8
62 #define IP_HEADER_SIZE 20
63 #define LLC_HEADER_SIZE 2       /* ??? */
64 #define MAC_HEADER_SIZE 26      /* With QoS field added */
65 #define MAC_FCS_SIZE 4
66
67 #define RTS_FRAME_SIZE 20
68 #define CTS_FRAME_SIZE 14
69 #define ACK_FRAME_SIZE 14
70
71 #define ASLOTTIME_USEC 20       /* 802.11g-2003 p. 46 */
72 #define ASIFSTIME_USEC 10
73 #define ADIFSTIME_USEC (ASIFSTIME_USEC + 2*ASLOTTIME_USEC)
74 #define AAIFSTIME_USEC(n) (ASIFSTIME_USEC + (n)*ASLOTTIME_USEC)
75
76 /* Default values from 802.11e */
77 const int aifsn[FWP_AC_NUM] = { 2, 2, 3, 7 };
78 const int cwmin[FWP_AC_NUM] = { 3, 7, 15, 15 };
79
80 /* Experimental konstants - weight of backoff */
81 const int pisvejc[FWP_AC_NUM] = { 6, 5, 2, 2 };
82
83 int fwp_reserved_utilization;
84
85 /**
86  * Calucaltes frame duration in microseconds. If the real duration is
87  * represented by a fractional number, the value is rounded up.
88  *
89  * @param length Number of bytes in PSDU.
90  * @param rate_bps Transmit rate of PSDU. The rate should correspond
91  *            to other parameters accoring to 19.3.2 (802.11g)
92  * @param erp_ofdm Whether Extended Rate PHY (part of 802.11g) and ERP-OFDM
93  *            modulation is used.
94  * @param short_preamble Whether short preamble (HR/DSSS/short) is
95  *            used (802.11b)
96  *
97  * @return The number of microseconds or a negative number in case of error.
98  */
99 static int frame_duration(uint16_t length, int rate_bps, bool erp_ofdm, bool short_preamble)
100 {
101         uint32_t duration_usec = 0;
102
103         if (!erp_ofdm) {
104                 duration_usec = ((int64_t)(length) * 8 * SEC_TO_USEC / rate_bps);
105                 if (short_preamble) {
106                         /* For HR/DSSS/short (2, 5.5 and 11 Mbit,
107                          * 802.11b-1999), also for DSSS-OFDM rates and
108                          * ERP-PBCC rates.
109                          * Preamble sent at 1 MBit, header at 2 Mbit */
110                         duration_usec += 72/*bits*/+48/*bits*//2;
111                 } else {
112                         /* For DSSS PHY (1 and 2 Mbit rates,
113                          * 802.11-1999) and for DSS-OFDM and ERP-PBCC
114                          * rates. Always sent at 1 MBit */
115                         duration_usec += 144/*bits*/+48/*bits*/;
116                 }
117         } else {
118 #define MBIT_TO_INDEX(rate_Mbps)  ((rate_Mbps)/3 - 2)
119                 const int N_dbps[] = {
120                         [MBIT_TO_INDEX(6)] = 24,
121                         [MBIT_TO_INDEX(9)] = 36,
122                         [MBIT_TO_INDEX(12)] = 48,
123                         [MBIT_TO_INDEX(18)] = 72,
124                         [MBIT_TO_INDEX(24)] = 96,
125                         [MBIT_TO_INDEX(36)] = 144,
126                         [MBIT_TO_INDEX(48)] = 192,
127                         [MBIT_TO_INDEX(54)] = 216
128                 };
129                 int rate_idx = MBIT_TO_INDEX(rate_bps/1000/1000);
130 #undef MBIT_TO_INDEX
131                 if (rate_idx < 0 ||
132                     rate_idx >= sizeof(N_dbps)/sizeof(*N_dbps) ||
133                     N_dbps[rate_idx] == 0)
134                         return -1;
135
136                 duration_usec += 16 + 4; /* Preamble, SIGNAL */
137                 int bits =
138                         16 +        /* SERVICE */
139                         length * 8 +
140                         6;          /* tail bits */
141                 int Nsym = (bits + N_dbps[rate_idx] - 1)/N_dbps[rate_idx];
142
143                 duration_usec += Nsym * 4;
144
145                 duration_usec += 6; /* signal extension */
146         }
147         return duration_usec;
148 }
149
150 int fwp_admctrl_utilization(struct fres_sa_scenario *scenario, void *priv,
151                                 bool *schedulable)
152 {
153         int utilization = 0;
154         struct frm_fwp_priv *pr = priv;
155         const int rate = pr->rate_mbps*1000*1000;
156         struct fres_sa_contract *c;
157         long int period_usec;
158         fres_block_fwp_sched *fwp_sched;
159
160         size_t bytes; 
161         long int duration_usec, tmp_usec;
162         int fragments;
163         int ac = FWP_AC_VO; 
164         
165         fres_sa_scenario_for_each_no_cancel_contract(scenario, c) {
166                 fres_block_basic *basic;
167                 char id[40];
168                 fres_contract_id_to_string(id, &c->contract->id, sizeof(id));
169                 basic = fres_contract_get_basic(c->contract);
170
171                 frsh_network_budget_to_bytes(FRSH_NETPF_FWP,&basic->budget,&bytes);
172                 ul_logmsg("processing: id=%s, period=%ld ms, budget=%lu bytes\n",
173                        id,
174                        fosa_rel_time_to_msec(basic->period),
175                        (long unsigned int)bytes);
176
177                 period_usec = basic ? fosa_rel_time_to_msec(basic->period)*1000 : 0;
178
179                 /* Calculate protocol overhead */
180                 fragments = (bytes + MTU - 1) / MTU;
181
182                 if (fragments == 0 && period_usec != 0)
183                         continue;
184
185                 const int data_overhead = UDP_HEADER_SIZE + IP_HEADER_SIZE +
186                         LLC_HEADER_SIZE + MAC_FCS_SIZE;
187
188                 duration_usec = frame_duration(data_overhead + bytes%MTU, rate, 
189                                                pr->erp_ofdm, pr->short_preamble);
190                 tmp_usec = frame_duration(data_overhead + MTU, rate,
191                                           pr->erp_ofdm, pr->short_preamble);
192                 duration_usec += tmp_usec*(fragments-1);
193                 /* Add average backoff - assume there is no collision */
194                 tmp_usec = ASLOTTIME_USEC*fragments*pisvejc[ac];
195                 duration_usec += (aifsn[ac] + cwmin[ac]/2)*tmp_usec;
196                 /* We use ACK and ignore burst */
197                 tmp_usec = frame_duration(ACK_FRAME_SIZE, rate,
198                                           pr->erp_ofdm, pr->short_preamble)
199                         + ASIFSTIME_USEC; 
200                 duration_usec += fragments * tmp_usec; 
201                 //printf("duration: %ld ms\n", duration_usec/1000);
202
203                 /* TODO: If STA-to-STA, multiply it by two. Note that
204                  * AP may use different values for backoff. */
205                 duration_usec *= 2; /* For demo, we have always STA-to-STA */
206
207                 if (c->contract == c->new) {
208                         if (period_usec == 0) {
209                                 ul_logmsg("Period is zero!\n");
210                                 goto not_schedulable;
211                         }
212
213                         frsh_rel_time_t deadline;
214                         long int d;
215                         if (fres_contract_get_deadline(&c->contract, &deadline)) {
216                                 d = fosa_rel_time_to_msec(deadline)*1000;
217                         } else {
218                                 d = 100/*sec*/*1000*1000;
219                         }
220                         if (d < 30/*msec*/*1000) {
221                                 ul_logmsg("Deadline shorter than 30 ms!\n");
222                                 goto not_schedulable;
223                         };
224                         
225                         fwp_sched = malloc(sizeof(*fwp_sched));
226                         fwp_sched->ac_id =
227                                 d <  100*1000 ? FWP_AC_VO :
228                                 d <  500*1000 ? FWP_AC_VI :
229                                 d < 1000*1000 ? FWP_AC_BE :
230                                 FWP_AC_BK;
231                         fres_contract_add_block(c->contract, FRES_BLOCK_FWP_SCHED, fwp_sched);
232                 }
233
234                 utilization += (long long)(duration_usec * 10000) / period_usec;
235         }
236         
237         if (utilization >= 10000 * 96/100 && !pr->bypass) {
238                 goto not_schedulable;
239         }
240         scenario->utilization = utilization/100; /* For GUI */
241
242         ul_logmsg("accepted\n");
243         *schedulable = true;
244         return 0;
245
246 not_schedulable:
247         ul_logmsg("rejected\n");
248         *schedulable = false;
249         return 0;
250 }