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

Performs Ethernet II encapsulation/decapsulation. More...

#include <EtherEncap.h>

Inheritance diagram for inet::EtherEncap:

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void processPacketFromHigherLayer (cPacket *msg)
 
virtual void processFrameFromMAC (EtherFrame *msg)
 
virtual void handleSendPause (cMessage *msg)
 
virtual void refreshDisplay () const override
 

Protected Attributes

int seqNum
 
long totalFromHigherLayer
 
long totalFromMAC
 
long totalPauseSent
 
int interfaceId = -1
 
bool useSNAP
 

Static Protected Attributes

static simsignal_t encapPkSignal = registerSignal("encapPk")
 
static simsignal_t decapPkSignal = registerSignal("decapPk")
 
static simsignal_t pauseSentSignal = registerSignal("pauseSent")
 

Detailed Description

Performs Ethernet II encapsulation/decapsulation.

More info in the NED file.

Member Function Documentation

void inet::EtherEncap::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
55 {
56  if (msg->arrivedOn("lowerLayerIn")) {
57  EV_INFO << "Received " << msg << " from lower layer." << endl;
58  processFrameFromMAC(check_and_cast<EtherFrame *>(msg));
59  }
60  else {
61  EV_INFO << "Received " << msg << " from upper layer." << endl;
62  // from higher layer
63  switch (msg->getKind()) {
64  case IEEE802CTRL_DATA:
65  case 0: // default message kind (0) is also accepted
67  break;
68 
70  // higher layer want MAC to send PAUSE frame
71  handleSendPause(msg);
72  break;
73 
74  default:
75  throw cRuntimeError("Received message `%s' with unknown message kind %d", msg->getName(), msg->getKind());
76  }
77  }
78 }
Definition: Ieee802Ctrl_m.h:54
Definition: Ieee802Ctrl_m.h:57
virtual void processPacketFromHigherLayer(cPacket *msg)
Definition: EtherEncap.cc:87
virtual void processFrameFromMAC(EtherFrame *msg)
Definition: EtherEncap.cc:136
#define PK(msg)
Definition: INETDefs.h:92
virtual void handleSendPause(cMessage *msg)
Definition: EtherEncap.cc:164
void inet::EtherEncap::handleSendPause ( cMessage *  msg)
protectedvirtual

Referenced by handleMessage().

