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