Hide contents of sw_chain.
[sliver-openvswitch.git] / switch / chain.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "chain.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include "switch-flow.h"
39 #include "table.h"
40
41 #define THIS_MODULE VLM_chain
42 #include "vlog.h"
43
44 /* Set of tables chained together in sequence from cheap to expensive. */
45 #define CHAIN_MAX_TABLES 4
46 struct sw_chain {
47     int n_tables;
48     struct sw_table *tables[CHAIN_MAX_TABLES];
49 };
50
51 /* Attempts to append 'table' to the set of tables in 'chain'.  Returns 0 or
52  * negative error.  If 'table' is null it is assumed that table creation failed
53  * due to out-of-memory. */
54 static int add_table(struct sw_chain *chain, struct sw_table *table)
55 {
56     if (table == NULL)
57         return -ENOMEM;
58     if (chain->n_tables >= CHAIN_MAX_TABLES) {
59         VLOG_ERR("too many tables in chain\n");
60         table->destroy(table);
61         return -ENOBUFS;
62     }
63     chain->tables[chain->n_tables++] = table;
64     return 0;
65 }
66
67 /* Creates and returns a new chain.  Returns NULL if the chain cannot be
68  * created. */
69 struct sw_chain *chain_create(void)
70 {
71     struct sw_chain *chain = calloc(1, sizeof *chain);
72     if (chain == NULL)
73         return NULL;
74
75     if (add_table(chain, table_mac_create(TABLE_MAC_NUM_BUCKETS, 
76                                           TABLE_MAC_MAX_FLOWS))
77         || add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
78                                                0x741B8CD7, TABLE_HASH_MAX_FLOWS))
79         || add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS))) {
80         chain_destroy(chain);
81         return NULL;
82     }
83
84     return chain;
85 }
86
87 /* Searches 'chain' for a flow matching 'key', which must not have any wildcard
88  * fields.  Returns the flow if successful, otherwise a null pointer. */
89 struct sw_flow *
90 chain_lookup(struct sw_chain *chain, const struct sw_flow_key *key)
91 {
92     int i;
93
94     assert(!key->wildcards);
95     for (i = 0; i < chain->n_tables; i++) {
96         struct sw_table *t = chain->tables[i];
97         struct sw_flow *flow = t->lookup(t, key);
98         if (flow)
99             return flow;
100     }
101     return NULL;
102 }
103
104 /* Inserts 'flow' into 'chain', replacing any duplicate flow.  Returns 0 if
105  * successful or a negative error.
106  *
107  * If successful, 'flow' becomes owned by the chain, otherwise it is retained
108  * by the caller. */
109 int
110 chain_insert(struct sw_chain *chain, struct sw_flow *flow)
111 {
112     int i;
113
114     for (i = 0; i < chain->n_tables; i++) {
115         struct sw_table *t = chain->tables[i];
116         if (t->insert(t, flow))
117             return 0;
118     }
119
120     return -ENOBUFS;
121 }
122
123 /* Deletes from 'chain' any and all flows that match 'key'.  Returns the number
124  * of flows that were deleted.
125  *
126  * Expensive in the general case as currently implemented, since it requires
127  * iterating through the entire contents of each table for keys that contain
128  * wildcards.  Relatively cheap for fully specified keys.
129  *
130  * The caller need not hold any locks. */
131 int
132 chain_delete(struct sw_chain *chain, const struct sw_flow_key *key, int strict)
133 {
134     int count = 0;
135     int i;
136
137     for (i = 0; i < chain->n_tables; i++) {
138         struct sw_table *t = chain->tables[i];
139         count += t->delete(t, key, strict);
140     }
141
142     return count;
143
144 }
145
146 /* Performs timeout processing on all the tables in 'chain'.  Returns the
147  * number of flow entries deleted through expiration.
148  *
149  * Expensive as currently implemented, since it iterates through the entire
150  * contents of each table.
151  *
152  * The caller need not hold any locks. */
153 int
154 chain_timeout(struct sw_chain *chain, struct datapath *dp)
155 {
156     int count = 0;
157     int i;
158
159     for (i = 0; i < chain->n_tables; i++) {
160         struct sw_table *t = chain->tables[i];
161         count += t->timeout(dp, t);
162     }
163     return count;
164 }
165
166 /* Destroys 'chain', which must not have any users. */
167 void
168 chain_destroy(struct sw_chain *chain)
169 {
170     int i;
171
172     for (i = 0; i < chain->n_tables; i++) {
173         struct sw_table *t = chain->tables[i];
174         t->destroy(t);
175     }
176     free(chain);
177 }
178
179 /* Prints statistics for each of the tables in 'chain'. */
180 void
181 chain_print_stats(struct sw_chain *chain)
182 {
183     int i;
184
185     printf("\n");
186     for (i = 0; i < chain->n_tables; i++) {
187         struct sw_table *t = chain->tables[i];
188         struct sw_table_stats stats;
189         t->stats(t, &stats);
190         printf("%s: %lu/%lu flows\n",
191                stats.name, stats.n_flows, stats.max_flows);
192     }
193 }