forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inet.h
338 lines (288 loc) · 11.3 KB
/
inet.h
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
/*
* 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.
*
*/
#ifndef __DPVS_INET_H__
#define __DPVS_INET_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#define INET_DEF_TTL 64
#define INET_MAX_PROTS 256 /* cannot change */
union inet_addr {
struct in_addr in;
struct in6_addr in6;
};
struct inet_prefix {
int plen;
union inet_addr addr;
};
struct inet_addr_range {
union inet_addr min_addr;
union inet_addr max_addr;
__be16 min_port;
__be16 max_port;
};
struct inet_stats {
uint64_t inpkts; /* InReceives */
uint64_t inoctets; /* InOctets */
uint64_t indelivers; /* InDelivers */
uint64_t outforwdatagrams; /* OutForwDatagrams */
uint64_t outpkts; /* OutRequests */
uint64_t outoctets; /* OutOctets */
uint64_t inhdrerrors; /* InHdrErrors */
uint64_t intoobigerrors; /* InTooBigErrors */
uint64_t innoroutes; /* InNoRoutes */
uint64_t inaddrerrors; /* InAddrErrors */
uint64_t inunknownprotos; /* InUnknownProtos */
uint64_t intruncatedpkts; /* InTruncatedPkts */
uint64_t indiscards; /* InDiscards */
uint64_t outdiscards; /* OutDiscards */
uint64_t outnoroutes; /* OutNoRoutes */
uint64_t reasmtimeout; /* ReasmTimeout */
uint64_t reasmreqds; /* ReasmReqds */
uint64_t reasmoks; /* ReasmOKs */
uint64_t reasmfails; /* ReasmFails */
uint64_t fragoks; /* FragOKs */
uint64_t fragfails; /* FragFails */
uint64_t fragcreates; /* FragCreates */
uint64_t inmcastpkts; /* InMcastPkts */
uint64_t outmcastpkts; /* OutMcastPkts */
uint64_t inbcastpkts; /* InBcastPkts */
uint64_t outbcastpkts; /* OutBcastPkts */
uint64_t inmcastoctets; /* InMcastOctets */
uint64_t outmcastoctets; /* OutMcastOctets */
uint64_t inbcastoctets; /* InBcastOctets */
uint64_t outbcastoctets; /* OutBcastOctets */
uint64_t csumerrors; /* InCsumErrors */
uint64_t noectpkts; /* InNoECTPkts */
uint64_t ect1pkts; /* InECT1Pkts */
uint64_t ect0pkts; /* InECT0Pkts */
uint64_t cepkts; /* InCEPkts */
};
static inline const char *inet_proto_name(uint8_t proto)
{
const static char *proto_names[256] = {
[IPPROTO_TCP] = "TCP",
[IPPROTO_UDP] = "UDP",
[IPPROTO_ICMP] = "ICMP",
[IPPROTO_ICMPV6] = "ICMPV6",
};
return proto_names[proto] ? proto_names[proto] : "<unknow>";
}
static inline uint32_t inet_addr_fold(int af, const union inet_addr *addr)
{
uint32_t addr_fold = 0;
if (af == AF_INET) {
addr_fold = addr->in.s_addr;
} else if (af == AF_INET6) {
addr_fold = addr->in6.s6_addr32[0] ^ addr->in6.s6_addr32[1] ^
addr->in6.s6_addr32[2] ^ addr->in6.s6_addr32[3];
} else {
return 0;
}
return addr_fold;
}
/* ip1[-ip2][:port1[-port2]] */
static inline int inet_addr_range_parse(const char *param,
struct inet_addr_range *range,
int *af)
{
char _param[256], *ips, *ports = NULL;
char *ip1, *ip2, *port1, *port2;
if (strlen(param) == 0)
return EDPVS_OK; /* return asap */
snprintf(_param, sizeof(_param), "%s", param);
ips = _param;
if (_param[0] == '[') {
ips++;
ports = strrchr(_param, ']');
if (ports == NULL)
return EDPVS_INVAL;
*ports++ = '\0';
if (*ports == ':')
*ports++ = '\0';
else
return EDPVS_INVAL;
}
/* judge ipv4 */
if (strrchr(_param, ':') == strchr(_param, ':')) {
ports = strrchr(_param, ':');
if (ports)
*ports++ = '\0';
}
ip1 = ips;
ip2 = strrchr(ips, '-');
if (ip2)
*ip2++ = '\0';
if (ports) {
port1 = ports;
port2 = strrchr(ports, '-');
if (port2)
*port2++ = '\0';
} else {
port1 = port2 = NULL;
}
memset(range, 0, sizeof(*range));
if (strlen(ip1) && inet_pton(AF_INET6, ip1, &range->min_addr.in6) > 0) {
if (ip2 && strlen(ip2)) {
if (inet_pton(AF_INET6, ip2, &range->max_addr.in6) <= 0)
return EDPVS_INVAL;
} else {
range->max_addr = range->min_addr;
}
*af = AF_INET6;
} else {
if (strlen(ip1) && inet_pton(AF_INET, ip1, &range->min_addr.in) <= 0)
return EDPVS_INVAL;
if (ip2 && strlen(ip2)) {
if (inet_pton(AF_INET, ip2, &range->max_addr.in) <= 0)
return EDPVS_INVAL;
} else {
range->max_addr = range->min_addr;
}
*af = AF_INET;
}
if (port1 && strlen(port1))
range->min_port = htons(atoi(port1));
if (port2 && strlen(port2))
range->max_port = htons(atoi(port2));
else
range->max_port = range->min_port;
return EDPVS_OK;
}
static inline int inet_addr_range_dump(int af,
const struct inet_addr_range *range,
char *buf, size_t size)
{
char min_ip[64], max_ip[64];
char min_port[16], max_port[16];
inet_ntop(af, &range->min_addr, min_ip, sizeof(min_ip));
inet_ntop(af, &range->max_addr, max_ip, sizeof(max_ip));
snprintf(min_port, sizeof(min_port), "%u", ntohs(range->min_port));
snprintf(max_port, sizeof(max_port), "%u", ntohs(range->max_port));
if (af == AF_INET)
return snprintf(buf, size, "%s-%s:%s-%s",
min_ip, max_ip, min_port, max_port);
return snprintf(buf, size, "[%s-%s]:%s-%s",
min_ip, max_ip, min_port, max_port);
}
static inline void inet_stats_dump(const char *title, const char *prefix,
const struct inet_stats *st)
{
if (!st)
return;
if (title)
printf("%s\n", title);
printf("%s%-18s %lu\n", prefix ? : "", "InReceives:", st->inpkts);
printf("%s%-18s %lu\n", prefix ? : "", "InOctets:", st->inoctets);
printf("%s%-18s %lu\n", prefix ? : "", "InDelivers:", st->indelivers);
printf("%s%-18s %lu\n", prefix ? : "", "OutForwDatagrams:", st->outforwdatagrams);
printf("%s%-18s %lu\n", prefix ? : "", "OutRequests:", st->outpkts);
printf("%s%-18s %lu\n", prefix ? : "", "OutOctets:", st->outoctets);
printf("%s%-18s %lu\n", prefix ? : "", "InHdrErrors:", st->inhdrerrors);
printf("%s%-18s %lu\n", prefix ? : "", "InTooBigErrors:", st->intoobigerrors);
printf("%s%-18s %lu\n", prefix ? : "", "InNoRoutes:", st->innoroutes);
printf("%s%-18s %lu\n", prefix ? : "", "InAddrErrors:", st->inaddrerrors);
printf("%s%-18s %lu\n", prefix ? : "", "InUnknownProtos:", st->inunknownprotos);
printf("%s%-18s %lu\n", prefix ? : "", "InTruncatedPkts:", st->intruncatedpkts);
printf("%s%-18s %lu\n", prefix ? : "", "InDiscards:", st->indiscards);
printf("%s%-18s %lu\n", prefix ? : "", "OutDiscards:", st->outdiscards);
printf("%s%-18s %lu\n", prefix ? : "", "OutNoRoutes:", st->outnoroutes);
printf("%s%-18s %lu\n", prefix ? : "", "ReasmTimeout:", st->reasmtimeout);
printf("%s%-18s %lu\n", prefix ? : "", "ReasmReqds:", st->reasmreqds);
printf("%s%-18s %lu\n", prefix ? : "", "ReasmOKs:", st->reasmoks);
printf("%s%-18s %lu\n", prefix ? : "", "ReasmFails:", st->reasmfails);
printf("%s%-18s %lu\n", prefix ? : "", "FragOKs:", st->fragoks);
printf("%s%-18s %lu\n", prefix ? : "", "FragFails:", st->fragfails);
printf("%s%-18s %lu\n", prefix ? : "", "FragCreates:", st->fragcreates);
printf("%s%-18s %lu\n", prefix ? : "", "InMcastPkts:", st->inmcastpkts);
printf("%s%-18s %lu\n", prefix ? : "", "OutMcastPkts:", st->outmcastpkts);
printf("%s%-18s %lu\n", prefix ? : "", "InBcastPkts:", st->inbcastpkts);
printf("%s%-18s %lu\n", prefix ? : "", "OutBcastPkts:", st->outbcastpkts);
printf("%s%-18s %lu\n", prefix ? : "", "InMcastOctets:", st->inmcastoctets);
printf("%s%-18s %lu\n", prefix ? : "", "OutMcastOctets:", st->outmcastoctets);
printf("%s%-18s %lu\n", prefix ? : "", "InBcastOctets:", st->inbcastoctets);
printf("%s%-18s %lu\n", prefix ? : "", "OutBcastOctets:", st->outbcastoctets);
printf("%s%-18s %lu\n", prefix ? : "", "InCsumErrors:", st->csumerrors);
printf("%s%-18s %lu\n", prefix ? : "", "InNoECTPkts:", st->noectpkts);
printf("%s%-18s %lu\n", prefix ? : "", "InECT1Pkts:", st->ect1pkts);
printf("%s%-18s %lu\n", prefix ? : "", "InECT0Pkts:", st->ect0pkts);
printf("%s%-18s %lu\n", prefix ? : "", "InCEPkts:", st->cepkts);
}
#ifdef __DPVS__
#include "dpdk.h"
#include "netif.h"
/*
* Inet Hooks
*/
enum {
INET_HOOK_PRE_ROUTING,
INET_HOOK_LOCAL_IN,
INET_HOOK_FORWARD,
INET_HOOK_LOCAL_OUT,
INET_HOOK_POST_ROUTING,
INET_HOOK_NUMHOOKS,
};
struct inet_hook_state {
unsigned int hook;
} __rte_cache_aligned;
enum {
INET_DROP = 0,
INET_ACCEPT,
INET_STOLEN,
INET_REPEAT,
INET_STOP,
INET_VERDICT_NUM,
};
typedef int (*inet_hook_fn)(void *priv, struct rte_mbuf *mbuf,
const struct inet_hook_state *state);
struct inet_hook_ops {
inet_hook_fn hook;
unsigned int hooknum;
int af;
void *priv;
int priority;
struct list_head list;
};
struct netif_port;
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));
int inet_init(void);
int inet_term(void);
bool inet_addr_equal(int af, const union inet_addr *a1,
const union inet_addr *a2);
const char *inet_proto_name(uint8_t proto);
bool inet_is_addr_any(int af, const union inet_addr *addr);
int inet_plen_to_mask(int af, uint8_t plen, union inet_addr *mask);
int inet_addr_net(int af, const union inet_addr *addr,
const union inet_addr *mask,
union inet_addr *net);
bool inet_addr_same_net(int af, uint8_t plen,
const union inet_addr *addr1,
const union inet_addr *addr2);
int inet_addr_range_dump(int af, const struct inet_addr_range *range,
char *buf, size_t size);
int inet_register_hooks(struct inet_hook_ops *reg, size_t n);
int inet_unregister_hooks(struct inet_hook_ops *reg, size_t n);
void inet_stats_add(struct inet_stats *stats, const struct inet_stats *diff);
#endif /* __DPVS__ */
#endif /* __DPVS_INET_H__ */