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