libnl: /data/src/libnl/lib/netfilter/queue_obj.c Source File

libnl 3.12.0

queue_obj.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
4 */
5
6/**
7 * @ingroup nfnl
8 * @defgroup queue Queue
9 * @brief
10 * @{
11 */
12
13#include "nl-default.h"
14
15#include <netlink/netfilter/nfnl.h>
16#include <netlink/netfilter/queue.h>
17
18#include "nl-priv-dynamic-core/object-api.h"
19#include "nl-priv-dynamic-core/nl-core.h"
20
21/** @cond SKIP */
22struct nfnl_queue {
23 NLHDR_COMMON
24
25 uint16_t queue_group;
26 uint32_t queue_maxlen;
27 uint32_t queue_copy_range;
28 uint8_t queue_copy_mode;
29};
30
31#define QUEUE_ATTR_GROUP (1UL << 0)
32#define QUEUE_ATTR_MAXLEN (1UL << 1)
33#define QUEUE_ATTR_COPY_MODE (1UL << 2)
34#define QUEUE_ATTR_COPY_RANGE (1UL << 3)
35/** @endcond */
36
37
38static void nfnl_queue_dump(struct nl_object *a, struct nl_dump_params *p)
39{
40 struct nfnl_queue *queue = (struct nfnl_queue *) a;
41 char buf[64];
42
43 nl_new_line(p);
44
45 if (queue->ce_mask & QUEUE_ATTR_GROUP)
46 nl_dump(p, "group=%u ", queue->queue_group);
47
48 if (queue->ce_mask & QUEUE_ATTR_MAXLEN)
49 nl_dump(p, "maxlen=%u ", queue->queue_maxlen);
50
51 if (queue->ce_mask & QUEUE_ATTR_COPY_MODE)
52 nl_dump(p, "copy_mode=%s ",
53 nfnl_queue_copy_mode2str(queue->queue_copy_mode,
54 buf, sizeof(buf)));
55
56 if (queue->ce_mask & QUEUE_ATTR_COPY_RANGE)
57 nl_dump(p, "copy_range=%u ", queue->queue_copy_range);
58
59 nl_dump(p, "\n");
60}
61
62static const struct trans_tbl copy_modes[] = {
63 __ADD(NFNL_QUEUE_COPY_NONE, none),
64 __ADD(NFNL_QUEUE_COPY_META, meta),
65 __ADD(NFNL_QUEUE_COPY_PACKET, packet),
66};
67
68char *nfnl_queue_copy_mode2str(enum nfnl_queue_copy_mode copy_mode, char *buf,
69 size_t len)
70{
71 return __type2str(copy_mode, buf, len, copy_modes,
72 ARRAY_SIZE(copy_modes));
73}
74
75int nfnl_queue_str2copy_mode(const char *name)
76{
77 return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
78}
79
80/**
81 * @name Allocation/Freeing
82 * @{
83 */
84
85struct nfnl_queue *nfnl_queue_alloc(void)
86{
87 return (struct nfnl_queue *) nl_object_alloc(&queue_obj_ops);
88}
89
90void nfnl_queue_get(struct nfnl_queue *queue)
91{
92 nl_object_get((struct nl_object *) queue);
93}
94
95void nfnl_queue_put(struct nfnl_queue *queue)
96{
97 nl_object_put((struct nl_object *) queue);
98}
99
100/** @} */
101
102/**
103 * @name Attributes
104 * @{
105 */
106
107void nfnl_queue_set_group(struct nfnl_queue *queue, uint16_t group)
108{
109 queue->queue_group = group;
110 queue->ce_mask |= QUEUE_ATTR_GROUP;
111}
112
113int nfnl_queue_test_group(const struct nfnl_queue *queue)
114{
115 return !!(queue->ce_mask & QUEUE_ATTR_GROUP);
116}
117
118uint16_t nfnl_queue_get_group(const struct nfnl_queue *queue)
119{
120 return queue->queue_group;
121}
122
123void nfnl_queue_set_maxlen(struct nfnl_queue *queue, uint32_t maxlen)
124{
125 queue->queue_maxlen = maxlen;
126 queue->ce_mask |= QUEUE_ATTR_MAXLEN;
127}
128
129int nfnl_queue_test_maxlen(const struct nfnl_queue *queue)
130{
131 return !!(queue->ce_mask & QUEUE_ATTR_MAXLEN);
132}
133
134uint32_t nfnl_queue_get_maxlen(const struct nfnl_queue *queue)
135{
136 return queue->queue_maxlen;
137}
138
139void nfnl_queue_set_copy_mode(struct nfnl_queue *queue, enum nfnl_queue_copy_mode mode)
140{
141 queue->queue_copy_mode = mode;
142 queue->ce_mask |= QUEUE_ATTR_COPY_MODE;
143}
144
145int nfnl_queue_test_copy_mode(const struct nfnl_queue *queue)
146{
147 return !!(queue->ce_mask & QUEUE_ATTR_COPY_MODE);
148}
149
150enum nfnl_queue_copy_mode nfnl_queue_get_copy_mode(const struct nfnl_queue *queue)
151{
152 return queue->queue_copy_mode;
153}
154
155void nfnl_queue_set_copy_range(struct nfnl_queue *queue, uint32_t copy_range)
156{
157 queue->queue_copy_range = copy_range;
158 queue->ce_mask |= QUEUE_ATTR_COPY_RANGE;
159}
160
161int nfnl_queue_test_copy_range(const struct nfnl_queue *queue)
162{
163 return !!(queue->ce_mask & QUEUE_ATTR_COPY_RANGE);
164}
165
166uint32_t nfnl_queue_get_copy_range(const struct nfnl_queue *queue)
167{
168 return queue->queue_copy_range;
169}
170
171static uint64_t nfnl_queue_compare(struct nl_object *_a, struct nl_object *_b,
172 uint64_t attrs, int flags)
173{
174 struct nfnl_queue *a = (struct nfnl_queue *) _a;
175 struct nfnl_queue *b = (struct nfnl_queue *) _b;
176 uint64_t diff = 0;
177
178#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
179#define _DIFF_VAL(ATTR, FIELD) _DIFF(ATTR, a->FIELD != b->FIELD)
180 diff |= _DIFF_VAL(QUEUE_ATTR_GROUP, queue_group);
181 diff |= _DIFF_VAL(QUEUE_ATTR_MAXLEN, queue_maxlen);
182 diff |= _DIFF_VAL(QUEUE_ATTR_COPY_MODE, queue_copy_mode);
183 diff |= _DIFF_VAL(QUEUE_ATTR_COPY_RANGE, queue_copy_range);
184#undef _DIFF
185#undef _DIFF_VAL
186
187 return diff;
188}
189
190static const struct trans_tbl nfnl_queue_attrs[] = {
191 __ADD(QUEUE_ATTR_GROUP, group),
192 __ADD(QUEUE_ATTR_MAXLEN, maxlen),
193 __ADD(QUEUE_ATTR_COPY_MODE, copy_mode),
194 __ADD(QUEUE_ATTR_COPY_RANGE, copy_range),
195};
196
197static char *nfnl_queue_attrs2str(int attrs, char *buf, size_t len)
198{
199 return __flags2str(attrs, buf, len, nfnl_queue_attrs,
200 ARRAY_SIZE(nfnl_queue_attrs));
201}
202
203/** @} */
204
205struct nl_object_ops queue_obj_ops = {
206 .oo_name = "netfilter/queue",
207 .oo_size = sizeof(struct nfnl_queue),
208 .oo_dump = {
209 [NL_DUMP_LINE] = nfnl_queue_dump,
210 [NL_DUMP_DETAILS] = nfnl_queue_dump,
211 [NL_DUMP_STATS] = nfnl_queue_dump,
212 },
213 .oo_compare = nfnl_queue_compare,
214 .oo_attrs2str = nfnl_queue_attrs2str,
215 .oo_id_attrs = QUEUE_ATTR_GROUP,
216};
217
218/** @} */
nl_object_put
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
nl_object_get
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
nl_object_alloc
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
nl_new_line
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
Definition utils.c:966
NL_DUMP_STATS
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32