1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "switch-flow.h"
29 #define THIS_MODULE VLM_chain
32 /* Attempts to append 'table' to the set of tables in 'chain'. Returns 0 or
33 * negative error. If 'table' is null it is assumed that table creation failed
34 * due to out-of-memory. */
35 static int add_table(struct sw_chain *chain, struct sw_table *table)
39 if (chain->n_tables >= CHAIN_MAX_TABLES) {
40 VLOG_ERR("too many tables in chain\n");
41 table->destroy(table);
44 chain->tables[chain->n_tables++] = table;
48 /* Creates and returns a new chain. Returns NULL if the chain cannot be
50 struct sw_chain *chain_create(void)
52 struct sw_chain *chain = calloc(1, sizeof *chain);
56 if (add_table(chain, table_mac_create(TABLE_MAC_NUM_BUCKETS,
58 || add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
59 0x741B8CD7, TABLE_HASH_MAX_FLOWS))
60 || add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS))) {
68 /* Searches 'chain' for a flow matching 'key', which must not have any wildcard
69 * fields. Returns the flow if successful, otherwise a null pointer. */
71 chain_lookup(struct sw_chain *chain, const struct sw_flow_key *key)
75 assert(!key->wildcards);
76 for (i = 0; i < chain->n_tables; i++) {
77 struct sw_table *t = chain->tables[i];
78 struct sw_flow *flow = t->lookup(t, key);
85 /* Inserts 'flow' into 'chain', replacing any duplicate flow. Returns 0 if
86 * successful or a negative error.
88 * If successful, 'flow' becomes owned by the chain, otherwise it is retained
91 chain_insert(struct sw_chain *chain, struct sw_flow *flow)
95 for (i = 0; i < chain->n_tables; i++) {
96 struct sw_table *t = chain->tables[i];
97 if (t->insert(t, flow))
104 /* Deletes from 'chain' any and all flows that match 'key'. Returns the number
105 * of flows that were deleted.
107 * Expensive in the general case as currently implemented, since it requires
108 * iterating through the entire contents of each table for keys that contain
109 * wildcards. Relatively cheap for fully specified keys.
111 * The caller need not hold any locks. */
113 chain_delete(struct sw_chain *chain, const struct sw_flow_key *key, int strict)
118 for (i = 0; i < chain->n_tables; i++) {
119 struct sw_table *t = chain->tables[i];
120 count += t->delete(t, key, strict);
127 /* Performs timeout processing on all the tables in 'chain'. Returns the
128 * number of flow entries deleted through expiration.
130 * Expensive as currently implemented, since it iterates through the entire
131 * contents of each table.
133 * The caller need not hold any locks. */
135 chain_timeout(struct sw_chain *chain, struct datapath *dp)
140 for (i = 0; i < chain->n_tables; i++) {
141 struct sw_table *t = chain->tables[i];
142 count += t->timeout(dp, t);
147 /* Destroys 'chain', which must not have any users. */
149 chain_destroy(struct sw_chain *chain)
153 for (i = 0; i < chain->n_tables; i++) {
154 struct sw_table *t = chain->tables[i];
160 /* Prints statistics for each of the tables in 'chain'. */
162 chain_print_stats(struct sw_chain *chain)
167 for (i = 0; i < chain->n_tables; i++) {
168 struct sw_table *t = chain->tables[i];
169 struct sw_table_stats stats;
171 printf("%s: %lu/%lu flows\n",
172 stats.name, stats.n_flows, stats.max_flows);