Update 2007 copyrights to include 2008.
[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/list.h>
14
15 struct sw_table_linear {
16         struct sw_table swt;
17
18         spinlock_t lock;
19         unsigned int max_flows;
20         atomic_t n_flows;
21         struct list_head flows;
22 };
23
24 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
25                                          const struct sw_flow_key *key)
26 {
27         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
28         struct sw_flow *flow;
29         list_for_each_entry_rcu (flow, &tl->flows, u.node) {
30                 if (flow_matches(&flow->key, key))
31                         return flow;
32         }
33         return NULL;
34 }
35
36 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
37 {
38         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
39         unsigned long int flags;
40         struct sw_flow *f;
41
42         /* Replace flows that match exactly. */
43         spin_lock_irqsave(&tl->lock, flags);
44         list_for_each_entry_rcu (f, &tl->flows, u.node) {
45                 if (f->key.wildcards == flow->key.wildcards
46                                 && flow_matches(&f->key, &flow->key)
47                                 && flow_del(f)) {
48                         list_replace_rcu(&f->u.node, &flow->u.node);
49                         spin_unlock_irqrestore(&tl->lock, flags);
50                         flow_deferred_free(f);
51                         return 1;
52                 }
53         }
54
55         /* Table overflow? */
56         if (atomic_read(&tl->n_flows) >= tl->max_flows) {
57                 spin_unlock_irqrestore(&tl->lock, flags);
58                 return 0;
59         }
60         atomic_inc(&tl->n_flows);
61
62         /* FIXME: need to order rules from most to least specific. */
63         list_add_rcu(&flow->u.node, &tl->flows);
64         spin_unlock_irqrestore(&tl->lock, flags);
65         return 1;
66 }
67
68 static int do_delete(struct sw_table *swt, struct sw_flow *flow) 
69 {
70         if (flow_del(flow)) {
71                 list_del_rcu(&flow->u.node);
72                 flow_deferred_free(flow);
73                 return 1;
74         }
75         return 0;
76 }
77
78 static int table_linear_delete(struct sw_table *swt,
79                                 const struct sw_flow_key *key, int strict)
80 {
81         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
82         struct list_head *pos, *n;
83         unsigned int count = 0;
84
85         list_for_each_safe_rcu (pos, n, &tl->flows) {
86                 struct sw_flow *flow = list_entry(pos, struct sw_flow, u.node);
87                 if (flow_del_matches(&flow->key, key, strict))
88                         count += do_delete(swt, flow);
89         }
90         if (count)
91                 atomic_sub(count, &tl->n_flows);
92         return count;
93 }
94
95 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
96 {
97         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
98         struct list_head *pos, *n;
99         int count = 0;
100
101         list_for_each_safe_rcu (pos, n, &tl->flows) {
102                 struct sw_flow *flow = list_entry(pos, struct sw_flow, u.node);
103                 if (flow_timeout(flow)) {
104                         count += do_delete(swt, flow);
105                         if (dp->hello_flags & OFP_CHELLO_SEND_FLOW_EXP)
106                                 dp_send_flow_expired(dp, flow);
107                 }
108         }
109         if (count)
110                 atomic_sub(count, &tl->n_flows);
111         return count;
112 }
113
114 static void table_linear_destroy(struct sw_table *swt)
115 {
116         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
117
118         while (!list_empty(&tl->flows)) {
119                 struct sw_flow *flow = list_entry(tl->flows.next,
120                                                   struct sw_flow, u.node);
121                 list_del(&flow->u.node);
122                 flow_free(flow);
123         }
124         kfree(tl);
125 }
126
127 /* Linear table's private data is just a pointer to the table */
128
129 static int table_linear_iterator(struct sw_table *swt,
130                                  struct swt_iterator *swt_iter) 
131 {
132         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
133
134         swt_iter->private = tl;
135
136         if (atomic_read(&tl->n_flows) == 0)
137                 swt_iter->flow = NULL;
138         else
139                 swt_iter->flow = list_entry(tl->flows.next,
140                                 struct sw_flow, u.node);
141
142         return 1;
143 }
144
145 static void table_linear_next(struct swt_iterator *swt_iter)
146 {
147         struct sw_table_linear *tl;
148         struct list_head *next;
149
150         if (swt_iter->flow == NULL)
151                 return;
152
153         tl = (struct sw_table_linear *) swt_iter->private;
154
155         next = swt_iter->flow->u.node.next;
156         if (next == &tl->flows)
157                 swt_iter->flow = NULL;
158         else
159                 swt_iter->flow = list_entry(next, struct sw_flow, u.node);
160 }
161
162 static void table_linear_iterator_destroy(struct swt_iterator *swt_iter)
163 {}
164
165 static void table_linear_stats(struct sw_table *swt,
166                                 struct sw_table_stats *stats)
167 {
168         struct sw_table_linear *tl = (struct sw_table_linear *) swt;
169         stats->name = "linear";
170         stats->n_flows = atomic_read(&tl->n_flows);
171         stats->max_flows = tl->max_flows;
172 }
173
174
175 struct sw_table *table_linear_create(unsigned int max_flows)
176 {
177         struct sw_table_linear *tl;
178         struct sw_table *swt;
179
180         tl = kzalloc(sizeof *tl, GFP_KERNEL);
181         if (tl == NULL)
182                 return NULL;
183
184         swt = &tl->swt;
185         swt->lookup = table_linear_lookup;
186         swt->insert = table_linear_insert;
187         swt->delete = table_linear_delete;
188         swt->timeout = table_linear_timeout;
189         swt->destroy = table_linear_destroy;
190         swt->stats = table_linear_stats;
191
192                 swt->iterator = table_linear_iterator;
193         swt->iterator_next = table_linear_next;
194         swt->iterator_destroy = table_linear_iterator_destroy;
195
196         tl->max_flows = max_flows;
197         atomic_set(&tl->n_flows, 0);
198         INIT_LIST_HEAD(&tl->flows);
199         spin_lock_init(&tl->lock);
200
201         return swt;
202 }