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