INET Framework for OMNeT++/OMNEST
inet::DiffservUtil Namespace Reference

Classes

class  ColorAttribute
 

Enumerations

enum  Color { GREEN, YELLOW, RED }
 

Functions

const char * getRequiredAttribute (cXMLElement *element, const char *attrName)
 Returns the value of the named attribute of the XML element, or throws an exception if not found. More...
 
double parseInformationRate (const char *attrValue, const char *attrName, IInterfaceTable *ift, cSimpleModule &owner, int defaultValue)
 Parses the information rate parameter (bits/sec). More...
 
int parseIntAttribute (const char *attrValue, const char *attrName, bool isOptional=true)
 Parses an integer attribute. More...
 
int parseProtocol (const char *attrValue, const char *attrName)
 Parses an IP protocol number. More...
 
int parseDSCP (const char *attrValue, const char *attrName)
 Parses a Diffserv code point. More...
 
void parseDSCPs (const char *attrValue, const char *attrName, std::vector< int > &result)
 Parses a space separated list of DSCP values and puts them into the result vector. More...
 
std::string dscpToString (int dscp)
 Returns the string representation of the given DSCP value. More...
 
std::string colorToString (int color)
 Returns the string representation of the given color. More...
 
double getInterfaceDatarate (IInterfaceTable *ift, cSimpleModule *interfaceModule)
 Returns the datarate of the interface containing the given module. More...
 
cPacket * findIPDatagramInPacket (cPacket *packet)
 Returns the IP datagram encapsulated inside packet, or the packet itself if it is an IPv4/IPv6 datagram. More...
 
int getColor (cPacket *packet)
 Returns the color of the packet. More...
 
void setColor (cPacket *packet, int color)
 Sets the color of the packet. More...
 
bool isEmpty (const char *str)
 Returns true, if the string is empty (nullptr or "");. More...
 

Variables

cEnum * dscpEnum = nullptr
 
cEnum * protocolEnum = nullptr
 

Enumeration Type Documentation

Enumerator
GREEN 
YELLOW 
RED 
30 { GREEN, YELLOW, RED };
Definition: DiffservUtil.h:30
Definition: DiffservUtil.h:30
Definition: DiffservUtil.h:30

Function Documentation

std::string inet::DiffservUtil::colorToString ( int  color)

Returns the string representation of the given color.

For values defined in IMeter.h it returns their name, other values are returned as decimal constants.

Referenced by inet::DiffservUtil::ColorAttribute::info(), and isEmpty().

177 {
178  switch (color) {
179  case GREEN:
180  return "green";
181 
182  case YELLOW:
183  return "yellow";
184 
185  case RED:
186  return "red";
187 
188  default:
189  return ltostr(color);
190  }
191 }
Definition: DiffservUtil.h:30
Definition: DiffservUtil.h:30
std::string ltostr(long i)
Converts an integer to string.
Definition: INETUtils.cc:24
Definition: DiffservUtil.h:30
std::string inet::DiffservUtil::dscpToString ( int  dscp)

Returns the string representation of the given DSCP value.

Values defined in DSCP.msg are returned as "BE", "AF11", etc., others are returned as a decimal number.

Referenced by isEmpty(), and inet::DSCPMarker::markPacket().

163 {
164  if (!dscpEnum)
165  dscpEnum = cEnum::get("inet::DSCP");
166  const char *name = dscpEnum->getStringFor(dscp);
167  if (name) {
168  if (!strncmp(name, "DSCP_", 5))
169  name += 5;
170  return name;
171  }
172  else
173  return ltostr(dscp);
174 }
std::string ltostr(long i)
Converts an integer to string.
Definition: INETUtils.cc:24
cEnum * dscpEnum
Definition: DiffservUtil.cc:40
cPacket * inet::DiffservUtil::findIPDatagramInPacket ( cPacket *  packet)

Returns the IP datagram encapsulated inside packet, or the packet itself if it is an IPv4/IPv6 datagram.

Returns nullptr, if there is no IP datagram in the packet.

Referenced by inet::TokenBucketMeter::handleMessage(), inet::SingleRateThreeColorMeter::handleMessage(), inet::TwoRateThreeColorMeter::handleMessage(), and isEmpty().

200 {
201  for ( ; packet; packet = packet->getEncapsulatedPacket()) {
202 #ifdef WITH_IPv4
203  if (dynamic_cast<IPv4Datagram *>(packet))
204  return packet;
205 #endif // ifdef WITH_IPv4
206 #ifdef WITH_IPv6
207  if (dynamic_cast<IPv6Datagram *>(packet))
208  return packet;
209 #endif // ifdef WITH_IPv6
210  }
211 
212  return nullptr;
213 }
int inet::DiffservUtil::getColor ( cPacket *  packet)

Returns the color of the packet.

