classifier: New function cls_rule_equal().
[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 };
48
49 /* A flow classification rule.
50  *
51  * Use one of the cls_rule_*() functions to initialize a cls_rule.
52  *
53  * The cls_rule_*() functions below maintain the following important
54  * invariant that the classifier depends on:
55  *
56  *   - If a bit or a field is wildcarded in 'wc', then the corresponding bit or
57  *     field in 'flow' is set to all-0-bits.  (The
58  *     cls_rule_zero_wildcarded_fields() function can be used to restore this
59  *     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 void cls_rule_init(const struct flow *, const struct flow_wildcards *,
70                    unsigned int priority, struct cls_rule *);
71 void cls_rule_init_exact(const struct flow *, unsigned int priority,
72                          struct cls_rule *);
73 void cls_rule_from_match(const struct ofp_match *, unsigned int priority,
74                          int flow_format, uint64_t cookie, struct cls_rule *);
75 void cls_rule_init_catchall(struct cls_rule *, unsigned int priority);
76
77 void cls_rule_zero_wildcarded_fields(struct cls_rule *);
78
79 void cls_rule_set_in_port(struct cls_rule *, uint16_t odp_port);
80 void cls_rule_set_dl_type(struct cls_rule *, ovs_be16);
81 void cls_rule_set_dl_src(struct cls_rule *, const uint8_t[6]);
82 void cls_rule_set_dl_dst(struct cls_rule *, const uint8_t[6]);
83 bool cls_rule_set_dl_tci(struct cls_rule *, ovs_be16 tci);
84 bool cls_rule_set_dl_tci_masked(struct cls_rule *,
85                                 ovs_be16 tci, ovs_be16 mask);
86 void cls_rule_set_dl_vlan(struct cls_rule *, ovs_be16);
87 void cls_rule_set_dl_vlan_pcp(struct cls_rule *, uint8_t);
88 void cls_rule_set_tp_src(struct cls_rule *, ovs_be16);
89 void cls_rule_set_tp_dst(struct cls_rule *, ovs_be16);
90 void cls_rule_set_nw_proto(struct cls_rule *, uint8_t);
91 void cls_rule_set_nw_src(struct cls_rule *, ovs_be32);
92 bool cls_rule_set_nw_src_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
93 void cls_rule_set_nw_dst(struct cls_rule *, ovs_be32);
94 bool cls_rule_set_nw_dst_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
95 void cls_rule_set_nw_tos(struct cls_rule *, uint8_t);
96 void cls_rule_set_icmp_type(struct cls_rule *, uint8_t);
97 void cls_rule_set_icmp_code(struct cls_rule *, uint8_t);
98
99 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
100
101 char *cls_rule_to_string(const struct cls_rule *);
102 void cls_rule_print(const struct cls_rule *);
103
104 void classifier_init(struct classifier *);
105 void classifier_destroy(struct classifier *);
106 bool classifier_is_empty(const struct classifier *);
107 int classifier_count(const struct classifier *);
108 struct cls_rule *classifier_insert(struct classifier *, struct cls_rule *);
109 void classifier_remove(struct classifier *, struct cls_rule *);
110 struct cls_rule *classifier_lookup(const struct classifier *,
111                                    const struct flow *);
112 bool classifier_rule_overlaps(const struct classifier *,
113                               const struct cls_rule *);
114
115 typedef void cls_cb_func(struct cls_rule *, void *aux);
116
117 struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
118                                               const struct cls_rule *);
119 \f
120 /* Iteration. */
121
122 struct cls_cursor {
123     const struct classifier *cls;
124     const struct cls_table *table;
125     const struct cls_rule *target;
126 };
127
128 void cls_cursor_init(struct cls_cursor *, const struct classifier *,
129                      const struct cls_rule *match);
130 struct cls_rule *cls_cursor_first(struct cls_cursor *);
131 struct cls_rule *cls_cursor_next(struct cls_cursor *, struct cls_rule *);
132
133 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
134     for ((RULE) = OBJECT_CONTAINING(cls_cursor_first(CURSOR),           \
135                                     RULE, MEMBER);                      \
136          &(RULE)->MEMBER != NULL;                                       \
137          (RULE) = OBJECT_CONTAINING(cls_cursor_next(CURSOR,             \
138                                                     &(RULE)->MEMBER),   \
139                                     RULE, MEMBER))
140
141 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
142     for ((RULE) = OBJECT_CONTAINING(cls_cursor_first(CURSOR),           \
143                                     RULE, MEMBER);                      \
144          (&(RULE)->MEMBER != NULL                                       \
145           ? ((NEXT) = OBJECT_CONTAINING(cls_cursor_next(CURSOR,         \
146                                                         &(RULE)->MEMBER), \
147                                         RULE, MEMBER), 1)               \
148           : 0);                                                         \
149          (RULE) = (NEXT))
150
151 #endif /* classifier.h */