# Modeling Network-Controlled Device-to-Device Communications in SimuLTE
## 5 Modeling Device-to-Device Communications in SimuLTE
### Modifying Protocol Layers to Accommodate D2D Data Processing
While there is no standard prescription on how to do this, our understanding is that ACKs/NACKs are needed at both the transmitting endpoint (another UE) and the scheduling entity (the eNB), since both need to take action according to the feedback: the transmitting endpoint has to either free or keep busy the H-ARQ process involved in the transmission, and the scheduling entity has to schedule retransmissions of a busy H-ARQ process at the transmitter when the receiver sends a NACK. Therefore, coherently with [18], we assume that the eNB overhears the H-ARQ feedback sent by the receiving UE to the transmitter UE. This assumption is sound, since H-ARQ control channels in LTE use a fixed, robust modulation, and are power-controlled to reach the eNB. With reference to Figure 11, the above assumption is realized by sending the ACK/NACK through a sendDirect() to both the transmitter and the eNB, including a counter indexing the process that the feedback refers to. The transmitter updates its H-ARQ process the same way it would in an UL transmission (where it would receive the same feedback from the eNB itself). The eNB's MAC layer uses the feedback to update a data structure that mirrors the status of each H-ARQ Tx buffer on the transmitter, consisting of a vector of eight elements (i.e., one per H-ARQ process). Note that P2P D2D data transmissions are not overheard by the eNB, hence the contents of the H-ARQ Tx buffers are not stored at the eNB. In P2P D2D communications between two UEs, several factors, such as mobility, interference and network conditions, might affect the performance and, in some cases, trigger a mode switch of the communication, i.e., changing the communication mode to the infrastructure path or vice versa. For example, when the two endpoints move far from each other and get out of the respective hearing range, switching to the infrastructure mode is the only way to keep the connection alive. Conversely, two endpoints that were too far for D2D communications may become near enough, hence switching them to the SL could improve their performance. MS is also advisable when one endpoint of a D2D communication initiates handover: maintaining a direct communication between two UEs located in different cells would require that the corresponding eNBs coordinate their resource allocation in a tight lockstep [18], which is computationally costly. Direct communication might then resume if and when both UEs are again under the control of the same eNB. MS decisions are made by the eNB, which informs both UEs about the path that they should use thereafter. However, a MS requires actions to be taken at every layer of the LTE protocol stack at the UEs' side (e.g., clearing buffers at MAC and RLC layers). In SimuLTE, we implemented the MS notification as an OMNeT++ message generated by the eNB's MAC layer and sent (through its PHY layer) to both the endpoints of the P2P D2D communication. This message reaches the destinations' PHY layer in  
Figure 11: Example of H-ARQ buffer mirroring. one TTI and it is always decoded with no errors. With reference to Figure 12, the message traverses all the modules implementing the layers of the LTE protocol stack. Every layer provides a handler that is invoked upon reception of the notification from the lower layer, accomplishes the required MS operations for that layer, then it forwards the message to the upper layer. This implementation allows one to write its own handlers to implement different MS schemes. Examples of MS decisions at the different layers are the following:  
* At the MAC layer of the transmitter, how to manage busy H-ARQ processes, e.g., those waiting for an acknowledgement or a grant for retransmitting a MAC PDU. One choice may be to interrupt them, hence dropping MAC PDUs awaiting retransmission. Alternatively, one could allow H-ARQ processes to terminate their cycle along the \"old\" path before switching to the \"new\" one.