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
11 #include <linux/rcupdate.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
15 struct sw_table_linear {
19 unsigned int max_flows;
21 struct list_head flows;
22 struct list_head iter_flows;
23 unsigned long int next_serial;
26 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
27 const struct sw_flow_key *key)
29 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
31 list_for_each_entry_rcu (flow, &tl->flows, node) {
32 if (flow_matches(&flow->key, key))
38 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
40 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
41 unsigned long int flags;
45 /* Loop through the existing list of entries. New entries will
46 * always be placed behind those with equal priority. Just replace
47 * any flows that match exactly.
49 spin_lock_irqsave(&tl->lock, flags);
50 list_for_each_entry_rcu (f, &tl->flows, node) {
51 if (f->priority == flow->priority
52 && f->key.wildcards == flow->key.wildcards
53 && flow_matches(&f->key, &flow->key)
55 flow->serial = f->serial;
56 list_replace_rcu(&f->node, &flow->node);
57 list_replace_rcu(&f->iter_node, &flow->iter_node);
58 spin_unlock_irqrestore(&tl->lock, flags);
59 flow_deferred_free(f);
63 if (f->priority < flow->priority)
67 /* Make sure there's room in the table. */
68 if (atomic_read(&tl->n_flows) >= tl->max_flows) {
69 spin_unlock_irqrestore(&tl->lock, flags);
72 atomic_inc(&tl->n_flows);
74 /* Insert the entry immediately in front of where we're pointing. */
75 flow->serial = tl->next_serial++;
76 list_add_tail_rcu(&flow->node, &f->node);
77 list_add_rcu(&flow->iter_node, &tl->iter_flows);
78 spin_unlock_irqrestore(&tl->lock, flags);
82 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
85 list_del_rcu(&flow->node);
86 list_del_rcu(&flow->iter_node);
87 flow_deferred_free(flow);
93 static int table_linear_delete(struct sw_table *swt,
94 const struct sw_flow_key *key, uint16_t priority, int strict)
96 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
98 unsigned int count = 0;
100 list_for_each_entry_rcu (flow, &tl->flows, node) {
101 if (flow_del_matches(&flow->key, key, strict)
102 && (!strict || (flow->priority == priority)))
103 count += do_delete(swt, flow);
106 atomic_sub(count, &tl->n_flows);
110 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
112 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
113 struct sw_flow *flow;
116 mutex_lock(&dp_mutex);
117 list_for_each_entry_rcu (flow, &tl->flows, node) {
118 if (flow_timeout(flow)) {
119 count += do_delete(swt, flow);
120 if (dp->flags & OFPC_SEND_FLOW_EXP)
121 dp_send_flow_expired(dp, flow);
124 mutex_unlock(&dp_mutex);
127 atomic_sub(count, &tl->n_flows);
131 static void table_linear_destroy(struct sw_table *swt)
133 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
135 while (!list_empty(&tl->flows)) {
136 struct sw_flow *flow = list_entry(tl->flows.next,
137 struct sw_flow, node);
138 list_del(&flow->node);
144 static int table_linear_iterate(struct sw_table *swt,
145 const struct sw_flow_key *key,
146 struct sw_table_position *position,
147 int (*callback)(struct sw_flow *, void *),
150 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
151 struct sw_flow *flow;
154 start = position->private[0];
155 list_for_each_entry_rcu (flow, &tl->iter_flows, iter_node) {
156 if (flow->serial >= start && flow_matches(key, &flow->key)) {
157 int error = callback(flow, private);
159 position->private[0] = flow->serial;
167 static void table_linear_stats(struct sw_table *swt,
168 struct sw_table_stats *stats)
170 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
171 stats->name = "linear";
172 stats->n_flows = atomic_read(&tl->n_flows);
173 stats->max_flows = tl->max_flows;
177 struct sw_table *table_linear_create(unsigned int max_flows)
179 struct sw_table_linear *tl;
180 struct sw_table *swt;
182 tl = kzalloc(sizeof *tl, GFP_KERNEL);
187 swt->lookup = table_linear_lookup;
188 swt->insert = table_linear_insert;
189 swt->delete = table_linear_delete;
190 swt->timeout = table_linear_timeout;
191 swt->destroy = table_linear_destroy;
192 swt->iterate = table_linear_iterate;
193 swt->stats = table_linear_stats;
195 tl->max_flows = max_flows;
196 atomic_set(&tl->n_flows, 0);
197 INIT_LIST_HEAD(&tl->flows);
198 INIT_LIST_HEAD(&tl->iter_flows);
199 spin_lock_init(&tl->lock);