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