-
Notifications
You must be signed in to change notification settings - Fork 0
/
weightedFairQueuingScheduler.c
237 lines (206 loc) · 5.78 KB
/
weightedFairQueuingScheduler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#pragma warning(disable: 4996)
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "weightedFairQueuingScheduler.h"
static unsigned long long time;
static unsigned long long transmitting;
static Queue* incoming_packets;
static Packet* next_packet;
//global variable for the current round of the system according to the incoming packets
//used only for round calculations for each recive packet
static Round last_round;
/*
line format:
time Sadd Sport Dadd Dport length [weight]
*/
void parseLine(Packet* p, char* line)
{
int parameterIndex = 0;
char * pch;
char * net = NULL;
p->weight = -1; // weight default
p->net_data = malloc(sizeof(Net));
pch = strtok(line, " ");
while (pch != NULL)
{
if (parameterIndex == 0)
p->time = atol(pch);
if (parameterIndex == 1)
p->net_data->src_addr.S_un.S_addr = inet_addr(pch);
if (parameterIndex == 2)
p->net_data->src_port = atoi(pch);
if (parameterIndex == 3)
p->net_data->dst_addr.S_un.S_addr = inet_addr(pch);
if (parameterIndex == 4)
p->net_data->dst_port = atoi(pch);
if (parameterIndex == 5)
p->length = atoi(pch);
if (parameterIndex == 6)
p->weight = atoi(pch);
pch = strtok(NULL, " ");
parameterIndex++;
}
}
bool checkRoundValid(Packet* p)
{
//get the next packet to send from virtual heap
Packet* next_transmitter_p = showNextPacketToTransmit(TRUE);
if (next_transmitter_p == NULL)
return TRUE;
if (next_transmitter_p->finish_time < p->round_val)
return FALSE;
else
return TRUE;
}
/*
calc arrival time for the packet using last round
*/
void calcRound(Packet* p)
{
long active_links_weights = buffer_getTotalWeight(); //get weight of the virtual buffer
if (active_links_weights == 0)
p->round_val = last_round.round_val + (double)(p->time - last_round.round_time);
else
p->round_val = last_round.round_val + (double)(p->time - last_round.round_time) / active_links_weights;
}
/*
calc last_pi of current packet
*/
void calcFinishTime(Packet* p)
{
//get relevent flow and extract the last_p(i-1) of the previous packet from the virtual
Flow* packet_flow = findFlow(p,TRUE);
if (packet_flow == NULL)
{
int weight = 1;
if (p->weight > 0)
weight = p->weight;
p->finish_time = MAX(last_round.round_val, 0.0) + (double)p->length / weight;
}
else
{
int weight = 0;
if (p->weight > 0)
weight = p->weight;
else
weight = packet_flow->weight;
p->finish_time = MAX(last_round.round_val, packet_flow->last) + (double)p->length / weight;
packet_flow->last = p->finish_time;
}
}
/*
check if our real time heap is empty and there is nothing to transmit
*/
bool buffer_isIdle()
{
if (transmitting == 0 && buffer_isEmpty(FALSE) == TRUE)
return TRUE;
return FALSE;
}
/*
scan the incoming packets
for each packet :
- calc round (with packet delta)
- showNext packet to transmit - check if here finish time is smaller
- if not - insert to buffer and calc lastpi
- if yes - remove packet from the virtual heap and recalc round time
*/
void HandleInputPackets()
{
bool packetHandled = FALSE;
Packet* packet_pointer = (Packet*)queue_front(incoming_packets);
while (packet_pointer)
{
calcRound(packet_pointer);
//while current round is bigger then last_pi of virtual packet
//remove the virtual packet from buffer
while (checkRoundValid(packet_pointer) == FALSE)
{
//extract real delta and
long weights = buffer_getTotalWeight();
Packet* next_virtual_p = removePacketFromBuffer(TRUE);
double x = (next_virtual_p->finish_time - last_round.round_val)*weights;
//update last round
last_round.round_val = next_virtual_p->finish_time;
last_round.round_time += x;
free(next_virtual_p);
//calc round again with correct weights:
calcRound(packet_pointer);
}
//packet round is valid, update round global
last_round.round_time = packet_pointer->time;
last_round.round_val = packet_pointer->round_val;
calcFinishTime(packet_pointer);
//insert new packet to both virtual heap and real time heap
buffer_write(packet_pointer, TRUE);
buffer_write(packet_pointer,FALSE);
dequeue(incoming_packets);
//after dequeue we can free the packet
free(packet_pointer);
packet_pointer = (Packet*)queue_front(incoming_packets);
}
}
void transmitPacket(Packet pkt)
{
char* add = inet_ntoa(pkt.net_data->src_addr);
printf("%lld: %lld %s %hu ", time, pkt.time, add, pkt.net_data->src_port);
add = inet_ntoa(pkt.net_data->dst_addr);
if (pkt.weight != -1)
printf("%s %hu %u %ld\n", add, pkt.net_data->dst_port, pkt.length, pkt.weight);
else
printf("%s %hu %u\n", add, pkt.net_data->dst_port, pkt.length);
transmitting = pkt.length;
}
bool parsePackets()
{
char line[INPUT_SIZE];
if (time == 0)
{
next_packet = (Packet*)malloc(sizeof(Packet));
if (fgets(line, INPUT_SIZE, stdin) != NULL)
parseLine(next_packet, line); //fill in packet
else
return FALSE;
}
while (next_packet->time == time)
{
enqueue(incoming_packets, next_packet);
next_packet = (Packet*)malloc(sizeof(Packet));
if (fgets(line, INPUT_SIZE, stdin) != NULL)
parseLine(next_packet, line); //fill in packet
else
return FALSE;
}
return TRUE;
}
int main(void)
{
bool input = TRUE;
Packet* packet_to_transmit = NULL;
InitFlowBuffer();
time = 0;
transmitting = 0;
incoming_packets = create_queue();
do
{
if (input)
input = parsePackets();
HandleInputPackets();
// handle output
if (transmitting == 0 && !buffer_isEmpty(FALSE))
{
//remove packet from real heap
packet_to_transmit = removePacketFromBuffer(FALSE);
transmitPacket(*packet_to_transmit);
free(packet_to_transmit);
}
// advance time
time++;
if (transmitting > 0)
transmitting--;
} while (!buffer_isEmpty(FALSE) || input || transmitting != 0 || !queue_isEmpty(incoming_packets));
freeFlows();
queue_free(incoming_packets);
free(next_packet);
return 0;
}