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