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"
38 #include "openvswitch/vlog.h"
40 VLOG_DEFINE_THIS_MODULE(meta_flow);
42 #define FLOW_U32OFS(FIELD) \
43 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
45 #define MF_FIELD_SIZES(MEMBER) \
46 sizeof ((union mf_value *)0)->MEMBER, \
47 8 * sizeof ((union mf_value *)0)->MEMBER
49 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
51 const struct mf_field mf_fields[MFF_N_IDS] = {
52 #include "meta-flow.inc"
55 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
56 static struct shash mf_by_name;
58 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
59 * controller and so there's not much point in showing a lot of them. */
60 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
62 static void nxm_init(void);
64 /* Returns the field with the given 'name', or a null pointer if no field has
66 const struct mf_field *
67 mf_from_name(const char *name)
70 return shash_find_data(&mf_by_name, name);
78 shash_init(&mf_by_name);
79 for (i = 0; i < MFF_N_IDS; i++) {
80 const struct mf_field *mf = &mf_fields[i];
82 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
84 shash_add_once(&mf_by_name, mf->name, mf);
86 shash_add_once(&mf_by_name, mf->extra_name, mf);
94 static pthread_once_t once = PTHREAD_ONCE_INIT;
95 pthread_once(&once, nxm_do_init);
98 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
99 * specifies at least one bit in the field.
101 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
102 * meets 'mf''s prerequisites. */
104 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
108 return !wc->masks.dp_hash;
110 return !wc->masks.recirc_id;
112 return !wc->masks.conj_id;
114 return !wc->masks.tunnel.ip_src;
116 return !wc->masks.tunnel.ip_dst;
121 return !wc->masks.tunnel.tun_id;
123 return !wc->masks.metadata;
125 case MFF_IN_PORT_OXM:
126 return !wc->masks.in_port.ofp_port;
127 case MFF_SKB_PRIORITY:
128 return !wc->masks.skb_priority;
130 return !wc->masks.pkt_mark;
132 return !wc->masks.regs[mf->id - MFF_REG0];
134 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
135 case MFF_ACTSET_OUTPUT:
136 return !wc->masks.actset_output;
139 return eth_addr_is_zero(wc->masks.dl_src);
141 return eth_addr_is_zero(wc->masks.dl_dst);
143 return !wc->masks.dl_type;
147 return eth_addr_is_zero(wc->masks.arp_sha);
151 return eth_addr_is_zero(wc->masks.arp_tha);
154 return !wc->masks.vlan_tci;
156 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
158 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
159 case MFF_DL_VLAN_PCP:
161 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
164 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
166 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
168 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
171 return !wc->masks.nw_src;
173 return !wc->masks.nw_dst;
176 return ipv6_mask_is_any(&wc->masks.ipv6_src);
178 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
181 return !wc->masks.ipv6_label;
184 return !wc->masks.nw_proto;
186 case MFF_IP_DSCP_SHIFTED:
187 return !(wc->masks.nw_tos & IP_DSCP_MASK);
189 return !(wc->masks.nw_tos & IP_ECN_MASK);
191 return !wc->masks.nw_ttl;
194 return ipv6_mask_is_any(&wc->masks.nd_target);
197 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
200 return !wc->masks.nw_proto;
202 return !wc->masks.nw_src;
204 return !wc->masks.nw_dst;
209 case MFF_ICMPV4_TYPE:
210 case MFF_ICMPV6_TYPE:
211 return !wc->masks.tp_src;
215 case MFF_ICMPV4_CODE:
216 case MFF_ICMPV6_CODE:
217 return !wc->masks.tp_dst;
219 return !wc->masks.tcp_flags;
227 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
228 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
229 * purposes, or to 0 if it is wildcarded.
231 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
232 * meets 'mf''s prerequisites. */
234 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
235 union mf_value *mask)
237 mf_get_value(mf, &wc->masks, mask);
240 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
241 * if the mask is valid, false otherwise. */
243 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
245 switch (mf->maskable) {
247 return (is_all_zeros(mask, mf->n_bytes) ||
248 is_all_ones(mask, mf->n_bytes));
257 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
259 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
261 switch (mf->prereqs) {
266 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
267 flow->dl_type == htons(ETH_TYPE_RARP));
269 return flow->dl_type == htons(ETH_TYPE_IP);
271 return flow->dl_type == htons(ETH_TYPE_IPV6);
273 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
275 return eth_type_mpls(flow->dl_type);
277 return is_ip_any(flow);
280 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
281 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
283 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
284 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
286 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
287 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
289 return is_icmpv4(flow);
291 return is_icmpv6(flow);
294 return (is_icmpv6(flow)
295 && flow->tp_dst == htons(0)
296 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
297 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
299 return (is_icmpv6(flow)
300 && flow->tp_dst == htons(0)
301 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
303 return (is_icmpv6(flow)
304 && flow->tp_dst == htons(0)
305 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
311 /* Set field and it's prerequisities in the mask.
312 * This is only ever called for writeable 'mf's, but we do not make the
313 * distinction here. */
315 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
317 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
319 mf_set_flow_value(mf, &exact_match_mask, mask);
321 switch (mf->prereqs) {
325 mask->tp_src = OVS_BE16_MAX;
326 mask->tp_dst = OVS_BE16_MAX;
333 /* nw_frag always unwildcarded. */
334 mask->nw_proto = 0xff;
341 mask->dl_type = OVS_BE16_MAX;
344 mask->vlan_tci |= htons(VLAN_CFI);
352 /* Returns true if 'value' may be a valid value *as part of a masked match*,
355 * A value is not rejected just because it is not valid for the field in
356 * question, but only if it doesn't make sense to test the bits in question at
357 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
358 * without the VLAN_CFI bit being set, but we can't reject those values because
359 * it is still legitimate to test just for those bits (see the documentation
360 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
361 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
363 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
377 case MFF_SKB_PRIORITY:
401 case MFF_ICMPV4_TYPE:
402 case MFF_ICMPV4_CODE:
403 case MFF_ICMPV6_TYPE:
404 case MFF_ICMPV6_CODE:
410 case MFF_IN_PORT_OXM:
411 case MFF_ACTSET_OUTPUT: {
413 return !ofputil_port_from_ofp11(value->be32, &port);
417 return !(value->u8 & ~IP_DSCP_MASK);
418 case MFF_IP_DSCP_SHIFTED:
419 return !(value->u8 & (~IP_DSCP_MASK >> 2));
421 return !(value->u8 & ~IP_ECN_MASK);
423 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
425 return !(value->be16 & ~htons(0x0fff));
428 return !(value->be16 & htons(0xff00));
431 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
433 return !(value->be16 & htons(VLAN_PCP_MASK));
435 case MFF_DL_VLAN_PCP:
437 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
440 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
443 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
446 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
449 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
457 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
458 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
460 mf_get_value(const struct mf_field *mf, const struct flow *flow,
461 union mf_value *value)
465 value->be32 = htonl(flow->dp_hash);
468 value->be32 = htonl(flow->recirc_id);
471 value->be32 = htonl(flow->conj_id);
474 value->be64 = flow->tunnel.tun_id;
477 value->be32 = flow->tunnel.ip_src;
480 value->be32 = flow->tunnel.ip_dst;
483 value->be16 = htons(flow->tunnel.flags);
486 value->u8 = flow->tunnel.ip_ttl;
489 value->u8 = flow->tunnel.ip_tos;
493 value->be64 = flow->metadata;
497 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
499 case MFF_IN_PORT_OXM:
500 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
502 case MFF_ACTSET_OUTPUT:
503 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
506 case MFF_SKB_PRIORITY:
507 value->be32 = htonl(flow->skb_priority);
511 value->be32 = htonl(flow->pkt_mark);
515 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
519 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
523 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
527 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
531 value->be16 = flow->dl_type;
535 value->be16 = flow->vlan_tci;
539 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
542 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
545 case MFF_DL_VLAN_PCP:
547 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
551 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
555 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
559 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
563 value->be32 = flow->nw_src;
567 value->be32 = flow->nw_dst;
571 value->ipv6 = flow->ipv6_src;
575 value->ipv6 = flow->ipv6_dst;
579 value->be32 = flow->ipv6_label;
583 value->u8 = flow->nw_proto;
587 value->u8 = flow->nw_tos & IP_DSCP_MASK;
590 case MFF_IP_DSCP_SHIFTED:
591 value->u8 = flow->nw_tos >> 2;
595 value->u8 = flow->nw_tos & IP_ECN_MASK;
599 value->u8 = flow->nw_ttl;
603 value->u8 = flow->nw_frag;
607 value->be16 = htons(flow->nw_proto);
611 value->be32 = flow->nw_src;
615 value->be32 = flow->nw_dst;
620 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
625 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
631 value->be16 = flow->tp_src;
637 value->be16 = flow->tp_dst;
641 value->be16 = flow->tcp_flags;
644 case MFF_ICMPV4_TYPE:
645 case MFF_ICMPV6_TYPE:
646 value->u8 = ntohs(flow->tp_src);
649 case MFF_ICMPV4_CODE:
650 case MFF_ICMPV6_CODE:
651 value->u8 = ntohs(flow->tp_dst);
655 value->ipv6 = flow->nd_target;
664 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
665 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
668 mf_set_value(const struct mf_field *mf,
669 const union mf_value *value, struct match *match)
673 match_set_dp_hash(match, ntohl(value->be32));
676 match_set_recirc_id(match, ntohl(value->be32));
679 match_set_conj_id(match, ntohl(value->be32));
682 match_set_tun_id(match, value->be64);
685 match_set_tun_src(match, value->be32);
688 match_set_tun_dst(match, value->be32);
691 match_set_tun_flags(match, ntohs(value->be16));
694 match_set_tun_tos(match, value->u8);
697 match_set_tun_ttl(match, value->u8);
701 match_set_metadata(match, value->be64);
705 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
708 case MFF_IN_PORT_OXM: {
710 ofputil_port_from_ofp11(value->be32, &port);
711 match_set_in_port(match, port);
714 case MFF_ACTSET_OUTPUT: {
716 ofputil_port_from_ofp11(value->be32, &port);
717 match_set_actset_output(match, port);
721 case MFF_SKB_PRIORITY:
722 match_set_skb_priority(match, ntohl(value->be32));
726 match_set_pkt_mark(match, ntohl(value->be32));
730 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
734 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
738 match_set_dl_src(match, value->mac);
742 match_set_dl_dst(match, value->mac);
746 match_set_dl_type(match, value->be16);
750 match_set_dl_tci(match, value->be16);
754 match_set_dl_vlan(match, value->be16);
757 match_set_vlan_vid(match, value->be16);
760 case MFF_DL_VLAN_PCP:
762 match_set_dl_vlan_pcp(match, value->u8);
766 match_set_mpls_label(match, 0, value->be32);
770 match_set_mpls_tc(match, 0, value->u8);
774 match_set_mpls_bos(match, 0, value->u8);
778 match_set_nw_src(match, value->be32);
782 match_set_nw_dst(match, value->be32);
786 match_set_ipv6_src(match, &value->ipv6);
790 match_set_ipv6_dst(match, &value->ipv6);
794 match_set_ipv6_label(match, value->be32);
798 match_set_nw_proto(match, value->u8);
802 match_set_nw_dscp(match, value->u8);
805 case MFF_IP_DSCP_SHIFTED:
806 match_set_nw_dscp(match, value->u8 << 2);
810 match_set_nw_ecn(match, value->u8);
814 match_set_nw_ttl(match, value->u8);
818 match_set_nw_frag(match, value->u8);
822 match_set_nw_proto(match, ntohs(value->be16));
826 match_set_nw_src(match, value->be32);
830 match_set_nw_dst(match, value->be32);
835 match_set_arp_sha(match, value->mac);
840 match_set_arp_tha(match, value->mac);
846 match_set_tp_src(match, value->be16);
852 match_set_tp_dst(match, value->be16);
856 match_set_tcp_flags(match, value->be16);
859 case MFF_ICMPV4_TYPE:
860 case MFF_ICMPV6_TYPE:
861 match_set_icmp_type(match, value->u8);
864 case MFF_ICMPV4_CODE:
865 case MFF_ICMPV6_CODE:
866 match_set_icmp_code(match, value->u8);
870 match_set_nd_target(match, &value->ipv6);
879 /* Unwildcard 'mask' member field described by 'mf'. The caller is
880 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
882 mf_mask_field(const struct mf_field *mf, struct flow *mask)
884 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
886 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
887 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
888 * special case. For the rest, calling mf_set_flow_value() is good
890 if (mf->id == MFF_DL_VLAN) {
891 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
893 mf_set_flow_value(mf, &exact_match_mask, mask);
897 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
898 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
900 mf_set_flow_value(const struct mf_field *mf,
901 const union mf_value *value, struct flow *flow)
905 flow->dp_hash = ntohl(value->be32);
908 flow->recirc_id = ntohl(value->be32);
911 flow->conj_id = ntohl(value->be32);
914 flow->tunnel.tun_id = value->be64;
917 flow->tunnel.ip_src = value->be32;
920 flow->tunnel.ip_dst = value->be32;
923 flow->tunnel.flags = ntohs(value->be16);
926 flow->tunnel.ip_tos = value->u8;
929 flow->tunnel.ip_ttl = value->u8;
933 flow->metadata = value->be64;
937 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
940 case MFF_IN_PORT_OXM:
941 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
943 case MFF_ACTSET_OUTPUT:
944 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
947 case MFF_SKB_PRIORITY:
948 flow->skb_priority = ntohl(value->be32);
952 flow->pkt_mark = ntohl(value->be32);
956 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
960 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
964 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
968 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
972 flow->dl_type = value->be16;
976 flow->vlan_tci = value->be16;
980 flow_set_dl_vlan(flow, value->be16);
983 flow_set_vlan_vid(flow, value->be16);
986 case MFF_DL_VLAN_PCP:
988 flow_set_vlan_pcp(flow, value->u8);
992 flow_set_mpls_label(flow, 0, value->be32);
996 flow_set_mpls_tc(flow, 0, value->u8);
1000 flow_set_mpls_bos(flow, 0, value->u8);
1004 flow->nw_src = value->be32;
1008 flow->nw_dst = value->be32;
1012 flow->ipv6_src = value->ipv6;
1016 flow->ipv6_dst = value->ipv6;
1019 case MFF_IPV6_LABEL:
1020 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1024 flow->nw_proto = value->u8;
1028 flow->nw_tos &= ~IP_DSCP_MASK;
1029 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1032 case MFF_IP_DSCP_SHIFTED:
1033 flow->nw_tos &= ~IP_DSCP_MASK;
1034 flow->nw_tos |= value->u8 << 2;
1038 flow->nw_tos &= ~IP_ECN_MASK;
1039 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1043 flow->nw_ttl = value->u8;
1047 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1051 flow->nw_proto = ntohs(value->be16);
1055 flow->nw_src = value->be32;
1059 flow->nw_dst = value->be32;
1064 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1069 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1075 flow->tp_src = value->be16;
1081 flow->tp_dst = value->be16;
1085 flow->tcp_flags = value->be16;
1088 case MFF_ICMPV4_TYPE:
1089 case MFF_ICMPV6_TYPE:
1090 flow->tp_src = htons(value->u8);
1093 case MFF_ICMPV4_CODE:
1094 case MFF_ICMPV6_CODE:
1095 flow->tp_dst = htons(value->u8);
1099 flow->nd_target = value->ipv6;
1108 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1109 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1112 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1116 for (i = 0; i < n; i++) {
1117 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1121 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1122 * for which 'mask' has a 0-bit keep their existing values. The caller is
1123 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1125 mf_set_flow_value_masked(const struct mf_field *field,
1126 const union mf_value *value,
1127 const union mf_value *mask,
1132 mf_get_value(field, flow, &tmp);
1133 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1134 (uint8_t *) &tmp, field->n_bytes);
1135 mf_set_flow_value(field, &tmp, flow);
1138 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1140 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1143 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1145 union mf_value value;
1147 mf_get_value(mf, flow, &value);
1148 return is_all_zeros(&value, mf->n_bytes);
1151 /* Makes 'match' wildcard field 'mf'.
1153 * The caller is responsible for ensuring that 'match' meets 'mf''s
1156 mf_set_wild(const struct mf_field *mf, struct match *match)
1160 match->flow.dp_hash = 0;
1161 match->wc.masks.dp_hash = 0;
1164 match->flow.recirc_id = 0;
1165 match->wc.masks.recirc_id = 0;
1168 match->flow.conj_id = 0;
1169 match->wc.masks.conj_id = 0;
1172 match_set_tun_id_masked(match, htonll(0), htonll(0));
1175 match_set_tun_src_masked(match, htonl(0), htonl(0));
1178 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1181 match_set_tun_flags_masked(match, 0, 0);
1184 match_set_tun_tos_masked(match, 0, 0);
1187 match_set_tun_ttl_masked(match, 0, 0);
1191 match_set_metadata_masked(match, htonll(0), htonll(0));
1195 case MFF_IN_PORT_OXM:
1196 match->flow.in_port.ofp_port = 0;
1197 match->wc.masks.in_port.ofp_port = 0;
1199 case MFF_ACTSET_OUTPUT:
1200 match->flow.actset_output = 0;
1201 match->wc.masks.actset_output = 0;
1204 case MFF_SKB_PRIORITY:
1205 match->flow.skb_priority = 0;
1206 match->wc.masks.skb_priority = 0;
1210 match->flow.pkt_mark = 0;
1211 match->wc.masks.pkt_mark = 0;
1215 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1219 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1223 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1224 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1228 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1229 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1233 match->flow.dl_type = htons(0);
1234 match->wc.masks.dl_type = htons(0);
1238 match_set_dl_tci_masked(match, htons(0), htons(0));
1243 match_set_any_vid(match);
1246 case MFF_DL_VLAN_PCP:
1248 match_set_any_pcp(match);
1251 case MFF_MPLS_LABEL:
1252 match_set_any_mpls_label(match, 0);
1256 match_set_any_mpls_tc(match, 0);
1260 match_set_any_mpls_bos(match, 0);
1265 match_set_nw_src_masked(match, htonl(0), htonl(0));
1270 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1274 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1275 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1279 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1280 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1283 case MFF_IPV6_LABEL:
1284 match->wc.masks.ipv6_label = htonl(0);
1285 match->flow.ipv6_label = htonl(0);
1289 match->wc.masks.nw_proto = 0;
1290 match->flow.nw_proto = 0;
1294 case MFF_IP_DSCP_SHIFTED:
1295 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1296 match->flow.nw_tos &= ~IP_DSCP_MASK;
1300 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1301 match->flow.nw_tos &= ~IP_ECN_MASK;
1305 match->wc.masks.nw_ttl = 0;
1306 match->flow.nw_ttl = 0;
1310 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1311 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1315 match->wc.masks.nw_proto = 0;
1316 match->flow.nw_proto = 0;
1321 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1322 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1327 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1328 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1334 case MFF_ICMPV4_TYPE:
1335 case MFF_ICMPV6_TYPE:
1336 match->wc.masks.tp_src = htons(0);
1337 match->flow.tp_src = htons(0);
1343 case MFF_ICMPV4_CODE:
1344 case MFF_ICMPV6_CODE:
1345 match->wc.masks.tp_dst = htons(0);
1346 match->flow.tp_dst = htons(0);
1350 match->wc.masks.tcp_flags = htons(0);
1351 match->flow.tcp_flags = htons(0);
1355 memset(&match->wc.masks.nd_target, 0,
1356 sizeof match->wc.masks.nd_target);
1357 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1366 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1367 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1368 * with a 1-bit indicating that the corresponding value bit must match and a
1369 * 0-bit indicating a don't-care.
1371 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1372 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1373 * call is equivalent to mf_set_wild(mf, match).
1375 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1376 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1377 enum ofputil_protocol
1378 mf_set(const struct mf_field *mf,
1379 const union mf_value *value, const union mf_value *mask,
1380 struct match *match)
1382 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1383 mf_set_value(mf, value, match);
1384 return mf->usable_protocols_exact;
1385 } else if (is_all_zeros(mask, mf->n_bytes)) {
1386 mf_set_wild(mf, match);
1387 return OFPUTIL_P_ANY;
1394 case MFF_IN_PORT_OXM:
1395 case MFF_ACTSET_OUTPUT:
1396 case MFF_SKB_PRIORITY:
1399 case MFF_DL_VLAN_PCP:
1401 case MFF_MPLS_LABEL:
1407 case MFF_IP_DSCP_SHIFTED:
1410 case MFF_ICMPV4_TYPE:
1411 case MFF_ICMPV4_CODE:
1412 case MFF_ICMPV6_TYPE:
1413 case MFF_ICMPV6_CODE:
1414 return OFPUTIL_P_NONE;
1417 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1420 match_set_tun_id_masked(match, value->be64, mask->be64);
1423 match_set_tun_src_masked(match, value->be32, mask->be32);
1426 match_set_tun_dst_masked(match, value->be32, mask->be32);
1429 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1432 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1435 match_set_tun_tos_masked(match, value->u8, mask->u8);
1439 match_set_metadata_masked(match, value->be64, mask->be64);
1443 match_set_reg_masked(match, mf->id - MFF_REG0,
1444 ntohl(value->be32), ntohl(mask->be32));
1448 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1449 ntohll(value->be64), ntohll(mask->be64));
1453 match_set_pkt_mark_masked(match, ntohl(value->be32),
1458 match_set_dl_dst_masked(match, value->mac, mask->mac);
1462 match_set_dl_src_masked(match, value->mac, mask->mac);
1467 match_set_arp_sha_masked(match, value->mac, mask->mac);
1472 match_set_arp_tha_masked(match, value->mac, mask->mac);
1476 match_set_dl_tci_masked(match, value->be16, mask->be16);
1480 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1484 match_set_nw_src_masked(match, value->be32, mask->be32);
1488 match_set_nw_dst_masked(match, value->be32, mask->be32);
1492 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1496 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1499 case MFF_IPV6_LABEL:
1500 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1501 mf_set_value(mf, value, match);
1503 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1508 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1512 match_set_nw_frag_masked(match, value->u8, mask->u8);
1516 match_set_nw_src_masked(match, value->be32, mask->be32);
1520 match_set_nw_dst_masked(match, value->be32, mask->be32);
1526 match_set_tp_src_masked(match, value->be16, mask->be16);
1532 match_set_tp_dst_masked(match, value->be16, mask->be16);
1536 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1544 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1545 || ip_is_cidr(mask->be32))
1546 ? mf->usable_protocols_cidr
1547 : mf->usable_protocols_bitwise);
1551 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1555 VLOG_WARN_RL(&rl, "unknown %s field", type);
1556 return OFPERR_OFPBAC_BAD_SET_TYPE;
1557 } else if (!sf->n_bits) {
1558 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1559 return OFPERR_OFPBAC_BAD_SET_LEN;
1560 } else if (sf->ofs >= sf->field->n_bits) {
1561 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1562 sf->ofs, sf->field->n_bits, type, sf->field->name);
1563 return OFPERR_OFPBAC_BAD_SET_LEN;
1564 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1565 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1566 "of %s field %s", sf->ofs, sf->n_bits,
1567 sf->field->n_bits, type, sf->field->name);
1568 return OFPERR_OFPBAC_BAD_SET_LEN;
1569 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1570 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1571 type, sf->field->name);
1572 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1578 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1579 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1582 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1584 return mf_check__(sf, flow, "source");
1587 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1588 * if so, otherwise an OpenFlow error code (e.g. as returned by
1591 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1593 int error = mf_check__(sf, flow, "destination");
1594 if (!error && !sf->field->writable) {
1595 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1597 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1602 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1603 * 'value' and 'mask', respectively. */
1605 mf_get(const struct mf_field *mf, const struct match *match,
1606 union mf_value *value, union mf_value *mask)
1608 mf_get_value(mf, &match->flow, value);
1609 mf_get_mask(mf, &match->wc, mask);
1613 mf_from_integer_string(const struct mf_field *mf, const char *s,
1614 uint8_t *valuep, uint8_t *maskp)
1616 unsigned long long int integer, mask;
1621 integer = strtoull(s, &tail, 0);
1622 if (errno || (*tail != '\0' && *tail != '/')) {
1627 mask = strtoull(tail + 1, &tail, 0);
1628 if (errno || *tail != '\0') {
1635 for (i = mf->n_bytes - 1; i >= 0; i--) {
1636 valuep[i] = integer;
1642 return xasprintf("%s: value too large for %u-byte field %s",
1643 s, mf->n_bytes, mf->name);
1648 return xasprintf("%s: bad syntax for %s", s, mf->name);
1652 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1653 uint8_t mac[ETH_ADDR_LEN],
1654 uint8_t mask[ETH_ADDR_LEN])
1658 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1661 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1662 && n == strlen(s)) {
1663 memset(mask, 0xff, ETH_ADDR_LEN);
1668 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1669 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1670 && n == strlen(s)) {
1674 return xasprintf("%s: invalid Ethernet address", s);
1678 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1679 ovs_be32 *ip, ovs_be32 *mask)
1683 ovs_assert(mf->n_bytes == sizeof *ip);
1685 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1686 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1688 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1689 if (prefix <= 0 || prefix > 32) {
1690 return xasprintf("%s: network prefix bits not between 0 and "
1693 *mask = be32_prefix_mask(prefix);
1694 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1695 *mask = OVS_BE32_MAX;
1697 return xasprintf("%s: invalid IP address", s);
1703 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1704 struct in6_addr *value, struct in6_addr *mask)
1706 char *str = xstrdup(s);
1707 char *save_ptr = NULL;
1708 const char *name, *netmask;
1711 ovs_assert(mf->n_bytes == sizeof *value);
1713 name = strtok_r(str, "/", &save_ptr);
1714 retval = name ? lookup_ipv6(name, value) : EINVAL;
1718 err = xasprintf("%s: could not convert to IPv6 address", str);
1724 netmask = strtok_r(NULL, "/", &save_ptr);
1726 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1727 int prefix = atoi(netmask);
1728 if (prefix <= 0 || prefix > 128) {
1730 return xasprintf("%s: prefix bits not between 1 and 128", s);
1732 *mask = ipv6_create_mask(prefix);
1736 *mask = in6addr_exact;
1744 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1745 ovs_be16 *valuep, ovs_be16 *maskp)
1749 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1751 if (ofputil_port_from_string(s, &port)) {
1752 *valuep = htons(ofp_to_u16(port));
1753 *maskp = OVS_BE16_MAX;
1756 return xasprintf("%s: port value out of range for %s", s, mf->name);
1760 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1761 ovs_be32 *valuep, ovs_be32 *maskp)
1765 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1766 if (ofputil_port_from_string(s, &port)) {
1767 *valuep = ofputil_port_to_ofp11(port);
1768 *maskp = OVS_BE32_MAX;
1771 return xasprintf("%s: port value out of range for %s", s, mf->name);
1774 struct frag_handling {
1780 static const struct frag_handling all_frags[] = {
1781 #define A FLOW_NW_FRAG_ANY
1782 #define L FLOW_NW_FRAG_LATER
1783 /* name mask value */
1786 { "first", A|L, A },
1787 { "later", A|L, A|L },
1792 { "not_later", L, 0 },
1799 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1801 const struct frag_handling *h;
1803 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1804 if (!strcasecmp(s, h->name)) {
1805 /* We force the upper bits of the mask on to make mf_parse_value()
1806 * happy (otherwise it will never think it's an exact match.) */
1807 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1813 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1814 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1818 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1821 uint32_t result = 0;
1822 char *save_ptr = NULL;
1825 char *s = xstrdup(s_);
1827 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1828 name = strtok_r(NULL, " |", &save_ptr)) {
1830 unsigned long long int flags;
1833 if (ovs_scan(name, "%lli", &flags)) {
1837 name_len = strlen(name);
1838 for (bit = 1; bit; bit <<= 1) {
1839 const char *fname = bit_to_string(bit);
1846 len = strlen(fname);
1847 if (len != name_len) {
1850 if (!strncmp(name, fname, len)) {
1862 *res = htons(result);
1869 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1871 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1872 *maskp = OVS_BE16_MAX;
1876 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1877 "\"csum\", \"key\")", s);
1881 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1888 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1889 *flagsp = htons(flags);
1890 *maskp = htons(mask);
1893 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1894 *flagsp = htons(flags);
1895 *maskp = OVS_BE16_MAX;
1899 while (*s != '\0') {
1911 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1912 "or '-' (NOT SET)", s);
1916 name_len = strcspn(s,"+-");
1918 for (bit = 1; bit; bit <<= 1) {
1919 const char *fname = packet_tcp_flag_to_string(bit);
1926 len = strlen(fname);
1927 if (len != name_len) {
1930 if (!strncmp(s, fname, len)) {
1932 return xasprintf("%s: Each TCP flag can be specified only "
1944 return xasprintf("%s: unknown TCP flag(s)", s);
1949 *flagsp = htons(flags);
1950 *maskp = htons(mask);
1955 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1956 * NULL if successful, otherwise a malloc()'d string describing the error. */
1958 mf_parse(const struct mf_field *mf, const char *s,
1959 union mf_value *value, union mf_value *mask)
1963 if (!strcmp(s, "*")) {
1964 memset(value, 0, mf->n_bytes);
1965 memset(mask, 0, mf->n_bytes);
1969 switch (mf->string) {
1971 case MFS_HEXADECIMAL:
1972 error = mf_from_integer_string(mf, s,
1973 (uint8_t *) value, (uint8_t *) mask);
1977 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1981 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1985 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1989 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1992 case MFS_OFP_PORT_OXM:
1993 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1997 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2001 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2002 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2006 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2007 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2014 if (!error && !mf_is_mask_valid(mf, mask)) {
2015 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2020 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2021 * successful, otherwise a malloc()'d string describing the error. */
2023 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2025 union mf_value mask;
2028 error = mf_parse(mf, s, value, &mask);
2033 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2034 return xasprintf("%s: wildcards not allowed here", s);
2040 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2041 const uint8_t *maskp, struct ds *s)
2043 unsigned long long int integer;
2046 ovs_assert(mf->n_bytes <= 8);
2049 for (i = 0; i < mf->n_bytes; i++) {
2050 integer = (integer << 8) | valuep[i];
2052 if (mf->string == MFS_HEXADECIMAL) {
2053 ds_put_format(s, "%#llx", integer);
2055 ds_put_format(s, "%lld", integer);
2059 unsigned long long int mask;
2062 for (i = 0; i < mf->n_bytes; i++) {
2063 mask = (mask << 8) | maskp[i];
2066 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2067 * not sure that that a bit-mask written in decimal is ever easier to
2068 * understand than the same bit-mask written in hexadecimal. */
2069 ds_put_format(s, "/%#llx", mask);
2074 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2076 const struct frag_handling *h;
2078 mask &= FLOW_NW_FRAG_MASK;
2081 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2082 if (value == h->value && mask == h->mask) {
2083 ds_put_cstr(s, h->name);
2087 ds_put_cstr(s, "<error>");
2091 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2093 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2097 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2099 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2103 /* Appends to 's' a string representation of field 'mf' whose value is in
2104 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2106 mf_format(const struct mf_field *mf,
2107 const union mf_value *value, const union mf_value *mask,
2111 if (is_all_zeros(mask, mf->n_bytes)) {
2112 ds_put_cstr(s, "ANY");
2114 } else if (is_all_ones(mask, mf->n_bytes)) {
2119 switch (mf->string) {
2120 case MFS_OFP_PORT_OXM:
2123 ofputil_port_from_ofp11(value->be32, &port);
2124 ofputil_format_port(port, s);
2130 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2135 case MFS_HEXADECIMAL:
2136 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2140 eth_format_masked(value->mac, mask->mac, s);
2144 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2148 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2152 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2156 mf_format_tnl_flags_string(&value->be16, s);
2160 mf_format_tcp_flags_string(value->be16,
2161 mask ? mask->be16 : OVS_BE16_MAX, s);
2169 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2170 * least-significant bits in 'x'.
2173 mf_write_subfield_flow(const struct mf_subfield *sf,
2174 const union mf_subvalue *x, struct flow *flow)
2176 const struct mf_field *field = sf->field;
2177 union mf_value value;
2179 mf_get_value(field, flow, &value);
2180 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2181 sf->ofs, sf->n_bits);
2182 mf_set_flow_value(field, &value, flow);
2185 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2186 * least-significant bits in 'x'.
2189 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2190 struct match *match)
2192 const struct mf_field *field = sf->field;
2193 union mf_value value, mask;
2195 mf_get(field, match, &value, &mask);
2196 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2197 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2198 mf_set(field, &value, &mask, match);
2201 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2202 * reading 'flow', e.g. as checked by mf_check_src(). */
2204 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2205 union mf_subvalue *x)
2207 union mf_value value;
2209 mf_get_value(sf->field, flow, &value);
2211 memset(x, 0, sizeof *x);
2212 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2217 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2218 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2221 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2223 union mf_value value;
2225 mf_get_value(sf->field, flow, &value);
2226 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2230 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2234 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2235 if (subvalue->u8[i]) {
2236 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2237 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2238 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2243 ds_put_char(s, '0');