For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / udatapath / 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(struct datapath *dp)
64 {
65     struct sw_chain *chain = calloc(1, sizeof *chain);
66     if (chain == NULL)
67         return NULL;
68
69     chain->dp = dp;
70     if (add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
71                                                0x741B8CD7, TABLE_HASH_MAX_FLOWS))
72         || add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS))) {
73         chain_destroy(chain);
74         return NULL;
75     }
76
77     return chain;
78 }
79
80 /* Searches 'chain' for a flow matching 'key', which must not have any wildcard
81  * fields.  Returns the flow if successful, otherwise a null pointer. */
82 struct sw_flow *
83 chain_lookup(struct sw_chain *chain, const struct sw_flow_key *key)
84 {
85     int i;
86
87     assert(!key->wildcards);
88     for (i = 0; i < chain->n_tables; i++) {
89         struct sw_table *t = chain->tables[i];
90         struct sw_flow *flow = t->lookup(t, key);
91         t->n_lookup++;
92         if (flow) {
93             t->n_matched++;
94             return flow;
95         }
96     }
97     return NULL;
98 }
99
100 /* Inserts 'flow' into 'chain', replacing any duplicate flow.  Returns 0 if
101  * successful or a negative error.
102  *
103  * If successful, 'flow' becomes owned by the chain, otherwise it is retained
104  * by the caller. */
105 int
106 chain_insert(struct sw_chain *chain, struct sw_flow *flow)
107 {
108     int i;
109
110     for (i = 0; i < chain->n_tables; i++) {
111         struct sw_table *t = chain->tables[i];
112         if (t->insert(t, flow))
113             return 0;
114     }
115
116     return -ENOBUFS;
117 }
118
119 /* Modifies actions in 'chain' that match 'key'.  If 'strict' set, wildcards 
120  * and priority must match.  Returns the number of flows that were modified.
121  *
122  * Expensive in the general case as currently implemented, since it requires
123  * iterating through the entire contents of each table for keys that contain
124  * wildcards.  Relatively cheap for fully specified keys. */
125 int
126 chain_modify(struct sw_chain *chain, const struct sw_flow_key *key,
127         uint16_t priority, int strict,
128         const struct ofp_action_header *actions, size_t actions_len)
129 {
130     int count = 0;
131     int i;
132
133     for (i = 0; i < chain->n_tables; i++) {
134         struct sw_table *t = chain->tables[i];
135         count += t->modify(t, key, priority, strict, actions, actions_len);
136     }
137
138     return count;
139 }
140
141 /* Deletes from 'chain' any and all flows that match 'key'.  If 'out_port' 
142  * is not OFPP_NONE, then matching entries must have that port as an 
143  * argument for an output action.  If 'strict" is set, then wildcards and 
144  * priority must match.  Returns the number of flows that were deleted.
145  *
146  * Expensive in the general case as currently implemented, since it requires
147  * iterating through the entire contents of each table for keys that contain
148  * wildcards.  Relatively cheap for fully specified keys. */
149 int
150 chain_delete(struct sw_chain *chain, const struct sw_flow_key *key, 
151              uint16_t out_port, uint16_t priority, int strict)
152 {
153     int count = 0;
154     int i;
155
156     for (i = 0; i < chain->n_tables; i++) {
157         struct sw_table *t = chain->tables[i];
158         count += t->delete(chain->dp, t, key, out_port, priority, strict);
159     }
160
161     return count;
162
163 }
164
165 /* Deletes timed-out flow entries from all the tables in 'chain' and appends
166  * the deleted flows to 'deleted'.
167  *
168  * Expensive as currently implemented, since it iterates through the entire
169  * contents of each table. */
170 void
171 chain_timeout(struct sw_chain *chain, struct list *deleted)
172 {
173     int i;
174
175     for (i = 0; i < chain->n_tables; i++) {
176         struct sw_table *t = chain->tables[i];
177         t->timeout(t, deleted);
178     }
179 }
180
181 /* Destroys 'chain', which must not have any users. */
182 void
183 chain_destroy(struct sw_chain *chain)
184 {
185     int i;
186
187     for (i = 0; i < chain->n_tables; i++) {
188         struct sw_table *t = chain->tables[i];
189         t->destroy(t);
190     }
191     free(chain);
192 }