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