libnl: /data/src/libnl/lib/route/cls/ematch/cmp.c Source File

libnl 3.12.0

cmp.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2008-2013 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup ematch
8 * @defgroup em_cmp Simple packet data comparison
9 *
10 * @{
11 */
12
13#include "nl-default.h"
14
15#include <linux/tc_ematch/tc_em_cmp.h>
16
17#include <netlink/netlink.h>
18#include <netlink/route/cls/ematch.h>
19#include <netlink/route/cls/ematch/cmp.h>
20
21void rtnl_ematch_cmp_set(struct rtnl_ematch *e, struct tcf_em_cmp *cfg)
22{
23 memcpy(rtnl_ematch_data(e), cfg, sizeof(*cfg));
24}
25
26struct tcf_em_cmp *rtnl_ematch_cmp_get(struct rtnl_ematch *e)
27{
28 return rtnl_ematch_data(e);
29}
30
31static int cmp_parse(struct rtnl_ematch *e, void *data, size_t len)
32{
33 memcpy(rtnl_ematch_data(e), data, len);
34
35 return 0;
36}
37
38static const char *align_txt[] = {
39 [TCF_EM_ALIGN_U8] = "u8",
40 [TCF_EM_ALIGN_U16] = "u16",
41 [TCF_EM_ALIGN_U32] = "u32"
42};
43
44static const char *layer_txt[] = {
45 [TCF_LAYER_LINK] = "eth",
46 [TCF_LAYER_NETWORK] = "ip",
47 [TCF_LAYER_TRANSPORT] = "tcp"
48};
49
50static const char *operand_txt[] = {
51 [TCF_EM_OPND_EQ] = "=",
52 [TCF_EM_OPND_LT] = "<",
53 [TCF_EM_OPND_GT] = ">",
54};
55
56static void cmp_dump(struct rtnl_ematch *e, struct nl_dump_params *p)
57{
58 struct tcf_em_cmp *cmp = rtnl_ematch_data(e);
59
60 if (cmp->flags & TCF_EM_CMP_TRANS)
61 nl_dump(p, "ntoh%c(", (cmp->align == TCF_EM_ALIGN_U32) ? 'l' : 's');
62
63 nl_dump(p, "%s at %s+%u",
64 align_txt[cmp->align], layer_txt[cmp->layer], cmp->off);
65
66 if (cmp->mask)
67 nl_dump(p, " & 0x%x", cmp->mask);
68
69 if (cmp->flags & TCF_EM_CMP_TRANS)
70 nl_dump(p, ")");
71
72 nl_dump(p, " %s %u", operand_txt[cmp->opnd], cmp->val);
73}
74
75static struct rtnl_ematch_ops cmp_ops = {
76 .eo_kind = TCF_EM_CMP,
77 .eo_name = "cmp",
78 .eo_minlen = sizeof(struct tcf_em_cmp),
79 .eo_datalen = sizeof(struct tcf_em_cmp),
80 .eo_parse = cmp_parse,
81 .eo_dump = cmp_dump,
82};
83
84static void _nl_init cmp_init(void)
85{
86 rtnl_ematch_register(&cmp_ops);
87}
88
89/** @} */
rtnl_ematch_register
int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
Register ematch module.
Definition ematch.c:44
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
nl_dump_params
Dumping parameters.
Definition types.h:32
rtnl_ematch_ops
Extended Match Operations.
Definition ematch.h:28