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