15 const char *XDPProgUsingMetaForXDP2TC =
16 " void *data_end = (void *)(long)skb->data_end;\n"
17 " struct ethhdr *eth = (struct ethhdr *)(long)skb->data;\n"
18 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
19 " return XDP_ABORTED;\n"
21 " if (eth->h_proto == bpf_htons(0x0800) || eth->h_proto == bpf_htons(0x86DD)) {\n"
25 " struct internal_metadata *meta;\n"
26 " int ret = bpf_xdp_adjust_meta(skb, -(int)sizeof(*meta));\n"
28 " return XDP_ABORTED;\n"
30 " meta = (struct internal_metadata *)(unsigned long)skb->data_meta;\n"
31 " eth = (void *)(long)skb->data;\n"
32 " data_end = (void *)(long)skb->data_end;\n"
33 " if ((void *) ((struct internal_metadata *) meta + 1) > (void *)(long)skb->data)\n"
34 " return XDP_ABORTED;\n"
35 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
36 " return XDP_ABORTED;\n"
38 " meta->pkt_ether_type = eth->h_proto;\n"
39 " eth->h_proto = bpf_htons(0x0800);\n"
43 const char *XDPProgUsingHeadForXDP2TC =
44 " void *data = (void *)(long)skb->data;\n"
45 " void *data_end = (void *)(long)skb->data_end;\n"
46 " struct ethhdr *eth = data;\n"
47 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
48 " return XDP_ABORTED;\n"
50 " __u16 orig_ethtype = eth->h_proto;\n"
51 " int ret = bpf_xdp_adjust_head(skb, -2);\n"
53 " return XDP_ABORTED;\n"
56 " data = (void *)(long)skb->data;\n"
57 " data_end = (void *)(long)skb->data_end;\n"
59 " if ((void *)(data + 16) > data_end) {\n"
60 " return XDP_ABORTED;\n"
62 " __builtin_memmove(data, data + 2, 14);\n"
64 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
65 " return XDP_ABORTED;\n"
67 " eth->h_proto = bpf_htons(0x0800);\n"
68 " __builtin_memcpy((char *)data + 14, &orig_ethtype, 2);\n"
73 const char *XDPProgUsingCPUMAPForXDP2TC =
74 " void *data = (void *)(long)skb->data;\n"
75 " void *data_end = (void *)(long)skb->data_end;\n"
76 " struct ethhdr *eth = data;\n"
77 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
78 " return XDP_ABORTED;\n"
80 " u16 orig_ethtype = eth->h_proto;\n"
81 " eth->h_proto = bpf_htons(0x0800);\n"
83 " BPF_MAP_UPDATE_ELEM(xdp2tc_cpumap, &zero, &orig_ethtype, BPF_ANY);\n"
89 :
EBPFProgram(options,
nullptr,
nullptr,
nullptr,
nullptr) {
90 sectionName =
"xdp/xdp-ingress"_cs;
91 functionName =
"xdp_func"_cs;
95 builder->target->emitCodeSection(builder, sectionName);
96 builder->emitIndent();
97 builder->appendFormat(
"int %v(struct xdp_md *%s)", functionName, model.CPacketName.str());
99 builder->blockStart();
100 builder->emitIndent();
102 if (options.xdp2tcMode == XDP2TC_META) {
103 builder->appendLine(XDPProgUsingMetaForXDP2TC);
104 }
else if (options.xdp2tcMode == XDP2TC_HEAD) {
105 builder->appendLine(XDPProgUsingHeadForXDP2TC);
106 }
else if (options.xdp2tcMode == XDP2TC_CPUMAP) {
107 builder->appendLine(XDPProgUsingCPUMAPForXDP2TC);
109 builder->blockEnd(
true);
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:56