-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
lb.h
1491 lines (1300 loc) · 39.8 KB
/
lb.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
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
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2016-2020 Authors of Cilium */
#ifndef __LB_H_
#define __LB_H_
#include "csum.h"
#include "conntrack.h"
#include "ipv4.h"
#include "hash.h"
#include "ids.h"
#ifdef ENABLE_IPV6
struct bpf_elf_map __section_maps LB6_REVERSE_NAT_MAP = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_value = sizeof(struct lb6_reverse_nat),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
struct bpf_elf_map __section_maps LB6_SERVICES_MAP_V2 = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct lb6_key),
.size_value = sizeof(struct lb6_service),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
struct bpf_elf_map __section_maps LB6_BACKEND_MAP = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_value = sizeof(struct lb6_backend),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
#ifdef ENABLE_SESSION_AFFINITY
struct bpf_elf_map __section_maps LB6_AFFINITY_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(struct lb6_affinity_key),
.size_value = sizeof(struct lb_affinity_val),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
};
#endif
#ifdef ENABLE_SRC_RANGE_CHECK
struct bpf_elf_map __section_maps LB6_SRC_RANGE_MAP = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.size_key = sizeof(struct lb6_src_range_key),
.size_value = sizeof(__u8),
.pinning = PIN_GLOBAL_NS,
.max_elem = LB6_SRC_RANGE_MAP_SIZE,
.flags = BPF_F_NO_PREALLOC,
};
#endif
#ifdef ENABLE_HEALTH_CHECK
struct bpf_elf_map __section_maps LB6_HEALTH_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(__sock_cookie),
.size_value = sizeof(struct lb6_health),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
};
#endif
#if LB_SELECTION == LB_SELECTION_MAGLEV
struct bpf_elf_map __section_maps LB6_MAGLEV_MAP_INNER = {
.type = BPF_MAP_TYPE_ARRAY,
.size_key = sizeof(__u32),
.size_value = sizeof(__u16) * LB_MAGLEV_LUT_SIZE,
.pinning = PIN_NONE,
.max_elem = 1,
.inner_idx = NO_PREPOPULATE,
.id = CILIUM_MAP_MAGLEV6,
};
struct bpf_elf_map __section_maps LB6_MAGLEV_MAP_OUTER = {
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.size_key = sizeof(__u16),
.size_value = sizeof(__u32),
.pinning = PIN_GLOBAL_NS,
.inner_id = CILIUM_MAP_MAGLEV6,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
#endif /* LB_SELECTION == LB_SELECTION_MAGLEV */
#endif /* ENABLE_IPV6 */
#ifdef ENABLE_IPV4
struct bpf_elf_map __section_maps LB4_REVERSE_NAT_MAP = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_value = sizeof(struct lb4_reverse_nat),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
struct bpf_elf_map __section_maps LB4_SERVICES_MAP_V2 = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct lb4_key),
.size_value = sizeof(struct lb4_service),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
struct bpf_elf_map __section_maps LB4_BACKEND_MAP = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_value = sizeof(struct lb4_backend),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
#ifdef ENABLE_SESSION_AFFINITY
struct bpf_elf_map __section_maps LB4_AFFINITY_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(struct lb4_affinity_key),
.size_value = sizeof(struct lb_affinity_val),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
};
#endif
#ifdef ENABLE_SRC_RANGE_CHECK
struct bpf_elf_map __section_maps LB4_SRC_RANGE_MAP = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.size_key = sizeof(struct lb4_src_range_key),
.size_value = sizeof(__u8),
.pinning = PIN_GLOBAL_NS,
.max_elem = LB4_SRC_RANGE_MAP_SIZE,
.flags = BPF_F_NO_PREALLOC,
};
#endif
#ifdef ENABLE_HEALTH_CHECK
struct bpf_elf_map __section_maps LB4_HEALTH_MAP = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(__sock_cookie),
.size_value = sizeof(struct lb4_health),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
};
#endif
#if LB_SELECTION == LB_SELECTION_MAGLEV
struct bpf_elf_map __section_maps LB4_MAGLEV_MAP_INNER = {
.type = BPF_MAP_TYPE_ARRAY,
.size_key = sizeof(__u32),
.size_value = sizeof(__u16) * LB_MAGLEV_LUT_SIZE,
.pinning = PIN_NONE,
.max_elem = 1,
.inner_idx = NO_PREPOPULATE,
.id = CILIUM_MAP_MAGLEV4,
};
struct bpf_elf_map __section_maps LB4_MAGLEV_MAP_OUTER = {
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.size_key = sizeof(__u16),
.size_value = sizeof(__u32),
.pinning = PIN_GLOBAL_NS,
.inner_id = CILIUM_MAP_MAGLEV4,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
#endif /* LB_SELECTION == LB_SELECTION_MAGLEV */
#endif /* ENABLE_IPV4 */
#ifdef ENABLE_SESSION_AFFINITY
struct bpf_elf_map __section_maps LB_AFFINITY_MATCH_MAP = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct lb_affinity_match),
.size_value = sizeof(__u8), /* dummy value, map is used as a set */
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
.flags = CONDITIONAL_PREALLOC,
};
#endif
#define REV_NAT_F_TUPLE_SADDR 1
#ifndef DSR_XLATE_MODE
# define DSR_XLATE_MODE 0
# define DSR_XLATE_FRONTEND 1
#endif
#ifdef LB_DEBUG
#define cilium_dbg_lb cilium_dbg
#else
#define cilium_dbg_lb(a, b, c, d)
#endif
static __always_inline
bool lb4_svc_is_loadbalancer(const struct lb4_service *svc __maybe_unused)
{
#ifdef ENABLE_LOADBALANCER
return svc->flags & SVC_FLAG_LOADBALANCER;
#else
return false;
#endif /* ENABLE_LOADBALANCER */
}
static __always_inline
bool lb6_svc_is_loadbalancer(const struct lb6_service *svc __maybe_unused)
{
#ifdef ENABLE_LOADBALANCER
return svc->flags & SVC_FLAG_LOADBALANCER;
#else
return false;
#endif /* ENABLE_LOADBALANCER */
}
static __always_inline
bool lb4_svc_is_nodeport(const struct lb4_service *svc __maybe_unused)
{
#ifdef ENABLE_NODEPORT
return svc->flags & SVC_FLAG_NODEPORT;
#else
return false;
#endif /* ENABLE_NODEPORT */
}
static __always_inline
bool lb6_svc_is_nodeport(const struct lb6_service *svc __maybe_unused)
{
#ifdef ENABLE_NODEPORT
return svc->flags & SVC_FLAG_NODEPORT;
#else
return false;
#endif /* ENABLE_NODEPORT */
}
static __always_inline
bool lb4_svc_is_external_ip(const struct lb4_service *svc __maybe_unused)
{
#ifdef ENABLE_EXTERNAL_IP
return svc->flags & SVC_FLAG_EXTERNAL_IP;
#else
return false;
#endif
}
static __always_inline
bool lb6_svc_is_external_ip(const struct lb6_service *svc __maybe_unused)
{
#ifdef ENABLE_EXTERNAL_IP
return svc->flags & SVC_FLAG_EXTERNAL_IP;
#else
return false;
#endif
}
static __always_inline
bool lb4_svc_is_hostport(const struct lb4_service *svc __maybe_unused)
{
#ifdef ENABLE_HOSTPORT
return svc->flags & SVC_FLAG_HOSTPORT;
#else
return false;
#endif /* ENABLE_HOSTPORT */
}
static __always_inline
bool lb6_svc_is_hostport(const struct lb6_service *svc __maybe_unused)
{
#ifdef ENABLE_HOSTPORT
return svc->flags & SVC_FLAG_HOSTPORT;
#else
return false;
#endif /* ENABLE_HOSTPORT */
}
static __always_inline
bool lb4_svc_has_src_range_check(const struct lb4_service *svc __maybe_unused)
{
#ifdef ENABLE_SRC_RANGE_CHECK
return svc->flags & SVC_FLAG_SOURCE_RANGE;
#else
return false;
#endif /* ENABLE_SRC_RANGE_CHECK */
}
static __always_inline
bool lb6_svc_has_src_range_check(const struct lb6_service *svc __maybe_unused)
{
#ifdef ENABLE_SRC_RANGE_CHECK
return svc->flags & SVC_FLAG_SOURCE_RANGE;
#else
return false;
#endif /* ENABLE_SRC_RANGE_CHECK */
}
static __always_inline bool lb_skip_l4_dnat(void)
{
return DSR_XLATE_MODE == DSR_XLATE_FRONTEND;
}
static __always_inline
bool lb4_svc_is_local_scope(const struct lb4_service *svc)
{
return svc->flags & SVC_FLAG_LOCAL_SCOPE;
}
static __always_inline
bool lb6_svc_is_local_scope(const struct lb6_service *svc)
{
return svc->flags & SVC_FLAG_LOCAL_SCOPE;
}
static __always_inline
bool lb4_svc_is_affinity(const struct lb4_service *svc)
{
return svc->flags & SVC_FLAG_AFFINITY;
}
static __always_inline
bool lb6_svc_is_affinity(const struct lb6_service *svc)
{
return svc->flags & SVC_FLAG_AFFINITY;
}
static __always_inline bool __lb_svc_is_routable(__u8 flags)
{
return (flags & SVC_FLAG_ROUTABLE) != 0;
}
static __always_inline
bool lb4_svc_is_routable(const struct lb4_service *svc)
{
return __lb_svc_is_routable(svc->flags);
}
static __always_inline
bool lb6_svc_is_routable(const struct lb6_service *svc)
{
return __lb_svc_is_routable(svc->flags);
}
static __always_inline
bool lb4_svc_is_localredirect(const struct lb4_service *svc __maybe_unused)
{
return svc->flags2 & SVC_FLAG_LOCALREDIRECT;
}
static __always_inline int extract_l4_port(struct __ctx_buff *ctx, __u8 nexthdr,
int l4_off,
int dir __maybe_unused,
__be16 *port,
__maybe_unused struct iphdr *ip4)
{
int ret;
switch (nexthdr) {
case IPPROTO_TCP:
case IPPROTO_UDP:
#ifdef ENABLE_IPV4_FRAGMENTS
if (ip4) {
struct ipv4_frag_l4ports ports = { };
ret = ipv4_handle_fragmentation(ctx, ip4, l4_off,
dir, &ports, NULL);
if (IS_ERR(ret))
return ret;
*port = ports.dport;
break;
}
#endif
/* Port offsets for UDP and TCP are the same */
ret = l4_load_port(ctx, l4_off + TCP_DPORT_OFF, port);
if (IS_ERR(ret))
return ret;
break;
case IPPROTO_ICMPV6:
case IPPROTO_ICMP:
/* No need to perform a service lookup for ICMP packets */
return DROP_NO_SERVICE;
default:
/* Pass unknown L4 to stack */
return DROP_UNKNOWN_L4;
}
return 0;
}
static __always_inline int reverse_map_l4_port(struct __ctx_buff *ctx, __u8 nexthdr,
__be16 port, int l4_off,
struct csum_offset *csum_off)
{
switch (nexthdr) {
case IPPROTO_TCP:
case IPPROTO_UDP:
if (port) {
__be16 old_port;
int ret;
/* Port offsets for UDP and TCP are the same */
ret = l4_load_port(ctx, l4_off + TCP_SPORT_OFF, &old_port);
if (IS_ERR(ret))
return ret;
if (port != old_port) {
ret = l4_modify_port(ctx, l4_off, TCP_SPORT_OFF,
csum_off, port, old_port);
if (IS_ERR(ret))
return ret;
}
}
break;
case IPPROTO_ICMPV6:
case IPPROTO_ICMP:
return CTX_ACT_OK;
default:
return DROP_UNKNOWN_L4;
}
return 0;
}
#ifdef ENABLE_IPV6
static __always_inline int __lb6_rev_nat(struct __ctx_buff *ctx, int l4_off,
struct csum_offset *csum_off,
struct ipv6_ct_tuple *tuple, int flags,
struct lb6_reverse_nat *nat)
{
union v6addr old_saddr;
union v6addr tmp;
__u8 *new_saddr;
__be32 sum;
int ret;
cilium_dbg_lb(ctx, DBG_LB6_REVERSE_NAT, nat->address.p4, nat->port);
if (nat->port) {
ret = reverse_map_l4_port(ctx, tuple->nexthdr, nat->port, l4_off, csum_off);
if (IS_ERR(ret))
return ret;
}
if (flags & REV_NAT_F_TUPLE_SADDR) {
ipv6_addr_copy(&old_saddr, &tuple->saddr);
ipv6_addr_copy(&tuple->saddr, &nat->address);
new_saddr = tuple->saddr.addr;
} else {
if (ipv6_load_saddr(ctx, ETH_HLEN, &old_saddr) < 0)
return DROP_INVALID;
ipv6_addr_copy(&tmp, &nat->address);
new_saddr = tmp.addr;
}
ret = ipv6_store_saddr(ctx, new_saddr, ETH_HLEN);
if (IS_ERR(ret))
return DROP_WRITE_ERROR;
sum = csum_diff(old_saddr.addr, 16, new_saddr, 16, 0);
if (csum_l4_replace(ctx, l4_off, csum_off, 0, sum, BPF_F_PSEUDO_HDR) < 0)
return DROP_CSUM_L4;
return 0;
}
/** Perform IPv6 reverse NAT based on reverse NAT index
* @arg ctx packet
* @arg l4_off offset to L4
* @arg csum_off offset to L4 checksum field
* @arg csum_flags checksum flags
* @arg index reverse NAT index
* @arg tuple tuple
* @arg saddr_tuple If set, tuple address will be updated with new source address
*/
static __always_inline int lb6_rev_nat(struct __ctx_buff *ctx, int l4_off,
struct csum_offset *csum_off, __u16 index,
struct ipv6_ct_tuple *tuple, int flags)
{
struct lb6_reverse_nat *nat;
cilium_dbg_lb(ctx, DBG_LB6_REVERSE_NAT_LOOKUP, index, 0);
nat = map_lookup_elem(&LB6_REVERSE_NAT_MAP, &index);
if (nat == NULL)
return 0;
return __lb6_rev_nat(ctx, l4_off, csum_off, tuple, flags, nat);
}
/** Extract IPv6 LB key from packet
* @arg ctx Packet
* @arg tuple Tuple
* @arg l4_off Offset to L4 header
* @arg key Pointer to store LB key in
* @arg csum_off Pointer to store L4 checksum field offset and flags
* @arg dir Flow direction
*
* Expects the ctx to be validated for direct packet access up to L4. Fills
* lb6_key based on L4 nexthdr.
*
* Returns:
* - CTX_ACT_OK on successful extraction
* - DROP_UNKNOWN_L4 if packet should be ignore (sent to stack)
* - Negative error code
*/
static __always_inline int lb6_extract_key(struct __ctx_buff *ctx __maybe_unused,
struct ipv6_ct_tuple *tuple,
int l4_off __maybe_unused,
struct lb6_key *key,
struct csum_offset *csum_off,
int dir)
{
union v6addr *addr;
/* FIXME(brb): set after adding support for different L4 protocols in LB */
key->proto = 0;
addr = (dir == CT_INGRESS) ? &tuple->saddr : &tuple->daddr;
ipv6_addr_copy(&key->address, addr);
csum_l4_offset_and_flags(tuple->nexthdr, csum_off);
return extract_l4_port(ctx, tuple->nexthdr, l4_off, dir, &key->dport,
NULL);
}
static __always_inline
bool lb6_src_range_ok(const struct lb6_service *svc __maybe_unused,
const union v6addr *saddr __maybe_unused)
{
#ifdef ENABLE_SRC_RANGE_CHECK
struct lb6_src_range_key key;
if (!lb6_svc_has_src_range_check(svc))
return true;
key = (typeof(key)) {
.lpm_key = { SRC_RANGE_STATIC_PREFIX(key), {} },
.rev_nat_id = svc->rev_nat_index,
.addr = *saddr,
};
if (map_lookup_elem(&LB6_SRC_RANGE_MAP, &key))
return true;
return false;
#else
return true;
#endif /* ENABLE_SRC_RANGE_CHECK */
}
static __always_inline
struct lb6_service *lb6_lookup_service(struct lb6_key *key,
const bool scope_switch)
{
struct lb6_service *svc;
key->scope = LB_LOOKUP_SCOPE_EXT;
key->backend_slot = 0;
svc = map_lookup_elem(&LB6_SERVICES_MAP_V2, key);
if (svc) {
if (!scope_switch || !lb6_svc_is_local_scope(svc))
return svc->count ? svc : NULL;
key->scope = LB_LOOKUP_SCOPE_INT;
svc = map_lookup_elem(&LB6_SERVICES_MAP_V2, key);
if (svc && svc->count)
return svc;
}
return NULL;
}
static __always_inline struct lb6_backend *__lb6_lookup_backend(__u16 backend_id)
{
return map_lookup_elem(&LB6_BACKEND_MAP, &backend_id);
}
static __always_inline struct lb6_backend *
lb6_lookup_backend(struct __ctx_buff *ctx __maybe_unused, __u16 backend_id)
{
struct lb6_backend *backend;
backend = __lb6_lookup_backend(backend_id);
if (!backend)
cilium_dbg_lb(ctx, DBG_LB6_LOOKUP_BACKEND_FAIL, backend_id, 0);
return backend;
}
static __always_inline
struct lb6_service *__lb6_lookup_backend_slot(struct lb6_key *key)
{
return map_lookup_elem(&LB6_SERVICES_MAP_V2, key);
}
static __always_inline
struct lb6_service *lb6_lookup_backend_slot(struct __ctx_buff *ctx __maybe_unused,
struct lb6_key *key, __u16 slot)
{
struct lb6_service *svc;
key->backend_slot = slot;
cilium_dbg_lb(ctx, DBG_LB6_LOOKUP_BACKEND_SLOT, key->backend_slot, key->dport);
svc = __lb6_lookup_backend_slot(key);
if (svc)
return svc;
cilium_dbg_lb(ctx, DBG_LB6_LOOKUP_BACKEND_SLOT_V2_FAIL, key->backend_slot, key->dport);
return NULL;
}
/* Backend slot 0 is always reserved for the service frontend. */
#if LB_SELECTION == LB_SELECTION_RANDOM
static __always_inline __u16
lb6_select_backend_id(struct __ctx_buff *ctx,
struct lb6_key *key,
const struct ipv6_ct_tuple *tuple __maybe_unused,
const struct lb6_service *svc)
{
__u32 slot = (get_prandom_u32() % svc->count) + 1;
struct lb6_service *be = lb6_lookup_backend_slot(ctx, key, slot);
return be ? be->backend_id : 0;
}
#elif LB_SELECTION == LB_SELECTION_MAGLEV
static __always_inline __u16
lb6_select_backend_id(struct __ctx_buff *ctx __maybe_unused,
struct lb6_key *key __maybe_unused,
const struct ipv6_ct_tuple *tuple,
const struct lb6_service *svc)
{
__u32 zero = 0, index = svc->rev_nat_index;
__u16 *backend_ids;
void *maglev_lut;
maglev_lut = map_lookup_elem(&LB6_MAGLEV_MAP_OUTER, &index);
if (unlikely(!maglev_lut))
return 0;
backend_ids = map_lookup_elem(maglev_lut, &zero);
if (unlikely(!backend_ids))
return 0;
index = hash_from_tuple_v6(tuple) % LB_MAGLEV_LUT_SIZE;
return map_array_get_16(backend_ids, index, (LB_MAGLEV_LUT_SIZE - 1) << 1);
}
#else
# error "Invalid load balancer backend selection algorithm!"
#endif /* LB_SELECTION */
static __always_inline int lb6_xlate(struct __ctx_buff *ctx,
union v6addr *new_dst, __u8 nexthdr,
int l3_off, int l4_off,
struct csum_offset *csum_off,
const struct lb6_key *key,
const struct lb6_backend *backend,
const bool skip_l3_xlate)
{
if (skip_l3_xlate)
goto l4_xlate;
ipv6_store_daddr(ctx, new_dst->addr, l3_off);
if (csum_off) {
__be32 sum = csum_diff(key->address.addr, 16, new_dst->addr,
16, 0);
if (csum_l4_replace(ctx, l4_off, csum_off, 0, sum,
BPF_F_PSEUDO_HDR) < 0)
return DROP_CSUM_L4;
}
l4_xlate:
if (likely(backend->port) && key->dport != backend->port &&
(nexthdr == IPPROTO_TCP || nexthdr == IPPROTO_UDP)) {
__be16 tmp = backend->port;
int ret;
/* Port offsets for UDP and TCP are the same */
ret = l4_modify_port(ctx, l4_off, TCP_DPORT_OFF, csum_off,
tmp, key->dport);
if (IS_ERR(ret))
return ret;
}
return CTX_ACT_OK;
}
#ifdef ENABLE_SESSION_AFFINITY
static __always_inline __u32
__lb6_affinity_backend_id(const struct lb6_service *svc, bool netns_cookie,
union lb6_affinity_client_id *id)
{
struct lb6_affinity_key key = {
.rev_nat_id = svc->rev_nat_index,
.netns_cookie = netns_cookie,
};
struct lb_affinity_val *val;
ipv6_addr_copy(&key.client_id.client_ip, &id->client_ip);
val = map_lookup_elem(&LB6_AFFINITY_MAP, &key);
if (val != NULL) {
__u32 now = bpf_mono_now();
struct lb_affinity_match match = {
.rev_nat_id = svc->rev_nat_index,
.backend_id = val->backend_id,
};
if (READ_ONCE(val->last_used) +
bpf_sec_to_mono(svc->affinity_timeout) <= now) {
map_delete_elem(&LB6_AFFINITY_MAP, &key);
return 0;
}
if (!map_lookup_elem(&LB_AFFINITY_MATCH_MAP, &match)) {
map_delete_elem(&LB6_AFFINITY_MAP, &key);
return 0;
}
WRITE_ONCE(val->last_used, now);
return val->backend_id;
}
return 0;
}
static __always_inline __u32
lb6_affinity_backend_id_by_addr(const struct lb6_service *svc,
union lb6_affinity_client_id *id)
{
return __lb6_affinity_backend_id(svc, false, id);
}
static __always_inline void
__lb6_update_affinity(const struct lb6_service *svc, bool netns_cookie,
union lb6_affinity_client_id *id, __u32 backend_id)
{
__u32 now = bpf_mono_now();
struct lb6_affinity_key key = {
.rev_nat_id = svc->rev_nat_index,
.netns_cookie = netns_cookie,
};
struct lb_affinity_val val = {
.backend_id = backend_id,
.last_used = now,
};
ipv6_addr_copy(&key.client_id.client_ip, &id->client_ip);
map_update_elem(&LB6_AFFINITY_MAP, &key, &val, 0);
}
static __always_inline void
lb6_update_affinity_by_addr(const struct lb6_service *svc,
union lb6_affinity_client_id *id, __u32 backend_id)
{
__lb6_update_affinity(svc, false, id, backend_id);
}
#endif /* ENABLE_SESSION_AFFINITY */
static __always_inline __u32
lb6_affinity_backend_id_by_netns(const struct lb6_service *svc __maybe_unused,
union lb6_affinity_client_id *id __maybe_unused)
{
#if defined(ENABLE_SESSION_AFFINITY)
return __lb6_affinity_backend_id(svc, true, id);
#else
return 0;
#endif
}
static __always_inline void
lb6_update_affinity_by_netns(const struct lb6_service *svc __maybe_unused,
union lb6_affinity_client_id *id __maybe_unused,
__u32 backend_id __maybe_unused)
{
#if defined(ENABLE_SESSION_AFFINITY)
__lb6_update_affinity(svc, true, id, backend_id);
#endif
}
static __always_inline int lb6_local(const void *map, struct __ctx_buff *ctx,
int l3_off, int l4_off,
struct csum_offset *csum_off,
struct lb6_key *key,
struct ipv6_ct_tuple *tuple,
const struct lb6_service *svc,
struct ct_state *state,
const bool skip_l3_xlate)
{
__u32 monitor; /* Deliberately ignored; regular CT will determine monitoring. */
union v6addr *addr;
__u8 flags = tuple->flags;
struct lb6_backend *backend;
__u32 backend_id = 0;
int ret;
#ifdef ENABLE_SESSION_AFFINITY
union lb6_affinity_client_id client_id;
ipv6_addr_copy(&client_id.client_ip, &tuple->saddr);
#endif
/* See lb4_local comments re svc endpoint lookup process */
ret = ct_lookup6(map, tuple, ctx, l4_off, CT_SERVICE, state, &monitor);
switch (ret) {
case CT_NEW:
#ifdef ENABLE_SESSION_AFFINITY
if (lb6_svc_is_affinity(svc)) {
backend_id = lb6_affinity_backend_id_by_addr(svc, &client_id);
if (backend_id != 0) {
backend = lb6_lookup_backend(ctx, backend_id);
if (backend == NULL)
backend_id = 0;
}
}
#endif
if (backend_id == 0) {
backend_id = lb6_select_backend_id(ctx, key, tuple, svc);
backend = lb6_lookup_backend(ctx, backend_id);
if (backend == NULL)
goto drop_no_service;
}
state->backend_id = backend_id;
state->rev_nat_index = svc->rev_nat_index;
ret = ct_create6(map, NULL, tuple, ctx, CT_SERVICE, state, false);
/* Fail closed, if the conntrack entry create fails drop
* service lookup.
*/
if (IS_ERR(ret))
goto drop_no_service;
goto update_state;
case CT_REOPENED:
case CT_ESTABLISHED:
case CT_RELATED:
case CT_REPLY:
/* See lb4_local comment */
if (state->rev_nat_index == 0) {
state->rev_nat_index = svc->rev_nat_index;
ct_update6_rev_nat_index(map, tuple, state);
}
break;
default:
goto drop_no_service;
}
/* See lb4_local comment */
if (state->rev_nat_index != svc->rev_nat_index) {
#ifdef ENABLE_SESSION_AFFINITY
if (lb6_svc_is_affinity(svc))
backend_id = lb6_affinity_backend_id_by_addr(svc,
&client_id);
#endif
if (!backend_id) {
backend_id = lb6_select_backend_id(ctx, key, tuple, svc);
if (!backend_id)
goto drop_no_service;
}
state->backend_id = backend_id;
ct_update6_backend_id(map, tuple, state);
state->rev_nat_index = svc->rev_nat_index;
ct_update6_rev_nat_index(map, tuple, state);
}
/* If the lookup fails it means the user deleted the backend out from
* underneath us. To resolve this fall back to hash. If this is a TCP
* session we are likely to get a TCP RST.
*/
backend = lb6_lookup_backend(ctx, state->backend_id);
if (!backend) {
key->backend_slot = 0;
svc = lb6_lookup_service(key, false);
if (!svc)
goto drop_no_service;
backend_id = lb6_select_backend_id(ctx, key, tuple, svc);
backend = lb6_lookup_backend(ctx, backend_id);
if (!backend)
goto drop_no_service;
state->backend_id = backend_id;
ct_update6_backend_id(map, tuple, state);
}
update_state:
/* Restore flags so that SERVICE flag is only used in used when the
* service lookup happens and future lookups use EGRESS or INGRESS.
*/
tuple->flags = flags;
ipv6_addr_copy(&tuple->daddr, &backend->address);
addr = &tuple->daddr;
state->rev_nat_index = svc->rev_nat_index;
#ifdef ENABLE_SESSION_AFFINITY
if (lb6_svc_is_affinity(svc))
lb6_update_affinity_by_addr(svc, &client_id,
state->backend_id);
#endif
return lb_skip_l4_dnat() ? CTX_ACT_OK :
lb6_xlate(ctx, addr, tuple->nexthdr, l3_off, l4_off,
csum_off, key, backend, skip_l3_xlate);
drop_no_service:
tuple->flags = flags;
return DROP_NO_SERVICE;
}
#else
/* Stubs for v4-in-v6 socket cgroup hook case when only v4 is enabled to avoid
* additional map management.
*/
static __always_inline
struct lb6_service *lb6_lookup_service(struct lb6_key *key __maybe_unused,
const bool scope_switch __maybe_unused)
{
return NULL;
}
static __always_inline
struct lb6_service *__lb6_lookup_backend_slot(struct lb6_key *key __maybe_unused)
{
return NULL;
}
static __always_inline struct lb6_backend *
__lb6_lookup_backend(__u16 backend_id __maybe_unused)
{
return NULL;
}
#endif /* ENABLE_IPV6 */
#ifdef ENABLE_IPV4
static __always_inline int __lb4_rev_nat(struct __ctx_buff *ctx, int l3_off, int l4_off,
struct csum_offset *csum_off,
struct ipv4_ct_tuple *tuple, int flags,
const struct lb4_reverse_nat *nat,
const struct ct_state *ct_state, bool has_l4_header)
{
__be32 old_sip, new_sip, sum = 0;
int ret;
cilium_dbg_lb(ctx, DBG_LB4_REVERSE_NAT, nat->address, nat->port);
if (nat->port && has_l4_header) {
ret = reverse_map_l4_port(ctx, tuple->nexthdr, nat->port, l4_off, csum_off);
if (IS_ERR(ret))
return ret;
}
if (flags & REV_NAT_F_TUPLE_SADDR) {
old_sip = tuple->saddr;
tuple->saddr = new_sip = nat->address;
} else {
ret = ctx_load_bytes(ctx, l3_off + offsetof(struct iphdr, saddr), &old_sip, 4);
if (IS_ERR(ret))
return ret;
new_sip = nat->address;
}
if (ct_state->loopback) {
/* The packet was looped back to the sending endpoint on the
* forward service translation. This implies that the original
* source address of the packet is the source address of the
* current packet. We therefore need to make the current source
* address the new destination address.
*/
__be32 old_dip;
ret = ctx_load_bytes(ctx, l3_off + offsetof(struct iphdr, daddr), &old_dip, 4);
if (IS_ERR(ret))
return ret;
cilium_dbg_lb(ctx, DBG_LB4_LOOPBACK_SNAT_REV, old_dip, old_sip);
ret = ctx_store_bytes(ctx, l3_off + offsetof(struct iphdr, daddr), &old_sip, 4, 0);
if (IS_ERR(ret))
return DROP_WRITE_ERROR;
sum = csum_diff(&old_dip, 4, &old_sip, 4, 0);
/* Update the tuple address which is representing the destination address */
tuple->saddr = old_sip;
}
ret = ctx_store_bytes(ctx, l3_off + offsetof(struct iphdr, saddr),
&new_sip, 4, 0);
if (IS_ERR(ret))
return DROP_WRITE_ERROR;
sum = csum_diff(&old_sip, 4, &new_sip, 4, sum);
if (l3_csum_replace(ctx, l3_off + offsetof(struct iphdr, check), 0, sum, 0) < 0)
return DROP_CSUM_L3;
if (csum_off->offset &&
csum_l4_replace(ctx, l4_off, csum_off, 0, sum, BPF_F_PSEUDO_HDR) < 0)
return DROP_CSUM_L4;
return 0;
}