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