<commit_before>// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lbmap

import (
	"fmt"
	"unsafe"

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

const (
	AffinityMatchMapName = "cilium_lb_affinity_match"
	Affinity4MapName     = "cilium_lb4_affinity"
	Affinity6MapName     = "cilium_lb6_affinity"
)

var (
	AffinityMatchMap = bpf.NewMap(
		AffinityMatchMapName,
		bpf.MapTypeHash,
		&AffinityMatchKey{},
		int(unsafe.Sizeof(AffinityMatchKey{})),
		&AffinityMatchValue{},
		int(unsafe.Sizeof(AffinityMatchValue{})),
		MaxEntries,
		0, 0,
		func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
			aKey, aVal := mapKey.(*AffinityMatchKey), mapValue.(*AffinityMatchValue)

			if _, _, err := bpf.ConvertKeyValue(key, value, aKey, aVal); err != nil {
				return nil, nil, err
			}

			return aKey.ToNetwork(), aVal, nil
		}).WithCache()
	Affinity4Map = bpf.NewMap(
		Affinity4MapName,
		bpf.MapTypeLRUHash,
		&Affinity4Key{},
		int(unsafe.Sizeof(Affinity4Key{})),
		&AffinityValue{},
		int(unsafe.Sizeof(AffinityValue{})),
		MaxEntries,
		0,
		0,
		bpf.ConvertKeyValue,
	)
	Affinity6Map = bpf.NewMap(
		Affinity4MapName,
		bpf.MapTypeLRUHash,
		&Affinity6Key{},
		int(unsafe.Sizeof(Affinity4Key{})),
		&AffinityValue{},
		int(unsafe.Sizeof(AffinityValue{})),
		MaxEntries,
		0,
		0,
		bpf.ConvertKeyValue,
	)
)

// +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"`
}

// +k8s:deepcopy-gen=true<commit_msg>lbmap: Fix Affinity6Map key size and name<commit_after>// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lbmap

import (
	"fmt"
	"unsafe"

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

const (
	AffinityMatchMapName = "cilium_lb_affinity_match"
	Affinity4MapName     = "cilium_lb4_affinity"
	Affinity6MapName     = "cilium_lb6_affinity"
)

var (
	AffinityMatchMap = bpf.NewMap(
		AffinityMatchMapName,
		bpf.MapTypeHash,
		&AffinityMatchKey{},
		int(unsafe.Sizeof(AffinityMatchKey{})),
		&AffinityMatchValue{},
		int(unsafe.Sizeof(AffinityMatchValue{})),
		MaxEntries,
		0, 0,
		func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
			aKey, aVal := mapKey.(*AffinityMatchKey), mapValue.(*AffinityMatchValue)

			if _, _, err := bpf.ConvertKeyValue(key, value, aKey, aVal); err != nil {
				return nil, nil, err
			}

			return aKey.ToNetwork(), aVal, nil
		}).WithCache()
	Affinity4Map = bpf.NewMap(
		Affinity4MapName,
		bpf.MapTypeLRUHash,
		&Affinity4Key{},
		int(unsafe.Sizeof(Affinity4Key{})),
		&AffinityValue{},
		int(unsafe.Sizeof(AffinityValue{})),
		MaxEntries,
		0,
		0,
		bpf.ConvertKeyValue,
	)
	Affinity6Map = bpf.NewMap(
		Affinity6MapName,
		bpf.MapTypeLRUHash,
		&Affinity6Key{},
		int(unsafe.Sizeof(Affinity6Key{})),
		&AffinityValue{},
		int(unsafe.Sizeof(AffinityValue{})),
		MaxEntries,
		0,
		0,
		bpf.ConvertKeyValue,
	)
)

// +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"`
}

// +k8s:deepcopy-gen=true
{'fraction_non_alphanumeric': 0.13042525208241998, 'fraction_numerical': 0.015563349408154318, 'mean_word_length': 4.250863060989643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5599588', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1643, 'n_words': 369}