forked from linux-netdev/ynl-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfou-user.cpp
More file actions
289 lines (251 loc) · 8.97 KB
/
fou-user.cpp
File metadata and controls
289 lines (251 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
/* Do not edit directly, auto-generated from: */
/* */
/* YNL-GEN user source */
#include "fou-user.hpp"
#include <array>
#include <linux/fou.h>
#include <linux/genetlink.h>
namespace ynl_cpp {
/* Enums */
static constexpr std::array<std::string_view, FOU_CMD_GET + 1> fou_op_strmap = []() {
std::array<std::string_view, FOU_CMD_GET + 1> arr{};
arr[FOU_CMD_ADD] = "add";
arr[FOU_CMD_DEL] = "del";
arr[FOU_CMD_GET] = "get";
return arr;
} ();
std::string_view fou_op_str(int op)
{
if (op < 0 || op >= (int)(fou_op_strmap.size()))
return "";
return fou_op_strmap[op];
}
static constexpr std::array<std::string_view, 2 + 1> fou_encap_type_strmap = []() {
std::array<std::string_view, 2 + 1> arr{};
arr[0] = "unspec";
arr[1] = "direct";
arr[2] = "gue";
return arr;
} ();
std::string_view fou_encap_type_str(int value)
{
if (value < 0 || value >= (int)(fou_encap_type_strmap.size()))
return "";
return fou_encap_type_strmap[value];
}
/* Policies */
static std::array<ynl_policy_attr,FOU_ATTR_MAX + 1> fou_policy = []() {
std::array<ynl_policy_attr,FOU_ATTR_MAX + 1> arr{};
arr[FOU_ATTR_UNSPEC].name = "unspec";
arr[FOU_ATTR_UNSPEC].type = YNL_PT_REJECT;
arr[FOU_ATTR_PORT].name = "port";
arr[FOU_ATTR_PORT].type = YNL_PT_U16;
arr[FOU_ATTR_AF].name = "af";
arr[FOU_ATTR_AF].type = YNL_PT_U8;
arr[FOU_ATTR_IPPROTO].name = "ipproto";
arr[FOU_ATTR_IPPROTO].type = YNL_PT_U8;
arr[FOU_ATTR_TYPE].name = "type";
arr[FOU_ATTR_TYPE].type = YNL_PT_U8;
arr[FOU_ATTR_REMCSUM_NOPARTIAL].name = "remcsum-nopartial";
arr[FOU_ATTR_REMCSUM_NOPARTIAL].type = YNL_PT_FLAG;
arr[FOU_ATTR_LOCAL_V4].name = "local-v4";
arr[FOU_ATTR_LOCAL_V4].type = YNL_PT_U32;
arr[FOU_ATTR_LOCAL_V6].name = "local-v6";
arr[FOU_ATTR_LOCAL_V6].type = YNL_PT_BINARY;
arr[FOU_ATTR_PEER_V4].name = "peer-v4";
arr[FOU_ATTR_PEER_V4].type = YNL_PT_U32;
arr[FOU_ATTR_PEER_V6].name = "peer-v6";
arr[FOU_ATTR_PEER_V6].type = YNL_PT_BINARY;
arr[FOU_ATTR_PEER_PORT].name = "peer-port";
arr[FOU_ATTR_PEER_PORT].type = YNL_PT_U16;
arr[FOU_ATTR_IFINDEX].name = "ifindex";
arr[FOU_ATTR_IFINDEX].type = YNL_PT_U32;
return arr;
} ();
struct ynl_policy_nest fou_nest = {
.max_attr = static_cast<unsigned int>(FOU_ATTR_MAX),
.table = fou_policy.data(),
};
/* Common nested types */
/* ============== FOU_CMD_ADD ============== */
/* FOU_CMD_ADD - do */
int fou_add(ynl_cpp::ynl_socket& ys, fou_add_req& req)
{
struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
struct nlmsghdr *nlh;
int err;
nlh = ynl_gemsg_start_req(ys, ((struct ynl_sock*)ys)->family_id, FOU_CMD_ADD, 1);
((struct ynl_sock*)ys)->req_policy = &fou_nest;
if (req.port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PORT, req.port.value());
if (req.ipproto.has_value())
ynl_attr_put_u8(nlh, FOU_ATTR_IPPROTO, req.ipproto.value());
if (req.type.has_value())
ynl_attr_put_u8(nlh, FOU_ATTR_TYPE, req.type.value());
if (req.remcsum_nopartial)
ynl_attr_put(nlh, FOU_ATTR_REMCSUM_NOPARTIAL, NULL, 0);
if (req.local_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_LOCAL_V4, req.local_v4.value());
if (req.peer_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_PEER_V4, req.peer_v4.value());
if (req.local_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_LOCAL_V6, req.local_v6.data(), req.local_v6.size());
if (req.peer_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_PEER_V6, req.peer_v6.data(), req.peer_v6.size());
if (req.peer_port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PEER_PORT, req.peer_port.value());
if (req.ifindex.has_value())
ynl_attr_put_s32(nlh, FOU_ATTR_IFINDEX, req.ifindex.value());
err = ynl_exec(ys, nlh, &yrs);
if (err < 0)
return -1;
return 0;
}
/* ============== FOU_CMD_DEL ============== */
/* FOU_CMD_DEL - do */
int fou_del(ynl_cpp::ynl_socket& ys, fou_del_req& req)
{
struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
struct nlmsghdr *nlh;
int err;
nlh = ynl_gemsg_start_req(ys, ((struct ynl_sock*)ys)->family_id, FOU_CMD_DEL, 1);
((struct ynl_sock*)ys)->req_policy = &fou_nest;
if (req.af.has_value())
ynl_attr_put_u8(nlh, FOU_ATTR_AF, req.af.value());
if (req.ifindex.has_value())
ynl_attr_put_s32(nlh, FOU_ATTR_IFINDEX, req.ifindex.value());
if (req.port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PORT, req.port.value());
if (req.peer_port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PEER_PORT, req.peer_port.value());
if (req.local_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_LOCAL_V4, req.local_v4.value());
if (req.peer_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_PEER_V4, req.peer_v4.value());
if (req.local_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_LOCAL_V6, req.local_v6.data(), req.local_v6.size());
if (req.peer_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_PEER_V6, req.peer_v6.data(), req.peer_v6.size());
err = ynl_exec(ys, nlh, &yrs);
if (err < 0)
return -1;
return 0;
}
/* ============== FOU_CMD_GET ============== */
/* FOU_CMD_GET - do */
int fou_get_rsp_parse(const struct nlmsghdr *nlh, struct ynl_parse_arg *yarg)
{
const struct nlattr *attr;
fou_get_rsp *dst;
dst = (fou_get_rsp*)yarg->data;
ynl_attr_for_each(attr, nlh, yarg->ys->family->hdr_len) {
unsigned int type = ynl_attr_type(attr);
if (type == FOU_ATTR_PORT) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->port = (__u16)ynl_attr_get_u16(attr);
} else if (type == FOU_ATTR_IPPROTO) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->ipproto = (__u8)ynl_attr_get_u8(attr);
} else if (type == FOU_ATTR_TYPE) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->type = (__u8)ynl_attr_get_u8(attr);
} else if (type == FOU_ATTR_REMCSUM_NOPARTIAL) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
} else if (type == FOU_ATTR_LOCAL_V4) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->local_v4 = (__u32)ynl_attr_get_u32(attr);
} else if (type == FOU_ATTR_PEER_V4) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->peer_v4 = (__u32)ynl_attr_get_u32(attr);
} else if (type == FOU_ATTR_LOCAL_V6) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
unsigned int len = ynl_attr_data_len(attr);
__u8 *data = (__u8*)ynl_attr_data(attr);
dst->local_v6.assign(data, data + len);
} else if (type == FOU_ATTR_PEER_V6) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
unsigned int len = ynl_attr_data_len(attr);
__u8 *data = (__u8*)ynl_attr_data(attr);
dst->peer_v6.assign(data, data + len);
} else if (type == FOU_ATTR_PEER_PORT) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->peer_port = (__u16)ynl_attr_get_u16(attr);
} else if (type == FOU_ATTR_IFINDEX) {
if (ynl_attr_validate(yarg, attr))
return YNL_PARSE_CB_ERROR;
dst->ifindex = (__s32)ynl_attr_get_s32(attr);
}
}
return YNL_PARSE_CB_OK;
}
std::unique_ptr<fou_get_rsp> fou_get(ynl_cpp::ynl_socket& ys, fou_get_req& req)
{
struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
std::unique_ptr<fou_get_rsp> rsp;
struct nlmsghdr *nlh;
int err;
nlh = ynl_gemsg_start_req(ys, ((struct ynl_sock*)ys)->family_id, FOU_CMD_GET, 1);
((struct ynl_sock*)ys)->req_policy = &fou_nest;
yrs.yarg.rsp_policy = &fou_nest;
if (req.af.has_value())
ynl_attr_put_u8(nlh, FOU_ATTR_AF, req.af.value());
if (req.ifindex.has_value())
ynl_attr_put_s32(nlh, FOU_ATTR_IFINDEX, req.ifindex.value());
if (req.port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PORT, req.port.value());
if (req.peer_port.has_value())
ynl_attr_put_u16(nlh, FOU_ATTR_PEER_PORT, req.peer_port.value());
if (req.local_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_LOCAL_V4, req.local_v4.value());
if (req.peer_v4.has_value())
ynl_attr_put_u32(nlh, FOU_ATTR_PEER_V4, req.peer_v4.value());
if (req.local_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_LOCAL_V6, req.local_v6.data(), req.local_v6.size());
if (req.peer_v6.size() > 0)
ynl_attr_put(nlh, FOU_ATTR_PEER_V6, req.peer_v6.data(), req.peer_v6.size());
rsp.reset(new fou_get_rsp());
yrs.yarg.data = rsp.get();
yrs.cb = fou_get_rsp_parse;
yrs.rsp_cmd = FOU_CMD_GET;
err = ynl_exec(ys, nlh, &yrs);
if (err < 0)
return nullptr;
return rsp;
}
/* FOU_CMD_GET - dump */
std::unique_ptr<fou_get_list> fou_get_dump(ynl_cpp::ynl_socket& ys)
{
struct ynl_dump_no_alloc_state yds = {};
struct nlmsghdr *nlh;
int err;
auto ret = std::make_unique<fou_get_list>();
yds.yarg.ys = ys;
yds.yarg.rsp_policy = &fou_nest;
yds.yarg.data = ret.get();
yds.alloc_cb = [](void* arg)->void* {return &(static_cast<fou_get_list*>(arg)->objs.emplace_back());};
yds.cb = fou_get_rsp_parse;
yds.rsp_cmd = FOU_CMD_GET;
nlh = ynl_gemsg_start_dump(ys, ((struct ynl_sock*)ys)->family_id, FOU_CMD_GET, 1);
err = ynl_exec_dump_no_alloc(ys, nlh, &yds);
if (err < 0)
return nullptr;
return ret;
}
const struct ynl_family ynl_fou_family = {
.name = "fou",
.hdr_len = sizeof(struct genlmsghdr),
};
const struct ynl_family& get_ynl_fou_family() {
return ynl_fou_family;
};
} //namespace ynl_cpp