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