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