INET Framework for OMNeT++/OMNEST
inet::Gptp Class Reference

#include <Gptp.h>

Inheritance diagram for inet::Gptp:
inet::ClockUserModuleBase inet::ClockUserModuleMixin< cSimpleModule >

Public Member Functions

virtual ~Gptp ()
 
void sendPacketToNIC (Packet *packet, int portId)
 
void sendSync ()
 
void sendFollowUp (int portId, const GptpSync *sync, clocktime_t preciseOriginTimestamp)
 
void sendPdelayReq ()
 
void sendPdelayResp (GptpReqAnswerEvent *req)
 
void sendPdelayRespFollowUp (int portId, const GptpPdelayResp *resp)
 
void processSync (Packet *packet, const GptpSync *gptp)
 
void processFollowUp (Packet *packet, const GptpFollowUp *gptp)
 
void processPdelayReq (Packet *packet, const GptpPdelayReq *gptp)
 
void processPdelayResp (Packet *packet, const GptpPdelayResp *gptp)
 
void processPdelayRespFollowUp (Packet *packet, const GptpPdelayRespFollowUp *gptp)
 
clocktime_t getCalculatedDrift (IClock *clock, clocktime_t value)
 
void synchronize ()
 
virtual void receiveSignal (cComponent *source, simsignal_t signal, cObject *obj, cObject *details) override
 
- Public Member Functions inherited from inet::ClockUserModuleMixin< cSimpleModule >
virtual void scheduleClockEventAt (clocktime_t time, ClockEvent *msg)
 
virtual void scheduleClockEventAfter (clocktime_t delay, ClockEvent *msg)
 
virtual cMessage * cancelClockEvent (ClockEvent *msg)
 
virtual void cancelAndDeleteClockEvent (ClockEvent *msg)
 
virtual void rescheduleClockEventAt (clocktime_t time, ClockEvent *msg)
 
virtual void rescheduleClockEventAfter (clocktime_t time, ClockEvent *msg)
 
virtual clocktime_t computeClockTimeFromSimTime (simtime_t time) const
 
virtual simtime_t computeSimTimeFromClockTime (clocktime_t time) const
 
virtual clocktime_t getClockTime () const
 
virtual clocktime_t getArrivalClockTime (ClockEvent *msg) const
 

Static Public Attributes

static const MacAddress GPTP_MULTICAST_ADDRESS
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleSelfMessage (cMessage *msg)
 

Private Attributes

ModuleRefByPar< IInterfaceTableinterfaceTable
 
GptpNodeType gptpNodeType
 
int domainNumber = -1
 
int slavePortId = -1
 
std::set< int > masterPortIds
 
clocktime_t correctionField
 
int64_t clockIdentity = 0
 
double gmRateRatio = 1.0
 
double receivedRateRatio = 1.0
 
clocktime_t originTimestamp
 
clocktime_t receivedTimeSync
 
clocktime_t syncInterval
 
clocktime_t pdelayInterval
 
uint16_t sequenceId = 0
 
uint16_t lastSentPdelayReqSequenceId = 0
 
clocktime_t peerDelay
 
clocktime_t peerRequestReceiptTimestamp
 
clocktime_t peerResponseOriginTimestamp
 
clocktime_t pdelayRespEventIngressTimestamp
 
clocktime_t pdelayReqEventEgressTimestamp
 
clocktime_t pDelayReqProcessingTime
 
bool rcvdPdelayResp = false
 
clocktime_t sentTimeSyncSync
 
clocktime_t newLocalTimeAtTimeSync
 
clocktime_t oldLocalTimeAtTimeSync
 
clocktime_t peerSentTimeSync
 
clocktime_t syncIngressTimestamp
 
bool rcvdGptpSync = false
 
uint16_t lastReceivedGptpSyncSequenceId = 0xffff
 
ClockEventselfMsgSync = nullptr
 
ClockEventselfMsgDelayReq = nullptr
 
ClockEventrequestMsg = nullptr
 

Static Private Attributes

static simsignal_t localTimeSignal = cComponent::registerSignal("localTime")
 
static simsignal_t timeDifferenceSignal = cComponent::registerSignal("timeDifference")
 
static simsignal_t rateRatioSignal = cComponent::registerSignal("rateRatio")
 
static simsignal_t peerDelaySignal = cComponent::registerSignal("peerDelay")
 

Constructor & Destructor Documentation

◆ ~Gptp()

Member Function Documentation

◆ getCalculatedDrift()

clocktime_t inet::Gptp::getCalculatedDrift ( IClock clock,
clocktime_t  value 
)
inline
104 { return CLOCKTIME_ZERO; }

◆ handleMessage()

