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