2 * Copyright (c) 2011, 2012, 2013, 2014 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 "unaligned.h"
39 VLOG_DEFINE_THIS_MODULE(meta_flow);
41 #define FLOW_U32OFS(FIELD) \
42 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
44 #define MF_FIELD_SIZES(MEMBER) \
45 sizeof ((union mf_value *)0)->MEMBER, \
46 8 * sizeof ((union mf_value *)0)->MEMBER
48 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
50 const struct mf_field mf_fields[MFF_N_IDS] = {
51 #include "meta-flow.inc"
54 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
55 static struct shash mf_by_name;
57 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
58 * controller and so there's not much point in showing a lot of them. */
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
61 static void nxm_init(void);
63 /* Returns the field with the given 'name', or a null pointer if no field has
65 const struct mf_field *
66 mf_from_name(const char *name)
69 return shash_find_data(&mf_by_name, name);
77 shash_init(&mf_by_name);
78 for (i = 0; i < MFF_N_IDS; i++) {
79 const struct mf_field *mf = &mf_fields[i];
81 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
83 shash_add_once(&mf_by_name, mf->name, mf);
85 shash_add_once(&mf_by_name, mf->extra_name, mf);
93 static pthread_once_t once = PTHREAD_ONCE_INIT;
94 pthread_once(&once, nxm_do_init);
97 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
98 * specifies at least one bit in the field.
100 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
101 * meets 'mf''s prerequisites. */
103 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
107 return !wc->masks.dp_hash;
109 return !wc->masks.recirc_id;
111 return !wc->masks.tunnel.ip_src;
113 return !wc->masks.tunnel.ip_dst;
118 return !wc->masks.tunnel.tun_id;
120 return !wc->masks.metadata;
122 case MFF_IN_PORT_OXM:
123 return !wc->masks.in_port.ofp_port;
124 case MFF_SKB_PRIORITY:
125 return !wc->masks.skb_priority;
127 return !wc->masks.pkt_mark;
129 return !wc->masks.regs[mf->id - MFF_REG0];
131 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
132 case MFF_ACTSET_OUTPUT:
133 return !wc->masks.actset_output;
136 return eth_addr_is_zero(wc->masks.dl_src);
138 return eth_addr_is_zero(wc->masks.dl_dst);
140 return !wc->masks.dl_type;
144 return eth_addr_is_zero(wc->masks.arp_sha);
148 return eth_addr_is_zero(wc->masks.arp_tha);
151 return !wc->masks.vlan_tci;
153 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
155 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
156 case MFF_DL_VLAN_PCP:
158 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
161 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
163 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
165 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
168 return !wc->masks.nw_src;
170 return !wc->masks.nw_dst;
173 return ipv6_mask_is_any(&wc->masks.ipv6_src);
175 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
178 return !wc->masks.ipv6_label;
181 return !wc->masks.nw_proto;
183 case MFF_IP_DSCP_SHIFTED:
184 return !(wc->masks.nw_tos & IP_DSCP_MASK);
186 return !(wc->masks.nw_tos & IP_ECN_MASK);
188 return !wc->masks.nw_ttl;
191 return ipv6_mask_is_any(&wc->masks.nd_target);
194 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
197 return !wc->masks.nw_proto;
199 return !wc->masks.nw_src;
201 return !wc->masks.nw_dst;
206 case MFF_ICMPV4_TYPE:
207 case MFF_ICMPV6_TYPE:
208 return !wc->masks.tp_src;
212 case MFF_ICMPV4_CODE:
213 case MFF_ICMPV6_CODE:
214 return !wc->masks.tp_dst;
216 return !wc->masks.tcp_flags;
224 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
225 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
226 * purposes, or to 0 if it is wildcarded.
228 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
229 * meets 'mf''s prerequisites. */
231 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
232 union mf_value *mask)
234 mf_get_value(mf, &wc->masks, mask);
237 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
238 * if the mask is valid, false otherwise. */
240 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
242 switch (mf->maskable) {
244 return (is_all_zeros(mask, mf->n_bytes) ||
245 is_all_ones(mask, mf->n_bytes));
254 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
256 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
258 switch (mf->prereqs) {
263 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
264 flow->dl_type == htons(ETH_TYPE_RARP));
266 return flow->dl_type == htons(ETH_TYPE_IP);
268 return flow->dl_type == htons(ETH_TYPE_IPV6);
270 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
272 return eth_type_mpls(flow->dl_type);
274 return is_ip_any(flow);
277 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
279 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
281 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
283 return is_icmpv4(flow);
285 return is_icmpv6(flow);
288 return (is_icmpv6(flow)
289 && flow->tp_dst == htons(0)
290 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
291 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
293 return (is_icmpv6(flow)
294 && flow->tp_dst == htons(0)
295 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
297 return (is_icmpv6(flow)
298 && flow->tp_dst == htons(0)
299 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
305 /* Set field and it's prerequisities in the mask.
306 * This is only ever called for writeable 'mf's, but we do not make the
307 * distinction here. */
309 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
311 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
313 mf_set_flow_value(mf, &exact_match_mask, mask);
315 switch (mf->prereqs) {
319 mask->tp_src = OVS_BE16_MAX;
320 mask->tp_dst = OVS_BE16_MAX;
327 mask->nw_proto = 0xff;
334 mask->dl_type = OVS_BE16_MAX;
337 mask->vlan_tci |= htons(VLAN_CFI);
345 /* Returns true if 'value' may be a valid value *as part of a masked match*,
348 * A value is not rejected just because it is not valid for the field in
349 * question, but only if it doesn't make sense to test the bits in question at
350 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
351 * without the VLAN_CFI bit being set, but we can't reject those values because
352 * it is still legitimate to test just for those bits (see the documentation
353 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
354 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
356 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
369 case MFF_SKB_PRIORITY:
393 case MFF_ICMPV4_TYPE:
394 case MFF_ICMPV4_CODE:
395 case MFF_ICMPV6_TYPE:
396 case MFF_ICMPV6_CODE:
402 case MFF_IN_PORT_OXM:
403 case MFF_ACTSET_OUTPUT: {
405 return !ofputil_port_from_ofp11(value->be32, &port);
409 return !(value->u8 & ~IP_DSCP_MASK);
410 case MFF_IP_DSCP_SHIFTED:
411 return !(value->u8 & (~IP_DSCP_MASK >> 2));
413 return !(value->u8 & ~IP_ECN_MASK);
415 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
417 return !(value->be16 & ~htons(0x0fff));
420 return !(value->be16 & htons(0xff00));
423 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
425 return !(value->be16 & htons(VLAN_PCP_MASK));
427 case MFF_DL_VLAN_PCP:
429 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
432 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
435 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
438 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
441 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
449 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
450 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
452 mf_get_value(const struct mf_field *mf, const struct flow *flow,
453 union mf_value *value)
457 value->be32 = htonl(flow->dp_hash);
460 value->be32 = htonl(flow->recirc_id);
463 value->be64 = flow->tunnel.tun_id;
466 value->be32 = flow->tunnel.ip_src;
469 value->be32 = flow->tunnel.ip_dst;
472 value->be16 = htons(flow->tunnel.flags);
475 value->u8 = flow->tunnel.ip_ttl;
478 value->u8 = flow->tunnel.ip_tos;
482 value->be64 = flow->metadata;
486 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
488 case MFF_IN_PORT_OXM:
489 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
491 case MFF_ACTSET_OUTPUT:
492 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
495 case MFF_SKB_PRIORITY:
496 value->be32 = htonl(flow->skb_priority);
500 value->be32 = htonl(flow->pkt_mark);
504 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
508 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
512 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
516 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
520 value->be16 = flow->dl_type;
524 value->be16 = flow->vlan_tci;
528 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
531 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
534 case MFF_DL_VLAN_PCP:
536 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
540 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
544 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
548 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
552 value->be32 = flow->nw_src;
556 value->be32 = flow->nw_dst;
560 value->ipv6 = flow->ipv6_src;
564 value->ipv6 = flow->ipv6_dst;
568 value->be32 = flow->ipv6_label;
572 value->u8 = flow->nw_proto;
576 value->u8 = flow->nw_tos & IP_DSCP_MASK;
579 case MFF_IP_DSCP_SHIFTED:
580 value->u8 = flow->nw_tos >> 2;
584 value->u8 = flow->nw_tos & IP_ECN_MASK;
588 value->u8 = flow->nw_ttl;
592 value->u8 = flow->nw_frag;
596 value->be16 = htons(flow->nw_proto);
600 value->be32 = flow->nw_src;
604 value->be32 = flow->nw_dst;
609 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
614 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
620 value->be16 = flow->tp_src;
626 value->be16 = flow->tp_dst;
630 value->be16 = flow->tcp_flags;
633 case MFF_ICMPV4_TYPE:
634 case MFF_ICMPV6_TYPE:
635 value->u8 = ntohs(flow->tp_src);
638 case MFF_ICMPV4_CODE:
639 case MFF_ICMPV6_CODE:
640 value->u8 = ntohs(flow->tp_dst);
644 value->ipv6 = flow->nd_target;
653 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
654 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
657 mf_set_value(const struct mf_field *mf,
658 const union mf_value *value, struct match *match)
662 match_set_dp_hash(match, ntohl(value->be32));
665 match_set_recirc_id(match, ntohl(value->be32));
668 match_set_tun_id(match, value->be64);
671 match_set_tun_src(match, value->be32);
674 match_set_tun_dst(match, value->be32);
677 match_set_tun_flags(match, ntohs(value->be16));
680 match_set_tun_tos(match, value->u8);
683 match_set_tun_ttl(match, value->u8);
687 match_set_metadata(match, value->be64);
691 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
694 case MFF_IN_PORT_OXM: {
696 ofputil_port_from_ofp11(value->be32, &port);
697 match_set_in_port(match, port);
700 case MFF_ACTSET_OUTPUT: {
702 ofputil_port_from_ofp11(value->be32, &port);
703 match_set_actset_output(match, port);
707 case MFF_SKB_PRIORITY:
708 match_set_skb_priority(match, ntohl(value->be32));
712 match_set_pkt_mark(match, ntohl(value->be32));
716 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
720 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
724 match_set_dl_src(match, value->mac);
728 match_set_dl_dst(match, value->mac);
732 match_set_dl_type(match, value->be16);
736 match_set_dl_tci(match, value->be16);
740 match_set_dl_vlan(match, value->be16);
743 match_set_vlan_vid(match, value->be16);
746 case MFF_DL_VLAN_PCP:
748 match_set_dl_vlan_pcp(match, value->u8);
752 match_set_mpls_label(match, 0, value->be32);
756 match_set_mpls_tc(match, 0, value->u8);
760 match_set_mpls_bos(match, 0, value->u8);
764 match_set_nw_src(match, value->be32);
768 match_set_nw_dst(match, value->be32);
772 match_set_ipv6_src(match, &value->ipv6);
776 match_set_ipv6_dst(match, &value->ipv6);
780 match_set_ipv6_label(match, value->be32);
784 match_set_nw_proto(match, value->u8);
788 match_set_nw_dscp(match, value->u8);
791 case MFF_IP_DSCP_SHIFTED:
792 match_set_nw_dscp(match, value->u8 << 2);
796 match_set_nw_ecn(match, value->u8);
800 match_set_nw_ttl(match, value->u8);
804 match_set_nw_frag(match, value->u8);
808 match_set_nw_proto(match, ntohs(value->be16));
812 match_set_nw_src(match, value->be32);
816 match_set_nw_dst(match, value->be32);
821 match_set_arp_sha(match, value->mac);
826 match_set_arp_tha(match, value->mac);
832 match_set_tp_src(match, value->be16);
838 match_set_tp_dst(match, value->be16);
842 match_set_tcp_flags(match, value->be16);
845 case MFF_ICMPV4_TYPE:
846 case MFF_ICMPV6_TYPE:
847 match_set_icmp_type(match, value->u8);
850 case MFF_ICMPV4_CODE:
851 case MFF_ICMPV6_CODE:
852 match_set_icmp_code(match, value->u8);
856 match_set_nd_target(match, &value->ipv6);
865 /* Unwildcard 'mask' member field described by 'mf'. The caller is
866 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
868 mf_mask_field(const struct mf_field *mf, struct flow *mask)
870 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
872 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
873 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
874 * special case. For the rest, calling mf_set_flow_value() is good
876 if (mf->id == MFF_DL_VLAN) {
877 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
879 mf_set_flow_value(mf, &exact_match_mask, mask);
883 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
884 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
886 mf_set_flow_value(const struct mf_field *mf,
887 const union mf_value *value, struct flow *flow)
891 flow->dp_hash = ntohl(value->be32);
894 flow->recirc_id = ntohl(value->be32);
897 flow->tunnel.tun_id = value->be64;
900 flow->tunnel.ip_src = value->be32;
903 flow->tunnel.ip_dst = value->be32;
906 flow->tunnel.flags = ntohs(value->be16);
909 flow->tunnel.ip_tos = value->u8;
912 flow->tunnel.ip_ttl = value->u8;
916 flow->metadata = value->be64;
920 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
923 case MFF_IN_PORT_OXM:
924 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
926 case MFF_ACTSET_OUTPUT:
927 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
930 case MFF_SKB_PRIORITY:
931 flow->skb_priority = ntohl(value->be32);
935 flow->pkt_mark = ntohl(value->be32);
939 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
943 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
947 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
951 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
955 flow->dl_type = value->be16;
959 flow->vlan_tci = value->be16;
963 flow_set_dl_vlan(flow, value->be16);
966 flow_set_vlan_vid(flow, value->be16);
969 case MFF_DL_VLAN_PCP:
971 flow_set_vlan_pcp(flow, value->u8);
975 flow_set_mpls_label(flow, 0, value->be32);
979 flow_set_mpls_tc(flow, 0, value->u8);
983 flow_set_mpls_bos(flow, 0, value->u8);
987 flow->nw_src = value->be32;
991 flow->nw_dst = value->be32;
995 flow->ipv6_src = value->ipv6;
999 flow->ipv6_dst = value->ipv6;
1002 case MFF_IPV6_LABEL:
1003 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1007 flow->nw_proto = value->u8;
1011 flow->nw_tos &= ~IP_DSCP_MASK;
1012 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1015 case MFF_IP_DSCP_SHIFTED:
1016 flow->nw_tos &= ~IP_DSCP_MASK;
1017 flow->nw_tos |= value->u8 << 2;
1021 flow->nw_tos &= ~IP_ECN_MASK;
1022 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1026 flow->nw_ttl = value->u8;
1030 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1034 flow->nw_proto = ntohs(value->be16);
1038 flow->nw_src = value->be32;
1042 flow->nw_dst = value->be32;
1047 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1052 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1058 flow->tp_src = value->be16;
1064 flow->tp_dst = value->be16;
1068 flow->tcp_flags = value->be16;
1071 case MFF_ICMPV4_TYPE:
1072 case MFF_ICMPV6_TYPE:
1073 flow->tp_src = htons(value->u8);
1076 case MFF_ICMPV4_CODE:
1077 case MFF_ICMPV6_CODE:
1078 flow->tp_dst = htons(value->u8);
1082 flow->nd_target = value->ipv6;
1091 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1092 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1095 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1099 for (i = 0; i < n; i++) {
1100 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1104 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1105 * for which 'mask' has a 0-bit keep their existing values. The caller is
1106 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1108 mf_set_flow_value_masked(const struct mf_field *field,
1109 const union mf_value *value,
1110 const union mf_value *mask,
1115 mf_get_value(field, flow, &tmp);
1116 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1117 (uint8_t *) &tmp, field->n_bytes);
1118 mf_set_flow_value(field, &tmp, flow);
1121 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1123 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1126 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1128 union mf_value value;
1130 mf_get_value(mf, flow, &value);
1131 return is_all_zeros(&value, mf->n_bytes);
1134 /* Makes 'match' wildcard field 'mf'.
1136 * The caller is responsible for ensuring that 'match' meets 'mf''s
1139 mf_set_wild(const struct mf_field *mf, struct match *match)
1143 match->flow.dp_hash = 0;
1144 match->wc.masks.dp_hash = 0;
1147 match->flow.recirc_id = 0;
1148 match->wc.masks.recirc_id = 0;
1151 match_set_tun_id_masked(match, htonll(0), htonll(0));
1154 match_set_tun_src_masked(match, htonl(0), htonl(0));
1157 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1160 match_set_tun_flags_masked(match, 0, 0);
1163 match_set_tun_tos_masked(match, 0, 0);
1166 match_set_tun_ttl_masked(match, 0, 0);
1170 match_set_metadata_masked(match, htonll(0), htonll(0));
1174 case MFF_IN_PORT_OXM:
1175 match->flow.in_port.ofp_port = 0;
1176 match->wc.masks.in_port.ofp_port = 0;
1178 case MFF_ACTSET_OUTPUT:
1179 match->flow.actset_output = 0;
1180 match->wc.masks.actset_output = 0;
1183 case MFF_SKB_PRIORITY:
1184 match->flow.skb_priority = 0;
1185 match->wc.masks.skb_priority = 0;
1189 match->flow.pkt_mark = 0;
1190 match->wc.masks.pkt_mark = 0;
1194 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1198 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1202 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1203 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1207 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1208 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1212 match->flow.dl_type = htons(0);
1213 match->wc.masks.dl_type = htons(0);
1217 match_set_dl_tci_masked(match, htons(0), htons(0));
1222 match_set_any_vid(match);
1225 case MFF_DL_VLAN_PCP:
1227 match_set_any_pcp(match);
1230 case MFF_MPLS_LABEL:
1231 match_set_any_mpls_label(match, 0);
1235 match_set_any_mpls_tc(match, 0);
1239 match_set_any_mpls_bos(match, 0);
1244 match_set_nw_src_masked(match, htonl(0), htonl(0));
1249 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1253 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1254 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1258 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1259 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1262 case MFF_IPV6_LABEL:
1263 match->wc.masks.ipv6_label = htonl(0);
1264 match->flow.ipv6_label = htonl(0);
1268 match->wc.masks.nw_proto = 0;
1269 match->flow.nw_proto = 0;
1273 case MFF_IP_DSCP_SHIFTED:
1274 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1275 match->flow.nw_tos &= ~IP_DSCP_MASK;
1279 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1280 match->flow.nw_tos &= ~IP_ECN_MASK;
1284 match->wc.masks.nw_ttl = 0;
1285 match->flow.nw_ttl = 0;
1289 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1290 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1294 match->wc.masks.nw_proto = 0;
1295 match->flow.nw_proto = 0;
1300 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1301 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1306 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1307 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1313 case MFF_ICMPV4_TYPE:
1314 case MFF_ICMPV6_TYPE:
1315 match->wc.masks.tp_src = htons(0);
1316 match->flow.tp_src = htons(0);
1322 case MFF_ICMPV4_CODE:
1323 case MFF_ICMPV6_CODE:
1324 match->wc.masks.tp_dst = htons(0);
1325 match->flow.tp_dst = htons(0);
1329 match->wc.masks.tcp_flags = htons(0);
1330 match->flow.tcp_flags = htons(0);
1334 memset(&match->wc.masks.nd_target, 0,
1335 sizeof match->wc.masks.nd_target);
1336 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1345 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1346 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1347 * with a 1-bit indicating that the corresponding value bit must match and a
1348 * 0-bit indicating a don't-care.
1350 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1351 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1352 * call is equivalent to mf_set_wild(mf, match).
1354 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1355 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1356 enum ofputil_protocol
1357 mf_set(const struct mf_field *mf,
1358 const union mf_value *value, const union mf_value *mask,
1359 struct match *match)
1361 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1362 mf_set_value(mf, value, match);
1363 return mf->usable_protocols_exact;
1364 } else if (is_all_zeros(mask, mf->n_bytes)) {
1365 mf_set_wild(mf, match);
1366 return OFPUTIL_P_ANY;
1372 case MFF_IN_PORT_OXM:
1373 case MFF_ACTSET_OUTPUT:
1374 case MFF_SKB_PRIORITY:
1377 case MFF_DL_VLAN_PCP:
1379 case MFF_MPLS_LABEL:
1385 case MFF_IP_DSCP_SHIFTED:
1388 case MFF_ICMPV4_TYPE:
1389 case MFF_ICMPV4_CODE:
1390 case MFF_ICMPV6_TYPE:
1391 case MFF_ICMPV6_CODE:
1392 return OFPUTIL_P_NONE;
1395 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1398 match_set_tun_id_masked(match, value->be64, mask->be64);
1401 match_set_tun_src_masked(match, value->be32, mask->be32);
1404 match_set_tun_dst_masked(match, value->be32, mask->be32);
1407 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1410 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1413 match_set_tun_tos_masked(match, value->u8, mask->u8);
1417 match_set_metadata_masked(match, value->be64, mask->be64);
1421 match_set_reg_masked(match, mf->id - MFF_REG0,
1422 ntohl(value->be32), ntohl(mask->be32));
1426 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1427 ntohll(value->be64), ntohll(mask->be64));
1431 match_set_pkt_mark_masked(match, ntohl(value->be32),
1436 match_set_dl_dst_masked(match, value->mac, mask->mac);
1440 match_set_dl_src_masked(match, value->mac, mask->mac);
1445 match_set_arp_sha_masked(match, value->mac, mask->mac);
1450 match_set_arp_tha_masked(match, value->mac, mask->mac);
1454 match_set_dl_tci_masked(match, value->be16, mask->be16);
1458 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1462 match_set_nw_src_masked(match, value->be32, mask->be32);
1466 match_set_nw_dst_masked(match, value->be32, mask->be32);
1470 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1474 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1477 case MFF_IPV6_LABEL:
1478 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1479 mf_set_value(mf, value, match);
1481 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1486 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1490 match_set_nw_frag_masked(match, value->u8, mask->u8);
1494 match_set_nw_src_masked(match, value->be32, mask->be32);
1498 match_set_nw_dst_masked(match, value->be32, mask->be32);
1504 match_set_tp_src_masked(match, value->be16, mask->be16);
1510 match_set_tp_dst_masked(match, value->be16, mask->be16);
1514 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1522 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1523 || ip_is_cidr(mask->be32))
1524 ? mf->usable_protocols_cidr
1525 : mf->usable_protocols_bitwise);
1529 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1533 VLOG_WARN_RL(&rl, "unknown %s field", type);
1534 return OFPERR_OFPBAC_BAD_SET_TYPE;
1535 } else if (!sf->n_bits) {
1536 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1537 return OFPERR_OFPBAC_BAD_SET_LEN;
1538 } else if (sf->ofs >= sf->field->n_bits) {
1539 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1540 sf->ofs, sf->field->n_bits, type, sf->field->name);
1541 return OFPERR_OFPBAC_BAD_SET_LEN;
1542 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1543 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1544 "of %s field %s", sf->ofs, sf->n_bits,
1545 sf->field->n_bits, type, sf->field->name);
1546 return OFPERR_OFPBAC_BAD_SET_LEN;
1547 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1548 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1549 type, sf->field->name);
1550 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1556 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1557 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1560 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1562 return mf_check__(sf, flow, "source");
1565 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1566 * if so, otherwise an OpenFlow error code (e.g. as returned by
1569 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1571 int error = mf_check__(sf, flow, "destination");
1572 if (!error && !sf->field->writable) {
1573 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1575 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1580 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1581 * 'value' and 'mask', respectively. */
1583 mf_get(const struct mf_field *mf, const struct match *match,
1584 union mf_value *value, union mf_value *mask)
1586 mf_get_value(mf, &match->flow, value);
1587 mf_get_mask(mf, &match->wc, mask);
1591 mf_from_integer_string(const struct mf_field *mf, const char *s,
1592 uint8_t *valuep, uint8_t *maskp)
1594 unsigned long long int integer, mask;
1599 integer = strtoull(s, &tail, 0);
1600 if (errno || (*tail != '\0' && *tail != '/')) {
1605 mask = strtoull(tail + 1, &tail, 0);
1606 if (errno || *tail != '\0') {
1613 for (i = mf->n_bytes - 1; i >= 0; i--) {
1614 valuep[i] = integer;
1620 return xasprintf("%s: value too large for %u-byte field %s",
1621 s, mf->n_bytes, mf->name);
1626 return xasprintf("%s: bad syntax for %s", s, mf->name);
1630 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1631 uint8_t mac[ETH_ADDR_LEN],
1632 uint8_t mask[ETH_ADDR_LEN])
1636 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1639 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1640 && n == strlen(s)) {
1641 memset(mask, 0xff, ETH_ADDR_LEN);
1646 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1647 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1648 && n == strlen(s)) {
1652 return xasprintf("%s: invalid Ethernet address", s);
1656 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1657 ovs_be32 *ip, ovs_be32 *mask)
1661 ovs_assert(mf->n_bytes == sizeof *ip);
1663 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1664 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1666 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1667 if (prefix <= 0 || prefix > 32) {
1668 return xasprintf("%s: network prefix bits not between 1 and "
1670 } else if (prefix == 32) {
1671 *mask = OVS_BE32_MAX;
1673 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1675 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1676 *mask = OVS_BE32_MAX;
1678 return xasprintf("%s: invalid IP address", s);
1684 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1685 struct in6_addr *value, struct in6_addr *mask)
1687 char *str = xstrdup(s);
1688 char *save_ptr = NULL;
1689 const char *name, *netmask;
1692 ovs_assert(mf->n_bytes == sizeof *value);
1694 name = strtok_r(str, "/", &save_ptr);
1695 retval = name ? lookup_ipv6(name, value) : EINVAL;
1699 err = xasprintf("%s: could not convert to IPv6 address", str);
1705 netmask = strtok_r(NULL, "/", &save_ptr);
1707 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1708 int prefix = atoi(netmask);
1709 if (prefix <= 0 || prefix > 128) {
1711 return xasprintf("%s: prefix bits not between 1 and 128", s);
1713 *mask = ipv6_create_mask(prefix);
1717 *mask = in6addr_exact;
1725 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1726 ovs_be16 *valuep, ovs_be16 *maskp)
1730 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1732 if (ofputil_port_from_string(s, &port)) {
1733 *valuep = htons(ofp_to_u16(port));
1734 *maskp = OVS_BE16_MAX;
1737 return xasprintf("%s: port value out of range for %s", s, mf->name);
1741 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1742 ovs_be32 *valuep, ovs_be32 *maskp)
1746 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1747 if (ofputil_port_from_string(s, &port)) {
1748 *valuep = ofputil_port_to_ofp11(port);
1749 *maskp = OVS_BE32_MAX;
1752 return xasprintf("%s: port value out of range for %s", s, mf->name);
1755 struct frag_handling {
1761 static const struct frag_handling all_frags[] = {
1762 #define A FLOW_NW_FRAG_ANY
1763 #define L FLOW_NW_FRAG_LATER
1764 /* name mask value */
1767 { "first", A|L, A },
1768 { "later", A|L, A|L },
1773 { "not_later", L, 0 },
1780 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1782 const struct frag_handling *h;
1784 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1785 if (!strcasecmp(s, h->name)) {
1786 /* We force the upper bits of the mask on to make mf_parse_value()
1787 * happy (otherwise it will never think it's an exact match.) */
1788 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1794 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1795 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1799 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1802 uint32_t result = 0;
1803 char *save_ptr = NULL;
1806 char *s = xstrdup(s_);
1808 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1809 name = strtok_r(NULL, " |", &save_ptr)) {
1811 unsigned long long int flags;
1814 if (ovs_scan(name, "%lli", &flags)) {
1818 name_len = strlen(name);
1819 for (bit = 1; bit; bit <<= 1) {
1820 const char *fname = bit_to_string(bit);
1827 len = strlen(fname);
1828 if (len != name_len) {
1831 if (!strncmp(name, fname, len)) {
1843 *res = htons(result);
1850 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1852 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1853 *maskp = OVS_BE16_MAX;
1857 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1858 "\"csum\", \"key\")", s);
1862 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1869 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1870 *flagsp = htons(flags);
1871 *maskp = htons(mask);
1874 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1875 *flagsp = htons(flags);
1876 *maskp = OVS_BE16_MAX;
1880 while (*s != '\0') {
1892 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1893 "or '-' (NOT SET)", s);
1897 name_len = strcspn(s,"+-");
1899 for (bit = 1; bit; bit <<= 1) {
1900 const char *fname = packet_tcp_flag_to_string(bit);
1907 len = strlen(fname);
1908 if (len != name_len) {
1911 if (!strncmp(s, fname, len)) {
1913 return xasprintf("%s: Each TCP flag can be specified only "
1925 return xasprintf("%s: unknown TCP flag(s)", s);
1930 *flagsp = htons(flags);
1931 *maskp = htons(mask);
1936 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1937 * NULL if successful, otherwise a malloc()'d string describing the error. */
1939 mf_parse(const struct mf_field *mf, const char *s,
1940 union mf_value *value, union mf_value *mask)
1944 if (!strcmp(s, "*")) {
1945 memset(value, 0, mf->n_bytes);
1946 memset(mask, 0, mf->n_bytes);
1950 switch (mf->string) {
1952 case MFS_HEXADECIMAL:
1953 error = mf_from_integer_string(mf, s,
1954 (uint8_t *) value, (uint8_t *) mask);
1958 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1962 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1966 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1970 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1973 case MFS_OFP_PORT_OXM:
1974 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1978 error = mf_from_frag_string(s, &value->u8, &mask->u8);
1982 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1983 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
1987 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1988 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
1995 if (!error && !mf_is_mask_valid(mf, mask)) {
1996 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2001 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2002 * successful, otherwise a malloc()'d string describing the error. */
2004 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2006 union mf_value mask;
2009 error = mf_parse(mf, s, value, &mask);
2014 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2015 return xasprintf("%s: wildcards not allowed here", s);
2021 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2022 const uint8_t *maskp, struct ds *s)
2024 unsigned long long int integer;
2027 ovs_assert(mf->n_bytes <= 8);
2030 for (i = 0; i < mf->n_bytes; i++) {
2031 integer = (integer << 8) | valuep[i];
2033 if (mf->string == MFS_HEXADECIMAL) {
2034 ds_put_format(s, "%#llx", integer);
2036 ds_put_format(s, "%lld", integer);
2040 unsigned long long int mask;
2043 for (i = 0; i < mf->n_bytes; i++) {
2044 mask = (mask << 8) | maskp[i];
2047 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2048 * not sure that that a bit-mask written in decimal is ever easier to
2049 * understand than the same bit-mask written in hexadecimal. */
2050 ds_put_format(s, "/%#llx", mask);
2055 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2057 const struct frag_handling *h;
2059 mask &= FLOW_NW_FRAG_MASK;
2062 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2063 if (value == h->value && mask == h->mask) {
2064 ds_put_cstr(s, h->name);
2068 ds_put_cstr(s, "<error>");
2072 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2074 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2078 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2080 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2084 /* Appends to 's' a string representation of field 'mf' whose value is in
2085 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2087 mf_format(const struct mf_field *mf,
2088 const union mf_value *value, const union mf_value *mask,
2092 if (is_all_zeros(mask, mf->n_bytes)) {
2093 ds_put_cstr(s, "ANY");
2095 } else if (is_all_ones(mask, mf->n_bytes)) {
2100 switch (mf->string) {
2101 case MFS_OFP_PORT_OXM:
2104 ofputil_port_from_ofp11(value->be32, &port);
2105 ofputil_format_port(port, s);
2111 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2116 case MFS_HEXADECIMAL:
2117 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2121 eth_format_masked(value->mac, mask->mac, s);
2125 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2129 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2133 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2137 mf_format_tnl_flags_string(&value->be16, s);
2141 mf_format_tcp_flags_string(value->be16,
2142 mask ? mask->be16 : OVS_BE16_MAX, s);
2150 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2151 * least-significant bits in 'x'.
2154 mf_write_subfield_flow(const struct mf_subfield *sf,
2155 const union mf_subvalue *x, struct flow *flow)
2157 const struct mf_field *field = sf->field;
2158 union mf_value value;
2160 mf_get_value(field, flow, &value);
2161 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2162 sf->ofs, sf->n_bits);
2163 mf_set_flow_value(field, &value, flow);
2166 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2167 * least-significant bits in 'x'.
2170 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2171 struct match *match)
2173 const struct mf_field *field = sf->field;
2174 union mf_value value, mask;
2176 mf_get(field, match, &value, &mask);
2177 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2178 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2179 mf_set(field, &value, &mask, match);
2182 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2183 * reading 'flow', e.g. as checked by mf_check_src(). */
2185 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2186 union mf_subvalue *x)
2188 union mf_value value;
2190 mf_get_value(sf->field, flow, &value);
2192 memset(x, 0, sizeof *x);
2193 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2198 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2199 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2202 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2204 union mf_value value;
2206 mf_get_value(sf->field, flow, &value);
2207 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2211 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2215 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2216 if (subvalue->u8[i]) {
2217 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2218 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2219 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2224 ds_put_char(s, '0');