| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
* Copyright © 2019 VMware, Inc. */ |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "extract-word.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "parse-util.h" |
| 10 |
#include "percent-util.h" |
| 11 |
#include "tc-util.h" |
| 12 |
#include "time-util.h" |
| 13 |
|
| 14 |
int tc_init(double *ret_ticks_in_usec, uint32_t *ret_hz) { |
| 15 |
static double ticks_in_usec = -1; |
| 16 |
static uint32_t hz; |
| 17 |
|
| 18 |
if (ticks_in_usec < 0) { |
| 19 |
uint32_t clock_resolution, ticks_to_usec, usec_to_ticks; |
| 20 |
_cleanup_free_ char *line = NULL; |
| 21 |
double clock_factor; |
| 22 |
int r; |
| 23 |
|
| 24 |
r = read_one_line_file("/proc/net/psched", &line); |
| 25 |
if (r < 0) |
| 26 |
return r; |
| 27 |
|
| 28 |
r = sscanf(line, "%08x%08x%08x%08x", &ticks_to_usec, &usec_to_ticks, &clock_resolution, &hz); |
| 29 |
if (r < 4) |
| 30 |
return -EIO; |
| 31 |
|
| 32 |
clock_factor = (double) clock_resolution / USEC_PER_SEC; |
| 33 |
ticks_in_usec = (double) ticks_to_usec / usec_to_ticks * clock_factor; |
| 34 |
} |
| 35 |
|
| 36 |
if (ret_ticks_in_usec) |
| 37 |
*ret_ticks_in_usec = ticks_in_usec; |
| 38 |
if (ret_hz) |
| 39 |
*ret_hz = hz; |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int tc_time_to_tick(usec_t t, uint32_t *ret) { |
| 45 |
double ticks_in_usec; |
| 46 |
usec_t a; |
| 47 |
int r; |
| 48 |
|
| 49 |
assert(ret); |
| 50 |
|
| 51 |
r = tc_init(&ticks_in_usec, NULL); |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
|
| 55 |
a = t * ticks_in_usec; |
| 56 |
if (a > UINT32_MAX) |
| 57 |
return -ERANGE; |
| 58 |
|
| 59 |
*ret = a; |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
int parse_tc_percent(const char *s, uint32_t *ret_fraction) { |
| 64 |
int r; |
| 65 |
|
| 66 |
assert(s); |
| 67 |
assert(ret_fraction); |
| 68 |
|
| 69 |
r = parse_permyriad(s); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
|
| 73 |
*ret_fraction = (double) r / 10000 * UINT32_MAX; |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int tc_transmit_time(uint64_t rate, uint32_t size, uint32_t *ret) { |
| 78 |
return tc_time_to_tick(USEC_PER_SEC * ((double)size / (double)rate), ret); |
| 79 |
} |
| 80 |
|
| 81 |
int tc_fill_ratespec_and_table(struct tc_ratespec *rate, uint32_t *rtab, uint32_t mtu) { |
| 82 |
uint32_t cell_log = 0; |
| 83 |
int r; |
| 84 |
|
| 85 |
if (mtu == 0) |
| 86 |
mtu = 2047; |
| 87 |
|
| 88 |
while ((mtu >> cell_log) > 255) |
| 89 |
cell_log++; |
| 90 |
|
| 91 |
for (size_t i = 0; i < 256; i++) { |
| 92 |
uint32_t sz; |
| 93 |
|
| 94 |
sz = (i + 1) << cell_log; |
| 95 |
if (sz < rate->mpu) |
| 96 |
sz = rate->mpu; |
| 97 |
r = tc_transmit_time(rate->rate, sz, &rtab[i]); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
} |
| 101 |
|
| 102 |
rate->cell_align = -1; |
| 103 |
rate->cell_log = cell_log; |
| 104 |
rate->linklayer = TC_LINKLAYER_ETHERNET; |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
int parse_handle(const char *t, uint32_t *ret) { |
| 109 |
_cleanup_free_ char *word = NULL; |
| 110 |
uint16_t major, minor; |
| 111 |
int r; |
| 112 |
|
| 113 |
assert(t); |
| 114 |
assert(ret); |
| 115 |
|
| 116 |
/* Extract the major number. */ |
| 117 |
r = extract_first_word(&t, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
if (r == 0) |
| 121 |
return -EINVAL; |
| 122 |
if (!t) |
| 123 |
return -EINVAL; |
| 124 |
|
| 125 |
r = safe_atou16_full(word, 16, &major); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
|
| 129 |
r = safe_atou16_full(t, 16, &minor); |
| 130 |
if (r < 0) |
| 131 |
return r; |
| 132 |
|
| 133 |
*ret = ((uint32_t) major << 16) | minor; |
| 134 |
return 0; |
| 135 |
} |