ed0041f88756d2086b996432e5f09cbed12728ee
[cascardo/ovs.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "meta-flow.h"
20
21 #include <errno.h>
22 #include <limits.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
25
26 #include "classifier.h"
27 #include "dynamic-string.h"
28 #include "nx-match.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "ovs-thread.h"
32 #include "packets.h"
33 #include "random.h"
34 #include "shash.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
38 #include "util.h"
39 #include "openvswitch/vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
42
43 #define FLOW_U32OFS(FIELD)                                              \
44     offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
45
46 #define MF_FIELD_SIZES(MEMBER)                  \
47     sizeof ((union mf_value *)0)->MEMBER,       \
48     8 * sizeof ((union mf_value *)0)->MEMBER
49
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
51
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
54 };
55
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
58
59 /* Rate limit for parse errors.  These always indicate a bug in an OpenFlow
60  * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
62
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff,  0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
69
70 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
71
72 static void nxm_init(void);
73
74 /* Returns the field with the given 'name', or a null pointer if no field has
75  * that name. */
76 const struct mf_field *
77 mf_from_name(const char *name)
78 {
79     nxm_init();
80     return shash_find_data(&mf_by_name, name);
81 }
82
83 static void
84 nxm_do_init(void)
85 {
86     int i;
87
88     shash_init(&mf_by_name);
89     for (i = 0; i < MFF_N_IDS; i++) {
90         const struct mf_field *mf = &mf_fields[i];
91
92         ovs_assert(mf->id == i); /* Fields must be in the enum order. */
93
94         shash_add_once(&mf_by_name, mf->name, mf);
95         if (mf->extra_name) {
96             shash_add_once(&mf_by_name, mf->extra_name, mf);
97         }
98     }
99 }
100
101 static void
102 nxm_init(void)
103 {
104     static pthread_once_t once = PTHREAD_ONCE_INIT;
105     pthread_once(&once, nxm_do_init);
106 }
107
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109  * restrictions on a field's value.  Then, this function initializes
110  * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111  * This is not always possible, i.e. if one pair insists on a value of 0 in
112  * some bit and the other pair insists on a value of 1 in that bit.  This
113  * function returns false in a case where the combined restriction is
114  * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
115  * true otherwise.
116  *
117  * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118  * a corresponding 1-bit in its mask.) */
119 bool
120 mf_subvalue_intersect(const union mf_subvalue *a_value,
121                       const union mf_subvalue *a_mask,
122                       const union mf_subvalue *b_value,
123                       const union mf_subvalue *b_mask,
124                       union mf_subvalue *dst_value,
125                       union mf_subvalue *dst_mask)
126 {
127     for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
128         ovs_be64 av = a_value->be64[i];
129         ovs_be64 am = a_mask->be64[i];
130         ovs_be64 bv = b_value->be64[i];
131         ovs_be64 bm = b_mask->be64[i];
132         ovs_be64 *dv = &dst_value->be64[i];
133         ovs_be64 *dm = &dst_mask->be64[i];
134
135         if ((av ^ bv) & (am & bm)) {
136             return false;
137         }
138         *dv = av | bv;
139         *dm = am | bm;
140     }
141     return true;
142 }
143
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145  * set, 2 if the second-lowest-order bit is set, and so on. */
146 int
147 mf_subvalue_width(const union mf_subvalue *v)
148 {
149     return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
150 }
151
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153  * negative 'n', shifts the bits '-n' bits to the right. */
154 void
155 mf_subvalue_shift(union mf_subvalue *value, int n)
156 {
157     if (n) {
158         union mf_subvalue tmp;
159         memset(&tmp, 0, sizeof tmp);
160
161         if (n > 0 && n < 8 * sizeof tmp) {
162             bitwise_copy(value, sizeof *value, 0,
163                          &tmp, sizeof tmp, n,
164                          8 * sizeof tmp - n);
165         } else if (n < 0 && n > -8 * sizeof tmp) {
166             bitwise_copy(value, sizeof *value, -n,
167                          &tmp, sizeof tmp, 0,
168                          8 * sizeof tmp + n);
169         }
170         *value = tmp;
171     }
172 }
173
174 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
175  * specifies at least one bit in the field.
176  *
177  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
178  * meets 'mf''s prerequisites. */
179 bool
180 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
181 {
182     switch (mf->id) {
183     case MFF_DP_HASH:
184         return !wc->masks.dp_hash;
185     case MFF_RECIRC_ID:
186         return !wc->masks.recirc_id;
187     case MFF_CONJ_ID:
188         return !wc->masks.conj_id;
189     case MFF_TUN_SRC:
190         return !wc->masks.tunnel.ip_src;
191     case MFF_TUN_DST:
192         return !wc->masks.tunnel.ip_dst;
193     case MFF_TUN_ID:
194         return !wc->masks.tunnel.tun_id;
195     case MFF_TUN_TOS:
196         return !wc->masks.tunnel.ip_tos;
197     case MFF_TUN_TTL:
198         return !wc->masks.tunnel.ip_ttl;
199     case MFF_TUN_FLAGS:
200         return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
201     case MFF_TUN_GBP_ID:
202         return !wc->masks.tunnel.gbp_id;
203     case MFF_TUN_GBP_FLAGS:
204         return !wc->masks.tunnel.gbp_flags;
205     CASE_MFF_TUN_METADATA: {
206         union mf_value value;
207
208         tun_metadata_read(&wc->masks.tunnel, mf, &value);
209         return is_all_zeros(&value.tun_metadata, mf->n_bytes);
210     }
211     case MFF_METADATA:
212         return !wc->masks.metadata;
213     case MFF_IN_PORT:
214     case MFF_IN_PORT_OXM:
215         return !wc->masks.in_port.ofp_port;
216     case MFF_SKB_PRIORITY:
217         return !wc->masks.skb_priority;
218     case MFF_PKT_MARK:
219         return !wc->masks.pkt_mark;
220     CASE_MFF_REGS:
221         return !wc->masks.regs[mf->id - MFF_REG0];
222     CASE_MFF_XREGS:
223         return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
224     case MFF_ACTSET_OUTPUT:
225         return !wc->masks.actset_output;
226
227     case MFF_ETH_SRC:
228         return eth_addr_is_zero(wc->masks.dl_src);
229     case MFF_ETH_DST:
230         return eth_addr_is_zero(wc->masks.dl_dst);
231     case MFF_ETH_TYPE:
232         return !wc->masks.dl_type;
233
234     case MFF_ARP_SHA:
235     case MFF_ND_SLL:
236         return eth_addr_is_zero(wc->masks.arp_sha);
237
238     case MFF_ARP_THA:
239     case MFF_ND_TLL:
240         return eth_addr_is_zero(wc->masks.arp_tha);
241
242     case MFF_VLAN_TCI:
243         return !wc->masks.vlan_tci;
244     case MFF_DL_VLAN:
245         return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
246     case MFF_VLAN_VID:
247         return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
248     case MFF_DL_VLAN_PCP:
249     case MFF_VLAN_PCP:
250         return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
251
252     case MFF_MPLS_LABEL:
253         return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
254     case MFF_MPLS_TC:
255         return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
256     case MFF_MPLS_BOS:
257         return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
258
259     case MFF_IPV4_SRC:
260         return !wc->masks.nw_src;
261     case MFF_IPV4_DST:
262         return !wc->masks.nw_dst;
263
264     case MFF_IPV6_SRC:
265         return ipv6_mask_is_any(&wc->masks.ipv6_src);
266     case MFF_IPV6_DST:
267         return ipv6_mask_is_any(&wc->masks.ipv6_dst);
268
269     case MFF_IPV6_LABEL:
270         return !wc->masks.ipv6_label;
271
272     case MFF_IP_PROTO:
273         return !wc->masks.nw_proto;
274     case MFF_IP_DSCP:
275     case MFF_IP_DSCP_SHIFTED:
276         return !(wc->masks.nw_tos & IP_DSCP_MASK);
277     case MFF_IP_ECN:
278         return !(wc->masks.nw_tos & IP_ECN_MASK);
279     case MFF_IP_TTL:
280         return !wc->masks.nw_ttl;
281
282     case MFF_ND_TARGET:
283         return ipv6_mask_is_any(&wc->masks.nd_target);
284
285     case MFF_IP_FRAG:
286         return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
287
288     case MFF_ARP_OP:
289         return !wc->masks.nw_proto;
290     case MFF_ARP_SPA:
291         return !wc->masks.nw_src;
292     case MFF_ARP_TPA:
293         return !wc->masks.nw_dst;
294
295     case MFF_TCP_SRC:
296     case MFF_UDP_SRC:
297     case MFF_SCTP_SRC:
298     case MFF_ICMPV4_TYPE:
299     case MFF_ICMPV6_TYPE:
300         return !wc->masks.tp_src;
301     case MFF_TCP_DST:
302     case MFF_UDP_DST:
303     case MFF_SCTP_DST:
304     case MFF_ICMPV4_CODE:
305     case MFF_ICMPV6_CODE:
306         return !wc->masks.tp_dst;
307     case MFF_TCP_FLAGS:
308         return !wc->masks.tcp_flags;
309
310     case MFF_N_IDS:
311     default:
312         OVS_NOT_REACHED();
313     }
314 }
315
316 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
317  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
318  * purposes, or to 0 if it is wildcarded.
319  *
320  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
321  * meets 'mf''s prerequisites. */
322 void
323 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
324             union mf_value *mask)
325 {
326     mf_get_value(mf, &wc->masks, mask);
327 }
328
329 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
330  * if the mask is valid, false otherwise. */
331 bool
332 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
333 {
334     switch (mf->maskable) {
335     case MFM_NONE:
336         return (is_all_zeros(mask, mf->n_bytes) ||
337                 is_all_ones(mask, mf->n_bytes));
338
339     case MFM_FULLY:
340         return true;
341     }
342
343     OVS_NOT_REACHED();
344 }
345
346 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
347 bool
348 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
349 {
350     switch (mf->prereqs) {
351     case MFP_NONE:
352         return true;
353
354     case MFP_ARP:
355       return (flow->dl_type == htons(ETH_TYPE_ARP) ||
356               flow->dl_type == htons(ETH_TYPE_RARP));
357     case MFP_IPV4:
358         return flow->dl_type == htons(ETH_TYPE_IP);
359     case MFP_IPV6:
360         return flow->dl_type == htons(ETH_TYPE_IPV6);
361     case MFP_VLAN_VID:
362         return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
363     case MFP_MPLS:
364         return eth_type_mpls(flow->dl_type);
365     case MFP_IP_ANY:
366         return is_ip_any(flow);
367
368     case MFP_TCP:
369         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
370             && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
371     case MFP_UDP:
372         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
373             && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
374     case MFP_SCTP:
375         return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
376             && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
377     case MFP_ICMPV4:
378         return is_icmpv4(flow);
379     case MFP_ICMPV6:
380         return is_icmpv6(flow);
381
382     case MFP_ND:
383         return (is_icmpv6(flow)
384                 && flow->tp_dst == htons(0)
385                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
386                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
387     case MFP_ND_SOLICIT:
388         return (is_icmpv6(flow)
389                 && flow->tp_dst == htons(0)
390                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
391     case MFP_ND_ADVERT:
392         return (is_icmpv6(flow)
393                 && flow->tp_dst == htons(0)
394                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
395     }
396
397     OVS_NOT_REACHED();
398 }
399
400 /* Set field and it's prerequisities in the mask.
401  * This is only ever called for writeable 'mf's, but we do not make the
402  * distinction here. */
403 void
404 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
405 {
406     mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
407
408     switch (mf->prereqs) {
409     case MFP_ND:
410     case MFP_ND_SOLICIT:
411     case MFP_ND_ADVERT:
412         WC_MASK_FIELD(wc, tp_src);
413         WC_MASK_FIELD(wc, tp_dst);
414         /* Fall through. */
415     case MFP_TCP:
416     case MFP_UDP:
417     case MFP_SCTP:
418     case MFP_ICMPV4:
419     case MFP_ICMPV6:
420         /* nw_frag always unwildcarded. */
421         WC_MASK_FIELD(wc, nw_proto);
422         /* Fall through. */
423     case MFP_ARP:
424     case MFP_IPV4:
425     case MFP_IPV6:
426     case MFP_MPLS:
427     case MFP_IP_ANY:
428         /* dl_type always unwildcarded. */
429         break;
430     case MFP_VLAN_VID:
431         WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
432         break;
433     case MFP_NONE:
434         break;
435     }
436 }
437
438 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
439 void
440 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
441 {
442     bitmap_set1(bm->bm, mf->id);
443
444     switch (mf->prereqs) {
445     case MFP_ND:
446     case MFP_ND_SOLICIT:
447     case MFP_ND_ADVERT:
448         bitmap_set1(bm->bm, MFF_TCP_SRC);
449         bitmap_set1(bm->bm, MFF_TCP_DST);
450         /* Fall through. */
451     case MFP_TCP:
452     case MFP_UDP:
453     case MFP_SCTP:
454     case MFP_ICMPV4:
455     case MFP_ICMPV6:
456         /* nw_frag always unwildcarded. */
457         bitmap_set1(bm->bm, MFF_IP_PROTO);
458         /* Fall through. */
459     case MFP_ARP:
460     case MFP_IPV4:
461     case MFP_IPV6:
462     case MFP_MPLS:
463     case MFP_IP_ANY:
464         bitmap_set1(bm->bm, MFF_ETH_TYPE);
465         break;
466     case MFP_VLAN_VID:
467         bitmap_set1(bm->bm, MFF_VLAN_TCI);
468         break;
469     case MFP_NONE:
470         break;
471     }
472 }
473
474 /* Returns true if 'value' may be a valid value *as part of a masked match*,
475  * false otherwise.
476  *
477  * A value is not rejected just because it is not valid for the field in
478  * question, but only if it doesn't make sense to test the bits in question at
479  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
480  * without the VLAN_CFI bit being set, but we can't reject those values because
481  * it is still legitimate to test just for those bits (see the documentation
482  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
483  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
484 bool
485 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
486 {
487     switch (mf->id) {
488     case MFF_DP_HASH:
489     case MFF_RECIRC_ID:
490     case MFF_CONJ_ID:
491     case MFF_TUN_ID:
492     case MFF_TUN_SRC:
493     case MFF_TUN_DST:
494     case MFF_TUN_TOS:
495     case MFF_TUN_TTL:
496     case MFF_TUN_GBP_ID:
497     case MFF_TUN_GBP_FLAGS:
498     CASE_MFF_TUN_METADATA:
499     case MFF_METADATA:
500     case MFF_IN_PORT:
501     case MFF_SKB_PRIORITY:
502     case MFF_PKT_MARK:
503     CASE_MFF_REGS:
504     CASE_MFF_XREGS:
505     case MFF_ETH_SRC:
506     case MFF_ETH_DST:
507     case MFF_ETH_TYPE:
508     case MFF_VLAN_TCI:
509     case MFF_IPV4_SRC:
510     case MFF_IPV4_DST:
511     case MFF_IPV6_SRC:
512     case MFF_IPV6_DST:
513     case MFF_IP_PROTO:
514     case MFF_IP_TTL:
515     case MFF_ARP_SPA:
516     case MFF_ARP_TPA:
517     case MFF_ARP_SHA:
518     case MFF_ARP_THA:
519     case MFF_TCP_SRC:
520     case MFF_TCP_DST:
521     case MFF_UDP_SRC:
522     case MFF_UDP_DST:
523     case MFF_SCTP_SRC:
524     case MFF_SCTP_DST:
525     case MFF_ICMPV4_TYPE:
526     case MFF_ICMPV4_CODE:
527     case MFF_ICMPV6_TYPE:
528     case MFF_ICMPV6_CODE:
529     case MFF_ND_TARGET:
530     case MFF_ND_SLL:
531     case MFF_ND_TLL:
532         return true;
533
534     case MFF_IN_PORT_OXM:
535     case MFF_ACTSET_OUTPUT: {
536         ofp_port_t port;
537         return !ofputil_port_from_ofp11(value->be32, &port);
538     }
539
540     case MFF_IP_DSCP:
541         return !(value->u8 & ~IP_DSCP_MASK);
542     case MFF_IP_DSCP_SHIFTED:
543         return !(value->u8 & (~IP_DSCP_MASK >> 2));
544     case MFF_IP_ECN:
545         return !(value->u8 & ~IP_ECN_MASK);
546     case MFF_IP_FRAG:
547         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
548     case MFF_TCP_FLAGS:
549         return !(value->be16 & ~htons(0x0fff));
550
551     case MFF_ARP_OP:
552         return !(value->be16 & htons(0xff00));
553
554     case MFF_DL_VLAN:
555         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
556     case MFF_VLAN_VID:
557         return !(value->be16 & htons(VLAN_PCP_MASK));
558
559     case MFF_DL_VLAN_PCP:
560     case MFF_VLAN_PCP:
561         return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
562
563     case MFF_IPV6_LABEL:
564         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
565
566     case MFF_MPLS_LABEL:
567         return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
568
569     case MFF_MPLS_TC:
570         return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
571
572     case MFF_MPLS_BOS:
573         return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
574
575     case MFF_TUN_FLAGS:
576         return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
577
578     case MFF_N_IDS:
579     default:
580         OVS_NOT_REACHED();
581     }
582 }
583
584 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
585  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
586 void
587 mf_get_value(const struct mf_field *mf, const struct flow *flow,
588              union mf_value *value)
589 {
590     switch (mf->id) {
591     case MFF_DP_HASH:
592         value->be32 = htonl(flow->dp_hash);
593         break;
594     case MFF_RECIRC_ID:
595         value->be32 = htonl(flow->recirc_id);
596         break;
597     case MFF_CONJ_ID:
598         value->be32 = htonl(flow->conj_id);
599         break;
600     case MFF_TUN_ID:
601         value->be64 = flow->tunnel.tun_id;
602         break;
603     case MFF_TUN_SRC:
604         value->be32 = flow->tunnel.ip_src;
605         break;
606     case MFF_TUN_DST:
607         value->be32 = flow->tunnel.ip_dst;
608         break;
609     case MFF_TUN_FLAGS:
610         value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
611         break;
612     case MFF_TUN_GBP_ID:
613         value->be16 = flow->tunnel.gbp_id;
614         break;
615     case MFF_TUN_GBP_FLAGS:
616         value->u8 = flow->tunnel.gbp_flags;
617         break;
618     case MFF_TUN_TTL:
619         value->u8 = flow->tunnel.ip_ttl;
620         break;
621     case MFF_TUN_TOS:
622         value->u8 = flow->tunnel.ip_tos;
623         break;
624     CASE_MFF_TUN_METADATA:
625         tun_metadata_read(&flow->tunnel, mf, value);
626         break;
627
628     case MFF_METADATA:
629         value->be64 = flow->metadata;
630         break;
631
632     case MFF_IN_PORT:
633         value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
634         break;
635     case MFF_IN_PORT_OXM:
636         value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
637         break;
638     case MFF_ACTSET_OUTPUT:
639         value->be32 = ofputil_port_to_ofp11(flow->actset_output);
640         break;
641
642     case MFF_SKB_PRIORITY:
643         value->be32 = htonl(flow->skb_priority);
644         break;
645
646     case MFF_PKT_MARK:
647         value->be32 = htonl(flow->pkt_mark);
648         break;
649
650     CASE_MFF_REGS:
651         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
652         break;
653
654     CASE_MFF_XREGS:
655         value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
656         break;
657
658     case MFF_ETH_SRC:
659         value->mac = flow->dl_src;
660         break;
661
662     case MFF_ETH_DST:
663         value->mac = flow->dl_dst;
664         break;
665
666     case MFF_ETH_TYPE:
667         value->be16 = flow->dl_type;
668         break;
669
670     case MFF_VLAN_TCI:
671         value->be16 = flow->vlan_tci;
672         break;
673
674     case MFF_DL_VLAN:
675         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
676         break;
677     case MFF_VLAN_VID:
678         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
679         break;
680
681     case MFF_DL_VLAN_PCP:
682     case MFF_VLAN_PCP:
683         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
684         break;
685
686     case MFF_MPLS_LABEL:
687         value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
688         break;
689
690     case MFF_MPLS_TC:
691         value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
692         break;
693
694     case MFF_MPLS_BOS:
695         value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
696         break;
697
698     case MFF_IPV4_SRC:
699         value->be32 = flow->nw_src;
700         break;
701
702     case MFF_IPV4_DST:
703         value->be32 = flow->nw_dst;
704         break;
705
706     case MFF_IPV6_SRC:
707         value->ipv6 = flow->ipv6_src;
708         break;
709
710     case MFF_IPV6_DST:
711         value->ipv6 = flow->ipv6_dst;
712         break;
713
714     case MFF_IPV6_LABEL:
715         value->be32 = flow->ipv6_label;
716         break;
717
718     case MFF_IP_PROTO:
719         value->u8 = flow->nw_proto;
720         break;
721
722     case MFF_IP_DSCP:
723         value->u8 = flow->nw_tos & IP_DSCP_MASK;
724         break;
725
726     case MFF_IP_DSCP_SHIFTED:
727         value->u8 = flow->nw_tos >> 2;
728         break;
729
730     case MFF_IP_ECN:
731         value->u8 = flow->nw_tos & IP_ECN_MASK;
732         break;
733
734     case MFF_IP_TTL:
735         value->u8 = flow->nw_ttl;
736         break;
737
738     case MFF_IP_FRAG:
739         value->u8 = flow->nw_frag;
740         break;
741
742     case MFF_ARP_OP:
743         value->be16 = htons(flow->nw_proto);
744         break;
745
746     case MFF_ARP_SPA:
747         value->be32 = flow->nw_src;
748         break;
749
750     case MFF_ARP_TPA:
751         value->be32 = flow->nw_dst;
752         break;
753
754     case MFF_ARP_SHA:
755     case MFF_ND_SLL:
756         value->mac = flow->arp_sha;
757         break;
758
759     case MFF_ARP_THA:
760     case MFF_ND_TLL:
761         value->mac = flow->arp_tha;
762         break;
763
764     case MFF_TCP_SRC:
765     case MFF_UDP_SRC:
766     case MFF_SCTP_SRC:
767         value->be16 = flow->tp_src;
768         break;
769
770     case MFF_TCP_DST:
771     case MFF_UDP_DST:
772     case MFF_SCTP_DST:
773         value->be16 = flow->tp_dst;
774         break;
775
776     case MFF_TCP_FLAGS:
777         value->be16 = flow->tcp_flags;
778         break;
779
780     case MFF_ICMPV4_TYPE:
781     case MFF_ICMPV6_TYPE:
782         value->u8 = ntohs(flow->tp_src);
783         break;
784
785     case MFF_ICMPV4_CODE:
786     case MFF_ICMPV6_CODE:
787         value->u8 = ntohs(flow->tp_dst);
788         break;
789
790     case MFF_ND_TARGET:
791         value->ipv6 = flow->nd_target;
792         break;
793
794     case MFF_N_IDS:
795     default:
796         OVS_NOT_REACHED();
797     }
798 }
799
800 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
801  * 'value'.  The caller is responsible for ensuring that 'match' meets 'mf''s
802  * prerequisites. */
803 void
804 mf_set_value(const struct mf_field *mf,
805              const union mf_value *value, struct match *match)
806 {
807     switch (mf->id) {
808     case MFF_DP_HASH:
809         match_set_dp_hash(match, ntohl(value->be32));
810         break;
811     case MFF_RECIRC_ID:
812         match_set_recirc_id(match, ntohl(value->be32));
813         break;
814     case MFF_CONJ_ID:
815         match_set_conj_id(match, ntohl(value->be32));
816         break;
817     case MFF_TUN_ID:
818         match_set_tun_id(match, value->be64);
819         break;
820     case MFF_TUN_SRC:
821         match_set_tun_src(match, value->be32);
822         break;
823     case MFF_TUN_DST:
824         match_set_tun_dst(match, value->be32);
825         break;
826     case MFF_TUN_FLAGS:
827         match_set_tun_flags(match, ntohs(value->be16));
828         break;
829     case MFF_TUN_GBP_ID:
830          match_set_tun_gbp_id(match, value->be16);
831          break;
832     case MFF_TUN_GBP_FLAGS:
833          match_set_tun_gbp_flags(match, value->u8);
834          break;
835     case MFF_TUN_TOS:
836         match_set_tun_tos(match, value->u8);
837         break;
838     case MFF_TUN_TTL:
839         match_set_tun_ttl(match, value->u8);
840         break;
841     CASE_MFF_TUN_METADATA:
842         tun_metadata_set_match(mf, value, NULL, match);
843         break;
844
845     case MFF_METADATA:
846         match_set_metadata(match, value->be64);
847         break;
848
849     case MFF_IN_PORT:
850         match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
851         break;
852
853     case MFF_IN_PORT_OXM: {
854         ofp_port_t port;
855         ofputil_port_from_ofp11(value->be32, &port);
856         match_set_in_port(match, port);
857         break;
858     }
859     case MFF_ACTSET_OUTPUT: {
860         ofp_port_t port;
861         ofputil_port_from_ofp11(value->be32, &port);
862         match_set_actset_output(match, port);
863         break;
864     }
865
866     case MFF_SKB_PRIORITY:
867         match_set_skb_priority(match, ntohl(value->be32));
868         break;
869
870     case MFF_PKT_MARK:
871         match_set_pkt_mark(match, ntohl(value->be32));
872         break;
873
874     CASE_MFF_REGS:
875         match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
876         break;
877
878     CASE_MFF_XREGS:
879         match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
880         break;
881
882     case MFF_ETH_SRC:
883         match_set_dl_src(match, value->mac);
884         break;
885
886     case MFF_ETH_DST:
887         match_set_dl_dst(match, value->mac);
888         break;
889
890     case MFF_ETH_TYPE:
891         match_set_dl_type(match, value->be16);
892         break;
893
894     case MFF_VLAN_TCI:
895         match_set_dl_tci(match, value->be16);
896         break;
897
898     case MFF_DL_VLAN:
899         match_set_dl_vlan(match, value->be16);
900         break;
901     case MFF_VLAN_VID:
902         match_set_vlan_vid(match, value->be16);
903         break;
904
905     case MFF_DL_VLAN_PCP:
906     case MFF_VLAN_PCP:
907         match_set_dl_vlan_pcp(match, value->u8);
908         break;
909
910     case MFF_MPLS_LABEL:
911         match_set_mpls_label(match, 0, value->be32);
912         break;
913
914     case MFF_MPLS_TC:
915         match_set_mpls_tc(match, 0, value->u8);
916         break;
917
918     case MFF_MPLS_BOS:
919         match_set_mpls_bos(match, 0, value->u8);
920         break;
921
922     case MFF_IPV4_SRC:
923         match_set_nw_src(match, value->be32);
924         break;
925
926     case MFF_IPV4_DST:
927         match_set_nw_dst(match, value->be32);
928         break;
929
930     case MFF_IPV6_SRC:
931         match_set_ipv6_src(match, &value->ipv6);
932         break;
933
934     case MFF_IPV6_DST:
935         match_set_ipv6_dst(match, &value->ipv6);
936         break;
937
938     case MFF_IPV6_LABEL:
939         match_set_ipv6_label(match, value->be32);
940         break;
941
942     case MFF_IP_PROTO:
943         match_set_nw_proto(match, value->u8);
944         break;
945
946     case MFF_IP_DSCP:
947         match_set_nw_dscp(match, value->u8);
948         break;
949
950     case MFF_IP_DSCP_SHIFTED:
951         match_set_nw_dscp(match, value->u8 << 2);
952         break;
953
954     case MFF_IP_ECN:
955         match_set_nw_ecn(match, value->u8);
956         break;
957
958     case MFF_IP_TTL:
959         match_set_nw_ttl(match, value->u8);
960         break;
961
962     case MFF_IP_FRAG:
963         match_set_nw_frag(match, value->u8);
964         break;
965
966     case MFF_ARP_OP:
967         match_set_nw_proto(match, ntohs(value->be16));
968         break;
969
970     case MFF_ARP_SPA:
971         match_set_nw_src(match, value->be32);
972         break;
973
974     case MFF_ARP_TPA:
975         match_set_nw_dst(match, value->be32);
976         break;
977
978     case MFF_ARP_SHA:
979     case MFF_ND_SLL:
980         match_set_arp_sha(match, value->mac);
981         break;
982
983     case MFF_ARP_THA:
984     case MFF_ND_TLL:
985         match_set_arp_tha(match, value->mac);
986         break;
987
988     case MFF_TCP_SRC:
989     case MFF_UDP_SRC:
990     case MFF_SCTP_SRC:
991         match_set_tp_src(match, value->be16);
992         break;
993
994     case MFF_TCP_DST:
995     case MFF_UDP_DST:
996     case MFF_SCTP_DST:
997         match_set_tp_dst(match, value->be16);
998         break;
999
1000     case MFF_TCP_FLAGS:
1001         match_set_tcp_flags(match, value->be16);
1002         break;
1003
1004     case MFF_ICMPV4_TYPE:
1005     case MFF_ICMPV6_TYPE:
1006         match_set_icmp_type(match, value->u8);
1007         break;
1008
1009     case MFF_ICMPV4_CODE:
1010     case MFF_ICMPV6_CODE:
1011         match_set_icmp_code(match, value->u8);
1012         break;
1013
1014     case MFF_ND_TARGET:
1015         match_set_nd_target(match, &value->ipv6);
1016         break;
1017
1018     case MFF_N_IDS:
1019     default:
1020         OVS_NOT_REACHED();
1021     }
1022 }
1023
1024 /* Unwildcard 'mask' member field described by 'mf'.  The caller is
1025  * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1026 void
1027 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1028 {
1029     /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1030      * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1031      * special case. For the rest, calling mf_set_flow_value() is good
1032      * enough. */
1033     if (mf->id == MFF_DL_VLAN) {
1034         flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1035     } else {
1036         mf_set_flow_value(mf, &exact_match_mask, mask);
1037     }
1038 }
1039
1040 static int
1041 field_len(const struct mf_field *mf, const union mf_value *value_)
1042 {
1043     const uint8_t *value = &value_->u8;
1044     int i;
1045
1046     if (!mf->variable_len) {
1047         return mf->n_bytes;
1048     }
1049
1050     if (!value) {
1051         return 0;
1052     }
1053
1054     for (i = 0; i < mf->n_bytes; i++) {
1055         if (value[i] != 0) {
1056             break;
1057         }
1058     }
1059
1060     return mf->n_bytes - i;
1061 }
1062
1063 /* Returns the effective length of the field. For fixed length fields,
1064  * this is just the defined length. For variable length fields, it is
1065  * the minimum size encoding that retains the same meaning (i.e.
1066  * discarding leading zeros). */
1067 int
1068 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1069              const union mf_value *mask)
1070 {
1071     int len, mask_len;
1072
1073     len = field_len(mf, value);
1074     if (mask && !is_all_ones(mask, mf->n_bytes)) {
1075         mask_len = field_len(mf, mask);
1076         len = MAX(len, mask_len);
1077     }
1078
1079     return len;
1080 }
1081
1082 /* Sets 'flow' member field described by 'mf' to 'value'.  The caller is
1083  * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1084 void
1085 mf_set_flow_value(const struct mf_field *mf,
1086                   const union mf_value *value, struct flow *flow)
1087 {
1088     switch (mf->id) {
1089     case MFF_DP_HASH:
1090         flow->dp_hash = ntohl(value->be32);
1091         break;
1092     case MFF_RECIRC_ID:
1093         flow->recirc_id = ntohl(value->be32);
1094         break;
1095     case MFF_CONJ_ID:
1096         flow->conj_id = ntohl(value->be32);
1097         break;
1098     case MFF_TUN_ID:
1099         flow->tunnel.tun_id = value->be64;
1100         break;
1101     case MFF_TUN_SRC:
1102         flow->tunnel.ip_src = value->be32;
1103         break;
1104     case MFF_TUN_DST:
1105         flow->tunnel.ip_dst = value->be32;
1106         break;
1107     case MFF_TUN_FLAGS:
1108         flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1109                              ntohs(value->be16);
1110         break;
1111     case MFF_TUN_GBP_ID:
1112         flow->tunnel.gbp_id = value->be16;
1113         break;
1114     case MFF_TUN_GBP_FLAGS:
1115         flow->tunnel.gbp_flags = value->u8;
1116         break;
1117     case MFF_TUN_TOS:
1118         flow->tunnel.ip_tos = value->u8;
1119         break;
1120     case MFF_TUN_TTL:
1121         flow->tunnel.ip_ttl = value->u8;
1122         break;
1123     CASE_MFF_TUN_METADATA:
1124         tun_metadata_write(&flow->tunnel, mf, value);
1125         break;
1126     case MFF_METADATA:
1127         flow->metadata = value->be64;
1128         break;
1129
1130     case MFF_IN_PORT:
1131         flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1132         break;
1133
1134     case MFF_IN_PORT_OXM:
1135         ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1136         break;
1137     case MFF_ACTSET_OUTPUT:
1138         ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1139         break;
1140
1141     case MFF_SKB_PRIORITY:
1142         flow->skb_priority = ntohl(value->be32);
1143         break;
1144
1145     case MFF_PKT_MARK:
1146         flow->pkt_mark = ntohl(value->be32);
1147         break;
1148
1149     CASE_MFF_REGS:
1150         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1151         break;
1152
1153     CASE_MFF_XREGS:
1154         flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1155         break;
1156
1157     case MFF_ETH_SRC:
1158         flow->dl_src = value->mac;
1159         break;
1160
1161     case MFF_ETH_DST:
1162         flow->dl_dst = value->mac;
1163         break;
1164
1165     case MFF_ETH_TYPE:
1166         flow->dl_type = value->be16;
1167         break;
1168
1169     case MFF_VLAN_TCI:
1170         flow->vlan_tci = value->be16;
1171         break;
1172
1173     case MFF_DL_VLAN:
1174         flow_set_dl_vlan(flow, value->be16);
1175         break;
1176     case MFF_VLAN_VID:
1177         flow_set_vlan_vid(flow, value->be16);
1178         break;
1179
1180     case MFF_DL_VLAN_PCP:
1181     case MFF_VLAN_PCP:
1182         flow_set_vlan_pcp(flow, value->u8);
1183         break;
1184
1185     case MFF_MPLS_LABEL:
1186         flow_set_mpls_label(flow, 0, value->be32);
1187         break;
1188
1189     case MFF_MPLS_TC:
1190         flow_set_mpls_tc(flow, 0, value->u8);
1191         break;
1192
1193     case MFF_MPLS_BOS:
1194         flow_set_mpls_bos(flow, 0, value->u8);
1195         break;
1196
1197     case MFF_IPV4_SRC:
1198         flow->nw_src = value->be32;
1199         break;
1200
1201     case MFF_IPV4_DST:
1202         flow->nw_dst = value->be32;
1203         break;
1204
1205     case MFF_IPV6_SRC:
1206         flow->ipv6_src = value->ipv6;
1207         break;
1208
1209     case MFF_IPV6_DST:
1210         flow->ipv6_dst = value->ipv6;
1211         break;
1212
1213     case MFF_IPV6_LABEL:
1214         flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1215         break;
1216
1217     case MFF_IP_PROTO:
1218         flow->nw_proto = value->u8;
1219         break;
1220
1221     case MFF_IP_DSCP:
1222         flow->nw_tos &= ~IP_DSCP_MASK;
1223         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1224         break;
1225
1226     case MFF_IP_DSCP_SHIFTED:
1227         flow->nw_tos &= ~IP_DSCP_MASK;
1228         flow->nw_tos |= value->u8 << 2;
1229         break;
1230
1231     case MFF_IP_ECN:
1232         flow->nw_tos &= ~IP_ECN_MASK;
1233         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1234         break;
1235
1236     case MFF_IP_TTL:
1237         flow->nw_ttl = value->u8;
1238         break;
1239
1240     case MFF_IP_FRAG:
1241         flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1242         break;
1243
1244     case MFF_ARP_OP:
1245         flow->nw_proto = ntohs(value->be16);
1246         break;
1247
1248     case MFF_ARP_SPA:
1249         flow->nw_src = value->be32;
1250         break;
1251
1252     case MFF_ARP_TPA:
1253         flow->nw_dst = value->be32;
1254         break;
1255
1256     case MFF_ARP_SHA:
1257     case MFF_ND_SLL:
1258         flow->arp_sha = value->mac;
1259         break;
1260
1261     case MFF_ARP_THA:
1262     case MFF_ND_TLL:
1263         flow->arp_tha = value->mac;
1264         break;
1265
1266     case MFF_TCP_SRC:
1267     case MFF_UDP_SRC:
1268     case MFF_SCTP_SRC:
1269         flow->tp_src = value->be16;
1270         break;
1271
1272     case MFF_TCP_DST:
1273     case MFF_UDP_DST:
1274     case MFF_SCTP_DST:
1275         flow->tp_dst = value->be16;
1276         break;
1277
1278     case MFF_TCP_FLAGS:
1279         flow->tcp_flags = value->be16;
1280         break;
1281
1282     case MFF_ICMPV4_TYPE:
1283     case MFF_ICMPV6_TYPE:
1284         flow->tp_src = htons(value->u8);
1285         break;
1286
1287     case MFF_ICMPV4_CODE:
1288     case MFF_ICMPV6_CODE:
1289         flow->tp_dst = htons(value->u8);
1290         break;
1291
1292     case MFF_ND_TARGET:
1293         flow->nd_target = value->ipv6;
1294         break;
1295
1296     case MFF_N_IDS:
1297     default:
1298         OVS_NOT_REACHED();
1299     }
1300 }
1301
1302 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1303  * bits.  Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1304  * src[i].  */
1305 static void
1306 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1307 {
1308     size_t i;
1309
1310     for (i = 0; i < n; i++) {
1311         dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1312     }
1313 }
1314
1315 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1316  * for which 'mask' has a 0-bit keep their existing values.  The caller is
1317  * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1318 void
1319 mf_set_flow_value_masked(const struct mf_field *field,
1320                          const union mf_value *value,
1321                          const union mf_value *mask,
1322                          struct flow *flow)
1323 {
1324     union mf_value tmp;
1325
1326     mf_get_value(field, flow, &tmp);
1327     apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1328                (uint8_t *) &tmp, field->n_bytes);
1329     mf_set_flow_value(field, &tmp, flow);
1330 }
1331
1332 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1333  *
1334  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1335  * prerequisites. */
1336 bool
1337 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1338 {
1339     union mf_value value;
1340
1341     mf_get_value(mf, flow, &value);
1342     return is_all_zeros(&value, mf->n_bytes);
1343 }
1344
1345 /* Makes 'match' wildcard field 'mf'.
1346  *
1347  * The caller is responsible for ensuring that 'match' meets 'mf''s
1348  * prerequisites. */
1349 void
1350 mf_set_wild(const struct mf_field *mf, struct match *match)
1351 {
1352     switch (mf->id) {
1353     case MFF_DP_HASH:
1354         match->flow.dp_hash = 0;
1355         match->wc.masks.dp_hash = 0;
1356         break;
1357     case MFF_RECIRC_ID:
1358         match->flow.recirc_id = 0;
1359         match->wc.masks.recirc_id = 0;
1360         break;
1361     case MFF_CONJ_ID:
1362         match->flow.conj_id = 0;
1363         match->wc.masks.conj_id = 0;
1364         break;
1365     case MFF_TUN_ID:
1366         match_set_tun_id_masked(match, htonll(0), htonll(0));
1367         break;
1368     case MFF_TUN_SRC:
1369         match_set_tun_src_masked(match, htonl(0), htonl(0));
1370         break;
1371     case MFF_TUN_DST:
1372         match_set_tun_dst_masked(match, htonl(0), htonl(0));
1373         break;
1374     case MFF_TUN_FLAGS:
1375         match_set_tun_flags_masked(match, 0, 0);
1376         break;
1377     case MFF_TUN_GBP_ID:
1378         match_set_tun_gbp_id_masked(match, 0, 0);
1379         break;
1380     case MFF_TUN_GBP_FLAGS:
1381         match_set_tun_gbp_flags_masked(match, 0, 0);
1382         break;
1383     case MFF_TUN_TOS:
1384         match_set_tun_tos_masked(match, 0, 0);
1385         break;
1386     case MFF_TUN_TTL:
1387         match_set_tun_ttl_masked(match, 0, 0);
1388         break;
1389     CASE_MFF_TUN_METADATA:
1390         tun_metadata_set_match(mf, NULL, NULL, match);
1391         break;
1392
1393     case MFF_METADATA:
1394         match_set_metadata_masked(match, htonll(0), htonll(0));
1395         break;
1396
1397     case MFF_IN_PORT:
1398     case MFF_IN_PORT_OXM:
1399         match->flow.in_port.ofp_port = 0;
1400         match->wc.masks.in_port.ofp_port = 0;
1401         break;
1402     case MFF_ACTSET_OUTPUT:
1403         match->flow.actset_output = 0;
1404         match->wc.masks.actset_output = 0;
1405         break;
1406
1407     case MFF_SKB_PRIORITY:
1408         match->flow.skb_priority = 0;
1409         match->wc.masks.skb_priority = 0;
1410         break;
1411
1412     case MFF_PKT_MARK:
1413         match->flow.pkt_mark = 0;
1414         match->wc.masks.pkt_mark = 0;
1415         break;
1416
1417     CASE_MFF_REGS:
1418         match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1419         break;
1420
1421     CASE_MFF_XREGS:
1422         match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1423         break;
1424
1425     case MFF_ETH_SRC:
1426         match->flow.dl_src = eth_addr_zero;
1427         match->wc.masks.dl_src = eth_addr_zero;
1428         break;
1429
1430     case MFF_ETH_DST:
1431         match->flow.dl_dst = eth_addr_zero;
1432         match->wc.masks.dl_dst = eth_addr_zero;
1433         break;
1434
1435     case MFF_ETH_TYPE:
1436         match->flow.dl_type = htons(0);
1437         match->wc.masks.dl_type = htons(0);
1438         break;
1439
1440     case MFF_VLAN_TCI:
1441         match_set_dl_tci_masked(match, htons(0), htons(0));
1442         break;
1443
1444     case MFF_DL_VLAN:
1445     case MFF_VLAN_VID:
1446         match_set_any_vid(match);
1447         break;
1448
1449     case MFF_DL_VLAN_PCP:
1450     case MFF_VLAN_PCP:
1451         match_set_any_pcp(match);
1452         break;
1453
1454     case MFF_MPLS_LABEL:
1455         match_set_any_mpls_label(match, 0);
1456         break;
1457
1458     case MFF_MPLS_TC:
1459         match_set_any_mpls_tc(match, 0);
1460         break;
1461
1462     case MFF_MPLS_BOS:
1463         match_set_any_mpls_bos(match, 0);
1464         break;
1465
1466     case MFF_IPV4_SRC:
1467     case MFF_ARP_SPA:
1468         match_set_nw_src_masked(match, htonl(0), htonl(0));
1469         break;
1470
1471     case MFF_IPV4_DST:
1472     case MFF_ARP_TPA:
1473         match_set_nw_dst_masked(match, htonl(0), htonl(0));
1474         break;
1475
1476     case MFF_IPV6_SRC:
1477         memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1478         memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1479         break;
1480
1481     case MFF_IPV6_DST:
1482         memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1483         memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1484         break;
1485
1486     case MFF_IPV6_LABEL:
1487         match->wc.masks.ipv6_label = htonl(0);
1488         match->flow.ipv6_label = htonl(0);
1489         break;
1490
1491     case MFF_IP_PROTO:
1492         match->wc.masks.nw_proto = 0;
1493         match->flow.nw_proto = 0;
1494         break;
1495
1496     case MFF_IP_DSCP:
1497     case MFF_IP_DSCP_SHIFTED:
1498         match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1499         match->flow.nw_tos &= ~IP_DSCP_MASK;
1500         break;
1501
1502     case MFF_IP_ECN:
1503         match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1504         match->flow.nw_tos &= ~IP_ECN_MASK;
1505         break;
1506
1507     case MFF_IP_TTL:
1508         match->wc.masks.nw_ttl = 0;
1509         match->flow.nw_ttl = 0;
1510         break;
1511
1512     case MFF_IP_FRAG:
1513         match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1514         match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1515         break;
1516
1517     case MFF_ARP_OP:
1518         match->wc.masks.nw_proto = 0;
1519         match->flow.nw_proto = 0;
1520         break;
1521
1522     case MFF_ARP_SHA:
1523     case MFF_ND_SLL:
1524         match->flow.arp_sha = eth_addr_zero;
1525         match->wc.masks.arp_sha = eth_addr_zero;
1526         break;
1527
1528     case MFF_ARP_THA:
1529     case MFF_ND_TLL:
1530         match->flow.arp_tha = eth_addr_zero;
1531         match->wc.masks.arp_tha = eth_addr_zero;
1532         break;
1533
1534     case MFF_TCP_SRC:
1535     case MFF_UDP_SRC:
1536     case MFF_SCTP_SRC:
1537     case MFF_ICMPV4_TYPE:
1538     case MFF_ICMPV6_TYPE:
1539         match->wc.masks.tp_src = htons(0);
1540         match->flow.tp_src = htons(0);
1541         break;
1542
1543     case MFF_TCP_DST:
1544     case MFF_UDP_DST:
1545     case MFF_SCTP_DST:
1546     case MFF_ICMPV4_CODE:
1547     case MFF_ICMPV6_CODE:
1548         match->wc.masks.tp_dst = htons(0);
1549         match->flow.tp_dst = htons(0);
1550         break;
1551
1552     case MFF_TCP_FLAGS:
1553         match->wc.masks.tcp_flags = htons(0);
1554         match->flow.tcp_flags = htons(0);
1555         break;
1556
1557     case MFF_ND_TARGET:
1558         memset(&match->wc.masks.nd_target, 0,
1559                sizeof match->wc.masks.nd_target);
1560         memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1561         break;
1562
1563     case MFF_N_IDS:
1564     default:
1565         OVS_NOT_REACHED();
1566     }
1567 }
1568
1569 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1570  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1571  * with a 1-bit indicating that the corresponding value bit must match and a
1572  * 0-bit indicating a don't-care.
1573  *
1574  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1575  * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
1576  * call is equivalent to mf_set_wild(mf, match).
1577  *
1578  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1579  * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1580 enum ofputil_protocol
1581 mf_set(const struct mf_field *mf,
1582        const union mf_value *value, const union mf_value *mask,
1583        struct match *match)
1584 {
1585     if (!mask || is_all_ones(mask, mf->n_bytes)) {
1586         mf_set_value(mf, value, match);
1587         return mf->usable_protocols_exact;
1588     } else if (is_all_zeros(mask, mf->n_bytes)) {
1589         mf_set_wild(mf, match);
1590         return OFPUTIL_P_ANY;
1591     }
1592
1593     switch (mf->id) {
1594     case MFF_RECIRC_ID:
1595     case MFF_CONJ_ID:
1596     case MFF_IN_PORT:
1597     case MFF_IN_PORT_OXM:
1598     case MFF_ACTSET_OUTPUT:
1599     case MFF_SKB_PRIORITY:
1600     case MFF_ETH_TYPE:
1601     case MFF_DL_VLAN:
1602     case MFF_DL_VLAN_PCP:
1603     case MFF_VLAN_PCP:
1604     case MFF_MPLS_LABEL:
1605     case MFF_MPLS_TC:
1606     case MFF_MPLS_BOS:
1607     case MFF_IP_PROTO:
1608     case MFF_IP_TTL:
1609     case MFF_IP_DSCP:
1610     case MFF_IP_DSCP_SHIFTED:
1611     case MFF_IP_ECN:
1612     case MFF_ARP_OP:
1613     case MFF_ICMPV4_TYPE:
1614     case MFF_ICMPV4_CODE:
1615     case MFF_ICMPV6_TYPE:
1616     case MFF_ICMPV6_CODE:
1617         return OFPUTIL_P_NONE;
1618
1619     case MFF_DP_HASH:
1620         match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1621         break;
1622     case MFF_TUN_ID:
1623         match_set_tun_id_masked(match, value->be64, mask->be64);
1624         break;
1625     case MFF_TUN_SRC:
1626         match_set_tun_src_masked(match, value->be32, mask->be32);
1627         break;
1628     case MFF_TUN_DST:
1629         match_set_tun_dst_masked(match, value->be32, mask->be32);
1630         break;
1631     case MFF_TUN_FLAGS:
1632         match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1633         break;
1634     case MFF_TUN_GBP_ID:
1635         match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1636         break;
1637     case MFF_TUN_GBP_FLAGS:
1638         match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1639         break;
1640     case MFF_TUN_TTL:
1641         match_set_tun_ttl_masked(match, value->u8, mask->u8);
1642         break;
1643     case MFF_TUN_TOS:
1644         match_set_tun_tos_masked(match, value->u8, mask->u8);
1645         break;
1646     CASE_MFF_TUN_METADATA:
1647         tun_metadata_set_match(mf, value, mask, match);
1648         break;
1649
1650     case MFF_METADATA:
1651         match_set_metadata_masked(match, value->be64, mask->be64);
1652         break;
1653
1654     CASE_MFF_REGS:
1655         match_set_reg_masked(match, mf->id - MFF_REG0,
1656                              ntohl(value->be32), ntohl(mask->be32));
1657         break;
1658
1659     CASE_MFF_XREGS:
1660         match_set_xreg_masked(match, mf->id - MFF_XREG0,
1661                               ntohll(value->be64), ntohll(mask->be64));
1662         break;
1663
1664     case MFF_PKT_MARK:
1665         match_set_pkt_mark_masked(match, ntohl(value->be32),
1666                                   ntohl(mask->be32));
1667         break;
1668
1669     case MFF_ETH_DST:
1670         match_set_dl_dst_masked(match, value->mac, mask->mac);
1671         break;
1672
1673     case MFF_ETH_SRC:
1674         match_set_dl_src_masked(match, value->mac, mask->mac);
1675         break;
1676
1677     case MFF_ARP_SHA:
1678     case MFF_ND_SLL:
1679         match_set_arp_sha_masked(match, value->mac, mask->mac);
1680         break;
1681
1682     case MFF_ARP_THA:
1683     case MFF_ND_TLL:
1684         match_set_arp_tha_masked(match, value->mac, mask->mac);
1685         break;
1686
1687     case MFF_VLAN_TCI:
1688         match_set_dl_tci_masked(match, value->be16, mask->be16);
1689         break;
1690
1691     case MFF_VLAN_VID:
1692         match_set_vlan_vid_masked(match, value->be16, mask->be16);
1693         break;
1694
1695     case MFF_IPV4_SRC:
1696         match_set_nw_src_masked(match, value->be32, mask->be32);
1697         break;
1698
1699     case MFF_IPV4_DST:
1700         match_set_nw_dst_masked(match, value->be32, mask->be32);
1701         break;
1702
1703     case MFF_IPV6_SRC:
1704         match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1705         break;
1706
1707     case MFF_IPV6_DST:
1708         match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1709         break;
1710
1711     case MFF_IPV6_LABEL:
1712         if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1713             mf_set_value(mf, value, match);
1714         } else {
1715             match_set_ipv6_label_masked(match, value->be32, mask->be32);
1716         }
1717         break;
1718
1719     case MFF_ND_TARGET:
1720         match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1721         break;
1722
1723     case MFF_IP_FRAG:
1724         match_set_nw_frag_masked(match, value->u8, mask->u8);
1725         break;
1726
1727     case MFF_ARP_SPA:
1728         match_set_nw_src_masked(match, value->be32, mask->be32);
1729         break;
1730
1731     case MFF_ARP_TPA:
1732         match_set_nw_dst_masked(match, value->be32, mask->be32);
1733         break;
1734
1735     case MFF_TCP_SRC:
1736     case MFF_UDP_SRC:
1737     case MFF_SCTP_SRC:
1738         match_set_tp_src_masked(match, value->be16, mask->be16);
1739         break;
1740
1741     case MFF_TCP_DST:
1742     case MFF_UDP_DST:
1743     case MFF_SCTP_DST:
1744         match_set_tp_dst_masked(match, value->be16, mask->be16);
1745         break;
1746
1747     case MFF_TCP_FLAGS:
1748         match_set_tcp_flags_masked(match, value->be16, mask->be16);
1749         break;
1750
1751     case MFF_N_IDS:
1752     default:
1753         OVS_NOT_REACHED();
1754     }
1755
1756     return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1757              || ip_is_cidr(mask->be32))
1758             ? mf->usable_protocols_cidr
1759             : mf->usable_protocols_bitwise);
1760 }
1761
1762 static enum ofperr
1763 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1764            const char *type)
1765 {
1766     if (!sf->field) {
1767         VLOG_WARN_RL(&rl, "unknown %s field", type);
1768         return OFPERR_OFPBAC_BAD_SET_TYPE;
1769     } else if (!sf->n_bits) {
1770         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1771         return OFPERR_OFPBAC_BAD_SET_LEN;
1772     } else if (sf->ofs >= sf->field->n_bits) {
1773         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1774                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1775         return OFPERR_OFPBAC_BAD_SET_LEN;
1776     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1777         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1778                      "of %s field %s", sf->ofs, sf->n_bits,
1779                      sf->field->n_bits, type, sf->field->name);
1780         return OFPERR_OFPBAC_BAD_SET_LEN;
1781     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1782         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1783                      type, sf->field->name);
1784         return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1785     } else {
1786         return 0;
1787     }
1788 }
1789
1790 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1791  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1792  * ofp_mkerr()).  */
1793 enum ofperr
1794 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1795 {
1796     return mf_check__(sf, flow, "source");
1797 }
1798
1799 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1800  * if so, otherwise an OpenFlow error code (e.g. as returned by
1801  * ofp_mkerr()). */
1802 enum ofperr
1803 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1804 {
1805     int error = mf_check__(sf, flow, "destination");
1806     if (!error && !sf->field->writable) {
1807         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1808                      sf->field->name);
1809         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1810     }
1811     return error;
1812 }
1813
1814 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1815  * 'value' and 'mask', respectively. */
1816 void
1817 mf_get(const struct mf_field *mf, const struct match *match,
1818        union mf_value *value, union mf_value *mask)
1819 {
1820     mf_get_value(mf, &match->flow, value);
1821     mf_get_mask(mf, &match->wc, mask);
1822 }
1823
1824 static char *
1825 mf_from_integer_string(const struct mf_field *mf, const char *s,
1826                        uint8_t *valuep, uint8_t *maskp)
1827 {
1828     char *tail;
1829     const char *err_str = "";
1830     int err;
1831
1832     err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1833     if (err || (*tail != '\0' && *tail != '/')) {
1834         err_str = "value";
1835         goto syntax_error;
1836     }
1837
1838     if (*tail == '/') {
1839         err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1840         if (err || *tail != '\0') {
1841             err_str = "mask";
1842             goto syntax_error;
1843         }
1844     } else {
1845         memset(maskp, 0xff, mf->n_bytes);
1846     }
1847
1848     return NULL;
1849
1850 syntax_error:
1851     if (err == ERANGE) {
1852         return xasprintf("%s: %s too large for %u-byte field %s",
1853                          s, err_str, mf->n_bytes, mf->name);
1854     } else {
1855         return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1856     }
1857 }
1858
1859 static char *
1860 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1861                         struct eth_addr *mac, struct eth_addr *mask)
1862 {
1863     int n;
1864
1865     ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1866
1867     n = -1;
1868     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
1869         && n == strlen(s)) {
1870         *mask = eth_addr_exact;
1871         return NULL;
1872     }
1873
1874     n = -1;
1875     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1876                  ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
1877         && n == strlen(s)) {
1878         return NULL;
1879     }
1880
1881     return xasprintf("%s: invalid Ethernet address", s);
1882 }
1883
1884 static char *
1885 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1886                     ovs_be32 *ip, ovs_be32 *mask)
1887 {
1888     int prefix;
1889
1890     ovs_assert(mf->n_bytes == sizeof *ip);
1891
1892     if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1893                  IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1894         /* OK. */
1895     } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1896         if (prefix <= 0 || prefix > 32) {
1897             return xasprintf("%s: network prefix bits not between 0 and "
1898                              "32", s);
1899         }
1900         *mask = be32_prefix_mask(prefix);
1901     } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1902         *mask = OVS_BE32_MAX;
1903     } else {
1904         return xasprintf("%s: invalid IP address", s);
1905     }
1906     return NULL;
1907 }
1908
1909 static char *
1910 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1911                     struct in6_addr *value, struct in6_addr *mask)
1912 {
1913     char *str = xstrdup(s);
1914     char *save_ptr = NULL;
1915     const char *name, *netmask;
1916     int retval;
1917
1918     ovs_assert(mf->n_bytes == sizeof *value);
1919
1920     name = strtok_r(str, "/", &save_ptr);
1921     retval = name ? lookup_ipv6(name, value) : EINVAL;
1922     if (retval) {
1923         char *err;
1924
1925         err = xasprintf("%s: could not convert to IPv6 address", str);
1926         free(str);
1927
1928         return err;
1929     }
1930
1931     netmask = strtok_r(NULL, "/", &save_ptr);
1932     if (netmask) {
1933         if (inet_pton(AF_INET6, netmask, mask) != 1) {
1934             int prefix = atoi(netmask);
1935             if (prefix <= 0 || prefix > 128) {
1936                 free(str);
1937                 return xasprintf("%s: prefix bits not between 1 and 128", s);
1938             } else {
1939                 *mask = ipv6_create_mask(prefix);
1940             }
1941         }
1942     } else {
1943         *mask = in6addr_exact;
1944     }
1945     free(str);
1946
1947     return NULL;
1948 }
1949
1950 static char *
1951 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1952                         ovs_be16 *valuep, ovs_be16 *maskp)
1953 {
1954     ofp_port_t port;
1955
1956     ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1957
1958     if (ofputil_port_from_string(s, &port)) {
1959         *valuep = htons(ofp_to_u16(port));
1960         *maskp = OVS_BE16_MAX;
1961         return NULL;
1962     }
1963     return xasprintf("%s: port value out of range for %s", s, mf->name);
1964 }
1965
1966 static char *
1967 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1968                           ovs_be32 *valuep, ovs_be32 *maskp)
1969 {
1970     ofp_port_t port;
1971
1972     ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1973     if (ofputil_port_from_string(s, &port)) {
1974         *valuep = ofputil_port_to_ofp11(port);
1975         *maskp = OVS_BE32_MAX;
1976         return NULL;
1977     }
1978     return xasprintf("%s: port value out of range for %s", s, mf->name);
1979 }
1980
1981 struct frag_handling {
1982     const char *name;
1983     uint8_t mask;
1984     uint8_t value;
1985 };
1986
1987 static const struct frag_handling all_frags[] = {
1988 #define A FLOW_NW_FRAG_ANY
1989 #define L FLOW_NW_FRAG_LATER
1990     /* name               mask  value */
1991
1992     { "no",               A|L,  0     },
1993     { "first",            A|L,  A     },
1994     { "later",            A|L,  A|L   },
1995
1996     { "no",               A,    0     },
1997     { "yes",              A,    A     },
1998
1999     { "not_later",        L,    0     },
2000     { "later",            L,    L     },
2001 #undef A
2002 #undef L
2003 };
2004
2005 static char *
2006 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2007 {
2008     const struct frag_handling *h;
2009
2010     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2011         if (!strcasecmp(s, h->name)) {
2012             /* We force the upper bits of the mask on to make mf_parse_value()
2013              * happy (otherwise it will never think it's an exact match.) */
2014             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2015             *valuep = h->value;
2016             return NULL;
2017         }
2018     }
2019
2020     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2021                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2022 }
2023
2024 static char *
2025 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2026                const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2027                ovs_be16 *maskp)
2028 {
2029     int err;
2030     char *err_str;
2031     uint32_t flags, mask;
2032
2033     err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2034                       &flags, ntohs(allowed), maskp ? &mask : NULL);
2035     if (err < 0) {
2036         return err_str;
2037     }
2038
2039     *flagsp = htons(flags);
2040     if (maskp) {
2041         *maskp = htons(mask);
2042     }
2043
2044     return NULL;
2045 }
2046
2047 static char *
2048 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2049 {
2050     return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2051                           TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2052 }
2053
2054 static char *
2055 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2056 {
2057     return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2058                           htons(FLOW_TNL_PUB_F_MASK), maskp);
2059 }
2060
2061 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2062  * NULL if successful, otherwise a malloc()'d string describing the error. */
2063 char *
2064 mf_parse(const struct mf_field *mf, const char *s,
2065          union mf_value *value, union mf_value *mask)
2066 {
2067     char *error;
2068
2069     if (!strcmp(s, "*")) {
2070         memset(value, 0, mf->n_bytes);
2071         memset(mask, 0, mf->n_bytes);
2072         return NULL;
2073     }
2074
2075     switch (mf->string) {
2076     case MFS_DECIMAL:
2077     case MFS_HEXADECIMAL:
2078         error = mf_from_integer_string(mf, s,
2079                                        (uint8_t *) value, (uint8_t *) mask);
2080         break;
2081
2082     case MFS_ETHERNET:
2083         error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2084         break;
2085
2086     case MFS_IPV4:
2087         error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2088         break;
2089
2090     case MFS_IPV6:
2091         error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2092         break;
2093
2094     case MFS_OFP_PORT:
2095         error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2096         break;
2097
2098     case MFS_OFP_PORT_OXM:
2099         error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2100         break;
2101
2102     case MFS_FRAG:
2103         error = mf_from_frag_string(s, &value->u8, &mask->u8);
2104         break;
2105
2106     case MFS_TNL_FLAGS:
2107         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2108         error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2109         break;
2110
2111     case MFS_TCP_FLAGS:
2112         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2113         error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2114         break;
2115
2116     default:
2117         OVS_NOT_REACHED();
2118     }
2119
2120     if (!error && !mf_is_mask_valid(mf, mask)) {
2121         error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2122     }
2123     return error;
2124 }
2125
2126 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2127  * successful, otherwise a malloc()'d string describing the error. */
2128 char *
2129 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2130 {
2131     union mf_value mask;
2132     char *error;
2133
2134     error = mf_parse(mf, s, value, &mask);
2135     if (error) {
2136         return error;
2137     }
2138
2139     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2140         return xasprintf("%s: wildcards not allowed here", s);
2141     }
2142     return NULL;
2143 }
2144
2145 static void
2146 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2147                          const uint8_t *maskp, struct ds *s)
2148 {
2149     if (mf->string == MFS_HEXADECIMAL) {
2150         ds_put_hex(s, valuep, mf->n_bytes);
2151     } else {
2152         unsigned long long int integer = 0;
2153         int i;
2154
2155         ovs_assert(mf->n_bytes <= 8);
2156         for (i = 0; i < mf->n_bytes; i++) {
2157             integer = (integer << 8) | valuep[i];
2158         }
2159         ds_put_format(s, "%lld", integer);
2160     }
2161
2162     if (maskp) {
2163         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2164          * not sure that that a bit-mask written in decimal is ever easier to
2165          * understand than the same bit-mask written in hexadecimal. */
2166         ds_put_char(s, '/');
2167         ds_put_hex(s, maskp, mf->n_bytes);
2168     }
2169 }
2170
2171 static void
2172 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2173 {
2174     const struct frag_handling *h;
2175
2176     mask &= FLOW_NW_FRAG_MASK;
2177     value &= mask;
2178
2179     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2180         if (value == h->value && mask == h->mask) {
2181             ds_put_cstr(s, h->name);
2182             return;
2183         }
2184     }
2185     ds_put_cstr(s, "<error>");
2186 }
2187
2188 static void
2189 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2190 {
2191     format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2192                         ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2193 }
2194
2195 static void
2196 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2197 {
2198     format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2199                         TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2200 }
2201
2202 /* Appends to 's' a string representation of field 'mf' whose value is in
2203  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2204 void
2205 mf_format(const struct mf_field *mf,
2206           const union mf_value *value, const union mf_value *mask,
2207           struct ds *s)
2208 {
2209     if (mask) {
2210         if (is_all_zeros(mask, mf->n_bytes)) {
2211             ds_put_cstr(s, "ANY");
2212             return;
2213         } else if (is_all_ones(mask, mf->n_bytes)) {
2214             mask = NULL;
2215         }
2216     }
2217
2218     switch (mf->string) {
2219     case MFS_OFP_PORT_OXM:
2220         if (!mask) {
2221             ofp_port_t port;
2222             ofputil_port_from_ofp11(value->be32, &port);
2223             ofputil_format_port(port, s);
2224             break;
2225         }
2226         /* fall through */
2227     case MFS_OFP_PORT:
2228         if (!mask) {
2229             ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2230             break;
2231         }
2232         /* fall through */
2233     case MFS_DECIMAL:
2234     case MFS_HEXADECIMAL:
2235         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2236         break;
2237
2238     case MFS_ETHERNET:
2239         eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2240         break;
2241
2242     case MFS_IPV4:
2243         ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2244         break;
2245
2246     case MFS_IPV6:
2247         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2248         break;
2249
2250     case MFS_FRAG:
2251         mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2252         break;
2253
2254     case MFS_TNL_FLAGS:
2255         mf_format_tnl_flags_string(value->be16,
2256                                    mask ? mask->be16 : OVS_BE16_MAX, s);
2257         break;
2258
2259     case MFS_TCP_FLAGS:
2260         mf_format_tcp_flags_string(value->be16,
2261                                    mask ? mask->be16 : OVS_BE16_MAX, s);
2262         break;
2263
2264     default:
2265         OVS_NOT_REACHED();
2266     }
2267 }
2268 \f
2269 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2270  * least-significant bits in 'x'.
2271  */
2272 void
2273 mf_write_subfield_flow(const struct mf_subfield *sf,
2274                        const union mf_subvalue *x, struct flow *flow)
2275 {
2276     const struct mf_field *field = sf->field;
2277     union mf_value value;
2278
2279     mf_get_value(field, flow, &value);
2280     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2281                  sf->ofs, sf->n_bits);
2282     mf_set_flow_value(field, &value, flow);
2283 }
2284
2285 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2286  * least-significant bits in 'x'.
2287  */
2288 void
2289 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2290                   struct match *match)
2291 {
2292     const struct mf_field *field = sf->field;
2293     union mf_value value, mask;
2294
2295     mf_get(field, match, &value, &mask);
2296     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2297     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2298     mf_set(field, &value, &mask, match);
2299 }
2300
2301 /* 'v' and 'm' correspond to values of 'field'.  This function copies them into
2302  * 'match' in the correspond positions. */
2303 void
2304 mf_mask_subfield(const struct mf_field *field,
2305                  const union mf_subvalue *v,
2306                  const union mf_subvalue *m,
2307                  struct match *match)
2308 {
2309     union mf_value value, mask;
2310
2311     mf_get(field, match, &value, &mask);
2312     bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2313     bitwise_copy(m, sizeof *m, 0, &mask,  field->n_bytes, 0, field->n_bits);
2314     mf_set(field, &value, &mask, match);
2315 }
2316
2317 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2318  * reading 'flow', e.g. as checked by mf_check_src(). */
2319 void
2320 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2321                  union mf_subvalue *x)
2322 {
2323     union mf_value value;
2324
2325     mf_get_value(sf->field, flow, &value);
2326
2327     memset(x, 0, sizeof *x);
2328     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2329                  x, sizeof *x, 0,
2330                  sf->n_bits);
2331 }
2332
2333 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2334  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2335  * less. */
2336 uint64_t
2337 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2338 {
2339     union mf_value value;
2340
2341     mf_get_value(sf->field, flow, &value);
2342     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2343 }
2344
2345 void
2346 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2347 {
2348     ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2349 }
2350
2351 void
2352 field_array_set(enum mf_field_id id, const union mf_value *value,
2353                 struct field_array *fa)
2354 {
2355     ovs_assert(id < MFF_N_IDS);
2356     bitmap_set1(fa->used.bm, id);
2357     fa->value[id] = *value;
2358 }