- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2
Fuzzer for bmv2 #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            vvbae
  wants to merge
  2
  commits into
  nyu-systems:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
vvbae:vivi
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Fuzzer for bmv2 #39
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -4,6 +4,17 @@ if(ENABLE_TESTING) | |
| include(test/P4Tests.cmake) | ||
| endif() | ||
|  | ||
| find_program(RTSMITH_DRIVER p4rtsmith) | ||
| if (NOT RTSMITH_DRIVER) | ||
| message(FATAL_ERROR "RtSmith tool not found! Please ensure it is in your PATH.") | ||
| endif() | ||
|  | ||
| # Find p4c (for JSON generation) | ||
| find_program(P4C_EXECUTABLE p4c) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the wrong way to do this, this is why the current tests fail. You directly access the P4C binary in the script, you shouldn't need to use find_program. | ||
| if (NOT P4C_EXECUTABLE) | ||
| message(FATAL_ERROR "p4c not found! Please ensure it is in your PATH.") | ||
| endif() | ||
|  | ||
| # Source files for the main P4RtSmith. | ||
| set(RTSMITH_SOURCES | ||
| ${RTSMITH_SOURCES} | ||
|  | @@ -16,3 +27,56 @@ set(RTSMITH_SOURCES | |
|  | ||
| set(RTSMITH_LIBS ${RTSMITH_LIBS} ${P4C_LIBRARIES} ${P4C_LIB_DEPS} ${CMAKE_THREAD_LIBS_INIT} PARENT_SCOPE) | ||
|  | ||
| set(CONFIG_DIR "${CMAKE_BINARY_DIR}/rtsmith/configs") | ||
| set(JSON_DIR "${CMAKE_BINARY_DIR}/rtsmith/jsons") | ||
|  | ||
| file(MAKE_DIRECTORY ${CONFIG_DIR}) | ||
| file(MAKE_DIRECTORY ${JSON_DIR}) | ||
|  | ||
| file(GLOB P4_PROGRAMS | ||
| # "${P4C_SOURCE_DIR}/testdata/p4_16_samples/*.p4" | ||
| # "${P4C_SOURCE_DIR}/testdata/p4_16_samples/dash/*.p4" | ||
| # "${P4C_SOURCE_DIR}/testdata/p4_16_samples/fabric_*/fabric.p4" | ||
| # "${P4C_SOURCE_DIR}/testdata/p4_16_samples/omec/*.p4" | ||
| # "${P4C_SOURCE_DIR}/testdata/p4_16_samples/pins/*.p4" | ||
| # "${flay_SOURCE_DIR}/targets/bmv2/test/programs/*.p4" | ||
| "${P4C_SOURCE_DIR}/testdata/p4_16_samples/dash/dash-pipeline-pna-dpdk.p4" | ||
| ) | ||
|  | ||
| set(ALL_DEPENDENCIES "") | ||
|  | ||
| foreach(P4_PROGRAM ${P4_PROGRAMS}) | ||
| get_filename_component(PROGRAM_NAME ${P4_PROGRAM} NAME_WE) | ||
|  | ||
| # Generate random config | ||
| set(CONFIG_FILE "${CONFIG_DIR}/${PROGRAM_NAME}_init_config.txtpb") | ||
| if (NOT CONFIG_FILE IN_LIST ALL_DEPENDENCIES) | ||
| add_custom_command( | ||
| OUTPUT ${CONFIG_FILE} | ||
| COMMAND ${RTSMITH_DRIVER} ${P4_PROGRAM} --target bmv2 --arch v1model --output-dir ${CONFIG_DIR} --config-name ${PROGRAM_NAME} --random-seed | ||
| DEPENDS ${P4_PROGRAM} | ||
| COMMENT "Generating random config for ${PROGRAM_NAME}" | ||
| ) | ||
| list(APPEND ALL_DEPENDENCIES ${CONFIG_FILE}) | ||
| endif() | ||
|  | ||
| # Compile P4 program | ||
| set(JSON_FILE "${JSON_DIR}/${PROGRAM_NAME}.json") | ||
| if (NOT JSON_FILE IN_LIST ALL_DEPENDENCIES) | ||
| add_custom_command( | ||
| OUTPUT ${JSON_FILE} | ||
| COMMAND ${P4C_EXECUTABLE} --target bmv2 --arch v1model --output "${JSON_DIR}/${PROGRAM_NAME}.json" ${P4_PROGRAM} | ||
| DEPENDS ${P4_PROGRAM} | ||
| COMMENT "Compiling ${PROGRAM_NAME} to JSON" | ||
| ) | ||
| list(APPEND ALL_DEPENDENCIES ${JSON_FILE}) | ||
| endif() | ||
| endforeach() | ||
|  | ||
| # Step 3: Run BMv2 with the config | ||
| add_custom_target( | ||
| run_all | ||
| COMMAND ${CMAKE_SOURCE_DIR}/run_bmv2.sh "${JSON_DIR}" "${CONFIG_DIR}" | ||
| DEPENDS ${ALL_DEPENDENCIES} | ||
| COMMENT "Running BMv2 for all programs" | ||
| ) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/bin/bash | ||
|  | ||
| # Arguments | ||
| JSON_DIR=$1 | ||
| CONFIG_DIR=$1 | ||
|  | ||
| # Loop through all JSON files in the output directory | ||
| for JSON_FILE in ${JSON_DIR}/*.json; do | ||
| # Get the program name (e.g., program1 from program1.json) | ||
| PROGRAM_NAME=$(basename ${JSON_FILE} .json) | ||
|  | ||
| # Start BMv2 | ||
| simple_switch -i 0@veth0 -i 1@veth1 ${JSON_FILE} & | ||
|  | ||
| # Wait for BMv2 to start | ||
| sleep 2 | ||
|  | ||
| # Send the config file to BMv2 using P4Runtime | ||
| CONFIG_FILE="${CONFIG_DIR}/${PROGRAM_NAME}_init_config.txtpb" | ||
| if [[ -f ${CONFIG_FILE} ]]; then | ||
| python3 ${CMAKE_SOURCE_DIR}/send_config.py ${CONFIG_FILE} | ||
| else | ||
| echo "Config file not found: ${CONFIG_FILE}" | ||
| fi | ||
| done | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import sys | ||
| from p4runtime_lib import helper | ||
| from p4runtime_lib.switch import ShutdownAllSwitchConnections | ||
| from google.protobuf import text_format | ||
| from p4.v1 import p4runtime_pb2 | ||
|  | ||
| # Load the config file | ||
| config_file = sys.argv[1] | ||
| with open(config_file, "r") as f: | ||
| config = f.read() | ||
|  | ||
| # Parse the config | ||
| write_request = p4runtime_pb2.WriteRequest() | ||
| text_format.Parse(config, write_request) | ||
|  | ||
| # Connect to BMv2 | ||
| switch = helper.P4RuntimeSwitchConnection("localhost", 50051, device_id=0) | ||
|  | ||
| # Send the config | ||
| switch.WriteRequest(write_request) | ||
|  | ||
| # Clean up | ||
| ShutdownAllSwitchConnections() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already set this binary with a variable, you just need to use that variable. Do not use find_program