Remove useless use of <assert.h>.
[sliver-openvswitch.git] / lib / tag.h
1 /*
2  * Copyright (c) 2008, 2011, 2012 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 #ifndef TAG_H
18 #define TAG_H 1
19
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include "util.h"
23
24 /*
25  * Tagging support.
26  *
27  * A 'tag' represents an arbitrary category.  Currently, tags are used to
28  * represent categories of flows and in particular the dependencies for a flow
29  * switching decision.  For example, if a flow's output port is based on
30  * knowledge that source MAC 00:02:e3:0f:80:a4 is on eth0, then a tag that
31  * represents that dependency is attached to that flow in the flowtracking hash
32  * table.
33  *
34  * As this example shows, the universe of possible categories is very large,
35  * and even the number of categories that are in use at a given time can be
36  * very large.  This means that keeping track of category membership via
37  * conventional means (lists, bitmaps, etc.) is likely to be expensive.
38  *
39  * Tags are actually implemented via a "superimposed coding", as discussed in
40  * Knuth TAOCP v.3 section 6.5 "Retrieval on Secondary Keys".  A tag is an
41  * unsigned integer in which exactly 2 bits are set to 1 and the rest set to 0.
42  * For 32-bit integers (as currently used) there are 32 * 31 / 2 = 496 unique
43  * tags; for 64-bit integers there are 64 * 63 / 2 = 2,016.
44  *
45  * Because there is a small finite number of unique tags, tags must collide
46  * after some number of them have been created.  In practice we generally
47  * create tags by choosing bits randomly.
48  *
49  * The key property of tags is that we can combine them without increasing the
50  * amount of data required using bitwise-OR, since the result has the 1-bits
51  * from both tags set.  The necessary tradeoff is that the result is even more
52  * ambiguous: if combining two tags yields a value with 4 bits set to 1, then
53  * the result value will test as having 4 * 3 / 2 = 6 unique tags, not just the
54  * two tags that we combined.
55  *
56  * The upshot is this: a value that is the bitwise-OR combination of a number
57  * of tags will always include the tags that were combined, but it may contain
58  * any number of additional tags as well.  This is acceptable for flowtracking,
59  * since we want to be sure that we catch every flow that needs to be
60  * revalidated, but it is OK if we revalidate a few extra flows as well.
61  *
62  * If we combine too many tags, then the result will have every bit set, so
63  * that it will test as including every tag.  Fortunately, this is not a big
64  * problem for us: although there are many flows overall, each individual flow
65  * belongs only to a small number of categories.
66  */
67
68 /* Represents a tag, or the combination of 0 or more tags. */
69 typedef uint32_t tag_type;
70
71 tag_type tag_create_random(void);
72 tag_type tag_create_deterministic(uint32_t seed);
73 static inline bool tag_intersects(tag_type, tag_type);
74 static inline bool tag_is_valid(tag_type);
75
76 /* Returns true if 'a' and 'b' have at least one tag in common,
77  * false if their set of tags is disjoint. */
78 static inline bool
79 tag_intersects(tag_type a, tag_type b)
80 {
81     tag_type x = a & b;
82     return (x & (x - 1)) != 0;
83 }
84
85 /* Returns true if 'tag' is a valid tag, that is, if exactly two bits are set
86  * to 1 and the rest to 0.   Otherwise, returns false. */
87 static inline bool
88 tag_is_valid(tag_type tag)
89 {
90     tag_type x = tag & (tag - 1);
91     tag_type y = x & (x - 1);
92     return x && !y;
93 }
94 \f
95 /*
96  * A tag set accumulates tags with reduced ambiguity compared to a single tag.
97  * The flow tracking uses tag sets to keep track of tags that need to
98  * revalidated after a number of packets have been processed.
99  */
100 #define TAG_SET_SIZE 4
101 struct tag_set {
102     tag_type total;
103     tag_type tags[TAG_SET_SIZE];
104     unsigned int n;
105 };
106
107 void tag_set_init(struct tag_set *);
108 void tag_set_add(struct tag_set *, tag_type);
109 void tag_set_union(struct tag_set *, const struct tag_set *);
110 static inline bool tag_set_is_empty(const struct tag_set *);
111 static inline bool tag_set_intersects(const struct tag_set *, tag_type);
112
113 /* Returns true if 'set' will match no tags at all,
114  * false if it will match at least one tag. */
115 static inline bool
116 tag_set_is_empty(const struct tag_set *set)
117 {
118     return !set->n;
119 }
120
121 /* Returns true if any of the tags in 'tags' are also in 'set',
122  * false if the intersection is empty. */
123 static inline bool
124 tag_set_intersects(const struct tag_set *set, tag_type tags)
125 {
126     BUILD_ASSERT_DECL(TAG_SET_SIZE == 4);
127     return (tag_intersects(set->total, tags)
128             && (tag_intersects(set->tags[0], tags)
129                 || tag_intersects(set->tags[1], tags)
130                 || tag_intersects(set->tags[2], tags)
131                 || tag_intersects(set->tags[3], tags)));
132 }
133
134 #endif /* tag.h */