Example #1
0
    def test208(self, debug=1):

        program = """
layout T1_def { fields { type: 8 ; }  }
layout T2_def { fields { type: 8 ; }  }
layout T3_def { fields { type: 8 ; }  }

T1_def T1;
T2_def T2;
T3_def T3;

parser start  { extract ( T1 ) ;  extract ( T2 ) ;  extract ( T3 ) ;  return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { add_T1_to_T2_and_T3 ; } 
}

action add_T1_to_T2_and_T3( a_field, b_field) { 
    bad_action_name(T2.type, a_field) ; 
}

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds  = ['table1.set_default_action( add_T1_to_T2_and_T3( T1.type ) )'] 
            run_cmds( p4, runtime, setup_cmds )

            #                
            exp_bytes_used = 3
            pkts = [ [ i+5 for i in range(exp_bytes_used) ] ]


            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                    # List of packets
                    init_state='start',      # parser
                    init_ctrl='ingress',     # control program start
                    debug=debug)

            self.assert_( False, "Expected test to fail." )

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Expected Runtime Error:",err.data
            self.assert_(True)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
Example #2
0
    def test209(self, debug=1):

        program = """
header_type T1_def { fields { type: 8 ; }  }
layout      T2_def { fields { type: 8 ; }  }
layout      T3_def { fields { type: 8 ; }  }
layout      T4_def { fields { type: 8 ; }  }

T1_def T1;
T2_def T2;
T3_def T3;
T4_def T4;

parser start  { extract ( T1 ) ;  extract ( T2 ) ;  extract ( T3 ) ;  return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { do_my_action ; } 
}

action do_my_action() { 

    modify_field(T1.type, 1) ;
    modify_field(T2.type, 0xff, 0xf0) ; /* set top 4 bits only */
    modify_field(T3.type, T2.type) ;    /* should be set to ORIGINAL value of T2 i.e. 6 */
    modify_field(T4.type, 22) ;         /* does nothing - T4 not valid */

}

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds  = ['table1.set_default_action( do_my_action() )'] 
            run_cmds( p4, runtime, setup_cmds )

            #                
            pkts = [ [ i+5 for i in range(6) ] ]
            exp_pkts_out = [ [0x1, 0xf6, 0x6, 8,9,10 ] ]
            exp_bytes_used = 3

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                       # List of packets
                    init_state = 'start',       # parser start state
                    init_ctrl  = 'ingress',     # control program start
                    debug=debug )

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'T1.type', 0x1) 
            self.check_field( p4, 'T2.type', 0xf6) 
            self.check_field( p4, 'T3.type', 0x6)  
            self.check_field( p4, 'T4.type', 'invalid')  
            self.check_pkts_out(exp_pkts_out, pkts_out)

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
            self.assert_(False)
Example #3
0
    def test206(self, debug=1):

        program = """
header_type T1_def { fields { type: 8 ; }  }
layout      T2_def { fields { type: 8 ; }  }
layout      T3_def { fields { type: 8 ; }  }

T1_def T1;
T2_def T2;
T3_def T3;

parser start  { extract ( T1 ) ;  extract ( T2 ) ;  extract ( T3 ) ;  return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { add_T1_to_T2_and_T3 ; } 
}

action add_T1_to_T2_and_T3( a_field, b_field) { 

    add_to_field(T2.type, a_field) ; 
    add_to_field(T3.type, b_field) ; 
}

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds  = ['table1.set_default_action( add_T1_to_T2_and_T3( T1.type, T1.type ) )'] 
            run_cmds( p4, runtime, setup_cmds )

            #                
            exp_bytes_used = 3
            pkts = [ [ i+5 for i in range(exp_bytes_used) ] ]

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                       # List of packets
                    init_state = 'start',       # parser start state
                    init_ctrl  = 'ingress',     # control program start
                    debug=debug )

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'T1.type', 0x5) 
            self.check_field( p4, 'T2.type', 11) 
            self.check_field( p4, 'T3.type', 12) 

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
            self.assert_(False)
Example #4
0
    def test205(self, debug=1):

        program = """
