Skip to content

Commit

Permalink
p4tc: add table create, update, delete, get, flush and dump
Browse files Browse the repository at this point in the history
This commit introduces code to create and maintain P4 tables within a P4
program from user space and the next patch will have the code for
maintaining entries in the table.

As with all other P4TC objects, tables conform to CRUD operations and
it's important to note that write operations, such as create, update and
delete, can only be made if the pipeline is not sealed.

Per the P4 specification, tables prefix their name with the control block
(although this could be overridden by P4 annotations).

As an example, if one were to create a table named table1 in a
pipeline named myprog1, on control block "mycontrol", one would use
the following command:

tc p4template create table/myprog1/mycontrol/table1 tblid 1 \
   keysz 32 nummasks 8 tentries 8192

Above says that we are creating a table (table1) attached to pipeline
myprog1 on control block mycontrol which is called table1. Its key size is
32 bits and it can have up to 8 masks and 8192. The table id for table1 is
1. The table id is typically provided by the compiler.

Parameters such as nummasks (number of masks this table may have) and
tentries (maximum number of entries this table may have) may also be
omitted in which case 8 masks and 256 entries will be assumed.

If one were to retrieve the table named table1 (before or after the
pipeline is sealed) one would use the following command:

tc p4template get table/myprog1/mycontrol/table1

If one were to dump all the tables from a pipeline named myprog1, one would
use the following command:

tc p4template get table/myprog1

If one were to update table1 (before the pipeline is sealed) one would use
the following command:

tc p4template update table/myprog1/mycontrol/table1 ....

If one were to delete table1 (before the pipeline is sealed) one would use
the following command:

tc p4template del table/myprog1/mycontrol/table1

If one were to flush all the tables from a pipeline named myprog1, control
block "mycontrol" one would use the following command:

tc p4template del table/myprog1/mycontrol/

___Table Permissions___

Tables can have permissions which apply to all the entries in the specified
table. Permissions are defined for both what the control plane (user space)
is allowed to do as well as datapath.

The permissions field is a 16bit value which will hold CRUDX (create,
read, update, delete and execute) permissions for control and data path.
Bits 9-5 will have the CRUDX values for control and bits 4-0 will have
CRUDX values for data path. By default each table has the following
permissions:

CRUD--R--X

Which means the control plane can perform CRUD operations whereas the data
path can only Read and execute on the entries.
The user can override these permissions when creating the table or when
updating.

For example, the following command will create a table which will not allow
the datapath to create, update or delete entries but give full CRUD
permissions for the control plane.

$TC p4template create table/aP4proggie/cb/tname tblid 1 keysz 64
permissions 0x349 ...

Recall that these permissions come in the form of CRUDXCRUDX, where the
first CRUDX block is for control and the last is for data path.

So 0x349 is equivalent to CR-D--R--X

If we were to do a get with the following command:

$TC p4template get table/aP4proggie/cb/tname

The output would be the following:

pipeline name aP4proggie pipeline id 22
    table id 1
    table name cb/tname
    key_sz 64
    max entries 256
    masks 8
    table entries 0
    permissions CR-D--R--X

Note, the permissions concept is more powerful than classical const
definition currently taken by P4 which makes everything in a table
read-only.

___Initial Table Entries___

Templating can create initial table entries. For example:

tc p4template update table/myprog/cb/tname \
  entry srcAddr 10.10.10.10/24 dstAddr 1.1.1.0/24 prio 17

In this command we are "updating" table cb/tname with a new entry. This
entry has as its key srcAddr concatenated with dstAddr
(both IPv4 addresses) and prio 17.

If one was to read back the entry by issuing the following command:

tc p4template get myprog/table/cb/tname

They would get:

pipeline id 22
    table id 1
    table name cb/tname
    key_sz 64
    max entries 256
    masks 8
    table entries 1
    permissions CRUD--R--X
    entry:
        table id 1
        entry priority 17
        key blob    101010a0a0a0a
        mask blob   ffffff00ffffff
        create whodunnit tc
        permissions -RUD--R--X

