forked from vmware-archive/p4c-xdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4c-xdp.cpp
83 lines (68 loc) · 2.3 KB
/
p4c-xdp.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
/*
Copyright 2017 VMware, 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 <string>
#include <iostream>
#include "ir/ir.h"
#include "lib/log.h"
#include "lib/crash.h"
#include "lib/exceptions.h"
#include "lib/gc.h"
#include "lib/nullstream.h"
#include "backends/ebpf/midend.h"
#include "backends/ebpf/ebpfOptions.h"
#include "xdpBackend.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"
void compile(EbpfOptions& options) {
auto hook = options.getDebugHook();
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
if (isv1) {
::error("This compiler only handles P4-16");
return;
}
auto program = P4::parseP4File(options);
if (::errorCount() > 0)
return;
P4::FrontEnd frontend;
frontend.addDebugHook(hook);
program = frontend.run(options, program);
if (::errorCount() > 0)
return;
P4::serializeP4RuntimeIfRequired(program, options);
if (::errorCount() > 0)
return;
EBPF::MidEnd midend;
midend.addDebugHook(hook);
auto toplevel = midend.run(options, program);
if (options.dumpJsonFile)
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
if (::errorCount() > 0)
return;
XDP::run_xdp_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 = "0.0.1";
if (options.process(argc, argv) != nullptr)
options.setInputFile();
if (::errorCount() > 0)
exit(1);
compile(options);
if (Log::verbose())
std::cerr << "Done." << std::endl;
return ::errorCount() > 0;
}