The color was set by a previous meter component. Returns -1, if the color was not set.

Referenced by inet::RSVPPathMsg_Base::getERO(), inet::visualizer::ColorSet::getSize(), isEmpty(), inet::TokenBucketMeter::meterPacket(), inet::SingleRateThreeColorMeter::meterPacket(), and inet::TwoRateThreeColorMeter::meterPacket().

228 {
229  ColorAttribute *attr = dynamic_cast<ColorAttribute *>(packet->getParList().get("dscolor"));
230  return attr ? attr->color : -1;
231 }
double inet::DiffservUtil::getInterfaceDatarate ( IInterfaceTable ift,
cSimpleModule *  interfaceModule 
)

Returns the datarate of the interface containing the given module.

Returns -1, if the interface entry not found.

Referenced by isEmpty(), and parseInformationRate().

194 {
195  InterfaceEntry *ie = ift ? ift->getInterfaceByInterfaceModule(interfaceModule) : nullptr;
196  return ie ? ie->getDatarate() : -1;
197 }
const char * inet::DiffservUtil::getRequiredAttribute ( cXMLElement *  element,
const char *  attrName 
)

Returns the value of the named attribute of the XML element, or throws an exception if not found.

Referenced by inet::MultiFieldClassifier::configureFilters(), and isEmpty().

44 {
45  const char *attrValue = element->getAttribute(attrName);
46  if (!attrValue)
47  throw cRuntimeError("missing attribute '%s' from <%s> element", attrName, element->getTagName());
48  return attrValue;
49 }
bool inet::DiffservUtil::isEmpty ( const char *  str)
inline

Returns true, if the string is empty (nullptr or "");.

Referenced by parseDSCP(), parseDSCPs(), parseInformationRate(), parseIntAttribute(), and parseProtocol().

35 { return !str || !(*str); }
int inet::DiffservUtil::parseDSCP ( const char *  attrValue,
const char *  attrName 
)

Parses a Diffserv code point.

Recognizes the names defined in DSCP.msg (e.g. "BE", "AF11"), and accepts decimal/octal/hex/binary numbers.

Referenced by isEmpty(), and parseDSCPs().

122 {
123  if (isEmpty(attrValue))
124  throw cRuntimeError("missing %s attribute", attrName);
125  if (isdigit(*attrValue)) {
126  int dscp = parseIntAttribute(attrValue, attrName);
127  if (dscp < 0 || dscp >= DSCP_MAX)
128  throw cRuntimeError("value of %s attribute is out of range [0,%d)", DSCP_MAX);
129  return dscp;
130  }
131  if (!dscpEnum)
132  dscpEnum = cEnum::get("inet::DSCP");
133  char name[20];
134  strcpy(name, "DSCP_");
135  const char *src;
136  char *dest;
137  for (src = attrValue, dest = name + 5; *src; ++src, ++dest)
138  *dest = toupper(*src);
139  *dest = '\0';
140 
141  int dscp = dscpEnum->lookup(name);
142  if (dscp < 0)
143  throw cRuntimeError("malformed %s attribute", attrName);
144  return dscp;
145 }
Definition: DSCP_m.h:105
cEnum * dscpEnum
Definition: DiffservUtil.cc:40
bool isEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:35
int parseIntAttribute(const char *attrValue, const char *attrName, bool isOptional)
Parses an integer attribute.
Definition: DiffservUtil.cc:78
void inet::DiffservUtil::parseDSCPs ( const char *  attrValue,
const char *  attrName,
std::vector< int > &  result 
)

Parses a space separated list of DSCP values and puts them into the result vector.

"*" is interpreted as all possible DSCP values (i.e. the 0..63 range).

Referenced by inet::DSCPMarker::initialize(), inet::BehaviorAggregateClassifier::initialize(), and isEmpty().

148 {
149  if (isEmpty(attrValue))
150  return;
151  if (*attrValue == '*' && *(attrValue + 1) == '\0') {
152  for (int dscp = 0; dscp < DSCP_MAX; ++dscp)
153  result.push_back(dscp);
154  }
155  else {
156  cStringTokenizer tokens(attrValue);
157  while (tokens.hasMoreTokens())
158  result.push_back(parseDSCP(tokens.nextToken(), attrName));
159  }
160 }
int parseDSCP(const char *attrValue, const char *attrName)
Parses a Diffserv code point.
Definition: DiffservUtil.cc:121
Definition: DSCP_m.h:105
bool isEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:35
double inet::DiffservUtil::parseInformationRate ( const char *  attrValue,
const char *  attrName,
IInterfaceTable ift,
cSimpleModule &  owner,
int  defaultValue 
)

Parses the information rate parameter (bits/sec).

Supported formats:

  • absolute (e.g. 10Mbps)
  • relative to the datarate of the interface (e.g. 10%)

