NetSim Source Code Help
Loading...
Searching...
No Matches
OpenFlow.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
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
18
19//Event handler
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,
30 int nVersion_Type,
31 void **fnPointer)
32{
33 NETSIM_ID i;
34 for (i = 0; i < NETWORK->nDeviceCount; i++)
35 {
36 if (isOPENFLOWConfigured(i + 1))
37 {
38 ptrOPENFLOW of = GET_OPENFLOW_VAR(i + 1);
39 if (!isSDNController(i + 1))
40 {
42 if (!of->sdnControllerId)
43 {
44 fnNetSimError("%s device is not configured as SDN controller.\n"
45 "Disabling OPEN_FLOW protocol in device %d.\n",
47 DEVICE_CONFIGID(i + 1));
48 of->isSDNConfigured = false;
49 }
50 }
51 }
52 }
53
54 for (i = 0; i < NETWORK->nDeviceCount; i++)
55 {
56 if (isOPENFLOWConfigured(i + 1))
57 {
58 ptrOPENFLOW of = GET_OPENFLOW_VAR(i + 1);
59 of->myIP = openFlow_find_client_IP(i + 1);
60 if (isSDNController(i + 1))
61 {
62 }
63 else
64 {
65 of->INFO.controllerInfo = calloc(1, sizeof* of->INFO.controllerInfo);
67 }
68 }
69 }
70 return 0;
71}
72
73/**
74This function is called by NetworkStack.dll, whenever the event gets triggered
75inside the NetworkStack.dll for the OPEN_FLOW protocol
76*/
78{
80 {
81 case TIMER_EVENT:
83 break;
86 default:
87 fnNetSimError("%d event is not for Open flow protocol\n", pstruEventDetails->nEventType);
88 break;
89 }
90 return 0;
91}
92
93/**
94This function is called by NetworkStack.dll, once simulation end to free the
95allocated memory for the network.
96*/
98{
99 return 0;
100 //return fn_NetSim_OPEN_FLOW_Finish_F();
101}
102
103/**
104This function is called by NetworkStack.dll, while writing the event trace
105to get the sub event as a string.
106*/
108{
109 return "";
110 //return GetStringOPEN_FLOW_Subevent(nSubEvent);
111}
112
113/**
114This function is called by NetworkStack.dll, while configuring the device
115for Open flow protocol.
116*/
117_declspec(dllexport) int fn_NetSim_OPEN_FLOW_Configure(void** var)
118{
119 char* tag;
120 void* xmlNetSimNode;
121 NETSIM_ID nDeviceId;
122 NETSIM_ID nInterfaceId;
123 LAYER_TYPE nLayerType;
124
125 tag = (char*)var[0];
126 xmlNetSimNode = var[2];
127 if (!strcmp(tag, "PROTOCOL_PROPERTY"))
128 {
129 NETWORK = (struct stru_NetSim_Network*)var[1];
130 nDeviceId = *((NETSIM_ID*)var[3]);
131 nInterfaceId = *((NETSIM_ID*)var[4]);
132 nLayerType = *((LAYER_TYPE*)var[5]);
133
134 char* szVal;
135 ptrOPENFLOW of;
136 of = GET_OPENFLOW_VAR(nDeviceId);
137 if (!of)
138 {
139 of = calloc(1, sizeof* of);
140 SET_OPENFLOW_VAR(nDeviceId, of);
141 }
142 getXmlVar(&of->isSDNConfigured, OPEN_FLOW_ENABLE, xmlNetSimNode, 1, _BOOL, OPENFLOW);
143 getXmlVar(&of->isSDNController, SDN_CONTROLLER, xmlNetSimNode, 1, _BOOL, OPENFLOW);
144 if(!of->isSDNController)
145 getXmlVar(&of->sdnControllerDev, SDN_CONTROLLER_DEVICE, xmlNetSimNode, 1, _STRING, OPENFLOW);
146
147 }
148 return 0;
149}
150
151/**
152This function is called by NetworkStack.dll, to free the OPEN_FLOW protocol data.
153*/
155{
156 return 0;
157 //return fn_NetSim_OPEN_FLOW_FreePacket_F(pstruPacket);
158}
159
160/**
161This function is called by NetworkStack.dll, to copy the OPEN_FLOW protocol
162details from source packet to destination.
163*/
164_declspec(dllexport) int fn_NetSim_OPEN_FLOW_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
165{
166 return 0;
167 //return fn_NetSim_OPEN_FLOW_CopyPacket_F(pstruDestPacket, pstruSrcPacket);
168}
169
170/**
171This function write the Metrics
172*/
174{
175 return 0;
176}
177
178/**
179This function will return the string to write packet trace heading.
180*/
182{
183 return "";
184}
185
186/**
187This function will return the string to write packet trace.
188*/
189_declspec(dllexport) char* fn_NetSim_OPEN_FLOW_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
190{
191 return "";
192}
193
195{
197 if (of)
198 {
199 return of->isSDNConfigured;
200 }
201 return false;
202}
203
205{
207 {
208 case 0:
210 break;
211 default:
212 fnNetSimError("%d subevent is not for Open flow protocol\n", pstruEventDetails->nSubEventType);
213 break;
214 }
215 return 0;
216}
217
219{
221
222 UINT16 port = (UINT16)(NETSIM_RAND_01() * 65535);
223 of->INFO.controllerInfo->port = port;
229 port,
230 OFMSG_PORT);
231
233 d,
234 port);
235}
236
238{
239 NETSIM_ID i;
240 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
241 {
242 if (DEVICE_INTERFACE(d, i + 1)->nInterfaceType == INTERFACE_WAN_ROUTER)
243 return DEVICE_NWADDRESS(d, i + 1);
244 }
245 return DEVICE_NWADDRESS(d, 1);
246}
247
249{
253 {
255 return -1;
256 }
257
258 switch (packet->nControlDataType)
259 {
261 {
263 int len = 0;
264 bool isMore = false;
265 char* ret = openFlow_execute_command(msg->payload,
267 msg->id,
268 &len,
269 &isMore);
270 if(len)
272 msg->id,
273 ret,
274 len,
275 isMore);
277 }
278 break;
280 {
283 msg->id,
284 msg->payload,
285 msg->len,
286 msg->isMore);
288 }
289 break;
290 default:
291 fnNetSimError("Unknown packet type %d arrives in APP_IN event of OPEN_FLOW protocol.",
292 packet->nControlDataType);
293 break;
294 }
295 return 0;
296}
unsigned int NETSIM_ID
Definition: Animation.h:45
void openFlow_print_response(NETSIM_ID d, UINT id, void *msg, int len, bool isMore)
Definition: CLIInterface.c:167
char * openFlow_execute_command(void *handle, NETSIM_ID d, UINT id, int *len, bool *isMore)
Definition: CLIInterface.c:159
void handle_cli_input()
Definition: CLIInterface.c:115
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define UINT16
Definition: Linux.h:33
#define calloc(c, s)
Definition: Memory.h:29
void * PMETRICSWRITER
Definition: MetricsWriter.h:27
static void openFlow_createSocketInterface(NETSIM_ID d)
Definition: OpenFlow.c:218
int fn_NetSim_OPEN_FLOW_Configure(void **var)
Definition: OpenFlow.c:117
static int fn_NetSim_OPEN_FLOW_HandleAppInEvent()
Definition: OpenFlow.c:248
int fn_NetSim_OPEN_FLOW_FreePacket(NetSim_PACKET *pstruPacket)
Definition: OpenFlow.c:154
static int fn_NetSim_OPEN_FLOW_HandleTimerEvent()
Definition: OpenFlow.c:204
bool isOPENFLOWConfigured(NETSIM_ID d)
Definition: OpenFlow.c:194
char * fn_NetSim_OPEN_FLOW_Trace(NETSIM_ID nSubEvent)
Definition: OpenFlow.c:107
int fn_NetSim_OPEN_FLOW_Metrics(PMETRICSWRITER metricsWriter)
Definition: OpenFlow.c:173
int fn_NetSim_OPEN_FLOW_Finish()
Definition: OpenFlow.c:97
char * fn_NetSim_OPEN_FLOW_WritePacketTrace(NetSim_PACKET *pstruPacket, char **ppszTrace)
Definition: OpenFlow.c:189
int fn_NetSim_OPEN_FLOW_Init(struct stru_NetSim_Network *NETWORK_Formal, NetSim_EVENTDETAILS *pstruEventDetails_Formal, char *pszAppPath_Formal, char *pszWritePath_Formal, int nVersion_Type, void **fnPointer)
Definition: OpenFlow.c:26
int fn_NetSim_OPEN_FLOW_Run()
Definition: OpenFlow.c:77
char * fn_NetSim_OPEN_FLOW_ConfigPacketTrace()
Definition: OpenFlow.c:181
int fn_NetSim_OPEN_FLOW_CopyPacket(NetSim_PACKET *pstruDestPacket, NetSim_PACKET *pstruSrcPacket)
Definition: OpenFlow.c:164
NETSIM_IPAddress openFlow_find_client_IP(NETSIM_ID d)
Definition: OpenFlow.c:237
void openFlow_add_new_client(NETSIM_ID ct, NETSIM_ID ci, UINT16 port)
Definition: SDNController.c:40
#define GET_OPENFLOW_VAR(d)
Definition: OpenFlow.h:72
void openFlow_send_response(NETSIM_ID dev, UINT id, char *payload, int len, bool isMore)
Definition: OpenFlowMsg.c:145
bool isSDNController(NETSIM_ID d)
Definition: SDNController.c:17
#define OFMSG_PORT
Definition: OpenFlow.h:76
#define SET_OPENFLOW_VAR(d, var)
Definition: OpenFlow.h:73
@ OFMSGTYPE_COMMAND
Definition: OpenFlow.h:79
@ OFMSGTYPE_RESPONSE
Definition: OpenFlow.h:80
@ Segment_Unfragment
Definition: Packet.h:115
@ Segment_FirstSegment
Definition: Packet.h:112
NetSim_EVENTDETAILS * pstruEventDetails_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char * pszAppPath_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char char int nVersion_Type
Definition: RIP.h:178
NetSim_EVENTDETAILS char char * pszWritePath_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char char int void ** fnPointer
Definition: RIP.h:178
#define DEVICE(DeviceId)
Definition: Stack.h:769
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asName(const char *name)
#define getXmlVar(var, name, xmlNode, flag, type, protocol)
Definition: Stack.h:1046
ptrSOCKETINTERFACE fn_NetSim_Socket_CreateNewSocket(NETSIM_ID deviceId, int appProtocol, TRANSPORT_LAYER_PROTOCOL trxProtocol, UINT16 localPort, UINT16 remotePort)
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
@ _BOOL
Definition: Stack.h:1034
@ _STRING
Definition: Stack.h:1033
@ APP_PROTOCOL_OPENFLOW
Definition: Stack.h:158
@ TX_PROTOCOL_TCP
Definition: Stack.h:180
#define DEVICE_CONFIGID(DeviceId)
Definition: Stack.h:779
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ TIMER_EVENT
Definition: Stack.h:114
@ APPLICATION_IN_EVENT
Definition: Stack.h:113
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define NETSIM_RAND_01()
Definition: Stack.h:860
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
@ INTERFACE_WAN_ROUTER
Definition: Stack.h:254
enum enum_LayerType LAYER_TYPE
Definition: main.h:136
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
EVENT_TYPE nEventType
Definition: Stack.h:747
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
PACKET_FRAGMENT nPacketFragment
Definition: Packet.h:163
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
unsigned int nControlDataType
Definition: Packet.h:258
NETSIM_IPAddress controllerIP
Definition: OpenFlow.h:41
NETSIM_ID controllerId
Definition: OpenFlow.h:40
ptrSOCKETINTERFACE sock
Definition: OpenFlow.h:43
void * payload
Definition: OpenFlow.h:87
NETSIM_ID sdnControllerId
Definition: OpenFlow.h:61
char * sdnControllerDev
Definition: OpenFlow.h:60
NETSIM_IPAddress myIP
Definition: OpenFlow.h:58
bool isSDNController
Definition: OpenFlow.h:56
union stru_openflow::sdn_info INFO
bool isSDNConfigured
Definition: OpenFlow.h:55
ptrSDNCONTROLLERINFO controllerInfo
Definition: OpenFlow.h:65