Move Autoconf's macro definitions into config.h.
[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 <config.h>
35 #include "chain.h"
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include "switch-flow.h"
40 #include "table.h"
41
42 #define THIS_MODULE VLM_chain
43 #include "vlog.h"
44
45 /* Attempts to append 'table' to the set of tables in 'chain'.  Returns 0 or
46  * negative error.  If 'table' is null it is assumed that table creation failed
47  * due to out-of-memory. */
48 static int add_table(struct sw_chain *chain, struct sw_table *table)
49 {
50     if (table == NULL)
51         return -ENOMEM;
52     if (chain->n_tables >= CHAIN_MAX_TABLES) {
53         VLOG_ERR("too many tables in chain\n");
54         table->destroy(table);
55         return -ENOBUFS;
56     }
57     chain->tables[chain->n_tables++] = table;
58     return 0;
59 }
60
61 /* Creates and returns a new chain.  Returns NULL if the chain cannot be
62  * created. */
63 struct sw_chain *chain_create(void)
64 {
65     struct sw_chain *chain = calloc(1, sizeof *chain);
66     if (chain == NULL)
67         return NULL;
68
69     if (add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
70                                                0x741B8CD7, TABLE_HASH_MAX_FLOWS))
71         || add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS))) {
72         chain_destroy(chain);
73         return NULL;
74     }
75
76     return chain;
77 }
78
79 /* Searches 'chain' for a flow matching 'key', which must not have any wildcard
80  * fields.  Returns the flow if successful, otherwise a null pointer. */
81 struct sw_flow *
82 chain_lookup(struct sw_chain *chain, const struct sw_flow_key *key)
83 {
84     int i;
85
86     assert(!key->wildcards);
87     for (i = 0; i < chain->n_tables; i++) {
88         struct sw_table *t = chain->tables[i];
89         struct sw_flow *flow = t->lookup(t, key);
90         if (flow)
91             return flow;
92     }
93     return NULL;
94 }
95
96 /* Inserts 'flow' into 'chain', replacing any duplicate flow.  Returns 0 if
97  * successful or a negative error.
98  *
99  * If successful, 'flow' becomes owned by the chain, otherwise it is retained
100  * by the caller. */
101 int
102 chain_insert(struct sw_chain *chain, struct sw_flow *flow)
103 {
104     int i;
105
106     for (i = 0; i < chain->n_tables; i++) {
107         struct sw_table *t = chain->tables[i];
108         if (t->insert(t, flow))
109             return 0;
110     }
111
112     return -ENOBUFS;
113 }
114
115 /* Deletes from 'chain' any and all flows that match 'key'.  Returns the number
116  * of flows that were deleted.
117  *
118  * Expensive in the general case as currently implemented, since it requires
119  * iterating through the entire contents of each table for keys that contain
120  * wildcards.  Relatively cheap for fully specified keys. */
121 int
122 chain_delete(struct sw_chain *chain, const struct sw_flow_key *key, 
123              uint16_t priority, int strict)
124 {
125     int count = 0;
126     int i;
127
128     for (i = 0; i < chain->n_tables; i++) {
129         struct sw_table *t = chain->tables[i];
130         count += t->delete(t, key, priority, strict);
131     }
132
133     return count;
134
135 }
136
137 /* Deletes timed-out flow entries from all the tables in 'chain' and appends
138  * the deleted flows to 'deleted'.
139  *
140  * Expensive as currently implemented, since it iterates through the entire
141  * contents of each table. */
142 void
143 chain_timeout(struct sw_chain *chain, struct list *deleted)
144 {
145     int i;
146
147     for (i = 0; i < chain->n_tables; i++) {
148         struct sw_table *t = chain->tables[i];
149         t->timeout(t, deleted);
150     }
151 }
152
153 /* Destroys 'chain', which must not have any users. */
154 void
155 chain_destroy(struct sw_chain *chain)
156 {
157     int i;
158
159     for (i = 0; i < chain->n_tables; i++) {
160         struct sw_table *t = chain->tables[i];
161         t->destroy(t);
162     }
163     free(chain);
164 }
165
166 /* Prints statistics for each of the tables in 'chain'. */
167 void
168 chain_print_stats(struct sw_chain *chain)
169 {
170     int i;
171
172     printf("\n");
173     for (i = 0; i < chain->n_tables; i++) {
174         struct sw_table *t = chain->tables[i];
175         struct sw_table_stats stats;
176         t->stats(t, &stats);
177         printf("%s: %lu/%lu flows\n",
178                stats.name, stats.n_flows, stats.max_flows);
179     }
180 }