lib/meta-flow: introduce a macro, CASE_MFF_REGS, to catch "case MFF_REG<N>:"
[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     CASE_MFF_REGS:
579         return !wc->reg_masks[mf->id - MFF_REG0];
580
581     case MFF_ETH_SRC:
582         return eth_addr_is_zero(wc->dl_src_mask);
583     case MFF_ETH_DST:
584         return eth_addr_is_zero(wc->dl_dst_mask);
585
586     case MFF_VLAN_TCI:
587         return !wc->vlan_tci_mask;
588     case MFF_VLAN_VID:
589         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
590     case MFF_VLAN_PCP:
591         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
592
593     case MFF_IPV4_SRC:
594         return !wc->nw_src_mask;
595     case MFF_IPV4_DST:
596         return !wc->nw_dst_mask;
597
598     case MFF_IPV6_SRC:
599         return ipv6_mask_is_any(&wc->ipv6_src_mask);
600     case MFF_IPV6_DST:
601         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
602
603     case MFF_ND_TARGET:
604         return ipv6_mask_is_any(&wc->nd_target_mask);
605
606     case MFF_IP_FRAG:
607         return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
608
609     case MFF_ARP_SPA:
610         return !wc->nw_src_mask;
611     case MFF_ARP_TPA:
612         return !wc->nw_dst_mask;
613
614     case MFF_TCP_SRC:
615     case MFF_UDP_SRC:
616     case MFF_ICMPV4_TYPE:
617     case MFF_ICMPV6_TYPE:
618         return !wc->tp_src_mask;
619     case MFF_TCP_DST:
620     case MFF_UDP_DST:
621     case MFF_ICMPV4_CODE:
622     case MFF_ICMPV6_CODE:
623         return !wc->tp_dst_mask;
624
625     case MFF_N_IDS:
626     default:
627         NOT_REACHED();
628     }
629 }
630
631 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
632  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
633  * purposes, or to 0 if it is wildcarded.
634  *
635  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
636  * meets 'mf''s prerequisites. */
637 void
638 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
639             union mf_value *mask)
640 {
641     switch (mf->id) {
642     case MFF_IN_PORT:
643     case MFF_ETH_TYPE:
644     case MFF_IP_PROTO:
645     case MFF_IP_DSCP:
646     case MFF_IP_ECN:
647     case MFF_IP_TTL:
648     case MFF_IPV6_LABEL:
649     case MFF_ARP_OP:
650     case MFF_ARP_SHA:
651     case MFF_ARP_THA:
652     case MFF_ND_SLL:
653     case MFF_ND_TLL:
654         assert(mf->fww_bit != 0);
655         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
656         break;
657
658     case MFF_TUN_ID:
659         mask->be64 = wc->tun_id_mask;
660         break;
661     case MFF_METADATA:
662         mask->be64 = wc->metadata_mask;
663         break;
664
665     CASE_MFF_REGS:
666         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
667         break;
668
669     case MFF_ETH_DST:
670         memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
671         break;
672
673     case MFF_ETH_SRC:
674         memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN);
675         break;
676
677     case MFF_VLAN_TCI:
678         mask->be16 = wc->vlan_tci_mask;
679         break;
680     case MFF_VLAN_VID:
681         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
682         break;
683     case MFF_VLAN_PCP:
684         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
685         break;
686
687     case MFF_IPV4_SRC:
688         mask->be32 = wc->nw_src_mask;
689         break;
690     case MFF_IPV4_DST:
691         mask->be32 = wc->nw_dst_mask;
692         break;
693
694     case MFF_IPV6_SRC:
695         mask->ipv6 = wc->ipv6_src_mask;
696         break;
697     case MFF_IPV6_DST:
698         mask->ipv6 = wc->ipv6_dst_mask;
699         break;
700
701     case MFF_ND_TARGET:
702         mask->ipv6 = wc->nd_target_mask;
703         break;
704
705     case MFF_IP_FRAG:
706         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
707         break;
708
709     case MFF_ARP_SPA:
710         mask->be32 = wc->nw_src_mask;
711         break;
712     case MFF_ARP_TPA:
713         mask->be32 = wc->nw_dst_mask;
714         break;
715
716     case MFF_TCP_SRC:
717     case MFF_UDP_SRC:
718         mask->be16 = wc->tp_src_mask;
719         break;
720     case MFF_TCP_DST:
721     case MFF_UDP_DST:
722         mask->be16 = wc->tp_dst_mask;
723         break;
724
725     case MFF_ICMPV4_TYPE:
726     case MFF_ICMPV6_TYPE:
727         mask->u8 = ntohs(wc->tp_src_mask);
728         break;
729     case MFF_ICMPV4_CODE:
730     case MFF_ICMPV6_CODE:
731         mask->u8 = ntohs(wc->tp_dst_mask);
732         break;
733
734     case MFF_N_IDS:
735     default:
736         NOT_REACHED();
737     }
738 }
739
740 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
741  * if the mask is valid, false otherwise. */
742 bool
743 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
744 {
745     switch (mf->maskable) {
746     case MFM_NONE:
747         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
748                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
749
750     case MFM_FULLY:
751         return true;
752     }
753
754     NOT_REACHED();
755 }
756
757 static bool
758 is_ip_any(const struct flow *flow)
759 {
760     return (flow->dl_type == htons(ETH_TYPE_IP) ||
761             flow->dl_type == htons(ETH_TYPE_IPV6));
762 }
763
764 static bool
765 is_icmpv4(const struct flow *flow)
766 {
767     return (flow->dl_type == htons(ETH_TYPE_IP)
768             && flow->nw_proto == IPPROTO_ICMP);
769 }
770
771 static bool
772 is_icmpv6(const struct flow *flow)
773 {
774     return (flow->dl_type == htons(ETH_TYPE_IPV6)
775             && flow->nw_proto == IPPROTO_ICMPV6);
776 }
777
778 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
779 bool
780 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
781 {
782     switch (mf->prereqs) {
783     case MFP_NONE:
784         return true;
785
786     case MFP_ARP:
787         return flow->dl_type == htons(ETH_TYPE_ARP);
788     case MFP_IPV4:
789         return flow->dl_type == htons(ETH_TYPE_IP);
790     case MFP_IPV6:
791         return flow->dl_type == htons(ETH_TYPE_IPV6);
792     case MFP_IP_ANY:
793         return is_ip_any(flow);
794
795     case MFP_TCP:
796         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
797     case MFP_UDP:
798         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
799     case MFP_ICMPV4:
800         return is_icmpv4(flow);
801     case MFP_ICMPV6:
802         return is_icmpv6(flow);
803
804     case MFP_ND:
805         return (is_icmpv6(flow)
806                 && flow->tp_dst == htons(0)
807                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
808                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
809     case MFP_ND_SOLICIT:
810         return (is_icmpv6(flow)
811                 && flow->tp_dst == htons(0)
812                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
813     case MFP_ND_ADVERT:
814         return (is_icmpv6(flow)
815                 && flow->tp_dst == htons(0)
816                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
817     }
818
819     NOT_REACHED();
820 }
821
822 /* Returns true if 'value' may be a valid value *as part of a masked match*,
823  * false otherwise.
824  *
825  * A value is not rejected just because it is not valid for the field in
826  * question, but only if it doesn't make sense to test the bits in question at
827  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
828  * without the VLAN_CFI bit being set, but we can't reject those values because
829  * it is still legitimate to test just for those bits (see the documentation
830  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
831  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
832 bool
833 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
834 {
835     switch (mf->id) {
836     case MFF_TUN_ID:
837     case MFF_METADATA:
838     case MFF_IN_PORT:
839     CASE_MFF_REGS:
840     case MFF_ETH_SRC:
841     case MFF_ETH_DST:
842     case MFF_ETH_TYPE:
843     case MFF_VLAN_TCI:
844     case MFF_IPV4_SRC:
845     case MFF_IPV4_DST:
846     case MFF_IPV6_SRC:
847     case MFF_IPV6_DST:
848     case MFF_IP_PROTO:
849     case MFF_IP_TTL:
850     case MFF_ARP_SPA:
851     case MFF_ARP_TPA:
852     case MFF_ARP_SHA:
853     case MFF_ARP_THA:
854     case MFF_TCP_SRC:
855     case MFF_TCP_DST:
856     case MFF_UDP_SRC:
857     case MFF_UDP_DST:
858     case MFF_ICMPV4_TYPE:
859     case MFF_ICMPV4_CODE:
860     case MFF_ICMPV6_TYPE:
861     case MFF_ICMPV6_CODE:
862     case MFF_ND_TARGET:
863     case MFF_ND_SLL:
864     case MFF_ND_TLL:
865         return true;
866
867     case MFF_IP_DSCP:
868         return !(value->u8 & ~IP_DSCP_MASK);
869     case MFF_IP_ECN:
870         return !(value->u8 & ~IP_ECN_MASK);
871     case MFF_IP_FRAG:
872         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
873
874     case MFF_ARP_OP:
875         return !(value->be16 & htons(0xff00));
876
877     case MFF_VLAN_VID:
878         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
879
880     case MFF_VLAN_PCP:
881         return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
882
883     case MFF_IPV6_LABEL:
884         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
885
886     case MFF_N_IDS:
887     default:
888         NOT_REACHED();
889     }
890 }
891
892 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
893  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
894 void
895 mf_get_value(const struct mf_field *mf, const struct flow *flow,
896              union mf_value *value)
897 {
898     switch (mf->id) {
899     case MFF_TUN_ID:
900         value->be64 = flow->tun_id;
901         break;
902     case MFF_METADATA:
903         value->be64 = flow->metadata;
904         break;
905
906     case MFF_IN_PORT:
907         value->be16 = htons(flow->in_port);
908         break;
909
910     CASE_MFF_REGS:
911         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
912         break;
913
914     case MFF_ETH_SRC:
915         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
916         break;
917
918     case MFF_ETH_DST:
919         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
920         break;
921
922     case MFF_ETH_TYPE:
923         value->be16 = flow->dl_type;
924         break;
925
926     case MFF_VLAN_TCI:
927         value->be16 = flow->vlan_tci;
928         break;
929
930     case MFF_VLAN_VID:
931         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
932         break;
933
934     case MFF_VLAN_PCP:
935         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
936         break;
937
938     case MFF_IPV4_SRC:
939         value->be32 = flow->nw_src;
940         break;
941
942     case MFF_IPV4_DST:
943         value->be32 = flow->nw_dst;
944         break;
945
946     case MFF_IPV6_SRC:
947         value->ipv6 = flow->ipv6_src;
948         break;
949
950     case MFF_IPV6_DST:
951         value->ipv6 = flow->ipv6_dst;
952         break;
953
954     case MFF_IPV6_LABEL:
955         value->be32 = flow->ipv6_label;
956         break;
957
958     case MFF_IP_PROTO:
959         value->u8 = flow->nw_proto;
960         break;
961
962     case MFF_IP_DSCP:
963         value->u8 = flow->nw_tos & IP_DSCP_MASK;
964         break;
965
966     case MFF_IP_ECN:
967         value->u8 = flow->nw_tos & IP_ECN_MASK;
968         break;
969
970     case MFF_IP_TTL:
971         value->u8 = flow->nw_ttl;
972         break;
973
974     case MFF_IP_FRAG:
975         value->u8 = flow->nw_frag;
976         break;
977
978     case MFF_ARP_OP:
979         value->be16 = htons(flow->nw_proto);
980         break;
981
982     case MFF_ARP_SPA:
983         value->be32 = flow->nw_src;
984         break;
985
986     case MFF_ARP_TPA:
987         value->be32 = flow->nw_dst;
988         break;
989
990     case MFF_ARP_SHA:
991     case MFF_ND_SLL:
992         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
993         break;
994
995     case MFF_ARP_THA:
996     case MFF_ND_TLL:
997         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
998         break;
999
1000     case MFF_TCP_SRC:
1001         value->be16 = flow->tp_src;
1002         break;
1003
1004     case MFF_TCP_DST:
1005         value->be16 = flow->tp_dst;
1006         break;
1007
1008     case MFF_UDP_SRC:
1009         value->be16 = flow->tp_src;
1010         break;
1011
1012     case MFF_UDP_DST:
1013         value->be16 = flow->tp_dst;
1014         break;
1015
1016     case MFF_ICMPV4_TYPE:
1017     case MFF_ICMPV6_TYPE:
1018         value->u8 = ntohs(flow->tp_src);
1019         break;
1020
1021     case MFF_ICMPV4_CODE:
1022     case MFF_ICMPV6_CODE:
1023         value->u8 = ntohs(flow->tp_dst);
1024         break;
1025
1026     case MFF_ND_TARGET:
1027         value->ipv6 = flow->nd_target;
1028         break;
1029
1030     case MFF_N_IDS:
1031     default:
1032         NOT_REACHED();
1033     }
1034 }
1035
1036 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1037  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1038  * prerequisites. */
1039 void
1040 mf_set_value(const struct mf_field *mf,
1041              const union mf_value *value, struct cls_rule *rule)
1042 {
1043     switch (mf->id) {
1044     case MFF_TUN_ID:
1045         cls_rule_set_tun_id(rule, value->be64);
1046         break;
1047     case MFF_METADATA:
1048         cls_rule_set_metadata(rule, value->be64);
1049         break;
1050
1051     case MFF_IN_PORT:
1052         cls_rule_set_in_port(rule, ntohs(value->be16));
1053         break;
1054
1055     CASE_MFF_REGS:
1056         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1057         break;
1058
1059     case MFF_ETH_SRC:
1060         cls_rule_set_dl_src(rule, value->mac);
1061         break;
1062
1063     case MFF_ETH_DST:
1064         cls_rule_set_dl_dst(rule, value->mac);
1065         break;
1066
1067     case MFF_ETH_TYPE:
1068         cls_rule_set_dl_type(rule, value->be16);
1069         break;
1070
1071     case MFF_VLAN_TCI:
1072         cls_rule_set_dl_tci(rule, value->be16);
1073         break;
1074
1075     case MFF_VLAN_VID:
1076         cls_rule_set_dl_vlan(rule, value->be16);
1077         break;
1078
1079     case MFF_VLAN_PCP:
1080         cls_rule_set_dl_vlan_pcp(rule, value->u8);
1081         break;
1082
1083     case MFF_IPV4_SRC:
1084         cls_rule_set_nw_src(rule, value->be32);
1085         break;
1086
1087     case MFF_IPV4_DST:
1088         cls_rule_set_nw_dst(rule, value->be32);
1089         break;
1090
1091     case MFF_IPV6_SRC:
1092         cls_rule_set_ipv6_src(rule, &value->ipv6);
1093         break;
1094
1095     case MFF_IPV6_DST:
1096         cls_rule_set_ipv6_dst(rule, &value->ipv6);
1097         break;
1098
1099     case MFF_IPV6_LABEL:
1100         cls_rule_set_ipv6_label(rule, value->be32);
1101         break;
1102
1103     case MFF_IP_PROTO:
1104         cls_rule_set_nw_proto(rule, value->u8);
1105         break;
1106
1107     case MFF_IP_DSCP:
1108         cls_rule_set_nw_dscp(rule, value->u8);
1109         break;
1110
1111     case MFF_IP_ECN:
1112         cls_rule_set_nw_ecn(rule, value->u8);
1113         break;
1114
1115     case MFF_IP_TTL:
1116         cls_rule_set_nw_ttl(rule, value->u8);
1117         break;
1118
1119     case MFF_IP_FRAG:
1120         cls_rule_set_nw_frag(rule, value->u8);
1121         break;
1122
1123     case MFF_ARP_OP:
1124         cls_rule_set_nw_proto(rule, ntohs(value->be16));
1125         break;
1126
1127     case MFF_ARP_SPA:
1128         cls_rule_set_nw_src(rule, value->be32);
1129         break;
1130
1131     case MFF_ARP_TPA:
1132         cls_rule_set_nw_dst(rule, value->be32);
1133         break;
1134
1135     case MFF_ARP_SHA:
1136     case MFF_ND_SLL:
1137         cls_rule_set_arp_sha(rule, value->mac);
1138         break;
1139
1140     case MFF_ARP_THA:
1141     case MFF_ND_TLL:
1142         cls_rule_set_arp_tha(rule, value->mac);
1143         break;
1144
1145     case MFF_TCP_SRC:
1146         cls_rule_set_tp_src(rule, value->be16);
1147         break;
1148
1149     case MFF_TCP_DST:
1150         cls_rule_set_tp_dst(rule, value->be16);
1151         break;
1152
1153     case MFF_UDP_SRC:
1154         cls_rule_set_tp_src(rule, value->be16);
1155         break;
1156
1157     case MFF_UDP_DST:
1158         cls_rule_set_tp_dst(rule, value->be16);
1159         break;
1160
1161     case MFF_ICMPV4_TYPE:
1162     case MFF_ICMPV6_TYPE:
1163         cls_rule_set_icmp_type(rule, value->u8);
1164         break;
1165
1166     case MFF_ICMPV4_CODE:
1167     case MFF_ICMPV6_CODE:
1168         cls_rule_set_icmp_code(rule, value->u8);
1169         break;
1170
1171     case MFF_ND_TARGET:
1172         cls_rule_set_nd_target(rule, &value->ipv6);
1173         break;
1174
1175     case MFF_N_IDS:
1176     default:
1177         NOT_REACHED();
1178     }
1179 }
1180
1181 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1182  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1183  * prerequisites. */
1184 void
1185 mf_set_flow_value(const struct mf_field *mf,
1186                   const union mf_value *value, struct flow *flow)
1187 {
1188     switch (mf->id) {
1189     case MFF_TUN_ID:
1190         flow->tun_id = value->be64;
1191         break;
1192     case MFF_METADATA:
1193         flow->metadata = value->be64;
1194         break;
1195
1196     case MFF_IN_PORT:
1197         flow->in_port = ntohs(value->be16);
1198         break;
1199
1200     CASE_MFF_REGS:
1201         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1202         break;
1203
1204     case MFF_ETH_SRC:
1205         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1206         break;
1207
1208     case MFF_ETH_DST:
1209         memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1210         break;
1211
1212     case MFF_ETH_TYPE:
1213         flow->dl_type = value->be16;
1214         break;
1215
1216     case MFF_VLAN_TCI:
1217         flow->vlan_tci = value->be16;
1218         break;
1219
1220     case MFF_VLAN_VID:
1221         flow_set_vlan_vid(flow, value->be16);
1222         break;
1223
1224     case MFF_VLAN_PCP:
1225         flow_set_vlan_pcp(flow, value->u8);
1226         break;
1227
1228     case MFF_IPV4_SRC:
1229         flow->nw_src = value->be32;
1230         break;
1231
1232     case MFF_IPV4_DST:
1233         flow->nw_dst = value->be32;
1234         break;
1235
1236     case MFF_IPV6_SRC:
1237         flow->ipv6_src = value->ipv6;
1238         break;
1239
1240     case MFF_IPV6_DST:
1241         flow->ipv6_dst = value->ipv6;
1242         break;
1243
1244     case MFF_IPV6_LABEL:
1245         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1246         break;
1247
1248     case MFF_IP_PROTO:
1249         flow->nw_proto = value->u8;
1250         break;
1251
1252     case MFF_IP_DSCP:
1253         flow->nw_tos &= ~IP_DSCP_MASK;
1254         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1255         break;
1256
1257     case MFF_IP_ECN:
1258         flow->nw_tos &= ~IP_ECN_MASK;
1259         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1260         break;
1261
1262     case MFF_IP_TTL:
1263         flow->nw_ttl = value->u8;
1264         break;
1265
1266     case MFF_IP_FRAG:
1267         flow->nw_frag &= value->u8;
1268         break;
1269
1270     case MFF_ARP_OP:
1271         flow->nw_proto = ntohs(value->be16);
1272         break;
1273
1274     case MFF_ARP_SPA:
1275         flow->nw_src = value->be32;
1276         break;
1277
1278     case MFF_ARP_TPA:
1279         flow->nw_dst = value->be32;
1280         break;
1281
1282     case MFF_ARP_SHA:
1283     case MFF_ND_SLL:
1284         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1285         break;
1286
1287     case MFF_ARP_THA:
1288     case MFF_ND_TLL:
1289         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1290         break;
1291
1292     case MFF_TCP_SRC:
1293     case MFF_UDP_SRC:
1294         flow->tp_src = value->be16;
1295         break;
1296
1297     case MFF_TCP_DST:
1298     case MFF_UDP_DST:
1299         flow->tp_dst = value->be16;
1300         break;
1301
1302     case MFF_ICMPV4_TYPE:
1303     case MFF_ICMPV6_TYPE:
1304         flow->tp_src = htons(value->u8);
1305         break;
1306
1307     case MFF_ICMPV4_CODE:
1308     case MFF_ICMPV6_CODE:
1309         flow->tp_dst = htons(value->u8);
1310         break;
1311
1312     case MFF_ND_TARGET:
1313         flow->nd_target = value->ipv6;
1314         break;
1315
1316     case MFF_N_IDS:
1317     default:
1318         NOT_REACHED();
1319     }
1320 }
1321
1322 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1323  *
1324  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1325  * prerequisites. */
1326 bool
1327 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1328 {
1329     union mf_value value;
1330
1331     mf_get_value(mf, flow, &value);
1332     return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1333 }
1334
1335 /* Makes 'rule' wildcard field 'mf'.
1336  *
1337  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1338  * prerequisites. */
1339 void
1340 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1341 {
1342     switch (mf->id) {
1343     case MFF_TUN_ID:
1344         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1345         break;
1346     case MFF_METADATA:
1347         cls_rule_set_metadata_masked(rule, htonll(0), htonll(0));
1348
1349     case MFF_IN_PORT:
1350         rule->wc.wildcards |= FWW_IN_PORT;
1351         rule->flow.in_port = 0;
1352         break;
1353
1354     CASE_MFF_REGS:
1355         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0);
1356         break;
1357
1358     case MFF_ETH_SRC:
1359         memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1360         memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1361         break;
1362
1363     case MFF_ETH_DST:
1364         memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1365         memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1366         break;
1367
1368     case MFF_ETH_TYPE:
1369         rule->wc.wildcards |= FWW_DL_TYPE;
1370         rule->flow.dl_type = htons(0);
1371         break;
1372
1373     case MFF_VLAN_TCI:
1374         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1375         break;
1376
1377     case MFF_VLAN_VID:
1378         cls_rule_set_any_vid(rule);
1379         break;
1380
1381     case MFF_VLAN_PCP:
1382         cls_rule_set_any_pcp(rule);
1383         break;
1384
1385     case MFF_IPV4_SRC:
1386     case MFF_ARP_SPA:
1387         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1388         break;
1389
1390     case MFF_IPV4_DST:
1391     case MFF_ARP_TPA:
1392         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1393         break;
1394
1395     case MFF_IPV6_SRC:
1396         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1397         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1398         break;
1399
1400     case MFF_IPV6_DST:
1401         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1402         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1403         break;
1404
1405     case MFF_IPV6_LABEL:
1406         rule->wc.wildcards |= FWW_IPV6_LABEL;
1407         rule->flow.ipv6_label = 0;
1408         break;
1409
1410     case MFF_IP_PROTO:
1411         rule->wc.wildcards |= FWW_NW_PROTO;
1412         rule->flow.nw_proto = 0;
1413         break;
1414
1415     case MFF_IP_DSCP:
1416         rule->wc.wildcards |= FWW_NW_DSCP;
1417         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1418         break;
1419
1420     case MFF_IP_ECN:
1421         rule->wc.wildcards |= FWW_NW_ECN;
1422         rule->flow.nw_tos &= ~IP_ECN_MASK;
1423         break;
1424
1425     case MFF_IP_TTL:
1426         rule->wc.wildcards |= FWW_NW_TTL;
1427         rule->flow.nw_ttl = 0;
1428         break;
1429
1430     case MFF_IP_FRAG:
1431         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1432         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1433         break;
1434
1435     case MFF_ARP_OP:
1436         rule->wc.wildcards |= FWW_NW_PROTO;
1437         rule->flow.nw_proto = 0;
1438         break;
1439
1440     case MFF_ARP_SHA:
1441     case MFF_ND_SLL:
1442         rule->wc.wildcards |= FWW_ARP_SHA;
1443         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1444         break;
1445
1446     case MFF_ARP_THA:
1447     case MFF_ND_TLL:
1448         rule->wc.wildcards |= FWW_ARP_THA;
1449         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1450         break;
1451
1452     case MFF_TCP_SRC:
1453     case MFF_UDP_SRC:
1454     case MFF_ICMPV4_TYPE:
1455     case MFF_ICMPV6_TYPE:
1456         rule->wc.tp_src_mask = htons(0);
1457         rule->flow.tp_src = htons(0);
1458         break;
1459
1460     case MFF_TCP_DST:
1461     case MFF_UDP_DST:
1462     case MFF_ICMPV4_CODE:
1463     case MFF_ICMPV6_CODE:
1464         rule->wc.tp_dst_mask = htons(0);
1465         rule->flow.tp_dst = htons(0);
1466         break;
1467
1468     case MFF_ND_TARGET:
1469         memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1470         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1471         break;
1472
1473     case MFF_N_IDS:
1474     default:
1475         NOT_REACHED();
1476     }
1477 }
1478
1479 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1480  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1481  * with a 1-bit indicating that the corresponding value bit must match and a
1482  * 0-bit indicating a don't-care.
1483  *
1484  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1485  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1486  * call is equivalent to mf_set_wild(mf, rule).
1487  *
1488  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1489  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1490 void
1491 mf_set(const struct mf_field *mf,
1492        const union mf_value *value, const union mf_value *mask,
1493        struct cls_rule *rule)
1494 {
1495     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1496         mf_set_value(mf, value, rule);
1497         return;
1498     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1499         mf_set_wild(mf, rule);
1500         return;
1501     }
1502
1503     switch (mf->id) {
1504     case MFF_IN_PORT:
1505     case MFF_ETH_TYPE:
1506     case MFF_VLAN_VID:
1507     case MFF_VLAN_PCP:
1508     case MFF_IPV6_LABEL:
1509     case MFF_IP_PROTO:
1510     case MFF_IP_TTL:
1511     case MFF_IP_DSCP:
1512     case MFF_IP_ECN:
1513     case MFF_ARP_OP:
1514     case MFF_ARP_SHA:
1515     case MFF_ARP_THA:
1516     case MFF_ICMPV4_TYPE:
1517     case MFF_ICMPV4_CODE:
1518     case MFF_ICMPV6_TYPE:
1519     case MFF_ICMPV6_CODE:
1520     case MFF_ND_SLL:
1521     case MFF_ND_TLL:
1522         NOT_REACHED();
1523
1524     case MFF_TUN_ID:
1525         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1526         break;
1527     case MFF_METADATA:
1528         cls_rule_set_metadata_masked(rule, value->be64, mask->be64);
1529         break;
1530
1531     CASE_MFF_REGS:
1532         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1533                                 ntohl(value->be32), ntohl(mask->be32));
1534         break;
1535
1536     case MFF_ETH_DST:
1537         cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1538         break;
1539
1540     case MFF_ETH_SRC:
1541         cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1542         break;
1543
1544     case MFF_VLAN_TCI:
1545         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1546         break;
1547
1548     case MFF_IPV4_SRC:
1549         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1550         break;
1551
1552     case MFF_IPV4_DST:
1553         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1554         break;
1555
1556     case MFF_IPV6_SRC:
1557         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1558         break;
1559
1560     case MFF_IPV6_DST:
1561         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1562         break;
1563
1564     case MFF_ND_TARGET:
1565         cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1566         break;
1567
1568     case MFF_IP_FRAG:
1569         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1570         break;
1571
1572     case MFF_ARP_SPA:
1573         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1574         break;
1575
1576     case MFF_ARP_TPA:
1577         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1578         break;
1579
1580     case MFF_TCP_SRC:
1581     case MFF_UDP_SRC:
1582         cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1583         break;
1584
1585     case MFF_TCP_DST:
1586     case MFF_UDP_DST:
1587         cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1588         break;
1589
1590     case MFF_N_IDS:
1591     default:
1592         NOT_REACHED();
1593     }
1594 }
1595
1596 static enum ofperr
1597 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1598            const char *type)
1599 {
1600     if (!sf->field) {
1601         VLOG_WARN_RL(&rl, "unknown %s field", type);
1602     } else if (!sf->n_bits) {
1603         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1604     } else if (sf->ofs >= sf->field->n_bits) {
1605         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1606                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1607     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1608         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1609                      "of %s field %s", sf->ofs, sf->n_bits,
1610                      sf->field->n_bits, type, sf->field->name);
1611     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1612         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1613                      type, sf->field->name);
1614     } else {
1615         return 0;
1616     }
1617
1618     return OFPERR_OFPBAC_BAD_ARGUMENT;
1619 }
1620
1621 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1622  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1623  * ofp_mkerr()).  */
1624 enum ofperr
1625 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1626 {
1627     return mf_check__(sf, flow, "source");
1628 }
1629
1630 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1631  * if so, otherwise an OpenFlow error code (e.g. as returned by
1632  * ofp_mkerr()). */
1633 enum ofperr
1634 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1635 {
1636     int error = mf_check__(sf, flow, "destination");
1637     if (!error && !sf->field->writable) {
1638         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1639                      sf->field->name);
1640         return OFPERR_OFPBAC_BAD_ARGUMENT;
1641     }
1642     return error;
1643 }
1644
1645 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1646  * 'value' and 'mask', respectively. */
1647 void
1648 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1649        union mf_value *value, union mf_value *mask)
1650 {
1651     mf_get_value(mf, &rule->flow, value);
1652     mf_get_mask(mf, &rule->wc, mask);
1653 }
1654
1655 /* Assigns a random value for field 'mf' to 'value'. */
1656 void
1657 mf_random_value(const struct mf_field *mf, union mf_value *value)
1658 {
1659     random_bytes(value, mf->n_bytes);
1660
1661     switch (mf->id) {
1662     case MFF_TUN_ID:
1663     case MFF_METADATA:
1664     case MFF_IN_PORT:
1665     CASE_MFF_REGS:
1666     case MFF_ETH_SRC:
1667     case MFF_ETH_DST:
1668     case MFF_ETH_TYPE:
1669     case MFF_VLAN_TCI:
1670     case MFF_IPV4_SRC:
1671     case MFF_IPV4_DST:
1672     case MFF_IPV6_SRC:
1673     case MFF_IPV6_DST:
1674     case MFF_IP_PROTO:
1675     case MFF_IP_TTL:
1676     case MFF_ARP_SPA:
1677     case MFF_ARP_TPA:
1678     case MFF_ARP_SHA:
1679     case MFF_ARP_THA:
1680     case MFF_TCP_SRC:
1681     case MFF_TCP_DST:
1682     case MFF_UDP_SRC:
1683     case MFF_UDP_DST:
1684     case MFF_ICMPV4_TYPE:
1685     case MFF_ICMPV4_CODE:
1686     case MFF_ICMPV6_TYPE:
1687     case MFF_ICMPV6_CODE:
1688     case MFF_ND_TARGET:
1689     case MFF_ND_SLL:
1690     case MFF_ND_TLL:
1691         break;
1692
1693     case MFF_IPV6_LABEL:
1694         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1695         break;
1696
1697     case MFF_IP_DSCP:
1698         value->u8 &= IP_DSCP_MASK;
1699         break;
1700
1701     case MFF_IP_ECN:
1702         value->u8 &= IP_ECN_MASK;
1703         break;
1704
1705     case MFF_IP_FRAG:
1706         value->u8 &= FLOW_NW_FRAG_MASK;
1707         break;
1708
1709     case MFF_ARP_OP:
1710         value->be16 &= htons(0xff);
1711         break;
1712
1713     case MFF_VLAN_VID:
1714         value->be16 &= htons(VLAN_VID_MASK);
1715         break;
1716
1717     case MFF_VLAN_PCP:
1718         value->u8 &= 0x07;
1719         break;
1720
1721     case MFF_N_IDS:
1722     default:
1723         NOT_REACHED();
1724     }
1725 }
1726
1727 static char *
1728 mf_from_integer_string(const struct mf_field *mf, const char *s,
1729                        uint8_t *valuep, uint8_t *maskp)
1730 {
1731     unsigned long long int integer, mask;
1732     char *tail;
1733     int i;
1734
1735     errno = 0;
1736     integer = strtoull(s, &tail, 0);
1737     if (errno || (*tail != '\0' && *tail != '/')) {
1738         goto syntax_error;
1739     }
1740
1741     if (*tail == '/') {
1742         mask = strtoull(tail + 1, &tail, 0);
1743         if (errno || *tail != '\0') {
1744             goto syntax_error;
1745         }
1746     } else {
1747         mask = ULLONG_MAX;
1748     }
1749
1750     for (i = mf->n_bytes - 1; i >= 0; i--) {
1751         valuep[i] = integer;
1752         maskp[i] = mask;
1753         integer >>= 8;
1754         mask >>= 8;
1755     }
1756     if (integer) {
1757         return xasprintf("%s: value too large for %u-byte field %s",
1758                          s, mf->n_bytes, mf->name);
1759     }
1760     return NULL;
1761
1762 syntax_error:
1763     return xasprintf("%s: bad syntax for %s", s, mf->name);
1764 }
1765
1766 static char *
1767 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1768                         uint8_t mac[ETH_ADDR_LEN],
1769                         uint8_t mask[ETH_ADDR_LEN])
1770 {
1771     assert(mf->n_bytes == ETH_ADDR_LEN);
1772
1773     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1774                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1775     case ETH_ADDR_SCAN_COUNT * 2:
1776         return NULL;
1777
1778     case ETH_ADDR_SCAN_COUNT:
1779         memset(mask, 0xff, ETH_ADDR_LEN);
1780         return NULL;
1781
1782     default:
1783         return xasprintf("%s: invalid Ethernet address", s);
1784     }
1785 }
1786
1787 static char *
1788 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1789                     ovs_be32 *ip, ovs_be32 *mask)
1790 {
1791     int prefix;
1792
1793     assert(mf->n_bytes == sizeof *ip);
1794
1795     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1796                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1797         /* OK. */
1798     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1799                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1800         if (prefix <= 0 || prefix > 32) {
1801             return xasprintf("%s: network prefix bits not between 1 and "
1802                              "32", s);
1803         } else if (prefix == 32) {
1804             *mask = htonl(UINT32_MAX);
1805         } else {
1806             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1807         }
1808     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1809         *mask = htonl(UINT32_MAX);
1810     } else {
1811         return xasprintf("%s: invalid IP address", s);
1812     }
1813     return NULL;
1814 }
1815
1816 static char *
1817 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1818                     struct in6_addr *value, struct in6_addr *mask)
1819 {
1820     char *str = xstrdup(s);
1821     char *save_ptr = NULL;
1822     const char *name, *netmask;
1823     int retval;
1824
1825     assert(mf->n_bytes == sizeof *value);
1826
1827     name = strtok_r(str, "/", &save_ptr);
1828     retval = name ? lookup_ipv6(name, value) : EINVAL;
1829     if (retval) {
1830         char *err;
1831
1832         err = xasprintf("%s: could not convert to IPv6 address", str);
1833         free(str);
1834
1835         return err;
1836     }
1837
1838     netmask = strtok_r(NULL, "/", &save_ptr);
1839     if (netmask) {
1840         if (inet_pton(AF_INET6, netmask, mask) != 1) {
1841             int prefix = atoi(netmask);
1842             if (prefix <= 0 || prefix > 128) {
1843                 free(str);
1844                 return xasprintf("%s: prefix bits not between 1 and 128", s);
1845             } else {
1846                 *mask = ipv6_create_mask(prefix);
1847             }
1848         }
1849     } else {
1850         *mask = in6addr_exact;
1851     }
1852     free(str);
1853
1854     return NULL;
1855 }
1856
1857 static char *
1858 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1859                         ovs_be16 *valuep, ovs_be16 *maskp)
1860 {
1861     uint16_t port;
1862
1863     assert(mf->n_bytes == sizeof(ovs_be16));
1864     if (ofputil_port_from_string(s, &port)) {
1865         *valuep = htons(port);
1866         *maskp = htons(UINT16_MAX);
1867         return NULL;
1868     } else {
1869         return mf_from_integer_string(mf, s,
1870                                       (uint8_t *) valuep, (uint8_t *) maskp);
1871     }
1872 }
1873
1874 struct frag_handling {
1875     const char *name;
1876     uint8_t mask;
1877     uint8_t value;
1878 };
1879
1880 static const struct frag_handling all_frags[] = {
1881 #define A FLOW_NW_FRAG_ANY
1882 #define L FLOW_NW_FRAG_LATER
1883     /* name               mask  value */
1884
1885     { "no",               A|L,  0     },
1886     { "first",            A|L,  A     },
1887     { "later",            A|L,  A|L   },
1888
1889     { "no",               A,    0     },
1890     { "yes",              A,    A     },
1891
1892     { "not_later",        L,    0     },
1893     { "later",            L,    L     },
1894 #undef A
1895 #undef L
1896 };
1897
1898 static char *
1899 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1900 {
1901     const struct frag_handling *h;
1902
1903     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1904         if (!strcasecmp(s, h->name)) {
1905             /* We force the upper bits of the mask on to make mf_parse_value()
1906              * happy (otherwise it will never think it's an exact match.) */
1907             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1908             *valuep = h->value;
1909             return NULL;
1910         }
1911     }
1912
1913     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1914                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1915 }
1916
1917 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1918  * NULL if successful, otherwise a malloc()'d string describing the error. */
1919 char *
1920 mf_parse(const struct mf_field *mf, const char *s,
1921          union mf_value *value, union mf_value *mask)
1922 {
1923     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1924         memset(value, 0, mf->n_bytes);
1925         memset(mask, 0, mf->n_bytes);
1926         return NULL;
1927     }
1928
1929     switch (mf->string) {
1930     case MFS_DECIMAL:
1931     case MFS_HEXADECIMAL:
1932         return mf_from_integer_string(mf, s,
1933                                       (uint8_t *) value, (uint8_t *) mask);
1934
1935     case MFS_ETHERNET:
1936         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1937
1938     case MFS_IPV4:
1939         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1940
1941     case MFS_IPV6:
1942         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1943
1944     case MFS_OFP_PORT:
1945         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1946
1947     case MFS_FRAG:
1948         return mf_from_frag_string(s, &value->u8, &mask->u8);
1949     }
1950     NOT_REACHED();
1951 }
1952
1953 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1954  * successful, otherwise a malloc()'d string describing the error. */
1955 char *
1956 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1957 {
1958     union mf_value mask;
1959     char *error;
1960
1961     error = mf_parse(mf, s, value, &mask);
1962     if (error) {
1963         return error;
1964     }
1965
1966     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1967         return xasprintf("%s: wildcards not allowed here", s);
1968     }
1969     return NULL;
1970 }
1971
1972 static void
1973 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1974                          const uint8_t *maskp, struct ds *s)
1975 {
1976     unsigned long long int integer;
1977     int i;
1978
1979     assert(mf->n_bytes <= 8);
1980
1981     integer = 0;
1982     for (i = 0; i < mf->n_bytes; i++) {
1983         integer = (integer << 8) | valuep[i];
1984     }
1985     if (mf->string == MFS_HEXADECIMAL) {
1986         ds_put_format(s, "%#llx", integer);
1987     } else {
1988         ds_put_format(s, "%lld", integer);
1989     }
1990
1991     if (maskp) {
1992         unsigned long long int mask;
1993
1994         mask = 0;
1995         for (i = 0; i < mf->n_bytes; i++) {
1996             mask = (mask << 8) | maskp[i];
1997         }
1998
1999         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2000          * not sure that that a bit-mask written in decimal is ever easier to
2001          * understand than the same bit-mask written in hexadecimal. */
2002         ds_put_format(s, "/%#llx", mask);
2003     }
2004 }
2005
2006 static void
2007 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2008                       struct ds *s)
2009 {
2010     const struct frag_handling *h;
2011     uint8_t value = *valuep;
2012     uint8_t mask = *maskp;
2013
2014     value &= mask;
2015     mask &= FLOW_NW_FRAG_MASK;
2016
2017     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2018         if (value == h->value && mask == h->mask) {
2019             ds_put_cstr(s, h->name);
2020             return;
2021         }
2022     }
2023     ds_put_cstr(s, "<error>");
2024 }
2025
2026 /* Appends to 's' a string representation of field 'mf' whose value is in
2027  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2028 void
2029 mf_format(const struct mf_field *mf,
2030           const union mf_value *value, const union mf_value *mask,
2031           struct ds *s)
2032 {
2033     if (mask) {
2034         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2035             ds_put_cstr(s, "ANY");
2036             return;
2037         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2038             mask = NULL;
2039         }
2040     }
2041
2042     switch (mf->string) {
2043     case MFS_OFP_PORT:
2044         if (!mask) {
2045             ofputil_format_port(ntohs(value->be16), s);
2046             break;
2047         }
2048         /* fall through */
2049     case MFS_DECIMAL:
2050     case MFS_HEXADECIMAL:
2051         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2052         break;
2053
2054     case MFS_ETHERNET:
2055         eth_format_masked(value->mac, mask->mac, s);
2056         break;
2057
2058     case MFS_IPV4:
2059         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2060                          s);
2061         break;
2062
2063     case MFS_IPV6:
2064         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2065         break;
2066
2067     case MFS_FRAG:
2068         mf_format_frag_string(&value->u8, &mask->u8, s);
2069         break;
2070
2071     default:
2072         NOT_REACHED();
2073     }
2074 }
2075 \f
2076 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2077  * least-significant bits in 'x'.
2078  *
2079  * See mf_set_subfield() for an example.
2080  *
2081  * The difference between this function and mf_set_subfield() is that the
2082  * latter function can only handle subfields up to 64 bits wide, whereas this
2083  * one handles the general case.  On the other hand, mf_set_subfield() is
2084  * arguably easier to use. */
2085 void
2086 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2087                   struct cls_rule *rule)
2088 {
2089     const struct mf_field *field = sf->field;
2090     union mf_value value, mask;
2091
2092     mf_get(field, rule, &value, &mask);
2093     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2094     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2095     mf_set(field, &value, &mask, rule);
2096 }
2097
2098 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2099  * least-significant bits of 'x'.
2100  *
2101  * Example: suppose that 'sf->field' is originally the following 2-byte field
2102  * in 'rule':
2103  *
2104  *     value == 0xe00a == 2#1110000000001010
2105  *      mask == 0xfc3f == 2#1111110000111111
2106  *
2107  * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2108  * sf->n_bits == 7 would have the following effect (note that 0x55 is
2109  * 2#1010101):
2110  *
2111  *     value == 0xd50a == 2#1101010100001010
2112  *      mask == 0xff3f == 2#1111111100111111
2113  *                           ^^^^^^^ affected bits
2114  *
2115  * The caller is responsible for ensuring that the result will be a valid
2116  * wildcard pattern for 'sf->field'.  The caller is responsible for ensuring
2117  * that 'rule' meets 'sf->field''s prerequisites. */
2118 void
2119 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2120                 struct cls_rule *rule)
2121 {
2122     const struct mf_field *field = sf->field;
2123     unsigned int n_bits = sf->n_bits;
2124     unsigned int ofs = sf->ofs;
2125
2126     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2127         union mf_value value;
2128         int i;
2129
2130         for (i = field->n_bytes - 1; i >= 0; i--) {
2131             ((uint8_t *) &value)[i] = x;
2132             x >>= 8;
2133         }
2134         mf_set_value(field, &value, rule);
2135     } else {
2136         union mf_value value, mask;
2137         uint8_t *vp = (uint8_t *) &value;
2138         uint8_t *mp = (uint8_t *) &mask;
2139
2140         mf_get(field, rule, &value, &mask);
2141         bitwise_put(x,          vp, field->n_bytes, ofs, n_bits);
2142         bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2143         mf_set(field, &value, &mask, rule);
2144     }
2145 }
2146
2147 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2148 void
2149 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2150                       struct flow *flow)
2151 {
2152     const struct mf_field *field = sf->field;
2153     unsigned int n_bits = sf->n_bits;
2154     unsigned int ofs = sf->ofs;
2155     union mf_value value;
2156
2157     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2158         int i;
2159
2160         for (i = field->n_bytes - 1; i >= 0; i--) {
2161             ((uint8_t *) &value)[i] = x;
2162             x >>= 8;
2163         }
2164         mf_set_flow_value(field, &value, flow);
2165     } else {
2166         mf_get_value(field, flow, &value);
2167         bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2168         mf_set_flow_value(field, &value, flow);
2169     }
2170 }
2171
2172 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2173  * reading 'flow', e.g. as checked by mf_check_src(). */
2174 void
2175 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2176                  union mf_subvalue *x)
2177 {
2178     union mf_value value;
2179
2180     mf_get_value(sf->field, flow, &value);
2181
2182     memset(x, 0, sizeof *x);
2183     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2184                  x, sizeof *x, 0,
2185                  sf->n_bits);
2186 }
2187
2188 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2189  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2190  * less. */
2191 uint64_t
2192 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2193 {
2194     union mf_value value;
2195
2196     mf_get_value(sf->field, flow, &value);
2197     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2198 }
2199
2200 /* Formats 'sf' into 's' in a format normally acceptable to
2201  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2202  * sf->field has no NXM name.) */
2203 void
2204 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2205 {
2206     if (!sf->field) {
2207         ds_put_cstr(s, "<unknown>");
2208     } else if (sf->field->nxm_name) {
2209         ds_put_cstr(s, sf->field->nxm_name);
2210     } else if (sf->field->nxm_header) {
2211         uint32_t header = sf->field->nxm_header;
2212         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2213     } else {
2214         ds_put_cstr(s, sf->field->name);
2215     }
2216
2217     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2218         ds_put_cstr(s, "[]");
2219     } else if (sf->n_bits == 1) {
2220         ds_put_format(s, "[%d]", sf->ofs);
2221     } else {
2222         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2223     }
2224 }
2225
2226 static const struct mf_field *
2227 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2228 {
2229     int i;
2230
2231     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2232     if (*wild) {
2233         name_len -= 2;
2234     }
2235
2236     for (i = 0; i < MFF_N_IDS; i++) {
2237         const struct mf_field *mf = mf_from_id(i);
2238
2239         if (mf->nxm_name
2240             && !strncmp(mf->nxm_name, name, name_len)
2241             && mf->nxm_name[name_len] == '\0') {
2242             return mf;
2243         }
2244         if (mf->oxm_name
2245             && !strncmp(mf->oxm_name, name, name_len)
2246             && mf->oxm_name[name_len] == '\0') {
2247             return mf;
2248         }
2249     }
2250
2251     return NULL;
2252 }
2253
2254 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2255  * returns NULL and advances '*sp' to the first byte following the parsed
2256  * string.  On failure, returns a malloc()'d error message, does not modify
2257  * '*sp', and does not properly initialize 'sf'.
2258  *
2259  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2260  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2261  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2262  * may both be omitted (the [] are still required) to indicate an entire
2263  * field. */
2264 char *
2265 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2266 {
2267     const struct mf_field *field;
2268     const char *name;
2269     int start, end;
2270     const char *s;
2271     int name_len;
2272     bool wild;
2273
2274     s = *sp;
2275     name = s;
2276     name_len = strcspn(s, "[");
2277     if (s[name_len] != '[') {
2278         return xasprintf("%s: missing [ looking for field name", *sp);
2279     }
2280
2281     field = mf_parse_subfield_name(name, name_len, &wild);
2282     if (!field) {
2283         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2284     }
2285
2286     s += name_len;
2287     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2288         /* Nothing to do. */
2289     } else if (sscanf(s, "[%d]", &start) == 1) {
2290         end = start;
2291     } else if (!strncmp(s, "[]", 2)) {
2292         start = 0;
2293         end = field->n_bits - 1;
2294     } else {
2295         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2296                          "[<start>..<end>]", *sp);
2297     }
2298     s = strchr(s, ']') + 1;
2299
2300     if (start > end) {
2301         return xasprintf("%s: starting bit %d is after ending bit %d",
2302                          *sp, start, end);
2303     } else if (start >= field->n_bits) {
2304         return xasprintf("%s: starting bit %d is not valid because field is "
2305                          "only %d bits wide", *sp, start, field->n_bits);
2306     } else if (end >= field->n_bits){
2307         return xasprintf("%s: ending bit %d is not valid because field is "
2308                          "only %d bits wide", *sp, end, field->n_bits);
2309     }
2310
2311     sf->field = field;
2312     sf->ofs = start;
2313     sf->n_bits = end - start + 1;
2314
2315     *sp = s;
2316     return NULL;
2317 }
2318
2319 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2320  * byte in 's' following the parsed string.
2321  *
2322  * Exits with an error message if 's' has incorrect syntax.
2323  *
2324  * The syntax parsed from 's' takes the form "header[start..end]" where
2325  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2326  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2327  * may both be omitted (the [] are still required) to indicate an entire
2328  * field.  */
2329 const char *
2330 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2331 {
2332     char *msg = mf_parse_subfield__(sf, &s);
2333     if (msg) {
2334         ovs_fatal(0, "%s", msg);
2335     }
2336     return s;
2337 }