NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Ethernet.h
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* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24
25#ifndef _NETSIM_ETHERNET_H_
26#define _NETSIM_ETHERNET_H_
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#pragma comment(lib,"NetworkStack.lib")
32#pragma comment(lib,"Metrics.lib")
33
34#define isETHConfigured(d,i) (DEVICE_MACLAYER(d,i)->nMacProtocolId == MAC_PROTOCOL_IEEE802_3)
35 //Global variable
36 PNETSIM_MACADDRESS multicastSPTMAC;
37
38#define ETH_IFG 0.960 //Micro sec
39
40 typedef enum enum_eth_packet
41 {
42 ETH_CONFIGBPDU = MAC_PROTOCOL_IEEE802_3 * 100 + 1,
43 }ETH_PACKET;
44
45 /** Enumeration for Switching Technique */
46 typedef enum enum_SwitchingTechnique
47 {
48 SWITCHINGTECHNIQUE_NULL,
49 SWITCHINGTECHNIQUE_STORE_FORWARD,
50 SWITCHINGTECHNIQUE_CUT_THROUGH,
51 SWITCHINGTECHNIQUE_FRAGMENT_FREE,
52 }SWITCHING_TECHNIQUE;
53
54 /** Enumeration for SWITCH Port state*/
55 typedef enum enum_PortState
56 {
57 PORTSTATE_DISABLED,
58 PORTSTATE_LISTENING,
59 PORTSTATE_LEARNING,
60 PORTSTATE_FORWARDING,
61 PORTSTATE_BLOCKING
62 }SWITCH_PORTSTATE;
63
64 typedef enum enum_port_type
65 {
66 PORTTYPE_DESIGNATED,
67 PORTTYPE_NONDESIGNATED,
68 PORTTYPE_ROOT,
69 }SWITCH_PORTTYPE;
70
71
72 //Typedefs
73 typedef struct stru_ethernet_lan ETH_LAN, *ptrETH_LAN;
74
75#define ETH_HDR_CBPDU 1
76#define ETH_HDR_VID 2
77#define ETH_HDR_NW 3
78 typedef struct stru_eth_packet
79 {
80 UINT type;
81 void* var;
82 void* (*fncopy)(void*);
83 void(*fnfree)(void*);
84 }ETH_HDR,*ptrETH_HDR;
85 void set_eth_hdr(NetSim_PACKET* packet, UINT type, void* var, void*(fncopy)(void*), void(*fnfree)(void*));
86 void free_eth_hdr(NetSim_PACKET* packet);
87 void* get_eth_hdr_var(NetSim_PACKET* packet);
88
89 typedef struct stru_eth_bpdu
90 {
91 UINT16 protocolIdentifier;
92 UINT8 protocolVersionIdentifier;
93 UINT8 bpduType;
94 UINT8 flags;
95 char* rootId;
96 UINT rootPathCost;
97 char* bridgeId;
98 UINT16 portId;
99 UINT16 msgAge;
100 UINT16 maxAge;
101 UINT16 helloTime;
102 UINT16 forwardDelay;
103 }ETH_BPDU, *ptrETH_BPDU;
104#define ETH_BPDU_LEN 60 //Bytes (35 SPT+ 22 Ethernet + 3 LLC)
105
106 typedef struct ieee802_1q_tag
107 {
108 UINT16 TPId;
109 UINT PCP : 3;
110 UINT DEI : 1;
111 UINT VID : 12;
112 }IEEE801_1Q_TAG, *ptrIEEE801_1Q_TAG;
113#define IEEE801_1Q_TAG_LEN 4 //Bytes
114#define VLAN_TPID 0x8100
115 bool isVLANTagPresent(NetSim_PACKET* packet);
116
117 typedef struct stru_SwitchTable
118 {
119 NETSIM_ID vlanId;
120 PNETSIM_MACADDRESS mac;
121 NETSIM_ID outPort;
122 _ele* ele;
123 }SWITCHTABLE, *ptrSWITCHTABLE;
124#define SWITCHTABLE_ALLOC() (ptrSWITCHTABLE)list_alloc(sizeof(SWITCHTABLE),offsetof(SWITCHTABLE,ele))
125#define SWITCHTABLE_ADD(l,m) LIST_ADD_LAST((void**)(l),m)
126#define SWITCHTABLE_NEXT(t) t=(ptrSWITCHTABLE)LIST_NEXT(t)
127#define SWITCHTABLE_GET(d,in) (GET_ETH_LAN(d,in,0)->switchTable)
128#define SWITCHTABLE_GET_LAN(lan) (lan->switchTable)
129 ptrSWITCHTABLE SWITCHTABLE_FIND(ptrETH_LAN lan, PNETSIM_MACADDRESS dest);
130 void SWITCHTABLE_NEW(ptrETH_LAN lan, PNETSIM_MACADDRESS dest, NETSIM_ID outport);
131 void switchtable_metrics_print(PMETRICSWRITER metricsWriter);
132
133 typedef enum
134 {
135 VLANPORT_NONE,
136 VLANPORT_ACCESS,
137 VLANPORT_TRUNK,
138 }VLANPORT;
139 VLANPORT vlan_port_type_from_str(char* val);
140
142 {
143 NETSIM_ID interfaceId;
144 PNETSIM_MACADDRESS macAddress;
145 SWITCH_PORTSTATE portState;
146 SWITCH_PORTTYPE portType;
147 VLANPORT vlanPortType;
148 }ETH_IF, *ptrETH_IF;
149 ptrETH_IF GET_ETH_IF(ptrETH_LAN lan, NETSIM_ID i);
150
151 typedef struct stru_ethernet_spt
152 {
153 char* STPProtocol;
154 int stpCost;
155 UINT priority;
156 char* switchId;
157
158 char* rootId;
159 UINT rootPathCost;
160 NETSIM_ID rootPort;
161
162 }ETH_SPT, *ptrETH_SPT;
163
165 {
166 NETSIM_ID lanId;
167 UINT interfaceCount;
168 NETSIM_ID* interfaceId;
169 NETSIM_IPAddress lanIP; // For layer 3 or above device
170 UINT16 vlanId;
171
172 ptrETH_IF* ethIfVar;
173
174 ptrETH_SPT spt;
175
176 //Only for switch
177 SWITCHING_TECHNIQUE switchingTechnique;
178 double latency;
179 bool STPStatus;
180 ptrSWITCHTABLE switchTable;
181
182 char* ethernetStandard;
183 double speed;
184
185 bool ispromiscuousMode;
186 };
187 ptrETH_LAN GET_ETH_LAN(NETSIM_ID d, NETSIM_ID i, UINT16 vlanId);
188
189 typedef struct stru_ethernet_var
190 {
191 NETSIM_ID devId;
192 UINT lanCount;
193 ptrETH_LAN* lanVar;
194
195 bool isSPT;
196
197 NetSim_BUFFER* buffer;
198
199 bool isVLAN;
200 UINT trunkPortCount;
201 NETSIM_ID* trunkPortIds;
202
203 bool isIGMPSnooping;
204 }ETH_VAR,*ptrETH_VAR;
205 ptrETH_VAR GET_ETH_VAR(NETSIM_ID d);
206 void SET_ETH_VAR(NETSIM_ID d, ptrETH_VAR var);
207 void FREE_ETH_VAR(NETSIM_ID d);
208
209 typedef struct stru_eth_phy
210 {
211 NETSIM_ID interfaceId;
212 double speed;
213 double BER;
214 double propagationDelay;
215 NETSIM_ID connectedDevice;
216 NETSIM_ID connectedInterface;
217 double IFG;
218
219 NetSim_BUFFER* packet;
220 double lastPacketEndTime;
221
222 bool isErrorChkReqd;
223 }ETH_PHY, *ptrETH_PHY;
224#define ETH_PHY_GET(d,i) ((ptrETH_PHY)DEVICE_PHYLAYER(d,i)->phyVar)
225
226 //Spanning Tree
227 void init_spanning_tree_protocol();
228 int multicast_config_bpdu();
229 void process_configbpdu();
230
231 //VLAN
232 void vlan_add_trunk_port(ptrETH_VAR eth, NETSIM_ID in);
233 void vlan_forward_to_all_trunk(NETSIM_ID d,
234 NetSim_PACKET* packet,
235 ptrETH_LAN lan,
236 double time);
237 void vlan_macout_forward_packet(NETSIM_ID d,
238 NETSIM_ID in,
239 UINT16 vlanId,
240 NETSIM_ID incoming,
241 NetSim_PACKET* packet,
242 double time);
243 void vlan_macin_forward_packet();
244
245 // MAC
246 NETSIM_ID find_forward_interface(ptrETH_LAN lan, NetSim_PACKET* packet);
247 void check_move_frame_up(NETSIM_ID d,
248 NETSIM_ID in,
249 ptrETH_LAN lan,
250 NetSim_PACKET* packet,
251 double time);
252 void forward_frame(NETSIM_ID d,
253 NETSIM_ID in,
254 ptrETH_LAN lan,
255 NetSim_PACKET* packet,
256 double time);
257 void send_to_phy(NETSIM_ID d, NETSIM_ID in, NetSim_PACKET* packet, double time);
258
259 //Ethernet utility function
260 NETSIM_ID get_interface_id(NETSIM_ID d, NETSIM_ID c);
261 void print_ethernet_log(char* format, ...);
262 void print_spanning_tree();
263 void store_eth_param_macin(NetSim_PACKET* packet, NETSIM_ID devId, NETSIM_ID in, ptrETH_LAN lan);
264 void store_eth_incoming(NetSim_PACKET* packet, NETSIM_ID devId, NETSIM_ID incomingPort, UINT16 vlanId);
265 UINT16 eth_get_incoming_vlanid(NetSim_PACKET* packet, NETSIM_ID devId);
266 NETSIM_ID eth_get_incoming_port(NetSim_PACKET* packet, NETSIM_ID devId);
267 void clear_eth_incoming(NetSim_PACKET* packet);
268 double get_ifg(double link_speed);
269
270#ifdef __cplusplus
271}
272#endif
273#endif //_NETSIM_ETHERNET_H_