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 "byte-order.h"
23 #include "dynamic-string.h"
28 static struct cls_table *find_table(const struct classifier *,
29 const struct flow_wildcards *);
30 static struct cls_table *insert_table(struct classifier *,
31 const struct flow_wildcards *);
33 static struct cls_table *classifier_first_table(const struct classifier *);
34 static struct cls_table *classifier_next_table(const struct classifier *,
35 const struct cls_table *);
36 static void destroy_table(struct classifier *, struct cls_table *);
38 static struct cls_rule *find_match(const struct cls_table *,
40 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
42 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
44 static bool flow_equal_except(const struct flow *, const struct flow *,
45 const struct flow_wildcards *);
46 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
48 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
49 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
50 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
51 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
52 for ((RULE) = (HEAD); \
53 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
56 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
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 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
66 * 'wildcards' and 'priority'. */
68 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
69 unsigned int priority, struct cls_rule *rule)
72 rule->wc = *wildcards;
73 rule->priority = priority;
74 cls_rule_zero_wildcarded_fields(rule);
77 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
78 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
79 * priority, so 'priority' should be at least 65535.) */
81 cls_rule_init_exact(const struct flow *flow,
82 unsigned int priority, struct cls_rule *rule)
85 flow_wildcards_init_exact(&rule->wc);
86 rule->priority = priority;
89 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
90 * priority 'priority'. */
92 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
94 memset(&rule->flow, 0, sizeof rule->flow);
95 flow_wildcards_init(&rule->wc, OVSFW_ALL | FWW_ALL);
96 rule->priority = priority;
99 /* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
100 * into a cls_rule in 'rule', with the given 'priority'. 'cookie' is used
101 * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
103 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
104 int flow_format, uint64_t cookie,
105 struct cls_rule *rule)
107 uint32_t wildcards = ntohl(match->wildcards) & OVSFW_ALL;
109 rule->priority = !wildcards ? UINT16_MAX : priority;
111 rule->flow.tun_id = 0;
112 if (flow_format != NXFF_TUN_ID_FROM_COOKIE) {
113 wildcards |= NXFW_TUN_ID;
115 if (!(wildcards & NXFW_TUN_ID)) {
116 rule->flow.tun_id = htonl(ntohll(cookie) >> 32);
119 if (wildcards & OFPFW_DL_DST) {
120 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
121 * internally to OVS it excludes the multicast bit, which has to be set
122 * separately with FWW_ETH_MCAST. */
123 wildcards |= FWW_ETH_MCAST;
125 flow_wildcards_init(&rule->wc, wildcards);
127 rule->flow.nw_src = match->nw_src;
128 rule->flow.nw_dst = match->nw_dst;
129 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
130 : ntohs(match->in_port));
131 rule->flow.dl_vlan = match->dl_vlan;
132 rule->flow.dl_vlan_pcp = match->dl_vlan_pcp;
133 rule->flow.dl_type = match->dl_type;
134 rule->flow.tp_src = match->tp_src;
135 rule->flow.tp_dst = match->tp_dst;
136 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
137 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
138 rule->flow.nw_tos = match->nw_tos;
139 rule->flow.nw_proto = match->nw_proto;
141 cls_rule_zero_wildcarded_fields(rule);
144 /* Converts 'rule' into an OpenFlow match structure 'match' with the given flow
145 * format 'flow_format' (one of NXFF_*). */
147 cls_rule_to_match(const struct cls_rule *rule, int flow_format,
148 struct ofp_match *match)
150 match->wildcards = htonl(rule->wc.wildcards
151 & (flow_format == NXFF_TUN_ID_FROM_COOKIE
152 ? OVSFW_ALL : OFPFW_ALL));
153 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
154 : rule->flow.in_port);
155 match->dl_vlan = rule->flow.dl_vlan;
156 match->dl_vlan_pcp = rule->flow.dl_vlan_pcp;
157 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
158 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
159 match->dl_type = rule->flow.dl_type;
160 match->nw_src = rule->flow.nw_src;
161 match->nw_dst = rule->flow.nw_dst;
162 match->nw_tos = rule->flow.nw_tos;
163 match->nw_proto = rule->flow.nw_proto;
164 match->tp_src = rule->flow.tp_src;
165 match->tp_dst = rule->flow.tp_dst;
166 memset(match->pad1, '\0', sizeof match->pad1);
167 memset(match->pad2, '\0', sizeof match->pad2);
170 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
171 * field in 'flow' to all-0-bits. It is important to maintain this invariant
172 * in a clr_rule that might be inserted into a classifier.
174 * It is never necessary to call this function directly for a cls_rule that is
175 * initialized or modified only by cls_rule_*() functions. It is useful to
176 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
179 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
181 zero_wildcards(&rule->flow, &rule->wc);
185 cls_rule_set_in_port(struct cls_rule *rule, uint16_t odp_port)
187 rule->wc.wildcards &= ~OFPFW_IN_PORT;
188 rule->flow.in_port = odp_port;
192 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
194 rule->wc.wildcards &= ~OFPFW_DL_TYPE;
195 rule->flow.dl_type = dl_type;
199 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
201 rule->wc.wildcards &= ~OFPFW_DL_SRC;
202 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
206 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
208 rule->wc.wildcards &= ~(OFPFW_DL_DST | FWW_ETH_MCAST);
209 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
213 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
215 return cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
219 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
221 switch (ntohs(mask)) {
223 if (tci == htons(0)) {
224 /* Match only packets that have no 802.1Q header. */
225 rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
226 rule->flow.dl_vlan = htons(OFP_VLAN_NONE);
227 rule->flow.dl_vlan_pcp = 0;
229 } else if (tci & htons(VLAN_CFI)) {
230 /* Match only packets that have a specific 802.1Q VID and PCP. */
231 rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
232 rule->flow.dl_vlan = htons(vlan_tci_to_vid(tci));
233 rule->flow.dl_vlan_pcp = vlan_tci_to_pcp(tci);
241 if (!(tci & htons(VLAN_CFI))) {
244 /* Match only packets that have a specific 802.1Q VID. */
245 cls_rule_set_dl_vlan(rule, tci & htons(VLAN_VID_MASK));
246 rule->wc.wildcards |= OFPFW_DL_VLAN_PCP;
247 rule->flow.dl_vlan_pcp = 0;
252 if (!(tci & htons(VLAN_CFI))) {
255 /* Match only packets that have a specific 802.1Q PCP. */
256 cls_rule_set_dl_vlan_pcp(rule, vlan_tci_to_pcp(tci));
257 rule->wc.wildcards |= OFPFW_DL_VLAN;
258 rule->flow.dl_vlan = 0;
263 /* Match anything. */
264 rule->wc.wildcards |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
265 rule->flow.dl_vlan = htons(0);
266 rule->flow.dl_vlan_pcp = 0;
275 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
277 if (dl_vlan != htons(OFP_VLAN_NONE)) {
278 dl_vlan &= htons(VLAN_VID_MASK);
281 rule->wc.wildcards &= ~OFPFW_DL_VLAN;
282 rule->flow.dl_vlan = dl_vlan;
286 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
288 rule->wc.wildcards &= ~OFPFW_DL_VLAN_PCP;
289 rule->flow.dl_vlan_pcp = dl_vlan_pcp & 0x07;
293 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
295 rule->wc.wildcards &= ~OFPFW_TP_SRC;
296 rule->flow.tp_src = tp_src;
300 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
302 rule->wc.wildcards &= ~OFPFW_TP_DST;
303 rule->flow.tp_dst = tp_dst;
307 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
309 rule->wc.wildcards &= ~OFPFW_NW_PROTO;
310 rule->flow.nw_proto = nw_proto;
314 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
316 cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
320 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
322 if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
323 rule->flow.nw_src = ip & mask;
331 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
333 cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
337 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
339 if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
340 rule->flow.nw_dst = ip & mask;
348 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
350 rule->wc.wildcards &= ~OFPFW_NW_TOS;
351 rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
355 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
357 rule->wc.wildcards &= ~OFPFW_ICMP_TYPE;
358 rule->flow.icmp_type = htons(icmp_type);
363 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
365 rule->wc.wildcards &= ~OFPFW_ICMP_CODE;
366 rule->flow.icmp_code = htons(icmp_code);
369 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
370 * fields, and have the same values for fixed fields, otherwise false. */
372 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
374 return (a->priority == b->priority
375 && flow_wildcards_equal(&a->wc, &b->wc)
376 && flow_equal(&a->flow, &b->flow));
379 /* Converts 'rule' to a string and returns the string. The caller must free
380 * the string (with free()). */
382 cls_rule_to_string(const struct cls_rule *rule)
384 struct ds s = DS_EMPTY_INITIALIZER;
385 ds_put_format(&s, "wildcards=%x priority=%u ",
386 rule->wc.wildcards, rule->priority);
387 flow_format(&s, &rule->flow);
391 /* Prints cls_rule 'rule', for debugging.
393 * (The output could be improved and expanded, but this was good enough to
394 * debug the classifier.) */
396 cls_rule_print(const struct cls_rule *rule)
398 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
399 flow_print(stdout, &rule->flow);
403 /* Initializes 'cls' as a classifier that initially contains no classification
406 classifier_init(struct classifier *cls)
409 hmap_init(&cls->tables);
412 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
413 * caller's responsibility. */
415 classifier_destroy(struct classifier *cls)
418 struct cls_table *table, *next_table;
420 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
421 hmap_destroy(&table->rules);
422 hmap_remove(&cls->tables, &table->hmap_node);
425 hmap_destroy(&cls->tables);
429 /* Returns true if 'cls' contains no classification rules, false otherwise. */
431 classifier_is_empty(const struct classifier *cls)
433 return cls->n_rules == 0;
436 /* Returns the number of rules in 'classifier'. */
438 classifier_count(const struct classifier *cls)
443 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
444 * must not modify or free it.
446 * If 'cls' already contains an identical rule (including wildcards, values of
447 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
448 * rule that was replaced. The caller takes ownership of the returned rule and
449 * is thus responsible for freeing it, etc., as necessary.
451 * Returns NULL if 'cls' does not contain a rule with an identical key, after
452 * inserting the new rule. In this case, no rules are displaced by the new
453 * rule, even rules that cannot have any effect because the new rule matches a
454 * superset of their flows and has higher priority. */
456 classifier_insert(struct classifier *cls, struct cls_rule *rule)
458 struct cls_rule *old_rule;
459 struct cls_table *table;
461 table = find_table(cls, &rule->wc);
463 table = insert_table(cls, &rule->wc);
466 old_rule = insert_rule(table, rule);
468 table->n_table_rules++;
474 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
475 * 'rule', if this is desirable. */
477 classifier_remove(struct classifier *cls, struct cls_rule *rule)
479 struct cls_rule *head;
480 struct cls_table *table;
482 table = find_table(cls, &rule->wc);
483 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
485 list_remove(&rule->list);
486 } else if (list_is_empty(&rule->list)) {
487 hmap_remove(&table->rules, &rule->hmap_node);
489 struct cls_rule *next = CONTAINER_OF(rule->list.next,
490 struct cls_rule, list);
492 list_remove(&rule->list);
493 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
496 if (--table->n_table_rules == 0) {
497 destroy_table(cls, table);
503 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
504 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
505 * of equal priority match 'flow', returns one arbitrarily. */
507 classifier_lookup(const struct classifier *cls, const struct flow *flow)
509 struct cls_table *table;
510 struct cls_rule *best;
513 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
514 struct cls_rule *rule = find_match(table, flow);
515 if (rule && (!best || rule->priority > best->priority)) {
522 /* Finds and returns a rule in 'cls' with exactly the same priority and
523 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
524 * contain an exact match.
526 * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
527 * treats exact-match rules as highest priority). */
529 classifier_find_rule_exactly(const struct classifier *cls,
530 const struct cls_rule *target)
532 struct cls_rule *head, *rule;
533 struct cls_table *table;
535 table = find_table(cls, &target->wc);
540 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
541 if (flow_wildcards_is_exact(&target->wc)) {
544 FOR_EACH_RULE_IN_LIST (rule, head) {
545 if (target->priority >= rule->priority) {
546 return target->priority == rule->priority ? rule : NULL;
552 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
553 * considered to overlap if both rules have the same priority and a packet
554 * could match both. */
556 classifier_rule_overlaps(const struct classifier *cls,
557 const struct cls_rule *target)
559 struct cls_table *table;
561 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
562 struct flow_wildcards wc;
563 struct cls_rule *head;
565 flow_wildcards_combine(&wc, &target->wc, &table->wc);
566 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
567 struct cls_rule *rule;
569 FOR_EACH_RULE_IN_LIST (rule, head) {
570 if (rule->priority == target->priority
571 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
584 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
587 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
590 static struct cls_rule *
591 search_table(const struct cls_table *table, const struct cls_rule *target)
593 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
594 struct cls_rule *rule;
596 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
597 if (rule_matches(rule, target)) {
605 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
606 * 'target' or are more specific than 'target'. That is, a given 'rule'
607 * matches 'target' if, for every field:
609 * - 'target' and 'rule' specify the same (non-wildcarded) value for the
612 * - 'target' wildcards the field,
616 * - 'target' and 'rule' specify different values for the field, or
618 * - 'target' specifies a value for the field but 'rule' wildcards it.
620 * Equivalently, the truth table for whether a field matches is:
625 * +---------+---------+
626 * t wild | yes | yes |
628 * r +---------+---------+
629 * g exact | no |if values|
631 * t +---------+---------+
633 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
634 * commands and by OpenFlow 1.0 aggregate and flow stats.
636 * Ignores target->priority.
638 * 'target' may be NULL to iterate over every rule in 'cls'. */
640 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
641 const struct cls_rule *target)
644 cursor->target = target;
647 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
648 * pointer if there are no matches. */
650 cls_cursor_first(struct cls_cursor *cursor)
652 struct cls_table *table;
654 for (table = classifier_first_table(cursor->cls); table;
655 table = classifier_next_table(cursor->cls, table)) {
656 struct cls_rule *rule = search_table(table, cursor->target);
658 cursor->table = table;
666 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
667 * pointer if there are no more matches. */
669 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
671 const struct cls_table *table;
672 struct cls_rule *next;
674 next = next_rule_in_list__(rule);
675 if (next->priority < rule->priority) {
679 /* 'next' is the head of the list, that is, the rule that is included in
680 * the table's hmap. (This is important when the classifier contains rules
681 * that differ only in priority.) */
683 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
684 if (rule_matches(rule, cursor->target)) {
689 for (table = classifier_next_table(cursor->cls, cursor->table); table;
690 table = classifier_next_table(cursor->cls, table)) {
691 rule = search_table(table, cursor->target);
693 cursor->table = table;
701 static struct cls_table *
702 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
704 struct cls_table *table;
706 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
708 if (flow_wildcards_equal(wc, &table->wc)) {
715 static struct cls_table *
716 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
718 struct cls_table *table;
720 table = xzalloc(sizeof *table);
721 hmap_init(&table->rules);
723 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
728 static struct cls_table *
729 classifier_first_table(const struct classifier *cls)
731 return cls_table_from_hmap_node(hmap_first(&cls->tables));
734 static struct cls_table *
735 classifier_next_table(const struct classifier *cls,
736 const struct cls_table *table)
738 return cls_table_from_hmap_node(hmap_next(&cls->tables,
743 destroy_table(struct classifier *cls, struct cls_table *table)
745 hmap_remove(&cls->tables, &table->hmap_node);
746 hmap_destroy(&table->rules);
750 static struct cls_rule *
751 find_match(const struct cls_table *table, const struct flow *flow)
753 struct cls_rule *rule;
757 zero_wildcards(&f, &table->wc);
758 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
760 if (flow_equal(&f, &rule->flow)) {
767 static struct cls_rule *
768 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
770 struct cls_rule *head;
772 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
773 if (flow_equal(&head->flow, flow)) {
780 static struct cls_rule *
781 insert_rule(struct cls_table *table, struct cls_rule *new)
783 struct cls_rule *head;
785 new->hmap_node.hash = flow_hash(&new->flow, 0);
787 head = find_equal(table, &new->flow, new->hmap_node.hash);
789 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
790 list_init(&new->list);
793 /* Scan the list for the insertion point that will keep the list in
794 * order of decreasing priority. */
795 struct cls_rule *rule;
796 FOR_EACH_RULE_IN_LIST (rule, head) {
797 if (new->priority >= rule->priority) {
799 /* 'new' is the new highest-priority flow in the list. */
800 hmap_replace(&table->rules,
801 &rule->hmap_node, &new->hmap_node);
804 if (new->priority == rule->priority) {
805 list_replace(&new->list, &rule->list);
808 list_insert(&rule->list, &new->list);
814 /* Insert 'new' at the end of the list. */
815 list_push_back(&head->list, &new->list);
820 static struct cls_rule *
821 next_rule_in_list__(struct cls_rule *rule)
823 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
827 static struct cls_rule *
828 next_rule_in_list(struct cls_rule *rule)
830 struct cls_rule *next = next_rule_in_list__(rule);
831 return next->priority < rule->priority ? next : NULL;
835 flow_equal_except(const struct flow *a, const struct flow *b,
836 const struct flow_wildcards *wildcards)
838 const uint32_t wc = wildcards->wildcards;
841 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
843 for (i = 0; i < FLOW_N_REGS; i++) {
844 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
849 return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
850 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
851 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
852 && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
853 && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
854 && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
855 && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
856 && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
857 && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
858 && (wc & OFPFW_DL_DST
859 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
860 && a->dl_dst[1] == b->dl_dst[1]
861 && a->dl_dst[2] == b->dl_dst[2]
862 && a->dl_dst[3] == b->dl_dst[3]
863 && a->dl_dst[4] == b->dl_dst[4]
864 && a->dl_dst[5] == b->dl_dst[5]))
865 && (wc & FWW_ETH_MCAST || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
866 && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
867 && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
868 && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
872 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
874 const uint32_t wc = wildcards->wildcards;
877 BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
879 for (i = 0; i < FLOW_N_REGS; i++) {
880 flow->regs[i] &= wildcards->reg_masks[i];
882 if (wc & NXFW_TUN_ID) {
885 flow->nw_src &= wildcards->nw_src_mask;
886 flow->nw_dst &= wildcards->nw_dst_mask;
887 if (wc & OFPFW_IN_PORT) {
890 if (wc & OFPFW_DL_VLAN) {
893 if (wc & OFPFW_DL_TYPE) {
896 if (wc & OFPFW_TP_SRC) {
899 if (wc & OFPFW_TP_DST) {
902 if (wc & OFPFW_DL_SRC) {
903 memset(flow->dl_src, 0, sizeof flow->dl_src);
905 if (wc & OFPFW_DL_DST) {
906 flow->dl_dst[0] &= 0x01;
907 memset(&flow->dl_dst[1], 0, 5);
909 if (wc & FWW_ETH_MCAST) {
910 flow->dl_dst[0] &= 0xfe;
912 if (wc & OFPFW_NW_PROTO) {
915 if (wc & OFPFW_DL_VLAN_PCP) {
916 flow->dl_vlan_pcp = 0;
918 if (wc & OFPFW_NW_TOS) {