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