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