* First draft of Ubuntu 20.04 Vagrantfile and scripts to install 2021-Mar version of open source P4 development tools. * Add more tracing output of what files have been installed at each step * Don't do behavioral-model install_deps.sh before installing PI This is an experiment to see if the end result will be able to run tutorials basic exercise using Python3 only on an Ubuntu 20.04 system. Just before this commit, `vagrant up` resulted in a system that failed to run the basic exercise, because python3 failed to import google.grpc (if I recall correctly -- it may have been a different google.<something> Python3 module name). * Add missing patch file * Fix copy and paste mistake * Add missing patch file * Change how protobuf Python3 module files are installed * Correct a few desktop icon file names, and add clean.sh script * Enhance clean.sh script, and add README for manual steps in creating a VM * Changes to try to always use Python3, never Python2, in tutorials * Update README steps for preparing a VM * More additions to README on steps to create a single file VM image * Add empty-disk-block zeroing to clean.sh script * Also install PTF * Update versions of P4 dev tool source code to 2021-Apr-05 This includes a change to p4lang/PI that allows P4Runtime API clients to send the shortest byte sequences necessary to encode integer values, which I want for a PTF test that I have recently created. * Update README for 2021-Apr-05 version of VM image * Resolve Python 3 compatibility issues Most of the Python 2 to 3 code translation changes were automated with the 2to3 tool. Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org> * Update commit SHAs for 4 p4lang repos to latest as of 2021-May-04 * Update Ubuntu 20.04 README.md for how I created 2021-May-04 version of VM * mycontroller: Use Python 3 shebang line Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org> * Update Ubuntu 20.04 README.md for how I created 2021-Jun-01 version of VM * Update commit SHAs for 4 p4lang repos to latest as of 2021-Jul-07 * Update Ubuntu 20.04 README.md for how I created 2021-Jul-07 version of VM * Update commit SHAs for 4 p4lang repos to latest as of 2021-Aug-01 * Update Ubuntu 20.04 README.md for how I created 2021-Aug-01 version of VM * Update commit SHAs for 4 p4lang repos to latest as of 2021-Sep-07 * Update Ubuntu 20.04 README.md for how I created 2021-Sep-07 version of VM Co-authored-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
# Copyright 2013-present Barefoot Networks, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
|
|
import sys
|
|
|
|
from google.rpc import status_pb2, code_pb2
|
|
import grpc
|
|
from p4.v1 import p4runtime_pb2
|
|
from p4.v1 import p4runtime_pb2_grpc
|
|
|
|
# Used to indicate that the gRPC error Status object returned by the server has
|
|
# an incorrect format.
|
|
class P4RuntimeErrorFormatException(Exception):
|
|
def __init__(self, message):
|
|
super(P4RuntimeErrorFormatException, self).__init__(message)
|
|
|
|
|
|
# Parse the binary details of the gRPC error. This is required to print some
|
|
# helpful debugging information in tha case of batched Write / Read
|
|
# requests. Returns None if there are no useful binary details and throws
|
|
# P4RuntimeErrorFormatException if the error is not formatted
|
|
# properly. Otherwise, returns a list of tuples with the first element being the
|
|
# index of the operation in the batch that failed and the second element being
|
|
# the p4.Error Protobuf message.
|
|
def parseGrpcErrorBinaryDetails(grpc_error):
|
|
if grpc_error.code() != grpc.StatusCode.UNKNOWN:
|
|
return None
|
|
|
|
error = None
|
|
# The gRPC Python package does not have a convenient way to access the
|
|
# binary details for the error: they are treated as trailing metadata.
|
|
for meta in grpc_error.trailing_metadata():
|
|
if meta[0] == "grpc-status-details-bin":
|
|
error = status_pb2.Status()
|
|
error.ParseFromString(meta[1])
|
|
break
|
|
if error is None: # no binary details field
|
|
return None
|
|
if len(error.details) == 0:
|
|
# binary details field has empty Any details repeated field
|
|
return None
|
|
|
|
indexed_p4_errors = []
|
|
for idx, one_error_any in enumerate(error.details):
|
|
p4_error = p4runtime_pb2.Error()
|
|
if not one_error_any.Unpack(p4_error):
|
|
raise P4RuntimeErrorFormatException(
|
|
"Cannot convert Any message to p4.Error")
|
|
if p4_error.canonical_code == code_pb2.OK:
|
|
continue
|
|
indexed_p4_errors += [(idx, p4_error)]
|
|
|
|
return indexed_p4_errors
|
|
|
|
|
|
# P4Runtime uses a 3-level message in case of an error during the processing of
|
|
# a write batch. This means that some care is required when printing the
|
|
# exception if we do not want to end-up with a non-helpful message in case of
|
|
# failure as only the first level will be printed. In this function, we extract
|
|
# the nested error message when present (one for each operation included in the
|
|
# batch) in order to print error code + user-facing message. See P4Runtime
|
|
# documentation for more details on error-reporting.
|
|
def printGrpcError(grpc_error):
|
|
print("gRPC Error", grpc_error.details(), end=' ')
|
|
status_code = grpc_error.code()
|
|
print("({})".format(status_code.name), end=' ')
|
|
traceback = sys.exc_info()[2]
|
|
print("[{}:{}]".format(
|
|
traceback.tb_frame.f_code.co_filename, traceback.tb_lineno))
|
|
if status_code != grpc.StatusCode.UNKNOWN:
|
|
return
|
|
p4_errors = parseGrpcErrorBinaryDetails(grpc_error)
|
|
if p4_errors is None:
|
|
return
|
|
print("Errors in batch:")
|
|
for idx, p4_error in p4_errors:
|
|
code_name = code_pb2._CODE.values_by_number[
|
|
p4_error.canonical_code].name
|
|
print("\t* At index {}: {}, '{}'\n".format(
|
|
idx, code_name, p4_error.message))
|