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