2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "dynamic-string.h"
29 #include "ofp-errors.h"
31 #include "ovs-thread.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
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);
63 static void nxm_init(void);
65 /* Returns the field with the given 'name', or a null pointer if no field has
67 const struct mf_field *
68 mf_from_name(const char *name)
71 return shash_find_data(&mf_by_name, name);
79 shash_init(&mf_by_name);
80 for (i = 0; i < MFF_N_IDS; i++) {
81 const struct mf_field *mf = &mf_fields[i];
83 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
85 shash_add_once(&mf_by_name, mf->name, mf);
87 shash_add_once(&mf_by_name, mf->extra_name, mf);
95 static pthread_once_t once = PTHREAD_ONCE_INIT;
96 pthread_once(&once, nxm_do_init);
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),
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.) */
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)
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];
126 if ((av ^ bv) & (am & bm)) {
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. */
138 mf_subvalue_width(const union mf_subvalue *v)
140 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
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. */
146 mf_subvalue_shift(union mf_subvalue *value, int n)
149 union mf_subvalue tmp;
150 memset(&tmp, 0, sizeof tmp);
152 if (n > 0 && n < 8 * sizeof tmp) {
153 bitwise_copy(value, sizeof *value, 0,
156 } else if (n < 0 && n > -8 * sizeof tmp) {
157 bitwise_copy(value, sizeof *value, -n,
165 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
166 * specifies at least one bit in the field.
168 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
169 * meets 'mf''s prerequisites. */
171 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
175 return !wc->masks.dp_hash;
177 return !wc->masks.recirc_id;
179 return !wc->masks.conj_id;
181 return !wc->masks.tunnel.ip_src;
183 return !wc->masks.tunnel.ip_dst;
188 return !wc->masks.tunnel.tun_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;
196 tun_metadata_read(&wc->masks.tunnel.metadata, mf, &value);
197 return is_all_zeros(&value.tun_metadata, mf->n_bytes);
200 return !wc->masks.metadata;
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;
207 return !wc->masks.pkt_mark;
209 return !wc->masks.regs[mf->id - MFF_REG0];
211 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
212 case MFF_ACTSET_OUTPUT:
213 return !wc->masks.actset_output;
216 return eth_addr_is_zero(wc->masks.dl_src);
218 return eth_addr_is_zero(wc->masks.dl_dst);
220 return !wc->masks.dl_type;
224 return eth_addr_is_zero(wc->masks.arp_sha);
228 return eth_addr_is_zero(wc->masks.arp_tha);
231 return !wc->masks.vlan_tci;
233 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
235 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
236 case MFF_DL_VLAN_PCP:
238 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
241 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
243 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
245 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
248 return !wc->masks.nw_src;
250 return !wc->masks.nw_dst;
253 return ipv6_mask_is_any(&wc->masks.ipv6_src);
255 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
258 return !wc->masks.ipv6_label;
261 return !wc->masks.nw_proto;
263 case MFF_IP_DSCP_SHIFTED:
264 return !(wc->masks.nw_tos & IP_DSCP_MASK);
266 return !(wc->masks.nw_tos & IP_ECN_MASK);
268 return !wc->masks.nw_ttl;
271 return ipv6_mask_is_any(&wc->masks.nd_target);
274 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
277 return !wc->masks.nw_proto;
279 return !wc->masks.nw_src;
281 return !wc->masks.nw_dst;
286 case MFF_ICMPV4_TYPE:
287 case MFF_ICMPV6_TYPE:
288 return !wc->masks.tp_src;
292 case MFF_ICMPV4_CODE:
293 case MFF_ICMPV6_CODE:
294 return !wc->masks.tp_dst;
296 return !wc->masks.tcp_flags;
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.
308 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
309 * meets 'mf''s prerequisites. */
311 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
312 union mf_value *mask)
314 mf_get_value(mf, &wc->masks, mask);
317 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
318 * if the mask is valid, false otherwise. */
320 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
322 switch (mf->maskable) {
324 return (is_all_zeros(mask, mf->n_bytes) ||
325 is_all_ones(mask, mf->n_bytes));
334 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
336 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
338 switch (mf->prereqs) {
343 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
344 flow->dl_type == htons(ETH_TYPE_RARP));
346 return flow->dl_type == htons(ETH_TYPE_IP);
348 return flow->dl_type == htons(ETH_TYPE_IPV6);
350 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
352 return eth_type_mpls(flow->dl_type);
354 return is_ip_any(flow);
357 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
358 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
360 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
361 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
363 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
364 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
366 return is_icmpv4(flow);
368 return is_icmpv6(flow);
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)));
376 return (is_icmpv6(flow)
377 && flow->tp_dst == htons(0)
378 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
380 return (is_icmpv6(flow)
381 && flow->tp_dst == htons(0)
382 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
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. */
392 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
394 static union mf_value exact_match_mask;
396 memset(&exact_match_mask, 0xff, sizeof exact_match_mask);
397 mf_set_flow_value(mf, &exact_match_mask, mask);
399 switch (mf->prereqs) {
403 mask->tp_src = OVS_BE16_MAX;
404 mask->tp_dst = OVS_BE16_MAX;
411 /* nw_frag always unwildcarded. */
412 mask->nw_proto = 0xff;
419 mask->dl_type = OVS_BE16_MAX;
422 mask->vlan_tci |= htons(VLAN_CFI);
429 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
431 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
433 bitmap_set1(bm->bm, mf->id);
435 switch (mf->prereqs) {
439 bitmap_set1(bm->bm, MFF_TCP_SRC);
440 bitmap_set1(bm->bm, MFF_TCP_DST);
447 /* nw_frag always unwildcarded. */
448 bitmap_set1(bm->bm, MFF_IP_PROTO);
455 bitmap_set1(bm->bm, MFF_ETH_TYPE);
458 bitmap_set1(bm->bm, MFF_VLAN_TCI);
465 /* Returns true if 'value' may be a valid value *as part of a masked match*,
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. */
476 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
489 case MFF_TUN_GBP_FLAGS:
490 CASE_MFF_TUN_METADATA:
493 case MFF_SKB_PRIORITY:
517 case MFF_ICMPV4_TYPE:
518 case MFF_ICMPV4_CODE:
519 case MFF_ICMPV6_TYPE:
520 case MFF_ICMPV6_CODE:
526 case MFF_IN_PORT_OXM:
527 case MFF_ACTSET_OUTPUT: {
529 return !ofputil_port_from_ofp11(value->be32, &port);
533 return !(value->u8 & ~IP_DSCP_MASK);
534 case MFF_IP_DSCP_SHIFTED:
535 return !(value->u8 & (~IP_DSCP_MASK >> 2));
537 return !(value->u8 & ~IP_ECN_MASK);
539 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
541 return !(value->be16 & ~htons(0x0fff));
544 return !(value->be16 & htons(0xff00));
547 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
549 return !(value->be16 & htons(VLAN_PCP_MASK));
551 case MFF_DL_VLAN_PCP:
553 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
556 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
559 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
562 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
565 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
573 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
574 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
576 mf_get_value(const struct mf_field *mf, const struct flow *flow,
577 union mf_value *value)
581 value->be32 = htonl(flow->dp_hash);
584 value->be32 = htonl(flow->recirc_id);
587 value->be32 = htonl(flow->conj_id);
590 value->be64 = flow->tunnel.tun_id;
593 value->be32 = flow->tunnel.ip_src;
596 value->be32 = flow->tunnel.ip_dst;
599 value->be16 = htons(flow->tunnel.flags);
602 value->be16 = flow->tunnel.gbp_id;
604 case MFF_TUN_GBP_FLAGS:
605 value->u8 = flow->tunnel.gbp_flags;
608 value->u8 = flow->tunnel.ip_ttl;
611 value->u8 = flow->tunnel.ip_tos;
613 CASE_MFF_TUN_METADATA:
614 tun_metadata_read(&flow->tunnel.metadata, mf, value);
618 value->be64 = flow->metadata;
622 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
624 case MFF_IN_PORT_OXM:
625 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
627 case MFF_ACTSET_OUTPUT:
628 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
631 case MFF_SKB_PRIORITY:
632 value->be32 = htonl(flow->skb_priority);
636 value->be32 = htonl(flow->pkt_mark);
640 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
644 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
648 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
652 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
656 value->be16 = flow->dl_type;
660 value->be16 = flow->vlan_tci;
664 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
667 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
670 case MFF_DL_VLAN_PCP:
672 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
676 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
680 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
684 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
688 value->be32 = flow->nw_src;
692 value->be32 = flow->nw_dst;
696 value->ipv6 = flow->ipv6_src;
700 value->ipv6 = flow->ipv6_dst;
704 value->be32 = flow->ipv6_label;
708 value->u8 = flow->nw_proto;
712 value->u8 = flow->nw_tos & IP_DSCP_MASK;
715 case MFF_IP_DSCP_SHIFTED:
716 value->u8 = flow->nw_tos >> 2;
720 value->u8 = flow->nw_tos & IP_ECN_MASK;
724 value->u8 = flow->nw_ttl;
728 value->u8 = flow->nw_frag;
732 value->be16 = htons(flow->nw_proto);
736 value->be32 = flow->nw_src;
740 value->be32 = flow->nw_dst;
745 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
750 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
756 value->be16 = flow->tp_src;
762 value->be16 = flow->tp_dst;
766 value->be16 = flow->tcp_flags;
769 case MFF_ICMPV4_TYPE:
770 case MFF_ICMPV6_TYPE:
771 value->u8 = ntohs(flow->tp_src);
774 case MFF_ICMPV4_CODE:
775 case MFF_ICMPV6_CODE:
776 value->u8 = ntohs(flow->tp_dst);
780 value->ipv6 = flow->nd_target;
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
793 mf_set_value(const struct mf_field *mf,
794 const union mf_value *value, struct match *match)
798 match_set_dp_hash(match, ntohl(value->be32));
801 match_set_recirc_id(match, ntohl(value->be32));
804 match_set_conj_id(match, ntohl(value->be32));
807 match_set_tun_id(match, value->be64);
810 match_set_tun_src(match, value->be32);
813 match_set_tun_dst(match, value->be32);
816 match_set_tun_flags(match, ntohs(value->be16));
819 match_set_tun_gbp_id(match, value->be16);
821 case MFF_TUN_GBP_FLAGS:
822 match_set_tun_gbp_flags(match, value->u8);
825 match_set_tun_tos(match, value->u8);
828 match_set_tun_ttl(match, value->u8);
830 CASE_MFF_TUN_METADATA:
831 tun_metadata_set_match(mf, value, NULL, match);
835 match_set_metadata(match, value->be64);
839 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
842 case MFF_IN_PORT_OXM: {
844 ofputil_port_from_ofp11(value->be32, &port);
845 match_set_in_port(match, port);
848 case MFF_ACTSET_OUTPUT: {
850 ofputil_port_from_ofp11(value->be32, &port);
851 match_set_actset_output(match, port);
855 case MFF_SKB_PRIORITY:
856 match_set_skb_priority(match, ntohl(value->be32));
860 match_set_pkt_mark(match, ntohl(value->be32));
864 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
868 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
872 match_set_dl_src(match, value->mac);
876 match_set_dl_dst(match, value->mac);
880 match_set_dl_type(match, value->be16);
884 match_set_dl_tci(match, value->be16);
888 match_set_dl_vlan(match, value->be16);
891 match_set_vlan_vid(match, value->be16);
894 case MFF_DL_VLAN_PCP:
896 match_set_dl_vlan_pcp(match, value->u8);
900 match_set_mpls_label(match, 0, value->be32);
904 match_set_mpls_tc(match, 0, value->u8);
908 match_set_mpls_bos(match, 0, value->u8);
912 match_set_nw_src(match, value->be32);
916 match_set_nw_dst(match, value->be32);
920 match_set_ipv6_src(match, &value->ipv6);
924 match_set_ipv6_dst(match, &value->ipv6);
928 match_set_ipv6_label(match, value->be32);
932 match_set_nw_proto(match, value->u8);
936 match_set_nw_dscp(match, value->u8);
939 case MFF_IP_DSCP_SHIFTED:
940 match_set_nw_dscp(match, value->u8 << 2);
944 match_set_nw_ecn(match, value->u8);
948 match_set_nw_ttl(match, value->u8);
952 match_set_nw_frag(match, value->u8);
956 match_set_nw_proto(match, ntohs(value->be16));
960 match_set_nw_src(match, value->be32);
964 match_set_nw_dst(match, value->be32);
969 match_set_arp_sha(match, value->mac);
974 match_set_arp_tha(match, value->mac);
980 match_set_tp_src(match, value->be16);
986 match_set_tp_dst(match, value->be16);
990 match_set_tcp_flags(match, value->be16);
993 case MFF_ICMPV4_TYPE:
994 case MFF_ICMPV6_TYPE:
995 match_set_icmp_type(match, value->u8);
998 case MFF_ICMPV4_CODE:
999 case MFF_ICMPV6_CODE:
1000 match_set_icmp_code(match, value->u8);
1004 match_set_nd_target(match, &value->ipv6);
1013 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1014 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1016 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1018 union mf_value exact_match_mask;
1020 memset(&exact_match_mask, 0xff, sizeof exact_match_mask);
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
1026 if (mf->id == MFF_DL_VLAN) {
1027 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1029 mf_set_flow_value(mf, &exact_match_mask, mask);
1034 field_len(const struct mf_field *mf, const union mf_value *value_)
1036 const uint8_t *value = &value_->u8;
1039 if (!mf->variable_len) {
1047 for (i = 0; i < mf->n_bytes; i++) {
1048 if (value[i] != 0) {
1053 return mf->n_bytes - i;
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). */
1061 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1062 const union mf_value *mask)
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);
1075 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1076 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1078 mf_set_flow_value(const struct mf_field *mf,
1079 const union mf_value *value, struct flow *flow)
1083 flow->dp_hash = ntohl(value->be32);
1086 flow->recirc_id = ntohl(value->be32);
1089 flow->conj_id = ntohl(value->be32);
1092 flow->tunnel.tun_id = value->be64;
1095 flow->tunnel.ip_src = value->be32;
1098 flow->tunnel.ip_dst = value->be32;
1101 flow->tunnel.flags = ntohs(value->be16);
1103 case MFF_TUN_GBP_ID:
1104 flow->tunnel.gbp_id = value->be16;
1106 case MFF_TUN_GBP_FLAGS:
1107 flow->tunnel.gbp_flags = value->u8;
1110 flow->tunnel.ip_tos = value->u8;
1113 flow->tunnel.ip_ttl = value->u8;
1115 CASE_MFF_TUN_METADATA:
1116 tun_metadata_write(&flow->tunnel.metadata, mf, value);
1119 flow->metadata = value->be64;
1123 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1126 case MFF_IN_PORT_OXM:
1127 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1129 case MFF_ACTSET_OUTPUT:
1130 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1133 case MFF_SKB_PRIORITY:
1134 flow->skb_priority = ntohl(value->be32);
1138 flow->pkt_mark = ntohl(value->be32);
1142 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1146 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1150 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1154 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1158 flow->dl_type = value->be16;
1162 flow->vlan_tci = value->be16;
1166 flow_set_dl_vlan(flow, value->be16);
1169 flow_set_vlan_vid(flow, value->be16);
1172 case MFF_DL_VLAN_PCP:
1174 flow_set_vlan_pcp(flow, value->u8);
1177 case MFF_MPLS_LABEL:
1178 flow_set_mpls_label(flow, 0, value->be32);
1182 flow_set_mpls_tc(flow, 0, value->u8);
1186 flow_set_mpls_bos(flow, 0, value->u8);
1190 flow->nw_src = value->be32;
1194 flow->nw_dst = value->be32;
1198 flow->ipv6_src = value->ipv6;
1202 flow->ipv6_dst = value->ipv6;
1205 case MFF_IPV6_LABEL:
1206 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1210 flow->nw_proto = value->u8;
1214 flow->nw_tos &= ~IP_DSCP_MASK;
1215 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1218 case MFF_IP_DSCP_SHIFTED:
1219 flow->nw_tos &= ~IP_DSCP_MASK;
1220 flow->nw_tos |= value->u8 << 2;
1224 flow->nw_tos &= ~IP_ECN_MASK;
1225 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1229 flow->nw_ttl = value->u8;
1233 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1237 flow->nw_proto = ntohs(value->be16);
1241 flow->nw_src = value->be32;
1245 flow->nw_dst = value->be32;
1250 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1255 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1261 flow->tp_src = value->be16;
1267 flow->tp_dst = value->be16;
1271 flow->tcp_flags = value->be16;
1274 case MFF_ICMPV4_TYPE:
1275 case MFF_ICMPV6_TYPE:
1276 flow->tp_src = htons(value->u8);
1279 case MFF_ICMPV4_CODE:
1280 case MFF_ICMPV6_CODE:
1281 flow->tp_dst = htons(value->u8);
1285 flow->nd_target = value->ipv6;
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] =
1298 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1302 for (i = 0; i < n; i++) {
1303 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
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.*/
1311 mf_set_flow_value_masked(const struct mf_field *field,
1312 const union mf_value *value,
1313 const union mf_value *mask,
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);
1324 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1326 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1329 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1331 union mf_value value;
1333 mf_get_value(mf, flow, &value);
1334 return is_all_zeros(&value, mf->n_bytes);
1337 /* Makes 'match' wildcard field 'mf'.
1339 * The caller is responsible for ensuring that 'match' meets 'mf''s
1342 mf_set_wild(const struct mf_field *mf, struct match *match)
1346 match->flow.dp_hash = 0;
1347 match->wc.masks.dp_hash = 0;
1350 match->flow.recirc_id = 0;
1351 match->wc.masks.recirc_id = 0;
1354 match->flow.conj_id = 0;
1355 match->wc.masks.conj_id = 0;
1358 match_set_tun_id_masked(match, htonll(0), htonll(0));
1361 match_set_tun_src_masked(match, htonl(0), htonl(0));
1364 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1367 match_set_tun_flags_masked(match, 0, 0);
1369 case MFF_TUN_GBP_ID:
1370 match_set_tun_gbp_id_masked(match, 0, 0);
1372 case MFF_TUN_GBP_FLAGS:
1373 match_set_tun_gbp_flags_masked(match, 0, 0);
1376 match_set_tun_tos_masked(match, 0, 0);
1379 match_set_tun_ttl_masked(match, 0, 0);
1381 CASE_MFF_TUN_METADATA:
1382 tun_metadata_set_match(mf, NULL, NULL, match);
1386 match_set_metadata_masked(match, htonll(0), htonll(0));
1390 case MFF_IN_PORT_OXM:
1391 match->flow.in_port.ofp_port = 0;
1392 match->wc.masks.in_port.ofp_port = 0;
1394 case MFF_ACTSET_OUTPUT:
1395 match->flow.actset_output = 0;
1396 match->wc.masks.actset_output = 0;
1399 case MFF_SKB_PRIORITY:
1400 match->flow.skb_priority = 0;
1401 match->wc.masks.skb_priority = 0;
1405 match->flow.pkt_mark = 0;
1406 match->wc.masks.pkt_mark = 0;
1410 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1414 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1418 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1419 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1423 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1424 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1428 match->flow.dl_type = htons(0);
1429 match->wc.masks.dl_type = htons(0);
1433 match_set_dl_tci_masked(match, htons(0), htons(0));
1438 match_set_any_vid(match);
1441 case MFF_DL_VLAN_PCP:
1443 match_set_any_pcp(match);
1446 case MFF_MPLS_LABEL:
1447 match_set_any_mpls_label(match, 0);
1451 match_set_any_mpls_tc(match, 0);
1455 match_set_any_mpls_bos(match, 0);
1460 match_set_nw_src_masked(match, htonl(0), htonl(0));
1465 match_set_nw_dst_masked(match, htonl(0), htonl(0));
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);
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);
1478 case MFF_IPV6_LABEL:
1479 match->wc.masks.ipv6_label = htonl(0);
1480 match->flow.ipv6_label = htonl(0);
1484 match->wc.masks.nw_proto = 0;
1485 match->flow.nw_proto = 0;
1489 case MFF_IP_DSCP_SHIFTED:
1490 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1491 match->flow.nw_tos &= ~IP_DSCP_MASK;
1495 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1496 match->flow.nw_tos &= ~IP_ECN_MASK;
1500 match->wc.masks.nw_ttl = 0;
1501 match->flow.nw_ttl = 0;
1505 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1506 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1510 match->wc.masks.nw_proto = 0;
1511 match->flow.nw_proto = 0;
1516 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1517 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1522 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1523 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
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);
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);
1545 match->wc.masks.tcp_flags = htons(0);
1546 match->flow.tcp_flags = htons(0);
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);
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.
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).
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)
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;
1589 case MFF_IN_PORT_OXM:
1590 case MFF_ACTSET_OUTPUT:
1591 case MFF_SKB_PRIORITY:
1594 case MFF_DL_VLAN_PCP:
1596 case MFF_MPLS_LABEL:
1602 case MFF_IP_DSCP_SHIFTED:
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;
1612 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1615 match_set_tun_id_masked(match, value->be64, mask->be64);
1618 match_set_tun_src_masked(match, value->be32, mask->be32);
1621 match_set_tun_dst_masked(match, value->be32, mask->be32);
1624 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1626 case MFF_TUN_GBP_ID:
1627 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1629 case MFF_TUN_GBP_FLAGS:
1630 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1633 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1636 match_set_tun_tos_masked(match, value->u8, mask->u8);
1638 CASE_MFF_TUN_METADATA:
1639 tun_metadata_set_match(mf, value, mask, match);
1643 match_set_metadata_masked(match, value->be64, mask->be64);
1647 match_set_reg_masked(match, mf->id - MFF_REG0,
1648 ntohl(value->be32), ntohl(mask->be32));
1652 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1653 ntohll(value->be64), ntohll(mask->be64));
1657 match_set_pkt_mark_masked(match, ntohl(value->be32),
1662 match_set_dl_dst_masked(match, value->mac, mask->mac);
1666 match_set_dl_src_masked(match, value->mac, mask->mac);
1671 match_set_arp_sha_masked(match, value->mac, mask->mac);
1676 match_set_arp_tha_masked(match, value->mac, mask->mac);
1680 match_set_dl_tci_masked(match, value->be16, mask->be16);
1684 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1688 match_set_nw_src_masked(match, value->be32, mask->be32);
1692 match_set_nw_dst_masked(match, value->be32, mask->be32);
1696 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1700 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1703 case MFF_IPV6_LABEL:
1704 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1705 mf_set_value(mf, value, match);
1707 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1712 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1716 match_set_nw_frag_masked(match, value->u8, mask->u8);
1720 match_set_nw_src_masked(match, value->be32, mask->be32);
1724 match_set_nw_dst_masked(match, value->be32, mask->be32);
1730 match_set_tp_src_masked(match, value->be16, mask->be16);
1736 match_set_tp_dst_masked(match, value->be16, mask->be16);
1740 match_set_tcp_flags_masked(match, value->be16, mask->be16);
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);
1755 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
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;
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
1786 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1788 return mf_check__(sf, flow, "source");
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
1795 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
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",
1801 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1806 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1807 * 'value' and 'mask', respectively. */
1809 mf_get(const struct mf_field *mf, const struct match *match,
1810 union mf_value *value, union mf_value *mask)
1812 mf_get_value(mf, &match->flow, value);
1813 mf_get_mask(mf, &match->wc, mask);
1817 mf_from_integer_string(const struct mf_field *mf, const char *s,
1818 uint8_t *valuep, uint8_t *maskp)
1821 const char *err_str = "";
1824 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1825 if (err || (*tail != '\0' && *tail != '/')) {
1831 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1832 if (err || *tail != '\0') {
1837 memset(maskp, 0xff, mf->n_bytes);
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);
1847 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
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])
1858 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
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);
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)) {
1874 return xasprintf("%s: invalid Ethernet address", s);
1878 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1879 ovs_be32 *ip, ovs_be32 *mask)
1883 ovs_assert(mf->n_bytes == sizeof *ip);
1885 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1886 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
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 "
1893 *mask = be32_prefix_mask(prefix);
1894 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1895 *mask = OVS_BE32_MAX;
1897 return xasprintf("%s: invalid IP address", s);
1903 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1904 struct in6_addr *value, struct in6_addr *mask)
1906 char *str = xstrdup(s);
1907 char *save_ptr = NULL;
1908 const char *name, *netmask;
1911 ovs_assert(mf->n_bytes == sizeof *value);
1913 name = strtok_r(str, "/", &save_ptr);
1914 retval = name ? lookup_ipv6(name, value) : EINVAL;
1918 err = xasprintf("%s: could not convert to IPv6 address", str);
1924 netmask = strtok_r(NULL, "/", &save_ptr);
1926 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1927 int prefix = atoi(netmask);
1928 if (prefix <= 0 || prefix > 128) {
1930 return xasprintf("%s: prefix bits not between 1 and 128", s);
1932 *mask = ipv6_create_mask(prefix);
1936 *mask = in6addr_exact;
1944 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1945 ovs_be16 *valuep, ovs_be16 *maskp)
1949 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1951 if (ofputil_port_from_string(s, &port)) {
1952 *valuep = htons(ofp_to_u16(port));
1953 *maskp = OVS_BE16_MAX;
1956 return xasprintf("%s: port value out of range for %s", s, mf->name);
1960 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1961 ovs_be32 *valuep, ovs_be32 *maskp)
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;
1971 return xasprintf("%s: port value out of range for %s", s, mf->name);
1974 struct frag_handling {
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 */
1986 { "first", A|L, A },
1987 { "later", A|L, A|L },
1992 { "not_later", L, 0 },
1999 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2001 const struct frag_handling *h;
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;
2013 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2014 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2018 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2021 uint32_t result = 0;
2022 char *save_ptr = NULL;
2025 char *s = xstrdup(s_);
2027 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2028 name = strtok_r(NULL, " |", &save_ptr)) {
2030 unsigned long long int flags;
2033 if (ovs_scan(name, "%lli", &flags)) {
2037 name_len = strlen(name);
2038 for (bit = 1; bit; bit <<= 1) {
2039 const char *fname = bit_to_string(bit);
2046 len = strlen(fname);
2047 if (len != name_len) {
2050 if (!strncmp(name, fname, len)) {
2062 *res = htons(result);
2069 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2071 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2072 *maskp = OVS_BE16_MAX;
2076 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2077 "\"csum\", \"key\")", s);
2081 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2088 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2089 *flagsp = htons(flags);
2090 *maskp = htons(mask);
2093 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2094 *flagsp = htons(flags);
2095 *maskp = OVS_BE16_MAX;
2099 while (*s != '\0') {
2111 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2112 "or '-' (NOT SET)", s);
2116 name_len = strcspn(s,"+-");
2118 for (bit = 1; bit; bit <<= 1) {
2119 const char *fname = packet_tcp_flag_to_string(bit);
2126 len = strlen(fname);
2127 if (len != name_len) {
2130 if (!strncmp(s, fname, len)) {
2132 return xasprintf("%s: Each TCP flag can be specified only "
2144 return xasprintf("%s: unknown TCP flag(s)", s);
2149 *flagsp = htons(flags);
2150 *maskp = htons(mask);
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. */
2158 mf_parse(const struct mf_field *mf, const char *s,
2159 union mf_value *value, union mf_value *mask)
2163 if (!strcmp(s, "*")) {
2164 memset(value, 0, mf->n_bytes);
2165 memset(mask, 0, mf->n_bytes);
2169 switch (mf->string) {
2171 case MFS_HEXADECIMAL:
2172 error = mf_from_integer_string(mf, s,
2173 (uint8_t *) value, (uint8_t *) mask);
2177 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2181 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2185 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2189 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2192 case MFS_OFP_PORT_OXM:
2193 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2197 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2201 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2202 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2206 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2207 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2214 if (!error && !mf_is_mask_valid(mf, mask)) {
2215 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2220 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2221 * successful, otherwise a malloc()'d string describing the error. */
2223 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2225 union mf_value mask;
2228 error = mf_parse(mf, s, value, &mask);
2233 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2234 return xasprintf("%s: wildcards not allowed here", s);
2240 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2241 const uint8_t *maskp, struct ds *s)
2243 if (mf->string == MFS_HEXADECIMAL) {
2244 ds_put_hex(s, valuep, mf->n_bytes);
2246 unsigned long long int integer = 0;
2249 ovs_assert(mf->n_bytes <= 8);
2250 for (i = 0; i < mf->n_bytes; i++) {
2251 integer = (integer << 8) | valuep[i];
2253 ds_put_format(s, "%lld", integer);
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);
2266 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2268 const struct frag_handling *h;
2270 mask &= FLOW_NW_FRAG_MASK;
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);
2279 ds_put_cstr(s, "<error>");
2283 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2285 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2289 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2291 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
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. */
2298 mf_format(const struct mf_field *mf,
2299 const union mf_value *value, const union mf_value *mask,
2303 if (is_all_zeros(mask, mf->n_bytes)) {
2304 ds_put_cstr(s, "ANY");
2306 } else if (is_all_ones(mask, mf->n_bytes)) {
2311 switch (mf->string) {
2312 case MFS_OFP_PORT_OXM:
2315 ofputil_port_from_ofp11(value->be32, &port);
2316 ofputil_format_port(port, s);
2322 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2327 case MFS_HEXADECIMAL:
2328 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2332 eth_format_masked(value->mac, mask->mac, s);
2336 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2340 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2344 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2348 mf_format_tnl_flags_string(&value->be16, s);
2352 mf_format_tcp_flags_string(value->be16,
2353 mask ? mask->be16 : OVS_BE16_MAX, s);
2361 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2362 * least-significant bits in 'x'.
2365 mf_write_subfield_flow(const struct mf_subfield *sf,
2366 const union mf_subvalue *x, struct flow *flow)
2368 const struct mf_field *field = sf->field;
2369 union mf_value value;
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);
2377 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2378 * least-significant bits in 'x'.
2381 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2382 struct match *match)
2384 const struct mf_field *field = sf->field;
2385 union mf_value value, mask;
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);
2393 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2394 * 'match' in the correspond positions. */
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)
2401 union mf_value value, mask;
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);
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(). */
2412 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2413 union mf_subvalue *x)
2415 union mf_value value;
2417 mf_get_value(sf->field, flow, &value);
2419 memset(x, 0, sizeof *x);
2420 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
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
2429 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2431 union mf_value value;
2433 mf_get_value(sf->field, flow, &value);
2434 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2438 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2440 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2444 field_array_set(enum mf_field_id id, const union mf_value *value,
2445 struct field_array *fa)
2447 ovs_assert(id < MFF_N_IDS);
2448 bitmap_set1(fa->used.bm, id);
2449 fa->value[id] = *value;