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