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