libnl 3.7.0
gact.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2016 Sushma Sitaram <sushma.sitaram@intel.com>
4 */
5
6/**
7 * @ingroup act
8 * @defgroup act_gact GACT Editing
9 *
10 * @{
11 */
12
13#include <netlink-private/netlink.h>
14#include <netlink-private/tc.h>
15#include <netlink/netlink.h>
16#include <netlink/attr.h>
17#include <netlink/utils.h>
18#include <netlink-private/route/tc-api.h>
19#include <netlink/route/act/gact.h>
20
21static struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
22 [TCA_GACT_PARMS] = { .minlen = sizeof(struct tc_gact) },
23};
24
25static int gact_msg_parser(struct rtnl_tc *tc, void *data)
26{
27 struct rtnl_gact *u = data;
28 struct nlattr *tb[TCA_GACT_MAX + 1];
29 int err;
30
31 err = tca_parse(tb, TCA_GACT_MAX, tc, gact_policy);
32 if (err < 0)
33 return err;
34
35 if (!tb[TCA_GACT_PARMS])
36 return -NLE_MISSING_ATTR;
37
38 nla_memcpy(&u->g_parm, tb[TCA_GACT_PARMS], sizeof(u->g_parm));
39
40 return 0;
41}
42
43static void gact_free_data(struct rtnl_tc *tc, void *data)
44{
45}
46
47static void gact_dump_line(struct rtnl_tc *tc, void *data,
48 struct nl_dump_params *p)
49{
50 struct rtnl_gact *u = data;
51
52 if (!u)
53 return;
54
55 switch(u->g_parm.action){
56 case TC_ACT_UNSPEC:
57 nl_dump(p, " continue");
58 break;
59 case TC_ACT_SHOT:
60 nl_dump(p, " drop");
61 break;
62 case TC_ACT_RECLASSIFY:
63 nl_dump(p, " reclassify");
64 break;
65 case TC_ACT_OK:
66 nl_dump(p, " pass");
67 break;
68 }
69
70}
71
72static void gact_dump_details(struct rtnl_tc *tc, void *data,
73 struct nl_dump_params *p)
74{
75}
76
77static void gact_dump_stats(struct rtnl_tc *tc, void *data,
78 struct nl_dump_params *p)
79{
80 struct rtnl_gact *u = data;
81
82 if (!u)
83 return;
84 /* TODO */
85}
86
87
88static int gact_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
89{
90 struct rtnl_gact *u = data;
91
92 if (!u)
93 return 0;
94
95 NLA_PUT(msg, TCA_GACT_PARMS, sizeof(u->g_parm), &u->g_parm);
96
97 return 0;
98
99nla_put_failure:
100 return -NLE_NOMEM;
101}
102
103/**
104 * @name Attribute Modifications
105 * @{
106 */
107
108int rtnl_gact_set_action(struct rtnl_act *act, int action)
109{
110 struct rtnl_gact *u;
111
112 if (!(u = (struct rtnl_gact *) rtnl_tc_data(TC_CAST(act))))
113 return -NLE_NOMEM;
114
115 u->g_parm.action = action;
116
117 return 0;
118}
119
120int rtnl_gact_get_action(struct rtnl_act *act)
121{
122 struct rtnl_gact *u;
123
124 if (!(u = (struct rtnl_gact *) rtnl_tc_data(TC_CAST(act))))
125 return -NLE_NOMEM;
126 return u->g_parm.action;
127}
128
129
130/** @} */
131
132static struct rtnl_tc_ops gact_ops = {
133 .to_kind = "gact",
134 .to_type = RTNL_TC_TYPE_ACT,
135 .to_size = sizeof(struct rtnl_gact),
136 .to_msg_parser = gact_msg_parser,
137 .to_free_data = gact_free_data,
138 .to_clone = NULL,
139 .to_msg_fill = gact_msg_fill,
140 .to_dump = {
141 [NL_DUMP_LINE] = gact_dump_line,
142 [NL_DUMP_DETAILS] = gact_dump_details,
143 [NL_DUMP_STATS] = gact_dump_stats,
144 },
145};
146
147static void __init gact_init(void)
148{
149 rtnl_tc_register(&gact_ops);
150}
151
152static void __exit gact_exit(void)
153{
154 rtnl_tc_unregister(&gact_ops);
155}
156
157/** @} */
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:159
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:346
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1076
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1015
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1049
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
Attribute validation policy.
Definition: attr.h:63
uint16_t minlen
Minimal length of payload required.
Definition: attr.h:68