YARP
Yet Another Robot Platform
QosStyle.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/os/QosStyle.h>
7 #include <yarp/os/Vocab.h>
8 
9 #include <cstdlib>
10 #include <string>
11 
12 
14  threadPriority(-1),
15  threadPolicy(-1),
16  packetPriority(-1)
17 {
18 }
19 
21 {
22  if (dscp != DSCP_Invalid && dscp != DSCP_Undefined) {
23  packetPriority = ((int)dscp) << 2;
24  } else {
25  packetPriority = -1;
26  }
27 }
28 
29 
31 {
32  if (level != PacketPriorityInvalid && level != PacketPriorityUndefined) {
33  packetPriority = ((int)level) << 2;
34  } else {
35  packetPriority = -1;
36  }
37 }
38 
39 bool yarp::os::QosStyle::setPacketPriority(const std::string& priority)
40 {
41  size_t p = priority.find(':');
42  if (p == std::string::npos) {
43  return false;
44  }
45 
46  std::string key = priority.substr(0, p);
47  std::string value = priority.substr(p + 1);
48  if (key.length() <= 0 || value.length() <= 0) {
49  return false;
50  }
51 
52  if (key == "LEVEL") {
53  PacketPriorityLevel level = getLevelByVocab(yarp::os::Vocab32::encode(value));
54  if (level != PacketPriorityInvalid && level != PacketPriorityUndefined) {
55  setPacketPriorityByLevel(level);
56  return true;
57  }
58  } else if (key == "DSCP") {
59  PacketPriorityDSCP dscp = getDSCPByVocab(yarp::os::Vocab32::encode(value));
60  if (dscp != DSCP_Invalid && dscp != DSCP_Undefined) {
61  setPacketPriorityByDscp(dscp);
62  return true;
63  }
64  } else if (key == "TOS") {
65  char* p;
66  int tos = strtol(value.c_str(), &p, 10);
67  if (p == nullptr && tos >= 0) {
68  setPacketPrioritybyTOS(tos);
69  return true;
70  }
71  }
72 
73  packetPriority = -1;
74  return false;
75 }
76 
78 {
79  switch (packetPriority) {
80  case -1:
81  return DSCP_Invalid;
82  case (0 << 2):
83  return DSCP_CS0;
84  case (8 << 2):
85  return DSCP_CS1;
86  case (16 << 2):
87  return DSCP_CS2;
88  case (24 << 2):
89  return DSCP_CS3;
90  case (32 << 2):
91  return DSCP_CS4;
92  case (40 << 2):
93  return DSCP_CS5;
94  case (48 << 2):
95  return DSCP_CS6;
96  case (56 << 2):
97  return DSCP_CS7;
98  case (10 << 2):
99  return DSCP_AF11;
100  case (12 << 2):
101  return DSCP_AF12;
102  case (14 << 2):
103  return DSCP_AF13;
104  case (18 << 2):
105  return DSCP_AF21;
106  case (20 << 2):
107  return DSCP_AF22;
108  case (22 << 2):
109  return DSCP_AF23;
110  case (26 << 2):
111  return DSCP_AF31;
112  case (28 << 2):
113  return DSCP_AF32;
114  case (30 << 2):
115  return DSCP_AF33;
116  case (34 << 2):
117  return DSCP_AF41;
118  case (36 << 2):
119  return DSCP_AF42;
120  case (38 << 2):
121  return DSCP_AF43;
122  case (44 << 2):
123  return DSCP_VA;
124  case (46 << 2):
125  return DSCP_EF;
126  default:
127  return DSCP_Undefined;
128  }
129 }
130 
132 {
133  switch (packetPriority) {
134  case -1:
135  return PacketPriorityInvalid;
136  case (0 << 2):
137  return PacketPriorityNormal;
138  case (10 << 2):
139  return PacketPriorityLow;
140  case (36 << 2):
141  return PacketPriorityHigh;
142  case (44 << 2):
143  return PacketPriorityCritical;
144  default:
145  return PacketPriorityUndefined;
146  }
147 }
148 
149 
150 // | | Class 1 | Class 2 | Class 3 | Class 4 |
151 // |-----------|----------------|----------------|----------------|----------------|
152 // | Low Drop | AF11 (DSCP 10) | AF21 (DSCP 18) | AF31 (DSCP 26) | AF41 (DSCP 34) |
153 // | Med Drop | AF12 (DSCP 12) | AF22 (DSCP 20) | AF32 (DSCP 28) | AF42 (DSCP 36) |
154 // | High Drop | AF13 (DSCP 14) | AF23 (DSCP 22) | AF33 (DSCP 30) | AF43 (DSCP 38) |
156 {
157  switch (vocab) {
158  case yarp::os::createVocab32('C', 'S', '0'):
159  return DSCP_CS0;
160  case yarp::os::createVocab32('C', 'S', '1'):
161  return DSCP_CS1;
162  case yarp::os::createVocab32('C', 'S', '2'):
163  return DSCP_CS2;
164  case yarp::os::createVocab32('C', 'S', '3'):
165  return DSCP_CS3;
166  case yarp::os::createVocab32('C', 'S', '4'):
167  return DSCP_CS4;
168  case yarp::os::createVocab32('C', 'S', '5'):
169  return DSCP_CS5;
170  case yarp::os::createVocab32('C', 'S', '6'):
171  return DSCP_CS6;
172  case yarp::os::createVocab32('C', 'S', '7'):
173  return DSCP_CS7;
174  case yarp::os::createVocab32('A', 'F', '1', '1'):
175  return DSCP_AF11;
176  case yarp::os::createVocab32('A', 'F', '1', '2'):
177  return DSCP_AF12;
178  case yarp::os::createVocab32('A', 'F', '1', '3'):
179  return DSCP_AF13;
180  case yarp::os::createVocab32('A', 'F', '2', '1'):
181  return DSCP_AF21;
182  case yarp::os::createVocab32('A', 'F', '2', '2'):
183  return DSCP_AF22;
184  case yarp::os::createVocab32('A', 'F', '2', '3'):
185  return DSCP_AF23;
186  case yarp::os::createVocab32('A', 'F', '3', '1'):
187  return DSCP_AF31;
188  case yarp::os::createVocab32('A', 'F', '3', '2'):
189  return DSCP_AF32;
190  case yarp::os::createVocab32('A', 'F', '3', '3'):
191  return DSCP_AF33;
192  case yarp::os::createVocab32('A', 'F', '4', '1'):
193  return DSCP_AF41;
194  case yarp::os::createVocab32('A', 'F', '4', '2'):
195  return DSCP_AF42;
196  case yarp::os::createVocab32('A', 'F', '4', '3'):
197  return DSCP_AF43;
198  case yarp::os::createVocab32('V', 'A'):
199  return DSCP_VA;
200  case yarp::os::createVocab32('E', 'F'):
201  return DSCP_EF;
202  default:
203  return DSCP_Invalid;
204  };
205 }
206 
207 
209 {
210  switch (vocab) {
211  case yarp::os::createVocab32('N', 'O', 'R', 'M'):
212  return PacketPriorityNormal;
213  case yarp::os::createVocab32('L', 'O', 'W'):
214  return PacketPriorityLow;
215  case yarp::os::createVocab32('H', 'I', 'G', 'H'):
216  return PacketPriorityHigh;
217  case yarp::os::createVocab32('C', 'R', 'I', 'T'):
218  return PacketPriorityCritical;
219  default:
220  return PacketPriorityInvalid;
221  }
222 }
PacketPriorityDSCP
The PacketPriorityDSCP defines the packets quality of service (priority) using DSCP.
Definition: QosStyle.h:46
bool setPacketPriority(const std::string &priority)
sets the packet priority from a string.
Definition: QosStyle.cpp:39
static PacketPriorityLevel getLevelByVocab(yarp::conf::vocab32_t vocab)
returns the priority level
Definition: QosStyle.cpp:208
PacketPriorityDSCP getPacketPriorityAsDSCP() const
returns the packet DSCP class
Definition: QosStyle.cpp:77
QosStyle()
Constructor.
Definition: QosStyle.cpp:13
static PacketPriorityDSCP getDSCPByVocab(yarp::conf::vocab32_t vocab)
returns the IPV4/6 DSCP value given as DSCP code
Definition: QosStyle.cpp:155
PacketPriorityLevel
The PacketPriorityLevel defines the packets quality of service (priority) levels.
Definition: QosStyle.h:31
void setPacketPriorityByDscp(PacketPriorityDSCP dscp)
sets the packet priority given as DSCP class
Definition: QosStyle.cpp:20
void setPacketPriorityByLevel(PacketPriorityLevel level)
sets the packet priority given as PacketPriorityLevel
Definition: QosStyle.cpp:30
PacketPriorityLevel getPacketPriorityAsLevel() const
returns the packet TOS value
Definition: QosStyle.cpp:131
std::int32_t vocab32_t
Definition: numeric.h:78
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:11
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:28