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 {
18 unsigned int max_flows;
20 struct list_head flows;
21 struct list_head iter_flows;
22 unsigned long int next_serial;
25 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
26 const struct sw_flow_key *key)
28 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
30 list_for_each_entry_rcu (flow, &tl->flows, node) {
31 if (flow_matches(&flow->key, key))
37 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
39 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
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.
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)
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);
59 if (f->priority < flow->priority)
63 /* Make sure there's room in the table. */
64 if (tl->n_flows >= tl->max_flows) {
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);
76 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
79 list_del_rcu(&flow->node);
80 list_del_rcu(&flow->iter_node);
81 flow_deferred_free(flow);
87 static int table_linear_delete(struct sw_table *swt,
88 const struct sw_flow_key *key, uint16_t priority, int strict)
90 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
92 unsigned int count = 0;
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);
103 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
105 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
106 struct sw_flow *flow;
109 mutex_lock(&dp_mutex);
110 list_for_each_entry (flow, &tl->flows, node) {
111 if (flow_timeout(flow)) {
112 count += do_delete(swt, flow);
113 if (dp->flags & OFPC_SEND_FLOW_EXP)
114 dp_send_flow_expired(dp, flow);
117 tl->n_flows -= count;
118 mutex_unlock(&dp_mutex);
122 static void table_linear_destroy(struct sw_table *swt)
124 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
126 while (!list_empty(&tl->flows)) {
127 struct sw_flow *flow = list_entry(tl->flows.next,
128 struct sw_flow, node);
129 list_del(&flow->node);
135 static int table_linear_iterate(struct sw_table *swt,
136 const struct sw_flow_key *key,
137 struct sw_table_position *position,
138 int (*callback)(struct sw_flow *, void *),
141 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
142 struct sw_flow *flow;
145 start = position->private[0];
146 list_for_each_entry (flow, &tl->iter_flows, iter_node) {
147 if (flow->serial >= start && flow_matches(key, &flow->key)) {
148 int error = callback(flow, private);
150 position->private[0] = flow->serial;
158 static void table_linear_stats(struct sw_table *swt,
159 struct sw_table_stats *stats)
161 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
162 stats->name = "linear";
163 stats->n_flows = tl->n_flows;
164 stats->max_flows = tl->max_flows;
168 struct sw_table *table_linear_create(unsigned int max_flows)
170 struct sw_table_linear *tl;
171 struct sw_table *swt;
173 tl = kzalloc(sizeof *tl, GFP_KERNEL);
178 swt->lookup = table_linear_lookup;
179 swt->insert = table_linear_insert;
180 swt->delete = table_linear_delete;
181 swt->timeout = table_linear_timeout;
182 swt->destroy = table_linear_destroy;
183 swt->iterate = table_linear_iterate;
184 swt->stats = table_linear_stats;
186 tl->max_flows = max_flows;
188 INIT_LIST_HEAD(&tl->flows);
189 INIT_LIST_HEAD(&tl->iter_flows);