Implement new "learn" action.
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011 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-util.h"
30 #include "packets.h"
31 #include "random.h"
32 #include "shash.h"
33 #include "socket-util.h"
34 #include "unaligned.h"
35
36 #define MF_FIELD_SIZES(MEMBER)                  \
37     sizeof ((union mf_value *)0)->MEMBER,       \
38     8 * sizeof ((union mf_value *)0)->MEMBER
39
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
41     /* ## -------- ## */
42     /* ## metadata ## */
43     /* ## -------- ## */
44
45     {
46         MFF_TUN_ID, "tun_id", NULL,
47         MF_FIELD_SIZES(be64),
48         MFM_FULLY, 0,
49         MFS_HEXADECIMAL,
50         MFP_NONE,
51         NXM_NX_TUN_ID,
52     }, {
53         MFF_IN_PORT, "in_port", NULL,
54         MF_FIELD_SIZES(be16),
55         MFM_NONE, FWW_IN_PORT,
56         MFS_OFP_PORT,
57         MFP_NONE,
58         NXM_OF_IN_PORT,
59     },
60
61 #define REGISTER(IDX)                           \
62     {                                           \
63         MFF_REG##IDX, "reg" #IDX, NULL,         \
64         MF_FIELD_SIZES(be32),                   \
65         MFM_FULLY, 0,                           \
66         MFS_HEXADECIMAL,                        \
67         MFP_NONE,                               \
68         NXM_NX_REG(IDX),                        \
69     }
70 #if FLOW_N_REGS > 0
71     REGISTER(0),
72 #endif
73 #if FLOW_N_REGS > 1
74     REGISTER(1),
75 #endif
76 #if FLOW_N_REGS > 2
77     REGISTER(2),
78 #endif
79 #if FLOW_N_REGS > 3
80     REGISTER(3),
81 #endif
82 #if FLOW_N_REGS > 4
83 #error
84 #endif
85
86     /* ## -- ## */
87     /* ## L2 ## */
88     /* ## -- ## */
89
90     {
91         MFF_ETH_SRC, "eth_src", "dl_src",
92         MF_FIELD_SIZES(mac),
93         MFM_NONE, FWW_DL_SRC,
94         MFS_ETHERNET,
95         MFP_NONE,
96         NXM_OF_ETH_SRC,
97     }, {
98         MFF_ETH_DST, "eth_dst", "dl_dst",
99         MF_FIELD_SIZES(mac),
100         MFM_MCAST, 0,
101         MFS_ETHERNET,
102         MFP_NONE,
103         NXM_OF_ETH_DST,
104     }, {
105         MFF_ETH_TYPE, "eth_type", "dl_type",
106         MF_FIELD_SIZES(be16),
107         MFM_NONE, FWW_DL_TYPE,
108         MFS_HEXADECIMAL,
109         MFP_NONE,
110         NXM_OF_ETH_TYPE,
111     },
112
113     {
114         MFF_VLAN_TCI, "vlan_tci", NULL,
115         MF_FIELD_SIZES(be16),
116         MFM_FULLY, 0,
117         MFS_HEXADECIMAL,
118         MFP_NONE,
119         NXM_OF_VLAN_TCI,
120     }, {
121         MFF_VLAN_VID, "dl_vlan", NULL,
122         sizeof(ovs_be16), 12,
123         MFM_NONE, 0,
124         MFS_DECIMAL,
125         MFP_NONE,
126         0,
127     }, {
128         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
129         1, 3,
130         MFM_NONE, 0,
131         MFS_DECIMAL,
132         MFP_NONE,
133         0,
134     },
135
136     /* ## -- ## */
137     /* ## L3 ## */
138     /* ## -- ## */
139
140     {
141         MFF_IPV4_SRC, "ip_src", "nw_src",
142         MF_FIELD_SIZES(be32),
143         MFM_CIDR, 0,
144         MFS_IPV4,
145         MFP_IPV4,
146         NXM_OF_IP_SRC,
147     }, {
148         MFF_IPV4_DST, "ip_dst", "nw_dst",
149         MF_FIELD_SIZES(be32),
150         MFM_CIDR, 0,
151         MFS_IPV4,
152         MFP_IPV4,
153         NXM_OF_IP_DST,
154     },
155
156     {
157         MFF_IPV6_SRC, "ipv6_src", NULL,
158         MF_FIELD_SIZES(ipv6),
159         MFM_CIDR, 0,
160         MFS_IPV6,
161         MFP_IPV6,
162         NXM_NX_IPV6_SRC,
163     }, {
164         MFF_IPV6_DST, "ipv6_dst", NULL,
165         MF_FIELD_SIZES(ipv6),
166         MFM_CIDR, 0,
167         MFS_IPV6,
168         MFP_IPV6,
169         NXM_NX_IPV6_DST,
170     },
171
172     {
173         MFF_IP_PROTO, "nw_proto", NULL,
174         MF_FIELD_SIZES(u8),
175         MFM_NONE, FWW_NW_PROTO,
176         MFS_DECIMAL,
177         MFP_IP_ANY,
178         NXM_OF_IP_PROTO,
179     }, {
180         MFF_IP_TOS, "nw_tos", NULL,
181         MF_FIELD_SIZES(u8),
182         MFM_NONE, FWW_NW_TOS,
183         MFS_DECIMAL,
184         MFP_IP_ANY,
185         NXM_OF_IP_TOS,
186     },
187
188     {
189         MFF_ARP_OP, "arp_op", NULL,
190         MF_FIELD_SIZES(be16),
191         MFM_NONE, FWW_NW_PROTO,
192         MFS_DECIMAL,
193         MFP_ARP,
194         NXM_OF_ARP_OP,
195     }, {
196         MFF_ARP_SPA, "arp_spa", NULL,
197         MF_FIELD_SIZES(be32),
198         MFM_CIDR, 0,
199         MFS_IPV4,
200         MFP_ARP,
201         NXM_OF_ARP_SPA,
202     }, {
203         MFF_ARP_TPA, "arp_tpa", NULL,
204         MF_FIELD_SIZES(be32),
205         MFM_CIDR, 0,
206         MFS_IPV4,
207         MFP_ARP,
208         NXM_OF_ARP_TPA,
209     }, {
210         MFF_ARP_SHA, "arp_sha", NULL,
211         MF_FIELD_SIZES(mac),
212         MFM_NONE, FWW_ARP_SHA,
213         MFS_ETHERNET,
214         MFP_ARP,
215         NXM_NX_ARP_SHA,
216     }, {
217         MFF_ARP_THA, "arp_tha", NULL,
218         MF_FIELD_SIZES(mac),
219         MFM_NONE, FWW_ARP_THA,
220         MFS_ETHERNET,
221         MFP_ARP,
222         NXM_NX_ARP_THA,
223     },
224
225     /* ## -- ## */
226     /* ## L4 ## */
227     /* ## -- ## */
228
229     {
230         MFF_TCP_SRC, "tcp_src", "tp_src",
231         MF_FIELD_SIZES(be16),
232         MFM_NONE, FWW_TP_SRC,
233         MFS_DECIMAL,
234         MFP_TCP,
235         NXM_OF_TCP_SRC,
236     }, {
237         MFF_TCP_DST, "tcp_dst", "tp_dst",
238         MF_FIELD_SIZES(be16),
239         MFM_NONE, FWW_TP_DST,
240         MFS_DECIMAL,
241         MFP_TCP,
242         NXM_OF_TCP_DST,
243     },
244
245     {
246         MFF_UDP_SRC, "udp_src", NULL,
247         MF_FIELD_SIZES(be16),
248         MFM_NONE, FWW_TP_SRC,
249         MFS_DECIMAL,
250         MFP_UDP,
251         NXM_OF_UDP_SRC,
252     }, {
253         MFF_UDP_DST, "udp_dst", NULL,
254         MF_FIELD_SIZES(be16),
255         MFM_NONE, FWW_TP_DST,
256         MFS_DECIMAL,
257         MFP_UDP,
258         NXM_OF_UDP_DST,
259     },
260
261     {
262         MFF_ICMP_TYPE, "icmp_type", NULL,
263         MF_FIELD_SIZES(u8),
264         MFM_NONE, FWW_TP_SRC,
265         MFS_DECIMAL,
266         MFP_ICMP_ANY,
267         NXM_OF_ICMP_TYPE,
268     }, {
269         MFF_ICMP_CODE, "icmp_code", NULL,
270         MF_FIELD_SIZES(u8),
271         MFM_NONE, FWW_TP_SRC,
272         MFS_DECIMAL,
273         MFP_ICMP_ANY,
274         NXM_OF_ICMP_CODE,
275     },
276
277     /* ## ---- ## */
278     /* ## L"5" ## */
279     /* ## ---- ## */
280
281     {
282         MFF_ND_TARGET, "nd_target", NULL,
283         MF_FIELD_SIZES(ipv6),
284         MFM_NONE, FWW_ND_TARGET,
285         MFS_IPV6,
286         MFP_ND,
287         NXM_NX_ND_TARGET,
288     }, {
289         MFF_ND_SLL, "nd_sll", NULL,
290         MF_FIELD_SIZES(mac),
291         MFM_NONE, FWW_ARP_SHA,
292         MFS_ETHERNET,
293         MFP_ND_SOLICIT,
294         NXM_NX_ND_SLL,
295     }, {
296         MFF_ND_TLL, "nd_tll", NULL,
297         MF_FIELD_SIZES(mac),
298         MFM_NONE, FWW_ARP_THA,
299         MFS_ETHERNET,
300         MFP_ND_ADVERT,
301         NXM_NX_ND_TLL,
302     }
303 };
304
305 /* Returns the field with the given 'id'. */
306 const struct mf_field *
307 mf_from_id(enum mf_field_id id)
308 {
309     assert((unsigned int) id < MFF_N_IDS);
310     return &mf_fields[id];
311 }
312
313 /* Returns the field with the given 'name', or a null pointer if no field has
314  * that name. */
315 const struct mf_field *
316 mf_from_name(const char *name)
317 {
318     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
319
320     if (shash_is_empty(&mf_by_name)) {
321         const struct mf_field *mf;
322
323         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
324             shash_add_once(&mf_by_name, mf->name, mf);
325             if (mf->extra_name) {
326                 shash_add_once(&mf_by_name, mf->extra_name, mf);
327             }
328         }
329     }
330
331     return shash_find_data(&mf_by_name, name);
332 }
333
334 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
335  * specifies at least one bit in the field.
336  *
337  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
338  * meets 'mf''s prerequisites. */
339 bool
340 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
341 {
342     switch (mf->id) {
343     case MFF_IN_PORT:
344     case MFF_ETH_SRC:
345     case MFF_ETH_TYPE:
346     case MFF_IP_PROTO:
347     case MFF_IP_TOS:
348     case MFF_ARP_OP:
349     case MFF_ARP_SHA:
350     case MFF_ARP_THA:
351     case MFF_TCP_SRC:
352     case MFF_TCP_DST:
353     case MFF_UDP_SRC:
354     case MFF_UDP_DST:
355     case MFF_ICMP_TYPE:
356     case MFF_ICMP_CODE:
357     case MFF_ND_TARGET:
358     case MFF_ND_SLL:
359     case MFF_ND_TLL:
360         assert(mf->fww_bit != 0);
361         return (wc->wildcards & mf->fww_bit) != 0;
362
363     case MFF_TUN_ID:
364         return !wc->tun_id_mask;
365
366 #if FLOW_N_REGS > 0
367     case MFF_REG0:
368 #endif
369 #if FLOW_N_REGS > 1
370     case MFF_REG1:
371 #endif
372 #if FLOW_N_REGS > 2
373     case MFF_REG2:
374 #endif
375 #if FLOW_N_REGS > 3
376     case MFF_REG3:
377 #endif
378 #if FLOW_N_REGS > 4
379 #error
380 #endif
381         return !wc->reg_masks[mf->id - MFF_REG0];
382
383     case MFF_ETH_DST:
384         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
385                 == (FWW_ETH_MCAST | FWW_DL_DST));
386
387     case MFF_VLAN_TCI:
388         return !wc->vlan_tci_mask;
389     case MFF_VLAN_VID:
390         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
391     case MFF_VLAN_PCP:
392         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
393
394     case MFF_IPV4_SRC:
395         return !wc->nw_src_mask;
396     case MFF_IPV4_DST:
397         return !wc->nw_dst_mask;
398
399     case MFF_IPV6_SRC:
400         return ipv6_mask_is_any(&wc->ipv6_src_mask);
401     case MFF_IPV6_DST:
402         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
403
404     case MFF_ARP_SPA:
405         return !wc->nw_src_mask;
406     case MFF_ARP_TPA:
407         return !wc->nw_dst_mask;
408
409     case MFF_N_IDS:
410     default:
411         NOT_REACHED();
412     }
413 }
414
415 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
416  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
417  * purposes, or to 0 if it is wildcarded.
418  *
419  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
420  * meets 'mf''s prerequisites. */
421 void
422 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
423             union mf_value *mask)
424 {
425     switch (mf->id) {
426     case MFF_IN_PORT:
427     case MFF_ETH_SRC:
428     case MFF_ETH_TYPE:
429     case MFF_IP_PROTO:
430     case MFF_IP_TOS:
431     case MFF_ARP_OP:
432     case MFF_ARP_SHA:
433     case MFF_ARP_THA:
434     case MFF_TCP_SRC:
435     case MFF_TCP_DST:
436     case MFF_UDP_SRC:
437     case MFF_UDP_DST:
438     case MFF_ICMP_TYPE:
439     case MFF_ICMP_CODE:
440     case MFF_ND_TARGET:
441     case MFF_ND_SLL:
442     case MFF_ND_TLL:
443         assert(mf->fww_bit != 0);
444         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
445         break;
446
447     case MFF_TUN_ID:
448         mask->be64 = wc->tun_id_mask;
449         break;
450
451 #if FLOW_N_REGS > 0
452     case MFF_REG0:
453 #endif
454 #if FLOW_N_REGS > 1
455     case MFF_REG1:
456 #endif
457 #if FLOW_N_REGS > 2
458     case MFF_REG2:
459 #endif
460 #if FLOW_N_REGS > 3
461     case MFF_REG3:
462 #endif
463 #if FLOW_N_REGS > 4
464 #error
465 #endif
466         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
467         break;
468
469     case MFF_ETH_DST:
470         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
471                ETH_ADDR_LEN);
472         break;
473
474     case MFF_VLAN_TCI:
475         mask->be16 = wc->vlan_tci_mask;
476         break;
477     case MFF_VLAN_VID:
478         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
479         break;
480     case MFF_VLAN_PCP:
481         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
482         break;
483
484     case MFF_IPV4_SRC:
485         mask->be32 = wc->nw_src_mask;
486         break;
487     case MFF_IPV4_DST:
488         mask->be32 = wc->nw_dst_mask;
489         break;
490
491     case MFF_IPV6_SRC:
492         mask->ipv6 = wc->ipv6_src_mask;
493         break;
494     case MFF_IPV6_DST:
495         mask->ipv6 = wc->ipv6_dst_mask;
496         break;
497
498     case MFF_ARP_SPA:
499         mask->be32 = wc->nw_src_mask;
500         break;
501     case MFF_ARP_TPA:
502         mask->be32 = wc->nw_dst_mask;
503         break;
504
505     case MFF_N_IDS:
506     default:
507         NOT_REACHED();
508     }
509 }
510
511 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
512  * if the mask is valid, false otherwise. */
513 bool
514 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
515 {
516     switch (mf->maskable) {
517     case MFM_NONE:
518         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
519                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
520
521     case MFM_FULLY:
522         return true;
523
524     case MFM_CIDR:
525         return (mf->n_bytes == 4
526                 ? ip_is_cidr(mask->be32)
527                 : ipv6_is_cidr(&mask->ipv6));
528
529     case MFM_MCAST:
530         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
531     }
532
533     NOT_REACHED();
534 }
535
536 static bool
537 is_ip_any(const struct flow *flow)
538 {
539     return (flow->dl_type == htons(ETH_TYPE_IP) ||
540             flow->dl_type == htons(ETH_TYPE_IPV6));
541 }
542
543 static bool
544 is_icmpv4(const struct flow *flow)
545 {
546     return (flow->dl_type == htons(ETH_TYPE_IP)
547             && flow->nw_proto == IPPROTO_ICMP);
548 }
549
550 static bool
551 is_icmpv6(const struct flow *flow)
552 {
553     return (flow->dl_type == htons(ETH_TYPE_IPV6)
554             && flow->nw_proto == IPPROTO_ICMPV6);
555 }
556
557 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
558 bool
559 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
560 {
561     switch (mf->prereqs) {
562     case MFP_NONE:
563         return true;
564
565     case MFP_ARP:
566         return flow->dl_type == htons(ETH_TYPE_ARP);
567     case MFP_IPV4:
568         return flow->dl_type == htons(ETH_TYPE_IP);
569     case MFP_IPV6:
570         return flow->dl_type == htons(ETH_TYPE_IPV6);
571     case MFP_IP_ANY:
572         return is_ip_any(flow);
573
574     case MFP_TCP:
575         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
576     case MFP_UDP:
577         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
578     case MFP_ICMPV6:
579         return is_icmpv6(flow);
580     case MFP_ICMP_ANY:
581         return is_icmpv4(flow) || is_icmpv6(flow);
582
583     case MFP_ND:
584         return (is_icmpv6(flow)
585                 && flow->icmp_code == htons(0)
586                 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT) ||
587                     flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
588     case MFP_ND_SOLICIT:
589         return (is_icmpv6(flow)
590                 && flow->icmp_code == htons(0)
591                 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)));
592     case MFP_ND_ADVERT:
593         return (is_icmpv6(flow)
594                 && flow->icmp_code == htons(0)
595                 && (flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
596     }
597
598     NOT_REACHED();
599 }
600
601 /* Returns true if 'value' may be a valid value *as part of a masked match*,
602  * false otherwise.
603  *
604  * A value is not rejected just because it is not valid for the field in
605  * question, but only if it doesn't make sense to test the bits in question at
606  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
607  * without the VLAN_CFI bit being set, but we can't reject those values because
608  * it is still legitimate to test just for those bits (see the documentation
609  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
610  * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
611 bool
612 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
613 {
614     switch (mf->id) {
615     case MFF_TUN_ID:
616     case MFF_IN_PORT:
617 #if FLOW_N_REGS > 0
618     case MFF_REG0:
619 #endif
620 #if FLOW_N_REGS > 1
621     case MFF_REG1:
622 #endif
623 #if FLOW_N_REGS > 2
624     case MFF_REG2:
625 #endif
626 #if FLOW_N_REGS > 3
627     case MFF_REG3:
628 #endif
629 #if FLOW_N_REGS > 4
630 #error
631 #endif
632     case MFF_ETH_SRC:
633     case MFF_ETH_DST:
634     case MFF_ETH_TYPE:
635     case MFF_VLAN_TCI:
636     case MFF_IPV4_SRC:
637     case MFF_IPV4_DST:
638     case MFF_IPV6_SRC:
639     case MFF_IPV6_DST:
640     case MFF_IP_PROTO:
641     case MFF_ARP_SPA:
642     case MFF_ARP_TPA:
643     case MFF_ARP_SHA:
644     case MFF_ARP_THA:
645     case MFF_TCP_SRC:
646     case MFF_TCP_DST:
647     case MFF_UDP_SRC:
648     case MFF_UDP_DST:
649     case MFF_ICMP_TYPE:
650     case MFF_ICMP_CODE:
651     case MFF_ND_TARGET:
652     case MFF_ND_SLL:
653     case MFF_ND_TLL:
654         return true;
655
656     case MFF_IP_TOS:
657         return !(value->u8 & 0x03);
658
659     case MFF_ARP_OP:
660         return !(value->be16 & htons(0xff00));
661
662     case MFF_VLAN_VID:
663         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
664
665     case MFF_VLAN_PCP:
666         return !(value->u8 & ~7);
667
668     case MFF_N_IDS:
669     default:
670         NOT_REACHED();
671     }
672 }
673
674 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
675  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
676 void
677 mf_get_value(const struct mf_field *mf, const struct flow *flow,
678              union mf_value *value)
679 {
680     switch (mf->id) {
681     case MFF_TUN_ID:
682         value->be64 = flow->tun_id;
683         break;
684
685     case MFF_IN_PORT:
686         value->be16 = htons(flow->in_port);
687         break;
688
689 #if FLOW_N_REGS > 0
690     case MFF_REG0:
691 #endif
692 #if FLOW_N_REGS > 1
693     case MFF_REG1:
694 #endif
695 #if FLOW_N_REGS > 2
696     case MFF_REG2:
697 #endif
698 #if FLOW_N_REGS > 3
699     case MFF_REG3:
700 #endif
701 #if FLOW_N_REGS > 4
702 #error
703 #endif
704         value->be32 = htonl(flow->regs[0]);
705         break;
706
707     case MFF_ETH_SRC:
708         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
709         break;
710
711     case MFF_ETH_DST:
712         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
713         break;
714
715     case MFF_ETH_TYPE:
716         value->be16 = flow->dl_type;
717         break;
718
719     case MFF_VLAN_TCI:
720         value->be16 = flow->vlan_tci;
721         break;
722
723     case MFF_VLAN_VID:
724         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
725         break;
726
727     case MFF_VLAN_PCP:
728         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
729         break;
730
731     case MFF_IPV4_SRC:
732         value->be32 = flow->nw_src;
733         break;
734
735     case MFF_IPV4_DST:
736         value->be32 = flow->nw_dst;
737         break;
738
739     case MFF_IPV6_SRC:
740         value->ipv6 = flow->ipv6_src;
741         break;
742
743     case MFF_IPV6_DST:
744         value->ipv6 = flow->ipv6_dst;
745         break;
746
747     case MFF_IP_PROTO:
748         value->u8 = flow->nw_proto;
749         break;
750
751     case MFF_IP_TOS:
752         value->u8 = flow->nw_tos;
753         break;
754
755     case MFF_ARP_OP:
756         value->be16 = htons(flow->nw_proto);
757         break;
758
759     case MFF_ARP_SPA:
760         value->be32 = flow->nw_src;
761         break;
762
763     case MFF_ARP_TPA:
764         value->be32 = flow->nw_dst;
765         break;
766
767     case MFF_ARP_SHA:
768     case MFF_ND_SLL:
769         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
770         break;
771
772     case MFF_ARP_THA:
773     case MFF_ND_TLL:
774         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
775         break;
776
777     case MFF_TCP_SRC:
778         value->be16 = flow->tp_src;
779         break;
780
781     case MFF_TCP_DST:
782         value->be16 = flow->tp_dst;
783         break;
784
785     case MFF_UDP_SRC:
786         value->be16 = flow->tp_src;
787         break;
788
789     case MFF_UDP_DST:
790         value->be16 = flow->tp_dst;
791         break;
792
793     case MFF_ICMP_TYPE:
794         value->u8 = ntohs(flow->tp_src);
795         break;
796
797     case MFF_ICMP_CODE:
798         value->u8 = ntohs(flow->tp_dst);
799         break;
800
801     case MFF_ND_TARGET:
802         value->ipv6 = flow->nd_target;
803         break;
804
805     case MFF_N_IDS:
806     default:
807         NOT_REACHED();
808     }
809 }
810
811 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
812  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
813  * prerequisites. */
814 void
815 mf_set_value(const struct mf_field *mf,
816              const union mf_value *value, struct cls_rule *rule)
817 {
818     switch (mf->id) {
819     case MFF_TUN_ID:
820         cls_rule_set_tun_id(rule, value->be64);
821         break;
822
823     case MFF_IN_PORT:
824         cls_rule_set_in_port(rule, ntohs(value->be16));
825         break;
826
827 #if FLOW_N_REGS > 0
828     case MFF_REG0:
829 #endif
830 #if FLOW_N_REGS > 1
831     case MFF_REG1:
832 #endif
833 #if FLOW_N_REGS > 2
834     case MFF_REG2:
835 #endif
836 #if FLOW_N_REGS > 3
837     case MFF_REG3:
838 #endif
839 #if FLOW_N_REGS > 4
840 #error
841 #endif
842 #if FLOW_N_REGS > 0
843         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
844         break;
845 #endif
846
847     case MFF_ETH_SRC:
848         cls_rule_set_dl_src(rule, value->mac);
849         break;
850
851     case MFF_ETH_DST:
852         cls_rule_set_dl_dst(rule, value->mac);
853         break;
854
855     case MFF_ETH_TYPE:
856         cls_rule_set_dl_type(rule, value->be16);
857         break;
858
859     case MFF_VLAN_TCI:
860         cls_rule_set_dl_tci(rule, value->be16);
861         break;
862
863     case MFF_VLAN_VID:
864         cls_rule_set_dl_vlan(rule, value->be16);
865         break;
866
867     case MFF_VLAN_PCP:
868         cls_rule_set_dl_vlan_pcp(rule, value->u8);
869         break;
870
871     case MFF_IPV4_SRC:
872         cls_rule_set_nw_src(rule, value->be32);
873         break;
874
875     case MFF_IPV4_DST:
876         cls_rule_set_nw_dst(rule, value->be32);
877         break;
878
879     case MFF_IPV6_SRC:
880         cls_rule_set_ipv6_src(rule, &value->ipv6);
881         break;
882
883     case MFF_IPV6_DST:
884         cls_rule_set_ipv6_dst(rule, &value->ipv6);
885         break;
886
887     case MFF_IP_PROTO:
888         cls_rule_set_nw_proto(rule, value->u8);
889         break;
890
891     case MFF_IP_TOS:
892         cls_rule_set_nw_tos(rule, value->u8);
893         break;
894
895     case MFF_ARP_OP:
896         cls_rule_set_nw_proto(rule, ntohs(value->be16));
897         break;
898
899     case MFF_ARP_SPA:
900         cls_rule_set_nw_src(rule, value->be32);
901         break;
902
903     case MFF_ARP_TPA:
904         cls_rule_set_nw_dst(rule, value->be32);
905         break;
906
907     case MFF_ARP_SHA:
908     case MFF_ND_SLL:
909         cls_rule_set_arp_sha(rule, value->mac);
910         break;
911
912     case MFF_ARP_THA:
913     case MFF_ND_TLL:
914         cls_rule_set_arp_tha(rule, value->mac);
915         break;
916
917     case MFF_TCP_SRC:
918         cls_rule_set_tp_src(rule, value->be16);
919         break;
920
921     case MFF_TCP_DST:
922         cls_rule_set_tp_dst(rule, value->be16);
923         break;
924
925     case MFF_UDP_SRC:
926         cls_rule_set_tp_src(rule, value->be16);
927         break;
928
929     case MFF_UDP_DST:
930         cls_rule_set_tp_dst(rule, value->be16);
931         break;
932
933     case MFF_ICMP_TYPE:
934         cls_rule_set_icmp_type(rule, value->u8);
935         break;
936
937     case MFF_ICMP_CODE:
938         cls_rule_set_icmp_code(rule, value->u8);
939         break;
940
941     case MFF_ND_TARGET:
942         cls_rule_set_nd_target(rule, &value->ipv6);
943         break;
944
945     case MFF_N_IDS:
946     default:
947         NOT_REACHED();
948     }
949 }
950
951 /* Makes 'rule' wildcard field 'mf'.
952  *
953  * The caller is responsible for ensuring that 'rule' meets 'mf''s
954  * prerequisites. */
955 void
956 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
957 {
958     switch (mf->id) {
959     case MFF_TUN_ID:
960         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
961         break;
962
963     case MFF_IN_PORT:
964         rule->wc.wildcards |= FWW_IN_PORT;
965         rule->flow.in_port = 0;
966         break;
967
968 #if FLOW_N_REGS > 0
969     case MFF_REG0:
970         cls_rule_set_reg_masked(rule, 0, 0, 0);
971         break;
972 #endif
973 #if FLOW_N_REGS > 1
974     case MFF_REG1:
975         cls_rule_set_reg_masked(rule, 1, 0, 0);
976         break;
977 #endif
978 #if FLOW_N_REGS > 2
979     case MFF_REG2:
980         cls_rule_set_reg_masked(rule, 2, 0, 0);
981         break;
982 #endif
983 #if FLOW_N_REGS > 3
984     case MFF_REG3:
985         cls_rule_set_reg_masked(rule, 3, 0, 0);
986         break;
987 #endif
988 #if FLOW_N_REGS > 4
989 #error
990 #endif
991
992     case MFF_ETH_SRC:
993         rule->wc.wildcards |= FWW_DL_SRC;
994         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
995         break;
996
997     case MFF_ETH_DST:
998         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
999         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1000         break;
1001
1002     case MFF_ETH_TYPE:
1003         rule->wc.wildcards |= FWW_DL_TYPE;
1004         rule->flow.dl_type = htons(0);
1005         break;
1006
1007     case MFF_VLAN_TCI:
1008         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1009         break;
1010
1011     case MFF_VLAN_VID:
1012         cls_rule_set_any_vid(rule);
1013         break;
1014
1015     case MFF_VLAN_PCP:
1016         cls_rule_set_any_pcp(rule);
1017         break;
1018
1019     case MFF_IPV4_SRC:
1020     case MFF_ARP_SPA:
1021         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1022         break;
1023
1024     case MFF_IPV4_DST:
1025     case MFF_ARP_TPA:
1026         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1027         break;
1028
1029     case MFF_IPV6_SRC:
1030         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1031         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1032         break;
1033
1034     case MFF_IPV6_DST:
1035         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1036         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1037         break;
1038
1039     case MFF_IP_PROTO:
1040         rule->wc.wildcards |= FWW_NW_PROTO;
1041         rule->flow.nw_proto = 0;
1042         break;
1043
1044     case MFF_IP_TOS:
1045         rule->wc.wildcards |= FWW_NW_TOS;
1046         rule->flow.nw_tos = 0;
1047         break;
1048
1049     case MFF_ARP_OP:
1050         rule->wc.wildcards |= FWW_NW_PROTO;
1051         rule->flow.nw_proto = 0;
1052         break;
1053
1054     case MFF_ARP_SHA:
1055     case MFF_ND_SLL:
1056         rule->wc.wildcards |= FWW_ARP_SHA;
1057         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1058         break;
1059
1060     case MFF_ARP_THA:
1061     case MFF_ND_TLL:
1062         rule->wc.wildcards |= FWW_ARP_THA;
1063         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1064         break;
1065
1066     case MFF_TCP_SRC:
1067     case MFF_UDP_SRC:
1068     case MFF_ICMP_TYPE:
1069         rule->wc.wildcards |= FWW_TP_SRC;
1070         rule->flow.tp_src = htons(0);
1071         break;
1072
1073     case MFF_TCP_DST:
1074     case MFF_UDP_DST:
1075     case MFF_ICMP_CODE:
1076         rule->wc.wildcards |= FWW_TP_DST;
1077         rule->flow.tp_dst = htons(0);
1078         break;
1079
1080     case MFF_ND_TARGET:
1081         rule->wc.wildcards |= FWW_ND_TARGET;
1082         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1083         break;
1084
1085     case MFF_N_IDS:
1086     default:
1087         NOT_REACHED();
1088     }
1089 }
1090
1091 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1092  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1093  * with a 1-bit indicating that the corresponding value bit must match and a
1094  * 0-bit indicating a don't-care.
1095  *
1096  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1097  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1098  * call is equivalent to mf_set_wild(mf, rule).
1099  *
1100  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1101  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1102 void
1103 mf_set(const struct mf_field *mf,
1104        const union mf_value *value, const union mf_value *mask,
1105        struct cls_rule *rule)
1106 {
1107     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1108         mf_set_value(mf, value, rule);
1109         return;
1110     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1111         mf_set_wild(mf, rule);
1112         return;
1113     }
1114
1115     switch (mf->id) {
1116     case MFF_IN_PORT:
1117     case MFF_ETH_SRC:
1118     case MFF_ETH_TYPE:
1119     case MFF_VLAN_VID:
1120     case MFF_VLAN_PCP:
1121     case MFF_IP_PROTO:
1122     case MFF_IP_TOS:
1123     case MFF_ARP_OP:
1124     case MFF_ARP_SHA:
1125     case MFF_ARP_THA:
1126     case MFF_TCP_SRC:
1127     case MFF_TCP_DST:
1128     case MFF_UDP_SRC:
1129     case MFF_UDP_DST:
1130     case MFF_ICMP_TYPE:
1131     case MFF_ICMP_CODE:
1132     case MFF_ND_TARGET:
1133     case MFF_ND_SLL:
1134     case MFF_ND_TLL:
1135         NOT_REACHED();
1136
1137     case MFF_TUN_ID:
1138         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1139         break;
1140
1141 #if FLOW_N_REGS > 0
1142     case MFF_REG0:
1143 #endif
1144 #if FLOW_N_REGS > 1
1145     case MFF_REG1:
1146 #endif
1147 #if FLOW_N_REGS > 2
1148     case MFF_REG2:
1149 #endif
1150 #if FLOW_N_REGS > 3
1151     case MFF_REG3:
1152 #endif
1153 #if FLOW_N_REGS > 4
1154 #error
1155 #endif
1156         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1157                                 ntohl(value->be32), ntohl(mask->be32));
1158         break;
1159
1160     case MFF_ETH_DST:
1161         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1162             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1163         }
1164         break;
1165
1166     case MFF_VLAN_TCI:
1167         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1168         break;
1169
1170     case MFF_IPV4_SRC:
1171         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1172         break;
1173
1174     case MFF_IPV4_DST:
1175         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1176         break;
1177
1178     case MFF_IPV6_SRC:
1179         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1180         break;
1181
1182     case MFF_IPV6_DST:
1183         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1184         break;
1185
1186     case MFF_ARP_SPA:
1187         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1188         break;
1189
1190     case MFF_ARP_TPA:
1191         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1192         break;
1193
1194     case MFF_N_IDS:
1195     default:
1196         NOT_REACHED();
1197     }
1198 }
1199
1200 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1201  * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1202  * 'x'.
1203  *
1204  * Example: suppose that 'mf' is originally the following 2-byte field in
1205  * 'rule':
1206  *
1207  *     value == 0xe00a == 2#1110000000001010
1208  *      mask == 0xfc3f == 2#1111110000111111
1209  *
1210  * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1211  * effect (note that 0x55 is 2#1010101):
1212  *
1213  *     value == 0xd50a == 2#1101010100001010
1214  *      mask == 0xff3f == 2#1111111100111111
1215  *
1216  * The caller is responsible for ensuring that the result will be a valid
1217  * wildcard pattern for 'mf'.  The caller is responsible for ensuring that
1218  * 'rule' meets 'mf''s prerequisites. */
1219 void
1220 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1221                 unsigned int n_bits, struct cls_rule *rule)
1222 {
1223     if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1224         union mf_value value;
1225         int i;
1226
1227         for (i = mf->n_bytes - 1; i >= 0; i--) {
1228             ((uint8_t *) &value)[i] = x;
1229             x >>= 8;
1230         }
1231         mf_set_value(mf, &value, rule);
1232     } else {
1233         union mf_value value, mask;
1234         uint8_t *vp, *mp;
1235         unsigned int byte_ofs;
1236
1237         mf_get(mf, rule, &value, &mask);
1238
1239         byte_ofs = mf->n_bytes - ofs / 8;
1240         vp = &((uint8_t *) &value)[byte_ofs];
1241         mp = &((uint8_t *) &mask)[byte_ofs];
1242         if (ofs % 8) {
1243             unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1244             uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1245
1246             *--vp &= ~chunk_mask;
1247             *vp   |= chunk_mask & (x << (ofs % 8));
1248             *--mp |= chunk_mask;
1249
1250             x >>= chunk;
1251             n_bits -= chunk;
1252             ofs += chunk;
1253         }
1254         while (n_bits >= 8) {
1255             *--vp = x;
1256             *--mp = 0xff;
1257             x >>= 8;
1258             n_bits -= 8;
1259             ofs += 8;
1260         }
1261         if (n_bits) {
1262             uint8_t chunk_mask = (1 << n_bits) - 1;
1263
1264             *--vp &= ~chunk_mask;
1265             *vp   |= chunk_mask & x;
1266             *--mp |= chunk_mask;
1267         }
1268
1269         mf_set(mf, &value, &mask, rule);
1270     }
1271 }
1272
1273 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1274  * 'value' and 'mask', respectively. */
1275 void
1276 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1277        union mf_value *value, union mf_value *mask)
1278 {
1279     mf_get_value(mf, &rule->flow, value);
1280     mf_get_mask(mf, &rule->wc, mask);
1281 }
1282
1283 /* Assigns a random value for field 'mf' to 'value'. */
1284 void
1285 mf_random_value(const struct mf_field *mf, union mf_value *value)
1286 {
1287     random_bytes(value, mf->n_bytes);
1288
1289     switch (mf->id) {
1290     case MFF_TUN_ID:
1291     case MFF_IN_PORT:
1292 #if FLOW_N_REGS > 0
1293     case MFF_REG0:
1294 #endif
1295 #if FLOW_N_REGS > 1
1296     case MFF_REG1:
1297 #endif
1298 #if FLOW_N_REGS > 2
1299     case MFF_REG2:
1300 #endif
1301 #if FLOW_N_REGS > 3
1302     case MFF_REG3:
1303 #endif
1304 #if FLOW_N_REGS > 4
1305 #error
1306 #endif
1307     case MFF_ETH_SRC:
1308     case MFF_ETH_DST:
1309     case MFF_ETH_TYPE:
1310     case MFF_VLAN_TCI:
1311     case MFF_IPV4_SRC:
1312     case MFF_IPV4_DST:
1313     case MFF_IPV6_SRC:
1314     case MFF_IPV6_DST:
1315     case MFF_IP_PROTO:
1316     case MFF_ARP_SPA:
1317     case MFF_ARP_TPA:
1318     case MFF_ARP_SHA:
1319     case MFF_ARP_THA:
1320     case MFF_TCP_SRC:
1321     case MFF_TCP_DST:
1322     case MFF_UDP_SRC:
1323     case MFF_UDP_DST:
1324     case MFF_ICMP_TYPE:
1325     case MFF_ICMP_CODE:
1326     case MFF_ND_TARGET:
1327     case MFF_ND_SLL:
1328     case MFF_ND_TLL:
1329         break;
1330
1331     case MFF_IP_TOS:
1332         value->u8 &= ~0x03;
1333         break;
1334
1335     case MFF_ARP_OP:
1336         value->be16 &= htons(0xff);
1337         break;
1338
1339     case MFF_VLAN_VID:
1340         value->be16 &= htons(VLAN_VID_MASK);
1341         break;
1342
1343     case MFF_VLAN_PCP:
1344         value->u8 &= 0x07;
1345         break;
1346
1347     case MFF_N_IDS:
1348     default:
1349         NOT_REACHED();
1350     }
1351 }
1352
1353 static char *
1354 mf_from_integer_string(const struct mf_field *mf, const char *s,
1355                        uint8_t *valuep, uint8_t *maskp)
1356 {
1357     unsigned long long int integer, mask;
1358     char *tail;
1359     int i;
1360
1361     errno = 0;
1362     integer = strtoull(s, &tail, 0);
1363     if (errno || (*tail != '\0' && *tail != '/')) {
1364         goto syntax_error;
1365     }
1366
1367     if (*tail == '/') {
1368         mask = strtoull(tail + 1, &tail, 0);
1369         if (errno || *tail != '\0') {
1370             goto syntax_error;
1371         }
1372     } else {
1373         mask = ULLONG_MAX;
1374     }
1375
1376     for (i = mf->n_bytes - 1; i >= 0; i--) {
1377         valuep[i] = integer;
1378         maskp[i] = mask;
1379         integer >>= 8;
1380         mask >>= 8;
1381     }
1382     if (integer) {
1383         return xasprintf("%s: value too large for %u-byte field %s",
1384                          s, mf->n_bytes, mf->name);
1385     }
1386     return NULL;
1387
1388 syntax_error:
1389     return xasprintf("%s: bad syntax for %s", s, mf->name);
1390 }
1391
1392 static char *
1393 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1394                         uint8_t mac[ETH_ADDR_LEN],
1395                         uint8_t mask[ETH_ADDR_LEN])
1396 {
1397     assert(mf->n_bytes == ETH_ADDR_LEN);
1398
1399     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1400                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1401     case ETH_ADDR_SCAN_COUNT * 2:
1402         return NULL;
1403
1404     case ETH_ADDR_SCAN_COUNT:
1405         memset(mask, 0xff, ETH_ADDR_LEN);
1406         return NULL;
1407
1408     default:
1409         return xasprintf("%s: invalid Ethernet address", s);
1410     }
1411 }
1412
1413 static char *
1414 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1415                     ovs_be32 *ip, ovs_be32 *mask)
1416 {
1417     int prefix;
1418
1419     assert(mf->n_bytes == sizeof *ip);
1420
1421     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1422                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1423         /* OK. */
1424     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1425                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1426         if (prefix <= 0 || prefix > 32) {
1427             return xasprintf("%s: network prefix bits not between 1 and "
1428                              "32", s);
1429         } else if (prefix == 32) {
1430             *mask = htonl(UINT32_MAX);
1431         } else {
1432             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1433         }
1434     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1435         *mask = htonl(UINT32_MAX);
1436     } else {
1437         return xasprintf("%s: invalid IP address", s);
1438     }
1439     return NULL;
1440 }
1441
1442 static char *
1443 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1444                     struct in6_addr *value, struct in6_addr *mask)
1445 {
1446     char *str = xstrdup(s);
1447     char *save_ptr = NULL;
1448     const char *name, *netmask;
1449     int retval;
1450
1451     assert(mf->n_bytes == sizeof *value);
1452
1453     name = strtok_r(str, "/", &save_ptr);
1454     retval = name ? lookup_ipv6(name, value) : EINVAL;
1455     if (retval) {
1456         char *err;
1457
1458         err = xasprintf("%s: could not convert to IPv6 address", str);
1459         free(str);
1460
1461         return err;
1462     }
1463
1464     netmask = strtok_r(NULL, "/", &save_ptr);
1465     if (netmask) {
1466         int prefix = atoi(netmask);
1467         if (prefix <= 0 || prefix > 128) {
1468             free(str);
1469             return xasprintf("%s: prefix bits not between 1 and 128", s);
1470         } else {
1471             *mask = ipv6_create_mask(prefix);
1472         }
1473     } else {
1474         *mask = in6addr_exact;
1475     }
1476     free(str);
1477
1478     return NULL;
1479 }
1480
1481 static char *
1482 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1483                         ovs_be16 *valuep, ovs_be16 *maskp)
1484 {
1485     uint16_t port;
1486
1487     assert(mf->n_bytes == sizeof(ovs_be16));
1488     if (ofputil_port_from_string(s, &port)) {
1489         *valuep = htons(port);
1490         *maskp = htons(UINT16_MAX);
1491         return NULL;
1492     } else {
1493         return mf_from_integer_string(mf, s,
1494                                       (uint8_t *) valuep, (uint8_t *) maskp);
1495     }
1496 }
1497
1498 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1499  * NULL if successful, otherwise a malloc()'d string describing the error. */
1500 char *
1501 mf_parse(const struct mf_field *mf, const char *s,
1502          union mf_value *value, union mf_value *mask)
1503 {
1504     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1505         memset(value, 0, mf->n_bytes);
1506         memset(mask, 0, mf->n_bytes);
1507         return NULL;
1508     }
1509
1510     switch (mf->string) {
1511     case MFS_DECIMAL:
1512     case MFS_HEXADECIMAL:
1513         return mf_from_integer_string(mf, s,
1514                                       (uint8_t *) value, (uint8_t *) mask);
1515
1516     case MFS_ETHERNET:
1517         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1518
1519     case MFS_IPV4:
1520         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1521
1522     case MFS_IPV6:
1523         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1524
1525     case MFS_OFP_PORT:
1526         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1527     }
1528     NOT_REACHED();
1529 }
1530
1531 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1532  * successful, otherwise a malloc()'d string describing the error. */
1533 char *
1534 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1535 {
1536     union mf_value mask;
1537     char *error;
1538
1539     error = mf_parse(mf, s, value, &mask);
1540     if (error) {
1541         return error;
1542     }
1543
1544     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1545         return xasprintf("%s: wildcards not allowed here", s);
1546     }
1547     return NULL;
1548 }
1549
1550 static void
1551 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1552                          const uint8_t *maskp, struct ds *s)
1553 {
1554     unsigned long long int integer;
1555     int i;
1556
1557     assert(mf->n_bytes <= 8);
1558
1559     integer = 0;
1560     for (i = 0; i < mf->n_bytes; i++) {
1561         integer = (integer << 8) | valuep[i];
1562     }
1563     if (mf->string == MFS_HEXADECIMAL) {
1564         ds_put_format(s, "%#llx", integer);
1565     } else {
1566         ds_put_format(s, "%lld", integer);
1567     }
1568
1569     if (maskp) {
1570         unsigned long long int mask;
1571
1572         mask = 0;
1573         for (i = 0; i < mf->n_bytes; i++) {
1574             mask = (mask << 8) | maskp[i];
1575         }
1576
1577         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1578          * not sure that that a bit-mask written in decimal is ever easier to
1579          * understand than the same bit-mask written in hexadecimal. */
1580         ds_put_format(s, "/%#llx", mask);
1581     }
1582 }
1583
1584 /* Appends to 's' a string representation of field 'mf' whose value is in
1585  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
1586 void
1587 mf_format(const struct mf_field *mf,
1588           const union mf_value *value, const union mf_value *mask,
1589           struct ds *s)
1590 {
1591     if (mask) {
1592         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1593             ds_put_cstr(s, "ANY");
1594             return;
1595         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1596             mask = NULL;
1597         }
1598     }
1599
1600     switch (mf->string) {
1601     case MFS_OFP_PORT:
1602         if (!mask) {
1603             ofputil_format_port(ntohs(value->be16), s);
1604             break;
1605         }
1606         /* fall through */
1607     case MFS_DECIMAL:
1608     case MFS_HEXADECIMAL:
1609         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1610         break;
1611
1612     case MFS_ETHERNET:
1613         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1614         if (mask) {
1615             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1616         }
1617         break;
1618
1619     case MFS_IPV4:
1620         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1621                          s);
1622         break;
1623
1624     case MFS_IPV6:
1625         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1626         break;
1627
1628     default:
1629         NOT_REACHED();
1630     }
1631 }