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
278 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
280 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
281 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
283 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
284 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
286 return is_icmpv4(flow);
288 return is_icmpv6(flow);
291 return (is_icmpv6(flow)
292 && flow->tp_dst == htons(0)
293 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
294 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
296 return (is_icmpv6(flow)
297 && flow->tp_dst == htons(0)
298 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
300 return (is_icmpv6(flow)
301 && flow->tp_dst == htons(0)
302 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
308 /* Set field and it's prerequisities in the mask.
309 * This is only ever called for writeable 'mf's, but we do not make the
310 * distinction here. */
312 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
314 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
316 mf_set_flow_value(mf, &exact_match_mask, mask);
318 switch (mf->prereqs) {
322 mask->tp_src = OVS_BE16_MAX;
323 mask->tp_dst = OVS_BE16_MAX;
330 /* nw_frag always unwildcarded. */
331 mask->nw_proto = 0xff;
338 mask->dl_type = OVS_BE16_MAX;
341 mask->vlan_tci |= htons(VLAN_CFI);
349 /* Returns true if 'value' may be a valid value *as part of a masked match*,
352 * A value is not rejected just because it is not valid for the field in
353 * question, but only if it doesn't make sense to test the bits in question at
354 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
355 * without the VLAN_CFI bit being set, but we can't reject those values because
356 * it is still legitimate to test just for those bits (see the documentation
357 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
358 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
360 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
373 case MFF_SKB_PRIORITY:
397 case MFF_ICMPV4_TYPE:
398 case MFF_ICMPV4_CODE:
399 case MFF_ICMPV6_TYPE:
400 case MFF_ICMPV6_CODE:
406 case MFF_IN_PORT_OXM:
407 case MFF_ACTSET_OUTPUT: {
409 return !ofputil_port_from_ofp11(value->be32, &port);
413 return !(value->u8 & ~IP_DSCP_MASK);
414 case MFF_IP_DSCP_SHIFTED:
415 return !(value->u8 & (~IP_DSCP_MASK >> 2));
417 return !(value->u8 & ~IP_ECN_MASK);
419 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
421 return !(value->be16 & ~htons(0x0fff));
424 return !(value->be16 & htons(0xff00));
427 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
429 return !(value->be16 & htons(VLAN_PCP_MASK));
431 case MFF_DL_VLAN_PCP:
433 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
436 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
439 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
442 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
445 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
453 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
454 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
456 mf_get_value(const struct mf_field *mf, const struct flow *flow,
457 union mf_value *value)
461 value->be32 = htonl(flow->dp_hash);
464 value->be32 = htonl(flow->recirc_id);
467 value->be64 = flow->tunnel.tun_id;
470 value->be32 = flow->tunnel.ip_src;
473 value->be32 = flow->tunnel.ip_dst;
476 value->be16 = htons(flow->tunnel.flags);
479 value->u8 = flow->tunnel.ip_ttl;
482 value->u8 = flow->tunnel.ip_tos;
486 value->be64 = flow->metadata;
490 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
492 case MFF_IN_PORT_OXM:
493 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
495 case MFF_ACTSET_OUTPUT:
496 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
499 case MFF_SKB_PRIORITY:
500 value->be32 = htonl(flow->skb_priority);
504 value->be32 = htonl(flow->pkt_mark);
508 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
512 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
516 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
520 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
524 value->be16 = flow->dl_type;
528 value->be16 = flow->vlan_tci;
532 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
535 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
538 case MFF_DL_VLAN_PCP:
540 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
544 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
548 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
552 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
556 value->be32 = flow->nw_src;
560 value->be32 = flow->nw_dst;
564 value->ipv6 = flow->ipv6_src;
568 value->ipv6 = flow->ipv6_dst;
572 value->be32 = flow->ipv6_label;
576 value->u8 = flow->nw_proto;
580 value->u8 = flow->nw_tos & IP_DSCP_MASK;
583 case MFF_IP_DSCP_SHIFTED:
584 value->u8 = flow->nw_tos >> 2;
588 value->u8 = flow->nw_tos & IP_ECN_MASK;
592 value->u8 = flow->nw_ttl;
596 value->u8 = flow->nw_frag;
600 value->be16 = htons(flow->nw_proto);
604 value->be32 = flow->nw_src;
608 value->be32 = flow->nw_dst;
613 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
618 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
624 value->be16 = flow->tp_src;
630 value->be16 = flow->tp_dst;
634 value->be16 = flow->tcp_flags;
637 case MFF_ICMPV4_TYPE:
638 case MFF_ICMPV6_TYPE:
639 value->u8 = ntohs(flow->tp_src);
642 case MFF_ICMPV4_CODE:
643 case MFF_ICMPV6_CODE:
644 value->u8 = ntohs(flow->tp_dst);
648 value->ipv6 = flow->nd_target;
657 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
658 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
661 mf_set_value(const struct mf_field *mf,
662 const union mf_value *value, struct match *match)
666 match_set_dp_hash(match, ntohl(value->be32));
669 match_set_recirc_id(match, ntohl(value->be32));
672 match_set_tun_id(match, value->be64);
675 match_set_tun_src(match, value->be32);
678 match_set_tun_dst(match, value->be32);
681 match_set_tun_flags(match, ntohs(value->be16));
684 match_set_tun_tos(match, value->u8);
687 match_set_tun_ttl(match, value->u8);
691 match_set_metadata(match, value->be64);
695 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
698 case MFF_IN_PORT_OXM: {
700 ofputil_port_from_ofp11(value->be32, &port);
701 match_set_in_port(match, port);
704 case MFF_ACTSET_OUTPUT: {
706 ofputil_port_from_ofp11(value->be32, &port);
707 match_set_actset_output(match, port);
711 case MFF_SKB_PRIORITY:
712 match_set_skb_priority(match, ntohl(value->be32));
716 match_set_pkt_mark(match, ntohl(value->be32));
720 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
724 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
728 match_set_dl_src(match, value->mac);
732 match_set_dl_dst(match, value->mac);
736 match_set_dl_type(match, value->be16);
740 match_set_dl_tci(match, value->be16);
744 match_set_dl_vlan(match, value->be16);
747 match_set_vlan_vid(match, value->be16);
750 case MFF_DL_VLAN_PCP:
752 match_set_dl_vlan_pcp(match, value->u8);
756 match_set_mpls_label(match, 0, value->be32);
760 match_set_mpls_tc(match, 0, value->u8);
764 match_set_mpls_bos(match, 0, value->u8);
768 match_set_nw_src(match, value->be32);
772 match_set_nw_dst(match, value->be32);
776 match_set_ipv6_src(match, &value->ipv6);
780 match_set_ipv6_dst(match, &value->ipv6);
784 match_set_ipv6_label(match, value->be32);
788 match_set_nw_proto(match, value->u8);
792 match_set_nw_dscp(match, value->u8);
795 case MFF_IP_DSCP_SHIFTED:
796 match_set_nw_dscp(match, value->u8 << 2);
800 match_set_nw_ecn(match, value->u8);
804 match_set_nw_ttl(match, value->u8);
808 match_set_nw_frag(match, value->u8);
812 match_set_nw_proto(match, ntohs(value->be16));
816 match_set_nw_src(match, value->be32);
820 match_set_nw_dst(match, value->be32);
825 match_set_arp_sha(match, value->mac);
830 match_set_arp_tha(match, value->mac);
836 match_set_tp_src(match, value->be16);
842 match_set_tp_dst(match, value->be16);
846 match_set_tcp_flags(match, value->be16);
849 case MFF_ICMPV4_TYPE:
850 case MFF_ICMPV6_TYPE:
851 match_set_icmp_type(match, value->u8);
854 case MFF_ICMPV4_CODE:
855 case MFF_ICMPV6_CODE:
856 match_set_icmp_code(match, value->u8);
860 match_set_nd_target(match, &value->ipv6);
869 /* Unwildcard 'mask' member field described by 'mf'. The caller is
870 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
872 mf_mask_field(const struct mf_field *mf, struct flow *mask)
874 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
876 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
877 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
878 * special case. For the rest, calling mf_set_flow_value() is good
880 if (mf->id == MFF_DL_VLAN) {
881 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
883 mf_set_flow_value(mf, &exact_match_mask, mask);
887 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
888 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
890 mf_set_flow_value(const struct mf_field *mf,
891 const union mf_value *value, struct flow *flow)
895 flow->dp_hash = ntohl(value->be32);
898 flow->recirc_id = ntohl(value->be32);
901 flow->tunnel.tun_id = value->be64;
904 flow->tunnel.ip_src = value->be32;
907 flow->tunnel.ip_dst = value->be32;
910 flow->tunnel.flags = ntohs(value->be16);
913 flow->tunnel.ip_tos = value->u8;
916 flow->tunnel.ip_ttl = value->u8;
920 flow->metadata = value->be64;
924 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
927 case MFF_IN_PORT_OXM:
928 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
930 case MFF_ACTSET_OUTPUT:
931 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
934 case MFF_SKB_PRIORITY:
935 flow->skb_priority = ntohl(value->be32);
939 flow->pkt_mark = ntohl(value->be32);
943 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
947 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
951 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
955 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
959 flow->dl_type = value->be16;
963 flow->vlan_tci = value->be16;
967 flow_set_dl_vlan(flow, value->be16);
970 flow_set_vlan_vid(flow, value->be16);
973 case MFF_DL_VLAN_PCP:
975 flow_set_vlan_pcp(flow, value->u8);
979 flow_set_mpls_label(flow, 0, value->be32);
983 flow_set_mpls_tc(flow, 0, value->u8);
987 flow_set_mpls_bos(flow, 0, value->u8);
991 flow->nw_src = value->be32;
995 flow->nw_dst = value->be32;
999 flow->ipv6_src = value->ipv6;
1003 flow->ipv6_dst = value->ipv6;
1006 case MFF_IPV6_LABEL:
1007 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1011 flow->nw_proto = value->u8;
1015 flow->nw_tos &= ~IP_DSCP_MASK;
1016 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1019 case MFF_IP_DSCP_SHIFTED:
1020 flow->nw_tos &= ~IP_DSCP_MASK;
1021 flow->nw_tos |= value->u8 << 2;
1025 flow->nw_tos &= ~IP_ECN_MASK;
1026 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1030 flow->nw_ttl = value->u8;
1034 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1038 flow->nw_proto = ntohs(value->be16);
1042 flow->nw_src = value->be32;
1046 flow->nw_dst = value->be32;
1051 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1056 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1062 flow->tp_src = value->be16;
1068 flow->tp_dst = value->be16;
1072 flow->tcp_flags = value->be16;
1075 case MFF_ICMPV4_TYPE:
1076 case MFF_ICMPV6_TYPE:
1077 flow->tp_src = htons(value->u8);
1080 case MFF_ICMPV4_CODE:
1081 case MFF_ICMPV6_CODE:
1082 flow->tp_dst = htons(value->u8);
1086 flow->nd_target = value->ipv6;
1095 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1096 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1099 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1103 for (i = 0; i < n; i++) {
1104 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1108 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1109 * for which 'mask' has a 0-bit keep their existing values. The caller is
1110 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1112 mf_set_flow_value_masked(const struct mf_field *field,
1113 const union mf_value *value,
1114 const union mf_value *mask,
1119 mf_get_value(field, flow, &tmp);
1120 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1121 (uint8_t *) &tmp, field->n_bytes);
1122 mf_set_flow_value(field, &tmp, flow);
1125 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1127 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1130 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1132 union mf_value value;
1134 mf_get_value(mf, flow, &value);
1135 return is_all_zeros(&value, mf->n_bytes);
1138 /* Makes 'match' wildcard field 'mf'.
1140 * The caller is responsible for ensuring that 'match' meets 'mf''s
1143 mf_set_wild(const struct mf_field *mf, struct match *match)
1147 match->flow.dp_hash = 0;
1148 match->wc.masks.dp_hash = 0;
1151 match->flow.recirc_id = 0;
1152 match->wc.masks.recirc_id = 0;
1155 match_set_tun_id_masked(match, htonll(0), htonll(0));
1158 match_set_tun_src_masked(match, htonl(0), htonl(0));
1161 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1164 match_set_tun_flags_masked(match, 0, 0);
1167 match_set_tun_tos_masked(match, 0, 0);
1170 match_set_tun_ttl_masked(match, 0, 0);
1174 match_set_metadata_masked(match, htonll(0), htonll(0));
1178 case MFF_IN_PORT_OXM:
1179 match->flow.in_port.ofp_port = 0;
1180 match->wc.masks.in_port.ofp_port = 0;
1182 case MFF_ACTSET_OUTPUT:
1183 match->flow.actset_output = 0;
1184 match->wc.masks.actset_output = 0;
1187 case MFF_SKB_PRIORITY:
1188 match->flow.skb_priority = 0;
1189 match->wc.masks.skb_priority = 0;
1193 match->flow.pkt_mark = 0;
1194 match->wc.masks.pkt_mark = 0;
1198 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1202 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1206 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1207 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1211 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1212 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1216 match->flow.dl_type = htons(0);
1217 match->wc.masks.dl_type = htons(0);
1221 match_set_dl_tci_masked(match, htons(0), htons(0));
1226 match_set_any_vid(match);
1229 case MFF_DL_VLAN_PCP:
1231 match_set_any_pcp(match);
1234 case MFF_MPLS_LABEL:
1235 match_set_any_mpls_label(match, 0);
1239 match_set_any_mpls_tc(match, 0);
1243 match_set_any_mpls_bos(match, 0);
1248 match_set_nw_src_masked(match, htonl(0), htonl(0));
1253 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1257 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1258 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1262 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1263 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1266 case MFF_IPV6_LABEL:
1267 match->wc.masks.ipv6_label = htonl(0);
1268 match->flow.ipv6_label = htonl(0);
1272 match->wc.masks.nw_proto = 0;
1273 match->flow.nw_proto = 0;
1277 case MFF_IP_DSCP_SHIFTED:
1278 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1279 match->flow.nw_tos &= ~IP_DSCP_MASK;
1283 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1284 match->flow.nw_tos &= ~IP_ECN_MASK;
1288 match->wc.masks.nw_ttl = 0;
1289 match->flow.nw_ttl = 0;
1293 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1294 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1298 match->wc.masks.nw_proto = 0;
1299 match->flow.nw_proto = 0;
1304 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1305 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1310 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1311 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1317 case MFF_ICMPV4_TYPE:
1318 case MFF_ICMPV6_TYPE:
1319 match->wc.masks.tp_src = htons(0);
1320 match->flow.tp_src = htons(0);
1326 case MFF_ICMPV4_CODE:
1327 case MFF_ICMPV6_CODE:
1328 match->wc.masks.tp_dst = htons(0);
1329 match->flow.tp_dst = htons(0);
1333 match->wc.masks.tcp_flags = htons(0);
1334 match->flow.tcp_flags = htons(0);
1338 memset(&match->wc.masks.nd_target, 0,
1339 sizeof match->wc.masks.nd_target);
1340 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1349 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1350 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1351 * with a 1-bit indicating that the corresponding value bit must match and a
1352 * 0-bit indicating a don't-care.
1354 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1355 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1356 * call is equivalent to mf_set_wild(mf, match).
1358 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1359 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1360 enum ofputil_protocol
1361 mf_set(const struct mf_field *mf,
1362 const union mf_value *value, const union mf_value *mask,
1363 struct match *match)
1365 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1366 mf_set_value(mf, value, match);
1367 return mf->usable_protocols_exact;
1368 } else if (is_all_zeros(mask, mf->n_bytes)) {
1369 mf_set_wild(mf, match);
1370 return OFPUTIL_P_ANY;
1376 case MFF_IN_PORT_OXM:
1377 case MFF_ACTSET_OUTPUT:
1378 case MFF_SKB_PRIORITY:
1381 case MFF_DL_VLAN_PCP:
1383 case MFF_MPLS_LABEL:
1389 case MFF_IP_DSCP_SHIFTED:
1392 case MFF_ICMPV4_TYPE:
1393 case MFF_ICMPV4_CODE:
1394 case MFF_ICMPV6_TYPE:
1395 case MFF_ICMPV6_CODE:
1396 return OFPUTIL_P_NONE;
1399 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1402 match_set_tun_id_masked(match, value->be64, mask->be64);
1405 match_set_tun_src_masked(match, value->be32, mask->be32);
1408 match_set_tun_dst_masked(match, value->be32, mask->be32);
1411 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1414 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1417 match_set_tun_tos_masked(match, value->u8, mask->u8);
1421 match_set_metadata_masked(match, value->be64, mask->be64);
1425 match_set_reg_masked(match, mf->id - MFF_REG0,
1426 ntohl(value->be32), ntohl(mask->be32));
1430 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1431 ntohll(value->be64), ntohll(mask->be64));
1435 match_set_pkt_mark_masked(match, ntohl(value->be32),
1440 match_set_dl_dst_masked(match, value->mac, mask->mac);
1444 match_set_dl_src_masked(match, value->mac, mask->mac);
1449 match_set_arp_sha_masked(match, value->mac, mask->mac);
1454 match_set_arp_tha_masked(match, value->mac, mask->mac);
1458 match_set_dl_tci_masked(match, value->be16, mask->be16);
1462 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1466 match_set_nw_src_masked(match, value->be32, mask->be32);
1470 match_set_nw_dst_masked(match, value->be32, mask->be32);
1474 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1478 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1481 case MFF_IPV6_LABEL:
1482 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1483 mf_set_value(mf, value, match);
1485 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1490 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1494 match_set_nw_frag_masked(match, value->u8, mask->u8);
1498 match_set_nw_src_masked(match, value->be32, mask->be32);
1502 match_set_nw_dst_masked(match, value->be32, mask->be32);
1508 match_set_tp_src_masked(match, value->be16, mask->be16);
1514 match_set_tp_dst_masked(match, value->be16, mask->be16);
1518 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1526 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1527 || ip_is_cidr(mask->be32))
1528 ? mf->usable_protocols_cidr
1529 : mf->usable_protocols_bitwise);
1533 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1537 VLOG_WARN_RL(&rl, "unknown %s field", type);
1538 return OFPERR_OFPBAC_BAD_SET_TYPE;
1539 } else if (!sf->n_bits) {
1540 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1541 return OFPERR_OFPBAC_BAD_SET_LEN;
1542 } else if (sf->ofs >= sf->field->n_bits) {
1543 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1544 sf->ofs, sf->field->n_bits, type, sf->field->name);
1545 return OFPERR_OFPBAC_BAD_SET_LEN;
1546 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1547 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1548 "of %s field %s", sf->ofs, sf->n_bits,
1549 sf->field->n_bits, type, sf->field->name);
1550 return OFPERR_OFPBAC_BAD_SET_LEN;
1551 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1552 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1553 type, sf->field->name);
1554 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1560 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1561 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1564 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1566 return mf_check__(sf, flow, "source");
1569 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1570 * if so, otherwise an OpenFlow error code (e.g. as returned by
1573 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1575 int error = mf_check__(sf, flow, "destination");
1576 if (!error && !sf->field->writable) {
1577 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1579 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1584 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1585 * 'value' and 'mask', respectively. */
1587 mf_get(const struct mf_field *mf, const struct match *match,
1588 union mf_value *value, union mf_value *mask)
1590 mf_get_value(mf, &match->flow, value);
1591 mf_get_mask(mf, &match->wc, mask);
1595 mf_from_integer_string(const struct mf_field *mf, const char *s,
1596 uint8_t *valuep, uint8_t *maskp)
1598 unsigned long long int integer, mask;
1603 integer = strtoull(s, &tail, 0);
1604 if (errno || (*tail != '\0' && *tail != '/')) {
1609 mask = strtoull(tail + 1, &tail, 0);
1610 if (errno || *tail != '\0') {
1617 for (i = mf->n_bytes - 1; i >= 0; i--) {
1618 valuep[i] = integer;
1624 return xasprintf("%s: value too large for %u-byte field %s",
1625 s, mf->n_bytes, mf->name);
1630 return xasprintf("%s: bad syntax for %s", s, mf->name);
1634 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1635 uint8_t mac[ETH_ADDR_LEN],
1636 uint8_t mask[ETH_ADDR_LEN])
1640 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1643 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1644 && n == strlen(s)) {
1645 memset(mask, 0xff, ETH_ADDR_LEN);
1650 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1651 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1652 && n == strlen(s)) {
1656 return xasprintf("%s: invalid Ethernet address", s);
1660 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1661 ovs_be32 *ip, ovs_be32 *mask)
1665 ovs_assert(mf->n_bytes == sizeof *ip);
1667 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1668 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1670 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1671 if (prefix <= 0 || prefix > 32) {
1672 return xasprintf("%s: network prefix bits not between 1 and "
1674 } else if (prefix == 32) {
1675 *mask = OVS_BE32_MAX;
1677 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1679 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1680 *mask = OVS_BE32_MAX;
1682 return xasprintf("%s: invalid IP address", s);
1688 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1689 struct in6_addr *value, struct in6_addr *mask)
1691 char *str = xstrdup(s);
1692 char *save_ptr = NULL;
1693 const char *name, *netmask;
1696 ovs_assert(mf->n_bytes == sizeof *value);
1698 name = strtok_r(str, "/", &save_ptr);
1699 retval = name ? lookup_ipv6(name, value) : EINVAL;
1703 err = xasprintf("%s: could not convert to IPv6 address", str);
1709 netmask = strtok_r(NULL, "/", &save_ptr);
1711 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1712 int prefix = atoi(netmask);
1713 if (prefix <= 0 || prefix > 128) {
1715 return xasprintf("%s: prefix bits not between 1 and 128", s);
1717 *mask = ipv6_create_mask(prefix);
1721 *mask = in6addr_exact;
1729 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1730 ovs_be16 *valuep, ovs_be16 *maskp)
1734 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1736 if (ofputil_port_from_string(s, &port)) {
1737 *valuep = htons(ofp_to_u16(port));
1738 *maskp = OVS_BE16_MAX;
1741 return xasprintf("%s: port value out of range for %s", s, mf->name);
1745 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1746 ovs_be32 *valuep, ovs_be32 *maskp)
1750 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1751 if (ofputil_port_from_string(s, &port)) {
1752 *valuep = ofputil_port_to_ofp11(port);
1753 *maskp = OVS_BE32_MAX;
1756 return xasprintf("%s: port value out of range for %s", s, mf->name);
1759 struct frag_handling {
1765 static const struct frag_handling all_frags[] = {
1766 #define A FLOW_NW_FRAG_ANY
1767 #define L FLOW_NW_FRAG_LATER
1768 /* name mask value */
1771 { "first", A|L, A },
1772 { "later", A|L, A|L },
1777 { "not_later", L, 0 },
1784 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1786 const struct frag_handling *h;
1788 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1789 if (!strcasecmp(s, h->name)) {
1790 /* We force the upper bits of the mask on to make mf_parse_value()
1791 * happy (otherwise it will never think it's an exact match.) */
1792 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1798 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1799 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1803 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1806 uint32_t result = 0;
1807 char *save_ptr = NULL;
1810 char *s = xstrdup(s_);
1812 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1813 name = strtok_r(NULL, " |", &save_ptr)) {
1815 unsigned long long int flags;
1818 if (ovs_scan(name, "%lli", &flags)) {
1822 name_len = strlen(name);
1823 for (bit = 1; bit; bit <<= 1) {
1824 const char *fname = bit_to_string(bit);
1831 len = strlen(fname);
1832 if (len != name_len) {
1835 if (!strncmp(name, fname, len)) {
1847 *res = htons(result);
1854 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1856 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1857 *maskp = OVS_BE16_MAX;
1861 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1862 "\"csum\", \"key\")", s);
1866 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1873 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1874 *flagsp = htons(flags);
1875 *maskp = htons(mask);
1878 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1879 *flagsp = htons(flags);
1880 *maskp = OVS_BE16_MAX;
1884 while (*s != '\0') {
1896 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1897 "or '-' (NOT SET)", s);
1901 name_len = strcspn(s,"+-");
1903 for (bit = 1; bit; bit <<= 1) {
1904 const char *fname = packet_tcp_flag_to_string(bit);
1911 len = strlen(fname);
1912 if (len != name_len) {
1915 if (!strncmp(s, fname, len)) {
1917 return xasprintf("%s: Each TCP flag can be specified only "
1929 return xasprintf("%s: unknown TCP flag(s)", s);
1934 *flagsp = htons(flags);
1935 *maskp = htons(mask);
1940 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1941 * NULL if successful, otherwise a malloc()'d string describing the error. */
1943 mf_parse(const struct mf_field *mf, const char *s,
1944 union mf_value *value, union mf_value *mask)
1948 if (!strcmp(s, "*")) {
1949 memset(value, 0, mf->n_bytes);
1950 memset(mask, 0, mf->n_bytes);
1954 switch (mf->string) {
1956 case MFS_HEXADECIMAL:
1957 error = mf_from_integer_string(mf, s,
1958 (uint8_t *) value, (uint8_t *) mask);
1962 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1966 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1970 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1974 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1977 case MFS_OFP_PORT_OXM:
1978 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1982 error = mf_from_frag_string(s, &value->u8, &mask->u8);
1986 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1987 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
1991 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1992 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
1999 if (!error && !mf_is_mask_valid(mf, mask)) {
2000 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2005 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2006 * successful, otherwise a malloc()'d string describing the error. */
2008 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2010 union mf_value mask;
2013 error = mf_parse(mf, s, value, &mask);
2018 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2019 return xasprintf("%s: wildcards not allowed here", s);
2025 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2026 const uint8_t *maskp, struct ds *s)
2028 unsigned long long int integer;
2031 ovs_assert(mf->n_bytes <= 8);
2034 for (i = 0; i < mf->n_bytes; i++) {
2035 integer = (integer << 8) | valuep[i];
2037 if (mf->string == MFS_HEXADECIMAL) {
2038 ds_put_format(s, "%#llx", integer);
2040 ds_put_format(s, "%lld", integer);
2044 unsigned long long int mask;
2047 for (i = 0; i < mf->n_bytes; i++) {
2048 mask = (mask << 8) | maskp[i];
2051 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2052 * not sure that that a bit-mask written in decimal is ever easier to
2053 * understand than the same bit-mask written in hexadecimal. */
2054 ds_put_format(s, "/%#llx", mask);
2059 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2061 const struct frag_handling *h;
2063 mask &= FLOW_NW_FRAG_MASK;
2066 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2067 if (value == h->value && mask == h->mask) {
2068 ds_put_cstr(s, h->name);
2072 ds_put_cstr(s, "<error>");
2076 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2078 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2082 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2084 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2088 /* Appends to 's' a string representation of field 'mf' whose value is in
2089 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2091 mf_format(const struct mf_field *mf,
2092 const union mf_value *value, const union mf_value *mask,
2096 if (is_all_zeros(mask, mf->n_bytes)) {
2097 ds_put_cstr(s, "ANY");
2099 } else if (is_all_ones(mask, mf->n_bytes)) {
2104 switch (mf->string) {
2105 case MFS_OFP_PORT_OXM:
2108 ofputil_port_from_ofp11(value->be32, &port);
2109 ofputil_format_port(port, s);
2115 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2120 case MFS_HEXADECIMAL:
2121 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2125 eth_format_masked(value->mac, mask->mac, s);
2129 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2133 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2137 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2141 mf_format_tnl_flags_string(&value->be16, s);
2145 mf_format_tcp_flags_string(value->be16,
2146 mask ? mask->be16 : OVS_BE16_MAX, s);
2154 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2155 * least-significant bits in 'x'.
2158 mf_write_subfield_flow(const struct mf_subfield *sf,
2159 const union mf_subvalue *x, struct flow *flow)
2161 const struct mf_field *field = sf->field;
2162 union mf_value value;
2164 mf_get_value(field, flow, &value);
2165 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2166 sf->ofs, sf->n_bits);
2167 mf_set_flow_value(field, &value, flow);
2170 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2171 * least-significant bits in 'x'.
2174 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2175 struct match *match)
2177 const struct mf_field *field = sf->field;
2178 union mf_value value, mask;
2180 mf_get(field, match, &value, &mask);
2181 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2182 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2183 mf_set(field, &value, &mask, match);
2186 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2187 * reading 'flow', e.g. as checked by mf_check_src(). */
2189 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2190 union mf_subvalue *x)
2192 union mf_value value;
2194 mf_get_value(sf->field, flow, &value);
2196 memset(x, 0, sizeof *x);
2197 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2202 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2203 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2206 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2208 union mf_value value;
2210 mf_get_value(sf->field, flow, &value);
2211 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2215 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2219 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2220 if (subvalue->u8[i]) {
2221 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2222 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2223 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2228 ds_put_char(s, '0');