165 {
166  Ieee802Ctrl *etherctrl = dynamic_cast<Ieee802Ctrl *>(msg->removeControlInfo());
167  if (!etherctrl)
168  throw cRuntimeError("PAUSE command `%s' from higher layer received without Ieee802Ctrl", msg->getName());
169  int pauseUnits = etherctrl->getPauseUnits();
170  MACAddress dest = etherctrl->getDest();
171  delete etherctrl;
172 
173  EV_DETAIL << "Creating and sending PAUSE frame, with duration = " << pauseUnits << " units\n";
174 
175  // create Ethernet frame
176  char framename[40];
177  sprintf(framename, "pause-%d-%d", getId(), seqNum++);
178  EtherPauseFrame *frame = new EtherPauseFrame(framename);
179  frame->setPauseTime(pauseUnits);
180  if (dest.isUnspecified())
182  frame->setDest(dest);
183  frame->setByteLength(ETHER_PAUSE_COMMAND_PADDED_BYTES);
184 
185  EV_INFO << "Sending " << frame << " to lower layer.\n";
186  send(frame, "lowerLayerOut");
187  delete msg;
188 
189  emit(pauseSentSignal, pauseUnits);
190  totalPauseSent++;
191 }
long totalPauseSent
Definition: EtherEncap.h:41
int seqNum
Definition: EtherEncap.h:36
static const MACAddress MULTICAST_PAUSE_ADDRESS
The special multicast PAUSE MAC address, 01:80:C2:00:00:01.
Definition: MACAddress.h:63
static simsignal_t pauseSentSignal
Definition: EtherEncap.h:45
#define ETHER_PAUSE_COMMAND_PADDED_BYTES
Definition: Ethernet.h:53
void inet::EtherEncap::initialize ( int  stage)
overrideprotectedvirtual
37 {
38  if (stage == INITSTAGE_LOCAL) {
39  seqNum = 0;
40  WATCH(seqNum);
42  useSNAP = par("useSNAP").boolValue();
43  WATCH(totalFromHigherLayer);
44  WATCH(totalFromMAC);
45  WATCH(totalPauseSent);
46  }
47  else if (stage == INITSTAGE_LINK_LAYER_2) {
48  IInterfaceTable *ift = findModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
49  InterfaceEntry *myIface = ift != nullptr ? ift->getInterfaceByName(utils::stripnonalnum(findModuleUnderContainingNode(this)->getFullName()).c_str()) : nullptr;
50  interfaceId = (myIface != nullptr) ? myIface->getInterfaceId() : -1;
51  }
52 }
long totalFromHigherLayer
Definition: EtherEncap.h:39
long totalFromMAC
Definition: EtherEncap.h:40
long totalPauseSent
Definition: EtherEncap.h:41
int seqNum
Definition: EtherEncap.h:36
bool useSNAP
Definition: EtherEncap.h:46
cModule * findModuleUnderContainingNode(const cModule *from)
Find the ancestor module under the node containing the given module.
Definition: ModuleAccess.cc:73
std::string stripnonalnum(const char *s)
Removes non-alphanumeric characters from the given string.
Definition: INETUtils.cc:56
Local initializations.
Definition: InitStages.h:35
int interfaceId
Definition: EtherEncap.h:42
Additional link-layer initializations that depend on the previous stage.
Definition: InitStages.h:64
virtual int inet::EtherEncap::numInitStages ( ) const
inlineoverrideprotectedvirtual
49 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::EtherEncap::processFrameFromMAC ( EtherFrame msg)
protectedvirtual

Referenced by handleMessage().

137 {
138  // decapsulate and attach control info
139  cPacket *higherlayermsg = frame->decapsulate();
140 
141  // add Ieee802Ctrl to packet
142  Ieee802Ctrl *etherctrl = new Ieee802Ctrl();
143  etherctrl->setSrc(frame->getSrc());
144  etherctrl->setDest(frame->getDest());
145  etherctrl->setInterfaceId(interfaceId);
146  if (dynamic_cast<EthernetIIFrame *>(frame) != nullptr)
147  etherctrl->setEtherType(((EthernetIIFrame *)frame)->getEtherType());
148  else if (dynamic_cast<EtherFrameWithSNAP *>(frame) != nullptr)
149  etherctrl->setEtherType(((EtherFrameWithSNAP *)frame)->getLocalcode());
150  higherlayermsg->setControlInfo(etherctrl);
151 
152  EV_DETAIL << "Decapsulating frame `" << frame->getName() << "', passing up contained packet `"
153  << higherlayermsg->getName() << "' to higher layer\n";
154 
155  totalFromMAC++;
156  emit(decapPkSignal, higherlayermsg);
157 
158  // pass up to higher layers.
159  EV_INFO << "Sending " << higherlayermsg << " to upper layer.\n";
160  send(higherlayermsg, "upperLayerOut");
161  delete frame;
162 }
long totalFromMAC
Definition: EtherEncap.h:40
static simsignal_t decapPkSignal
Definition: EtherEncap.h:44
int interfaceId
Definition: EtherEncap.h:42
void inet::EtherEncap::processPacketFromHigherLayer ( cPacket *  msg)
protectedvirtual

Referenced by handleMessage().

