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