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