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