Referenced by inet::TokenBucketMeter::initialize(), inet::SingleRateThreeColorMeter::initialize(), inet::TwoRateThreeColorMeter::initialize(), and isEmpty().

52 {
53  if (isEmpty(attrValue))
54  return defaultValue;
55 
56  const char *percentPtr = strchr(attrValue, '%');
57  if (percentPtr) {
58  char *e;
59  double percent = strtod(attrValue, &e);
60  if (e != percentPtr)
61  throw cRuntimeError("malformed %s attribute: %s", attrName, attrValue);
62  if (percent < 0.0 || percent > 100.0)
63  throw cRuntimeError("%s must be between 0\% and 100\%, found: %s", attrName, attrValue);
64 
65  double datarate = getInterfaceDatarate(ift, &owner);
66  if (datarate < 0.0)
67  throw cRuntimeError("cannot determine datarate for module %s, (no interface table in the node?)", owner.getFullPath().c_str());
68 
69  return (percent / 100.0) * datarate;
70  }
71  else {
72  char *unit;
73  double datarate = strtod(attrValue, &unit);
74  return cNEDValue::convertUnit(datarate, unit, "bps");
75  }
76 }
double getInterfaceDatarate(IInterfaceTable *ift, cSimpleModule *interfaceModule)
Returns the datarate of the interface containing the given module.
Definition: DiffservUtil.cc:193
const value< double, units::C > e(1.602176487e-19)
scale< unit, 100 > percent
Definition: Units.h:991
pow< internal::none, 0 > unit
Definition: Units.h:60
bool isEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:35
int inet::DiffservUtil::parseIntAttribute ( const char *  attrValue,
const char *  attrName,
bool  isOptional = true 
)

Parses an integer attribute.

Supports decimal, octal ("0" prefix), hexadecimal ("0x" prefix), and binary ("0b" prefix) bases.

Referenced by inet::MultiFieldClassifier::configureFilters(), isEmpty(), parseDSCP(), and parseProtocol().

79 {
80  if (isEmpty(attrValue)) {
81  if (isOptional)
82  return -1;
83  else
84  throw cRuntimeError("missing %s attribute", attrName);
85  }
86 
87  unsigned long num;
88  char *endp;
89  if (*attrValue == '0' && *(attrValue + 1) == 'b') // 0b prefix for binary
90  num = strtoul(attrValue + 2, &endp, 2);
91  else
92  num = strtoul(attrValue, &endp, 0); // will handle hex/octal/decimal
93 
94  if (*endp != '\0')
95  throw cRuntimeError("malformed %s attribute: %s", attrName, attrValue);
96 
97  if (num > INT_MAX)
98  throw cRuntimeError("attribute %s is too large: %s", attrName, attrValue);
99 
100  return (int)num;
101 }
bool isEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:35
int inet::DiffservUtil::parseProtocol ( const char *  attrValue,
const char *  attrName 
)

Parses an IP protocol number.

Recognizes the names defined in IPProtocolId.msg (e.g. "UDP", "udp", "Tcp"), and accepts decimal/octal/hex/binary numbers.

Referenced by inet::MultiFieldClassifier::configureFilters(), and isEmpty().

104 {
105  if (isEmpty(attrValue))
106  return -1;
107  if (isdigit(*attrValue))
108  return parseIntAttribute(attrValue, attrName);
109  if (!protocolEnum)
110  protocolEnum = cEnum::get("inet::IPProtocolId");
111  char name[20];
112  strcpy(name, "IP_PROT_");
113  char *dest;
114  for (dest = name + 8; *attrValue; ++dest, ++attrValue)
115  *dest = toupper(*attrValue);
116  *dest = '\0';
117 
118  return protocolEnum->lookup(name);
119 }
bool isEmpty(const char *s)
Definition: L2NetworkConfigurator.cc:35
cEnum * protocolEnum
Definition: DiffservUtil.cc:41
int parseIntAttribute(const char *attrValue, const char *attrName, bool isOptional)
Parses an integer attribute.
Definition: DiffservUtil.cc:78
void inet::DiffservUtil::setColor ( cPacket *  packet,
int  color 
)

Sets the color of the packet.

The color is stored in the parlist of the cPacket object.

Referenced by inet::RSVPPathMsg_Base::getERO(), isEmpty(), inet::TokenBucketMeter::meterPacket(), inet::SingleRateThreeColorMeter::meterPacket(), and inet::TwoRateThreeColorMeter::meterPacket().

234 {
235  ColorAttribute *attr = dynamic_cast<ColorAttribute *>(packet->getParList().get("dscolor"));
236  if (attr)
237  attr->color = color;
238  else
239  packet->addObject(new ColorAttribute(color));
240 }

Variable Documentation

cEnum* inet::DiffservUtil::dscpEnum = nullptr
cEnum* inet::DiffservUtil::protocolEnum = nullptr