P4ELTE / t4p4s

Retargetable compiler for the P4 language
http://p4.elte.hu/
Apache License 2.0
120 stars 42 forks source link

Compilation error p4 #45

Open henanda123 opened 2 years ago

henanda123 commented 2 years ago

hello! Help: when I use t4p4s compile my P4, if I do not add "new_probability = (bit<33>) ((int<33>) new_probability + (int<33>) delta); ", the compilation is normal and there is no error. If I add "new_probability = (bit<33>) ((int<33>) new_probability + (int<33>) delta); ", the compilation reports an error, who can tell my why?

this is my *.p4;

include

define V1MODEL_VERSION 20180101

include

define WRITE_REG(r, v) r.write((bit<32>)standard_metadata.egress_port, v);

define READ_REG(r, v) r.read(v,(bit<32>)standard_metadata.egress_port);

define CAP(c, v, a, t){ if (v > c) a = c; else a = (t)v; }

const bit<16> TYPE_IPV4 = 0x800; const bit<32> MAX_RND = 0xFFFFFFFF;

/*** * H E A D E R S * ***/

typedef bit<9> egressSpec_t; typedef bit<48> macAddr_t; typedef bit<32> ip4Addr_t; //input parameters typedef int<32> alpha_t; typedef int<32> beta_t; typedef int<32> delay_t; typedef bit<5> interval_t;

@controller_header("packet_in") header packet_in_header_t { bit<9> ingress_port; bit<7> _pad; }

@controller_header("packet_out") header packet_out_header_t { bit<9> egress_port; bit<7> _pad; }

header ethernet_t { macAddr_t dstAddr; macAddr_t srcAddr; bit<16> etherType; }

