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