-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathif.c
383 lines (337 loc) · 8.87 KB
/
if.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Tempesta DB
*
* User-space communication routines.
*
* Copyright (C) 2015-2022 Tempesta Technologies, INC.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/ctype.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#include "htrie.h"
#include "table.h"
#include "tdb_if.h"
#include "lib/hash.h"
static struct sock *nls;
static DEFINE_MUTEX(tdb_if_mtx);
#define TDB_NLMSG_MAXSZ (NL_FR_SZ / 2 - NLMSG_HDRLEN - sizeof(TdbMsg) \
- sizeof(TdbMsgRec))
static int
tdb_if_info(struct sk_buff *skb, struct netlink_callback *cb)
{
TdbMsg *m;
struct nlmsghdr *nlh;
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
cb->nlh->nlmsg_type, TDB_NLMSG_MAXSZ, 0);
if (!nlh)
return -EMSGSIZE;
m = nlmsg_data(nlh);
/* Fill in response. */
memcpy(m, cb->data, sizeof(*m));
m->type |= TDB_NLF_RESP_OK;
m->rec_n = 1;
m->recs[0].klen = 0;
m->recs[0].dlen = tdb_info(m->recs[0].data, TDB_NLMSG_MAXSZ);
if (m->recs[0].dlen <= 0) {
nlmsg_cancel(skb, nlh);
return m->recs[0].dlen;
}
return 0; /* end transfer */
}
static int
tdb_if_open_close(struct sk_buff *skb, struct netlink_callback *cb)
{
TdbMsg *resp_m, *m = cb->data;
TdbCrTblRec *ct = (TdbCrTblRec *)(m->recs + 1);
struct nlmsghdr *nlh;
/* Create status response. */
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
cb->nlh->nlmsg_type, sizeof(TdbMsg), 0);
if (!nlh)
return -EMSGSIZE;
resp_m = nlmsg_data(nlh);
resp_m->rec_n = 0;
if (m->type == TDB_MSG_OPEN) {
resp_m->type = TDB_MSG_OPEN;
if (tdb_open(ct->path, ct->tbl_size, ct->rec_size, numa_node_id()))
resp_m->type |= TDB_NLF_RESP_OK;
} else {
TDB *db;
resp_m->type = TDB_MSG_CLOSE;
db = tdb_tbl_lookup(m->t_name, TDB_TBLNAME_LEN);
if (db) {
tdb_put(db);
tdb_close(db);
resp_m->type |= TDB_NLF_RESP_OK;
} else {
TDB_WARN("Tried to close non existent table '%s'\n",
m->t_name);
}
}
return 0;
}
static int
tdb_if_insert(struct sk_buff *skb, struct netlink_callback *cb)
{
unsigned int i, off;
unsigned long key, len;
TdbMsg *resp_m, *m = cb->data;
TdbMsgRec *r;
TdbVRec *vr;
TdbFRec *fr;
struct nlmsghdr *nlh;
TDB *db;
/* Create status response. */
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
cb->nlh->nlmsg_type, sizeof(TdbMsg), 0);
if (!nlh)
return -EMSGSIZE;
resp_m = nlmsg_data(nlh);
resp_m->rec_n = 0;
resp_m->type = TDB_MSG_INSERT;
db = tdb_tbl_lookup(m->t_name, TDB_TBLNAME_LEN);
if (!db) {
TDB_WARN("Tried to insert into non existent table '%s'\n",
m->t_name);
return 0;
}
if (TDB_HTRIE_VARLENRECS(db->hdr)) {
for (i = 0, off = 0; i < m->rec_n; ++i) {
r = (TdbMsgRec *)((char *)m->recs + off);
key = hash_calc(r->data, r->klen);
len = TDB_MSGREC_LEN(r);
vr = (TdbVRec *)tdb_entry_create(db, key, r, &len);
if (!vr) {
TDB_ERR("Cannot create variable-size record\n");
break;
}
for ( ; len < TDB_MSGREC_LEN(r); ) {
vr = tdb_entry_add(db, vr,
TDB_MSGREC_LEN(r) - len);
if (!vr) {
TDB_ERR("Cannot extend variable-size"
" record\n");
break;
}
memcpy(vr + 1, r->data + len, vr->len);
len += vr->len;
}
off += TDB_MSGREC_LEN(r);
}
} else {
for (i = 0, off = 0; i < m->rec_n; ++i) {
r = (TdbMsgRec *)((char *)m->recs + off);
key = hash_calc(r->data, r->klen);
len = TDB_MSGREC_LEN(r);
fr = (TdbFRec *)tdb_entry_create(db, key, r, &len);
if (!fr || len != r->dlen) {
TDB_ERR("Cannot create fixed-size record\n");
break;
}
off += TDB_MSGREC_LEN(r);
}
}
tdb_put(db);
if (i == m->rec_n)
resp_m->type |= TDB_NLF_RESP_OK;
resp_m->rec_n = i;
return 0;
}
static int
tdb_if_select(struct sk_buff *skb, struct netlink_callback *cb)
{
TdbIter iter;
unsigned long key;
TdbMsg *resp_m, *m = cb->data;
TdbRec *res;
struct nlmsghdr *nlh;
TDB *db;
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
cb->nlh->nlmsg_type, TDB_NLMSG_MAXSZ, 0);
if (!nlh)
return -EMSGSIZE;
resp_m = nlmsg_data(nlh);
resp_m->rec_n = 0;
resp_m->type = TDB_MSG_SELECT;
db = tdb_tbl_lookup(m->t_name, TDB_TBLNAME_LEN);
if (!db) {
TDB_WARN("Tried to select from non existent table '%s'\n",
m->t_name);
return 0;
}
/*
* TODO implement select of all records:
* 1. full HTrie iterator is required;
* 2. use many netlink frames to send probably large data set.
*/
key = hash_calc(m->recs[0].data, m->recs[0].klen);
iter = tdb_rec_get(db, key);
res = iter.rec;
if (res) {
resp_m->rec_n = 1;
if (TDB_HTRIE_VARLENRECS(db->hdr)) {
TdbVRec *vr = (TdbVRec *)res;
size_t off = vr->len;
memcpy(resp_m->recs, vr->data, vr->len);
while (1) {
size_t n = vr->len;
if (n + off > TDB_NLMSG_MAXSZ) {
n = TDB_NLMSG_MAXSZ - off;
resp_m->type |= TDB_NLF_RESP_TRUNC;
}
memcpy((char *)resp_m->recs + off, vr->data, n);
if (n < vr->len || !vr->chunk_next)
break;
vr = TDB_PTR(db->hdr, TDB_DI2O(vr->chunk_next));
off += n;
}
}
else {
memcpy(resp_m->recs, res->data, db->hdr->rec_len);
}
tdb_rec_put(res);
}
tdb_put(db);
/* Only one record is fetched for now. */
resp_m->type |= TDB_NLF_RESP_OK | TDB_NLF_RESP_END;
return 0;
}
static const struct {
int (*dump)(struct sk_buff *, struct netlink_callback *);
} tdb_if_call_tbl[__TDB_MSG_TYPE_MAX] = {
[TDB_MSG_INFO - __TDB_MSG_BASE] = { .dump = tdb_if_info },
[TDB_MSG_OPEN - __TDB_MSG_BASE] = { .dump = tdb_if_open_close },
[TDB_MSG_CLOSE - __TDB_MSG_BASE] = { .dump = tdb_if_open_close },
[TDB_MSG_INSERT - __TDB_MSG_BASE] = { .dump = tdb_if_insert },
[TDB_MSG_SELECT - __TDB_MSG_BASE] = { .dump = tdb_if_select },
};
static int
tdb_if_check_tblname(const TdbMsg *m)
{
int i, ret;
for (i = 0; m->t_name[i] && i < TDB_TBLNAME_LEN; ++i)
if (!isalnum(m->t_name[i]))
return false;
ret = !m->t_name[i];
if (!ret)
TDB_ERR("Bad table name %.*s\n", i, m->t_name);
return ret;
}
static int
tdb_if_proc_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
TdbMsg *m;
if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(TdbMsg)) {
TDB_ERR("too short netlink msg\n");
return -EINVAL;
}
m = nlmsg_data(nlh);
/* Check the message type and do consistency checking for each type. */
switch (m->type) {
case TDB_MSG_INFO:
if (m->rec_n || m->t_name[0] != '*' || m->t_name[1] != 0) {
TDB_ERR("Bad info netlink msg: rec_n=%u t_name=%x%x\n",
m->rec_n, m->t_name[0], m->t_name[1]);
return -EINVAL;
}
break;
case TDB_MSG_OPEN:
if (m->rec_n != 1) {
TDB_ERR("empty create table msg\n");
return -EINVAL;
}
if (!tdb_if_check_tblname(m))
return -EINVAL;
if (m->recs[0].dlen < sizeof(TdbCrTblRec)) {
TDB_ERR("empty record in create table msg\n");
return -EINVAL;
}
{
TdbCrTblRec *ct = (TdbCrTblRec *)(m->recs + 1);
if (!ct->tbl_size || ct->path_len < sizeof(TDB_SUFFIX))
{
TDB_ERR("malformed create table msg:"
" tbl_size=%lu path_len=%u\n",
ct->tbl_size, ct->path_len);
return -EINVAL;
}
}
break;
case TDB_MSG_CLOSE:
if (m->rec_n) {
TDB_ERR("Bad close table msg: rec_n=%u\n", m->rec_n);
return -EINVAL;
}
if (!tdb_if_check_tblname(m))
return -EINVAL;
break;
case TDB_MSG_INSERT:
if (m->rec_n < 1) {
TDB_ERR("empty insert msg\n");
return -EINVAL;
}
if (!tdb_if_check_tblname(m))
return -EINVAL;
break;
case TDB_MSG_SELECT:
if (m->rec_n != 1) {
TDB_ERR("empty select msg\n");
return -EINVAL;
}
if (!tdb_if_check_tblname(m))
return -EINVAL;
break;
default:
TDB_ERR("bad netlink msg type %u\n", m->type);
return -EINVAL;
}
{
struct netlink_dump_control c = {
.dump = tdb_if_call_tbl[m->type - __TDB_MSG_BASE].dump,
.data = m,
.min_dump_alloc = NL_FR_SZ / 2,
};
return netlink_dump_start(nls, skb, nlh, &c);
}
}
static void
tdb_if_rcv(struct sk_buff *skb)
{
/* TODO remove the mutex for concurrent user-space updates. */
mutex_lock(&tdb_if_mtx);
netlink_rcv_skb(skb, &tdb_if_proc_msg);
mutex_unlock(&tdb_if_mtx);
}
static struct netlink_kernel_cfg tdb_if_nlcfg = {
.input = tdb_if_rcv,
};
int __init
tdb_if_init(void)
{
nls = netlink_kernel_create(&init_net, NETLINK_TEMPESTA, &tdb_if_nlcfg);
if (!nls) {
TDB_ERR("Failed to create netlink socket\n");
return -ENOMEM;
}
return 0;
}
void __exit
tdb_if_exit(void)
{
netlink_kernel_release(nls);
}