void inet::Gptp::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
165 {
166  if (msg->isSelfMessage()) {
167  handleSelfMessage(msg);
168  }
169  else {
170  Packet *packet = check_and_cast<Packet *>(msg);
171  auto gptp = packet->peekAtFront<GptpBase>();
172  auto gptpMessageType = gptp->getMessageType();
173  auto incomingNicId = packet->getTag<InterfaceInd>()->getInterfaceId();
174 
175  if (incomingNicId == slavePortId) {
176  // slave port
177  switch (gptpMessageType) {
178  case GPTPTYPE_SYNC:
179  processSync(packet, check_and_cast<const GptpSync *>(gptp.get()));
180  break;
181  case GPTPTYPE_FOLLOW_UP:
182  processFollowUp(packet, check_and_cast<const GptpFollowUp *>(gptp.get()));
183  // Send a request to send Sync message
184  // through other gptp Ethernet interfaces
186  sendSync();
187  break;
189  processPdelayResp(packet, check_and_cast<const GptpPdelayResp *>(gptp.get()));
190  break;
192  processPdelayRespFollowUp(packet, check_and_cast<const GptpPdelayRespFollowUp *>(gptp.get()));
193  break;
194  default:
195  throw cRuntimeError("Unknown gPTP packet type: %d", (int)(gptpMessageType));
196  }
197  }
198  else if (masterPortIds.find(incomingNicId) != masterPortIds.end()) {
199  // master port
200  if(gptpMessageType == GPTPTYPE_PDELAY_REQ) {
201  processPdelayReq(packet, check_and_cast<const GptpPdelayReq *>(gptp.get()));
202  }
203  else {
204  throw cRuntimeError("Unaccepted gPTP type: %d", (int)(gptpMessageType));
205  }
206  }
207  else {
208  // passive port
209  EV_ERROR << "Message " << msg->getClassAndFullName() << " arrived on passive port " << incomingNicId << ", dropped\n";
210  }
211  delete msg;
212  }
213 }

◆ handleSelfMessage()

void inet::Gptp::handleSelfMessage ( cMessage *  msg)
protectedvirtual
135 {
136  switch(msg->getKind()) {
137  case GPTP_SELF_MSG_SYNC:
138  // masterport:
139  ASSERT(selfMsgSync == msg);
140  sendSync();
141 
142  /* Schedule next Sync message at next sync interval
143  * Grand master always works at simulation time */
145  break;
146 
148  // masterport:
149  sendPdelayResp(check_and_cast<GptpReqAnswerEvent*>(msg));
150  delete msg;
151  break;
152 
154  // slaveport:
155  sendPdelayReq(); //TODO on slaveports only
157  break;
158 
159  default:
160  throw cRuntimeError("Unknown self message (%s)%s, kind=%d", msg->getClassName(), msg->getName(), msg->getKind());
161  }
162 }

Referenced by handleMessage().

◆ initialize()

void inet::Gptp::initialize ( int  stage)
overrideprotectedvirtual
49 {
50  ClockUserModuleBase::initialize(stage);
51 
52  if (stage == INITSTAGE_LOCAL) {
53  gptpNodeType = static_cast<GptpNodeType>(cEnum::get("GptpNodeType", "inet")->resolve(par("gptpNodeType")));
54  domainNumber = par("domainNumber");
55  syncInterval = par("syncInterval");
56  pDelayReqProcessingTime = par("pDelayReqProcessingTime");
57  std::hash<std::string> strHash;
58  clockIdentity = strHash(getFullPath());
59  }
60  if (stage == INITSTAGE_LINK_LAYER) {
61  peerDelay = 0;
63 
64  interfaceTable.reference(this, "interfaceTableModule", true);
65 
66  const char *str = par("slavePort");
67  if (*str) {
69  throw cRuntimeError("Parameter inconsistency: MASTER_NODE with slave port");
70  auto nic = CHK(interfaceTable->findInterfaceByName(str));
71  slavePortId = nic->getInterfaceId();
72  nic->subscribe(transmissionEndedSignal, this);
73  nic->subscribe(receptionEndedSignal, this);
74  }
75  else
77  throw cRuntimeError("Parameter error: Missing slave port for %s", par("gptpNodeType").stringValue());
78 
79  auto v = check_and_cast<cValueArray *>(par("masterPorts").objectValue())->asStringVector();
80  if (v.empty() and gptpNodeType != SLAVE_NODE)
81  throw cRuntimeError("Parameter error: Missing any master port for %s", par("gptpNodeType").stringValue());
82  for (const auto& p : v) {
83  auto nic = CHK(interfaceTable->findInterfaceByName(p.c_str()));
84  int portId = nic->getInterfaceId();
85  if (portId == slavePortId)
86  throw cRuntimeError("Parameter error: the port '%s' specified both master and slave port", p.c_str());
87  masterPortIds.insert(portId);
88  nic->subscribe(transmissionEndedSignal, this);
89  nic->subscribe(receptionEndedSignal, this);
90  }
91 
92  if (slavePortId != -1) {
93  auto networkInterface = interfaceTable->getInterfaceById(slavePortId);
94  if (!networkInterface->matchesMulticastMacAddress(GPTP_MULTICAST_ADDRESS))
95  networkInterface->addMulticastMacAddress(GPTP_MULTICAST_ADDRESS);
96  }
97  for (auto id: masterPortIds) {
98  auto networkInterface = interfaceTable->getInterfaceById(id);
99  if (!networkInterface->matchesMulticastMacAddress(GPTP_MULTICAST_ADDRESS))
100  networkInterface->addMulticastMacAddress(GPTP_MULTICAST_ADDRESS);
101  }
102 
103  correctionField = par("correctionField");
104 
105  gmRateRatio = 1.0;
106 
107  registerProtocol(Protocol::gptp, gate("socketOut"), gate("socketIn"));
108 
109  /* Only grandmaster in the domain can initialize the synchronization message periodically
110  * so below condition checks whether it is grandmaster and then schedule first sync message */
112  {
113  // Schedule Sync message to be sent
114  selfMsgSync = new ClockEvent("selfMsgSync", GPTP_SELF_MSG_SYNC);
115 
116  clocktime_t scheduleSync = par("syncInitialOffset");
117  originTimestamp = clock->getClockTime() + scheduleSync;
118  scheduleClockEventAfter(scheduleSync, selfMsgSync);
119  }
120  if(slavePortId != -1)
121  {
122  requestMsg = new ClockEvent("requestToSendSync", GPTP_REQUEST_TO_SEND_SYNC);
123 
124  // Schedule Pdelay_Req message is sent by slave port
125  // without depending on node type which is grandmaster or bridge
126  selfMsgDelayReq = new ClockEvent("selfMsgPdelay", GPTP_SELF_MSG_PDELAY_REQ);
127  pdelayInterval = par("pdelayInterval");
128  scheduleClockEventAfter(par("pdelayInitialOffset"), selfMsgDelayReq);
129  }
130  WATCH(peerDelay);
131  }
132 }

