-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathip_vs_core.c
More file actions
1317 lines (1123 loc) · 41.8 KB
/
ip_vs_core.c
File metadata and controls
1317 lines (1123 loc) · 41.8 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2021 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 <assert.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include "conf/common.h"
#include "ipv4.h"
#include "ipv6.h"
#include "icmp.h"
#include "icmp6.h"
#include "sa_pool.h"
#include "ipvs/ipvs.h"
#include "ipvs/conn.h"
#include "ipvs/proto.h"
#include "ipvs/service.h"
#include "ipvs/dest.h"
#include "ipvs/sched.h"
#include "ipvs/laddr.h"
#include "ipvs/xmit.h"
#include "ipvs/synproxy.h"
#include "ipvs/blklst.h"
#include "ipvs/whtlst.h"
#include "ipvs/proto_udp.h"
#include "route6.h"
#include "ipvs/redirect.h"
#include "ipvs/proto_ah_esp.h"
static inline int dp_vs_fill_iphdr(int af, struct rte_mbuf *mbuf,
struct dp_vs_iphdr *iph)
{
if (af == AF_INET) {
struct rte_ipv4_hdr *ip4h = ip4_hdr(mbuf);
iph->af = AF_INET;
iph->len = ip4_hdrlen(mbuf);
iph->proto = ip4h->next_proto_id;
iph->saddr.in.s_addr = ip4h->src_addr;
iph->daddr.in.s_addr = ip4h->dst_addr;
} else if (af == AF_INET6) {
struct ip6_hdr *ip6h = ip6_hdr(mbuf);
uint8_t ip6nxt = ip6h->ip6_nxt;
iph->af = AF_INET6;
iph->len = ip6_skip_exthdr(mbuf, sizeof(struct ip6_hdr), &ip6nxt);
iph->proto = ip6nxt;
iph->saddr.in6 = ip6h->ip6_src;
iph->daddr.in6 = ip6h->ip6_dst;
} else {
return EDPVS_NOTSUPP;
}
return EDPVS_OK;
}
/*
* IPVS persistent scheduling funciton.
* It create a connection entry according to its template if exists,
* or selects a server and creates a connection entry plus a template.
*/
static struct dp_vs_conn *dp_vs_sched_persist(struct dp_vs_service *svc,
const struct dp_vs_iphdr *iph, struct rte_mbuf *mbuf, bool is_synproxy_on)
{
uint32_t conn_flags;
uint16_t _ports[2], *ports;
uint16_t dport;
struct dp_vs_dest *dest;
struct dp_vs_conn *conn, *ct;
struct dp_vs_conn_param param;
union inet_addr snet; /* source network of eth client after masking */
#ifdef CONFIG_DPVS_IPVS_DEBUG
char sbuf[64], dbuf[64], maskbuf[64];
#endif
assert(svc && iph && mbuf);
conn_flags = (is_synproxy_on ? DPVS_CONN_F_SYNPROXY : 0);
if (svc->af == AF_INET6) {
/* FIXME: Is OK to use svc->netmask as IPv6 prefix length ? */
ipv6_addr_prefix_copy(&snet.in6, &iph->saddr.in6, svc->netmask);
} else {
snet.in.s_addr = iph->saddr.in.s_addr & svc->netmask;
}
ports = mbuf_header_pointer(mbuf, iph->len, sizeof(_ports), _ports);
if (!ports)
return NULL;
#ifdef CONFIG_DPVS_IPVS_DEBUG
RTE_LOG(DEBUG, IPVS, "%s: persist-schedule: src %s/%u dest %s/%u snet %s\n",
__func__,
inet_ntop(svc->af, &iph->saddr, sbuf, sizeof(sbuf)),
ntohs(ports[0]),
inet_ntop(svc->af, &iph->daddr, dbuf, sizeof(dbuf)),
ntohs(ports[1]),
inet_ntop(svc->af, &snet, maskbuf, sizeof(maskbuf)));
#endif
if (ports[1] == svc->port) {
/* regular persistent service: <proto, caddr, 0, vaddr, vport, daddr, dport> */
ct = dp_vs_ct_in_get(svc->af, iph->proto, &snet, &iph->daddr, 0, ports[1]);
if (!ct || !dp_vs_check_template(ct)) {
/* no template found, or the dest of the conn template is not available */
dest = svc->scheduler->schedule(svc, mbuf, iph);
if (unlikely(NULL == dest)) {
RTE_LOG(WARNING, IPVS, "%s: persist-schedule: no dest found.\n", __func__);
return NULL;
}
/* create a conn template */
dp_vs_conn_fill_param(iph->af, iph->proto, &snet, &iph->daddr,
0, ports[1], 0, ¶m);
ct = dp_vs_conn_new(mbuf, iph, ¶m, dest, conn_flags | DPVS_CONN_F_TEMPLATE);
if(unlikely(NULL == ct))
return NULL;
ct->timeout.tv_sec = svc->timeout;
} else {
/* set destination with the found template */
dest = ct->dest;
}
dport = dest->port;
} else {
/* port zero service: <proto, caddr, 0, vaddr, 0, daddr, 0>
* fw-mark based service: not support */
ct = dp_vs_ct_in_get(svc->af, iph->proto, &snet, &iph->daddr, 0, 0);
if (!ct || !dp_vs_check_template(ct)) {
dest = svc->scheduler->schedule(svc, mbuf, iph);
if (unlikely(NULL == dest)) {
RTE_LOG(WARNING, IPVS, "%s: persist-schedule: no dest found.\n", __func__);
return NULL;
}
/* create a conn template */
dp_vs_conn_fill_param(iph->af, iph->proto, &snet, &iph->daddr,
0, 0, 0, ¶m);
ct = dp_vs_conn_new(mbuf, iph, ¶m, dest, conn_flags | DPVS_CONN_F_TEMPLATE);
if(unlikely(NULL == ct))
return NULL;
ct->timeout.tv_sec = svc->timeout;
} else {
/* set destination with the found template */
dest = ct->dest;
}
dport = ports[1];
}
/* create a new connection according to the template */
dp_vs_conn_fill_param(iph->af, iph->proto, &iph->saddr, &iph->daddr,
ports[0], ports[1], dport, ¶m);
conn = dp_vs_conn_new(mbuf, iph, ¶m, dest, conn_flags);
if (unlikely(NULL == conn)) {
dp_vs_conn_put(ct);
return NULL;
}
/* add control for the new connection */
dp_vs_control_add(conn, ct);
dp_vs_conn_put(ct);
dp_vs_stats_conn(conn);
return conn;
}
static struct dp_vs_conn *dp_vs_snat_schedule(struct dp_vs_dest *dest,
const struct dp_vs_iphdr *iph,
uint16_t *ports,
struct rte_mbuf *mbuf,
bool outwall)
{
int err;
struct dp_vs_conn *conn;
struct dp_vs_conn_param param;
struct sockaddr_storage daddr, saddr;
uint16_t _ports[2];
if (unlikely(iph->proto == IPPROTO_ICMP)) {
struct icmphdr *ich, _icmph;
ich = mbuf_header_pointer(mbuf, iph->len, sizeof(_icmph), &_icmph);
if (!ich)
return NULL;
_ports[0] = icmp4_id(ich);
_ports[1] = ich->type << 8 | ich->code;
/* ID may confict for diff host,
* need we use ID pool ? */
dp_vs_conn_fill_param(iph->af, iph->proto,
&iph->daddr, &dest->addr,
_ports[1], _ports[0],
0, ¶m);
} else if (unlikely(iph->proto == IPPROTO_ICMPV6)) {
struct icmp6_hdr *ic6h, _ic6hp;
ic6h = mbuf_header_pointer(mbuf, iph->len, sizeof(_ic6hp), &_ic6hp);
if (!ic6h)
return NULL;
_ports[0] = icmp6h_id(ic6h);
_ports[1] = ic6h->icmp6_type << 8 | ic6h->icmp6_code;
dp_vs_conn_fill_param(iph->af, iph->proto,
&iph->daddr, &dest->addr,
_ports[1], _ports[0],
0, ¶m);
} else {
/* we cannot inherit dest (host's src port),
* that may confict for diff hosts,
* and using dest->port is worse choice. */
if (iph->af == AF_INET) {
struct sockaddr_in *daddr4 = (struct sockaddr_in *)&daddr;
struct sockaddr_in *saddr4 = (struct sockaddr_in *)&saddr;
memset(&daddr, 0, sizeof(daddr));
daddr4->sin_family = AF_INET;
daddr4->sin_addr = iph->daddr.in;
daddr4->sin_port = ports[1];
memset(&saddr, 0, sizeof(saddr));
saddr4->sin_family = AF_INET;
saddr4->sin_addr = dest->addr.in;
saddr4->sin_port = 0;
err = sa_fetch(AF_INET, NULL, &daddr, &saddr);
if (err != 0)
return NULL;
dp_vs_conn_fill_param(AF_INET, iph->proto, &iph->daddr, &dest->addr,
ports[1], saddr4->sin_port, 0, ¶m);
} else { /* AF_INET6 */
struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)&daddr;
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)&saddr;
memset(&daddr, 0, sizeof(daddr));
daddr6->sin6_family = AF_INET6;
daddr6->sin6_addr = iph->daddr.in6;
daddr6->sin6_port = ports[1];
memset(&saddr, 0, sizeof(saddr));
saddr6->sin6_family = AF_INET6;
saddr6->sin6_addr = dest->addr.in6;
saddr6->sin6_port = 0;
err = sa_fetch(AF_INET6, NULL, &daddr, &saddr);
if (err != 0)
return NULL;
dp_vs_conn_fill_param(AF_INET6, iph->proto, &iph->daddr, &dest->addr,
ports[1], saddr6->sin6_port, 0, ¶m);
}
}
param.outwall = outwall;
conn = dp_vs_conn_new(mbuf, iph, ¶m, dest, 0);
if (!conn) {
sa_release(NULL, &daddr, &saddr);
return NULL;
}
dp_vs_stats_conn(conn);
return conn;
}
/* select an RS by service's scheduler and create a connection */
struct dp_vs_conn *dp_vs_schedule(struct dp_vs_service *svc,
const struct dp_vs_iphdr *iph,
struct rte_mbuf *mbuf,
bool is_synproxy_on,
bool outwall)
{
uint16_t _ports[2], *ports; /* sport, dport */
struct dp_vs_dest *dest;
struct dp_vs_conn *conn;
struct dp_vs_conn_param param;
uint32_t flags = 0;
assert(svc && iph && mbuf);
ports = mbuf_header_pointer(mbuf, iph->len, sizeof(_ports), _ports);
if (!ports)
return NULL;
/* persistent service */
if (svc->flags & DP_VS_SVC_F_PERSISTENT)
return dp_vs_sched_persist(svc, iph, mbuf, is_synproxy_on);
dest = svc->scheduler->schedule(svc, mbuf, iph);
if (!dest) {
RTE_LOG(WARNING, IPVS, "%s: no dest found.\n", __func__);
#ifdef CONFIG_DPVS_MBUF_DEBUG
dp_vs_mbuf_dump("found dest failed.", iph->af, mbuf);
#endif
return NULL;
}
if (dest->fwdmode == DPVS_FWD_MODE_SNAT)
return dp_vs_snat_schedule(dest, iph, ports, mbuf, outwall);
if (unlikely(iph->proto == IPPROTO_ICMP)) {
struct icmphdr *ich, _icmph;
ich = mbuf_header_pointer(mbuf, iph->len, sizeof(_icmph), &_icmph);
if (!ich)
return NULL;
ports = _ports;
_ports[0] = icmp4_id(ich);
_ports[1] = ich->type << 8 | ich->code;
dp_vs_conn_fill_param(iph->af, iph->proto,
&iph->saddr, &iph->daddr,
ports[0], ports[1], 0, ¶m);
} else if (unlikely(iph->proto == IPPROTO_ICMPV6)) {
struct icmp6_hdr *ic6h, _ic6hp;
ic6h = mbuf_header_pointer(mbuf, iph->len, sizeof(_ic6hp), &_ic6hp);
if (!ic6h)
return NULL;
ports = _ports;
_ports[0] = icmp6h_id(ic6h);
_ports[1] = ic6h->icmp6_type << 8 | ic6h->icmp6_code;
dp_vs_conn_fill_param(iph->af, iph->proto,
&iph->daddr, &dest->addr,
ports[1], ports[0],
0, ¶m);
} else if (unlikely((iph->proto == IPPROTO_AH) || (iph->proto == IPPROTO_ESP))) {
dp_vs_conn_fill_param(iph->af, IPPROTO_UDP,
&iph->daddr, &dest->addr,
htons(PORT_ISAKMP), htons(PORT_ISAKMP),
0, ¶m);
} else {
dp_vs_conn_fill_param(iph->af, iph->proto,
&iph->saddr, &iph->daddr,
ports[0], ports[1], 0, ¶m);
}
if (is_synproxy_on) {
flags |= DPVS_CONN_F_SYNPROXY;
}
if (svc->flags & DP_VS_SVC_F_ONEPACKET && iph->proto == IPPROTO_UDP) {
flags |= DPVS_CONN_F_ONE_PACKET;
}
conn = dp_vs_conn_new(mbuf, iph, ¶m, dest, flags);
if (!conn)
return NULL;
dp_vs_stats_conn(conn);
return conn;
}
/* return verdict INET_XXX */
static int xmit_outbound(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
int err;
assert(mbuf && prot && conn);
if (dp_vs_stats_out(conn, mbuf)) {
dp_vs_conn_put(conn);
return INET_DROP;
}
if (!conn->packet_out_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing out_xmit\n", __func__);
dp_vs_conn_put(conn);
return INET_ACCEPT;
}
err = conn->packet_out_xmit(prot, conn, mbuf);
if (err != EDPVS_OK)
RTE_LOG(DEBUG, IPVS, "%s: fail to out xmit: %d\n", __func__, err);
dp_vs_conn_put(conn);
/* always stolen the packet */
return INET_STOLEN;
}
/* return verdict INET_XXX */
static int xmit_inbound(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
int err;
assert(mbuf && prot && conn);
if (dp_vs_stats_in(conn, mbuf)) {
dp_vs_conn_put(conn);
return INET_DROP;
}
/* is dest avaible to forward the packet ? */
if (!conn->dest) {
/* silently drop packet without reset connection timer.
* wait for dest available or connection timeout. */
dp_vs_conn_put_no_reset(conn);
return INET_DROP;
}
if (!conn->packet_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing packet_xmit\n", __func__);
dp_vs_conn_put(conn);
return INET_ACCEPT;
}
/* forward to RS */
err = conn->packet_xmit(prot, conn, mbuf);
if (err != EDPVS_OK)
RTE_LOG(DEBUG, IPVS, "%s: fail to transmit: %d\n", __func__, err);
dp_vs_conn_put(conn);
/* always stolen the packet */
return INET_STOLEN;
}
/* mbuf should be consumed here. */
static int __xmit_outbound_icmp4(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
struct flow4 fl4;
struct route_entry *rt = NULL;
struct rte_ipv4_hdr *iph = ip4_hdr(mbuf);
/* no translation needed for DR/TUN. */
if (conn->dest->fwdmode != DPVS_FWD_MODE_FNAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_NAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_SNAT) {
if (!conn->packet_out_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing packet_out_xmit\n", __func__);
rte_pktmbuf_free(mbuf);
return EDPVS_NOTSUPP;
}
return conn->packet_out_xmit(prot, conn, mbuf);
}
memset(&fl4, 0, sizeof(struct flow4));
fl4.fl4_daddr = conn->caddr.in;
fl4.fl4_saddr = conn->vaddr.in;
fl4.fl4_tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROUTE;
}
if ((mbuf->pkt_len > rt->mtu)
&& (ip4_hdr(mbuf)->fragment_offset & RTE_IPV4_HDR_DF_FLAG)) {
route4_put(rt);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG,
htonl(rt->mtu));
rte_pktmbuf_free(mbuf);
return EDPVS_FRAG;
}
if (unlikely(MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE) != NULL))
route4_put(MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE));
MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE) = rt;
/* translation for outer L3, ICMP, and inner L3 and L4 */
dp_vs_xmit_icmp(mbuf, prot, conn, DPVS_CONN_DIR_OUTBOUND);
return INET_HOOK(AF_INET, INET_HOOK_LOCAL_OUT, mbuf,
NULL, rt->port, ipv4_output);
}
/* mbuf should be consumed here. */
static int __xmit_outbound_icmp6(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
struct flow6 fl6;
struct route6 *rt6 = NULL;
/* no translation needed for DR/TUN. */
if (conn->dest->fwdmode != DPVS_FWD_MODE_FNAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_NAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_SNAT) {
if (!conn->packet_out_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing packet_out_xmit\n", __func__);
rte_pktmbuf_free(mbuf);
return EDPVS_NOTSUPP;
}
return conn->packet_out_xmit(prot, conn, mbuf);
}
memset(&fl6, 0, sizeof(struct flow6));
fl6.fl6_daddr = conn->caddr.in6;
fl6.fl6_saddr = conn->vaddr.in6;
rt6 = route6_output(mbuf, &fl6);
if (!rt6) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROUTE;
}
if (mbuf->pkt_len > rt6->rt6_mtu) {
route6_put(rt6);
icmp6_send(mbuf, ICMP6_PACKET_TOO_BIG, 0, htonl(rt6->rt6_mtu));
rte_pktmbuf_free(mbuf);
return EDPVS_FRAG;
}
if (unlikely(MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE) != NULL))
route6_put(MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE));
MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE) = rt6;
/* translation for outer L3, ICMP, and inner L3 and L4 */
dp_vs_xmit_icmp(mbuf, prot, conn, DPVS_CONN_DIR_OUTBOUND);
return INET_HOOK(AF_INET6, INET_HOOK_LOCAL_OUT, mbuf,
NULL, rt6->rt6_dev, ip6_output);
}
static int xmit_outbound_icmp(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
int af = conn->af;
assert(af == AF_INET || af == AF_INET6);
if (af == AF_INET)
return __xmit_outbound_icmp4(mbuf, prot, conn);
else
return __xmit_outbound_icmp6(mbuf, prot, conn);
}
/* mbuf should be consumed here. */
static int __xmit_inbound_icmp4(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
struct flow4 fl4;
struct route_entry *rt = NULL;
struct rte_ipv4_hdr *iph = ip4_hdr(mbuf);
/* no translation needed for DR/TUN. */
if (conn->dest->fwdmode != DPVS_FWD_MODE_NAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_FNAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_SNAT) {
if (!conn->packet_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing packet_xmit\n", __func__);
rte_pktmbuf_free(mbuf);
return EDPVS_NOTSUPP;
}
return conn->packet_xmit(prot, conn, mbuf);
}
memset(&fl4, 0, sizeof(struct flow4));
fl4.fl4_daddr = conn->daddr.in;
fl4.fl4_saddr = conn->laddr.in;
fl4.fl4_tos = iph->type_of_service;
rt = route4_output(&fl4);
if (!rt) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROUTE;
}
if ((mbuf->pkt_len > rt->mtu)
&& (ip4_hdr(mbuf)->fragment_offset & RTE_IPV4_HDR_DF_FLAG)) {
route4_put(rt);
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_UNREACH_NEEDFRAG,
htonl(rt->mtu));
rte_pktmbuf_free(mbuf);
return EDPVS_FRAG;
}
if (unlikely(MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE) != NULL))
route4_put(MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE));
MBUF_USERDATA(mbuf, struct route_entry *, MBUF_FIELD_ROUTE) = rt;
/* translation for outer L3, ICMP, and inner L3 and L4 */
dp_vs_xmit_icmp(mbuf, prot, conn, DPVS_CONN_DIR_INBOUND);
return INET_HOOK(AF_INET, INET_HOOK_LOCAL_OUT, mbuf,
NULL, rt->port, ipv4_output);
}
/* mbuf should be consumed here. */
static int __xmit_inbound_icmp6(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
struct flow6 fl6;
struct route6 *rt6 = NULL;
/* no translation needed for DR/TUN. */
if (conn->dest->fwdmode != DPVS_FWD_MODE_NAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_FNAT &&
conn->dest->fwdmode != DPVS_FWD_MODE_SNAT) {
if (!conn->packet_xmit) {
RTE_LOG(WARNING, IPVS, "%s: missing packet_xmit\n", __func__);
rte_pktmbuf_free(mbuf);
return EDPVS_NOTSUPP;
}
return conn->packet_xmit(prot, conn, mbuf);
}
memset(&fl6, 0, sizeof(struct flow6));
fl6.fl6_daddr = conn->daddr.in6;
fl6.fl6_saddr = conn->laddr.in6;
rt6 = route6_output(mbuf, &fl6);
if (!rt6) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROUTE;
}
if (mbuf->pkt_len > rt6->rt6_mtu) {
route6_put(rt6);
icmp6_send(mbuf, ICMP6_PACKET_TOO_BIG, 0, htonl(rt6->rt6_mtu));
rte_pktmbuf_free(mbuf);
return EDPVS_FRAG;
}
if (unlikely(MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE) != NULL))
route6_put(MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE));
MBUF_USERDATA(mbuf, struct route6 *, MBUF_FIELD_ROUTE) = rt6;
/* translation for outer L3, ICMP, and inner L3 and L4 */
dp_vs_xmit_icmp(mbuf, prot, conn, DPVS_CONN_DIR_INBOUND);
return INET_HOOK(AF_INET6, INET_HOOK_LOCAL_OUT, mbuf,
NULL, rt6->rt6_dev, ip6_output);
}
static int xmit_inbound_icmp(struct rte_mbuf *mbuf,
struct dp_vs_proto *prot,
struct dp_vs_conn *conn)
{
int af = conn->af;
assert(af == AF_INET || af == AF_INET6);
if (af == AF_INET)
return __xmit_inbound_icmp4(mbuf, prot, conn);
else
return __xmit_inbound_icmp6(mbuf, prot, conn);
}
/* return verdict INET_XXX */
static int __dp_vs_in_icmp4(struct rte_mbuf *mbuf, int *related)
{
struct icmphdr *ich, _icmph;
struct rte_ipv4_hdr *iph = ip4_hdr(mbuf);
struct rte_ipv4_hdr *ciph, _ciph;
struct dp_vs_iphdr dciph;
struct dp_vs_proto *prot;
struct dp_vs_conn *conn;
int off, dir, err;
lcoreid_t cid, peer_cid;
bool drop = false;
*related = 0; /* not related until found matching conn */
cid = peer_cid = rte_lcore_id();
if (unlikely(ip4_is_frag(iph))) {
if (ip4_defrag(mbuf, IP_DEFRAG_VS_FWD) != EDPVS_OK)
return INET_STOLEN;
iph = ip4_hdr(mbuf); /* reload with new mbuf */
ip4_send_csum(iph);
}
off = ip4_hdrlen(mbuf);
ich = mbuf_header_pointer(mbuf, off, sizeof(_icmph), &_icmph);
if (unlikely(!ich))
return INET_DROP;
#ifdef CONFIG_DPVS_IPVS_DEBUG
RTE_LOG(DEBUG, IPVS, "ICMP (%d,%d) %08X->%08X\n",
ich->type, ntohs(icmp4_id(ich)), iph->src_addr, iph->dst_addr);
#endif
/* support these related error types only,
* others either not support or not related. */
if (ich->type != ICMP_DEST_UNREACH
&& ich->type != ICMP_SOURCE_QUENCH
&& ich->type != ICMP_TIME_EXCEEDED)
return INET_ACCEPT;
/* inner (contained) IP header */
off += sizeof(struct icmphdr);
ciph = mbuf_header_pointer(mbuf, off, sizeof(_ciph), &_ciph);
if (unlikely(!ciph))
return INET_ACCEPT;
prot = dp_vs_proto_lookup(ciph->next_proto_id);
if (!prot)
return INET_ACCEPT;
if (unlikely((ciph->fragment_offset & htons(RTE_IPV4_HDR_OFFSET_MASK)))) {
RTE_LOG(WARNING, IPVS, "%s: frag needed.\n", __func__);
return INET_DROP;
}
/*
* lookup conn with inner IP pkt.
* it need to move mbuf.data_off to inner IP pkt,
* and restore it later. although it looks strange.
*/
rte_pktmbuf_adj(mbuf, off);
if (mbuf_may_pull(mbuf, sizeof(struct rte_ipv4_hdr)) != 0)
return INET_DROP;
dp_vs_fill_iphdr(AF_INET, mbuf, &dciph);
conn = prot->conn_lookup(prot, &dciph, mbuf, &dir, true, &drop, &peer_cid);
/*
* The connection is not locally found, however the redirect is found so
* forward the packet to the remote redirect owner core.
*/
if (cid != peer_cid) {
/* recover mbuf.data_off to outer Ether header */
rte_pktmbuf_prepend(mbuf, (uint16_t)sizeof(struct rte_ether_hdr) + off);
return dp_vs_redirect_pkt(mbuf, peer_cid);
}
/* recover mbuf.data_off to outer IP header. */
rte_pktmbuf_prepend(mbuf, off);
if (!conn)
return INET_ACCEPT;
/* so the ICMP is related to existing conn */
*related = 1;
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0) {
RTE_LOG(WARNING, IPVS, "%s: may_pull icmp error\n", __func__);
dp_vs_conn_put_no_reset(conn);
return INET_DROP;
}
// re-fetch IP header and Icmp address
iph = ip4_hdr(mbuf);
ich = (struct icmphdr*)((void*)iph + ip4_hdrlen(mbuf));
if (rte_raw_cksum(ich, mbuf->pkt_len - ip4_hdrlen(mbuf)) != 0xffff) {
RTE_LOG(DEBUG, IPVS, "%s: bad checksum\n", __func__);
dp_vs_conn_put_no_reset(conn);
return INET_DROP;
}
if (dp_vs_stats_in(conn, mbuf)) {
dp_vs_conn_put(conn);
return INET_DROP;
}
/* note
* 1. the direction of inner IP pkt is reversed with ICMP pkt.
* 2. but we use (@reverse == true) for prot->conn_lookup()
* as a result, @dir is same with icmp packet. */
if (dir == DPVS_CONN_DIR_INBOUND)
err = xmit_inbound_icmp(mbuf, prot, conn);
else
err = xmit_outbound_icmp(mbuf, prot, conn);
if (err != EDPVS_OK)
RTE_LOG(WARNING, IPVS, "%s: xmit icmp error: %s\n",
__func__, dpvs_strerror(err));
dp_vs_conn_put_no_reset(conn);
return INET_STOLEN;
}
#ifdef CONFIG_DPVS_IPVS_DEBUG
static void __dp_vs_icmp6_show(struct ip6_hdr *ip6h, struct icmp6_hdr *ic6h)
{
char src_addr_buff[64], dst_addr_buff[64];
inet_ntop(AF_INET6, &ip6h->ip6_src, src_addr_buff, sizeof(src_addr_buff));
inet_ntop(AF_INET6, &ip6h->ip6_dst, dst_addr_buff, sizeof(dst_addr_buff));
RTE_LOG(DEBUG, IPVS, "%s: ICMP6 (%d, %d) %s->%s\n",
__func__, ic6h->icmp6_type, ntohs(icmp6h_id(ic6h)), src_addr_buff, dst_addr_buff);
}
#endif
/* return verdict INET_XXX */
static int __dp_vs_in_icmp6(struct rte_mbuf *mbuf, int *related)
{
struct icmp6_hdr *ic6h, _icmp6h;
struct ip6_hdr *ip6h = ip6_hdr(mbuf);
struct ip6_hdr *cip6h, _cip6h;
struct dp_vs_iphdr dcip6h;
struct dp_vs_proto *prot;
struct dp_vs_conn *conn;
int off, ic6h_off, dir, err;
lcoreid_t cid, peer_cid;
bool drop = false;
uint8_t nexthdr = ip6h->ip6_nxt;
*related = 0; /* not related until found matching conn */
cid = peer_cid = rte_lcore_id();
// don't suppurt frag now
if (unlikely(ip6_is_frag(ip6h))) {
RTE_LOG(WARNING, IPVS, "%s: ip packet is frag.\n", __func__);
return INET_DROP;
}
off = sizeof(struct ip6_hdr);
off = ip6_skip_exthdr(mbuf, off, &nexthdr);
if (off < 0 || nexthdr != IPPROTO_ICMPV6) {
RTE_LOG(WARNING, IPVS, "%s: off or nexthdr is illegal. off is %d, nexthdr is %u.\n",
__func__, off, nexthdr);
return INET_DROP;
}
ic6h_off = off;
ic6h = mbuf_header_pointer(mbuf, off, sizeof(_icmp6h), &_icmp6h);
if (unlikely(!ic6h))
return INET_DROP;
#ifdef CONFIG_DPVS_IPVS_DEBUG
__dp_vs_icmp6_show(ip6h, ic6h);
#endif
/* support these related error types only,
* others either not support or not related.
*/
if (ic6h->icmp6_type != ICMP6_DST_UNREACH
&& ic6h->icmp6_type != ICMP6_PACKET_TOO_BIG
&& ic6h->icmp6_type != ICMP6_TIME_EXCEEDED)
return INET_ACCEPT;
/* inner (contained) IP header */
off += sizeof(struct icmp6_hdr);
cip6h = mbuf_header_pointer(mbuf, off, sizeof(_cip6h), &_cip6h);
if (unlikely(!cip6h))
return INET_ACCEPT;
if (unlikely(ip6_is_frag(cip6h))) {
RTE_LOG(WARNING, IPVS, "%s: frag needed.\n", __func__);
return INET_ACCEPT;
}
/*
* lookup conn with inner IP pkt.
* it need to move mbuf.data_off to inner IP pkt,
* and restore it later. although it looks strange.
*/
rte_pktmbuf_adj(mbuf, off);
if (mbuf_may_pull(mbuf, sizeof(struct ip6_hdr)) != 0)
return INET_DROP;
dp_vs_fill_iphdr(AF_INET6, mbuf, &dcip6h);
prot = dp_vs_proto_lookup(dcip6h.proto);
if (!prot)
return INET_ACCEPT;
conn = prot->conn_lookup(prot, &dcip6h, mbuf, &dir, true, &drop, &peer_cid);
/*
* The connection is not locally found, however the redirect is found so
* forward the packet to the remote redirect owner core.
*/
if (cid != peer_cid) {
/* recover mbuf.data_off to outer Ether header */
rte_pktmbuf_prepend(mbuf, (uint16_t)sizeof(struct rte_ether_hdr) + off);
return dp_vs_redirect_pkt(mbuf, peer_cid);
}
/* recover mbuf.data_off to outer IP header. */
rte_pktmbuf_prepend(mbuf, off);
if (!conn)
return INET_ACCEPT;
/* so the ICMP is related to existing conn */
*related = 1;
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0) {
RTE_LOG(WARNING, IPVS, "%s: may_pull icmp error\n", __func__);
dp_vs_conn_put_no_reset(conn);
return INET_DROP;
}
/*
* check checksum
* re-fetch IP header and Icmp address
*/
ip6h = ip6_hdr(mbuf);
ic6h = (struct icmp6_hdr *)((void*)(ip6h) + ic6h_off);
if (icmp6_csum(ip6h, ic6h) != 0xffff) {
RTE_LOG(DEBUG, IPVS, "%s: bad checksum\n", __func__);
dp_vs_conn_put_no_reset(conn);
return INET_DROP;
}
if (dp_vs_stats_in(conn, mbuf)) {
dp_vs_conn_put(conn);
return INET_DROP;
}
/* note
* 1. the direction of inner IP pkt is reversed with ICMP pkt.
* 2. but we use (@reverse == true) for prot->conn_lookup()
* as a result, @dir is same with icmp packet. */
if (dir == DPVS_CONN_DIR_INBOUND)
err = xmit_inbound_icmp(mbuf, prot, conn);
else
err = xmit_outbound_icmp(mbuf, prot, conn);
if (err != EDPVS_OK)
RTE_LOG(WARNING, IPVS, "%s: xmit icmp error: %s\n",
__func__, dpvs_strerror(err));
dp_vs_conn_put_no_reset(conn);
return INET_STOLEN;
}
static int dp_vs_in_icmp(int af, struct rte_mbuf *mbuf, int *related)
{
*related = 0;
switch (af) {
case AF_INET:
return __dp_vs_in_icmp4(mbuf, related);
case AF_INET6:
return __dp_vs_in_icmp6(mbuf, related);
}
return INET_ACCEPT;
}
/* return verdict INET_XXX
* af from mbuf->l3_type? No! The field is rewritten by netif and conflicts with
* m.packet_type(an union), so using a wrapper to get af.
* */
static int __dp_vs_in(void *priv, struct rte_mbuf *mbuf,
const struct inet_hook_state *state, int af)
{
struct dp_vs_iphdr iph;
struct dp_vs_proto *prot;
struct dp_vs_conn *conn;
int dir, verdict, err, related;
bool drop = false;
lcoreid_t cid, peer_cid;
eth_type_t etype = mbuf->packet_type; /* FIXME: use other field ? */
assert(mbuf && state);
cid = peer_cid = rte_lcore_id();
if (unlikely(etype != ETH_PKT_HOST))
return INET_ACCEPT;
if (dp_vs_fill_iphdr(af, mbuf, &iph) != EDPVS_OK)
return INET_ACCEPT;
if (unlikely(iph.proto == IPPROTO_ICMP ||
iph.proto == IPPROTO_ICMPV6)) {
/* handle related ICMP error to existing conn */
verdict = dp_vs_in_icmp(af, mbuf, &related);
if (related || verdict != INET_ACCEPT)
return verdict;
/* let unrelated and valid ICMP goes down,
* may implement ICMP fwd in the futher. */
}
prot = dp_vs_proto_lookup(iph.proto);
if (unlikely(!prot))
return INET_ACCEPT;
/*
* Defrag ipvs-forwarding TCP/UDP is not supported for some reasons,
*
* - RSS/flow-director do not support TCP/UDP fragments, means it's
* not able to direct frags to same lcore as original TCP/UDP packets.
* - per-lcore conn table will miss if frags reachs wrong lcore.
*
* If we redirect frags to "correct" lcore, it may cause performance
* issue. Also it need to understand RSS algorithm. Moreover, for the
* case frags in same flow are not occur in same lcore, a global lock is
* needed, which is not a good idea.
*/
if (af == AF_INET && ip4_is_frag(ip4_hdr(mbuf))) {
RTE_LOG(DEBUG, IPVS, "%s: frag not support.\n", __func__);
return INET_DROP;
}
/* packet belongs to existing connection ? */
conn = prot->conn_lookup(prot, &iph, mbuf, &dir, false, &drop, &peer_cid);
if (unlikely(drop)) {
RTE_LOG(DEBUG, IPVS, "%s: deny ip try to visit.\n", __func__);
return INET_DROP;
}
/*
* The connection is not locally found, however the redirect is found so