88 {
89  if (msg->getByteLength() > MAX_ETHERNET_DATA_BYTES)
90  throw cRuntimeError("packet from higher layer (%d bytes) exceeds maximum Ethernet payload length (%d)", (int)msg->getByteLength(), MAX_ETHERNET_DATA_BYTES);
91 
93  emit(encapPkSignal, msg);
94 
95  // Creates MAC header information and encapsulates received higher layer data
96  // with this information and transmits resultant frame to lower layer
97 
98  // create Ethernet frame, fill it in from Ieee802Ctrl and encapsulate msg in it
99  EV_DETAIL << "Encapsulating higher layer packet `" << msg->getName() << "' for MAC\n";
100 
101  IMACProtocolControlInfo *controlInfo = check_and_cast<IMACProtocolControlInfo *>(msg->removeControlInfo());
102  Ieee802Ctrl *etherctrl = dynamic_cast<Ieee802Ctrl *>(controlInfo);
103  EtherFrame *frame = nullptr;
104 
105  if (useSNAP) {
106  EtherFrameWithSNAP *snapFrame = new EtherFrameWithSNAP(msg->getName());
107 
108  snapFrame->setSrc(controlInfo->getSourceAddress()); // if blank, will be filled in by MAC
109  snapFrame->setDest(controlInfo->getDestinationAddress());
110  snapFrame->setOrgCode(0);
111  if (etherctrl)
112  snapFrame->setLocalcode(etherctrl->getEtherType());
113  frame = snapFrame;
114  }
115  else {
116  EthernetIIFrame *eth2Frame = new EthernetIIFrame(msg->getName());
117 
118  eth2Frame->setSrc(controlInfo->getSourceAddress()); // if blank, will be filled in by MAC
119  eth2Frame->setDest(controlInfo->getDestinationAddress());
120  if (etherctrl)
121  eth2Frame->setEtherType(etherctrl->getEtherType());
122  frame = eth2Frame;
123  }
124  delete controlInfo;
125 
126  ASSERT(frame->getByteLength() > 0); // length comes from msg file
127 
128  frame->encapsulate(msg);
129  if (frame->getByteLength() < MIN_ETHERNET_FRAME_BYTES)
130  frame->setByteLength(MIN_ETHERNET_FRAME_BYTES); // "padding"
131 
132  EV_INFO << "Sending " << frame << " to lower layer.\n";
133  send(frame, "lowerLayerOut");
134 }
long totalFromHigherLayer
Definition: EtherEncap.h:39
#define MAX_ETHERNET_DATA_BYTES
Definition: Ethernet.h:29
static simsignal_t encapPkSignal
Definition: EtherEncap.h:43
bool useSNAP
Definition: EtherEncap.h:46
#define MIN_ETHERNET_FRAME_BYTES
Definition: Ethernet.h:31
void inet::EtherEncap::refreshDisplay ( ) const
overrideprotectedvirtual
81 {
82  char buf[80];
83  sprintf(buf, "passed up: %ld\nsent: %ld", totalFromMAC, totalFromHigherLayer);
84  getDisplayString().setTagArg("t", 0, buf);
85 }
long totalFromHigherLayer
Definition: EtherEncap.h:39
long totalFromMAC
Definition: EtherEncap.h:40

Member Data Documentation

simsignal_t inet::EtherEncap::decapPkSignal = registerSignal("decapPk")
staticprotected

Referenced by processFrameFromMAC().

simsignal_t inet::EtherEncap::encapPkSignal = registerSignal("encapPk")
staticprotected
int inet::EtherEncap::interfaceId = -1
protected

Referenced by initialize(), and processFrameFromMAC().

simsignal_t inet::EtherEncap::pauseSentSignal = registerSignal("pauseSent")
staticprotected

Referenced by handleSendPause().

int inet::EtherEncap::seqNum
protected

Referenced by handleSendPause(), and initialize().

long inet::EtherEncap::totalFromHigherLayer
protected
long inet::EtherEncap::totalFromMAC
protected
long inet::EtherEncap::totalPauseSent
protected

Referenced by handleSendPause(), and initialize().

bool inet::EtherEncap::useSNAP
protected

The documentation for this class was generated from the following files: