libnftnl  1.0.8
queue.c
1 /*
2  * (C) 2013 by Eric Leblond <eric@regit.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <arpa/inet.h>
15 #include <errno.h>
16 #include <linux/netfilter/nf_tables.h>
17 
18 #include "internal.h"
19 #include <libmnl/libmnl.h>
20 #include <libnftnl/expr.h>
21 #include <libnftnl/rule.h>
22 
24  enum nft_registers sreg_qnum;
25  uint16_t queuenum;
26  uint16_t queues_total;
27  uint16_t flags;
28 };
29 
30 static int nftnl_expr_queue_set(struct nftnl_expr *e, uint16_t type,
31  const void *data, uint32_t data_len)
32 {
33  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
34 
35  switch(type) {
36  case NFTNL_EXPR_QUEUE_NUM:
37  queue->queuenum = *((uint16_t *)data);
38  break;
39  case NFTNL_EXPR_QUEUE_TOTAL:
40  queue->queues_total = *((uint16_t *)data);
41  break;
42  case NFTNL_EXPR_QUEUE_FLAGS:
43  queue->flags = *((uint16_t *)data);
44  break;
45  case NFTNL_EXPR_QUEUE_SREG_QNUM:
46  queue->sreg_qnum = *((uint32_t *)data);
47  break;
48  default:
49  return -1;
50  }
51  return 0;
52 }
53 
54 static const void *
55 nftnl_expr_queue_get(const struct nftnl_expr *e, uint16_t type,
56  uint32_t *data_len)
57 {
58  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
59 
60  switch(type) {
61  case NFTNL_EXPR_QUEUE_NUM:
62  *data_len = sizeof(queue->queuenum);
63  return &queue->queuenum;
64  case NFTNL_EXPR_QUEUE_TOTAL:
65  *data_len = sizeof(queue->queues_total);
66  return &queue->queues_total;
67  case NFTNL_EXPR_QUEUE_FLAGS:
68  *data_len = sizeof(queue->flags);
69  return &queue->flags;
70  case NFTNL_EXPR_QUEUE_SREG_QNUM:
71  *data_len = sizeof(queue->sreg_qnum);
72  return &queue->sreg_qnum;
73  }
74  return NULL;
75 }
76 
77 static int nftnl_expr_queue_cb(const struct nlattr *attr, void *data)
78 {
79  const struct nlattr **tb = data;
80  int type = mnl_attr_get_type(attr);
81 
82  if (mnl_attr_type_valid(attr, NFTA_QUEUE_MAX) < 0)
83  return MNL_CB_OK;
84 
85  switch(type) {
86  case NFTA_QUEUE_NUM:
87  case NFTA_QUEUE_TOTAL:
88  case NFTA_QUEUE_FLAGS:
89  if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
90  abi_breakage();
91  break;
92  case NFTA_QUEUE_SREG_QNUM:
93  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
94  abi_breakage();
95  break;
96  }
97 
98  tb[type] = attr;
99  return MNL_CB_OK;
100 }
101 
102 static void
103 nftnl_expr_queue_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
104 {
105  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
106 
107  if (e->flags & (1 << NFTNL_EXPR_QUEUE_NUM))
108  mnl_attr_put_u16(nlh, NFTA_QUEUE_NUM, htons(queue->queuenum));
109  if (e->flags & (1 << NFTNL_EXPR_QUEUE_TOTAL))
110  mnl_attr_put_u16(nlh, NFTA_QUEUE_TOTAL, htons(queue->queues_total));
111  if (e->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS))
112  mnl_attr_put_u16(nlh, NFTA_QUEUE_FLAGS, htons(queue->flags));
113  if (e->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM))
114  mnl_attr_put_u32(nlh, NFTA_QUEUE_SREG_QNUM, htonl(queue->sreg_qnum));
115 }
116 
117 static int
118 nftnl_expr_queue_parse(struct nftnl_expr *e, struct nlattr *attr)
119 {
120  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
121  struct nlattr *tb[NFTA_QUEUE_MAX+1] = {};
122 
123  if (mnl_attr_parse_nested(attr, nftnl_expr_queue_cb, tb) < 0)
124  return -1;
125 
126  if (tb[NFTA_QUEUE_NUM]) {
127  queue->queuenum = ntohs(mnl_attr_get_u16(tb[NFTA_QUEUE_NUM]));
128  e->flags |= (1 << NFTNL_EXPR_QUEUE_NUM);
129  }
130  if (tb[NFTA_QUEUE_TOTAL]) {
131  queue->queues_total = ntohs(mnl_attr_get_u16(tb[NFTA_QUEUE_TOTAL]));
132  e->flags |= (1 << NFTNL_EXPR_QUEUE_TOTAL);
133  }
134  if (tb[NFTA_QUEUE_FLAGS]) {
135  queue->flags = ntohs(mnl_attr_get_u16(tb[NFTA_QUEUE_FLAGS]));
136  e->flags |= (1 << NFTNL_EXPR_QUEUE_FLAGS);
137  }
138  if (tb[NFTA_QUEUE_SREG_QNUM]) {
139  queue->sreg_qnum = ntohl(mnl_attr_get_u32(tb[NFTA_QUEUE_SREG_QNUM]));
140  e->flags |= (1 << NFTNL_EXPR_QUEUE_SREG_QNUM);
141  }
142 
143  return 0;
144 }
145 
146 static int
147 nftnl_expr_queue_json_parse(struct nftnl_expr *e, json_t *root,
148  struct nftnl_parse_err *err)
149 {
150 #ifdef JSON_PARSING
151  uint32_t sreg_qnum;
152  uint16_t type;
153  uint16_t code;
154 
155  if (nftnl_jansson_parse_val(root, "num", NFTNL_TYPE_U16, &type, err) == 0)
156  nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_NUM, type);
157 
158  if (nftnl_jansson_parse_val(root, "total", NFTNL_TYPE_U16, &code, err) == 0)
159  nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_TOTAL, code);
160 
161  if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U16, &code, err) == 0)
162  nftnl_expr_set_u16(e, NFTNL_EXPR_QUEUE_FLAGS, code);
163 
164  if (nftnl_jansson_parse_val(root, "sreg_qnum", NFTNL_TYPE_U32, &sreg_qnum,
165  err) == 0)
166  nftnl_expr_set_u32(e, NFTNL_EXPR_QUEUE_SREG_QNUM, sreg_qnum);
167 
168  return 0;
169 #else
170  errno = EOPNOTSUPP;
171  return -1;
172 #endif
173 }
174 
175 static int nftnl_expr_queue_snprintf_default(char *buf, size_t len,
176  const struct nftnl_expr *e)
177 {
178  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
179  int ret, remain = len, offset = 0;
180  uint16_t total_queues;
181 
182  if (e->flags & (1 << NFTNL_EXPR_QUEUE_NUM)) {
183  total_queues = queue->queuenum + queue->queues_total - 1;
184 
185  ret = snprintf(buf + offset, len, "num %u", queue->queuenum);
186  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
187 
188  if (queue->queues_total && total_queues != queue->queuenum) {
189  ret = snprintf(buf + offset, len, "-%u", total_queues);
190  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
191  }
192 
193  ret = snprintf(buf + offset, len, " ");
194  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
195  }
196 
197  if (e->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM)) {
198  ret = snprintf(buf + offset, len, "sreg_qnum %u ",
199  queue->sreg_qnum);
200  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
201  }
202 
203  if (e->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS)) {
204  if (queue->flags & (NFT_QUEUE_FLAG_BYPASS)) {
205  ret = snprintf(buf + offset, len, "bypass ");
206  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
207  }
208  if (queue->flags & (NFT_QUEUE_FLAG_CPU_FANOUT)) {
209  ret = snprintf(buf + offset, len, "fanout ");
210  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
211  }
212  }
213  return offset;
214 }
215 
216 static int nftnl_expr_queue_export(char *buf, size_t size,
217  const struct nftnl_expr *e, int type)
218 {
219  struct nftnl_expr_queue *queue = nftnl_expr_data(e);
220  NFTNL_BUF_INIT(b, buf, size);
221 
222  if (e->flags & (1 << NFTNL_EXPR_QUEUE_NUM))
223  nftnl_buf_u32(&b, type, queue->queuenum, NUM);
224  if (e->flags & (1 << NFTNL_EXPR_QUEUE_TOTAL))
225  nftnl_buf_u32(&b, type, queue->queues_total, TOTAL);
226  if (e->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS))
227  nftnl_buf_u32(&b, type, queue->flags, FLAGS);
228  if (e->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM))
229  nftnl_buf_u32(&b, type, queue->sreg_qnum, SREG_QNUM);
230 
231  return nftnl_buf_done(&b);
232 }
233 
234 static int
235 nftnl_expr_queue_snprintf(char *buf, size_t len, uint32_t type,
236  uint32_t flags, const struct nftnl_expr *e)
237 {
238  switch (type) {
239  case NFTNL_OUTPUT_DEFAULT:
240  return nftnl_expr_queue_snprintf_default(buf, len, e);
241  case NFTNL_OUTPUT_XML:
242  case NFTNL_OUTPUT_JSON:
243  return nftnl_expr_queue_export(buf, len, e, type);
244  default:
245  break;
246  }
247  return -1;
248 }
249 
250 static bool nftnl_expr_queue_cmp(const struct nftnl_expr *e1,
251  const struct nftnl_expr *e2)
252 {
253  struct nftnl_expr_queue *q1 = nftnl_expr_data(e1);
254  struct nftnl_expr_queue *q2 = nftnl_expr_data(e2);
255  bool eq = true;
256 
257  if (e1->flags & (1 << NFTNL_EXPR_QUEUE_NUM))
258  eq &= (q1->queuenum == q2->queuenum);
259  if (e1->flags & (1 << NFTNL_EXPR_QUEUE_TOTAL))
260  eq &= (q1->queues_total == q2->queues_total);
261  if (e1->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS))
262  eq &= (q1->flags == q2->flags);
263  if (e1->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM))
264  eq &= (q1->sreg_qnum == q2->sreg_qnum);
265 
266  return eq;
267 }
268 
269 struct expr_ops expr_ops_queue = {
270  .name = "queue",
271  .alloc_len = sizeof(struct nftnl_expr_queue),
272  .max_attr = NFTA_QUEUE_MAX,
273  .cmp = nftnl_expr_queue_cmp,
274  .set = nftnl_expr_queue_set,
275  .get = nftnl_expr_queue_get,
276  .parse = nftnl_expr_queue_parse,
277  .build = nftnl_expr_queue_build,
278  .snprintf = nftnl_expr_queue_snprintf,
279  .json_parse = nftnl_expr_queue_json_parse,
280 };