Add OXM_OF_METADATA field as a step toward OpenFlow 1.1 support.
[sliver-openvswitch.git] / lib / classifier.h
1 /*
2  * Copyright (c) 2009, 2010, 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 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 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* A flow classifier. */
40 struct classifier {
41     int n_rules;                /* Total number of rules. */
42     struct hmap tables;         /* Contains "struct cls_table"s.  */
43 };
44
45 /* A set of rules that all have the same fields wildcarded. */
46 struct cls_table {
47     struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
48     struct hmap rules;          /* Contains "struct cls_rule"s. */
49     struct flow_wildcards wc;   /* Wildcards for fields. */
50     int n_table_rules;          /* Number of rules, including duplicates. */
51     bool is_catchall;           /* True if this table wildcards every field. */
52 };
53
54 /* Returns true if 'table' is a "catch-all" table that will match every
55  * packet (if there is no higher-priority match). */
56 static inline bool
57 cls_table_is_catchall(const struct cls_table *table)
58 {
59     return table->is_catchall;
60 }
61
62 /* A flow classification rule.
63  *
64  * Use one of the cls_rule_*() functions to initialize a cls_rule.
65  *
66  * The cls_rule_*() functions below maintain the following important
67  * invariant that the classifier depends on:
68  *
69  *   - If a bit or a field is wildcarded in 'wc', then the corresponding bit or
70  *     field in 'flow' is set to all-0-bits.  (The
71  *     cls_rule_zero_wildcarded_fields() function can be used to restore this
72  *     invariant after adding wildcards.)
73  */
74 struct cls_rule {
75     struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
76     struct list list;           /* List of identical, lower-priority rules. */
77     struct flow flow;           /* All field values. */
78     struct flow_wildcards wc;   /* Wildcards for fields. */
79     unsigned int priority;      /* Larger numbers are higher priorities. */
80 };
81
82 void cls_rule_init(const struct flow *, const struct flow_wildcards *,
83                    unsigned int priority, struct cls_rule *);
84 void cls_rule_init_exact(const struct flow *, unsigned int priority,
85                          struct cls_rule *);
86 void cls_rule_init_catchall(struct cls_rule *, unsigned int priority);
87
88 void cls_rule_zero_wildcarded_fields(struct cls_rule *);
89
90 void cls_rule_set_reg(struct cls_rule *, unsigned int reg_idx, uint32_t value);
91 void cls_rule_set_reg_masked(struct cls_rule *, unsigned int reg_idx,
92                              uint32_t value, uint32_t mask);
93 void cls_rule_set_metadata(struct cls_rule *, ovs_be64 metadata);
94 void cls_rule_set_metadata_masked(struct cls_rule *, ovs_be64 metadata,
95                                   ovs_be64 mask);
96 void cls_rule_set_tun_id(struct cls_rule *, ovs_be64 tun_id);
97 void cls_rule_set_tun_id_masked(struct cls_rule *,
98                                 ovs_be64 tun_id, ovs_be64 mask);
99 void cls_rule_set_in_port(struct cls_rule *, uint16_t ofp_port);
100 void cls_rule_set_dl_type(struct cls_rule *, ovs_be16);
101 void cls_rule_set_dl_src(struct cls_rule *, const uint8_t[6]);
102 void cls_rule_set_dl_src_masked(struct cls_rule *, const uint8_t dl_src[6],
103                                 const uint8_t mask[6]);
104 void cls_rule_set_dl_dst(struct cls_rule *, const uint8_t[6]);
105 void cls_rule_set_dl_dst_masked(struct cls_rule *, const uint8_t dl_dst[6],
106                                 const uint8_t mask[6]);
107 void cls_rule_set_dl_tci(struct cls_rule *, ovs_be16 tci);
108 void cls_rule_set_dl_tci_masked(struct cls_rule *,
109                                 ovs_be16 tci, ovs_be16 mask);
110 void cls_rule_set_any_vid(struct cls_rule *);
111 void cls_rule_set_dl_vlan(struct cls_rule *, ovs_be16);
112 void cls_rule_set_any_pcp(struct cls_rule *);
113 void cls_rule_set_dl_vlan_pcp(struct cls_rule *, uint8_t);
114 void cls_rule_set_tp_src(struct cls_rule *, ovs_be16);
115 void cls_rule_set_tp_src_masked(struct cls_rule *,
116                                 ovs_be16 port, ovs_be16 mask);
117 void cls_rule_set_tp_dst(struct cls_rule *, ovs_be16);
118 void cls_rule_set_tp_dst_masked(struct cls_rule *,
119                                 ovs_be16 port, ovs_be16 mask);
120 void cls_rule_set_nw_proto(struct cls_rule *, uint8_t);
121 void cls_rule_set_nw_src(struct cls_rule *, ovs_be32);
122 void cls_rule_set_nw_src_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
123 void cls_rule_set_nw_dst(struct cls_rule *, ovs_be32);
124 void cls_rule_set_nw_dst_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
125 void cls_rule_set_nw_dscp(struct cls_rule *, uint8_t);
126 void cls_rule_set_nw_ecn(struct cls_rule *, uint8_t);
127 void cls_rule_set_nw_ttl(struct cls_rule *, uint8_t);
128 void cls_rule_set_nw_frag(struct cls_rule *, uint8_t nw_frag);
129 void cls_rule_set_nw_frag_masked(struct cls_rule *,
130                                  uint8_t nw_frag, uint8_t mask);
131 void cls_rule_set_icmp_type(struct cls_rule *, uint8_t);
132 void cls_rule_set_icmp_code(struct cls_rule *, uint8_t);
133 void cls_rule_set_arp_sha(struct cls_rule *, const uint8_t[6]);
134 void cls_rule_set_arp_tha(struct cls_rule *, const uint8_t[6]);
135 void cls_rule_set_ipv6_src(struct cls_rule *, const struct in6_addr *);
136 void cls_rule_set_ipv6_src_masked(struct cls_rule *, const struct in6_addr *,
137                                   const struct in6_addr *);
138 void cls_rule_set_ipv6_dst(struct cls_rule *, const struct in6_addr *);
139 void cls_rule_set_ipv6_dst_masked(struct cls_rule *, const struct in6_addr *,
140                                   const struct in6_addr *);
141 void cls_rule_set_ipv6_label(struct cls_rule *, ovs_be32);
142 void cls_rule_set_nd_target(struct cls_rule *, const struct in6_addr *);
143 void cls_rule_set_nd_target_masked(struct cls_rule *, const struct in6_addr *,
144                                    const struct in6_addr *);
145
146 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
147 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
148
149 void cls_rule_format(const struct cls_rule *, struct ds *);
150 char *cls_rule_to_string(const struct cls_rule *);
151 void cls_rule_print(const struct cls_rule *);
152
153 void classifier_init(struct classifier *);
154 void classifier_destroy(struct classifier *);
155 bool classifier_is_empty(const struct classifier *);
156 int classifier_count(const struct classifier *);
157 void classifier_insert(struct classifier *, struct cls_rule *);
158 struct cls_rule *classifier_replace(struct classifier *, struct cls_rule *);
159 void classifier_remove(struct classifier *, struct cls_rule *);
160 struct cls_rule *classifier_lookup(const struct classifier *,
161                                    const struct flow *);
162 bool classifier_rule_overlaps(const struct classifier *,
163                               const struct cls_rule *);
164
165 typedef void cls_cb_func(struct cls_rule *, void *aux);
166
167 struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
168                                               const struct cls_rule *);
169 \f
170 /* Iteration. */
171
172 struct cls_cursor {
173     const struct classifier *cls;
174     const struct cls_table *table;
175     const struct cls_rule *target;
176 };
177
178 void cls_cursor_init(struct cls_cursor *, const struct classifier *,
179                      const struct cls_rule *match);
180 struct cls_rule *cls_cursor_first(struct cls_cursor *);
181 struct cls_rule *cls_cursor_next(struct cls_cursor *, struct cls_rule *);
182
183 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
184     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
185          &(RULE)->MEMBER != NULL;                                       \
186          ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
187                           MEMBER))
188
189 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
190     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
191          (&(RULE)->MEMBER != NULL                                       \
192           ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
193                              MEMBER)                                    \
194           : 0);                                                         \
195          (RULE) = (NEXT))
196
197 #ifdef __cplusplus
198 }
199 #endif
200
201 #endif /* classifier.h */