# \[BMv2\] Parse P4runtime\_pb2

**URL:** <https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276>\
**Category:** API Working Group\
**Created:** [March 17, 2022, 4:01pm UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276 "2022-03-17T16:01:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Finnonto](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Finnonto](https://forum.p4.org/u/Finnonto)\
**Post date:** [March 17, 2022, 4:01pm UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276/1 "2022-03-17T16:01:18Z")

</div>

Hi!

Because there is no API for register R/W on BMv2, I read register through CPU\_IN and CPU\_OUT packet. However, when I dumped the data, which type is **\<class ‘p4.v1.p4runtime\_pb2.StreamMessageResponse’\>** ,be like

```auto
packet {
  payload: "CNSRL"
  metadata {
    metadata_id: 1
    value: "\377"
  }
  metadata {
    metadata_id: 2
    value: "\242\016>s&"
  }
  metadata {
    metadata_id: 3
    value: "\242\017bZ\300"
  }
  metadata {
    metadata_id: 4
    value: "\242\n\031\241\222"
  }

```

I think the value field should be hex format but octal and some of them are converted to character(ASCII).

My question is how to parse “value” field and converted into readable data.

---

<div class="post-metadata">

**Author:** ![antonin](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@antonin](https://forum.p4.org/u/antonin)\
**Post date:** [March 17, 2022, 9:02pm UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276/2 "2022-03-17T21:02:33Z")

</div>

Depends on what the data is and what you want to do with it. It’s a [bytestring](https://p4.org/p4-spec/p4runtime/v1.3.0/P4Runtime-Spec.html#sec-bytestrings).

If it’s a plain integer, you could use `int.from_bytes`. For example:

```python
>>> int.from_bytes(b"\377", byteorder="big")
255
>>> int.from_bytes(b"\242\017bZ\300", byteorder="big")
696042805952

```

If you want an hexadecimal string:

```python
>>> b"\242\017bZ\300".hex()
'a20f625ac0'

```

---

<div class="post-metadata">

**Author:** ![Finnonto](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Finnonto](https://forum.p4.org/u/Finnonto)\
**Post date:** [March 19, 2022, 7:42am UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276/3 "2022-03-19T07:42:20Z")

</div>

Antonin appreciate your reply!

---

<div class="post-metadata">

**Author:** ![Finnonto](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Finnonto](https://forum.p4.org/u/Finnonto)\
**Post date:** [March 20, 2022, 10:16am UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276/4 "2022-03-20T10:16:49Z")

</div>

My final python3 solution as below.

I use str() function convert **\<class ‘p4.v1.p4runtime\_pb2.StreamMessageResponse’\>** into **\<class ‘str’\>**.  
Then use regex to extract value fileds

```auto
re.findall(r'value: "(.*)(?<!\\)"',str)

```

Extracted values are strings and have backslash escape character. It can not use int.from\_bytes() directly.

```auto
value_str = "\\377"

```

So I did some transfers

```auto
value_str = value_str.encode('utf-8').decode('unicode_escape')
value_int = int.from_bytes(bytes(value_str,'latin_1'), byteorder="big")

```

---

<div class="post-metadata">

**Author:** ![antonin](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@antonin](https://forum.p4.org/u/antonin)\
**Post date:** [March 21, 2022, 7:00pm UTC](https://forum.p4.org/t/bmv2-parse-p4runtime-pb2/276/5 "2022-03-21T19:00:37Z")

</div>

There is no need to use a regex to parse Protobuf messages…  
You are taking a Python object of type `p4.v1.p4runtime_pb2.StreamMessageResponse`, serializing it to a string, and parsing the string using a regex, all of this to get access to information which is already available as class members of your original object. I find it super confusing.

```python
for metadata in packet.metadata:
   print(metada.metadata_id)
   print(metadata.value)

```

Maybe give a quick read to [Protocol Buffer Basics: Python &nbsp;|&nbsp; Protocol Buffers &nbsp;|&nbsp; Google Developers](https://developers.google.com/protocol-buffers/docs/pythontutorial)
