libnl  3.6.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 
21 static struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
22  [TCA_GACT_PARMS] = { .minlen = sizeof(struct tc_gact) },
23 };
24 
25 static 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 
43 static void gact_free_data(struct rtnl_tc *tc, void *data)
44 {
45 }
46 
47 static 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 
72 static void gact_dump_details(struct rtnl_tc *tc, void *data,
73  struct nl_dump_params *p)
74 {
75 }
76 
77 static 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 
88 static 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 
99 nla_put_failure:
100  return -NLE_NOMEM;
101 }
102 
103 /**
104  * @name Attribute Modifications
105  * @{
106  */
107 
108 int 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  if (action > TC_ACT_SHOT || action < TC_ACT_UNSPEC)
116  return -NLE_INVAL;
117 
118  switch (action) {
119  case TC_ACT_UNSPEC:
120  case TC_ACT_SHOT:
121  u->g_parm.action = action;
122  break;
123  case TC_ACT_OK:
124  case TC_ACT_RECLASSIFY:
125  default:
126  return NLE_OPNOTSUPP;
127  }
128 
129  return 0;
130 }
131 
132 int rtnl_gact_get_action(struct rtnl_act *act)
133 {
134  struct rtnl_gact *u;
135 
136  if (!(u = (struct rtnl_gact *) rtnl_tc_data(TC_CAST(act))))
137  return -NLE_NOMEM;
138  return u->g_parm.action;
139 }
140 
141 
142 /** @} */
143 
144 static struct rtnl_tc_ops gact_ops = {
145  .to_kind = "gact",
146  .to_type = RTNL_TC_TYPE_ACT,
147  .to_size = sizeof(struct rtnl_gact),
148  .to_msg_parser = gact_msg_parser,
149  .to_free_data = gact_free_data,
150  .to_clone = NULL,
151  .to_msg_fill = gact_msg_fill,
152  .to_dump = {
153  [NL_DUMP_LINE] = gact_dump_line,
154  [NL_DUMP_DETAILS] = gact_dump_details,
155  [NL_DUMP_STATS] = gact_dump_stats,
156  },
157 };
158 
159 static void __init gact_init(void)
160 {
161  rtnl_tc_register(&gact_ops);
162 }
163 
164 static void __exit gact_exit(void)
165 {
166  rtnl_tc_unregister(&gact_ops);
167 }
168 
169 /** @} */
#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
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1079
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1018
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1052
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