c71f0bdbdc98cbb699811cfbdc9f126006f8e5f8
[sliver-openvswitch.git] / datapath / table-linear.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "table.h"
8 #include "flow.h"
9 #include "datapath.h"
10
11 #include <linux/rcupdate.h>
12 #include <linux/slab.h>
13 #include <linux/rculist.h>
14
15 struct sw_table_linear {
16         struct sw_table swt;
17
18         unsigned int max_flows;
19         unsigned int n_flows;
20         struct list_head flows;
21         struct list_head iter_flows;
22         unsigned long int next_serial;
23 };
24
25 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
26                                          const struct sw_flow_key *key)
27 {
28         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
29         struct sw_flow *flow;
30         list_for_each_entry_rcu (flow, &tl->flows, node) {
31                 if (flow_matches_1wild(key, &flow->key))
32                         return flow;
33         }
34         return NULL;
35 }
36
37 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
38 {
39         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
40         struct sw_flow *f;
41
42
43         /* Loop through the existing list of entries.  New entries will
44          * always be placed behind those with equal priority.  Just replace 
45          * any flows that match exactly.
46          */
47         list_for_each_entry (f, &tl->flows, node) {
48                 if (f->priority == flow->priority
49                                 && f->key.wildcards == flow->key.wildcards
50                                 && flow_matches_2wild(&f->key, &flow->key)) {
51                         flow->serial = f->serial;
52                         list_replace_rcu(&f->node, &flow->node);
53                         list_replace_rcu(&f->iter_node, &flow->iter_node);
54                         flow_deferred_free(f);
55                         return 1;
56                 }
57
58                 if (f->priority < flow->priority)
59                         break;
60         }
61
62         /* Make sure there's room in the table. */
63         if (tl->n_flows >= tl->max_flows) {
64                 return 0;
65         }
66         tl->n_flows++;
67
68         /* Insert the entry immediately in front of where we're pointing. */
69         flow->serial = tl->next_serial++;
70         list_add_tail_rcu(&flow->node, &f->node);
71         list_add_rcu(&flow->iter_node, &tl->iter_flows);
72         return 1;
73 }
74
75 static int table_linear_modify(struct sw_table *swt,
76                                 const struct sw_flow_key *key, uint16_t priority, int strict,
77                                 const struct ofp_action *actions, int n_actions)
78 {
79         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
80         struct sw_flow *flow;
81         unsigned int count = 0;
82
83         list_for_each_entry (flow, &tl->flows, node) {
84                 if (flow_matches_desc(&flow->key, key, strict)
85                                 && (!strict || (flow->priority == priority))) {
86                         flow_replace_acts(flow, actions, n_actions);
87                         count++;
88                 }
89         }
90         return count;
91 }
92
93 static int do_delete(struct sw_table *swt, struct sw_flow *flow) 
94 {
95         list_del_rcu(&flow->node);
96         list_del_rcu(&flow->iter_node);
97         flow_deferred_free(flow);
98         return 1;
99 }
100
101 static int table_linear_delete(struct sw_table *swt,
102                                 const struct sw_flow_key *key, uint16_t priority, int strict)
103 {
104         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
105         struct sw_flow *flow;
106         unsigned int count = 0;
107
108         list_for_each_entry (flow, &tl->flows, node) {
109                 if (flow_matches_desc(&flow->key, key, strict)
110                                 && (!strict || (flow->priority == priority)))
111                         count += do_delete(swt, flow);
112         }
113         tl->n_flows -= count;
114         return count;
115 }
116
117 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
118 {
119         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
120         struct sw_flow *flow;
121         int count = 0;
122
123         mutex_lock(&dp_mutex);
124         list_for_each_entry (flow, &tl->flows, node) {
125                 int reason = flow_timeout(flow);
126                 if (reason >= 0) {
127                         count += do_delete(swt, flow);
128                         dp_send_flow_expired(dp, flow, reason);
129                 }
130         }
131         tl->n_flows -= count;
132         mutex_unlock(&dp_mutex);
133         return count;
134 }
135
136 static void table_linear_destroy(struct sw_table *swt)
137 {
138         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
139
140         while (!list_empty(&tl->flows)) {
141                 struct sw_flow *flow = list_entry(tl->flows.next,
142                                                   struct sw_flow, node);
143                 list_del(&flow->node);
144                 flow_free(flow);
145         }
146         kfree(tl);
147 }
148
149 static int table_linear_iterate(struct sw_table *swt,
150                                 const struct sw_flow_key *key,
151                                 struct sw_table_position *position,
152                                 int (*callback)(struct sw_flow *, void *),
153                                 void *private)
154 {
155         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
156         struct sw_flow *flow;
157         unsigned long start;
158
159         start = position->private[0];
160         list_for_each_entry (flow, &tl->iter_flows, iter_node) {
161                 if (flow->serial >= start
162                     && flow_matches_2wild(key, &flow->key)) {
163                         int error = callback(flow, private);
164                         if (error) {
165                                 position->private[0] = flow->serial;
166                                 return error;
167                         }
168                 }
169         }
170         return 0;
171 }
172
173 static void table_linear_stats(struct sw_table *swt,
174                                 struct sw_table_stats *stats)
175 {
176         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
177         stats->name = "linear";
178         stats->wildcards = OFPFW_ALL;
179         stats->n_flows   = tl->n_flows;
180         stats->max_flows = tl->max_flows;
181         stats->n_lookup  = swt->n_lookup;
182         stats->n_matched = swt->n_matched;
183 }
184
185
186 struct sw_table *table_linear_create(unsigned int max_flows)
187 {
188         struct sw_table_linear *tl;
189         struct sw_table *swt;
190
191         tl = kzalloc(sizeof *tl, GFP_KERNEL);
192         if (tl == NULL)
193                 return NULL;
194
195         swt = &tl->swt;
196         swt->lookup = table_linear_lookup;
197         swt->insert = table_linear_insert;
198         swt->modify = table_linear_modify;
199         swt->delete = table_linear_delete;
200         swt->timeout = table_linear_timeout;
201         swt->destroy = table_linear_destroy;
202         swt->iterate = table_linear_iterate;
203         swt->stats = table_linear_stats;
204
205         tl->max_flows = max_flows;
206         tl->n_flows = 0;
207         INIT_LIST_HEAD(&tl->flows);
208         INIT_LIST_HEAD(&tl->iter_flows);
209         tl->next_serial = 0;
210
211         return swt;
212 }