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