___Table Actions List___

P4 tables allow certain actions but not other to be part of match entry on
a table or as default actions when there is a miss.

We also allow flags for each of the actions in this list that specify if
the action can be added only as a table entry (tableonly), or only as a
default action (defaultonly). If no flags are specified, it is assumed
that the action can be used in both contexts.

In P4TC we extend the concept of default action - which in P4 is mapped to
"a default miss action". Our extension is to add a "hit action" which is
executed every time there is a hit.

The default miss action will be executed whenever a table lookup doesn't
match any of the entries.

Both default hit and default miss are optional.

An example of specifying a default miss action is as follows:

tc p4template update table/myprog/cb/mytable \
    default_miss_action permissions 0x109 action drop

The above will drop packets if the entry is not found in mytable.
Note the above makes the default action a const. Meaning the control
plane can neither replace it nor delete it.

tc p4template update table/myprog/mytable \
  default_hit_action permissions 0x30F action ok

Whereas the above allows a default hit action to accept the packet.
The permission 0x30F in binary is (1100001111), which means we have only
Create and Read permissions in the control plane and Read, Update, Delete
and eXecute permissions in the data plane. This means, for example, that
now we can only delete the default hit action from the data plane.

__Packet Flow___

As with the pipeline, we also have preactions and postactions for tables
which can be programmed to teach the kernel how to process the packet.
Both are optional.
When a table apply() cmd is invoked on a table:

 1) The table preaction if present is invoked
 2) A "key action" is invoked to construct the table key
 3) A table lookup is done using the key from #2
 4a) If there is a hit
 - the match entry action will be executed
 - if there was a match and the entry has no action and a default hit
   action has been specified then the default hit action will be executed.
 4b) If there was a miss
 - if there was a default miss action it will be executed then

 5) if there is table post action then that is invoked next

Example of how one would create a key action for a table:
tc p4template create action/myprog/mytable/tkey \
   cmd set key.myprog.cb/mytable \
      hdrfield.myprog.parser1.ipv4.dstAddr

and now bind the key action to the table "mytable"
$TC p4template update table/myprog/cb/mytable \
        key action myprog/mytable/tkey

Example of how one would create a table post action is:
tc p4template create action/myprog/mytable/T_mytable_POA \
 cmd print prefix T_mytable_POA_res results.hit \
 cmd print prefix T_mytable_POA hdrfield.myprog.parser1.ipv4.dstAddr

Activate it..
tc p4template update action/myprog/mytable/T_mytable_POA state active

bind it..
$TC p4template update table/myprog/cb/mytable postactions \
    action myprog/mytable/T_mytable_POA

Co-developed-by: Victor Nogueira <victor@mojatatu.com>
Signed-off-by: Victor Nogueira <victor@mojatatu.com>
Co-developed-by: Pedro Tammela <pctammela@mojatatu.com>
Signed-off-by: Pedro Tammela <pctammela@mojatatu.com>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
  • Loading branch information
jhsmt authored and intel-lab-lkp committed May 17, 2023
1 parent dc5e6ef commit a3f3b24
Show file tree
Hide file tree
Showing 7 changed files with 1,886 additions and 3 deletions.
92 changes: 92 additions & 0 deletions include/net/p4tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
#define P4TC_DEFAULT_MAX_RULES 1
#define P4TC_MAXMETA_OFFSET 512
#define P4TC_PATH_MAX 3
#define P4TC_MAX_TENTRIES (2 << 23)
#define P4TC_DEFAULT_TENTRIES 256
#define P4TC_MAX_TMASKS 1024
#define P4TC_DEFAULT_TMASKS 8

#define P4TC_MAX_PERMISSION (GENMASK(P4TC_PERM_MAX_BIT, 0))

#define P4TC_KERNEL_PIPEID 0

