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