meta-flow: New functions for reading and writing generalized subfields.
[cascardo/ovs.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011, 2012 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-errors.h"
30 #include "ofp-util.h"
31 #include "packets.h"
32 #include "random.h"
33 #include "shash.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
39
40 #define MF_FIELD_SIZES(MEMBER)                  \
41     sizeof ((union mf_value *)0)->MEMBER,       \
42     8 * sizeof ((union mf_value *)0)->MEMBER
43
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
45     /* ## -------- ## */
46     /* ## metadata ## */
47     /* ## -------- ## */
48
49     {
50         MFF_TUN_ID, "tun_id", NULL,
51         MF_FIELD_SIZES(be64),
52         MFM_FULLY, 0,
53         MFS_HEXADECIMAL,
54         MFP_NONE,
55         true,
56         NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57     }, {
58         MFF_IN_PORT, "in_port", NULL,
59         MF_FIELD_SIZES(be16),
60         MFM_NONE, FWW_IN_PORT,
61         MFS_OFP_PORT,
62         MFP_NONE,
63         false,
64         NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
65     },
66
67 #define REGISTER(IDX)                           \
68     {                                           \
69         MFF_REG##IDX, "reg" #IDX, NULL,         \
70         MF_FIELD_SIZES(be32),                   \
71         MFM_FULLY, 0,                           \
72         MFS_HEXADECIMAL,                        \
73         MFP_NONE,                               \
74         true,                                   \
75         NXM_NX_REG(IDX),                        \
76         "NXM_NX_REG" #IDX                       \
77     }
78 #if FLOW_N_REGS > 0
79     REGISTER(0),
80 #endif
81 #if FLOW_N_REGS > 1
82     REGISTER(1),
83 #endif
84 #if FLOW_N_REGS > 2
85     REGISTER(2),
86 #endif
87 #if FLOW_N_REGS > 3
88     REGISTER(3),
89 #endif
90 #if FLOW_N_REGS > 4
91     REGISTER(4),
92 #endif
93 #if FLOW_N_REGS > 5
94     REGISTER(5),
95 #endif
96 #if FLOW_N_REGS > 6
97     REGISTER(6),
98 #endif
99 #if FLOW_N_REGS > 7
100     REGISTER(7),
101 #endif
102 #if FLOW_N_REGS > 8
103 #error
104 #endif
105
106     /* ## -- ## */
107     /* ## L2 ## */
108     /* ## -- ## */
109
110     {
111         MFF_ETH_SRC, "eth_src", "dl_src",
112         MF_FIELD_SIZES(mac),
113         MFM_NONE, FWW_DL_SRC,
114         MFS_ETHERNET,
115         MFP_NONE,
116         true,
117         NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
118     }, {
119         MFF_ETH_DST, "eth_dst", "dl_dst",
120         MF_FIELD_SIZES(mac),
121         MFM_MCAST, 0,
122         MFS_ETHERNET,
123         MFP_NONE,
124         true,
125         NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
126     }, {
127         MFF_ETH_TYPE, "eth_type", "dl_type",
128         MF_FIELD_SIZES(be16),
129         MFM_NONE, FWW_DL_TYPE,
130         MFS_HEXADECIMAL,
131         MFP_NONE,
132         false,
133         NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
134     },
135
136     {
137         MFF_VLAN_TCI, "vlan_tci", NULL,
138         MF_FIELD_SIZES(be16),
139         MFM_FULLY, 0,
140         MFS_HEXADECIMAL,
141         MFP_NONE,
142         true,
143         NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
144     }, {
145         MFF_VLAN_VID, "dl_vlan", NULL,
146         sizeof(ovs_be16), 12,
147         MFM_NONE, 0,
148         MFS_DECIMAL,
149         MFP_NONE,
150         true,
151         0, NULL
152     }, {
153         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
154         1, 3,
155         MFM_NONE, 0,
156         MFS_DECIMAL,
157         MFP_NONE,
158         true,
159         0, NULL
160     },
161
162     /* ## -- ## */
163     /* ## L3 ## */
164     /* ## -- ## */
165
166     {
167         MFF_IPV4_SRC, "ip_src", "nw_src",
168         MF_FIELD_SIZES(be32),
169         MFM_CIDR, 0,
170         MFS_IPV4,
171         MFP_IPV4,
172         true,
173         NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
174     }, {
175         MFF_IPV4_DST, "ip_dst", "nw_dst",
176         MF_FIELD_SIZES(be32),
177         MFM_CIDR, 0,
178         MFS_IPV4,
179         MFP_IPV4,
180         true,
181         NXM_OF_IP_DST, "NXM_OF_IP_DST",
182     },
183
184     {
185         MFF_IPV6_SRC, "ipv6_src", NULL,
186         MF_FIELD_SIZES(ipv6),
187         MFM_CIDR, 0,
188         MFS_IPV6,
189         MFP_IPV6,
190         true,
191         NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
192     }, {
193         MFF_IPV6_DST, "ipv6_dst", NULL,
194         MF_FIELD_SIZES(ipv6),
195         MFM_CIDR, 0,
196         MFS_IPV6,
197         MFP_IPV6,
198         true,
199         NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
200     },
201     {
202         MFF_IPV6_LABEL, "ipv6_label", NULL,
203         4, 20,
204         MFM_NONE, FWW_IPV6_LABEL,
205         MFS_HEXADECIMAL,
206         MFP_IPV6,
207         false,
208         NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
209     },
210
211     {
212         MFF_IP_PROTO, "nw_proto", NULL,
213         MF_FIELD_SIZES(u8),
214         MFM_NONE, FWW_NW_PROTO,
215         MFS_DECIMAL,
216         MFP_IP_ANY,
217         false,
218         NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
219     }, {
220         MFF_IP_DSCP, "nw_tos", NULL,
221         MF_FIELD_SIZES(u8),
222         MFM_NONE, FWW_NW_DSCP,
223         MFS_DECIMAL,
224         MFP_IP_ANY,
225         true,
226         NXM_OF_IP_TOS, "NXM_OF_IP_TOS"
227     }, {
228         MFF_IP_ECN, "nw_ecn", NULL,
229         1, 2,
230         MFM_NONE, FWW_NW_ECN,
231         MFS_DECIMAL,
232         MFP_IP_ANY,
233         true,
234         NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
235     }, {
236         MFF_IP_TTL, "nw_ttl", NULL,
237         MF_FIELD_SIZES(u8),
238         MFM_NONE, FWW_NW_TTL,
239         MFS_DECIMAL,
240         MFP_IP_ANY,
241         true,
242         NXM_NX_IP_TTL, "NXM_NX_IP_TTL"
243     }, {
244         MFF_IP_FRAG, "ip_frag", NULL,
245         1, 2,
246         MFM_FULLY, 0,
247         MFS_FRAG,
248         MFP_IP_ANY,
249         false,
250         NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG"
251     },
252
253     {
254         MFF_ARP_OP, "arp_op", NULL,
255         MF_FIELD_SIZES(be16),
256         MFM_NONE, FWW_NW_PROTO,
257         MFS_DECIMAL,
258         MFP_ARP,
259         false,
260         NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
261     }, {
262         MFF_ARP_SPA, "arp_spa", NULL,
263         MF_FIELD_SIZES(be32),
264         MFM_CIDR, 0,
265         MFS_IPV4,
266         MFP_ARP,
267         false,
268         NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
269     }, {
270         MFF_ARP_TPA, "arp_tpa", NULL,
271         MF_FIELD_SIZES(be32),
272         MFM_CIDR, 0,
273         MFS_IPV4,
274         MFP_ARP,
275         false,
276         NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
277     }, {
278         MFF_ARP_SHA, "arp_sha", NULL,
279         MF_FIELD_SIZES(mac),
280         MFM_NONE, FWW_ARP_SHA,
281         MFS_ETHERNET,
282         MFP_ARP,
283         false,
284         NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
285     }, {
286         MFF_ARP_THA, "arp_tha", NULL,
287         MF_FIELD_SIZES(mac),
288         MFM_NONE, FWW_ARP_THA,
289         MFS_ETHERNET,
290         MFP_ARP,
291         false,
292         NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
293     },
294
295     /* ## -- ## */
296     /* ## L4 ## */
297     /* ## -- ## */
298
299     {
300         MFF_TCP_SRC, "tcp_src", "tp_src",
301         MF_FIELD_SIZES(be16),
302         MFM_FULLY, 0,
303         MFS_DECIMAL,
304         MFP_TCP,
305         true,
306         NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
307     }, {
308         MFF_TCP_DST, "tcp_dst", "tp_dst",
309         MF_FIELD_SIZES(be16),
310         MFM_FULLY, 0,
311         MFS_DECIMAL,
312         MFP_TCP,
313         true,
314         NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
315     },
316
317     {
318         MFF_UDP_SRC, "udp_src", NULL,
319         MF_FIELD_SIZES(be16),
320         MFM_FULLY, 0,
321         MFS_DECIMAL,
322         MFP_UDP,
323         true,
324         NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
325     }, {
326         MFF_UDP_DST, "udp_dst", NULL,
327         MF_FIELD_SIZES(be16),
328         MFM_FULLY, 0,
329         MFS_DECIMAL,
330         MFP_UDP,
331         true,
332         NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
333     },
334
335     {
336         MFF_ICMPV4_TYPE, "icmp_type", NULL,
337         MF_FIELD_SIZES(u8),
338         MFM_NONE, 0,
339         MFS_DECIMAL,
340         MFP_ICMPV4,
341         false,
342         NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
343     }, {
344         MFF_ICMPV4_CODE, "icmp_code", NULL,
345         MF_FIELD_SIZES(u8),
346         MFM_NONE, 0,
347         MFS_DECIMAL,
348         MFP_ICMPV4,
349         false,
350         NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
351     },
352
353     {
354         MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
355         MF_FIELD_SIZES(u8),
356         MFM_NONE, 0,
357         MFS_DECIMAL,
358         MFP_ICMPV6,
359         false,
360         NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
361     }, {
362         MFF_ICMPV6_CODE, "icmpv6_code", NULL,
363         MF_FIELD_SIZES(u8),
364         MFM_NONE, 0,
365         MFS_DECIMAL,
366         MFP_ICMPV6,
367         false,
368         NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
369     },
370
371     /* ## ---- ## */
372     /* ## L"5" ## */
373     /* ## ---- ## */
374
375     {
376         MFF_ND_TARGET, "nd_target", NULL,
377         MF_FIELD_SIZES(ipv6),
378         MFM_NONE, FWW_ND_TARGET,
379         MFS_IPV6,
380         MFP_ND,
381         false,
382         NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
383     }, {
384         MFF_ND_SLL, "nd_sll", NULL,
385         MF_FIELD_SIZES(mac),
386         MFM_NONE, FWW_ARP_SHA,
387         MFS_ETHERNET,
388         MFP_ND_SOLICIT,
389         false,
390         NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
391     }, {
392         MFF_ND_TLL, "nd_tll", NULL,
393         MF_FIELD_SIZES(mac),
394         MFM_NONE, FWW_ARP_THA,
395         MFS_ETHERNET,
396         MFP_ND_ADVERT,
397         false,
398         NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
399     }
400 };
401
402 struct nxm_field {
403     struct hmap_node hmap_node;
404     uint32_t nxm_header;
405     const struct mf_field *mf;
406 };
407
408 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
409
410 /* Rate limit for parse errors.  These always indicate a bug in an OpenFlow
411  * controller and so there's not much point in showing a lot of them. */
412 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
413
414 /* Returns the field with the given 'id'. */
415 const struct mf_field *
416 mf_from_id(enum mf_field_id id)
417 {
418     assert((unsigned int) id < MFF_N_IDS);
419     return &mf_fields[id];
420 }
421
422 /* Returns the field with the given 'name', or a null pointer if no field has
423  * that name. */
424 const struct mf_field *
425 mf_from_name(const char *name)
426 {
427     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
428
429     if (shash_is_empty(&mf_by_name)) {
430         const struct mf_field *mf;
431
432         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
433             shash_add_once(&mf_by_name, mf->name, mf);
434             if (mf->extra_name) {
435                 shash_add_once(&mf_by_name, mf->extra_name, mf);
436             }
437         }
438     }
439
440     return shash_find_data(&mf_by_name, name);
441 }
442
443 static void
444 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
445 {
446     struct nxm_field *f;
447
448     f = xmalloc(sizeof *f);
449     hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
450     f->nxm_header = nxm_header;
451     f->mf = mf;
452 }
453
454 static void
455 nxm_init(void)
456 {
457     const struct mf_field *mf;
458
459     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
460         if (mf->nxm_header) {
461             add_nxm_field(mf->nxm_header, mf);
462             if (mf->maskable != MFM_NONE) {
463                 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
464             }
465         }
466     }
467
468 #ifndef NDEBUG
469     /* Verify that the header values are unique. */
470     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
471         if (mf->nxm_header) {
472             assert(mf_from_nxm_header(mf->nxm_header) == mf);
473             if (mf->maskable != MFM_NONE) {
474                 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
475                        == mf);
476             }
477         }
478     }
479 #endif
480 }
481
482 const struct mf_field *
483 mf_from_nxm_header(uint32_t header)
484 {
485     const struct nxm_field *f;
486
487     if (hmap_is_empty(&all_nxm_fields)) {
488         nxm_init();
489     }
490
491     HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
492                              &all_nxm_fields) {
493         if (f->nxm_header == header) {
494             return f->mf;
495         }
496     }
497
498     return NULL;
499 }
500
501 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
502  * specifies at least one bit in the field.
503  *
504  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
505  * meets 'mf''s prerequisites. */
506 bool
507 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
508 {
509     switch (mf->id) {
510     case MFF_IN_PORT:
511     case MFF_ETH_SRC:
512     case MFF_ETH_TYPE:
513     case MFF_IP_PROTO:
514     case MFF_IP_DSCP:
515     case MFF_IP_ECN:
516     case MFF_IP_TTL:
517     case MFF_IPV6_LABEL:
518     case MFF_ARP_OP:
519     case MFF_ARP_SHA:
520     case MFF_ARP_THA:
521     case MFF_ND_TARGET:
522     case MFF_ND_SLL:
523     case MFF_ND_TLL:
524         assert(mf->fww_bit != 0);
525         return (wc->wildcards & mf->fww_bit) != 0;
526
527     case MFF_TUN_ID:
528         return !wc->tun_id_mask;
529
530 #if FLOW_N_REGS > 0
531     case MFF_REG0:
532 #endif
533 #if FLOW_N_REGS > 1
534     case MFF_REG1:
535 #endif
536 #if FLOW_N_REGS > 2
537     case MFF_REG2:
538 #endif
539 #if FLOW_N_REGS > 3
540     case MFF_REG3:
541 #endif
542 #if FLOW_N_REGS > 4
543     case MFF_REG4:
544 #endif
545 #if FLOW_N_REGS > 5
546     case MFF_REG5:
547 #endif
548 #if FLOW_N_REGS > 6
549     case MFF_REG6:
550 #endif
551 #if FLOW_N_REGS > 7
552     case MFF_REG7:
553 #endif
554 #if FLOW_N_REGS > 8
555 #error
556 #endif
557         return !wc->reg_masks[mf->id - MFF_REG0];
558
559     case MFF_ETH_DST:
560         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
561                 == (FWW_ETH_MCAST | FWW_DL_DST));
562
563     case MFF_VLAN_TCI:
564         return !wc->vlan_tci_mask;
565     case MFF_VLAN_VID:
566         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
567     case MFF_VLAN_PCP:
568         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
569
570     case MFF_IPV4_SRC:
571         return !wc->nw_src_mask;
572     case MFF_IPV4_DST:
573         return !wc->nw_dst_mask;
574
575     case MFF_IPV6_SRC:
576         return ipv6_mask_is_any(&wc->ipv6_src_mask);
577     case MFF_IPV6_DST:
578         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
579
580     case MFF_IP_FRAG:
581         return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
582
583     case MFF_ARP_SPA:
584         return !wc->nw_src_mask;
585     case MFF_ARP_TPA:
586         return !wc->nw_dst_mask;
587
588     case MFF_TCP_SRC:
589     case MFF_UDP_SRC:
590     case MFF_ICMPV4_TYPE:
591     case MFF_ICMPV6_TYPE:
592         return !wc->tp_src_mask;
593     case MFF_TCP_DST:
594     case MFF_UDP_DST:
595     case MFF_ICMPV4_CODE:
596     case MFF_ICMPV6_CODE:
597         return !wc->tp_dst_mask;
598
599     case MFF_N_IDS:
600     default:
601         NOT_REACHED();
602     }
603 }
604
605 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
606  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
607  * purposes, or to 0 if it is wildcarded.
608  *
609  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
610  * meets 'mf''s prerequisites. */
611 void
612 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
613             union mf_value *mask)
614 {
615     switch (mf->id) {
616     case MFF_IN_PORT:
617     case MFF_ETH_SRC:
618     case MFF_ETH_TYPE:
619     case MFF_IP_PROTO:
620     case MFF_IP_DSCP:
621     case MFF_IP_ECN:
622     case MFF_IP_TTL:
623     case MFF_IPV6_LABEL:
624     case MFF_ARP_OP:
625     case MFF_ARP_SHA:
626     case MFF_ARP_THA:
627     case MFF_ND_TARGET:
628     case MFF_ND_SLL:
629     case MFF_ND_TLL:
630         assert(mf->fww_bit != 0);
631         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
632         break;
633
634     case MFF_TUN_ID:
635         mask->be64 = wc->tun_id_mask;
636         break;
637
638 #if FLOW_N_REGS > 0
639     case MFF_REG0:
640 #endif
641 #if FLOW_N_REGS > 1
642     case MFF_REG1:
643 #endif
644 #if FLOW_N_REGS > 2
645     case MFF_REG2:
646 #endif
647 #if FLOW_N_REGS > 3
648     case MFF_REG3:
649 #endif
650 #if FLOW_N_REGS > 4
651     case MFF_REG4:
652 #endif
653 #if FLOW_N_REGS > 5
654     case MFF_REG5:
655 #endif
656 #if FLOW_N_REGS > 6
657     case MFF_REG6:
658 #endif
659 #if FLOW_N_REGS > 7
660     case MFF_REG7:
661 #endif
662 #if FLOW_N_REGS > 8
663 #error
664 #endif
665         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
666         break;
667
668     case MFF_ETH_DST:
669         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
670                ETH_ADDR_LEN);
671         break;
672
673     case MFF_VLAN_TCI:
674         mask->be16 = wc->vlan_tci_mask;
675         break;
676     case MFF_VLAN_VID:
677         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
678         break;
679     case MFF_VLAN_PCP:
680         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
681         break;
682
683     case MFF_IPV4_SRC:
684         mask->be32 = wc->nw_src_mask;
685         break;
686     case MFF_IPV4_DST:
687         mask->be32 = wc->nw_dst_mask;
688         break;
689
690     case MFF_IPV6_SRC:
691         mask->ipv6 = wc->ipv6_src_mask;
692         break;
693     case MFF_IPV6_DST:
694         mask->ipv6 = wc->ipv6_dst_mask;
695         break;
696
697     case MFF_IP_FRAG:
698         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
699         break;
700
701     case MFF_ARP_SPA:
702         mask->be32 = wc->nw_src_mask;
703         break;
704     case MFF_ARP_TPA:
705         mask->be32 = wc->nw_dst_mask;
706         break;
707
708     case MFF_TCP_SRC:
709     case MFF_UDP_SRC:
710         mask->be16 = wc->tp_src_mask;
711         break;
712     case MFF_TCP_DST:
713     case MFF_UDP_DST:
714         mask->be16 = wc->tp_dst_mask;
715         break;
716
717     case MFF_ICMPV4_TYPE:
718     case MFF_ICMPV6_TYPE:
719         mask->u8 = ntohs(wc->tp_src_mask);
720         break;
721     case MFF_ICMPV4_CODE:
722     case MFF_ICMPV6_CODE:
723         mask->u8 = ntohs(wc->tp_dst_mask);
724         break;
725
726     case MFF_N_IDS:
727     default:
728         NOT_REACHED();
729     }
730 }
731
732 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
733  * if the mask is valid, false otherwise. */
734 bool
735 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
736 {
737     switch (mf->maskable) {
738     case MFM_NONE:
739         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
740                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
741
742     case MFM_FULLY:
743         return true;
744
745     case MFM_CIDR:
746         return (mf->n_bytes == 4
747                 ? ip_is_cidr(mask->be32)
748                 : ipv6_is_cidr(&mask->ipv6));
749
750     case MFM_MCAST:
751         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
752     }
753
754     NOT_REACHED();
755 }
756
757 static bool
758 is_ip_any(const struct flow *flow)
759 {
760     return (flow->dl_type == htons(ETH_TYPE_IP) ||
761             flow->dl_type == htons(ETH_TYPE_IPV6));
762 }
763
764 static bool
765 is_icmpv4(const struct flow *flow)
766 {
767     return (flow->dl_type == htons(ETH_TYPE_IP)
768             && flow->nw_proto == IPPROTO_ICMP);
769 }
770
771 static bool
772 is_icmpv6(const struct flow *flow)
773 {
774     return (flow->dl_type == htons(ETH_TYPE_IPV6)
775             && flow->nw_proto == IPPROTO_ICMPV6);
776 }
777
778 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
779 bool
780 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
781 {
782     switch (mf->prereqs) {
783     case MFP_NONE:
784         return true;
785
786     case MFP_ARP:
787         return flow->dl_type == htons(ETH_TYPE_ARP);
788     case MFP_IPV4:
789         return flow->dl_type == htons(ETH_TYPE_IP);
790     case MFP_IPV6:
791         return flow->dl_type == htons(ETH_TYPE_IPV6);
792     case MFP_IP_ANY:
793         return is_ip_any(flow);
794
795     case MFP_TCP:
796         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
797     case MFP_UDP:
798         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
799     case MFP_ICMPV4:
800         return is_icmpv4(flow);
801     case MFP_ICMPV6:
802         return is_icmpv6(flow);
803
804     case MFP_ND:
805         return (is_icmpv6(flow)
806                 && flow->tp_dst == htons(0)
807                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
808                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
809     case MFP_ND_SOLICIT:
810         return (is_icmpv6(flow)
811                 && flow->tp_dst == htons(0)
812                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
813     case MFP_ND_ADVERT:
814         return (is_icmpv6(flow)
815                 && flow->tp_dst == htons(0)
816                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
817     }
818
819     NOT_REACHED();
820 }
821
822 /* Returns true if 'value' may be a valid value *as part of a masked match*,
823  * false otherwise.
824  *
825  * A value is not rejected just because it is not valid for the field in
826  * question, but only if it doesn't make sense to test the bits in question at
827  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
828  * without the VLAN_CFI bit being set, but we can't reject those values because
829  * it is still legitimate to test just for those bits (see the documentation
830  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
831  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
832 bool
833 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
834 {
835     switch (mf->id) {
836     case MFF_TUN_ID:
837     case MFF_IN_PORT:
838 #if FLOW_N_REGS > 0
839     case MFF_REG0:
840 #endif
841 #if FLOW_N_REGS > 1
842     case MFF_REG1:
843 #endif
844 #if FLOW_N_REGS > 2
845     case MFF_REG2:
846 #endif
847 #if FLOW_N_REGS > 3
848     case MFF_REG3:
849 #endif
850 #if FLOW_N_REGS > 4
851     case MFF_REG4:
852 #endif
853 #if FLOW_N_REGS > 5
854     case MFF_REG5:
855 #endif
856 #if FLOW_N_REGS > 6
857     case MFF_REG6:
858 #endif
859 #if FLOW_N_REGS > 7
860     case MFF_REG7:
861 #endif
862 #if FLOW_N_REGS > 8
863 #error
864 #endif
865     case MFF_ETH_SRC:
866     case MFF_ETH_DST:
867     case MFF_ETH_TYPE:
868     case MFF_VLAN_TCI:
869     case MFF_IPV4_SRC:
870     case MFF_IPV4_DST:
871     case MFF_IPV6_SRC:
872     case MFF_IPV6_DST:
873     case MFF_IP_PROTO:
874     case MFF_IP_TTL:
875     case MFF_ARP_SPA:
876     case MFF_ARP_TPA:
877     case MFF_ARP_SHA:
878     case MFF_ARP_THA:
879     case MFF_TCP_SRC:
880     case MFF_TCP_DST:
881     case MFF_UDP_SRC:
882     case MFF_UDP_DST:
883     case MFF_ICMPV4_TYPE:
884     case MFF_ICMPV4_CODE:
885     case MFF_ICMPV6_TYPE:
886     case MFF_ICMPV6_CODE:
887     case MFF_ND_TARGET:
888     case MFF_ND_SLL:
889     case MFF_ND_TLL:
890         return true;
891
892     case MFF_IP_DSCP:
893         return !(value->u8 & ~IP_DSCP_MASK);
894     case MFF_IP_ECN:
895         return !(value->u8 & ~IP_ECN_MASK);
896     case MFF_IP_FRAG:
897         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
898
899     case MFF_ARP_OP:
900         return !(value->be16 & htons(0xff00));
901
902     case MFF_VLAN_VID:
903         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
904
905     case MFF_VLAN_PCP:
906         return !(value->u8 & ~7);
907
908     case MFF_IPV6_LABEL:
909         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
910
911     case MFF_N_IDS:
912     default:
913         NOT_REACHED();
914     }
915 }
916
917 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
918  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
919 void
920 mf_get_value(const struct mf_field *mf, const struct flow *flow,
921              union mf_value *value)
922 {
923     switch (mf->id) {
924     case MFF_TUN_ID:
925         value->be64 = flow->tun_id;
926         break;
927
928     case MFF_IN_PORT:
929         value->be16 = htons(flow->in_port);
930         break;
931
932 #if FLOW_N_REGS > 0
933     case MFF_REG0:
934 #endif
935 #if FLOW_N_REGS > 1
936     case MFF_REG1:
937 #endif
938 #if FLOW_N_REGS > 2
939     case MFF_REG2:
940 #endif
941 #if FLOW_N_REGS > 3
942     case MFF_REG3:
943 #endif
944 #if FLOW_N_REGS > 4
945     case MFF_REG4:
946 #endif
947 #if FLOW_N_REGS > 5
948     case MFF_REG5:
949 #endif
950 #if FLOW_N_REGS > 6
951     case MFF_REG6:
952 #endif
953 #if FLOW_N_REGS > 7
954     case MFF_REG7:
955 #endif
956 #if FLOW_N_REGS > 8
957 #error
958 #endif
959         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
960         break;
961
962     case MFF_ETH_SRC:
963         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
964         break;
965
966     case MFF_ETH_DST:
967         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
968         break;
969
970     case MFF_ETH_TYPE:
971         value->be16 = flow->dl_type;
972         break;
973
974     case MFF_VLAN_TCI:
975         value->be16 = flow->vlan_tci;
976         break;
977
978     case MFF_VLAN_VID:
979         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
980         break;
981
982     case MFF_VLAN_PCP:
983         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
984         break;
985
986     case MFF_IPV4_SRC:
987         value->be32 = flow->nw_src;
988         break;
989
990     case MFF_IPV4_DST:
991         value->be32 = flow->nw_dst;
992         break;
993
994     case MFF_IPV6_SRC:
995         value->ipv6 = flow->ipv6_src;
996         break;
997
998     case MFF_IPV6_DST:
999         value->ipv6 = flow->ipv6_dst;
1000         break;
1001
1002     case MFF_IPV6_LABEL:
1003         value->be32 = flow->ipv6_label;
1004         break;
1005
1006     case MFF_IP_PROTO:
1007         value->u8 = flow->nw_proto;
1008         break;
1009
1010     case MFF_IP_DSCP:
1011         value->u8 = flow->nw_tos & IP_DSCP_MASK;
1012         break;
1013
1014     case MFF_IP_ECN:
1015         value->u8 = flow->nw_tos & IP_ECN_MASK;
1016         break;
1017
1018     case MFF_IP_TTL:
1019         value->u8 = flow->nw_ttl;
1020         break;
1021
1022     case MFF_IP_FRAG:
1023         value->u8 = flow->nw_frag;
1024         break;
1025
1026     case MFF_ARP_OP:
1027         value->be16 = htons(flow->nw_proto);
1028         break;
1029
1030     case MFF_ARP_SPA:
1031         value->be32 = flow->nw_src;
1032         break;
1033
1034     case MFF_ARP_TPA:
1035         value->be32 = flow->nw_dst;
1036         break;
1037
1038     case MFF_ARP_SHA:
1039     case MFF_ND_SLL:
1040         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1041         break;
1042
1043     case MFF_ARP_THA:
1044     case MFF_ND_TLL:
1045         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1046         break;
1047
1048     case MFF_TCP_SRC:
1049         value->be16 = flow->tp_src;
1050         break;
1051
1052     case MFF_TCP_DST:
1053         value->be16 = flow->tp_dst;
1054         break;
1055
1056     case MFF_UDP_SRC:
1057         value->be16 = flow->tp_src;
1058         break;
1059
1060     case MFF_UDP_DST:
1061         value->be16 = flow->tp_dst;
1062         break;
1063
1064     case MFF_ICMPV4_TYPE:
1065     case MFF_ICMPV6_TYPE:
1066         value->u8 = ntohs(flow->tp_src);
1067         break;
1068
1069     case MFF_ICMPV4_CODE:
1070     case MFF_ICMPV6_CODE:
1071         value->u8 = ntohs(flow->tp_dst);
1072         break;
1073
1074     case MFF_ND_TARGET:
1075         value->ipv6 = flow->nd_target;
1076         break;
1077
1078     case MFF_N_IDS:
1079     default:
1080         NOT_REACHED();
1081     }
1082 }
1083
1084 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1085  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1086  * prerequisites. */
1087 void
1088 mf_set_value(const struct mf_field *mf,
1089              const union mf_value *value, struct cls_rule *rule)
1090 {
1091     switch (mf->id) {
1092     case MFF_TUN_ID:
1093         cls_rule_set_tun_id(rule, value->be64);
1094         break;
1095
1096     case MFF_IN_PORT:
1097         cls_rule_set_in_port(rule, ntohs(value->be16));
1098         break;
1099
1100 #if FLOW_N_REGS > 0
1101     case MFF_REG0:
1102 #endif
1103 #if FLOW_N_REGS > 1
1104     case MFF_REG1:
1105 #endif
1106 #if FLOW_N_REGS > 2
1107     case MFF_REG2:
1108 #endif
1109 #if FLOW_N_REGS > 3
1110     case MFF_REG3:
1111 #endif
1112 #if FLOW_N_REGS > 4
1113     case MFF_REG4:
1114 #endif
1115 #if FLOW_N_REGS > 5
1116     case MFF_REG5:
1117 #endif
1118 #if FLOW_N_REGS > 6
1119     case MFF_REG6:
1120 #endif
1121 #if FLOW_N_REGS > 7
1122     case MFF_REG7:
1123 #endif
1124 #if FLOW_N_REGS > 8
1125 #error
1126 #endif
1127 #if FLOW_N_REGS > 0
1128         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1129         break;
1130 #endif
1131
1132     case MFF_ETH_SRC:
1133         cls_rule_set_dl_src(rule, value->mac);
1134         break;
1135
1136     case MFF_ETH_DST:
1137         cls_rule_set_dl_dst(rule, value->mac);
1138         break;
1139
1140     case MFF_ETH_TYPE:
1141         cls_rule_set_dl_type(rule, value->be16);
1142         break;
1143
1144     case MFF_VLAN_TCI:
1145         cls_rule_set_dl_tci(rule, value->be16);
1146         break;
1147
1148     case MFF_VLAN_VID:
1149         cls_rule_set_dl_vlan(rule, value->be16);
1150         break;
1151
1152     case MFF_VLAN_PCP:
1153         cls_rule_set_dl_vlan_pcp(rule, value->u8);
1154         break;
1155
1156     case MFF_IPV4_SRC:
1157         cls_rule_set_nw_src(rule, value->be32);
1158         break;
1159
1160     case MFF_IPV4_DST:
1161         cls_rule_set_nw_dst(rule, value->be32);
1162         break;
1163
1164     case MFF_IPV6_SRC:
1165         cls_rule_set_ipv6_src(rule, &value->ipv6);
1166         break;
1167
1168     case MFF_IPV6_DST:
1169         cls_rule_set_ipv6_dst(rule, &value->ipv6);
1170         break;
1171
1172     case MFF_IPV6_LABEL:
1173         cls_rule_set_ipv6_label(rule, value->be32);
1174         break;
1175
1176     case MFF_IP_PROTO:
1177         cls_rule_set_nw_proto(rule, value->u8);
1178         break;
1179
1180     case MFF_IP_DSCP:
1181         cls_rule_set_nw_dscp(rule, value->u8);
1182         break;
1183
1184     case MFF_IP_ECN:
1185         cls_rule_set_nw_ecn(rule, value->u8);
1186         break;
1187
1188     case MFF_IP_TTL:
1189         cls_rule_set_nw_ttl(rule, value->u8);
1190         break;
1191
1192     case MFF_IP_FRAG:
1193         cls_rule_set_nw_frag(rule, value->u8);
1194         break;
1195
1196     case MFF_ARP_OP:
1197         cls_rule_set_nw_proto(rule, ntohs(value->be16));
1198         break;
1199
1200     case MFF_ARP_SPA:
1201         cls_rule_set_nw_src(rule, value->be32);
1202         break;
1203
1204     case MFF_ARP_TPA:
1205         cls_rule_set_nw_dst(rule, value->be32);
1206         break;
1207
1208     case MFF_ARP_SHA:
1209     case MFF_ND_SLL:
1210         cls_rule_set_arp_sha(rule, value->mac);
1211         break;
1212
1213     case MFF_ARP_THA:
1214     case MFF_ND_TLL:
1215         cls_rule_set_arp_tha(rule, value->mac);
1216         break;
1217
1218     case MFF_TCP_SRC:
1219         cls_rule_set_tp_src(rule, value->be16);
1220         break;
1221
1222     case MFF_TCP_DST:
1223         cls_rule_set_tp_dst(rule, value->be16);
1224         break;
1225
1226     case MFF_UDP_SRC:
1227         cls_rule_set_tp_src(rule, value->be16);
1228         break;
1229
1230     case MFF_UDP_DST:
1231         cls_rule_set_tp_dst(rule, value->be16);
1232         break;
1233
1234     case MFF_ICMPV4_TYPE:
1235     case MFF_ICMPV6_TYPE:
1236         cls_rule_set_icmp_type(rule, value->u8);
1237         break;
1238
1239     case MFF_ICMPV4_CODE:
1240     case MFF_ICMPV6_CODE:
1241         cls_rule_set_icmp_code(rule, value->u8);
1242         break;
1243
1244     case MFF_ND_TARGET:
1245         cls_rule_set_nd_target(rule, &value->ipv6);
1246         break;
1247
1248     case MFF_N_IDS:
1249     default:
1250         NOT_REACHED();
1251     }
1252 }
1253
1254 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1255  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1256  * prerequisites. */
1257 void
1258 mf_set_flow_value(const struct mf_field *mf,
1259                   const union mf_value *value, struct flow *flow)
1260 {
1261     switch (mf->id) {
1262     case MFF_TUN_ID:
1263         flow->tun_id = value->be64;
1264         break;
1265
1266     case MFF_IN_PORT:
1267         flow->in_port = ntohs(value->be16);
1268         break;
1269
1270 #if FLOW_N_REGS > 0
1271     case MFF_REG0:
1272 #endif
1273 #if FLOW_N_REGS > 1
1274     case MFF_REG1:
1275 #endif
1276 #if FLOW_N_REGS > 2
1277     case MFF_REG2:
1278 #endif
1279 #if FLOW_N_REGS > 3
1280     case MFF_REG3:
1281 #endif
1282 #if FLOW_N_REGS > 4
1283     case MFF_REG4:
1284 #endif
1285 #if FLOW_N_REGS > 5
1286     case MFF_REG5:
1287 #endif
1288 #if FLOW_N_REGS > 6
1289     case MFF_REG6:
1290 #endif
1291 #if FLOW_N_REGS > 7
1292     case MFF_REG7:
1293 #endif
1294 #if FLOW_N_REGS > 8
1295 #error
1296 #endif
1297 #if FLOW_N_REGS > 0
1298         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1299         break;
1300 #endif
1301
1302     case MFF_ETH_SRC:
1303         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1304         break;
1305
1306     case MFF_ETH_DST:
1307         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1308         break;
1309
1310     case MFF_ETH_TYPE:
1311         flow->dl_type = value->be16;
1312         break;
1313
1314     case MFF_VLAN_TCI:
1315         flow->vlan_tci = value->be16;
1316         break;
1317
1318     case MFF_VLAN_VID:
1319         flow_set_vlan_vid(flow, value->be16);
1320         break;
1321
1322     case MFF_VLAN_PCP:
1323         flow_set_vlan_pcp(flow, value->u8);
1324         break;
1325
1326     case MFF_IPV4_SRC:
1327         flow->nw_src = value->be32;
1328         break;
1329
1330     case MFF_IPV4_DST:
1331         flow->nw_dst = value->be32;
1332         break;
1333
1334     case MFF_IPV6_SRC:
1335         flow->ipv6_src = value->ipv6;
1336         break;
1337
1338     case MFF_IPV6_DST:
1339         flow->ipv6_dst = value->ipv6;
1340         break;
1341
1342     case MFF_IPV6_LABEL:
1343         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1344         break;
1345
1346     case MFF_IP_PROTO:
1347         flow->nw_proto = value->u8;
1348         break;
1349
1350     case MFF_IP_DSCP:
1351         flow->nw_tos &= ~IP_DSCP_MASK;
1352         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1353         break;
1354
1355     case MFF_IP_ECN:
1356         flow->nw_tos &= ~IP_ECN_MASK;
1357         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1358         break;
1359
1360     case MFF_IP_TTL:
1361         flow->nw_ttl = value->u8;
1362         break;
1363
1364     case MFF_IP_FRAG:
1365         flow->nw_frag &= value->u8;
1366         break;
1367
1368     case MFF_ARP_OP:
1369         flow->nw_proto = ntohs(value->be16);
1370         break;
1371
1372     case MFF_ARP_SPA:
1373         flow->nw_src = value->be32;
1374         break;
1375
1376     case MFF_ARP_TPA:
1377         flow->nw_dst = value->be32;
1378         break;
1379
1380     case MFF_ARP_SHA:
1381     case MFF_ND_SLL:
1382         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1383         break;
1384
1385     case MFF_ARP_THA:
1386     case MFF_ND_TLL:
1387         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1388         break;
1389
1390     case MFF_TCP_SRC:
1391     case MFF_UDP_SRC:
1392         flow->tp_src = value->be16;
1393         break;
1394
1395     case MFF_TCP_DST:
1396     case MFF_UDP_DST:
1397         flow->tp_dst = value->be16;
1398         break;
1399
1400     case MFF_ICMPV4_TYPE:
1401     case MFF_ICMPV6_TYPE:
1402         flow->tp_src = htons(value->u8);
1403         break;
1404
1405     case MFF_ICMPV4_CODE:
1406     case MFF_ICMPV6_CODE:
1407         flow->tp_dst = htons(value->u8);
1408         break;
1409
1410     case MFF_ND_TARGET:
1411         flow->nd_target = value->ipv6;
1412         break;
1413
1414     case MFF_N_IDS:
1415     default:
1416         NOT_REACHED();
1417     }
1418 }
1419
1420 /* Makes 'rule' wildcard field 'mf'.
1421  *
1422  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1423  * prerequisites. */
1424 void
1425 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1426 {
1427     switch (mf->id) {
1428     case MFF_TUN_ID:
1429         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1430         break;
1431
1432     case MFF_IN_PORT:
1433         rule->wc.wildcards |= FWW_IN_PORT;
1434         rule->flow.in_port = 0;
1435         break;
1436
1437 #if FLOW_N_REGS > 0
1438     case MFF_REG0:
1439         cls_rule_set_reg_masked(rule, 0, 0, 0);
1440         break;
1441 #endif
1442 #if FLOW_N_REGS > 1
1443     case MFF_REG1:
1444         cls_rule_set_reg_masked(rule, 1, 0, 0);
1445         break;
1446 #endif
1447 #if FLOW_N_REGS > 2
1448     case MFF_REG2:
1449         cls_rule_set_reg_masked(rule, 2, 0, 0);
1450         break;
1451 #endif
1452 #if FLOW_N_REGS > 3
1453     case MFF_REG3:
1454         cls_rule_set_reg_masked(rule, 3, 0, 0);
1455         break;
1456 #endif
1457 #if FLOW_N_REGS > 4
1458     case MFF_REG4:
1459         cls_rule_set_reg_masked(rule, 4, 0, 0);
1460         break;
1461 #endif
1462 #if FLOW_N_REGS > 5
1463     case MFF_REG5:
1464         cls_rule_set_reg_masked(rule, 5, 0, 0);
1465         break;
1466 #endif
1467 #if FLOW_N_REGS > 6
1468     case MFF_REG6:
1469         cls_rule_set_reg_masked(rule, 6, 0, 0);
1470         break;
1471 #endif
1472 #if FLOW_N_REGS > 7
1473     case MFF_REG7:
1474         cls_rule_set_reg_masked(rule, 7, 0, 0);
1475         break;
1476 #endif
1477 #if FLOW_N_REGS > 8
1478 #error
1479 #endif
1480
1481     case MFF_ETH_SRC:
1482         rule->wc.wildcards |= FWW_DL_SRC;
1483         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1484         break;
1485
1486     case MFF_ETH_DST:
1487         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1488         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1489         break;
1490
1491     case MFF_ETH_TYPE:
1492         rule->wc.wildcards |= FWW_DL_TYPE;
1493         rule->flow.dl_type = htons(0);
1494         break;
1495
1496     case MFF_VLAN_TCI:
1497         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1498         break;
1499
1500     case MFF_VLAN_VID:
1501         cls_rule_set_any_vid(rule);
1502         break;
1503
1504     case MFF_VLAN_PCP:
1505         cls_rule_set_any_pcp(rule);
1506         break;
1507
1508     case MFF_IPV4_SRC:
1509     case MFF_ARP_SPA:
1510         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1511         break;
1512
1513     case MFF_IPV4_DST:
1514     case MFF_ARP_TPA:
1515         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1516         break;
1517
1518     case MFF_IPV6_SRC:
1519         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1520         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1521         break;
1522
1523     case MFF_IPV6_DST:
1524         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1525         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1526         break;
1527
1528     case MFF_IPV6_LABEL:
1529         rule->wc.wildcards |= FWW_IPV6_LABEL;
1530         rule->flow.ipv6_label = 0;
1531         break;
1532
1533     case MFF_IP_PROTO:
1534         rule->wc.wildcards |= FWW_NW_PROTO;
1535         rule->flow.nw_proto = 0;
1536         break;
1537
1538     case MFF_IP_DSCP:
1539         rule->wc.wildcards |= FWW_NW_DSCP;
1540         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1541         break;
1542
1543     case MFF_IP_ECN:
1544         rule->wc.wildcards |= FWW_NW_ECN;
1545         rule->flow.nw_tos &= ~IP_ECN_MASK;
1546         break;
1547
1548     case MFF_IP_TTL:
1549         rule->wc.wildcards |= FWW_NW_TTL;
1550         rule->flow.nw_ttl = 0;
1551         break;
1552
1553     case MFF_IP_FRAG:
1554         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1555         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1556         break;
1557
1558     case MFF_ARP_OP:
1559         rule->wc.wildcards |= FWW_NW_PROTO;
1560         rule->flow.nw_proto = 0;
1561         break;
1562
1563     case MFF_ARP_SHA:
1564     case MFF_ND_SLL:
1565         rule->wc.wildcards |= FWW_ARP_SHA;
1566         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1567         break;
1568
1569     case MFF_ARP_THA:
1570     case MFF_ND_TLL:
1571         rule->wc.wildcards |= FWW_ARP_THA;
1572         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1573         break;
1574
1575     case MFF_TCP_SRC:
1576     case MFF_UDP_SRC:
1577     case MFF_ICMPV4_TYPE:
1578     case MFF_ICMPV6_TYPE:
1579         rule->wc.tp_src_mask = htons(0);
1580         rule->flow.tp_src = htons(0);
1581         break;
1582
1583     case MFF_TCP_DST:
1584     case MFF_UDP_DST:
1585     case MFF_ICMPV4_CODE:
1586     case MFF_ICMPV6_CODE:
1587         rule->wc.tp_dst_mask = htons(0);
1588         rule->flow.tp_dst = htons(0);
1589         break;
1590
1591     case MFF_ND_TARGET:
1592         rule->wc.wildcards |= FWW_ND_TARGET;
1593         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1594         break;
1595
1596     case MFF_N_IDS:
1597     default:
1598         NOT_REACHED();
1599     }
1600 }
1601
1602 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1603  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1604  * with a 1-bit indicating that the corresponding value bit must match and a
1605  * 0-bit indicating a don't-care.
1606  *
1607  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1608  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1609  * call is equivalent to mf_set_wild(mf, rule).
1610  *
1611  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1612  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1613 void
1614 mf_set(const struct mf_field *mf,
1615        const union mf_value *value, const union mf_value *mask,
1616        struct cls_rule *rule)
1617 {
1618     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1619         mf_set_value(mf, value, rule);
1620         return;
1621     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1622         mf_set_wild(mf, rule);
1623         return;
1624     }
1625
1626     switch (mf->id) {
1627     case MFF_IN_PORT:
1628     case MFF_ETH_SRC:
1629     case MFF_ETH_TYPE:
1630     case MFF_VLAN_VID:
1631     case MFF_VLAN_PCP:
1632     case MFF_IPV6_LABEL:
1633     case MFF_IP_PROTO:
1634     case MFF_IP_TTL:
1635     case MFF_IP_DSCP:
1636     case MFF_IP_ECN:
1637     case MFF_ARP_OP:
1638     case MFF_ARP_SHA:
1639     case MFF_ARP_THA:
1640     case MFF_ICMPV4_TYPE:
1641     case MFF_ICMPV4_CODE:
1642     case MFF_ICMPV6_TYPE:
1643     case MFF_ICMPV6_CODE:
1644     case MFF_ND_TARGET:
1645     case MFF_ND_SLL:
1646     case MFF_ND_TLL:
1647         NOT_REACHED();
1648
1649     case MFF_TUN_ID:
1650         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1651         break;
1652
1653 #if FLOW_N_REGS > 0
1654     case MFF_REG0:
1655 #endif
1656 #if FLOW_N_REGS > 1
1657     case MFF_REG1:
1658 #endif
1659 #if FLOW_N_REGS > 2
1660     case MFF_REG2:
1661 #endif
1662 #if FLOW_N_REGS > 3
1663     case MFF_REG3:
1664 #endif
1665 #if FLOW_N_REGS > 4
1666     case MFF_REG4:
1667 #endif
1668 #if FLOW_N_REGS > 5
1669     case MFF_REG5:
1670 #endif
1671 #if FLOW_N_REGS > 6
1672     case MFF_REG6:
1673 #endif
1674 #if FLOW_N_REGS > 7
1675     case MFF_REG7:
1676 #endif
1677 #if FLOW_N_REGS > 8
1678 #error
1679 #endif
1680         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1681                                 ntohl(value->be32), ntohl(mask->be32));
1682         break;
1683
1684     case MFF_ETH_DST:
1685         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1686             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1687         }
1688         break;
1689
1690     case MFF_VLAN_TCI:
1691         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1692         break;
1693
1694     case MFF_IPV4_SRC:
1695         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1696         break;
1697
1698     case MFF_IPV4_DST:
1699         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1700         break;
1701
1702     case MFF_IPV6_SRC:
1703         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1704         break;
1705
1706     case MFF_IPV6_DST:
1707         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1708         break;
1709
1710     case MFF_IP_FRAG:
1711         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1712         break;
1713
1714     case MFF_ARP_SPA:
1715         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1716         break;
1717
1718     case MFF_ARP_TPA:
1719         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1720         break;
1721
1722     case MFF_TCP_SRC:
1723     case MFF_UDP_SRC:
1724         cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1725         break;
1726
1727     case MFF_TCP_DST:
1728     case MFF_UDP_DST:
1729         cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1730         break;
1731
1732     case MFF_N_IDS:
1733     default:
1734         NOT_REACHED();
1735     }
1736 }
1737
1738 static enum ofperr
1739 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1740            const char *type)
1741 {
1742     if (!sf->field) {
1743         VLOG_WARN_RL(&rl, "unknown %s field", type);
1744     } else if (!sf->n_bits) {
1745         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1746     } else if (sf->ofs >= sf->field->n_bits) {
1747         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1748                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1749     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1750         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1751                      "of %s field %s", sf->ofs, sf->n_bits,
1752                      sf->field->n_bits, type, sf->field->name);
1753     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1754         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1755                      type, sf->field->name);
1756     } else {
1757         return 0;
1758     }
1759
1760     return OFPERR_OFPBAC_BAD_ARGUMENT;
1761 }
1762
1763 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1764  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1765  * ofp_mkerr()).  */
1766 enum ofperr
1767 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1768 {
1769     return mf_check__(sf, flow, "source");
1770 }
1771
1772 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1773  * if so, otherwise an OpenFlow error code (e.g. as returned by
1774  * ofp_mkerr()). */
1775 enum ofperr
1776 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1777 {
1778     int error = mf_check__(sf, flow, "destination");
1779     if (!error && !sf->field->writable) {
1780         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1781                      sf->field->name);
1782         return OFPERR_OFPBAC_BAD_ARGUMENT;
1783     }
1784     return error;
1785 }
1786
1787 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1788  * 'value' and 'mask', respectively. */
1789 void
1790 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1791        union mf_value *value, union mf_value *mask)
1792 {
1793     mf_get_value(mf, &rule->flow, value);
1794     mf_get_mask(mf, &rule->wc, mask);
1795 }
1796
1797 /* Assigns a random value for field 'mf' to 'value'. */
1798 void
1799 mf_random_value(const struct mf_field *mf, union mf_value *value)
1800 {
1801     random_bytes(value, mf->n_bytes);
1802
1803     switch (mf->id) {
1804     case MFF_TUN_ID:
1805     case MFF_IN_PORT:
1806 #if FLOW_N_REGS > 0
1807     case MFF_REG0:
1808 #endif
1809 #if FLOW_N_REGS > 1
1810     case MFF_REG1:
1811 #endif
1812 #if FLOW_N_REGS > 2
1813     case MFF_REG2:
1814 #endif
1815 #if FLOW_N_REGS > 3
1816     case MFF_REG3:
1817 #endif
1818 #if FLOW_N_REGS > 4
1819     case MFF_REG4:
1820 #endif
1821 #if FLOW_N_REGS > 5
1822     case MFF_REG5:
1823 #endif
1824 #if FLOW_N_REGS > 6
1825     case MFF_REG6:
1826 #endif
1827 #if FLOW_N_REGS > 7
1828     case MFF_REG7:
1829 #endif
1830 #if FLOW_N_REGS > 8
1831 #error
1832 #endif
1833     case MFF_ETH_SRC:
1834     case MFF_ETH_DST:
1835     case MFF_ETH_TYPE:
1836     case MFF_VLAN_TCI:
1837     case MFF_IPV4_SRC:
1838     case MFF_IPV4_DST:
1839     case MFF_IPV6_SRC:
1840     case MFF_IPV6_DST:
1841     case MFF_IP_PROTO:
1842     case MFF_IP_TTL:
1843     case MFF_ARP_SPA:
1844     case MFF_ARP_TPA:
1845     case MFF_ARP_SHA:
1846     case MFF_ARP_THA:
1847     case MFF_TCP_SRC:
1848     case MFF_TCP_DST:
1849     case MFF_UDP_SRC:
1850     case MFF_UDP_DST:
1851     case MFF_ICMPV4_TYPE:
1852     case MFF_ICMPV4_CODE:
1853     case MFF_ICMPV6_TYPE:
1854     case MFF_ICMPV6_CODE:
1855     case MFF_ND_TARGET:
1856     case MFF_ND_SLL:
1857     case MFF_ND_TLL:
1858         break;
1859
1860     case MFF_IPV6_LABEL:
1861         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1862         break;
1863
1864     case MFF_IP_DSCP:
1865         value->u8 &= IP_DSCP_MASK;
1866         break;
1867
1868     case MFF_IP_ECN:
1869         value->u8 &= IP_ECN_MASK;
1870         break;
1871
1872     case MFF_IP_FRAG:
1873         value->u8 &= FLOW_NW_FRAG_MASK;
1874         break;
1875
1876     case MFF_ARP_OP:
1877         value->be16 &= htons(0xff);
1878         break;
1879
1880     case MFF_VLAN_VID:
1881         value->be16 &= htons(VLAN_VID_MASK);
1882         break;
1883
1884     case MFF_VLAN_PCP:
1885         value->u8 &= 0x07;
1886         break;
1887
1888     case MFF_N_IDS:
1889     default:
1890         NOT_REACHED();
1891     }
1892 }
1893
1894 static char *
1895 mf_from_integer_string(const struct mf_field *mf, const char *s,
1896                        uint8_t *valuep, uint8_t *maskp)
1897 {
1898     unsigned long long int integer, mask;
1899     char *tail;
1900     int i;
1901
1902     errno = 0;
1903     integer = strtoull(s, &tail, 0);
1904     if (errno || (*tail != '\0' && *tail != '/')) {
1905         goto syntax_error;
1906     }
1907
1908     if (*tail == '/') {
1909         mask = strtoull(tail + 1, &tail, 0);
1910         if (errno || *tail != '\0') {
1911             goto syntax_error;
1912         }
1913     } else {
1914         mask = ULLONG_MAX;
1915     }
1916
1917     for (i = mf->n_bytes - 1; i >= 0; i--) {
1918         valuep[i] = integer;
1919         maskp[i] = mask;
1920         integer >>= 8;
1921         mask >>= 8;
1922     }
1923     if (integer) {
1924         return xasprintf("%s: value too large for %u-byte field %s",
1925                          s, mf->n_bytes, mf->name);
1926     }
1927     return NULL;
1928
1929 syntax_error:
1930     return xasprintf("%s: bad syntax for %s", s, mf->name);
1931 }
1932
1933 static char *
1934 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1935                         uint8_t mac[ETH_ADDR_LEN],
1936                         uint8_t mask[ETH_ADDR_LEN])
1937 {
1938     assert(mf->n_bytes == ETH_ADDR_LEN);
1939
1940     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1941                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1942     case ETH_ADDR_SCAN_COUNT * 2:
1943         return NULL;
1944
1945     case ETH_ADDR_SCAN_COUNT:
1946         memset(mask, 0xff, ETH_ADDR_LEN);
1947         return NULL;
1948
1949     default:
1950         return xasprintf("%s: invalid Ethernet address", s);
1951     }
1952 }
1953
1954 static char *
1955 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1956                     ovs_be32 *ip, ovs_be32 *mask)
1957 {
1958     int prefix;
1959
1960     assert(mf->n_bytes == sizeof *ip);
1961
1962     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1963                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1964         /* OK. */
1965     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1966                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1967         if (prefix <= 0 || prefix > 32) {
1968             return xasprintf("%s: network prefix bits not between 1 and "
1969                              "32", s);
1970         } else if (prefix == 32) {
1971             *mask = htonl(UINT32_MAX);
1972         } else {
1973             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1974         }
1975     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1976         *mask = htonl(UINT32_MAX);
1977     } else {
1978         return xasprintf("%s: invalid IP address", s);
1979     }
1980     return NULL;
1981 }
1982
1983 static char *
1984 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1985                     struct in6_addr *value, struct in6_addr *mask)
1986 {
1987     char *str = xstrdup(s);
1988     char *save_ptr = NULL;
1989     const char *name, *netmask;
1990     int retval;
1991
1992     assert(mf->n_bytes == sizeof *value);
1993
1994     name = strtok_r(str, "/", &save_ptr);
1995     retval = name ? lookup_ipv6(name, value) : EINVAL;
1996     if (retval) {
1997         char *err;
1998
1999         err = xasprintf("%s: could not convert to IPv6 address", str);
2000         free(str);
2001
2002         return err;
2003     }
2004
2005     netmask = strtok_r(NULL, "/", &save_ptr);
2006     if (netmask) {
2007         int prefix = atoi(netmask);
2008         if (prefix <= 0 || prefix > 128) {
2009             free(str);
2010             return xasprintf("%s: prefix bits not between 1 and 128", s);
2011         } else {
2012             *mask = ipv6_create_mask(prefix);
2013         }
2014     } else {
2015         *mask = in6addr_exact;
2016     }
2017     free(str);
2018
2019     return NULL;
2020 }
2021
2022 static char *
2023 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2024                         ovs_be16 *valuep, ovs_be16 *maskp)
2025 {
2026     uint16_t port;
2027
2028     assert(mf->n_bytes == sizeof(ovs_be16));
2029     if (ofputil_port_from_string(s, &port)) {
2030         *valuep = htons(port);
2031         *maskp = htons(UINT16_MAX);
2032         return NULL;
2033     } else {
2034         return mf_from_integer_string(mf, s,
2035                                       (uint8_t *) valuep, (uint8_t *) maskp);
2036     }
2037 }
2038
2039 struct frag_handling {
2040     const char *name;
2041     uint8_t mask;
2042     uint8_t value;
2043 };
2044
2045 static const struct frag_handling all_frags[] = {
2046 #define A FLOW_NW_FRAG_ANY
2047 #define L FLOW_NW_FRAG_LATER
2048     /* name               mask  value */
2049
2050     { "no",               A|L,  0     },
2051     { "first",            A|L,  A     },
2052     { "later",            A|L,  A|L   },
2053
2054     { "no",               A,    0     },
2055     { "yes",              A,    A     },
2056
2057     { "not_later",        L,    0     },
2058     { "later",            L,    L     },
2059 #undef A
2060 #undef L
2061 };
2062
2063 static char *
2064 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2065 {
2066     const struct frag_handling *h;
2067
2068     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2069         if (!strcasecmp(s, h->name)) {
2070             /* We force the upper bits of the mask on to make mf_parse_value()
2071              * happy (otherwise it will never think it's an exact match.) */
2072             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2073             *valuep = h->value;
2074             return NULL;
2075         }
2076     }
2077
2078     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2079                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2080 }
2081
2082 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2083  * NULL if successful, otherwise a malloc()'d string describing the error. */
2084 char *
2085 mf_parse(const struct mf_field *mf, const char *s,
2086          union mf_value *value, union mf_value *mask)
2087 {
2088     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2089         memset(value, 0, mf->n_bytes);
2090         memset(mask, 0, mf->n_bytes);
2091         return NULL;
2092     }
2093
2094     switch (mf->string) {
2095     case MFS_DECIMAL:
2096     case MFS_HEXADECIMAL:
2097         return mf_from_integer_string(mf, s,
2098                                       (uint8_t *) value, (uint8_t *) mask);
2099
2100     case MFS_ETHERNET:
2101         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2102
2103     case MFS_IPV4:
2104         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2105
2106     case MFS_IPV6:
2107         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2108
2109     case MFS_OFP_PORT:
2110         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2111
2112     case MFS_FRAG:
2113         return mf_from_frag_string(s, &value->u8, &mask->u8);
2114     }
2115     NOT_REACHED();
2116 }
2117
2118 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2119  * successful, otherwise a malloc()'d string describing the error. */
2120 char *
2121 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2122 {
2123     union mf_value mask;
2124     char *error;
2125
2126     error = mf_parse(mf, s, value, &mask);
2127     if (error) {
2128         return error;
2129     }
2130
2131     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2132         return xasprintf("%s: wildcards not allowed here", s);
2133     }
2134     return NULL;
2135 }
2136
2137 static void
2138 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2139                          const uint8_t *maskp, struct ds *s)
2140 {
2141     unsigned long long int integer;
2142     int i;
2143
2144     assert(mf->n_bytes <= 8);
2145
2146     integer = 0;
2147     for (i = 0; i < mf->n_bytes; i++) {
2148         integer = (integer << 8) | valuep[i];
2149     }
2150     if (mf->string == MFS_HEXADECIMAL) {
2151         ds_put_format(s, "%#llx", integer);
2152     } else {
2153         ds_put_format(s, "%lld", integer);
2154     }
2155
2156     if (maskp) {
2157         unsigned long long int mask;
2158
2159         mask = 0;
2160         for (i = 0; i < mf->n_bytes; i++) {
2161             mask = (mask << 8) | maskp[i];
2162         }
2163
2164         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2165          * not sure that that a bit-mask written in decimal is ever easier to
2166          * understand than the same bit-mask written in hexadecimal. */
2167         ds_put_format(s, "/%#llx", mask);
2168     }
2169 }
2170
2171 static void
2172 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2173                       struct ds *s)
2174 {
2175     const struct frag_handling *h;
2176     uint8_t value = *valuep;
2177     uint8_t mask = *maskp;
2178
2179     value &= mask;
2180     mask &= FLOW_NW_FRAG_MASK;
2181
2182     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2183         if (value == h->value && mask == h->mask) {
2184             ds_put_cstr(s, h->name);
2185             return;
2186         }
2187     }
2188     ds_put_cstr(s, "<error>");
2189 }
2190
2191 /* Appends to 's' a string representation of field 'mf' whose value is in
2192  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2193 void
2194 mf_format(const struct mf_field *mf,
2195           const union mf_value *value, const union mf_value *mask,
2196           struct ds *s)
2197 {
2198     if (mask) {
2199         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2200             ds_put_cstr(s, "ANY");
2201             return;
2202         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2203             mask = NULL;
2204         }
2205     }
2206
2207     switch (mf->string) {
2208     case MFS_OFP_PORT:
2209         if (!mask) {
2210             ofputil_format_port(ntohs(value->be16), s);
2211             break;
2212         }
2213         /* fall through */
2214     case MFS_DECIMAL:
2215     case MFS_HEXADECIMAL:
2216         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2217         break;
2218
2219     case MFS_ETHERNET:
2220         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2221         if (mask) {
2222             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2223         }
2224         break;
2225
2226     case MFS_IPV4:
2227         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2228                          s);
2229         break;
2230
2231     case MFS_IPV6:
2232         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2233         break;
2234
2235     case MFS_FRAG:
2236         mf_format_frag_string(&value->u8, &mask->u8, s);
2237         break;
2238
2239     default:
2240         NOT_REACHED();
2241     }
2242 }
2243 \f
2244 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2245  * least-significant bits in 'x'.
2246  *
2247  * See mf_set_subfield() for an example.
2248  *
2249  * The difference between this function and mf_set_subfield() is that the
2250  * latter function can only handle subfields up to 64 bits wide, whereas this
2251  * one handles the general case.  On the other hand, mf_set_subfield() is
2252  * arguably easier to use. */
2253 void
2254 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2255                   struct cls_rule *rule)
2256 {
2257     const struct mf_field *field = sf->field;
2258     union mf_value value, mask;
2259
2260     mf_get(field, rule, &value, &mask);
2261     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2262     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2263     mf_set(field, &value, &mask, rule);
2264 }
2265
2266 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2267  * least-significant bits of 'x'.
2268  *
2269  * Example: suppose that 'sf->field' is originally the following 2-byte field
2270  * in 'rule':
2271  *
2272  *     value == 0xe00a == 2#1110000000001010
2273  *      mask == 0xfc3f == 2#1111110000111111
2274  *
2275  * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2276  * sf->n_bits == 7 would have the following effect (note that 0x55 is
2277  * 2#1010101):
2278  *
2279  *     value == 0xd50a == 2#1101010100001010
2280  *      mask == 0xff3f == 2#1111111100111111
2281  *                           ^^^^^^^ affected bits
2282  *
2283  * The caller is responsible for ensuring that the result will be a valid
2284  * wildcard pattern for 'sf->field'.  The caller is responsible for ensuring
2285  * that 'rule' meets 'sf->field''s prerequisites. */
2286 void
2287 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2288                 struct cls_rule *rule)
2289 {
2290     const struct mf_field *field = sf->field;
2291     unsigned int n_bits = sf->n_bits;
2292     unsigned int ofs = sf->ofs;
2293
2294     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2295         union mf_value value;
2296         int i;
2297
2298         for (i = field->n_bytes - 1; i >= 0; i--) {
2299             ((uint8_t *) &value)[i] = x;
2300             x >>= 8;
2301         }
2302         mf_set_value(field, &value, rule);
2303     } else {
2304         union mf_value value, mask;
2305         uint8_t *vp = (uint8_t *) &value;
2306         uint8_t *mp = (uint8_t *) &mask;
2307
2308         mf_get(field, rule, &value, &mask);
2309         bitwise_put(x,          vp, field->n_bytes, ofs, n_bits);
2310         bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2311         mf_set(field, &value, &mask, rule);
2312     }
2313 }
2314
2315 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2316 void
2317 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2318                       struct flow *flow)
2319 {
2320     const struct mf_field *field = sf->field;
2321     unsigned int n_bits = sf->n_bits;
2322     unsigned int ofs = sf->ofs;
2323     union mf_value value;
2324
2325     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2326         int i;
2327
2328         for (i = field->n_bytes - 1; i >= 0; i--) {
2329             ((uint8_t *) &value)[i] = x;
2330             x >>= 8;
2331         }
2332         mf_set_flow_value(field, &value, flow);
2333     } else {
2334         mf_get_value(field, flow, &value);
2335         bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2336         mf_set_flow_value(field, &value, flow);
2337     }
2338 }
2339
2340 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2341  * reading 'flow', e.g. as checked by mf_check_src(). */
2342 void
2343 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2344                  union mf_subvalue *x)
2345 {
2346     union mf_value value;
2347
2348     mf_get_value(sf->field, flow, &value);
2349
2350     memset(x, 0, sizeof *x);
2351     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2352                  x, sizeof *x, 0,
2353                  sf->n_bits);
2354 }
2355
2356 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2357  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2358  * less. */
2359 uint64_t
2360 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2361 {
2362     union mf_value value;
2363
2364     mf_get_value(sf->field, flow, &value);
2365     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2366 }
2367
2368 /* Formats 'sf' into 's' in a format normally acceptable to
2369  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2370  * sf->field has no NXM name.) */
2371 void
2372 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2373 {
2374     if (!sf->field) {
2375         ds_put_cstr(s, "<unknown>");
2376     } else if (sf->field->nxm_name) {
2377         ds_put_cstr(s, sf->field->nxm_name);
2378     } else if (sf->field->nxm_header) {
2379         uint32_t header = sf->field->nxm_header;
2380         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2381     } else {
2382         ds_put_cstr(s, sf->field->name);
2383     }
2384
2385     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2386         ds_put_cstr(s, "[]");
2387     } else if (sf->n_bits == 1) {
2388         ds_put_format(s, "[%d]", sf->ofs);
2389     } else {
2390         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2391     }
2392 }
2393
2394 static const struct mf_field *
2395 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2396 {
2397     int i;
2398
2399     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2400     if (*wild) {
2401         name_len -= 2;
2402     }
2403
2404     for (i = 0; i < MFF_N_IDS; i++) {
2405         const struct mf_field *mf = mf_from_id(i);
2406
2407         if (mf->nxm_name
2408             && !strncmp(mf->nxm_name, name, name_len)
2409             && mf->nxm_name[name_len] == '\0') {
2410             return mf;
2411         }
2412     }
2413
2414     return NULL;
2415 }
2416
2417 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2418  * returns NULL and advances '*sp' to the first byte following the parsed
2419  * string.  On failure, returns a malloc()'d error message, does not modify
2420  * '*sp', and does not properly initialize 'sf'.
2421  *
2422  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2423  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2424  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2425  * may both be omitted (the [] are still required) to indicate an entire
2426  * field. */
2427 char *
2428 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2429 {
2430     const struct mf_field *field;
2431     const char *name;
2432     int start, end;
2433     const char *s;
2434     int name_len;
2435     bool wild;
2436
2437     s = *sp;
2438     name = s;
2439     name_len = strcspn(s, "[");
2440     if (s[name_len] != '[') {
2441         return xasprintf("%s: missing [ looking for field name", *sp);
2442     }
2443
2444     field = mf_parse_subfield_name(name, name_len, &wild);
2445     if (!field) {
2446         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2447     }
2448
2449     s += name_len;
2450     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2451         /* Nothing to do. */
2452     } else if (sscanf(s, "[%d]", &start) == 1) {
2453         end = start;
2454     } else if (!strncmp(s, "[]", 2)) {
2455         start = 0;
2456         end = field->n_bits - 1;
2457     } else {
2458         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2459                          "[<start>..<end>]", *sp);
2460     }
2461     s = strchr(s, ']') + 1;
2462
2463     if (start > end) {
2464         return xasprintf("%s: starting bit %d is after ending bit %d",
2465                          *sp, start, end);
2466     } else if (start >= field->n_bits) {
2467         return xasprintf("%s: starting bit %d is not valid because field is "
2468                          "only %d bits wide", *sp, start, field->n_bits);
2469     } else if (end >= field->n_bits){
2470         return xasprintf("%s: ending bit %d is not valid because field is "
2471                          "only %d bits wide", *sp, end, field->n_bits);
2472     }
2473
2474     sf->field = field;
2475     sf->ofs = start;
2476     sf->n_bits = end - start + 1;
2477
2478     *sp = s;
2479     return NULL;
2480 }
2481
2482 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2483  * byte in 's' following the parsed string.
2484  *
2485  * Exits with an error message if 's' has incorrect syntax.
2486  *
2487  * The syntax parsed from 's' takes the form "header[start..end]" where
2488  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2489  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2490  * may both be omitted (the [] are still required) to indicate an entire
2491  * field.  */
2492 const char *
2493 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2494 {
2495     char *msg = mf_parse_subfield__(sf, &s);
2496     if (msg) {
2497         ovs_fatal(0, "%s", msg);
2498     }
2499     return s;
2500 }