◆ numInitStages()

virtual int inet::Gptp::numInitStages ( ) const
inlineoverrideprotectedvirtual
82 { return NUM_INIT_STAGES; }

◆ processFollowUp()

void inet::Gptp::processFollowUp ( Packet packet,
const GptpFollowUp gptp 
)
340 {
341  // check: is received the GptpSync for this GptpFollowUp?
342  if (!rcvdGptpSync) {
343  EV_WARN << "GptpFollowUp arrived without GptpSync, dropped";
344  return;
345  }
346  // verify IDs
347  if (gptp->getSequenceId() != lastReceivedGptpSyncSequenceId) {
348  EV_WARN << "GptpFollowUp arrived with invalid sequence ID, dropped";
349  return;
350  }
351 
352 
353  peerSentTimeSync = gptp->getPreciseOriginTimestamp();
354  correctionField = gptp->getCorrectionField();
355  receivedRateRatio = gptp->getFollowUpInformationTLV().getRateRatio();
356 
357  synchronize();
358 
359  EV_INFO << "############## FOLLOW_UP ################################"<< endl;
360  EV_INFO << "RECEIVED TIME AFTER SYNC - " << newLocalTimeAtTimeSync << endl;
361  EV_INFO << "ORIGIN TIME SYNC - " << originTimestamp << endl;
362  EV_INFO << "CORRECTION FIELD - " << correctionField << endl;
363  EV_INFO << "PROPAGATION DELAY - " << peerDelay << endl;
364 
365  rcvdGptpSync = false;
366 }

Referenced by handleMessage().

◆ processPdelayReq()

void inet::Gptp::processPdelayReq ( Packet packet,
const GptpPdelayReq gptp 
)
416 {
417  auto resp = new GptpReqAnswerEvent("selfMsgPdelayResp", GPTP_SELF_REQ_ANSWER_KIND);
418  resp->setPortId(packet->getTag<InterfaceInd>()->getInterfaceId());
419  resp->setIngressTimestamp(packet->getTag<GptpIngressTimeInd>()->getArrivalClockTime());
420  resp->setSourcePortIdentity(gptp->getSourcePortIdentity());
421  resp->setSequenceId(gptp->getSequenceId());
422 
424 }

Referenced by handleMessage().

◆ processPdelayResp()

void inet::Gptp::processPdelayResp ( Packet packet,
const GptpPdelayResp gptp 
)
427 {
428  // verify IDs
429  if (gptp->getRequestingPortIdentity().clockIdentity != clockIdentity || gptp->getRequestingPortIdentity().portNumber != slavePortId) {
430  EV_WARN << "GptpPdelayResp arrived with invalid PortIdentity, dropped";
431  return;
432  }
433  if (gptp->getSequenceId() != lastSentPdelayReqSequenceId) {
434  EV_WARN << "GptpPdelayResp arrived with invalid sequence ID, dropped";
435  return;
436  }
437 
438  rcvdPdelayResp = true;
439  pdelayRespEventIngressTimestamp = packet->getTag<GptpIngressTimeInd>()->getArrivalClockTime();
440  peerRequestReceiptTimestamp = gptp->getRequestReceiptTimestamp();
442 }

Referenced by handleMessage().

◆ processPdelayRespFollowUp()

void inet::Gptp::processPdelayRespFollowUp ( Packet packet,
const GptpPdelayRespFollowUp gptp 
)
445 {
446  if (!rcvdPdelayResp) {
447  EV_WARN << "GptpPdelayRespFollowUp arrived without GptpPdelayResp, dropped";
448  return;
449  }
450  // verify IDs
451  if (gptp->getRequestingPortIdentity().clockIdentity != clockIdentity || gptp->getRequestingPortIdentity().portNumber != slavePortId) {
452  EV_WARN << "GptpPdelayRespFollowUp arrived with invalid PortIdentity, dropped";
453  return;
454  }
455  if (gptp->getSequenceId() != lastSentPdelayReqSequenceId) {
456  EV_WARN << "GptpPdelayRespFollowUp arrived with invalid sequence ID, dropped";
457  return;
458  }
459 
460  peerResponseOriginTimestamp = gptp->getResponseOriginTimestamp();
461 
462  // computePropTime():
464 
465  EV_INFO << "RATE RATIO - " << gmRateRatio << endl;
466  EV_INFO << "pdelayReqEventEgressTimestamp - " << pdelayReqEventEgressTimestamp << endl;
467  EV_INFO << "peerResponseOriginTimestamp - " << peerResponseOriginTimestamp << endl;
468  EV_INFO << "pdelayRespEventIngressTimestamp - " << pdelayRespEventIngressTimestamp << endl;
469  EV_INFO << "peerRequestReceiptTimestamp - " << peerRequestReceiptTimestamp << endl;
470  EV_INFO << "PEER DELAY - " << peerDelay << endl;
471 
473 }

Referenced by handleMessage().

◆ processSync()

