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);
134 return eth_addr_is_zero(wc->masks.dl_src);
136 return eth_addr_is_zero(wc->masks.dl_dst);
138 return !wc->masks.dl_type;
142 return eth_addr_is_zero(wc->masks.arp_sha);
146 return eth_addr_is_zero(wc->masks.arp_tha);
149 return !wc->masks.vlan_tci;
151 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
153 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
154 case MFF_DL_VLAN_PCP:
156 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
159 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
161 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
163 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
166 return !wc->masks.nw_src;
168 return !wc->masks.nw_dst;
171 return ipv6_mask_is_any(&wc->masks.ipv6_src);
173 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
176 return !wc->masks.ipv6_label;
179 return !wc->masks.nw_proto;
181 case MFF_IP_DSCP_SHIFTED:
182 return !(wc->masks.nw_tos & IP_DSCP_MASK);
184 return !(wc->masks.nw_tos & IP_ECN_MASK);
186 return !wc->masks.nw_ttl;
189 return ipv6_mask_is_any(&wc->masks.nd_target);
192 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
195 return !wc->masks.nw_proto;
197 return !wc->masks.nw_src;
199 return !wc->masks.nw_dst;
204 case MFF_ICMPV4_TYPE:
205 case MFF_ICMPV6_TYPE:
206 return !wc->masks.tp_src;
210 case MFF_ICMPV4_CODE:
211 case MFF_ICMPV6_CODE:
212 return !wc->masks.tp_dst;
214 return !wc->masks.tcp_flags;
222 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
223 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
224 * purposes, or to 0 if it is wildcarded.
226 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
227 * meets 'mf''s prerequisites. */
229 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
230 union mf_value *mask)
232 mf_get_value(mf, &wc->masks, mask);
235 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
236 * if the mask is valid, false otherwise. */
238 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
240 switch (mf->maskable) {
242 return (is_all_zeros(mask, mf->n_bytes) ||
243 is_all_ones(mask, mf->n_bytes));
252 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
254 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
256 switch (mf->prereqs) {
261 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
262 flow->dl_type == htons(ETH_TYPE_RARP));
264 return flow->dl_type == htons(ETH_TYPE_IP);
266 return flow->dl_type == htons(ETH_TYPE_IPV6);
268 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
270 return eth_type_mpls(flow->dl_type);
272 return is_ip_any(flow);
275 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
277 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
279 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
281 return is_icmpv4(flow);
283 return is_icmpv6(flow);
286 return (is_icmpv6(flow)
287 && flow->tp_dst == htons(0)
288 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
289 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
291 return (is_icmpv6(flow)
292 && flow->tp_dst == htons(0)
293 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
295 return (is_icmpv6(flow)
296 && flow->tp_dst == htons(0)
297 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
303 /* Set field and it's prerequisities in the mask.
304 * This is only ever called for writeable 'mf's, but we do not make the
305 * distinction here. */
307 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
309 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
311 mf_set_flow_value(mf, &exact_match_mask, mask);
313 switch (mf->prereqs) {
317 mask->tp_src = OVS_BE16_MAX;
318 mask->tp_dst = OVS_BE16_MAX;
325 mask->nw_proto = 0xff;
332 mask->dl_type = OVS_BE16_MAX;
335 mask->vlan_tci |= htons(VLAN_CFI);
343 /* Returns true if 'value' may be a valid value *as part of a masked match*,
346 * A value is not rejected just because it is not valid for the field in
347 * question, but only if it doesn't make sense to test the bits in question at
348 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
349 * without the VLAN_CFI bit being set, but we can't reject those values because
350 * it is still legitimate to test just for those bits (see the documentation
351 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
352 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
354 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
367 case MFF_SKB_PRIORITY:
391 case MFF_ICMPV4_TYPE:
392 case MFF_ICMPV4_CODE:
393 case MFF_ICMPV6_TYPE:
394 case MFF_ICMPV6_CODE:
400 case MFF_IN_PORT_OXM: {
402 return !ofputil_port_from_ofp11(value->be32, &port);
406 return !(value->u8 & ~IP_DSCP_MASK);
407 case MFF_IP_DSCP_SHIFTED:
408 return !(value->u8 & (~IP_DSCP_MASK >> 2));
410 return !(value->u8 & ~IP_ECN_MASK);
412 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
414 return !(value->be16 & ~htons(0x0fff));
417 return !(value->be16 & htons(0xff00));
420 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
422 return !(value->be16 & htons(VLAN_PCP_MASK));
424 case MFF_DL_VLAN_PCP:
426 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
429 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
432 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
435 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
438 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
446 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
447 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
449 mf_get_value(const struct mf_field *mf, const struct flow *flow,
450 union mf_value *value)
454 value->be32 = htonl(flow->dp_hash);
457 value->be32 = htonl(flow->recirc_id);
460 value->be64 = flow->tunnel.tun_id;
463 value->be32 = flow->tunnel.ip_src;
466 value->be32 = flow->tunnel.ip_dst;
469 value->be16 = htons(flow->tunnel.flags);
472 value->u8 = flow->tunnel.ip_ttl;
475 value->u8 = flow->tunnel.ip_tos;
479 value->be64 = flow->metadata;
483 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
485 case MFF_IN_PORT_OXM:
486 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
489 case MFF_SKB_PRIORITY:
490 value->be32 = htonl(flow->skb_priority);
494 value->be32 = htonl(flow->pkt_mark);
498 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
502 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
506 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
510 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
514 value->be16 = flow->dl_type;
518 value->be16 = flow->vlan_tci;
522 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
525 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
528 case MFF_DL_VLAN_PCP:
530 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
534 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
538 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
542 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
546 value->be32 = flow->nw_src;
550 value->be32 = flow->nw_dst;
554 value->ipv6 = flow->ipv6_src;
558 value->ipv6 = flow->ipv6_dst;
562 value->be32 = flow->ipv6_label;
566 value->u8 = flow->nw_proto;
570 value->u8 = flow->nw_tos & IP_DSCP_MASK;
573 case MFF_IP_DSCP_SHIFTED:
574 value->u8 = flow->nw_tos >> 2;
578 value->u8 = flow->nw_tos & IP_ECN_MASK;
582 value->u8 = flow->nw_ttl;
586 value->u8 = flow->nw_frag;
590 value->be16 = htons(flow->nw_proto);
594 value->be32 = flow->nw_src;
598 value->be32 = flow->nw_dst;
603 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
608 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
614 value->be16 = flow->tp_src;
620 value->be16 = flow->tp_dst;
624 value->be16 = flow->tcp_flags;
627 case MFF_ICMPV4_TYPE:
628 case MFF_ICMPV6_TYPE:
629 value->u8 = ntohs(flow->tp_src);
632 case MFF_ICMPV4_CODE:
633 case MFF_ICMPV6_CODE:
634 value->u8 = ntohs(flow->tp_dst);
638 value->ipv6 = flow->nd_target;
647 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
648 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
651 mf_set_value(const struct mf_field *mf,
652 const union mf_value *value, struct match *match)
656 match_set_dp_hash(match, ntohl(value->be32));
659 match_set_recirc_id(match, ntohl(value->be32));
662 match_set_tun_id(match, value->be64);
665 match_set_tun_src(match, value->be32);
668 match_set_tun_dst(match, value->be32);
671 match_set_tun_flags(match, ntohs(value->be16));
674 match_set_tun_tos(match, value->u8);
677 match_set_tun_ttl(match, value->u8);
681 match_set_metadata(match, value->be64);
685 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
688 case MFF_IN_PORT_OXM: {
690 ofputil_port_from_ofp11(value->be32, &port);
691 match_set_in_port(match, port);
695 case MFF_SKB_PRIORITY:
696 match_set_skb_priority(match, ntohl(value->be32));
700 match_set_pkt_mark(match, ntohl(value->be32));
704 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
708 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
712 match_set_dl_src(match, value->mac);
716 match_set_dl_dst(match, value->mac);
720 match_set_dl_type(match, value->be16);
724 match_set_dl_tci(match, value->be16);
728 match_set_dl_vlan(match, value->be16);
731 match_set_vlan_vid(match, value->be16);
734 case MFF_DL_VLAN_PCP:
736 match_set_dl_vlan_pcp(match, value->u8);
740 match_set_mpls_label(match, 0, value->be32);
744 match_set_mpls_tc(match, 0, value->u8);
748 match_set_mpls_bos(match, 0, value->u8);
752 match_set_nw_src(match, value->be32);
756 match_set_nw_dst(match, value->be32);
760 match_set_ipv6_src(match, &value->ipv6);
764 match_set_ipv6_dst(match, &value->ipv6);
768 match_set_ipv6_label(match, value->be32);
772 match_set_nw_proto(match, value->u8);
776 match_set_nw_dscp(match, value->u8);
779 case MFF_IP_DSCP_SHIFTED:
780 match_set_nw_dscp(match, value->u8 << 2);
784 match_set_nw_ecn(match, value->u8);
788 match_set_nw_ttl(match, value->u8);
792 match_set_nw_frag(match, value->u8);
796 match_set_nw_proto(match, ntohs(value->be16));
800 match_set_nw_src(match, value->be32);
804 match_set_nw_dst(match, value->be32);
809 match_set_arp_sha(match, value->mac);
814 match_set_arp_tha(match, value->mac);
820 match_set_tp_src(match, value->be16);
826 match_set_tp_dst(match, value->be16);
830 match_set_tcp_flags(match, value->be16);
833 case MFF_ICMPV4_TYPE:
834 case MFF_ICMPV6_TYPE:
835 match_set_icmp_type(match, value->u8);
838 case MFF_ICMPV4_CODE:
839 case MFF_ICMPV6_CODE:
840 match_set_icmp_code(match, value->u8);
844 match_set_nd_target(match, &value->ipv6);
853 /* Unwildcard 'mask' member field described by 'mf'. The caller is
854 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
856 mf_mask_field(const struct mf_field *mf, struct flow *mask)
858 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
860 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
861 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
862 * special case. For the rest, calling mf_set_flow_value() is good
864 if (mf->id == MFF_DL_VLAN) {
865 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
867 mf_set_flow_value(mf, &exact_match_mask, mask);
871 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
872 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
874 mf_set_flow_value(const struct mf_field *mf,
875 const union mf_value *value, struct flow *flow)
879 flow->dp_hash = ntohl(value->be32);
882 flow->recirc_id = ntohl(value->be32);
885 flow->tunnel.tun_id = value->be64;
888 flow->tunnel.ip_src = value->be32;
891 flow->tunnel.ip_dst = value->be32;
894 flow->tunnel.flags = ntohs(value->be16);
897 flow->tunnel.ip_tos = value->u8;
900 flow->tunnel.ip_ttl = value->u8;
904 flow->metadata = value->be64;
908 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
911 case MFF_IN_PORT_OXM: {
913 ofputil_port_from_ofp11(value->be32, &port);
914 flow->in_port.ofp_port = port;
918 case MFF_SKB_PRIORITY:
919 flow->skb_priority = ntohl(value->be32);
923 flow->pkt_mark = ntohl(value->be32);
927 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
931 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
935 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
939 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
943 flow->dl_type = value->be16;
947 flow->vlan_tci = value->be16;
951 flow_set_dl_vlan(flow, value->be16);
954 flow_set_vlan_vid(flow, value->be16);
957 case MFF_DL_VLAN_PCP:
959 flow_set_vlan_pcp(flow, value->u8);
963 flow_set_mpls_label(flow, 0, value->be32);
967 flow_set_mpls_tc(flow, 0, value->u8);
971 flow_set_mpls_bos(flow, 0, value->u8);
975 flow->nw_src = value->be32;
979 flow->nw_dst = value->be32;
983 flow->ipv6_src = value->ipv6;
987 flow->ipv6_dst = value->ipv6;
991 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
995 flow->nw_proto = value->u8;
999 flow->nw_tos &= ~IP_DSCP_MASK;
1000 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1003 case MFF_IP_DSCP_SHIFTED:
1004 flow->nw_tos &= ~IP_DSCP_MASK;
1005 flow->nw_tos |= value->u8 << 2;
1009 flow->nw_tos &= ~IP_ECN_MASK;
1010 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1014 flow->nw_ttl = value->u8;
1018 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1022 flow->nw_proto = ntohs(value->be16);
1026 flow->nw_src = value->be32;
1030 flow->nw_dst = value->be32;
1035 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1040 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1046 flow->tp_src = value->be16;
1052 flow->tp_dst = value->be16;
1056 flow->tcp_flags = value->be16;
1059 case MFF_ICMPV4_TYPE:
1060 case MFF_ICMPV6_TYPE:
1061 flow->tp_src = htons(value->u8);
1064 case MFF_ICMPV4_CODE:
1065 case MFF_ICMPV6_CODE:
1066 flow->tp_dst = htons(value->u8);
1070 flow->nd_target = value->ipv6;
1079 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1080 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1083 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1087 for (i = 0; i < n; i++) {
1088 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1092 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1093 * for which 'mask' has a 0-bit keep their existing values. The caller is
1094 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1096 mf_set_flow_value_masked(const struct mf_field *field,
1097 const union mf_value *value,
1098 const union mf_value *mask,
1103 mf_get_value(field, flow, &tmp);
1104 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1105 (uint8_t *) &tmp, field->n_bytes);
1106 mf_set_flow_value(field, &tmp, flow);
1109 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1111 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1114 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1116 union mf_value value;
1118 mf_get_value(mf, flow, &value);
1119 return is_all_zeros(&value, mf->n_bytes);
1122 /* Makes 'match' wildcard field 'mf'.
1124 * The caller is responsible for ensuring that 'match' meets 'mf''s
1127 mf_set_wild(const struct mf_field *mf, struct match *match)
1131 match->flow.dp_hash = 0;
1132 match->wc.masks.dp_hash = 0;
1135 match->flow.recirc_id = 0;
1136 match->wc.masks.recirc_id = 0;
1139 match_set_tun_id_masked(match, htonll(0), htonll(0));
1142 match_set_tun_src_masked(match, htonl(0), htonl(0));
1145 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1148 match_set_tun_flags_masked(match, 0, 0);
1151 match_set_tun_tos_masked(match, 0, 0);
1154 match_set_tun_ttl_masked(match, 0, 0);
1158 match_set_metadata_masked(match, htonll(0), htonll(0));
1162 case MFF_IN_PORT_OXM:
1163 match->flow.in_port.ofp_port = 0;
1164 match->wc.masks.in_port.ofp_port = 0;
1167 case MFF_SKB_PRIORITY:
1168 match->flow.skb_priority = 0;
1169 match->wc.masks.skb_priority = 0;
1173 match->flow.pkt_mark = 0;
1174 match->wc.masks.pkt_mark = 0;
1178 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1182 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1186 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1187 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1191 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1192 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1196 match->flow.dl_type = htons(0);
1197 match->wc.masks.dl_type = htons(0);
1201 match_set_dl_tci_masked(match, htons(0), htons(0));
1206 match_set_any_vid(match);
1209 case MFF_DL_VLAN_PCP:
1211 match_set_any_pcp(match);
1214 case MFF_MPLS_LABEL:
1215 match_set_any_mpls_label(match, 0);
1219 match_set_any_mpls_tc(match, 0);
1223 match_set_any_mpls_bos(match, 0);
1228 match_set_nw_src_masked(match, htonl(0), htonl(0));
1233 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1237 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1238 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1242 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1243 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1246 case MFF_IPV6_LABEL:
1247 match->wc.masks.ipv6_label = htonl(0);
1248 match->flow.ipv6_label = htonl(0);
1252 match->wc.masks.nw_proto = 0;
1253 match->flow.nw_proto = 0;
1257 case MFF_IP_DSCP_SHIFTED:
1258 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1259 match->flow.nw_tos &= ~IP_DSCP_MASK;
1263 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1264 match->flow.nw_tos &= ~IP_ECN_MASK;
1268 match->wc.masks.nw_ttl = 0;
1269 match->flow.nw_ttl = 0;
1273 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1274 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1278 match->wc.masks.nw_proto = 0;
1279 match->flow.nw_proto = 0;
1284 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1285 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1290 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1291 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1297 case MFF_ICMPV4_TYPE:
1298 case MFF_ICMPV6_TYPE:
1299 match->wc.masks.tp_src = htons(0);
1300 match->flow.tp_src = htons(0);
1306 case MFF_ICMPV4_CODE:
1307 case MFF_ICMPV6_CODE:
1308 match->wc.masks.tp_dst = htons(0);
1309 match->flow.tp_dst = htons(0);
1313 match->wc.masks.tcp_flags = htons(0);
1314 match->flow.tcp_flags = htons(0);
1318 memset(&match->wc.masks.nd_target, 0,
1319 sizeof match->wc.masks.nd_target);
1320 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1329 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1330 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1331 * with a 1-bit indicating that the corresponding value bit must match and a
1332 * 0-bit indicating a don't-care.
1334 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1335 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1336 * call is equivalent to mf_set_wild(mf, match).
1338 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1339 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1340 enum ofputil_protocol
1341 mf_set(const struct mf_field *mf,
1342 const union mf_value *value, const union mf_value *mask,
1343 struct match *match)
1345 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1346 mf_set_value(mf, value, match);
1347 return mf->usable_protocols_exact;
1348 } else if (is_all_zeros(mask, mf->n_bytes)) {
1349 mf_set_wild(mf, match);
1350 return OFPUTIL_P_ANY;
1356 case MFF_IN_PORT_OXM:
1357 case MFF_SKB_PRIORITY:
1360 case MFF_DL_VLAN_PCP:
1362 case MFF_MPLS_LABEL:
1368 case MFF_IP_DSCP_SHIFTED:
1371 case MFF_ICMPV4_TYPE:
1372 case MFF_ICMPV4_CODE:
1373 case MFF_ICMPV6_TYPE:
1374 case MFF_ICMPV6_CODE:
1375 return OFPUTIL_P_NONE;
1378 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1381 match_set_tun_id_masked(match, value->be64, mask->be64);
1384 match_set_tun_src_masked(match, value->be32, mask->be32);
1387 match_set_tun_dst_masked(match, value->be32, mask->be32);
1390 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1393 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1396 match_set_tun_tos_masked(match, value->u8, mask->u8);
1400 match_set_metadata_masked(match, value->be64, mask->be64);
1404 match_set_reg_masked(match, mf->id - MFF_REG0,
1405 ntohl(value->be32), ntohl(mask->be32));
1409 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1410 ntohll(value->be64), ntohll(mask->be64));
1414 match_set_pkt_mark_masked(match, ntohl(value->be32),
1419 match_set_dl_dst_masked(match, value->mac, mask->mac);
1423 match_set_dl_src_masked(match, value->mac, mask->mac);
1428 match_set_arp_sha_masked(match, value->mac, mask->mac);
1433 match_set_arp_tha_masked(match, value->mac, mask->mac);
1437 match_set_dl_tci_masked(match, value->be16, mask->be16);
1441 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1445 match_set_nw_src_masked(match, value->be32, mask->be32);
1449 match_set_nw_dst_masked(match, value->be32, mask->be32);
1453 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1457 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1460 case MFF_IPV6_LABEL:
1461 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1462 mf_set_value(mf, value, match);
1464 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1469 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1473 match_set_nw_frag_masked(match, value->u8, mask->u8);
1477 match_set_nw_src_masked(match, value->be32, mask->be32);
1481 match_set_nw_dst_masked(match, value->be32, mask->be32);
1487 match_set_tp_src_masked(match, value->be16, mask->be16);
1493 match_set_tp_dst_masked(match, value->be16, mask->be16);
1497 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1505 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1506 || ip_is_cidr(mask->be32))
1507 ? mf->usable_protocols_cidr
1508 : mf->usable_protocols_bitwise);
1512 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1516 VLOG_WARN_RL(&rl, "unknown %s field", type);
1517 return OFPERR_OFPBAC_BAD_SET_TYPE;
1518 } else if (!sf->n_bits) {
1519 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1520 return OFPERR_OFPBAC_BAD_SET_LEN;
1521 } else if (sf->ofs >= sf->field->n_bits) {
1522 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1523 sf->ofs, sf->field->n_bits, type, sf->field->name);
1524 return OFPERR_OFPBAC_BAD_SET_LEN;
1525 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1526 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1527 "of %s field %s", sf->ofs, sf->n_bits,
1528 sf->field->n_bits, type, sf->field->name);
1529 return OFPERR_OFPBAC_BAD_SET_LEN;
1530 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1531 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1532 type, sf->field->name);
1533 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1539 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1540 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1543 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1545 return mf_check__(sf, flow, "source");
1548 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1549 * if so, otherwise an OpenFlow error code (e.g. as returned by
1552 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1554 int error = mf_check__(sf, flow, "destination");
1555 if (!error && !sf->field->writable) {
1556 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1558 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1563 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1564 * 'value' and 'mask', respectively. */
1566 mf_get(const struct mf_field *mf, const struct match *match,
1567 union mf_value *value, union mf_value *mask)
1569 mf_get_value(mf, &match->flow, value);
1570 mf_get_mask(mf, &match->wc, mask);
1574 mf_from_integer_string(const struct mf_field *mf, const char *s,
1575 uint8_t *valuep, uint8_t *maskp)
1577 unsigned long long int integer, mask;
1582 integer = strtoull(s, &tail, 0);
1583 if (errno || (*tail != '\0' && *tail != '/')) {
1588 mask = strtoull(tail + 1, &tail, 0);
1589 if (errno || *tail != '\0') {
1596 for (i = mf->n_bytes - 1; i >= 0; i--) {
1597 valuep[i] = integer;
1603 return xasprintf("%s: value too large for %u-byte field %s",
1604 s, mf->n_bytes, mf->name);
1609 return xasprintf("%s: bad syntax for %s", s, mf->name);
1613 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1614 uint8_t mac[ETH_ADDR_LEN],
1615 uint8_t mask[ETH_ADDR_LEN])
1619 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1622 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1623 && n == strlen(s)) {
1624 memset(mask, 0xff, ETH_ADDR_LEN);
1629 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1630 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1631 && n == strlen(s)) {
1635 return xasprintf("%s: invalid Ethernet address", s);
1639 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1640 ovs_be32 *ip, ovs_be32 *mask)
1644 ovs_assert(mf->n_bytes == sizeof *ip);
1646 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1647 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1649 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1650 if (prefix <= 0 || prefix > 32) {
1651 return xasprintf("%s: network prefix bits not between 1 and "
1653 } else if (prefix == 32) {
1654 *mask = OVS_BE32_MAX;
1656 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1658 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1659 *mask = OVS_BE32_MAX;
1661 return xasprintf("%s: invalid IP address", s);
1667 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1668 struct in6_addr *value, struct in6_addr *mask)
1670 char *str = xstrdup(s);
1671 char *save_ptr = NULL;
1672 const char *name, *netmask;
1675 ovs_assert(mf->n_bytes == sizeof *value);
1677 name = strtok_r(str, "/", &save_ptr);
1678 retval = name ? lookup_ipv6(name, value) : EINVAL;
1682 err = xasprintf("%s: could not convert to IPv6 address", str);
1688 netmask = strtok_r(NULL, "/", &save_ptr);
1690 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1691 int prefix = atoi(netmask);
1692 if (prefix <= 0 || prefix > 128) {
1694 return xasprintf("%s: prefix bits not between 1 and 128", s);
1696 *mask = ipv6_create_mask(prefix);
1700 *mask = in6addr_exact;
1708 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1709 ovs_be16 *valuep, ovs_be16 *maskp)
1713 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1715 if (ofputil_port_from_string(s, &port)) {
1716 *valuep = htons(ofp_to_u16(port));
1717 *maskp = OVS_BE16_MAX;
1720 return xasprintf("%s: port value out of range for %s", s, mf->name);
1724 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1725 ovs_be32 *valuep, ovs_be32 *maskp)
1729 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1730 if (ofputil_port_from_string(s, &port)) {
1731 *valuep = ofputil_port_to_ofp11(port);
1732 *maskp = OVS_BE32_MAX;
1735 return xasprintf("%s: port value out of range for %s", s, mf->name);
1738 struct frag_handling {
1744 static const struct frag_handling all_frags[] = {
1745 #define A FLOW_NW_FRAG_ANY
1746 #define L FLOW_NW_FRAG_LATER
1747 /* name mask value */
1750 { "first", A|L, A },
1751 { "later", A|L, A|L },
1756 { "not_later", L, 0 },
1763 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1765 const struct frag_handling *h;
1767 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1768 if (!strcasecmp(s, h->name)) {
1769 /* We force the upper bits of the mask on to make mf_parse_value()
1770 * happy (otherwise it will never think it's an exact match.) */
1771 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1777 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1778 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1782 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1785 uint32_t result = 0;
1786 char *save_ptr = NULL;
1789 char *s = xstrdup(s_);
1791 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1792 name = strtok_r(NULL, " |", &save_ptr)) {
1794 unsigned long long int flags;
1797 if (ovs_scan(name, "%lli", &flags)) {
1801 name_len = strlen(name);
1802 for (bit = 1; bit; bit <<= 1) {
1803 const char *fname = bit_to_string(bit);
1810 len = strlen(fname);
1811 if (len != name_len) {
1814 if (!strncmp(name, fname, len)) {
1826 *res = htons(result);
1833 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1835 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1836 *maskp = OVS_BE16_MAX;
1840 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1841 "\"csum\", \"key\")", s);
1845 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1852 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1853 *flagsp = htons(flags);
1854 *maskp = htons(mask);
1857 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1858 *flagsp = htons(flags);
1859 *maskp = OVS_BE16_MAX;
1863 while (*s != '\0') {
1875 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1876 "or '-' (NOT SET)", s);
1880 name_len = strcspn(s,"+-");
1882 for (bit = 1; bit; bit <<= 1) {
1883 const char *fname = packet_tcp_flag_to_string(bit);
1890 len = strlen(fname);
1891 if (len != name_len) {
1894 if (!strncmp(s, fname, len)) {
1896 return xasprintf("%s: Each TCP flag can be specified only "
1908 return xasprintf("%s: unknown TCP flag(s)", s);
1913 *flagsp = htons(flags);
1914 *maskp = htons(mask);
1919 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1920 * NULL if successful, otherwise a malloc()'d string describing the error. */
1922 mf_parse(const struct mf_field *mf, const char *s,
1923 union mf_value *value, union mf_value *mask)
1927 if (!strcmp(s, "*")) {
1928 memset(value, 0, mf->n_bytes);
1929 memset(mask, 0, mf->n_bytes);
1933 switch (mf->string) {
1935 case MFS_HEXADECIMAL:
1936 error = mf_from_integer_string(mf, s,
1937 (uint8_t *) value, (uint8_t *) mask);
1941 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1945 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1949 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1953 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1956 case MFS_OFP_PORT_OXM:
1957 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1961 error = mf_from_frag_string(s, &value->u8, &mask->u8);
1965 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1966 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
1970 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1971 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
1978 if (!error && !mf_is_mask_valid(mf, mask)) {
1979 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
1984 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1985 * successful, otherwise a malloc()'d string describing the error. */
1987 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1989 union mf_value mask;
1992 error = mf_parse(mf, s, value, &mask);
1997 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1998 return xasprintf("%s: wildcards not allowed here", s);
2004 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2005 const uint8_t *maskp, struct ds *s)
2007 unsigned long long int integer;
2010 ovs_assert(mf->n_bytes <= 8);
2013 for (i = 0; i < mf->n_bytes; i++) {
2014 integer = (integer << 8) | valuep[i];
2016 if (mf->string == MFS_HEXADECIMAL) {
2017 ds_put_format(s, "%#llx", integer);
2019 ds_put_format(s, "%lld", integer);
2023 unsigned long long int mask;
2026 for (i = 0; i < mf->n_bytes; i++) {
2027 mask = (mask << 8) | maskp[i];
2030 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2031 * not sure that that a bit-mask written in decimal is ever easier to
2032 * understand than the same bit-mask written in hexadecimal. */
2033 ds_put_format(s, "/%#llx", mask);
2038 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2040 const struct frag_handling *h;
2042 mask &= FLOW_NW_FRAG_MASK;
2045 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2046 if (value == h->value && mask == h->mask) {
2047 ds_put_cstr(s, h->name);
2051 ds_put_cstr(s, "<error>");
2055 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2057 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2061 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2063 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2067 /* Appends to 's' a string representation of field 'mf' whose value is in
2068 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2070 mf_format(const struct mf_field *mf,
2071 const union mf_value *value, const union mf_value *mask,
2075 if (is_all_zeros(mask, mf->n_bytes)) {
2076 ds_put_cstr(s, "ANY");
2078 } else if (is_all_ones(mask, mf->n_bytes)) {
2083 switch (mf->string) {
2084 case MFS_OFP_PORT_OXM:
2087 ofputil_port_from_ofp11(value->be32, &port);
2088 ofputil_format_port(port, s);
2094 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2099 case MFS_HEXADECIMAL:
2100 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2104 eth_format_masked(value->mac, mask->mac, s);
2108 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2112 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2116 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2120 mf_format_tnl_flags_string(&value->be16, s);
2124 mf_format_tcp_flags_string(value->be16,
2125 mask ? mask->be16 : OVS_BE16_MAX, s);
2133 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2134 * least-significant bits in 'x'.
2137 mf_write_subfield_flow(const struct mf_subfield *sf,
2138 const union mf_subvalue *x, struct flow *flow)
2140 const struct mf_field *field = sf->field;
2141 union mf_value value;
2143 mf_get_value(field, flow, &value);
2144 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2145 sf->ofs, sf->n_bits);
2146 mf_set_flow_value(field, &value, flow);
2149 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2150 * least-significant bits in 'x'.
2153 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2154 struct match *match)
2156 const struct mf_field *field = sf->field;
2157 union mf_value value, mask;
2159 mf_get(field, match, &value, &mask);
2160 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2161 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2162 mf_set(field, &value, &mask, match);
2165 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2166 * reading 'flow', e.g. as checked by mf_check_src(). */
2168 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2169 union mf_subvalue *x)
2171 union mf_value value;
2173 mf_get_value(sf->field, flow, &value);
2175 memset(x, 0, sizeof *x);
2176 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2181 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2182 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2185 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2187 union mf_value value;
2189 mf_get_value(sf->field, flow, &value);
2190 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2194 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2198 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2199 if (subvalue->u8[i]) {
2200 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2201 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2202 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2207 ds_put_char(s, '0');