]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_bwres.c
changes in configuration file
[frescor/fna.git] / src_frescan / frescan_bwres.c
1 /*!
2  * @file frescan_bwres.c
3  *
4  * @brief FRESCAN bandwidth reservation layer
5  *
6  * This module contains function to negotiate contracts and get the
7  * corresponding frescan sporadic servers.
8  *
9  * @version 0.01
10  *
11  * @date 1-Apr-2008
12  *
13  * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
14  *
15  * @license
16  *
17  * -----------------------------------------------------------------------
18  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
19  *
20  *    Universidad de Cantabria,              SPAIN
21  *    University of York,                    UK
22  *    Scuola Superiore Sant'Anna,            ITALY
23  *    Kaiserslautern University,             GERMANY
24  *    Univ. Politécnica  Valencia,           SPAIN
25  *    Czech Technical University in Prague,  CZECH REPUBLIC
26  *    ENEA                                   SWEDEN
27  *    Thales Communication S.A.              FRANCE
28  *    Visual Tools S.A.                      SPAIN
29  *    Rapita Systems Ltd                     UK
30  *    Evidence                               ITALY
31  *
32  *    See http://www.frescor.org for a link to partners' websites
33  *
34  *           FRESCOR project (FP6/2005/IST/5-034026) is funded
35  *        in part by the European Union Sixth Framework Programme
36  *        The European Union is not liable of any use that may be
37  *        made of this code.
38  *
39  *  This file is part of FRESCAN
40  *
41  *  FRESCAN is free software; you can  redistribute it and/or  modify
42  *  it under the terms of  the GNU General Public License as published by
43  *  the Free Software Foundation;  either  version 2, or (at  your option)
44  *  any later version.
45  *
46  *  FRESCAN  is distributed  in  the hope  that  it  will  be useful,  but
47  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
48  *  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
49  *  General Public License for more details.
50  *
51  *  You should have  received a  copy of  the  GNU  General Public License
52  *  distributed  with  FRESCAN;  see file COPYING.   If not,  write to the
53  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
54  *  02111-1307, USA.
55  *
56  * As a special exception, including FRESCAN header files in a file,
57  * instantiating FRESCAN generics or templates, or linking other files
58  * with FRESCAN objects to produce an executable application, does not
59  * by itself cause the resulting executable application to be covered
60  * by the GNU General Public License. This exception does not
61  * however invalidate any other reasons why the executable file might be
62  * covered by the GNU Public License.
63  * -----------------------------------------------------------------------
64  *
65  */
66
67 #include "frescan_bwres.h"
68 #include "frescan_bwres_requests.h"
69 #include "frescan_bwres_robjs.h"
70 #include "frescan_bwres_messages.h"
71 #include "frescan_bwres_threads.h"
72 #include "frescan_bwres_analysis.h"
73 #include "frescan_data.h"
74 #include "frescan_debug.h"
75 #include "frescan_config.h"
76 #include "frescan_servers.h"
77
78 /**
79  * frescan_bwres_init()
80  *
81  * Init the frescan bandwidth reservation layer
82  */
83
84 int frescan_bwres_init(frescan_network_t net)
85 {
86         int ret;
87         frescan_server_params_t  server_params;  // TODO: improve this...
88         frescan_bwres_sa_init_params_t init_params; // TODO: improve this...
89
90         init_params.max_prio = 30;
91         init_params.min_prio = 1;
92
93         if (frescan_data[net].local_node == FRESCAN_BWRES_MASTER_NODE) {
94             ret = frescan_bwres_sa_init(&frescan_data[net].scenario,
95                                         &init_params);
96             if (ret != 0) return ret;
97         }
98
99         server_params.budget = 5;
100         server_params.period.tv_sec = 1;
101         server_params.period.tv_nsec = 0;
102         server_params.prio = FRESCAN_BWRES_NEG_MESSAGES_PRIO;
103
104         ret = frescan_servers_create(net, &server_params,
105                                      &frescan_data[net].neg_messages_ss_id);
106         if (ret != 0) return ret;
107
108         // TODO: we have to add this negotiation contracts to the sa table
109
110         ret = frescan_bwres_robjs_init(FRESCAN_BWRES_ROBJS_MX_CEILING);
111         if (ret != 0) return ret;
112
113         ret = frescan_bwres_requests_init(FRESCAN_BWRES_REQ_MX_CEILING);
114         if (ret != 0) return ret;
115
116         ret = frescan_messages_init(net);
117         if (ret != 0) return ret;
118
119         ret = frescan_manager_thread_create(net);
120         if (ret != 0) return ret;
121
122         ret = frescan_acceptor_thread_create(net);
123         if (ret != 0) return ret;
124
125         return 0;
126 }
127
128 /**
129  * frescan_bwres_group_change_mode_sync()
130  *
131  * 1.- allocate and prepare the GN request
132  * 2.- enqueue the request
133  * 3.- wait in the reply object for a reply
134  *     (unless there are only cancellations)
135  * 4.- free the request
136  */
137
138 int frescan_bwres_group_change_mode_sync
139                 (frescan_network_t            net,
140                  const frsh_contracts_group_t *contracts_to_neg,
141                  const frsh_contracts_group_t *contracts_to_reneg,
142                  const frescan_ss_group_t     *ss_to_reneg,
143                  const frescan_ss_group_t     *ss_to_cancel,
144                  frescan_ss_group_t           *ss_new,
145                  bool                         *accepted)
146 {
147         int ret;
148         frescan_bwres_request_id_t req;
149         frescan_bwres_request_data_t *req_data;
150
151         DEBUG(FRESCAN_BWRES_ENABLE_DEBUG, "preparing a GN request\n");
152
153         ret = frescan_bwres_requests_alloc(&req);
154         if (ret != 0) return ret;
155
156         ret = frescan_bwres_requests_get_data(req, &req_data);
157         if (ret != 0) return ret;
158
159         req_data->net          = net;
160         req_data->type         = FRESCAN_BWRES_REQ_GN;
161         req_data->req          = req;
162         req_data->request_node = frescan_data[net].local_node;
163         req_data->contracts_to_neg   = (frsh_contracts_group_t *)
164                                                         contracts_to_neg;
165         req_data->contracts_to_reneg = (frsh_contracts_group_t *)
166                                                         contracts_to_reneg;
167         req_data->ss_to_reneg        = (frescan_ss_group_t *)ss_to_reneg;
168         req_data->ss_to_cancel       = (frescan_ss_group_t *)ss_to_cancel;
169         req_data->ss_new             = ss_new;
170
171         ret = frescan_bwres_robjs_alloc(&req_data->robj, FRESCAN_BWRES_MX_PRIO);
172         if (ret != 0) return ret;
173
174         DEBUG(FRESCAN_BWRES_ENABLE_DEBUG, "enqueue the negotiation request\n");
175
176         ret = frescan_bwres_requests_enqueue(req);
177         if (ret != 0) return ret;
178
179         DEBUG(FRESCAN_BWRES_ENABLE_DEBUG, "wait for a reply\n");
180
181         ret = frescan_bwres_robjs_wait(req_data->robj);
182         if (ret != 0) return ret;
183
184         ret = frescan_bwres_robjs_free(req_data->robj);
185         if (ret != 0) return ret;
186
187         switch (req_data->return_value) {
188                 case FRESCAN_BWRES_REQ_ACCEPTED:
189                         DEBUG(FRESCAN_BWRES_ENABLE_DEBUG, "negotiation OK\n");
190                         *accepted = true;
191                         break;
192
193                 case FRESCAN_BWRES_REQ_NOT_ACCEPTED:
194                         DEBUG(FRESCAN_BWRES_ENABLE_DEBUG, "negotiation FAIL\n");
195                         *accepted = false;
196                         break;
197
198                 default:
199                         FRESCAN_ERROR("return_value unknown\n");
200                         return -1;
201         }
202
203         frescan_bwres_requests_free(req);
204         return 0;
205 }
206
207 /**
208  * frescan_bwres_negotiate()
209  */
210
211 int frescan_bwres_negotiate(frescan_network_t net,
212                             const frsh_contract_t *contract,
213                             frescan_ss_t *ss,
214                             bool *accepted)
215 {
216         int ret;
217         frsh_contracts_group_t contracts_to_neg;
218         frescan_ss_group_t ss_new;
219
220         contracts_to_neg.size = 1;
221         contracts_to_neg.contracts[0] = *contract;
222
223         ret = frescan_bwres_group_change_mode_sync
224                         (net, &contracts_to_neg, NULL, NULL,
225                          NULL, &ss_new, accepted);
226         if (ret != 0) return ret;
227
228         if (*accepted) {
229                 *ss = ss_new.ss[0];
230         }
231
232         return 0;
233 }
234
235 /**
236  * frescan_bwres_renegotiate()
237  */
238
239 int frescan_bwres_renegotiate(frescan_network_t net,
240                               const frsh_contract_t *contract,
241                               frescan_ss_t ss,
242                               bool *accepted)
243 {
244         frsh_contracts_group_t contracts_to_reneg;
245         frescan_ss_group_t ss_to_reneg;
246
247         contracts_to_reneg.size = 1;
248         contracts_to_reneg.contracts[0] = *contract;
249
250         ss_to_reneg.size = 1;
251         ss_to_reneg.ss[0] = ss;
252
253         return frescan_bwres_group_change_mode_sync
254                 (net, NULL, &contracts_to_reneg, &ss_to_reneg,
255                  NULL, NULL, accepted);
256 }
257
258 /**
259  * frescan_bwres_cancel()
260  */
261
262 int frescan_bwres_cancel(frescan_network_t net,
263                          frescan_ss_t      ss)
264 {
265         frescan_ss_group_t ss_to_cancel;
266
267         ss_to_cancel.size = 1;
268         ss_to_cancel.ss[0] = ss;
269
270         return frescan_bwres_group_change_mode_sync
271                         (net, NULL, NULL, NULL, &ss_to_cancel, NULL, NULL);
272 }