P4C
The P4 Compiler
Loading...
Searching...
No Matches
xdpHelpProgram.h
1/*
2Copyright 2022-present Orange
3Copyright 2022-present Open Networking Foundation
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17#ifndef BACKENDS_EBPF_PSA_XDPHELPPROGRAM_H_
18#define BACKENDS_EBPF_PSA_XDPHELPPROGRAM_H_
19
20#include "backends/ebpf/ebpfProgram.h"
21
22namespace P4::EBPF {
23
25 const char *XDPProgUsingMetaForXDP2TC =
26 " void *data_end = (void *)(long)skb->data_end;\n"
27 " struct ethhdr *eth = (struct ethhdr *)(long)skb->data;\n"
28 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
29 " return XDP_ABORTED;\n"
30 " }\n"
31 " if (eth->h_proto == bpf_htons(0x0800) || eth->h_proto == bpf_htons(0x86DD)) {\n"
32 " return XDP_PASS;\n"
33 " }\n"
34 "\n"
35 " struct internal_metadata *meta;\n"
36 " int ret = bpf_xdp_adjust_meta(skb, -(int)sizeof(*meta));\n"
37 " if (ret < 0) {\n"
38 " return XDP_ABORTED;\n"
39 " }\n"
40 " meta = (struct internal_metadata *)(unsigned long)skb->data_meta;\n"
41 " eth = (void *)(long)skb->data;\n"
42 " data_end = (void *)(long)skb->data_end;\n"
43 " if ((void *) ((struct internal_metadata *) meta + 1) > (void *)(long)skb->data)\n"
44 " return XDP_ABORTED;\n"
45 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
46 " return XDP_ABORTED;\n"
47 " }\n"
48 " meta->pkt_ether_type = eth->h_proto;\n"
49 " eth->h_proto = bpf_htons(0x0800);\n"
50 "\n"
51 " return XDP_PASS;";
52
53 const char *XDPProgUsingHeadForXDP2TC =
54 " void *data = (void *)(long)skb->data;\n"
55 " void *data_end = (void *)(long)skb->data_end;\n"
56 " struct ethhdr *eth = data;\n"
57 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
58 " return XDP_ABORTED;\n"
59 " }\n"
60 " __u16 orig_ethtype = eth->h_proto;\n"
61 " int ret = bpf_xdp_adjust_head(skb, -2);\n"
62 " if (ret < 0) {\n"
63 " return XDP_ABORTED;\n"
64 " }\n"
65 "\n"
66 " data = (void *)(long)skb->data;\n"
67 " data_end = (void *)(long)skb->data_end;\n"
68 "\n"
69 " if ((void *)(data + 16) > data_end) {\n"
70 " return XDP_ABORTED;\n"
71 " }\n"
72 " __builtin_memmove(data, data + 2, 14);\n"
73 " eth = data;\n"
74 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
75 " return XDP_ABORTED;\n"
76 " }\n"
77 " eth->h_proto = bpf_htons(0x0800);\n"
78 " __builtin_memcpy((char *)data + 14, &orig_ethtype, 2);\n"
79 " "
80 "\n"
81 " return XDP_PASS;";
82
83 const char *XDPProgUsingCPUMAPForXDP2TC =
84 " void *data = (void *)(long)skb->data;\n"
85 " void *data_end = (void *)(long)skb->data_end;\n"
86 " struct ethhdr *eth = data;\n"
87 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
88 " return XDP_ABORTED;\n"
89 " }\n"
90 " u16 orig_ethtype = eth->h_proto;\n"
91 " eth->h_proto = bpf_htons(0x0800);\n"
92 " u32 zero = 0;\n"
93 " BPF_MAP_UPDATE_ELEM(xdp2tc_cpumap, &zero, &orig_ethtype, BPF_ANY);\n"
94 " return XDP_PASS;";
95
96 public:
97 cstring sectionName;
98 explicit XDPHelpProgram(const EbpfOptions &options)
99 : EBPFProgram(options, nullptr, nullptr, nullptr, nullptr) {
100 sectionName = "xdp/xdp-ingress"_cs;
101 functionName = "xdp_func"_cs;
102 }
103
104 void emit(CodeBuilder *builder) {
105 builder->target->emitCodeSection(builder, sectionName);
106 builder->emitIndent();
107 builder->appendFormat("int %v(struct xdp_md *%s)", functionName, model.CPacketName.str());
108 builder->spc();
109 builder->blockStart();
110 builder->emitIndent();
111 // This is static program, so we can just paste a piece of code.
112 if (options.xdp2tcMode == XDP2TC_META) {
113 builder->appendLine(XDPProgUsingMetaForXDP2TC);
114 } else if (options.xdp2tcMode == XDP2TC_HEAD) {
115 builder->appendLine(XDPProgUsingHeadForXDP2TC);
116 } else if (options.xdp2tcMode == XDP2TC_CPUMAP) {
117 builder->appendLine(XDPProgUsingCPUMAPForXDP2TC);
118 }
119 builder->blockEnd(true);
120 }
121};
122
123} // namespace P4::EBPF
124
125#endif /* BACKENDS_EBPF_PSA_XDPHELPPROGRAM_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfProgram.h:39
EBPFProgram(const EbpfOptions &options, const IR::P4Program *program, P4::ReferenceMap *refMap, P4::TypeMap *typeMap, const IR::ToplevelBlock *toplevel)
return 'true' on success
Definition ebpfProgram.h:65
Definition xdpHelpProgram.h:24
Definition ebpfOptions.h:26
Definition cstring.h:85
Definition codeGen.cpp:25