Merge commit 'origin/master'
[sliver-openvswitch.git] / datapath / table.h
1 /* Individual switching tables.  Generally grouped together in a chain (see
2  * chain.h). */
3
4 #ifndef TABLE_H
5 #define TABLE_H 1
6
7 #include <linux/types.h>
8
9 struct sw_flow;
10 struct sw_flow_key;
11 struct ofp_action;
12 struct datapath;
13
14 /* Table statistics. */
15 struct sw_table_stats {
16         const char *name;            /* Human-readable name. */
17         uint32_t wildcards;          /* Bitmap of OFPFW_* wildcards that are
18                                         supported by the table. */
19         unsigned int n_flows;        /* Number of active flows. */
20         unsigned int max_flows;      /* Flow capacity. */
21         unsigned long int n_matched; /* Number of packets that have hit. */
22 };
23
24 /* Position within an iteration of a sw_table.
25  *
26  * The contents are private to the table implementation, except that a position
27  * initialized to all-zero-bits represents the start of a table. */
28 struct sw_table_position {
29         unsigned long private[4];
30 };
31
32 /* A single table of flows.
33  *
34  * All functions, except destroy, must be called holding the
35  * rcu_read_lock.  destroy must be fully serialized.
36  */
37 struct sw_table {
38         /* Keep track of the number of packets that matched this table.  To
39          * make this 100% accurate, it should be atomic.  However, we're
40          * primarily concerned about speed. */
41         unsigned long int n_matched;
42
43         /* Searches 'table' for a flow matching 'key', which must not have any
44          * wildcard fields.  Returns the flow if successful, a null pointer
45          * otherwise. */
46         struct sw_flow *(*lookup)(struct sw_table *table,
47                         const struct sw_flow_key *key);
48
49         /* Inserts 'flow' into 'table', replacing any duplicate flow.  Returns
50          * 0 if successful or a negative error.  Error can be due to an
51          * over-capacity table or because the flow is not one of the kind that
52          * the table accepts.
53          *
54          * If successful, 'flow' becomes owned by 'table', otherwise it is
55          * retained by the caller. */
56         int (*insert)(struct sw_table *table, struct sw_flow *flow);
57
58         /* Modifies the actions in 'table' that match 'key'.  Returns the
59          * number of flows that were modified. */
60         int (*modify)(struct sw_table *table, const struct sw_flow_key *key,
61                         const struct ofp_action *actions, int n_actions);
62
63         /* Deletes from 'table' any and all flows that match 'key' from
64          * 'table'.  If 'strict' set, wildcards and priority must match.  
65          * Returns the number of flows that were deleted. */
66         int (*delete)(struct sw_table *table, const struct sw_flow_key *key, 
67                         uint16_t priority, int strict);
68
69         /* Performs timeout processing on all the flow entries in 'table'.
70          * Returns the number of flow entries deleted through expiration. */
71         int (*timeout)(struct datapath *dp, struct sw_table *table);
72
73         /* Destroys 'table', which must not have any users. */
74         void (*destroy)(struct sw_table *table);
75
76         /* Iterates through the flow entries in 'table', passing each one
77          * matches 'key' to 'callback'.  The callback function should return 0
78          * to continue iteration or a nonzero error code to stop.  The iterator
79          * function returns either 0 if the table iteration completed or the
80          * value returned by the callback function otherwise.
81          *
82          * The iteration starts at 'position', which may be initialized to
83          * all-zero-bits to iterate from the beginning of the table.  If the
84          * iteration terminates due to an error from the callback function,
85          * 'position' is updated to a value that can be passed back to the
86          * iterator function to continue iteration later from the same position
87          * that caused the error (assuming that that flow entry has not been
88          * deleted in the meantime). */
89         int (*iterate)(struct sw_table *table,
90                        const struct sw_flow_key *key,
91                        struct sw_table_position *position,
92                        int (*callback)(struct sw_flow *flow, void *private),
93                        void *private);
94
95         /* Dumps statistics for 'table' into 'stats'. */
96         void (*stats)(struct sw_table *table, struct sw_table_stats *stats);
97 };
98
99 struct sw_table *table_hash_create(unsigned int polynomial,
100                 unsigned int n_buckets);
101 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
102                 unsigned int poly1, unsigned int buckets1);
103 struct sw_table *table_linear_create(unsigned int max_flows);
104
105 #endif /* table.h */