NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Ethernet.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* 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#include "main.h"
25#include "Ethernet.h"
26#include "Ethernet_enum.h"
27#include "AdvancedPlots.h"
28#pragma comment(lib,"AdvancedPlots.lib")
29char* GetStringEthernet_Subevent(NETSIM_ID);
30
31#pragma comment(lib,"Ethernet.lib")
32
33//Function prototype
34int fn_NetSim_Ethernet_Configure_F(void **var);
35int fn_NetSim_Ethernet_Init_F(struct stru_NetSim_Network* net,
36 NetSim_EVENTDETAILS* pevent,
37 char* appPath,
38 char* iopath,
39 int version,
40 void** fnPointer);
41int fn_NetSim_Ethernet_CopyPacket_F(NetSim_PACKET* dst,
42 NetSim_PACKET* src);
43int fn_NetSim_Ethernet_FreePacket_F(NetSim_PACKET* packet);
44int fn_NetSim_Ethernet_Finish_F();
45int fn_NetSim_Ethernet_Metrics_F(PMETRICSWRITER metricsWriter);
46char* fn_NetSim_Ethernet_ConfigPacketTrace_F(const void* xmlNetSimNode);
47int fn_NetSim_Ethernet_WritePacketTrace_F(NetSim_PACKET* pstruPacket, char** ppszTrace);
48
49int fn_NetSim_Ethernet_HandleMacOut();
50int fn_NetSim_Ethernet_HandleMacIn();
51int fn_NetSim_Ethernet_HandlePhyOut();
52int fn_NetSim_Ethernet_HandlePhyIn();
53static int fn_NetSim_Ethernet_HandleTimer();
54
55/**
56This function is called by NetworkStack.dll, whenever the event gets triggered
57inside the NetworkStack.dll for the Mac/Phy layer Ethernet protocol
58It includes MAC_OUT, MAC_IN, PHY_OUT, PHY_IN and TIMER_EVENT.
59*/
60_declspec (dllexport) int fn_NetSim_Ethernet_Run()
61{
62 switch (pstruEventDetails->nEventType)
63 {
64 case MAC_OUT_EVENT:
65 return fn_NetSim_Ethernet_HandleMacOut();
66 break;
67 case MAC_IN_EVENT:
68 return fn_NetSim_Ethernet_HandleMacIn();
69 break;
70 case PHYSICAL_OUT_EVENT:
71 return fn_NetSim_Ethernet_HandlePhyOut();
72 break;
73 case PHYSICAL_IN_EVENT:
74 return fn_NetSim_Ethernet_HandlePhyIn();
75 break;
76 case TIMER_EVENT:
77 return fn_NetSim_Ethernet_HandleTimer();
78 break;
79 default:
80 fnNetSimError("Unknown event %d for Ethernet protocolin %s\n",
81 pstruEventDetails->nEventType,
82 __FUNCTION__);
83 return -1;
84 }
85}
86
87/**
88This function is called by NetworkStack.dll, while configuring the device
89Mac/Phy layer for Ethernet protocol.
90*/
91_declspec(dllexport) int fn_NetSim_Ethernet_Configure(void** var)
92{
93 return fn_NetSim_Ethernet_Configure_F(var);
94}
95
96/**
97This function initializes the Ethernet parameters.
98*/
99_declspec (dllexport) int fn_NetSim_Ethernet_Init(struct stru_NetSim_Network *NETWORK_Formal,
100 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
101 char *pszAppPath_Formal,
102 char *pszWritePath_Formal,
103 int nVersion_Type,
104 void **fnPointer)
105{
106 init_linkpacketlog();
107 return fn_NetSim_Ethernet_Init_F(NETWORK_Formal,
108 pstruEventDetails_Formal,
109 pszAppPath_Formal,
110 pszWritePath_Formal,
111 nVersion_Type,
112 fnPointer);
113}
114
115/**
116This function is called by NetworkStack.dll, once simulation end to free the
117allocated memory for the network.
118*/
119_declspec(dllexport) int fn_NetSim_Ethernet_Finish()
120{
121 return fn_NetSim_Ethernet_Finish_F();
122}
123
124/**
125This function is called by NetworkStack.dll, while writing the event trace
126to get the sub event as a string.
127*/
128_declspec (dllexport) char *fn_NetSim_Ethernet_Trace(int nSubEvent)
129{
130 return (GetStringEthernet_Subevent(nSubEvent));
131}
132
133/**
134This function is called by NetworkStack.dll, to free the Ethernet protocol
135*/
136_declspec(dllexport) int fn_NetSim_Ethernet_FreePacket(NetSim_PACKET* pstruPacket)
137{
138 return fn_NetSim_Ethernet_FreePacket_F(pstruPacket);
139}
140
141/**
142This function is called by NetworkStack.dll, to copy the Ethernet protocol from source packet to destination.
143*/
144_declspec(dllexport) int fn_NetSim_Ethernet_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
145{
146 return fn_NetSim_Ethernet_CopyPacket_F(pstruDestPacket, pstruSrcPacket);
147}
148
149/**
150This function write the Metrics in Metrics.txt
151*/
152_declspec(dllexport) int fn_NetSim_Ethernet_Metrics(PMETRICSWRITER metricsWriter)
153{
154 return fn_NetSim_Ethernet_Metrics_F(metricsWriter);
155}
156
157/**
158This function will return the string to write packet trace heading.
159*/
160_declspec(dllexport) char* fn_NetSim_Ethernet_ConfigPacketTrace(const void* xmlNetSimNode)
161{
162 return fn_NetSim_Ethernet_ConfigPacketTrace_F(xmlNetSimNode);
163}
164
165/**
166This function will return the string to write packet trace.
167*/
168_declspec(dllexport) int fn_NetSim_Ethernet_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
169{
170 return fn_NetSim_Ethernet_WritePacketTrace_F(pstruPacket, ppszTrace);
171}
172
173static int fn_NetSim_Ethernet_HandleTimer()
174{
175 switch (pstruEventDetails->nSubEventType)
176 {
177 case ETH_IF_UP:
178 notify_interface_up(pstruEventDetails->nDeviceId, pstruEventDetails->nInterfaceId);
179 break;
180 default:
181 fnNetSimError("Unknown subevent %d in %s\n",
182 pstruEventDetails->nSubEventType,
183 __FUNCTION__);
184 break;
185 }
186 return 0;
187}
188/* Ethernet IPG[1]
189Ethernet variant Minimum transmitted IPG Minimum received IPG
190
19110 Mbit/s Ethernet 9.6 µs 4.7 µs (47 bit times)
192100 Mbit/s (Fast) Ethernet 0.96 µs 0.96 µs (96 bit times)
193Gigabit Ethernet 96 ns 64 ns (64 bit times)
1942.5 Gigabit Ethernet 38.4 ns 16 ns (40 bit times)
1955 Gigabit Ethernet 19.2 ns 8 ns (40 bit times)
19610 Gigabit Ethernet 9.6 ns 4 ns (40 bit times)
19725 Gigabit Ethernet 3.84 ns 1.6 ns (40 bit times)
19840 Gigabit Ethernet 2.4 ns 200 ps (8 bit times)
19950 Gigabit Ethernet 1.92 ns 160 ps (8 bit times)
200100 Gigabit Ethernet 0.96 ns 80 ps (8 bit times)
201200 Gigabit Ethernet 0.48 ns 40 ps (8 bit times)
202400 Gigabit Ethernet 0.24 ns 20 ps (8 bit times)
203*/
204double get_ifg(double link_speed)
205{
206 NETSIM_ID ifg_bits;
207
208 if (link_speed <= 10) // 10 Mbit/s Ethernet
209 ifg_bits = 47;
210 else if (link_speed <= 100) // 100 Mbit/s Ethernet
211 ifg_bits = 96;
212 else if (link_speed <= 1000) // 1Gbit Ethernet
213 ifg_bits = 64;
214 else if (link_speed <= 25000) // 2.5/5/10/25 Gbit Ethernet
215 ifg_bits = 40;
216 else // 40G/50G/100G/200G/400G
217 ifg_bits = 8;
218
219 /*Convert IFG to microseconds
220 IFG (in microseconds) = Ifg_bits / (Link_speed) */
221 return ((double)ifg_bits) / link_speed;
222}