void inet::Gptp::processSync ( Packet packet,
const GptpSync gptp 
)
331 {
332  rcvdGptpSync = true;
333  lastReceivedGptpSyncSequenceId = gptp->getSequenceId();
334 
335  // peerSentTimeSync = gptp->getOriginTimestamp(); // TODO this is unfilled in two-step mode
336  syncIngressTimestamp = packet->getTag<GptpIngressTimeInd>()->getArrivalClockTime();
337 }

Referenced by handleMessage().

◆ receiveSignal()

void inet::Gptp::receiveSignal ( cComponent *  source,
simsignal_t  signal,
cObject *  obj,
cObject *  details 
)
overridevirtual
476 {
477  Enter_Method("%s", cComponent::getSignalName(signal));
478 
479  if (signal == receptionEndedSignal) {
480  auto signal = check_and_cast<cPacket *>(obj);
481  auto packet = check_and_cast_nullable<Packet *>(signal->getEncapsulatedPacket());
482  if (packet) {
483  auto protocol = packet->getTag<PacketProtocolTag>()->getProtocol();
485  const auto& ethPhyHeader = packet->peekAtFront<physicallayer::EthernetPhyHeader>();
486  const auto& ethMacHeader = packet->peekAt<EthernetMacHeader>(ethPhyHeader->getChunkLength());
487  if (ethMacHeader->getTypeOrLength() == ETHERTYPE_GPTP) {
488  const auto& gptp = packet->peekAt<GptpBase>(ethPhyHeader->getChunkLength() + ethMacHeader->getChunkLength());
489  if (gptp->getDomainNumber() == domainNumber)
490  packet->addTagIfAbsent<GptpIngressTimeInd>()->setArrivalClockTime(clock->getClockTime());
491  }
492  }
493  }
494  }
495  else if (signal == transmissionEndedSignal) {
496  auto signal = check_and_cast<cPacket *>(obj);
497  auto packet = check_and_cast_nullable<Packet *>(signal->getEncapsulatedPacket());
498  if (packet) {
499  auto protocol = packet->getTag<PacketProtocolTag>()->getProtocol();
501  const auto& ethPhyHeader = packet->peekAtFront<physicallayer::EthernetPhyHeader>();
502  const auto& ethMacHeader = packet->peekAt<EthernetMacHeader>(ethPhyHeader->getChunkLength());
503  if (ethMacHeader->getTypeOrLength() == ETHERTYPE_GPTP) {
504  const auto& gptp = packet->peekAt<GptpBase>(ethPhyHeader->getChunkLength() + ethMacHeader->getChunkLength());
505  if (gptp->getDomainNumber() == domainNumber) {
506  int portId = getContainingNicModule(check_and_cast<cModule*>(source))->getInterfaceId();
507  switch (gptp->getMessageType()) {
508  case GPTPTYPE_PDELAY_RESP: {
509  auto gptpResp = dynamicPtrCast<const GptpPdelayResp>(gptp);
510  sendPdelayRespFollowUp(portId, gptpResp.get());
511  break;
512  }
513  case GPTPTYPE_SYNC: {
514  auto gptpSync = dynamicPtrCast<const GptpSync>(gptp);
515  sendFollowUp(portId, gptpSync.get(), clock->getClockTime());
516  break;
517  }
518  case GPTPTYPE_PDELAY_REQ:
519  if (portId == slavePortId)
520  pdelayReqEventEgressTimestamp = clock->getClockTime();
521  break;
522  default:
523  break;
524  }
525  }
526  }
527  }
528  }
529  }
530 }

◆ sendFollowUp()

void inet::Gptp::sendFollowUp ( int  portId,
const GptpSync sync,
clocktime_t  preciseOriginTimestamp 
)
254 {
255  auto packet = new Packet("GptpFollowUp");
256  packet->addTag<MacAddressReq>()->setDestAddress(GPTP_MULTICAST_ADDRESS);
257  auto gptp = makeShared<GptpFollowUp>();
258  gptp->setDomainNumber(domainNumber);
259  gptp->setPreciseOriginTimestamp(preciseOriginTimestamp);
260  gptp->setSequenceId(sync->getSequenceId());
261 
262  if (gptpNodeType == MASTER_NODE)
263  gptp->setCorrectionField(CLOCKTIME_ZERO);
264  else if (gptpNodeType == BRIDGE_NODE)
265  {
266  /**************** Correction field calculation *********************************************
267  * It is calculated by adding peer delay, residence time and packet transmission time *
268  * correctionField(i)=correctionField(i-1)+peerDelay+(timeReceivedSync-timeSentSync)*(1-f) *
269  *******************************************************************************************/
270  // gptp->setCorrectionField(correctionField + peerDelay + sentTimeSyncSync - receivedTimeSync); // TODO revise it!!! see prev. comment, where is the (1-f), ???
271  gptp->setCorrectionField(CLOCKTIME_ZERO); // TODO revise it!!! see prev. comment, where is the (1-f), ???
272  }
273  gptp->getFollowUpInformationTLVForUpdate().setRateRatio(gmRateRatio);
274  packet->insertAtFront(gptp);
275  sendPacketToNIC(packet, portId);
276 }

Referenced by receiveSignal().

◆ sendPacketToNIC()

void inet::Gptp::sendPacketToNIC ( Packet packet,
int  portId 
)
216 {
217  auto networkInterface = interfaceTable->getInterfaceById(portId);
218  EV_INFO << "Sending " << packet << " to output interface = " << networkInterface->getInterfaceName() << ".\n";
219  packet->addTag<InterfaceReq>()->setInterfaceId(portId);
220  packet->addTag<PacketProtocolTag>()->setProtocol(&Protocol::gptp);
221  packet->addTag<DispatchProtocolInd>()->setProtocol(&Protocol::gptp);
222  auto protocol = networkInterface->getProtocol();
223  if (protocol != nullptr)
224  packet->addTagIfAbsent<DispatchProtocolReq>()->setProtocol(protocol);
225  else
226  packet->removeTagIfPresent<DispatchProtocolReq>();
227  send(packet, "socketOut");
228 }

