]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
8368c090 | 2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "classifier.h" | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <netinet/in.h> | |
844dff32 | 22 | #include "byte-order.h" |
68d1c8c3 | 23 | #include "dynamic-string.h" |
064af421 BP |
24 | #include "flow.h" |
25 | #include "hash.h" | |
07b37e8f | 26 | #include "odp-util.h" |
d8ae4d67 | 27 | #include "ofp-util.h" |
b5d97350 | 28 | #include "packets.h" |
064af421 | 29 | |
b5d97350 BP |
30 | static struct cls_table *find_table(const struct classifier *, |
31 | const struct flow_wildcards *); | |
32 | static struct cls_table *insert_table(struct classifier *, | |
33 | const struct flow_wildcards *); | |
34 | ||
35 | static struct cls_table *classifier_first_table(const struct classifier *); | |
36 | static struct cls_table *classifier_next_table(const struct classifier *, | |
37 | const struct cls_table *); | |
38 | static void destroy_table(struct classifier *, struct cls_table *); | |
39 | ||
b5d97350 BP |
40 | static struct cls_rule *find_match(const struct cls_table *, |
41 | const struct flow *); | |
42 | static struct cls_rule *find_equal(struct cls_table *, const struct flow *, | |
43 | uint32_t hash); | |
44 | static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *); | |
45 | ||
46 | static bool flow_equal_except(const struct flow *, const struct flow *, | |
47 | const struct flow_wildcards *); | |
b5d97350 BP |
48 | |
49 | /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */ | |
50 | #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \ | |
51 | for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE)) | |
52 | #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \ | |
53 | for ((RULE) = (HEAD); \ | |
54 | (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \ | |
55 | (RULE) = (NEXT)) | |
56 | ||
955f579d | 57 | static struct cls_rule *next_rule_in_list__(struct cls_rule *); |
b5d97350 BP |
58 | static struct cls_rule *next_rule_in_list(struct cls_rule *); |
59 | ||
60 | static struct cls_table * | |
61 | cls_table_from_hmap_node(const struct hmap_node *node) | |
62 | { | |
63 | return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL; | |
64 | } | |
65 | ||
b63f2ea7 BP |
66 | /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given |
67 | * 'wildcards' and 'priority'. */ | |
68 | void | |
69 | cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards, | |
70 | unsigned int priority, struct cls_rule *rule) | |
b5d97350 BP |
71 | { |
72 | rule->flow = *flow; | |
b63f2ea7 BP |
73 | rule->wc = *wildcards; |
74 | rule->priority = priority; | |
52ce26ee | 75 | cls_rule_zero_wildcarded_fields(rule); |
b5d97350 | 76 | } |
064af421 | 77 | |
b63f2ea7 BP |
78 | /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the |
79 | * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest | |
80 | * priority, so 'priority' should be at least 65535.) */ | |
064af421 | 81 | void |
b63f2ea7 BP |
82 | cls_rule_init_exact(const struct flow *flow, |
83 | unsigned int priority, struct cls_rule *rule) | |
064af421 | 84 | { |
b63f2ea7 | 85 | rule->flow = *flow; |
abff858b | 86 | rule->flow.priority = 0; |
b63f2ea7 | 87 | flow_wildcards_init_exact(&rule->wc); |
064af421 | 88 | rule->priority = priority; |
064af421 BP |
89 | } |
90 | ||
844dff32 BP |
91 | /* Initializes 'rule' as a "catch-all" rule that matches every packet, with |
92 | * priority 'priority'. */ | |
93 | void | |
94 | cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority) | |
95 | { | |
96 | memset(&rule->flow, 0, sizeof rule->flow); | |
d8ae4d67 | 97 | flow_wildcards_init_catchall(&rule->wc); |
844dff32 BP |
98 | rule->priority = priority; |
99 | } | |
100 | ||
b5d97350 BP |
101 | /* For each bit or field wildcarded in 'rule', sets the corresponding bit or |
102 | * field in 'flow' to all-0-bits. It is important to maintain this invariant | |
103 | * in a clr_rule that might be inserted into a classifier. | |
104 | * | |
105 | * It is never necessary to call this function directly for a cls_rule that is | |
106 | * initialized or modified only by cls_rule_*() functions. It is useful to | |
107 | * restore the invariant in a cls_rule whose 'wc' member is modified by hand. | |
108 | */ | |
109 | void | |
52ce26ee | 110 | cls_rule_zero_wildcarded_fields(struct cls_rule *rule) |
b5d97350 | 111 | { |
993410fb | 112 | flow_zero_wildcards(&rule->flow, &rule->wc); |
064af421 BP |
113 | } |
114 | ||
87542e21 BP |
115 | void |
116 | cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value) | |
117 | { | |
118 | cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX); | |
119 | } | |
120 | ||
121 | void | |
122 | cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx, | |
123 | uint32_t value, uint32_t mask) | |
124 | { | |
125 | assert(reg_idx < FLOW_N_REGS); | |
126 | flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask); | |
127 | rule->flow.regs[reg_idx] = value & mask; | |
128 | } | |
129 | ||
130 | void | |
b9298d3f | 131 | cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id) |
87542e21 | 132 | { |
8368c090 BP |
133 | cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX)); |
134 | } | |
135 | ||
136 | void | |
137 | cls_rule_set_tun_id_masked(struct cls_rule *rule, | |
138 | ovs_be64 tun_id, ovs_be64 mask) | |
139 | { | |
140 | rule->wc.tun_id_mask = mask; | |
141 | rule->flow.tun_id = tun_id & mask; | |
87542e21 BP |
142 | } |
143 | ||
64420dfa | 144 | void |
abe529af | 145 | cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port) |
64420dfa | 146 | { |
d8ae4d67 | 147 | rule->wc.wildcards &= ~FWW_IN_PORT; |
abe529af | 148 | rule->flow.in_port = ofp_port; |
64420dfa BP |
149 | } |
150 | ||
151 | void | |
152 | cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type) | |
153 | { | |
d8ae4d67 | 154 | rule->wc.wildcards &= ~FWW_DL_TYPE; |
64420dfa BP |
155 | rule->flow.dl_type = dl_type; |
156 | } | |
157 | ||
158 | void | |
159 | cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN]) | |
160 | { | |
d8ae4d67 | 161 | rule->wc.wildcards &= ~FWW_DL_SRC; |
64420dfa BP |
162 | memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN); |
163 | } | |
164 | ||
db7f8281 | 165 | /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' exactly. */ |
64420dfa BP |
166 | void |
167 | cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN]) | |
168 | { | |
d8ae4d67 | 169 | rule->wc.wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST); |
64420dfa BP |
170 | memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN); |
171 | } | |
172 | ||
db7f8281 BP |
173 | /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' after each |
174 | * byte is ANDed with the appropriate byte in 'mask'. | |
175 | * | |
176 | * This function will assert-fail if 'mask' is invalid. Only 'mask' values | |
177 | * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */ | |
178 | void | |
179 | cls_rule_set_dl_dst_masked(struct cls_rule *rule, | |
180 | const uint8_t dl_dst[ETH_ADDR_LEN], | |
181 | const uint8_t mask[ETH_ADDR_LEN]) | |
182 | { | |
183 | flow_wildcards_t *wc = &rule->wc.wildcards; | |
184 | size_t i; | |
185 | ||
186 | *wc = flow_wildcards_set_dl_dst_mask(*wc, mask); | |
187 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
188 | rule->flow.dl_dst[i] = dl_dst[i] & mask[i]; | |
189 | } | |
190 | } | |
191 | ||
66642cb4 | 192 | void |
81c9dad2 BP |
193 | cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci) |
194 | { | |
66642cb4 | 195 | cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff)); |
81c9dad2 BP |
196 | } |
197 | ||
66642cb4 | 198 | void |
81c9dad2 BP |
199 | cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask) |
200 | { | |
66642cb4 BP |
201 | rule->flow.vlan_tci = tci & mask; |
202 | rule->wc.vlan_tci_mask = mask; | |
203 | } | |
81c9dad2 | 204 | |
66642cb4 BP |
205 | /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already |
206 | * wildcarded, then 'rule' will match a packet regardless of whether it has an | |
207 | * 802.1Q header or not. */ | |
208 | void | |
209 | cls_rule_set_any_vid(struct cls_rule *rule) | |
210 | { | |
211 | if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) { | |
212 | rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK); | |
213 | rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK); | |
214 | } else { | |
215 | cls_rule_set_dl_tci_masked(rule, htons(0), htons(0)); | |
81c9dad2 BP |
216 | } |
217 | } | |
218 | ||
66642cb4 BP |
219 | /* Modifies 'rule' depending on 'dl_vlan': |
220 | * | |
221 | * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets | |
222 | * without an 802.1Q header. | |
223 | * | |
224 | * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose | |
225 | * VID equals the low 12 bits of 'dl_vlan'. | |
226 | */ | |
81c9dad2 BP |
227 | void |
228 | cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan) | |
229 | { | |
66642cb4 BP |
230 | if (dl_vlan == htons(OFP_VLAN_NONE)) { |
231 | cls_rule_set_dl_tci(rule, htons(0)); | |
232 | } else { | |
81c9dad2 | 233 | dl_vlan &= htons(VLAN_VID_MASK); |
66642cb4 BP |
234 | rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK); |
235 | rule->flow.vlan_tci |= htons(VLAN_CFI) | dl_vlan; | |
236 | rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI); | |
81c9dad2 | 237 | } |
66642cb4 | 238 | } |
81c9dad2 | 239 | |
66642cb4 BP |
240 | /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already |
241 | * wildcarded, then 'rule' will match a packet regardless of whether it has an | |
242 | * 802.1Q header or not. */ | |
243 | void | |
244 | cls_rule_set_any_pcp(struct cls_rule *rule) | |
245 | { | |
246 | if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) { | |
247 | rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK); | |
248 | rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
249 | } else { | |
250 | cls_rule_set_dl_tci_masked(rule, htons(0), htons(0)); | |
251 | } | |
81c9dad2 BP |
252 | } |
253 | ||
66642cb4 BP |
254 | /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose |
255 | * PCP equals the low 3 bits of 'dl_vlan_pcp'. */ | |
81c9dad2 BP |
256 | void |
257 | cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp) | |
258 | { | |
66642cb4 BP |
259 | dl_vlan_pcp &= 0x07; |
260 | rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
261 | rule->flow.vlan_tci |= htons((dl_vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI); | |
262 | rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK); | |
81c9dad2 BP |
263 | } |
264 | ||
64420dfa BP |
265 | void |
266 | cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src) | |
267 | { | |
d8ae4d67 | 268 | rule->wc.wildcards &= ~FWW_TP_SRC; |
64420dfa BP |
269 | rule->flow.tp_src = tp_src; |
270 | } | |
271 | ||
272 | void | |
273 | cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst) | |
274 | { | |
d8ae4d67 | 275 | rule->wc.wildcards &= ~FWW_TP_DST; |
64420dfa BP |
276 | rule->flow.tp_dst = tp_dst; |
277 | } | |
278 | ||
279 | void | |
280 | cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto) | |
281 | { | |
d8ae4d67 | 282 | rule->wc.wildcards &= ~FWW_NW_PROTO; |
64420dfa BP |
283 | rule->flow.nw_proto = nw_proto; |
284 | } | |
285 | ||
286 | void | |
287 | cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src) | |
288 | { | |
209c0b17 JP |
289 | rule->flow.nw_src = nw_src; |
290 | rule->wc.nw_src_mask = htonl(UINT32_MAX); | |
81c9dad2 BP |
291 | } |
292 | ||
209c0b17 JP |
293 | void |
294 | cls_rule_set_nw_src_masked(struct cls_rule *rule, | |
295 | ovs_be32 nw_src, ovs_be32 mask) | |
81c9dad2 | 296 | { |
209c0b17 JP |
297 | rule->flow.nw_src = nw_src & mask; |
298 | rule->wc.nw_src_mask = mask; | |
64420dfa BP |
299 | } |
300 | ||
301 | void | |
302 | cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst) | |
303 | { | |
209c0b17 JP |
304 | rule->flow.nw_dst = nw_dst; |
305 | rule->wc.nw_dst_mask = htonl(UINT32_MAX); | |
81c9dad2 BP |
306 | } |
307 | ||
209c0b17 | 308 | void |
81c9dad2 BP |
309 | cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask) |
310 | { | |
209c0b17 JP |
311 | rule->flow.nw_dst = ip & mask; |
312 | rule->wc.nw_dst_mask = mask; | |
81c9dad2 BP |
313 | } |
314 | ||
315 | void | |
530180fd | 316 | cls_rule_set_nw_dscp(struct cls_rule *rule, uint8_t nw_dscp) |
81c9dad2 | 317 | { |
eadef313 JP |
318 | rule->wc.nw_tos_mask |= IP_DSCP_MASK; |
319 | rule->flow.nw_tos &= ~IP_DSCP_MASK; | |
320 | rule->flow.nw_tos |= nw_dscp & IP_DSCP_MASK; | |
530180fd JP |
321 | } |
322 | ||
323 | void | |
324 | cls_rule_set_nw_ecn(struct cls_rule *rule, uint8_t nw_ecn) | |
325 | { | |
eadef313 JP |
326 | rule->wc.nw_tos_mask |= IP_ECN_MASK; |
327 | rule->flow.nw_tos &= ~IP_ECN_MASK; | |
328 | rule->flow.nw_tos |= nw_ecn & IP_ECN_MASK; | |
7257b535 BP |
329 | } |
330 | ||
a61680c6 JP |
331 | void |
332 | cls_rule_set_nw_ttl(struct cls_rule *rule, uint8_t nw_ttl) | |
333 | { | |
334 | rule->wc.wildcards &= ~FWW_NW_TTL; | |
335 | rule->flow.nw_ttl = nw_ttl; | |
336 | } | |
337 | ||
7257b535 | 338 | void |
eadef313 | 339 | cls_rule_set_nw_frag(struct cls_rule *rule, uint8_t nw_frag) |
7257b535 | 340 | { |
eadef313 JP |
341 | rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK; |
342 | rule->flow.nw_frag = nw_frag; | |
7257b535 BP |
343 | } |
344 | ||
345 | void | |
eadef313 JP |
346 | cls_rule_set_nw_frag_masked(struct cls_rule *rule, |
347 | uint8_t nw_frag, uint8_t mask) | |
7257b535 | 348 | { |
eadef313 JP |
349 | rule->flow.nw_frag = nw_frag & mask; |
350 | rule->wc.nw_frag_mask = mask; | |
81c9dad2 BP |
351 | } |
352 | ||
353 | void | |
354 | cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type) | |
355 | { | |
d8ae4d67 | 356 | rule->wc.wildcards &= ~FWW_TP_SRC; |
3ee8a9f0 | 357 | rule->flow.tp_src = htons(icmp_type); |
81c9dad2 BP |
358 | } |
359 | ||
360 | void | |
361 | cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code) | |
362 | { | |
d8ae4d67 | 363 | rule->wc.wildcards &= ~FWW_TP_DST; |
3ee8a9f0 | 364 | rule->flow.tp_dst = htons(icmp_code); |
64420dfa BP |
365 | } |
366 | ||
bad68a99 JP |
367 | void |
368 | cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN]) | |
369 | { | |
370 | rule->wc.wildcards &= ~FWW_ARP_SHA; | |
371 | memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN); | |
372 | } | |
373 | ||
374 | void | |
375 | cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN]) | |
376 | { | |
377 | rule->wc.wildcards &= ~FWW_ARP_THA; | |
378 | memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN); | |
379 | } | |
380 | ||
d31f1109 JP |
381 | void |
382 | cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src) | |
383 | { | |
209c0b17 JP |
384 | rule->flow.ipv6_src = *src; |
385 | rule->wc.ipv6_src_mask = in6addr_exact; | |
d31f1109 JP |
386 | } |
387 | ||
209c0b17 | 388 | void |
d31f1109 JP |
389 | cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src, |
390 | const struct in6_addr *mask) | |
391 | { | |
209c0b17 JP |
392 | rule->flow.ipv6_src = ipv6_addr_bitand(src, mask); |
393 | rule->wc.ipv6_src_mask = *mask; | |
d31f1109 JP |
394 | } |
395 | ||
396 | void | |
397 | cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst) | |
398 | { | |
209c0b17 JP |
399 | rule->flow.ipv6_dst = *dst; |
400 | rule->wc.ipv6_dst_mask = in6addr_exact; | |
d31f1109 JP |
401 | } |
402 | ||
209c0b17 | 403 | void |
d31f1109 JP |
404 | cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst, |
405 | const struct in6_addr *mask) | |
406 | { | |
209c0b17 JP |
407 | rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask); |
408 | rule->wc.ipv6_dst_mask = *mask; | |
d31f1109 JP |
409 | } |
410 | ||
fa8223b7 JP |
411 | void |
412 | cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label) | |
413 | { | |
414 | rule->wc.wildcards &= ~FWW_IPV6_LABEL; | |
415 | rule->flow.ipv6_label = ipv6_label; | |
416 | } | |
417 | ||
685a51a5 | 418 | void |
e7ed3a3a | 419 | cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target) |
685a51a5 JP |
420 | { |
421 | rule->wc.wildcards &= ~FWW_ND_TARGET; | |
e7ed3a3a | 422 | rule->flow.nd_target = *target; |
685a51a5 JP |
423 | } |
424 | ||
193eb874 BP |
425 | /* Returns true if 'a' and 'b' have the same priority, wildcard the same |
426 | * fields, and have the same values for fixed fields, otherwise false. */ | |
427 | bool | |
428 | cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b) | |
429 | { | |
430 | return (a->priority == b->priority | |
431 | && flow_wildcards_equal(&a->wc, &b->wc) | |
432 | && flow_equal(&a->flow, &b->flow)); | |
433 | } | |
434 | ||
57452fdc BP |
435 | /* Returns a hash value for the flow, wildcards, and priority in 'rule', |
436 | * starting from 'basis'. */ | |
437 | uint32_t | |
438 | cls_rule_hash(const struct cls_rule *rule, uint32_t basis) | |
439 | { | |
440 | uint32_t h0 = flow_hash(&rule->flow, basis); | |
441 | uint32_t h1 = flow_wildcards_hash(&rule->wc, h0); | |
442 | return hash_int(rule->priority, h1); | |
443 | } | |
444 | ||
07b37e8f BP |
445 | static void |
446 | format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip, | |
447 | ovs_be32 netmask) | |
448 | { | |
449 | if (netmask) { | |
aad29cd1 BP |
450 | ds_put_format(s, "%s=", name); |
451 | ip_format_masked(ip, netmask, s); | |
07b37e8f BP |
452 | ds_put_char(s, ','); |
453 | } | |
454 | } | |
455 | ||
d31f1109 JP |
456 | static void |
457 | format_ipv6_netmask(struct ds *s, const char *name, | |
458 | const struct in6_addr *addr, | |
459 | const struct in6_addr *netmask) | |
460 | { | |
461 | if (!ipv6_mask_is_any(netmask)) { | |
462 | ds_put_format(s, "%s=", name); | |
aad29cd1 | 463 | print_ipv6_masked(s, addr, netmask); |
d31f1109 JP |
464 | ds_put_char(s, ','); |
465 | } | |
466 | } | |
467 | ||
07b37e8f BP |
468 | void |
469 | cls_rule_format(const struct cls_rule *rule, struct ds *s) | |
470 | { | |
471 | const struct flow_wildcards *wc = &rule->wc; | |
472 | size_t start_len = s->length; | |
473 | flow_wildcards_t w = wc->wildcards; | |
474 | const struct flow *f = &rule->flow; | |
475 | bool skip_type = false; | |
476 | bool skip_proto = false; | |
477 | ||
478 | int i; | |
479 | ||
a61680c6 | 480 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6); |
a877206f | 481 | |
8fe2b968 | 482 | if (rule->priority != OFP_DEFAULT_PRIORITY) { |
07b37e8f BP |
483 | ds_put_format(s, "priority=%d,", rule->priority); |
484 | } | |
485 | ||
486 | if (!(w & FWW_DL_TYPE)) { | |
487 | skip_type = true; | |
488 | if (f->dl_type == htons(ETH_TYPE_IP)) { | |
489 | if (!(w & FWW_NW_PROTO)) { | |
490 | skip_proto = true; | |
6767a2cc | 491 | if (f->nw_proto == IPPROTO_ICMP) { |
07b37e8f | 492 | ds_put_cstr(s, "icmp,"); |
6767a2cc | 493 | } else if (f->nw_proto == IPPROTO_TCP) { |
07b37e8f | 494 | ds_put_cstr(s, "tcp,"); |
6767a2cc | 495 | } else if (f->nw_proto == IPPROTO_UDP) { |
07b37e8f BP |
496 | ds_put_cstr(s, "udp,"); |
497 | } else { | |
498 | ds_put_cstr(s, "ip,"); | |
499 | skip_proto = false; | |
500 | } | |
501 | } else { | |
502 | ds_put_cstr(s, "ip,"); | |
503 | } | |
d31f1109 JP |
504 | } else if (f->dl_type == htons(ETH_TYPE_IPV6)) { |
505 | if (!(w & FWW_NW_PROTO)) { | |
506 | skip_proto = true; | |
507 | if (f->nw_proto == IPPROTO_ICMPV6) { | |
508 | ds_put_cstr(s, "icmp6,"); | |
509 | } else if (f->nw_proto == IPPROTO_TCP) { | |
510 | ds_put_cstr(s, "tcp6,"); | |
511 | } else if (f->nw_proto == IPPROTO_UDP) { | |
512 | ds_put_cstr(s, "udp6,"); | |
513 | } else { | |
514 | ds_put_cstr(s, "ipv6,"); | |
515 | skip_proto = false; | |
516 | } | |
517 | } else { | |
518 | ds_put_cstr(s, "ipv6,"); | |
519 | } | |
07b37e8f BP |
520 | } else if (f->dl_type == htons(ETH_TYPE_ARP)) { |
521 | ds_put_cstr(s, "arp,"); | |
522 | } else { | |
523 | skip_type = false; | |
524 | } | |
525 | } | |
526 | for (i = 0; i < FLOW_N_REGS; i++) { | |
527 | switch (wc->reg_masks[i]) { | |
528 | case 0: | |
529 | break; | |
530 | case UINT32_MAX: | |
531 | ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]); | |
532 | break; | |
533 | default: | |
534 | ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",", | |
535 | i, f->regs[i], wc->reg_masks[i]); | |
536 | break; | |
537 | } | |
538 | } | |
8368c090 BP |
539 | switch (wc->tun_id_mask) { |
540 | case 0: | |
541 | break; | |
542 | case CONSTANT_HTONLL(UINT64_MAX): | |
543 | ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id)); | |
544 | break; | |
545 | default: | |
546 | ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",", | |
547 | ntohll(f->tun_id), ntohll(wc->tun_id_mask)); | |
548 | break; | |
07b37e8f BP |
549 | } |
550 | if (!(w & FWW_IN_PORT)) { | |
abe529af | 551 | ds_put_format(s, "in_port=%"PRIu16",", f->in_port); |
07b37e8f BP |
552 | } |
553 | if (wc->vlan_tci_mask) { | |
554 | ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK); | |
555 | ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK); | |
556 | ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI); | |
557 | ||
558 | if (cfi && f->vlan_tci & htons(VLAN_CFI) | |
559 | && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) | |
560 | && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) | |
561 | && (vid_mask || pcp_mask)) { | |
562 | if (vid_mask) { | |
563 | ds_put_format(s, "dl_vlan=%"PRIu16",", | |
564 | vlan_tci_to_vid(f->vlan_tci)); | |
565 | } | |
566 | if (pcp_mask) { | |
567 | ds_put_format(s, "dl_vlan_pcp=%d,", | |
568 | vlan_tci_to_pcp(f->vlan_tci)); | |
569 | } | |
ce0307c4 BP |
570 | } else if (wc->vlan_tci_mask == htons(0xffff)) { |
571 | ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci)); | |
07b37e8f BP |
572 | } else { |
573 | ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",", | |
574 | ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask)); | |
575 | } | |
576 | } | |
577 | if (!(w & FWW_DL_SRC)) { | |
578 | ds_put_format(s, "dl_src="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_src)); | |
579 | } | |
580 | switch (w & (FWW_DL_DST | FWW_ETH_MCAST)) { | |
581 | case 0: | |
582 | ds_put_format(s, "dl_dst="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_dst)); | |
583 | break; | |
584 | case FWW_DL_DST: | |
585 | ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/01:00:00:00:00:00,", | |
586 | ETH_ADDR_ARGS(f->dl_dst)); | |
587 | break; | |
588 | case FWW_ETH_MCAST: | |
589 | ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/fe:ff:ff:ff:ff:ff,", | |
590 | ETH_ADDR_ARGS(f->dl_dst)); | |
591 | break; | |
592 | case FWW_DL_DST | FWW_ETH_MCAST: | |
593 | break; | |
594 | } | |
595 | if (!skip_type && !(w & FWW_DL_TYPE)) { | |
596 | ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type)); | |
597 | } | |
d31f1109 JP |
598 | if (f->dl_type == htons(ETH_TYPE_IPV6)) { |
599 | format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask); | |
600 | format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask); | |
fa8223b7 JP |
601 | if (!(w & FWW_IPV6_LABEL)) { |
602 | ds_put_format(s, "ipv6_label=0x%05"PRIx32",", ntohl(f->ipv6_label)); | |
603 | } | |
d31f1109 JP |
604 | } else { |
605 | format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask); | |
606 | format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask); | |
607 | } | |
07b37e8f BP |
608 | if (!skip_proto && !(w & FWW_NW_PROTO)) { |
609 | if (f->dl_type == htons(ETH_TYPE_ARP)) { | |
fb115f91 | 610 | ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto); |
07b37e8f | 611 | } else { |
92ec5741 | 612 | ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto); |
07b37e8f BP |
613 | } |
614 | } | |
bad68a99 JP |
615 | if (f->dl_type == htons(ETH_TYPE_ARP)) { |
616 | if (!(w & FWW_ARP_SHA)) { | |
617 | ds_put_format(s, "arp_sha="ETH_ADDR_FMT",", | |
618 | ETH_ADDR_ARGS(f->arp_sha)); | |
619 | } | |
620 | if (!(w & FWW_ARP_THA)) { | |
621 | ds_put_format(s, "arp_tha="ETH_ADDR_FMT",", | |
622 | ETH_ADDR_ARGS(f->arp_tha)); | |
623 | } | |
624 | } | |
eadef313 JP |
625 | if (wc->nw_tos_mask & IP_DSCP_MASK) { |
626 | ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK); | |
7257b535 | 627 | } |
eadef313 JP |
628 | if (wc->nw_tos_mask & IP_ECN_MASK) { |
629 | ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK); | |
530180fd | 630 | } |
a61680c6 JP |
631 | if (!(w & FWW_NW_TTL)) { |
632 | ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl); | |
633 | } | |
eadef313 JP |
634 | switch (wc->nw_frag_mask) { |
635 | case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER: | |
636 | ds_put_format(s, "nw_frag=%s,", | |
637 | f->nw_frag & FLOW_NW_FRAG_ANY | |
638 | ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first") | |
639 | : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no")); | |
7257b535 BP |
640 | break; |
641 | ||
eadef313 JP |
642 | case FLOW_NW_FRAG_ANY: |
643 | ds_put_format(s, "nw_frag=%s,", | |
644 | f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no"); | |
7257b535 BP |
645 | break; |
646 | ||
eadef313 JP |
647 | case FLOW_NW_FRAG_LATER: |
648 | ds_put_format(s, "nw_frag=%s,", | |
649 | f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later"); | |
7257b535 | 650 | break; |
07b37e8f | 651 | } |
6767a2cc | 652 | if (f->nw_proto == IPPROTO_ICMP) { |
07b37e8f | 653 | if (!(w & FWW_TP_SRC)) { |
92ec5741 | 654 | ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src)); |
07b37e8f BP |
655 | } |
656 | if (!(w & FWW_TP_DST)) { | |
92ec5741 | 657 | ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst)); |
07b37e8f | 658 | } |
d31f1109 JP |
659 | } else if (f->nw_proto == IPPROTO_ICMPV6) { |
660 | if (!(w & FWW_TP_SRC)) { | |
661 | ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src)); | |
662 | } | |
663 | if (!(w & FWW_TP_DST)) { | |
664 | ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst)); | |
665 | } | |
685a51a5 JP |
666 | if (!(w & FWW_ND_TARGET)) { |
667 | ds_put_cstr(s, "nd_target="); | |
668 | print_ipv6_addr(s, &f->nd_target); | |
669 | ds_put_char(s, ','); | |
670 | } | |
671 | if (!(w & FWW_ARP_SHA)) { | |
b53055f4 | 672 | ds_put_format(s, "nd_sll="ETH_ADDR_FMT",", |
685a51a5 JP |
673 | ETH_ADDR_ARGS(f->arp_sha)); |
674 | } | |
675 | if (!(w & FWW_ARP_THA)) { | |
b53055f4 | 676 | ds_put_format(s, "nd_tll="ETH_ADDR_FMT",", |
685a51a5 JP |
677 | ETH_ADDR_ARGS(f->arp_tha)); |
678 | } | |
679 | } else { | |
07b37e8f | 680 | if (!(w & FWW_TP_SRC)) { |
92ec5741 | 681 | ds_put_format(s, "tp_src=%"PRIu16",", ntohs(f->tp_src)); |
07b37e8f BP |
682 | } |
683 | if (!(w & FWW_TP_DST)) { | |
92ec5741 | 684 | ds_put_format(s, "tp_dst=%"PRIu16",", ntohs(f->tp_dst)); |
07b37e8f BP |
685 | } |
686 | } | |
687 | ||
688 | if (s->length > start_len && ds_last(s) == ',') { | |
689 | s->length--; | |
690 | } | |
691 | } | |
692 | ||
68d1c8c3 BP |
693 | /* Converts 'rule' to a string and returns the string. The caller must free |
694 | * the string (with free()). */ | |
695 | char * | |
696 | cls_rule_to_string(const struct cls_rule *rule) | |
697 | { | |
698 | struct ds s = DS_EMPTY_INITIALIZER; | |
07b37e8f BP |
699 | cls_rule_format(rule, &s); |
700 | return ds_steal_cstr(&s); | |
68d1c8c3 BP |
701 | } |
702 | ||
064af421 BP |
703 | void |
704 | cls_rule_print(const struct cls_rule *rule) | |
705 | { | |
07b37e8f BP |
706 | char *s = cls_rule_to_string(rule); |
707 | puts(s); | |
708 | free(s); | |
064af421 | 709 | } |
064af421 BP |
710 | \f |
711 | /* Initializes 'cls' as a classifier that initially contains no classification | |
712 | * rules. */ | |
713 | void | |
714 | classifier_init(struct classifier *cls) | |
715 | { | |
064af421 | 716 | cls->n_rules = 0; |
b5d97350 | 717 | hmap_init(&cls->tables); |
064af421 BP |
718 | } |
719 | ||
720 | /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the | |
721 | * caller's responsibility. */ | |
722 | void | |
723 | classifier_destroy(struct classifier *cls) | |
724 | { | |
725 | if (cls) { | |
b5d97350 | 726 | struct cls_table *table, *next_table; |
064af421 | 727 | |
b5d97350 BP |
728 | HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) { |
729 | hmap_destroy(&table->rules); | |
730 | hmap_remove(&cls->tables, &table->hmap_node); | |
731 | free(table); | |
064af421 | 732 | } |
b5d97350 | 733 | hmap_destroy(&cls->tables); |
064af421 BP |
734 | } |
735 | } | |
736 | ||
b5d97350 | 737 | /* Returns true if 'cls' contains no classification rules, false otherwise. */ |
064af421 BP |
738 | bool |
739 | classifier_is_empty(const struct classifier *cls) | |
740 | { | |
741 | return cls->n_rules == 0; | |
742 | } | |
743 | ||
744 | /* Returns the number of rules in 'classifier'. */ | |
745 | int | |
746 | classifier_count(const struct classifier *cls) | |
747 | { | |
748 | return cls->n_rules; | |
749 | } | |
750 | ||
b5d97350 BP |
751 | /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller |
752 | * must not modify or free it. | |
064af421 BP |
753 | * |
754 | * If 'cls' already contains an identical rule (including wildcards, values of | |
755 | * fixed fields, and priority), replaces the old rule by 'rule' and returns the | |
756 | * rule that was replaced. The caller takes ownership of the returned rule and | |
757 | * is thus responsible for freeing it, etc., as necessary. | |
758 | * | |
759 | * Returns NULL if 'cls' does not contain a rule with an identical key, after | |
760 | * inserting the new rule. In this case, no rules are displaced by the new | |
761 | * rule, even rules that cannot have any effect because the new rule matches a | |
762 | * superset of their flows and has higher priority. */ | |
763 | struct cls_rule * | |
08944c1d | 764 | classifier_replace(struct classifier *cls, struct cls_rule *rule) |
064af421 | 765 | { |
b5d97350 BP |
766 | struct cls_rule *old_rule; |
767 | struct cls_table *table; | |
768 | ||
769 | table = find_table(cls, &rule->wc); | |
770 | if (!table) { | |
771 | table = insert_table(cls, &rule->wc); | |
772 | } | |
773 | ||
774 | old_rule = insert_rule(table, rule); | |
775 | if (!old_rule) { | |
776 | table->n_table_rules++; | |
064af421 BP |
777 | cls->n_rules++; |
778 | } | |
b5d97350 | 779 | return old_rule; |
064af421 BP |
780 | } |
781 | ||
08944c1d BP |
782 | /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller |
783 | * must not modify or free it. | |
784 | * | |
785 | * 'cls' must not contain an identical rule (including wildcards, values of | |
786 | * fixed fields, and priority). Use classifier_find_rule_exactly() to find | |
787 | * such a rule. */ | |
788 | void | |
789 | classifier_insert(struct classifier *cls, struct cls_rule *rule) | |
790 | { | |
791 | struct cls_rule *displaced_rule = classifier_replace(cls, rule); | |
792 | assert(!displaced_rule); | |
793 | } | |
794 | ||
b5d97350 BP |
795 | /* Removes 'rule' from 'cls'. It is the caller's responsibility to free |
796 | * 'rule', if this is desirable. */ | |
064af421 BP |
797 | void |
798 | classifier_remove(struct classifier *cls, struct cls_rule *rule) | |
799 | { | |
b5d97350 BP |
800 | struct cls_rule *head; |
801 | struct cls_table *table; | |
064af421 | 802 | |
b5d97350 BP |
803 | table = find_table(cls, &rule->wc); |
804 | head = find_equal(table, &rule->flow, rule->hmap_node.hash); | |
805 | if (head != rule) { | |
806 | list_remove(&rule->list); | |
807 | } else if (list_is_empty(&rule->list)) { | |
808 | hmap_remove(&table->rules, &rule->hmap_node); | |
809 | } else { | |
810 | struct cls_rule *next = CONTAINER_OF(rule->list.next, | |
811 | struct cls_rule, list); | |
064af421 | 812 | |
b5d97350 BP |
813 | list_remove(&rule->list); |
814 | hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node); | |
815 | } | |
064af421 | 816 | |
f6acdb44 | 817 | if (--table->n_table_rules == 0) { |
b5d97350 | 818 | destroy_table(cls, table); |
064af421 | 819 | } |
b5d97350 BP |
820 | |
821 | cls->n_rules--; | |
064af421 BP |
822 | } |
823 | ||
48c3de13 BP |
824 | /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'. |
825 | * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules | |
3c4486a5 | 826 | * of equal priority match 'flow', returns one arbitrarily. */ |
48c3de13 | 827 | struct cls_rule * |
3c4486a5 | 828 | classifier_lookup(const struct classifier *cls, const struct flow *flow) |
48c3de13 | 829 | { |
b5d97350 BP |
830 | struct cls_table *table; |
831 | struct cls_rule *best; | |
48c3de13 | 832 | |
b5d97350 BP |
833 | best = NULL; |
834 | HMAP_FOR_EACH (table, hmap_node, &cls->tables) { | |
3c4486a5 BP |
835 | struct cls_rule *rule = find_match(table, flow); |
836 | if (rule && (!best || rule->priority > best->priority)) { | |
837 | best = rule; | |
b5d97350 | 838 | } |
48c3de13 | 839 | } |
b5d97350 | 840 | return best; |
48c3de13 BP |
841 | } |
842 | ||
b5d97350 BP |
843 | /* Finds and returns a rule in 'cls' with exactly the same priority and |
844 | * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't | |
c084ce1d | 845 | * contain an exact match. */ |
064af421 BP |
846 | struct cls_rule * |
847 | classifier_find_rule_exactly(const struct classifier *cls, | |
76ecc721 | 848 | const struct cls_rule *target) |
064af421 | 849 | { |
b5d97350 BP |
850 | struct cls_rule *head, *rule; |
851 | struct cls_table *table; | |
064af421 | 852 | |
b5d97350 BP |
853 | table = find_table(cls, &target->wc); |
854 | if (!table) { | |
855 | return NULL; | |
064af421 BP |
856 | } |
857 | ||
b5d97350 | 858 | head = find_equal(table, &target->flow, flow_hash(&target->flow, 0)); |
b5d97350 BP |
859 | FOR_EACH_RULE_IN_LIST (rule, head) { |
860 | if (target->priority >= rule->priority) { | |
861 | return target->priority == rule->priority ? rule : NULL; | |
064af421 BP |
862 | } |
863 | } | |
864 | return NULL; | |
865 | } | |
866 | ||
faa50f40 BP |
867 | /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are |
868 | * considered to overlap if both rules have the same priority and a packet | |
869 | * could match both. */ | |
49bdc010 JP |
870 | bool |
871 | classifier_rule_overlaps(const struct classifier *cls, | |
faa50f40 | 872 | const struct cls_rule *target) |
49bdc010 | 873 | { |
b5d97350 | 874 | struct cls_table *table; |
49bdc010 | 875 | |
b5d97350 BP |
876 | HMAP_FOR_EACH (table, hmap_node, &cls->tables) { |
877 | struct flow_wildcards wc; | |
878 | struct cls_rule *head; | |
49bdc010 | 879 | |
b5d97350 BP |
880 | flow_wildcards_combine(&wc, &target->wc, &table->wc); |
881 | HMAP_FOR_EACH (head, hmap_node, &table->rules) { | |
49bdc010 JP |
882 | struct cls_rule *rule; |
883 | ||
b5d97350 | 884 | FOR_EACH_RULE_IN_LIST (rule, head) { |
faa50f40 | 885 | if (rule->priority == target->priority |
b5d97350 | 886 | && flow_equal_except(&target->flow, &rule->flow, &wc)) { |
49bdc010 JP |
887 | return true; |
888 | } | |
889 | } | |
890 | } | |
891 | } | |
892 | ||
893 | return false; | |
894 | } | |
b5d97350 | 895 | \f |
5ecc9d81 BP |
896 | /* Iteration. */ |
897 | ||
898 | static bool | |
899 | rule_matches(const struct cls_rule *rule, const struct cls_rule *target) | |
900 | { | |
901 | return (!target | |
902 | || flow_equal_except(&rule->flow, &target->flow, &target->wc)); | |
903 | } | |
904 | ||
905 | static struct cls_rule * | |
906 | search_table(const struct cls_table *table, const struct cls_rule *target) | |
907 | { | |
908 | if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) { | |
909 | struct cls_rule *rule; | |
910 | ||
911 | HMAP_FOR_EACH (rule, hmap_node, &table->rules) { | |
912 | if (rule_matches(rule, target)) { | |
913 | return rule; | |
914 | } | |
915 | } | |
916 | } | |
917 | return NULL; | |
918 | } | |
919 | ||
920 | /* Initializes 'cursor' for iterating through 'cls' rules that exactly match | |
921 | * 'target' or are more specific than 'target'. That is, a given 'rule' | |
922 | * matches 'target' if, for every field: | |
923 | * | |
924 | * - 'target' and 'rule' specify the same (non-wildcarded) value for the | |
925 | * field, or | |
926 | * | |
927 | * - 'target' wildcards the field, | |
928 | * | |
929 | * but not if: | |
930 | * | |
931 | * - 'target' and 'rule' specify different values for the field, or | |
932 | * | |
933 | * - 'target' specifies a value for the field but 'rule' wildcards it. | |
934 | * | |
935 | * Equivalently, the truth table for whether a field matches is: | |
936 | * | |
937 | * rule | |
938 | * | |
939 | * wildcard exact | |
940 | * +---------+---------+ | |
941 | * t wild | yes | yes | | |
942 | * a card | | | | |
943 | * r +---------+---------+ | |
944 | * g exact | no |if values| | |
945 | * e | |are equal| | |
946 | * t +---------+---------+ | |
947 | * | |
948 | * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD | |
949 | * commands and by OpenFlow 1.0 aggregate and flow stats. | |
950 | * | |
951 | * Ignores target->priority. | |
952 | * | |
953 | * 'target' may be NULL to iterate over every rule in 'cls'. */ | |
954 | void | |
955 | cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls, | |
956 | const struct cls_rule *target) | |
957 | { | |
958 | cursor->cls = cls; | |
959 | cursor->target = target; | |
960 | } | |
961 | ||
962 | /* Returns the first matching cls_rule in 'cursor''s iteration, or a null | |
963 | * pointer if there are no matches. */ | |
964 | struct cls_rule * | |
965 | cls_cursor_first(struct cls_cursor *cursor) | |
966 | { | |
967 | struct cls_table *table; | |
968 | ||
969 | for (table = classifier_first_table(cursor->cls); table; | |
970 | table = classifier_next_table(cursor->cls, table)) { | |
971 | struct cls_rule *rule = search_table(table, cursor->target); | |
972 | if (rule) { | |
973 | cursor->table = table; | |
974 | return rule; | |
975 | } | |
976 | } | |
977 | ||
978 | return NULL; | |
979 | } | |
980 | ||
981 | /* Returns the next matching cls_rule in 'cursor''s iteration, or a null | |
982 | * pointer if there are no more matches. */ | |
983 | struct cls_rule * | |
984 | cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule) | |
985 | { | |
986 | const struct cls_table *table; | |
987 | struct cls_rule *next; | |
988 | ||
955f579d BP |
989 | next = next_rule_in_list__(rule); |
990 | if (next->priority < rule->priority) { | |
5ecc9d81 BP |
991 | return next; |
992 | } | |
993 | ||
955f579d BP |
994 | /* 'next' is the head of the list, that is, the rule that is included in |
995 | * the table's hmap. (This is important when the classifier contains rules | |
996 | * that differ only in priority.) */ | |
997 | rule = next; | |
5ecc9d81 BP |
998 | HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) { |
999 | if (rule_matches(rule, cursor->target)) { | |
1000 | return rule; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | for (table = classifier_next_table(cursor->cls, cursor->table); table; | |
1005 | table = classifier_next_table(cursor->cls, table)) { | |
1006 | rule = search_table(table, cursor->target); | |
1007 | if (rule) { | |
1008 | cursor->table = table; | |
1009 | return rule; | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | return NULL; | |
1014 | } | |
1015 | \f | |
b5d97350 BP |
1016 | static struct cls_table * |
1017 | find_table(const struct classifier *cls, const struct flow_wildcards *wc) | |
1018 | { | |
1019 | struct cls_table *table; | |
064af421 | 1020 | |
1006cda6 | 1021 | HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0), |
b5d97350 BP |
1022 | &cls->tables) { |
1023 | if (flow_wildcards_equal(wc, &table->wc)) { | |
1024 | return table; | |
064af421 BP |
1025 | } |
1026 | } | |
b5d97350 | 1027 | return NULL; |
064af421 | 1028 | } |
064af421 | 1029 | |
b5d97350 BP |
1030 | static struct cls_table * |
1031 | insert_table(struct classifier *cls, const struct flow_wildcards *wc) | |
1032 | { | |
1033 | struct cls_table *table; | |
064af421 | 1034 | |
b5d97350 BP |
1035 | table = xzalloc(sizeof *table); |
1036 | hmap_init(&table->rules); | |
1037 | table->wc = *wc; | |
1006cda6 | 1038 | hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0)); |
064af421 | 1039 | |
b5d97350 | 1040 | return table; |
064af421 BP |
1041 | } |
1042 | ||
b5d97350 BP |
1043 | static struct cls_table * |
1044 | classifier_first_table(const struct classifier *cls) | |
064af421 | 1045 | { |
b5d97350 | 1046 | return cls_table_from_hmap_node(hmap_first(&cls->tables)); |
064af421 BP |
1047 | } |
1048 | ||
b5d97350 BP |
1049 | static struct cls_table * |
1050 | classifier_next_table(const struct classifier *cls, | |
1051 | const struct cls_table *table) | |
064af421 | 1052 | { |
b5d97350 BP |
1053 | return cls_table_from_hmap_node(hmap_next(&cls->tables, |
1054 | &table->hmap_node)); | |
1055 | } | |
064af421 | 1056 | |
b5d97350 BP |
1057 | static void |
1058 | destroy_table(struct classifier *cls, struct cls_table *table) | |
1059 | { | |
1060 | hmap_remove(&cls->tables, &table->hmap_node); | |
1061 | hmap_destroy(&table->rules); | |
1062 | free(table); | |
1063 | } | |
064af421 | 1064 | |
064af421 | 1065 | static struct cls_rule * |
b5d97350 BP |
1066 | find_match(const struct cls_table *table, const struct flow *flow) |
1067 | { | |
1068 | struct cls_rule *rule; | |
1069 | struct flow f; | |
1070 | ||
1071 | f = *flow; | |
993410fb | 1072 | flow_zero_wildcards(&f, &table->wc); |
b5d97350 BP |
1073 | HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0), |
1074 | &table->rules) { | |
1075 | if (flow_equal(&f, &rule->flow)) { | |
1076 | return rule; | |
064af421 BP |
1077 | } |
1078 | } | |
064af421 BP |
1079 | return NULL; |
1080 | } | |
1081 | ||
1082 | static struct cls_rule * | |
b5d97350 | 1083 | find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash) |
064af421 | 1084 | { |
b5d97350 | 1085 | struct cls_rule *head; |
064af421 | 1086 | |
b5d97350 BP |
1087 | HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) { |
1088 | if (flow_equal(&head->flow, flow)) { | |
1089 | return head; | |
064af421 BP |
1090 | } |
1091 | } | |
1092 | return NULL; | |
1093 | } | |
1094 | ||
b5d97350 BP |
1095 | static struct cls_rule * |
1096 | insert_rule(struct cls_table *table, struct cls_rule *new) | |
064af421 | 1097 | { |
b5d97350 | 1098 | struct cls_rule *head; |
064af421 | 1099 | |
b5d97350 | 1100 | new->hmap_node.hash = flow_hash(&new->flow, 0); |
064af421 | 1101 | |
b5d97350 BP |
1102 | head = find_equal(table, &new->flow, new->hmap_node.hash); |
1103 | if (!head) { | |
1104 | hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash); | |
1105 | list_init(&new->list); | |
1106 | return NULL; | |
1107 | } else { | |
1108 | /* Scan the list for the insertion point that will keep the list in | |
1109 | * order of decreasing priority. */ | |
1110 | struct cls_rule *rule; | |
1111 | FOR_EACH_RULE_IN_LIST (rule, head) { | |
1112 | if (new->priority >= rule->priority) { | |
1113 | if (rule == head) { | |
1114 | /* 'new' is the new highest-priority flow in the list. */ | |
1115 | hmap_replace(&table->rules, | |
1116 | &rule->hmap_node, &new->hmap_node); | |
1117 | } | |
064af421 | 1118 | |
b5d97350 BP |
1119 | if (new->priority == rule->priority) { |
1120 | list_replace(&new->list, &rule->list); | |
1121 | return rule; | |
1122 | } else { | |
1123 | list_insert(&rule->list, &new->list); | |
1124 | return NULL; | |
1125 | } | |
1126 | } | |
1127 | } | |
064af421 | 1128 | |
b5d97350 BP |
1129 | /* Insert 'new' at the end of the list. */ |
1130 | list_push_back(&head->list, &new->list); | |
1131 | return NULL; | |
064af421 | 1132 | } |
064af421 BP |
1133 | } |
1134 | ||
b5d97350 | 1135 | static struct cls_rule * |
955f579d | 1136 | next_rule_in_list__(struct cls_rule *rule) |
064af421 | 1137 | { |
b5d97350 | 1138 | struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list); |
955f579d BP |
1139 | return next; |
1140 | } | |
1141 | ||
1142 | static struct cls_rule * | |
1143 | next_rule_in_list(struct cls_rule *rule) | |
1144 | { | |
1145 | struct cls_rule *next = next_rule_in_list__(rule); | |
b5d97350 | 1146 | return next->priority < rule->priority ? next : NULL; |
064af421 BP |
1147 | } |
1148 | ||
d31f1109 JP |
1149 | static bool |
1150 | ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b, | |
1151 | const struct in6_addr *mask) | |
1152 | { | |
1153 | int i; | |
1154 | ||
1155 | #ifdef s6_addr32 | |
1156 | for (i=0; i<4; i++) { | |
1157 | if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) { | |
1158 | return false; | |
1159 | } | |
1160 | } | |
1161 | #else | |
1162 | for (i=0; i<16; i++) { | |
1163 | if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) { | |
1164 | return false; | |
1165 | } | |
1166 | } | |
1167 | #endif | |
1168 | ||
1169 | return true; | |
1170 | } | |
1171 | ||
1172 | ||
064af421 | 1173 | static bool |
b5d97350 BP |
1174 | flow_equal_except(const struct flow *a, const struct flow *b, |
1175 | const struct flow_wildcards *wildcards) | |
064af421 | 1176 | { |
d8ae4d67 | 1177 | const flow_wildcards_t wc = wildcards->wildcards; |
b6c9e612 | 1178 | int i; |
49bdc010 | 1179 | |
a61680c6 | 1180 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6); |
b6c9e612 BP |
1181 | |
1182 | for (i = 0; i < FLOW_N_REGS; i++) { | |
1183 | if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) { | |
1184 | return false; | |
1185 | } | |
1186 | } | |
b5d97350 | 1187 | |
8368c090 | 1188 | return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask) |
b5d97350 BP |
1189 | && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask) |
1190 | && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask) | |
d8ae4d67 | 1191 | && (wc & FWW_IN_PORT || a->in_port == b->in_port) |
66642cb4 | 1192 | && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask) |
d8ae4d67 BP |
1193 | && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type) |
1194 | && (wc & FWW_TP_SRC || a->tp_src == b->tp_src) | |
1195 | && (wc & FWW_TP_DST || a->tp_dst == b->tp_dst) | |
1196 | && (wc & FWW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src)) | |
1197 | && (wc & FWW_DL_DST | |
1e37a2d7 BP |
1198 | || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe) |
1199 | && a->dl_dst[1] == b->dl_dst[1] | |
1200 | && a->dl_dst[2] == b->dl_dst[2] | |
1201 | && a->dl_dst[3] == b->dl_dst[3] | |
1202 | && a->dl_dst[4] == b->dl_dst[4] | |
1203 | && a->dl_dst[5] == b->dl_dst[5])) | |
d8ae4d67 BP |
1204 | && (wc & FWW_ETH_MCAST |
1205 | || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01)) | |
1206 | && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto) | |
a61680c6 | 1207 | && (wc & FWW_NW_TTL || a->nw_ttl == b->nw_ttl) |
eadef313 JP |
1208 | && !((a->nw_tos ^ b->nw_tos) & wildcards->nw_tos_mask) |
1209 | && !((a->nw_frag ^ b->nw_frag) & wildcards->nw_frag_mask) | |
bad68a99 | 1210 | && (wc & FWW_ARP_SHA || eth_addr_equals(a->arp_sha, b->arp_sha)) |
d31f1109 | 1211 | && (wc & FWW_ARP_THA || eth_addr_equals(a->arp_tha, b->arp_tha)) |
fa8223b7 | 1212 | && (wc & FWW_IPV6_LABEL || a->ipv6_label == b->ipv6_label) |
d31f1109 JP |
1213 | && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src, |
1214 | &wildcards->ipv6_src_mask) | |
1215 | && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst, | |
685a51a5 | 1216 | &wildcards->ipv6_dst_mask) |
b53055f4 | 1217 | && (wc & FWW_ND_TARGET |
685a51a5 | 1218 | || ipv6_addr_equals(&a->nd_target, &b->nd_target))); |
064af421 | 1219 | } |