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