NetSim Source Code Help
Loading...
Searching...
No Matches
Ethernet_Phy.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2021 *
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
29{
30 phy->packet = calloc(1, sizeof* phy->packet);
32 NETSIM_ID in = phy->interfaceId;
34 memcpy(phy->packet, buff, sizeof* phy->packet);
37 phy->packet->pstruPacketlist = NULL;
38}
39
41{
42 if (!packet)
43 return;
44
45 if (!phy->packet)
47
49}
50
52{
53 if (!phy->packet)
54 return NULL;
55
57}
58
59static bool isPhyHasPacket(ptrETH_PHY phy)
60{
61 if (phy->packet)
63 else
64 return false;
65}
66
67double calculate_txtime(ptrETH_PHY phy, double size)
68{
69 double rate = phy->speed;
70 return (size * 8.0) / rate; //In Microsec
71}
72
74 double start,
75 double end,
76 double tx)
77{
78 packet->pstruPhyData->dArrivalTime = start;
79 packet->pstruPhyData->dEndTime = end;
80 packet->pstruPhyData->dOverhead = 0;
83 packet->pstruPhyData->dStartTime = start + tx;
85}
86
89 NETSIM_ID ci)
90{
91 packet->nReceiverId = c;
93
95 memset(&pevent, 0, sizeof pevent);
96 pevent.dEventTime = packet->pstruPhyData->dEndTime;
97 pevent.dPacketSize = packet->pstruPhyData->dPacketSize;
98 if (packet->pstruAppData)
99 {
101 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
102 }
103 pevent.nDeviceId = c;
104 pevent.nDeviceType = DEVICE_TYPE(c);
106 pevent.nInterfaceId = ci;
107 pevent.nPacketId = packet->nPacketId;
109 pevent.pPacket = packet;
110 fnpAddEvent(&pevent);
111}
112
113static void eth_phy_add_phy_out(double time, NetSim_PACKET* packet)
114{
115 NetSim_EVENTDETAILS pevent;
116 memcpy(&pevent, pstruEventDetails, sizeof pevent);
117 pevent.dEventTime = time;
118 pevent.dPacketSize = packet->pstruMacData->dPacketSize;
119 if (packet->pstruAppData)
120 {
122 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
123 }
124 else
125 {
126 pevent.nApplicationId = 0;
127 pevent.nSegmentId = 0;
128 }
129 pevent.nPacketId = packet->nPacketId;
130 pevent.nSubEventType = 0;
131 pevent.pPacket = NULL;
132 fnpAddEvent(&pevent);
133}
135{
138 ptrETH_PHY phy = ETH_PHY_GET(d, in);
140 bool flag = true;
141
142 if (isPhyHasPacket(phy))
143 flag = false;
144
145 eth_phy_packet_add_to_list(phy, packet);
146
148 {
149 // Wait for current transmission to finish
150 if (flag)
151 goto ADD_NEXT;
152 else
153 return 1;
154 }
155
156 packet = eth_phy_packet_get_from_list(phy);
157 if (!packet)
158 return 2; // No packet is there for transmission
159
160 double start;
163 else
164 start = phy->lastPacketEndTime + phy->IFG;
165
166 double size = packet->pstruMacData->dPacketSize;
167
168 double txtime = calculate_txtime(phy, size);
169 double end = start + txtime + phy->propagationDelay;
170 eth_phy_packet_set_param(packet, start, end, txtime);
171
173 phy->lastPacketEndTime = start + txtime;
174
175ADD_NEXT:
176 if (isPhyHasPacket(phy))
179 return 0;
180}
181
183{
187 ptrETH_PHY phy = ETH_PHY_GET(d, in);
188
189 if (phy->isErrorChkReqd)
190 {
192 if (packet->nPacketStatus)
193 {
195 fn_NetSim_Metrics_Add(packet);
197 return -1; //Packet is error. Dropped
198 }
199 }
200
202 fn_NetSim_Metrics_Add(packet);
205 return 0;
206}
unsigned int NETSIM_ID
Definition: Animation.h:45
#define ETH_PHY_GET(d, i)
Definition: Ethernet.h:224
int fn_NetSim_Ethernet_HandlePhyIn()
Definition: Ethernet_Phy.c:182
static void eth_phy_add_phy_out(double time, NetSim_PACKET *packet)
Definition: Ethernet_Phy.c:113
static bool isPhyHasPacket(ptrETH_PHY phy)
Definition: Ethernet_Phy.c:59
static void eth_phy_add_phy_in(NetSim_PACKET *packet, NETSIM_ID c, NETSIM_ID ci)
Definition: Ethernet_Phy.c:87
static void eth_phy_packet_set_param(NetSim_PACKET *packet, double start, double end, double tx)
Definition: Ethernet_Phy.c:73
double calculate_txtime(ptrETH_PHY phy, double size)
Definition: Ethernet_Phy.c:67
static void eth_phy_packet_add_to_list(ptrETH_PHY phy, NetSim_PACKET *packet)
Definition: Ethernet_Phy.c:40
static NetSim_PACKET * eth_phy_packet_get_from_list(ptrETH_PHY phy)
Definition: Ethernet_Phy.c:51
static void init_eth_phy_buffer(ptrETH_PHY phy)
Definition: Ethernet_Phy.c:28
int fn_NetSim_Ethernet_HandlePhyOut()
Definition: Ethernet_Phy.c:134
#define c
#define calloc(c, s)
Definition: Memory.h:29
bool fn_NetSim_GetBufferStatus(NetSim_BUFFER *pstruBuffer)
Definition: Scheduling.c:41
NetSim_PACKET * fn_NetSim_Packet_GetPacketFromBuffer(NetSim_BUFFER *pstruBuffer, int nFlag)
PACKET_STATUS fn_NetSim_Packet_DecideError(double dBER, long double dPacketSize)
@ SCHEDULING_FIFO
Definition: Stack.h:345
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ PHY_MEDIUM_WIRED
Definition: Stack.h:243
@ QUEUING_DROPTAIL
Definition: Stack.h:356
@ MAC_PROTOCOL_IEEE802_3
Definition: Stack.h:213
@ MAC_IN_EVENT
Definition: Stack.h:107
@ PHYSICAL_IN_EVENT
Definition: Stack.h:105
#define DEVICE_ACCESSBUFFER(DeviceId, InterfaceId)
Definition: Stack.h:794
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define fn_NetSim_Metrics_Add(packet)
Definition: Stack.h:998
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fn_NetSim_WritePacketTrace(pstruPacket)
Definition: main.h:188
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
#define fn_NetSim_Packet_AddPacketToList(pstruBuffer, pstruPacket, nInsertionType)
Definition: main.h:179
SCHEDULING_TYPE nSchedulingType
Definition: Stack.h:574
QUEUINGTECHNIQUE queuingTechnique
Definition: Stack.h:575
struct stru_NetSim_Packet * pstruPacketlist
Definition: Stack.h:585
NETSIM_ID nApplicationId
Definition: Stack.h:752
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
long long int nPacketId
Definition: Stack.h:755
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
PHYSICAL_LAYER_MEDIUM nPhyMedium
Definition: Packet.h:237
long long int nPacketId
Definition: Packet.h:256
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
PACKET_STATUS nPacketStatus
Definition: Packet.h:272
struct stru_NetSim_Packet_PhyLayer * pstruPhyData
Definition: Packet.h:277
NETSIM_ID nReceiverId
Definition: Packet.h:266
NETSIM_ID nTransmitterId
Definition: Packet.h:265
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276
NETSIM_ID connectedDevice
Definition: Ethernet.h:215
double propagationDelay
Definition: Ethernet.h:214
double lastPacketEndTime
Definition: Ethernet.h:220
NETSIM_ID connectedInterface
Definition: Ethernet.h:216
bool isErrorChkReqd
Definition: Ethernet.h:222
NETSIM_ID interfaceId
Definition: Ethernet.h:211
double IFG
Definition: Ethernet.h:217
double BER
Definition: Ethernet.h:213
double speed
Definition: Ethernet.h:212
NetSim_BUFFER * packet
Definition: Ethernet.h:219