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