Referenced by sendFollowUp(), sendPdelayReq(), sendPdelayResp(), sendPdelayRespFollowUp(), and sendSync().

◆ sendPdelayReq()

void inet::Gptp::sendPdelayReq ( )
309 {
310  ASSERT(slavePortId != -1);
311  auto packet = new Packet("GptpPdelayReq");
312  packet->addTag<MacAddressReq>()->setDestAddress(GPTP_MULTICAST_ADDRESS);
313  auto gptp = makeShared<GptpPdelayReq>();
314  gptp->setDomainNumber(domainNumber);
315  gptp->setCorrectionField(CLOCKTIME_ZERO);
316  //save and send IDs
317  PortIdentity portId;
318  portId.clockIdentity = clockIdentity;
319  portId.portNumber = slavePortId;
320  gptp->setSourcePortIdentity(portId);
322  gptp->setSequenceId(lastSentPdelayReqSequenceId);
323  packet->insertAtFront(gptp);
324  pdelayReqEventEgressTimestamp = clock->getClockTime();
325  rcvdPdelayResp = false;
326  sendPacketToNIC(packet, slavePortId);
327 }

Referenced by handleSelfMessage().

◆ sendPdelayResp()

void inet::Gptp::sendPdelayResp ( GptpReqAnswerEvent req)
279 {
280  int portId = req->getPortId();
281  auto packet = new Packet("GptpPdelayResp");
282  packet->addTag<MacAddressReq>()->setDestAddress(GPTP_MULTICAST_ADDRESS);
283  auto gptp = makeShared<GptpPdelayResp>();
284  gptp->setDomainNumber(domainNumber);
285  //??? gptp->setSentTime(clock->getClockTime());
286  gptp->setRequestingPortIdentity(req->getSourcePortIdentity());
287  gptp->setSequenceId(req->getSequenceId());
288  gptp->setRequestReceiptTimestamp(req->getIngressTimestamp());
289  packet->insertAtFront(gptp);
290  sendPacketToNIC(packet, portId);
291  // The sendPdelayRespFollowUp(portId) called by receiveSignal(), when GptpPdelayResp sent
292 }

Referenced by handleSelfMessage().

◆ sendPdelayRespFollowUp()

void inet::Gptp::sendPdelayRespFollowUp ( int  portId,
const GptpPdelayResp resp 
)
295 {
296  auto packet = new Packet("GptpPdelayRespFollowUp");
297  packet->addTag<MacAddressReq>()->setDestAddress(GPTP_MULTICAST_ADDRESS);
298  auto gptp = makeShared<GptpPdelayRespFollowUp>();
299  auto now = clock->getClockTime();
300  gptp->setDomainNumber(domainNumber);
301  gptp->setResponseOriginTimestamp(now);
302  gptp->setRequestingPortIdentity(resp->getRequestingPortIdentity());
303  gptp->setSequenceId(resp->getSequenceId());
304  packet->insertAtFront(gptp);
305  sendPacketToNIC(packet, portId);
306 }

Referenced by receiveSignal().

◆ sendSync()

void inet::Gptp::sendSync ( )
231 {
232  auto packet = new Packet("GptpSync");
233  packet->addTag<MacAddressReq>()->setDestAddress(GPTP_MULTICAST_ADDRESS);
234  auto gptp = makeShared<GptpSync>();
235  gptp->setDomainNumber(domainNumber);
236  /* OriginTimestamp always get Sync departure time from grand master */
237  if (gptpNodeType == MASTER_NODE) {
238  originTimestamp = clock->getClockTime();
239  }
240  //gptp->setOriginTimestamp(CLOCKTIME_ZERO);
241  gptp->setSequenceId(sequenceId++);
242 
243  sentTimeSyncSync = clock->getClockTime();
244  packet->insertAtFront(gptp);
245 
246  for (auto port: masterPortIds)
247  sendPacketToNIC(packet->dup(), port);
248  delete packet;
249 
250  // The sendFollowUp(portId) called by receiveSignal(), when GptpSync sent
251 }

Referenced by handleMessage(), and handleSelfMessage().

◆ synchronize()

void inet::Gptp::synchronize ( )
369 {
370  simtime_t now = simTime();
371  clocktime_t origNow = clock->getClockTime();
372  clocktime_t residenceTime = origNow - syncIngressTimestamp;
373 
374  emit(timeDifferenceSignal, CLOCKTIME_AS_SIMTIME(origNow) - now);
375 
376  /************** Time synchronization *****************************************
377  * Local time is adjusted using peer delay, correction field, residence time *
378  * and packet transmission time based departure time of Sync message from GM *
379  *****************************************************************************/
380  clocktime_t newTime = peerSentTimeSync + peerDelay + correctionField + residenceTime;
381 
382  ASSERT(gptpNodeType != MASTER_NODE);
383  check_and_cast<SettableClock *>(clock.get())->setClockTime(newTime);
384 
385  // TODO computeGmRateRatio:
387  gmRateRatio = 1.0 / gmRateRatio;
388 
389  oldLocalTimeAtTimeSync = origNow;
390  newLocalTimeAtTimeSync = clock->getClockTime();
392 
393  // adjust local timestamps, too
395 
396  /************** Rate ratio calculation *************************************
397  * It is calculated based on interval between two successive Sync messages *
398  ***************************************************************************/
399 
400  EV_INFO << "############## SYNC #####################################"<< endl;
401  EV_INFO << "RECEIVED TIME AFTER SYNC - " << newLocalTimeAtTimeSync << endl;
402  EV_INFO << "RECEIVED SIM TIME - " << now << endl;
403  EV_INFO << "ORIGIN TIME SYNC - " << peerSentTimeSync << endl;
404  EV_INFO << "RESIDENCE TIME - " << residenceTime << endl;
405  EV_INFO << "CORRECTION FIELD - " << correctionField << endl;
406  EV_INFO << "PROPAGATION DELAY - " << peerDelay << endl;
407  EV_INFO << "TIME DIFFERENCE TO SIMTIME - " << CLOCKTIME_AS_SIMTIME(newLocalTimeAtTimeSync) - now << endl;
408  EV_INFO << "RATE RATIO - " << gmRateRatio << endl;
409 
413 }

