userspace: Define and use struct eth_addr.
[cascardo/ovs.git] / ofproto / ofproto-dpif-rid.c
1 /*
2  * Copyright (c) 2014, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "ofpbuf.h"
20 #include "ofproto-dpif.h"
21 #include "ofproto-dpif-rid.h"
22 #include "ofproto-provider.h"
23 #include "openvswitch/vlog.h"
24
25 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_rid);
26
27 static struct ovs_mutex mutex;
28
29 static struct cmap id_map;
30 static struct cmap metadata_map;
31
32 static struct ovs_list expiring OVS_GUARDED_BY(mutex);
33 static struct ovs_list expired OVS_GUARDED_BY(mutex);
34
35 static uint32_t next_id OVS_GUARDED_BY(mutex); /* Possible next free id. */
36
37 #define RECIRC_POOL_STATIC_IDS 1024
38
39 void
40 recirc_init(void)
41 {
42     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
43
44     if (ovsthread_once_start(&once)) {
45         ovs_mutex_init(&mutex);
46         ovs_mutex_lock(&mutex);
47         next_id = 1; /* 0 is not a valid ID. */
48         cmap_init(&id_map);
49         cmap_init(&metadata_map);
50         list_init(&expiring);
51         list_init(&expired);
52         ovs_mutex_unlock(&mutex);
53
54         ovsthread_once_done(&once);
55     }
56
57 }
58
59 /* This should be called by the revalidator once at each round (every 500ms or
60  * more). */
61 void
62 recirc_run(void)
63 {
64     static long long int last = 0;
65     long long int now = time_msec();
66
67     /* Do maintenance at most 4 times / sec. */
68     ovs_mutex_lock(&mutex);
69     if (now - last > 250) {
70         struct recirc_id_node *node;
71
72         last = now;
73
74         /* Nodes in 'expiring' and 'expired' lists have the refcount of zero,
75          * which means that while they can still be found (by id), no new
76          * references can be taken on them.  We have removed the entry from the
77          * 'metadata_map', at the time when refcount reached zero, causing any
78          * new translations to allocate a new ID.  This allows the expiring
79          * entry to be safely deleted while any sudden new use of the similar
80          * recirculation will safely start using a new recirculation ID.  When
81          * the refcount gets to zero, the node is also added to the 'expiring'
82          * list.  At any time after that the nodes in the 'expiring' list can
83          * be moved to the 'expired' list, from which they are deleted at least
84          * 250ms afterwards. */
85
86         /* Delete the expired.  These have been lingering for at least 250 ms,
87          * which should be enough for any ongoing recirculations to be
88          * finished. */
89         LIST_FOR_EACH_POP (node, exp_node, &expired) {
90             cmap_remove(&id_map, &node->id_node, node->id);
91             ovsrcu_postpone(free, node);
92         }
93
94         if (!list_is_empty(&expiring)) {
95             /* 'expired' is now empty, move nodes in 'expiring' to it. */
96             list_splice(&expired, list_front(&expiring), &expiring);
97         }
98     }
99     ovs_mutex_unlock(&mutex);
100 }
101
102 /* We use the id as the hash value, which works due to cmap internal rehashing.
103  * We also only insert nodes with unique IDs, so all possible hash collisions
104  * remain internal to the cmap. */
105 static struct recirc_id_node *
106 recirc_find__(uint32_t id)
107     OVS_REQUIRES(mutex)
108 {
109     struct cmap_node *node = cmap_find_protected(&id_map, id);
110
111     return node ? CONTAINER_OF(node, struct recirc_id_node, id_node) : NULL;
112 }
113
114 /* Lockless RCU protected lookup.  If node is needed accross RCU quiescent
115  * state, caller should copy the contents. */
116 const struct recirc_id_node *
117 recirc_id_node_find(uint32_t id)
118 {
119     const struct cmap_node *node = cmap_find(&id_map, id);
120
121     return node
122         ? CONTAINER_OF(node, const struct recirc_id_node, id_node)
123         : NULL;
124 }
125
126 static uint32_t
127 recirc_metadata_hash(const struct recirc_state *state)
128 {
129     uint32_t hash;
130
131     hash = hash_pointer(state->ofproto, 0);
132     hash = hash_int(state->table_id, hash);
133     if (state->metadata.tunnel->ip_dst) {
134         /* We may leave remainder bytes unhashed, but that is unlikely as
135          * the tunnel is not in the datapath format. */
136         hash = hash_words64((const uint64_t *) state->metadata.tunnel,
137                             flow_tnl_size(state->metadata.tunnel)
138                             / sizeof(uint64_t), hash);
139     }
140     hash = hash_words64((const uint64_t *) &state->metadata.metadata,
141                         (sizeof state->metadata - sizeof state->metadata.tunnel)
142                         / sizeof(uint64_t),
143                         hash);
144     if (state->stack && state->stack->size != 0) {
145         hash = hash_words64((const uint64_t *) state->stack->data,
146                             state->stack->size / sizeof(uint64_t), hash);
147     }
148     hash = hash_int(state->mirrors, hash);
149     hash = hash_int(state->action_set_len, hash);
150     if (state->ofpacts_len) {
151         hash = hash_words64(ALIGNED_CAST(const uint64_t *, state->ofpacts),
152                             state->ofpacts_len / sizeof(uint64_t),
153                             hash);
154     }
155     return hash;
156 }
157
158 static bool
159 recirc_metadata_equal(const struct recirc_state *a,
160                       const struct recirc_state *b)
161 {
162     return (a->table_id == b->table_id
163             && a->ofproto == b->ofproto
164             && flow_tnl_equal(a->metadata.tunnel, b->metadata.tunnel)
165             && !memcmp(&a->metadata.metadata, &b->metadata.metadata,
166                        sizeof a->metadata - sizeof a->metadata.tunnel)
167             && (((!a->stack || !a->stack->size) &&
168                  (!b->stack || !b->stack->size))
169                 || (a->stack && b->stack && ofpbuf_equal(a->stack, b->stack)))
170             && a->mirrors == b->mirrors
171             && a->action_set_len == b->action_set_len
172             && ofpacts_equal(a->ofpacts, a->ofpacts_len,
173                              b->ofpacts, b->ofpacts_len));
174 }
175
176 /* Lockless RCU protected lookup.  If node is needed accross RCU quiescent
177  * state, caller should take a reference. */
178 static struct recirc_id_node *
179 recirc_find_equal(const struct recirc_state *target, uint32_t hash)
180 {
181     struct recirc_id_node *node;
182
183     CMAP_FOR_EACH_WITH_HASH (node, metadata_node, hash, &metadata_map) {
184         if (recirc_metadata_equal(&node->state, target)) {
185             return node;
186         }
187     }
188     return NULL;
189 }
190
191 static struct recirc_id_node *
192 recirc_ref_equal(const struct recirc_state *target, uint32_t hash)
193 {
194     struct recirc_id_node *node;
195
196     do {
197         node = recirc_find_equal(target, hash);
198
199         /* Try again if the node was released before we get the reference. */
200     } while (node && !ovs_refcount_try_ref_rcu(&node->refcount));
201
202     return node;
203 }
204
205 static void
206 recirc_state_clone(struct recirc_state *new, const struct recirc_state *old,
207                    struct flow_tnl *tunnel)
208 {
209     *new = *old;
210     flow_tnl_copy__(tunnel, old->metadata.tunnel);
211     new->metadata.tunnel = tunnel;
212
213     if (new->stack) {
214         new->stack = new->stack->size ? ofpbuf_clone(new->stack) : NULL;
215     }
216     if (new->ofpacts) {
217         new->ofpacts = (new->ofpacts_len
218                         ? xmemdup(new->ofpacts, new->ofpacts_len)
219                         : NULL);
220     }
221 }
222
223 /* Allocate a unique recirculation id for the given set of flow metadata.
224  * The ID space is 2^^32, so there should never be a situation in which all
225  * the IDs are used up.  We loop until we find a free one.
226  * hash is recomputed if it is passed in as 0. */
227 static struct recirc_id_node *
228 recirc_alloc_id__(const struct recirc_state *state, uint32_t hash)
229 {
230     ovs_assert(state->action_set_len <= state->ofpacts_len);
231
232     struct recirc_id_node *node = xzalloc(sizeof *node);
233
234     node->hash = hash;
235     ovs_refcount_init(&node->refcount);
236     recirc_state_clone(CONST_CAST(struct recirc_state *, &node->state), state,
237                        &node->state_metadata_tunnel);
238
239     ovs_mutex_lock(&mutex);
240     for (;;) {
241         /* Claim the next ID.  The ID space should be sparse enough for the
242            allocation to succeed at the first try.  We do skip the first
243            RECIRC_POOL_STATIC_IDS IDs on the later rounds, though, as some of
244            the initial allocations may be for long term uses (like bonds). */
245         node->id = next_id++;
246         if (OVS_UNLIKELY(!node->id)) {
247             next_id = RECIRC_POOL_STATIC_IDS + 1;
248             node->id = next_id++;
249         }
250         /* Find if the id is free. */
251         if (OVS_LIKELY(!recirc_find__(node->id))) {
252             break;
253         }
254     }
255     cmap_insert(&id_map, &node->id_node, node->id);
256     cmap_insert(&metadata_map, &node->metadata_node, node->hash);
257     ovs_mutex_unlock(&mutex);
258     return node;
259 }
260
261 /* Look up an existing ID for the given flow's metadata and optional actions.
262  */
263 uint32_t
264 recirc_find_id(const struct recirc_state *target)
265 {
266     uint32_t hash = recirc_metadata_hash(target);
267     struct recirc_id_node *node = recirc_find_equal(target, hash);
268     return node ? node->id : 0;
269 }
270
271 /* Allocate a unique recirculation id for the given set of flow metadata and
272    optional actions. */
273 uint32_t
274 recirc_alloc_id_ctx(const struct recirc_state *state)
275 {
276     uint32_t hash = recirc_metadata_hash(state);
277     struct recirc_id_node *node = recirc_ref_equal(state, hash);
278     if (!node) {
279         node = recirc_alloc_id__(state, hash);
280     }
281     return node->id;
282 }
283
284 /* Allocate a unique recirculation id. */
285 uint32_t
286 recirc_alloc_id(struct ofproto_dpif *ofproto)
287 {
288     struct flow_tnl tunnel;
289     tunnel.ip_dst = htonl(0);
290     struct recirc_state state = {
291         .table_id = TBL_INTERNAL,
292         .ofproto = ofproto,
293         .metadata = { .tunnel = &tunnel, .in_port = OFPP_NONE },
294     };
295     return recirc_alloc_id__(&state, recirc_metadata_hash(&state))->id;
296 }
297
298 void
299 recirc_id_node_unref(const struct recirc_id_node *node_)
300     OVS_EXCLUDED(mutex)
301 {
302     struct recirc_id_node *node = CONST_CAST(struct recirc_id_node *, node_);
303
304     if (node && ovs_refcount_unref(&node->refcount) == 1) {
305         ovs_mutex_lock(&mutex);
306         /* Prevent re-use of this node by removing the node from 'metadata_map'
307          */
308         cmap_remove(&metadata_map, &node->metadata_node, node->hash);
309         /* We keep the node in the 'id_map' so that it can be found as long
310          * as it lingers, and add it to the 'expiring' list. */
311         list_insert(&expiring, &node->exp_node);
312         ovs_mutex_unlock(&mutex);
313     }
314 }
315
316 void
317 recirc_free_id(uint32_t id)
318 {
319     const struct recirc_id_node *node;
320
321     node = recirc_id_node_find(id);
322     if (node) {
323         recirc_id_node_unref(node);
324     } else {
325         VLOG_ERR("Freeing nonexistent recirculation ID: %"PRIu32, id);
326     }
327 }
328
329 /* Called when 'ofproto' is destructed.  Checks for and clears any
330  * recirc_id leak.
331  * No other thread may have access to the 'ofproto' being destructed.
332  * All related datapath flows must be deleted before calling this. */
333 void
334 recirc_free_ofproto(struct ofproto_dpif *ofproto, const char *ofproto_name)
335 {
336     struct recirc_id_node *n;
337
338     CMAP_FOR_EACH (n, metadata_node, &metadata_map) {
339         if (n->state.ofproto == ofproto) {
340             VLOG_ERR("recirc_id %"PRIu32
341                      " left allocated when ofproto (%s)"
342                      " is destructed", n->id, ofproto_name);
343         }
344     }
345 }