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