forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.cpp
154 lines (128 loc) · 6.18 KB
/
target.cpp
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
/*
Copyright 2013-present Barefoot Networks, Inc.
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.
*/
#include "target.h"
#include "ebpfType.h"
namespace EBPF {
void KernelSamplesTarget::emitIncludes(Util::SourceCodeBuilder* builder) const {
builder->append("#include \"ebpf_kernel.h\"\n");
builder->newline();
}
void KernelSamplesTarget::emitTableLookup(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("%s = BPF_MAP_LOOKUP_ELEM(%s, &%s)",
value.c_str(), tblName.c_str(), key.c_str());
}
void KernelSamplesTarget::emitTableUpdate(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("BPF_MAP_UPDATE_ELEM(%s, &%s, &%s, BPF_ANY);",
tblName.c_str(), key.c_str(), value.c_str());
}
void KernelSamplesTarget::emitUserTableUpdate(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("BPF_USER_MAP_UPDATE_ELEM(%s, &%s, &%s, BPF_ANY);",
tblName.c_str(), key.c_str(), value.c_str());
}
void KernelSamplesTarget::emitTableDecl(Util::SourceCodeBuilder* builder,
cstring tblName, TableKind tableKind,
cstring keyType, cstring valueType,
unsigned size) const {
cstring kind;
if (tableKind == TableHash)
kind = "BPF_MAP_TYPE_HASH";
else if (tableKind == TableArray)
kind = "BPF_MAP_TYPE_ARRAY";
else if (tableKind == TableLPMTrie)
kind = "BPF_MAP_TYPE_LPM_TRIE";
else
BUG("%1%: unsupported table kind", tableKind);
builder->appendFormat("REGISTER_TABLE(%s, %s, ", tblName.c_str(), kind.c_str());
builder->appendFormat("sizeof(%s), sizeof(%s), %d)",
keyType.c_str(), valueType.c_str(), size);
builder->newline();
}
void KernelSamplesTarget::emitLicense(Util::SourceCodeBuilder* builder, cstring license) const {
builder->emitIndent();
builder->appendFormat("char _license[] SEC(\"license\") = \"%s\";", license.c_str());
builder->newline();
}
void KernelSamplesTarget::emitCodeSection(
Util::SourceCodeBuilder* builder, cstring sectionName) const {
builder->appendFormat("SEC(\"prog\")\n", sectionName.c_str());
}
void KernelSamplesTarget::emitMain(Util::SourceCodeBuilder* builder,
cstring functionName,
cstring argName) const {
builder->appendFormat("int %s(SK_BUFF *%s)",
functionName.c_str(), argName.c_str());
}
//////////////////////////////////////////////////////////////
void TestTarget::emitIncludes(Util::SourceCodeBuilder* builder) const {
builder->append("#include \"ebpf_test.h\"\n");
builder->newline();
}
void TestTarget::emitTableDecl(Util::SourceCodeBuilder* builder,
cstring tblName, TableKind,
cstring keyType, cstring valueType,
unsigned size) const {
builder->appendFormat("REGISTER_TABLE(%s, 0 /* unused */,", tblName.c_str());
builder->appendFormat("sizeof(%s), sizeof(%s), %d)",
keyType.c_str(), valueType.c_str(), size);
builder->newline();
}
//////////////////////////////////////////////////////////////
void BccTarget::emitTableLookup(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("%s = %s.lookup(&%s)",
value.c_str(), tblName.c_str(), key.c_str());
}
void BccTarget::emitTableUpdate(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("%s.update(&%s, &%s);",
tblName.c_str(), key.c_str(), value.c_str());
}
void BccTarget::emitUserTableUpdate(Util::SourceCodeBuilder* builder, cstring tblName,
cstring key, cstring value) const {
builder->appendFormat("bpf_update_elem(%s, &%s, &%s, BPF_ANY);",
tblName.c_str(), key.c_str(), value.c_str());
}
void BccTarget::emitIncludes(Util::SourceCodeBuilder* builder) const {
builder->append("#include <uapi/linux/bpf.h>\n"
"#include <uapi/linux/if_ether.h>\n"
"#include <uapi/linux/if_packet.h>\n"
"#include <uapi/linux/ip.h>\n"
"#include <linux/skbuff.h>\n"
"#include <linux/netdevice.h>\n");
}
void BccTarget::emitTableDecl(Util::SourceCodeBuilder* builder,
cstring tblName, TableKind tableKind,
cstring keyType, cstring valueType, unsigned size) const {
cstring kind;
if (tableKind == TableHash)
kind = "hash";
else if (tableKind == TableArray)
kind = "array";
else if (tableKind == TableLPMTrie)
kind = "lpm_trie";
else
BUG("%1%: unsupported table kind", tableKind);
builder->appendFormat("BPF_TABLE(\"%s\", %s, %s, %s, %d);",
kind.c_str(), keyType.c_str(), valueType.c_str(), tblName.c_str(), size);
builder->newline();
}
void BccTarget::emitMain(Util::SourceCodeBuilder* builder,
cstring functionName,
cstring argName) const {
builder->appendFormat("int %s(struct __sk_buff* %s)",
functionName.c_str(), argName.c_str());
}
} // namespace EBPF