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