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/rculist.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_1wild(key, &flow->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_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;
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);
63 if (f->priority < flow->priority)
67 /* Make sure there's room in the table. */
68 if (tl->n_flows >= tl->max_flows) {
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);
80 static int table_linear_modify(struct sw_table *swt,
81 const struct sw_flow_key *key,
82 const struct ofp_action *actions, int n_actions)
84 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
86 unsigned int count = 0;
88 list_for_each_entry (flow, &tl->flows, node) {
89 if (flow_matches_1wild(&flow->key, key)) {
90 flow_replace_acts(flow, actions, n_actions);
97 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
99 list_del_rcu(&flow->node);
100 list_del_rcu(&flow->iter_node);
101 flow_deferred_free(flow);
105 static int table_linear_delete(struct sw_table *swt,
106 const struct sw_flow_key *key, uint16_t priority, int strict)
108 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
109 struct sw_flow *flow;
110 unsigned int count = 0;
112 list_for_each_entry (flow, &tl->flows, node) {
113 if (flow_del_matches(&flow->key, key, strict)
114 && (!strict || (flow->priority == priority)))
115 count += do_delete(swt, flow);
117 tl->n_flows -= count;
121 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
123 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
124 struct sw_flow *flow;
127 mutex_lock(&dp_mutex);
128 list_for_each_entry (flow, &tl->flows, node) {
129 int reason = flow_timeout(flow);
131 count += do_delete(swt, flow);
132 dp_send_flow_expired(dp, flow, reason);
135 tl->n_flows -= count;
136 mutex_unlock(&dp_mutex);
140 static void table_linear_destroy(struct sw_table *swt)
142 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
144 while (!list_empty(&tl->flows)) {
145 struct sw_flow *flow = list_entry(tl->flows.next,
146 struct sw_flow, node);
147 list_del(&flow->node);
153 static int table_linear_iterate(struct sw_table *swt,
154 const struct sw_flow_key *key,
155 struct sw_table_position *position,
156 int (*callback)(struct sw_flow *, void *),
159 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
160 struct sw_flow *flow;
163 start = position->private[0];
164 list_for_each_entry (flow, &tl->iter_flows, iter_node) {
165 if (flow->serial >= start
166 && flow_matches_2wild(key, &flow->key)) {
167 int error = callback(flow, private);
169 position->private[0] = flow->serial;
177 static void table_linear_stats(struct sw_table *swt,
178 struct sw_table_stats *stats)
180 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
181 stats->name = "linear";
182 stats->wildcards = OFPFW_ALL;
183 stats->n_flows = tl->n_flows;
184 stats->max_flows = tl->max_flows;
185 stats->n_matched = swt->n_matched;
189 struct sw_table *table_linear_create(unsigned int max_flows)
191 struct sw_table_linear *tl;
192 struct sw_table *swt;
194 tl = kzalloc(sizeof *tl, GFP_KERNEL);
199 swt->lookup = table_linear_lookup;
200 swt->insert = table_linear_insert;
201 swt->modify = table_linear_modify;
202 swt->delete = table_linear_delete;
203 swt->timeout = table_linear_timeout;
204 swt->destroy = table_linear_destroy;
205 swt->iterate = table_linear_iterate;
206 swt->stats = table_linear_stats;
208 tl->max_flows = max_flows;
210 INIT_LIST_HEAD(&tl->flows);
211 INIT_LIST_HEAD(&tl->iter_flows);