-
Notifications
You must be signed in to change notification settings - Fork 480
/
EthernetMacBase.cc
622 lines (531 loc) · 22.5 KB
/
EthernetMacBase.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//
// Copyright (C) 2004 OpenSim Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//
#include "inet/linklayer/ethernet/base/EthernetMacBase.h"
#include <stdlib.h>
#include "inet/common/INETUtils.h"
#include "inet/common/ModuleAccess.h"
#include "inet/common/ProtocolTag_m.h"
#include "inet/common/StringFormat.h"
#include "inet/common/checksum/EthernetCRC.h"
#include "inet/common/lifecycle/ModuleOperations.h"
#include "inet/common/packet/chunk/BytesChunk.h"
#include "inet/linklayer/common/EtherType_m.h"
#include "inet/linklayer/ethernet/common/Ethernet.h"
#include "inet/linklayer/ethernet/common/EthernetControlFrame_m.h"
#include "inet/linklayer/ethernet/common/EthernetMacHeader_m.h"
#include "inet/networklayer/common/NetworkInterface.h"
#include "inet/physicallayer/wired/ethernet/EthernetPhyHeader_m.h"
#include "inet/physicallayer/wired/ethernet/EthernetSignal_m.h"
#include "inet/queueing/function/PacketComparatorFunction.h"
namespace inet {
static int compareEthernetFrameType(Packet *a, Packet *b)
{
const auto& ah = a->peekAtFront<EthernetMacHeader>();
const auto& bh = b->peekAtFront<EthernetMacHeader>();
int ac = (ah->getTypeOrLength() == ETHERTYPE_FLOW_CONTROL) ? 0 : 1;
int bc = (bh->getTypeOrLength() == ETHERTYPE_FLOW_CONTROL) ? 0 : 1;
return ac - bc;
}
Register_Packet_Comparator_Function(EthernetFrameTypeComparator, compareEthernetFrameType);
simsignal_t EthernetMacBase::rxPkOkSignal = registerSignal("rxPkOk");
simsignal_t EthernetMacBase::txPausePkUnitsSignal = registerSignal("txPausePkUnits");
simsignal_t EthernetMacBase::rxPausePkUnitsSignal = registerSignal("rxPausePkUnits");
simsignal_t EthernetMacBase::transmissionStateChangedSignal = registerSignal("transmissionStateChanged");
simsignal_t EthernetMacBase::receptionStateChangedSignal = registerSignal("receptionStateChanged");
EthernetMacBase::EthernetMacBase()
{
lastTxFinishTime = -1.0; // never equals to current simtime
}
EthernetMacBase::~EthernetMacBase()
{
delete curTxSignal;
cancelAndDelete(endTxTimer);
cancelAndDelete(endIfgTimer);
cancelAndDelete(endPauseTimer);
}
void EthernetMacBase::initialize(int stage)
{
MacProtocolBase::initialize(stage);
if (stage == INITSTAGE_LOCAL) {
fcsMode = parseFcsMode(par("fcsMode"));
allowNonstandardBitrate = par("allowNonstandardBitrate");
physInGate = gate("phys$i");
physOutGate = gate("phys$o");
lowerLayerInGateId = physInGate->getId();
lowerLayerOutGateId = physOutGate->getId();
transmissionChannel = nullptr;
txQueue = getQueue(gate(upperLayerInGateId));
initializeFlags();
initializeStatistics();
lastTxFinishTime = -1.0; // not equals with current simtime.
// initialize self messages
endTxTimer = new cMessage("EndTransmission", ENDTRANSMISSION);
endIfgTimer = new cMessage("EndIFG", ENDIFG);
endPauseTimer = new cMessage("EndPause", ENDPAUSE);
// initialize states
transmitState = TX_IDLE_STATE;
receiveState = RX_IDLE_STATE;
// initialize pause
pauseUnitsRequested = 0;
subscribe(POST_MODEL_CHANGE, this);
WATCH(transmitState);
WATCH(receiveState);
WATCH(connected);
WATCH(frameBursting);
WATCH(promiscuous);
WATCH(pauseUnitsRequested);
}
}
void EthernetMacBase::initializeFlags()
{
displayStringTextFormat = par("displayStringTextFormat");
sendRawBytes = par("sendRawBytes");
duplexMode = true;
// initialize connected flag
connected = physOutGate->getPathEndGate()->isConnected() && physInGate->getPathStartGate()->isConnected();
if (!connected)
EV_WARN << "MAC not connected to a network.\n";
// initialize promiscuous flag
promiscuous = par("promiscuous");
frameBursting = false;
}
void EthernetMacBase::initializeStatistics()
{
numFramesSent = numFramesReceivedOK = numBytesSent = numBytesReceivedOK = 0;
numFramesPassedToHL = numDroppedBitError = numDroppedNotForUs = 0;
numFramesFromHL = numDroppedIfaceDown = numDroppedPkFromHLIfaceDown = 0;
numPauseFramesRcvd = numPauseFramesSent = 0;
WATCH(numFramesSent);
WATCH(numFramesReceivedOK);
WATCH(numBytesSent);
WATCH(numBytesReceivedOK);
WATCH(numFramesFromHL);
WATCH(numDroppedPkFromHLIfaceDown);
WATCH(numDroppedIfaceDown);
WATCH(numDroppedBitError);
WATCH(numDroppedNotForUs);
WATCH(numFramesPassedToHL);
WATCH(numPauseFramesRcvd);
WATCH(numPauseFramesSent);
}
void EthernetMacBase::configureNetworkInterface()
{
// MTU: typical values are 576 (Internet de facto), 1500 (Ethernet-friendly),
// 4000 (on some point-to-point links), 4470 (Cisco routers default, FDDI compatible)
networkInterface->setMtu(par("mtu"));
// capabilities
networkInterface->setMulticast(true);
networkInterface->setBroadcast(true);
}
void EthernetMacBase::handleStartOperation(LifecycleOperation *operation)
{
networkInterface->setState(NetworkInterface::State::UP);
initializeFlags();
readChannelParameters(true);
}
void EthernetMacBase::handleStopOperation(LifecycleOperation *operation)
{
if (currentTxFrame != nullptr || !txQueue->isEmpty()) {
networkInterface->setState(NetworkInterface::State::GOING_DOWN);
delayActiveOperationFinish(par("stopOperationTimeout"));
}
else {
networkInterface->setCarrier(false);
networkInterface->setState(NetworkInterface::State::DOWN);
connected = false;
startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
}
}
void EthernetMacBase::handleCrashOperation(LifecycleOperation *operation)
{
// clearQueue();
connected = false;
networkInterface->setCarrier(false);
processConnectDisconnect();
networkInterface->setState(NetworkInterface::State::DOWN);
}
// TODO this method should be renamed and called where processing is finished on the current frame (i.e. curTxFrame becomes nullptr)
void EthernetMacBase::processAtHandleMessageFinished()
{
if (operationalState == State::STOPPING_OPERATION) {
if (currentTxFrame == nullptr && txQueue->isEmpty()) {
EV << "Ethernet Queue is empty, MAC stopped\n";
connected = false;
networkInterface->setCarrier(false);
processConnectDisconnect();
networkInterface->setState(NetworkInterface::State::DOWN);
connected = false;
startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
}
}
}
void EthernetMacBase::receiveSignal(cComponent *source, simsignal_t signalID, cObject *obj, cObject *details)
{
Enter_Method("%s", cComponent::getSignalName(signalID));
MacProtocolBase::receiveSignal(source, signalID, obj, details);
if (signalID == POST_MODEL_CHANGE) {
if (auto gcobj = dynamic_cast<cPostPathCreateNotification *>(obj)) {
if ((physOutGate == gcobj->pathStartGate) || (physInGate == gcobj->pathEndGate))
refreshConnection();
}
else if (auto gcobj = dynamic_cast<cPostPathCutNotification *>(obj)) {
if ((physOutGate == gcobj->pathStartGate) || (physInGate == gcobj->pathEndGate))
refreshConnection();
}
else if (transmissionChannel && dynamic_cast<cPostParameterChangeNotification *>(obj)) { // note: we are subscribed to the channel object too
cPostParameterChangeNotification *gcobj = static_cast<cPostParameterChangeNotification *>(obj);
if (transmissionChannel == gcobj->par->getOwner())
refreshConnection();
}
}
}
void EthernetMacBase::processConnectDisconnect()
{
if (!connected) {
cancelEvent(endIfgTimer);
cancelEvent(endPauseTimer);
if (curTxSignal) {
#if OMNETPP_BUILDNUM < 2001
if (getSimulation()->getSimulationStage() == STAGE(EVENT) && physOutGate->getPathEndGate()->isConnected()) {
#else
if (getSimulation()->getStage() == STAGE(EVENT) && physOutGate->getPathEndGate()->isConnected()) {
#endif
ASSERT(endTxTimer->isScheduled());
curTxSignal->setDuration(endTxTimer->getArrivalTime() - curTxSignal->getCreationTime());
simtime_t duration = simTime() - curTxSignal->getCreationTime(); // TODO save and use start tx time
cutEthernetSignalEnd(curTxSignal, duration);
emit(transmissionEndedSignal, curTxSignal);
send(curTxSignal, SendOptions().finishTx(curTxSignal->getId()), physOutGate);
}
else
delete curTxSignal;
curTxSignal = nullptr;
cancelEvent(endTxTimer);
}
ASSERT(!endTxTimer->isScheduled());
if (currentTxFrame) {
EV_DETAIL << "Interface is not connected, dropping packet " << currentTxFrame << endl;
numDroppedPkFromHLIfaceDown++;
PacketDropDetails details;
details.setReason(INTERFACE_DOWN);
dropCurrentTxFrame(details);
lastTxFinishTime = -1.0; // so that it never equals to the current simtime, used for Burst mode detection.
}
// Clear queue
while (canDequeuePacket()) {
Packet *msg = dequeuePacket();
EV_DETAIL << "Interface is not connected, dropping packet " << msg << endl;
numDroppedPkFromHLIfaceDown++;
PacketDropDetails details;
details.setReason(INTERFACE_DOWN);
emit(packetDroppedSignal, msg, &details);
delete msg;
}
changeTransmissionState(TX_IDLE_STATE); // FIXME replace status to OFF
changeReceptionState(RX_IDLE_STATE);
}
// FIXME when connect, set statuses to RECONNECT or IDLE
}
void EthernetMacBase::encapsulate(Packet *frame)
{
auto phyHeader = makeShared<EthernetPhyHeader>();
frame->insertAtFront(phyHeader);
frame->addTagIfAbsent<PacketProtocolTag>()->setProtocol(&Protocol::ethernetPhy);
}
void EthernetMacBase::decapsulate(Packet *packet)
{
auto phyHeader = packet->popAtFront<EthernetPhyHeader>();
ASSERT(packet->getDataLength() >= MIN_ETHERNET_FRAME_BYTES);
packet->addTagIfAbsent<PacketProtocolTag>()->setProtocol(&Protocol::ethernetMac);
}
// FIXME should use it in EthernetCsmaMac, EthernetMac, etc. modules. But should not use it in EtherBus, EthernetHub.
bool EthernetMacBase::verifyCrcAndLength(Packet *packet)
{
EV_STATICCONTEXT;
auto ethHeader = packet->peekAtFront<EthernetMacHeader>(); // FIXME can I use any flags?
const auto& ethTrailer = packet->peekAtBack<EthernetFcs>(ETHER_FCS_BYTES); // FIXME can I use any flags?
switch (ethTrailer->getFcsMode()) {
case FCS_DECLARED_CORRECT:
break;
case FCS_DECLARED_INCORRECT:
EV_ERROR << "incorrect FCS in ethernet frame\n";
return false;
case FCS_COMPUTED: {
bool isFcsBad = false;
// check the FCS
auto ethBytes = packet->peekDataAt<BytesChunk>(B(0), packet->getDataLength() - ethTrailer->getChunkLength());
auto bufferLength = B(ethBytes->getChunkLength()).get();
auto buffer = new uint8_t[bufferLength];
// 1. fill in the data
ethBytes->copyToBuffer(buffer, bufferLength);
// 2. compute the FCS
auto computedFcs = ethernetCRC(buffer, bufferLength);
delete[] buffer;
isFcsBad = (computedFcs != ethTrailer->getFcs()); // FIXME how to check fcs?
if (isFcsBad)
return false;
break;
}
default:
throw cRuntimeError("invalid FCS mode in ethernet frame");
}
if (isIeee8023Header(*ethHeader)) {
b payloadLength = B(ethHeader->getTypeOrLength());
return payloadLength <= packet->getDataLength() - (ethHeader->getChunkLength() + ethTrailer->getChunkLength());
}
return true;
}
void EthernetMacBase::refreshConnection()
{
Enter_Method("refreshConnection");
bool oldConn = connected;
readChannelParameters(false);
if (oldConn != connected)
processConnectDisconnect();
}
bool EthernetMacBase::dropFrameNotForUs(Packet *packet, const Ptr<const EthernetMacHeader>& frame)
{
// Current ethernet mac implementation does not support the configuration of multicast
// ethernet address groups. We rather accept all multicast frames (just like they were
// broadcasts) and pass it up to the higher layer where they will be dropped
// if not needed.
//
// PAUSE frames must be handled a bit differently because they are processed at
// this level. Multicast PAUSE frames should not be processed unless they have a
// destination of MULTICAST_PAUSE_ADDRESS. We drop all PAUSE frames that have a
// different muticast destination. (Note: Would the multicast ethernet addressing
// implemented, we could also process the PAUSE frames destined to any of our
// multicast adresses)
// All NON-PAUSE frames must be passed to the upper layer if the interface is
// in promiscuous mode.
if (frame->getDest().equals(getMacAddress()))
return false;
if (frame->getDest().isBroadcast())
return false;
bool isPause = (frame->getTypeOrLength() == ETHERTYPE_FLOW_CONTROL);
if (!isPause && (promiscuous || frame->getDest().isMulticast()))
return false;
if (isPause && frame->getDest().equals(MacAddress::MULTICAST_PAUSE_ADDRESS))
return false;
EV_WARN << "Frame `" << packet->getName() << "' not destined to us, discarding\n";
numDroppedNotForUs++;
PacketDropDetails details;
details.setReason(NOT_ADDRESSED_TO_US);
emit(packetDroppedSignal, packet, &details);
delete packet;
return true;
}
void EthernetMacBase::readChannelParameters(bool errorWhenAsymmetric)
{
// When the connected channels change at runtime, we'll receive
// two separate notifications (one for the rx channel and one for the tx one),
// so we cannot immediately raise an error when they differ. Rather, we'll need
// to verify at the next opportunity (event) that the two channels have eventually
// been set to the same value.
cDatarateChannel *outTrChannel = check_and_cast_nullable<cDatarateChannel *>(physOutGate->findTransmissionChannel());
cDatarateChannel *inTrChannel = check_and_cast_nullable<cDatarateChannel *>(physInGate->findIncomingTransmissionChannel());
connected = physOutGate->getPathEndGate()->isConnected() && physInGate->getPathStartGate()->isConnected();
if (connected && ((!outTrChannel) || (!inTrChannel)))
throw cRuntimeError("Ethernet phys gate must be connected using a transmission channel");
double txRate = outTrChannel ? outTrChannel->getNominalDatarate() : 0.0;
double rxRate = inTrChannel ? inTrChannel->getNominalDatarate() : 0.0;
bool rxDisabled = !inTrChannel || inTrChannel->isDisabled();
bool txDisabled = !outTrChannel || outTrChannel->isDisabled();
if (errorWhenAsymmetric && (rxDisabled != txDisabled))
throw cRuntimeError("The enablements of the input/output channels differ (rx=%s vs tx=%s)", rxDisabled ? "off" : "on", txDisabled ? "off" : "on");
if (txDisabled)
connected = false;
bool dataratesDiffer;
if (!connected) {
curEtherDescr = EthernetModes::nullEthernetMode;
halfBitTime = SIMTIME_ZERO;
dataratesDiffer = false;
if (!outTrChannel)
transmissionChannel = nullptr;
if (networkInterface) {
networkInterface->setCarrier(false);
networkInterface->setDatarate(0);
}
}
else {
if (outTrChannel && !transmissionChannel)
outTrChannel->subscribe(POST_MODEL_CHANGE, this);
transmissionChannel = outTrChannel;
dataratesDiffer = (txRate != rxRate);
}
channelsDiffer = dataratesDiffer || (rxDisabled != txDisabled);
if (errorWhenAsymmetric && dataratesDiffer)
throw cRuntimeError("The input/output datarates differ (rx=%g bps vs tx=%g bps)", rxRate, txRate);
if (connected) {
// Check valid speeds
curEtherDescr = EthernetModes::getEthernetMode(txRate, allowNonstandardBitrate);
halfBitTime = 0.5 / txRate;
if (networkInterface) {
networkInterface->setCarrier(true);
networkInterface->setDatarate(txRate);
}
}
}
void EthernetMacBase::printParameters()
{
// Dump parameters
EV_DETAIL << "MAC address: " << getMacAddress() << (promiscuous ? ", promiscuous mode" : "") << endl
<< "txrate: " << curEtherDescr.bitrate << " bps, "
<< (duplexMode ? "full-duplex" : "half-duplex") << endl
<< "bitTime: " << 1e9 / curEtherDescr.bitrate << " ns" << endl
<< "frameBursting: " << (frameBursting ? "on" : "off") << endl
<< "slotBitLength: " << curEtherDescr.slotBitLength << endl
<< "interFrameGap: " << INTERFRAME_GAP_BITS / curEtherDescr.bitrate << endl
<< endl;
}
void EthernetMacBase::finish()
{
{
simtime_t t = simTime();
recordScalar("simulated time", t);
recordScalar("full-duplex", duplexMode);
if (t > 0) {
recordScalar("frames/sec sent", numFramesSent / t);
recordScalar("frames/sec rcvd", numFramesReceivedOK / t);
recordScalar("bits/sec sent", (8.0 * numBytesSent) / t);
recordScalar("bits/sec rcvd", (8.0 * numBytesReceivedOK) / t);
}
}
}
void EthernetMacBase::refreshDisplay() const
{
MacProtocolBase::refreshDisplay();
// icon coloring
const char *color;
if (receiveState == RX_COLLISION_STATE)
color = "red";
else if (transmitState == TRANSMITTING_STATE)
color = "yellow";
else if (transmitState == JAMMING_STATE)
color = "red";
else if (receiveState == RECEIVING_STATE)
color = "#4040ff";
else if (transmitState == BACKOFF_STATE)
color = "white";
else if (transmitState == PAUSE_STATE)
color = "gray";
else
color = "";
getDisplayString().setTagArg("i", 1, color);
if (!strcmp(getParentModule()->getNedTypeName(), "inet.linklayer.ethernet.EthernetInterface"))
getParentModule()->getDisplayString().setTagArg("i", 1, color);
auto text = StringFormat::formatString(displayStringTextFormat, [&] (char directive) -> std::string {
switch (directive) {
case 's':
return std::to_string(numFramesSent);
case 'r':
return std::to_string(numFramesReceivedOK);
case 'd':
return std::to_string(numDroppedPkFromHLIfaceDown + numDroppedIfaceDown + numDroppedBitError + numDroppedNotForUs);
case 'q':
return txQueue != nullptr ? std::to_string(txQueue->getNumPackets()) : "";
case 'b':
if (transmissionChannel == nullptr)
return "not connected";
else {
char datarateText[40];
double datarate = transmissionChannel->getNominalDatarate();
if (datarate >= 1e9)
sprintf(datarateText, "%gGbps", datarate / 1e9);
else if (datarate >= 1e6)
sprintf(datarateText, "%gMbps", datarate / 1e6);
else if (datarate >= 1e3)
sprintf(datarateText, "%gkbps", datarate / 1e3);
else
sprintf(datarateText, "%gbps", datarate);
return datarateText;
}
default:
throw cRuntimeError("Unknown directive: %c", directive);
}
});
getDisplayString().setTagArg("t", 0, text.c_str());
}
void EthernetMacBase::changeTransmissionState(MacTransmitState newState)
{
transmitState = newState;
emit(transmissionStateChangedSignal, newState);
}
void EthernetMacBase::changeReceptionState(MacReceiveState newState)
{
receiveState = newState;
emit(receptionStateChangedSignal, newState);
}
void EthernetMacBase::addPaddingAndSetFcs(Packet *packet, B requiredMinBytes) const
{
auto ethFcs = packet->removeAtBack<EthernetFcs>(ETHER_FCS_BYTES);
ethFcs->setFcsMode(fcsMode);
B paddingLength = requiredMinBytes - ETHER_FCS_BYTES - B(packet->getByteLength());
if (paddingLength > B(0)) {
const auto& ethPadding = makeShared<EthernetPadding>();
ethPadding->setChunkLength(paddingLength);
packet->insertAtBack(ethPadding);
}
switch (ethFcs->getFcsMode()) {
case FCS_DECLARED_CORRECT:
ethFcs->setFcs(0xC00DC00DL);
break;
case FCS_DECLARED_INCORRECT:
ethFcs->setFcs(0xBAADBAADL);
break;
case FCS_COMPUTED: { // calculate FCS
auto ethBytes = packet->peekDataAsBytes();
auto bufferLength = B(ethBytes->getChunkLength()).get();
auto buffer = new uint8_t[bufferLength];
// 1. fill in the data
ethBytes->copyToBuffer(buffer, bufferLength);
// 2. compute the FCS
auto computedFcs = ethernetCRC(buffer, bufferLength);
delete[] buffer;
ethFcs->setFcs(computedFcs);
break;
}
default:
throw cRuntimeError("Unknown FCS mode: %d", (int)(ethFcs->getFcsMode()));
}
packet->insertAtBack(ethFcs);
}
void EthernetMacBase::cutEthernetSignalEnd(EthernetSignalBase *signal, simtime_t duration)
{
ASSERT(duration <= signal->getDuration());
if (duration == signal->getDuration())
return;
int64_t newBitLength = duration.dbl() * signal->getBitrate();
if (auto packet = check_and_cast_nullable<Packet *>(signal->decapsulate())) {
// TODO removed length calculation based on the PHY layer (parallel bits, bit order, etc.)
if (newBitLength < packet->getBitLength()) {
packet->trimFront();
packet->setBackOffset(B(newBitLength / 8)); //TODO rounded down to byte align instead of b(newBitLength)
packet->trimBack();
packet->setBitError(true);
}
signal->encapsulate(packet);
}
signal->setBitError(true);
signal->setBitLength(newBitLength);
signal->setDuration(duration);
}
void EthernetMacBase::txFinished()
{
ASSERT(curTxSignal != nullptr);
delete curTxSignal;
curTxSignal = nullptr;
}
queueing::IPassivePacketSource *EthernetMacBase::getProvider(const cGate *gate)
{
return (gate->getId() == upperLayerInGateId) ? txQueue.get() : nullptr;
}
void EthernetMacBase::handlePullPacketProcessed(Packet *packet, const cGate *gate, bool successful)
{
Enter_Method("handlePullPacketProcessed");
throw cRuntimeError("Not supported callback");
}
} // namespace inet