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