|
9 | 9 | #include "qdisc.h" |
10 | 10 | #include "htb.h" |
11 | 11 | #include "string-util.h" |
| 12 | +#include "tc-util.h" |
12 | 13 |
|
13 | 14 | static int hierarchy_token_bucket_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
14 | 15 | HierarchyTokenBucket *htb; |
@@ -96,3 +97,183 @@ const QDiscVTable htb_vtable = { |
96 | 97 | .tca_kind = "htb", |
97 | 98 | .fill_message = hierarchy_token_bucket_fill_message, |
98 | 99 | }; |
| 100 | + |
| 101 | +static int hierarchy_token_bucket_class_fill_message(Link *link, TClass *tclass, sd_netlink_message *req) { |
| 102 | + HierarchyTokenBucketClass *htb; |
| 103 | + struct tc_htb_opt opt = {}; |
| 104 | + uint32_t rtab[256], ctab[256], mtu = 1600; /* Ethernet packet length */ |
| 105 | + int r; |
| 106 | + |
| 107 | + assert(link); |
| 108 | + assert(tclass); |
| 109 | + assert(req); |
| 110 | + |
| 111 | + htb = TCLASS_TO_HTB(tclass); |
| 112 | + |
| 113 | + if (htb->ceil_rate == 0) |
| 114 | + htb->ceil_rate = htb->rate; |
| 115 | + |
| 116 | + opt.prio = htb->priority; |
| 117 | + opt.rate.rate = (htb->rate >= (1ULL << 32)) ? ~0U : htb->rate; |
| 118 | + opt.ceil.rate = (htb->ceil_rate >= (1ULL << 32)) ? ~0U : htb->ceil_rate; |
| 119 | + r = tc_transmit_time(htb->rate, mtu, &opt.buffer); |
| 120 | + if (r < 0) |
| 121 | + return log_link_error_errno(link, r, "Failed to calculate buffer size: %m"); |
| 122 | + |
| 123 | + r = tc_transmit_time(htb->ceil_rate, mtu, &opt.cbuffer); |
| 124 | + if (r < 0) |
| 125 | + return log_link_error_errno(link, r, "Failed to calculate ceil buffer size: %m"); |
| 126 | + |
| 127 | + r = tc_fill_ratespec_and_table(&opt.rate, rtab, mtu); |
| 128 | + if (r < 0) |
| 129 | + return log_link_error_errno(link, r, "Failed to calculate rate table: %m"); |
| 130 | + |
| 131 | + r = tc_fill_ratespec_and_table(&opt.ceil, ctab, mtu); |
| 132 | + if (r < 0) |
| 133 | + return log_link_error_errno(link, r, "Failed to calculate ceil rate table: %m"); |
| 134 | + |
| 135 | + r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "htb"); |
| 136 | + if (r < 0) |
| 137 | + return log_link_error_errno(link, r, "Could not open container TCA_OPTIONS: %m"); |
| 138 | + |
| 139 | + r = sd_netlink_message_append_data(req, TCA_HTB_PARMS, &opt, sizeof(opt)); |
| 140 | + if (r < 0) |
| 141 | + return log_link_error_errno(link, r, "Could not append TCA_HTB_PARMS attribute: %m"); |
| 142 | + |
| 143 | + if (htb->rate >= (1ULL << 32)) { |
| 144 | + r = sd_netlink_message_append_u64(req, TCA_HTB_RATE64, htb->rate); |
| 145 | + if (r < 0) |
| 146 | + return log_link_error_errno(link, r, "Could not append TCA_HTB_RATE64 attribute: %m"); |
| 147 | + } |
| 148 | + |
| 149 | + if (htb->ceil_rate >= (1ULL << 32)) { |
| 150 | + r = sd_netlink_message_append_u64(req, TCA_HTB_CEIL64, htb->ceil_rate); |
| 151 | + if (r < 0) |
| 152 | + return log_link_error_errno(link, r, "Could not append TCA_HTB_CEIL64 attribute: %m"); |
| 153 | + } |
| 154 | + |
| 155 | + r = sd_netlink_message_append_data(req, TCA_HTB_RTAB, rtab, sizeof(rtab)); |
| 156 | + if (r < 0) |
| 157 | + return log_link_error_errno(link, r, "Could not append TCA_HTB_RTAB attribute: %m"); |
| 158 | + |
| 159 | + r = sd_netlink_message_append_data(req, TCA_HTB_CTAB, ctab, sizeof(ctab)); |
| 160 | + if (r < 0) |
| 161 | + return log_link_error_errno(link, r, "Could not append TCA_HTB_CTAB attribute: %m"); |
| 162 | + |
| 163 | + r = sd_netlink_message_close_container(req); |
| 164 | + if (r < 0) |
| 165 | + return log_link_error_errno(link, r, "Could not close container TCA_OPTIONS: %m"); |
| 166 | + return 0; |
| 167 | +} |
| 168 | + |
| 169 | +int config_parse_hierarchy_token_bucket_u32( |
| 170 | + const char *unit, |
| 171 | + const char *filename, |
| 172 | + unsigned line, |
| 173 | + const char *section, |
| 174 | + unsigned section_line, |
| 175 | + const char *lvalue, |
| 176 | + int ltype, |
| 177 | + const char *rvalue, |
| 178 | + void *data, |
| 179 | + void *userdata) { |
| 180 | + |
| 181 | + _cleanup_(tclass_free_or_set_invalidp) TClass *tclass = NULL; |
| 182 | + HierarchyTokenBucketClass *htb; |
| 183 | + Network *network = data; |
| 184 | + int r; |
| 185 | + |
| 186 | + assert(filename); |
| 187 | + assert(lvalue); |
| 188 | + assert(rvalue); |
| 189 | + assert(data); |
| 190 | + |
| 191 | + r = tclass_new_static(TCLASS_KIND_HTB, network, filename, section_line, &tclass); |
| 192 | + if (r < 0) |
| 193 | + return log_syntax(unit, LOG_ERR, filename, line, r, |
| 194 | + "Failed to create traffic control class, ignoring assignment: %m"); |
| 195 | + |
| 196 | + htb = TCLASS_TO_HTB(tclass); |
| 197 | + |
| 198 | + if (isempty(rvalue)) { |
| 199 | + htb->priority = 0; |
| 200 | + |
| 201 | + tclass = NULL; |
| 202 | + return 0; |
| 203 | + } |
| 204 | + |
| 205 | + r = safe_atou32(rvalue, &htb->priority); |
| 206 | + if (r < 0) { |
| 207 | + log_syntax(unit, LOG_ERR, filename, line, r, |
| 208 | + "Failed to parse '%s=', ignoring assignment: %s", |
| 209 | + lvalue, rvalue); |
| 210 | + return 0; |
| 211 | + } |
| 212 | + |
| 213 | + tclass = NULL; |
| 214 | + |
| 215 | + return 0; |
| 216 | +} |
| 217 | + |
| 218 | +int config_parse_hierarchy_token_bucket_rate( |
| 219 | + const char *unit, |
| 220 | + const char *filename, |
| 221 | + unsigned line, |
| 222 | + const char *section, |
| 223 | + unsigned section_line, |
| 224 | + const char *lvalue, |
| 225 | + int ltype, |
| 226 | + const char *rvalue, |
| 227 | + void *data, |
| 228 | + void *userdata) { |
| 229 | + |
| 230 | + _cleanup_(tclass_free_or_set_invalidp) TClass *tclass = NULL; |
| 231 | + HierarchyTokenBucketClass *htb; |
| 232 | + Network *network = data; |
| 233 | + uint64_t *v; |
| 234 | + int r; |
| 235 | + |
| 236 | + assert(filename); |
| 237 | + assert(lvalue); |
| 238 | + assert(rvalue); |
| 239 | + assert(data); |
| 240 | + |
| 241 | + r = tclass_new_static(TCLASS_KIND_HTB, network, filename, section_line, &tclass); |
| 242 | + if (r < 0) |
| 243 | + return log_syntax(unit, LOG_ERR, filename, line, r, |
| 244 | + "Failed to create traffic control class, ignoring assignment: %m"); |
| 245 | + |
| 246 | + htb = TCLASS_TO_HTB(tclass); |
| 247 | + if (streq(lvalue, "Rate")) |
| 248 | + v = &htb->rate; |
| 249 | + else if (streq(lvalue, "CeilRate")) |
| 250 | + v = &htb->ceil_rate; |
| 251 | + else |
| 252 | + assert_not_reached("Invalid lvalue"); |
| 253 | + |
| 254 | + if (isempty(rvalue)) { |
| 255 | + *v = 0; |
| 256 | + |
| 257 | + tclass = NULL; |
| 258 | + return 0; |
| 259 | + } |
| 260 | + |
| 261 | + r = parse_size(rvalue, 1000, v); |
| 262 | + if (r < 0) { |
| 263 | + log_syntax(unit, LOG_ERR, filename, line, r, |
| 264 | + "Failed to parse '%s=', ignoring assignment: %s", |
| 265 | + lvalue, rvalue); |
| 266 | + return 0; |
| 267 | + } |
| 268 | + |
| 269 | + *v /= 8; |
| 270 | + tclass = NULL; |
| 271 | + |
| 272 | + return 0; |
| 273 | +} |
| 274 | + |
| 275 | +const TClassVTable htb_tclass_vtable = { |
| 276 | + .object_size = sizeof(HierarchyTokenBucketClass), |
| 277 | + .tca_kind = "htb", |
| 278 | + .fill_message = hierarchy_token_bucket_class_fill_message, |
| 279 | +}; |
0 commit comments