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

Classes

class  ColorAttribute
 

Enumerations

enum  Color { GREEN, YELLOW, RED }
 

Functions

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...
 
int getColor (cPacket *packet)
 Returns the color of the packet. More...
 
void setColor (cPacket *packet, int color)
 Sets the color of the packet. More...
 

Variables

cEnum * dscpEnum = nullptr
 
cEnum * protocolEnum = nullptr
 

Enumeration Type Documentation

◆ Color

Enumerator
GREEN 
YELLOW 
RED 
17 { GREEN, YELLOW, RED };

Function Documentation

◆ colorToString()

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.

157 {
158  switch (color) {
159  case GREEN:
160  return "green";
161 
162  case YELLOW:
163  return "yellow";
164 
165  case RED:
166  return "red";
167 
168  default:
169  return ltostr(color);
170  }
171 }

Referenced by inet::DiffservUtil::ColorAttribute::str().

◆ dscpToString()

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.

143 {
144  if (!dscpEnum)
145  dscpEnum = cEnum::get("inet::Dscp");
146  const char *name = dscpEnum->getStringFor(dscp);
147  if (name) {
148  if (!strncmp(name, "DSCP_", 5))
149  name += 5;
150  return name;
151  }
152  else
153  return ltostr(dscp);
154 }

Referenced by inet::DscpMarker::markPacket().

◆ getColor()

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.

192 {
193  ColorAttribute *attr = dynamic_cast<ColorAttribute *>(packet->getParList().get("dscolor"));
194  return attr ? attr->color : -1;
195 }

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

◆ getInterfaceDatarate()

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.

174 {
175  NetworkInterface *ie = ift ? ift->findInterfaceByInterfaceModule(interfaceModule) : nullptr;
176  return ie ? ie->getDatarate() : -1;
177 }

Referenced by parseInformationRate().

◆ parseDSCP()

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.

102 {
103  if (opp_isempty(attrValue))
104  throw cRuntimeError("missing %s attribute", attrName);
105  if (isdigit(*attrValue)) {
106  int dscp = parseIntAttribute(attrValue, attrName);
107  if (dscp < 0 || dscp >= DSCP_MAX)
108  throw cRuntimeError("value of %s attribute is out of range [0,%d)", attrName, DSCP_MAX);
109  return dscp;
110  }
111  if (!dscpEnum)
112  dscpEnum = cEnum::get("inet::Dscp");
113  char name[20];
114  strcpy(name, "DSCP_");
115  const char *src;
116  char *dest;
117  for (src = attrValue, dest = name + 5; *src; ++src, ++dest)
118  *dest = toupper(*src);
119  *dest = '\0';
120 
121  int dscp = dscpEnum->lookup(name);
122  if (dscp < 0)
123  throw cRuntimeError("malformed %s attribute", attrName);
124  return dscp;
125 }

Referenced by parseDSCPs().

◆ parseDSCPs()

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).

128 {
129  if (opp_isempty(attrValue))
130  return;
131  if (*attrValue == '*' && *(attrValue + 1) == '\0') {
132  for (int dscp = 0; dscp < DSCP_MAX; ++dscp)
133  result.push_back(dscp);
134  }
135  else {
136  cStringTokenizer tokens(attrValue);
137  while (tokens.hasMoreTokens())
138  result.push_back(parseDSCP(tokens.nextToken(), attrName));
139  }
140 }

Referenced by inet::DscpMarker::initialize(), and inet::BehaviorAggregateClassifier::initialize().

◆ parseInformationRate()

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%)
32 {
33  if (opp_isempty(attrValue))
34  return defaultValue;
35 
36  const char *percentPtr = strchr(attrValue, '%');
37  if (percentPtr) {
38  char *e;
39  double percent = strtod(attrValue, &e);
40  if (e != percentPtr)
41  throw cRuntimeError("malformed %s attribute: %s", attrName, attrValue);
42  if (percent < 0.0 || percent > 100.0)
43  throw cRuntimeError("%s must be between 0%% and 100%%, found: %s", attrName, attrValue);
44 
45  double datarate = getInterfaceDatarate(ift, &owner);
46  if (datarate < 0.0)
47  throw cRuntimeError("cannot determine datarate for module %s, (no interface table in the node?)", owner.getFullPath().c_str());
48 
49  return (percent / 100.0) * datarate;
50  }
51  else {
52  char *unit;
53  double datarate = strtod(attrValue, &unit);
54  return cNEDValue::convertUnit(datarate, unit, "bps");
55  }
56 }

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

