Skip to content

Commit

Permalink
This adds a new bpf map cilium_lb4_backends_v2
Browse files Browse the repository at this point in the history
The V2 map key is u32 typed instead of u16.

Upon agent (re)start:
- we restore backends from v1 map and copy all
entries from v1 map to v2 map if v1 map exists
(this is the upgrade scenario).
- we then remove v1 map and operate on v2 map.

Signed-off-by: Weilong Cui <cuiwl@google.com>
  • Loading branch information
Weil0ng authored and jibi committed Sep 28, 2021
1 parent f900a92 commit dee982f
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 119 deletions.
12 changes: 6 additions & 6 deletions bpf/include/bpf/access.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include "compiler.h"

#if !defined(__non_bpf_context) && defined(__bpf__)
static __always_inline __maybe_unused __u16
map_array_get_16(const __u16 *array, __u32 index, const __u32 limit)
static __always_inline __maybe_unused __u32
map_array_get_32(const __u32 *array, __u32 index, const __u32 limit)
{
__u16 datum = 0;
__u32 datum = 0;

if (__builtin_constant_p(index) ||
!__builtin_constant_p(limit))
Expand All @@ -21,17 +21,17 @@ map_array_get_16(const __u16 *array, __u32 index, const __u32 limit)
* for this util function, so we never fail here, and returned datum is
* always valid.
*/
asm volatile("%[index] <<= 1\n\t"
asm volatile("%[index] <<= 2\n\t"
"if %[index] > %[limit] goto +1\n\t"
"%[array] += %[index]\n\t"
"%[datum] = *(u16 *)(%[array] + 0)\n\t"
"%[datum] = *(u32 *)(%[array] + 0)\n\t"
: [datum]"=r"(datum)
: [limit]"i"(limit), [array]"r"(array), [index]"r"(index)
: /* no clobbers */ );

return datum;
}
#else
# define map_array_get_16(array, index, limit) __throw_build_bug()
# define map_array_get_32(array, index, limit) __throw_build_bug()
#endif /* !__non_bpf_context && __bpf__ */
#endif /* __BPF_ACCESS_H_ */
2 changes: 1 addition & 1 deletion bpf/lib/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ struct ct_state {
__be32 svc_addr;
__u32 src_sec_id;
__u16 ifindex;
__u16 backend_id; /* Backend ID in lb4_backends */
__u32 backend_id; /* Backend ID in lb4_backends */
};

#define SRC_RANGE_STATIC_PREFIX(STRUCT) \
Expand Down
40 changes: 20 additions & 20 deletions bpf/lib/lb.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct bpf_elf_map __section_maps LB6_SERVICES_MAP_V2 = {
.flags = CONDITIONAL_PREALLOC,
};

struct bpf_elf_map __section_maps LB6_BACKEND_MAP = {
struct bpf_elf_map __section_maps LB6_BACKEND_MAP_V2 = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_key = sizeof(__u32),
.size_value = sizeof(struct lb6_backend),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
Expand Down Expand Up @@ -73,7 +73,7 @@ struct bpf_elf_map __section_maps LB6_HEALTH_MAP = {
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,
.size_value = sizeof(__u32) * LB_MAGLEV_LUT_SIZE,
.pinning = PIN_NONE,
.max_elem = 1,
.inner_idx = NO_PREPOPULATE,
Expand Down Expand Up @@ -111,9 +111,9 @@ struct bpf_elf_map __section_maps LB4_SERVICES_MAP_V2 = {
.flags = CONDITIONAL_PREALLOC,
};

struct bpf_elf_map __section_maps LB4_BACKEND_MAP = {
struct bpf_elf_map __section_maps LB4_BACKEND_MAP_V2 = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(__u16),
.size_key = sizeof(__u32),
.size_value = sizeof(struct lb4_backend),
.pinning = PIN_GLOBAL_NS,
.max_elem = CILIUM_LB_MAP_MAX_ENTRIES,
Expand Down Expand Up @@ -155,7 +155,7 @@ struct bpf_elf_map __section_maps LB4_HEALTH_MAP = {
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,
.size_value = sizeof(__u32) * LB_MAGLEV_LUT_SIZE,
.pinning = PIN_NONE,
.max_elem = 1,
.inner_idx = NO_PREPOPULATE,
Expand Down Expand Up @@ -548,13 +548,13 @@ struct lb6_service *lb6_lookup_service(struct lb6_key *key,
return NULL;
}

static __always_inline struct lb6_backend *__lb6_lookup_backend(__u16 backend_id)
static __always_inline struct lb6_backend *__lb6_lookup_backend(__u32 backend_id)
{
return map_lookup_elem(&LB6_BACKEND_MAP, &backend_id);
return map_lookup_elem(&LB6_BACKEND_MAP_V2, &backend_id);
}

static __always_inline struct lb6_backend *
lb6_lookup_backend(struct __ctx_buff *ctx __maybe_unused, __u16 backend_id)
lb6_lookup_backend(struct __ctx_buff *ctx __maybe_unused, __u32 backend_id)
{
struct lb6_backend *backend;

Expand Down Expand Up @@ -590,7 +590,7 @@ struct lb6_service *lb6_lookup_backend_slot(struct __ctx_buff *ctx __maybe_unuse

/* Backend slot 0 is always reserved for the service frontend. */
#if LB_SELECTION == LB_SELECTION_RANDOM
static __always_inline __u16
static __always_inline __u32
lb6_select_backend_id(struct __ctx_buff *ctx,
struct lb6_key *key,
const struct ipv6_ct_tuple *tuple __maybe_unused,
Expand All @@ -602,14 +602,14 @@ lb6_select_backend_id(struct __ctx_buff *ctx,
return be ? be->backend_id : 0;
}
#elif LB_SELECTION == LB_SELECTION_MAGLEV
static __always_inline __u16
static __always_inline __u32
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;
__u32 *backend_ids;
void *maglev_lut;

maglev_lut = map_lookup_elem(&LB6_MAGLEV_MAP_OUTER, &index);
Expand All @@ -621,7 +621,7 @@ lb6_select_backend_id(struct __ctx_buff *ctx __maybe_unused,
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);
return map_array_get_32(backend_ids, index, (LB_MAGLEV_LUT_SIZE - 1) << 2);
}
#else
# error "Invalid load balancer backend selection algorithm!"
Expand Down Expand Up @@ -1076,13 +1076,13 @@ struct lb4_service *lb4_lookup_service(struct lb4_key *key,
return NULL;
}

static __always_inline struct lb4_backend *__lb4_lookup_backend(__u16 backend_id)
static __always_inline struct lb4_backend *__lb4_lookup_backend(__u32 backend_id)
{
return map_lookup_elem(&LB4_BACKEND_MAP, &backend_id);
return map_lookup_elem(&LB4_BACKEND_MAP_V2, &backend_id);
}

static __always_inline struct lb4_backend *
lb4_lookup_backend(struct __ctx_buff *ctx __maybe_unused, __u16 backend_id)
lb4_lookup_backend(struct __ctx_buff *ctx __maybe_unused, __u32 backend_id)
{
struct lb4_backend *backend;

Expand Down Expand Up @@ -1118,7 +1118,7 @@ struct lb4_service *lb4_lookup_backend_slot(struct __ctx_buff *ctx __maybe_unuse

/* Backend slot 0 is always reserved for the service frontend. */
#if LB_SELECTION == LB_SELECTION_RANDOM
static __always_inline __u16
static __always_inline __u32
lb4_select_backend_id(struct __ctx_buff *ctx,
struct lb4_key *key,
const struct ipv4_ct_tuple *tuple __maybe_unused,
Expand All @@ -1130,14 +1130,14 @@ lb4_select_backend_id(struct __ctx_buff *ctx,
return be ? be->backend_id : 0;
}
#elif LB_SELECTION == LB_SELECTION_MAGLEV
static __always_inline __u16
static __always_inline __u32
lb4_select_backend_id(struct __ctx_buff *ctx __maybe_unused,
struct lb4_key *key __maybe_unused,
const struct ipv4_ct_tuple *tuple,
const struct lb4_service *svc)
{
__u32 zero = 0, index = svc->rev_nat_index;
__u16 *backend_ids;
__u32 *backend_ids;
void *maglev_lut;

maglev_lut = map_lookup_elem(&LB4_MAGLEV_MAP_OUTER, &index);
Expand All @@ -1149,7 +1149,7 @@ lb4_select_backend_id(struct __ctx_buff *ctx __maybe_unused,
return 0;

index = hash_from_tuple_v4(tuple) % LB_MAGLEV_LUT_SIZE;
return map_array_get_16(backend_ids, index, (LB_MAGLEV_LUT_SIZE - 1) << 1);
return map_array_get_32(backend_ids, index, (LB_MAGLEV_LUT_SIZE - 1) << 2);
}
#else
# error "Invalid load balancer backend selection algorithm!"
Expand Down
4 changes: 2 additions & 2 deletions bpf/node_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ DEFINE_IPV6(HOST_IP, 0xbe, 0xef, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xa, 0x
#define EP_POLICY_MAP test_cilium_ep_to_policy
#define LB6_REVERSE_NAT_MAP test_cilium_lb6_reverse_nat
#define LB6_SERVICES_MAP_V2 test_cilium_lb6_services
#define LB6_BACKEND_MAP test_cilium_lb6_backends
#define LB6_BACKEND_MAP_V2 test_cilium_lb6_backends
#define LB6_REVERSE_NAT_SK_MAP test_cilium_lb6_reverse_sk
#define LB6_REVERSE_NAT_SK_MAP_SIZE 262144
#define LB4_REVERSE_NAT_MAP test_cilium_lb4_reverse_nat
#define LB4_SERVICES_MAP_V2 test_cilium_lb4_services
#define LB4_BACKEND_MAP test_cilium_lb4_backends
#define LB4_BACKEND_MAP_V2 test_cilium_lb4_backends
#define LB4_REVERSE_NAT_SK_MAP test_cilium_lb4_reverse_sk
#define LB4_REVERSE_NAT_SK_MAP_SIZE 262144
#define LB4_AFFINITY_MAP test_cilium_lb4_affinity
Expand Down
2 changes: 2 additions & 0 deletions bugtool/cmd/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func defaultCommands(confDir string, cmdDir string, k8sPods []string) []string {
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_egress_v4", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb4_services_v2", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb4_services", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb4_backends_v2", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb4_backends", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb4_reverse_nat", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_ct4_global", bpffsMountpoint),
Expand All @@ -168,6 +169,7 @@ func defaultCommands(confDir string, cmdDir string, k8sPods []string) []string {
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb_affinity_match", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb6_services_v2", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb6_services", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb6_backends_v2", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb6_backends", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_lb6_reverse_nat", bpffsMountpoint),
fmt.Sprintf("bpftool map dump pinned %s/tc/globals/cilium_ct6_global", bpffsMountpoint),
Expand Down
4 changes: 2 additions & 2 deletions cilium/cmd/bpf_lb_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func dumpSVC(serviceList map[string][]string) {
id := key.(lbmap.BackendKey).GetID()
backendMap[id] = value.DeepCopyMapValue().(lbmap.BackendValue).ToHost()
}
if err := lbmap.Backend4Map.DumpWithCallbackIfExists(parseBackendEntry); err != nil {
if err := lbmap.Backend4MapV2.DumpWithCallbackIfExists(parseBackendEntry); err != nil {
Fatalf("Unable to dump IPv4 backends table: %s", err)
}
if err := lbmap.Backend6Map.DumpWithCallbackIfExists(parseBackendEntry); err != nil {
if err := lbmap.Backend6MapV2.DumpWithCallbackIfExists(parseBackendEntry); err != nil {
Fatalf("Unable to dump IPv6 backends table: %s", err)
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ func (d *Daemon) getBPFMapStatus() *models.BPFMapStatus {
Size: int64(lbmap.MaxEntries),
},
{
Name: "IPv4 service backend", // cilium_lb4_backends
Name: "IPv4 service backend", // cilium_lb4_backends_v2
Size: int64(lbmap.MaxEntries),
},
{
Name: "IPv6 service backend", // cilium_lb6_backends
Name: "IPv6 service backend", // cilium_lb6_backends_v2
Size: int64(lbmap.MaxEntries),
},
{
Expand Down
2 changes: 2 additions & 0 deletions pkg/datapath/alignchecker/alignchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func CheckStructAlignments(path string) error {
reflect.TypeOf(lbmap.RevNat6Key{}),
},
"__u32": {
reflect.TypeOf(lbmap.Backend4KeyV2{}),
reflect.TypeOf(lbmap.Backend6KeyV2{}),
reflect.TypeOf(signalmap.Key{}),
reflect.TypeOf(signalmap.Value{}),
reflect.TypeOf(eventsmap.Key{}),
Expand Down
4 changes: 2 additions & 2 deletions pkg/datapath/linux/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ func (h *HeaderfileWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeC
cDefinesMap["EP_POLICY_MAP"] = eppolicymap.MapName
cDefinesMap["LB6_REVERSE_NAT_MAP"] = "cilium_lb6_reverse_nat"
cDefinesMap["LB6_SERVICES_MAP_V2"] = "cilium_lb6_services_v2"
cDefinesMap["LB6_BACKEND_MAP"] = "cilium_lb6_backends"
cDefinesMap["LB6_BACKEND_MAP_V2"] = "cilium_lb6_backends_v2"
cDefinesMap["LB6_REVERSE_NAT_SK_MAP"] = lbmap.SockRevNat6MapName
cDefinesMap["LB6_REVERSE_NAT_SK_MAP_SIZE"] = fmt.Sprintf("%d", lbmap.MaxSockRevNat6MapEntries)
cDefinesMap["LB4_REVERSE_NAT_MAP"] = "cilium_lb4_reverse_nat"
cDefinesMap["LB4_SERVICES_MAP_V2"] = "cilium_lb4_services_v2"
cDefinesMap["LB4_BACKEND_MAP"] = "cilium_lb4_backends"
cDefinesMap["LB4_BACKEND_MAP_V2"] = "cilium_lb4_backends_v2"
cDefinesMap["LB4_REVERSE_NAT_SK_MAP"] = lbmap.SockRevNat4MapName
cDefinesMap["LB4_REVERSE_NAT_SK_MAP_SIZE"] = fmt.Sprintf("%d", lbmap.MaxSockRevNat4MapEntries)

Expand Down
4 changes: 2 additions & 2 deletions pkg/datapath/maps/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ms *MapSweeper) RemoveDisabledMaps() {
"cilium_lb6_services",
"cilium_lb6_services_v2",
"cilium_lb6_rr_seq_v2",
"cilium_lb6_backends",
"cilium_lb6_backends_v2",
"cilium_lb6_reverse_sk",
"cilium_snat_v6_external",
"cilium_proxy6",
Expand All @@ -158,7 +158,7 @@ func (ms *MapSweeper) RemoveDisabledMaps() {
"cilium_lb4_services",
"cilium_lb4_services_v2",
"cilium_lb4_rr_seq_v2",
"cilium_lb4_backends",
"cilium_lb4_backends_v2",
"cilium_lb4_reverse_sk",
"cilium_snat_v4_external",
"cilium_proxy4",
Expand Down
2 changes: 1 addition & 1 deletion pkg/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type FEPortName string
type ServiceID uint16

// BackendID is the backend's ID.
type BackendID uint16
type BackendID uint32

// ID is the ID of L3n4Addr endpoint (either service or backend).
type ID uint32
Expand Down
9 changes: 5 additions & 4 deletions pkg/maps/lbmap/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/types"
)

Expand Down Expand Up @@ -73,9 +74,9 @@ func initAffinity(params InitParams) {
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type AffinityMatchKey struct {
BackendID uint32 `align:"backend_id"`
RevNATID uint16 `align:"rev_nat_id"`
Pad uint16 `align:"pad"`
BackendID loadbalancer.BackendID `align:"backend_id"`
RevNATID uint16 `align:"rev_nat_id"`
Pad uint16 `align:"pad"`
}

// +k8s:deepcopy-gen=true
Expand All @@ -85,7 +86,7 @@ type AffinityMatchValue struct {
}

// NewAffinityMatchKey creates the AffinityMatch key
func NewAffinityMatchKey(revNATID uint16, backendID uint32) *AffinityMatchKey {
func NewAffinityMatchKey(revNATID uint16, backendID loadbalancer.BackendID) *AffinityMatchKey {
return &AffinityMatchKey{
BackendID: backendID,
RevNATID: revNATID,
Expand Down

0 comments on commit dee982f

Please sign in to comment.