Subversion Repositories shark

Rev

Rev 875 | Rev 880 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
668 trimarchi 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Trimarchi Michael   <trimarchi@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000 Giorgio Buttazzo, Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 *
877 trimarchi 37
 * CVS :        $Id: server-task.c,v 1.8 2004-11-08 10:55:59 trimarchi Exp $
668 trimarchi 38
 */
39
 
679 trimarchi 40
#include "ll/i386/64bit.h"
868 trimarchi 41
#include "fsf_configuration_parameters.h"
42
#include "fsf_core.h"
43
#include "fsf_server.h"
44
#include "fsf_service_task.h"
45
#include "message.h"
668 trimarchi 46
#include <kernel/kern.h>
679 trimarchi 47
#include <modules/sem.h>
48
#include <modules/hartport.h>
668 trimarchi 49
#include <modules/cabs.h>
50
 
868 trimarchi 51
//#define FSF_DEBUG
52
 
53
TASK service_task(void) {
679 trimarchi 54
 
868 trimarchi 55
  struct mess msg;
56
  fsf_renegotiation_status_t status;
57
  int wait_renegotiation=0;
679 trimarchi 58
  PORT rec,tra;
868 trimarchi 59
 
679 trimarchi 60
  rec = port_connect("CHANW",sizeof(struct mess),STREAM,READ);
668 trimarchi 61
 
679 trimarchi 62
  tra = port_connect("CHANR",sizeof(struct mess),STREAM,WRITE);
668 trimarchi 63
 
64
  while(1) {
868 trimarchi 65
    // wait for operation
66
    if (wait_renegotiation) {
67
      if (!fsf_get_renegotiation_status(msg.server,&status)) {
68
        if (status==FSF_ADMITTED) {
69
          // send response server is -1 if the operation fail
70
#ifdef FSF_DEBUG
71
          kern_printf("ADM");
72
#endif
73
          wait_renegotiation=0;
74
          port_send(tra,&msg,BLOCK);
75
          //task_endcycle();
76
          continue;
77
        } else
78
#ifdef FSF_DEBUG
79
          kern_printf("WAIT")
80
#endif
81
;
82
      }
83
    } else {
84
      port_receive(rec,&msg,BLOCK);
877 trimarchi 85
#ifdef FSF_DEBUG
868 trimarchi 86
      kern_printf("Message ");
877 trimarchi 87
#endif
868 trimarchi 88
      switch (msg.type) {
89
 
90
      case NEGOTIATE_CONTRACT:
91
        if (negotiate_contract(&msg.contract, &msg.server)==FSF_ERR_CONTRACT_REJECTED)
92
          msg.server=-1;       
93
        break;
94
 
95
      case REQUEST_RENEGOTIATE_CONTRACT:
96
        if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
97
          msg.server=-1;
98
        break;
99
 
100
      case RENEGOTIATE_CONTRACT:
101
        if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
102
          msg.server=-1;
103
        else {
104
          wait_renegotiation=1;
105
          //task_endcycle();
106
          continue;
107
        }
108
        break;                   
109
 
110
      default :
111
        break;
112
 
113
      }
114
      // send response server is -1 if the operation fail
115
      port_send(tra,&msg,BLOCK);
116
      //task_endcycle();
668 trimarchi 117
 
118
    }
119
  }
120
}
121
 
688 trimarchi 122
 
123
static __inline void set_contract_parameter(void) {
124
 
125
  int i=0;
679 trimarchi 126
  TIME T,Q;
868 trimarchi 127
 
128
  for (i=0; i<current_server; i++) {
679 trimarchi 129
      mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
130
      if (T > server_list[i].Tmin ) {
131
         server_list[i].actual_budget = server_list[i].Cmin;
132
         server_list[i].actual_period = T;
133
      #ifdef FSF_DEBUG
134
         kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
135
      #endif
136
         if (server_list[i].d_equals_t == TRUE)
137
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
138
         else
139
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
140
 
141
      } else {
142
        mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
143
        server_list[i].actual_budget = Q;
144
        server_list[i].actual_period = server_list[i].Tmin;
145
      #ifdef FSF_DEBUG
146
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
147
      #endif
148
 
149
         if (server_list[i].d_equals_t == TRUE)
150
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].Tmin);
151
         else
152
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].deadline);                                                                                                        
153
     }
154
 
155
     server_list[i].U=server_list[i].Umin;
156
   }
157
 
688 trimarchi 158
}
159
 
160
 
161
int renegotiate_contract
162
  (const fsf_contract_parameters_t *new_contract,
163
   fsf_server_id_t                 server)
164
{
165
 
166
  #ifdef FSF_DEBUG
167
    kern_printf("(Renegotiate for server %d)",server);
168
  #endif
169
 
170
  if (!new_contract)
868 trimarchi 171
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 172
 
173
  if (server < 0)
875 trimarchi 174
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 175
 
176
   // change the parameter
177
   relink_contract_to_server(new_contract, server);
178
   if (recalculate_contract(fsf_max_bw)==-1)  {
875 trimarchi 179
       return  FSF_ERR_CONTRACT_REJECTED;
688 trimarchi 180
   }
181
 
182
   set_contract_parameter();
183
 
679 trimarchi 184
   return 0;
185
}
186
 
668 trimarchi 187
int negotiate_contract
679 trimarchi 188
  (const fsf_contract_parameters_t *contract,
189
   fsf_server_id_t                 *server)
668 trimarchi 190
{
191
  /* Check if contract is initialized */
868 trimarchi 192
  if (!contract) return  FSF_ERR_BAD_ARGUMENT;
668 trimarchi 193
 
194
  /* Admission Test */
195
  if (FSF_ADMISSION_TEST_IS_ENABLED)
196
    if (add_contract(contract))
197
      return FSF_ERR_CONTRACT_REJECTED;
198
 
679 trimarchi 199
  /* SERVER => BUDGET */    
200
  set_SERVER_budget_from_contract(contract,server);
668 trimarchi 201
 
688 trimarchi 202
#ifdef FSF_DEBUG
203
  kern_printf("(New Server %d)",*server);
204
#endif
679 trimarchi 205
 
206
  if (*server >= 0) {
207
    link_contract_to_server(contract,*server);
208
    if (recalculate_contract(fsf_max_bw)==-1)  {
868 trimarchi 209
      remove_contract(server);
875 trimarchi 210
      return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 211
    }
688 trimarchi 212
    set_contract_parameter();
679 trimarchi 213
#ifdef  FSF_DEBUG
214
    kern_printf("(Adjust budget)");
215
#endif    
688 trimarchi 216
 
679 trimarchi 217
  }
218
  else  {
875 trimarchi 219
    return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 220
  }
688 trimarchi 221
 
679 trimarchi 222
  return 0;
223
 
668 trimarchi 224
}
225