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