layout T1_def { fields { type: 8 ; }  }
layout T2_def { fields { type: 8 ; }  }

T1_def T1;
T2_def T2;

parser start  { extract ( T1 ) ; 
                return switch (T1.type ) 
                { 5           : GET_T2 ; 
                  default     : P4_PARSING_DONE ; 
                }
              }
parser GET_T2 { extract ( T2 ); 
                return  P4_PARSING_DONE ; 
              }    

control ingress { 
    apply_table( table1 );
}

table table1 { 
    reads { T1.type : valid ; }
    actions { no_action next_table table2; } 
}

table table2 { 
    reads { T2.type : valid ; }
    actions { add_to_field; } 
}

"""

        p4, runtime = create_P4_and_runtime(program)

        #                
        setup_cmds  = [  
                         'table1.add_entry( any, [1], no_action() )',
                         'table2.add_entry( 1, [1], add_to_field( T2.type, 22) )'
                      ] 
        run_cmds( p4, runtime, setup_cmds )

        exp_bytes_used = 2
        pkts = [ [ i+5 for i in range(exp_bytes_used) ] ]

        try:

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                    # List of packets
                    init_state='start',      # parser
                    init_ctrl='ingress',     # control program start
                    debug=debug)

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'T1.type', 0x5) 
            self.check_field( p4, 'T2.type', 28) 

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data


        # Now put in a packet with an unparsed (invalid) T2  (T1.type is not 5)

        exp_bytes_used = 1
        pkts = [ [ i for i in range(exp_bytes_used) ] ]

        try:

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                    # List of packets
                    init_state='start',      # parser
                    init_ctrl='ingress',     # control program start
                    debug=debug)

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'T1.type', 0x0) 


        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data


        print"\n\n---- Now add an entry that matches on an invalid T2 and adjusts T1.type\n\n"

        # Now add an entry that matches on an invalid T2 and adjusts T1.type
        setup_cmds  = [  
                         'table2.add_entry( 2, [0], add_to_field( T1.type, 100) )'
                      ] 
        run_cmds( p4, runtime, setup_cmds )

        exp_bytes_used = 1
        pkts = [ [ 0 for i in range(exp_bytes_used) ] ]

        try:

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                    # List of packets
                    init_state='start',      # parser
                    init_ctrl='ingress',     # control program start
                    debug=debug)

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'T1.type', 100) 

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
Example #5
0
    def test301(self, debug=1):

        program = """
layout h1 { fields { b8 : 8   ; } }
layout h2 { fields { b16 : 16 ; } }
layout h3 { fields { b24 : 24 ; } }

h1 H1; h2 H2; h3 H3;

parser start  { extract ( H1 ) ; extract ( H3) ; return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { do_my_action ; } 
}

action do_my_action() { 
    add_header( H2 );
    modify_field( H2.b16, 0x1234);
}

deparse add_h2_in_middle { H1; H2; H3; }

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds = ["table1.set_default_action( do_my_action() )"]
            run_cmds(p4, runtime, setup_cmds)

            #
            pkts = [[i for i in range(10)], [5, 6, 7, 8, 9]]
            exp_pkts_out = [[0, 0x12, 0x34, 1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 0x12, 0x34, 6, 7, 8, 9]]
            exp_bytes_used = 8

            (err, num_bytes_used, pkts_out) = process_pkts(
                p4,
                runtime,
                pkts,  # List of packets
                init_state="start",  # parser start state
                init_ctrl="ingress",  # control program start
                debug=debug,
            )

            self.assert_(err == "", "Saw err:" + str(err))
            self.assert_(
                num_bytes_used == exp_bytes_used,
                "Expected %d bytes consumed, Saw %d." % (exp_bytes_used, num_bytes_used),
            )
            self.check_field(p4, "H1.b8", 5)
            self.check_field(p4, "H2.b16", 0x1234)
            self.check_field(p4, "H3.b24", 0x60708)
            self.check_pkts_out(exp_pkts_out, pkts_out)

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:", err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:", err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
            self.assert_(False)
