NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
OpenFlow.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "OpenFlow.h"
16
17static void openFlow_createSocketInterface(NETSIM_ID d);
18
19//Event handler
20static int fn_NetSim_OPEN_FLOW_HandleTimerEvent();
21static int fn_NetSim_OPEN_FLOW_HandleAppInEvent();
22
23/**
24OPEN_FLOW Init function initializes the OPEN_FLOW parameters.
25*/
26_declspec (dllexport) int fn_NetSim_OPEN_FLOW_Init(struct stru_NetSim_Network *NETWORK_Formal,
27 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
28 char *pszAppPath_Formal,
29 char *pszWritePath_Formal,
30 int nVersion_Type,
31 void **fnPointer)
32{
33 fnPointer;
34 nVersion_Type;
35 pszWritePath_Formal;
36 pszAppPath_Formal;
37 pstruEventDetails_Formal;
38 NETWORK_Formal;
39 NETSIM_ID i;
40 for (i = 0; i < NETWORK->nDeviceCount; i++)
41 {
42 if (isOPENFLOWConfigured(i + 1))
43 {
44 ptrOPENFLOW of = GET_OPENFLOW_VAR(i + 1);
45 if (!isSDNController(i + 1))
46 {
47 of->sdnControllerId = fn_NetSim_Stack_GetDeviceId_asName(of->sdnControllerDev);
48 if (!of->sdnControllerId)
49 {
50 fnNetSimError("%s device is not configured as SDN controller.\n"
51 "Disabling OPEN_FLOW protocol in device %d.\n",
52 of->sdnControllerDev,
53 DEVICE_CONFIGID(i + 1));
54 of->isSDNConfigured = false;
55 }
56 }
57 }
58 }
59
60 for (i = 0; i < NETWORK->nDeviceCount; i++)
61 {
62 if (isOPENFLOWConfigured(i + 1))
63 {
64 ptrOPENFLOW of = GET_OPENFLOW_VAR(i + 1);
65 of->myIP = openFlow_find_client_IP(i + 1);
66 if (isSDNController(i + 1))
67 {
68 }
69 else
70 {
71 of->INFO.controllerInfo = calloc(1, sizeof* of->INFO.controllerInfo);
72 openFlow_createSocketInterface(i + 1);
73 }
74 }
75 }
76 return 0;
77}
78
79/**
80This function is called by NetworkStack.dll, whenever the event gets triggered
81inside the NetworkStack.dll for the OPEN_FLOW protocol
82*/
83_declspec (dllexport) int fn_NetSim_OPEN_FLOW_Run()
84{
85 switch (pstruEventDetails->nEventType)
86 {
87 case TIMER_EVENT:
88 return fn_NetSim_OPEN_FLOW_HandleTimerEvent();
89 break;
90 case APPLICATION_IN_EVENT:
91 return fn_NetSim_OPEN_FLOW_HandleAppInEvent();
92 default:
93 fnNetSimError("%d event is not for Open flow protocol\n", pstruEventDetails->nEventType);
94 break;
95 }
96 return 0;
97}
98
99/**
100This function is called by NetworkStack.dll, once simulation end to free the
101allocated memory for the network.
102*/
103_declspec(dllexport) int fn_NetSim_OPEN_FLOW_Finish()
104{
105 return 0;
106 //return fn_NetSim_OPEN_FLOW_Finish_F();
107}
108
109/**
110This function is called by NetworkStack.dll, while writing the event trace
111to get the sub event as a string.
112*/
113_declspec (dllexport) char* fn_NetSim_OPEN_FLOW_Trace(NETSIM_ID nSubEvent)
114{
115 nSubEvent;
116 return "";
117 //return GetStringOPEN_FLOW_Subevent(nSubEvent);
118}
119
120/**
121This function is called by NetworkStack.dll, while configuring the device
122for Open flow protocol.
123*/
124_declspec(dllexport) int fn_NetSim_OPEN_FLOW_Configure(void** var)
125{
126 char* tag;
127 void* xmlNetSimNode;
128 NETSIM_ID nDeviceId;
129 NETSIM_ID nInterfaceId;
130 LAYER_TYPE nLayerType;
131
132 tag = (char*)var[0];
133 xmlNetSimNode = var[2];
134 if (!strcmp(tag, "PROTOCOL_PROPERTY"))
135 {
136 NETWORK = (struct stru_NetSim_Network*)var[1];
137 nDeviceId = *((NETSIM_ID*)var[3]);
138 nInterfaceId = *((NETSIM_ID*)var[4]);
139 nLayerType = *((LAYER_TYPE*)var[5]);
140
141 ptrOPENFLOW of;
142 of = GET_OPENFLOW_VAR(nDeviceId);
143 if (!of)
144 {
145 of = calloc(1, sizeof* of);
146 SET_OPENFLOW_VAR(nDeviceId, of);
147 }
148 getXmlVar(&of->isSDNConfigured, OPEN_FLOW_ENABLE, xmlNetSimNode, 1, _BOOL, OPENFLOW);
149 if (of->isSDNConfigured)
150 {
151 getXmlVar(&of->isSDNController, SDN_CONTROLLER, xmlNetSimNode, 1, _BOOL, OPENFLOW);
152 if(!of->isSDNController)
153 getXmlVar(&of->sdnControllerDev, SDN_CONTROLLER_DEVICE, xmlNetSimNode, 1, _STRING, OPENFLOW);
154 }
155
156 }
157 return 0;
158}
159
160/**
161This function is called by NetworkStack.dll, to free the OPEN_FLOW protocol data.
162*/
163_declspec(dllexport) int fn_NetSim_OPEN_FLOW_FreePacket(NetSim_PACKET* pstruPacket)
164{
165 pstruPacket;
166 return 0;
167 //return fn_NetSim_OPEN_FLOW_FreePacket_F(pstruPacket);
168}
169
170/**
171This function is called by NetworkStack.dll, to copy the OPEN_FLOW protocol
172details from source packet to destination.
173*/
174_declspec(dllexport) int fn_NetSim_OPEN_FLOW_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
175{
176 pstruSrcPacket;
177 pstruDestPacket;
178 return 0;
179 //return fn_NetSim_OPEN_FLOW_CopyPacket_F(pstruDestPacket, pstruSrcPacket);
180}
181
182/**
183This function write the Metrics
184*/
185_declspec(dllexport) int fn_NetSim_OPEN_FLOW_Metrics(PMETRICSWRITER metricsWriter)
186{
187 metricsWriter;
188 return 0;
189}
190
191/**
192This function will return the string to write packet trace heading.
193*/
194_declspec(dllexport) char* fn_NetSim_OPEN_FLOW_ConfigPacketTrace()
195{
196 return "";
197}
198
199/**
200This function will return the string to write packet trace.
201*/
202_declspec(dllexport) char* fn_NetSim_OPEN_FLOW_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
203{
204 ppszTrace;
205 pstruPacket;
206 return "";
207}
208
209bool isOPENFLOWConfigured(NETSIM_ID d)
210{
211 ptrOPENFLOW of = GET_OPENFLOW_VAR(d);
212 if (of)
213 {
214 return of->isSDNConfigured;
215 }
216 return false;
217}
218
219static int fn_NetSim_OPEN_FLOW_HandleTimerEvent()
220{
221 switch (pstruEventDetails->nSubEventType)
222 {
223 case 0:
224 handle_cli_input();
225 break;
226 default:
227 fnNetSimError("%d subevent is not for Open flow protocol\n", pstruEventDetails->nSubEventType);
228 break;
229 }
230 return 0;
231}
232
233static void openFlow_createSocketInterface(NETSIM_ID d)
234{
235 ptrOPENFLOW of = GET_OPENFLOW_VAR(d);
236
237 UINT16 port = (UINT16)(NETSIM_RAND_01() * 65535);
238 of->INFO.controllerInfo->port = port;
239 of->INFO.controllerInfo->controllerId = of->sdnControllerId;
240 of->INFO.controllerInfo->controllerIP = openFlow_find_client_IP(of->sdnControllerId);
241 of->INFO.controllerInfo->sock = fn_NetSim_Socket_CreateNewSocket(d,
242 APP_PROTOCOL_OPENFLOW,
243 TX_PROTOCOL_TCP,
244 port,
245 OFMSG_PORT);
246
247 openFlow_add_new_client(of->sdnControllerId,
248 d,
249 port);
250}
251
252NETSIM_IPAddress openFlow_find_client_IP(NETSIM_ID d)
253{
254 NETSIM_ID i;
255 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
256 {
257 if (DEVICE_INTERFACE(d, i + 1)->nInterfaceType == INTERFACE_WAN_ROUTER)
258 return DEVICE_NWADDRESS(d, i + 1);
259 }
260 return DEVICE_NWADDRESS(d, 1);
261}
262
263static int fn_NetSim_OPEN_FLOW_HandleAppInEvent()
264{
265 NetSim_PACKET* packet = pstruEventDetails->pPacket;
266 if (!(packet->pstruAppData->nPacketFragment == Segment_Unfragment ||
267 packet->pstruAppData->nPacketFragment == Segment_FirstSegment))
268 {
269 fn_NetSim_Packet_FreePacket(packet);
270 return -1;
271 }
272
273 switch (packet->nControlDataType)
274 {
275 case OFMSGTYPE_COMMAND:
276 {
277 ptrOPENFLOWMSG msg = packet->pstruAppData->Packet_AppProtocol;
278 int len = 0;
279 bool isMore = false;
280 char* ret = openFlow_execute_command(msg->payload,
281 pstruEventDetails->nDeviceId,
282 msg->id,
283 &len,
284 &isMore);
285 if(len)
286 openFlow_send_response(pstruEventDetails->nDeviceId,
287 msg->id,
288 ret,
289 len,
290 isMore);
291 fn_NetSim_Packet_FreePacket(packet);
292 }
293 break;
294 case OFMSGTYPE_RESPONSE:
295 {
296 ptrOPENFLOWMSG msg = packet->pstruAppData->Packet_AppProtocol;
297 openFlow_print_response(pstruEventDetails->nDeviceId,
298 msg->id,
299 msg->payload,
300 msg->len,
301 msg->isMore);
302 fn_NetSim_Packet_FreePacket(packet);
303 }
304 break;
305 default:
306 fnNetSimError("Unknown packet type %d arrives in APP_IN event of OPEN_FLOW protocol.",
307 packet->nControlDataType);
308 break;
309 }
310 return 0;
311}