classifier: Use OFP_DEFAULT_PRIORITY instead of literal 32768.
[sliver-openvswitch.git] / lib / classifier.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "classifier.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "hash.h"
26 #include "odp-util.h"
27 #include "ofp-util.h"
28 #include "packets.h"
29
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
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 *);
48 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
49
50 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
51 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD)                               \
52     for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
53 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD)                    \
54     for ((RULE) = (HEAD);                                               \
55          (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true);    \
56          (RULE) = (NEXT))
57
58 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
59 static struct cls_rule *next_rule_in_list(struct cls_rule *);
60
61 static struct cls_table *
62 cls_table_from_hmap_node(const struct hmap_node *node)
63 {
64     return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
65 }
66
67 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
68  * 'wildcards' and 'priority'. */
69 void
70 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
71               unsigned int priority, struct cls_rule *rule)
72 {
73     rule->flow = *flow;
74     rule->wc = *wildcards;
75     rule->priority = priority;
76     cls_rule_zero_wildcarded_fields(rule);
77 }
78
79 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
80  * given 'priority'.  (For OpenFlow 1.0, exact-match rule are always highest
81  * priority, so 'priority' should be at least 65535.) */
82 void
83 cls_rule_init_exact(const struct flow *flow,
84                     unsigned int priority, struct cls_rule *rule)
85 {
86     rule->flow = *flow;
87     flow_wildcards_init_exact(&rule->wc);
88     rule->priority = priority;
89 }
90
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);
97     flow_wildcards_init_catchall(&rule->wc);
98     rule->priority = priority;
99 }
100
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
110 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
111 {
112     zero_wildcards(&rule->flow, &rule->wc);
113 }
114
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
131 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
132 {
133     rule->wc.wildcards &= ~FWW_TUN_ID;
134     rule->flow.tun_id = tun_id;
135 }
136
137 void
138 cls_rule_set_in_port(struct cls_rule *rule, uint16_t odp_port)
139 {
140     rule->wc.wildcards &= ~FWW_IN_PORT;
141     rule->flow.in_port = odp_port;
142 }
143
144 void
145 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
146 {
147     rule->wc.wildcards &= ~FWW_DL_TYPE;
148     rule->flow.dl_type = dl_type;
149 }
150
151 void
152 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
153 {
154     rule->wc.wildcards &= ~FWW_DL_SRC;
155     memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
156 }
157
158 void
159 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
160 {
161     rule->wc.wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
162     memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
163 }
164
165 void
166 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
167 {
168     cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
169 }
170
171 void
172 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
173 {
174     rule->flow.vlan_tci = tci & mask;
175     rule->wc.vlan_tci_mask = mask;
176 }
177
178 /* Modifies 'rule' so that the VLAN VID is wildcarded.  If the PCP is already
179  * wildcarded, then 'rule' will match a packet regardless of whether it has an
180  * 802.1Q header or not. */
181 void
182 cls_rule_set_any_vid(struct cls_rule *rule)
183 {
184     if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
185         rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
186         rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
187     } else {
188         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
189     }
190 }
191
192 /* Modifies 'rule' depending on 'dl_vlan':
193  *
194  *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
195  *     without an 802.1Q header.
196  *
197  *   - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
198  *     VID equals the low 12 bits of 'dl_vlan'.
199  */
200 void
201 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
202 {
203     if (dl_vlan == htons(OFP_VLAN_NONE)) {
204         cls_rule_set_dl_tci(rule, htons(0));
205     } else {
206         dl_vlan &= htons(VLAN_VID_MASK);
207         rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
208         rule->flow.vlan_tci |= htons(VLAN_CFI) | dl_vlan;
209         rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
210     }
211 }
212
213 /* Modifies 'rule' so that the VLAN PCP is wildcarded.  If the VID is already
214  * wildcarded, then 'rule' will match a packet regardless of whether it has an
215  * 802.1Q header or not. */
216 void
217 cls_rule_set_any_pcp(struct cls_rule *rule)
218 {
219     if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
220         rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
221         rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
222     } else {
223         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
224     }
225 }
226
227 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
228  * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
229 void
230 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
231 {
232     dl_vlan_pcp &= 0x07;
233     rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
234     rule->flow.vlan_tci |= htons((dl_vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
235     rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
236 }
237
238 void
239 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
240 {
241     rule->wc.wildcards &= ~FWW_TP_SRC;
242     rule->flow.tp_src = tp_src;
243 }
244
245 void
246 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
247 {
248     rule->wc.wildcards &= ~FWW_TP_DST;
249     rule->flow.tp_dst = tp_dst;
250 }
251
252 void
253 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
254 {
255     rule->wc.wildcards &= ~FWW_NW_PROTO;
256     rule->flow.nw_proto = nw_proto;
257 }
258
259 void
260 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
261 {
262     cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
263 }
264
265 bool
266 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
267 {
268     if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
269         rule->flow.nw_src = ip & mask;
270         return true;
271     } else {
272         return false;
273     }
274 }
275
276 void
277 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
278 {
279     cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
280 }
281
282 bool
283 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
284 {
285     if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
286         rule->flow.nw_dst = ip & mask;
287         return true;
288     } else {
289         return false;
290     }
291 }
292
293 void
294 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
295 {
296     rule->wc.wildcards &= ~FWW_NW_TOS;
297     rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
298 }
299
300 void
301 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
302 {
303     rule->wc.wildcards &= ~FWW_TP_SRC;
304     rule->flow.icmp_type = htons(icmp_type);
305
306 }
307
308 void
309 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
310 {
311     rule->wc.wildcards &= ~FWW_TP_DST;
312     rule->flow.icmp_code = htons(icmp_code);
313 }
314
315 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
316  * fields, and have the same values for fixed fields, otherwise false. */
317 bool
318 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
319 {
320     return (a->priority == b->priority
321             && flow_wildcards_equal(&a->wc, &b->wc)
322             && flow_equal(&a->flow, &b->flow));
323 }
324
325 static void
326 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
327                   ovs_be32 netmask)
328 {
329     if (netmask) {
330         ds_put_format(s, "%s="IP_FMT, name, IP_ARGS(&ip));
331         if (netmask != htonl(UINT32_MAX)) {
332             if (ip_is_cidr(netmask)) {
333                 int wcbits = ofputil_netmask_to_wcbits(netmask);
334                 ds_put_format(s, "/%d", 32 - wcbits);
335             } else {
336                 ds_put_format(s, "/"IP_FMT, IP_ARGS(&netmask));
337             }
338         }
339         ds_put_char(s, ',');
340     }
341 }
342
343 void
344 cls_rule_format(const struct cls_rule *rule, struct ds *s)
345 {
346     const struct flow_wildcards *wc = &rule->wc;
347     size_t start_len = s->length;
348     flow_wildcards_t w = wc->wildcards;
349     const struct flow *f = &rule->flow;
350     bool skip_type = false;
351     bool skip_proto = false;
352
353     int i;
354
355     if (rule->priority != OFP_DEFAULT_PRIORITY) {
356         ds_put_format(s, "priority=%d,", rule->priority);
357     }
358
359     if (!(w & FWW_DL_TYPE)) {
360         skip_type = true;
361         if (f->dl_type == htons(ETH_TYPE_IP)) {
362             if (!(w & FWW_NW_PROTO)) {
363                 skip_proto = true;
364                 if (f->nw_proto == IP_TYPE_ICMP) {
365                     ds_put_cstr(s, "icmp,");
366                 } else if (f->nw_proto == IP_TYPE_TCP) {
367                     ds_put_cstr(s, "tcp,");
368                 } else if (f->nw_proto == IP_TYPE_UDP) {
369                     ds_put_cstr(s, "udp,");
370                 } else {
371                     ds_put_cstr(s, "ip,");
372                     skip_proto = false;
373                 }
374             } else {
375                 ds_put_cstr(s, "ip,");
376             }
377         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
378             ds_put_cstr(s, "arp,");
379         } else {
380             skip_type = false;
381         }
382     }
383     for (i = 0; i < FLOW_N_REGS; i++) {
384         switch (wc->reg_masks[i]) {
385         case 0:
386             break;
387         case UINT32_MAX:
388             ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
389             break;
390         default:
391             ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
392                           i, f->regs[i], wc->reg_masks[i]);
393             break;
394         }
395     }
396     if (!(w & FWW_TUN_ID)) {
397         ds_put_format(s, "tun_id=0x%"PRIx64",", ntohll(f->tun_id));
398     }
399     if (!(w & FWW_IN_PORT)) {
400         ds_put_format(s, "in_port=%"PRIu16",",
401                       odp_port_to_ofp_port(f->in_port));
402     }
403     if (wc->vlan_tci_mask) {
404         ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
405         ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
406         ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
407
408         if (cfi && f->vlan_tci & htons(VLAN_CFI)
409             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
410             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
411             && (vid_mask || pcp_mask)) {
412             if (vid_mask) {
413                 ds_put_format(s, "dl_vlan=%"PRIu16",",
414                               vlan_tci_to_vid(f->vlan_tci));
415             }
416             if (pcp_mask) {
417                 ds_put_format(s, "dl_vlan_pcp=%d,",
418                               vlan_tci_to_pcp(f->vlan_tci));
419             }
420         } else {
421             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
422                           ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
423         }
424     }
425     if (!(w & FWW_DL_SRC)) {
426         ds_put_format(s, "dl_src="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_src));
427     }
428     switch (w & (FWW_DL_DST | FWW_ETH_MCAST)) {
429     case 0:
430         ds_put_format(s, "dl_dst="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_dst));
431         break;
432     case FWW_DL_DST:
433         ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/01:00:00:00:00:00,",
434                       ETH_ADDR_ARGS(f->dl_dst));
435         break;
436     case FWW_ETH_MCAST:
437         ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/fe:ff:ff:ff:ff:ff,",
438                       ETH_ADDR_ARGS(f->dl_dst));
439         break;
440     case FWW_DL_DST | FWW_ETH_MCAST:
441         break;
442     }
443     if (!skip_type && !(w & FWW_DL_TYPE)) {
444         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
445     }
446     format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
447     format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
448     if (!skip_proto && !(w & FWW_NW_PROTO)) {
449         if (f->dl_type == htons(ETH_TYPE_ARP)) {
450             ds_put_format(s, "arp_op=%"PRIu8, f->nw_proto);
451         } else {
452             ds_put_format(s, "nw_proto=%"PRIu8, f->nw_proto);
453         }
454     }
455     if (!(w & FWW_NW_TOS)) {
456         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos);
457     }
458     if (f->nw_proto == IP_TYPE_ICMP) {
459         if (!(w & FWW_TP_SRC)) {
460             ds_put_format(s, "icmp_type=%"PRIu16, ntohs(f->tp_src));
461         }
462         if (!(w & FWW_TP_DST)) {
463             ds_put_format(s, "icmp_code=%"PRIu16, ntohs(f->tp_dst));
464         }
465     } else {
466         if (!(w & FWW_TP_SRC)) {
467             ds_put_format(s, "tp_src=%"PRIu16, ntohs(f->tp_src));
468         }
469         if (!(w & FWW_TP_DST)) {
470             ds_put_format(s, "tp_dst=%"PRIu16, ntohs(f->tp_dst));
471         }
472     }
473
474     if (s->length > start_len && ds_last(s) == ',') {
475         s->length--;
476     }
477 }
478
479 /* Converts 'rule' to a string and returns the string.  The caller must free
480  * the string (with free()). */
481 char *
482 cls_rule_to_string(const struct cls_rule *rule)
483 {
484     struct ds s = DS_EMPTY_INITIALIZER;
485     cls_rule_format(rule, &s);
486     return ds_steal_cstr(&s);
487 }
488
489 void
490 cls_rule_print(const struct cls_rule *rule)
491 {
492     char *s = cls_rule_to_string(rule);
493     puts(s);
494     free(s);
495 }
496 \f
497 /* Initializes 'cls' as a classifier that initially contains no classification
498  * rules. */
499 void
500 classifier_init(struct classifier *cls)
501 {
502     cls->n_rules = 0;
503     hmap_init(&cls->tables);
504 }
505
506 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
507  * caller's responsibility. */
508 void
509 classifier_destroy(struct classifier *cls)
510 {
511     if (cls) {
512         struct cls_table *table, *next_table;
513
514         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
515             hmap_destroy(&table->rules);
516             hmap_remove(&cls->tables, &table->hmap_node);
517             free(table);
518         }
519         hmap_destroy(&cls->tables);
520     }
521 }
522
523 /* Returns true if 'cls' contains no classification rules, false otherwise. */
524 bool
525 classifier_is_empty(const struct classifier *cls)
526 {
527     return cls->n_rules == 0;
528 }
529
530 /* Returns the number of rules in 'classifier'. */
531 int
532 classifier_count(const struct classifier *cls)
533 {
534     return cls->n_rules;
535 }
536
537 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
538  * must not modify or free it.
539  *
540  * If 'cls' already contains an identical rule (including wildcards, values of
541  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
542  * rule that was replaced.  The caller takes ownership of the returned rule and
543  * is thus responsible for freeing it, etc., as necessary.
544  *
545  * Returns NULL if 'cls' does not contain a rule with an identical key, after
546  * inserting the new rule.  In this case, no rules are displaced by the new
547  * rule, even rules that cannot have any effect because the new rule matches a
548  * superset of their flows and has higher priority. */
549 struct cls_rule *
550 classifier_insert(struct classifier *cls, struct cls_rule *rule)
551 {
552     struct cls_rule *old_rule;
553     struct cls_table *table;
554
555     table = find_table(cls, &rule->wc);
556     if (!table) {
557         table = insert_table(cls, &rule->wc);
558     }
559
560     old_rule = insert_rule(table, rule);
561     if (!old_rule) {
562         table->n_table_rules++;
563         cls->n_rules++;
564     }
565     return old_rule;
566 }
567
568 /* Removes 'rule' from 'cls'.  It is the caller's responsibility to free
569  * 'rule', if this is desirable. */
570 void
571 classifier_remove(struct classifier *cls, struct cls_rule *rule)
572 {
573     struct cls_rule *head;
574     struct cls_table *table;
575
576     table = find_table(cls, &rule->wc);
577     head = find_equal(table, &rule->flow, rule->hmap_node.hash);
578     if (head != rule) {
579         list_remove(&rule->list);
580     } else if (list_is_empty(&rule->list)) {
581         hmap_remove(&table->rules, &rule->hmap_node);
582     } else {
583         struct cls_rule *next = CONTAINER_OF(rule->list.next,
584                                              struct cls_rule, list);
585
586         list_remove(&rule->list);
587         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
588     }
589
590     if (--table->n_table_rules == 0) {
591         destroy_table(cls, table);
592     }
593
594     cls->n_rules--;
595 }
596
597 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
598  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
599  * of equal priority match 'flow', returns one arbitrarily. */
600 struct cls_rule *
601 classifier_lookup(const struct classifier *cls, const struct flow *flow)
602 {
603     struct cls_table *table;
604     struct cls_rule *best;
605
606     best = NULL;
607     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
608         struct cls_rule *rule = find_match(table, flow);
609         if (rule && (!best || rule->priority > best->priority)) {
610             best = rule;
611         }
612     }
613     return best;
614 }
615
616 /* Finds and returns a rule in 'cls' with exactly the same priority and
617  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
618  * contain an exact match.
619  *
620  * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
621  * treats exact-match rules as highest priority). */
622 struct cls_rule *
623 classifier_find_rule_exactly(const struct classifier *cls,
624                              const struct cls_rule *target)
625 {
626     struct cls_rule *head, *rule;
627     struct cls_table *table;
628
629     table = find_table(cls, &target->wc);
630     if (!table) {
631         return NULL;
632     }
633
634     head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
635     if (flow_wildcards_is_exact(&target->wc)) {
636         return head;
637     }
638     FOR_EACH_RULE_IN_LIST (rule, head) {
639         if (target->priority >= rule->priority) {
640             return target->priority == rule->priority ? rule : NULL;
641         }
642     }
643     return NULL;
644 }
645
646 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
647  * considered to overlap if both rules have the same priority and a packet
648  * could match both. */
649 bool
650 classifier_rule_overlaps(const struct classifier *cls,
651                          const struct cls_rule *target)
652 {
653     struct cls_table *table;
654
655     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
656         struct flow_wildcards wc;
657         struct cls_rule *head;
658
659         flow_wildcards_combine(&wc, &target->wc, &table->wc);
660         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
661             struct cls_rule *rule;
662
663             FOR_EACH_RULE_IN_LIST (rule, head) {
664                 if (rule->priority == target->priority
665                     && flow_equal_except(&target->flow, &rule->flow, &wc)) {
666                     return true;
667                 }
668             }
669         }
670     }
671
672     return false;
673 }
674 \f
675 /* Iteration. */
676
677 static bool
678 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
679 {
680     return (!target
681             || flow_equal_except(&rule->flow, &target->flow, &target->wc));
682 }
683
684 static struct cls_rule *
685 search_table(const struct cls_table *table, const struct cls_rule *target)
686 {
687     if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
688         struct cls_rule *rule;
689
690         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
691             if (rule_matches(rule, target)) {
692                 return rule;
693             }
694         }
695     }
696     return NULL;
697 }
698
699 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
700  * 'target' or are more specific than 'target'.  That is, a given 'rule'
701  * matches 'target' if, for every field:
702  *
703  *   - 'target' and 'rule' specify the same (non-wildcarded) value for the
704  *     field, or
705  *
706  *   - 'target' wildcards the field,
707  *
708  * but not if:
709  *
710  *   - 'target' and 'rule' specify different values for the field, or
711  *
712  *   - 'target' specifies a value for the field but 'rule' wildcards it.
713  *
714  * Equivalently, the truth table for whether a field matches is:
715  *
716  *                                     rule
717  *
718  *                             wildcard    exact
719  *                            +---------+---------+
720  *                   t   wild |   yes   |   yes   |
721  *                   a   card |         |         |
722  *                   r        +---------+---------+
723  *                   g  exact |    no   |if values|
724  *                   e        |         |are equal|
725  *                   t        +---------+---------+
726  *
727  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
728  * commands and by OpenFlow 1.0 aggregate and flow stats.
729  *
730  * Ignores target->priority.
731  *
732  * 'target' may be NULL to iterate over every rule in 'cls'. */
733 void
734 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
735                 const struct cls_rule *target)
736 {
737     cursor->cls = cls;
738     cursor->target = target;
739 }
740
741 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
742  * pointer if there are no matches. */
743 struct cls_rule *
744 cls_cursor_first(struct cls_cursor *cursor)
745 {
746     struct cls_table *table;
747
748     for (table = classifier_first_table(cursor->cls); table;
749          table = classifier_next_table(cursor->cls, table)) {
750         struct cls_rule *rule = search_table(table, cursor->target);
751         if (rule) {
752             cursor->table = table;
753             return rule;
754         }
755     }
756
757     return NULL;
758 }
759
760 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
761  * pointer if there are no more matches. */
762 struct cls_rule *
763 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
764 {
765     const struct cls_table *table;
766     struct cls_rule *next;
767
768     next = next_rule_in_list__(rule);
769     if (next->priority < rule->priority) {
770         return next;
771     }
772
773     /* 'next' is the head of the list, that is, the rule that is included in
774      * the table's hmap.  (This is important when the classifier contains rules
775      * that differ only in priority.) */
776     rule = next;
777     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
778         if (rule_matches(rule, cursor->target)) {
779             return rule;
780         }
781     }
782
783     for (table = classifier_next_table(cursor->cls, cursor->table); table;
784          table = classifier_next_table(cursor->cls, table)) {
785         rule = search_table(table, cursor->target);
786         if (rule) {
787             cursor->table = table;
788             return rule;
789         }
790     }
791
792     return NULL;
793 }
794 \f
795 static struct cls_table *
796 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
797 {
798     struct cls_table *table;
799
800     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
801                              &cls->tables) {
802         if (flow_wildcards_equal(wc, &table->wc)) {
803             return table;
804         }
805     }
806     return NULL;
807 }
808
809 static struct cls_table *
810 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
811 {
812     struct cls_table *table;
813
814     table = xzalloc(sizeof *table);
815     hmap_init(&table->rules);
816     table->wc = *wc;
817     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
818
819     return table;
820 }
821
822 static struct cls_table *
823 classifier_first_table(const struct classifier *cls)
824 {
825     return cls_table_from_hmap_node(hmap_first(&cls->tables));
826 }
827
828 static struct cls_table *
829 classifier_next_table(const struct classifier *cls,
830                       const struct cls_table *table)
831 {
832     return cls_table_from_hmap_node(hmap_next(&cls->tables,
833                                               &table->hmap_node));
834 }
835
836 static void
837 destroy_table(struct classifier *cls, struct cls_table *table)
838 {
839     hmap_remove(&cls->tables, &table->hmap_node);
840     hmap_destroy(&table->rules);
841     free(table);
842 }
843
844 static struct cls_rule *
845 find_match(const struct cls_table *table, const struct flow *flow)
846 {
847     struct cls_rule *rule;
848     struct flow f;
849
850     f = *flow;
851     zero_wildcards(&f, &table->wc);
852     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
853                              &table->rules) {
854         if (flow_equal(&f, &rule->flow)) {
855             return rule;
856         }
857     }
858     return NULL;
859 }
860
861 static struct cls_rule *
862 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
863 {
864     struct cls_rule *head;
865
866     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
867         if (flow_equal(&head->flow, flow)) {
868             return head;
869         }
870     }
871     return NULL;
872 }
873
874 static struct cls_rule *
875 insert_rule(struct cls_table *table, struct cls_rule *new)
876 {
877     struct cls_rule *head;
878
879     new->hmap_node.hash = flow_hash(&new->flow, 0);
880
881     head = find_equal(table, &new->flow, new->hmap_node.hash);
882     if (!head) {
883         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
884         list_init(&new->list);
885         return NULL;
886     } else {
887         /* Scan the list for the insertion point that will keep the list in
888          * order of decreasing priority. */
889         struct cls_rule *rule;
890         FOR_EACH_RULE_IN_LIST (rule, head) {
891             if (new->priority >= rule->priority) {
892                 if (rule == head) {
893                     /* 'new' is the new highest-priority flow in the list. */
894                     hmap_replace(&table->rules,
895                                  &rule->hmap_node, &new->hmap_node);
896                 }
897
898                 if (new->priority == rule->priority) {
899                     list_replace(&new->list, &rule->list);
900                     return rule;
901                 } else {
902                     list_insert(&rule->list, &new->list);
903                     return NULL;
904                 }
905             }
906         }
907
908         /* Insert 'new' at the end of the list. */
909         list_push_back(&head->list, &new->list);
910         return NULL;
911     }
912 }
913
914 static struct cls_rule *
915 next_rule_in_list__(struct cls_rule *rule)
916 {
917     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
918     return next;
919 }
920
921 static struct cls_rule *
922 next_rule_in_list(struct cls_rule *rule)
923 {
924     struct cls_rule *next = next_rule_in_list__(rule);
925     return next->priority < rule->priority ? next : NULL;
926 }
927
928 static bool
929 flow_equal_except(const struct flow *a, const struct flow *b,
930                   const struct flow_wildcards *wildcards)
931 {
932     const flow_wildcards_t wc = wildcards->wildcards;
933     int i;
934
935     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 40 + FLOW_N_REGS * 4);
936
937     for (i = 0; i < FLOW_N_REGS; i++) {
938         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
939             return false;
940         }
941     }
942
943     return ((wc & FWW_TUN_ID || a->tun_id == b->tun_id)
944             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
945             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
946             && (wc & FWW_IN_PORT || a->in_port == b->in_port)
947             && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
948             && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
949             && (wc & FWW_TP_SRC || a->tp_src == b->tp_src)
950             && (wc & FWW_TP_DST || a->tp_dst == b->tp_dst)
951             && (wc & FWW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
952             && (wc & FWW_DL_DST
953                 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
954                     && a->dl_dst[1] == b->dl_dst[1]
955                     && a->dl_dst[2] == b->dl_dst[2]
956                     && a->dl_dst[3] == b->dl_dst[3]
957                     && a->dl_dst[4] == b->dl_dst[4]
958                     && a->dl_dst[5] == b->dl_dst[5]))
959             && (wc & FWW_ETH_MCAST
960                 || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
961             && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
962             && (wc & FWW_NW_TOS || a->nw_tos == b->nw_tos));
963 }
964
965 static void
966 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
967 {
968     const flow_wildcards_t wc = wildcards->wildcards;
969     int i;
970
971     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 40 + 4 * FLOW_N_REGS);
972
973     for (i = 0; i < FLOW_N_REGS; i++) {
974         flow->regs[i] &= wildcards->reg_masks[i];
975     }
976     if (wc & FWW_TUN_ID) {
977         flow->tun_id = 0;
978     }
979     flow->nw_src &= wildcards->nw_src_mask;
980     flow->nw_dst &= wildcards->nw_dst_mask;
981     if (wc & FWW_IN_PORT) {
982         flow->in_port = 0;
983     }
984     flow->vlan_tci &= wildcards->vlan_tci_mask;
985     if (wc & FWW_DL_TYPE) {
986         flow->dl_type = 0;
987     }
988     if (wc & FWW_TP_SRC) {
989         flow->tp_src = 0;
990     }
991     if (wc & FWW_TP_DST) {
992         flow->tp_dst = 0;
993     }
994     if (wc & FWW_DL_SRC) {
995         memset(flow->dl_src, 0, sizeof flow->dl_src);
996     }
997     if (wc & FWW_DL_DST) {
998         flow->dl_dst[0] &= 0x01;
999         memset(&flow->dl_dst[1], 0, 5);
1000     }
1001     if (wc & FWW_ETH_MCAST) {
1002         flow->dl_dst[0] &= 0xfe;
1003     }
1004     if (wc & FWW_NW_PROTO) {
1005         flow->nw_proto = 0;
1006     }
1007     if (wc & FWW_NW_TOS) {
1008         flow->nw_tos = 0;
1009     }
1010 }