This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
inet.c
369 lines (325 loc) · 9.96 KB
/
inet.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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* 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.
*
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <netinet/in.h>
#include "inet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "route.h"
#include "route6.h"
#include "neigh.h"
#include "icmp.h"
#include "icmp6.h"
#include "inetaddr.h"
#include "ipset.h"
#define INET
#define RTE_LOGTYPE_INET RTE_LOGTYPE_USER1
static struct list_head inet_hooks[INET_HOOK_NUMHOOKS];
static rte_rwlock_t inet_hook_lock;
static struct list_head inet6_hooks[INET_HOOK_NUMHOOKS];
static rte_rwlock_t inet6_hook_lock;
static inline struct list_head *af_inet_hooks(int af, size_t num)
{
assert((af == AF_INET || af == AF_INET6) && num < INET_HOOK_NUMHOOKS);
if (af == AF_INET)
return &inet_hooks[num];
else
return &inet6_hooks[num];
}
static inline rte_rwlock_t *af_inet_hook_lock(int af)
{
assert(af == AF_INET || af == AF_INET6);
if (af == AF_INET)
return &inet_hook_lock;
else
return &inet6_hook_lock;
}
static int inet_hook_init(void)
{
int i;
rte_rwlock_init(&inet_hook_lock);
rte_rwlock_write_lock(&inet_hook_lock);
for (i = 0; i < NELEMS(inet_hooks); i++)
INIT_LIST_HEAD(&inet_hooks[i]);
rte_rwlock_write_unlock(&inet_hook_lock);
rte_rwlock_init(&inet6_hook_lock);
rte_rwlock_write_lock(&inet6_hook_lock);
for (i = 0; i < NELEMS(inet6_hooks); i++)
INIT_LIST_HEAD(&inet6_hooks[i]);
rte_rwlock_write_unlock(&inet6_hook_lock);
return EDPVS_OK;
}
int inet_init(void)
{
int err;
if ((err = ipset_init()) != 0)
return err;
if ((err = neigh_init()) != 0)
return err;
if ((err = route_init()) != 0)
return err;
if ((err = route6_init()) != 0)
return err;
if ((err = inet_hook_init()) != 0)
return err;
if ((err = ipv4_init()) != 0)
return err;
if ((err = ipv6_init()) != 0)
return err;
if ((err = icmp_init()) != 0)
return err;
if ((err = icmpv6_init()) != 0)
return err;
if ((err = inet_addr_init()) != 0)
return err;
return EDPVS_OK;
}
int inet_term(void)
{
int err;
if ((err = inet_addr_term()) != 0)
return err;
if ((err = icmpv6_term()) != 0)
return err;
if ((err = icmp_term()) != 0)
return err;
if ((err = ipv6_term()) != 0)
return err;
if ((err = ipv4_term()) != 0)
return err;
if ((err = route6_term()) != 0)
return err;
if ((err = route_term()) != 0)
return err;
if ((err = neigh_term()) != 0)
return err;
if ((err = ipset_term()) != 0)
return err;
return EDPVS_OK;
}
bool inet_addr_equal(int af, const union inet_addr *a1,
const union inet_addr *a2)
{
switch (af) {
case AF_INET:
return a1->in.s_addr == a2->in.s_addr;
case AF_INET6:
return memcmp(a1->in6.s6_addr, a2->in6.s6_addr, 16) == 0;
default:
return memcmp(a1, a2, sizeof(union inet_addr)) == 0;
}
}
bool inet_is_addr_any(int af, const union inet_addr *addr)
{
switch (af) {
case AF_INET:
return addr->in.s_addr == htonl(INADDR_ANY);
case AF_INET6:
return IN6_ARE_ADDR_EQUAL(&addr->in6, &in6addr_any);
default:
return false; /* ? */
}
}
int inet_plen_to_mask(int af, uint8_t plen, union inet_addr *mask)
{
switch (af) {
case AF_INET:
if (plen == 0)
return mask->in.s_addr = 0;
return mask->in.s_addr = htonl(~((1U<<(32-plen))-1));
case AF_INET6:
return EDPVS_NOTSUPP;
default:
return EDPVS_INVAL;
}
}
int inet_addr_net(int af, const union inet_addr *addr,
const union inet_addr *mask,
union inet_addr *net)
{
switch (af) {
case AF_INET:
net->in.s_addr = addr->in.s_addr & mask->in.s_addr;
break;
case AF_INET6:
return EDPVS_NOTSUPP;
default:
return EDPVS_INVAL;
}
return EDPVS_OK;
}
bool inet_addr_same_net(int af, uint8_t plen,
const union inet_addr *addr1,
const union inet_addr *addr2)
{
uint32_t mask;
switch (af) {
case AF_INET:
mask = htonl(~((0x1<<(32-plen)) - 1));
return !((addr1->in.s_addr^addr2->in.s_addr)&mask);
case AF_INET6:
return ipv6_prefix_equal(&addr1->in6, &addr2->in6, plen);
default:
return false;
}
}
static int __inet_register_hooks(struct list_head *head,
struct inet_hook_ops *reg)
{
struct inet_hook_ops *elem;
/* check if exist */
list_for_each_entry(elem, head, list) {
if (elem == reg) {
RTE_LOG(ERR, INET, "%s: hook already exist\n", __func__);
return EDPVS_EXIST; /* error ? */
}
}
list_for_each_entry(elem, head, list) {
if (reg->priority < elem->priority)
break;
}
list_add(®->list, elem->list.prev);
return EDPVS_OK;
}
int INET_HOOK(int af, unsigned int hook, struct rte_mbuf *mbuf,
struct netif_port *in, struct netif_port *out,
int (*okfn)(struct rte_mbuf *mbuf))
{
struct list_head *hook_list;
struct inet_hook_ops *ops;
struct inet_hook_state state;
int verdict = INET_ACCEPT;
state.hook = hook;
hook_list = af_inet_hooks(af, hook);
ops = list_entry(hook_list, struct inet_hook_ops, list);
if (!list_empty(hook_list)) {
verdict = INET_ACCEPT;
list_for_each_entry_continue(ops, hook_list, list) {
repeat:
verdict = ops->hook(ops->priv, mbuf, &state);
if (verdict != INET_ACCEPT) {
if (verdict == INET_REPEAT)
goto repeat;
break;
}
}
}
if (verdict == INET_ACCEPT || verdict == INET_STOP) {
return okfn(mbuf);
} else if (verdict == INET_DROP) {
rte_pktmbuf_free(mbuf);
return EDPVS_DROP;
} else { /* INET_STOLEN */
return EDPVS_OK;
}
}
int inet_register_hooks(struct inet_hook_ops *reg, size_t n)
{
int af;
size_t i, err;
struct list_head *hook_list;
assert(reg);
for (i = 0; i < n; i++) {
af = reg[i].af;
if (reg[i].hooknum >= INET_HOOK_NUMHOOKS || !reg[i].hook) {
err = EDPVS_INVAL;
goto rollback;
}
hook_list = af_inet_hooks(af, reg[i].hooknum);
rte_rwlock_write_lock(af_inet_hook_lock(af));
err = __inet_register_hooks(hook_list, ®[i]);
rte_rwlock_write_unlock(af_inet_hook_lock(af));
if (err != EDPVS_OK)
goto rollback;
}
return EDPVS_OK;
rollback:
inet_unregister_hooks(reg, n);
return err;
}
int inet_unregister_hooks(struct inet_hook_ops *reg, size_t n)
{
int af;
size_t i;
struct inet_hook_ops *elem, *next;
struct list_head *hook_list;
assert(reg);
for (i = 0; i < n; i++) {
af = reg[i].af;
if (reg[i].hooknum >= INET_HOOK_NUMHOOKS) {
RTE_LOG(WARNING, INET, "%s: bad hook number\n", __func__);
continue; /* return error ? */
}
hook_list = af_inet_hooks(af, reg[i].hooknum);
#ifdef CONFIG_DPVS_IPV4_INET_HOOK
rte_rwlock_write_lock(&inet_hook_lock);
#endif
list_for_each_entry_safe(elem, next, hook_list, list) {
if (elem == ®[i]) {
list_del(&elem->list);
break;
}
}
#ifdef CONFIG_DPVS_IPV4_INET_HOOK
rte_rwlock_write_unlock(&inet_hook_lock);
#endif
if (&elem->list == hook_list)
RTE_LOG(WARNING, INET, "%s: hook not found\n", __func__);
}
return EDPVS_OK;
}
void inet_stats_add(struct inet_stats *stats, const struct inet_stats *diff)
{
stats->inpkts += diff->inpkts;
stats->inoctets += diff->inoctets;
stats->indelivers += diff->indelivers;
stats->outforwdatagrams += diff->outforwdatagrams;
stats->outpkts += diff->outpkts;
stats->outoctets += diff->outoctets;
stats->inhdrerrors += diff->inhdrerrors;
stats->intoobigerrors += diff->intoobigerrors;
stats->innoroutes += diff->innoroutes;
stats->inaddrerrors += diff->inaddrerrors;
stats->inunknownprotos += diff->inunknownprotos;
stats->intruncatedpkts += diff->intruncatedpkts;
stats->indiscards += diff->indiscards;
stats->outdiscards += diff->outdiscards;
stats->outnoroutes += diff->outnoroutes;
stats->reasmtimeout += diff->reasmtimeout;
stats->reasmreqds += diff->reasmreqds;
stats->reasmoks += diff->reasmoks;
stats->reasmfails += diff->reasmfails;
stats->fragoks += diff->fragoks;
stats->fragfails += diff->fragfails;
stats->fragcreates += diff->fragcreates;
stats->inmcastpkts += diff->inmcastpkts;
stats->outmcastpkts += diff->outmcastpkts;
stats->inbcastpkts += diff->inbcastpkts;
stats->outbcastpkts += diff->outbcastpkts;
stats->inmcastoctets += diff->inmcastoctets;
stats->outmcastoctets += diff->outmcastoctets;
stats->inbcastoctets += diff->inbcastoctets;
stats->outbcastoctets += diff->outbcastoctets;
stats->csumerrors += diff->csumerrors;
stats->noectpkts += diff->noectpkts;
stats->ect1pkts += diff->ect1pkts;
stats->ect0pkts += diff->ect0pkts;
stats->cepkts += diff->cepkts;
}