2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 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 "tun-metadata.h"
37 #include "unaligned.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 static void nxm_init(void);
65 /* Returns the field with the given 'name', or a null pointer if no field has
67 const struct mf_field *
68 mf_from_name(const char *name)
71 return shash_find_data(&mf_by_name, name);
79 shash_init(&mf_by_name);
80 for (i = 0; i < MFF_N_IDS; i++) {
81 const struct mf_field *mf = &mf_fields[i];
83 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
85 shash_add_once(&mf_by_name, mf->name, mf);
87 shash_add_once(&mf_by_name, mf->extra_name, mf);
95 static pthread_once_t once = PTHREAD_ONCE_INIT;
96 pthread_once(&once, nxm_do_init);
99 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
100 * restrictions on a field's value. Then, this function initializes
101 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
102 * This is not always possible, i.e. if one pair insists on a value of 0 in
103 * some bit and the other pair insists on a value of 1 in that bit. This
104 * function returns false in a case where the combined restriction is
105 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
108 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
109 * a corresponding 1-bit in its mask.) */
111 mf_subvalue_intersect(const union mf_subvalue *a_value,
112 const union mf_subvalue *a_mask,
113 const union mf_subvalue *b_value,
114 const union mf_subvalue *b_mask,
115 union mf_subvalue *dst_value,
116 union mf_subvalue *dst_mask)
118 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
119 ovs_be64 av = a_value->be64[i];
120 ovs_be64 am = a_mask->be64[i];
121 ovs_be64 bv = b_value->be64[i];
122 ovs_be64 bm = b_mask->be64[i];
123 ovs_be64 *dv = &dst_value->be64[i];
124 ovs_be64 *dm = &dst_mask->be64[i];
126 if ((av ^ bv) & (am & bm)) {
135 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
136 * set, 2 if the second-lowest-order bit is set, and so on. */
138 mf_subvalue_width(const union mf_subvalue *v)
140 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
143 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
144 * negative 'n', shifts the bits '-n' bits to the right. */
146 mf_subvalue_shift(union mf_subvalue *value, int n)
149 union mf_subvalue tmp;
150 memset(&tmp, 0, sizeof tmp);
152 if (n > 0 && n < 8 * sizeof tmp) {
153 bitwise_copy(value, sizeof *value, 0,
156 } else if (n < 0 && n > -8 * sizeof tmp) {
157 bitwise_copy(value, sizeof *value, -n,
165 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
166 * specifies at least one bit in the field.
168 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
169 * meets 'mf''s prerequisites. */
171 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
175 return !wc->masks.dp_hash;
177 return !wc->masks.recirc_id;
179 return !wc->masks.conj_id;
181 return !wc->masks.tunnel.ip_src;
183 return !wc->masks.tunnel.ip_dst;
185 return !wc->masks.tunnel.tun_id;
187 return !wc->masks.tunnel.ip_tos;
189 return !wc->masks.tunnel.ip_ttl;
191 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
193 return !wc->masks.tunnel.gbp_id;
194 case MFF_TUN_GBP_FLAGS:
195 return !wc->masks.tunnel.gbp_flags;
196 CASE_MFF_TUN_METADATA: {
197 union mf_value value;
199 tun_metadata_read(&wc->masks.tunnel.metadata, mf, &value);
200 return is_all_zeros(&value.tun_metadata, mf->n_bytes);
203 return !wc->masks.metadata;
205 case MFF_IN_PORT_OXM:
206 return !wc->masks.in_port.ofp_port;
207 case MFF_SKB_PRIORITY:
208 return !wc->masks.skb_priority;
210 return !wc->masks.pkt_mark;
212 return !wc->masks.regs[mf->id - MFF_REG0];
214 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
215 case MFF_ACTSET_OUTPUT:
216 return !wc->masks.actset_output;
219 return eth_addr_is_zero(wc->masks.dl_src);
221 return eth_addr_is_zero(wc->masks.dl_dst);
223 return !wc->masks.dl_type;
227 return eth_addr_is_zero(wc->masks.arp_sha);
231 return eth_addr_is_zero(wc->masks.arp_tha);
234 return !wc->masks.vlan_tci;
236 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
238 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
239 case MFF_DL_VLAN_PCP:
241 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
244 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
246 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
248 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
251 return !wc->masks.nw_src;
253 return !wc->masks.nw_dst;
256 return ipv6_mask_is_any(&wc->masks.ipv6_src);
258 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
261 return !wc->masks.ipv6_label;
264 return !wc->masks.nw_proto;
266 case MFF_IP_DSCP_SHIFTED:
267 return !(wc->masks.nw_tos & IP_DSCP_MASK);
269 return !(wc->masks.nw_tos & IP_ECN_MASK);
271 return !wc->masks.nw_ttl;
274 return ipv6_mask_is_any(&wc->masks.nd_target);
277 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
280 return !wc->masks.nw_proto;
282 return !wc->masks.nw_src;
284 return !wc->masks.nw_dst;
289 case MFF_ICMPV4_TYPE:
290 case MFF_ICMPV6_TYPE:
291 return !wc->masks.tp_src;
295 case MFF_ICMPV4_CODE:
296 case MFF_ICMPV6_CODE:
297 return !wc->masks.tp_dst;
299 return !wc->masks.tcp_flags;
307 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
308 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
309 * purposes, or to 0 if it is wildcarded.
311 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
312 * meets 'mf''s prerequisites. */
314 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
315 union mf_value *mask)
317 mf_get_value(mf, &wc->masks, mask);
320 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
321 * if the mask is valid, false otherwise. */
323 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
325 switch (mf->maskable) {
327 return (is_all_zeros(mask, mf->n_bytes) ||
328 is_all_ones(mask, mf->n_bytes));
337 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
339 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
341 switch (mf->prereqs) {
346 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
347 flow->dl_type == htons(ETH_TYPE_RARP));
349 return flow->dl_type == htons(ETH_TYPE_IP);
351 return flow->dl_type == htons(ETH_TYPE_IPV6);
353 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
355 return eth_type_mpls(flow->dl_type);
357 return is_ip_any(flow);
360 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
361 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
363 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
364 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
366 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
367 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
369 return is_icmpv4(flow);
371 return is_icmpv6(flow);
374 return (is_icmpv6(flow)
375 && flow->tp_dst == htons(0)
376 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
377 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
379 return (is_icmpv6(flow)
380 && flow->tp_dst == htons(0)
381 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
383 return (is_icmpv6(flow)
384 && flow->tp_dst == htons(0)
385 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
391 /* Set field and it's prerequisities in the mask.
392 * This is only ever called for writeable 'mf's, but we do not make the
393 * distinction here. */
395 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
397 static union mf_value exact_match_mask;
399 memset(&exact_match_mask, 0xff, sizeof exact_match_mask);
400 mf_set_flow_value(mf, &exact_match_mask, mask);
402 switch (mf->prereqs) {
406 mask->tp_src = OVS_BE16_MAX;
407 mask->tp_dst = OVS_BE16_MAX;
414 /* nw_frag always unwildcarded. */
415 mask->nw_proto = 0xff;
422 mask->dl_type = OVS_BE16_MAX;
425 mask->vlan_tci |= htons(VLAN_CFI);
432 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
434 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
436 bitmap_set1(bm->bm, mf->id);
438 switch (mf->prereqs) {
442 bitmap_set1(bm->bm, MFF_TCP_SRC);
443 bitmap_set1(bm->bm, MFF_TCP_DST);
450 /* nw_frag always unwildcarded. */
451 bitmap_set1(bm->bm, MFF_IP_PROTO);
458 bitmap_set1(bm->bm, MFF_ETH_TYPE);
461 bitmap_set1(bm->bm, MFF_VLAN_TCI);
468 /* Returns true if 'value' may be a valid value *as part of a masked match*,
471 * A value is not rejected just because it is not valid for the field in
472 * question, but only if it doesn't make sense to test the bits in question at
473 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
474 * without the VLAN_CFI bit being set, but we can't reject those values because
475 * it is still legitimate to test just for those bits (see the documentation
476 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
477 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
479 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
491 case MFF_TUN_GBP_FLAGS:
492 CASE_MFF_TUN_METADATA:
495 case MFF_SKB_PRIORITY:
519 case MFF_ICMPV4_TYPE:
520 case MFF_ICMPV4_CODE:
521 case MFF_ICMPV6_TYPE:
522 case MFF_ICMPV6_CODE:
528 case MFF_IN_PORT_OXM:
529 case MFF_ACTSET_OUTPUT: {
531 return !ofputil_port_from_ofp11(value->be32, &port);
535 return !(value->u8 & ~IP_DSCP_MASK);
536 case MFF_IP_DSCP_SHIFTED:
537 return !(value->u8 & (~IP_DSCP_MASK >> 2));
539 return !(value->u8 & ~IP_ECN_MASK);
541 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
543 return !(value->be16 & ~htons(0x0fff));
546 return !(value->be16 & htons(0xff00));
549 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
551 return !(value->be16 & htons(VLAN_PCP_MASK));
553 case MFF_DL_VLAN_PCP:
555 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
558 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
561 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
564 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
567 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
570 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
578 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
579 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
581 mf_get_value(const struct mf_field *mf, const struct flow *flow,
582 union mf_value *value)
586 value->be32 = htonl(flow->dp_hash);
589 value->be32 = htonl(flow->recirc_id);
592 value->be32 = htonl(flow->conj_id);
595 value->be64 = flow->tunnel.tun_id;
598 value->be32 = flow->tunnel.ip_src;
601 value->be32 = flow->tunnel.ip_dst;
604 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
607 value->be16 = flow->tunnel.gbp_id;
609 case MFF_TUN_GBP_FLAGS:
610 value->u8 = flow->tunnel.gbp_flags;
613 value->u8 = flow->tunnel.ip_ttl;
616 value->u8 = flow->tunnel.ip_tos;
618 CASE_MFF_TUN_METADATA:
619 tun_metadata_read(&flow->tunnel.metadata, mf, value);
623 value->be64 = flow->metadata;
627 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
629 case MFF_IN_PORT_OXM:
630 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
632 case MFF_ACTSET_OUTPUT:
633 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
636 case MFF_SKB_PRIORITY:
637 value->be32 = htonl(flow->skb_priority);
641 value->be32 = htonl(flow->pkt_mark);
645 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
649 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
653 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
657 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
661 value->be16 = flow->dl_type;
665 value->be16 = flow->vlan_tci;
669 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
672 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
675 case MFF_DL_VLAN_PCP:
677 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
681 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
685 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
689 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
693 value->be32 = flow->nw_src;
697 value->be32 = flow->nw_dst;
701 value->ipv6 = flow->ipv6_src;
705 value->ipv6 = flow->ipv6_dst;
709 value->be32 = flow->ipv6_label;
713 value->u8 = flow->nw_proto;
717 value->u8 = flow->nw_tos & IP_DSCP_MASK;
720 case MFF_IP_DSCP_SHIFTED:
721 value->u8 = flow->nw_tos >> 2;
725 value->u8 = flow->nw_tos & IP_ECN_MASK;
729 value->u8 = flow->nw_ttl;
733 value->u8 = flow->nw_frag;
737 value->be16 = htons(flow->nw_proto);
741 value->be32 = flow->nw_src;
745 value->be32 = flow->nw_dst;
750 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
755 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
761 value->be16 = flow->tp_src;
767 value->be16 = flow->tp_dst;
771 value->be16 = flow->tcp_flags;
774 case MFF_ICMPV4_TYPE:
775 case MFF_ICMPV6_TYPE:
776 value->u8 = ntohs(flow->tp_src);
779 case MFF_ICMPV4_CODE:
780 case MFF_ICMPV6_CODE:
781 value->u8 = ntohs(flow->tp_dst);
785 value->ipv6 = flow->nd_target;
794 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
795 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
798 mf_set_value(const struct mf_field *mf,
799 const union mf_value *value, struct match *match)
803 match_set_dp_hash(match, ntohl(value->be32));
806 match_set_recirc_id(match, ntohl(value->be32));
809 match_set_conj_id(match, ntohl(value->be32));
812 match_set_tun_id(match, value->be64);
815 match_set_tun_src(match, value->be32);
818 match_set_tun_dst(match, value->be32);
821 match_set_tun_flags(match, ntohs(value->be16));
824 match_set_tun_gbp_id(match, value->be16);
826 case MFF_TUN_GBP_FLAGS:
827 match_set_tun_gbp_flags(match, value->u8);
830 match_set_tun_tos(match, value->u8);
833 match_set_tun_ttl(match, value->u8);
835 CASE_MFF_TUN_METADATA:
836 tun_metadata_set_match(mf, value, NULL, match);
840 match_set_metadata(match, value->be64);
844 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
847 case MFF_IN_PORT_OXM: {
849 ofputil_port_from_ofp11(value->be32, &port);
850 match_set_in_port(match, port);
853 case MFF_ACTSET_OUTPUT: {
855 ofputil_port_from_ofp11(value->be32, &port);
856 match_set_actset_output(match, port);
860 case MFF_SKB_PRIORITY:
861 match_set_skb_priority(match, ntohl(value->be32));
865 match_set_pkt_mark(match, ntohl(value->be32));
869 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
873 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
877 match_set_dl_src(match, value->mac);
881 match_set_dl_dst(match, value->mac);
885 match_set_dl_type(match, value->be16);
889 match_set_dl_tci(match, value->be16);
893 match_set_dl_vlan(match, value->be16);
896 match_set_vlan_vid(match, value->be16);
899 case MFF_DL_VLAN_PCP:
901 match_set_dl_vlan_pcp(match, value->u8);
905 match_set_mpls_label(match, 0, value->be32);
909 match_set_mpls_tc(match, 0, value->u8);
913 match_set_mpls_bos(match, 0, value->u8);
917 match_set_nw_src(match, value->be32);
921 match_set_nw_dst(match, value->be32);
925 match_set_ipv6_src(match, &value->ipv6);
929 match_set_ipv6_dst(match, &value->ipv6);
933 match_set_ipv6_label(match, value->be32);
937 match_set_nw_proto(match, value->u8);
941 match_set_nw_dscp(match, value->u8);
944 case MFF_IP_DSCP_SHIFTED:
945 match_set_nw_dscp(match, value->u8 << 2);
949 match_set_nw_ecn(match, value->u8);
953 match_set_nw_ttl(match, value->u8);
957 match_set_nw_frag(match, value->u8);
961 match_set_nw_proto(match, ntohs(value->be16));
965 match_set_nw_src(match, value->be32);
969 match_set_nw_dst(match, value->be32);
974 match_set_arp_sha(match, value->mac);
979 match_set_arp_tha(match, value->mac);
985 match_set_tp_src(match, value->be16);
991 match_set_tp_dst(match, value->be16);
995 match_set_tcp_flags(match, value->be16);
998 case MFF_ICMPV4_TYPE:
999 case MFF_ICMPV6_TYPE:
1000 match_set_icmp_type(match, value->u8);
1003 case MFF_ICMPV4_CODE:
1004 case MFF_ICMPV6_CODE:
1005 match_set_icmp_code(match, value->u8);
1009 match_set_nd_target(match, &value->ipv6);
1018 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1019 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1021 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1023 union mf_value exact_match_mask;
1025 memset(&exact_match_mask, 0xff, sizeof exact_match_mask);
1027 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1028 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1029 * special case. For the rest, calling mf_set_flow_value() is good
1031 if (mf->id == MFF_DL_VLAN) {
1032 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1034 mf_set_flow_value(mf, &exact_match_mask, mask);
1039 field_len(const struct mf_field *mf, const union mf_value *value_)
1041 const uint8_t *value = &value_->u8;
1044 if (!mf->variable_len) {
1052 for (i = 0; i < mf->n_bytes; i++) {
1053 if (value[i] != 0) {
1058 return mf->n_bytes - i;
1061 /* Returns the effective length of the field. For fixed length fields,
1062 * this is just the defined length. For variable length fields, it is
1063 * the minimum size encoding that retains the same meaning (i.e.
1064 * discarding leading zeros). */
1066 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1067 const union mf_value *mask)
1071 len = field_len(mf, value);
1072 if (mask && !is_all_ones(mask, mf->n_bytes)) {
1073 mask_len = field_len(mf, mask);
1074 len = MAX(len, mask_len);
1080 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1081 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1083 mf_set_flow_value(const struct mf_field *mf,
1084 const union mf_value *value, struct flow *flow)
1088 flow->dp_hash = ntohl(value->be32);
1091 flow->recirc_id = ntohl(value->be32);
1094 flow->conj_id = ntohl(value->be32);
1097 flow->tunnel.tun_id = value->be64;
1100 flow->tunnel.ip_src = value->be32;
1103 flow->tunnel.ip_dst = value->be32;
1106 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1109 case MFF_TUN_GBP_ID:
1110 flow->tunnel.gbp_id = value->be16;
1112 case MFF_TUN_GBP_FLAGS:
1113 flow->tunnel.gbp_flags = value->u8;
1116 flow->tunnel.ip_tos = value->u8;
1119 flow->tunnel.ip_ttl = value->u8;
1121 CASE_MFF_TUN_METADATA:
1122 tun_metadata_write(&flow->tunnel.metadata, mf, value);
1125 flow->metadata = value->be64;
1129 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1132 case MFF_IN_PORT_OXM:
1133 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1135 case MFF_ACTSET_OUTPUT:
1136 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1139 case MFF_SKB_PRIORITY:
1140 flow->skb_priority = ntohl(value->be32);
1144 flow->pkt_mark = ntohl(value->be32);
1148 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1152 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1156 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1160 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1164 flow->dl_type = value->be16;
1168 flow->vlan_tci = value->be16;
1172 flow_set_dl_vlan(flow, value->be16);
1175 flow_set_vlan_vid(flow, value->be16);
1178 case MFF_DL_VLAN_PCP:
1180 flow_set_vlan_pcp(flow, value->u8);
1183 case MFF_MPLS_LABEL:
1184 flow_set_mpls_label(flow, 0, value->be32);
1188 flow_set_mpls_tc(flow, 0, value->u8);
1192 flow_set_mpls_bos(flow, 0, value->u8);
1196 flow->nw_src = value->be32;
1200 flow->nw_dst = value->be32;
1204 flow->ipv6_src = value->ipv6;
1208 flow->ipv6_dst = value->ipv6;
1211 case MFF_IPV6_LABEL:
1212 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1216 flow->nw_proto = value->u8;
1220 flow->nw_tos &= ~IP_DSCP_MASK;
1221 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1224 case MFF_IP_DSCP_SHIFTED:
1225 flow->nw_tos &= ~IP_DSCP_MASK;
1226 flow->nw_tos |= value->u8 << 2;
1230 flow->nw_tos &= ~IP_ECN_MASK;
1231 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1235 flow->nw_ttl = value->u8;
1239 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1243 flow->nw_proto = ntohs(value->be16);
1247 flow->nw_src = value->be32;
1251 flow->nw_dst = value->be32;
1256 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1261 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1267 flow->tp_src = value->be16;
1273 flow->tp_dst = value->be16;
1277 flow->tcp_flags = value->be16;
1280 case MFF_ICMPV4_TYPE:
1281 case MFF_ICMPV6_TYPE:
1282 flow->tp_src = htons(value->u8);
1285 case MFF_ICMPV4_CODE:
1286 case MFF_ICMPV6_CODE:
1287 flow->tp_dst = htons(value->u8);
1291 flow->nd_target = value->ipv6;
1300 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1301 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1304 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1308 for (i = 0; i < n; i++) {
1309 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1313 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1314 * for which 'mask' has a 0-bit keep their existing values. The caller is
1315 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1317 mf_set_flow_value_masked(const struct mf_field *field,
1318 const union mf_value *value,
1319 const union mf_value *mask,
1324 mf_get_value(field, flow, &tmp);
1325 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1326 (uint8_t *) &tmp, field->n_bytes);
1327 mf_set_flow_value(field, &tmp, flow);
1330 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1332 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1335 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1337 union mf_value value;
1339 mf_get_value(mf, flow, &value);
1340 return is_all_zeros(&value, mf->n_bytes);
1343 /* Makes 'match' wildcard field 'mf'.
1345 * The caller is responsible for ensuring that 'match' meets 'mf''s
1348 mf_set_wild(const struct mf_field *mf, struct match *match)
1352 match->flow.dp_hash = 0;
1353 match->wc.masks.dp_hash = 0;
1356 match->flow.recirc_id = 0;
1357 match->wc.masks.recirc_id = 0;
1360 match->flow.conj_id = 0;
1361 match->wc.masks.conj_id = 0;
1364 match_set_tun_id_masked(match, htonll(0), htonll(0));
1367 match_set_tun_src_masked(match, htonl(0), htonl(0));
1370 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1373 match_set_tun_flags_masked(match, 0, 0);
1375 case MFF_TUN_GBP_ID:
1376 match_set_tun_gbp_id_masked(match, 0, 0);
1378 case MFF_TUN_GBP_FLAGS:
1379 match_set_tun_gbp_flags_masked(match, 0, 0);
1382 match_set_tun_tos_masked(match, 0, 0);
1385 match_set_tun_ttl_masked(match, 0, 0);
1387 CASE_MFF_TUN_METADATA:
1388 tun_metadata_set_match(mf, NULL, NULL, match);
1392 match_set_metadata_masked(match, htonll(0), htonll(0));
1396 case MFF_IN_PORT_OXM:
1397 match->flow.in_port.ofp_port = 0;
1398 match->wc.masks.in_port.ofp_port = 0;
1400 case MFF_ACTSET_OUTPUT:
1401 match->flow.actset_output = 0;
1402 match->wc.masks.actset_output = 0;
1405 case MFF_SKB_PRIORITY:
1406 match->flow.skb_priority = 0;
1407 match->wc.masks.skb_priority = 0;
1411 match->flow.pkt_mark = 0;
1412 match->wc.masks.pkt_mark = 0;
1416 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1420 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1424 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1425 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1429 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1430 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1434 match->flow.dl_type = htons(0);
1435 match->wc.masks.dl_type = htons(0);
1439 match_set_dl_tci_masked(match, htons(0), htons(0));
1444 match_set_any_vid(match);
1447 case MFF_DL_VLAN_PCP:
1449 match_set_any_pcp(match);
1452 case MFF_MPLS_LABEL:
1453 match_set_any_mpls_label(match, 0);
1457 match_set_any_mpls_tc(match, 0);
1461 match_set_any_mpls_bos(match, 0);
1466 match_set_nw_src_masked(match, htonl(0), htonl(0));
1471 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1475 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1476 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1480 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1481 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1484 case MFF_IPV6_LABEL:
1485 match->wc.masks.ipv6_label = htonl(0);
1486 match->flow.ipv6_label = htonl(0);
1490 match->wc.masks.nw_proto = 0;
1491 match->flow.nw_proto = 0;
1495 case MFF_IP_DSCP_SHIFTED:
1496 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1497 match->flow.nw_tos &= ~IP_DSCP_MASK;
1501 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1502 match->flow.nw_tos &= ~IP_ECN_MASK;
1506 match->wc.masks.nw_ttl = 0;
1507 match->flow.nw_ttl = 0;
1511 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1512 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1516 match->wc.masks.nw_proto = 0;
1517 match->flow.nw_proto = 0;
1522 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1523 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1528 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1529 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1535 case MFF_ICMPV4_TYPE:
1536 case MFF_ICMPV6_TYPE:
1537 match->wc.masks.tp_src = htons(0);
1538 match->flow.tp_src = htons(0);
1544 case MFF_ICMPV4_CODE:
1545 case MFF_ICMPV6_CODE:
1546 match->wc.masks.tp_dst = htons(0);
1547 match->flow.tp_dst = htons(0);
1551 match->wc.masks.tcp_flags = htons(0);
1552 match->flow.tcp_flags = htons(0);
1556 memset(&match->wc.masks.nd_target, 0,
1557 sizeof match->wc.masks.nd_target);
1558 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1567 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1568 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1569 * with a 1-bit indicating that the corresponding value bit must match and a
1570 * 0-bit indicating a don't-care.
1572 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1573 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1574 * call is equivalent to mf_set_wild(mf, match).
1576 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1577 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1578 enum ofputil_protocol
1579 mf_set(const struct mf_field *mf,
1580 const union mf_value *value, const union mf_value *mask,
1581 struct match *match)
1583 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1584 mf_set_value(mf, value, match);
1585 return mf->usable_protocols_exact;
1586 } else if (is_all_zeros(mask, mf->n_bytes)) {
1587 mf_set_wild(mf, match);
1588 return OFPUTIL_P_ANY;
1595 case MFF_IN_PORT_OXM:
1596 case MFF_ACTSET_OUTPUT:
1597 case MFF_SKB_PRIORITY:
1600 case MFF_DL_VLAN_PCP:
1602 case MFF_MPLS_LABEL:
1608 case MFF_IP_DSCP_SHIFTED:
1611 case MFF_ICMPV4_TYPE:
1612 case MFF_ICMPV4_CODE:
1613 case MFF_ICMPV6_TYPE:
1614 case MFF_ICMPV6_CODE:
1615 return OFPUTIL_P_NONE;
1618 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1621 match_set_tun_id_masked(match, value->be64, mask->be64);
1624 match_set_tun_src_masked(match, value->be32, mask->be32);
1627 match_set_tun_dst_masked(match, value->be32, mask->be32);
1630 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1632 case MFF_TUN_GBP_ID:
1633 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1635 case MFF_TUN_GBP_FLAGS:
1636 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1639 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1642 match_set_tun_tos_masked(match, value->u8, mask->u8);
1644 CASE_MFF_TUN_METADATA:
1645 tun_metadata_set_match(mf, value, mask, match);
1649 match_set_metadata_masked(match, value->be64, mask->be64);
1653 match_set_reg_masked(match, mf->id - MFF_REG0,
1654 ntohl(value->be32), ntohl(mask->be32));
1658 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1659 ntohll(value->be64), ntohll(mask->be64));
1663 match_set_pkt_mark_masked(match, ntohl(value->be32),
1668 match_set_dl_dst_masked(match, value->mac, mask->mac);
1672 match_set_dl_src_masked(match, value->mac, mask->mac);
1677 match_set_arp_sha_masked(match, value->mac, mask->mac);
1682 match_set_arp_tha_masked(match, value->mac, mask->mac);
1686 match_set_dl_tci_masked(match, value->be16, mask->be16);
1690 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1694 match_set_nw_src_masked(match, value->be32, mask->be32);
1698 match_set_nw_dst_masked(match, value->be32, mask->be32);
1702 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1706 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1709 case MFF_IPV6_LABEL:
1710 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1711 mf_set_value(mf, value, match);
1713 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1718 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1722 match_set_nw_frag_masked(match, value->u8, mask->u8);
1726 match_set_nw_src_masked(match, value->be32, mask->be32);
1730 match_set_nw_dst_masked(match, value->be32, mask->be32);
1736 match_set_tp_src_masked(match, value->be16, mask->be16);
1742 match_set_tp_dst_masked(match, value->be16, mask->be16);
1746 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1754 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1755 || ip_is_cidr(mask->be32))
1756 ? mf->usable_protocols_cidr
1757 : mf->usable_protocols_bitwise);
1761 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1765 VLOG_WARN_RL(&rl, "unknown %s field", type);
1766 return OFPERR_OFPBAC_BAD_SET_TYPE;
1767 } else if (!sf->n_bits) {
1768 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1769 return OFPERR_OFPBAC_BAD_SET_LEN;
1770 } else if (sf->ofs >= sf->field->n_bits) {
1771 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1772 sf->ofs, sf->field->n_bits, type, sf->field->name);
1773 return OFPERR_OFPBAC_BAD_SET_LEN;
1774 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1775 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1776 "of %s field %s", sf->ofs, sf->n_bits,
1777 sf->field->n_bits, type, sf->field->name);
1778 return OFPERR_OFPBAC_BAD_SET_LEN;
1779 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1780 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1781 type, sf->field->name);
1782 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1788 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1789 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1792 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1794 return mf_check__(sf, flow, "source");
1797 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1798 * if so, otherwise an OpenFlow error code (e.g. as returned by
1801 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1803 int error = mf_check__(sf, flow, "destination");
1804 if (!error && !sf->field->writable) {
1805 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1807 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1812 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1813 * 'value' and 'mask', respectively. */
1815 mf_get(const struct mf_field *mf, const struct match *match,
1816 union mf_value *value, union mf_value *mask)
1818 mf_get_value(mf, &match->flow, value);
1819 mf_get_mask(mf, &match->wc, mask);
1823 mf_from_integer_string(const struct mf_field *mf, const char *s,
1824 uint8_t *valuep, uint8_t *maskp)
1827 const char *err_str = "";
1830 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1831 if (err || (*tail != '\0' && *tail != '/')) {
1837 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1838 if (err || *tail != '\0') {
1843 memset(maskp, 0xff, mf->n_bytes);
1849 if (err == ERANGE) {
1850 return xasprintf("%s: %s too large for %u-byte field %s",
1851 s, err_str, mf->n_bytes, mf->name);
1853 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1858 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1859 uint8_t mac[ETH_ADDR_LEN],
1860 uint8_t mask[ETH_ADDR_LEN])
1864 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1867 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1868 && n == strlen(s)) {
1869 memset(mask, 0xff, ETH_ADDR_LEN);
1874 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1875 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1876 && n == strlen(s)) {
1880 return xasprintf("%s: invalid Ethernet address", s);
1884 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1885 ovs_be32 *ip, ovs_be32 *mask)
1889 ovs_assert(mf->n_bytes == sizeof *ip);
1891 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1892 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1894 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1895 if (prefix <= 0 || prefix > 32) {
1896 return xasprintf("%s: network prefix bits not between 0 and "
1899 *mask = be32_prefix_mask(prefix);
1900 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1901 *mask = OVS_BE32_MAX;
1903 return xasprintf("%s: invalid IP address", s);
1909 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1910 struct in6_addr *value, struct in6_addr *mask)
1912 char *str = xstrdup(s);
1913 char *save_ptr = NULL;
1914 const char *name, *netmask;
1917 ovs_assert(mf->n_bytes == sizeof *value);
1919 name = strtok_r(str, "/", &save_ptr);
1920 retval = name ? lookup_ipv6(name, value) : EINVAL;
1924 err = xasprintf("%s: could not convert to IPv6 address", str);
1930 netmask = strtok_r(NULL, "/", &save_ptr);
1932 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1933 int prefix = atoi(netmask);
1934 if (prefix <= 0 || prefix > 128) {
1936 return xasprintf("%s: prefix bits not between 1 and 128", s);
1938 *mask = ipv6_create_mask(prefix);
1942 *mask = in6addr_exact;
1950 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1951 ovs_be16 *valuep, ovs_be16 *maskp)
1955 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1957 if (ofputil_port_from_string(s, &port)) {
1958 *valuep = htons(ofp_to_u16(port));
1959 *maskp = OVS_BE16_MAX;
1962 return xasprintf("%s: port value out of range for %s", s, mf->name);
1966 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1967 ovs_be32 *valuep, ovs_be32 *maskp)
1971 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1972 if (ofputil_port_from_string(s, &port)) {
1973 *valuep = ofputil_port_to_ofp11(port);
1974 *maskp = OVS_BE32_MAX;
1977 return xasprintf("%s: port value out of range for %s", s, mf->name);
1980 struct frag_handling {
1986 static const struct frag_handling all_frags[] = {
1987 #define A FLOW_NW_FRAG_ANY
1988 #define L FLOW_NW_FRAG_LATER
1989 /* name mask value */
1992 { "first", A|L, A },
1993 { "later", A|L, A|L },
1998 { "not_later", L, 0 },
2005 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2007 const struct frag_handling *h;
2009 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2010 if (!strcasecmp(s, h->name)) {
2011 /* We force the upper bits of the mask on to make mf_parse_value()
2012 * happy (otherwise it will never think it's an exact match.) */
2013 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2019 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2020 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2024 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2025 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2030 uint32_t flags, mask;
2032 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2033 &flags, ntohs(allowed), maskp ? &mask : NULL);
2038 *flagsp = htons(flags);
2040 *maskp = htons(mask);
2047 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2049 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2050 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2054 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2056 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2057 htons(FLOW_TNL_PUB_F_MASK), maskp);
2060 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2061 * NULL if successful, otherwise a malloc()'d string describing the error. */
2063 mf_parse(const struct mf_field *mf, const char *s,
2064 union mf_value *value, union mf_value *mask)
2068 if (!strcmp(s, "*")) {
2069 memset(value, 0, mf->n_bytes);
2070 memset(mask, 0, mf->n_bytes);
2074 switch (mf->string) {
2076 case MFS_HEXADECIMAL:
2077 error = mf_from_integer_string(mf, s,
2078 (uint8_t *) value, (uint8_t *) mask);
2082 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2086 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2090 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2094 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2097 case MFS_OFP_PORT_OXM:
2098 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2102 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2106 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2107 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2111 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2112 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2119 if (!error && !mf_is_mask_valid(mf, mask)) {
2120 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2125 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2126 * successful, otherwise a malloc()'d string describing the error. */
2128 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2130 union mf_value mask;
2133 error = mf_parse(mf, s, value, &mask);
2138 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2139 return xasprintf("%s: wildcards not allowed here", s);
2145 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2146 const uint8_t *maskp, struct ds *s)
2148 if (mf->string == MFS_HEXADECIMAL) {
2149 ds_put_hex(s, valuep, mf->n_bytes);
2151 unsigned long long int integer = 0;
2154 ovs_assert(mf->n_bytes <= 8);
2155 for (i = 0; i < mf->n_bytes; i++) {
2156 integer = (integer << 8) | valuep[i];
2158 ds_put_format(s, "%lld", integer);
2162 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2163 * not sure that that a bit-mask written in decimal is ever easier to
2164 * understand than the same bit-mask written in hexadecimal. */
2165 ds_put_char(s, '/');
2166 ds_put_hex(s, maskp, mf->n_bytes);
2171 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2173 const struct frag_handling *h;
2175 mask &= FLOW_NW_FRAG_MASK;
2178 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2179 if (value == h->value && mask == h->mask) {
2180 ds_put_cstr(s, h->name);
2184 ds_put_cstr(s, "<error>");
2188 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2190 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2191 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2195 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2197 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2198 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2201 /* Appends to 's' a string representation of field 'mf' whose value is in
2202 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2204 mf_format(const struct mf_field *mf,
2205 const union mf_value *value, const union mf_value *mask,
2209 if (is_all_zeros(mask, mf->n_bytes)) {
2210 ds_put_cstr(s, "ANY");
2212 } else if (is_all_ones(mask, mf->n_bytes)) {
2217 switch (mf->string) {
2218 case MFS_OFP_PORT_OXM:
2221 ofputil_port_from_ofp11(value->be32, &port);
2222 ofputil_format_port(port, s);
2228 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2233 case MFS_HEXADECIMAL:
2234 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2238 eth_format_masked(value->mac, mask->mac, s);
2242 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2246 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2250 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2254 mf_format_tnl_flags_string(value->be16,
2255 mask ? mask->be16 : OVS_BE16_MAX, s);
2259 mf_format_tcp_flags_string(value->be16,
2260 mask ? mask->be16 : OVS_BE16_MAX, s);
2268 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2269 * least-significant bits in 'x'.
2272 mf_write_subfield_flow(const struct mf_subfield *sf,
2273 const union mf_subvalue *x, struct flow *flow)
2275 const struct mf_field *field = sf->field;
2276 union mf_value value;
2278 mf_get_value(field, flow, &value);
2279 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2280 sf->ofs, sf->n_bits);
2281 mf_set_flow_value(field, &value, flow);
2284 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2285 * least-significant bits in 'x'.
2288 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2289 struct match *match)
2291 const struct mf_field *field = sf->field;
2292 union mf_value value, mask;
2294 mf_get(field, match, &value, &mask);
2295 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2296 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2297 mf_set(field, &value, &mask, match);
2300 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2301 * 'match' in the correspond positions. */
2303 mf_mask_subfield(const struct mf_field *field,
2304 const union mf_subvalue *v,
2305 const union mf_subvalue *m,
2306 struct match *match)
2308 union mf_value value, mask;
2310 mf_get(field, match, &value, &mask);
2311 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2312 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2313 mf_set(field, &value, &mask, match);
2316 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2317 * reading 'flow', e.g. as checked by mf_check_src(). */
2319 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2320 union mf_subvalue *x)
2322 union mf_value value;
2324 mf_get_value(sf->field, flow, &value);
2326 memset(x, 0, sizeof *x);
2327 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2332 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2333 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2336 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2338 union mf_value value;
2340 mf_get_value(sf->field, flow, &value);
2341 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2345 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2347 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2351 field_array_set(enum mf_field_id id, const union mf_value *value,
2352 struct field_array *fa)
2354 ovs_assert(id < MFF_N_IDS);
2355 bitmap_set1(fa->used.bm, id);
2356 fa->value[id] = *value;