header ipv4_t { bit<4> version; bit<4> ihl; bit<6> diffserv; bit<2> ecn; bit<16> totalLen; // bit<16> identification; // repurpose identification field... bit<5> drops; // up to 31 previous packets dropped bit<11> qdelay_ms; // up to 2047ms queue delay bit<3> flags; bit<13> fragOffset; bit<8> ttl; bit<8> protocol; bit<16> hdrChecksum; ip4Addr_t srcAddr; ip4Addr_t dstAddr; }

struct metadata { bit<1> mark_drop; int<32> new_probability; }

struct headers { ethernet_t ethernet; ipv4_t ipv4; }

error { IPHeaderTooShort }

/***** **** C H E C K S U M V E R I F I C A T I O N * *****/

control MyVerifyChecksum(inout headers hdr, inout metadata meta) { apply { } }

/***** * C H E C K S U M C O M P U T A T I O N ** *****/

control MyComputeChecksum(inout headers hdr, inout metadata meta) { apply { //update_checksum(hdr.ipv4.isValid(), { hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.dscp, hdr.ipv4.ecn, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr }, hdr.ipv4.hdrChecksum, HashAlgorithm.csum16); } }

/*** * P A R S E R * ***/

parser MyParser(packet_in packet, out headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {

state start {
    transition parse_ethernet;
}

state parse_ethernet {
    packet.extract(hdr.ethernet);
    transition select(hdr.ethernet.etherType) {
        TYPE_IPV4: parse_ipv4;
        default: accept;
    }
}

state parse_ipv4 {
    packet.extract(hdr.ipv4);
    transition accept;
} 

}

/*** ** I N G R E S S P R O C E S S I N G * *****/

control MyIngress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {

action drop() {
    mark_to_drop(standard_metadata);

}

action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {

}

table ipv4_lpm {
    key = {
        hdr.ipv4.dstAddr: lpm;
    }
    actions = {
        ipv4_forward;
        drop;
    }
    size = 1024;
    default_action = drop;
}

apply {
    if (hdr.ipv4.isValid()) {
        ipv4_lpm.apply();
    }
}

}

/*** **** E G R E S S P R O C E S S I N G * *****/

control MyEgress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {

bit<32> last_update_time = 0;
int<32> last_queue_delay;   
bit<32> last_probability;
bit<48> time_temp;

//state registers
register<bit<32>>(256) r_update_time;  // timestamp of previous PI calculation
register<int<32>>(256) r_queue_delay;  // queue delay at previous PI calculation
register<bit<32>>(256) r_probability;  // probability at previous PI calculation
register<bit<32>>(256) r_dropped;      // packets dropped to report in next not dropped packe

action drop() {
    bit<32> dropped_pks;
    mark_to_drop(standard_metadata);
    READ_REG(r_dropped, dropped_pks);
    dropped_pks = dropped_pks + 1;
    WRITE_REG(r_dropped, dropped_pks);
}

// alpha and beta needs to be multiplied by (2^32-1)/1000000 = 4295 which is due to random value goes to 2^32
// alpha = 0,3125 => 1342 and beta = 3,125 => 13422
// delay target is in us = 20000 => 20 msec
// PI update interval is 2^x us, x = 15 => 32768 us ~= 33 msec
action pi21(alpha_t alpha, beta_t beta, delay_t target, interval_t interval){

    READ_REG(r_update_time, last_update_time)  // read r_update_time -> timestamp of last prob update
    time_temp = (bit<48>)last_update_time;
    READ_REG(r_queue_delay, last_queue_delay)  // read q_delay during previous update time
    READ_REG(r_probability, last_probability)  // read last calculated PI probability

    // initialization - no previous update time
    if (time_temp == 0) {
        time_temp = standard_metadata.egress_global_timestamp;
    }
    //find how many time laps - divide by 2^interval = 32768
    bit<32> update_laps = (bit<32>) ((standard_metadata.egress_global_timestamp - time_temp) >> interval); 

    if (update_laps >= 1){
        if (update_laps >= 2000) update_laps = 2000;   // limit to max useful number = max queue_del / min target (1ms)

        int<32> prev_queue_delay = last_queue_delay;   // preserve previous queue delay
        CAP(1000000, standard_metadata.deq_timedelta, last_queue_delay, int<32>); // update and cap queueing delay to 1s

        // calculate change in probability;  subtract extra alpha.TARGET for every extra lap Q assumed 0
        int<32> delta = (last_queue_delay - (int<32>)update_laps * target) * alpha + (last_queue_delay - prev_queue_delay) * beta;
        bit<33> new_probability = (bit<33>) last_probability; // add one bit to detect under- and overflows
        new_probability = (bit<33>) ((int<33>) new_probability + (int<33>) delta);  // delta needs sign preservation

    }

    bit<32> rnd;
    rnd = 10;
    //random(rnd,0,MAX_RND);

    //check based on p (scalable)  or p (non scalable) and mark or drop
    if (hdr.ipv4.ecn & 2 == 2) { // scalable tcp if ecn
      if ((rnd>>1) < last_probability) 
                hdr.ipv4.ecn = 3;         // mark insteaf of drop
    } else {
          if ((rnd < last_probability) && ((rnd << 16) < last_probability)) // squaring by using 16 lsb as second independent random value
                meta.mark_drop = 1;         
    }

}

table aqm{
    key = {
        standard_metadata.egress_port: exact;
    }    
    actions = {
        pi21();
        NoAction;
    }
    default_action = NoAction; 
}

apply {
    // store queuing delay in ms (up to 2047ms)
    hdr.ipv4.qdelay_ms = (bit<11>)(standard_metadata.deq_timedelta >> 10);

    aqm.apply();    
    if (meta.mark_drop == 1) {
        drop();
    } else {
        bit<32> dropped_pks;
        bit<5> drops;
        READ_REG(r_dropped, dropped_pks);
        CAP(31, dropped_pks, drops, bit<5>); // max 31 drops can be reported
        dropped_pks = dropped_pks - (bit<32>)drops;
        WRITE_REG(r_dropped, dropped_pks);
        hdr.ipv4.drops = drops;

    }
}

}

/*** * D E P A R S E R * ***/

control MyDeparser(packet_out packet, in headers hdr) { apply { packet.emit(hdr.ethernet); packet.emit(hdr.ipv4); } }

V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;

henanda123 commented 2 years ago

Can't I use a large number of operations in P4? It can be compiled in bmv2, but not in t4p4s

henanda123 commented 2 years ago

this is error ######################################################

p4 to JSON cost:23.00 milliseconds

JSON to IR cost:41.00 milliseconds

Exception: 'id' Traceback (most recent call last): File "compiler/compiler.py", line 319, in generate_code exec(code, localvars, localvars) File "", line 152, in File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1498, in format_statement ret = gen_format_statement(stmt) ## compiler/utils/codegen.sugar.py:1364 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 565, in gen_format_statement generated_code += str( gen_do_assignment(dst, src)) ## compiler/utils/codegen.sugar.py:468 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 540, in gen_do_assignment generated_code += add_code(' {} {} = {};'.format(format_type(dst.type), tmpvar, format_expr(src, expand_parameters=True)), 446) File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1158, in gen_format_expr return '(' + format_expr(e.e0) + '?' + format_expr(e.e1) + ':' + format_expr(e.e2) + ')' ## compiler/utils/codegen.sugar.py:1032 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1134, in gen_format_expr return '(({}){}({}))'.format(format_expr(e.left), simple_binary_ops[e.node_type], format_expr(e.right)) ## compiler/utils/codegen.sugar.py:1008 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1177, in gen_format_expr return '(' + format_type_mask(e.destType) + format_expr(e.expr) + ')' ## compiler/utils/codegen.sugar.py:1051 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1144, in gen_format_expr temp_expr = '(' + format_expr(e.left,expand_parameters=True) + complex_binary_ops[e.node_type] + format_expr(e.right,expand_parameters=True) + ')' ## compiler/utils/codegen.sugar.py:1018 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1138, in gen_format_expr return '(' + format_type_mask(e.type) + '(' + format_expr(e.left) + '+(' + str(2e.type.size) + '-' + format_expr(e.right) + ')))' ## compiler/utils/codegen.sugar.py:1012 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1267, in gen_format_expr var_name = generate_varname("hdr{}_{}".format(e.expr.header_ref.id, e.field_ref.id)) ## compiler/utils/codegen.sugar.py:1141 File "/home/five/p4/ppk/compiler/hlir16/p4node.py", line 239, in getattr return self.dict[key] KeyError: 'id' Error during the compilation of {'to': './build//srcgen/actions.c', 'from': 'compiler/backend/actions.c.py'} Traceback (most recent call last): File "compiler/compiler.py", line 492, in main() File "compiler/compiler.py", line 488, in main print_with_backtrace(sys.exc_info(), current_compilation['from'] if current_compilation else "(no compiled file)") File "compiler/compiler.py", line 475, in main generate_files() File "compiler/compiler.py", line 463, in generate_files generate_desugared_c(filename, os.path.join(base, filename)) File "compiler/compiler.py", line 369, in generate_desugared_c code = generate_code(filepath, genfile, {'hlir16': hlir}) File "compiler/compiler.py", line 319, in generate_code exec(code, localvars, localvars) File "", line 152, in File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1498, in format_statement ret = gen_format_statement(stmt) ## compiler/utils/codegen.sugar.py:1364 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 565, in gen_format_statement generated_code += str( gen_do_assignment(dst, src)) ## compiler/utils/codegen.sugar.py:468 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 540, in gen_do_assignment generated_code += add_code(' {} {} = {};'.format(format_type(dst.type), tmpvar, format_expr(src, expand_parameters=True)), 446) File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1158, in gen_format_expr return '(' + format_expr(e.e0) + '?' + format_expr(e.e1) + ':' + format_expr(e.e2) + ')' ## compiler/utils/codegen.sugar.py:1032 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1134, in gen_format_expr return '(({}){}({}))'.format(format_expr(e.left), simple_binary_ops[e.node_type], format_expr(e.right)) ## compiler/utils/codegen.sugar.py:1008 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1177, in gen_format_expr return '(' + format_type_mask(e.destType) + format_expr(e.expr) + ')' ## compiler/utils/codegen.sugar.py:1051 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1144, in gen_format_expr temp_expr = '(' + format_expr(e.left,expand_parameters=True) + complex_binary_ops[e.node_type] + format_expr(e.right,expand_parameters=True) + ')' ## compiler/utils/codegen.sugar.py:1018 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1138, in gen_format_expr return '(' + format_type_mask(e.type) + '(' + format_expr(e.left) + '+(' + str(2e.type.size) + '-' + format_expr(e.right) + ')))' ## compiler/utils/codegen.sugar.py:1012 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1486, in format_expr return gen_format_expr(e, format_as_value, expand_parameters) ## compiler/utils/codegen.sugar.py:1352 File "/home/five/p4/ppk/compiler/utils/codegen.py", line 1267, in gen_format_expr var_name = generate_varname("hdr{}_{}".format(e.expr.header_ref.id, e.field_ref.id)) ## compiler/utils/codegen.sugar.py:1141 File "/home/five/p4/ppk/compiler/hlir16/p4node.py", line 239, in getattr return self.dict[key] KeyError: 'id'

srihari39 commented 2 years ago

@henanda123 , can we run p4 program directly here as on p4c? I mean, what's the compile command? Does the compilation command differ from that of p4c?