forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_vs_xmit.c
574 lines (485 loc) · 15.8 KB
/
ip_vs_xmit.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* 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 <netinet/ip_icmp.h>
#include <assert.h>
#include "dpdk.h"
#include "ipv4.h"
#include "route.h"
#include "icmp.h"
#include "neigh.h"
#include "ipvs/xmit.h"
#include "parser/parser.h"
static bool fast_xmit_close = false;
static int dp_vs_fast_xmit_fnat(struct dp_vs_proto *proto,
struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct ether_hdr *eth;
int err;
if(unlikely(conn->in_dev == NULL))
return EDPVS_NOROUTE;
iph->hdr_checksum = 0;
iph->src_addr = conn->laddr.in.s_addr;
iph->dst_addr = conn->daddr.in.s_addr;
if(proto->fnat_in_handler) {
err = proto->fnat_in_handler(proto, conn, mbuf);
if(err != EDPVS_OK)
return err;
}
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM)) {
iph->hdr_checksum = 0;
} else {
ip4_send_csum(iph);
}
eth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf,
(uint16_t)sizeof(struct ether_hdr));
ether_addr_copy(&conn->in_dmac, ð->d_addr);
ether_addr_copy(&conn->in_smac, ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
err = netif_xmit(mbuf, conn->in_dev);
if (err != EDPVS_OK)
RTE_LOG(DEBUG, IPVS, "%s: fail to netif_xmit.\n", __func__);
/* must return OK since netif_xmit alway consume mbuf */
return EDPVS_OK;
}
static int dp_vs_fast_outxmit_fnat(struct dp_vs_proto *proto,
struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct ether_hdr *eth;
int err;
/*need to judge?*/
if(unlikely(conn->out_dev == NULL))
return EDPVS_NOROUTE;
iph->hdr_checksum = 0;
iph->src_addr = conn->vaddr.in.s_addr;
iph->dst_addr = conn->caddr.in.s_addr;
if(proto->fnat_out_handler) {
err = proto->fnat_out_handler(proto, conn, mbuf);
if(err != EDPVS_OK)
return err;
}
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM)) {
iph->hdr_checksum = 0;
} else {
ip4_send_csum(iph);
}
eth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf,
(uint16_t)sizeof(struct ether_hdr));
ether_addr_copy(&conn->out_dmac, ð->d_addr);
ether_addr_copy(&conn->out_smac, ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
err = netif_xmit(mbuf, conn->out_dev);
if (err != EDPVS_OK)
RTE_LOG(DEBUG, IPVS, "%s: fail to netif_xmit.\n", __func__);
/* must return OK since netif_xmit alway consume mbuf */
return EDPVS_OK;
}
/*ARP_HDR_ETHER SUPPORT ONLY
*save source mac(client) for output in conn as dest mac
*save port for output
* */
static void dp_vs_save_xmit_info(struct rte_mbuf *mbuf,
struct dp_vs_proto *proto,
struct dp_vs_conn *conn)
{
struct ether_hdr *eth = NULL;
struct netif_port *port = NULL;
if (conn->out_dev)
return;
if (unlikely(mbuf->l2_len != sizeof(struct ether_hdr)))
return;
port = netif_port_get(mbuf->port);
if (!port)
return;
eth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf, mbuf->l2_len);
conn->out_dev = port;
ether_addr_copy(ð->s_addr, &conn->out_dmac);
ether_addr_copy(ð->d_addr, &conn->out_smac);
rte_pktmbuf_adj(mbuf, sizeof(struct ether_hdr));
}
/*save source mac(rs) for input in conn as dest mac
*save port for output
*/
static void dp_vs_save_outxmit_info(struct rte_mbuf *mbuf,
struct dp_vs_proto *proto,
struct dp_vs_conn *conn)
{
struct ether_hdr *eth = NULL;
struct netif_port *port = NULL;
if (conn->in_dev)
return;
if (mbuf->l2_len != sizeof(struct ether_hdr))
return;
port = netif_port_get(mbuf->port);
if (!port)
return;
eth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf, mbuf->l2_len);
conn->in_dev = port;
ether_addr_copy(ð->s_addr, &conn->in_dmac);
ether_addr_copy(ð->d_addr, &conn->in_smac);
rte_pktmbuf_adj(mbuf, sizeof(struct ether_hdr));
}
int dp_vs_xmit_fnat(struct dp_vs_proto *proto,
struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct flow4 fl4;
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct route_entry *rt;
int err, mtu;
if (!fast_xmit_close) {
dp_vs_save_xmit_info(mbuf, proto, conn);
if (!dp_vs_fast_xmit_fnat(proto, conn, mbuf))
return EDPVS_OK;
}
/* drop old route. just for safe, because
* FNAT is PRE_ROUTING, should not have route. */
if (unlikely(mbuf->userdata != NULL)) {
RTE_LOG(WARNING, IPVS, "%s: FNAT have route %p ?\n",
__func__, mbuf->userdata);
route4_put((struct route_entry *)mbuf->userdata);
}
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr = conn->daddr.in;
fl4.saddr = conn->laddr.in;
fl4.tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
mtu = rt->mtu;
if (mbuf->pkt_len > mtu
&& (iph->fragment_offset & htons(IPV4_HDR_DF_FLAG))) {
RTE_LOG(DEBUG, IPVS, "%s: frag needed.\n", __func__);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG, htonl(mtu));
err = EDPVS_FRAG;
goto errout;
}
mbuf->userdata = rt;
/* L3 translation before l4 re-csum */
iph->hdr_checksum = 0;
iph->src_addr = conn->laddr.in.s_addr;
iph->dst_addr = conn->daddr.in.s_addr;
/* L4 FNAT translation */
if (proto->fnat_in_handler) {
err = proto->fnat_in_handler(proto, conn, mbuf);
if (err != EDPVS_OK)
goto errout;
}
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM)) {
iph->hdr_checksum = 0;
} else {
ip4_send_csum(iph);
}
return INET_HOOK(INET_HOOK_LOCAL_OUT, mbuf, NULL, rt->port, ipv4_output);
errout:
if (rt)
route4_put(rt);
rte_pktmbuf_free(mbuf);
return err;
}
int dp_vs_out_xmit_fnat(struct dp_vs_proto *proto,
struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct flow4 fl4;
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct route_entry *rt;
int err, mtu;
if (!fast_xmit_close) {
dp_vs_save_outxmit_info(mbuf, proto, conn);
if (!dp_vs_fast_outxmit_fnat(proto, conn, mbuf))
return EDPVS_OK;
}
/* drop old route. just for safe, because
* FNAT is PRE_ROUTING, should not have route. */
if (unlikely(mbuf->userdata != NULL))
route4_put((struct route_entry *)mbuf->userdata);
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr = conn->caddr.in;
fl4.saddr = conn->vaddr.in;
fl4.tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
mtu = rt->mtu;
if (mbuf->pkt_len > mtu
&& (iph->fragment_offset & htons(IPV4_HDR_DF_FLAG))) {
RTE_LOG(DEBUG, IPVS, "%s: frag needed.\n", __func__);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG, htonl(mtu));
err = EDPVS_FRAG;
goto errout;
}
mbuf->userdata = rt;
/* L3 translation before l4 re-csum */
iph->hdr_checksum = 0;
iph->src_addr = conn->vaddr.in.s_addr;
iph->dst_addr = conn->caddr.in.s_addr;
/* L4 FNAT translation */
if (proto->fnat_out_handler) {
err = proto->fnat_out_handler(proto, conn, mbuf);
if (err != EDPVS_OK)
goto errout;
}
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM)) {
iph->hdr_checksum = 0;
} else {
ip4_send_csum(iph);
}
return INET_HOOK(INET_HOOK_LOCAL_OUT, mbuf, NULL, rt->port, ipv4_output);
errout:
if (rt)
route4_put(rt);
rte_pktmbuf_free(mbuf);
return err;
}
/* mbuf's data should pointer to outer IP packet. */
void dp_vs_xmit_icmp(struct rte_mbuf *mbuf, struct dp_vs_proto *prot,
struct dp_vs_conn *conn, int dir)
{
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct icmphdr *icmph = (struct icmphdr *)
((unsigned char *)ip4_hdr(mbuf) + ip4_hdrlen(mbuf));
struct ipv4_hdr *ciph = (struct ipv4_hdr *)(icmph + 1);
int fullnat = (conn->dest->fwdmode == DPVS_FWD_MODE_FNAT);
uint16_t csum;
/*
* outer/inner L3 translation.
*/
if (fullnat) {
if (dir == DPVS_CONN_DIR_INBOUND) {
iph->src_addr = conn->laddr.in.s_addr;
ciph->dst_addr = conn->laddr.in.s_addr;
} else {
iph->dst_addr = conn->caddr.in.s_addr;
ciph->src_addr = conn->caddr.in.s_addr;
}
}
if (dir == DPVS_CONN_DIR_INBOUND) {
iph->dst_addr = conn->daddr.in.s_addr;
ip4_send_csum(iph);
ciph->src_addr = conn->daddr.in.s_addr;
ip4_send_csum(ciph);
} else {
iph->src_addr = conn->vaddr.in.s_addr;
ip4_send_csum(iph);
ciph->dst_addr = conn->vaddr.in.s_addr;
ip4_send_csum(ciph);
}
/*
* inner L4 translation.
*
* note it's no way to recalc inner csum to lack of data,
* actually it's not needed.
*/
if (ciph->next_proto_id == IPPROTO_TCP
|| ciph->next_proto_id == IPPROTO_UDP) {
uint16_t *ports = (void *)ciph + \
((ciph->version_ihl & IPV4_HDR_IHL_MASK)<<2);
if (fullnat) {
if (dir == DPVS_CONN_DIR_INBOUND) {
ports[1] = conn->lport;
} else {
ports[0] = conn->cport;
/* seq adjustment (changed by FNAT) */
if (ciph->next_proto_id == IPPROTO_TCP) {
uint32_t *seq = (uint32_t *)ports + 1;
*seq = htonl(ntohl(*seq) - conn->fnat_seq.delta);
}
}
}
if (dir == DPVS_CONN_DIR_INBOUND) {
ports[0] = conn->dport;
/* seq adjustment (changed by SynProxy) */
if (ciph->next_proto_id == IPPROTO_TCP) {
uint32_t *seq = (uint32_t *)ports + 1;
*seq = htonl(ntohl(*seq) - conn->syn_proxy_seq.delta);
}
} else {
ports[1] = conn->vport;
}
}
/*
* ICMP recalc csum.
*/
icmph->checksum = 0;
csum = rte_raw_cksum(icmph, mbuf->pkt_len - ip4_hdrlen(mbuf));
icmph->checksum = (csum == 0xffff) ? csum : ~csum;
return;
}
int dp_vs_xmit_dr(struct dp_vs_proto *proto,
struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct flow4 fl4;
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct route_entry *rt;
int err, mtu;
if (unlikely(mbuf->userdata != NULL)) {
RTE_LOG(WARNING, IPVS, "%s: Already have route %p ?\n",
__func__, mbuf->userdata);
route4_put((struct route_entry *)mbuf->userdata);
}
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr.s_addr = conn->daddr.in.s_addr;
fl4.saddr.s_addr = iph->src_addr;
fl4.tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
mtu = rt->mtu;
if (mbuf->pkt_len > mtu
&& (iph->fragment_offset & htons(IPV4_HDR_DF_FLAG))) {
RTE_LOG(DEBUG, IPVS, "%s: frag needed.\n", __func__);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG, htonl(mtu));
err = EDPVS_FRAG;
goto errout;
}
mbuf->packet_type = ETHER_TYPE_IPv4;
err = neigh_resolve_output(&conn->daddr.in, mbuf, rt->port);
if (rt)
route4_put(rt);
return err;
errout:
if (rt)
route4_put(rt);
rte_pktmbuf_free(mbuf);
return err;
}
int dp_vs_xmit_snat(struct dp_vs_proto *proto, struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
struct flow4 fl4;
struct ipv4_hdr *iph = ip4_hdr(mbuf);
struct route_entry *rt;
int err, mtu;
/* drop old route. just for safe, because
* inbound SNAT traffic is hooked at PRE_ROUTING,
* should not have route. */
if (unlikely(mbuf->userdata != NULL)) {
RTE_LOG(WARNING, IPVS, "%s: SNAT have route %p ?\n",
__func__, mbuf->userdata);
route4_put((struct route_entry *)mbuf->userdata);
}
/* hosts inside SNAT may belongs to diff net,
* let's route it. */
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr = conn->daddr.in;
fl4.saddr = conn->caddr.in;
fl4.tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
mtu = rt->mtu;
if (mbuf->pkt_len > mtu
&& (iph->fragment_offset & htons(IPV4_HDR_DF_FLAG))) {
RTE_LOG(DEBUG, IPVS, "%s: frag needed.\n", __func__);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG, htonl(mtu));
err = EDPVS_FRAG;
goto errout;
}
mbuf->userdata = rt;
/* L3 translation before l4 re-csum */
iph->hdr_checksum = 0;
iph->dst_addr = conn->daddr.in.s_addr;
/* L4 translation */
if (proto->snat_in_handler) {
err = proto->snat_in_handler(proto, conn, mbuf);
if (err != EDPVS_OK)
goto errout;
}
/* L3 re-checksum */
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM))
iph->hdr_checksum = 0;
else
ip4_send_csum(iph);
return INET_HOOK(INET_HOOK_LOCAL_OUT, mbuf, NULL, rt->port, ipv4_output);
errout:
if (rt)
route4_put(rt);
rte_pktmbuf_free(mbuf);
return err;
}
int dp_vs_out_xmit_snat(struct dp_vs_proto *proto, struct dp_vs_conn *conn,
struct rte_mbuf *mbuf)
{
int err;
struct flow4 fl4;
struct route_entry *rt = mbuf->userdata;
struct ipv4_hdr *iph = ip4_hdr(mbuf);
if (!rt) {
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr = conn->caddr.in;
fl4.saddr = conn->vaddr.in;
fl4.tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
mbuf->userdata = rt;
}
if (mbuf->pkt_len > rt->mtu &&
(iph->fragment_offset & htons(IPV4_HDR_DF_FLAG))) {
RTE_LOG(DEBUG, IPVS, "%s: frag needed.\n", __func__);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG,
htonl(rt->mtu));
err = EDPVS_FRAG;
goto errout;
}
/* L3 translation before L4 re-csum */
iph->hdr_checksum = 0;
iph->src_addr = conn->vaddr.in.s_addr;
/* L4 translation */
if (proto->snat_out_handler) {
err = proto->snat_out_handler(proto, conn, mbuf);
if (err != EDPVS_OK)
goto errout;
}
/* L3 re-checksum */
if (likely(mbuf->ol_flags & PKT_TX_IP_CKSUM))
iph->hdr_checksum = 0;
else
ip4_send_csum(iph);
return INET_HOOK(INET_HOOK_LOCAL_OUT, mbuf, NULL, rt->port, ipv4_output);
errout:
if (rt)
route4_put(rt);
rte_pktmbuf_free(mbuf);
return err;
}
static void conn_fast_xmit_handler(vector_t tockens)
{
RTE_LOG(INFO, IPVS, "fast xmit OFF\n");
fast_xmit_close = true;
}
void install_xmit_keywords(void)
{
install_keyword("fast_xmit_close", conn_fast_xmit_handler,
KW_TYPE_INIT);
}