]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/em_cmp.c
tc/m_estimator: Print proper value for estimator interval in raw.
[mirror_iproute2.git] / tc / em_cmp.c
CommitLineData
311b4145 1/*
bc45ded4 2 * em_cmp.c Simple comparison Ematch
311b4145
SH
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: Thomas Graf <tgraf@suug.ch>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
311b4145
SH
15#include <fcntl.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19#include <string.h>
311b4145
SH
20#include <errno.h>
21
22#include "m_ematch.h"
23#include <linux/tc_ematch/tc_em_cmp.h>
24
25extern struct ematch_util cmp_ematch_util;
26
27static void cmp_print_usage(FILE *fd)
28{
29 fprintf(fd,
30 "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \
31 "where: ALIGN := { u8 | u16 | u32 }\n" \
32 " ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \
bc45ded4 33 " LAYER := { link | network | transport | 0..%d }\n" \
311b4145
SH
34 "\n" \
35 "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n",
36 TCF_LAYER_MAX);
37}
38
39static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
40 struct bstr *args)
41{
42 struct bstr *a;
43 int align, opnd = 0;
44 unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0;
45 int offset_present = 0, value_present = 0;
d17b136f 46 struct tcf_em_cmp cmp = {};
311b4145
SH
47
48#define PARSE_ERR(CARG, FMT, ARGS...) \
32a121cb 49 em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS)
311b4145
SH
50
51 if (args == NULL)
52 return PARSE_ERR(args, "cmp: missing arguments");
53
54 if (!bstrcmp(args, "u8"))
55 align = TCF_EM_ALIGN_U8;
56 else if (!bstrcmp(args, "u16"))
57 align = TCF_EM_ALIGN_U16;
58 else if (!bstrcmp(args, "u32"))
59 align = TCF_EM_ALIGN_U32;
60 else
61 return PARSE_ERR(args, "cmp: invalid alignment");
ae665a52 62
311b4145
SH
63 for (a = bstr_next(args); a; a = bstr_next(a)) {
64 if (!bstrcmp(a, "at")) {
65 if (a->next == NULL)
66 return PARSE_ERR(a, "cmp: missing argument");
67 a = bstr_next(a);
68
69 offset = bstrtoul(a);
70 if (offset == ULONG_MAX)
71 return PARSE_ERR(a, "cmp: invalid offset, " \
72 "must be numeric");
73
74 offset_present = 1;
75 } else if (!bstrcmp(a, "layer")) {
76 if (a->next == NULL)
77 return PARSE_ERR(a, "cmp: missing argument");
78 a = bstr_next(a);
79
80 layer = parse_layer(a);
81 if (layer == INT_MAX) {
82 layer = bstrtoul(a);
83 if (layer == ULONG_MAX)
84 return PARSE_ERR(a, "cmp: invalid " \
85 "layer");
86 }
87
88 if (layer > TCF_LAYER_MAX)
89 return PARSE_ERR(a, "cmp: illegal layer, " \
90 "must be in 0..%d", TCF_LAYER_MAX);
91 } else if (!bstrcmp(a, "mask")) {
92 if (a->next == NULL)
93 return PARSE_ERR(a, "cmp: missing argument");
94 a = bstr_next(a);
95
96 mask = bstrtoul(a);
97 if (mask == ULONG_MAX)
98 return PARSE_ERR(a, "cmp: invalid mask");
99 } else if (!bstrcmp(a, "trans")) {
100 cmp.flags |= TCF_EM_CMP_TRANS;
101 } else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") ||
102 !bstrcmp(a, "lt")) {
103
104 if (!bstrcmp(a, "eq"))
105 opnd = TCF_EM_OPND_EQ;
106 else if (!bstrcmp(a, "gt"))
107 opnd = TCF_EM_OPND_GT;
108 else if (!bstrcmp(a, "lt"))
109 opnd = TCF_EM_OPND_LT;
ae665a52 110
311b4145
SH
111 if (a->next == NULL)
112 return PARSE_ERR(a, "cmp: missing argument");
113 a = bstr_next(a);
114
115 value = bstrtoul(a);
116 if (value == ULONG_MAX)
117 return PARSE_ERR(a, "cmp: invalid value");
118
119 value_present = 1;
120 } else
121 return PARSE_ERR(a, "nbyte: unknown parameter");
122 }
123
124 if (offset_present == 0 || value_present == 0)
125 return PARSE_ERR(a, "cmp: offset and value required");
126
127 cmp.val = (__u32) value;
128 cmp.mask = (__u32) mask;
129 cmp.off = (__u16) offset;
130 cmp.align = (__u8) align;
131 cmp.layer = (__u8) layer;
132 cmp.opnd = (__u8) opnd;
133
134 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
135 addraw_l(n, MAX_MSG, &cmp, sizeof(cmp));
136
137#undef PARSE_ERR
138 return 0;
139}
140
141static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
142 int data_len)
143{
144 struct tcf_em_cmp *cmp = data;
145
146 if (data_len < sizeof(*cmp)) {
147 fprintf(stderr, "CMP header size mismatch\n");
148 return -1;
149 }
150
151 if (cmp->align == TCF_EM_ALIGN_U8)
152 fprintf(fd, "u8 ");
153 else if (cmp->align == TCF_EM_ALIGN_U16)
154 fprintf(fd, "u16 ");
6d5ee98a 155 else if (cmp->align == TCF_EM_ALIGN_U32)
311b4145
SH
156 fprintf(fd, "u32 ");
157
158 fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);
159
160 if (cmp->mask)
161 fprintf(fd, "mask 0x%x ", cmp->mask);
162
163 if (cmp->flags & TCF_EM_CMP_TRANS)
164 fprintf(fd, "trans ");
165
166 if (cmp->opnd == TCF_EM_OPND_EQ)
167 fprintf(fd, "eq ");
168 else if (cmp->opnd == TCF_EM_OPND_LT)
169 fprintf(fd, "lt ");
170 else if (cmp->opnd == TCF_EM_OPND_GT)
171 fprintf(fd, "gt ");
172
173 fprintf(fd, "%d", cmp->val);
174
175 return 0;
176}
177
178struct ematch_util cmp_ematch_util = {
179 .kind = "cmp",
180 .kind_num = TCF_EM_CMP,
181 .parse_eopt = cmp_parse_eopt,
182 .print_eopt = cmp_print_eopt,
183 .print_usage = cmp_print_usage
184};