2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "classifier.h"
21 #include <netinet/in.h>
22 #include "dynamic-string.h"
27 static struct cls_table *find_table(const struct classifier *,
28 const struct flow_wildcards *);
29 static struct cls_table *insert_table(struct classifier *,
30 const struct flow_wildcards *);
32 static struct cls_table *classifier_first_table(const struct classifier *);
33 static struct cls_table *classifier_next_table(const struct classifier *,
34 const struct cls_table *);
35 static void destroy_table(struct classifier *, struct cls_table *);
37 static bool should_include(const struct cls_table *, int include);
39 static struct cls_rule *find_match(const struct cls_table *,
41 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
43 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
45 static bool flow_equal_except(const struct flow *, const struct flow *,
46 const struct flow_wildcards *);
47 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
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); \
57 static struct cls_rule *next_rule_in_list(struct cls_rule *);
59 static struct cls_table *
60 cls_table_from_hmap_node(const struct hmap_node *node)
62 return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
65 static struct cls_rule *
66 cls_rule_from_hmap_node(const struct hmap_node *node)
68 return node ? CONTAINER_OF(node, struct cls_rule, hmap_node) : NULL;
71 /* Returns the cls_table within 'cls' that has no wildcards, or NULL if there
74 classifier_exact_table(const struct classifier *cls)
76 struct flow_wildcards exact_wc;
77 flow_wildcards_init_exact(&exact_wc);
78 return find_table(cls, &exact_wc);
81 /* Returns the first rule in 'table', or a null pointer if 'table' is NULL. */
83 cls_table_first_rule(const struct cls_table *table)
85 return table ? cls_rule_from_hmap_node(hmap_first(&table->rules)) : NULL;
88 /* Returns the next rule in 'table' following 'rule', or a null pointer if
89 * 'rule' is the last rule in 'table'. */
91 cls_table_next_rule(const struct cls_table *table, const struct cls_rule *rule)
94 = CONTAINER_OF(rule->list.next, struct cls_rule, hmap_node);
96 return (next->priority < rule->priority
98 : cls_rule_from_hmap_node(hmap_next(&table->rules,
103 cls_rule_init__(struct cls_rule *rule,
104 const struct flow *flow, uint32_t wildcards)
107 flow_wildcards_init(&rule->wc, wildcards);
108 cls_rule_zero_wildcarded_fields(rule);
111 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
112 * 'wildcards' and 'priority'.*/
114 cls_rule_from_flow(const struct flow *flow, uint32_t wildcards,
115 unsigned int priority, struct cls_rule *rule)
117 cls_rule_init__(rule, flow, wildcards);
118 rule->priority = priority;
121 /* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
122 * into a cls_rule in 'rule', with the given 'priority'. 'cookie' is used
123 * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
125 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
126 int flow_format, uint64_t cookie,
127 struct cls_rule *rule)
132 flow_from_match(match, flow_format, cookie, &flow, &wildcards);
133 cls_rule_init__(rule, &flow, wildcards);
134 rule->priority = rule->wc.wildcards ? priority : UINT16_MAX;
137 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
138 * priority 'priority'. */
140 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
142 memset(&rule->flow, 0, sizeof rule->flow);
143 flow_wildcards_init(&rule->wc, OVSFW_ALL);
144 rule->priority = priority;
147 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
148 * field in 'flow' to all-0-bits. It is important to maintain this invariant
149 * in a clr_rule that might be inserted into a classifier.
151 * It is never necessary to call this function directly for a cls_rule that is
152 * initialized or modified only by cls_rule_*() functions. It is useful to
153 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
156 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
158 zero_wildcards(&rule->flow, &rule->wc);
161 /* Converts 'rule' to a string and returns the string. The caller must free
162 * the string (with free()). */
164 cls_rule_to_string(const struct cls_rule *rule)
166 struct ds s = DS_EMPTY_INITIALIZER;
167 ds_put_format(&s, "wildcards=%x priority=%u ",
168 rule->wc.wildcards, rule->priority);
169 flow_format(&s, &rule->flow);
173 /* Prints cls_rule 'rule', for debugging.
175 * (The output could be improved and expanded, but this was good enough to
176 * debug the classifier.) */
178 cls_rule_print(const struct cls_rule *rule)
180 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
181 flow_print(stdout, &rule->flow);
185 /* Initializes 'cls' as a classifier that initially contains no classification
188 classifier_init(struct classifier *cls)
191 hmap_init(&cls->tables);
194 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
195 * caller's responsibility. */
197 classifier_destroy(struct classifier *cls)
200 struct cls_table *table, *next_table;
202 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
203 hmap_destroy(&table->rules);
204 hmap_remove(&cls->tables, &table->hmap_node);
207 hmap_destroy(&cls->tables);
211 /* Returns true if 'cls' contains no classification rules, false otherwise. */
213 classifier_is_empty(const struct classifier *cls)
215 return cls->n_rules == 0;
218 /* Returns the number of rules in 'classifier'. */
220 classifier_count(const struct classifier *cls)
225 /* Returns the number of rules in 'classifier' that have no wildcards. */
227 classifier_count_exact(const struct classifier *cls)
229 struct cls_table *exact_table = classifier_exact_table(cls);
230 return exact_table ? exact_table->n_table_rules : 0;
233 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
234 * must not modify or free it.
236 * If 'cls' already contains an identical rule (including wildcards, values of
237 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
238 * rule that was replaced. The caller takes ownership of the returned rule and
239 * is thus responsible for freeing it, etc., as necessary.
241 * Returns NULL if 'cls' does not contain a rule with an identical key, after
242 * inserting the new rule. In this case, no rules are displaced by the new
243 * rule, even rules that cannot have any effect because the new rule matches a
244 * superset of their flows and has higher priority. */
246 classifier_insert(struct classifier *cls, struct cls_rule *rule)
248 struct cls_rule *old_rule;
249 struct cls_table *table;
251 table = find_table(cls, &rule->wc);
253 table = insert_table(cls, &rule->wc);
256 old_rule = insert_rule(table, rule);
258 table->n_table_rules++;
264 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
265 * 'rule', if this is desirable. */
267 classifier_remove(struct classifier *cls, struct cls_rule *rule)
269 struct cls_rule *head;
270 struct cls_table *table;
272 table = find_table(cls, &rule->wc);
273 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
275 list_remove(&rule->list);
276 } else if (list_is_empty(&rule->list)) {
277 hmap_remove(&table->rules, &rule->hmap_node);
279 struct cls_rule *next = CONTAINER_OF(rule->list.next,
280 struct cls_rule, list);
282 list_remove(&rule->list);
283 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
286 if (--table->n_table_rules == 0 && !table->n_refs) {
287 destroy_table(cls, table);
293 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
294 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
295 * of equal priority match 'flow', returns one arbitrarily.
297 * 'include' is a combination of CLS_INC_* values that specify tables to
298 * include in the search. */
300 classifier_lookup(const struct classifier *cls, const struct flow *flow,
303 struct cls_table *table;
304 struct cls_rule *best;
307 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
308 if (should_include(table, include)) {
309 struct cls_rule *rule = find_match(table, flow);
310 if (rule && (!best || rule->priority > best->priority)) {
318 /* Finds and returns a rule in 'cls' with exactly the same priority and
319 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
320 * contain an exact match.
322 * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
323 * treats exact-match rules as highest priority). */
325 classifier_find_rule_exactly(const struct classifier *cls,
326 const struct cls_rule *target)
328 struct cls_rule *head, *rule;
329 struct cls_table *table;
331 table = find_table(cls, &target->wc);
336 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
337 if (!target->wc.wildcards) {
340 FOR_EACH_RULE_IN_LIST (rule, head) {
341 if (target->priority >= rule->priority) {
342 return target->priority == rule->priority ? rule : NULL;
348 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
349 * considered to overlap if both rules have the same priority and a packet
350 * could match both. */
352 classifier_rule_overlaps(const struct classifier *cls,
353 const struct cls_rule *target)
355 struct cls_table *table;
357 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
358 struct flow_wildcards wc;
359 struct cls_rule *head;
361 flow_wildcards_combine(&wc, &target->wc, &table->wc);
362 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
363 struct cls_rule *rule;
365 FOR_EACH_RULE_IN_LIST (rule, head) {
366 if (rule->priority == target->priority
367 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
377 /* Searches 'cls' for rules that exactly match 'target' or are more specific
378 * than 'target'. That is, a given 'rule' matches 'target' if, for every
381 * - 'target' and 'rule' specify the same (non-wildcarded) value for the
384 * - 'target' wildcards the field,
388 * - 'target' and 'rule' specify different values for the field, or
390 * - 'target' specifies a value for the field but 'rule' wildcards it.
392 * Equivalently, the truth table for whether a field matches is:
397 * +---------+---------+
398 * t wild | yes | yes |
400 * r +---------+---------+
401 * g exact | no |if values|
403 * t +---------+---------+
405 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
406 * commands and by OpenFlow 1.0 aggregate and flow stats.
408 * Ignores target->priority.
410 * 'callback' is allowed to delete the rule that is passed as its argument, but
411 * it must not delete (or move) any other rules in 'cls' that have the same
412 * wildcards as the argument rule. */
414 classifier_for_each_match(const struct classifier *cls_,
415 const struct cls_rule *target,
416 int include, cls_cb_func *callback, void *aux)
418 struct classifier *cls = (struct classifier *) cls_;
419 struct cls_table *table, *next_table;
421 for (table = classifier_first_table(cls); table; table = next_table) {
422 if (should_include(table, include)
423 && !flow_wildcards_has_extra(&table->wc, &target->wc)) {
424 /* We have eliminated the "no" case in the truth table above. Two
425 * of the three remaining cases are trivial. We only need to check
426 * the fourth case, where both 'rule' and 'target' require an exact
428 struct cls_rule *head, *next_head;
431 HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
432 if (flow_equal_except(&head->flow, &target->flow,
434 struct cls_rule *rule, *next_rule;
436 FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
441 next_table = classifier_next_table(cls, table);
442 if (!--table->n_refs && !table->n_table_rules) {
443 destroy_table(cls, table);
446 next_table = classifier_next_table(cls, table);
451 /* 'callback' is allowed to delete the rule that is passed as its argument, but
452 * it must not delete (or move) any other rules in 'cls' that have the same
453 * wildcards as the argument rule.
455 * If 'include' is CLS_INC_EXACT then CLASSIFIER_FOR_EACH_EXACT_RULE is
456 * probably easier to use. */
458 classifier_for_each(const struct classifier *cls_, int include,
459 cls_cb_func *callback, void *aux)
461 struct classifier *cls = (struct classifier *) cls_;
462 struct cls_table *table, *next_table;
464 for (table = classifier_first_table(cls); table; table = next_table) {
465 if (should_include(table, include)) {
466 struct cls_rule *head, *next_head;
469 HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
470 struct cls_rule *rule, *next_rule;
472 FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
476 next_table = classifier_next_table(cls, table);
477 if (!--table->n_refs && !table->n_table_rules) {
478 destroy_table(cls, table);
481 next_table = classifier_next_table(cls, table);
486 static struct cls_table *
487 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
489 struct cls_table *table;
491 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
493 if (flow_wildcards_equal(wc, &table->wc)) {
500 static struct cls_table *
501 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
503 struct cls_table *table;
505 table = xzalloc(sizeof *table);
506 hmap_init(&table->rules);
508 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
513 static struct cls_table *
514 classifier_first_table(const struct classifier *cls)
516 return cls_table_from_hmap_node(hmap_first(&cls->tables));
519 static struct cls_table *
520 classifier_next_table(const struct classifier *cls,
521 const struct cls_table *table)
523 return cls_table_from_hmap_node(hmap_next(&cls->tables,
528 destroy_table(struct classifier *cls, struct cls_table *table)
530 hmap_remove(&cls->tables, &table->hmap_node);
531 hmap_destroy(&table->rules);
535 /* Returns true if 'table' should be included by an operation with the
536 * specified 'include' (a combination of CLS_INC_*). */
538 should_include(const struct cls_table *table, int include)
540 return include & (table->wc.wildcards ? CLS_INC_WILD : CLS_INC_EXACT);
543 static struct cls_rule *
544 find_match(const struct cls_table *table, const struct flow *flow)
546 struct cls_rule *rule;
550 zero_wildcards(&f, &table->wc);
551 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
553 if (flow_equal(&f, &rule->flow)) {
560 static struct cls_rule *
561 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
563 struct cls_rule *head;
565 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
566 if (flow_equal(&head->flow, flow)) {
573 static struct cls_rule *
574 insert_rule(struct cls_table *table, struct cls_rule *new)
576 struct cls_rule *head;
578 new->hmap_node.hash = flow_hash(&new->flow, 0);
580 head = find_equal(table, &new->flow, new->hmap_node.hash);
582 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
583 list_init(&new->list);
586 /* Scan the list for the insertion point that will keep the list in
587 * order of decreasing priority. */
588 struct cls_rule *rule;
589 FOR_EACH_RULE_IN_LIST (rule, head) {
590 if (new->priority >= rule->priority) {
592 /* 'new' is the new highest-priority flow in the list. */
593 hmap_replace(&table->rules,
594 &rule->hmap_node, &new->hmap_node);
597 if (new->priority == rule->priority) {
598 list_replace(&new->list, &rule->list);
601 list_insert(&rule->list, &new->list);
607 /* Insert 'new' at the end of the list. */
608 list_push_back(&head->list, &new->list);
613 static struct cls_rule *
614 next_rule_in_list(struct cls_rule *rule)
616 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
617 return next->priority < rule->priority ? next : NULL;
621 flow_equal_except(const struct flow *a, const struct flow *b,
622 const struct flow_wildcards *wildcards)
624 const uint32_t wc = wildcards->wildcards;
626 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37);
628 return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
629 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
630 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
631 && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
632 && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
633 && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
634 && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
635 && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
636 && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
637 && (wc & OFPFW_DL_DST || eth_addr_equals(a->dl_dst, b->dl_dst))
638 && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
639 && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
640 && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
644 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
646 const uint32_t wc = wildcards->wildcards;
648 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37);
650 if (wc & NXFW_TUN_ID) {
653 flow->nw_src &= wildcards->nw_src_mask;
654 flow->nw_dst &= wildcards->nw_dst_mask;
655 if (wc & OFPFW_IN_PORT) {
658 if (wc & OFPFW_DL_VLAN) {
661 if (wc & OFPFW_DL_TYPE) {
664 if (wc & OFPFW_TP_SRC) {
667 if (wc & OFPFW_TP_DST) {
670 if (wc & OFPFW_DL_SRC) {
671 memset(flow->dl_src, 0, sizeof flow->dl_src);
673 if (wc & OFPFW_DL_DST) {
674 memset(flow->dl_dst, 0, sizeof flow->dl_dst);
676 if (wc & OFPFW_NW_PROTO) {
679 if (wc & OFPFW_DL_VLAN_PCP) {
680 flow->dl_vlan_pcp = 0;
682 if (wc & OFPFW_NW_TOS) {