forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
145 lines (107 loc) · 2.79 KB
/
map.go
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
// Copyright (c) 2020 Tigera, Inc. All rights reserved.
// 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 arp
import (
"encoding/binary"
"fmt"
"net"
log "github.com/sirupsen/logrus"
"github.com/dtest11/calico/felix/bpf/maps"
)
func init() {
maps.SetSize(MapParams.VersionedName(), MapParams.MaxEntries)
}
var MapParams = maps.MapParameters{
Type: "lru_hash",
KeySize: KeySize,
ValueSize: ValueSize,
MaxEntries: 10000, // max number of nodes that can forward nodeports to a single node
Name: "cali_v4_arp",
Version: 2,
}
func Map() maps.Map {
return maps.NewPinnedMap(MapParams)
}
const KeySize = 8
type Key [KeySize]byte
func NewKey(ip net.IP, ifIndex uint32) Key {
var k Key
ip = ip.To4()
if len(ip) != 4 {
log.WithField("ip", ip).Panic("Bad IP")
}
copy(k[:4], ip)
binary.LittleEndian.PutUint32(k[4:8], ifIndex)
return k
}
func (k Key) IP() net.IP {
return net.IP(k[:4])
}
func (k Key) IfIndex() uint32 {
return binary.LittleEndian.Uint32(k[4:8])
}
func (k Key) String() string {
return fmt.Sprintf("ip %s ifindex %d", k.IP(), k.IfIndex())
}
func (k Key) AsBytes() []byte {
return k[:]
}
const ValueSize = 12
type Value [ValueSize]byte
func NewValue(macSrc, macDst net.HardwareAddr) Value {
var v Value
copy(v[0:6], macSrc)
copy(v[6:12], macDst)
return v
}
func (v Value) SrcMAC() net.HardwareAddr {
return net.HardwareAddr(v[0:6])
}
func (v Value) DstMAC() net.HardwareAddr {
return net.HardwareAddr(v[6:12])
}
func (v Value) String() string {
return fmt.Sprintf("src: %s dst %s", v.SrcMAC(), v.DstMAC())
}
func (v Value) AsBytes() []byte {
return v[:]
}
type MapMem map[Key]Value
// LoadMapMem loads ConntrackMap into memory
func LoadMapMem(m maps.Map) (MapMem, error) {
ret := make(MapMem)
err := m.Iter(func(k, v []byte) maps.IteratorAction {
ks := len(Key{})
vs := len(Value{})
var key Key
copy(key[:ks], k[:ks])
var val Value
copy(val[:vs], v[:vs])
ret[key] = val
return maps.IterNone
})
return ret, err
}
// MapMemIter returns maps.MapIter that loads the provided MapMem
func MapMemIter(m MapMem) maps.IterCallback {
ks := len(Key{})
vs := len(Value{})
return func(k, v []byte) maps.IteratorAction {
var key Key
copy(key[:ks], k[:ks])
var val Value
copy(val[:vs], v[:vs])
m[key] = val
return maps.IterNone
}
}