NetSim Source Code Help
Loading...
Searching...
No Matches
Fragmentation.c
Go to the documentation of this file.
1#include "main.h"
2#include "802_22.h"
3/** In the MAC, the transmitting side has full discretion whether or not to pack
4a group of MAC SDUs into a single MAC PDU. BSs and CPEs shall both have the capability
5of unpacking. If packing is turned on for a connection, the MAC may pack multiple MAC SDUs
6into a single MAC PDU. Also, packing makes use of the connection attribute indicating
7whether the connection carries fixed-length or variable-length packets. */
9{
10 NetSim_PACKET* pstruprevFrag=NULL;
11 FRAGMENT_SUB_HEADER* pstruFragHed;
12 GMH* pstruGMH = (GMH*)pstruPacket->pstruMacData->Packet_MACProtocol;
13 NetSim_PACKET* pstruFragmentList = pstruBSMac->pstruFragmentPacketList;
14 if(pstruGMH->sz_Type[FRAGMANET_BIT] == '0')
15 {
16 return pstruPacket;
17 }
18 pstruFragHed = pstruGMH->subheader[FRAGMANET_BIT];
19 if(pstruFragmentList == NULL)
20 {
21 pstruBSMac->pstruFragmentPacketList = pstruPacket;
22 return NULL;
23 }
24 while(pstruFragmentList)
25 {
26 FRAGMENT_SUB_HEADER* pstruFrag = ((GMH*)pstruFragmentList->pstruMacData->Packet_MACProtocol)->subheader[FRAGMANET_BIT];
27 if(pstruFrag->nFragmentId == pstruFragHed->nFragmentId)
28 {
29 pstruFragmentList->pstruMacData->dPacketSize += pstruPacket->pstruMacData->dPacketSize;
30 pstruFragmentList->pstruMacData->dPayload += pstruPacket->pstruMacData->dPayload;
31 pstruFragmentList->pstruMacData->dOverhead += pstruPacket->pstruMacData->dOverhead;
32 pstruFragmentList->pstruMacData->dEndTime = pstruPacket->pstruMacData->dEndTime;
33
34 pstruFragmentList->pstruPhyData->dPacketSize += pstruPacket->pstruPhyData->dPacketSize;
35 pstruFragmentList->pstruPhyData->dPayload += pstruPacket->pstruPhyData->dPayload;
36 pstruFragmentList->pstruPhyData->dOverhead += pstruPacket->pstruPhyData->dOverhead;
37 pstruFragmentList->pstruPhyData->dEndTime = pstruPacket->pstruPhyData->dEndTime;
38
39 if(pstruFragmentList->pstruAppData && pstruPacket->pstruAppData)
40 pstruFragmentList->pstruAppData->dPayload += pstruPacket->pstruAppData->dPayload;
41 fn_NetSim_Packet_FreePacket(pstruPacket);
42 pstruPacket = NULL;
43 if(pstruFragHed->FC == B2_01)
44 {
45 //Last fragment
46 if(pstruprevFrag)
47 {
48 pstruprevFrag->pstruNextPacket = pstruFragmentList->pstruNextPacket;
49 pstruFragmentList->pstruNextPacket = NULL;
50 }
51 else
52 {
53 pstruBSMac->pstruFragmentPacketList = pstruFragmentList->pstruNextPacket;
54 pstruFragmentList->pstruNextPacket = NULL;
55 }
56 break;
57 }
58 return NULL;
59 }
60 pstruprevFrag = pstruFragmentList;
61 pstruFragmentList = pstruFragmentList->pstruNextPacket;
62 }
63 if(pstruFragmentList)
64 {
65 return pstruFragmentList;
66 }
67 if(pstruPacket)
68 {
69 if(pstruprevFrag)
70 {
71 pstruprevFrag->pstruNextPacket = pstruPacket;
72 }
73 }
74 return NULL;
75}
76/** In the MAC, the transmitting side has full discretion whether or not to pack
77a group of MAC SDUs into a single MAC PDU. BSs and CPEs shall both have the capability
78of unpacking. If packing is turned on for a connection, the MAC may pack multiple MAC SDUs
79into a single MAC PDU. Also, packing makes use of the connection attribute indicating
80whether the connection carries fixed-length or variable-length packets. */
82{
83 NetSim_PACKET* pstruprevFrag=NULL;
84 FRAGMENT_SUB_HEADER* pstruFragHed;
87 GMH* pstruGMH = (GMH*)pstruPacket->pstruMacData->Packet_MACProtocol;
88 NetSim_PACKET* pstruFragmentList = pstruCPEMAC->pstruFragmentPacketList;
89 if(pstruGMH->sz_Type[FRAGMANET_BIT] == '0')
90 {
91 //Move packet to network in
97 return 0;
98 }
99 pstruFragHed = pstruGMH->subheader[FRAGMANET_BIT];
100 if(pstruFragmentList == NULL)
101 {
102 pstruCPEMAC->pstruFragmentPacketList = pstruPacket;
103 return 0;
104 }
105 while(pstruFragmentList)
106 {
107 FRAGMENT_SUB_HEADER* pstruFrag = ((GMH*)pstruFragmentList->pstruMacData->Packet_MACProtocol)->subheader[FRAGMANET_BIT];
108 if(pstruFrag->nFragmentId == pstruFragHed->nFragmentId)
109 {
110 pstruFragmentList->pstruMacData->dPacketSize += pstruPacket->pstruMacData->dPacketSize;
111 pstruFragmentList->pstruMacData->dPayload += pstruPacket->pstruMacData->dPayload;
112 pstruFragmentList->pstruMacData->dOverhead += pstruPacket->pstruMacData->dOverhead;
113 pstruFragmentList->pstruMacData->dEndTime = pstruPacket->pstruMacData->dEndTime;
114
115 pstruFragmentList->pstruPhyData->dPacketSize += pstruPacket->pstruPhyData->dPacketSize;
116 pstruFragmentList->pstruPhyData->dPayload += pstruPacket->pstruPhyData->dPayload;
117 pstruFragmentList->pstruPhyData->dOverhead += pstruPacket->pstruPhyData->dOverhead;
118 pstruFragmentList->pstruPhyData->dEndTime = pstruPacket->pstruPhyData->dEndTime;
119
120
121 if(pstruFragmentList->pstruAppData && pstruPacket->pstruAppData)
122 pstruFragmentList->pstruAppData->dPayload += pstruPacket->pstruAppData->dPayload;
123 if(pstruFragHed->FC == B2_01)
124 {
125 //Last fragment
126 if(pstruprevFrag)
127 {
128 pstruprevFrag->pstruNextPacket = pstruFragmentList->pstruNextPacket;
129 pstruFragmentList->pstruNextPacket = NULL;
130 }
131 else
132 {
133 pstruCPEMAC->pstruFragmentPacketList = pstruFragmentList->pstruNextPacket;
134 pstruFragmentList->pstruNextPacket = NULL;
135 }
136 break;
137 }
138 fn_NetSim_Packet_FreePacket(pstruPacket);
139 pstruPacket = NULL;
140 return 0;
141 }
142 pstruprevFrag = pstruFragmentList;
143 pstruFragmentList = pstruFragmentList->pstruNextPacket;
144 }
145 if(pstruFragmentList)
146 {
147 //Move packet to network in
149 pstruEventDetails->dPacketSize -= pstruFragmentList->pstruMacData->dOverhead;
152 pstruEventDetails->pPacket = pstruFragmentList;
154 return 0;
155 }
156 if(pstruPacket)
157 {
158 if(pstruprevFrag)
159 {
160 pstruprevFrag->pstruNextPacket = pstruPacket;
161 }
162 }
163 return 0;
164}
165
166/** Fragmentation is the process by which a MAC SDU is divided into one or more MAC PDUs.
167This process is undertaken to allow efficient use of available bandwidth relative to the
168QoS requirements of a connection’s service flow. Upon the creation of a connection by the
169MAC SAP, fragmentation capability is defined. Fragmentation may be initiated by a BS for
170downstream connections and by a CPE for upstream connections. */
171int fn_NetSim_CR_FragmentPacket(NetSim_PACKET* pstruPacket,double dSDUSize)
172{
173 NetSim_PACKET* pstruTempPacket;
174 FRAGMENT_SUB_HEADER* pstruFragment;
175 GMH* pstruGMH;
176 int nCount;
177 unsigned int n=0;
178 double dSize = pstruPacket->pstruNetworkData->dPacketSize;
179 double dAppSize;
180 if(dSize <= dSDUSize)
181 {
182 /* update the MAC header*/
184 pstruPacket->pstruMacData->dPayload = pstruPacket->pstruNetworkData->dPacketSize;
185 pstruPacket->pstruMacData->dOverhead = GMH_SIZE;
186 pstruPacket->pstruMacData->dPacketSize = pstruPacket->pstruMacData->dOverhead + pstruPacket->pstruMacData->dPayload;
187
188 pstruPacket->pstruPhyData->dPayload = pstruPacket->pstruMacData->dPacketSize;
189 pstruPacket->pstruPhyData->dOverhead = 0;
190 pstruPacket->pstruPhyData->dPacketSize = pstruPacket->pstruPhyData->dOverhead + pstruPacket->pstruPhyData->dPayload;
191
192 pstruGMH = fnpAllocateMemory(1,sizeof *pstruGMH);
193 pstruGMH->n_Length = (int)pstruPacket->pstruMacData->dPacketSize;
194 pstruGMH->n_UCS = 0;
195 pstruGMH->sz_Type[FRAGMANET_BIT] = '0';
196 pstruPacket->pstruMacData->Packet_MACProtocol =pstruGMH;
198 return 0;
199 }
200 ++g_FragmentId;
201 nCount = (int)ceil(dSize/dSDUSize);
202 if(pstruPacket->pstruAppData)
203 dAppSize = pstruPacket->pstruAppData->dPayload/nCount;
204 else
205 dAppSize = 0;
206 /* update the MAC header*/
208 pstruPacket->pstruMacData->dPayload = dSDUSize;
210 pstruPacket->pstruMacData->dPacketSize = pstruPacket->pstruMacData->dOverhead + pstruPacket->pstruMacData->dPayload;
211
212 pstruPacket->pstruPhyData->dPayload = pstruPacket->pstruMacData->dPacketSize;
213 pstruPacket->pstruPhyData->dOverhead = 0;
214 pstruPacket->pstruPhyData->dPacketSize = pstruPacket->pstruPhyData->dOverhead + pstruPacket->pstruPhyData->dPayload;
215
216 if(pstruPacket->pstruAppData)
217 pstruPacket->pstruAppData->dPayload = dAppSize;
218 pstruGMH = fnpAllocateMemory(1,sizeof *pstruGMH);
219 pstruGMH->n_Length = (int)pstruPacket->pstruMacData->dPacketSize;
220 pstruGMH->n_UCS = 0;
221 pstruPacket->pstruMacData->Packet_MACProtocol =pstruGMH;
223 pstruFragment = fnpAllocateMemory(1,sizeof *pstruFragment);
224 pstruFragment->purpos = 0;
225 pstruFragment->FC = B2_10;
226 pstruFragment->length = (unsigned int)dSDUSize;
227 pstruFragment->FSN = ++n;
228 pstruFragment->nFragmentId = g_FragmentId;
229 pstruGMH->subheader[FRAGMANET_BIT] = pstruFragment;
230 pstruGMH->sz_Type[FRAGMANET_BIT] = '1';
231 dSize -= dSDUSize;
232 nCount--;
233 while(nCount)
234 {
235 pstruTempPacket = fn_NetSim_Packet_CopyPacket(pstruPacket);
236 pstruTempPacket->pstruNextPacket = NULL;
237 pstruPacket->pstruNextPacket = pstruTempPacket;
238 pstruPacket = pstruPacket->pstruNextPacket;
241 pstruPacket->pstruMacData->dPacketSize = pstruPacket->pstruMacData->dOverhead + pstruPacket->pstruMacData->dPayload;
242
243 pstruPacket->pstruPhyData->dPayload = pstruPacket->pstruMacData->dPacketSize;
244 pstruPacket->pstruPhyData->dOverhead = 0;
245 pstruPacket->pstruPhyData->dPacketSize = pstruPacket->pstruPhyData->dOverhead + pstruPacket->pstruPhyData->dPayload;
246
247 pstruGMH = PACKET_MACPROTOCOLDATA(pstruPacket);
248 pstruGMH->n_Length = (int)pstruPacket->pstruMacData->dPacketSize;
249 pstruGMH->n_UCS = 0;
250 pstruPacket->pstruMacData->Packet_MACProtocol =pstruGMH;
252 pstruFragment = pstruGMH->subheader[FRAGMANET_BIT];
253 pstruFragment->purpos = 0;
254 pstruFragment->length = (unsigned int)dSDUSize;
255 pstruFragment->FSN = ++n;
256 pstruFragment->nFragmentId = g_FragmentId;
257 if(dSize>dSDUSize)
258 {
259 pstruPacket->pstruMacData->dPayload = dSDUSize;
260 pstruFragment->FC = B2_11;
261 }
262 else
263 {
264 pstruPacket->pstruMacData->dPayload = dSize;
265 pstruFragment->FC = B2_01;
266 }
267 pstruGMH->sz_Type[FRAGMANET_BIT] = '1';
268 dSize -= dSDUSize;
269 nCount--;
270 }
271 return 0;
272}
273
274
#define GMH_SIZE
GMH Size is 4 Bytes.
Definition: 802_22.h:121
unsigned int g_FragmentId
Definition: 802_22.h:132
#define FRAGMENT_SIZE
Fragment size is 3 Bytes.
Definition: 802_22.h:123
#define B2_11
Definition: 802_22.h:28
#define B2_10
Definition: 802_22.h:27
#define B2_01
Definition: 802_22.h:26
#define FRAGMANET_BIT
Definition: 802_22.h:65
#define NETSIM_ID
Definition: CLIInterface.h:46
NetSim_PACKET * fn_NetSim_CR_BS_PackPacket(BS_MAC *pstruBSMac, NetSim_PACKET *pstruPacket)
Definition: Fragmentation.c:8
int fn_NetSim_CR_CPE_PackPacket()
Definition: Fragmentation.c:81
int fn_NetSim_CR_FragmentPacket(NetSim_PACKET *pstruPacket, double dSDUSize)
#define fnpAllocateMemory(count, size)
Definition: Memory.h:34
#define PACKET_MACPROTOCOLDATA(pPacket)
Definition: Packet.h:300
NETWORK_LAYER_PROTOCOL fn_NetSim_Stack_GetNWProtocol(NETSIM_ID nDeviceId)
@ MAC_PROTOCOL_IEEE802_22
Definition: Stack.h:210
@ NETWORK_IN_EVENT
Definition: Stack.h:109
#define DEVICE_MACVAR(DeviceId, InterfaceId)
Definition: Stack.h:798
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define fn_NetSim_Packet_CopyPacket(pstruPacket)
Definition: main.h:182
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
NetSim_PACKET * pstruFragmentPacketList
Definition: 802_22.h:811
CPE structure for IEEE 802.22 MAC.
Definition: 802_22.h:869
NetSim_PACKET * pstruFragmentPacketList
Definition: 802_22.h:894
unsigned int nFragmentId
Definition: 802_22.h:614
void * subheader[5]
Subheader.
Definition: 802_22.h:605
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
NETSIM_ID nInterfaceId
Definition: Stack.h:751
MAC_LAYER_PROTOCOL nMACProtocol
Definition: Packet.h:223
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
struct stru_NetSim_Packet_PhyLayer * pstruPhyData
Definition: Packet.h:277
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
struct stru_NetSim_Packet * pstruNextPacket
Definition: Packet.h:278
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276