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