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