flow: Use bit-mask for IP protocol 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, 0,
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, 0,
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         assert(mf->fww_bit != 0);
579         return (wc->wildcards & mf->fww_bit) != 0;
580
581     case MFF_TUN_ID:
582         return !wc->tun_id_mask;
583     case MFF_METADATA:
584         return !wc->metadata_mask;
585
586     CASE_MFF_REGS:
587         return !wc->reg_masks[mf->id - MFF_REG0];
588
589     case MFF_ETH_SRC:
590         return eth_addr_is_zero(wc->dl_src_mask);
591     case MFF_ETH_DST:
592         return eth_addr_is_zero(wc->dl_dst_mask);
593
594     case MFF_ARP_SHA:
595     case MFF_ND_SLL:
596         return eth_addr_is_zero(wc->arp_sha_mask);
597
598     case MFF_ARP_THA:
599     case MFF_ND_TLL:
600         return eth_addr_is_zero(wc->arp_tha_mask);
601
602     case MFF_VLAN_TCI:
603         return !wc->vlan_tci_mask;
604     case MFF_DL_VLAN:
605         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
606     case MFF_VLAN_VID:
607         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK | VLAN_CFI));
608     case MFF_DL_VLAN_PCP:
609     case MFF_VLAN_PCP:
610         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
611
612     case MFF_IPV4_SRC:
613         return !wc->nw_src_mask;
614     case MFF_IPV4_DST:
615         return !wc->nw_dst_mask;
616
617     case MFF_IPV6_SRC:
618         return ipv6_mask_is_any(&wc->ipv6_src_mask);
619     case MFF_IPV6_DST:
620         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
621
622     case MFF_IPV6_LABEL:
623         return !wc->ipv6_label_mask;
624
625     case MFF_IP_PROTO:
626         return !wc->nw_proto_mask;
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_OP:
641         return !wc->nw_proto_mask;
642     case MFF_ARP_SPA:
643         return !wc->nw_src_mask;
644     case MFF_ARP_TPA:
645         return !wc->nw_dst_mask;
646
647     case MFF_TCP_SRC:
648     case MFF_UDP_SRC:
649     case MFF_ICMPV4_TYPE:
650     case MFF_ICMPV6_TYPE:
651         return !wc->tp_src_mask;
652     case MFF_TCP_DST:
653     case MFF_UDP_DST:
654     case MFF_ICMPV4_CODE:
655     case MFF_ICMPV6_CODE:
656         return !wc->tp_dst_mask;
657
658     case MFF_N_IDS:
659     default:
660         NOT_REACHED();
661     }
662 }
663
664 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
665  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
666  * purposes, or to 0 if it is wildcarded.
667  *
668  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
669  * meets 'mf''s prerequisites. */
670 void
671 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
672             union mf_value *mask)
673 {
674     switch (mf->id) {
675     case MFF_IN_PORT:
676     case MFF_ETH_TYPE:
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_PROTO:
732         mask->u8 = wc->nw_proto_mask;
733         break;
734     case MFF_IP_DSCP:
735         mask->u8 = wc->nw_tos_mask & IP_DSCP_MASK;
736         break;
737     case MFF_IP_ECN:
738         mask->u8 = wc->nw_tos_mask & IP_ECN_MASK;
739         break;
740
741     case MFF_ND_TARGET:
742         mask->ipv6 = wc->nd_target_mask;
743         break;
744
745     case MFF_IP_TTL:
746         mask->u8 = wc->nw_ttl_mask;
747         break;
748     case MFF_IP_FRAG:
749         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
750         break;
751
752     case MFF_ARP_OP:
753         mask->u8 = wc->nw_proto_mask;
754         break;
755     case MFF_ARP_SPA:
756         mask->be32 = wc->nw_src_mask;
757         break;
758     case MFF_ARP_TPA:
759         mask->be32 = wc->nw_dst_mask;
760         break;
761     case MFF_ARP_SHA:
762     case MFF_ND_SLL:
763         memcpy(mask->mac, wc->arp_sha_mask, ETH_ADDR_LEN);
764         break;
765     case MFF_ARP_THA:
766     case MFF_ND_TLL:
767         memcpy(mask->mac, wc->arp_tha_mask, ETH_ADDR_LEN);
768         break;
769
770     case MFF_TCP_SRC:
771     case MFF_UDP_SRC:
772         mask->be16 = wc->tp_src_mask;
773         break;
774     case MFF_TCP_DST:
775     case MFF_UDP_DST:
776         mask->be16 = wc->tp_dst_mask;
777         break;
778
779     case MFF_ICMPV4_TYPE:
780     case MFF_ICMPV6_TYPE:
781         mask->u8 = ntohs(wc->tp_src_mask);
782         break;
783     case MFF_ICMPV4_CODE:
784     case MFF_ICMPV6_CODE:
785         mask->u8 = ntohs(wc->tp_dst_mask);
786         break;
787
788     case MFF_N_IDS:
789     default:
790         NOT_REACHED();
791     }
792 }
793
794 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
795  * if the mask is valid, false otherwise. */
796 bool
797 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
798 {
799     switch (mf->maskable) {
800     case MFM_NONE:
801         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
802                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
803
804     case MFM_FULLY:
805         return true;
806     }
807
808     NOT_REACHED();
809 }
810
811 static bool
812 is_ip_any(const struct flow *flow)
813 {
814     return (flow->dl_type == htons(ETH_TYPE_IP) ||
815             flow->dl_type == htons(ETH_TYPE_IPV6));
816 }
817
818 static bool
819 is_icmpv4(const struct flow *flow)
820 {
821     return (flow->dl_type == htons(ETH_TYPE_IP)
822             && flow->nw_proto == IPPROTO_ICMP);
823 }
824
825 static bool
826 is_icmpv6(const struct flow *flow)
827 {
828     return (flow->dl_type == htons(ETH_TYPE_IPV6)
829             && flow->nw_proto == IPPROTO_ICMPV6);
830 }
831
832 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
833 bool
834 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
835 {
836     switch (mf->prereqs) {
837     case MFP_NONE:
838         return true;
839
840     case MFP_ARP:
841         return flow->dl_type == htons(ETH_TYPE_ARP);
842     case MFP_IPV4:
843         return flow->dl_type == htons(ETH_TYPE_IP);
844     case MFP_IPV6:
845         return flow->dl_type == htons(ETH_TYPE_IPV6);
846     case MFP_VLAN_VID:
847         return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
848     case MFP_IP_ANY:
849         return is_ip_any(flow);
850
851     case MFP_TCP:
852         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
853     case MFP_UDP:
854         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
855     case MFP_ICMPV4:
856         return is_icmpv4(flow);
857     case MFP_ICMPV6:
858         return is_icmpv6(flow);
859
860     case MFP_ND:
861         return (is_icmpv6(flow)
862                 && flow->tp_dst == htons(0)
863                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
864                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
865     case MFP_ND_SOLICIT:
866         return (is_icmpv6(flow)
867                 && flow->tp_dst == htons(0)
868                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
869     case MFP_ND_ADVERT:
870         return (is_icmpv6(flow)
871                 && flow->tp_dst == htons(0)
872                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
873     }
874
875     NOT_REACHED();
876 }
877
878 /* Returns true if 'value' may be a valid value *as part of a masked match*,
879  * false otherwise.
880  *
881  * A value is not rejected just because it is not valid for the field in
882  * question, but only if it doesn't make sense to test the bits in question at
883  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
884  * without the VLAN_CFI bit being set, but we can't reject those values because
885  * it is still legitimate to test just for those bits (see the documentation
886  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
887  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
888 bool
889 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
890 {
891     switch (mf->id) {
892     case MFF_TUN_ID:
893     case MFF_METADATA:
894     case MFF_IN_PORT:
895     CASE_MFF_REGS:
896     case MFF_ETH_SRC:
897     case MFF_ETH_DST:
898     case MFF_ETH_TYPE:
899     case MFF_VLAN_TCI:
900     case MFF_IPV4_SRC:
901     case MFF_IPV4_DST:
902     case MFF_IPV6_SRC:
903     case MFF_IPV6_DST:
904     case MFF_IP_PROTO:
905     case MFF_IP_TTL:
906     case MFF_ARP_SPA:
907     case MFF_ARP_TPA:
908     case MFF_ARP_SHA:
909     case MFF_ARP_THA:
910     case MFF_TCP_SRC:
911     case MFF_TCP_DST:
912     case MFF_UDP_SRC:
913     case MFF_UDP_DST:
914     case MFF_ICMPV4_TYPE:
915     case MFF_ICMPV4_CODE:
916     case MFF_ICMPV6_TYPE:
917     case MFF_ICMPV6_CODE:
918     case MFF_ND_TARGET:
919     case MFF_ND_SLL:
920     case MFF_ND_TLL:
921         return true;
922
923     case MFF_IP_DSCP:
924         return !(value->u8 & ~IP_DSCP_MASK);
925     case MFF_IP_ECN:
926         return !(value->u8 & ~IP_ECN_MASK);
927     case MFF_IP_FRAG:
928         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
929
930     case MFF_ARP_OP:
931         return !(value->be16 & htons(0xff00));
932
933     case MFF_DL_VLAN:
934         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
935     case MFF_VLAN_VID:
936         return !(value->be16 & htons(VLAN_PCP_MASK));
937
938     case MFF_DL_VLAN_PCP:
939     case MFF_VLAN_PCP:
940         return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
941
942     case MFF_IPV6_LABEL:
943         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
944
945     case MFF_N_IDS:
946     default:
947         NOT_REACHED();
948     }
949 }
950
951 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
952  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
953 void
954 mf_get_value(const struct mf_field *mf, const struct flow *flow,
955              union mf_value *value)
956 {
957     switch (mf->id) {
958     case MFF_TUN_ID:
959         value->be64 = flow->tun_id;
960         break;
961     case MFF_METADATA:
962         value->be64 = flow->metadata;
963         break;
964
965     case MFF_IN_PORT:
966         value->be16 = htons(flow->in_port);
967         break;
968
969     CASE_MFF_REGS:
970         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
971         break;
972
973     case MFF_ETH_SRC:
974         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
975         break;
976
977     case MFF_ETH_DST:
978         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
979         break;
980
981     case MFF_ETH_TYPE:
982         value->be16 = flow->dl_type;
983         break;
984
985     case MFF_VLAN_TCI:
986         value->be16 = flow->vlan_tci;
987         break;
988
989     case MFF_DL_VLAN:
990         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
991         break;
992     case MFF_VLAN_VID:
993         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
994         break;
995
996     case MFF_DL_VLAN_PCP:
997     case MFF_VLAN_PCP:
998         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
999         break;
1000
1001     case MFF_IPV4_SRC:
1002         value->be32 = flow->nw_src;
1003         break;
1004
1005     case MFF_IPV4_DST:
1006         value->be32 = flow->nw_dst;
1007         break;
1008
1009     case MFF_IPV6_SRC:
1010         value->ipv6 = flow->ipv6_src;
1011         break;
1012
1013     case MFF_IPV6_DST:
1014         value->ipv6 = flow->ipv6_dst;
1015         break;
1016
1017     case MFF_IPV6_LABEL:
1018         value->be32 = flow->ipv6_label;
1019         break;
1020
1021     case MFF_IP_PROTO:
1022         value->u8 = flow->nw_proto;
1023         break;
1024
1025     case MFF_IP_DSCP:
1026         value->u8 = flow->nw_tos & IP_DSCP_MASK;
1027         break;
1028
1029     case MFF_IP_ECN:
1030         value->u8 = flow->nw_tos & IP_ECN_MASK;
1031         break;
1032
1033     case MFF_IP_TTL:
1034         value->u8 = flow->nw_ttl;
1035         break;
1036
1037     case MFF_IP_FRAG:
1038         value->u8 = flow->nw_frag;
1039         break;
1040
1041     case MFF_ARP_OP:
1042         value->be16 = htons(flow->nw_proto);
1043         break;
1044
1045     case MFF_ARP_SPA:
1046         value->be32 = flow->nw_src;
1047         break;
1048
1049     case MFF_ARP_TPA:
1050         value->be32 = flow->nw_dst;
1051         break;
1052
1053     case MFF_ARP_SHA:
1054     case MFF_ND_SLL:
1055         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1056         break;
1057
1058     case MFF_ARP_THA:
1059     case MFF_ND_TLL:
1060         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1061         break;
1062
1063     case MFF_TCP_SRC:
1064     case MFF_UDP_SRC:
1065         value->be16 = flow->tp_src;
1066         break;
1067
1068     case MFF_TCP_DST:
1069     case MFF_UDP_DST:
1070         value->be16 = flow->tp_dst;
1071         break;
1072
1073     case MFF_ICMPV4_TYPE:
1074     case MFF_ICMPV6_TYPE:
1075         value->u8 = ntohs(flow->tp_src);
1076         break;
1077
1078     case MFF_ICMPV4_CODE:
1079     case MFF_ICMPV6_CODE:
1080         value->u8 = ntohs(flow->tp_dst);
1081         break;
1082
1083     case MFF_ND_TARGET:
1084         value->ipv6 = flow->nd_target;
1085         break;
1086
1087     case MFF_N_IDS:
1088     default:
1089         NOT_REACHED();
1090     }
1091 }
1092
1093 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1094  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1095  * prerequisites. */
1096 void
1097 mf_set_value(const struct mf_field *mf,
1098              const union mf_value *value, struct cls_rule *rule)
1099 {
1100     switch (mf->id) {
1101     case MFF_TUN_ID:
1102         cls_rule_set_tun_id(rule, value->be64);
1103         break;
1104     case MFF_METADATA:
1105         cls_rule_set_metadata(rule, value->be64);
1106         break;
1107
1108     case MFF_IN_PORT:
1109         cls_rule_set_in_port(rule, ntohs(value->be16));
1110         break;
1111
1112     CASE_MFF_REGS:
1113         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1114         break;
1115
1116     case MFF_ETH_SRC:
1117         cls_rule_set_dl_src(rule, value->mac);
1118         break;
1119
1120     case MFF_ETH_DST:
1121         cls_rule_set_dl_dst(rule, value->mac);
1122         break;
1123
1124     case MFF_ETH_TYPE:
1125         cls_rule_set_dl_type(rule, value->be16);
1126         break;
1127
1128     case MFF_VLAN_TCI:
1129         cls_rule_set_dl_tci(rule, value->be16);
1130         break;
1131
1132     case MFF_DL_VLAN:
1133         cls_rule_set_dl_vlan(rule, value->be16);
1134         break;
1135     case MFF_VLAN_VID:
1136         cls_rule_set_vlan_vid(rule, value->be16);
1137         break;
1138
1139     case MFF_DL_VLAN_PCP:
1140     case MFF_VLAN_PCP:
1141         cls_rule_set_dl_vlan_pcp(rule, value->u8);
1142         break;
1143
1144     case MFF_IPV4_SRC:
1145         cls_rule_set_nw_src(rule, value->be32);
1146         break;
1147
1148     case MFF_IPV4_DST:
1149         cls_rule_set_nw_dst(rule, value->be32);
1150         break;
1151
1152     case MFF_IPV6_SRC:
1153         cls_rule_set_ipv6_src(rule, &value->ipv6);
1154         break;
1155
1156     case MFF_IPV6_DST:
1157         cls_rule_set_ipv6_dst(rule, &value->ipv6);
1158         break;
1159
1160     case MFF_IPV6_LABEL:
1161         cls_rule_set_ipv6_label(rule, value->be32);
1162         break;
1163
1164     case MFF_IP_PROTO:
1165         cls_rule_set_nw_proto(rule, value->u8);
1166         break;
1167
1168     case MFF_IP_DSCP:
1169         cls_rule_set_nw_dscp(rule, value->u8);
1170         break;
1171
1172     case MFF_IP_ECN:
1173         cls_rule_set_nw_ecn(rule, value->u8);
1174         break;
1175
1176     case MFF_IP_TTL:
1177         cls_rule_set_nw_ttl(rule, value->u8);
1178         break;
1179
1180     case MFF_IP_FRAG:
1181         cls_rule_set_nw_frag(rule, value->u8);
1182         break;
1183
1184     case MFF_ARP_OP:
1185         cls_rule_set_nw_proto(rule, ntohs(value->be16));
1186         break;
1187
1188     case MFF_ARP_SPA:
1189         cls_rule_set_nw_src(rule, value->be32);
1190         break;
1191
1192     case MFF_ARP_TPA:
1193         cls_rule_set_nw_dst(rule, value->be32);
1194         break;
1195
1196     case MFF_ARP_SHA:
1197     case MFF_ND_SLL:
1198         cls_rule_set_arp_sha(rule, value->mac);
1199         break;
1200
1201     case MFF_ARP_THA:
1202     case MFF_ND_TLL:
1203         cls_rule_set_arp_tha(rule, value->mac);
1204         break;
1205
1206     case MFF_TCP_SRC:
1207     case MFF_UDP_SRC:
1208         cls_rule_set_tp_src(rule, value->be16);
1209         break;
1210
1211     case MFF_TCP_DST:
1212     case MFF_UDP_DST:
1213         cls_rule_set_tp_dst(rule, value->be16);
1214         break;
1215
1216     case MFF_ICMPV4_TYPE:
1217     case MFF_ICMPV6_TYPE:
1218         cls_rule_set_icmp_type(rule, value->u8);
1219         break;
1220
1221     case MFF_ICMPV4_CODE:
1222     case MFF_ICMPV6_CODE:
1223         cls_rule_set_icmp_code(rule, value->u8);
1224         break;
1225
1226     case MFF_ND_TARGET:
1227         cls_rule_set_nd_target(rule, &value->ipv6);
1228         break;
1229
1230     case MFF_N_IDS:
1231     default:
1232         NOT_REACHED();
1233     }
1234 }
1235
1236 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1237  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1238  * prerequisites. */
1239 void
1240 mf_set_flow_value(const struct mf_field *mf,
1241                   const union mf_value *value, struct flow *flow)
1242 {
1243     switch (mf->id) {
1244     case MFF_TUN_ID:
1245         flow->tun_id = value->be64;
1246         break;
1247     case MFF_METADATA:
1248         flow->metadata = value->be64;
1249         break;
1250
1251     case MFF_IN_PORT:
1252         flow->in_port = ntohs(value->be16);
1253         break;
1254
1255     CASE_MFF_REGS:
1256         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1257         break;
1258
1259     case MFF_ETH_SRC:
1260         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1261         break;
1262
1263     case MFF_ETH_DST:
1264         memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1265         break;
1266
1267     case MFF_ETH_TYPE:
1268         flow->dl_type = value->be16;
1269         break;
1270
1271     case MFF_VLAN_TCI:
1272         flow->vlan_tci = value->be16;
1273         break;
1274
1275     case MFF_DL_VLAN:
1276         flow_set_dl_vlan(flow, value->be16);
1277         break;
1278     case MFF_VLAN_VID:
1279         flow_set_vlan_vid(flow, value->be16);
1280         break;
1281
1282     case MFF_DL_VLAN_PCP:
1283     case MFF_VLAN_PCP:
1284         flow_set_vlan_pcp(flow, value->u8);
1285         break;
1286
1287     case MFF_IPV4_SRC:
1288         flow->nw_src = value->be32;
1289         break;
1290
1291     case MFF_IPV4_DST:
1292         flow->nw_dst = value->be32;
1293         break;
1294
1295     case MFF_IPV6_SRC:
1296         flow->ipv6_src = value->ipv6;
1297         break;
1298
1299     case MFF_IPV6_DST:
1300         flow->ipv6_dst = value->ipv6;
1301         break;
1302
1303     case MFF_IPV6_LABEL:
1304         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1305         break;
1306
1307     case MFF_IP_PROTO:
1308         flow->nw_proto = value->u8;
1309         break;
1310
1311     case MFF_IP_DSCP:
1312         flow->nw_tos &= ~IP_DSCP_MASK;
1313         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1314         break;
1315
1316     case MFF_IP_ECN:
1317         flow->nw_tos &= ~IP_ECN_MASK;
1318         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1319         break;
1320
1321     case MFF_IP_TTL:
1322         flow->nw_ttl = value->u8;
1323         break;
1324
1325     case MFF_IP_FRAG:
1326         flow->nw_frag &= value->u8;
1327         break;
1328
1329     case MFF_ARP_OP:
1330         flow->nw_proto = ntohs(value->be16);
1331         break;
1332
1333     case MFF_ARP_SPA:
1334         flow->nw_src = value->be32;
1335         break;
1336
1337     case MFF_ARP_TPA:
1338         flow->nw_dst = value->be32;
1339         break;
1340
1341     case MFF_ARP_SHA:
1342     case MFF_ND_SLL:
1343         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1344         break;
1345
1346     case MFF_ARP_THA:
1347     case MFF_ND_TLL:
1348         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1349         break;
1350
1351     case MFF_TCP_SRC:
1352     case MFF_UDP_SRC:
1353         flow->tp_src = value->be16;
1354         break;
1355
1356     case MFF_TCP_DST:
1357     case MFF_UDP_DST:
1358         flow->tp_dst = value->be16;
1359         break;
1360
1361     case MFF_ICMPV4_TYPE:
1362     case MFF_ICMPV6_TYPE:
1363         flow->tp_src = htons(value->u8);
1364         break;
1365
1366     case MFF_ICMPV4_CODE:
1367     case MFF_ICMPV6_CODE:
1368         flow->tp_dst = htons(value->u8);
1369         break;
1370
1371     case MFF_ND_TARGET:
1372         flow->nd_target = value->ipv6;
1373         break;
1374
1375     case MFF_N_IDS:
1376     default:
1377         NOT_REACHED();
1378     }
1379 }
1380
1381 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1382  *
1383  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1384  * prerequisites. */
1385 bool
1386 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1387 {
1388     union mf_value value;
1389
1390     mf_get_value(mf, flow, &value);
1391     return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1392 }
1393
1394 /* Makes 'rule' wildcard field 'mf'.
1395  *
1396  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1397  * prerequisites. */
1398 void
1399 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1400 {
1401     switch (mf->id) {
1402     case MFF_TUN_ID:
1403         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1404         break;
1405     case MFF_METADATA:
1406         cls_rule_set_metadata_masked(rule, htonll(0), htonll(0));
1407
1408     case MFF_IN_PORT:
1409         rule->wc.wildcards |= FWW_IN_PORT;
1410         rule->flow.in_port = 0;
1411         break;
1412
1413     CASE_MFF_REGS:
1414         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0);
1415         break;
1416
1417     case MFF_ETH_SRC:
1418         memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1419         memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1420         break;
1421
1422     case MFF_ETH_DST:
1423         memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1424         memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1425         break;
1426
1427     case MFF_ETH_TYPE:
1428         rule->wc.wildcards |= FWW_DL_TYPE;
1429         rule->flow.dl_type = htons(0);
1430         break;
1431
1432     case MFF_VLAN_TCI:
1433         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1434         break;
1435
1436     case MFF_DL_VLAN:
1437     case MFF_VLAN_VID:
1438         cls_rule_set_any_vid(rule);
1439         break;
1440
1441     case MFF_DL_VLAN_PCP:
1442     case MFF_VLAN_PCP:
1443         cls_rule_set_any_pcp(rule);
1444         break;
1445
1446     case MFF_IPV4_SRC:
1447     case MFF_ARP_SPA:
1448         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1449         break;
1450
1451     case MFF_IPV4_DST:
1452     case MFF_ARP_TPA:
1453         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1454         break;
1455
1456     case MFF_IPV6_SRC:
1457         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1458         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1459         break;
1460
1461     case MFF_IPV6_DST:
1462         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1463         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1464         break;
1465
1466     case MFF_IPV6_LABEL:
1467         rule->wc.ipv6_label_mask = 0;
1468         rule->flow.ipv6_label = 0;
1469         break;
1470
1471     case MFF_IP_PROTO:
1472         rule->wc.nw_proto_mask = 0;
1473         rule->flow.nw_proto = 0;
1474         break;
1475
1476     case MFF_IP_DSCP:
1477         rule->wc.nw_tos_mask &= ~IP_DSCP_MASK;
1478         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1479         break;
1480
1481     case MFF_IP_ECN:
1482         rule->wc.nw_tos_mask &= ~IP_ECN_MASK;
1483         rule->flow.nw_tos &= ~IP_ECN_MASK;
1484         break;
1485
1486     case MFF_IP_TTL:
1487         rule->wc.nw_ttl_mask = 0;
1488         rule->flow.nw_ttl = 0;
1489         break;
1490
1491     case MFF_IP_FRAG:
1492         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1493         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1494         break;
1495
1496     case MFF_ARP_OP:
1497         rule->wc.nw_proto_mask = 0;
1498         rule->flow.nw_proto = 0;
1499         break;
1500
1501     case MFF_ARP_SHA:
1502     case MFF_ND_SLL:
1503         memset(rule->flow.arp_sha, 0, ETH_ADDR_LEN);
1504         memset(rule->wc.arp_sha_mask, 0, ETH_ADDR_LEN);
1505         break;
1506
1507     case MFF_ARP_THA:
1508     case MFF_ND_TLL:
1509         memset(rule->flow.arp_tha, 0, ETH_ADDR_LEN);
1510         memset(rule->wc.arp_tha_mask, 0, ETH_ADDR_LEN);
1511         break;
1512
1513     case MFF_TCP_SRC:
1514     case MFF_UDP_SRC:
1515     case MFF_ICMPV4_TYPE:
1516     case MFF_ICMPV6_TYPE:
1517         rule->wc.tp_src_mask = htons(0);
1518         rule->flow.tp_src = htons(0);
1519         break;
1520
1521     case MFF_TCP_DST:
1522     case MFF_UDP_DST:
1523     case MFF_ICMPV4_CODE:
1524     case MFF_ICMPV6_CODE:
1525         rule->wc.tp_dst_mask = htons(0);
1526         rule->flow.tp_dst = htons(0);
1527         break;
1528
1529     case MFF_ND_TARGET:
1530         memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1531         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1532         break;
1533
1534     case MFF_N_IDS:
1535     default:
1536         NOT_REACHED();
1537     }
1538 }
1539
1540 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1541  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1542  * with a 1-bit indicating that the corresponding value bit must match and a
1543  * 0-bit indicating a don't-care.
1544  *
1545  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1546  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1547  * call is equivalent to mf_set_wild(mf, rule).
1548  *
1549  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1550  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1551 void
1552 mf_set(const struct mf_field *mf,
1553        const union mf_value *value, const union mf_value *mask,
1554        struct cls_rule *rule)
1555 {
1556     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1557         mf_set_value(mf, value, rule);
1558         return;
1559     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1560         mf_set_wild(mf, rule);
1561         return;
1562     }
1563
1564     switch (mf->id) {
1565     case MFF_IN_PORT:
1566     case MFF_ETH_TYPE:
1567     case MFF_DL_VLAN:
1568     case MFF_DL_VLAN_PCP:
1569     case MFF_VLAN_PCP:
1570     case MFF_IP_PROTO:
1571     case MFF_IP_TTL:
1572     case MFF_IP_DSCP:
1573     case MFF_IP_ECN:
1574     case MFF_ARP_OP:
1575     case MFF_ICMPV4_TYPE:
1576     case MFF_ICMPV4_CODE:
1577     case MFF_ICMPV6_TYPE:
1578     case MFF_ICMPV6_CODE:
1579         NOT_REACHED();
1580
1581     case MFF_TUN_ID:
1582         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1583         break;
1584     case MFF_METADATA:
1585         cls_rule_set_metadata_masked(rule, value->be64, mask->be64);
1586         break;
1587
1588     CASE_MFF_REGS:
1589         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1590                                 ntohl(value->be32), ntohl(mask->be32));
1591         break;
1592
1593     case MFF_ETH_DST:
1594         cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1595         break;
1596
1597     case MFF_ETH_SRC:
1598         cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1599         break;
1600
1601     case MFF_ARP_SHA:
1602     case MFF_ND_SLL:
1603         cls_rule_set_arp_sha_masked(rule, value->mac, mask->mac);
1604         break;
1605
1606     case MFF_ARP_THA:
1607     case MFF_ND_TLL:
1608         cls_rule_set_arp_tha_masked(rule, value->mac, mask->mac);
1609         break;
1610
1611     case MFF_VLAN_TCI:
1612         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1613         break;
1614
1615     case MFF_VLAN_VID:
1616         cls_rule_set_vlan_vid_masked(rule, value->be16, mask->be16);
1617         break;
1618
1619     case MFF_IPV4_SRC:
1620         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1621         break;
1622
1623     case MFF_IPV4_DST:
1624         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1625         break;
1626
1627     case MFF_IPV6_SRC:
1628         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1629         break;
1630
1631     case MFF_IPV6_DST:
1632         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1633         break;
1634
1635     case MFF_IPV6_LABEL:
1636         if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1637             mf_set_value(mf, value, rule);
1638         } else {
1639             cls_rule_set_ipv6_label_masked(rule, value->be32, mask->be32);
1640         }
1641         break;
1642
1643     case MFF_ND_TARGET:
1644         cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1645         break;
1646
1647     case MFF_IP_FRAG:
1648         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1649         break;
1650
1651     case MFF_ARP_SPA:
1652         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1653         break;
1654
1655     case MFF_ARP_TPA:
1656         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1657         break;
1658
1659     case MFF_TCP_SRC:
1660     case MFF_UDP_SRC:
1661         cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1662         break;
1663
1664     case MFF_TCP_DST:
1665     case MFF_UDP_DST:
1666         cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1667         break;
1668
1669     case MFF_N_IDS:
1670     default:
1671         NOT_REACHED();
1672     }
1673 }
1674
1675 static enum ofperr
1676 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1677            const char *type)
1678 {
1679     if (!sf->field) {
1680         VLOG_WARN_RL(&rl, "unknown %s field", type);
1681     } else if (!sf->n_bits) {
1682         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1683     } else if (sf->ofs >= sf->field->n_bits) {
1684         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1685                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1686     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1687         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1688                      "of %s field %s", sf->ofs, sf->n_bits,
1689                      sf->field->n_bits, type, sf->field->name);
1690     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1691         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1692                      type, sf->field->name);
1693     } else {
1694         return 0;
1695     }
1696
1697     return OFPERR_OFPBAC_BAD_ARGUMENT;
1698 }
1699
1700 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1701  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1702  * ofp_mkerr()).  */
1703 enum ofperr
1704 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1705 {
1706     return mf_check__(sf, flow, "source");
1707 }
1708
1709 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1710  * if so, otherwise an OpenFlow error code (e.g. as returned by
1711  * ofp_mkerr()). */
1712 enum ofperr
1713 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1714 {
1715     int error = mf_check__(sf, flow, "destination");
1716     if (!error && !sf->field->writable) {
1717         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1718                      sf->field->name);
1719         return OFPERR_OFPBAC_BAD_ARGUMENT;
1720     }
1721     return error;
1722 }
1723
1724 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1725  * 'value' and 'mask', respectively. */
1726 void
1727 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1728        union mf_value *value, union mf_value *mask)
1729 {
1730     mf_get_value(mf, &rule->flow, value);
1731     mf_get_mask(mf, &rule->wc, mask);
1732 }
1733
1734 /* Assigns a random value for field 'mf' to 'value'. */
1735 void
1736 mf_random_value(const struct mf_field *mf, union mf_value *value)
1737 {
1738     random_bytes(value, mf->n_bytes);
1739
1740     switch (mf->id) {
1741     case MFF_TUN_ID:
1742     case MFF_METADATA:
1743     case MFF_IN_PORT:
1744     CASE_MFF_REGS:
1745     case MFF_ETH_SRC:
1746     case MFF_ETH_DST:
1747     case MFF_ETH_TYPE:
1748     case MFF_VLAN_TCI:
1749     case MFF_IPV4_SRC:
1750     case MFF_IPV4_DST:
1751     case MFF_IPV6_SRC:
1752     case MFF_IPV6_DST:
1753     case MFF_IP_PROTO:
1754     case MFF_IP_TTL:
1755     case MFF_ARP_SPA:
1756     case MFF_ARP_TPA:
1757     case MFF_ARP_SHA:
1758     case MFF_ARP_THA:
1759     case MFF_TCP_SRC:
1760     case MFF_TCP_DST:
1761     case MFF_UDP_SRC:
1762     case MFF_UDP_DST:
1763     case MFF_ICMPV4_TYPE:
1764     case MFF_ICMPV4_CODE:
1765     case MFF_ICMPV6_TYPE:
1766     case MFF_ICMPV6_CODE:
1767     case MFF_ND_TARGET:
1768     case MFF_ND_SLL:
1769     case MFF_ND_TLL:
1770         break;
1771
1772     case MFF_IPV6_LABEL:
1773         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1774         break;
1775
1776     case MFF_IP_DSCP:
1777         value->u8 &= IP_DSCP_MASK;
1778         break;
1779
1780     case MFF_IP_ECN:
1781         value->u8 &= IP_ECN_MASK;
1782         break;
1783
1784     case MFF_IP_FRAG:
1785         value->u8 &= FLOW_NW_FRAG_MASK;
1786         break;
1787
1788     case MFF_ARP_OP:
1789         value->be16 &= htons(0xff);
1790         break;
1791
1792     case MFF_DL_VLAN:
1793         value->be16 &= htons(VLAN_VID_MASK);
1794         break;
1795     case MFF_VLAN_VID:
1796         value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1797         break;
1798
1799     case MFF_DL_VLAN_PCP:
1800     case MFF_VLAN_PCP:
1801         value->u8 &= 0x07;
1802         break;
1803
1804     case MFF_N_IDS:
1805     default:
1806         NOT_REACHED();
1807     }
1808 }
1809
1810 static char *
1811 mf_from_integer_string(const struct mf_field *mf, const char *s,
1812                        uint8_t *valuep, uint8_t *maskp)
1813 {
1814     unsigned long long int integer, mask;
1815     char *tail;
1816     int i;
1817
1818     errno = 0;
1819     integer = strtoull(s, &tail, 0);
1820     if (errno || (*tail != '\0' && *tail != '/')) {
1821         goto syntax_error;
1822     }
1823
1824     if (*tail == '/') {
1825         mask = strtoull(tail + 1, &tail, 0);
1826         if (errno || *tail != '\0') {
1827             goto syntax_error;
1828         }
1829     } else {
1830         mask = ULLONG_MAX;
1831     }
1832
1833     for (i = mf->n_bytes - 1; i >= 0; i--) {
1834         valuep[i] = integer;
1835         maskp[i] = mask;
1836         integer >>= 8;
1837         mask >>= 8;
1838     }
1839     if (integer) {
1840         return xasprintf("%s: value too large for %u-byte field %s",
1841                          s, mf->n_bytes, mf->name);
1842     }
1843     return NULL;
1844
1845 syntax_error:
1846     return xasprintf("%s: bad syntax for %s", s, mf->name);
1847 }
1848
1849 static char *
1850 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1851                         uint8_t mac[ETH_ADDR_LEN],
1852                         uint8_t mask[ETH_ADDR_LEN])
1853 {
1854     assert(mf->n_bytes == ETH_ADDR_LEN);
1855
1856     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1857                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1858     case ETH_ADDR_SCAN_COUNT * 2:
1859         return NULL;
1860
1861     case ETH_ADDR_SCAN_COUNT:
1862         memset(mask, 0xff, ETH_ADDR_LEN);
1863         return NULL;
1864
1865     default:
1866         return xasprintf("%s: invalid Ethernet address", s);
1867     }
1868 }
1869
1870 static char *
1871 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1872                     ovs_be32 *ip, ovs_be32 *mask)
1873 {
1874     int prefix;
1875
1876     assert(mf->n_bytes == sizeof *ip);
1877
1878     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1879                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1880         /* OK. */
1881     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1882                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1883         if (prefix <= 0 || prefix > 32) {
1884             return xasprintf("%s: network prefix bits not between 1 and "
1885                              "32", s);
1886         } else if (prefix == 32) {
1887             *mask = htonl(UINT32_MAX);
1888         } else {
1889             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1890         }
1891     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1892         *mask = htonl(UINT32_MAX);
1893     } else {
1894         return xasprintf("%s: invalid IP address", s);
1895     }
1896     return NULL;
1897 }
1898
1899 static char *
1900 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1901                     struct in6_addr *value, struct in6_addr *mask)
1902 {
1903     char *str = xstrdup(s);
1904     char *save_ptr = NULL;
1905     const char *name, *netmask;
1906     int retval;
1907
1908     assert(mf->n_bytes == sizeof *value);
1909
1910     name = strtok_r(str, "/", &save_ptr);
1911     retval = name ? lookup_ipv6(name, value) : EINVAL;
1912     if (retval) {
1913         char *err;
1914
1915         err = xasprintf("%s: could not convert to IPv6 address", str);
1916         free(str);
1917
1918         return err;
1919     }
1920
1921     netmask = strtok_r(NULL, "/", &save_ptr);
1922     if (netmask) {
1923         if (inet_pton(AF_INET6, netmask, mask) != 1) {
1924             int prefix = atoi(netmask);
1925             if (prefix <= 0 || prefix > 128) {
1926                 free(str);
1927                 return xasprintf("%s: prefix bits not between 1 and 128", s);
1928             } else {
1929                 *mask = ipv6_create_mask(prefix);
1930             }
1931         }
1932     } else {
1933         *mask = in6addr_exact;
1934     }
1935     free(str);
1936
1937     return NULL;
1938 }
1939
1940 static char *
1941 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1942                         ovs_be16 *valuep, ovs_be16 *maskp)
1943 {
1944     uint16_t port;
1945
1946     assert(mf->n_bytes == sizeof(ovs_be16));
1947     if (ofputil_port_from_string(s, &port)) {
1948         *valuep = htons(port);
1949         *maskp = htons(UINT16_MAX);
1950         return NULL;
1951     } else {
1952         return mf_from_integer_string(mf, s,
1953                                       (uint8_t *) valuep, (uint8_t *) maskp);
1954     }
1955 }
1956
1957 struct frag_handling {
1958     const char *name;
1959     uint8_t mask;
1960     uint8_t value;
1961 };
1962
1963 static const struct frag_handling all_frags[] = {
1964 #define A FLOW_NW_FRAG_ANY
1965 #define L FLOW_NW_FRAG_LATER
1966     /* name               mask  value */
1967
1968     { "no",               A|L,  0     },
1969     { "first",            A|L,  A     },
1970     { "later",            A|L,  A|L   },
1971
1972     { "no",               A,    0     },
1973     { "yes",              A,    A     },
1974
1975     { "not_later",        L,    0     },
1976     { "later",            L,    L     },
1977 #undef A
1978 #undef L
1979 };
1980
1981 static char *
1982 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1983 {
1984     const struct frag_handling *h;
1985
1986     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1987         if (!strcasecmp(s, h->name)) {
1988             /* We force the upper bits of the mask on to make mf_parse_value()
1989              * happy (otherwise it will never think it's an exact match.) */
1990             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1991             *valuep = h->value;
1992             return NULL;
1993         }
1994     }
1995
1996     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1997                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1998 }
1999
2000 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2001  * NULL if successful, otherwise a malloc()'d string describing the error. */
2002 char *
2003 mf_parse(const struct mf_field *mf, const char *s,
2004          union mf_value *value, union mf_value *mask)
2005 {
2006     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2007         memset(value, 0, mf->n_bytes);
2008         memset(mask, 0, mf->n_bytes);
2009         return NULL;
2010     }
2011
2012     switch (mf->string) {
2013     case MFS_DECIMAL:
2014     case MFS_HEXADECIMAL:
2015         return mf_from_integer_string(mf, s,
2016                                       (uint8_t *) value, (uint8_t *) mask);
2017
2018     case MFS_ETHERNET:
2019         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2020
2021     case MFS_IPV4:
2022         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2023
2024     case MFS_IPV6:
2025         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2026
2027     case MFS_OFP_PORT:
2028         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2029
2030     case MFS_FRAG:
2031         return mf_from_frag_string(s, &value->u8, &mask->u8);
2032     }
2033     NOT_REACHED();
2034 }
2035
2036 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2037  * successful, otherwise a malloc()'d string describing the error. */
2038 char *
2039 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2040 {
2041     union mf_value mask;
2042     char *error;
2043
2044     error = mf_parse(mf, s, value, &mask);
2045     if (error) {
2046         return error;
2047     }
2048
2049     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2050         return xasprintf("%s: wildcards not allowed here", s);
2051     }
2052     return NULL;
2053 }
2054
2055 static void
2056 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2057                          const uint8_t *maskp, struct ds *s)
2058 {
2059     unsigned long long int integer;
2060     int i;
2061
2062     assert(mf->n_bytes <= 8);
2063
2064     integer = 0;
2065     for (i = 0; i < mf->n_bytes; i++) {
2066         integer = (integer << 8) | valuep[i];
2067     }
2068     if (mf->string == MFS_HEXADECIMAL) {
2069         ds_put_format(s, "%#llx", integer);
2070     } else {
2071         ds_put_format(s, "%lld", integer);
2072     }
2073
2074     if (maskp) {
2075         unsigned long long int mask;
2076
2077         mask = 0;
2078         for (i = 0; i < mf->n_bytes; i++) {
2079             mask = (mask << 8) | maskp[i];
2080         }
2081
2082         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2083          * not sure that that a bit-mask written in decimal is ever easier to
2084          * understand than the same bit-mask written in hexadecimal. */
2085         ds_put_format(s, "/%#llx", mask);
2086     }
2087 }
2088
2089 static void
2090 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2091                       struct ds *s)
2092 {
2093     const struct frag_handling *h;
2094     uint8_t value = *valuep;
2095     uint8_t mask = *maskp;
2096
2097     value &= mask;
2098     mask &= FLOW_NW_FRAG_MASK;
2099
2100     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2101         if (value == h->value && mask == h->mask) {
2102             ds_put_cstr(s, h->name);
2103             return;
2104         }
2105     }
2106     ds_put_cstr(s, "<error>");
2107 }
2108
2109 /* Appends to 's' a string representation of field 'mf' whose value is in
2110  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2111 void
2112 mf_format(const struct mf_field *mf,
2113           const union mf_value *value, const union mf_value *mask,
2114           struct ds *s)
2115 {
2116     if (mask) {
2117         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2118             ds_put_cstr(s, "ANY");
2119             return;
2120         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2121             mask = NULL;
2122         }
2123     }
2124
2125     switch (mf->string) {
2126     case MFS_OFP_PORT:
2127         if (!mask) {
2128             ofputil_format_port(ntohs(value->be16), s);
2129             break;
2130         }
2131         /* fall through */
2132     case MFS_DECIMAL:
2133     case MFS_HEXADECIMAL:
2134         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2135         break;
2136
2137     case MFS_ETHERNET:
2138         eth_format_masked(value->mac, mask->mac, s);
2139         break;
2140
2141     case MFS_IPV4:
2142         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2143                          s);
2144         break;
2145
2146     case MFS_IPV6:
2147         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2148         break;
2149
2150     case MFS_FRAG:
2151         mf_format_frag_string(&value->u8, &mask->u8, s);
2152         break;
2153
2154     default:
2155         NOT_REACHED();
2156     }
2157 }
2158 \f
2159 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2160  * least-significant bits in 'x'.
2161  */
2162 void
2163 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2164                   struct cls_rule *rule)
2165 {
2166     const struct mf_field *field = sf->field;
2167     union mf_value value, mask;
2168
2169     mf_get(field, rule, &value, &mask);
2170     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2171     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2172     mf_set(field, &value, &mask, rule);
2173 }
2174
2175 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2176  * reading 'flow', e.g. as checked by mf_check_src(). */
2177 void
2178 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2179                  union mf_subvalue *x)
2180 {
2181     union mf_value value;
2182
2183     mf_get_value(sf->field, flow, &value);
2184
2185     memset(x, 0, sizeof *x);
2186     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2187                  x, sizeof *x, 0,
2188                  sf->n_bits);
2189 }
2190
2191 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2192  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2193  * less. */
2194 uint64_t
2195 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2196 {
2197     union mf_value value;
2198
2199     mf_get_value(sf->field, flow, &value);
2200     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2201 }
2202
2203 /* Formats 'sf' into 's' in a format normally acceptable to
2204  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2205  * sf->field has no NXM name.) */
2206 void
2207 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2208 {
2209     if (!sf->field) {
2210         ds_put_cstr(s, "<unknown>");
2211     } else if (sf->field->nxm_name) {
2212         ds_put_cstr(s, sf->field->nxm_name);
2213     } else if (sf->field->nxm_header) {
2214         uint32_t header = sf->field->nxm_header;
2215         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2216     } else {
2217         ds_put_cstr(s, sf->field->name);
2218     }
2219
2220     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2221         ds_put_cstr(s, "[]");
2222     } else if (sf->n_bits == 1) {
2223         ds_put_format(s, "[%d]", sf->ofs);
2224     } else {
2225         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2226     }
2227 }
2228
2229 static const struct mf_field *
2230 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2231 {
2232     int i;
2233
2234     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2235     if (*wild) {
2236         name_len -= 2;
2237     }
2238
2239     for (i = 0; i < MFF_N_IDS; i++) {
2240         const struct mf_field *mf = mf_from_id(i);
2241
2242         if (mf->nxm_name
2243             && !strncmp(mf->nxm_name, name, name_len)
2244             && mf->nxm_name[name_len] == '\0') {
2245             return mf;
2246         }
2247         if (mf->oxm_name
2248             && !strncmp(mf->oxm_name, name, name_len)
2249             && mf->oxm_name[name_len] == '\0') {
2250             return mf;
2251         }
2252     }
2253
2254     return NULL;
2255 }
2256
2257 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2258  * returns NULL and advances '*sp' to the first byte following the parsed
2259  * string.  On failure, returns a malloc()'d error message, does not modify
2260  * '*sp', and does not properly initialize 'sf'.
2261  *
2262  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2263  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2264  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2265  * may both be omitted (the [] are still required) to indicate an entire
2266  * field. */
2267 char *
2268 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2269 {
2270     const struct mf_field *field;
2271     const char *name;
2272     int start, end;
2273     const char *s;
2274     int name_len;
2275     bool wild;
2276
2277     s = *sp;
2278     name = s;
2279     name_len = strcspn(s, "[");
2280     if (s[name_len] != '[') {
2281         return xasprintf("%s: missing [ looking for field name", *sp);
2282     }
2283
2284     field = mf_parse_subfield_name(name, name_len, &wild);
2285     if (!field) {
2286         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2287     }
2288
2289     s += name_len;
2290     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2291         /* Nothing to do. */
2292     } else if (sscanf(s, "[%d]", &start) == 1) {
2293         end = start;
2294     } else if (!strncmp(s, "[]", 2)) {
2295         start = 0;
2296         end = field->n_bits - 1;
2297     } else {
2298         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2299                          "[<start>..<end>]", *sp);
2300     }
2301     s = strchr(s, ']') + 1;
2302
2303     if (start > end) {
2304         return xasprintf("%s: starting bit %d is after ending bit %d",
2305                          *sp, start, end);
2306     } else if (start >= field->n_bits) {
2307         return xasprintf("%s: starting bit %d is not valid because field is "
2308                          "only %d bits wide", *sp, start, field->n_bits);
2309     } else if (end >= field->n_bits){
2310         return xasprintf("%s: ending bit %d is not valid because field is "
2311                          "only %d bits wide", *sp, end, field->n_bits);
2312     }
2313
2314     sf->field = field;
2315     sf->ofs = start;
2316     sf->n_bits = end - start + 1;
2317
2318     *sp = s;
2319     return NULL;
2320 }
2321
2322 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2323  * byte in 's' following the parsed string.
2324  *
2325  * Exits with an error message if 's' has incorrect syntax.
2326  *
2327  * The syntax parsed from 's' takes the form "header[start..end]" where
2328  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2329  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2330  * may both be omitted (the [] are still required) to indicate an entire
2331  * field.  */
2332 const char *
2333 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2334 {
2335     char *msg = mf_parse_subfield__(sf, &s);
2336     if (msg) {
2337         ovs_fatal(0, "%s", msg);
2338     }
2339     return s;
2340 }