forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4c-ebpf.cpp
114 lines (95 loc) · 3.51 KB
/
p4c-ebpf.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
/*
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 <stdio.h>
#include <iostream>
#include <string>
#include "backends/ebpf/version.h"
#include "control-plane/p4RuntimeSerializer.h"
#include "ebpfBackend.h"
#include "ebpfOptions.h"
#include "frontends/common/applyOptionsPragmas.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"
#include "fstream"
#include "ir/ir.h"
#include "ir/json_loader.h"
#include "lib/crash.h"
#include "lib/exceptions.h"
#include "lib/gc.h"
#include "lib/log.h"
#include "lib/nullstream.h"
#include "midend.h"
void compile(EbpfOptions &options) {
auto hook = options.getDebugHook();
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
if (isv1) {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET, "This compiler only handles P4-16");
return;
}
const IR::P4Program *program = nullptr;
if (options.loadIRFromJson) {
std::filebuf fb;
if (fb.open(options.file, std::ios::in) == nullptr) {
::error(ErrorType::ERR_IO, "%s: No such file or directory.", options.file);
return;
}
std::istream inJson(&fb);
JSONLoader jsonFileLoader(inJson);
if (jsonFileLoader.json == nullptr) {
::error(ErrorType::ERR_IO, "%s: Not valid input file", options.file);
return;
}
program = new IR::P4Program(jsonFileLoader);
fb.close();
} else {
program = P4::parseP4File(options);
if (::errorCount() > 0) return;
P4::P4COptionPragmaParser optionsPragmaParser;
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));
P4::FrontEnd frontend;
frontend.addDebugHook(hook);
program = frontend.run(options, program);
if (::errorCount() > 0) return;
}
if (!options.arch.isNullOrEmpty() && options.arch != "filter") {
P4::serializeP4RuntimeIfRequired(program, options);
if (::errorCount() > 0) return;
}
EBPF::MidEnd midend;
midend.addDebugHook(hook);
auto toplevel = midend.run(options, program);
if (!options.dumpJsonFile.empty())
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
if (::errorCount() > 0) return;
EBPF::run_ebpf_backend(options, toplevel, &midend.refMap, &midend.typeMap);
}
int main(int argc, char *const argv[]) {
setup_gc_logging();
setup_signals();
AutoCompileContext autoEbpfContext(new EbpfContext);
auto &options = EbpfContext::get().options();
options.compilerVersion = cstring(P4C_EBPF_VERSION_STRING);
if (options.process(argc, argv) != nullptr) {
if (options.loadIRFromJson == false) options.setInputFile();
}
if (::errorCount() > 0) exit(1);
options.calculateXDP2TCMode();
try {
compile(options);
} catch (const std::exception &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (Log::verbose()) std::cerr << "Done." << std::endl;
return ::errorCount() > 0;
}