#define P4TC_PID_IDX 0
#define P4TC_MID_IDX 1
#define P4TC_TBLID_IDX 1
#define P4TC_AID_IDX 1
#define P4TC_PARSEID_IDX 1
#define P4TC_HDRFIELDID_IDX 2
Expand Down Expand Up @@ -111,6 +118,7 @@ struct p4tc_pipeline {
struct p4tc_template_common common;
struct idr p_meta_idr;
struct idr p_act_idr;
struct idr p_tbl_idr;
struct rcu_head rcu;
struct net *net;
struct p4tc_parser *parser;
Expand Down Expand Up @@ -197,6 +205,70 @@ struct p4tc_metadata {

extern const struct p4tc_template_ops p4tc_meta_ops;

struct p4tc_table_key {
struct tc_action **key_acts;
int key_num_acts;
};

#define P4TC_CONTROL_PERMISSIONS (GENMASK(9, 5))
#define P4TC_DATA_PERMISSIONS (GENMASK(4, 0))

#define P4TC_TABLE_PERMISSIONS \
((GENMASK(P4TC_CTRL_PERM_C_BIT, P4TC_CTRL_PERM_D_BIT)) | \
P4TC_DATA_PERM_R | P4TC_DATA_PERM_X)

#define P4TC_PERMISSIONS_UNINIT (1 << P4TC_PERM_MAX_BIT)

struct p4tc_table_defact {
struct tc_action **default_acts;
/* Will have 2 5 bits blocks containing CRUDX (Create, read, update,
* delete, execute) permissions for control plane and data plane.
* The first 5 bits are for control and the next five are for data plane.
* |crudxcrudx| if we were to denote it as UNIX permission flags.
*/
__u16 permissions;
struct rcu_head rcu;
};

struct p4tc_table_perm {
__u16 permissions;
struct rcu_head rcu;
};

struct p4tc_table {
struct p4tc_template_common common;
struct list_head tbl_acts_list;
struct p4tc_table_key *tbl_key;
struct idr tbl_masks_idr;
struct idr tbl_prio_idr;
struct rhltable tbl_entries;
struct tc_action **tbl_preacts;
struct tc_action **tbl_postacts;
struct p4tc_table_defact __rcu *tbl_default_hitact;
struct p4tc_table_defact __rcu *tbl_default_missact;
struct p4tc_table_perm __rcu *tbl_permissions;
struct p4tc_table_entry_mask __rcu **tbl_masks_array;
unsigned long __rcu *tbl_free_masks_bitmap;
spinlock_t tbl_masks_idr_lock;
spinlock_t tbl_prio_idr_lock;
int tbl_num_postacts;
int tbl_num_preacts;
u32 tbl_count;
u32 tbl_curr_count;
u32 tbl_keysz;
u32 tbl_id;
u32 tbl_max_entries;
u32 tbl_max_masks;
u32 tbl_curr_used_entries;
u32 tbl_curr_num_masks;
refcount_t tbl_ctrl_ref;
refcount_t tbl_ref;
refcount_t tbl_entries_ref;
u16 tbl_type;
};

extern const struct p4tc_template_ops p4tc_table_ops;

struct p4tc_ipv4_param_value {
u32 value;
u32 mask;
Expand Down Expand Up @@ -254,6 +326,12 @@ struct p4tc_act {
refcount_t a_ref;
};

struct p4tc_table_act {
struct list_head node;
struct tc_action_ops *ops;
u8 flags;
};

extern const struct p4tc_template_ops p4tc_act_ops;
extern const struct rhashtable_params p4tc_label_ht_params;
extern const struct rhashtable_params acts_params;
Expand Down Expand Up @@ -352,6 +430,19 @@ struct p4tc_act_param *tcf_param_find_byany(struct p4tc_act *act,
const u32 param_id,
struct netlink_ext_ack *extack);

struct p4tc_table *tcf_table_find_byany(struct p4tc_pipeline *pipeline,
const char *tblname, const u32 tbl_id,
struct netlink_ext_ack *extack);
struct p4tc_table *tcf_table_find_byid(struct p4tc_pipeline *pipeline,
const u32 tbl_id);
void *tcf_table_fetch(struct sk_buff *skb, void *tbl_value_ops);
int tcf_table_try_set_state_ready(struct p4tc_pipeline *pipeline,
struct netlink_ext_ack *extack);
struct p4tc_table *tcf_table_get(struct p4tc_pipeline *pipeline,
const char *tblname, const u32 tbl_id,
struct netlink_ext_ack *extack);
void tcf_table_put_ref(struct p4tc_table *table);

struct p4tc_parser *tcf_parser_create(struct p4tc_pipeline *pipeline,
const char *parser_name,
u32 parser_inst_id,
Expand Down Expand Up @@ -402,5 +493,6 @@ int generic_dump_param_value(struct sk_buff *skb, struct p4tc_type *type,
#define to_meta(t) ((struct p4tc_metadata *)t)
#define to_hdrfield(t) ((struct p4tc_hdrfield *)t)
#define to_act(t) ((struct p4tc_act *)t)
#define to_table(t) ((struct p4tc_table *)t)

#endif
2 changes: 1 addition & 1 deletion include/net/p4tc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <uapi/linux/p4tc.h>

#define P4T_MAX_BITSZ 128
#define P4T_MAX_BITSZ P4TC_MAX_KEYSZ

struct p4tc_type_mask_shift {
void *mask;
Expand Down
112 changes: 112 additions & 0 deletions include/uapi/linux/p4tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,70 @@ struct p4tcmsg {
#define PARSERNAMSIZ TEMPLATENAMSZ
#define HDRFIELDNAMSIZ TEMPLATENAMSZ
#define ACTPARAMNAMSIZ TEMPLATENAMSZ
#define TABLENAMSIZ TEMPLATENAMSZ

#define P4TC_TABLE_FLAGS_KEYSZ 0x01
#define P4TC_TABLE_FLAGS_MAX_ENTRIES 0x02
#define P4TC_TABLE_FLAGS_MAX_MASKS 0x04
#define P4TC_TABLE_FLAGS_DEFAULT_KEY 0x08
#define P4TC_TABLE_FLAGS_PERMISSIONS 0x10
#define P4TC_TABLE_FLAGS_TYPE 0x20

enum {
P4TC_TABLE_TYPE_EXACT = 1,
P4TC_TABLE_TYPE_LPM = 2,
__P4TC_TABLE_TYPE_MAX,
};
#define P4TC_TABLE_TYPE_MAX (__P4TC_TABLE_TYPE_MAX - 1)

#define P4TC_CTRL_PERM_C_BIT 9
#define P4TC_CTRL_PERM_R_BIT 8
#define P4TC_CTRL_PERM_U_BIT 7
#define P4TC_CTRL_PERM_D_BIT 6
#define P4TC_CTRL_PERM_X_BIT 5

#define P4TC_DATA_PERM_C_BIT 4
#define P4TC_DATA_PERM_R_BIT 3
#define P4TC_DATA_PERM_U_BIT 2
#define P4TC_DATA_PERM_D_BIT 1
#define P4TC_DATA_PERM_X_BIT 0

#define P4TC_PERM_MAX_BIT P4TC_CTRL_PERM_C_BIT

#define P4TC_CTRL_PERM_C (1 << P4TC_CTRL_PERM_C_BIT)
#define P4TC_CTRL_PERM_R (1 << P4TC_CTRL_PERM_R_BIT)
#define P4TC_CTRL_PERM_U (1 << P4TC_CTRL_PERM_U_BIT)
#define P4TC_CTRL_PERM_D (1 << P4TC_CTRL_PERM_D_BIT)
#define P4TC_CTRL_PERM_X (1 << P4TC_CTRL_PERM_X_BIT)

#define P4TC_DATA_PERM_C (1 << P4TC_DATA_PERM_C_BIT)
#define P4TC_DATA_PERM_R (1 << P4TC_DATA_PERM_R_BIT)
#define P4TC_DATA_PERM_U (1 << P4TC_DATA_PERM_U_BIT)
#define P4TC_DATA_PERM_D (1 << P4TC_DATA_PERM_D_BIT)
#define P4TC_DATA_PERM_X (1 << P4TC_DATA_PERM_X_BIT)

#define p4tc_ctrl_create_ok(perm) (perm & P4TC_CTRL_PERM_C)
#define p4tc_ctrl_read_ok(perm) (perm & P4TC_CTRL_PERM_R)
#define p4tc_ctrl_update_ok(perm) (perm & P4TC_CTRL_PERM_U)
#define p4tc_ctrl_delete_ok(perm) (perm & P4TC_CTRL_PERM_D)
#define p4tc_ctrl_exec_ok(perm) (perm & P4TC_CTRL_PERM_X)

#define p4tc_data_create_ok(perm) (perm & P4TC_DATA_PERM_C)
#define p4tc_data_read_ok(perm) (perm & P4TC_DATA_PERM_R)
#define p4tc_data_update_ok(perm) (perm & P4TC_DATA_PERM_U)
#define p4tc_data_delete_ok(perm) (perm & P4TC_DATA_PERM_D)
#define p4tc_data_exec_ok(perm) (perm & P4TC_DATA_PERM_X)

struct p4tc_table_parm {
__u32 tbl_keysz;
__u32 tbl_max_entries;
__u32 tbl_max_masks;
__u32 tbl_flags;
__u32 tbl_num_entries;
__u16 tbl_permissions;
__u8 tbl_type;
__u8 PAD0;
};

#define LABELNAMSIZ 32

Expand Down Expand Up @@ -63,6 +127,7 @@ enum {
P4TC_OBJ_META,
P4TC_OBJ_HDR_FIELD,
P4TC_OBJ_ACT,
P4TC_OBJ_TABLE,
__P4TC_OBJ_MAX,
};
#define P4TC_OBJ_MAX __P4TC_OBJ_MAX
Expand Down Expand Up @@ -161,6 +226,53 @@ enum {
};
#define P4TC_KERNEL_META_MAX (__P4TC_KERNEL_META_MAX - 1)

/* Table key attributes */
enum {
P4TC_KEY_UNSPEC,
P4TC_KEY_ACT, /* nested key actions */
__P4TC_TKEY_MAX
};
#define P4TC_TKEY_MAX __P4TC_TKEY_MAX

enum {
P4TC_TABLE_DEFAULT_UNSPEC,
P4TC_TABLE_DEFAULT_ACTION,
P4TC_TABLE_DEFAULT_PERMISSIONS,
__P4TC_TABLE_DEFAULT_MAX
};
#define P4TC_TABLE_DEFAULT_MAX (__P4TC_TABLE_DEFAULT_MAX - 1)

enum {
P4TC_TABLE_ACTS_DEFAULT_ONLY,
P4TC_TABLE_ACTS_TABLE_ONLY,
__P4TC_TABLE_ACTS_FLAGS_MAX,
};
#define P4TC_TABLE_ACTS_FLAGS_MAX (__P4TC_TABLE_ACTS_FLAGS_MAX - 1)

enum {
P4TC_TABLE_ACT_UNSPEC,
P4TC_TABLE_ACT_FLAGS, /* u8 */
P4TC_TABLE_ACT_NAME, /* string */
__P4TC_TABLE_ACT_MAX
};
#define P4TC_TABLE_ACT_MAX (__P4TC_TABLE_ACT_MAX - 1)

/* Table type attributes */
enum {
P4TC_TABLE_UNSPEC,
P4TC_TABLE_NAME, /* string */
P4TC_TABLE_INFO, /* struct tc_p4_table_type_parm */
P4TC_TABLE_PREACTIONS, /* nested table preactions */
P4TC_TABLE_KEY, /* nested table key */
P4TC_TABLE_POSTACTIONS, /* nested table postactions */
P4TC_TABLE_DEFAULT_HIT, /* nested default hit action attributes */
P4TC_TABLE_DEFAULT_MISS, /* nested default miss action attributes */
P4TC_TABLE_OPT_ENTRY, /* nested const table entry*/
P4TC_TABLE_ACTS_LIST, /* nested table actions list */
__P4TC_TABLE_MAX
};
#define P4TC_TABLE_MAX __P4TC_TABLE_MAX

struct p4tc_hdrfield_ty {
__u16 startbit;
__u16 endbit;
Expand Down
2 changes: 1 addition & 1 deletion net/sched/p4tc/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0

obj-y := p4tc_types.o p4tc_pipeline.o p4tc_tmpl_api.o p4tc_meta.o \
p4tc_parser_api.o p4tc_hdrfield.o p4tc_action.o
p4tc_parser_api.o p4tc_hdrfield.o p4tc_action.o p4tc_table.o
15 changes: 14 additions & 1 deletion net/sched/p4tc/p4tc_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ static void tcf_pipeline_destroy(struct p4tc_pipeline *pipeline,
{
idr_destroy(&pipeline->p_meta_idr);
idr_destroy(&pipeline->p_act_idr);
idr_destroy(&pipeline->p_tbl_idr);

if (free_pipeline)
kfree(pipeline);
Expand All @@ -323,8 +324,9 @@ static int tcf_pipeline_put(struct net *net,
struct p4tc_pipeline *pipeline = to_pipeline(template);
struct net *pipeline_net = maybe_get_net(net);
struct p4tc_act_dep_node *act_node, *node_tmp;
unsigned long m_id, tmp;
unsigned long tbl_id, m_id, tmp;
struct p4tc_metadata *meta;
struct p4tc_table *table;

if (pipeline_net && !refcount_dec_if_one(&pipeline->p_ref)) {
NL_SET_ERR_MSG(extack, "Can't delete referenced pipeline");
Expand All @@ -339,6 +341,9 @@ static int tcf_pipeline_put(struct net *net,
p4tc_action_destroy(pipeline->preacts);
p4tc_action_destroy(pipeline->postacts);

idr_for_each_entry_ul(&pipeline->p_tbl_idr, table, tmp, tbl_id)
table->common.ops->put(net, &table->common, true, extack);

act_dep_graph_free(&pipeline->act_dep_graph);

list_for_each_entry_safe(act_node, node_tmp,
Expand Down Expand Up @@ -371,6 +376,8 @@ static int tcf_pipeline_put(struct net *net,
static inline int pipeline_try_set_state_ready(struct p4tc_pipeline *pipeline,
struct netlink_ext_ack *extack)
{
int ret;

if (pipeline->curr_tables != pipeline->num_tables) {
NL_SET_ERR_MSG(extack,
"Must have all table defined to update state to ready");
Expand All @@ -388,6 +395,9 @@ static inline int pipeline_try_set_state_ready(struct p4tc_pipeline *pipeline,
"Must specify pipeline postactions before sealing");
return -EINVAL;
}
ret = tcf_table_try_set_state_ready(pipeline, extack);
if (ret < 0)
return ret;

/* Will never fail in this case */
determine_act_topological_order(pipeline, false);
Expand Down Expand Up @@ -542,6 +552,9 @@ static struct p4tc_pipeline *tcf_pipeline_create(struct net *net,

idr_init(&pipeline->p_act_idr);

idr_init(&pipeline->p_tbl_idr);
pipeline->curr_tables = 0;

idr_init(&pipeline->p_meta_idr);
pipeline->p_meta_offset = 0;

Expand Down

0 comments on commit a3f3b24

Please sign in to comment.