2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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 "openvswitch/meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/ofp-util.h"
30 #include "ovs-thread.h"
34 #include "socket-util.h"
35 #include "tun-metadata.h"
36 #include "unaligned.h"
38 #include "openvswitch/ofp-errors.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
59 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
60 * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
70 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
72 static void nxm_init(void);
74 /* Returns the field with the given 'name', or a null pointer if no field has
76 const struct mf_field *
77 mf_from_name(const char *name)
80 return shash_find_data(&mf_by_name, name);
88 shash_init(&mf_by_name);
89 for (i = 0; i < MFF_N_IDS; i++) {
90 const struct mf_field *mf = &mf_fields[i];
92 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
94 shash_add_once(&mf_by_name, mf->name, mf);
96 shash_add_once(&mf_by_name, mf->extra_name, mf);
104 static pthread_once_t once = PTHREAD_ONCE_INIT;
105 pthread_once(&once, nxm_do_init);
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109 * restrictions on a field's value. Then, this function initializes
110 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111 * This is not always possible, i.e. if one pair insists on a value of 0 in
112 * some bit and the other pair insists on a value of 1 in that bit. This
113 * function returns false in a case where the combined restriction is
114 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
117 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118 * a corresponding 1-bit in its mask.) */
120 mf_subvalue_intersect(const union mf_subvalue *a_value,
121 const union mf_subvalue *a_mask,
122 const union mf_subvalue *b_value,
123 const union mf_subvalue *b_mask,
124 union mf_subvalue *dst_value,
125 union mf_subvalue *dst_mask)
127 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
128 ovs_be64 av = a_value->be64[i];
129 ovs_be64 am = a_mask->be64[i];
130 ovs_be64 bv = b_value->be64[i];
131 ovs_be64 bm = b_mask->be64[i];
132 ovs_be64 *dv = &dst_value->be64[i];
133 ovs_be64 *dm = &dst_mask->be64[i];
135 if ((av ^ bv) & (am & bm)) {
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145 * set, 2 if the second-lowest-order bit is set, and so on. */
147 mf_subvalue_width(const union mf_subvalue *v)
149 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153 * negative 'n', shifts the bits '-n' bits to the right. */
155 mf_subvalue_shift(union mf_subvalue *value, int n)
158 union mf_subvalue tmp;
159 memset(&tmp, 0, sizeof tmp);
161 if (n > 0 && n < 8 * sizeof tmp) {
162 bitwise_copy(value, sizeof *value, 0,
165 } else if (n < 0 && n > -8 * sizeof tmp) {
166 bitwise_copy(value, sizeof *value, -n,
174 /* Appends a formatted representation of 'sv' to 's'. */
176 mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
178 ds_put_hex(s, sv, sizeof *sv);
181 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
182 * specifies at least one bit in the field.
184 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
185 * meets 'mf''s prerequisites. */
187 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
191 return !wc->masks.dp_hash;
193 return !wc->masks.recirc_id;
195 return !wc->masks.conj_id;
197 return !wc->masks.tunnel.ip_src;
199 return !wc->masks.tunnel.ip_dst;
200 case MFF_TUN_IPV6_SRC:
201 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
202 case MFF_TUN_IPV6_DST:
203 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
205 return !wc->masks.tunnel.tun_id;
207 return !wc->masks.tunnel.ip_tos;
209 return !wc->masks.tunnel.ip_ttl;
211 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
213 return !wc->masks.tunnel.gbp_id;
214 case MFF_TUN_GBP_FLAGS:
215 return !wc->masks.tunnel.gbp_flags;
216 CASE_MFF_TUN_METADATA:
217 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
218 mf->id - MFF_TUN_METADATA0);
220 return !wc->masks.metadata;
222 case MFF_IN_PORT_OXM:
223 return !wc->masks.in_port.ofp_port;
224 case MFF_SKB_PRIORITY:
225 return !wc->masks.skb_priority;
227 return !wc->masks.pkt_mark;
229 return !wc->masks.ct_state;
231 return !wc->masks.ct_zone;
233 return !wc->masks.ct_mark;
235 return ovs_u128_is_zero(wc->masks.ct_label);
237 return !wc->masks.regs[mf->id - MFF_REG0];
239 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
240 case MFF_ACTSET_OUTPUT:
241 return !wc->masks.actset_output;
244 return eth_addr_is_zero(wc->masks.dl_src);
246 return eth_addr_is_zero(wc->masks.dl_dst);
248 return !wc->masks.dl_type;
252 return eth_addr_is_zero(wc->masks.arp_sha);
256 return eth_addr_is_zero(wc->masks.arp_tha);
259 return !wc->masks.vlan_tci;
261 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
263 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
264 case MFF_DL_VLAN_PCP:
266 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
269 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
271 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
273 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
275 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
278 return !wc->masks.nw_src;
280 return !wc->masks.nw_dst;
283 return ipv6_mask_is_any(&wc->masks.ipv6_src);
285 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
288 return !wc->masks.ipv6_label;
291 return !wc->masks.nw_proto;
293 case MFF_IP_DSCP_SHIFTED:
294 return !(wc->masks.nw_tos & IP_DSCP_MASK);
296 return !(wc->masks.nw_tos & IP_ECN_MASK);
298 return !wc->masks.nw_ttl;
301 return ipv6_mask_is_any(&wc->masks.nd_target);
304 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
307 return !wc->masks.nw_proto;
309 return !wc->masks.nw_src;
311 return !wc->masks.nw_dst;
316 case MFF_ICMPV4_TYPE:
317 case MFF_ICMPV6_TYPE:
318 return !wc->masks.tp_src;
322 case MFF_ICMPV4_CODE:
323 case MFF_ICMPV6_CODE:
324 return !wc->masks.tp_dst;
326 return !wc->masks.tcp_flags;
334 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
335 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
336 * purposes, or to 0 if it is wildcarded.
338 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
339 * meets 'mf''s prerequisites. */
341 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
342 union mf_value *mask)
344 mf_get_value(mf, &wc->masks, mask);
347 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
348 * if the mask is valid, false otherwise. */
350 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
352 switch (mf->maskable) {
354 return (is_all_zeros(mask, mf->n_bytes) ||
355 is_all_ones(mask, mf->n_bytes));
364 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
366 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
368 switch (mf->prereqs) {
373 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
374 flow->dl_type == htons(ETH_TYPE_RARP));
376 return flow->dl_type == htons(ETH_TYPE_IP);
378 return flow->dl_type == htons(ETH_TYPE_IPV6);
380 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
382 return eth_type_mpls(flow->dl_type);
384 return is_ip_any(flow);
387 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
388 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
390 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
391 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
393 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
394 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
396 return is_icmpv4(flow, NULL);
398 return is_icmpv6(flow, NULL);
401 return (is_icmpv6(flow, NULL)
402 && flow->tp_dst == htons(0)
403 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
404 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
406 return (is_icmpv6(flow, NULL)
407 && flow->tp_dst == htons(0)
408 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
410 return (is_icmpv6(flow, NULL)
411 && flow->tp_dst == htons(0)
412 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
418 /* Set field and it's prerequisities in the mask.
419 * This is only ever called for writeable 'mf's, but we do not make the
420 * distinction here. */
422 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
424 mf_mask_field_and_prereqs__(mf, &exact_match_mask, wc);
428 mf_mask_field_and_prereqs__(const struct mf_field *mf,
429 const union mf_value *mask,
430 struct flow_wildcards *wc)
432 mf_set_flow_value_masked(mf, &exact_match_mask, mask, &wc->masks);
434 switch (mf->prereqs) {
438 WC_MASK_FIELD(wc, tp_src);
439 WC_MASK_FIELD(wc, tp_dst);
446 /* nw_frag always unwildcarded. */
447 WC_MASK_FIELD(wc, nw_proto);
454 /* dl_type always unwildcarded. */
457 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
464 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
466 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
468 bitmap_set1(bm->bm, mf->id);
470 switch (mf->prereqs) {
474 bitmap_set1(bm->bm, MFF_TCP_SRC);
475 bitmap_set1(bm->bm, MFF_TCP_DST);
482 /* nw_frag always unwildcarded. */
483 bitmap_set1(bm->bm, MFF_IP_PROTO);
490 bitmap_set1(bm->bm, MFF_ETH_TYPE);
493 bitmap_set1(bm->bm, MFF_VLAN_TCI);
500 /* Returns true if 'value' may be a valid value *as part of a masked match*,
503 * A value is not rejected just because it is not valid for the field in
504 * question, but only if it doesn't make sense to test the bits in question at
505 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
506 * without the VLAN_CFI bit being set, but we can't reject those values because
507 * it is still legitimate to test just for those bits (see the documentation
508 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
509 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
511 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
520 case MFF_TUN_IPV6_SRC:
521 case MFF_TUN_IPV6_DST:
525 case MFF_TUN_GBP_FLAGS:
526 CASE_MFF_TUN_METADATA:
529 case MFF_SKB_PRIORITY:
557 case MFF_ICMPV4_TYPE:
558 case MFF_ICMPV4_CODE:
559 case MFF_ICMPV6_TYPE:
560 case MFF_ICMPV6_CODE:
566 case MFF_IN_PORT_OXM:
567 case MFF_ACTSET_OUTPUT: {
569 return !ofputil_port_from_ofp11(value->be32, &port);
573 return !(value->u8 & ~IP_DSCP_MASK);
574 case MFF_IP_DSCP_SHIFTED:
575 return !(value->u8 & (~IP_DSCP_MASK >> 2));
577 return !(value->u8 & ~IP_ECN_MASK);
579 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
581 return !(value->be16 & ~htons(0x0fff));
584 return !(value->be16 & htons(0xff00));
587 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
589 return !(value->be16 & htons(VLAN_PCP_MASK));
591 case MFF_DL_VLAN_PCP:
593 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
596 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
599 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
602 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
605 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
608 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
611 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
619 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
620 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
622 mf_get_value(const struct mf_field *mf, const struct flow *flow,
623 union mf_value *value)
627 value->be32 = htonl(flow->dp_hash);
630 value->be32 = htonl(flow->recirc_id);
633 value->be32 = htonl(flow->conj_id);
636 value->be64 = flow->tunnel.tun_id;
639 value->be32 = flow->tunnel.ip_src;
642 value->be32 = flow->tunnel.ip_dst;
644 case MFF_TUN_IPV6_SRC:
645 value->ipv6 = flow->tunnel.ipv6_src;
647 case MFF_TUN_IPV6_DST:
648 value->ipv6 = flow->tunnel.ipv6_dst;
651 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
654 value->be16 = flow->tunnel.gbp_id;
656 case MFF_TUN_GBP_FLAGS:
657 value->u8 = flow->tunnel.gbp_flags;
660 value->u8 = flow->tunnel.ip_ttl;
663 value->u8 = flow->tunnel.ip_tos;
665 CASE_MFF_TUN_METADATA:
666 tun_metadata_read(&flow->tunnel, mf, value);
670 value->be64 = flow->metadata;
674 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
676 case MFF_IN_PORT_OXM:
677 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
679 case MFF_ACTSET_OUTPUT:
680 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
683 case MFF_SKB_PRIORITY:
684 value->be32 = htonl(flow->skb_priority);
688 value->be32 = htonl(flow->pkt_mark);
692 value->be32 = htonl(flow->ct_state);
696 value->be16 = htons(flow->ct_zone);
700 value->be32 = htonl(flow->ct_mark);
704 value->be128 = hton128(flow->ct_label);
708 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
712 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
716 value->mac = flow->dl_src;
720 value->mac = flow->dl_dst;
724 value->be16 = flow->dl_type;
728 value->be16 = flow->vlan_tci;
732 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
735 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
738 case MFF_DL_VLAN_PCP:
740 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
744 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
748 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
752 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
756 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
760 value->be32 = flow->nw_src;
764 value->be32 = flow->nw_dst;
768 value->ipv6 = flow->ipv6_src;
772 value->ipv6 = flow->ipv6_dst;
776 value->be32 = flow->ipv6_label;
780 value->u8 = flow->nw_proto;
784 value->u8 = flow->nw_tos & IP_DSCP_MASK;
787 case MFF_IP_DSCP_SHIFTED:
788 value->u8 = flow->nw_tos >> 2;
792 value->u8 = flow->nw_tos & IP_ECN_MASK;
796 value->u8 = flow->nw_ttl;
800 value->u8 = flow->nw_frag;
804 value->be16 = htons(flow->nw_proto);
808 value->be32 = flow->nw_src;
812 value->be32 = flow->nw_dst;
817 value->mac = flow->arp_sha;
822 value->mac = flow->arp_tha;
828 value->be16 = flow->tp_src;
834 value->be16 = flow->tp_dst;
838 value->be16 = flow->tcp_flags;
841 case MFF_ICMPV4_TYPE:
842 case MFF_ICMPV6_TYPE:
843 value->u8 = ntohs(flow->tp_src);
846 case MFF_ICMPV4_CODE:
847 case MFF_ICMPV6_CODE:
848 value->u8 = ntohs(flow->tp_dst);
852 value->ipv6 = flow->nd_target;
861 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
862 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
865 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
866 * with the request or NULL if there is no error. The caller is reponsible
867 * for freeing the string. */
869 mf_set_value(const struct mf_field *mf,
870 const union mf_value *value, struct match *match, char **err_str)
878 match_set_dp_hash(match, ntohl(value->be32));
881 match_set_recirc_id(match, ntohl(value->be32));
884 match_set_conj_id(match, ntohl(value->be32));
887 match_set_tun_id(match, value->be64);
890 match_set_tun_src(match, value->be32);
893 match_set_tun_dst(match, value->be32);
895 case MFF_TUN_IPV6_SRC:
896 match_set_tun_ipv6_src(match, &value->ipv6);
898 case MFF_TUN_IPV6_DST:
899 match_set_tun_ipv6_dst(match, &value->ipv6);
902 match_set_tun_flags(match, ntohs(value->be16));
905 match_set_tun_gbp_id(match, value->be16);
907 case MFF_TUN_GBP_FLAGS:
908 match_set_tun_gbp_flags(match, value->u8);
911 match_set_tun_tos(match, value->u8);
914 match_set_tun_ttl(match, value->u8);
916 CASE_MFF_TUN_METADATA:
917 tun_metadata_set_match(mf, value, NULL, match, err_str);
921 match_set_metadata(match, value->be64);
925 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
928 case MFF_IN_PORT_OXM: {
930 ofputil_port_from_ofp11(value->be32, &port);
931 match_set_in_port(match, port);
934 case MFF_ACTSET_OUTPUT: {
936 ofputil_port_from_ofp11(value->be32, &port);
937 match_set_actset_output(match, port);
941 case MFF_SKB_PRIORITY:
942 match_set_skb_priority(match, ntohl(value->be32));
946 match_set_pkt_mark(match, ntohl(value->be32));
950 match_set_ct_state(match, ntohl(value->be32));
954 match_set_ct_zone(match, ntohs(value->be16));
958 match_set_ct_mark(match, ntohl(value->be32));
962 match_set_ct_label(match, ntoh128(value->be128));
966 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
970 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
974 match_set_dl_src(match, value->mac);
978 match_set_dl_dst(match, value->mac);
982 match_set_dl_type(match, value->be16);
986 match_set_dl_tci(match, value->be16);
990 match_set_dl_vlan(match, value->be16);
993 match_set_vlan_vid(match, value->be16);
996 case MFF_DL_VLAN_PCP:
998 match_set_dl_vlan_pcp(match, value->u8);
1001 case MFF_MPLS_LABEL:
1002 match_set_mpls_label(match, 0, value->be32);
1006 match_set_mpls_tc(match, 0, value->u8);
1010 match_set_mpls_bos(match, 0, value->u8);
1014 match_set_mpls_ttl(match, 0, value->u8);
1018 match_set_nw_src(match, value->be32);
1022 match_set_nw_dst(match, value->be32);
1026 match_set_ipv6_src(match, &value->ipv6);
1030 match_set_ipv6_dst(match, &value->ipv6);
1033 case MFF_IPV6_LABEL:
1034 match_set_ipv6_label(match, value->be32);
1038 match_set_nw_proto(match, value->u8);
1042 match_set_nw_dscp(match, value->u8);
1045 case MFF_IP_DSCP_SHIFTED:
1046 match_set_nw_dscp(match, value->u8 << 2);
1050 match_set_nw_ecn(match, value->u8);
1054 match_set_nw_ttl(match, value->u8);
1058 match_set_nw_frag(match, value->u8);
1062 match_set_nw_proto(match, ntohs(value->be16));
1066 match_set_nw_src(match, value->be32);
1070 match_set_nw_dst(match, value->be32);
1075 match_set_arp_sha(match, value->mac);
1080 match_set_arp_tha(match, value->mac);
1086 match_set_tp_src(match, value->be16);
1092 match_set_tp_dst(match, value->be16);
1096 match_set_tcp_flags(match, value->be16);
1099 case MFF_ICMPV4_TYPE:
1100 case MFF_ICMPV6_TYPE:
1101 match_set_icmp_type(match, value->u8);
1104 case MFF_ICMPV4_CODE:
1105 case MFF_ICMPV6_CODE:
1106 match_set_icmp_code(match, value->u8);
1110 match_set_nd_target(match, &value->ipv6);
1119 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1120 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1122 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1124 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1125 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1126 * special case. For the rest, calling mf_set_flow_value() is good
1128 if (mf->id == MFF_DL_VLAN) {
1129 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1131 mf_set_flow_value(mf, &exact_match_mask, mask);
1136 field_len(const struct mf_field *mf, const union mf_value *value_)
1138 const uint8_t *value = &value_->u8;
1141 if (!mf->variable_len) {
1149 for (i = 0; i < mf->n_bytes; i++) {
1150 if (value[i] != 0) {
1155 return mf->n_bytes - i;
1158 /* Returns the effective length of the field. For fixed length fields,
1159 * this is just the defined length. For variable length fields, it is
1160 * the minimum size encoding that retains the same meaning (i.e.
1161 * discarding leading zeros).
1163 * 'is_masked' returns (if non-NULL) whether the original contained
1164 * a mask. Otherwise, a mask that is the same length as the value
1165 * might be misinterpreted as an exact match. */
1167 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1168 const union mf_value *mask, bool *is_masked_)
1171 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1173 len = field_len(mf, value);
1175 mask_len = field_len(mf, mask);
1176 len = MAX(len, mask_len);
1180 *is_masked_ = is_masked;
1186 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1187 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1189 mf_set_flow_value(const struct mf_field *mf,
1190 const union mf_value *value, struct flow *flow)
1194 flow->dp_hash = ntohl(value->be32);
1197 flow->recirc_id = ntohl(value->be32);
1200 flow->conj_id = ntohl(value->be32);
1203 flow->tunnel.tun_id = value->be64;
1206 flow->tunnel.ip_src = value->be32;
1209 flow->tunnel.ip_dst = value->be32;
1211 case MFF_TUN_IPV6_SRC:
1212 flow->tunnel.ipv6_src = value->ipv6;
1214 case MFF_TUN_IPV6_DST:
1215 flow->tunnel.ipv6_dst = value->ipv6;
1218 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1221 case MFF_TUN_GBP_ID:
1222 flow->tunnel.gbp_id = value->be16;
1224 case MFF_TUN_GBP_FLAGS:
1225 flow->tunnel.gbp_flags = value->u8;
1228 flow->tunnel.ip_tos = value->u8;
1231 flow->tunnel.ip_ttl = value->u8;
1233 CASE_MFF_TUN_METADATA:
1234 tun_metadata_write(&flow->tunnel, mf, value);
1237 flow->metadata = value->be64;
1241 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1244 case MFF_IN_PORT_OXM:
1245 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1247 case MFF_ACTSET_OUTPUT:
1248 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1251 case MFF_SKB_PRIORITY:
1252 flow->skb_priority = ntohl(value->be32);
1256 flow->pkt_mark = ntohl(value->be32);
1260 flow->ct_state = ntohl(value->be32);
1264 flow->ct_zone = ntohs(value->be16);
1268 flow->ct_mark = ntohl(value->be32);
1272 flow->ct_label = ntoh128(value->be128);
1276 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1280 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1284 flow->dl_src = value->mac;
1288 flow->dl_dst = value->mac;
1292 flow->dl_type = value->be16;
1296 flow->vlan_tci = value->be16;
1300 flow_set_dl_vlan(flow, value->be16);
1303 flow_set_vlan_vid(flow, value->be16);
1306 case MFF_DL_VLAN_PCP:
1308 flow_set_vlan_pcp(flow, value->u8);
1311 case MFF_MPLS_LABEL:
1312 flow_set_mpls_label(flow, 0, value->be32);
1316 flow_set_mpls_tc(flow, 0, value->u8);
1320 flow_set_mpls_bos(flow, 0, value->u8);
1324 flow_set_mpls_ttl(flow, 0, value->u8);
1328 flow->nw_src = value->be32;
1332 flow->nw_dst = value->be32;
1336 flow->ipv6_src = value->ipv6;
1340 flow->ipv6_dst = value->ipv6;
1343 case MFF_IPV6_LABEL:
1344 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1348 flow->nw_proto = value->u8;
1352 flow->nw_tos &= ~IP_DSCP_MASK;
1353 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1356 case MFF_IP_DSCP_SHIFTED:
1357 flow->nw_tos &= ~IP_DSCP_MASK;
1358 flow->nw_tos |= value->u8 << 2;
1362 flow->nw_tos &= ~IP_ECN_MASK;
1363 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1367 flow->nw_ttl = value->u8;
1371 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1375 flow->nw_proto = ntohs(value->be16);
1379 flow->nw_src = value->be32;
1383 flow->nw_dst = value->be32;
1388 flow->arp_sha = value->mac;
1393 flow->arp_tha = value->mac;
1399 flow->tp_src = value->be16;
1405 flow->tp_dst = value->be16;
1409 flow->tcp_flags = value->be16;
1412 case MFF_ICMPV4_TYPE:
1413 case MFF_ICMPV6_TYPE:
1414 flow->tp_src = htons(value->u8);
1417 case MFF_ICMPV4_CODE:
1418 case MFF_ICMPV6_CODE:
1419 flow->tp_dst = htons(value->u8);
1423 flow->nd_target = value->ipv6;
1432 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1433 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1436 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1440 for (i = 0; i < n; i++) {
1441 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1445 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1446 * for which 'mask' has a 0-bit keep their existing values. The caller is
1447 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1449 mf_set_flow_value_masked(const struct mf_field *field,
1450 const union mf_value *value,
1451 const union mf_value *mask,
1456 mf_get_value(field, flow, &tmp);
1457 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1458 (uint8_t *) &tmp, field->n_bytes);
1459 mf_set_flow_value(field, &tmp, flow);
1463 mf_is_tun_metadata(const struct mf_field *mf)
1465 return mf->id >= MFF_TUN_METADATA0 &&
1466 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1469 /* Returns true if 'mf' has previously been set in 'flow', false if
1470 * it contains a non-default value.
1472 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1475 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1477 if (!mf_is_tun_metadata(mf)) {
1478 union mf_value value;
1480 mf_get_value(mf, flow, &value);
1481 return !is_all_zeros(&value, mf->n_bytes);
1483 return ULLONG_GET(flow->tunnel.metadata.present.map,
1484 mf->id - MFF_TUN_METADATA0);
1488 /* Makes 'match' wildcard field 'mf'.
1490 * The caller is responsible for ensuring that 'match' meets 'mf''s
1493 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1494 * with the request or NULL if there is no error. The caller is reponsible
1495 * for freeing the string. */
1497 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1505 match->flow.dp_hash = 0;
1506 match->wc.masks.dp_hash = 0;
1509 match->flow.recirc_id = 0;
1510 match->wc.masks.recirc_id = 0;
1513 match->flow.conj_id = 0;
1514 match->wc.masks.conj_id = 0;
1517 match_set_tun_id_masked(match, htonll(0), htonll(0));
1520 match_set_tun_src_masked(match, htonl(0), htonl(0));
1523 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1525 case MFF_TUN_IPV6_SRC:
1526 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1527 sizeof match->wc.masks.tunnel.ipv6_src);
1528 memset(&match->flow.tunnel.ipv6_src, 0,
1529 sizeof match->flow.tunnel.ipv6_src);
1531 case MFF_TUN_IPV6_DST:
1532 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1533 sizeof match->wc.masks.tunnel.ipv6_dst);
1534 memset(&match->flow.tunnel.ipv6_dst, 0,
1535 sizeof match->flow.tunnel.ipv6_dst);
1538 match_set_tun_flags_masked(match, 0, 0);
1540 case MFF_TUN_GBP_ID:
1541 match_set_tun_gbp_id_masked(match, 0, 0);
1543 case MFF_TUN_GBP_FLAGS:
1544 match_set_tun_gbp_flags_masked(match, 0, 0);
1547 match_set_tun_tos_masked(match, 0, 0);
1550 match_set_tun_ttl_masked(match, 0, 0);
1552 CASE_MFF_TUN_METADATA:
1553 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1557 match_set_metadata_masked(match, htonll(0), htonll(0));
1561 case MFF_IN_PORT_OXM:
1562 match->flow.in_port.ofp_port = 0;
1563 match->wc.masks.in_port.ofp_port = 0;
1565 case MFF_ACTSET_OUTPUT:
1566 match->flow.actset_output = 0;
1567 match->wc.masks.actset_output = 0;
1570 case MFF_SKB_PRIORITY:
1571 match->flow.skb_priority = 0;
1572 match->wc.masks.skb_priority = 0;
1576 match->flow.pkt_mark = 0;
1577 match->wc.masks.pkt_mark = 0;
1581 match->flow.ct_state = 0;
1582 match->wc.masks.ct_state = 0;
1586 match->flow.ct_zone = 0;
1587 match->wc.masks.ct_zone = 0;
1591 match->flow.ct_mark = 0;
1592 match->wc.masks.ct_mark = 0;
1596 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1597 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1601 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1605 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1609 match->flow.dl_src = eth_addr_zero;
1610 match->wc.masks.dl_src = eth_addr_zero;
1614 match->flow.dl_dst = eth_addr_zero;
1615 match->wc.masks.dl_dst = eth_addr_zero;
1619 match->flow.dl_type = htons(0);
1620 match->wc.masks.dl_type = htons(0);
1624 match_set_dl_tci_masked(match, htons(0), htons(0));
1629 match_set_any_vid(match);
1632 case MFF_DL_VLAN_PCP:
1634 match_set_any_pcp(match);
1637 case MFF_MPLS_LABEL:
1638 match_set_any_mpls_label(match, 0);
1642 match_set_any_mpls_tc(match, 0);
1646 match_set_any_mpls_bos(match, 0);
1650 match_set_any_mpls_ttl(match, 0);
1655 match_set_nw_src_masked(match, htonl(0), htonl(0));
1660 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1664 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1665 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1669 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1670 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1673 case MFF_IPV6_LABEL:
1674 match->wc.masks.ipv6_label = htonl(0);
1675 match->flow.ipv6_label = htonl(0);
1679 match->wc.masks.nw_proto = 0;
1680 match->flow.nw_proto = 0;
1684 case MFF_IP_DSCP_SHIFTED:
1685 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1686 match->flow.nw_tos &= ~IP_DSCP_MASK;
1690 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1691 match->flow.nw_tos &= ~IP_ECN_MASK;
1695 match->wc.masks.nw_ttl = 0;
1696 match->flow.nw_ttl = 0;
1700 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1701 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1705 match->wc.masks.nw_proto = 0;
1706 match->flow.nw_proto = 0;
1711 match->flow.arp_sha = eth_addr_zero;
1712 match->wc.masks.arp_sha = eth_addr_zero;
1717 match->flow.arp_tha = eth_addr_zero;
1718 match->wc.masks.arp_tha = eth_addr_zero;
1724 case MFF_ICMPV4_TYPE:
1725 case MFF_ICMPV6_TYPE:
1726 match->wc.masks.tp_src = htons(0);
1727 match->flow.tp_src = htons(0);
1733 case MFF_ICMPV4_CODE:
1734 case MFF_ICMPV6_CODE:
1735 match->wc.masks.tp_dst = htons(0);
1736 match->flow.tp_dst = htons(0);
1740 match->wc.masks.tcp_flags = htons(0);
1741 match->flow.tcp_flags = htons(0);
1745 memset(&match->wc.masks.nd_target, 0,
1746 sizeof match->wc.masks.nd_target);
1747 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1756 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1757 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1758 * with a 1-bit indicating that the corresponding value bit must match and a
1759 * 0-bit indicating a don't-care.
1761 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1762 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1763 * call is equivalent to mf_set_wild(mf, match).
1765 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1766 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1768 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1769 * with the request or NULL if there is no error. The caller is reponsible
1770 * for freeing the string.
1772 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1773 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1774 * protocol versions can support this functionality. */
1776 mf_set(const struct mf_field *mf,
1777 const union mf_value *value, const union mf_value *mask,
1778 struct match *match, char **err_str)
1780 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1781 mf_set_value(mf, value, match, err_str);
1782 return mf->usable_protocols_exact;
1783 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1784 /* Tunnel metadata matches on the existence of the field itself, so
1785 * it still needs to be encoded even if the value is wildcarded. */
1786 mf_set_wild(mf, match, err_str);
1787 return OFPUTIL_P_ANY;
1799 case MFF_IN_PORT_OXM:
1800 case MFF_ACTSET_OUTPUT:
1801 case MFF_SKB_PRIORITY:
1804 case MFF_DL_VLAN_PCP:
1806 case MFF_MPLS_LABEL:
1813 case MFF_IP_DSCP_SHIFTED:
1816 case MFF_ICMPV4_TYPE:
1817 case MFF_ICMPV4_CODE:
1818 case MFF_ICMPV6_TYPE:
1819 case MFF_ICMPV6_CODE:
1820 return OFPUTIL_P_NONE;
1823 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1826 match_set_tun_id_masked(match, value->be64, mask->be64);
1829 match_set_tun_src_masked(match, value->be32, mask->be32);
1832 match_set_tun_dst_masked(match, value->be32, mask->be32);
1834 case MFF_TUN_IPV6_SRC:
1835 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1837 case MFF_TUN_IPV6_DST:
1838 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1841 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1843 case MFF_TUN_GBP_ID:
1844 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1846 case MFF_TUN_GBP_FLAGS:
1847 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1850 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1853 match_set_tun_tos_masked(match, value->u8, mask->u8);
1855 CASE_MFF_TUN_METADATA:
1856 tun_metadata_set_match(mf, value, mask, match, err_str);
1860 match_set_metadata_masked(match, value->be64, mask->be64);
1864 match_set_reg_masked(match, mf->id - MFF_REG0,
1865 ntohl(value->be32), ntohl(mask->be32));
1869 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1870 ntohll(value->be64), ntohll(mask->be64));
1874 match_set_pkt_mark_masked(match, ntohl(value->be32),
1879 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1883 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1887 match_set_ct_label_masked(match, ntoh128(value->be128),
1888 mask ? ntoh128(mask->be128) : OVS_U128_MAX);
1892 match_set_dl_dst_masked(match, value->mac, mask->mac);
1896 match_set_dl_src_masked(match, value->mac, mask->mac);
1901 match_set_arp_sha_masked(match, value->mac, mask->mac);
1906 match_set_arp_tha_masked(match, value->mac, mask->mac);
1910 match_set_dl_tci_masked(match, value->be16, mask->be16);
1914 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1918 match_set_nw_src_masked(match, value->be32, mask->be32);
1922 match_set_nw_dst_masked(match, value->be32, mask->be32);
1926 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1930 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1933 case MFF_IPV6_LABEL:
1934 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1935 mf_set_value(mf, value, match, err_str);
1937 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1942 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1946 match_set_nw_frag_masked(match, value->u8, mask->u8);
1950 match_set_nw_src_masked(match, value->be32, mask->be32);
1954 match_set_nw_dst_masked(match, value->be32, mask->be32);
1960 match_set_tp_src_masked(match, value->be16, mask->be16);
1966 match_set_tp_dst_masked(match, value->be16, mask->be16);
1970 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1978 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1979 || ip_is_cidr(mask->be32))
1980 ? mf->usable_protocols_cidr
1981 : mf->usable_protocols_bitwise);
1985 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1989 VLOG_WARN_RL(&rl, "unknown %s field", type);
1990 return OFPERR_OFPBAC_BAD_SET_TYPE;
1991 } else if (!sf->n_bits) {
1992 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1993 return OFPERR_OFPBAC_BAD_SET_LEN;
1994 } else if (sf->ofs >= sf->field->n_bits) {
1995 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1996 sf->ofs, sf->field->n_bits, type, sf->field->name);
1997 return OFPERR_OFPBAC_BAD_SET_LEN;
1998 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1999 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2000 "of %s field %s", sf->ofs, sf->n_bits,
2001 sf->field->n_bits, type, sf->field->name);
2002 return OFPERR_OFPBAC_BAD_SET_LEN;
2003 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2004 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2005 type, sf->field->name);
2006 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2012 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2013 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2016 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2018 return mf_check__(sf, flow, "source");
2021 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2022 * if so, otherwise an OpenFlow error code (e.g. as returned by
2025 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2027 int error = mf_check__(sf, flow, "destination");
2028 if (!error && !sf->field->writable) {
2029 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2031 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2036 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2037 * 'value' and 'mask', respectively. */
2039 mf_get(const struct mf_field *mf, const struct match *match,
2040 union mf_value *value, union mf_value *mask)
2042 mf_get_value(mf, &match->flow, value);
2043 mf_get_mask(mf, &match->wc, mask);
2047 mf_from_integer_string(const struct mf_field *mf, const char *s,
2048 uint8_t *valuep, uint8_t *maskp)
2051 const char *err_str = "";
2054 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2055 if (err || (*tail != '\0' && *tail != '/')) {
2061 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2062 if (err || *tail != '\0') {
2067 memset(maskp, 0xff, mf->n_bytes);
2073 if (err == ERANGE) {
2074 return xasprintf("%s: %s too large for %u-byte field %s",
2075 s, err_str, mf->n_bytes, mf->name);
2077 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2082 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2083 struct eth_addr *mac, struct eth_addr *mask)
2087 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2090 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2091 && n == strlen(s)) {
2092 *mask = eth_addr_exact;
2097 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2098 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2099 && n == strlen(s)) {
2103 return xasprintf("%s: invalid Ethernet address", s);
2107 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2108 ovs_be32 *ip, ovs_be32 *mask)
2110 ovs_assert(mf->n_bytes == sizeof *ip);
2111 return ip_parse_masked(s, ip, mask);
2115 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2116 struct in6_addr *ipv6, struct in6_addr *mask)
2118 ovs_assert(mf->n_bytes == sizeof *ipv6);
2119 return ipv6_parse_masked(s, ipv6, mask);
2123 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2124 ovs_be16 *valuep, ovs_be16 *maskp)
2128 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2130 if (ofputil_port_from_string(s, &port)) {
2131 *valuep = htons(ofp_to_u16(port));
2132 *maskp = OVS_BE16_MAX;
2135 return xasprintf("%s: port value out of range for %s", s, mf->name);
2139 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2140 ovs_be32 *valuep, ovs_be32 *maskp)
2144 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2145 if (ofputil_port_from_string(s, &port)) {
2146 *valuep = ofputil_port_to_ofp11(port);
2147 *maskp = OVS_BE32_MAX;
2150 return xasprintf("%s: port value out of range for %s", s, mf->name);
2153 struct frag_handling {
2159 static const struct frag_handling all_frags[] = {
2160 #define A FLOW_NW_FRAG_ANY
2161 #define L FLOW_NW_FRAG_LATER
2162 /* name mask value */
2165 { "first", A|L, A },
2166 { "later", A|L, A|L },
2171 { "not_later", L, 0 },
2178 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2180 const struct frag_handling *h;
2182 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2183 if (!strcasecmp(s, h->name)) {
2184 /* We force the upper bits of the mask on to make mf_parse_value()
2185 * happy (otherwise it will never think it's an exact match.) */
2186 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2192 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2193 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2197 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2198 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2203 uint32_t flags, mask;
2205 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2206 &flags, ntohs(allowed), maskp ? &mask : NULL);
2211 *flagsp = htons(flags);
2213 *maskp = htons(mask);
2220 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2222 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2223 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2227 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2229 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2230 htons(FLOW_TNL_PUB_F_MASK), maskp);
2234 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2238 uint32_t flags, mask;
2240 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2241 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2246 *flagsp = htonl(flags);
2248 *maskp = htonl(mask);
2254 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2255 * NULL if successful, otherwise a malloc()'d string describing the error. */
2257 mf_parse(const struct mf_field *mf, const char *s,
2258 union mf_value *value, union mf_value *mask)
2262 if (!strcmp(s, "*")) {
2263 memset(value, 0, mf->n_bytes);
2264 memset(mask, 0, mf->n_bytes);
2268 switch (mf->string) {
2270 case MFS_HEXADECIMAL:
2271 error = mf_from_integer_string(mf, s,
2272 (uint8_t *) value, (uint8_t *) mask);
2276 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2277 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2281 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2285 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2289 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2293 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2296 case MFS_OFP_PORT_OXM:
2297 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2301 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2305 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2306 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2310 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2311 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2318 if (!error && !mf_is_mask_valid(mf, mask)) {
2319 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2324 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2325 * successful, otherwise a malloc()'d string describing the error. */
2327 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2329 union mf_value mask;
2332 error = mf_parse(mf, s, value, &mask);
2337 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2338 return xasprintf("%s: wildcards not allowed here", s);
2344 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2345 const uint8_t *maskp, struct ds *s)
2347 if (mf->string == MFS_HEXADECIMAL) {
2348 ds_put_hex(s, valuep, mf->n_bytes);
2350 unsigned long long int integer = 0;
2353 ovs_assert(mf->n_bytes <= 8);
2354 for (i = 0; i < mf->n_bytes; i++) {
2355 integer = (integer << 8) | valuep[i];
2357 ds_put_format(s, "%lld", integer);
2361 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2362 * not sure that that a bit-mask written in decimal is ever easier to
2363 * understand than the same bit-mask written in hexadecimal. */
2364 ds_put_char(s, '/');
2365 ds_put_hex(s, maskp, mf->n_bytes);
2370 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2372 const struct frag_handling *h;
2374 mask &= FLOW_NW_FRAG_MASK;
2377 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2378 if (value == h->value && mask == h->mask) {
2379 ds_put_cstr(s, h->name);
2383 ds_put_cstr(s, "<error>");
2387 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2389 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2390 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2394 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2396 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2397 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2401 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2403 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2404 ntohl(mask), UINT16_MAX);
2407 /* Appends to 's' a string representation of field 'mf' whose value is in
2408 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2410 mf_format(const struct mf_field *mf,
2411 const union mf_value *value, const union mf_value *mask,
2415 if (is_all_zeros(mask, mf->n_bytes)) {
2416 ds_put_cstr(s, "ANY");
2418 } else if (is_all_ones(mask, mf->n_bytes)) {
2423 switch (mf->string) {
2424 case MFS_OFP_PORT_OXM:
2427 ofputil_port_from_ofp11(value->be32, &port);
2428 ofputil_format_port(port, s);
2434 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2439 case MFS_HEXADECIMAL:
2440 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2444 mf_format_ct_state_string(value->be32,
2445 mask ? mask->be32 : OVS_BE32_MAX, s);
2449 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2453 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2457 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
2461 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2465 mf_format_tnl_flags_string(value->be16,
2466 mask ? mask->be16 : OVS_BE16_MAX, s);
2470 mf_format_tcp_flags_string(value->be16,
2471 mask ? mask->be16 : OVS_BE16_MAX, s);
2479 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2480 * least-significant bits in 'x'.
2483 mf_write_subfield_flow(const struct mf_subfield *sf,
2484 const union mf_subvalue *x, struct flow *flow)
2486 const struct mf_field *field = sf->field;
2487 union mf_value value;
2489 mf_get_value(field, flow, &value);
2490 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2491 sf->ofs, sf->n_bits);
2492 mf_set_flow_value(field, &value, flow);
2495 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2496 * least-significant bits in 'x'.
2499 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2500 struct match *match)
2502 const struct mf_field *field = sf->field;
2503 union mf_value value, mask;
2505 mf_get(field, match, &value, &mask);
2506 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2507 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2508 mf_set(field, &value, &mask, match, NULL);
2511 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2512 * 'match' in the correspond positions. */
2514 mf_mask_subfield(const struct mf_field *field,
2515 const union mf_subvalue *v,
2516 const union mf_subvalue *m,
2517 struct match *match)
2519 union mf_value value, mask;
2521 mf_get(field, match, &value, &mask);
2522 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2523 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2524 mf_set(field, &value, &mask, match, NULL);
2527 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2528 * reading 'flow', e.g. as checked by mf_check_src(). */
2530 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2531 union mf_subvalue *x)
2533 union mf_value value;
2535 mf_get_value(sf->field, flow, &value);
2537 memset(x, 0, sizeof *x);
2538 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2543 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2544 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2547 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2549 union mf_value value;
2551 mf_get_value(sf->field, flow, &value);
2552 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2556 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2558 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2562 field_array_set(enum mf_field_id id, const union mf_value *value,
2563 struct field_array *fa)
2565 ovs_assert(id < MFF_N_IDS);
2566 bitmap_set1(fa->used.bm, id);
2567 fa->value[id] = *value;