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