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