1515# limitations under the License.
1616
1717import logging
18+ import os
19+ import sys
1820
1921import ptf
2022import ptf .testutils as tu
2123from ptf .base_tests import BaseTest
22- import p4runtime_sh .shell as sh
23- import p4runtime_shell_utils as shu
24+
25+ # Import p4runtime_lib from the tutorials repo utils directory
26+ sys .path .append (
27+ os .path .join (os .path .dirname (os .path .abspath (__file__ )),
28+ '../../../utils/' ))
29+ import p4runtime_lib .bmv2
30+ import p4runtime_lib .helper
31+ from p4runtime_lib .switch import ShutdownAllSwitchConnections
2432
2533
2634# Configure logging
@@ -38,32 +46,53 @@ def setUp(self):
3846 self .dataplane .flush ()
3947
4048 logging .debug ("BasicFwdTest.setUp()" )
49+
50+ # Get test parameters
4151 grpc_addr = tu .test_param_get ("grpcaddr" )
4252 if grpc_addr is None :
4353 grpc_addr = 'localhost:9559'
4454 p4info_txt_fname = tu .test_param_get ("p4info" )
4555 p4prog_binary_fname = tu .test_param_get ("config" )
46- sh .setup (device_id = 0 ,
47- grpc_addr = grpc_addr ,
48- election_id = (0 , 1 ),
49- config = sh .FwdPipeConfig (p4info_txt_fname , p4prog_binary_fname ),
50- verbose = False )
56+
57+ # Create P4Info helper for building table entries
58+ self .p4info_helper = p4runtime_lib .helper .P4InfoHelper (p4info_txt_fname )
59+
60+ # Connect to the switch via gRPC
61+ self .sw = p4runtime_lib .bmv2 .Bmv2SwitchConnection (
62+ name = 's1' ,
63+ address = grpc_addr ,
64+ device_id = 0 ,
65+ proto_dump_file = 'logs/s1-p4runtime-requests.txt' )
66+
67+ # Establish as master controller
68+ self .sw .MasterArbitrationUpdate ()
69+
70+ # Load the P4 program onto the switch
71+ self .sw .SetForwardingPipelineConfig (
72+ p4info = self .p4info_helper .p4info ,
73+ bmv2_json_file_path = p4prog_binary_fname )
5174
5275 def tearDown (self ):
5376 logging .debug ("BasicFwdTest.tearDown()" )
54- sh . teardown ()
77+ ShutdownAllSwitchConnections ()
5578
5679
5780######################################################################
5881# Helper function to add entries to ipv4_lpm table
5982######################################################################
6083
61- def add_ipv4_lpm_entry (ipv4_addr_str , prefix_len , dst_mac_str , port ):
62- te = sh .TableEntry ('MyIngress.ipv4_lpm' )(action = 'MyIngress.ipv4_forward' )
63- te .match ['hdr.ipv4.dstAddr' ] = '%s/%d' % (ipv4_addr_str , prefix_len )
64- te .action ['dstAddr' ] = dst_mac_str
65- te .action ['port' ] = '%d' % port
66- te .insert ()
84+ def add_ipv4_lpm_entry (self , ipv4_addr_str , prefix_len , dst_mac_str , port ):
85+ table_entry = self .p4info_helper .buildTableEntry (
86+ table_name = 'MyIngress.ipv4_lpm' ,
87+ match_fields = {
88+ 'hdr.ipv4.dstAddr' : (ipv4_addr_str , prefix_len )
89+ },
90+ action_name = 'MyIngress.ipv4_forward' ,
91+ action_params = {
92+ 'dstAddr' : dst_mac_str ,
93+ 'port' : port
94+ })
95+ self .sw .WriteTableEntry (table_entry )
6796
6897
6998class DropTest (BasicFwdTest ):
@@ -92,7 +121,7 @@ def runTest(self):
92121 out_dmac = '08:00:00:00:02:22'
93122
94123 # Add a forwarding entry
95- add_ipv4_lpm_entry (ip_dst , 32 , out_dmac , eg_port )
124+ self . add_ipv4_lpm_entry (ip_dst , 32 , out_dmac , eg_port )
96125
97126 # Send packet
98127 pkt = tu .simple_tcp_packet (eth_src = in_smac , eth_dst = in_dmac ,
@@ -131,8 +160,8 @@ def runTest(self):
131160
132161 # Add all entries
133162 for e in entries :
134- add_ipv4_lpm_entry (e ['ip_dst' ], e ['prefix_len' ],
135- e ['out_dmac' ], e ['eg_port' ])
163+ self . add_ipv4_lpm_entry (e ['ip_dst' ], e ['prefix_len' ],
164+ e ['out_dmac' ], e ['eg_port' ])
136165
137166 # Test each entry
138167 ttl_in = 64
0 commit comments