Referenced by processFollowUp().

Member Data Documentation

◆ clockIdentity

int64_t inet::Gptp::clockIdentity = 0
private

◆ correctionField

clocktime_t inet::Gptp::correctionField
private

◆ domainNumber

int inet::Gptp::domainNumber = -1
private

◆ gmRateRatio

double inet::Gptp::gmRateRatio = 1.0
private

◆ GPTP_MULTICAST_ADDRESS

const MacAddress inet::Gptp::GPTP_MULTICAST_ADDRESS
static

◆ gptpNodeType

GptpNodeType inet::Gptp::gptpNodeType
private

◆ interfaceTable

ModuleRefByPar<IInterfaceTable> inet::Gptp::interfaceTable
private

Referenced by initialize(), and sendPacketToNIC().

◆ lastReceivedGptpSyncSequenceId

uint16_t inet::Gptp::lastReceivedGptpSyncSequenceId = 0xffff
private

Referenced by processFollowUp(), and processSync().

◆ lastSentPdelayReqSequenceId

uint16_t inet::Gptp::lastSentPdelayReqSequenceId = 0
private

◆ localTimeSignal

simsignal_t inet::Gptp::localTimeSignal = cComponent::registerSignal("localTime")
staticprivate

Referenced by synchronize().

◆ masterPortIds

std::set<int> inet::Gptp::masterPortIds
private

Referenced by handleMessage(), initialize(), and sendSync().

◆ newLocalTimeAtTimeSync

clocktime_t inet::Gptp::newLocalTimeAtTimeSync
private

Referenced by processFollowUp(), and synchronize().

◆ oldLocalTimeAtTimeSync

clocktime_t inet::Gptp::oldLocalTimeAtTimeSync
private

Referenced by synchronize().

◆ originTimestamp

clocktime_t inet::Gptp::originTimestamp
private

◆ pdelayInterval

clocktime_t inet::Gptp::pdelayInterval
private

Referenced by handleSelfMessage(), and initialize().

◆ pdelayReqEventEgressTimestamp

clocktime_t inet::Gptp::pdelayReqEventEgressTimestamp
private

◆ pDelayReqProcessingTime

clocktime_t inet::Gptp::pDelayReqProcessingTime
private

Referenced by initialize(), and processPdelayReq().

◆ pdelayRespEventIngressTimestamp

clocktime_t inet::Gptp::pdelayRespEventIngressTimestamp
private

◆ peerDelay

clocktime_t inet::Gptp::peerDelay
private

◆ peerDelaySignal

simsignal_t inet::Gptp::peerDelaySignal = cComponent::registerSignal("peerDelay")
staticprivate

◆ peerRequestReceiptTimestamp

clocktime_t inet::Gptp::peerRequestReceiptTimestamp
private

◆ peerResponseOriginTimestamp

clocktime_t inet::Gptp::peerResponseOriginTimestamp
private

◆ peerSentTimeSync

clocktime_t inet::Gptp::peerSentTimeSync
private

Referenced by processFollowUp(), and synchronize().

◆ rateRatioSignal

simsignal_t inet::Gptp::rateRatioSignal = cComponent::registerSignal("rateRatio")
staticprivate

Referenced by synchronize().

◆ rcvdGptpSync

bool inet::Gptp::rcvdGptpSync = false
private

Referenced by processFollowUp(), and processSync().

◆ rcvdPdelayResp

bool inet::Gptp::rcvdPdelayResp = false
private

◆ receivedRateRatio

double inet::Gptp::receivedRateRatio = 1.0
private

Referenced by processFollowUp().

◆ receivedTimeSync

clocktime_t inet::Gptp::receivedTimeSync
private

Referenced by initialize(), and synchronize().

◆ requestMsg

ClockEvent* inet::Gptp::requestMsg = nullptr
private

Referenced by initialize(), and ~Gptp().

◆ selfMsgDelayReq

ClockEvent* inet::Gptp::selfMsgDelayReq = nullptr
private

◆ selfMsgSync

ClockEvent* inet::Gptp::selfMsgSync = nullptr
private

◆ sentTimeSyncSync

clocktime_t inet::Gptp::sentTimeSyncSync
private

Referenced by sendSync().

◆ sequenceId

uint16_t inet::Gptp::sequenceId = 0
private

Referenced by sendPdelayReq(), and sendSync().

◆ slavePortId

int inet::Gptp::slavePortId = -1
private

◆ syncIngressTimestamp

clocktime_t inet::Gptp::syncIngressTimestamp
private

Referenced by processSync(), and synchronize().

◆ syncInterval

clocktime_t inet::Gptp::syncInterval
private

