Support matching and modifying IP ECN bits.
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 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
19 #include "meta-flow.h"
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
26
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-util.h"
30 #include "packets.h"
31 #include "random.h"
32 #include "shash.h"
33 #include "socket-util.h"
34 #include "unaligned.h"
35
36 #define MF_FIELD_SIZES(MEMBER)                  \
37     sizeof ((union mf_value *)0)->MEMBER,       \
38     8 * sizeof ((union mf_value *)0)->MEMBER
39
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
41     /* ## -------- ## */
42     /* ## metadata ## */
43     /* ## -------- ## */
44
45     {
46         MFF_TUN_ID, "tun_id", NULL,
47         MF_FIELD_SIZES(be64),
48         MFM_FULLY, 0,
49         MFS_HEXADECIMAL,
50         MFP_NONE,
51         NXM_NX_TUN_ID,
52     }, {
53         MFF_IN_PORT, "in_port", NULL,
54         MF_FIELD_SIZES(be16),
55         MFM_NONE, FWW_IN_PORT,
56         MFS_OFP_PORT,
57         MFP_NONE,
58         NXM_OF_IN_PORT,
59     },
60
61 #define REGISTER(IDX)                           \
62     {                                           \
63         MFF_REG##IDX, "reg" #IDX, NULL,         \
64         MF_FIELD_SIZES(be32),                   \
65         MFM_FULLY, 0,                           \
66         MFS_HEXADECIMAL,                        \
67         MFP_NONE,                               \
68         NXM_NX_REG(IDX),                        \
69     }
70 #if FLOW_N_REGS > 0
71     REGISTER(0),
72 #endif
73 #if FLOW_N_REGS > 1
74     REGISTER(1),
75 #endif
76 #if FLOW_N_REGS > 2
77     REGISTER(2),
78 #endif
79 #if FLOW_N_REGS > 3
80     REGISTER(3),
81 #endif
82 #if FLOW_N_REGS > 4
83     REGISTER(4),
84 #endif
85 #if FLOW_N_REGS > 5
86 #error
87 #endif
88
89     /* ## -- ## */
90     /* ## L2 ## */
91     /* ## -- ## */
92
93     {
94         MFF_ETH_SRC, "eth_src", "dl_src",
95         MF_FIELD_SIZES(mac),
96         MFM_NONE, FWW_DL_SRC,
97         MFS_ETHERNET,
98         MFP_NONE,
99         NXM_OF_ETH_SRC,
100     }, {
101         MFF_ETH_DST, "eth_dst", "dl_dst",
102         MF_FIELD_SIZES(mac),
103         MFM_MCAST, 0,
104         MFS_ETHERNET,
105         MFP_NONE,
106         NXM_OF_ETH_DST,
107     }, {
108         MFF_ETH_TYPE, "eth_type", "dl_type",
109         MF_FIELD_SIZES(be16),
110         MFM_NONE, FWW_DL_TYPE,
111         MFS_HEXADECIMAL,
112         MFP_NONE,
113         NXM_OF_ETH_TYPE,
114     },
115
116     {
117         MFF_VLAN_TCI, "vlan_tci", NULL,
118         MF_FIELD_SIZES(be16),
119         MFM_FULLY, 0,
120         MFS_HEXADECIMAL,
121         MFP_NONE,
122         NXM_OF_VLAN_TCI,
123     }, {
124         MFF_VLAN_VID, "dl_vlan", NULL,
125         sizeof(ovs_be16), 12,
126         MFM_NONE, 0,
127         MFS_DECIMAL,
128         MFP_NONE,
129         0,
130     }, {
131         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
132         1, 3,
133         MFM_NONE, 0,
134         MFS_DECIMAL,
135         MFP_NONE,
136         0,
137     },
138
139     /* ## -- ## */
140     /* ## L3 ## */
141     /* ## -- ## */
142
143     {
144         MFF_IPV4_SRC, "ip_src", "nw_src",
145         MF_FIELD_SIZES(be32),
146         MFM_CIDR, 0,
147         MFS_IPV4,
148         MFP_IPV4,
149         NXM_OF_IP_SRC,
150     }, {
151         MFF_IPV4_DST, "ip_dst", "nw_dst",
152         MF_FIELD_SIZES(be32),
153         MFM_CIDR, 0,
154         MFS_IPV4,
155         MFP_IPV4,
156         NXM_OF_IP_DST,
157     },
158
159     {
160         MFF_IPV6_SRC, "ipv6_src", NULL,
161         MF_FIELD_SIZES(ipv6),
162         MFM_CIDR, 0,
163         MFS_IPV6,
164         MFP_IPV6,
165         NXM_NX_IPV6_SRC,
166     }, {
167         MFF_IPV6_DST, "ipv6_dst", NULL,
168         MF_FIELD_SIZES(ipv6),
169         MFM_CIDR, 0,
170         MFS_IPV6,
171         MFP_IPV6,
172         NXM_NX_IPV6_DST,
173     },
174     {
175         MFF_IPV6_LABEL, "ipv6_label", NULL,
176         4, 20,
177         MFM_NONE, FWW_IPV6_LABEL,
178         MFS_HEXADECIMAL,
179         MFP_IPV6,
180         NXM_NX_IPV6_LABEL,
181     },
182
183     {
184         MFF_IP_PROTO, "nw_proto", NULL,
185         MF_FIELD_SIZES(u8),
186         MFM_NONE, FWW_NW_PROTO,
187         MFS_DECIMAL,
188         MFP_IP_ANY,
189         NXM_OF_IP_PROTO,
190     }, {
191         MFF_IP_DSCP, "nw_tos", NULL,
192         MF_FIELD_SIZES(u8),
193         MFM_NONE, 0,
194         MFS_DECIMAL,
195         MFP_IP_ANY,
196         NXM_OF_IP_TOS,
197     }, {
198         MFF_IP_ECN, "nw_ecn", NULL,
199         1, 2,
200         MFM_NONE, 0,
201         MFS_DECIMAL,
202         MFP_IP_ANY,
203         NXM_NX_IP_ECN,
204     }, {
205         MFF_IP_FRAG, "ip_frag", NULL,
206         1, 2,
207         MFM_FULLY, 0,
208         MFS_FRAG,
209         MFP_IP_ANY,
210         NXM_NX_IP_FRAG,
211     },
212
213     {
214         MFF_ARP_OP, "arp_op", NULL,
215         MF_FIELD_SIZES(be16),
216         MFM_NONE, FWW_NW_PROTO,
217         MFS_DECIMAL,
218         MFP_ARP,
219         NXM_OF_ARP_OP,
220     }, {
221         MFF_ARP_SPA, "arp_spa", NULL,
222         MF_FIELD_SIZES(be32),
223         MFM_CIDR, 0,
224         MFS_IPV4,
225         MFP_ARP,
226         NXM_OF_ARP_SPA,
227     }, {
228         MFF_ARP_TPA, "arp_tpa", NULL,
229         MF_FIELD_SIZES(be32),
230         MFM_CIDR, 0,
231         MFS_IPV4,
232         MFP_ARP,
233         NXM_OF_ARP_TPA,
234     }, {
235         MFF_ARP_SHA, "arp_sha", NULL,
236         MF_FIELD_SIZES(mac),
237         MFM_NONE, FWW_ARP_SHA,
238         MFS_ETHERNET,
239         MFP_ARP,
240         NXM_NX_ARP_SHA,
241     }, {
242         MFF_ARP_THA, "arp_tha", NULL,
243         MF_FIELD_SIZES(mac),
244         MFM_NONE, FWW_ARP_THA,
245         MFS_ETHERNET,
246         MFP_ARP,
247         NXM_NX_ARP_THA,
248     },
249
250     /* ## -- ## */
251     /* ## L4 ## */
252     /* ## -- ## */
253
254     {
255         MFF_TCP_SRC, "tcp_src", "tp_src",
256         MF_FIELD_SIZES(be16),
257         MFM_NONE, FWW_TP_SRC,
258         MFS_DECIMAL,
259         MFP_TCP,
260         NXM_OF_TCP_SRC,
261     }, {
262         MFF_TCP_DST, "tcp_dst", "tp_dst",
263         MF_FIELD_SIZES(be16),
264         MFM_NONE, FWW_TP_DST,
265         MFS_DECIMAL,
266         MFP_TCP,
267         NXM_OF_TCP_DST,
268     },
269
270     {
271         MFF_UDP_SRC, "udp_src", NULL,
272         MF_FIELD_SIZES(be16),
273         MFM_NONE, FWW_TP_SRC,
274         MFS_DECIMAL,
275         MFP_UDP,
276         NXM_OF_UDP_SRC,
277     }, {
278         MFF_UDP_DST, "udp_dst", NULL,
279         MF_FIELD_SIZES(be16),
280         MFM_NONE, FWW_TP_DST,
281         MFS_DECIMAL,
282         MFP_UDP,
283         NXM_OF_UDP_DST,
284     },
285
286     {
287         MFF_ICMP_TYPE, "icmp_type", NULL,
288         MF_FIELD_SIZES(u8),
289         MFM_NONE, FWW_TP_SRC,
290         MFS_DECIMAL,
291         MFP_ICMP_ANY,
292         NXM_OF_ICMP_TYPE,
293     }, {
294         MFF_ICMP_CODE, "icmp_code", NULL,
295         MF_FIELD_SIZES(u8),
296         MFM_NONE, FWW_TP_DST,
297         MFS_DECIMAL,
298         MFP_ICMP_ANY,
299         NXM_OF_ICMP_CODE,
300     },
301
302     /* ## ---- ## */
303     /* ## L"5" ## */
304     /* ## ---- ## */
305
306     {
307         MFF_ND_TARGET, "nd_target", NULL,
308         MF_FIELD_SIZES(ipv6),
309         MFM_NONE, FWW_ND_TARGET,
310         MFS_IPV6,
311         MFP_ND,
312         NXM_NX_ND_TARGET,
313     }, {
314         MFF_ND_SLL, "nd_sll", NULL,
315         MF_FIELD_SIZES(mac),
316         MFM_NONE, FWW_ARP_SHA,
317         MFS_ETHERNET,
318         MFP_ND_SOLICIT,
319         NXM_NX_ND_SLL,
320     }, {
321         MFF_ND_TLL, "nd_tll", NULL,
322         MF_FIELD_SIZES(mac),
323         MFM_NONE, FWW_ARP_THA,
324         MFS_ETHERNET,
325         MFP_ND_ADVERT,
326         NXM_NX_ND_TLL,
327     }
328 };
329
330 /* Returns the field with the given 'id'. */
331 const struct mf_field *
332 mf_from_id(enum mf_field_id id)
333 {
334     assert((unsigned int) id < MFF_N_IDS);
335     return &mf_fields[id];
336 }
337
338 /* Returns the field with the given 'name', or a null pointer if no field has
339  * that name. */
340 const struct mf_field *
341 mf_from_name(const char *name)
342 {
343     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
344
345     if (shash_is_empty(&mf_by_name)) {
346         const struct mf_field *mf;
347
348         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
349             shash_add_once(&mf_by_name, mf->name, mf);
350             if (mf->extra_name) {
351                 shash_add_once(&mf_by_name, mf->extra_name, mf);
352             }
353         }
354     }
355
356     return shash_find_data(&mf_by_name, name);
357 }
358
359 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
360  * specifies at least one bit in the field.
361  *
362  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
363  * meets 'mf''s prerequisites. */
364 bool
365 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
366 {
367     switch (mf->id) {
368     case MFF_IN_PORT:
369     case MFF_ETH_SRC:
370     case MFF_ETH_TYPE:
371     case MFF_IP_PROTO:
372     case MFF_IPV6_LABEL:
373     case MFF_ARP_OP:
374     case MFF_ARP_SHA:
375     case MFF_ARP_THA:
376     case MFF_TCP_SRC:
377     case MFF_TCP_DST:
378     case MFF_UDP_SRC:
379     case MFF_UDP_DST:
380     case MFF_ICMP_TYPE:
381     case MFF_ICMP_CODE:
382     case MFF_ND_TARGET:
383     case MFF_ND_SLL:
384     case MFF_ND_TLL:
385         assert(mf->fww_bit != 0);
386         return (wc->wildcards & mf->fww_bit) != 0;
387
388     case MFF_TUN_ID:
389         return !wc->tun_id_mask;
390
391 #if FLOW_N_REGS > 0
392     case MFF_REG0:
393 #endif
394 #if FLOW_N_REGS > 1
395     case MFF_REG1:
396 #endif
397 #if FLOW_N_REGS > 2
398     case MFF_REG2:
399 #endif
400 #if FLOW_N_REGS > 3
401     case MFF_REG3:
402 #endif
403 #if FLOW_N_REGS > 4
404     case MFF_REG4:
405 #endif
406 #if FLOW_N_REGS > 5
407 #error
408 #endif
409         return !wc->reg_masks[mf->id - MFF_REG0];
410
411     case MFF_ETH_DST:
412         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
413                 == (FWW_ETH_MCAST | FWW_DL_DST));
414
415     case MFF_VLAN_TCI:
416         return !wc->vlan_tci_mask;
417     case MFF_VLAN_VID:
418         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
419     case MFF_VLAN_PCP:
420         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
421
422     case MFF_IPV4_SRC:
423         return !wc->nw_src_mask;
424     case MFF_IPV4_DST:
425         return !wc->nw_dst_mask;
426
427     case MFF_IPV6_SRC:
428         return ipv6_mask_is_any(&wc->ipv6_src_mask);
429     case MFF_IPV6_DST:
430         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
431
432     case MFF_IP_DSCP:
433         return !(wc->tos_mask & IP_DSCP_MASK);
434     case MFF_IP_ECN:
435         return !(wc->tos_mask & IP_ECN_MASK);
436     case MFF_IP_FRAG:
437         return !(wc->frag_mask & FLOW_FRAG_MASK);
438
439     case MFF_ARP_SPA:
440         return !wc->nw_src_mask;
441     case MFF_ARP_TPA:
442         return !wc->nw_dst_mask;
443
444     case MFF_N_IDS:
445     default:
446         NOT_REACHED();
447     }
448 }
449
450 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
451  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
452  * purposes, or to 0 if it is wildcarded.
453  *
454  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
455  * meets 'mf''s prerequisites. */
456 void
457 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
458             union mf_value *mask)
459 {
460     switch (mf->id) {
461     case MFF_IN_PORT:
462     case MFF_ETH_SRC:
463     case MFF_ETH_TYPE:
464     case MFF_IP_PROTO:
465     case MFF_IPV6_LABEL:
466     case MFF_ARP_OP:
467     case MFF_ARP_SHA:
468     case MFF_ARP_THA:
469     case MFF_TCP_SRC:
470     case MFF_TCP_DST:
471     case MFF_UDP_SRC:
472     case MFF_UDP_DST:
473     case MFF_ICMP_TYPE:
474     case MFF_ICMP_CODE:
475     case MFF_ND_TARGET:
476     case MFF_ND_SLL:
477     case MFF_ND_TLL:
478         assert(mf->fww_bit != 0);
479         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
480         break;
481
482     case MFF_TUN_ID:
483         mask->be64 = wc->tun_id_mask;
484         break;
485
486 #if FLOW_N_REGS > 0
487     case MFF_REG0:
488 #endif
489 #if FLOW_N_REGS > 1
490     case MFF_REG1:
491 #endif
492 #if FLOW_N_REGS > 2
493     case MFF_REG2:
494 #endif
495 #if FLOW_N_REGS > 3
496     case MFF_REG3:
497 #endif
498 #if FLOW_N_REGS > 4
499     case MFF_REG4:
500 #endif
501 #if FLOW_N_REGS > 5
502 #error
503 #endif
504         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
505         break;
506
507     case MFF_ETH_DST:
508         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
509                ETH_ADDR_LEN);
510         break;
511
512     case MFF_VLAN_TCI:
513         mask->be16 = wc->vlan_tci_mask;
514         break;
515     case MFF_VLAN_VID:
516         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
517         break;
518     case MFF_VLAN_PCP:
519         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
520         break;
521
522     case MFF_IPV4_SRC:
523         mask->be32 = wc->nw_src_mask;
524         break;
525     case MFF_IPV4_DST:
526         mask->be32 = wc->nw_dst_mask;
527         break;
528
529     case MFF_IPV6_SRC:
530         mask->ipv6 = wc->ipv6_src_mask;
531         break;
532     case MFF_IPV6_DST:
533         mask->ipv6 = wc->ipv6_dst_mask;
534         break;
535
536     case MFF_IP_DSCP:
537         mask->u8 = wc->tos_mask & IP_DSCP_MASK;
538         break;
539     case MFF_IP_ECN:
540         mask->u8 = wc->tos_mask & IP_ECN_MASK;
541         break;
542     case MFF_IP_FRAG:
543         mask->u8 = wc->frag_mask & FLOW_FRAG_MASK;
544         break;
545
546     case MFF_ARP_SPA:
547         mask->be32 = wc->nw_src_mask;
548         break;
549     case MFF_ARP_TPA:
550         mask->be32 = wc->nw_dst_mask;
551         break;
552
553     case MFF_N_IDS:
554     default:
555         NOT_REACHED();
556     }
557 }
558
559 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
560  * if the mask is valid, false otherwise. */
561 bool
562 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
563 {
564     switch (mf->maskable) {
565     case MFM_NONE:
566         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
567                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
568
569     case MFM_FULLY:
570         return true;
571
572     case MFM_CIDR:
573         return (mf->n_bytes == 4
574                 ? ip_is_cidr(mask->be32)
575                 : ipv6_is_cidr(&mask->ipv6));
576
577     case MFM_MCAST:
578         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
579     }
580
581     NOT_REACHED();
582 }
583
584 static bool
585 is_ip_any(const struct flow *flow)
586 {
587     return (flow->dl_type == htons(ETH_TYPE_IP) ||
588             flow->dl_type == htons(ETH_TYPE_IPV6));
589 }
590
591 static bool
592 is_icmpv4(const struct flow *flow)
593 {
594     return (flow->dl_type == htons(ETH_TYPE_IP)
595             && flow->nw_proto == IPPROTO_ICMP);
596 }
597
598 static bool
599 is_icmpv6(const struct flow *flow)
600 {
601     return (flow->dl_type == htons(ETH_TYPE_IPV6)
602             && flow->nw_proto == IPPROTO_ICMPV6);
603 }
604
605 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
606 bool
607 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
608 {
609     switch (mf->prereqs) {
610     case MFP_NONE:
611         return true;
612
613     case MFP_ARP:
614         return flow->dl_type == htons(ETH_TYPE_ARP);
615     case MFP_IPV4:
616         return flow->dl_type == htons(ETH_TYPE_IP);
617     case MFP_IPV6:
618         return flow->dl_type == htons(ETH_TYPE_IPV6);
619     case MFP_IP_ANY:
620         return is_ip_any(flow);
621
622     case MFP_TCP:
623         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
624     case MFP_UDP:
625         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
626     case MFP_ICMPV6:
627         return is_icmpv6(flow);
628     case MFP_ICMP_ANY:
629         return is_icmpv4(flow) || is_icmpv6(flow);
630
631     case MFP_ND:
632         return (is_icmpv6(flow)
633                 && flow->tp_dst == htons(0)
634                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
635                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
636     case MFP_ND_SOLICIT:
637         return (is_icmpv6(flow)
638                 && flow->tp_dst == htons(0)
639                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
640     case MFP_ND_ADVERT:
641         return (is_icmpv6(flow)
642                 && flow->tp_dst == htons(0)
643                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
644     }
645
646     NOT_REACHED();
647 }
648
649 /* Returns true if 'value' may be a valid value *as part of a masked match*,
650  * false otherwise.
651  *
652  * A value is not rejected just because it is not valid for the field in
653  * question, but only if it doesn't make sense to test the bits in question at
654  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
655  * without the VLAN_CFI bit being set, but we can't reject those values because
656  * it is still legitimate to test just for those bits (see the documentation
657  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
658  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
659 bool
660 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
661 {
662     switch (mf->id) {
663     case MFF_TUN_ID:
664     case MFF_IN_PORT:
665 #if FLOW_N_REGS > 0
666     case MFF_REG0:
667 #endif
668 #if FLOW_N_REGS > 1
669     case MFF_REG1:
670 #endif
671 #if FLOW_N_REGS > 2
672     case MFF_REG2:
673 #endif
674 #if FLOW_N_REGS > 3
675     case MFF_REG3:
676 #endif
677 #if FLOW_N_REGS > 4
678     case MFF_REG4:
679 #endif
680 #if FLOW_N_REGS > 5
681 #error
682 #endif
683     case MFF_ETH_SRC:
684     case MFF_ETH_DST:
685     case MFF_ETH_TYPE:
686     case MFF_VLAN_TCI:
687     case MFF_IPV4_SRC:
688     case MFF_IPV4_DST:
689     case MFF_IPV6_SRC:
690     case MFF_IPV6_DST:
691     case MFF_IP_PROTO:
692     case MFF_ARP_SPA:
693     case MFF_ARP_TPA:
694     case MFF_ARP_SHA:
695     case MFF_ARP_THA:
696     case MFF_TCP_SRC:
697     case MFF_TCP_DST:
698     case MFF_UDP_SRC:
699     case MFF_UDP_DST:
700     case MFF_ICMP_TYPE:
701     case MFF_ICMP_CODE:
702     case MFF_ND_TARGET:
703     case MFF_ND_SLL:
704     case MFF_ND_TLL:
705         return true;
706
707     case MFF_IP_DSCP:
708         return !(value->u8 & ~IP_DSCP_MASK);
709     case MFF_IP_ECN:
710         return !(value->u8 & ~IP_ECN_MASK);
711     case MFF_IP_FRAG:
712         return !(value->u8 & ~FLOW_FRAG_MASK);
713
714     case MFF_ARP_OP:
715         return !(value->be16 & htons(0xff00));
716
717     case MFF_VLAN_VID:
718         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
719
720     case MFF_VLAN_PCP:
721         return !(value->u8 & ~7);
722
723     case MFF_IPV6_LABEL:
724         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
725
726     case MFF_N_IDS:
727     default:
728         NOT_REACHED();
729     }
730 }
731
732 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
733  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
734 void
735 mf_get_value(const struct mf_field *mf, const struct flow *flow,
736              union mf_value *value)
737 {
738     switch (mf->id) {
739     case MFF_TUN_ID:
740         value->be64 = flow->tun_id;
741         break;
742
743     case MFF_IN_PORT:
744         value->be16 = htons(flow->in_port);
745         break;
746
747 #if FLOW_N_REGS > 0
748     case MFF_REG0:
749 #endif
750 #if FLOW_N_REGS > 1
751     case MFF_REG1:
752 #endif
753 #if FLOW_N_REGS > 2
754     case MFF_REG2:
755 #endif
756 #if FLOW_N_REGS > 3
757     case MFF_REG3:
758 #endif
759 #if FLOW_N_REGS > 4
760     case MFF_REG4:
761 #endif
762 #if FLOW_N_REGS > 5
763 #error
764 #endif
765         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
766         break;
767
768     case MFF_ETH_SRC:
769         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
770         break;
771
772     case MFF_ETH_DST:
773         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
774         break;
775
776     case MFF_ETH_TYPE:
777         value->be16 = flow->dl_type;
778         break;
779
780     case MFF_VLAN_TCI:
781         value->be16 = flow->vlan_tci;
782         break;
783
784     case MFF_VLAN_VID:
785         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
786         break;
787
788     case MFF_VLAN_PCP:
789         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
790         break;
791
792     case MFF_IPV4_SRC:
793         value->be32 = flow->nw_src;
794         break;
795
796     case MFF_IPV4_DST:
797         value->be32 = flow->nw_dst;
798         break;
799
800     case MFF_IPV6_SRC:
801         value->ipv6 = flow->ipv6_src;
802         break;
803
804     case MFF_IPV6_DST:
805         value->ipv6 = flow->ipv6_dst;
806         break;
807
808     case MFF_IPV6_LABEL:
809         value->be32 = flow->ipv6_label;
810         break;
811
812     case MFF_IP_PROTO:
813         value->u8 = flow->nw_proto;
814         break;
815
816     case MFF_IP_DSCP:
817         value->u8 = flow->tos & IP_DSCP_MASK;
818         break;
819
820     case MFF_IP_ECN:
821         value->u8 = flow->tos & IP_ECN_MASK;
822         break;
823
824     case MFF_IP_FRAG:
825         value->u8 = flow->frag;
826         break;
827
828     case MFF_ARP_OP:
829         value->be16 = htons(flow->nw_proto);
830         break;
831
832     case MFF_ARP_SPA:
833         value->be32 = flow->nw_src;
834         break;
835
836     case MFF_ARP_TPA:
837         value->be32 = flow->nw_dst;
838         break;
839
840     case MFF_ARP_SHA:
841     case MFF_ND_SLL:
842         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
843         break;
844
845     case MFF_ARP_THA:
846     case MFF_ND_TLL:
847         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
848         break;
849
850     case MFF_TCP_SRC:
851         value->be16 = flow->tp_src;
852         break;
853
854     case MFF_TCP_DST:
855         value->be16 = flow->tp_dst;
856         break;
857
858     case MFF_UDP_SRC:
859         value->be16 = flow->tp_src;
860         break;
861
862     case MFF_UDP_DST:
863         value->be16 = flow->tp_dst;
864         break;
865
866     case MFF_ICMP_TYPE:
867         value->u8 = ntohs(flow->tp_src);
868         break;
869
870     case MFF_ICMP_CODE:
871         value->u8 = ntohs(flow->tp_dst);
872         break;
873
874     case MFF_ND_TARGET:
875         value->ipv6 = flow->nd_target;
876         break;
877
878     case MFF_N_IDS:
879     default:
880         NOT_REACHED();
881     }
882 }
883
884 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
885  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
886  * prerequisites. */
887 void
888 mf_set_value(const struct mf_field *mf,
889              const union mf_value *value, struct cls_rule *rule)
890 {
891     switch (mf->id) {
892     case MFF_TUN_ID:
893         cls_rule_set_tun_id(rule, value->be64);
894         break;
895
896     case MFF_IN_PORT:
897         cls_rule_set_in_port(rule, ntohs(value->be16));
898         break;
899
900 #if FLOW_N_REGS > 0
901     case MFF_REG0:
902 #endif
903 #if FLOW_N_REGS > 1
904     case MFF_REG1:
905 #endif
906 #if FLOW_N_REGS > 2
907     case MFF_REG2:
908 #endif
909 #if FLOW_N_REGS > 3
910     case MFF_REG3:
911 #endif
912 #if FLOW_N_REGS > 4
913     case MFF_REG4:
914 #endif
915 #if FLOW_N_REGS > 5
916 #error
917 #endif
918 #if FLOW_N_REGS > 0
919         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
920         break;
921 #endif
922
923     case MFF_ETH_SRC:
924         cls_rule_set_dl_src(rule, value->mac);
925         break;
926
927     case MFF_ETH_DST:
928         cls_rule_set_dl_dst(rule, value->mac);
929         break;
930
931     case MFF_ETH_TYPE:
932         cls_rule_set_dl_type(rule, value->be16);
933         break;
934
935     case MFF_VLAN_TCI:
936         cls_rule_set_dl_tci(rule, value->be16);
937         break;
938
939     case MFF_VLAN_VID:
940         cls_rule_set_dl_vlan(rule, value->be16);
941         break;
942
943     case MFF_VLAN_PCP:
944         cls_rule_set_dl_vlan_pcp(rule, value->u8);
945         break;
946
947     case MFF_IPV4_SRC:
948         cls_rule_set_nw_src(rule, value->be32);
949         break;
950
951     case MFF_IPV4_DST:
952         cls_rule_set_nw_dst(rule, value->be32);
953         break;
954
955     case MFF_IPV6_SRC:
956         cls_rule_set_ipv6_src(rule, &value->ipv6);
957         break;
958
959     case MFF_IPV6_DST:
960         cls_rule_set_ipv6_dst(rule, &value->ipv6);
961         break;
962
963     case MFF_IPV6_LABEL:
964         cls_rule_set_ipv6_label(rule, value->be32);
965         break;
966
967     case MFF_IP_PROTO:
968         cls_rule_set_nw_proto(rule, value->u8);
969         break;
970
971     case MFF_IP_DSCP:
972         cls_rule_set_nw_dscp(rule, value->u8);
973         break;
974
975     case MFF_IP_ECN:
976         cls_rule_set_nw_ecn(rule, value->u8);
977         break;
978
979     case MFF_IP_FRAG:
980         cls_rule_set_frag(rule, value->u8);
981         break;
982
983     case MFF_ARP_OP:
984         cls_rule_set_nw_proto(rule, ntohs(value->be16));
985         break;
986
987     case MFF_ARP_SPA:
988         cls_rule_set_nw_src(rule, value->be32);
989         break;
990
991     case MFF_ARP_TPA:
992         cls_rule_set_nw_dst(rule, value->be32);
993         break;
994
995     case MFF_ARP_SHA:
996     case MFF_ND_SLL:
997         cls_rule_set_arp_sha(rule, value->mac);
998         break;
999
1000     case MFF_ARP_THA:
1001     case MFF_ND_TLL:
1002         cls_rule_set_arp_tha(rule, value->mac);
1003         break;
1004
1005     case MFF_TCP_SRC:
1006         cls_rule_set_tp_src(rule, value->be16);
1007         break;
1008
1009     case MFF_TCP_DST:
1010         cls_rule_set_tp_dst(rule, value->be16);
1011         break;
1012
1013     case MFF_UDP_SRC:
1014         cls_rule_set_tp_src(rule, value->be16);
1015         break;
1016
1017     case MFF_UDP_DST:
1018         cls_rule_set_tp_dst(rule, value->be16);
1019         break;
1020
1021     case MFF_ICMP_TYPE:
1022         cls_rule_set_icmp_type(rule, value->u8);
1023         break;
1024
1025     case MFF_ICMP_CODE:
1026         cls_rule_set_icmp_code(rule, value->u8);
1027         break;
1028
1029     case MFF_ND_TARGET:
1030         cls_rule_set_nd_target(rule, &value->ipv6);
1031         break;
1032
1033     case MFF_N_IDS:
1034     default:
1035         NOT_REACHED();
1036     }
1037 }
1038
1039 /* Makes 'rule' wildcard field 'mf'.
1040  *
1041  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1042  * prerequisites. */
1043 void
1044 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1045 {
1046     switch (mf->id) {
1047     case MFF_TUN_ID:
1048         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1049         break;
1050
1051     case MFF_IN_PORT:
1052         rule->wc.wildcards |= FWW_IN_PORT;
1053         rule->flow.in_port = 0;
1054         break;
1055
1056 #if FLOW_N_REGS > 0
1057     case MFF_REG0:
1058         cls_rule_set_reg_masked(rule, 0, 0, 0);
1059         break;
1060 #endif
1061 #if FLOW_N_REGS > 1
1062     case MFF_REG1:
1063         cls_rule_set_reg_masked(rule, 1, 0, 0);
1064         break;
1065 #endif
1066 #if FLOW_N_REGS > 2
1067     case MFF_REG2:
1068         cls_rule_set_reg_masked(rule, 2, 0, 0);
1069         break;
1070 #endif
1071 #if FLOW_N_REGS > 3
1072     case MFF_REG3:
1073         cls_rule_set_reg_masked(rule, 3, 0, 0);
1074         break;
1075 #endif
1076 #if FLOW_N_REGS > 4
1077     case MFF_REG4:
1078         cls_rule_set_reg_masked(rule, 4, 0, 0);
1079         break;
1080 #endif
1081 #if FLOW_N_REGS > 5
1082 #error
1083 #endif
1084
1085     case MFF_ETH_SRC:
1086         rule->wc.wildcards |= FWW_DL_SRC;
1087         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1088         break;
1089
1090     case MFF_ETH_DST:
1091         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1092         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1093         break;
1094
1095     case MFF_ETH_TYPE:
1096         rule->wc.wildcards |= FWW_DL_TYPE;
1097         rule->flow.dl_type = htons(0);
1098         break;
1099
1100     case MFF_VLAN_TCI:
1101         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1102         break;
1103
1104     case MFF_VLAN_VID:
1105         cls_rule_set_any_vid(rule);
1106         break;
1107
1108     case MFF_VLAN_PCP:
1109         cls_rule_set_any_pcp(rule);
1110         break;
1111
1112     case MFF_IPV4_SRC:
1113     case MFF_ARP_SPA:
1114         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1115         break;
1116
1117     case MFF_IPV4_DST:
1118     case MFF_ARP_TPA:
1119         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1120         break;
1121
1122     case MFF_IPV6_SRC:
1123         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1124         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1125         break;
1126
1127     case MFF_IPV6_DST:
1128         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1129         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1130         break;
1131
1132     case MFF_IPV6_LABEL:
1133         rule->wc.wildcards |= FWW_IPV6_LABEL;
1134         rule->flow.ipv6_label = 0;
1135         break;
1136
1137     case MFF_IP_PROTO:
1138         rule->wc.wildcards |= FWW_NW_PROTO;
1139         rule->flow.nw_proto = 0;
1140         break;
1141
1142     case MFF_IP_DSCP:
1143         rule->wc.tos_mask |= IP_DSCP_MASK;
1144         rule->flow.tos &= ~IP_DSCP_MASK;
1145         break;
1146
1147     case MFF_IP_ECN:
1148         rule->wc.tos_mask |= IP_ECN_MASK;
1149         rule->flow.tos &= ~IP_ECN_MASK;
1150         break;
1151
1152     case MFF_IP_FRAG:
1153         rule->wc.frag_mask |= FLOW_FRAG_MASK;
1154         rule->flow.frag &= ~FLOW_FRAG_MASK;
1155         break;
1156
1157     case MFF_ARP_OP:
1158         rule->wc.wildcards |= FWW_NW_PROTO;
1159         rule->flow.nw_proto = 0;
1160         break;
1161
1162     case MFF_ARP_SHA:
1163     case MFF_ND_SLL:
1164         rule->wc.wildcards |= FWW_ARP_SHA;
1165         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1166         break;
1167
1168     case MFF_ARP_THA:
1169     case MFF_ND_TLL:
1170         rule->wc.wildcards |= FWW_ARP_THA;
1171         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1172         break;
1173
1174     case MFF_TCP_SRC:
1175     case MFF_UDP_SRC:
1176     case MFF_ICMP_TYPE:
1177         rule->wc.wildcards |= FWW_TP_SRC;
1178         rule->flow.tp_src = htons(0);
1179         break;
1180
1181     case MFF_TCP_DST:
1182     case MFF_UDP_DST:
1183     case MFF_ICMP_CODE:
1184         rule->wc.wildcards |= FWW_TP_DST;
1185         rule->flow.tp_dst = htons(0);
1186         break;
1187
1188     case MFF_ND_TARGET:
1189         rule->wc.wildcards |= FWW_ND_TARGET;
1190         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1191         break;
1192
1193     case MFF_N_IDS:
1194     default:
1195         NOT_REACHED();
1196     }
1197 }
1198
1199 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1200  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1201  * with a 1-bit indicating that the corresponding value bit must match and a
1202  * 0-bit indicating a don't-care.
1203  *
1204  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1205  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1206  * call is equivalent to mf_set_wild(mf, rule).
1207  *
1208  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1209  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1210 void
1211 mf_set(const struct mf_field *mf,
1212        const union mf_value *value, const union mf_value *mask,
1213        struct cls_rule *rule)
1214 {
1215     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1216         mf_set_value(mf, value, rule);
1217         return;
1218     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1219         mf_set_wild(mf, rule);
1220         return;
1221     }
1222
1223     switch (mf->id) {
1224     case MFF_IN_PORT:
1225     case MFF_ETH_SRC:
1226     case MFF_ETH_TYPE:
1227     case MFF_VLAN_VID:
1228     case MFF_VLAN_PCP:
1229     case MFF_IPV6_LABEL:
1230     case MFF_IP_PROTO:
1231     case MFF_IP_DSCP:
1232     case MFF_IP_ECN:
1233     case MFF_ARP_OP:
1234     case MFF_ARP_SHA:
1235     case MFF_ARP_THA:
1236     case MFF_TCP_SRC:
1237     case MFF_TCP_DST:
1238     case MFF_UDP_SRC:
1239     case MFF_UDP_DST:
1240     case MFF_ICMP_TYPE:
1241     case MFF_ICMP_CODE:
1242     case MFF_ND_TARGET:
1243     case MFF_ND_SLL:
1244     case MFF_ND_TLL:
1245         NOT_REACHED();
1246
1247     case MFF_TUN_ID:
1248         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1249         break;
1250
1251 #if FLOW_N_REGS > 0
1252     case MFF_REG0:
1253 #endif
1254 #if FLOW_N_REGS > 1
1255     case MFF_REG1:
1256 #endif
1257 #if FLOW_N_REGS > 2
1258     case MFF_REG2:
1259 #endif
1260 #if FLOW_N_REGS > 3
1261     case MFF_REG3:
1262 #endif
1263 #if FLOW_N_REGS > 4
1264     case MFF_REG4:
1265 #endif
1266 #if FLOW_N_REGS > 5
1267 #error
1268 #endif
1269         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1270                                 ntohl(value->be32), ntohl(mask->be32));
1271         break;
1272
1273     case MFF_ETH_DST:
1274         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1275             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1276         }
1277         break;
1278
1279     case MFF_VLAN_TCI:
1280         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1281         break;
1282
1283     case MFF_IPV4_SRC:
1284         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1285         break;
1286
1287     case MFF_IPV4_DST:
1288         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1289         break;
1290
1291     case MFF_IPV6_SRC:
1292         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1293         break;
1294
1295     case MFF_IPV6_DST:
1296         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1297         break;
1298
1299     case MFF_IP_FRAG:
1300         cls_rule_set_frag_masked(rule, value->u8, mask->u8);
1301         break;
1302
1303     case MFF_ARP_SPA:
1304         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1305         break;
1306
1307     case MFF_ARP_TPA:
1308         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1309         break;
1310
1311     case MFF_N_IDS:
1312     default:
1313         NOT_REACHED();
1314     }
1315 }
1316
1317 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1318  * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1319  * 'x'.
1320  *
1321  * Example: suppose that 'mf' is originally the following 2-byte field in
1322  * 'rule':
1323  *
1324  *     value == 0xe00a == 2#1110000000001010
1325  *      mask == 0xfc3f == 2#1111110000111111
1326  *
1327  * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1328  * effect (note that 0x55 is 2#1010101):
1329  *
1330  *     value == 0xd50a == 2#1101010100001010
1331  *      mask == 0xff3f == 2#1111111100111111
1332  *
1333  * The caller is responsible for ensuring that the result will be a valid
1334  * wildcard pattern for 'mf'.  The caller is responsible for ensuring that
1335  * 'rule' meets 'mf''s prerequisites. */
1336 void
1337 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1338                 unsigned int n_bits, struct cls_rule *rule)
1339 {
1340     if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1341         union mf_value value;
1342         int i;
1343
1344         for (i = mf->n_bytes - 1; i >= 0; i--) {
1345             ((uint8_t *) &value)[i] = x;
1346             x >>= 8;
1347         }
1348         mf_set_value(mf, &value, rule);
1349     } else {
1350         union mf_value value, mask;
1351         uint8_t *vp, *mp;
1352         unsigned int byte_ofs;
1353
1354         mf_get(mf, rule, &value, &mask);
1355
1356         byte_ofs = mf->n_bytes - ofs / 8;
1357         vp = &((uint8_t *) &value)[byte_ofs];
1358         mp = &((uint8_t *) &mask)[byte_ofs];
1359         if (ofs % 8) {
1360             unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1361             uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1362
1363             *--vp &= ~chunk_mask;
1364             *vp   |= chunk_mask & (x << (ofs % 8));
1365             *--mp |= chunk_mask;
1366
1367             x >>= chunk;
1368             n_bits -= chunk;
1369             ofs += chunk;
1370         }
1371         while (n_bits >= 8) {
1372             *--vp = x;
1373             *--mp = 0xff;
1374             x >>= 8;
1375             n_bits -= 8;
1376             ofs += 8;
1377         }
1378         if (n_bits) {
1379             uint8_t chunk_mask = (1 << n_bits) - 1;
1380
1381             *--vp &= ~chunk_mask;
1382             *vp   |= chunk_mask & x;
1383             *--mp |= chunk_mask;
1384         }
1385
1386         mf_set(mf, &value, &mask, rule);
1387     }
1388 }
1389
1390 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1391  * 'value' and 'mask', respectively. */
1392 void
1393 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1394        union mf_value *value, union mf_value *mask)
1395 {
1396     mf_get_value(mf, &rule->flow, value);
1397     mf_get_mask(mf, &rule->wc, mask);
1398 }
1399
1400 /* Assigns a random value for field 'mf' to 'value'. */
1401 void
1402 mf_random_value(const struct mf_field *mf, union mf_value *value)
1403 {
1404     random_bytes(value, mf->n_bytes);
1405
1406     switch (mf->id) {
1407     case MFF_TUN_ID:
1408     case MFF_IN_PORT:
1409 #if FLOW_N_REGS > 0
1410     case MFF_REG0:
1411 #endif
1412 #if FLOW_N_REGS > 1
1413     case MFF_REG1:
1414 #endif
1415 #if FLOW_N_REGS > 2
1416     case MFF_REG2:
1417 #endif
1418 #if FLOW_N_REGS > 3
1419     case MFF_REG3:
1420 #endif
1421 #if FLOW_N_REGS > 4
1422     case MFF_REG4:
1423 #endif
1424 #if FLOW_N_REGS > 5
1425 #error
1426 #endif
1427     case MFF_ETH_SRC:
1428     case MFF_ETH_DST:
1429     case MFF_ETH_TYPE:
1430     case MFF_VLAN_TCI:
1431     case MFF_IPV4_SRC:
1432     case MFF_IPV4_DST:
1433     case MFF_IPV6_SRC:
1434     case MFF_IPV6_DST:
1435     case MFF_IP_PROTO:
1436     case MFF_ARP_SPA:
1437     case MFF_ARP_TPA:
1438     case MFF_ARP_SHA:
1439     case MFF_ARP_THA:
1440     case MFF_TCP_SRC:
1441     case MFF_TCP_DST:
1442     case MFF_UDP_SRC:
1443     case MFF_UDP_DST:
1444     case MFF_ICMP_TYPE:
1445     case MFF_ICMP_CODE:
1446     case MFF_ND_TARGET:
1447     case MFF_ND_SLL:
1448     case MFF_ND_TLL:
1449         break;
1450
1451     case MFF_IPV6_LABEL:
1452         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1453         break;
1454
1455     case MFF_IP_DSCP:
1456         value->u8 &= IP_DSCP_MASK;
1457         break;
1458
1459     case MFF_IP_ECN:
1460         value->u8 &= IP_ECN_MASK;
1461         break;
1462
1463     case MFF_IP_FRAG:
1464         value->u8 &= FLOW_FRAG_MASK;
1465         break;
1466
1467     case MFF_ARP_OP:
1468         value->be16 &= htons(0xff);
1469         break;
1470
1471     case MFF_VLAN_VID:
1472         value->be16 &= htons(VLAN_VID_MASK);
1473         break;
1474
1475     case MFF_VLAN_PCP:
1476         value->u8 &= 0x07;
1477         break;
1478
1479     case MFF_N_IDS:
1480     default:
1481         NOT_REACHED();
1482     }
1483 }
1484
1485 static char *
1486 mf_from_integer_string(const struct mf_field *mf, const char *s,
1487                        uint8_t *valuep, uint8_t *maskp)
1488 {
1489     unsigned long long int integer, mask;
1490     char *tail;
1491     int i;
1492
1493     errno = 0;
1494     integer = strtoull(s, &tail, 0);
1495     if (errno || (*tail != '\0' && *tail != '/')) {
1496         goto syntax_error;
1497     }
1498
1499     if (*tail == '/') {
1500         mask = strtoull(tail + 1, &tail, 0);
1501         if (errno || *tail != '\0') {
1502             goto syntax_error;
1503         }
1504     } else {
1505         mask = ULLONG_MAX;
1506     }
1507
1508     for (i = mf->n_bytes - 1; i >= 0; i--) {
1509         valuep[i] = integer;
1510         maskp[i] = mask;
1511         integer >>= 8;
1512         mask >>= 8;
1513     }
1514     if (integer) {
1515         return xasprintf("%s: value too large for %u-byte field %s",
1516                          s, mf->n_bytes, mf->name);
1517     }
1518     return NULL;
1519
1520 syntax_error:
1521     return xasprintf("%s: bad syntax for %s", s, mf->name);
1522 }
1523
1524 static char *
1525 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1526                         uint8_t mac[ETH_ADDR_LEN],
1527                         uint8_t mask[ETH_ADDR_LEN])
1528 {
1529     assert(mf->n_bytes == ETH_ADDR_LEN);
1530
1531     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1532                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1533     case ETH_ADDR_SCAN_COUNT * 2:
1534         return NULL;
1535
1536     case ETH_ADDR_SCAN_COUNT:
1537         memset(mask, 0xff, ETH_ADDR_LEN);
1538         return NULL;
1539
1540     default:
1541         return xasprintf("%s: invalid Ethernet address", s);
1542     }
1543 }
1544
1545 static char *
1546 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1547                     ovs_be32 *ip, ovs_be32 *mask)
1548 {
1549     int prefix;
1550
1551     assert(mf->n_bytes == sizeof *ip);
1552
1553     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1554                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1555         /* OK. */
1556     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1557                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1558         if (prefix <= 0 || prefix > 32) {
1559             return xasprintf("%s: network prefix bits not between 1 and "
1560                              "32", s);
1561         } else if (prefix == 32) {
1562             *mask = htonl(UINT32_MAX);
1563         } else {
1564             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1565         }
1566     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1567         *mask = htonl(UINT32_MAX);
1568     } else {
1569         return xasprintf("%s: invalid IP address", s);
1570     }
1571     return NULL;
1572 }
1573
1574 static char *
1575 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1576                     struct in6_addr *value, struct in6_addr *mask)
1577 {
1578     char *str = xstrdup(s);
1579     char *save_ptr = NULL;
1580     const char *name, *netmask;
1581     int retval;
1582
1583     assert(mf->n_bytes == sizeof *value);
1584
1585     name = strtok_r(str, "/", &save_ptr);
1586     retval = name ? lookup_ipv6(name, value) : EINVAL;
1587     if (retval) {
1588         char *err;
1589
1590         err = xasprintf("%s: could not convert to IPv6 address", str);
1591         free(str);
1592
1593         return err;
1594     }
1595
1596     netmask = strtok_r(NULL, "/", &save_ptr);
1597     if (netmask) {
1598         int prefix = atoi(netmask);
1599         if (prefix <= 0 || prefix > 128) {
1600             free(str);
1601             return xasprintf("%s: prefix bits not between 1 and 128", s);
1602         } else {
1603             *mask = ipv6_create_mask(prefix);
1604         }
1605     } else {
1606         *mask = in6addr_exact;
1607     }
1608     free(str);
1609
1610     return NULL;
1611 }
1612
1613 static char *
1614 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1615                         ovs_be16 *valuep, ovs_be16 *maskp)
1616 {
1617     uint16_t port;
1618
1619     assert(mf->n_bytes == sizeof(ovs_be16));
1620     if (ofputil_port_from_string(s, &port)) {
1621         *valuep = htons(port);
1622         *maskp = htons(UINT16_MAX);
1623         return NULL;
1624     } else {
1625         return mf_from_integer_string(mf, s,
1626                                       (uint8_t *) valuep, (uint8_t *) maskp);
1627     }
1628 }
1629
1630 struct frag_handling {
1631     const char *name;
1632     uint8_t mask;
1633     uint8_t value;
1634 };
1635
1636 static const struct frag_handling all_frags[] = {
1637 #define A FLOW_FRAG_ANY
1638 #define L FLOW_FRAG_LATER
1639     /* name               mask  value */
1640
1641     { "no",               A|L,  0     },
1642     { "first",            A|L,  A     },
1643     { "later",            A|L,  A|L   },
1644
1645     { "no",               A,    0     },
1646     { "yes",              A,    A     },
1647
1648     { "not_later",        L,    0     },
1649     { "later",            L,    L     },
1650 #undef A
1651 #undef L
1652 };
1653
1654 static char *
1655 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1656 {
1657     const struct frag_handling *h;
1658
1659     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1660         if (!strcasecmp(s, h->name)) {
1661             /* We force the upper bits of the mask on to make mf_parse_value()
1662              * happy (otherwise it will never think it's an exact match.) */
1663             *maskp = h->mask | ~FLOW_FRAG_MASK;
1664             *valuep = h->value;
1665             return NULL;
1666         }
1667     }
1668
1669     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1670                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1671 }
1672
1673 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1674  * NULL if successful, otherwise a malloc()'d string describing the error. */
1675 char *
1676 mf_parse(const struct mf_field *mf, const char *s,
1677          union mf_value *value, union mf_value *mask)
1678 {
1679     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1680         memset(value, 0, mf->n_bytes);
1681         memset(mask, 0, mf->n_bytes);
1682         return NULL;
1683     }
1684
1685     switch (mf->string) {
1686     case MFS_DECIMAL:
1687     case MFS_HEXADECIMAL:
1688         return mf_from_integer_string(mf, s,
1689                                       (uint8_t *) value, (uint8_t *) mask);
1690
1691     case MFS_ETHERNET:
1692         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1693
1694     case MFS_IPV4:
1695         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1696
1697     case MFS_IPV6:
1698         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1699
1700     case MFS_OFP_PORT:
1701         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1702
1703     case MFS_FRAG:
1704         return mf_from_frag_string(s, &value->u8, &mask->u8);
1705     }
1706     NOT_REACHED();
1707 }
1708
1709 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1710  * successful, otherwise a malloc()'d string describing the error. */
1711 char *
1712 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1713 {
1714     union mf_value mask;
1715     char *error;
1716
1717     error = mf_parse(mf, s, value, &mask);
1718     if (error) {
1719         return error;
1720     }
1721
1722     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1723         return xasprintf("%s: wildcards not allowed here", s);
1724     }
1725     return NULL;
1726 }
1727
1728 static void
1729 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1730                          const uint8_t *maskp, struct ds *s)
1731 {
1732     unsigned long long int integer;
1733     int i;
1734
1735     assert(mf->n_bytes <= 8);
1736
1737     integer = 0;
1738     for (i = 0; i < mf->n_bytes; i++) {
1739         integer = (integer << 8) | valuep[i];
1740     }
1741     if (mf->string == MFS_HEXADECIMAL) {
1742         ds_put_format(s, "%#llx", integer);
1743     } else {
1744         ds_put_format(s, "%lld", integer);
1745     }
1746
1747     if (maskp) {
1748         unsigned long long int mask;
1749
1750         mask = 0;
1751         for (i = 0; i < mf->n_bytes; i++) {
1752             mask = (mask << 8) | maskp[i];
1753         }
1754
1755         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1756          * not sure that that a bit-mask written in decimal is ever easier to
1757          * understand than the same bit-mask written in hexadecimal. */
1758         ds_put_format(s, "/%#llx", mask);
1759     }
1760 }
1761
1762 static void
1763 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1764                       struct ds *s)
1765 {
1766     const struct frag_handling *h;
1767     uint8_t value = *valuep;
1768     uint8_t mask = *maskp;
1769
1770     value &= mask;
1771     mask &= FLOW_FRAG_MASK;
1772
1773     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1774         if (value == h->value && mask == h->mask) {
1775             ds_put_cstr(s, h->name);
1776             return;
1777         }
1778     }
1779     ds_put_cstr(s, "<error>");
1780 }
1781
1782 /* Appends to 's' a string representation of field 'mf' whose value is in
1783  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
1784 void
1785 mf_format(const struct mf_field *mf,
1786           const union mf_value *value, const union mf_value *mask,
1787           struct ds *s)
1788 {
1789     if (mask) {
1790         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1791             ds_put_cstr(s, "ANY");
1792             return;
1793         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1794             mask = NULL;
1795         }
1796     }
1797
1798     switch (mf->string) {
1799     case MFS_OFP_PORT:
1800         if (!mask) {
1801             ofputil_format_port(ntohs(value->be16), s);
1802             break;
1803         }
1804         /* fall through */
1805     case MFS_DECIMAL:
1806     case MFS_HEXADECIMAL:
1807         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1808         break;
1809
1810     case MFS_ETHERNET:
1811         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1812         if (mask) {
1813             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1814         }
1815         break;
1816
1817     case MFS_IPV4:
1818         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1819                          s);
1820         break;
1821
1822     case MFS_IPV6:
1823         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1824         break;
1825
1826     case MFS_FRAG:
1827         mf_format_frag_string(&value->u8, &mask->u8, s);
1828         break;
1829
1830     default:
1831         NOT_REACHED();
1832     }
1833 }