◆ parseIntAttribute()

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.

59 {
60  if (opp_isempty(attrValue)) {
61  if (isOptional)
62  return -1;
63  else
64  throw cRuntimeError("missing %s attribute", attrName);
65  }
66 
67  unsigned long num;
68  char *endp;
69  if (*attrValue == '0' && *(attrValue + 1) == 'b') // 0b prefix for binary
70  num = strtoul(attrValue + 2, &endp, 2);
71  else
72  num = strtoul(attrValue, &endp, 0); // will handle hex/octal/decimal
73 
74  if (*endp != '\0')
75  throw cRuntimeError("malformed %s attribute: %s", attrName, attrValue);
76 
77  if (num > INT_MAX)
78  throw cRuntimeError("attribute %s is too large: %s", attrName, attrValue);
79 
80  return (int)num;
81 }

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

◆ parseProtocol()

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.

84 {
85  if (opp_isempty(attrValue))
86  return -1;
87  if (isdigit(*attrValue))
88  return parseIntAttribute(attrValue, attrName);
89  if (!protocolEnum)
90  protocolEnum = cEnum::get("inet::IpProtocolId");
91  char name[20];
92  strcpy(name, "IP_PROT_");
93  char *dest;
94  for (dest = name + 8; *attrValue; ++dest, ++attrValue)
95  *dest = toupper(*attrValue);
96  *dest = '\0';
97 
98  return protocolEnum->lookup(name);
99 }

Referenced by inet::MultiFieldClassifier::configureFilters().

◆ setColor()

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.

198 {
199  ColorAttribute *attr = dynamic_cast<ColorAttribute *>(packet->getParList().get("dscolor"));
200  if (attr)
201  attr->color = color;
202  else
203  packet->addObject(new ColorAttribute(color));
204 }

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

Variable Documentation

◆ dscpEnum

cEnum* inet::DiffservUtil::dscpEnum = nullptr

Referenced by dscpToString(), and parseDSCP().

◆ protocolEnum

cEnum* inet::DiffservUtil::protocolEnum = nullptr

Referenced by parseProtocol().

inet::DSCP_MAX
@ DSCP_MAX
Definition: Dscp_m.h:108
inet::units::constants::e
const value< double, units::C > e(1.602176487e-19)
inet::DiffservUtil::parseIntAttribute
int parseIntAttribute(const char *attrValue, const char *attrName, bool isOptional)
Parses an integer attribute.
Definition: DiffservUtil.cc:58
inet::DiffservUtil::protocolEnum
cEnum * protocolEnum
Definition: DiffservUtil.cc:29
inet::DiffservUtil::getInterfaceDatarate
double getInterfaceDatarate(IInterfaceTable *ift, cSimpleModule *interfaceModule)
Returns the datarate of the interface containing the given module.
Definition: DiffservUtil.cc:173
inet::DiffservUtil::YELLOW
@ YELLOW
Definition: DiffservUtil.h:17
inet::DiffservUtil::RED
@ RED
Definition: DiffservUtil.h:17
inet::utils::ltostr
std::string ltostr(long i)
Converts an integer to string.
Definition: INETUtils.cc:14
inet::units::units::percent
scale< unit, 100 > percent
Definition: Units.h:1176
inet::DiffservUtil::dscpEnum
cEnum * dscpEnum
Definition: DiffservUtil.cc:28
inet::units::unit
pow< internal::none, 0 > unit
Definition: Units.h:72
inet::DiffservUtil::GREEN
@ GREEN
Definition: DiffservUtil.h:17
inet::DiffservUtil::parseDSCP
int parseDSCP(const char *attrValue, const char *attrName)
Parses a Diffserv code point.
Definition: DiffservUtil.cc:101