Sign inSign up

davidjosearaujo/p4capi

By davidjosearaujo

Updated over 3 years ago

Image
0

838

davidjosearaujo/p4capi repository overview

REST API for compiling P4

To avoid the painful installation of the P4 compiler (p4c), since P4.org already builds a docker with the p4c inside, the p4capi is a wrapper that implements a simple REST API so any system can utilize the compiler in an easy manner, all a user need to do is make a POST request with the P4 source file and a JSON with the parameter one normally would use with p4c in the terminal, and the response will be a zip file with the set of files resulting from the compilation.

How to use

Spin up the container with the following command. The REST API is built using Flask so the docker inside port will always be 5000.

docker run -d -p 5000:5000 --name p4capi davidjosearaujo/p4capi:latest

This is an example of how to compile a P4 source file and recover the resulting compiled files into a directory /response. (Take into account basic.p4 is a file in the same directory)

import requests
import json
import os
from zipfile import ZipFile

url = 'http://localhost:5000/compile'
p4_file = "basic.p4"

# Set of parameters accepted by the p4 compiler
params = {
    'help': False,                          # Standalone option
    'target-help': False,                   # Standalone option
    'target': 'bmv2',                       # Obligatory parameter
    'arch': 'v1model',                      # Obligatory parameter
    'p4runtime-files': 'basic.p4info.txt',  # Optional parameter
    'std': False,                           # Optional parameter
}
files = {
    'params': (None, json.dumps(params), 'application/json'),
    'file': (os.path.basename(p4_file), open(p4_file, 'rb'), 'application/octet-stream')
}

# Posting request to compiler
r = requests.post(url, files=files)

if r.content != b"Error":
    # Write .zip file received
    filename = 'response.zip'
    open(filename, 'wb').write(r.content)

    # Extract files inside .zip file to /response directory
    with ZipFile(filename, 'r') as zip:
        zip.extractall(path="./response")

    # Erase .zip file
    os.system(f"rm {filename}")

Tag summary

Content type

Image

Digest

sha256:3334adf6a

Size

444.8 MB

Last updated

over 3 years ago

docker pull davidjosearaujo/p4capi