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