13#include <netlink-private/netlink.h>
14#include <netlink-private/tc.h>
15#include <netlink/netlink.h>
16#include <netlink/route/cls/ematch.h>
17#include <netlink/route/cls/ematch/meta.h>
18#include <linux/tc_ematch/tc_em_meta.h>
36 uint8_t shift,
void *data,
41 if (!(value = calloc(1,
sizeof(*value) + len)))
44 value->mv_type = type;
46 value->mv_shift = shift;
50 memcpy(value + 1, data, len);
57 return meta_alloc(TCF_META_TYPE_INT, TCF_META_ID_VALUE, 0, &value, 8);
60struct rtnl_meta_value *rtnl_meta_value_alloc_var(
void *data,
size_t len)
62 return meta_alloc(TCF_META_TYPE_VAR, TCF_META_ID_VALUE, 0, data, len);
65struct rtnl_meta_value *rtnl_meta_value_alloc_id(uint8_t type, uint16_t
id,
66 uint8_t shift, uint64_t mask)
70 if (
id > TCF_META_ID_MAX)
74 if (type == TCF_META_TYPE_VAR)
80 return meta_alloc(type,
id, shift, &mask, masklen);
88void rtnl_ematch_meta_set_lvalue(
struct rtnl_ematch *e,
struct rtnl_meta_value *v)
90 struct meta_data *m = rtnl_ematch_data(e);
94void rtnl_ematch_meta_set_rvalue(
struct rtnl_ematch *e,
struct rtnl_meta_value *v)
96 struct meta_data *m = rtnl_ematch_data(e);
100void rtnl_ematch_meta_set_operand(
struct rtnl_ematch *e, uint8_t opnd)
102 struct meta_data *m = rtnl_ematch_data(e);
106static struct nla_policy meta_policy[TCA_EM_META_MAX+1] = {
107 [TCA_EM_META_HDR] = { .
minlen =
sizeof(
struct tcf_meta_hdr) },
108 [TCA_EM_META_LVALUE] = { .minlen = 1, },
109 [TCA_EM_META_RVALUE] = { .minlen = 1, },
112static int meta_parse(
struct rtnl_ematch *e,
void *data,
size_t len)
114 struct meta_data *m = rtnl_ematch_data(e);
115 struct nlattr *tb[TCA_EM_META_MAX+1];
117 struct tcf_meta_hdr *hdr;
122 if ((err =
nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy)) < 0)
125 if (!tb[TCA_EM_META_HDR])
126 return -NLE_MISSING_ATTR;
128 hdr =
nla_data(tb[TCA_EM_META_HDR]);
130 if (tb[TCA_EM_META_LVALUE]) {
131 vdata =
nla_data(tb[TCA_EM_META_LVALUE]);
132 vlen =
nla_len(tb[TCA_EM_META_LVALUE]);
135 v = meta_alloc(TCF_META_TYPE(hdr->left.kind),
136 TCF_META_ID(hdr->left.kind),
137 hdr->left.shift, vdata, vlen);
144 if (tb[TCA_EM_META_RVALUE]) {
145 vdata =
nla_data(tb[TCA_EM_META_RVALUE]);
146 vlen =
nla_len(tb[TCA_EM_META_RVALUE]);
149 v = meta_alloc(TCF_META_TYPE(hdr->right.kind),
150 TCF_META_ID(hdr->right.kind),
151 hdr->right.shift, vdata, vlen);
153 rtnl_meta_value_put(m->left);
158 m->opnd = hdr->left.op;
163static const struct trans_tbl meta_int[] = {
164 __ADD(TCF_META_ID_RANDOM, random),
165 __ADD(TCF_META_ID_LOADAVG_0, loadavg_0),
166 __ADD(TCF_META_ID_LOADAVG_1, loadavg_1),
167 __ADD(TCF_META_ID_LOADAVG_2, loadavg_2),
168 __ADD(TCF_META_ID_DEV, dev),
169 __ADD(TCF_META_ID_PRIORITY, prio),
170 __ADD(TCF_META_ID_PROTOCOL, proto),
171 __ADD(TCF_META_ID_PKTTYPE, pkttype),
172 __ADD(TCF_META_ID_PKTLEN, pktlen),
173 __ADD(TCF_META_ID_DATALEN, datalen),
174 __ADD(TCF_META_ID_MACLEN, maclen),
175 __ADD(TCF_META_ID_NFMARK, mark),
176 __ADD(TCF_META_ID_TCINDEX, tcindex),
177 __ADD(TCF_META_ID_RTCLASSID, rtclassid),
178 __ADD(TCF_META_ID_RTIIF, rtiif),
179 __ADD(TCF_META_ID_SK_FAMILY, sk_family),
180 __ADD(TCF_META_ID_SK_STATE, sk_state),
181 __ADD(TCF_META_ID_SK_REUSE, sk_reuse),
182 __ADD(TCF_META_ID_SK_REFCNT, sk_refcnt),
183 __ADD(TCF_META_ID_SK_RCVBUF, sk_rcvbuf),
184 __ADD(TCF_META_ID_SK_SNDBUF, sk_sndbuf),
185 __ADD(TCF_META_ID_SK_SHUTDOWN, sk_sutdown),
186 __ADD(TCF_META_ID_SK_PROTO, sk_proto),
187 __ADD(TCF_META_ID_SK_TYPE, sk_type),
188 __ADD(TCF_META_ID_SK_RMEM_ALLOC, sk_rmem_alloc),
189 __ADD(TCF_META_ID_SK_WMEM_ALLOC, sk_wmem_alloc),
190 __ADD(TCF_META_ID_SK_WMEM_QUEUED, sk_wmem_queued),
191 __ADD(TCF_META_ID_SK_RCV_QLEN, sk_rcv_qlen),
192 __ADD(TCF_META_ID_SK_SND_QLEN, sk_snd_qlen),
193 __ADD(TCF_META_ID_SK_ERR_QLEN, sk_err_qlen),
194 __ADD(TCF_META_ID_SK_FORWARD_ALLOCS, sk_forward_allocs),
195 __ADD(TCF_META_ID_SK_ALLOCS, sk_allocs),
196 __ADD(__TCF_META_ID_SK_ROUTE_CAPS, sk_route_caps),
197 __ADD(TCF_META_ID_SK_HASH, sk_hash),
198 __ADD(TCF_META_ID_SK_LINGERTIME, sk_lingertime),
199 __ADD(TCF_META_ID_SK_ACK_BACKLOG, sk_ack_backlog),
200 __ADD(TCF_META_ID_SK_MAX_ACK_BACKLOG, sk_max_ack_backlog),
201 __ADD(TCF_META_ID_SK_PRIO, sk_prio),
202 __ADD(TCF_META_ID_SK_RCVLOWAT, sk_rcvlowat),
203 __ADD(TCF_META_ID_SK_RCVTIMEO, sk_rcvtimeo),
204 __ADD(TCF_META_ID_SK_SNDTIMEO, sk_sndtimeo),
205 __ADD(TCF_META_ID_SK_SENDMSG_OFF, sk_sendmsg_off),
206 __ADD(TCF_META_ID_SK_WRITE_PENDING, sk_write_pending),
207 __ADD(TCF_META_ID_VLAN_TAG, vlan),
208 __ADD(TCF_META_ID_RXHASH, rxhash),
211static char *int_id2str(
int id,
char *buf,
size_t size)
213 return __type2str(
id, buf, size, meta_int, ARRAY_SIZE(meta_int));
216static const struct trans_tbl meta_var[] = {
217 __ADD(TCF_META_ID_DEV,devname),
218 __ADD(TCF_META_ID_SK_BOUND_IF,sk_bound_if),
221static char *var_id2str(
int id,
char *buf,
size_t size)
223 return __type2str(
id, buf, size, meta_var, ARRAY_SIZE(meta_var));
230 switch (v->mv_type) {
231 case TCF_META_TYPE_INT:
232 if (v->mv_id == TCF_META_ID_VALUE) {
234 *(uint32_t *) (v + 1));
237 int_id2str(v->mv_id, buf,
sizeof(buf)));
240 nl_dump(p,
" >> %u", v->mv_shift);
243 nl_dump(p,
" & %#lx", (
long unsigned) *(uint32_t *) (v + 1));
244 else if (v->mv_len == 8)
245 nl_dump(p,
" & %#llx", (
long long unsigned) (*(uint64_t *) (v + 1)));
249 case TCF_META_TYPE_VAR:
250 if (v->mv_id == TCF_META_ID_VALUE) {
251 nl_dump(p,
"%s", (
char *) (v + 1));
254 var_id2str(v->mv_id, buf,
sizeof(buf)));
257 nl_dump(p,
" >> %u", v->mv_shift);
263static void meta_dump(
struct rtnl_ematch *e,
struct nl_dump_params *p)
265 struct meta_data *m = rtnl_ematch_data(e);
269 dump_value(m->left, p);
271 nl_dump(p,
" %s ", rtnl_ematch_opnd2txt(m->opnd, buf,
sizeof(buf)));
273 dump_value(m->right, p);
277static int meta_fill(
struct rtnl_ematch *e,
struct nl_msg *msg)
279 struct meta_data *m = rtnl_ematch_data(e);
280 struct tcf_meta_hdr hdr;
282 if (!(m->left && m->right))
283 return -NLE_MISSING_ATTR;
285 memset(&hdr, 0,
sizeof(hdr));
286 hdr.left.kind = (m->left->mv_type << 12) & TCF_META_TYPE_MASK;
287 hdr.left.kind |= m->left->mv_id & TCF_META_ID_MASK;
288 hdr.left.shift = m->left->mv_shift;
289 hdr.left.op = m->opnd;
290 hdr.right.kind = (m->right->mv_type << 12) & TCF_META_TYPE_MASK;
291 hdr.right.kind |= m->right->mv_id & TCF_META_ID_MASK;
293 NLA_PUT(msg, TCA_EM_META_HDR,
sizeof(hdr), &hdr);
296 NLA_PUT(msg, TCA_EM_META_LVALUE, m->left->mv_len, (m->left + 1));
298 if (m->right->mv_len)
299 NLA_PUT(msg, TCA_EM_META_RVALUE, m->right->mv_len, (m->right + 1));
307static void meta_free(
struct rtnl_ematch *e)
309 struct meta_data *m = rtnl_ematch_data(e);
315 .eo_kind = TCF_EM_META,
317 .eo_minlen =
sizeof(
struct tcf_meta_hdr),
319 .eo_parse = meta_parse,
320 .eo_dump = meta_dump,
321 .eo_fill = meta_fill,
322 .eo_free = meta_free,
325static void __init meta_init(
void)
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
Register ematch module.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
Extended Match Operations.