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