Get rid of unnecessary synchronization in tables.
[openvswitch] / 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/list.h>
14
15 struct sw_table_linear {
16         struct sw_table swt;
17
18         unsigned int max_flows;
19         atomic_t 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(&flow->key, 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(&f->key, &flow->key)
51                                 && flow_del(f)) {
52                         flow->serial = f->serial;
53                         list_replace_rcu(&f->node, &flow->node);
54                         list_replace_rcu(&f->iter_node, &flow->iter_node);
55                         flow_deferred_free(f);
56                         return 1;
57                 }
58
59                 if (f->priority < flow->priority)
60                         break;
61         }
62
63         /* Make sure there's room in the table. */
64         if (atomic_read(&tl->n_flows) >= tl->max_flows) {
65                 return 0;
66         }
67         atomic_inc(&tl->n_flows);
68
69         /* Insert the entry immediately in front of where we're pointing. */
70         flow->serial = tl->next_serial++;
71         list_add_tail_rcu(&flow->node, &f->node);
72         list_add_rcu(&flow->iter_node, &tl->iter_flows);
73         return 1;
74 }
75
76 static int do_delete(struct sw_table *swt, struct sw_flow *flow) 
77 {
78         if (flow_del(flow)) {
79                 list_del_rcu(&flow->node);
80                 list_del_rcu(&flow->iter_node);
81                 flow_deferred_free(flow);
82                 return 1;
83         }
84         return 0;
85 }
86
87 static int table_linear_delete(struct sw_table *swt,
88                                 const struct sw_flow_key *key, uint16_t priority, int strict)
89 {
90         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
91         struct sw_flow *flow;
92         unsigned int count = 0;
93
94         list_for_each_entry (flow, &tl->flows, node) {
95                 if (flow_del_matches(&flow->key, key, strict)
96                                 && (!strict || (flow->priority == priority)))
97                         count += do_delete(swt, flow);
98         }
99         if (count)
100                 atomic_sub(count, &tl->n_flows);
101         return count;
102 }
103
104 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
105 {
106         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
107         struct sw_flow *flow;
108         int count = 0;
109
110         mutex_lock(&dp_mutex);
111         list_for_each_entry (flow, &tl->flows, node) {
112                 if (flow_timeout(flow)) {
113                         count += do_delete(swt, flow);
114                         if (dp->flags & OFPC_SEND_FLOW_EXP)
115                                 dp_send_flow_expired(dp, flow);
116                 }
117         }
118         mutex_unlock(&dp_mutex);
119
120         if (count)
121                 atomic_sub(count, &tl->n_flows);
122         return count;
123 }
124
125 static void table_linear_destroy(struct sw_table *swt)
126 {
127         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
128
129         while (!list_empty(&tl->flows)) {
130                 struct sw_flow *flow = list_entry(tl->flows.next,
131                                                   struct sw_flow, node);
132                 list_del(&flow->node);
133                 flow_free(flow);
134         }
135         kfree(tl);
136 }
137
138 static int table_linear_iterate(struct sw_table *swt,
139                                 const struct sw_flow_key *key,
140                                 struct sw_table_position *position,
141                                 int (*callback)(struct sw_flow *, void *),
142                                 void *private)
143 {
144         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
145         struct sw_flow *flow;
146         unsigned long start;
147
148         start = position->private[0];
149         list_for_each_entry (flow, &tl->iter_flows, iter_node) {
150                 if (flow->serial >= start && flow_matches(key, &flow->key)) {
151                         int error = callback(flow, private);
152                         if (error) {
153                                 position->private[0] = flow->serial;
154                                 return error;
155                         }
156                 }
157         }
158         return 0;
159 }
160
161 static void table_linear_stats(struct sw_table *swt,
162                                 struct sw_table_stats *stats)
163 {
164         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
165         stats->name = "linear";
166         stats->n_flows = atomic_read(&tl->n_flows);
167         stats->max_flows = tl->max_flows;
168 }
169
170
171 struct sw_table *table_linear_create(unsigned int max_flows)
172 {
173         struct sw_table_linear *tl;
174         struct sw_table *swt;
175
176         tl = kzalloc(sizeof *tl, GFP_KERNEL);
177         if (tl == NULL)
178                 return NULL;
179
180         swt = &tl->swt;
181         swt->lookup = table_linear_lookup;
182         swt->insert = table_linear_insert;
183         swt->delete = table_linear_delete;
184         swt->timeout = table_linear_timeout;
185         swt->destroy = table_linear_destroy;
186         swt->iterate = table_linear_iterate;
187         swt->stats = table_linear_stats;
188
189         tl->max_flows = max_flows;
190         atomic_set(&tl->n_flows, 0);
191         INIT_LIST_HEAD(&tl->flows);
192         INIT_LIST_HEAD(&tl->iter_flows);
193         tl->next_serial = 0;
194
195         return swt;
196 }