]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_matchall.c
ll_map: Add function to remove link cache entry by index
[mirror_iproute2.git] / tc / f_matchall.c
CommitLineData
d5cbf3ff
YG
1/*
2 * f_matchall.c Match-all Classifier
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: Jiri Pirko <jiri@mellanox.com>, Yotam Gigi <yotamg@mellanox.com>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
d5cbf3ff
YG
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21#include <linux/if.h>
22
23#include "utils.h"
24#include "tc_util.h"
25
26static void explain(void)
27{
28 fprintf(stderr, "Usage: ... matchall [skip_sw | skip_hw]\n");
29 fprintf(stderr, " [ action ACTION_SPEC ] [ classid CLASSID ]\n");
30 fprintf(stderr, "\n");
31 fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n");
32 fprintf(stderr, " FILTERID := X:Y:Z\n");
33 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
34 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
35}
36
37static int matchall_parse_opt(struct filter_util *qu, char *handle,
38 int argc, char **argv, struct nlmsghdr *n)
39{
40 struct tcmsg *t = NLMSG_DATA(n);
41 struct rtattr *tail;
42 __u32 flags = 0;
43 long h = 0;
44
45 if (handle) {
46 h = strtol(handle, NULL, 0);
47 if (h == LONG_MIN || h == LONG_MAX) {
48 fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n",
49 handle);
50 return -1;
51 }
52 }
53 t->tcm_handle = h;
54
55 if (argc == 0)
56 return 0;
57
58 tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len));
59 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
60
61 while (argc > 0) {
62 if (matches(*argv, "classid") == 0 ||
63 strcmp(*argv, "flowid") == 0) {
64 unsigned int handle;
65
66 NEXT_ARG();
67 if (get_tc_classid(&handle, *argv)) {
68 fprintf(stderr, "Illegal \"classid\"\n");
69 return -1;
70 }
71 addattr_l(n, MAX_MSG, TCA_MATCHALL_CLASSID, &handle, 4);
72 } else if (matches(*argv, "action") == 0) {
73 NEXT_ARG();
74 if (parse_action(&argc, &argv, TCA_MATCHALL_ACT, n)) {
75 fprintf(stderr, "Illegal \"action\"\n");
76 return -1;
77 }
78 continue;
79
80 } else if (strcmp(*argv, "skip_hw") == 0) {
81 NEXT_ARG();
82 flags |= TCA_CLS_FLAGS_SKIP_HW;
83 continue;
84 } else if (strcmp(*argv, "skip_sw") == 0) {
85 NEXT_ARG();
86 flags |= TCA_CLS_FLAGS_SKIP_SW;
87 continue;
88 } else if (strcmp(*argv, "help") == 0) {
89 explain();
90 return -1;
91 } else {
92 fprintf(stderr, "What is \"%s\"?\n", *argv);
93 explain();
94 return -1;
95 }
96 argc--; argv++;
97 }
98
99 if (flags) {
100 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW |
101 TCA_CLS_FLAGS_SKIP_SW))) {
102 fprintf(stderr,
103 "skip_hw and skip_sw are mutually exclusive\n");
104 return -1;
105 }
106 addattr_l(n, MAX_MSG, TCA_MATCHALL_FLAGS, &flags, 4);
107 }
108
109 tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail;
110 return 0;
111}
112
113static int matchall_print_opt(struct filter_util *qu, FILE *f,
114 struct rtattr *opt, __u32 handle)
115{
116 struct rtattr *tb[TCA_MATCHALL_MAX+1];
b0ca46a1 117 struct tc_matchall_pcnt *pf = NULL;
d5cbf3ff
YG
118
119 if (opt == NULL)
120 return 0;
121
122 parse_rtattr_nested(tb, TCA_MATCHALL_MAX, opt);
123
124 if (handle)
619ca351 125 print_uint(PRINT_ANY, "handle", "handle 0x%x ", handle);
d5cbf3ff
YG
126
127 if (tb[TCA_MATCHALL_CLASSID]) {
128 SPRINT_BUF(b1);
619ca351 129 print_string(PRINT_ANY, "flowid", "flowid %s ",
d5cbf3ff
YG
130 sprint_tc_classid(rta_getattr_u32(tb[TCA_MATCHALL_CLASSID]), b1));
131 }
132
afdc1fed
OG
133 if (tb[TCA_MATCHALL_FLAGS]) {
134 __u32 flags = rta_getattr_u32(tb[TCA_MATCHALL_FLAGS]);
135
136 if (flags & TCA_CLS_FLAGS_SKIP_HW)
619ca351 137 print_bool(PRINT_ANY, "skip_hw", "\n skip_hw", true);
afdc1fed 138 if (flags & TCA_CLS_FLAGS_SKIP_SW)
619ca351 139 print_bool(PRINT_ANY, "skip_sw", "\n skip_sw", true);
e57285b8
OG
140
141 if (flags & TCA_CLS_FLAGS_IN_HW)
619ca351 142 print_bool(PRINT_ANY, "in_hw", "\n in_hw", true);
e57285b8 143 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
619ca351 144 print_bool(PRINT_ANY, "not_in_hw", "\n not_in_hw", true);
afdc1fed
OG
145 }
146
b0ca46a1
CW
147 if (tb[TCA_MATCHALL_PCNT]) {
148 if (RTA_PAYLOAD(tb[TCA_MATCHALL_PCNT]) < sizeof(*pf)) {
149 print_string(PRINT_FP, NULL, "Broken perf counters\n", NULL);
150 return -1;
151 }
152 pf = RTA_DATA(tb[TCA_MATCHALL_PCNT]);
153 }
154
155 if (show_stats && NULL != pf)
156 print_u64(PRINT_ANY, "rule_hit", " (rule hit %llu)",
157 (unsigned long long) pf->rhit);
158
159
d5cbf3ff 160 if (tb[TCA_MATCHALL_ACT])
9e713525 161 tc_print_action(f, tb[TCA_MATCHALL_ACT], 0);
d5cbf3ff
YG
162
163 return 0;
164}
165
166struct filter_util matchall_filter_util = {
167 .id = "matchall",
168 .parse_fopt = matchall_parse_opt,
169 .print_fopt = matchall_print_opt,
170};