datapath-windows: Fix IP fragmentation
[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  *
801  * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
802  * with the request or NULL if there is no error. The caller is reponsible
803  * for freeing the string. */
804 void
805 mf_set_value(const struct mf_field *mf,
806              const union mf_value *value, struct match *match, char **err_str)
807 {
808     if (err_str) {
809         *err_str = NULL;
810     }
811
812     switch (mf->id) {
813     case MFF_DP_HASH:
814         match_set_dp_hash(match, ntohl(value->be32));
815         break;
816     case MFF_RECIRC_ID:
817         match_set_recirc_id(match, ntohl(value->be32));
818         break;
819     case MFF_CONJ_ID:
820         match_set_conj_id(match, ntohl(value->be32));
821         break;
822     case MFF_TUN_ID:
823         match_set_tun_id(match, value->be64);
824         break;
825     case MFF_TUN_SRC:
826         match_set_tun_src(match, value->be32);
827         break;
828     case MFF_TUN_DST:
829         match_set_tun_dst(match, value->be32);
830         break;
831     case MFF_TUN_FLAGS:
832         match_set_tun_flags(match, ntohs(value->be16));
833         break;
834     case MFF_TUN_GBP_ID:
835          match_set_tun_gbp_id(match, value->be16);
836          break;
837     case MFF_TUN_GBP_FLAGS:
838          match_set_tun_gbp_flags(match, value->u8);
839          break;
840     case MFF_TUN_TOS:
841         match_set_tun_tos(match, value->u8);
842         break;
843     case MFF_TUN_TTL:
844         match_set_tun_ttl(match, value->u8);
845         break;
846     CASE_MFF_TUN_METADATA:
847         tun_metadata_set_match(mf, value, NULL, match, err_str);
848         break;
849
850     case MFF_METADATA:
851         match_set_metadata(match, value->be64);
852         break;
853
854     case MFF_IN_PORT:
855         match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
856         break;
857
858     case MFF_IN_PORT_OXM: {
859         ofp_port_t port;
860         ofputil_port_from_ofp11(value->be32, &port);
861         match_set_in_port(match, port);
862         break;
863     }
864     case MFF_ACTSET_OUTPUT: {
865         ofp_port_t port;
866         ofputil_port_from_ofp11(value->be32, &port);
867         match_set_actset_output(match, port);
868         break;
869     }
870
871     case MFF_SKB_PRIORITY:
872         match_set_skb_priority(match, ntohl(value->be32));
873         break;
874
875     case MFF_PKT_MARK:
876         match_set_pkt_mark(match, ntohl(value->be32));
877         break;
878
879     CASE_MFF_REGS:
880         match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
881         break;
882
883     CASE_MFF_XREGS:
884         match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
885         break;
886
887     case MFF_ETH_SRC:
888         match_set_dl_src(match, value->mac);
889         break;
890
891     case MFF_ETH_DST:
892         match_set_dl_dst(match, value->mac);
893         break;
894
895     case MFF_ETH_TYPE:
896         match_set_dl_type(match, value->be16);
897         break;
898
899     case MFF_VLAN_TCI:
900         match_set_dl_tci(match, value->be16);
901         break;
902
903     case MFF_DL_VLAN:
904         match_set_dl_vlan(match, value->be16);
905         break;
906     case MFF_VLAN_VID:
907         match_set_vlan_vid(match, value->be16);
908         break;
909
910     case MFF_DL_VLAN_PCP:
911     case MFF_VLAN_PCP:
912         match_set_dl_vlan_pcp(match, value->u8);
913         break;
914
915     case MFF_MPLS_LABEL:
916         match_set_mpls_label(match, 0, value->be32);
917         break;
918
919     case MFF_MPLS_TC:
920         match_set_mpls_tc(match, 0, value->u8);
921         break;
922
923     case MFF_MPLS_BOS:
924         match_set_mpls_bos(match, 0, value->u8);
925         break;
926
927     case MFF_IPV4_SRC:
928         match_set_nw_src(match, value->be32);
929         break;
930
931     case MFF_IPV4_DST:
932         match_set_nw_dst(match, value->be32);
933         break;
934
935     case MFF_IPV6_SRC:
936         match_set_ipv6_src(match, &value->ipv6);
937         break;
938
939     case MFF_IPV6_DST:
940         match_set_ipv6_dst(match, &value->ipv6);
941         break;
942
943     case MFF_IPV6_LABEL:
944         match_set_ipv6_label(match, value->be32);
945         break;
946
947     case MFF_IP_PROTO:
948         match_set_nw_proto(match, value->u8);
949         break;
950
951     case MFF_IP_DSCP:
952         match_set_nw_dscp(match, value->u8);
953         break;
954
955     case MFF_IP_DSCP_SHIFTED:
956         match_set_nw_dscp(match, value->u8 << 2);
957         break;
958
959     case MFF_IP_ECN:
960         match_set_nw_ecn(match, value->u8);
961         break;
962
963     case MFF_IP_TTL:
964         match_set_nw_ttl(match, value->u8);
965         break;
966
967     case MFF_IP_FRAG:
968         match_set_nw_frag(match, value->u8);
969         break;
970
971     case MFF_ARP_OP:
972         match_set_nw_proto(match, ntohs(value->be16));
973         break;
974
975     case MFF_ARP_SPA:
976         match_set_nw_src(match, value->be32);
977         break;
978
979     case MFF_ARP_TPA:
980         match_set_nw_dst(match, value->be32);
981         break;
982
983     case MFF_ARP_SHA:
984     case MFF_ND_SLL:
985         match_set_arp_sha(match, value->mac);
986         break;
987
988     case MFF_ARP_THA:
989     case MFF_ND_TLL:
990         match_set_arp_tha(match, value->mac);
991         break;
992
993     case MFF_TCP_SRC:
994     case MFF_UDP_SRC:
995     case MFF_SCTP_SRC:
996         match_set_tp_src(match, value->be16);
997         break;
998
999     case MFF_TCP_DST:
1000     case MFF_UDP_DST:
1001     case MFF_SCTP_DST:
1002         match_set_tp_dst(match, value->be16);
1003         break;
1004
1005     case MFF_TCP_FLAGS:
1006         match_set_tcp_flags(match, value->be16);
1007         break;
1008
1009     case MFF_ICMPV4_TYPE:
1010     case MFF_ICMPV6_TYPE:
1011         match_set_icmp_type(match, value->u8);
1012         break;
1013
1014     case MFF_ICMPV4_CODE:
1015     case MFF_ICMPV6_CODE:
1016         match_set_icmp_code(match, value->u8);
1017         break;
1018
1019     case MFF_ND_TARGET:
1020         match_set_nd_target(match, &value->ipv6);
1021         break;
1022
1023     case MFF_N_IDS:
1024     default:
1025         OVS_NOT_REACHED();
1026     }
1027 }
1028
1029 /* Unwildcard 'mask' member field described by 'mf'.  The caller is
1030  * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1031 void
1032 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1033 {
1034     /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1035      * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1036      * special case. For the rest, calling mf_set_flow_value() is good
1037      * enough. */
1038     if (mf->id == MFF_DL_VLAN) {
1039         flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1040     } else {
1041         mf_set_flow_value(mf, &exact_match_mask, mask);
1042     }
1043 }
1044
1045 static int
1046 field_len(const struct mf_field *mf, const union mf_value *value_)
1047 {
1048     const uint8_t *value = &value_->u8;
1049     int i;
1050
1051     if (!mf->variable_len) {
1052         return mf->n_bytes;
1053     }
1054
1055     if (!value) {
1056         return 0;
1057     }
1058
1059     for (i = 0; i < mf->n_bytes; i++) {
1060         if (value[i] != 0) {
1061             break;
1062         }
1063     }
1064
1065     return mf->n_bytes - i;
1066 }
1067
1068 /* Returns the effective length of the field. For fixed length fields,
1069  * this is just the defined length. For variable length fields, it is
1070  * the minimum size encoding that retains the same meaning (i.e.
1071  * discarding leading zeros).
1072  *
1073  * 'is_masked' returns (if non-NULL) whether the original contained
1074  * a mask. Otherwise, a mask that is the same length as the value
1075  * might be misinterpreted as an exact match. */
1076 int
1077 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1078              const union mf_value *mask, bool *is_masked_)
1079 {
1080     int len, mask_len;
1081     bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1082
1083     len = field_len(mf, value);
1084     if (is_masked) {
1085         mask_len = field_len(mf, mask);
1086         len = MAX(len, mask_len);
1087     }
1088
1089     if (is_masked_) {
1090         *is_masked_ = is_masked;
1091     }
1092
1093     return len;
1094 }
1095
1096 /* Sets 'flow' member field described by 'mf' to 'value'.  The caller is
1097  * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1098 void
1099 mf_set_flow_value(const struct mf_field *mf,
1100                   const union mf_value *value, struct flow *flow)
1101 {
1102     switch (mf->id) {
1103     case MFF_DP_HASH:
1104         flow->dp_hash = ntohl(value->be32);
1105         break;
1106     case MFF_RECIRC_ID:
1107         flow->recirc_id = ntohl(value->be32);
1108         break;
1109     case MFF_CONJ_ID:
1110         flow->conj_id = ntohl(value->be32);
1111         break;
1112     case MFF_TUN_ID:
1113         flow->tunnel.tun_id = value->be64;
1114         break;
1115     case MFF_TUN_SRC:
1116         flow->tunnel.ip_src = value->be32;
1117         break;
1118     case MFF_TUN_DST:
1119         flow->tunnel.ip_dst = value->be32;
1120         break;
1121     case MFF_TUN_FLAGS:
1122         flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1123                              ntohs(value->be16);
1124         break;
1125     case MFF_TUN_GBP_ID:
1126         flow->tunnel.gbp_id = value->be16;
1127         break;
1128     case MFF_TUN_GBP_FLAGS:
1129         flow->tunnel.gbp_flags = value->u8;
1130         break;
1131     case MFF_TUN_TOS:
1132         flow->tunnel.ip_tos = value->u8;
1133         break;
1134     case MFF_TUN_TTL:
1135         flow->tunnel.ip_ttl = value->u8;
1136         break;
1137     CASE_MFF_TUN_METADATA:
1138         tun_metadata_write(&flow->tunnel, mf, value);
1139         break;
1140     case MFF_METADATA:
1141         flow->metadata = value->be64;
1142         break;
1143
1144     case MFF_IN_PORT:
1145         flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1146         break;
1147
1148     case MFF_IN_PORT_OXM:
1149         ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1150         break;
1151     case MFF_ACTSET_OUTPUT:
1152         ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1153         break;
1154
1155     case MFF_SKB_PRIORITY:
1156         flow->skb_priority = ntohl(value->be32);
1157         break;
1158
1159     case MFF_PKT_MARK:
1160         flow->pkt_mark = ntohl(value->be32);
1161         break;
1162
1163     CASE_MFF_REGS:
1164         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1165         break;
1166
1167     CASE_MFF_XREGS:
1168         flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1169         break;
1170
1171     case MFF_ETH_SRC:
1172         flow->dl_src = value->mac;
1173         break;
1174
1175     case MFF_ETH_DST:
1176         flow->dl_dst = value->mac;
1177         break;
1178
1179     case MFF_ETH_TYPE:
1180         flow->dl_type = value->be16;
1181         break;
1182
1183     case MFF_VLAN_TCI:
1184         flow->vlan_tci = value->be16;
1185         break;
1186
1187     case MFF_DL_VLAN:
1188         flow_set_dl_vlan(flow, value->be16);
1189         break;
1190     case MFF_VLAN_VID:
1191         flow_set_vlan_vid(flow, value->be16);
1192         break;
1193
1194     case MFF_DL_VLAN_PCP:
1195     case MFF_VLAN_PCP:
1196         flow_set_vlan_pcp(flow, value->u8);
1197         break;
1198
1199     case MFF_MPLS_LABEL:
1200         flow_set_mpls_label(flow, 0, value->be32);
1201         break;
1202
1203     case MFF_MPLS_TC:
1204         flow_set_mpls_tc(flow, 0, value->u8);
1205         break;
1206
1207     case MFF_MPLS_BOS:
1208         flow_set_mpls_bos(flow, 0, value->u8);
1209         break;
1210
1211     case MFF_IPV4_SRC:
1212         flow->nw_src = value->be32;
1213         break;
1214
1215     case MFF_IPV4_DST:
1216         flow->nw_dst = value->be32;
1217         break;
1218
1219     case MFF_IPV6_SRC:
1220         flow->ipv6_src = value->ipv6;
1221         break;
1222
1223     case MFF_IPV6_DST:
1224         flow->ipv6_dst = value->ipv6;
1225         break;
1226
1227     case MFF_IPV6_LABEL:
1228         flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1229         break;
1230
1231     case MFF_IP_PROTO:
1232         flow->nw_proto = value->u8;
1233         break;
1234
1235     case MFF_IP_DSCP:
1236         flow->nw_tos &= ~IP_DSCP_MASK;
1237         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1238         break;
1239
1240     case MFF_IP_DSCP_SHIFTED:
1241         flow->nw_tos &= ~IP_DSCP_MASK;
1242         flow->nw_tos |= value->u8 << 2;
1243         break;
1244
1245     case MFF_IP_ECN:
1246         flow->nw_tos &= ~IP_ECN_MASK;
1247         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1248         break;
1249
1250     case MFF_IP_TTL:
1251         flow->nw_ttl = value->u8;
1252         break;
1253
1254     case MFF_IP_FRAG:
1255         flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1256         break;
1257
1258     case MFF_ARP_OP:
1259         flow->nw_proto = ntohs(value->be16);
1260         break;
1261
1262     case MFF_ARP_SPA:
1263         flow->nw_src = value->be32;
1264         break;
1265
1266     case MFF_ARP_TPA:
1267         flow->nw_dst = value->be32;
1268         break;
1269
1270     case MFF_ARP_SHA:
1271     case MFF_ND_SLL:
1272         flow->arp_sha = value->mac;
1273         break;
1274
1275     case MFF_ARP_THA:
1276     case MFF_ND_TLL:
1277         flow->arp_tha = value->mac;
1278         break;
1279
1280     case MFF_TCP_SRC:
1281     case MFF_UDP_SRC:
1282     case MFF_SCTP_SRC:
1283         flow->tp_src = value->be16;
1284         break;
1285
1286     case MFF_TCP_DST:
1287     case MFF_UDP_DST:
1288     case MFF_SCTP_DST:
1289         flow->tp_dst = value->be16;
1290         break;
1291
1292     case MFF_TCP_FLAGS:
1293         flow->tcp_flags = value->be16;
1294         break;
1295
1296     case MFF_ICMPV4_TYPE:
1297     case MFF_ICMPV6_TYPE:
1298         flow->tp_src = htons(value->u8);
1299         break;
1300
1301     case MFF_ICMPV4_CODE:
1302     case MFF_ICMPV6_CODE:
1303         flow->tp_dst = htons(value->u8);
1304         break;
1305
1306     case MFF_ND_TARGET:
1307         flow->nd_target = value->ipv6;
1308         break;
1309
1310     case MFF_N_IDS:
1311     default:
1312         OVS_NOT_REACHED();
1313     }
1314 }
1315
1316 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1317  * bits.  Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1318  * src[i].  */
1319 static void
1320 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1321 {
1322     size_t i;
1323
1324     for (i = 0; i < n; i++) {
1325         dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1326     }
1327 }
1328
1329 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1330  * for which 'mask' has a 0-bit keep their existing values.  The caller is
1331  * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1332 void
1333 mf_set_flow_value_masked(const struct mf_field *field,
1334                          const union mf_value *value,
1335                          const union mf_value *mask,
1336                          struct flow *flow)
1337 {
1338     union mf_value tmp;
1339
1340     mf_get_value(field, flow, &tmp);
1341     apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1342                (uint8_t *) &tmp, field->n_bytes);
1343     mf_set_flow_value(field, &tmp, flow);
1344 }
1345
1346 bool
1347 mf_is_tun_metadata(const struct mf_field *mf)
1348 {
1349     return mf->id >= MFF_TUN_METADATA0 &&
1350            mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1351 }
1352
1353 /* Returns true if 'mf' has previously been set in 'flow', false if
1354  * it contains a non-default value.
1355  *
1356  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1357  * prerequisites. */
1358 bool
1359 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1360 {
1361     if (!mf_is_tun_metadata(mf)) {
1362         union mf_value value;
1363
1364         mf_get_value(mf, flow, &value);
1365         return !is_all_zeros(&value, mf->n_bytes);
1366     } else {
1367         return ULLONG_GET(flow->tunnel.metadata.present.map,
1368                           mf->id - MFF_TUN_METADATA0);
1369     }
1370 }
1371
1372 /* Makes 'match' wildcard field 'mf'.
1373  *
1374  * The caller is responsible for ensuring that 'match' meets 'mf''s
1375  * prerequisites.
1376  *
1377  * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1378  * with the request or NULL if there is no error. The caller is reponsible
1379  * for freeing the string. */
1380 void
1381 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1382 {
1383     if (err_str) {
1384         *err_str = NULL;
1385     }
1386
1387     switch (mf->id) {
1388     case MFF_DP_HASH:
1389         match->flow.dp_hash = 0;
1390         match->wc.masks.dp_hash = 0;
1391         break;
1392     case MFF_RECIRC_ID:
1393         match->flow.recirc_id = 0;
1394         match->wc.masks.recirc_id = 0;
1395         break;
1396     case MFF_CONJ_ID:
1397         match->flow.conj_id = 0;
1398         match->wc.masks.conj_id = 0;
1399         break;
1400     case MFF_TUN_ID:
1401         match_set_tun_id_masked(match, htonll(0), htonll(0));
1402         break;
1403     case MFF_TUN_SRC:
1404         match_set_tun_src_masked(match, htonl(0), htonl(0));
1405         break;
1406     case MFF_TUN_DST:
1407         match_set_tun_dst_masked(match, htonl(0), htonl(0));
1408         break;
1409     case MFF_TUN_FLAGS:
1410         match_set_tun_flags_masked(match, 0, 0);
1411         break;
1412     case MFF_TUN_GBP_ID:
1413         match_set_tun_gbp_id_masked(match, 0, 0);
1414         break;
1415     case MFF_TUN_GBP_FLAGS:
1416         match_set_tun_gbp_flags_masked(match, 0, 0);
1417         break;
1418     case MFF_TUN_TOS:
1419         match_set_tun_tos_masked(match, 0, 0);
1420         break;
1421     case MFF_TUN_TTL:
1422         match_set_tun_ttl_masked(match, 0, 0);
1423         break;
1424     CASE_MFF_TUN_METADATA:
1425         tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1426         break;
1427
1428     case MFF_METADATA:
1429         match_set_metadata_masked(match, htonll(0), htonll(0));
1430         break;
1431
1432     case MFF_IN_PORT:
1433     case MFF_IN_PORT_OXM:
1434         match->flow.in_port.ofp_port = 0;
1435         match->wc.masks.in_port.ofp_port = 0;
1436         break;
1437     case MFF_ACTSET_OUTPUT:
1438         match->flow.actset_output = 0;
1439         match->wc.masks.actset_output = 0;
1440         break;
1441
1442     case MFF_SKB_PRIORITY:
1443         match->flow.skb_priority = 0;
1444         match->wc.masks.skb_priority = 0;
1445         break;
1446
1447     case MFF_PKT_MARK:
1448         match->flow.pkt_mark = 0;
1449         match->wc.masks.pkt_mark = 0;
1450         break;
1451
1452     CASE_MFF_REGS:
1453         match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1454         break;
1455
1456     CASE_MFF_XREGS:
1457         match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1458         break;
1459
1460     case MFF_ETH_SRC:
1461         match->flow.dl_src = eth_addr_zero;
1462         match->wc.masks.dl_src = eth_addr_zero;
1463         break;
1464
1465     case MFF_ETH_DST:
1466         match->flow.dl_dst = eth_addr_zero;
1467         match->wc.masks.dl_dst = eth_addr_zero;
1468         break;
1469
1470     case MFF_ETH_TYPE:
1471         match->flow.dl_type = htons(0);
1472         match->wc.masks.dl_type = htons(0);
1473         break;
1474
1475     case MFF_VLAN_TCI:
1476         match_set_dl_tci_masked(match, htons(0), htons(0));
1477         break;
1478
1479     case MFF_DL_VLAN:
1480     case MFF_VLAN_VID:
1481         match_set_any_vid(match);
1482         break;
1483
1484     case MFF_DL_VLAN_PCP:
1485     case MFF_VLAN_PCP:
1486         match_set_any_pcp(match);
1487         break;
1488
1489     case MFF_MPLS_LABEL:
1490         match_set_any_mpls_label(match, 0);
1491         break;
1492
1493     case MFF_MPLS_TC:
1494         match_set_any_mpls_tc(match, 0);
1495         break;
1496
1497     case MFF_MPLS_BOS:
1498         match_set_any_mpls_bos(match, 0);
1499         break;
1500
1501     case MFF_IPV4_SRC:
1502     case MFF_ARP_SPA:
1503         match_set_nw_src_masked(match, htonl(0), htonl(0));
1504         break;
1505
1506     case MFF_IPV4_DST:
1507     case MFF_ARP_TPA:
1508         match_set_nw_dst_masked(match, htonl(0), htonl(0));
1509         break;
1510
1511     case MFF_IPV6_SRC:
1512         memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1513         memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1514         break;
1515
1516     case MFF_IPV6_DST:
1517         memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1518         memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1519         break;
1520
1521     case MFF_IPV6_LABEL:
1522         match->wc.masks.ipv6_label = htonl(0);
1523         match->flow.ipv6_label = htonl(0);
1524         break;
1525
1526     case MFF_IP_PROTO:
1527         match->wc.masks.nw_proto = 0;
1528         match->flow.nw_proto = 0;
1529         break;
1530
1531     case MFF_IP_DSCP:
1532     case MFF_IP_DSCP_SHIFTED:
1533         match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1534         match->flow.nw_tos &= ~IP_DSCP_MASK;
1535         break;
1536
1537     case MFF_IP_ECN:
1538         match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1539         match->flow.nw_tos &= ~IP_ECN_MASK;
1540         break;
1541
1542     case MFF_IP_TTL:
1543         match->wc.masks.nw_ttl = 0;
1544         match->flow.nw_ttl = 0;
1545         break;
1546
1547     case MFF_IP_FRAG:
1548         match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1549         match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1550         break;
1551
1552     case MFF_ARP_OP:
1553         match->wc.masks.nw_proto = 0;
1554         match->flow.nw_proto = 0;
1555         break;
1556
1557     case MFF_ARP_SHA:
1558     case MFF_ND_SLL:
1559         match->flow.arp_sha = eth_addr_zero;
1560         match->wc.masks.arp_sha = eth_addr_zero;
1561         break;
1562
1563     case MFF_ARP_THA:
1564     case MFF_ND_TLL:
1565         match->flow.arp_tha = eth_addr_zero;
1566         match->wc.masks.arp_tha = eth_addr_zero;
1567         break;
1568
1569     case MFF_TCP_SRC:
1570     case MFF_UDP_SRC:
1571     case MFF_SCTP_SRC:
1572     case MFF_ICMPV4_TYPE:
1573     case MFF_ICMPV6_TYPE:
1574         match->wc.masks.tp_src = htons(0);
1575         match->flow.tp_src = htons(0);
1576         break;
1577
1578     case MFF_TCP_DST:
1579     case MFF_UDP_DST:
1580     case MFF_SCTP_DST:
1581     case MFF_ICMPV4_CODE:
1582     case MFF_ICMPV6_CODE:
1583         match->wc.masks.tp_dst = htons(0);
1584         match->flow.tp_dst = htons(0);
1585         break;
1586
1587     case MFF_TCP_FLAGS:
1588         match->wc.masks.tcp_flags = htons(0);
1589         match->flow.tcp_flags = htons(0);
1590         break;
1591
1592     case MFF_ND_TARGET:
1593         memset(&match->wc.masks.nd_target, 0,
1594                sizeof match->wc.masks.nd_target);
1595         memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1596         break;
1597
1598     case MFF_N_IDS:
1599     default:
1600         OVS_NOT_REACHED();
1601     }
1602 }
1603
1604 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1605  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1606  * with a 1-bit indicating that the corresponding value bit must match and a
1607  * 0-bit indicating a don't-care.
1608  *
1609  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1610  * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
1611  * call is equivalent to mf_set_wild(mf, match).
1612  *
1613  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1614  * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1615  *
1616  * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1617  * with the request or NULL if there is no error. The caller is reponsible
1618  * for freeing the string.*/
1619 enum ofputil_protocol
1620 mf_set(const struct mf_field *mf,
1621        const union mf_value *value, const union mf_value *mask,
1622        struct match *match, char **err_str)
1623 {
1624     if (!mask || is_all_ones(mask, mf->n_bytes)) {
1625         mf_set_value(mf, value, match, err_str);
1626         return mf->usable_protocols_exact;
1627     } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1628         /* Tunnel metadata matches on the existence of the field itself, so
1629          * it still needs to be encoded even if the value is wildcarded. */
1630         mf_set_wild(mf, match, err_str);
1631         return OFPUTIL_P_ANY;
1632     }
1633
1634     if (err_str) {
1635         *err_str = NULL;
1636     }
1637
1638     switch (mf->id) {
1639     case MFF_RECIRC_ID:
1640     case MFF_CONJ_ID:
1641     case MFF_IN_PORT:
1642     case MFF_IN_PORT_OXM:
1643     case MFF_ACTSET_OUTPUT:
1644     case MFF_SKB_PRIORITY:
1645     case MFF_ETH_TYPE:
1646     case MFF_DL_VLAN:
1647     case MFF_DL_VLAN_PCP:
1648     case MFF_VLAN_PCP:
1649     case MFF_MPLS_LABEL:
1650     case MFF_MPLS_TC:
1651     case MFF_MPLS_BOS:
1652     case MFF_IP_PROTO:
1653     case MFF_IP_TTL:
1654     case MFF_IP_DSCP:
1655     case MFF_IP_DSCP_SHIFTED:
1656     case MFF_IP_ECN:
1657     case MFF_ARP_OP:
1658     case MFF_ICMPV4_TYPE:
1659     case MFF_ICMPV4_CODE:
1660     case MFF_ICMPV6_TYPE:
1661     case MFF_ICMPV6_CODE:
1662         return OFPUTIL_P_NONE;
1663
1664     case MFF_DP_HASH:
1665         match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1666         break;
1667     case MFF_TUN_ID:
1668         match_set_tun_id_masked(match, value->be64, mask->be64);
1669         break;
1670     case MFF_TUN_SRC:
1671         match_set_tun_src_masked(match, value->be32, mask->be32);
1672         break;
1673     case MFF_TUN_DST:
1674         match_set_tun_dst_masked(match, value->be32, mask->be32);
1675         break;
1676     case MFF_TUN_FLAGS:
1677         match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1678         break;
1679     case MFF_TUN_GBP_ID:
1680         match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1681         break;
1682     case MFF_TUN_GBP_FLAGS:
1683         match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1684         break;
1685     case MFF_TUN_TTL:
1686         match_set_tun_ttl_masked(match, value->u8, mask->u8);
1687         break;
1688     case MFF_TUN_TOS:
1689         match_set_tun_tos_masked(match, value->u8, mask->u8);
1690         break;
1691     CASE_MFF_TUN_METADATA:
1692         tun_metadata_set_match(mf, value, mask, match, err_str);
1693         break;
1694
1695     case MFF_METADATA:
1696         match_set_metadata_masked(match, value->be64, mask->be64);
1697         break;
1698
1699     CASE_MFF_REGS:
1700         match_set_reg_masked(match, mf->id - MFF_REG0,
1701                              ntohl(value->be32), ntohl(mask->be32));
1702         break;
1703
1704     CASE_MFF_XREGS:
1705         match_set_xreg_masked(match, mf->id - MFF_XREG0,
1706                               ntohll(value->be64), ntohll(mask->be64));
1707         break;
1708
1709     case MFF_PKT_MARK:
1710         match_set_pkt_mark_masked(match, ntohl(value->be32),
1711                                   ntohl(mask->be32));
1712         break;
1713
1714     case MFF_ETH_DST:
1715         match_set_dl_dst_masked(match, value->mac, mask->mac);
1716         break;
1717
1718     case MFF_ETH_SRC:
1719         match_set_dl_src_masked(match, value->mac, mask->mac);
1720         break;
1721
1722     case MFF_ARP_SHA:
1723     case MFF_ND_SLL:
1724         match_set_arp_sha_masked(match, value->mac, mask->mac);
1725         break;
1726
1727     case MFF_ARP_THA:
1728     case MFF_ND_TLL:
1729         match_set_arp_tha_masked(match, value->mac, mask->mac);
1730         break;
1731
1732     case MFF_VLAN_TCI:
1733         match_set_dl_tci_masked(match, value->be16, mask->be16);
1734         break;
1735
1736     case MFF_VLAN_VID:
1737         match_set_vlan_vid_masked(match, value->be16, mask->be16);
1738         break;
1739
1740     case MFF_IPV4_SRC:
1741         match_set_nw_src_masked(match, value->be32, mask->be32);
1742         break;
1743
1744     case MFF_IPV4_DST:
1745         match_set_nw_dst_masked(match, value->be32, mask->be32);
1746         break;
1747
1748     case MFF_IPV6_SRC:
1749         match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1750         break;
1751
1752     case MFF_IPV6_DST:
1753         match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1754         break;
1755
1756     case MFF_IPV6_LABEL:
1757         if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1758             mf_set_value(mf, value, match, err_str);
1759         } else {
1760             match_set_ipv6_label_masked(match, value->be32, mask->be32);
1761         }
1762         break;
1763
1764     case MFF_ND_TARGET:
1765         match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1766         break;
1767
1768     case MFF_IP_FRAG:
1769         match_set_nw_frag_masked(match, value->u8, mask->u8);
1770         break;
1771
1772     case MFF_ARP_SPA:
1773         match_set_nw_src_masked(match, value->be32, mask->be32);
1774         break;
1775
1776     case MFF_ARP_TPA:
1777         match_set_nw_dst_masked(match, value->be32, mask->be32);
1778         break;
1779
1780     case MFF_TCP_SRC:
1781     case MFF_UDP_SRC:
1782     case MFF_SCTP_SRC:
1783         match_set_tp_src_masked(match, value->be16, mask->be16);
1784         break;
1785
1786     case MFF_TCP_DST:
1787     case MFF_UDP_DST:
1788     case MFF_SCTP_DST:
1789         match_set_tp_dst_masked(match, value->be16, mask->be16);
1790         break;
1791
1792     case MFF_TCP_FLAGS:
1793         match_set_tcp_flags_masked(match, value->be16, mask->be16);
1794         break;
1795
1796     case MFF_N_IDS:
1797     default:
1798         OVS_NOT_REACHED();
1799     }
1800
1801     return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1802              || ip_is_cidr(mask->be32))
1803             ? mf->usable_protocols_cidr
1804             : mf->usable_protocols_bitwise);
1805 }
1806
1807 static enum ofperr
1808 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1809            const char *type)
1810 {
1811     if (!sf->field) {
1812         VLOG_WARN_RL(&rl, "unknown %s field", type);
1813         return OFPERR_OFPBAC_BAD_SET_TYPE;
1814     } else if (!sf->n_bits) {
1815         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1816         return OFPERR_OFPBAC_BAD_SET_LEN;
1817     } else if (sf->ofs >= sf->field->n_bits) {
1818         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1819                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1820         return OFPERR_OFPBAC_BAD_SET_LEN;
1821     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1822         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1823                      "of %s field %s", sf->ofs, sf->n_bits,
1824                      sf->field->n_bits, type, sf->field->name);
1825         return OFPERR_OFPBAC_BAD_SET_LEN;
1826     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1827         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1828                      type, sf->field->name);
1829         return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1830     } else {
1831         return 0;
1832     }
1833 }
1834
1835 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1836  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1837  * ofp_mkerr()).  */
1838 enum ofperr
1839 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1840 {
1841     return mf_check__(sf, flow, "source");
1842 }
1843
1844 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1845  * if so, otherwise an OpenFlow error code (e.g. as returned by
1846  * ofp_mkerr()). */
1847 enum ofperr
1848 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1849 {
1850     int error = mf_check__(sf, flow, "destination");
1851     if (!error && !sf->field->writable) {
1852         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1853                      sf->field->name);
1854         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1855     }
1856     return error;
1857 }
1858
1859 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1860  * 'value' and 'mask', respectively. */
1861 void
1862 mf_get(const struct mf_field *mf, const struct match *match,
1863        union mf_value *value, union mf_value *mask)
1864 {
1865     mf_get_value(mf, &match->flow, value);
1866     mf_get_mask(mf, &match->wc, mask);
1867 }
1868
1869 static char *
1870 mf_from_integer_string(const struct mf_field *mf, const char *s,
1871                        uint8_t *valuep, uint8_t *maskp)
1872 {
1873     char *tail;
1874     const char *err_str = "";
1875     int err;
1876
1877     err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1878     if (err || (*tail != '\0' && *tail != '/')) {
1879         err_str = "value";
1880         goto syntax_error;
1881     }
1882
1883     if (*tail == '/') {
1884         err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1885         if (err || *tail != '\0') {
1886             err_str = "mask";
1887             goto syntax_error;
1888         }
1889     } else {
1890         memset(maskp, 0xff, mf->n_bytes);
1891     }
1892
1893     return NULL;
1894
1895 syntax_error:
1896     if (err == ERANGE) {
1897         return xasprintf("%s: %s too large for %u-byte field %s",
1898                          s, err_str, mf->n_bytes, mf->name);
1899     } else {
1900         return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1901     }
1902 }
1903
1904 static char *
1905 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1906                         struct eth_addr *mac, struct eth_addr *mask)
1907 {
1908     int n;
1909
1910     ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1911
1912     n = -1;
1913     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
1914         && n == strlen(s)) {
1915         *mask = eth_addr_exact;
1916         return NULL;
1917     }
1918
1919     n = -1;
1920     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1921                  ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
1922         && n == strlen(s)) {
1923         return NULL;
1924     }
1925
1926     return xasprintf("%s: invalid Ethernet address", s);
1927 }
1928
1929 static char *
1930 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1931                     ovs_be32 *ip, ovs_be32 *mask)
1932 {
1933     int prefix;
1934
1935     ovs_assert(mf->n_bytes == sizeof *ip);
1936
1937     if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1938                  IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1939         /* OK. */
1940     } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1941         if (prefix <= 0 || prefix > 32) {
1942             return xasprintf("%s: network prefix bits not between 0 and "
1943                              "32", s);
1944         }
1945         *mask = be32_prefix_mask(prefix);
1946     } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1947         *mask = OVS_BE32_MAX;
1948     } else {
1949         return xasprintf("%s: invalid IP address", s);
1950     }
1951     return NULL;
1952 }
1953
1954 static char *
1955 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1956                     struct in6_addr *value, struct in6_addr *mask)
1957 {
1958     char *str = xstrdup(s);
1959     char *save_ptr = NULL;
1960     const char *name, *netmask;
1961     int retval;
1962
1963     ovs_assert(mf->n_bytes == sizeof *value);
1964
1965     name = strtok_r(str, "/", &save_ptr);
1966     retval = name ? lookup_ipv6(name, value) : EINVAL;
1967     if (retval) {
1968         char *err;
1969
1970         err = xasprintf("%s: could not convert to IPv6 address", str);
1971         free(str);
1972
1973         return err;
1974     }
1975
1976     netmask = strtok_r(NULL, "/", &save_ptr);
1977     if (netmask) {
1978         if (inet_pton(AF_INET6, netmask, mask) != 1) {
1979             int prefix = atoi(netmask);
1980             if (prefix <= 0 || prefix > 128) {
1981                 free(str);
1982                 return xasprintf("%s: prefix bits not between 1 and 128", s);
1983             } else {
1984                 *mask = ipv6_create_mask(prefix);
1985             }
1986         }
1987     } else {
1988         *mask = in6addr_exact;
1989     }
1990     free(str);
1991
1992     return NULL;
1993 }
1994
1995 static char *
1996 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1997                         ovs_be16 *valuep, ovs_be16 *maskp)
1998 {
1999     ofp_port_t port;
2000
2001     ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2002
2003     if (ofputil_port_from_string(s, &port)) {
2004         *valuep = htons(ofp_to_u16(port));
2005         *maskp = OVS_BE16_MAX;
2006         return NULL;
2007     }
2008     return xasprintf("%s: port value out of range for %s", s, mf->name);
2009 }
2010
2011 static char *
2012 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2013                           ovs_be32 *valuep, ovs_be32 *maskp)
2014 {
2015     ofp_port_t port;
2016
2017     ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2018     if (ofputil_port_from_string(s, &port)) {
2019         *valuep = ofputil_port_to_ofp11(port);
2020         *maskp = OVS_BE32_MAX;
2021         return NULL;
2022     }
2023     return xasprintf("%s: port value out of range for %s", s, mf->name);
2024 }
2025
2026 struct frag_handling {
2027     const char *name;
2028     uint8_t mask;
2029     uint8_t value;
2030 };
2031
2032 static const struct frag_handling all_frags[] = {
2033 #define A FLOW_NW_FRAG_ANY
2034 #define L FLOW_NW_FRAG_LATER
2035     /* name               mask  value */
2036
2037     { "no",               A|L,  0     },
2038     { "first",            A|L,  A     },
2039     { "later",            A|L,  A|L   },
2040
2041     { "no",               A,    0     },
2042     { "yes",              A,    A     },
2043
2044     { "not_later",        L,    0     },
2045     { "later",            L,    L     },
2046 #undef A
2047 #undef L
2048 };
2049
2050 static char *
2051 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2052 {
2053     const struct frag_handling *h;
2054
2055     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2056         if (!strcasecmp(s, h->name)) {
2057             /* We force the upper bits of the mask on to make mf_parse_value()
2058              * happy (otherwise it will never think it's an exact match.) */
2059             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2060             *valuep = h->value;
2061             return NULL;
2062         }
2063     }
2064
2065     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2066                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2067 }
2068
2069 static char *
2070 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2071                const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2072                ovs_be16 *maskp)
2073 {
2074     int err;
2075     char *err_str;
2076     uint32_t flags, mask;
2077
2078     err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2079                       &flags, ntohs(allowed), maskp ? &mask : NULL);
2080     if (err < 0) {
2081         return err_str;
2082     }
2083
2084     *flagsp = htons(flags);
2085     if (maskp) {
2086         *maskp = htons(mask);
2087     }
2088
2089     return NULL;
2090 }
2091
2092 static char *
2093 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2094 {
2095     return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2096                           TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2097 }
2098
2099 static char *
2100 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2101 {
2102     return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2103                           htons(FLOW_TNL_PUB_F_MASK), maskp);
2104 }
2105
2106 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2107  * NULL if successful, otherwise a malloc()'d string describing the error. */
2108 char *
2109 mf_parse(const struct mf_field *mf, const char *s,
2110          union mf_value *value, union mf_value *mask)
2111 {
2112     char *error;
2113
2114     if (!strcmp(s, "*")) {
2115         memset(value, 0, mf->n_bytes);
2116         memset(mask, 0, mf->n_bytes);
2117         return NULL;
2118     }
2119
2120     switch (mf->string) {
2121     case MFS_DECIMAL:
2122     case MFS_HEXADECIMAL:
2123         error = mf_from_integer_string(mf, s,
2124                                        (uint8_t *) value, (uint8_t *) mask);
2125         break;
2126
2127     case MFS_ETHERNET:
2128         error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2129         break;
2130
2131     case MFS_IPV4:
2132         error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2133         break;
2134
2135     case MFS_IPV6:
2136         error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2137         break;
2138
2139     case MFS_OFP_PORT:
2140         error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2141         break;
2142
2143     case MFS_OFP_PORT_OXM:
2144         error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2145         break;
2146
2147     case MFS_FRAG:
2148         error = mf_from_frag_string(s, &value->u8, &mask->u8);
2149         break;
2150
2151     case MFS_TNL_FLAGS:
2152         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2153         error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2154         break;
2155
2156     case MFS_TCP_FLAGS:
2157         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2158         error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2159         break;
2160
2161     default:
2162         OVS_NOT_REACHED();
2163     }
2164
2165     if (!error && !mf_is_mask_valid(mf, mask)) {
2166         error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2167     }
2168     return error;
2169 }
2170
2171 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2172  * successful, otherwise a malloc()'d string describing the error. */
2173 char *
2174 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2175 {
2176     union mf_value mask;
2177     char *error;
2178
2179     error = mf_parse(mf, s, value, &mask);
2180     if (error) {
2181         return error;
2182     }
2183
2184     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2185         return xasprintf("%s: wildcards not allowed here", s);
2186     }
2187     return NULL;
2188 }
2189
2190 static void
2191 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2192                          const uint8_t *maskp, struct ds *s)
2193 {
2194     if (mf->string == MFS_HEXADECIMAL) {
2195         ds_put_hex(s, valuep, mf->n_bytes);
2196     } else {
2197         unsigned long long int integer = 0;
2198         int i;
2199
2200         ovs_assert(mf->n_bytes <= 8);
2201         for (i = 0; i < mf->n_bytes; i++) {
2202             integer = (integer << 8) | valuep[i];
2203         }
2204         ds_put_format(s, "%lld", integer);
2205     }
2206
2207     if (maskp) {
2208         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2209          * not sure that that a bit-mask written in decimal is ever easier to
2210          * understand than the same bit-mask written in hexadecimal. */
2211         ds_put_char(s, '/');
2212         ds_put_hex(s, maskp, mf->n_bytes);
2213     }
2214 }
2215
2216 static void
2217 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2218 {
2219     const struct frag_handling *h;
2220
2221     mask &= FLOW_NW_FRAG_MASK;
2222     value &= mask;
2223
2224     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2225         if (value == h->value && mask == h->mask) {
2226             ds_put_cstr(s, h->name);
2227             return;
2228         }
2229     }
2230     ds_put_cstr(s, "<error>");
2231 }
2232
2233 static void
2234 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2235 {
2236     format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2237                         ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2238 }
2239
2240 static void
2241 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2242 {
2243     format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2244                         TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2245 }
2246
2247 /* Appends to 's' a string representation of field 'mf' whose value is in
2248  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2249 void
2250 mf_format(const struct mf_field *mf,
2251           const union mf_value *value, const union mf_value *mask,
2252           struct ds *s)
2253 {
2254     if (mask) {
2255         if (is_all_zeros(mask, mf->n_bytes)) {
2256             ds_put_cstr(s, "ANY");
2257             return;
2258         } else if (is_all_ones(mask, mf->n_bytes)) {
2259             mask = NULL;
2260         }
2261     }
2262
2263     switch (mf->string) {
2264     case MFS_OFP_PORT_OXM:
2265         if (!mask) {
2266             ofp_port_t port;
2267             ofputil_port_from_ofp11(value->be32, &port);
2268             ofputil_format_port(port, s);
2269             break;
2270         }
2271         /* fall through */
2272     case MFS_OFP_PORT:
2273         if (!mask) {
2274             ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2275             break;
2276         }
2277         /* fall through */
2278     case MFS_DECIMAL:
2279     case MFS_HEXADECIMAL:
2280         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2281         break;
2282
2283     case MFS_ETHERNET:
2284         eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2285         break;
2286
2287     case MFS_IPV4:
2288         ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2289         break;
2290
2291     case MFS_IPV6:
2292         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2293         break;
2294
2295     case MFS_FRAG:
2296         mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2297         break;
2298
2299     case MFS_TNL_FLAGS:
2300         mf_format_tnl_flags_string(value->be16,
2301                                    mask ? mask->be16 : OVS_BE16_MAX, s);
2302         break;
2303
2304     case MFS_TCP_FLAGS:
2305         mf_format_tcp_flags_string(value->be16,
2306                                    mask ? mask->be16 : OVS_BE16_MAX, s);
2307         break;
2308
2309     default:
2310         OVS_NOT_REACHED();
2311     }
2312 }
2313 \f
2314 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2315  * least-significant bits in 'x'.
2316  */
2317 void
2318 mf_write_subfield_flow(const struct mf_subfield *sf,
2319                        const union mf_subvalue *x, struct flow *flow)
2320 {
2321     const struct mf_field *field = sf->field;
2322     union mf_value value;
2323
2324     mf_get_value(field, flow, &value);
2325     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2326                  sf->ofs, sf->n_bits);
2327     mf_set_flow_value(field, &value, flow);
2328 }
2329
2330 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2331  * least-significant bits in 'x'.
2332  */
2333 void
2334 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2335                   struct match *match)
2336 {
2337     const struct mf_field *field = sf->field;
2338     union mf_value value, mask;
2339
2340     mf_get(field, match, &value, &mask);
2341     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2342     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2343     mf_set(field, &value, &mask, match, NULL);
2344 }
2345
2346 /* 'v' and 'm' correspond to values of 'field'.  This function copies them into
2347  * 'match' in the correspond positions. */
2348 void
2349 mf_mask_subfield(const struct mf_field *field,
2350                  const union mf_subvalue *v,
2351                  const union mf_subvalue *m,
2352                  struct match *match)
2353 {
2354     union mf_value value, mask;
2355
2356     mf_get(field, match, &value, &mask);
2357     bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2358     bitwise_copy(m, sizeof *m, 0, &mask,  field->n_bytes, 0, field->n_bits);
2359     mf_set(field, &value, &mask, match, NULL);
2360 }
2361
2362 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2363  * reading 'flow', e.g. as checked by mf_check_src(). */
2364 void
2365 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2366                  union mf_subvalue *x)
2367 {
2368     union mf_value value;
2369
2370     mf_get_value(sf->field, flow, &value);
2371
2372     memset(x, 0, sizeof *x);
2373     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2374                  x, sizeof *x, 0,
2375                  sf->n_bits);
2376 }
2377
2378 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2379  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2380  * less. */
2381 uint64_t
2382 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2383 {
2384     union mf_value value;
2385
2386     mf_get_value(sf->field, flow, &value);
2387     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2388 }
2389
2390 void
2391 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2392 {
2393     ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2394 }
2395
2396 void
2397 field_array_set(enum mf_field_id id, const union mf_value *value,
2398                 struct field_array *fa)
2399 {
2400     ovs_assert(id < MFF_N_IDS);
2401     bitmap_set1(fa->used.bm, id);
2402     fa->value[id] = *value;
2403 }