Example #6
0
    def test303(self, debug=1):

        program = """
layout h1 { fields { b8 : 8   ; } }

h1 H1; h1 H2; h1 H3;

parser start  { extract ( H1 ) ; extract ( H2 ); return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { do_my_action ; } 
}

action do_my_action() { 
    copy_header( H2, H3);  /* invalidates it */
    copy_header( H1, H3);  /* invalidates it */
}

deparse add_h2_in_middle { H1; H2; H3; } /* None are valid */

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds = ["table1.set_default_action( do_my_action() )"]
            run_cmds(p4, runtime, setup_cmds)

            #
            pkts = [[i + 5 for i in range(6)]]
            exp_pkts_out = [[7, 8, 9, 10]]
            exp_bytes_used = 2

            (err, num_bytes_used, pkts_out) = process_pkts(
                p4,
                runtime,
                pkts,  # List of packets
                init_state="start",  # parser start state
                init_ctrl="ingress",  # control program start
                debug=debug,
            )

            self.assert_(err == "", "Saw err:" + str(err))
            self.assert_(
                num_bytes_used == exp_bytes_used,
                "Expected %d bytes consumed, Saw %d." % (exp_bytes_used, num_bytes_used),
            )
            self.check_header(p4, "H1", "invalid")
            self.check_header(p4, "H2", "invalid")
            self.check_header(p4, "H3", "invalid")
            self.check_pkts_out(exp_pkts_out, pkts_out)

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:", err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:", err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
            self.assert_(False)
Example #7
0
    def test301(self, debug=1):

        program = """
layout vlan_tagish {
    fields {
        pcp : 3 ;
        vid : 12;
        da  : 48 ;
        sa  : 48 ;
        t18 : 18; 
        vid2 : 15;
    }
}

vlan_tagish V1;

parser start  { extract ( V1 ) ; return P4_PARSING_DONE ; }

control ingress { 
    apply_table( table1 );
}

table table1 { 
    actions { do_my_action ; } 
}

action do_my_action() { 

    add_to_field(V1.pcp, 0x4) ;
    add_to_field(V1.vid, 0xf00) ;
    add_to_field(V1.t18, 0x10000) ;
    add_to_field(V1.vid2, 0x2000) ;
    modify_field(V1.da, V1.sa);
    modify_field(V1.sa, V1.da);
}

"""

        try:
            p4, runtime = create_P4_and_runtime(program)

            setup_cmds  = ['table1.set_default_action( do_my_action() )'] 
            run_cmds( p4, runtime, setup_cmds )

            #                
            pkts = [ [ i for i in range(20) ] ]
            exp_pkts_out = [ [0x9e, 1,8,9,0xa, 0xb, 0xc, 0xd, 2,3,4,5,6,7, 0x8e, 0xf, 0x30, 17,18,19 ] ]
            exp_bytes_used = 18

            (err, num_bytes_used, pkts_out ) = process_pkts(
                    p4,
                    runtime,
                    pkts,                       # List of packets
                    init_state = 'start',       # parser start state
                    init_ctrl  = 'ingress',     # control program start
                    debug=debug )

            self.assert_( err=='', 'Saw err:' + str(err) )
            self.assert_( num_bytes_used == exp_bytes_used, 
                      'Expected %d bytes consumed, Saw %d.' % (exp_bytes_used, num_bytes_used ))
            self.check_field( p4, 'V1.pcp', 0x4) 
            self.check_field( p4, 'V1.vid', 0xf00) 
            self.check_field( p4, 'V1.da', 0x840485058606) 
            self.check_field( p4, 'V1.sa', 0x810182028303) 
            self.check_field( p4, 'V1.vid2', 0x3011) 
            self.check_field( p4, 'V1.t18', 0x31c1e) 
            self.check_pkts_out(exp_pkts_out, pkts_out)

        except GP4.GP4_Exceptions.RuntimeError as err:
            print "Unexpected Runtime Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.InternalError as err:
            print "Unexpected Internal Error:",err.data
            self.assert_(False)
        except GP4.GP4_Exceptions.SyntaxError as ex_err:
            print "Unexpected SyntaxError:", ex_err.data
            self.assert_(False)