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"
31 " if (eth->h_proto == bpf_htons(0x0800) || eth->h_proto == bpf_htons(0x86DD)) {\n"
35 " struct internal_metadata *meta;\n"
36 " int ret = bpf_xdp_adjust_meta(skb, -(int)sizeof(*meta));\n"
38 " return XDP_ABORTED;\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"
48 " meta->pkt_ether_type = eth->h_proto;\n"
49 " eth->h_proto = bpf_htons(0x0800);\n"
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"
60 " __u16 orig_ethtype = eth->h_proto;\n"
61 " int ret = bpf_xdp_adjust_head(skb, -2);\n"
63 " return XDP_ABORTED;\n"
66 " data = (void *)(long)skb->data;\n"
67 " data_end = (void *)(long)skb->data_end;\n"
69 " if ((void *)(data + 16) > data_end) {\n"
70 " return XDP_ABORTED;\n"
72 " __builtin_memmove(data, data + 2, 14);\n"
74 " if ((void *)((struct ethhdr *) eth + 1) > data_end) {\n"
75 " return XDP_ABORTED;\n"
77 " eth->h_proto = bpf_htons(0x0800);\n"
78 " __builtin_memcpy((char *)data + 14, &orig_ethtype, 2);\n"
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"
90 " u16 orig_ethtype = eth->h_proto;\n"
91 " eth->h_proto = bpf_htons(0x0800);\n"
93 " BPF_MAP_UPDATE_ELEM(xdp2tc_cpumap, &zero, &orig_ethtype, BPF_ANY);\n"
99 :
EBPFProgram(options,
nullptr,
nullptr,
nullptr,
nullptr) {
100 sectionName =
"xdp/xdp-ingress"_cs;
101 functionName =
"xdp_func"_cs;
105 builder->target->emitCodeSection(builder, sectionName);
106 builder->emitIndent();
107 builder->appendFormat(
"int %v(struct xdp_md *%s)", functionName, model.CPacketName.str());
109 builder->blockStart();
110 builder->emitIndent();
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);
119 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:65