libnftnl 1.2.6
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
30static 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 memcpy(&queue->queuenum, data, sizeof(queue->queuenum));
38 break;
39 case NFTNL_EXPR_QUEUE_TOTAL:
40 memcpy(&queue->queues_total, data, sizeof(queue->queues_total));
41 break;
42 case NFTNL_EXPR_QUEUE_FLAGS:
43 memcpy(&queue->flags, data, sizeof(queue->flags));
44 break;
45 case NFTNL_EXPR_QUEUE_SREG_QNUM:
46 memcpy(&queue->sreg_qnum, data, sizeof(queue->sreg_qnum));
47 break;
48 default:
49 return -1;
50 }
51 return 0;
52}
53
54static const void *
55nftnl_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
77static 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
102static void
103nftnl_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
117static int
118nftnl_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
146static int
147nftnl_expr_queue_snprintf(char *buf, size_t remain,
148 uint32_t flags, const struct nftnl_expr *e)
149{
150 struct nftnl_expr_queue *queue = nftnl_expr_data(e);
151 uint16_t total_queues;
152 int ret, offset = 0;
153
154 if (e->flags & (1 << NFTNL_EXPR_QUEUE_NUM)) {
155 total_queues = queue->queuenum + queue->queues_total - 1;
156
157 ret = snprintf(buf + offset, remain, "num %u", queue->queuenum);
158 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
159
160 if (queue->queues_total && total_queues != queue->queuenum) {
161 ret = snprintf(buf + offset, remain, "-%u", total_queues);
162 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
163 }
164
165 ret = snprintf(buf + offset, remain, " ");
166 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
167 }
168
169 if (e->flags & (1 << NFTNL_EXPR_QUEUE_SREG_QNUM)) {
170 ret = snprintf(buf + offset, remain, "sreg_qnum %u ",
171 queue->sreg_qnum);
172 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
173 }
174
175 if (e->flags & (1 << NFTNL_EXPR_QUEUE_FLAGS)) {
176 if (queue->flags & (NFT_QUEUE_FLAG_BYPASS)) {
177 ret = snprintf(buf + offset, remain, "bypass ");
178 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
179 }
180 if (queue->flags & (NFT_QUEUE_FLAG_CPU_FANOUT)) {
181 ret = snprintf(buf + offset, remain, "fanout ");
182 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
183 }
184 }
185 return offset;
186}
187
188struct expr_ops expr_ops_queue = {
189 .name = "queue",
190 .alloc_len = sizeof(struct nftnl_expr_queue),
191 .max_attr = NFTA_QUEUE_MAX,
192 .set = nftnl_expr_queue_set,
193 .get = nftnl_expr_queue_get,
194 .parse = nftnl_expr_queue_parse,
195 .build = nftnl_expr_queue_build,
196 .output = nftnl_expr_queue_snprintf,
197};