nx-match: Move all knowledge of OXM/NXM here.
[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 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1080  *
1081  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1082  * prerequisites. */
1083 bool
1084 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1085 {
1086     union mf_value value;
1087
1088     mf_get_value(mf, flow, &value);
1089     return is_all_zeros(&value, mf->n_bytes);
1090 }
1091
1092 /* Makes 'match' wildcard field 'mf'.
1093  *
1094  * The caller is responsible for ensuring that 'match' meets 'mf''s
1095  * prerequisites. */
1096 void
1097 mf_set_wild(const struct mf_field *mf, struct match *match)
1098 {
1099     switch (mf->id) {
1100     case MFF_DP_HASH:
1101         match->flow.dp_hash = 0;
1102         match->wc.masks.dp_hash = 0;
1103         break;
1104     case MFF_RECIRC_ID:
1105         match->flow.recirc_id = 0;
1106         match->wc.masks.recirc_id = 0;
1107         break;
1108     case MFF_TUN_ID:
1109         match_set_tun_id_masked(match, htonll(0), htonll(0));
1110         break;
1111     case MFF_TUN_SRC:
1112         match_set_tun_src_masked(match, htonl(0), htonl(0));
1113         break;
1114     case MFF_TUN_DST:
1115         match_set_tun_dst_masked(match, htonl(0), htonl(0));
1116         break;
1117     case MFF_TUN_FLAGS:
1118         match_set_tun_flags_masked(match, 0, 0);
1119         break;
1120     case MFF_TUN_TOS:
1121         match_set_tun_tos_masked(match, 0, 0);
1122         break;
1123     case MFF_TUN_TTL:
1124         match_set_tun_ttl_masked(match, 0, 0);
1125         break;
1126
1127     case MFF_METADATA:
1128         match_set_metadata_masked(match, htonll(0), htonll(0));
1129         break;
1130
1131     case MFF_IN_PORT:
1132     case MFF_IN_PORT_OXM:
1133         match->flow.in_port.ofp_port = 0;
1134         match->wc.masks.in_port.ofp_port = 0;
1135         break;
1136
1137     case MFF_SKB_PRIORITY:
1138         match->flow.skb_priority = 0;
1139         match->wc.masks.skb_priority = 0;
1140         break;
1141
1142     case MFF_PKT_MARK:
1143         match->flow.pkt_mark = 0;
1144         match->wc.masks.pkt_mark = 0;
1145         break;
1146
1147     CASE_MFF_REGS:
1148         match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1149         break;
1150
1151     CASE_MFF_XREGS:
1152         match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1153         break;
1154
1155     case MFF_ETH_SRC:
1156         memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1157         memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1158         break;
1159
1160     case MFF_ETH_DST:
1161         memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1162         memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1163         break;
1164
1165     case MFF_ETH_TYPE:
1166         match->flow.dl_type = htons(0);
1167         match->wc.masks.dl_type = htons(0);
1168         break;
1169
1170     case MFF_VLAN_TCI:
1171         match_set_dl_tci_masked(match, htons(0), htons(0));
1172         break;
1173
1174     case MFF_DL_VLAN:
1175     case MFF_VLAN_VID:
1176         match_set_any_vid(match);
1177         break;
1178
1179     case MFF_DL_VLAN_PCP:
1180     case MFF_VLAN_PCP:
1181         match_set_any_pcp(match);
1182         break;
1183
1184     case MFF_MPLS_LABEL:
1185         match_set_any_mpls_label(match, 0);
1186         break;
1187
1188     case MFF_MPLS_TC:
1189         match_set_any_mpls_tc(match, 0);
1190         break;
1191
1192     case MFF_MPLS_BOS:
1193         match_set_any_mpls_bos(match, 0);
1194         break;
1195
1196     case MFF_IPV4_SRC:
1197     case MFF_ARP_SPA:
1198         match_set_nw_src_masked(match, htonl(0), htonl(0));
1199         break;
1200
1201     case MFF_IPV4_DST:
1202     case MFF_ARP_TPA:
1203         match_set_nw_dst_masked(match, htonl(0), htonl(0));
1204         break;
1205
1206     case MFF_IPV6_SRC:
1207         memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1208         memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1209         break;
1210
1211     case MFF_IPV6_DST:
1212         memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1213         memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1214         break;
1215
1216     case MFF_IPV6_LABEL:
1217         match->wc.masks.ipv6_label = htonl(0);
1218         match->flow.ipv6_label = htonl(0);
1219         break;
1220
1221     case MFF_IP_PROTO:
1222         match->wc.masks.nw_proto = 0;
1223         match->flow.nw_proto = 0;
1224         break;
1225
1226     case MFF_IP_DSCP:
1227     case MFF_IP_DSCP_SHIFTED:
1228         match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1229         match->flow.nw_tos &= ~IP_DSCP_MASK;
1230         break;
1231
1232     case MFF_IP_ECN:
1233         match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1234         match->flow.nw_tos &= ~IP_ECN_MASK;
1235         break;
1236
1237     case MFF_IP_TTL:
1238         match->wc.masks.nw_ttl = 0;
1239         match->flow.nw_ttl = 0;
1240         break;
1241
1242     case MFF_IP_FRAG:
1243         match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1244         match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1245         break;
1246
1247     case MFF_ARP_OP:
1248         match->wc.masks.nw_proto = 0;
1249         match->flow.nw_proto = 0;
1250         break;
1251
1252     case MFF_ARP_SHA:
1253     case MFF_ND_SLL:
1254         memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1255         memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1256         break;
1257
1258     case MFF_ARP_THA:
1259     case MFF_ND_TLL:
1260         memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1261         memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1262         break;
1263
1264     case MFF_TCP_SRC:
1265     case MFF_UDP_SRC:
1266     case MFF_SCTP_SRC:
1267     case MFF_ICMPV4_TYPE:
1268     case MFF_ICMPV6_TYPE:
1269         match->wc.masks.tp_src = htons(0);
1270         match->flow.tp_src = htons(0);
1271         break;
1272
1273     case MFF_TCP_DST:
1274     case MFF_UDP_DST:
1275     case MFF_SCTP_DST:
1276     case MFF_ICMPV4_CODE:
1277     case MFF_ICMPV6_CODE:
1278         match->wc.masks.tp_dst = htons(0);
1279         match->flow.tp_dst = htons(0);
1280         break;
1281
1282     case MFF_TCP_FLAGS:
1283         match->wc.masks.tcp_flags = htons(0);
1284         match->flow.tcp_flags = htons(0);
1285         break;
1286
1287     case MFF_ND_TARGET:
1288         memset(&match->wc.masks.nd_target, 0,
1289                sizeof match->wc.masks.nd_target);
1290         memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1291         break;
1292
1293     case MFF_N_IDS:
1294     default:
1295         OVS_NOT_REACHED();
1296     }
1297 }
1298
1299 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1300  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1301  * with a 1-bit indicating that the corresponding value bit must match and a
1302  * 0-bit indicating a don't-care.
1303  *
1304  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1305  * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
1306  * call is equivalent to mf_set_wild(mf, match).
1307  *
1308  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1309  * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1310 enum ofputil_protocol
1311 mf_set(const struct mf_field *mf,
1312        const union mf_value *value, const union mf_value *mask,
1313        struct match *match)
1314 {
1315     if (!mask || is_all_ones(mask, mf->n_bytes)) {
1316         mf_set_value(mf, value, match);
1317         return mf->usable_protocols_exact;
1318     } else if (is_all_zeros(mask, mf->n_bytes)) {
1319         mf_set_wild(mf, match);
1320         return OFPUTIL_P_ANY;
1321     }
1322
1323     switch (mf->id) {
1324     case MFF_RECIRC_ID:
1325     case MFF_IN_PORT:
1326     case MFF_IN_PORT_OXM:
1327     case MFF_SKB_PRIORITY:
1328     case MFF_ETH_TYPE:
1329     case MFF_DL_VLAN:
1330     case MFF_DL_VLAN_PCP:
1331     case MFF_VLAN_PCP:
1332     case MFF_MPLS_LABEL:
1333     case MFF_MPLS_TC:
1334     case MFF_MPLS_BOS:
1335     case MFF_IP_PROTO:
1336     case MFF_IP_TTL:
1337     case MFF_IP_DSCP:
1338     case MFF_IP_DSCP_SHIFTED:
1339     case MFF_IP_ECN:
1340     case MFF_ARP_OP:
1341     case MFF_ICMPV4_TYPE:
1342     case MFF_ICMPV4_CODE:
1343     case MFF_ICMPV6_TYPE:
1344     case MFF_ICMPV6_CODE:
1345         return OFPUTIL_P_NONE;
1346
1347     case MFF_DP_HASH:
1348         match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1349         break;
1350     case MFF_TUN_ID:
1351         match_set_tun_id_masked(match, value->be64, mask->be64);
1352         break;
1353     case MFF_TUN_SRC:
1354         match_set_tun_src_masked(match, value->be32, mask->be32);
1355         break;
1356     case MFF_TUN_DST:
1357         match_set_tun_dst_masked(match, value->be32, mask->be32);
1358         break;
1359     case MFF_TUN_FLAGS:
1360         match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1361         break;
1362     case MFF_TUN_TTL:
1363         match_set_tun_ttl_masked(match, value->u8, mask->u8);
1364         break;
1365     case MFF_TUN_TOS:
1366         match_set_tun_tos_masked(match, value->u8, mask->u8);
1367         break;
1368
1369     case MFF_METADATA:
1370         match_set_metadata_masked(match, value->be64, mask->be64);
1371         break;
1372
1373     CASE_MFF_REGS:
1374         match_set_reg_masked(match, mf->id - MFF_REG0,
1375                              ntohl(value->be32), ntohl(mask->be32));
1376         break;
1377
1378     CASE_MFF_XREGS:
1379         match_set_xreg_masked(match, mf->id - MFF_XREG0,
1380                               ntohll(value->be64), ntohll(mask->be64));
1381         break;
1382
1383     case MFF_PKT_MARK:
1384         match_set_pkt_mark_masked(match, ntohl(value->be32),
1385                                   ntohl(mask->be32));
1386         break;
1387
1388     case MFF_ETH_DST:
1389         match_set_dl_dst_masked(match, value->mac, mask->mac);
1390         break;
1391
1392     case MFF_ETH_SRC:
1393         match_set_dl_src_masked(match, value->mac, mask->mac);
1394         break;
1395
1396     case MFF_ARP_SHA:
1397     case MFF_ND_SLL:
1398         match_set_arp_sha_masked(match, value->mac, mask->mac);
1399         break;
1400
1401     case MFF_ARP_THA:
1402     case MFF_ND_TLL:
1403         match_set_arp_tha_masked(match, value->mac, mask->mac);
1404         break;
1405
1406     case MFF_VLAN_TCI:
1407         match_set_dl_tci_masked(match, value->be16, mask->be16);
1408         break;
1409
1410     case MFF_VLAN_VID:
1411         match_set_vlan_vid_masked(match, value->be16, mask->be16);
1412         break;
1413
1414     case MFF_IPV4_SRC:
1415         match_set_nw_src_masked(match, value->be32, mask->be32);
1416         break;
1417
1418     case MFF_IPV4_DST:
1419         match_set_nw_dst_masked(match, value->be32, mask->be32);
1420         break;
1421
1422     case MFF_IPV6_SRC:
1423         match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1424         break;
1425
1426     case MFF_IPV6_DST:
1427         match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1428         break;
1429
1430     case MFF_IPV6_LABEL:
1431         if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1432             mf_set_value(mf, value, match);
1433         } else {
1434             match_set_ipv6_label_masked(match, value->be32, mask->be32);
1435         }
1436         break;
1437
1438     case MFF_ND_TARGET:
1439         match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1440         break;
1441
1442     case MFF_IP_FRAG:
1443         match_set_nw_frag_masked(match, value->u8, mask->u8);
1444         break;
1445
1446     case MFF_ARP_SPA:
1447         match_set_nw_src_masked(match, value->be32, mask->be32);
1448         break;
1449
1450     case MFF_ARP_TPA:
1451         match_set_nw_dst_masked(match, value->be32, mask->be32);
1452         break;
1453
1454     case MFF_TCP_SRC:
1455     case MFF_UDP_SRC:
1456     case MFF_SCTP_SRC:
1457         match_set_tp_src_masked(match, value->be16, mask->be16);
1458         break;
1459
1460     case MFF_TCP_DST:
1461     case MFF_UDP_DST:
1462     case MFF_SCTP_DST:
1463         match_set_tp_dst_masked(match, value->be16, mask->be16);
1464         break;
1465
1466     case MFF_TCP_FLAGS:
1467         match_set_tcp_flags_masked(match, value->be16, mask->be16);
1468         break;
1469
1470     case MFF_N_IDS:
1471     default:
1472         OVS_NOT_REACHED();
1473     }
1474
1475     return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1476              || ip_is_cidr(mask->be32))
1477             ? mf->usable_protocols_cidr
1478             : mf->usable_protocols_bitwise);
1479 }
1480
1481 static enum ofperr
1482 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1483            const char *type)
1484 {
1485     if (!sf->field) {
1486         VLOG_WARN_RL(&rl, "unknown %s field", type);
1487         return OFPERR_OFPBAC_BAD_SET_TYPE;
1488     } else if (!sf->n_bits) {
1489         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1490         return OFPERR_OFPBAC_BAD_SET_LEN;
1491     } else if (sf->ofs >= sf->field->n_bits) {
1492         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1493                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1494         return OFPERR_OFPBAC_BAD_SET_LEN;
1495     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1496         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1497                      "of %s field %s", sf->ofs, sf->n_bits,
1498                      sf->field->n_bits, type, sf->field->name);
1499         return OFPERR_OFPBAC_BAD_SET_LEN;
1500     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1501         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1502                      type, sf->field->name);
1503         return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1504     } else {
1505         return 0;
1506     }
1507 }
1508
1509 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1510  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1511  * ofp_mkerr()).  */
1512 enum ofperr
1513 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1514 {
1515     return mf_check__(sf, flow, "source");
1516 }
1517
1518 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1519  * if so, otherwise an OpenFlow error code (e.g. as returned by
1520  * ofp_mkerr()). */
1521 enum ofperr
1522 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1523 {
1524     int error = mf_check__(sf, flow, "destination");
1525     if (!error && !sf->field->writable) {
1526         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1527                      sf->field->name);
1528         return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1529     }
1530     return error;
1531 }
1532
1533 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1534  * 'value' and 'mask', respectively. */
1535 void
1536 mf_get(const struct mf_field *mf, const struct match *match,
1537        union mf_value *value, union mf_value *mask)
1538 {
1539     mf_get_value(mf, &match->flow, value);
1540     mf_get_mask(mf, &match->wc, mask);
1541 }
1542
1543 static char *
1544 mf_from_integer_string(const struct mf_field *mf, const char *s,
1545                        uint8_t *valuep, uint8_t *maskp)
1546 {
1547     unsigned long long int integer, mask;
1548     char *tail;
1549     int i;
1550
1551     errno = 0;
1552     integer = strtoull(s, &tail, 0);
1553     if (errno || (*tail != '\0' && *tail != '/')) {
1554         goto syntax_error;
1555     }
1556
1557     if (*tail == '/') {
1558         mask = strtoull(tail + 1, &tail, 0);
1559         if (errno || *tail != '\0') {
1560             goto syntax_error;
1561         }
1562     } else {
1563         mask = ULLONG_MAX;
1564     }
1565
1566     for (i = mf->n_bytes - 1; i >= 0; i--) {
1567         valuep[i] = integer;
1568         maskp[i] = mask;
1569         integer >>= 8;
1570         mask >>= 8;
1571     }
1572     if (integer) {
1573         return xasprintf("%s: value too large for %u-byte field %s",
1574                          s, mf->n_bytes, mf->name);
1575     }
1576     return NULL;
1577
1578 syntax_error:
1579     return xasprintf("%s: bad syntax for %s", s, mf->name);
1580 }
1581
1582 static char *
1583 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1584                         uint8_t mac[ETH_ADDR_LEN],
1585                         uint8_t mask[ETH_ADDR_LEN])
1586 {
1587     int n;
1588
1589     ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1590
1591     n = -1;
1592     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1593         && n == strlen(s)) {
1594         memset(mask, 0xff, ETH_ADDR_LEN);
1595         return NULL;
1596     }
1597
1598     n = -1;
1599     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1600                  ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1601         && n == strlen(s)) {
1602         return NULL;
1603     }
1604
1605     return xasprintf("%s: invalid Ethernet address", s);
1606 }
1607
1608 static char *
1609 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1610                     ovs_be32 *ip, ovs_be32 *mask)
1611 {
1612     int prefix;
1613
1614     ovs_assert(mf->n_bytes == sizeof *ip);
1615
1616     if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1617                  IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1618         /* OK. */
1619     } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1620         if (prefix <= 0 || prefix > 32) {
1621             return xasprintf("%s: network prefix bits not between 1 and "
1622                              "32", s);
1623         } else if (prefix == 32) {
1624             *mask = OVS_BE32_MAX;
1625         } else {
1626             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1627         }
1628     } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1629         *mask = OVS_BE32_MAX;
1630     } else {
1631         return xasprintf("%s: invalid IP address", s);
1632     }
1633     return NULL;
1634 }
1635
1636 static char *
1637 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1638                     struct in6_addr *value, struct in6_addr *mask)
1639 {
1640     char *str = xstrdup(s);
1641     char *save_ptr = NULL;
1642     const char *name, *netmask;
1643     int retval;
1644
1645     ovs_assert(mf->n_bytes == sizeof *value);
1646
1647     name = strtok_r(str, "/", &save_ptr);
1648     retval = name ? lookup_ipv6(name, value) : EINVAL;
1649     if (retval) {
1650         char *err;
1651
1652         err = xasprintf("%s: could not convert to IPv6 address", str);
1653         free(str);
1654
1655         return err;
1656     }
1657
1658     netmask = strtok_r(NULL, "/", &save_ptr);
1659     if (netmask) {
1660         if (inet_pton(AF_INET6, netmask, mask) != 1) {
1661             int prefix = atoi(netmask);
1662             if (prefix <= 0 || prefix > 128) {
1663                 free(str);
1664                 return xasprintf("%s: prefix bits not between 1 and 128", s);
1665             } else {
1666                 *mask = ipv6_create_mask(prefix);
1667             }
1668         }
1669     } else {
1670         *mask = in6addr_exact;
1671     }
1672     free(str);
1673
1674     return NULL;
1675 }
1676
1677 static char *
1678 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1679                         ovs_be16 *valuep, ovs_be16 *maskp)
1680 {
1681     ofp_port_t port;
1682
1683     ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1684
1685     if (ofputil_port_from_string(s, &port)) {
1686         *valuep = htons(ofp_to_u16(port));
1687         *maskp = OVS_BE16_MAX;
1688         return NULL;
1689     }
1690     return xasprintf("%s: port value out of range for %s", s, mf->name);
1691 }
1692
1693 static char *
1694 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1695                           ovs_be32 *valuep, ovs_be32 *maskp)
1696 {
1697     ofp_port_t port;
1698
1699     ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1700     if (ofputil_port_from_string(s, &port)) {
1701         *valuep = ofputil_port_to_ofp11(port);
1702         *maskp = OVS_BE32_MAX;
1703         return NULL;
1704     }
1705     return xasprintf("%s: port value out of range for %s", s, mf->name);
1706 }
1707
1708 struct frag_handling {
1709     const char *name;
1710     uint8_t mask;
1711     uint8_t value;
1712 };
1713
1714 static const struct frag_handling all_frags[] = {
1715 #define A FLOW_NW_FRAG_ANY
1716 #define L FLOW_NW_FRAG_LATER
1717     /* name               mask  value */
1718
1719     { "no",               A|L,  0     },
1720     { "first",            A|L,  A     },
1721     { "later",            A|L,  A|L   },
1722
1723     { "no",               A,    0     },
1724     { "yes",              A,    A     },
1725
1726     { "not_later",        L,    0     },
1727     { "later",            L,    L     },
1728 #undef A
1729 #undef L
1730 };
1731
1732 static char *
1733 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1734 {
1735     const struct frag_handling *h;
1736
1737     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1738         if (!strcasecmp(s, h->name)) {
1739             /* We force the upper bits of the mask on to make mf_parse_value()
1740              * happy (otherwise it will never think it's an exact match.) */
1741             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1742             *valuep = h->value;
1743             return NULL;
1744         }
1745     }
1746
1747     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1748                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1749 }
1750
1751 static int
1752 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1753                      ovs_be16 *res)
1754 {
1755     uint32_t result = 0;
1756     char *save_ptr = NULL;
1757     char *name;
1758     int rc = 0;
1759     char *s = xstrdup(s_);
1760
1761     for (name = strtok_r((char *)s, " |", &save_ptr); name;
1762          name = strtok_r(NULL, " |", &save_ptr)) {
1763         int name_len;
1764         unsigned long long int flags;
1765         uint32_t bit;
1766
1767         if (ovs_scan(name, "%lli", &flags)) {
1768             result |= flags;
1769             continue;
1770         }
1771         name_len = strlen(name);
1772         for (bit = 1; bit; bit <<= 1) {
1773             const char *fname = bit_to_string(bit);
1774             size_t len;
1775
1776             if (!fname) {
1777                 continue;
1778             }
1779
1780             len = strlen(fname);
1781             if (len != name_len) {
1782                 continue;
1783             }
1784             if (!strncmp(name, fname, len)) {
1785                 result |= bit;
1786                 break;
1787             }
1788         }
1789
1790         if (!bit) {
1791             rc = -ENOENT;
1792             goto out;
1793         }
1794     }
1795
1796     *res = htons(result);
1797 out:
1798     free(s);
1799     return rc;
1800 }
1801
1802 static char *
1803 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1804 {
1805     if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1806         *maskp = OVS_BE16_MAX;
1807         return NULL;
1808     }
1809
1810     return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1811                      "\"csum\", \"key\")", s);
1812 }
1813
1814 static char *
1815 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1816 {
1817     uint16_t flags = 0;
1818     uint16_t mask = 0;
1819     uint16_t bit;
1820     int n;
1821
1822     if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1823         *flagsp = htons(flags);
1824         *maskp = htons(mask);
1825         return NULL;
1826     }
1827     if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1828         *flagsp = htons(flags);
1829         *maskp = OVS_BE16_MAX;
1830         return NULL;
1831     }
1832
1833     while (*s != '\0') {
1834         bool set;
1835         int name_len;
1836
1837         switch (*s) {
1838         case '+':
1839             set = true;
1840             break;
1841         case '-':
1842             set = false;
1843             break;
1844         default:
1845             return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1846                              "or '-' (NOT SET)", s);
1847         }
1848         s++;
1849
1850         name_len = strcspn(s,"+-");
1851
1852         for (bit = 1; bit; bit <<= 1) {
1853             const char *fname = packet_tcp_flag_to_string(bit);
1854             size_t len;
1855
1856             if (!fname) {
1857                 continue;
1858             }
1859
1860             len = strlen(fname);
1861             if (len != name_len) {
1862                 continue;
1863             }
1864             if (!strncmp(s, fname, len)) {
1865                 if (mask & bit) {
1866                     return xasprintf("%s: Each TCP flag can be specified only "
1867                                      "once", s);
1868                 }
1869                 if (set) {
1870                     flags |= bit;
1871                 }
1872                 mask |= bit;
1873                 break;
1874             }
1875         }
1876
1877         if (!bit) {
1878             return xasprintf("%s: unknown TCP flag(s)", s);
1879         }
1880         s += name_len;
1881     }
1882
1883     *flagsp = htons(flags);
1884     *maskp = htons(mask);
1885     return NULL;
1886 }
1887
1888
1889 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1890  * NULL if successful, otherwise a malloc()'d string describing the error. */
1891 char *
1892 mf_parse(const struct mf_field *mf, const char *s,
1893          union mf_value *value, union mf_value *mask)
1894 {
1895     char *error;
1896
1897     if (!strcmp(s, "*")) {
1898         memset(value, 0, mf->n_bytes);
1899         memset(mask, 0, mf->n_bytes);
1900         return NULL;
1901     }
1902
1903     switch (mf->string) {
1904     case MFS_DECIMAL:
1905     case MFS_HEXADECIMAL:
1906         error = mf_from_integer_string(mf, s,
1907                                        (uint8_t *) value, (uint8_t *) mask);
1908         break;
1909
1910     case MFS_ETHERNET:
1911         error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1912         break;
1913
1914     case MFS_IPV4:
1915         error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1916         break;
1917
1918     case MFS_IPV6:
1919         error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1920         break;
1921
1922     case MFS_OFP_PORT:
1923         error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1924         break;
1925
1926     case MFS_OFP_PORT_OXM:
1927         error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1928         break;
1929
1930     case MFS_FRAG:
1931         error = mf_from_frag_string(s, &value->u8, &mask->u8);
1932         break;
1933
1934     case MFS_TNL_FLAGS:
1935         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1936         error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
1937         break;
1938
1939     case MFS_TCP_FLAGS:
1940         ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1941         error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
1942         break;
1943
1944     default:
1945         OVS_NOT_REACHED();
1946     }
1947
1948     if (!error && !mf_is_mask_valid(mf, mask)) {
1949         error = xasprintf("%s: invalid mask for field %s", s, mf->name);
1950     }
1951     return error;
1952 }
1953
1954 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1955  * successful, otherwise a malloc()'d string describing the error. */
1956 char *
1957 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1958 {
1959     union mf_value mask;
1960     char *error;
1961
1962     error = mf_parse(mf, s, value, &mask);
1963     if (error) {
1964         return error;
1965     }
1966
1967     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1968         return xasprintf("%s: wildcards not allowed here", s);
1969     }
1970     return NULL;
1971 }
1972
1973 static void
1974 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1975                          const uint8_t *maskp, struct ds *s)
1976 {
1977     unsigned long long int integer;
1978     int i;
1979
1980     ovs_assert(mf->n_bytes <= 8);
1981
1982     integer = 0;
1983     for (i = 0; i < mf->n_bytes; i++) {
1984         integer = (integer << 8) | valuep[i];
1985     }
1986     if (mf->string == MFS_HEXADECIMAL) {
1987         ds_put_format(s, "%#llx", integer);
1988     } else {
1989         ds_put_format(s, "%lld", integer);
1990     }
1991
1992     if (maskp) {
1993         unsigned long long int mask;
1994
1995         mask = 0;
1996         for (i = 0; i < mf->n_bytes; i++) {
1997             mask = (mask << 8) | maskp[i];
1998         }
1999
2000         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2001          * not sure that that a bit-mask written in decimal is ever easier to
2002          * understand than the same bit-mask written in hexadecimal. */
2003         ds_put_format(s, "/%#llx", mask);
2004     }
2005 }
2006
2007 static void
2008 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2009 {
2010     const struct frag_handling *h;
2011
2012     mask &= FLOW_NW_FRAG_MASK;
2013     value &= mask;
2014
2015     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2016         if (value == h->value && mask == h->mask) {
2017             ds_put_cstr(s, h->name);
2018             return;
2019         }
2020     }
2021     ds_put_cstr(s, "<error>");
2022 }
2023
2024 static void
2025 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2026 {
2027     format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2028 }
2029
2030 static void
2031 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2032 {
2033     format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2034                         TCP_FLAGS(mask));
2035 }
2036
2037 /* Appends to 's' a string representation of field 'mf' whose value is in
2038  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2039 void
2040 mf_format(const struct mf_field *mf,
2041           const union mf_value *value, const union mf_value *mask,
2042           struct ds *s)
2043 {
2044     if (mask) {
2045         if (is_all_zeros(mask, mf->n_bytes)) {
2046             ds_put_cstr(s, "ANY");
2047             return;
2048         } else if (is_all_ones(mask, mf->n_bytes)) {
2049             mask = NULL;
2050         }
2051     }
2052
2053     switch (mf->string) {
2054     case MFS_OFP_PORT_OXM:
2055         if (!mask) {
2056             ofp_port_t port;
2057             ofputil_port_from_ofp11(value->be32, &port);
2058             ofputil_format_port(port, s);
2059             break;
2060         }
2061         /* fall through */
2062     case MFS_OFP_PORT:
2063         if (!mask) {
2064             ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2065             break;
2066         }
2067         /* fall through */
2068     case MFS_DECIMAL:
2069     case MFS_HEXADECIMAL:
2070         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2071         break;
2072
2073     case MFS_ETHERNET:
2074         eth_format_masked(value->mac, mask->mac, s);
2075         break;
2076
2077     case MFS_IPV4:
2078         ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2079         break;
2080
2081     case MFS_IPV6:
2082         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2083         break;
2084
2085     case MFS_FRAG:
2086         mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2087         break;
2088
2089     case MFS_TNL_FLAGS:
2090         mf_format_tnl_flags_string(&value->be16, s);
2091         break;
2092
2093     case MFS_TCP_FLAGS:
2094         mf_format_tcp_flags_string(value->be16,
2095                                    mask ? mask->be16 : OVS_BE16_MAX, s);
2096         break;
2097
2098     default:
2099         OVS_NOT_REACHED();
2100     }
2101 }
2102 \f
2103 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2104  * least-significant bits in 'x'.
2105  */
2106 void
2107 mf_write_subfield_flow(const struct mf_subfield *sf,
2108                        const union mf_subvalue *x, struct flow *flow)
2109 {
2110     const struct mf_field *field = sf->field;
2111     union mf_value value;
2112
2113     mf_get_value(field, flow, &value);
2114     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2115                  sf->ofs, sf->n_bits);
2116     mf_set_flow_value(field, &value, flow);
2117 }
2118
2119 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2120  * least-significant bits in 'x'.
2121  */
2122 void
2123 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2124                   struct match *match)
2125 {
2126     const struct mf_field *field = sf->field;
2127     union mf_value value, mask;
2128
2129     mf_get(field, match, &value, &mask);
2130     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2131     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2132     mf_set(field, &value, &mask, match);
2133 }
2134
2135 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2136  * reading 'flow', e.g. as checked by mf_check_src(). */
2137 void
2138 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2139                  union mf_subvalue *x)
2140 {
2141     union mf_value value;
2142
2143     mf_get_value(sf->field, flow, &value);
2144
2145     memset(x, 0, sizeof *x);
2146     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2147                  x, sizeof *x, 0,
2148                  sf->n_bits);
2149 }
2150
2151 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2152  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2153  * less. */
2154 uint64_t
2155 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2156 {
2157     union mf_value value;
2158
2159     mf_get_value(sf->field, flow, &value);
2160     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2161 }
2162
2163 void
2164 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2165 {
2166     int i;
2167
2168     for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2169         if (subvalue->u8[i]) {
2170             ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2171             for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2172                 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2173             }
2174             return;
2175         }
2176     }
2177     ds_put_char(s, '0');
2178 }