-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cc
367 lines (327 loc) · 13.5 KB
/
App.cc
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SDN Control Traffic Simulator: simulate control traffic throughput
// and latencies in a variety of topologies
// Copyright (C) 2020 Ananya Gopal, Jesse Chen
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// You should have received a copy of the
// GNU General Public License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
#include "App.h"
#include "Routing.h"
Define_Module(App);
App::App()
{
sendPacketIn = nullptr;
}
App::~App()
{
cancelAndDelete(sendPacketIn);
}
/*
* App module has a two-stage initialization.
* */
int App::numInitStages() const {
return (APP_STAGES);
}
/*
* In the Topology, the IDs are dynamic, so we
* first populate the Controller ID.
*/
int App::getControllerID() {
cModule *network = cSimulation::getActiveSimulation()->getSystemModule();
for (SubmoduleIterator it(network); !it.end(); ++it) {
cModule * mod = *it;
if (!strcmp(mod->getName(), "controller")) {
return (mod->getId());
}
}
return (0);
}
/*
* Param explanation to understand the init fn:
* mode: We can specify the mode of the init fn.
* mode MODE_QUEUING_LATENCY - We simulate the 4 packet sequence between each node and controller.
* mode MODE_275_FLOWS: This is to simulate a realistic 275 messages from each node to the controller, per second.
* ALso gives us a realistic latency graph
*
*
* */
void App::initialize(int stage)
{
mode = par("mode");
EV << "\n\n\n" << "Initialized:" << getModuleType() << "-Ports:"
<< numPorts<< ", Parent:" << getParentModule() << ",mode:" << mode << "\n";
if (stage == 1) {
cModule *targetModule = getParentModule()->getSubmodule("routing");
Routing *target = check_and_cast<Routing *>(targetModule);
EV << "Myaddress:" << myAddress << "-" << target->queuing_DFC;
EV << "Myaddress:" << myAddress << "-" << target->queuing_DTC;
queuing_DFC = target->queuing_DFC;
queuing_DTC = target->queuing_DTC;
}
if (stage == 0) {
myAddress = par("address");
numFlows = getParentModule()->par("num_flows");
numQueues = par("num_queues");
numPorts = par("num_ports");
total_nodes = par("total_nodes");
sendPollt = &par("sendPollTime");
sendPacketInTime = &par("sendPacketInTime");
sendPacketInTime275 = &par("sendPacketInTime275");
WATCH(myAddress);
endToEndDelaySignal = registerSignal("endToEndDelay");
hopCountSignal = registerSignal("hopCount");
sourceAddressSignal = registerSignal("sourceAddress");
CONTROLLER_ID = getControllerID();
if (!CONTROLLER_ID) {
throw cRuntimeError("Invalid ID for Controller");
}
/* Send the entire Sequence */
if (getParentModule()->getId() == CONTROLLER_ID) {
amIcontroller = true;
/* We calculated the thruput by sending an entire polling sequence between
* the switch and the controller. Please check README.
* This poll message was part of that. */
//pollMessage = new cMessage("pollMessage");
//scheduleAt(simTime(), pollMessage);
}
switch (mode) {
case MODE_QUEUING_LATENCY:
/* Send 4-packet entire Sequence */
msg_id = MSG_ID_START;
if (!amIcontroller) {
/* Switch schedules a MSG every second.*/
/* Schedule a POLL message for packet_in */
sendPacketIn = new cMessage("sendPacketIn");
scheduleAt(simTime() + myAddress*0.5, sendPacketIn);
}
break;
case MODE_275_FLOWS:
if (!amIcontroller) {
/* Switch schedules a MSG every second.*/
/* Schedule a POLL message for packet_in */
sendPacketIn = new cMessage("sendPacketIn");
scheduleAt(simTime(), sendPacketIn);
}
break;
}
}
}
/*
* For a STATS_FLOW_REPLY Packet, we simulate the length with the
* following formula:
* STATS_FLOW_REPLY length in bytes = 68 + (104 * num_flows)
*/
int App::calculateLenFlowStatsResponsePacket(int num_flows) {
return (68 + (104*num_flows));
}
/*
* For a STATS_PORT_STATS_REPLY Packet, we simulate the length with the
* following formula:
* STATS_PORT_STATS_REPLY length in bytes = 68 + (112*num_ports)
*/
int App::calculateLenPortStatsResponsePacket(int num_ports) {
return (68 + (112*num_ports));
}
/*
* For a STATS_QUEUE_REPLY Packet, we simulate the length with the
* following formula:
* STATS_QUEUE_REPLY length in bytes = 108 * (40*num_queues)
*/
int App::calculateLenQueueStatsResponsePacket(int num_queues) {
return (108 + (40*num_queues));
}
Packet* App::sendToController(int src, int len, char pk_name[], pk_kind_t pk_type, int id) {
Packet *new_packet = new Packet(pk_name);
new_packet->setByteLength(len);
new_packet->setKind(pk_type);
new_packet->setSrcAddr(src);
new_packet->setDestAddr(CONTROLLER_ADDRS);
new_packet->setPacket_id(id);
return new_packet;
}
Packet* App::sendPacketToNodes(int dest, int len, char pk_name[], pk_kind_t pk_type, int id) {
Packet *pk = new Packet(pk_name);
pk->setByteLength(len);
pk->setKind(pk_type);
pk->setSrcAddr(CONTROLLER_ADDRS);
pk->setDestAddr(dest);
pk->setPacket_id(id);
return pk;
}
bool App::isPacketFromController(Packet* pk) {
if (pk->getSrcAddr() != CONTROLLER_ADDRS)
{
EV << "Packet not received from Controller, discarding" << endl;
return (false);
}
return (true);
}
/*
*
* Packet routine:
* 1. Switch sends to controller
* 2. controller sends to Switch
* 3. controller sends a bigger packet to Switch
* 4. Switch sends reply to controller.
* */
void App::handleMessage(cMessage *msg)
{
Packet *pk, *pk_2;
int size = 0;
pk_kind_t reply_type, incoming_packet_type;
char pk_out_msg[40];
double tx_delay = 0.0;
int src_addr, msg_id_send;
bool should_return = false;
bool isParentController = getParentModule()->getId() == CONTROLLER_ID;
bool isSelfMessage = msg->isSelfMessage();
// Every time period, send a packet_in to the controller
if (isSelfMessage) {
if (isParentController) {
delete msg;
return;
}
if ((msg == sendPacketIn) && (mode == MODE_QUEUING_LATENCY)) {
sprintf(pk_out_msg, "%d_%d_%d", msg_id, myAddress, CONTROLLER_ADDRS);
size = PACKET_IN_LEN;
reply_type = PACKET_IN;
tx_delay = (double (size)/double(LINE_RATE)) + queuing_DTC;
//EV << "App: " << msg_id << "generating packet from:" << myAddress << "-" << pk_out_msg << ": Qdelay is" << tx_delay << endl;
//EV << "App: " << msg_id << ":Sending A PACKET of type: " << reply_type << "to controller "<< size << "--delay" << tx_delay;
pk = sendToController(myAddress, size, pk_out_msg, reply_type, msg_id);
msg_id++;
sendDelayed(pk, tx_delay, "out");
// The chain of events should happen every sendPacketInTime seconds
if (sendPacketIn->isScheduled())
cancelEvent(sendPacketIn);
scheduleAt(simTime() + sendPacketInTime->doubleValue(), sendPacketIn);
}
if ((msg == sendPacketIn) && (mode==MODE_275_FLOWS)) {
msg_id = 0;
while (msg_id < NUM_POLLING_SEQ) {
sprintf(pk_out_msg, "%d_%d_%d", msg_id, myAddress, CONTROLLER_ADDRS);
size = PACKET_IN_LEN;
reply_type = PACKET_IN;
Packet *packet = sendToController(myAddress, size, pk_out_msg, reply_type, msg_id);
msg_id++;
send(packet, "out");
}
// The chain of events should happen every sendPacketInTime275 seconds
if (sendPacketIn->isScheduled())
cancelEvent(sendPacketIn);
scheduleAt(simTime() + sendPacketInTime275->doubleValue(), sendPacketIn);
}
return;
}
// If we reach this point, we have already scheduled the self messages.
// We have a received a new message, and the processing of that starts here.
incoming_packet_type = static_cast<pk_kind_t> (msg->getKind());
pk = check_and_cast<Packet *>(msg);
src_addr = pk->getSrcAddr();
msg_id_send = pk->getPacket_id();
//EV << "App: " << msg_id << "App:Received x message at" << myAddress << "-"<< msg->getDisplayString() << msg->getKind() << endl;
// Processing all messages at the Nodes
if (!isParentController){
//EV << "App: " << msg_id << "App:Received x message at" << myAddress << "-"<< msg->getDisplayString() << msg->getKind() << endl;
if (!isPacketFromController(pk)) {
//EV << " Packet not from Controller."<< endl;
delete msg;
return;
}
switch (incoming_packet_type){
case PACKET_FLOW_MOD:
//EV <<"No reply needed";
should_return = true;
break;
case STATS_FLOW_REQ:
size = this->calculateLenFlowStatsResponsePacket(numFlows);
reply_type = STATS_FLOW_REPLY;
sprintf(pk_out_msg, "%d_%d_%d", msg_id_send, myAddress, CONTROLLER_ADDRS);
if (mode==MODE_QUEUING_LATENCY) {
tx_delay = (double (size)/double(LINE_RATE)) + queuing_DTC;
EV << "App: " << msg_id << "App:Sending A PACKET of type: " << reply_type << "to controller "<< size << "--delay" << tx_delay;
pk = sendToController(myAddress, size, pk_out_msg, reply_type, msg_id_send);
sendDelayed(pk, tx_delay, "out");
} else if (mode==MODE_275_FLOWS && msg_id_send==0) {
//EV << "App: " << msg_id << "App:Sending A PACKET of type: " << reply_type << "to controller "<< size << "--delay" << tx_delay;
pk = sendToController(myAddress, size, pk_out_msg, reply_type, msg_id_send);
send(pk, "out");
}
should_return = true;
break;
default:
should_return = true;
break;
}
if (should_return) {
delete msg;
return;
}
} else { // The Controller's processing
if (mode == MODE_QUEUING_LATENCY) {
switch (incoming_packet_type){
case PACKET_IN:
size = PACKET_FLOW_MOD_LEN;
reply_type = PACKET_FLOW_MOD;
sprintf(pk_out_msg, "%d_%d_%d", msg_id_send, myAddress, src_addr);
tx_delay = double (size)/double(LINE_RATE);
//EV << "App: " << msg_id <<"-tx-delay" << tx_delay;
pk = sendPacketToNodes(src_addr, size, pk_out_msg, reply_type, msg_id_send);
sendDelayed(pk, tx_delay, "out");
//send(pk, "out");
// Once first packet is sent, we will send another one, right after.
size = STATS_FLOW_REQ_LEN;
reply_type = STATS_FLOW_REQ;
tx_delay += double (size)/double(LINE_RATE);
//EV << "App: " << msg_id << "-tx-delay" << tx_delay;
sprintf(pk_out_msg, "%d_%d_%d", msg_id_send, myAddress, src_addr);
pk_2 = sendPacketToNodes(src_addr, size, pk_out_msg, reply_type, msg_id_send);
sendDelayed(pk_2, tx_delay, "out");
break;
case STATS_FLOW_REPLY:
//EV << "Polling sequence over.";
break;
default:
//EV << "Invalid Packet... Dropping";
should_return = true;
break;
}
} else if (mode==MODE_275_FLOWS) {
switch (incoming_packet_type){
case PACKET_IN:
size = PACKET_FLOW_MOD_LEN;
reply_type = PACKET_FLOW_MOD;
sprintf(pk_out_msg, "%d_%d_%d", msg_id_send, myAddress, src_addr);
pk = sendPacketToNodes(src_addr, size, pk_out_msg, reply_type, msg_id_send);
send(pk, "out");
if (msg_id_send==0) {
size = STATS_FLOW_REQ_LEN;
reply_type = STATS_FLOW_REQ;
sprintf(pk_out_msg, "%d_%d_%d", msg_id_send, myAddress, src_addr);
pk_2= sendPacketToNodes(src_addr, size, pk_out_msg, reply_type, msg_id_send);
send(pk_2, "out");
}
break;
case STATS_FLOW_REPLY:
//EV << "Polling sequence over.";
break;
default:
//EV << "Invalid Packet... Dropping";
should_return = true;
break;
}
}
if (should_return) {
delete msg;
return;
}
delete msg;
}
}