diff --git a/testdata/p4tc_samples/simple_l3_varbits.p4 b/testdata/p4tc_samples/simple_l3_varbits.p4 new file mode 100644 index 0000000000..bdef6c67dd --- /dev/null +++ b/testdata/p4tc_samples/simple_l3_varbits.p4 @@ -0,0 +1,159 @@ +/* -*- P4_16 -*- */ + +#include +#include + +struct my_ingress_metadata_t { +} + +struct empty_metadata_t { +} + +/* + * CONST VALUES FOR TYPES + */ +const bit<8> IP_PROTO_TCP = 0x06; +const bit<16> ETHERTYPE_IPV4 = 0x0800; + +/* + * Standard ethernet header + */ +header ethernet_t { + bit<48> dstAddr; + bit<48> srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + bit<32> srcAddr; + bit<32> dstAddr; +} + +header v4_options_t { + varbit<320> opts; +} + +struct my_ingress_headers_t { + ethernet_t ethernet; + ipv4_t ipv4; + v4_options_t opts; +} + + /*********************** P A R S E R **************************/ +parser Ingress_Parser( + packet_in pkt, + out my_ingress_headers_t hdr, + inout my_ingress_metadata_t meta, + in pna_main_parser_input_metadata_t istd) +{ + + state start { + transition parse_ethernet; + } + + state parse_ethernet { + pkt.extract(hdr.ethernet); + transition select(hdr.ethernet.etherType) { + ETHERTYPE_IPV4: parse_ipv4; + default: reject; + } + } + state parse_ipv4 { + pkt.extract(hdr.ipv4); + transition select(hdr.ipv4.ihl) { + 4w0: reject; + 4w1: reject; + 4w2: reject; + 4w3: reject; + 4w4: reject; + 5: parse_proto; + default: parse_v4_opts; + } + } + state parse_v4_opts { + // XXX The cast to bit<32> is because pkt.extract demands + // bit<32> for its second arg + pkt.extract(hdr.opts,(bit<32>)(32*(bit<9>)(hdr.ipv4.ihl-5))); + transition parse_proto; + } + state parse_proto { + transition select(hdr.ipv4.protocol) { + IP_PROTO_TCP : accept; + default: reject; + } + } +} + +#define L3_TABLE_SIZE 2048 + +/***************** M A T C H - A C T I O N *********************/ + +control ingress( + inout my_ingress_headers_t hdr, + inout my_ingress_metadata_t meta, + in pna_main_input_metadata_t istd, + inout pna_main_output_metadata_t ostd +) +{ + action send_nh(@tc_type("dev") PortId_t port, @tc_type("macaddr") bit<48> srcMac, @tc_type("macaddr") bit<48> dstMac) { + hdr.ethernet.srcAddr = srcMac; + hdr.ethernet.dstAddr = dstMac; + hdr.ipv4.ttl = 31; + send_to_port(port); + } + + action drop() { + drop_packet(); + } + + table nh_table { + key = { + hdr.ipv4.dstAddr : exact @tc_type("ipv4") @name("dstAddr"); + } + actions = { + send_nh; + drop; + } + size = L3_TABLE_SIZE; + const default_action = drop; + } + + apply { + if (hdr.ipv4.isValid() && hdr.ipv4.protocol == IP_PROTO_TCP) { + nh_table.apply(); + } + } +} + + /********************* D E P A R S E R ************************/ + +control Ingress_Deparser( + packet_out pkt, + inout my_ingress_headers_t hdr, + in my_ingress_metadata_t meta, + in pna_main_output_metadata_t ostd) +{ + apply { + pkt.emit(hdr.ethernet); + pkt.emit(hdr.ipv4); + pkt.emit(hdr.opts); + } +} + +/************ F I N A L P A C K A G E ******************************/ + +PNA_NIC( + Ingress_Parser(), + ingress(), + Ingress_Deparser() +) main; diff --git a/testdata/p4tc_samples_outputs/simple_l3_varbits.json b/testdata/p4tc_samples_outputs/simple_l3_varbits.json new file mode 100644 index 0000000000..1a326fe461 --- /dev/null +++ b/testdata/p4tc_samples_outputs/simple_l3_varbits.json @@ -0,0 +1,63 @@ +{ + "schema_version" : "1.0.0", + "pipeline_name" : "simple_l3_varbits", + "externs" : [], + "tables" : [ + { + "name" : "ingress/nh_table", + "id" : 1, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 32, + "keyfields" : [ + { + "id" : 1, + "name" : "dstAddr", + "type" : "ipv4", + "match_type" : "exact", + "bitwidth" : 32 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "ingress/send_nh", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [ + { + "id" : 1, + "name" : "port", + "type" : "dev", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "srcMac", + "type" : "macaddr", + "bitwidth" : 48 + }, + { + "id" : 3, + "name" : "dstMac", + "type" : "macaddr", + "bitwidth" : 48 + } + ], + "default_hit_action" : false, + "default_miss_action" : false + }, + { + "id" : 2, + "name" : "ingress/drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : true + } + ] + } + ] +} \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/simple_l3_varbits.template b/testdata/p4tc_samples_outputs/simple_l3_varbits.template new file mode 100755 index 0000000000..f7e33c9a3c --- /dev/null +++ b/testdata/p4tc_samples_outputs/simple_l3_varbits.template @@ -0,0 +1,24 @@ +#!/bin/bash -x + +set -e + +TC="tc" +$TC p4template create pipeline/simple_l3_varbits numtables 1 + +$TC p4template create action/simple_l3_varbits/ingress/send_nh actid 1 \ + param port type dev \ + param srcMac type macaddr \ + param dstMac type macaddr +$TC p4template update action/simple_l3_varbits/ingress/send_nh state active + +$TC p4template create action/simple_l3_varbits/ingress/drop actid 2 +$TC p4template update action/simple_l3_varbits/ingress/drop state active + +$TC p4template create table/simple_l3_varbits/ingress/nh_table \ + tblid 1 \ + type exact \ + keysz 32 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name simple_l3_varbits/ingress/send_nh \ + act name simple_l3_varbits/ingress/drop +$TC p4template update table/simple_l3_varbits/ingress/nh_table default_miss_action permissions 0x1024 action simple_l3_varbits/ingress/drop +$TC p4template update pipeline/simple_l3_varbits state ready \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/simple_l3_varbits_control_blocks.c b/testdata/p4tc_samples_outputs/simple_l3_varbits_control_blocks.c new file mode 100644 index 0000000000..57f7845bd6 --- /dev/null +++ b/testdata/p4tc_samples_outputs/simple_l3_varbits_control_blocks.c @@ -0,0 +1,395 @@ +/* Automatically generated by p4c-pna-p4tc from ../testdata/p4tc_samples/simple_l3_varbits.p4 on Tue Jun 4 14:45:31 2024 + */ +#include "simple_l3_varbits_parser.h" +struct p4tc_filter_fields p4tc_filter_fields; + +struct internal_metadata { + __u16 pkt_ether_type; +} __attribute__((aligned(4))); + +struct __attribute__((__packed__)) ingress_nh_table_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ +} __attribute__((aligned(8))); +#define INGRESS_NH_TABLE_ACT_INGRESS_SEND_NH 1 +#define INGRESS_NH_TABLE_ACT_INGRESS_DROP 2 +#define INGRESS_NH_TABLE_ACT_NOACTION 0 +struct __attribute__((__packed__)) ingress_nh_table_value { + unsigned int action; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 port; + u64 srcMac; + u64 dstMac; + } ingress_send_nh; + struct { + } ingress_drop; + } u; +}; + +static __always_inline int process(struct __sk_buff *skb, struct my_ingress_headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct my_ingress_metadata_t *meta; + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits; + hdr_start = pkt + BYTES(ebpf_packetOffsetInBits); + hdr = &(hdrMd->cpumap_hdr); + meta = &(hdrMd->cpumap_usermeta); +{ + u8 hit; + { +if (/* hdr->ipv4.isValid() */ + hdr->ipv4.ebpf_valid && hdr->ipv4.protocol == 0x6) { +/* nh_table_0.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 1 + }; + struct ingress_nh_table_key key; + // XXX eBPF gets upset at memset(); let's + // hope __builtin_memset() does what we want + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 32; + key.field0 = bpf_htonl(hdr->ipv4.dstAddr); + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct ingress_nh_table_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct ingress_nh_table_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + switch (value->action) { + case INGRESS_NH_TABLE_ACT_INGRESS_SEND_NH: + { + hdr->ethernet.srcAddr = ntohll(value->u.ingress_send_nh.srcMac << 16); + hdr->ethernet.dstAddr = ntohll(value->u.ingress_send_nh.dstMac << 16); + hdr->ipv4.ttl = 31; + /* send_to_port(value->u.ingress_send_nh.port) */ + compiler_meta__->drop = false; + send_to_port(value->u.ingress_send_nh.port); + } + break; + case INGRESS_NH_TABLE_ACT_INGRESS_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case INGRESS_NH_TABLE_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; } + + } + } + { +{ +; + ; + ; + } + + if (compiler_meta__->drop) { + return TC_ACT_SHOT; + } + int outHeaderLength = 0; + if (hdr->ethernet.ebpf_valid) { + outHeaderLength += 112; + } +; if (hdr->ipv4.ebpf_valid) { + outHeaderLength += 160; + } +; if (hdr->opts.ebpf_valid) { + outHeaderLength += 0; + } +; + int outHeaderOffset = BYTES(outHeaderLength) - (hdr_start - (u8*)pkt); + if (outHeaderOffset != 0) { + int returnCode = 0; + returnCode = bpf_skb_adjust_room(skb, outHeaderOffset, 1, 0); + if (returnCode) { + return TC_ACT_SHOT; + } + } + pkt = ((void*)(long)skb->data); + ebpf_packetEnd = ((void*)(long)skb->data_end); + ebpf_packetOffsetInBits = 0; + if (hdr->ethernet.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 112)) { + return TC_ACT_SHOT; + } + + hdr->ethernet.dstAddr = htonll(hdr->ethernet.dstAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = htonll(hdr->ethernet.srcAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = bpf_htons(hdr->ethernet.etherType); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + } +; if (hdr->ipv4.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 160)) { + return TC_ACT_SHOT; + } + + ebpf_byte = ((char*)(&hdr->ipv4.version))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 4, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.ihl))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 0, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.diffserv))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = bpf_htons(hdr->ipv4.totalLen); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = bpf_htons(hdr->ipv4.identification); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.flags))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 3, 5, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = bpf_htons(hdr->ipv4.fragOffset << 3); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 5, 0, (ebpf_byte >> 3)); + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0 + 1, 3, 5, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[1]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 1, 5, 0, (ebpf_byte >> 3)); + ebpf_packetOffsetInBits += 13; + + ebpf_byte = ((char*)(&hdr->ipv4.ttl))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + ebpf_byte = ((char*)(&hdr->ipv4.protocol))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = bpf_htons(hdr->ipv4.hdrChecksum); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.srcAddr = htonl(hdr->ipv4.srcAddr); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + hdr->ipv4.dstAddr = htonl(hdr->ipv4.dstAddr); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + } +; if (hdr->opts.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 0)) { + return TC_ACT_SHOT; + } + + ebpf_byte = ((char*)(&hdr->opts.opts))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[6]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 6, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[7]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 7, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[8]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 8, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[9]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 9, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[10]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 10, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[11]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 11, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[12]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 12, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[13]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 13, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[14]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 14, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[15]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 15, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[16]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 16, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[17]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 17, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[18]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 18, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[19]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 19, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[20]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 20, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[21]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 21, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[22]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 22, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[23]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 23, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[24]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 24, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[25]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 25, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[26]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 26, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[27]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 27, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[28]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 28, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[29]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 29, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[30]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 30, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[31]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 31, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[32]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 32, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[33]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 33, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[34]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 34, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[35]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 35, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[36]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 36, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[37]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 37, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[38]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 38, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->opts.opts))[39]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 39, (ebpf_byte)); + ebpf_packetOffsetInBits += 320; + + } +; + } + return -1; +} +SEC("p4tc/main") +int tc_ingress_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + if (compiler_meta__->pass_to_kernel == true) return TC_ACT_OK; + if (!compiler_meta__->recirculated) { + compiler_meta__->mark = 153; + struct internal_metadata *md = (struct internal_metadata *)(unsigned long)skb->data_meta; + if ((void *) ((struct internal_metadata *) md + 1) <= (void *)(long)skb->data) { + __u16 *ether_type = (__u16 *) ((void *) (long)skb->data + 12); + if ((void *) ((__u16 *) ether_type + 1) > (void *) (long) skb->data_end) { + return TC_ACT_SHOT; + } + *ether_type = md->pkt_ether_type; + } + } + struct hdr_md *hdrMd; + struct my_ingress_headers_t *hdr; + int ret = -1; + ret = process(skb, (struct my_ingress_headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + if (!compiler_meta__->drop && compiler_meta__->egress_port == 0) { + compiler_meta__->pass_to_kernel = true; + return bpf_redirect(skb->ifindex, BPF_F_INGRESS); + } + return bpf_redirect(compiler_meta__->egress_port, 0); +} +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.c b/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.c new file mode 100644 index 0000000000..9cc24c4785 --- /dev/null +++ b/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.c @@ -0,0 +1,252 @@ +/* Automatically generated by p4c-pna-p4tc from ../testdata/p4tc_samples/simple_l3_varbits.p4 on Tue Jun 4 14:45:31 2024 + */ +#include "simple_l3_varbits_parser.h" + +struct p4tc_filter_fields p4tc_filter_fields; + +static __always_inline int run_parser(struct __sk_buff *skb, struct my_ingress_headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct my_ingress_metadata_t *meta; + + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + __builtin_memset(hdrMd, 0, sizeof(struct hdr_md)); + + unsigned ebpf_packetOffsetInBits = 0; + hdr = &(hdrMd->cpumap_hdr); + meta = &(hdrMd->cpumap_usermeta); + { + goto start; + parse_ipv4: { +/* extract(hdr->ipv4) */ +//compileExtract +// compileExtract: dest = hdr.ipv4 +// compileExtract: varsize = nil + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(160 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + +/* TC::PnaStateTranslationVisitor::compileExtractField: field version (fixed scalar) */ + hdr->ipv4.version = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 4) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field ihl (fixed scalar) */ + hdr->ipv4.ihl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field diffserv (fixed scalar) */ + hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field totalLen (fixed scalar) */ + hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field identification (fixed scalar) */ + hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field flags (fixed scalar) */ + hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3)); + ebpf_packetOffsetInBits += 3; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field fragOffset (fixed scalar) */ + hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13)); + ebpf_packetOffsetInBits += 13; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field ttl (fixed scalar) */ + hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field protocol (fixed scalar) */ + hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field hdrChecksum (fixed scalar) */ + hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field srcAddr (fixed scalar) */ + hdr->ipv4.srcAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 32; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field dstAddr (fixed scalar) */ + hdr->ipv4.dstAddr = (u32)((load_word(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 32; + + + hdr->ipv4.ebpf_valid = 1; + hdr_start += BYTES(160); + +; + u8 select_0; + select_0 = hdr->ipv4.ihl; + if (select_0 == 0)goto reject; + if (select_0 == 1)goto reject; + if (select_0 == 2)goto reject; + if (select_0 == 3)goto reject; + if (select_0 == 4)goto reject; + if (select_0 == 5)goto parse_proto; + if ((select_0 & 0x0) == (0x0 & 0x0))goto parse_v4_opts; + else goto reject; + } + parse_proto: { + u8 select_1; + select_1 = hdr->ipv4.protocol; + if (select_1 == 0x6)goto accept; + if ((select_1 & 0x0) == (0x0 & 0x0))goto reject; + else goto reject; + } + parse_v4_opts: { +/* extract(hdr->opts, (u32)(((u16)((hdr->ipv4.ihl + 11) & ((1 << 4) - 1)) << 5) & ((1 << 9) - 1))) */ +//compileExtract +// compileExtract: dest = hdr.opts +// compileExtract: varsize = (bit<32>)(bit<9>)(hdr.ipv4.ihl + 11) << 5 + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(0 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + +/* compileExtract varbit size = (u32)(((u16)((hdr->ipv4.ihl + 11) & ((1 << 4) - 1)) << 5) & ((1 << 9) - 1)) */ +/* TC::PnaStateTranslationVisitor::compileExtractField: field opts (variable scalar), sizecode = (u32)(((u16)((hdr->ipv4.ihl + 11) & ((1 << 4) - 1)) << 5) & ((1 << 9) - 1)) */ + { + + u32 ebpf_varbits_width = (u32)(((u16)((hdr->ipv4.ihl + 11) & ((1 << 4) - 1)) << 5) & ((1 << 9) - 1)); + u32 ebpf_varbits_offset; + // Can't handle this case yet + if (ebpf_varbits_width & 7) { + ebpf_errorCode = ParserInvalidArgument; + goto reject; + } + ebpf_varbits_width >>= 3; + ebpf_varbits_offset = BYTES(ebpf_packetOffsetInBits) + ebpf_varbits_width; + + switch (ebpf_varbits_width) + { + case 40: hdr->opts.opts.data[39] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 39: hdr->opts.opts.data[38] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 38: hdr->opts.opts.data[37] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 37: hdr->opts.opts.data[36] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 36: hdr->opts.opts.data[35] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 35: hdr->opts.opts.data[34] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 34: hdr->opts.opts.data[33] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 33: hdr->opts.opts.data[32] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 32: hdr->opts.opts.data[31] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 31: hdr->opts.opts.data[30] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 30: hdr->opts.opts.data[29] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 29: hdr->opts.opts.data[28] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 28: hdr->opts.opts.data[27] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 27: hdr->opts.opts.data[26] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 26: hdr->opts.opts.data[25] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 25: hdr->opts.opts.data[24] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 24: hdr->opts.opts.data[23] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 23: hdr->opts.opts.data[22] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 22: hdr->opts.opts.data[21] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 21: hdr->opts.opts.data[20] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 20: hdr->opts.opts.data[19] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 19: hdr->opts.opts.data[18] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 18: hdr->opts.opts.data[17] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 17: hdr->opts.opts.data[16] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 16: hdr->opts.opts.data[15] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 15: hdr->opts.opts.data[14] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 14: hdr->opts.opts.data[13] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 13: hdr->opts.opts.data[12] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 12: hdr->opts.opts.data[11] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 11: hdr->opts.opts.data[10] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 10: hdr->opts.opts.data[9] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 9: hdr->opts.opts.data[8] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 8: hdr->opts.opts.data[7] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 7: hdr->opts.opts.data[6] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 6: hdr->opts.opts.data[5] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 5: hdr->opts.opts.data[4] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 4: hdr->opts.opts.data[3] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 3: hdr->opts.opts.data[2] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 2: hdr->opts.opts.data[1] = (u8)load_byte(pkt,--ebpf_varbits_offset); + case 1: hdr->opts.opts.data[0] = (u8)load_byte(pkt,--ebpf_varbits_offset); + } + hdr->opts.opts.curwidth = ebpf_varbits_width; + ebpf_packetOffsetInBits += ebpf_varbits_width << 3; + } + + hdr->opts.ebpf_valid = 1; + hdr_start += BYTES(0); + +; + goto parse_proto; + } + start: { +/* extract(hdr->ethernet) */ +//compileExtract +// compileExtract: dest = hdr.ethernet +// compileExtract: varsize = nil + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(112 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + +/* TC::PnaStateTranslationVisitor::compileExtractField: field dstAddr (fixed scalar) */ + hdr->ethernet.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field srcAddr (fixed scalar) */ + hdr->ethernet.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + +/* TC::PnaStateTranslationVisitor::compileExtractField: field etherType (fixed scalar) */ + hdr->ethernet.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + + hdr->ethernet.ebpf_valid = 1; + hdr_start += BYTES(112); + +; + u16 select_2; + select_2 = hdr->ethernet.etherType; + if (select_2 == 0x800)goto parse_ipv4; + if ((select_2 & 0x0) == (0x0 & 0x0))goto reject; + else goto reject; + } + + reject: { + if (ebpf_errorCode == 0) { + return TC_ACT_SHOT; + } + compiler_meta__->parser_error = ebpf_errorCode; + goto accept; + } + + } + + accept: + hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits; + return -1; +} + +SEC("p4tc/parse") +int tc_parse_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + struct hdr_md *hdrMd; + struct my_ingress_headers_t *hdr; + int ret = -1; + ret = run_parser(skb, (struct my_ingress_headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + return TC_ACT_PIPE; + } +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.h b/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.h new file mode 100644 index 0000000000..67f278d3a0 --- /dev/null +++ b/testdata/p4tc_samples_outputs/simple_l3_varbits_parser.h @@ -0,0 +1,172 @@ +/* Automatically generated by p4c-pna-p4tc from ../testdata/p4tc_samples/simple_l3_varbits.p4 on Tue Jun 4 14:45:31 2024 + */ +#include "ebpf_kernel.h" + +#include +#include +#include "pna.h" + +#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1) +#define BYTES(w) ((w) / 8) +#define write_partial(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) & ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0) +#define write_byte(base, offset, v) do { *(u8*)((base) + (offset)) = (v); } while (0) +#define bpf_trace_message(fmt, ...) + + +struct my_ingress_metadata_t { +}; +struct empty_metadata_t { +}; +struct ethernet_t { + u64 dstAddr; /* bit<48> */ + u64 srcAddr; /* bit<48> */ + u16 etherType; /* bit<16> */ + u8 ebpf_valid; +}; +struct ipv4_t { + u8 version; /* bit<4> */ + u8 ihl; /* bit<4> */ + u8 diffserv; /* bit<8> */ + u16 totalLen; /* bit<16> */ + u16 identification; /* bit<16> */ + u8 flags; /* bit<3> */ + u16 fragOffset; /* bit<13> */ + u8 ttl; /* bit<8> */ + u8 protocol; /* bit<8> */ + u16 hdrChecksum; /* bit<16> */ + u32 srcAddr; /* bit<32> */ + u32 dstAddr; /* bit<32> */ + u8 ebpf_valid; +}; +struct v4_options_t { + struct { u8 data[40]; u16 curwidth; } opts; /* varbit<320> */ + u8 ebpf_valid; +}; +struct my_ingress_headers_t { + struct ethernet_t ethernet; /* ethernet_t */ + struct ipv4_t ipv4; /* ipv4_t */ + struct v4_options_t opts; /* v4_options_t */ +}; + +struct hdr_md { + struct my_ingress_headers_t cpumap_hdr; + struct my_ingress_metadata_t cpumap_usermeta; + unsigned ebpf_packetOffsetInBits; + __u8 __hook; +}; + +struct p4tc_filter_fields { + __u32 pipeid; + __u32 handle; + __u32 classid; + __u32 chain; + __u32 blockid; + __be16 proto; + __u16 prio; +}; + +REGISTER_START() +REGISTER_TABLE(hdr_md_cpumap, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct hdr_md, 2) +BPF_ANNOTATE_KV_PAIR(hdr_md_cpumap, u32, struct hdr_md) +REGISTER_END() + +static __always_inline +void crc16_update(u16 * reg, const u8 * data, u16 data_size, const u16 poly) { + if (data_size <= 8) + data += data_size - 1; + #pragma clang loop unroll(full) + for (u16 i = 0; i < data_size; i++) { + bpf_trace_message("CRC16: data byte: %x\n", *data); + *reg ^= *data; + for (u8 bit = 0; bit < 8; bit++) { + *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1; + } + if (data_size <= 8) + data--; + else + data++; + } +} +static __always_inline u16 crc16_finalize(u16 reg) { + return reg; +} +static __always_inline +void crc32_update(u32 * reg, const u8 * data, u16 data_size, const u32 poly) { + u32* current = (u32*) data; + u32 index = 0; + u32 lookup_key = 0; + u32 lookup_value = 0; + u32 lookup_value1 = 0; + u32 lookup_value2 = 0; + u32 lookup_value3 = 0; + u32 lookup_value4 = 0; + u32 lookup_value5 = 0; + u32 lookup_value6 = 0; + u32 lookup_value7 = 0; + u32 lookup_value8 = 0; + u16 tmp = 0; + if (crc32_table != NULL) { + for (u16 i = data_size; i >= 8; i -= 8) { + /* Vars one and two will have swapped byte order if data_size == 8 */ + if (data_size == 8) current = (u32 *)(data + 4); + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 one = (data_size == 8 ? __builtin_bswap32(*current--) : *current++) ^ *reg; + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 two = (data_size == 8 ? __builtin_bswap32(*current--) : *current++); + lookup_key = (one & 0x000000FF); + lookup_value8 = crc32_table[(u16)(1792 + (u8)lookup_key)]; + lookup_key = (one >> 8) & 0x000000FF; + lookup_value7 = crc32_table[(u16)(1536 + (u8)lookup_key)]; + lookup_key = (one >> 16) & 0x000000FF; + lookup_value6 = crc32_table[(u16)(1280 + (u8)lookup_key)]; + lookup_key = one >> 24; + lookup_value5 = crc32_table[(u16)(1024 + (u8)(lookup_key))]; + lookup_key = (two & 0x000000FF); + lookup_value4 = crc32_table[(u16)(768 + (u8)lookup_key)]; + lookup_key = (two >> 8) & 0x000000FF; + lookup_value3 = crc32_table[(u16)(512 + (u8)lookup_key)]; + lookup_key = (two >> 16) & 0x000000FF; + lookup_value2 = crc32_table[(u16)(256 + (u8)lookup_key)]; + lookup_key = two >> 24; + lookup_value1 = crc32_table[(u8)(lookup_key)]; + *reg = lookup_value8 ^ lookup_value7 ^ lookup_value6 ^ lookup_value5 ^ + lookup_value4 ^ lookup_value3 ^ lookup_value2 ^ lookup_value1; + tmp += 8; + } + volatile int std_algo_lookup_key = 0; + if (data_size < 8) { + unsigned char *currentChar = (unsigned char *) current; + currentChar += data_size - 1; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar--); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } else { + /* Consume data not processed by slice-by-8 algorithm above, these data are in network byte order */ + unsigned char *currentChar = (unsigned char *) current; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar++); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } + } +} +static __always_inline u32 crc32_finalize(u32 reg) { + return reg ^ 0xFFFFFFFF; +} +inline u16 csum16_add(u16 csum, u16 addend) { + u16 res = csum; + res += addend; + return (res + (res < addend)); +} +inline u16 csum16_sub(u16 csum, u16 addend) { + return csum16_add(csum, ~addend); +}