| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_sample.c ingress/egress packet sampling module |
| 4 |
* |
| 5 |
* Authors: Yotam Gigi <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include "utils.h" |
| 10 |
#include "tc_util.h" |
| 11 |
#include "tc_common.h" |
| 12 |
#include <linux/tc_act/tc_sample.h> |
| 13 |
|
| 14 |
static void explain(void) |
| 15 |
{ |
| 16 |
fprintf(stderr, |
| 17 |
"Usage: sample SAMPLE_CONF\n" |
| 18 |
"where:\n" |
| 19 |
"\tSAMPLE_CONF := SAMPLE_PARAMS | SAMPLE_INDEX\n" |
| 20 |
"\tSAMPLE_PARAMS := rate RATE group GROUP [trunc SIZE] [SAMPLE_INDEX]\n" |
| 21 |
"\tSAMPLE_INDEX := index INDEX\n" |
| 22 |
"\tRATE := The ratio of packets observed at the data source to the samples generated.\n" |
| 23 |
"\tGROUP := the psample sampling group\n" |
| 24 |
"\tSIZE := the truncation size\n" |
| 25 |
"\tINDEX := integer index of the sample action\n"); |
| 26 |
} |
| 27 |
|
| 28 |
static void usage(void) |
| 29 |
{ |
| 30 |
explain(); |
| 31 |
exit(-1); |
| 32 |
} |
| 33 |
|
| 34 |
static int parse_sample(const struct action_util *a, int *argc_p, char ***argv_p, |
| 35 |
int tca_id, struct nlmsghdr *n) |
| 36 |
{ |
| 37 |
struct tc_sample p = { 0 }; |
| 38 |
bool trunc_set = false; |
| 39 |
bool group_set = false; |
| 40 |
bool rate_set = false; |
| 41 |
char **argv = *argv_p; |
| 42 |
struct rtattr *tail; |
| 43 |
int argc = *argc_p; |
| 44 |
__u32 trunc; |
| 45 |
__u32 group; |
| 46 |
__u32 rate; |
| 47 |
|
| 48 |
if (argc <= 1) |
| 49 |
missarg("sample count"); |
| 50 |
|
| 51 |
if (matches(*argv, "sample") == 0) { |
| 52 |
NEXT_ARG(); |
| 53 |
} else { |
| 54 |
fprintf(stderr, "sample bad argument %s\n", *argv); |
| 55 |
return -1; |
| 56 |
} |
| 57 |
|
| 58 |
while (argc > 0) { |
| 59 |
if (matches(*argv, "rate") == 0) { |
| 60 |
NEXT_ARG(); |
| 61 |
if (get_u32(&rate, *argv, 10) != 0) { |
| 62 |
fprintf(stderr, "Illegal rate %s\n", *argv); |
| 63 |
usage(); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
rate_set = true; |
| 67 |
} else if (matches(*argv, "group") == 0) { |
| 68 |
NEXT_ARG(); |
| 69 |
if (get_u32(&group, *argv, 10) != 0) { |
| 70 |
fprintf(stderr, "Illegal group num %s\n", |
| 71 |
*argv); |
| 72 |
usage(); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
group_set = true; |
| 76 |
} else if (matches(*argv, "trunc") == 0) { |
| 77 |
NEXT_ARG(); |
| 78 |
if (get_u32(&trunc, *argv, 10) != 0) { |
| 79 |
fprintf(stderr, "Illegal truncation size %s\n", |
| 80 |
*argv); |
| 81 |
usage(); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
trunc_set = true; |
| 85 |
} else if (matches(*argv, "help") == 0) { |
| 86 |
usage(); |
| 87 |
} else { |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
NEXT_ARG_FWD(); |
| 92 |
} |
| 93 |
|
| 94 |
parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE); |
| 95 |
|
| 96 |
if (argc) { |
| 97 |
if (matches(*argv, "index") == 0) { |
| 98 |
NEXT_ARG(); |
| 99 |
if (get_u32(&p.index, *argv, 10)) { |
| 100 |
fprintf(stderr, "sample: Illegal \"index\"\n"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
NEXT_ARG_FWD(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (!p.index && !group_set) { |
| 108 |
fprintf(stderr, "param \"group\" not set\n"); |
| 109 |
usage(); |
| 110 |
} |
| 111 |
|
| 112 |
if (!p.index && !rate_set) { |
| 113 |
fprintf(stderr, "param \"rate\" not set\n"); |
| 114 |
usage(); |
| 115 |
} |
| 116 |
|
| 117 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 118 |
addattr_l(n, MAX_MSG, TCA_SAMPLE_PARMS, &p, sizeof(p)); |
| 119 |
if (rate_set) |
| 120 |
addattr32(n, MAX_MSG, TCA_SAMPLE_RATE, rate); |
| 121 |
if (group_set) |
| 122 |
addattr32(n, MAX_MSG, TCA_SAMPLE_PSAMPLE_GROUP, group); |
| 123 |
if (trunc_set) |
| 124 |
addattr32(n, MAX_MSG, TCA_SAMPLE_TRUNC_SIZE, trunc); |
| 125 |
|
| 126 |
addattr_nest_end(n, tail); |
| 127 |
|
| 128 |
*argc_p = argc; |
| 129 |
*argv_p = argv; |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static int print_sample(const struct action_util *au, FILE *f, struct rtattr *arg) |
| 134 |
{ |
| 135 |
struct rtattr *tb[TCA_SAMPLE_MAX + 1]; |
| 136 |
struct tc_sample *p; |
| 137 |
|
| 138 |
print_string(PRINT_ANY, "kind", "%s ", "sample"); |
| 139 |
if (arg == NULL) |
| 140 |
return 0; |
| 141 |
|
| 142 |
parse_rtattr_nested(tb, TCA_SAMPLE_MAX, arg); |
| 143 |
|
| 144 |
if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] || |
| 145 |
!tb[TCA_SAMPLE_PSAMPLE_GROUP]) { |
| 146 |
fprintf(stderr, "Missing sample parameters\n"); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
p = RTA_DATA(tb[TCA_SAMPLE_PARMS]); |
| 150 |
|
| 151 |
print_uint(PRINT_ANY, "rate", "rate 1/%u ", |
| 152 |
rta_getattr_u32(tb[TCA_SAMPLE_RATE])); |
| 153 |
print_uint(PRINT_ANY, "group", "group %u", |
| 154 |
rta_getattr_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP])); |
| 155 |
|
| 156 |
if (tb[TCA_SAMPLE_TRUNC_SIZE]) |
| 157 |
print_uint(PRINT_ANY, "trunc_size", " trunc_size %u", |
| 158 |
rta_getattr_u32(tb[TCA_SAMPLE_TRUNC_SIZE])); |
| 159 |
|
| 160 |
print_action_control(" ", p->action, ""); |
| 161 |
|
| 162 |
print_nl(); |
| 163 |
print_uint(PRINT_ANY, "index", "\t index %u", p->index); |
| 164 |
print_int(PRINT_ANY, "ref", " ref %d", p->refcnt); |
| 165 |
print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt); |
| 166 |
|
| 167 |
if (show_stats) { |
| 168 |
if (tb[TCA_SAMPLE_TM]) { |
| 169 |
struct tcf_t *tm = RTA_DATA(tb[TCA_SAMPLE_TM]); |
| 170 |
|
| 171 |
print_tm(tm); |
| 172 |
} |
| 173 |
} |
| 174 |
print_nl(); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
struct action_util sample_action_util = { |
| 179 |
.id = "sample", |
| 180 |
.parse_aopt = parse_sample, |
| 181 |
.print_aopt = print_sample, |
| 182 |
}; |