forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
161 lines (146 loc) · 3.87 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2020-2022 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 state
import (
"unsafe"
log "github.com/sirupsen/logrus"
"github.com/dtest11/calico/felix/bpf/maps"
)
func init() {
maps.SetSize(MapParameters.VersionedName(), MapParameters.MaxEntries)
}
type PolicyResult int32
const (
PolicyNoMatch PolicyResult = iota
PolicyAllow
PolicyDeny
PolicyTailCallFailed = 10
MaxRuleIDs = 32
)
// struct cali_tc_state {
// __be32 ip_src;
// __be32 ip_src1;
// __be32 ip_src2;
// __be32 ip_src3;
// __be32 ip_dst;
// __be32 ip_dst1;
// __be32 ip_dst2;
// __be32 ip_dst3;
// __be32 pre_nat_ip_dst;
// __be32 pre_nat_ip_dst1;
// __be32 pre_nat_ip_dst2;
// __be32 pre_nat_ip_dst3;
// __be32 post_nat_ip_dst;
// __be32 post_nat_ip_dst1;
// __be32 post_nat_ip_dst2;
// __be32 post_nat_ip_dst3;
// __be32 tun_ip;
// __be32 tun_ip1;
// __be32 tun_ip2;
// __be32 tun_ip3;
// __u32 unused;
// __s32 pol_rc;
// __u16 sport;
// __u16 dport;
// __u16 pre_nat_dport;
// __u16 post_nat_dport;
// __u8 ip_proto;
// __u8 __pad;
// __be16 ip_size;
// __u32 rules_hit;
// __u64 rule_ids[MAX_RULE_IDS];
// struct calico_ct_result ct_result;
// struct calico_nat_dest nat_dest;
// __u64 prog_start_time;
// __u64 flags;
// };
type State struct {
SrcAddr uint32
SrcAddr1 uint32
SrcAddr2 uint32
SrcAddr3 uint32
DstAddr uint32
DstAddr1 uint32
DstAddr2 uint32
DstAddr3 uint32
PreNATDstAddr uint32
PreNATDstAddr1 uint32
PreNATDstAddr2 uint32
PreNATDstAddr3 uint32
PostNATDstAddr uint32
PostNATDstAddr1 uint32
PostNATDstAddr2 uint32
PostNATDstAddr3 uint32
TunIP uint32
TunIP1 uint32
TunIP2 uint32
TunIP3 uint32
_ uint32
PolicyRC PolicyResult
SrcPort uint16
DstPort uint16
PreNATDstPort uint16
PostNATDstPort uint16
IPProto uint8
pad uint8
IPSize uint16
RulesHit uint32
RuleIDs [MaxRuleIDs]uint64
ConntrackRCFlags uint32
_ uint32
ConntrackNATIPPort uint64
ConntrackTunIP uint32
ConntrackIfIndexFwd uint32
ConntrackIfIndexCtd uint32
_ uint32
NATData uint64
ProgStartTime uint64
Flags uint64
}
const expectedSize = 416
func (s *State) AsBytes() []byte {
size := unsafe.Sizeof(State{})
if size != expectedSize {
log.WithField("size", size).Panic("Incorrect struct size")
}
bPtr := (*[expectedSize]byte)(unsafe.Pointer(s))
bytes := make([]byte, expectedSize)
copy(bytes, bPtr[:])
return bytes
}
func StateFromBytes(bytes []byte) State {
s := State{}
bPtr := (*[expectedSize]byte)(unsafe.Pointer(&s))
copy(bPtr[:], bytes)
return s
}
var MapParameters = maps.MapParameters{
Type: "percpu_array",
KeySize: 4,
ValueSize: expectedSize,
MaxEntries: 1,
Name: "cali_state",
Version: 2,
}
func Map() maps.Map {
return maps.NewPinnedMap(MapParameters)
}
func MapForTest() maps.Map {
return maps.NewPinnedMap(maps.MapParameters{
Type: "array",
KeySize: 4,
ValueSize: expectedSize,
MaxEntries: 1,
Name: "test_state",
})
}