p4lang / p4runtime-shell

An interactive Python shell for P4Runtime
Apache License 2.0
79 stars 40 forks source link

how can I CRUD to p4 tables #34

Open ghost opened 4 years ago

ghost commented 4 years ago

I want to filter an entry from tables, ,the only way I know is te.read() .then add an lambda function to match the key. Is there any way to read and filter an entry object?

And I also want to update one of parameter of action rather than override all parameters. I can't find an elegant way to do this using p4rumtime-shell

antoninbas commented 4 years ago

Your approach is pretty good, but I assume you want to filter entries on the server side instead - to avoid having to send all the entries from the server to the client.

The different server-side filtering issues are described in the P4Runtime spec: https://p4.org/p4runtime/spec/v1.1.0/P4Runtime-Spec.html#sec-table-wildcard-reads. If you just want to "match the key", it is very easy, just set the match key in the request and it will return either 0 (no match) or 1 entry (one entry has the provided key). Unfortunately, the other filtering options described in the spec (e.g. on the action id) are currently not supported by the reference P4Runtime server implementation. See this TODO.


Regarding your second question (update an action parameter). This is not supported by the P4Runtime protocol, i.e. the full action specification needs to be sent to the P4Runtime server and your entry will be "overwritten". However, I don't understand what you mean by "I can't find an elegant way to do this using p4runtime-shell". The following looks pretty elegant to me:

t = table_entry['<table_name>'](action='<action_name>')
# match fields
t.match['<f1>'] = ...
...
t.match['<fN>'] = ...
# action parameters
t.action['<p1>'] = ...
...
t.action['<pM>'] = ...
# insert the entry
t.insert
# modify one action parameter
t.action['<p1>'] = <new value>
t.modify

The above should work (please let me know if it doesn't), even though behind the scene it will "overwrite" the full entry. By which I mean that the full entry will be sent to the P4Runtime server, and , in the case of bmv2 at least, the entry will be overwritten (not just the memory area storing the action parameter).