]> git.proxmox.com Git - mirror_ovs.git/blame - lib/classifier.c
classifier: Fix segfault iterating with rules that differ only in priority.
[mirror_ovs.git] / lib / classifier.c
CommitLineData
064af421 1/*
63e60b86 2 * Copyright (c) 2009, 2010 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>
68d1c8c3 22#include "dynamic-string.h"
064af421
BP
23#include "flow.h"
24#include "hash.h"
b5d97350 25#include "packets.h"
064af421 26
b5d97350
BP
27static struct cls_table *find_table(const struct classifier *,
28 const struct flow_wildcards *);
29static struct cls_table *insert_table(struct classifier *,
30 const struct flow_wildcards *);
31
32static struct cls_table *classifier_first_table(const struct classifier *);
33static struct cls_table *classifier_next_table(const struct classifier *,
34 const struct cls_table *);
35static void destroy_table(struct classifier *, struct cls_table *);
36
b5d97350
BP
37static struct cls_rule *find_match(const struct cls_table *,
38 const struct flow *);
39static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
40 uint32_t hash);
41static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
42
43static bool flow_equal_except(const struct flow *, const struct flow *,
44 const struct flow_wildcards *);
45static void zero_wildcards(struct flow *, const struct flow_wildcards *);
46
47/* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
48#define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
49 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
50#define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
51 for ((RULE) = (HEAD); \
52 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
53 (RULE) = (NEXT))
54
955f579d 55static struct cls_rule *next_rule_in_list__(struct cls_rule *);
b5d97350
BP
56static struct cls_rule *next_rule_in_list(struct cls_rule *);
57
58static struct cls_table *
59cls_table_from_hmap_node(const struct hmap_node *node)
60{
61 return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
62}
63
b63f2ea7
BP
64/* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
65 * 'wildcards' and 'priority'. */
66void
67cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
68 unsigned int priority, struct cls_rule *rule)
b5d97350
BP
69{
70 rule->flow = *flow;
b63f2ea7
BP
71 rule->wc = *wildcards;
72 rule->priority = priority;
52ce26ee 73 cls_rule_zero_wildcarded_fields(rule);
b5d97350 74}
064af421 75
b63f2ea7
BP
76/* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
77 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
78 * priority, so 'priority' should be at least 65535.) */
064af421 79void
b63f2ea7
BP
80cls_rule_init_exact(const struct flow *flow,
81 unsigned int priority, struct cls_rule *rule)
064af421 82{
b63f2ea7
BP
83 rule->flow = *flow;
84 flow_wildcards_init_exact(&rule->wc);
064af421 85 rule->priority = priority;
064af421
BP
86}
87
f9bfea14
BP
88/* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
89 * into a cls_rule in 'rule', with the given 'priority'. 'cookie' is used
90 * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
064af421 91void
659586ef 92cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
f9bfea14 93 int flow_format, uint64_t cookie,
659586ef 94 struct cls_rule *rule)
064af421 95{
b63f2ea7
BP
96 flow_from_match(match, flow_format, cookie, &rule->flow, &rule->wc);
97 rule->priority = !rule->wc.wildcards ? UINT16_MAX : priority;
98 cls_rule_zero_wildcarded_fields(rule);
b5d97350
BP
99}
100
cf3fad8a
BP
101/* Initializes 'rule' as a "catch-all" rule that matches every packet, with
102 * priority 'priority'. */
103void
104cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
105{
106 memset(&rule->flow, 0, sizeof rule->flow);
1e37a2d7 107 flow_wildcards_init(&rule->wc, OVSFW_ALL | FWW_ALL);
cf3fad8a
BP
108 rule->priority = priority;
109}
110
b5d97350
BP
111/* For each bit or field wildcarded in 'rule', sets the corresponding bit or
112 * field in 'flow' to all-0-bits. It is important to maintain this invariant
113 * in a clr_rule that might be inserted into a classifier.
114 *
115 * It is never necessary to call this function directly for a cls_rule that is
116 * initialized or modified only by cls_rule_*() functions. It is useful to
117 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
118 */
119void
52ce26ee 120cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
b5d97350
BP
121{
122 zero_wildcards(&rule->flow, &rule->wc);
064af421
BP
123}
124
64420dfa
BP
125void
126cls_rule_set_in_port(struct cls_rule *rule, uint16_t odp_port)
127{
128 rule->wc.wildcards &= ~OFPFW_IN_PORT;
129 rule->flow.in_port = odp_port;
130}
131
132void
133cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
134{
135 rule->wc.wildcards &= ~OFPFW_DL_TYPE;
136 rule->flow.dl_type = dl_type;
137}
138
139void
140cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
141{
142 rule->wc.wildcards &= ~OFPFW_DL_SRC;
143 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
144}
145
146void
147cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
148{
1e37a2d7 149 rule->wc.wildcards &= ~(OFPFW_DL_DST | FWW_ETH_MCAST);
64420dfa
BP
150 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
151}
152
81c9dad2
BP
153bool
154cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
155{
156 return cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
157}
158
159bool
160cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
161{
162 switch (ntohs(mask)) {
163 case 0xffff:
164 if (tci == htons(0)) {
165 /* Match only packets that have no 802.1Q header. */
166 rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
167 rule->flow.dl_vlan = htons(OFP_VLAN_NONE);
168 rule->flow.dl_vlan_pcp = 0;
169 return true;
170 } else if (tci & htons(VLAN_CFI)) {
171 /* Match only packets that have a specific 802.1Q VID and PCP. */
172 rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
173 rule->flow.dl_vlan = htons(vlan_tci_to_vid(tci));
174 rule->flow.dl_vlan_pcp = vlan_tci_to_pcp(tci);
175 return true;
176 } else {
177 /* Impossible. */
178 return false;
179 }
180
181 case 0x1fff:
182 if (!(tci & htons(VLAN_CFI))) {
183 return false;
184 } else {
185 /* Match only packets that have a specific 802.1Q VID. */
186 cls_rule_set_dl_vlan(rule, tci & htons(VLAN_VID_MASK));
187 rule->wc.wildcards |= OFPFW_DL_VLAN_PCP;
188 rule->flow.dl_vlan_pcp = 0;
189 return true;
190 }
191
192 case 0xf000:
193 if (!(tci & htons(VLAN_CFI))) {
194 return false;
195 } else {
196 /* Match only packets that have a specific 802.1Q PCP. */
197 cls_rule_set_dl_vlan_pcp(rule, vlan_tci_to_pcp(tci));
198 rule->wc.wildcards |= OFPFW_DL_VLAN;
199 rule->flow.dl_vlan = 0;
200 return true;
201 }
202
203 case 0x0000:
204 /* Match anything. */
205 rule->wc.wildcards |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
206 rule->flow.dl_vlan = htons(0);
207 rule->flow.dl_vlan_pcp = 0;
208 return true;
209
210 default:
211 return false;
212 }
213}
214
215void
216cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
217{
218 if (dl_vlan != htons(OFP_VLAN_NONE)) {
219 dl_vlan &= htons(VLAN_VID_MASK);
220 }
221
222 rule->wc.wildcards &= ~OFPFW_DL_VLAN;
223 rule->flow.dl_vlan = dl_vlan;
224}
225
226void
227cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
228{
229 rule->wc.wildcards &= ~OFPFW_DL_VLAN_PCP;
230 rule->flow.dl_vlan_pcp = dl_vlan_pcp & 0x07;
231}
232
64420dfa
BP
233void
234cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
235{
236 rule->wc.wildcards &= ~OFPFW_TP_SRC;
237 rule->flow.tp_src = tp_src;
238}
239
240void
241cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
242{
243 rule->wc.wildcards &= ~OFPFW_TP_DST;
244 rule->flow.tp_dst = tp_dst;
245}
246
247void
248cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
249{
250 rule->wc.wildcards &= ~OFPFW_NW_PROTO;
251 rule->flow.nw_proto = nw_proto;
252}
253
254void
255cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
256{
81c9dad2
BP
257 cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
258}
259
260bool
261cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
262{
263 if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
264 rule->flow.nw_src = ip & mask;
265 return true;
266 } else {
267 return false;
268 }
64420dfa
BP
269}
270
271void
272cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
273{
81c9dad2
BP
274 cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
275}
276
277bool
278cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
279{
280 if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
281 rule->flow.nw_dst = ip & mask;
282 return true;
283 } else {
284 return false;
285 }
286}
287
288void
289cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
290{
291 rule->wc.wildcards &= ~OFPFW_NW_TOS;
292 rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
293}
294
295void
296cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
297{
298 rule->wc.wildcards &= ~OFPFW_ICMP_TYPE;
299 rule->flow.icmp_type = htons(icmp_type);
300
301}
302
303void
304cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
305{
306 rule->wc.wildcards &= ~OFPFW_ICMP_CODE;
307 rule->flow.icmp_code = htons(icmp_code);
64420dfa
BP
308}
309
68d1c8c3
BP
310/* Converts 'rule' to a string and returns the string. The caller must free
311 * the string (with free()). */
312char *
313cls_rule_to_string(const struct cls_rule *rule)
314{
315 struct ds s = DS_EMPTY_INITIALIZER;
316 ds_put_format(&s, "wildcards=%x priority=%u ",
317 rule->wc.wildcards, rule->priority);
318 flow_format(&s, &rule->flow);
319 return ds_cstr(&s);
320}
321
064af421
BP
322/* Prints cls_rule 'rule', for debugging.
323 *
324 * (The output could be improved and expanded, but this was good enough to
325 * debug the classifier.) */
326void
327cls_rule_print(const struct cls_rule *rule)
328{
329 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
330 flow_print(stdout, &rule->flow);
331 putc('\n', stdout);
332}
064af421
BP
333\f
334/* Initializes 'cls' as a classifier that initially contains no classification
335 * rules. */
336void
337classifier_init(struct classifier *cls)
338{
064af421 339 cls->n_rules = 0;
b5d97350 340 hmap_init(&cls->tables);
064af421
BP
341}
342
343/* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
344 * caller's responsibility. */
345void
346classifier_destroy(struct classifier *cls)
347{
348 if (cls) {
b5d97350 349 struct cls_table *table, *next_table;
064af421 350
b5d97350
BP
351 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
352 hmap_destroy(&table->rules);
353 hmap_remove(&cls->tables, &table->hmap_node);
354 free(table);
064af421 355 }
b5d97350 356 hmap_destroy(&cls->tables);
064af421
BP
357 }
358}
359
b5d97350 360/* Returns true if 'cls' contains no classification rules, false otherwise. */
064af421
BP
361bool
362classifier_is_empty(const struct classifier *cls)
363{
364 return cls->n_rules == 0;
365}
366
367/* Returns the number of rules in 'classifier'. */
368int
369classifier_count(const struct classifier *cls)
370{
371 return cls->n_rules;
372}
373
b5d97350
BP
374/* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
375 * must not modify or free it.
064af421
BP
376 *
377 * If 'cls' already contains an identical rule (including wildcards, values of
378 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
379 * rule that was replaced. The caller takes ownership of the returned rule and
380 * is thus responsible for freeing it, etc., as necessary.
381 *
382 * Returns NULL if 'cls' does not contain a rule with an identical key, after
383 * inserting the new rule. In this case, no rules are displaced by the new
384 * rule, even rules that cannot have any effect because the new rule matches a
385 * superset of their flows and has higher priority. */
386struct cls_rule *
387classifier_insert(struct classifier *cls, struct cls_rule *rule)
388{
b5d97350
BP
389 struct cls_rule *old_rule;
390 struct cls_table *table;
391
392 table = find_table(cls, &rule->wc);
393 if (!table) {
394 table = insert_table(cls, &rule->wc);
395 }
396
397 old_rule = insert_rule(table, rule);
398 if (!old_rule) {
399 table->n_table_rules++;
064af421
BP
400 cls->n_rules++;
401 }
b5d97350 402 return old_rule;
064af421
BP
403}
404
b5d97350
BP
405/* Removes 'rule' from 'cls'. It is the caller's responsibility to free
406 * 'rule', if this is desirable. */
064af421
BP
407void
408classifier_remove(struct classifier *cls, struct cls_rule *rule)
409{
b5d97350
BP
410 struct cls_rule *head;
411 struct cls_table *table;
064af421 412
b5d97350
BP
413 table = find_table(cls, &rule->wc);
414 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
415 if (head != rule) {
416 list_remove(&rule->list);
417 } else if (list_is_empty(&rule->list)) {
418 hmap_remove(&table->rules, &rule->hmap_node);
419 } else {
420 struct cls_rule *next = CONTAINER_OF(rule->list.next,
421 struct cls_rule, list);
064af421 422
b5d97350
BP
423 list_remove(&rule->list);
424 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
425 }
064af421 426
f6acdb44 427 if (--table->n_table_rules == 0) {
b5d97350 428 destroy_table(cls, table);
064af421 429 }
b5d97350
BP
430
431 cls->n_rules--;
064af421
BP
432}
433
48c3de13
BP
434/* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
435 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
3c4486a5 436 * of equal priority match 'flow', returns one arbitrarily. */
48c3de13 437struct cls_rule *
3c4486a5 438classifier_lookup(const struct classifier *cls, const struct flow *flow)
48c3de13 439{
b5d97350
BP
440 struct cls_table *table;
441 struct cls_rule *best;
48c3de13 442
b5d97350
BP
443 best = NULL;
444 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
3c4486a5
BP
445 struct cls_rule *rule = find_match(table, flow);
446 if (rule && (!best || rule->priority > best->priority)) {
447 best = rule;
b5d97350 448 }
48c3de13 449 }
b5d97350 450 return best;
48c3de13
BP
451}
452
b5d97350
BP
453/* Finds and returns a rule in 'cls' with exactly the same priority and
454 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
455 * contain an exact match.
456 *
457 * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
458 * treats exact-match rules as highest priority). */
064af421
BP
459struct cls_rule *
460classifier_find_rule_exactly(const struct classifier *cls,
76ecc721 461 const struct cls_rule *target)
064af421 462{
b5d97350
BP
463 struct cls_rule *head, *rule;
464 struct cls_table *table;
064af421 465
b5d97350
BP
466 table = find_table(cls, &target->wc);
467 if (!table) {
468 return NULL;
064af421
BP
469 }
470
b5d97350
BP
471 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
472 if (!target->wc.wildcards) {
473 return head;
474 }
475 FOR_EACH_RULE_IN_LIST (rule, head) {
476 if (target->priority >= rule->priority) {
477 return target->priority == rule->priority ? rule : NULL;
064af421
BP
478 }
479 }
480 return NULL;
481}
482
faa50f40
BP
483/* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
484 * considered to overlap if both rules have the same priority and a packet
485 * could match both. */
49bdc010
JP
486bool
487classifier_rule_overlaps(const struct classifier *cls,
faa50f40 488 const struct cls_rule *target)
49bdc010 489{
b5d97350 490 struct cls_table *table;
49bdc010 491
b5d97350
BP
492 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
493 struct flow_wildcards wc;
494 struct cls_rule *head;
49bdc010 495
b5d97350
BP
496 flow_wildcards_combine(&wc, &target->wc, &table->wc);
497 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
49bdc010
JP
498 struct cls_rule *rule;
499
b5d97350 500 FOR_EACH_RULE_IN_LIST (rule, head) {
faa50f40 501 if (rule->priority == target->priority
b5d97350 502 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
49bdc010
JP
503 return true;
504 }
505 }
506 }
507 }
508
509 return false;
510}
b5d97350 511\f
5ecc9d81
BP
512/* Iteration. */
513
514static bool
515rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
516{
517 return (!target
518 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
519}
520
521static struct cls_rule *
522search_table(const struct cls_table *table, const struct cls_rule *target)
523{
524 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
525 struct cls_rule *rule;
526
527 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
528 if (rule_matches(rule, target)) {
529 return rule;
530 }
531 }
532 }
533 return NULL;
534}
535
536/* Initializes 'cursor' for iterating through 'cls' rules that exactly match
537 * 'target' or are more specific than 'target'. That is, a given 'rule'
538 * matches 'target' if, for every field:
539 *
540 * - 'target' and 'rule' specify the same (non-wildcarded) value for the
541 * field, or
542 *
543 * - 'target' wildcards the field,
544 *
545 * but not if:
546 *
547 * - 'target' and 'rule' specify different values for the field, or
548 *
549 * - 'target' specifies a value for the field but 'rule' wildcards it.
550 *
551 * Equivalently, the truth table for whether a field matches is:
552 *
553 * rule
554 *
555 * wildcard exact
556 * +---------+---------+
557 * t wild | yes | yes |
558 * a card | | |
559 * r +---------+---------+
560 * g exact | no |if values|
561 * e | |are equal|
562 * t +---------+---------+
563 *
564 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
565 * commands and by OpenFlow 1.0 aggregate and flow stats.
566 *
567 * Ignores target->priority.
568 *
569 * 'target' may be NULL to iterate over every rule in 'cls'. */
570void
571cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
572 const struct cls_rule *target)
573{
574 cursor->cls = cls;
575 cursor->target = target;
576}
577
578/* Returns the first matching cls_rule in 'cursor''s iteration, or a null
579 * pointer if there are no matches. */
580struct cls_rule *
581cls_cursor_first(struct cls_cursor *cursor)
582{
583 struct cls_table *table;
584
585 for (table = classifier_first_table(cursor->cls); table;
586 table = classifier_next_table(cursor->cls, table)) {
587 struct cls_rule *rule = search_table(table, cursor->target);
588 if (rule) {
589 cursor->table = table;
590 return rule;
591 }
592 }
593
594 return NULL;
595}
596
597/* Returns the next matching cls_rule in 'cursor''s iteration, or a null
598 * pointer if there are no more matches. */
599struct cls_rule *
600cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
601{
602 const struct cls_table *table;
603 struct cls_rule *next;
604
955f579d
BP
605 next = next_rule_in_list__(rule);
606 if (next->priority < rule->priority) {
5ecc9d81
BP
607 return next;
608 }
609
955f579d
BP
610 /* 'next' is the head of the list, that is, the rule that is included in
611 * the table's hmap. (This is important when the classifier contains rules
612 * that differ only in priority.) */
613 rule = next;
5ecc9d81
BP
614 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
615 if (rule_matches(rule, cursor->target)) {
616 return rule;
617 }
618 }
619
620 for (table = classifier_next_table(cursor->cls, cursor->table); table;
621 table = classifier_next_table(cursor->cls, table)) {
622 rule = search_table(table, cursor->target);
623 if (rule) {
624 cursor->table = table;
625 return rule;
626 }
627 }
628
629 return NULL;
630}
631\f
b5d97350
BP
632static struct cls_table *
633find_table(const struct classifier *cls, const struct flow_wildcards *wc)
634{
635 struct cls_table *table;
064af421 636
b5d97350
BP
637 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
638 &cls->tables) {
639 if (flow_wildcards_equal(wc, &table->wc)) {
640 return table;
064af421
BP
641 }
642 }
b5d97350 643 return NULL;
064af421 644}
064af421 645
b5d97350
BP
646static struct cls_table *
647insert_table(struct classifier *cls, const struct flow_wildcards *wc)
648{
649 struct cls_table *table;
064af421 650
b5d97350
BP
651 table = xzalloc(sizeof *table);
652 hmap_init(&table->rules);
653 table->wc = *wc;
654 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
064af421 655
b5d97350 656 return table;
064af421
BP
657}
658
b5d97350
BP
659static struct cls_table *
660classifier_first_table(const struct classifier *cls)
064af421 661{
b5d97350 662 return cls_table_from_hmap_node(hmap_first(&cls->tables));
064af421
BP
663}
664
b5d97350
BP
665static struct cls_table *
666classifier_next_table(const struct classifier *cls,
667 const struct cls_table *table)
064af421 668{
b5d97350
BP
669 return cls_table_from_hmap_node(hmap_next(&cls->tables,
670 &table->hmap_node));
671}
064af421 672
b5d97350
BP
673static void
674destroy_table(struct classifier *cls, struct cls_table *table)
675{
676 hmap_remove(&cls->tables, &table->hmap_node);
677 hmap_destroy(&table->rules);
678 free(table);
679}
064af421 680
064af421 681static struct cls_rule *
b5d97350
BP
682find_match(const struct cls_table *table, const struct flow *flow)
683{
684 struct cls_rule *rule;
685 struct flow f;
686
687 f = *flow;
688 zero_wildcards(&f, &table->wc);
689 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
690 &table->rules) {
691 if (flow_equal(&f, &rule->flow)) {
692 return rule;
064af421
BP
693 }
694 }
064af421
BP
695 return NULL;
696}
697
698static struct cls_rule *
b5d97350 699find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
064af421 700{
b5d97350 701 struct cls_rule *head;
064af421 702
b5d97350
BP
703 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
704 if (flow_equal(&head->flow, flow)) {
705 return head;
064af421
BP
706 }
707 }
708 return NULL;
709}
710
b5d97350
BP
711static struct cls_rule *
712insert_rule(struct cls_table *table, struct cls_rule *new)
064af421 713{
b5d97350 714 struct cls_rule *head;
064af421 715
b5d97350 716 new->hmap_node.hash = flow_hash(&new->flow, 0);
064af421 717
b5d97350
BP
718 head = find_equal(table, &new->flow, new->hmap_node.hash);
719 if (!head) {
720 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
721 list_init(&new->list);
722 return NULL;
723 } else {
724 /* Scan the list for the insertion point that will keep the list in
725 * order of decreasing priority. */
726 struct cls_rule *rule;
727 FOR_EACH_RULE_IN_LIST (rule, head) {
728 if (new->priority >= rule->priority) {
729 if (rule == head) {
730 /* 'new' is the new highest-priority flow in the list. */
731 hmap_replace(&table->rules,
732 &rule->hmap_node, &new->hmap_node);
733 }
064af421 734
b5d97350
BP
735 if (new->priority == rule->priority) {
736 list_replace(&new->list, &rule->list);
737 return rule;
738 } else {
739 list_insert(&rule->list, &new->list);
740 return NULL;
741 }
742 }
743 }
064af421 744
b5d97350
BP
745 /* Insert 'new' at the end of the list. */
746 list_push_back(&head->list, &new->list);
747 return NULL;
064af421 748 }
064af421
BP
749}
750
b5d97350 751static struct cls_rule *
955f579d 752next_rule_in_list__(struct cls_rule *rule)
064af421 753{
b5d97350 754 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
955f579d
BP
755 return next;
756}
757
758static struct cls_rule *
759next_rule_in_list(struct cls_rule *rule)
760{
761 struct cls_rule *next = next_rule_in_list__(rule);
b5d97350 762 return next->priority < rule->priority ? next : NULL;
064af421
BP
763}
764
064af421 765static bool
b5d97350
BP
766flow_equal_except(const struct flow *a, const struct flow *b,
767 const struct flow_wildcards *wildcards)
064af421 768{
b5d97350 769 const uint32_t wc = wildcards->wildcards;
b6c9e612 770 int i;
49bdc010 771
b6c9e612
BP
772 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
773
774 for (i = 0; i < FLOW_N_REGS; i++) {
775 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
776 return false;
777 }
778 }
b5d97350
BP
779
780 return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
781 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
782 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
783 && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
784 && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
785 && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
786 && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
787 && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
788 && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
1e37a2d7
BP
789 && (wc & OFPFW_DL_DST
790 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
791 && a->dl_dst[1] == b->dl_dst[1]
792 && a->dl_dst[2] == b->dl_dst[2]
793 && a->dl_dst[3] == b->dl_dst[3]
794 && a->dl_dst[4] == b->dl_dst[4]
795 && a->dl_dst[5] == b->dl_dst[5]))
796 && (wc & FWW_ETH_MCAST || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
b5d97350
BP
797 && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
798 && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
799 && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
064af421
BP
800}
801
b5d97350
BP
802static void
803zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
064af421 804{
b5d97350 805 const uint32_t wc = wildcards->wildcards;
b6c9e612 806 int i;
064af421 807
b6c9e612 808 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
064af421 809
b6c9e612
BP
810 for (i = 0; i < FLOW_N_REGS; i++) {
811 flow->regs[i] &= wildcards->reg_masks[i];
812 }
b5d97350
BP
813 if (wc & NXFW_TUN_ID) {
814 flow->tun_id = 0;
064af421 815 }
b5d97350
BP
816 flow->nw_src &= wildcards->nw_src_mask;
817 flow->nw_dst &= wildcards->nw_dst_mask;
818 if (wc & OFPFW_IN_PORT) {
819 flow->in_port = 0;
064af421 820 }
b5d97350
BP
821 if (wc & OFPFW_DL_VLAN) {
822 flow->dl_vlan = 0;
064af421 823 }
b5d97350
BP
824 if (wc & OFPFW_DL_TYPE) {
825 flow->dl_type = 0;
826 }
827 if (wc & OFPFW_TP_SRC) {
828 flow->tp_src = 0;
829 }
830 if (wc & OFPFW_TP_DST) {
831 flow->tp_dst = 0;
832 }
833 if (wc & OFPFW_DL_SRC) {
834 memset(flow->dl_src, 0, sizeof flow->dl_src);
835 }
836 if (wc & OFPFW_DL_DST) {
1e37a2d7
BP
837 flow->dl_dst[0] &= 0x01;
838 memset(&flow->dl_dst[1], 0, 5);
839 }
840 if (wc & FWW_ETH_MCAST) {
841 flow->dl_dst[0] &= 0xfe;
b5d97350
BP
842 }
843 if (wc & OFPFW_NW_PROTO) {
844 flow->nw_proto = 0;
845 }
846 if (wc & OFPFW_DL_VLAN_PCP) {
847 flow->dl_vlan_pcp = 0;
848 }
849 if (wc & OFPFW_NW_TOS) {
850 flow->nw_tos = 0;
064af421 851 }
064af421 852}