Referenced by handleSelfMessage(), and initialize().

◆ timeDifferenceSignal

simsignal_t inet::Gptp::timeDifferenceSignal = cComponent::registerSignal("timeDifference")
staticprivate

Referenced by synchronize().


The documentation for this class was generated from the following files:
inet::Gptp::syncIngressTimestamp
clocktime_t syncIngressTimestamp
Definition: Gptp.h:63
CHK
#define CHK(x)
Definition: INETDefs.h:87
inet::GPTPTYPE_PDELAY_RESP_FOLLOW_UP
@ GPTPTYPE_PDELAY_RESP_FOLLOW_UP
Definition: GptpPacket_m.h:137
inet::Gptp::rcvdGptpSync
bool rcvdGptpSync
Definition: Gptp.h:65
inet::Gptp::interfaceTable
ModuleRefByPar< IInterfaceTable > interfaceTable
Definition: Gptp.h:26
inet::clocktime_t
ClockTime clocktime_t
Definition: contract/ClockTime.h:25
inet::Gptp::gmRateRatio
double gmRateRatio
Definition: Gptp.h:35
inet::Gptp::oldLocalTimeAtTimeSync
clocktime_t oldLocalTimeAtTimeSync
Definition: Gptp.h:61
protocol
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down protocol
Definition: IUdp-gates.txt:25
inet::GPTP_SELF_MSG_PDELAY_REQ
@ GPTP_SELF_MSG_PDELAY_REQ
Definition: GptpPacket_m.h:159
inet::MASTER_NODE
@ MASTER_NODE
Definition: GptpPacket_m.h:91
inet::Gptp::timeDifferenceSignal
static simsignal_t timeDifferenceSignal
Definition: Gptp.h:75
inet::Gptp::masterPortIds
std::set< int > masterPortIds
Definition: Gptp.h:31
CLOCKTIME_AS_SIMTIME
#define CLOCKTIME_AS_SIMTIME(x)
Definition: contract/ClockTime.h:19
inet::Gptp::receivedTimeSync
clocktime_t receivedTimeSync
Definition: Gptp.h:40
inet::GPTPTYPE_PDELAY_REQ
@ GPTPTYPE_PDELAY_REQ
Definition: GptpPacket_m.h:135
inet::ClockUserModuleMixin< cSimpleModule >::getArrivalClockTime
virtual clocktime_t getArrivalClockTime(ClockEvent *msg) const
Definition: ClockUserModuleMixin.h:73
inet::Gptp::localTimeSignal
static simsignal_t localTimeSignal
Definition: Gptp.h:74
inet::ClockUserModuleMixin< cSimpleModule >::scheduleClockEventAfter
virtual void scheduleClockEventAfter(clocktime_t delay, ClockEvent *msg)
Definition: ClockUserModuleMixin.h:65
inet::Gptp::sentTimeSyncSync
clocktime_t sentTimeSyncSync
Definition: Gptp.h:56
InterfaceReq
removed InterfaceReq
Definition: IUdp-gates.txt:11
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
inet::getContainingNicModule
NetworkInterface * getContainingNicModule(const cModule *from)
Find the nic module (inside the networkNode) containing the given module.
Definition: NetworkInterface.cc:691
inet::Gptp::pDelayReqProcessingTime
clocktime_t pDelayReqProcessingTime
Definition: Gptp.h:53
inet::GPTPTYPE_FOLLOW_UP
@ GPTPTYPE_FOLLOW_UP
Definition: GptpPacket_m.h:134
inet::Gptp::sendPdelayReq
void sendPdelayReq()
Definition: Gptp.cc:308
inet::Gptp::slavePortId
int slavePortId
Definition: Gptp.h:30
inet::Gptp::selfMsgDelayReq
ClockEvent * selfMsgDelayReq
Definition: Gptp.h:70
inet::Gptp::domainNumber
int domainNumber
Definition: Gptp.h:29
CLOCKTIME_ZERO
#define CLOCKTIME_ZERO
Constant for zero simulation time.
Definition: contract/ClockTime.h:36
inet::GPTP_SELF_REQ_ANSWER_KIND
@ GPTP_SELF_REQ_ANSWER_KIND
Definition: GptpPacket_m.h:156
inet::Gptp::newLocalTimeAtTimeSync
clocktime_t newLocalTimeAtTimeSync
Definition: Gptp.h:60
inet::Gptp::processPdelayResp
void processPdelayResp(Packet *packet, const GptpPdelayResp *gptp)
Definition: Gptp.cc:426
inet::Gptp::correctionField
clocktime_t correctionField
Definition: Gptp.h:32
PacketProtocolTag
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down PacketProtocolTag
Definition: IUdp-gates.txt:25
inet::Gptp::GPTP_MULTICAST_ADDRESS
static const MacAddress GPTP_MULTICAST_ADDRESS
Definition: Gptp.h:79
inet::Protocol::gptp
static const Protocol gptp
Definition: Protocol.h:69
inet::Gptp::sendFollowUp
void sendFollowUp(int portId, const GptpSync *sync, clocktime_t preciseOriginTimestamp)
Definition: Gptp.cc:253
inet::ClockEvent
cMessage ClockEvent
Definition: contract/ClockEvent.h:18
inet::Gptp::originTimestamp
clocktime_t originTimestamp
Definition: Gptp.h:38
inet::Gptp::processPdelayRespFollowUp
void processPdelayRespFollowUp(Packet *packet, const GptpPdelayRespFollowUp *gptp)
Definition: Gptp.cc:444
inet::Gptp::peerResponseOriginTimestamp
clocktime_t peerResponseOriginTimestamp
Definition: Gptp.h:50
inet::transmissionEndedSignal
simsignal_t transmissionEndedSignal
Definition: Simsignals.cc:117
inet::Gptp::gptpNodeType
GptpNodeType gptpNodeType
Definition: Gptp.h:28
inet::Gptp::rateRatioSignal
static simsignal_t rateRatioSignal
Definition: Gptp.h:76
inet::GptpNodeType
GptpNodeType
Enum generated from inet/linklayer/ieee8021as/GptpPacket.msg:45 by opp_msgtool.
Definition: GptpPacket_m.h:90
inet::Gptp::rcvdPdelayResp
bool rcvdPdelayResp
Definition: Gptp.h:54
inet::NetworkInterface::getInterfaceId
int getInterfaceId() const
Definition: NetworkInterface.h:232
inet::receptionEndedSignal
simsignal_t receptionEndedSignal
Definition: Simsignals.cc:119
inet::Gptp::sendSync
void sendSync()
Definition: Gptp.cc:230
inet::Gptp::peerSentTimeSync
clocktime_t peerSentTimeSync
Definition: Gptp.h:62
inet::Gptp::receivedRateRatio
double receivedRateRatio
Definition: Gptp.h:36
inet::Gptp::sendPdelayResp
void sendPdelayResp(GptpReqAnswerEvent *req)
Definition: Gptp.cc:278
inet::ETHERTYPE_GPTP
@ ETHERTYPE_GPTP
Definition: EtherType_m.h:98
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::ClockUserModuleMixin< cSimpleModule >::cancelAndDeleteClockEvent
virtual void cancelAndDeleteClockEvent(ClockEvent *msg)
Definition: ClockUserModuleMixin.h:67
inet::Gptp::synchronize
void synchronize()
Definition: Gptp.cc:368
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::Gptp::peerDelaySignal
static simsignal_t peerDelaySignal
Definition: Gptp.h:77
inet::Gptp::processPdelayReq
void processPdelayReq(Packet *packet, const GptpPdelayReq *gptp)
Definition: Gptp.cc:415
inet::GPTP_SELF_MSG_SYNC
@ GPTP_SELF_MSG_SYNC
Definition: GptpPacket_m.h:157
inet::Gptp::pdelayInterval
clocktime_t pdelayInterval
Definition: Gptp.h:43
inet::Protocol::ethernetPhy
static const Protocol ethernetPhy
Definition: Protocol.h:66
inet::Gptp::peerRequestReceiptTimestamp
clocktime_t peerRequestReceiptTimestamp
Definition: Gptp.h:49
inet::GPTPTYPE_SYNC
@ GPTPTYPE_SYNC
Definition: GptpPacket_m.h:133
inet::Gptp::sequenceId
uint16_t sequenceId
Definition: Gptp.h:45
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
inet::Gptp::peerDelay
clocktime_t peerDelay
Definition: Gptp.h:48
inet::Gptp::sendPacketToNIC
void sendPacketToNIC(Packet *packet, int portId)
Definition: Gptp.cc:215
inet::Gptp::pdelayRespEventIngressTimestamp
clocktime_t pdelayRespEventIngressTimestamp
Definition: Gptp.h:51
inet::Gptp::clockIdentity
int64_t clockIdentity
Definition: Gptp.h:33
inet::Gptp::requestMsg
ClockEvent * requestMsg
Definition: Gptp.h:71
inet::Gptp::selfMsgSync
ClockEvent * selfMsgSync
Definition: Gptp.h:69
inet::Gptp::lastReceivedGptpSyncSequenceId
uint16_t lastReceivedGptpSyncSequenceId
Definition: Gptp.h:66
inet::Gptp::lastSentPdelayReqSequenceId
uint16_t lastSentPdelayReqSequenceId
Definition: Gptp.h:47
inet::Gptp::processSync
void processSync(Packet *packet, const GptpSync *gptp)
Definition: Gptp.cc:330
inet::Gptp::pdelayReqEventEgressTimestamp
clocktime_t pdelayReqEventEgressTimestamp
Definition: Gptp.h:52
inet::registerProtocol
void registerProtocol(const Protocol &protocol, cGate *gate, ServicePrimitive servicePrimitive)
Registers a protocol primitive (PDU processing) at the given gate.
Definition: IProtocolRegistrationListener.cc:83
inet::Gptp::processFollowUp
void processFollowUp(Packet *packet, const GptpFollowUp *gptp)
Definition: Gptp.cc:339
inet::GPTPTYPE_PDELAY_RESP
@ GPTPTYPE_PDELAY_RESP
Definition: GptpPacket_m.h:136
inet::GPTP_REQUEST_TO_SEND_SYNC
@ GPTP_REQUEST_TO_SEND_SYNC
Definition: GptpPacket_m.h:158
inet::BRIDGE_NODE
@ BRIDGE_NODE
Definition: GptpPacket_m.h:92
inet::SLAVE_NODE
@ SLAVE_NODE
Definition: GptpPacket_m.h:93
inet::Gptp::syncInterval
clocktime_t syncInterval
Definition: Gptp.h:42
inet::INITSTAGE_LINK_LAYER
INET_API InitStage INITSTAGE_LINK_LAYER
Initialization of link-layer protocols.
inet::Gptp::sendPdelayRespFollowUp
void sendPdelayRespFollowUp(int portId, const GptpPdelayResp *resp)
Definition: Gptp.cc:294
inet::Gptp::handleSelfMessage
virtual void handleSelfMessage(cMessage *msg)
Definition: Gptp.cc:134