ofproto: Generalize tun_id_from_cookie into flow_format.
[sliver-openvswitch.git] / lib / classifier.h
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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 CLASSIFIER_H
18 #define CLASSIFIER_H 1
19
20 /* Flow classifier.
21  *
22  * A classifier is a "struct classifier",
23  *      a hash map from a set of wildcards to a "struct cls_table",
24  *              a hash map from fixed field values to "struct cls_rule",
25  *                      which can contain a list of otherwise identical rules
26  *                      with lower priorities.
27  */
28
29 #include "flow.h"
30 #include "hmap.h"
31 #include "list.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
34
35 /* A flow classifier. */
36 struct classifier {
37     int n_rules;                /* Total number of rules. */
38     struct hmap tables;         /* Contains "struct cls_table"s.  */
39 };
40
41 /* A set of rules that all have the same fields wildcarded. */
42 struct cls_table {
43     struct hmap_node hmap_node; /* Within struct classifier 'wctables'. */
44     struct hmap rules;          /* Contains "struct cls_rule"s. */
45     struct flow_wildcards wc;   /* Wildcards for fields. */
46     int n_table_rules;          /* Number of rules, including duplicates. */
47     int n_refs;                 /* Reference count used during iteration. */
48 };
49
50 /* A flow classification rule.
51  *
52  * Use one of the cls_rule_*() functions to initialize a cls_rule.
53  *
54  * The cls_rule_*() functions below maintain the following important
55  * invariant that the classifier depends on:
56  *
57  *   - If a bit or a field is wildcarded in 'wc', then the corresponding bit or
58  *     field in 'flow' is set to all-0-bits.  (The cls_rule_zero_wildcards()
59  *     function can be used to restore this invariant after adding wildcards.)
60  */
61 struct cls_rule {
62     struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
63     struct list list;           /* List of identical, lower-priority rules. */
64     struct flow flow;           /* All field values. */
65     struct flow_wildcards wc;   /* Wildcards for fields. */
66     unsigned int priority;      /* Larger numbers are higher priorities. */
67 };
68
69 enum {
70     CLS_INC_EXACT = 1 << 0,     /* Include exact-match flows? */
71     CLS_INC_WILD = 1 << 1,      /* Include flows with wildcards? */
72     CLS_INC_ALL = CLS_INC_EXACT | CLS_INC_WILD
73 };
74
75 void cls_rule_from_flow(const struct flow *, uint32_t wildcards,
76                         unsigned int priority, struct cls_rule *);
77 void cls_rule_from_match(const struct ofp_match *, unsigned int priority,
78                          int flow_format, uint64_t cookie, struct cls_rule *);
79
80 void cls_rule_zero_wildcards(struct cls_rule *);
81
82 char *cls_rule_to_string(const struct cls_rule *);
83 void cls_rule_print(const struct cls_rule *);
84
85 void classifier_init(struct classifier *);
86 void classifier_destroy(struct classifier *);
87 bool classifier_is_empty(const struct classifier *);
88 int classifier_count(const struct classifier *);
89 int classifier_count_exact(const struct classifier *);
90 struct cls_rule *classifier_insert(struct classifier *, struct cls_rule *);
91 void classifier_insert_exact(struct classifier *, struct cls_rule *);
92 void classifier_remove(struct classifier *, struct cls_rule *);
93 struct cls_rule *classifier_lookup(const struct classifier *,
94                                    const struct flow *, int include);
95 bool classifier_rule_overlaps(const struct classifier *,
96                               const struct cls_rule *);
97
98 typedef void cls_cb_func(struct cls_rule *, void *aux);
99
100 void classifier_for_each(const struct classifier *, int include,
101                          cls_cb_func *, void *aux);
102 void classifier_for_each_match(const struct classifier *,
103                                const struct cls_rule *,
104                                int include, cls_cb_func *, void *aux);
105 struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
106                                               const struct cls_rule *);
107
108 /* Iteration shorthands. */
109
110 struct cls_table *classifier_exact_table(const struct classifier *);
111 struct cls_rule *cls_table_first_rule(const struct cls_table *);
112 struct cls_rule *cls_table_next_rule(const struct cls_table *,
113                                      const struct cls_rule *);
114
115 #define CLS_TABLE_FOR_EACH_RULE(RULE, MEMBER, TABLE)                    \
116     for ((RULE) = OBJECT_CONTAINING(cls_table_first_rule(TABLE),        \
117                                     RULE, MEMBER);                      \
118          &(RULE)->MEMBER != NULL;                                       \
119          (RULE) = OBJECT_CONTAINING(cls_table_next_rule(TABLE,          \
120                                                         &(RULE)->MEMBER), \
121                                     RULE, MEMBER))
122
123 #define CLASSIFIER_FOR_EACH_EXACT_RULE(RULE, MEMBER, CLS)               \
124     CLS_TABLE_FOR_EACH_RULE (RULE, MEMBER, classifier_exact_table(CLS))
125
126 #endif /* classifier.h */