vswitchd: Log all tunnel parameters of given flow.
[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     switch (mf->id) {
723     case MFF_TUN_ID:
724     case MFF_TUN_SRC:
725     case MFF_TUN_DST:
726     case MFF_TUN_TOS:
727     case MFF_TUN_TTL:
728     case MFF_TUN_FLAGS:
729         mask->be64 = wc->masks.tunnel.tun_id;
730         break;
731     case MFF_METADATA:
732         mask->be64 = wc->masks.metadata;
733         break;
734     case MFF_IN_PORT:
735         mask->be16 = htons(wc->masks.in_port);
736         break;
737     CASE_MFF_REGS:
738         mask->be32 = htonl(wc->masks.regs[mf->id - MFF_REG0]);
739         break;
740
741     case MFF_ETH_DST:
742         memcpy(mask->mac, wc->masks.dl_dst, ETH_ADDR_LEN);
743         break;
744     case MFF_ETH_SRC:
745         memcpy(mask->mac, wc->masks.dl_src, ETH_ADDR_LEN);
746         break;
747     case MFF_ETH_TYPE:
748         mask->be16 = wc->masks.dl_type;
749         break;
750
751     case MFF_VLAN_TCI:
752         mask->be16 = wc->masks.vlan_tci;
753         break;
754     case MFF_DL_VLAN:
755         mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
756         break;
757     case MFF_VLAN_VID:
758         mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
759         break;
760     case MFF_DL_VLAN_PCP:
761     case MFF_VLAN_PCP:
762         mask->u8 = vlan_tci_to_pcp(wc->masks.vlan_tci);
763         break;
764
765     case MFF_IPV4_SRC:
766         mask->be32 = wc->masks.nw_src;
767         break;
768     case MFF_IPV4_DST:
769         mask->be32 = wc->masks.nw_dst;
770         break;
771
772     case MFF_IPV6_SRC:
773         mask->ipv6 = wc->masks.ipv6_src;
774         break;
775     case MFF_IPV6_DST:
776         mask->ipv6 = wc->masks.ipv6_dst;
777         break;
778     case MFF_IPV6_LABEL:
779         mask->be32 = wc->masks.ipv6_label;
780         break;
781
782     case MFF_IP_PROTO:
783         mask->u8 = wc->masks.nw_proto;
784         break;
785     case MFF_IP_DSCP:
786         mask->u8 = wc->masks.nw_tos & IP_DSCP_MASK;
787         break;
788     case MFF_IP_ECN:
789         mask->u8 = wc->masks.nw_tos & IP_ECN_MASK;
790         break;
791
792     case MFF_ND_TARGET:
793         mask->ipv6 = wc->masks.nd_target;
794         break;
795
796     case MFF_IP_TTL:
797         mask->u8 = wc->masks.nw_ttl;
798         break;
799     case MFF_IP_FRAG:
800         mask->u8 = wc->masks.nw_frag & FLOW_NW_FRAG_MASK;
801         break;
802
803     case MFF_ARP_OP:
804         mask->u8 = wc->masks.nw_proto;
805         break;
806     case MFF_ARP_SPA:
807         mask->be32 = wc->masks.nw_src;
808         break;
809     case MFF_ARP_TPA:
810         mask->be32 = wc->masks.nw_dst;
811         break;
812     case MFF_ARP_SHA:
813     case MFF_ND_SLL:
814         memcpy(mask->mac, wc->masks.arp_sha, ETH_ADDR_LEN);
815         break;
816     case MFF_ARP_THA:
817     case MFF_ND_TLL:
818         memcpy(mask->mac, wc->masks.arp_tha, ETH_ADDR_LEN);
819         break;
820
821     case MFF_TCP_SRC:
822     case MFF_UDP_SRC:
823         mask->be16 = wc->masks.tp_src;
824         break;
825     case MFF_TCP_DST:
826     case MFF_UDP_DST:
827         mask->be16 = wc->masks.tp_dst;
828         break;
829
830     case MFF_ICMPV4_TYPE:
831     case MFF_ICMPV6_TYPE:
832         mask->u8 = ntohs(wc->masks.tp_src);
833         break;
834     case MFF_ICMPV4_CODE:
835     case MFF_ICMPV6_CODE:
836         mask->u8 = ntohs(wc->masks.tp_dst);
837         break;
838
839     case MFF_N_IDS:
840     default:
841         NOT_REACHED();
842     }
843 }
844
845 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
846  * if the mask is valid, false otherwise. */
847 bool
848 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
849 {
850     switch (mf->maskable) {
851     case MFM_NONE:
852         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
853                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
854
855     case MFM_FULLY:
856         return true;
857     }
858
859     NOT_REACHED();
860 }
861
862 static bool
863 is_ip_any(const struct flow *flow)
864 {
865     return (flow->dl_type == htons(ETH_TYPE_IP) ||
866             flow->dl_type == htons(ETH_TYPE_IPV6));
867 }
868
869 static bool
870 is_icmpv4(const struct flow *flow)
871 {
872     return (flow->dl_type == htons(ETH_TYPE_IP)
873             && flow->nw_proto == IPPROTO_ICMP);
874 }
875
876 static bool
877 is_icmpv6(const struct flow *flow)
878 {
879     return (flow->dl_type == htons(ETH_TYPE_IPV6)
880             && flow->nw_proto == IPPROTO_ICMPV6);
881 }
882
883 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
884 bool
885 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
886 {
887     switch (mf->prereqs) {
888     case MFP_NONE:
889         return true;
890
891     case MFP_ARP:
892       return (flow->dl_type == htons(ETH_TYPE_ARP) ||
893               flow->dl_type == htons(ETH_TYPE_RARP));
894     case MFP_IPV4:
895         return flow->dl_type == htons(ETH_TYPE_IP);
896     case MFP_IPV6:
897         return flow->dl_type == htons(ETH_TYPE_IPV6);
898     case MFP_VLAN_VID:
899         return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
900     case MFP_IP_ANY:
901         return is_ip_any(flow);
902
903     case MFP_TCP:
904         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
905     case MFP_UDP:
906         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
907     case MFP_ICMPV4:
908         return is_icmpv4(flow);
909     case MFP_ICMPV6:
910         return is_icmpv6(flow);
911
912     case MFP_ND:
913         return (is_icmpv6(flow)
914                 && flow->tp_dst == htons(0)
915                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
916                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
917     case MFP_ND_SOLICIT:
918         return (is_icmpv6(flow)
919                 && flow->tp_dst == htons(0)
920                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
921     case MFP_ND_ADVERT:
922         return (is_icmpv6(flow)
923                 && flow->tp_dst == htons(0)
924                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
925     }
926
927     NOT_REACHED();
928 }
929
930 /* Returns true if 'value' may be a valid value *as part of a masked match*,
931  * false otherwise.
932  *
933  * A value is not rejected just because it is not valid for the field in
934  * question, but only if it doesn't make sense to test the bits in question at
935  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
936  * without the VLAN_CFI bit being set, but we can't reject those values because
937  * it is still legitimate to test just for those bits (see the documentation
938  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
939  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
940 bool
941 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
942 {
943     switch (mf->id) {
944     case MFF_TUN_ID:
945     case MFF_TUN_SRC:
946     case MFF_TUN_DST:
947     case MFF_TUN_TOS:
948     case MFF_TUN_TTL:
949     case MFF_TUN_FLAGS:
950     case MFF_METADATA:
951     case MFF_IN_PORT:
952     CASE_MFF_REGS:
953     case MFF_ETH_SRC:
954     case MFF_ETH_DST:
955     case MFF_ETH_TYPE:
956     case MFF_VLAN_TCI:
957     case MFF_IPV4_SRC:
958     case MFF_IPV4_DST:
959     case MFF_IPV6_SRC:
960     case MFF_IPV6_DST:
961     case MFF_IP_PROTO:
962     case MFF_IP_TTL:
963     case MFF_ARP_SPA:
964     case MFF_ARP_TPA:
965     case MFF_ARP_SHA:
966     case MFF_ARP_THA:
967     case MFF_TCP_SRC:
968     case MFF_TCP_DST:
969     case MFF_UDP_SRC:
970     case MFF_UDP_DST:
971     case MFF_ICMPV4_TYPE:
972     case MFF_ICMPV4_CODE:
973     case MFF_ICMPV6_TYPE:
974     case MFF_ICMPV6_CODE:
975     case MFF_ND_TARGET:
976     case MFF_ND_SLL:
977     case MFF_ND_TLL:
978         return true;
979
980     case MFF_IP_DSCP:
981         return !(value->u8 & ~IP_DSCP_MASK);
982     case MFF_IP_ECN:
983         return !(value->u8 & ~IP_ECN_MASK);
984     case MFF_IP_FRAG:
985         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
986
987     case MFF_ARP_OP:
988         return !(value->be16 & htons(0xff00));
989
990     case MFF_DL_VLAN:
991         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
992     case MFF_VLAN_VID:
993         return !(value->be16 & htons(VLAN_PCP_MASK));
994
995     case MFF_DL_VLAN_PCP:
996     case MFF_VLAN_PCP:
997         return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
998
999     case MFF_IPV6_LABEL:
1000         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1001
1002     case MFF_N_IDS:
1003     default:
1004         NOT_REACHED();
1005     }
1006 }
1007
1008 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
1009  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1010 void
1011 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1012              union mf_value *value)
1013 {
1014     switch (mf->id) {
1015     case MFF_TUN_ID:
1016         value->be64 = flow->tunnel.tun_id;
1017         break;
1018     case MFF_TUN_SRC:
1019         value->be32 = flow->tunnel.ip_src;
1020         break;
1021     case MFF_TUN_DST:
1022         value->be32 = flow->tunnel.ip_dst;
1023         break;
1024     case MFF_TUN_FLAGS:
1025         value->be16 = htons(flow->tunnel.flags);
1026         break;
1027     case MFF_TUN_TTL:
1028         value->u8 = flow->tunnel.ip_ttl;
1029         break;
1030     case MFF_TUN_TOS:
1031         value->u8 = flow->tunnel.ip_tos;
1032         break;
1033
1034     case MFF_METADATA:
1035         value->be64 = flow->metadata;
1036         break;
1037
1038     case MFF_IN_PORT:
1039         value->be16 = htons(flow->in_port);
1040         break;
1041
1042     CASE_MFF_REGS:
1043         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1044         break;
1045
1046     case MFF_ETH_SRC:
1047         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1048         break;
1049
1050     case MFF_ETH_DST:
1051         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1052         break;
1053
1054     case MFF_ETH_TYPE:
1055         value->be16 = flow->dl_type;
1056         break;
1057
1058     case MFF_VLAN_TCI:
1059         value->be16 = flow->vlan_tci;
1060         break;
1061
1062     case MFF_DL_VLAN:
1063         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1064         break;
1065     case MFF_VLAN_VID:
1066         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1067         break;
1068
1069     case MFF_DL_VLAN_PCP:
1070     case MFF_VLAN_PCP:
1071         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1072         break;
1073
1074     case MFF_IPV4_SRC:
1075         value->be32 = flow->nw_src;
1076         break;
1077
1078     case MFF_IPV4_DST:
1079         value->be32 = flow->nw_dst;
1080         break;
1081
1082     case MFF_IPV6_SRC:
1083         value->ipv6 = flow->ipv6_src;
1084         break;
1085
1086     case MFF_IPV6_DST:
1087         value->ipv6 = flow->ipv6_dst;
1088         break;
1089
1090     case MFF_IPV6_LABEL:
1091         value->be32 = flow->ipv6_label;
1092         break;
1093
1094     case MFF_IP_PROTO:
1095         value->u8 = flow->nw_proto;
1096         break;
1097
1098     case MFF_IP_DSCP:
1099         value->u8 = flow->nw_tos & IP_DSCP_MASK;
1100         break;
1101
1102     case MFF_IP_ECN:
1103         value->u8 = flow->nw_tos & IP_ECN_MASK;
1104         break;
1105
1106     case MFF_IP_TTL:
1107         value->u8 = flow->nw_ttl;
1108         break;
1109
1110     case MFF_IP_FRAG:
1111         value->u8 = flow->nw_frag;
1112         break;
1113
1114     case MFF_ARP_OP:
1115         value->be16 = htons(flow->nw_proto);
1116         break;
1117
1118     case MFF_ARP_SPA:
1119         value->be32 = flow->nw_src;
1120         break;
1121
1122     case MFF_ARP_TPA:
1123         value->be32 = flow->nw_dst;
1124         break;
1125
1126     case MFF_ARP_SHA:
1127     case MFF_ND_SLL:
1128         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1129         break;
1130
1131     case MFF_ARP_THA:
1132     case MFF_ND_TLL:
1133         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1134         break;
1135
1136     case MFF_TCP_SRC:
1137     case MFF_UDP_SRC:
1138         value->be16 = flow->tp_src;
1139         break;
1140
1141     case MFF_TCP_DST:
1142     case MFF_UDP_DST:
1143         value->be16 = flow->tp_dst;
1144         break;
1145
1146     case MFF_ICMPV4_TYPE:
1147     case MFF_ICMPV6_TYPE:
1148         value->u8 = ntohs(flow->tp_src);
1149         break;
1150
1151     case MFF_ICMPV4_CODE:
1152     case MFF_ICMPV6_CODE:
1153         value->u8 = ntohs(flow->tp_dst);
1154         break;
1155
1156     case MFF_ND_TARGET:
1157         value->ipv6 = flow->nd_target;
1158         break;
1159
1160     case MFF_N_IDS:
1161     default:
1162         NOT_REACHED();
1163     }
1164 }
1165
1166 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1167  * 'value'.  The caller is responsible for ensuring that 'match' meets 'mf''s
1168  * prerequisites. */
1169 void
1170 mf_set_value(const struct mf_field *mf,
1171              const union mf_value *value, struct match *match)
1172 {
1173     switch (mf->id) {
1174     case MFF_TUN_ID:
1175         match_set_tun_id(match, value->be64);
1176         break;
1177     case MFF_TUN_SRC:
1178         match_set_tun_src(match, value->be32);
1179         break;
1180     case MFF_TUN_DST:
1181         match_set_tun_dst(match, value->be32);
1182         break;
1183     case MFF_TUN_FLAGS:
1184         match_set_tun_flags(match, ntohs(value->be16));
1185         break;
1186     case MFF_TUN_TOS:
1187         match_set_tun_tos(match, value->u8);
1188         break;
1189     case MFF_TUN_TTL:
1190         match_set_tun_ttl(match, value->u8);
1191         break;
1192
1193     case MFF_METADATA:
1194         match_set_metadata(match, value->be64);
1195         break;
1196
1197     case MFF_IN_PORT:
1198         match_set_in_port(match, ntohs(value->be16));
1199         break;
1200
1201     CASE_MFF_REGS:
1202         match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1203         break;
1204
1205     case MFF_ETH_SRC:
1206         match_set_dl_src(match, value->mac);
1207         break;
1208
1209     case MFF_ETH_DST:
1210         match_set_dl_dst(match, value->mac);
1211         break;
1212
1213     case MFF_ETH_TYPE:
1214         match_set_dl_type(match, value->be16);
1215         break;
1216
1217     case MFF_VLAN_TCI:
1218         match_set_dl_tci(match, value->be16);
1219         break;
1220
1221     case MFF_DL_VLAN:
1222         match_set_dl_vlan(match, value->be16);
1223         break;
1224     case MFF_VLAN_VID:
1225         match_set_vlan_vid(match, value->be16);
1226         break;
1227
1228     case MFF_DL_VLAN_PCP:
1229     case MFF_VLAN_PCP:
1230         match_set_dl_vlan_pcp(match, value->u8);
1231         break;
1232
1233     case MFF_IPV4_SRC:
1234         match_set_nw_src(match, value->be32);
1235         break;
1236
1237     case MFF_IPV4_DST:
1238         match_set_nw_dst(match, value->be32);
1239         break;
1240
1241     case MFF_IPV6_SRC:
1242         match_set_ipv6_src(match, &value->ipv6);
1243         break;
1244
1245     case MFF_IPV6_DST:
1246         match_set_ipv6_dst(match, &value->ipv6);
1247         break;
1248
1249     case MFF_IPV6_LABEL:
1250         match_set_ipv6_label(match, value->be32);
1251         break;
1252
1253     case MFF_IP_PROTO:
1254         match_set_nw_proto(match, value->u8);
1255         break;
1256
1257     case MFF_IP_DSCP:
1258         match_set_nw_dscp(match, value->u8);
1259         break;
1260
1261     case MFF_IP_ECN:
1262         match_set_nw_ecn(match, value->u8);
1263         break;
1264
1265     case MFF_IP_TTL:
1266         match_set_nw_ttl(match, value->u8);
1267         break;
1268
1269     case MFF_IP_FRAG:
1270         match_set_nw_frag(match, value->u8);
1271         break;
1272
1273     case MFF_ARP_OP:
1274         match_set_nw_proto(match, ntohs(value->be16));
1275         break;
1276
1277     case MFF_ARP_SPA:
1278         match_set_nw_src(match, value->be32);
1279         break;
1280
1281     case MFF_ARP_TPA:
1282         match_set_nw_dst(match, value->be32);
1283         break;
1284
1285     case MFF_ARP_SHA:
1286     case MFF_ND_SLL:
1287         match_set_arp_sha(match, value->mac);
1288         break;
1289
1290     case MFF_ARP_THA:
1291     case MFF_ND_TLL:
1292         match_set_arp_tha(match, value->mac);
1293         break;
1294
1295     case MFF_TCP_SRC:
1296     case MFF_UDP_SRC:
1297         match_set_tp_src(match, value->be16);
1298         break;
1299
1300     case MFF_TCP_DST:
1301     case MFF_UDP_DST:
1302         match_set_tp_dst(match, value->be16);
1303         break;
1304
1305     case MFF_ICMPV4_TYPE:
1306     case MFF_ICMPV6_TYPE:
1307         match_set_icmp_type(match, value->u8);
1308         break;
1309
1310     case MFF_ICMPV4_CODE:
1311     case MFF_ICMPV6_CODE:
1312         match_set_icmp_code(match, value->u8);
1313         break;
1314
1315     case MFF_ND_TARGET:
1316         match_set_nd_target(match, &value->ipv6);
1317         break;
1318
1319     case MFF_N_IDS:
1320     default:
1321         NOT_REACHED();
1322     }
1323 }
1324
1325 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1326  * 'value'.  The caller is responsible for ensuring that 'match' meets 'mf''s
1327  * prerequisites. */
1328 void
1329 mf_set_flow_value(const struct mf_field *mf,
1330                   const union mf_value *value, struct flow *flow)
1331 {
1332     switch (mf->id) {
1333     case MFF_TUN_ID:
1334         flow->tunnel.tun_id = value->be64;
1335         break;
1336     case MFF_TUN_SRC:
1337         flow->tunnel.ip_src = value->be32;
1338         break;
1339     case MFF_TUN_DST:
1340         flow->tunnel.ip_dst = value->be32;
1341         break;
1342     case MFF_TUN_FLAGS:
1343         flow->tunnel.flags = ntohs(value->be16);
1344         break;
1345     case MFF_TUN_TOS:
1346         flow->tunnel.ip_tos = value->u8;
1347         break;
1348     case MFF_TUN_TTL:
1349         flow->tunnel.ip_ttl = value->u8;
1350         break;
1351
1352     case MFF_METADATA:
1353         flow->metadata = value->be64;
1354         break;
1355
1356     case MFF_IN_PORT:
1357         flow->in_port = ntohs(value->be16);
1358         break;
1359
1360     CASE_MFF_REGS:
1361         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1362         break;
1363
1364     case MFF_ETH_SRC:
1365         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1366         break;
1367
1368     case MFF_ETH_DST:
1369         memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1370         break;
1371
1372     case MFF_ETH_TYPE:
1373         flow->dl_type = value->be16;
1374         break;
1375
1376     case MFF_VLAN_TCI:
1377         flow->vlan_tci = value->be16;
1378         break;
1379
1380     case MFF_DL_VLAN:
1381         flow_set_dl_vlan(flow, value->be16);
1382         break;
1383     case MFF_VLAN_VID:
1384         flow_set_vlan_vid(flow, value->be16);
1385         break;
1386
1387     case MFF_DL_VLAN_PCP:
1388     case MFF_VLAN_PCP:
1389         flow_set_vlan_pcp(flow, value->u8);
1390         break;
1391
1392     case MFF_IPV4_SRC:
1393         flow->nw_src = value->be32;
1394         break;
1395
1396     case MFF_IPV4_DST:
1397         flow->nw_dst = value->be32;
1398         break;
1399
1400     case MFF_IPV6_SRC:
1401         flow->ipv6_src = value->ipv6;
1402         break;
1403
1404     case MFF_IPV6_DST:
1405         flow->ipv6_dst = value->ipv6;
1406         break;
1407
1408     case MFF_IPV6_LABEL:
1409         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1410         break;
1411
1412     case MFF_IP_PROTO:
1413         flow->nw_proto = value->u8;
1414         break;
1415
1416     case MFF_IP_DSCP:
1417         flow->nw_tos &= ~IP_DSCP_MASK;
1418         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1419         break;
1420
1421     case MFF_IP_ECN:
1422         flow->nw_tos &= ~IP_ECN_MASK;
1423         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1424         break;
1425
1426     case MFF_IP_TTL:
1427         flow->nw_ttl = value->u8;
1428         break;
1429
1430     case MFF_IP_FRAG:
1431         flow->nw_frag &= value->u8;
1432         break;
1433
1434     case MFF_ARP_OP:
1435         flow->nw_proto = ntohs(value->be16);
1436         break;
1437
1438     case MFF_ARP_SPA:
1439         flow->nw_src = value->be32;
1440         break;
1441
1442     case MFF_ARP_TPA:
1443         flow->nw_dst = value->be32;
1444         break;
1445
1446     case MFF_ARP_SHA:
1447     case MFF_ND_SLL:
1448         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1449         break;
1450
1451     case MFF_ARP_THA:
1452     case MFF_ND_TLL:
1453         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1454         break;
1455
1456     case MFF_TCP_SRC:
1457     case MFF_UDP_SRC:
1458         flow->tp_src = value->be16;
1459         break;
1460
1461     case MFF_TCP_DST:
1462     case MFF_UDP_DST:
1463         flow->tp_dst = value->be16;
1464         break;
1465
1466     case MFF_ICMPV4_TYPE:
1467     case MFF_ICMPV6_TYPE:
1468         flow->tp_src = htons(value->u8);
1469         break;
1470
1471     case MFF_ICMPV4_CODE:
1472     case MFF_ICMPV6_CODE:
1473         flow->tp_dst = htons(value->u8);
1474         break;
1475
1476     case MFF_ND_TARGET:
1477         flow->nd_target = value->ipv6;
1478         break;
1479
1480     case MFF_N_IDS:
1481     default:
1482         NOT_REACHED();
1483     }
1484 }
1485
1486 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1487  *
1488  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1489  * prerequisites. */
1490 bool
1491 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1492 {
1493     union mf_value value;
1494
1495     mf_get_value(mf, flow, &value);
1496     return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1497 }
1498
1499 /* Makes 'match' wildcard field 'mf'.
1500  *
1501  * The caller is responsible for ensuring that 'match' meets 'mf''s
1502  * prerequisites. */
1503 void
1504 mf_set_wild(const struct mf_field *mf, struct match *match)
1505 {
1506     switch (mf->id) {
1507     case MFF_TUN_ID:
1508         match_set_tun_id_masked(match, htonll(0), htonll(0));
1509         break;
1510     case MFF_TUN_SRC:
1511         match_set_tun_src_masked(match, htonl(0), htonl(0));
1512         break;
1513     case MFF_TUN_DST:
1514         match_set_tun_dst_masked(match, htonl(0), htonl(0));
1515         break;
1516     case MFF_TUN_FLAGS:
1517         match_set_tun_flags_masked(match, 0, 0);
1518         break;
1519     case MFF_TUN_TOS:
1520         match_set_tun_tos_masked(match, 0, 0);
1521         break;
1522     case MFF_TUN_TTL:
1523         match_set_tun_ttl_masked(match, 0, 0);
1524         break;
1525
1526     case MFF_METADATA:
1527         match_set_metadata_masked(match, htonll(0), htonll(0));
1528
1529     case MFF_IN_PORT:
1530         match->flow.in_port = 0;
1531         match->wc.masks.in_port = 0;
1532         break;
1533
1534     CASE_MFF_REGS:
1535         match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1536         break;
1537
1538     case MFF_ETH_SRC:
1539         memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1540         memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1541         break;
1542
1543     case MFF_ETH_DST:
1544         memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1545         memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1546         break;
1547
1548     case MFF_ETH_TYPE:
1549         match->flow.dl_type = htons(0);
1550         match->wc.masks.dl_type = htons(0);
1551         break;
1552
1553     case MFF_VLAN_TCI:
1554         match_set_dl_tci_masked(match, htons(0), htons(0));
1555         break;
1556
1557     case MFF_DL_VLAN:
1558     case MFF_VLAN_VID:
1559         match_set_any_vid(match);
1560         break;
1561
1562     case MFF_DL_VLAN_PCP:
1563     case MFF_VLAN_PCP:
1564         match_set_any_pcp(match);
1565         break;
1566
1567     case MFF_IPV4_SRC:
1568     case MFF_ARP_SPA:
1569         match_set_nw_src_masked(match, htonl(0), htonl(0));
1570         break;
1571
1572     case MFF_IPV4_DST:
1573     case MFF_ARP_TPA:
1574         match_set_nw_dst_masked(match, htonl(0), htonl(0));
1575         break;
1576
1577     case MFF_IPV6_SRC:
1578         memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1579         memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1580         break;
1581
1582     case MFF_IPV6_DST:
1583         memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1584         memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1585         break;
1586
1587     case MFF_IPV6_LABEL:
1588         match->wc.masks.ipv6_label = htonl(0);
1589         match->flow.ipv6_label = htonl(0);
1590         break;
1591
1592     case MFF_IP_PROTO:
1593         match->wc.masks.nw_proto = 0;
1594         match->flow.nw_proto = 0;
1595         break;
1596
1597     case MFF_IP_DSCP:
1598         match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1599         match->flow.nw_tos &= ~IP_DSCP_MASK;
1600         break;
1601
1602     case MFF_IP_ECN:
1603         match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1604         match->flow.nw_tos &= ~IP_ECN_MASK;
1605         break;
1606
1607     case MFF_IP_TTL:
1608         match->wc.masks.nw_ttl = 0;
1609         match->flow.nw_ttl = 0;
1610         break;
1611
1612     case MFF_IP_FRAG:
1613         match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1614         match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1615         break;
1616
1617     case MFF_ARP_OP:
1618         match->wc.masks.nw_proto = 0;
1619         match->flow.nw_proto = 0;
1620         break;
1621
1622     case MFF_ARP_SHA:
1623     case MFF_ND_SLL:
1624         memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1625         memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1626         break;
1627
1628     case MFF_ARP_THA:
1629     case MFF_ND_TLL:
1630         memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1631         memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1632         break;
1633
1634     case MFF_TCP_SRC:
1635     case MFF_UDP_SRC:
1636     case MFF_ICMPV4_TYPE:
1637     case MFF_ICMPV6_TYPE:
1638         match->wc.masks.tp_src = htons(0);
1639         match->flow.tp_src = htons(0);
1640         break;
1641
1642     case MFF_TCP_DST:
1643     case MFF_UDP_DST:
1644     case MFF_ICMPV4_CODE:
1645     case MFF_ICMPV6_CODE:
1646         match->wc.masks.tp_dst = htons(0);
1647         match->flow.tp_dst = htons(0);
1648         break;
1649
1650     case MFF_ND_TARGET:
1651         memset(&match->wc.masks.nd_target, 0,
1652                sizeof match->wc.masks.nd_target);
1653         memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1654         break;
1655
1656     case MFF_N_IDS:
1657     default:
1658         NOT_REACHED();
1659     }
1660 }
1661
1662 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1663  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1664  * with a 1-bit indicating that the corresponding value bit must match and a
1665  * 0-bit indicating a don't-care.
1666  *
1667  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1668  * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
1669  * call is equivalent to mf_set_wild(mf, match).
1670  *
1671  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1672  * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1673 void
1674 mf_set(const struct mf_field *mf,
1675        const union mf_value *value, const union mf_value *mask,
1676        struct match *match)
1677 {
1678     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1679         mf_set_value(mf, value, match);
1680         return;
1681     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1682         mf_set_wild(mf, match);
1683         return;
1684     }
1685
1686     switch (mf->id) {
1687     case MFF_IN_PORT:
1688     case MFF_ETH_TYPE:
1689     case MFF_DL_VLAN:
1690     case MFF_DL_VLAN_PCP:
1691     case MFF_VLAN_PCP:
1692     case MFF_IP_PROTO:
1693     case MFF_IP_TTL:
1694     case MFF_IP_DSCP:
1695     case MFF_IP_ECN:
1696     case MFF_ARP_OP:
1697     case MFF_ICMPV4_TYPE:
1698     case MFF_ICMPV4_CODE:
1699     case MFF_ICMPV6_TYPE:
1700     case MFF_ICMPV6_CODE:
1701         NOT_REACHED();
1702
1703     case MFF_TUN_ID:
1704         match_set_tun_id_masked(match, value->be64, mask->be64);
1705         break;
1706     case MFF_TUN_SRC:
1707         match_set_tun_src_masked(match, value->be32, mask->be32);
1708         break;
1709     case MFF_TUN_DST:
1710         match_set_tun_dst_masked(match, value->be32, mask->be32);
1711         break;
1712     case MFF_TUN_FLAGS:
1713         match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1714         break;
1715     case MFF_TUN_TTL:
1716         match_set_tun_ttl_masked(match, value->u8, mask->u8);
1717         break;
1718     case MFF_TUN_TOS:
1719         match_set_tun_tos_masked(match, value->u8, mask->u8);
1720         break;
1721
1722     case MFF_METADATA:
1723         match_set_metadata_masked(match, value->be64, mask->be64);
1724         break;
1725
1726     CASE_MFF_REGS:
1727         match_set_reg_masked(match, mf->id - MFF_REG0,
1728                              ntohl(value->be32), ntohl(mask->be32));
1729         break;
1730
1731     case MFF_ETH_DST:
1732         match_set_dl_dst_masked(match, value->mac, mask->mac);
1733         break;
1734
1735     case MFF_ETH_SRC:
1736         match_set_dl_src_masked(match, value->mac, mask->mac);
1737         break;
1738
1739     case MFF_ARP_SHA:
1740     case MFF_ND_SLL:
1741         match_set_arp_sha_masked(match, value->mac, mask->mac);
1742         break;
1743
1744     case MFF_ARP_THA:
1745     case MFF_ND_TLL:
1746         match_set_arp_tha_masked(match, value->mac, mask->mac);
1747         break;
1748
1749     case MFF_VLAN_TCI:
1750         match_set_dl_tci_masked(match, value->be16, mask->be16);
1751         break;
1752
1753     case MFF_VLAN_VID:
1754         match_set_vlan_vid_masked(match, value->be16, mask->be16);
1755         break;
1756
1757     case MFF_IPV4_SRC:
1758         match_set_nw_src_masked(match, value->be32, mask->be32);
1759         break;
1760
1761     case MFF_IPV4_DST:
1762         match_set_nw_dst_masked(match, value->be32, mask->be32);
1763         break;
1764
1765     case MFF_IPV6_SRC:
1766         match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1767         break;
1768
1769     case MFF_IPV6_DST:
1770         match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1771         break;
1772
1773     case MFF_IPV6_LABEL:
1774         if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1775             mf_set_value(mf, value, match);
1776         } else {
1777             match_set_ipv6_label_masked(match, value->be32, mask->be32);
1778         }
1779         break;
1780
1781     case MFF_ND_TARGET:
1782         match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1783         break;
1784
1785     case MFF_IP_FRAG:
1786         match_set_nw_frag_masked(match, value->u8, mask->u8);
1787         break;
1788
1789     case MFF_ARP_SPA:
1790         match_set_nw_src_masked(match, value->be32, mask->be32);
1791         break;
1792
1793     case MFF_ARP_TPA:
1794         match_set_nw_dst_masked(match, value->be32, mask->be32);
1795         break;
1796
1797     case MFF_TCP_SRC:
1798     case MFF_UDP_SRC:
1799         match_set_tp_src_masked(match, value->be16, mask->be16);
1800         break;
1801
1802     case MFF_TCP_DST:
1803     case MFF_UDP_DST:
1804         match_set_tp_dst_masked(match, value->be16, mask->be16);
1805         break;
1806
1807     case MFF_N_IDS:
1808     default:
1809         NOT_REACHED();
1810     }
1811 }
1812
1813 static enum ofperr
1814 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1815            const char *type)
1816 {
1817     if (!sf->field) {
1818         VLOG_WARN_RL(&rl, "unknown %s field", type);
1819     } else if (!sf->n_bits) {
1820         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1821     } else if (sf->ofs >= sf->field->n_bits) {
1822         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1823                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1824     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1825         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1826                      "of %s field %s", sf->ofs, sf->n_bits,
1827                      sf->field->n_bits, type, sf->field->name);
1828     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1829         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1830                      type, sf->field->name);
1831     } else {
1832         return 0;
1833     }
1834
1835     return OFPERR_OFPBAC_BAD_ARGUMENT;
1836 }
1837
1838 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1839  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1840  * ofp_mkerr()).  */
1841 enum ofperr
1842 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1843 {
1844     return mf_check__(sf, flow, "source");
1845 }
1846
1847 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1848  * if so, otherwise an OpenFlow error code (e.g. as returned by
1849  * ofp_mkerr()). */
1850 enum ofperr
1851 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1852 {
1853     int error = mf_check__(sf, flow, "destination");
1854     if (!error && !sf->field->writable) {
1855         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1856                      sf->field->name);
1857         return OFPERR_OFPBAC_BAD_ARGUMENT;
1858     }
1859     return error;
1860 }
1861
1862 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1863  * 'value' and 'mask', respectively. */
1864 void
1865 mf_get(const struct mf_field *mf, const struct match *match,
1866        union mf_value *value, union mf_value *mask)
1867 {
1868     mf_get_value(mf, &match->flow, value);
1869     mf_get_mask(mf, &match->wc, mask);
1870 }
1871
1872 /* Assigns a random value for field 'mf' to 'value'. */
1873 void
1874 mf_random_value(const struct mf_field *mf, union mf_value *value)
1875 {
1876     random_bytes(value, mf->n_bytes);
1877
1878     switch (mf->id) {
1879     case MFF_TUN_ID:
1880     case MFF_TUN_SRC:
1881     case MFF_TUN_DST:
1882     case MFF_TUN_TOS:
1883     case MFF_TUN_TTL:
1884     case MFF_TUN_FLAGS:
1885     case MFF_METADATA:
1886     case MFF_IN_PORT:
1887     CASE_MFF_REGS:
1888     case MFF_ETH_SRC:
1889     case MFF_ETH_DST:
1890     case MFF_ETH_TYPE:
1891     case MFF_VLAN_TCI:
1892     case MFF_IPV4_SRC:
1893     case MFF_IPV4_DST:
1894     case MFF_IPV6_SRC:
1895     case MFF_IPV6_DST:
1896     case MFF_IP_PROTO:
1897     case MFF_IP_TTL:
1898     case MFF_ARP_SPA:
1899     case MFF_ARP_TPA:
1900     case MFF_ARP_SHA:
1901     case MFF_ARP_THA:
1902     case MFF_TCP_SRC:
1903     case MFF_TCP_DST:
1904     case MFF_UDP_SRC:
1905     case MFF_UDP_DST:
1906     case MFF_ICMPV4_TYPE:
1907     case MFF_ICMPV4_CODE:
1908     case MFF_ICMPV6_TYPE:
1909     case MFF_ICMPV6_CODE:
1910     case MFF_ND_TARGET:
1911     case MFF_ND_SLL:
1912     case MFF_ND_TLL:
1913         break;
1914
1915     case MFF_IPV6_LABEL:
1916         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1917         break;
1918
1919     case MFF_IP_DSCP:
1920         value->u8 &= IP_DSCP_MASK;
1921         break;
1922
1923     case MFF_IP_ECN:
1924         value->u8 &= IP_ECN_MASK;
1925         break;
1926
1927     case MFF_IP_FRAG:
1928         value->u8 &= FLOW_NW_FRAG_MASK;
1929         break;
1930
1931     case MFF_ARP_OP:
1932         value->be16 &= htons(0xff);
1933         break;
1934
1935     case MFF_DL_VLAN:
1936         value->be16 &= htons(VLAN_VID_MASK);
1937         break;
1938     case MFF_VLAN_VID:
1939         value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1940         break;
1941
1942     case MFF_DL_VLAN_PCP:
1943     case MFF_VLAN_PCP:
1944         value->u8 &= 0x07;
1945         break;
1946
1947     case MFF_N_IDS:
1948     default:
1949         NOT_REACHED();
1950     }
1951 }
1952
1953 static char *
1954 mf_from_integer_string(const struct mf_field *mf, const char *s,
1955                        uint8_t *valuep, uint8_t *maskp)
1956 {
1957     unsigned long long int integer, mask;
1958     char *tail;
1959     int i;
1960
1961     errno = 0;
1962     integer = strtoull(s, &tail, 0);
1963     if (errno || (*tail != '\0' && *tail != '/')) {
1964         goto syntax_error;
1965     }
1966
1967     if (*tail == '/') {
1968         mask = strtoull(tail + 1, &tail, 0);
1969         if (errno || *tail != '\0') {
1970             goto syntax_error;
1971         }
1972     } else {
1973         mask = ULLONG_MAX;
1974     }
1975
1976     for (i = mf->n_bytes - 1; i >= 0; i--) {
1977         valuep[i] = integer;
1978         maskp[i] = mask;
1979         integer >>= 8;
1980         mask >>= 8;
1981     }
1982     if (integer) {
1983         return xasprintf("%s: value too large for %u-byte field %s",
1984                          s, mf->n_bytes, mf->name);
1985     }
1986     return NULL;
1987
1988 syntax_error:
1989     return xasprintf("%s: bad syntax for %s", s, mf->name);
1990 }
1991
1992 static char *
1993 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1994                         uint8_t mac[ETH_ADDR_LEN],
1995                         uint8_t mask[ETH_ADDR_LEN])
1996 {
1997     assert(mf->n_bytes == ETH_ADDR_LEN);
1998
1999     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
2000                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
2001     case ETH_ADDR_SCAN_COUNT * 2:
2002         return NULL;
2003
2004     case ETH_ADDR_SCAN_COUNT:
2005         memset(mask, 0xff, ETH_ADDR_LEN);
2006         return NULL;
2007
2008     default:
2009         return xasprintf("%s: invalid Ethernet address", s);
2010     }
2011 }
2012
2013 static char *
2014 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2015                     ovs_be32 *ip, ovs_be32 *mask)
2016 {
2017     int prefix;
2018
2019     assert(mf->n_bytes == sizeof *ip);
2020
2021     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2022                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2023         /* OK. */
2024     } else if (sscanf(s, IP_SCAN_FMT"/%d",
2025                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2026         if (prefix <= 0 || prefix > 32) {
2027             return xasprintf("%s: network prefix bits not between 1 and "
2028                              "32", s);
2029         } else if (prefix == 32) {
2030             *mask = htonl(UINT32_MAX);
2031         } else {
2032             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2033         }
2034     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2035         *mask = htonl(UINT32_MAX);
2036     } else {
2037         return xasprintf("%s: invalid IP address", s);
2038     }
2039     return NULL;
2040 }
2041
2042 static char *
2043 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2044                     struct in6_addr *value, struct in6_addr *mask)
2045 {
2046     char *str = xstrdup(s);
2047     char *save_ptr = NULL;
2048     const char *name, *netmask;
2049     int retval;
2050
2051     assert(mf->n_bytes == sizeof *value);
2052
2053     name = strtok_r(str, "/", &save_ptr);
2054     retval = name ? lookup_ipv6(name, value) : EINVAL;
2055     if (retval) {
2056         char *err;
2057
2058         err = xasprintf("%s: could not convert to IPv6 address", str);
2059         free(str);
2060
2061         return err;
2062     }
2063
2064     netmask = strtok_r(NULL, "/", &save_ptr);
2065     if (netmask) {
2066         if (inet_pton(AF_INET6, netmask, mask) != 1) {
2067             int prefix = atoi(netmask);
2068             if (prefix <= 0 || prefix > 128) {
2069                 free(str);
2070                 return xasprintf("%s: prefix bits not between 1 and 128", s);
2071             } else {
2072                 *mask = ipv6_create_mask(prefix);
2073             }
2074         }
2075     } else {
2076         *mask = in6addr_exact;
2077     }
2078     free(str);
2079
2080     return NULL;
2081 }
2082
2083 static char *
2084 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2085                         ovs_be16 *valuep, ovs_be16 *maskp)
2086 {
2087     uint16_t port;
2088
2089     assert(mf->n_bytes == sizeof(ovs_be16));
2090     if (ofputil_port_from_string(s, &port)) {
2091         *valuep = htons(port);
2092         *maskp = htons(UINT16_MAX);
2093         return NULL;
2094     } else {
2095         return mf_from_integer_string(mf, s,
2096                                       (uint8_t *) valuep, (uint8_t *) maskp);
2097     }
2098 }
2099
2100 struct frag_handling {
2101     const char *name;
2102     uint8_t mask;
2103     uint8_t value;
2104 };
2105
2106 static const struct frag_handling all_frags[] = {
2107 #define A FLOW_NW_FRAG_ANY
2108 #define L FLOW_NW_FRAG_LATER
2109     /* name               mask  value */
2110
2111     { "no",               A|L,  0     },
2112     { "first",            A|L,  A     },
2113     { "later",            A|L,  A|L   },
2114
2115     { "no",               A,    0     },
2116     { "yes",              A,    A     },
2117
2118     { "not_later",        L,    0     },
2119     { "later",            L,    L     },
2120 #undef A
2121 #undef L
2122 };
2123
2124 static char *
2125 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2126 {
2127     const struct frag_handling *h;
2128
2129     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2130         if (!strcasecmp(s, h->name)) {
2131             /* We force the upper bits of the mask on to make mf_parse_value()
2132              * happy (otherwise it will never think it's an exact match.) */
2133             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2134             *valuep = h->value;
2135             return NULL;
2136         }
2137     }
2138
2139     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2140                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2141 }
2142
2143 static int
2144 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2145                      ovs_be16 *res)
2146 {
2147     uint32_t result = 0;
2148     char *save_ptr = NULL;
2149     char *name;
2150     int rc = 0;
2151     char *s = xstrdup(s_);
2152
2153     for (name = strtok_r((char *)s, " |", &save_ptr); name;
2154          name = strtok_r(NULL, " |", &save_ptr)) {
2155         int name_len;
2156         unsigned long long int flags;
2157         uint32_t bit;
2158         int n0;
2159
2160         if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2161             result |= flags;
2162             continue;
2163         }
2164         name_len = strlen(name);
2165         for (bit = 1; bit; bit <<= 1) {
2166             const char *fname = bit_to_string(bit);
2167             size_t len;
2168
2169             if (!fname) {
2170                 continue;
2171             }
2172
2173             len = strlen(fname);
2174             if (len != name_len) {
2175                 continue;
2176             }
2177             if (!strncmp(name, fname, len)) {
2178                 result |= bit;
2179                 break;
2180             }
2181         }
2182
2183         if (!bit) {
2184             rc = -ENOENT;
2185             goto out;
2186         }
2187     }
2188
2189     *res = htons(result);
2190 out:
2191     free(s);
2192     return rc;
2193 }
2194
2195 static char *
2196 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep)
2197 {
2198     if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2199         return NULL;
2200     }
2201
2202     return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2203                      "\"csum\", \"key\"", s);
2204 }
2205
2206 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2207  * NULL if successful, otherwise a malloc()'d string describing the error. */
2208 char *
2209 mf_parse(const struct mf_field *mf, const char *s,
2210          union mf_value *value, union mf_value *mask)
2211 {
2212     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2213         memset(value, 0, mf->n_bytes);
2214         memset(mask, 0, mf->n_bytes);
2215         return NULL;
2216     }
2217
2218     switch (mf->string) {
2219     case MFS_DECIMAL:
2220     case MFS_HEXADECIMAL:
2221         return mf_from_integer_string(mf, s,
2222                                       (uint8_t *) value, (uint8_t *) mask);
2223
2224     case MFS_ETHERNET:
2225         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2226
2227     case MFS_IPV4:
2228         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2229
2230     case MFS_IPV6:
2231         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2232
2233     case MFS_OFP_PORT:
2234         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2235
2236     case MFS_FRAG:
2237         return mf_from_frag_string(s, &value->u8, &mask->u8);
2238
2239     case MFS_TNL_FLAGS:
2240         return mf_from_tun_flags_string(s, &value->be16);
2241     }
2242     NOT_REACHED();
2243 }
2244
2245 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2246  * successful, otherwise a malloc()'d string describing the error. */
2247 char *
2248 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2249 {
2250     union mf_value mask;
2251     char *error;
2252
2253     error = mf_parse(mf, s, value, &mask);
2254     if (error) {
2255         return error;
2256     }
2257
2258     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2259         return xasprintf("%s: wildcards not allowed here", s);
2260     }
2261     return NULL;
2262 }
2263
2264 static void
2265 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2266                          const uint8_t *maskp, struct ds *s)
2267 {
2268     unsigned long long int integer;
2269     int i;
2270
2271     assert(mf->n_bytes <= 8);
2272
2273     integer = 0;
2274     for (i = 0; i < mf->n_bytes; i++) {
2275         integer = (integer << 8) | valuep[i];
2276     }
2277     if (mf->string == MFS_HEXADECIMAL) {
2278         ds_put_format(s, "%#llx", integer);
2279     } else {
2280         ds_put_format(s, "%lld", integer);
2281     }
2282
2283     if (maskp) {
2284         unsigned long long int mask;
2285
2286         mask = 0;
2287         for (i = 0; i < mf->n_bytes; i++) {
2288             mask = (mask << 8) | maskp[i];
2289         }
2290
2291         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2292          * not sure that that a bit-mask written in decimal is ever easier to
2293          * understand than the same bit-mask written in hexadecimal. */
2294         ds_put_format(s, "/%#llx", mask);
2295     }
2296 }
2297
2298 static void
2299 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2300                       struct ds *s)
2301 {
2302     const struct frag_handling *h;
2303     uint8_t value = *valuep;
2304     uint8_t mask = *maskp;
2305
2306     value &= mask;
2307     mask &= FLOW_NW_FRAG_MASK;
2308
2309     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2310         if (value == h->value && mask == h->mask) {
2311             ds_put_cstr(s, h->name);
2312             return;
2313         }
2314     }
2315     ds_put_cstr(s, "<error>");
2316 }
2317
2318 static void
2319 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2320 {
2321     format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2322 }
2323
2324 /* Appends to 's' a string representation of field 'mf' whose value is in
2325  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2326 void
2327 mf_format(const struct mf_field *mf,
2328           const union mf_value *value, const union mf_value *mask,
2329           struct ds *s)
2330 {
2331     if (mask) {
2332         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2333             ds_put_cstr(s, "ANY");
2334             return;
2335         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2336             mask = NULL;
2337         }
2338     }
2339
2340     switch (mf->string) {
2341     case MFS_OFP_PORT:
2342         if (!mask) {
2343             ofputil_format_port(ntohs(value->be16), s);
2344             break;
2345         }
2346         /* fall through */
2347     case MFS_DECIMAL:
2348     case MFS_HEXADECIMAL:
2349         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2350         break;
2351
2352     case MFS_ETHERNET:
2353         eth_format_masked(value->mac, mask->mac, s);
2354         break;
2355
2356     case MFS_IPV4:
2357         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2358                          s);
2359         break;
2360
2361     case MFS_IPV6:
2362         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2363         break;
2364
2365     case MFS_FRAG:
2366         mf_format_frag_string(&value->u8, &mask->u8, s);
2367         break;
2368
2369     case MFS_TNL_FLAGS:
2370         mf_format_tnl_flags_string(&value->be16, s);
2371         break;
2372
2373     default:
2374         NOT_REACHED();
2375     }
2376 }
2377 \f
2378 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2379  * least-significant bits in 'x'.
2380  */
2381 void
2382 mf_write_subfield_flow(const struct mf_subfield *sf,
2383                        const union mf_subvalue *x, struct flow *flow)
2384 {
2385     const struct mf_field *field = sf->field;
2386     union mf_value value;
2387
2388     mf_get_value(field, flow, &value);
2389     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2390                  sf->ofs, sf->n_bits);
2391     mf_set_flow_value(field, &value, flow);
2392 }
2393
2394 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2395  * least-significant bits in 'x'.
2396  */
2397 void
2398 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2399                   struct match *match)
2400 {
2401     const struct mf_field *field = sf->field;
2402     union mf_value value, mask;
2403
2404     mf_get(field, match, &value, &mask);
2405     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2406     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2407     mf_set(field, &value, &mask, match);
2408 }
2409
2410 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2411  * reading 'flow', e.g. as checked by mf_check_src(). */
2412 void
2413 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2414                  union mf_subvalue *x)
2415 {
2416     union mf_value value;
2417
2418     mf_get_value(sf->field, flow, &value);
2419
2420     memset(x, 0, sizeof *x);
2421     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2422                  x, sizeof *x, 0,
2423                  sf->n_bits);
2424 }
2425
2426 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2427  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2428  * less. */
2429 uint64_t
2430 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2431 {
2432     union mf_value value;
2433
2434     mf_get_value(sf->field, flow, &value);
2435     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2436 }
2437
2438 /* Formats 'sf' into 's' in a format normally acceptable to
2439  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2440  * sf->field has no NXM name.) */
2441 void
2442 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2443 {
2444     if (!sf->field) {
2445         ds_put_cstr(s, "<unknown>");
2446     } else if (sf->field->nxm_name) {
2447         ds_put_cstr(s, sf->field->nxm_name);
2448     } else if (sf->field->nxm_header) {
2449         uint32_t header = sf->field->nxm_header;
2450         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2451     } else {
2452         ds_put_cstr(s, sf->field->name);
2453     }
2454
2455     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2456         ds_put_cstr(s, "[]");
2457     } else if (sf->n_bits == 1) {
2458         ds_put_format(s, "[%d]", sf->ofs);
2459     } else {
2460         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2461     }
2462 }
2463
2464 static const struct mf_field *
2465 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2466 {
2467     int i;
2468
2469     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2470     if (*wild) {
2471         name_len -= 2;
2472     }
2473
2474     for (i = 0; i < MFF_N_IDS; i++) {
2475         const struct mf_field *mf = mf_from_id(i);
2476
2477         if (mf->nxm_name
2478             && !strncmp(mf->nxm_name, name, name_len)
2479             && mf->nxm_name[name_len] == '\0') {
2480             return mf;
2481         }
2482         if (mf->oxm_name
2483             && !strncmp(mf->oxm_name, name, name_len)
2484             && mf->oxm_name[name_len] == '\0') {
2485             return mf;
2486         }
2487     }
2488
2489     return NULL;
2490 }
2491
2492 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2493  * returns NULL and advances '*sp' to the first byte following the parsed
2494  * string.  On failure, returns a malloc()'d error message, does not modify
2495  * '*sp', and does not properly initialize 'sf'.
2496  *
2497  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2498  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2499  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2500  * may both be omitted (the [] are still required) to indicate an entire
2501  * field. */
2502 char *
2503 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2504 {
2505     const struct mf_field *field;
2506     const char *name;
2507     int start, end;
2508     const char *s;
2509     int name_len;
2510     bool wild;
2511
2512     s = *sp;
2513     name = s;
2514     name_len = strcspn(s, "[");
2515     if (s[name_len] != '[') {
2516         return xasprintf("%s: missing [ looking for field name", *sp);
2517     }
2518
2519     field = mf_parse_subfield_name(name, name_len, &wild);
2520     if (!field) {
2521         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2522     }
2523
2524     s += name_len;
2525     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2526         /* Nothing to do. */
2527     } else if (sscanf(s, "[%d]", &start) == 1) {
2528         end = start;
2529     } else if (!strncmp(s, "[]", 2)) {
2530         start = 0;
2531         end = field->n_bits - 1;
2532     } else {
2533         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2534                          "[<start>..<end>]", *sp);
2535     }
2536     s = strchr(s, ']') + 1;
2537
2538     if (start > end) {
2539         return xasprintf("%s: starting bit %d is after ending bit %d",
2540                          *sp, start, end);
2541     } else if (start >= field->n_bits) {
2542         return xasprintf("%s: starting bit %d is not valid because field is "
2543                          "only %d bits wide", *sp, start, field->n_bits);
2544     } else if (end >= field->n_bits){
2545         return xasprintf("%s: ending bit %d is not valid because field is "
2546                          "only %d bits wide", *sp, end, field->n_bits);
2547     }
2548
2549     sf->field = field;
2550     sf->ofs = start;
2551     sf->n_bits = end - start + 1;
2552
2553     *sp = s;
2554     return NULL;
2555 }
2556
2557 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2558  * byte in 's' following the parsed string.
2559  *
2560  * Exits with an error message if 's' has incorrect syntax.
2561  *
2562  * The syntax parsed from 's' takes the form "header[start..end]" where
2563  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2564  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2565  * may both be omitted (the [] are still required) to indicate an entire
2566  * field.  */
2567 const char *
2568 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2569 {
2570     char *msg = mf_parse_subfield__(sf, &s);
2571     if (msg) {
2572         ovs_fatal(0, "%s", msg);
2573     }
2574     return s;
2575 }
2576
2577 void
2578 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2579 {
2580     int i;
2581
2582     for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2583         if (subvalue->u8[i]) {
2584             ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2585             for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2586                 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2587             }
2588             return;
2589         }
2590     }
2591     ds_put_char(s, '0');
2592 }