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