2 * Copyright (c) 2011, 2012, 2013 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"
28 #include "ofp-errors.h"
30 #include "ovs-thread.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow
);
40 #define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 static const struct mf_field mf_fields
[MFF_N_IDS
] = {
50 MFF_TUN_ID
, "tun_id", NULL
,
56 NXM_NX_TUN_ID
, "NXM_NX_TUN_ID",
57 OXM_OF_TUNNEL_ID
, "OXM_OF_TUNNEL_ID",
59 MFF_TUN_SRC
, "tun_src", NULL
,
65 NXM_NX_TUN_IPV4_SRC
, "NXM_NX_TUN_IPV4_SRC",
66 NXM_NX_TUN_IPV4_SRC
, "NXM_NX_TUN_IPV4_SRC",
68 MFF_TUN_DST
, "tun_dst", NULL
,
74 NXM_NX_TUN_IPV4_DST
, "NXM_NX_TUN_IPV4_DST",
75 NXM_NX_TUN_IPV4_DST
, "NXM_NX_TUN_IPV4_DST",
77 MFF_TUN_FLAGS
, "tun_flags", NULL
,
86 MFF_TUN_TOS
, "tun_tos", NULL
,
95 MFF_TUN_TTL
, "tun_ttl", NULL
,
104 MFF_METADATA
, "metadata", NULL
,
105 MF_FIELD_SIZES(be64
),
110 OXM_OF_METADATA
, "OXM_OF_METADATA",
111 OXM_OF_METADATA
, "OXM_OF_METADATA",
113 MFF_IN_PORT
, "in_port", NULL
,
114 MF_FIELD_SIZES(be16
),
119 NXM_OF_IN_PORT
, "NXM_OF_IN_PORT",
120 NXM_OF_IN_PORT
, "NXM_OF_IN_PORT",
122 MFF_IN_PORT_OXM
, "in_port_oxm", NULL
,
123 MF_FIELD_SIZES(be32
),
128 OXM_OF_IN_PORT
, "OXM_OF_IN_PORT",
129 OXM_OF_IN_PORT
, "OXM_OF_IN_PORT",
131 MFF_SKB_PRIORITY
, "skb_priority", NULL
,
132 MF_FIELD_SIZES(be32
),
140 MFF_PKT_MARK
, "pkt_mark", NULL
,
141 MF_FIELD_SIZES(be32
),
146 NXM_NX_PKT_MARK
, "NXM_NX_PKT_MARK",
147 NXM_NX_PKT_MARK
, "NXM_NX_PKT_MARK",
150 #define REGISTER(IDX) \
152 MFF_REG##IDX, "reg" #IDX, NULL, \
153 MF_FIELD_SIZES(be32), \
158 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
159 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
194 MFF_ETH_SRC
, "eth_src", "dl_src",
200 NXM_OF_ETH_SRC
, "NXM_OF_ETH_SRC",
201 OXM_OF_ETH_SRC
, "OXM_OF_ETH_SRC",
203 MFF_ETH_DST
, "eth_dst", "dl_dst",
209 NXM_OF_ETH_DST
, "NXM_OF_ETH_DST",
210 OXM_OF_ETH_DST
, "OXM_OF_ETH_DST",
212 MFF_ETH_TYPE
, "eth_type", "dl_type",
213 MF_FIELD_SIZES(be16
),
218 NXM_OF_ETH_TYPE
, "NXM_OF_ETH_TYPE",
219 OXM_OF_ETH_TYPE
, "OXM_OF_ETH_TYPE",
223 MFF_VLAN_TCI
, "vlan_tci", NULL
,
224 MF_FIELD_SIZES(be16
),
229 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
230 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
232 MFF_DL_VLAN
, "dl_vlan", NULL
,
233 sizeof(ovs_be16
), 12,
241 MFF_VLAN_VID
, "vlan_vid", NULL
,
242 sizeof(ovs_be16
), 12,
247 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
248 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
250 MFF_DL_VLAN_PCP
, "dl_vlan_pcp", NULL
,
259 MFF_VLAN_PCP
, "vlan_pcp", NULL
,
265 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
266 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
273 MFF_MPLS_LABEL
, "mpls_label", NULL
,
279 OXM_OF_MPLS_LABEL
, "OXM_OF_MPLS_LABEL",
280 OXM_OF_MPLS_LABEL
, "OXM_OF_MPLS_LABEL",
282 MFF_MPLS_TC
, "mpls_tc", NULL
,
288 OXM_OF_MPLS_TC
, "OXM_OF_MPLS_TC",
289 OXM_OF_MPLS_TC
, "OXM_OF_MPLS_TC",
291 MFF_MPLS_BOS
, "mpls_bos", NULL
,
297 OXM_OF_MPLS_BOS
, "OXM_OF_MPLS_BOS",
298 OXM_OF_MPLS_BOS
, "OXM_OF_MPLS_BOS",
306 MFF_IPV4_SRC
, "ip_src", "nw_src",
307 MF_FIELD_SIZES(be32
),
312 NXM_OF_IP_SRC
, "NXM_OF_IP_SRC",
313 OXM_OF_IPV4_SRC
, "OXM_OF_IPV4_SRC",
315 MFF_IPV4_DST
, "ip_dst", "nw_dst",
316 MF_FIELD_SIZES(be32
),
321 NXM_OF_IP_DST
, "NXM_OF_IP_DST",
322 OXM_OF_IPV4_DST
, "OXM_OF_IPV4_DST",
326 MFF_IPV6_SRC
, "ipv6_src", NULL
,
327 MF_FIELD_SIZES(ipv6
),
332 NXM_NX_IPV6_SRC
, "NXM_NX_IPV6_SRC",
333 OXM_OF_IPV6_SRC
, "OXM_OF_IPV6_SRC",
335 MFF_IPV6_DST
, "ipv6_dst", NULL
,
336 MF_FIELD_SIZES(ipv6
),
341 NXM_NX_IPV6_DST
, "NXM_NX_IPV6_DST",
342 OXM_OF_IPV6_DST
, "OXM_OF_IPV6_DST",
345 MFF_IPV6_LABEL
, "ipv6_label", NULL
,
351 NXM_NX_IPV6_LABEL
, "NXM_NX_IPV6_LABEL",
352 OXM_OF_IPV6_FLABEL
, "OXM_OF_IPV6_FLABEL",
356 MFF_IP_PROTO
, "nw_proto", NULL
,
362 NXM_OF_IP_PROTO
, "NXM_OF_IP_PROTO",
363 OXM_OF_IP_PROTO
, "OXM_OF_IP_PROTO",
365 MFF_IP_DSCP
, "nw_tos", NULL
,
371 NXM_OF_IP_TOS
, "NXM_OF_IP_TOS",
372 NXM_OF_IP_TOS
, "NXM_OF_IP_TOS",
374 MFF_IP_DSCP_SHIFTED
, "nw_tos_shifted", NULL
,
380 OXM_OF_IP_DSCP
, "OXM_OF_IP_DSCP",
381 OXM_OF_IP_DSCP
, "OXM_OF_IP_DSCP",
383 MFF_IP_ECN
, "nw_ecn", NULL
,
389 NXM_NX_IP_ECN
, "NXM_NX_IP_ECN",
390 OXM_OF_IP_ECN
, "OXM_OF_IP_ECN",
392 MFF_IP_TTL
, "nw_ttl", NULL
,
398 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
399 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
401 MFF_IP_FRAG
, "ip_frag", NULL
,
407 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
408 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
412 MFF_ARP_OP
, "arp_op", NULL
,
413 MF_FIELD_SIZES(be16
),
418 NXM_OF_ARP_OP
, "NXM_OF_ARP_OP",
419 OXM_OF_ARP_OP
, "OXM_OF_ARP_OP",
421 MFF_ARP_SPA
, "arp_spa", NULL
,
422 MF_FIELD_SIZES(be32
),
427 NXM_OF_ARP_SPA
, "NXM_OF_ARP_SPA",
428 OXM_OF_ARP_SPA
, "OXM_OF_ARP_SPA",
430 MFF_ARP_TPA
, "arp_tpa", NULL
,
431 MF_FIELD_SIZES(be32
),
436 NXM_OF_ARP_TPA
, "NXM_OF_ARP_TPA",
437 OXM_OF_ARP_TPA
, "OXM_OF_ARP_TPA",
439 MFF_ARP_SHA
, "arp_sha", NULL
,
445 NXM_NX_ARP_SHA
, "NXM_NX_ARP_SHA",
446 OXM_OF_ARP_SHA
, "OXM_OF_ARP_SHA",
448 MFF_ARP_THA
, "arp_tha", NULL
,
454 NXM_NX_ARP_THA
, "NXM_NX_ARP_THA",
455 OXM_OF_ARP_THA
, "OXM_OF_ARP_THA",
463 MFF_TCP_SRC
, "tcp_src", "tp_src",
464 MF_FIELD_SIZES(be16
),
469 NXM_OF_TCP_SRC
, "NXM_OF_TCP_SRC",
470 OXM_OF_TCP_SRC
, "OXM_OF_TCP_SRC",
472 MFF_TCP_DST
, "tcp_dst", "tp_dst",
473 MF_FIELD_SIZES(be16
),
478 NXM_OF_TCP_DST
, "NXM_OF_TCP_DST",
479 OXM_OF_TCP_DST
, "OXM_OF_TCP_DST",
483 MFF_UDP_SRC
, "udp_src", NULL
,
484 MF_FIELD_SIZES(be16
),
489 NXM_OF_UDP_SRC
, "NXM_OF_UDP_SRC",
490 OXM_OF_UDP_SRC
, "OXM_OF_UDP_SRC",
492 MFF_UDP_DST
, "udp_dst", NULL
,
493 MF_FIELD_SIZES(be16
),
498 NXM_OF_UDP_DST
, "NXM_OF_UDP_DST",
499 OXM_OF_UDP_DST
, "OXM_OF_UDP_DST",
503 MFF_ICMPV4_TYPE
, "icmp_type", NULL
,
509 NXM_OF_ICMP_TYPE
, "NXM_OF_ICMP_TYPE",
510 OXM_OF_ICMPV4_TYPE
, "OXM_OF_ICMPV4_TYPE",
512 MFF_ICMPV4_CODE
, "icmp_code", NULL
,
518 NXM_OF_ICMP_CODE
, "NXM_OF_ICMP_CODE",
519 OXM_OF_ICMPV4_CODE
, "OXM_OF_ICMPV4_CODE",
523 MFF_ICMPV6_TYPE
, "icmpv6_type", NULL
,
529 NXM_NX_ICMPV6_TYPE
, "NXM_NX_ICMPV6_TYPE",
530 OXM_OF_ICMPV6_TYPE
, "OXM_OF_ICMPV6_TYPE",
532 MFF_ICMPV6_CODE
, "icmpv6_code", NULL
,
538 NXM_NX_ICMPV6_CODE
, "NXM_NX_ICMPV6_CODE",
539 OXM_OF_ICMPV6_CODE
, "OXM_OF_ICMPV6_CODE",
547 MFF_ND_TARGET
, "nd_target", NULL
,
548 MF_FIELD_SIZES(ipv6
),
553 NXM_NX_ND_TARGET
, "NXM_NX_ND_TARGET",
554 OXM_OF_IPV6_ND_TARGET
, "OXM_OF_IPV6_ND_TARGET",
556 MFF_ND_SLL
, "nd_sll", NULL
,
562 NXM_NX_ND_SLL
, "NXM_NX_ND_SLL",
563 OXM_OF_IPV6_ND_SLL
, "OXM_OF_IPV6_ND_SLL",
565 MFF_ND_TLL
, "nd_tll", NULL
,
571 NXM_NX_ND_TLL
, "NXM_NX_ND_TLL",
572 OXM_OF_IPV6_ND_TLL
, "OXM_OF_IPV6_ND_TLL",
576 /* Maps an NXM or OXM header value to an mf_field. */
578 struct hmap_node hmap_node
; /* In 'all_fields' hmap. */
579 uint32_t header
; /* NXM or OXM header value. */
580 const struct mf_field
*mf
;
583 /* Contains 'struct nxm_field's. */
584 static struct hmap all_fields
;
586 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
587 static struct shash mf_by_name
;
589 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
590 * controller and so there's not much point in showing a lot of them. */
591 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
593 const struct mf_field
*mf_from_nxm_header__(uint32_t header
);
594 static void nxm_init(void);
596 /* Returns the field with the given 'id'. */
597 const struct mf_field
*
598 mf_from_id(enum mf_field_id id
)
600 ovs_assert((unsigned int) id
< MFF_N_IDS
);
601 return &mf_fields
[id
];
604 /* Returns the field with the given 'name', or a null pointer if no field has
606 const struct mf_field
*
607 mf_from_name(const char *name
)
610 return shash_find_data(&mf_by_name
, name
);
614 add_nxm_field(uint32_t header
, const struct mf_field
*mf
)
618 f
= xmalloc(sizeof *f
);
619 hmap_insert(&all_fields
, &f
->hmap_node
, hash_int(header
, 0));
625 nxm_init_add_field(const struct mf_field
*mf
, uint32_t header
)
628 ovs_assert(!mf_from_nxm_header__(header
));
629 add_nxm_field(header
, mf
);
630 if (mf
->maskable
!= MFM_NONE
) {
631 add_nxm_field(NXM_MAKE_WILD_HEADER(header
), mf
);
639 const struct mf_field
*mf
;
641 hmap_init(&all_fields
);
642 shash_init(&mf_by_name
);
643 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
644 nxm_init_add_field(mf
, mf
->nxm_header
);
645 if (mf
->oxm_header
!= mf
->nxm_header
) {
646 nxm_init_add_field(mf
, mf
->oxm_header
);
649 shash_add_once(&mf_by_name
, mf
->name
, mf
);
650 if (mf
->extra_name
) {
651 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
659 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
660 pthread_once(&once
, nxm_do_init
);
663 const struct mf_field
*
664 mf_from_nxm_header(uint32_t header
)
667 return mf_from_nxm_header__(header
);
670 const struct mf_field
*
671 mf_from_nxm_header__(uint32_t header
)
673 const struct nxm_field
*f
;
675 HMAP_FOR_EACH_IN_BUCKET (f
, hmap_node
, hash_int(header
, 0), &all_fields
) {
676 if (f
->header
== header
) {
684 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
685 * specifies at least one bit in the field.
687 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
688 * meets 'mf''s prerequisites. */
690 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
694 return !wc
->masks
.tunnel
.ip_src
;
696 return !wc
->masks
.tunnel
.ip_dst
;
701 return !wc
->masks
.tunnel
.tun_id
;
703 return !wc
->masks
.metadata
;
705 case MFF_IN_PORT_OXM
:
706 return !wc
->masks
.in_port
.ofp_port
;
707 case MFF_SKB_PRIORITY
:
708 return !wc
->masks
.skb_priority
;
710 return !wc
->masks
.pkt_mark
;
712 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
715 return eth_addr_is_zero(wc
->masks
.dl_src
);
717 return eth_addr_is_zero(wc
->masks
.dl_dst
);
719 return !wc
->masks
.dl_type
;
723 return eth_addr_is_zero(wc
->masks
.arp_sha
);
727 return eth_addr_is_zero(wc
->masks
.arp_tha
);
730 return !wc
->masks
.vlan_tci
;
732 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
));
734 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
735 case MFF_DL_VLAN_PCP
:
737 return !(wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
));
740 return !(wc
->masks
.mpls_lse
& htonl(MPLS_LABEL_MASK
));
742 return !(wc
->masks
.mpls_lse
& htonl(MPLS_TC_MASK
));
744 return !(wc
->masks
.mpls_lse
& htonl(MPLS_BOS_MASK
));
747 return !wc
->masks
.nw_src
;
749 return !wc
->masks
.nw_dst
;
752 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
754 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
757 return !wc
->masks
.ipv6_label
;
760 return !wc
->masks
.nw_proto
;
762 case MFF_IP_DSCP_SHIFTED
:
763 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
765 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
767 return !wc
->masks
.nw_ttl
;
770 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
773 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
776 return !wc
->masks
.nw_proto
;
778 return !wc
->masks
.nw_src
;
780 return !wc
->masks
.nw_dst
;
784 case MFF_ICMPV4_TYPE
:
785 case MFF_ICMPV6_TYPE
:
786 return !wc
->masks
.tp_src
;
789 case MFF_ICMPV4_CODE
:
790 case MFF_ICMPV6_CODE
:
791 return !wc
->masks
.tp_dst
;
799 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
800 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
801 * purposes, or to 0 if it is wildcarded.
803 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
804 * meets 'mf''s prerequisites. */
806 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
807 union mf_value
*mask
)
809 mf_get_value(mf
, &wc
->masks
, mask
);
812 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
813 * if the mask is valid, false otherwise. */
815 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
817 switch (mf
->maskable
) {
819 return (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
) ||
820 is_all_ones((const uint8_t *) mask
, mf
->n_bytes
));
830 is_icmpv4(const struct flow
*flow
)
832 return (flow
->dl_type
== htons(ETH_TYPE_IP
)
833 && flow
->nw_proto
== IPPROTO_ICMP
);
837 is_icmpv6(const struct flow
*flow
)
839 return (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
840 && flow
->nw_proto
== IPPROTO_ICMPV6
);
843 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
845 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
847 switch (mf
->prereqs
) {
852 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
853 flow
->dl_type
== htons(ETH_TYPE_RARP
));
855 return flow
->dl_type
== htons(ETH_TYPE_IP
);
857 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
859 return (flow
->vlan_tci
& htons(VLAN_CFI
)) != 0;
861 return eth_type_mpls(flow
->dl_type
);
863 return is_ip_any(flow
);
866 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
;
868 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
;
870 return is_icmpv4(flow
);
872 return is_icmpv6(flow
);
875 return (is_icmpv6(flow
)
876 && flow
->tp_dst
== htons(0)
877 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
878 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
880 return (is_icmpv6(flow
)
881 && flow
->tp_dst
== htons(0)
882 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
884 return (is_icmpv6(flow
)
885 && flow
->tp_dst
== htons(0)
886 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
892 /* Returns true if 'value' may be a valid value *as part of a masked match*,
895 * A value is not rejected just because it is not valid for the field in
896 * question, but only if it doesn't make sense to test the bits in question at
897 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
898 * without the VLAN_CFI bit being set, but we can't reject those values because
899 * it is still legitimate to test just for those bits (see the documentation
900 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
901 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
903 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
914 case MFF_SKB_PRIORITY
:
935 case MFF_ICMPV4_TYPE
:
936 case MFF_ICMPV4_CODE
:
937 case MFF_ICMPV6_TYPE
:
938 case MFF_ICMPV6_CODE
:
944 case MFF_IN_PORT_OXM
: {
946 return !ofputil_port_from_ofp11(value
->be32
, &port
);
950 return !(value
->u8
& ~IP_DSCP_MASK
);
951 case MFF_IP_DSCP_SHIFTED
:
952 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
954 return !(value
->u8
& ~IP_ECN_MASK
);
956 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
959 return !(value
->be16
& htons(0xff00));
962 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
964 return !(value
->be16
& htons(VLAN_PCP_MASK
));
966 case MFF_DL_VLAN_PCP
:
968 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
971 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
974 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
977 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
980 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
988 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
989 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
991 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
992 union mf_value
*value
)
996 value
->be64
= flow
->tunnel
.tun_id
;
999 value
->be32
= flow
->tunnel
.ip_src
;
1002 value
->be32
= flow
->tunnel
.ip_dst
;
1005 value
->be16
= htons(flow
->tunnel
.flags
);
1008 value
->u8
= flow
->tunnel
.ip_ttl
;
1011 value
->u8
= flow
->tunnel
.ip_tos
;
1015 value
->be64
= flow
->metadata
;
1019 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
1021 case MFF_IN_PORT_OXM
:
1022 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
1025 case MFF_SKB_PRIORITY
:
1026 value
->be32
= htonl(flow
->skb_priority
);
1030 value
->be32
= htonl(flow
->pkt_mark
);
1034 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
1038 memcpy(value
->mac
, flow
->dl_src
, ETH_ADDR_LEN
);
1042 memcpy(value
->mac
, flow
->dl_dst
, ETH_ADDR_LEN
);
1046 value
->be16
= flow
->dl_type
;
1050 value
->be16
= flow
->vlan_tci
;
1054 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
1057 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
1060 case MFF_DL_VLAN_PCP
:
1062 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
1065 case MFF_MPLS_LABEL
:
1066 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
));
1070 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
);
1074 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
);
1078 value
->be32
= flow
->nw_src
;
1082 value
->be32
= flow
->nw_dst
;
1086 value
->ipv6
= flow
->ipv6_src
;
1090 value
->ipv6
= flow
->ipv6_dst
;
1093 case MFF_IPV6_LABEL
:
1094 value
->be32
= flow
->ipv6_label
;
1098 value
->u8
= flow
->nw_proto
;
1102 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
1105 case MFF_IP_DSCP_SHIFTED
:
1106 value
->u8
= flow
->nw_tos
>> 2;
1110 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
1114 value
->u8
= flow
->nw_ttl
;
1118 value
->u8
= flow
->nw_frag
;
1122 value
->be16
= htons(flow
->nw_proto
);
1126 value
->be32
= flow
->nw_src
;
1130 value
->be32
= flow
->nw_dst
;
1135 memcpy(value
->mac
, flow
->arp_sha
, ETH_ADDR_LEN
);
1140 memcpy(value
->mac
, flow
->arp_tha
, ETH_ADDR_LEN
);
1145 value
->be16
= flow
->tp_src
;
1150 value
->be16
= flow
->tp_dst
;
1153 case MFF_ICMPV4_TYPE
:
1154 case MFF_ICMPV6_TYPE
:
1155 value
->u8
= ntohs(flow
->tp_src
);
1158 case MFF_ICMPV4_CODE
:
1159 case MFF_ICMPV6_CODE
:
1160 value
->u8
= ntohs(flow
->tp_dst
);
1164 value
->ipv6
= flow
->nd_target
;
1173 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1174 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1177 mf_set_value(const struct mf_field
*mf
,
1178 const union mf_value
*value
, struct match
*match
)
1182 match_set_tun_id(match
, value
->be64
);
1185 match_set_tun_src(match
, value
->be32
);
1188 match_set_tun_dst(match
, value
->be32
);
1191 match_set_tun_flags(match
, ntohs(value
->be16
));
1194 match_set_tun_tos(match
, value
->u8
);
1197 match_set_tun_ttl(match
, value
->u8
);
1201 match_set_metadata(match
, value
->be64
);
1205 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
1208 case MFF_IN_PORT_OXM
: {
1210 ofputil_port_from_ofp11(value
->be32
, &port
);
1211 match_set_in_port(match
, port
);
1215 case MFF_SKB_PRIORITY
:
1216 match_set_skb_priority(match
, ntohl(value
->be32
));
1220 match_set_pkt_mark(match
, ntohl(value
->be32
));
1224 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1228 match_set_dl_src(match
, value
->mac
);
1232 match_set_dl_dst(match
, value
->mac
);
1236 match_set_dl_type(match
, value
->be16
);
1240 match_set_dl_tci(match
, value
->be16
);
1244 match_set_dl_vlan(match
, value
->be16
);
1247 match_set_vlan_vid(match
, value
->be16
);
1250 case MFF_DL_VLAN_PCP
:
1252 match_set_dl_vlan_pcp(match
, value
->u8
);
1255 case MFF_MPLS_LABEL
:
1256 match_set_mpls_label(match
, value
->be32
);
1260 match_set_mpls_tc(match
, value
->u8
);
1264 match_set_mpls_bos(match
, value
->u8
);
1268 match_set_nw_src(match
, value
->be32
);
1272 match_set_nw_dst(match
, value
->be32
);
1276 match_set_ipv6_src(match
, &value
->ipv6
);
1280 match_set_ipv6_dst(match
, &value
->ipv6
);
1283 case MFF_IPV6_LABEL
:
1284 match_set_ipv6_label(match
, value
->be32
);
1288 match_set_nw_proto(match
, value
->u8
);
1292 match_set_nw_dscp(match
, value
->u8
);
1295 case MFF_IP_DSCP_SHIFTED
:
1296 match_set_nw_dscp(match
, value
->u8
<< 2);
1300 match_set_nw_ecn(match
, value
->u8
);
1304 match_set_nw_ttl(match
, value
->u8
);
1308 match_set_nw_frag(match
, value
->u8
);
1312 match_set_nw_proto(match
, ntohs(value
->be16
));
1316 match_set_nw_src(match
, value
->be32
);
1320 match_set_nw_dst(match
, value
->be32
);
1325 match_set_arp_sha(match
, value
->mac
);
1330 match_set_arp_tha(match
, value
->mac
);
1335 match_set_tp_src(match
, value
->be16
);
1340 match_set_tp_dst(match
, value
->be16
);
1343 case MFF_ICMPV4_TYPE
:
1344 case MFF_ICMPV6_TYPE
:
1345 match_set_icmp_type(match
, value
->u8
);
1348 case MFF_ICMPV4_CODE
:
1349 case MFF_ICMPV6_CODE
:
1350 match_set_icmp_code(match
, value
->u8
);
1354 match_set_nd_target(match
, &value
->ipv6
);
1363 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1364 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1366 mf_set_flow_value(const struct mf_field
*mf
,
1367 const union mf_value
*value
, struct flow
*flow
)
1371 flow
->tunnel
.tun_id
= value
->be64
;
1374 flow
->tunnel
.ip_src
= value
->be32
;
1377 flow
->tunnel
.ip_dst
= value
->be32
;
1380 flow
->tunnel
.flags
= ntohs(value
->be16
);
1383 flow
->tunnel
.ip_tos
= value
->u8
;
1386 flow
->tunnel
.ip_ttl
= value
->u8
;
1390 flow
->metadata
= value
->be64
;
1394 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1397 case MFF_IN_PORT_OXM
: {
1399 ofputil_port_from_ofp11(value
->be32
, &port
);
1400 flow
->in_port
.ofp_port
= port
;
1404 case MFF_SKB_PRIORITY
:
1405 flow
->skb_priority
= ntohl(value
->be32
);
1409 flow
->pkt_mark
= ntohl(value
->be32
);
1413 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1417 memcpy(flow
->dl_src
, value
->mac
, ETH_ADDR_LEN
);
1421 memcpy(flow
->dl_dst
, value
->mac
, ETH_ADDR_LEN
);
1425 flow
->dl_type
= value
->be16
;
1429 flow
->vlan_tci
= value
->be16
;
1433 flow_set_dl_vlan(flow
, value
->be16
);
1436 flow_set_vlan_vid(flow
, value
->be16
);
1439 case MFF_DL_VLAN_PCP
:
1441 flow_set_vlan_pcp(flow
, value
->u8
);
1444 case MFF_MPLS_LABEL
:
1445 flow_set_mpls_label(flow
, value
->be32
);
1449 flow_set_mpls_tc(flow
, value
->u8
);
1453 flow_set_mpls_bos(flow
, value
->u8
);
1457 flow
->nw_src
= value
->be32
;
1461 flow
->nw_dst
= value
->be32
;
1465 flow
->ipv6_src
= value
->ipv6
;
1469 flow
->ipv6_dst
= value
->ipv6
;
1472 case MFF_IPV6_LABEL
:
1473 flow
->ipv6_label
= value
->be32
& ~htonl(IPV6_LABEL_MASK
);
1477 flow
->nw_proto
= value
->u8
;
1481 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1482 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1485 case MFF_IP_DSCP_SHIFTED
:
1486 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1487 flow
->nw_tos
|= value
->u8
<< 2;
1491 flow
->nw_tos
&= ~IP_ECN_MASK
;
1492 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1496 flow
->nw_ttl
= value
->u8
;
1500 flow
->nw_frag
&= value
->u8
;
1504 flow
->nw_proto
= ntohs(value
->be16
);
1508 flow
->nw_src
= value
->be32
;
1512 flow
->nw_dst
= value
->be32
;
1517 memcpy(flow
->arp_sha
, value
->mac
, ETH_ADDR_LEN
);
1522 memcpy(flow
->arp_tha
, value
->mac
, ETH_ADDR_LEN
);
1527 flow
->tp_src
= value
->be16
;
1532 flow
->tp_dst
= value
->be16
;
1535 case MFF_ICMPV4_TYPE
:
1536 case MFF_ICMPV6_TYPE
:
1537 flow
->tp_src
= htons(value
->u8
);
1540 case MFF_ICMPV4_CODE
:
1541 case MFF_ICMPV6_CODE
:
1542 flow
->tp_dst
= htons(value
->u8
);
1546 flow
->nd_target
= value
->ipv6
;
1555 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1557 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1560 mf_is_zero(const struct mf_field
*mf
, const struct flow
*flow
)
1562 union mf_value value
;
1564 mf_get_value(mf
, flow
, &value
);
1565 return is_all_zeros((const uint8_t *) &value
, mf
->n_bytes
);
1568 /* Makes 'match' wildcard field 'mf'.
1570 * The caller is responsible for ensuring that 'match' meets 'mf''s
1573 mf_set_wild(const struct mf_field
*mf
, struct match
*match
)
1577 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1580 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1583 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1586 match_set_tun_flags_masked(match
, 0, 0);
1589 match_set_tun_tos_masked(match
, 0, 0);
1592 match_set_tun_ttl_masked(match
, 0, 0);
1596 match_set_metadata_masked(match
, htonll(0), htonll(0));
1600 case MFF_IN_PORT_OXM
:
1601 match
->flow
.in_port
.ofp_port
= 0;
1602 match
->wc
.masks
.in_port
.ofp_port
= 0;
1605 case MFF_SKB_PRIORITY
:
1606 match
->flow
.skb_priority
= 0;
1607 match
->wc
.masks
.skb_priority
= 0;
1611 match
->flow
.pkt_mark
= 0;
1612 match
->wc
.masks
.pkt_mark
= 0;
1616 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1620 memset(match
->flow
.dl_src
, 0, ETH_ADDR_LEN
);
1621 memset(match
->wc
.masks
.dl_src
, 0, ETH_ADDR_LEN
);
1625 memset(match
->flow
.dl_dst
, 0, ETH_ADDR_LEN
);
1626 memset(match
->wc
.masks
.dl_dst
, 0, ETH_ADDR_LEN
);
1630 match
->flow
.dl_type
= htons(0);
1631 match
->wc
.masks
.dl_type
= htons(0);
1635 match_set_dl_tci_masked(match
, htons(0), htons(0));
1640 match_set_any_vid(match
);
1643 case MFF_DL_VLAN_PCP
:
1645 match_set_any_pcp(match
);
1648 case MFF_MPLS_LABEL
:
1649 match_set_any_mpls_label(match
);
1653 match_set_any_mpls_tc(match
);
1657 match_set_any_mpls_bos(match
);
1662 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1667 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1671 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1672 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1676 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1677 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1680 case MFF_IPV6_LABEL
:
1681 match
->wc
.masks
.ipv6_label
= htonl(0);
1682 match
->flow
.ipv6_label
= htonl(0);
1686 match
->wc
.masks
.nw_proto
= 0;
1687 match
->flow
.nw_proto
= 0;
1691 case MFF_IP_DSCP_SHIFTED
:
1692 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1693 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1697 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1698 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1702 match
->wc
.masks
.nw_ttl
= 0;
1703 match
->flow
.nw_ttl
= 0;
1707 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
1708 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1712 match
->wc
.masks
.nw_proto
= 0;
1713 match
->flow
.nw_proto
= 0;
1718 memset(match
->flow
.arp_sha
, 0, ETH_ADDR_LEN
);
1719 memset(match
->wc
.masks
.arp_sha
, 0, ETH_ADDR_LEN
);
1724 memset(match
->flow
.arp_tha
, 0, ETH_ADDR_LEN
);
1725 memset(match
->wc
.masks
.arp_tha
, 0, ETH_ADDR_LEN
);
1730 case MFF_ICMPV4_TYPE
:
1731 case MFF_ICMPV6_TYPE
:
1732 match
->wc
.masks
.tp_src
= htons(0);
1733 match
->flow
.tp_src
= htons(0);
1738 case MFF_ICMPV4_CODE
:
1739 case MFF_ICMPV6_CODE
:
1740 match
->wc
.masks
.tp_dst
= htons(0);
1741 match
->flow
.tp_dst
= 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 mf_set(const struct mf_field
*mf
,
1769 const union mf_value
*value
, const union mf_value
*mask
,
1770 struct match
*match
)
1772 if (!mask
|| is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
1773 mf_set_value(mf
, value
, match
);
1775 } else if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
1776 mf_set_wild(mf
, match
);
1782 case MFF_IN_PORT_OXM
:
1783 case MFF_SKB_PRIORITY
:
1786 case MFF_DL_VLAN_PCP
:
1788 case MFF_MPLS_LABEL
:
1794 case MFF_IP_DSCP_SHIFTED
:
1797 case MFF_ICMPV4_TYPE
:
1798 case MFF_ICMPV4_CODE
:
1799 case MFF_ICMPV6_TYPE
:
1800 case MFF_ICMPV6_CODE
:
1804 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
1807 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
1810 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
1813 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
1816 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
1819 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
1823 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
1827 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
1828 ntohl(value
->be32
), ntohl(mask
->be32
));
1832 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
1837 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
1841 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
1846 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
1851 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
1855 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
1859 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
1863 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1867 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1871 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1875 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1878 case MFF_IPV6_LABEL
:
1879 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1880 mf_set_value(mf
, value
, match
);
1882 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
1887 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1891 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
1895 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1899 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1904 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
1909 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
1919 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1923 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1924 } else if (!sf
->n_bits
) {
1925 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1926 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1927 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1928 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1929 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1930 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1931 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1932 sf
->field
->n_bits
, type
, sf
->field
->name
);
1933 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1934 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1935 type
, sf
->field
->name
);
1940 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1943 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1944 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1947 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1949 return mf_check__(sf
, flow
, "source");
1952 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1953 * if so, otherwise an OpenFlow error code (e.g. as returned by
1956 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1958 int error
= mf_check__(sf
, flow
, "destination");
1959 if (!error
&& !sf
->field
->writable
) {
1960 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1962 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1967 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1968 * 'value' and 'mask', respectively. */
1970 mf_get(const struct mf_field
*mf
, const struct match
*match
,
1971 union mf_value
*value
, union mf_value
*mask
)
1973 mf_get_value(mf
, &match
->flow
, value
);
1974 mf_get_mask(mf
, &match
->wc
, mask
);
1977 /* Assigns a random value for field 'mf' to 'value'. */
1979 mf_random_value(const struct mf_field
*mf
, union mf_value
*value
)
1981 random_bytes(value
, mf
->n_bytes
);
1993 case MFF_SKB_PRIORITY
:
2013 case MFF_ICMPV4_TYPE
:
2014 case MFF_ICMPV4_CODE
:
2015 case MFF_ICMPV6_TYPE
:
2016 case MFF_ICMPV6_CODE
:
2022 case MFF_IN_PORT_OXM
:
2023 value
->be32
= ofputil_port_to_ofp11(u16_to_ofp(ntohs(value
->be16
)));
2026 case MFF_IPV6_LABEL
:
2027 value
->be32
&= ~htonl(IPV6_LABEL_MASK
);
2031 value
->u8
&= IP_DSCP_MASK
;
2034 case MFF_IP_DSCP_SHIFTED
:
2035 value
->u8
&= IP_DSCP_MASK
>> 2;
2039 value
->u8
&= IP_ECN_MASK
;
2043 value
->u8
&= FLOW_NW_FRAG_MASK
;
2047 value
->be16
&= htons(0xff);
2051 value
->be16
&= htons(VLAN_VID_MASK
);
2054 value
->be16
&= htons(VLAN_VID_MASK
| VLAN_CFI
);
2057 case MFF_DL_VLAN_PCP
:
2062 case MFF_MPLS_LABEL
:
2063 value
->be32
&= htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
);
2067 value
->u8
&= MPLS_TC_MASK
>> MPLS_TC_SHIFT
;
2071 value
->u8
&= MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
;
2081 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2082 uint8_t *valuep
, uint8_t *maskp
)
2084 unsigned long long int integer
, mask
;
2089 integer
= strtoull(s
, &tail
, 0);
2090 if (errno
|| (*tail
!= '\0' && *tail
!= '/')) {
2095 mask
= strtoull(tail
+ 1, &tail
, 0);
2096 if (errno
|| *tail
!= '\0') {
2103 for (i
= mf
->n_bytes
- 1; i
>= 0; i
--) {
2104 valuep
[i
] = integer
;
2110 return xasprintf("%s: value too large for %u-byte field %s",
2111 s
, mf
->n_bytes
, mf
->name
);
2116 return xasprintf("%s: bad syntax for %s", s
, mf
->name
);
2120 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2121 uint8_t mac
[ETH_ADDR_LEN
],
2122 uint8_t mask
[ETH_ADDR_LEN
])
2126 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2129 if (sscanf(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(mac
), &n
) > 0
2130 && n
== strlen(s
)) {
2131 memset(mask
, 0xff, ETH_ADDR_LEN
);
2136 if (sscanf(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2137 ETH_ADDR_SCAN_ARGS(mac
), ETH_ADDR_SCAN_ARGS(mask
), &n
) > 0
2138 && n
== strlen(s
)) {
2142 return xasprintf("%s: invalid Ethernet address", s
);
2146 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2147 ovs_be32
*ip
, ovs_be32
*mask
)
2151 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2153 if (sscanf(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
2154 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
)) == IP_SCAN_COUNT
* 2) {
2156 } else if (sscanf(s
, IP_SCAN_FMT
"/%d",
2157 IP_SCAN_ARGS(ip
), &prefix
) == IP_SCAN_COUNT
+ 1) {
2158 if (prefix
<= 0 || prefix
> 32) {
2159 return xasprintf("%s: network prefix bits not between 1 and "
2161 } else if (prefix
== 32) {
2162 *mask
= htonl(UINT32_MAX
);
2164 *mask
= htonl(((1u << prefix
) - 1) << (32 - prefix
));
2166 } else if (sscanf(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
)) == IP_SCAN_COUNT
) {
2167 *mask
= htonl(UINT32_MAX
);
2169 return xasprintf("%s: invalid IP address", s
);
2175 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2176 struct in6_addr
*value
, struct in6_addr
*mask
)
2178 char *str
= xstrdup(s
);
2179 char *save_ptr
= NULL
;
2180 const char *name
, *netmask
;
2183 ovs_assert(mf
->n_bytes
== sizeof *value
);
2185 name
= strtok_r(str
, "/", &save_ptr
);
2186 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
2190 err
= xasprintf("%s: could not convert to IPv6 address", str
);
2196 netmask
= strtok_r(NULL
, "/", &save_ptr
);
2198 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
2199 int prefix
= atoi(netmask
);
2200 if (prefix
<= 0 || prefix
> 128) {
2202 return xasprintf("%s: prefix bits not between 1 and 128", s
);
2204 *mask
= ipv6_create_mask(prefix
);
2208 *mask
= in6addr_exact
;
2216 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2217 ovs_be16
*valuep
, ovs_be16
*maskp
)
2221 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2223 if (ofputil_port_from_string(s
, &port
)) {
2224 *valuep
= htons(ofp_to_u16(port
));
2225 *maskp
= htons(UINT16_MAX
);
2228 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2232 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2233 ovs_be32
*valuep
, ovs_be32
*maskp
)
2237 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2238 if (ofputil_port_from_string(s
, &port
)) {
2239 *valuep
= ofputil_port_to_ofp11(port
);
2240 *maskp
= htonl(UINT32_MAX
);
2243 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2246 struct frag_handling
{
2252 static const struct frag_handling all_frags
[] = {
2253 #define A FLOW_NW_FRAG_ANY
2254 #define L FLOW_NW_FRAG_LATER
2255 /* name mask value */
2258 { "first", A
|L
, A
},
2259 { "later", A
|L
, A
|L
},
2264 { "not_later", L
, 0 },
2271 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2273 const struct frag_handling
*h
;
2275 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2276 if (!strcasecmp(s
, h
->name
)) {
2277 /* We force the upper bits of the mask on to make mf_parse_value()
2278 * happy (otherwise it will never think it's an exact match.) */
2279 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2285 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2286 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2290 parse_flow_tun_flags(const char *s_
, const char *(*bit_to_string
)(uint32_t),
2293 uint32_t result
= 0;
2294 char *save_ptr
= NULL
;
2297 char *s
= xstrdup(s_
);
2299 for (name
= strtok_r((char *)s
, " |", &save_ptr
); name
;
2300 name
= strtok_r(NULL
, " |", &save_ptr
)) {
2302 unsigned long long int flags
;
2306 if (sscanf(name
, "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
2310 name_len
= strlen(name
);
2311 for (bit
= 1; bit
; bit
<<= 1) {
2312 const char *fname
= bit_to_string(bit
);
2319 len
= strlen(fname
);
2320 if (len
!= name_len
) {
2323 if (!strncmp(name
, fname
, len
)) {
2335 *res
= htons(result
);
2342 mf_from_tun_flags_string(const char *s
, ovs_be16
*valuep
, ovs_be16
*maskp
)
2344 if (!parse_flow_tun_flags(s
, flow_tun_flag_to_string
, valuep
)) {
2345 *maskp
= htons(UINT16_MAX
);
2349 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2350 "\"csum\", \"key\"", s
);
2353 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2354 * NULL if successful, otherwise a malloc()'d string describing the error. */
2356 mf_parse(const struct mf_field
*mf
, const char *s
,
2357 union mf_value
*value
, union mf_value
*mask
)
2359 if (!strcmp(s
, "*")) {
2360 memset(value
, 0, mf
->n_bytes
);
2361 memset(mask
, 0, mf
->n_bytes
);
2365 switch (mf
->string
) {
2367 case MFS_HEXADECIMAL
:
2368 return mf_from_integer_string(mf
, s
,
2369 (uint8_t *) value
, (uint8_t *) mask
);
2372 return mf_from_ethernet_string(mf
, s
, value
->mac
, mask
->mac
);
2375 return mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2378 return mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2381 return mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
2383 case MFS_OFP_PORT_OXM
:
2384 return mf_from_ofp_port_string32(mf
, s
, &value
->be32
, &mask
->be32
);
2387 return mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2390 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2391 return mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2396 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2397 * successful, otherwise a malloc()'d string describing the error. */
2399 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
2401 union mf_value mask
;
2404 error
= mf_parse(mf
, s
, value
, &mask
);
2409 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2410 return xasprintf("%s: wildcards not allowed here", s
);
2416 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2417 const uint8_t *maskp
, struct ds
*s
)
2419 unsigned long long int integer
;
2422 ovs_assert(mf
->n_bytes
<= 8);
2425 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2426 integer
= (integer
<< 8) | valuep
[i
];
2428 if (mf
->string
== MFS_HEXADECIMAL
) {
2429 ds_put_format(s
, "%#llx", integer
);
2431 ds_put_format(s
, "%lld", integer
);
2435 unsigned long long int mask
;
2438 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2439 mask
= (mask
<< 8) | maskp
[i
];
2442 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2443 * not sure that that a bit-mask written in decimal is ever easier to
2444 * understand than the same bit-mask written in hexadecimal. */
2445 ds_put_format(s
, "/%#llx", mask
);
2450 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2452 const struct frag_handling
*h
;
2454 mask
&= FLOW_NW_FRAG_MASK
;
2457 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2458 if (value
== h
->value
&& mask
== h
->mask
) {
2459 ds_put_cstr(s
, h
->name
);
2463 ds_put_cstr(s
, "<error>");
2467 mf_format_tnl_flags_string(const ovs_be16
*valuep
, struct ds
*s
)
2469 format_flags(s
, flow_tun_flag_to_string
, ntohs(*valuep
), '|');
2472 /* Appends to 's' a string representation of field 'mf' whose value is in
2473 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2475 mf_format(const struct mf_field
*mf
,
2476 const union mf_value
*value
, const union mf_value
*mask
,
2480 if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
2481 ds_put_cstr(s
, "ANY");
2483 } else if (is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
2488 switch (mf
->string
) {
2489 case MFS_OFP_PORT_OXM
:
2492 ofputil_port_from_ofp11(value
->be32
, &port
);
2493 ofputil_format_port(port
, s
);
2499 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), s
);
2504 case MFS_HEXADECIMAL
:
2505 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2509 eth_format_masked(value
->mac
, mask
->mac
, s
);
2513 ip_format_masked(value
->be32
, mask
? mask
->be32
: htonl(UINT32_MAX
),
2518 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2522 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2526 mf_format_tnl_flags_string(&value
->be16
, s
);
2534 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2535 * least-significant bits in 'x'.
2538 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2539 const union mf_subvalue
*x
, struct flow
*flow
)
2541 const struct mf_field
*field
= sf
->field
;
2542 union mf_value value
;
2544 mf_get_value(field
, flow
, &value
);
2545 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2546 sf
->ofs
, sf
->n_bits
);
2547 mf_set_flow_value(field
, &value
, flow
);
2550 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2551 * least-significant bits in 'x'.
2554 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2555 struct match
*match
)
2557 const struct mf_field
*field
= sf
->field
;
2558 union mf_value value
, mask
;
2560 mf_get(field
, match
, &value
, &mask
);
2561 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2562 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2563 mf_set(field
, &value
, &mask
, match
);
2566 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2567 * reading 'flow', e.g. as checked by mf_check_src(). */
2569 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2570 union mf_subvalue
*x
)
2572 union mf_value value
;
2574 mf_get_value(sf
->field
, flow
, &value
);
2576 memset(x
, 0, sizeof *x
);
2577 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2582 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2583 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2586 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2588 union mf_value value
;
2590 mf_get_value(sf
->field
, flow
, &value
);
2591 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2594 /* Formats 'sf' into 's' in a format normally acceptable to
2595 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2596 * sf->field has no NXM name.) */
2598 mf_format_subfield(const struct mf_subfield
*sf
, struct ds
*s
)
2601 ds_put_cstr(s
, "<unknown>");
2602 } else if (sf
->field
->nxm_name
) {
2603 ds_put_cstr(s
, sf
->field
->nxm_name
);
2604 } else if (sf
->field
->nxm_header
) {
2605 uint32_t header
= sf
->field
->nxm_header
;
2606 ds_put_format(s
, "%d:%d", NXM_VENDOR(header
), NXM_FIELD(header
));
2608 ds_put_cstr(s
, sf
->field
->name
);
2611 if (sf
->field
&& sf
->ofs
== 0 && sf
->n_bits
== sf
->field
->n_bits
) {
2612 ds_put_cstr(s
, "[]");
2613 } else if (sf
->n_bits
== 1) {
2614 ds_put_format(s
, "[%d]", sf
->ofs
);
2616 ds_put_format(s
, "[%d..%d]", sf
->ofs
, sf
->ofs
+ sf
->n_bits
- 1);
2620 static const struct mf_field
*
2621 mf_parse_subfield_name(const char *name
, int name_len
, bool *wild
)
2625 *wild
= name_len
> 2 && !memcmp(&name
[name_len
- 2], "_W", 2);
2630 for (i
= 0; i
< MFF_N_IDS
; i
++) {
2631 const struct mf_field
*mf
= mf_from_id(i
);
2634 && !strncmp(mf
->nxm_name
, name
, name_len
)
2635 && mf
->nxm_name
[name_len
] == '\0') {
2639 && !strncmp(mf
->oxm_name
, name
, name_len
)
2640 && mf
->oxm_name
[name_len
] == '\0') {
2648 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2649 * returns NULL and advances '*sp' to the first byte following the parsed
2650 * string. On failure, returns a malloc()'d error message, does not modify
2651 * '*sp', and does not properly initialize 'sf'.
2653 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2654 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2655 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2656 * may both be omitted (the [] are still required) to indicate an entire
2658 char * WARN_UNUSED_RESULT
2659 mf_parse_subfield__(struct mf_subfield
*sf
, const char **sp
)
2661 const struct mf_field
*field
;
2670 name_len
= strcspn(s
, "[");
2671 if (s
[name_len
] != '[') {
2672 return xasprintf("%s: missing [ looking for field name", *sp
);
2675 field
= mf_parse_subfield_name(name
, name_len
, &wild
);
2677 return xasprintf("%s: unknown field `%.*s'", *sp
, name_len
, s
);
2681 if (sscanf(s
, "[%d..%d]", &start
, &end
) == 2) {
2682 /* Nothing to do. */
2683 } else if (sscanf(s
, "[%d]", &start
) == 1) {
2685 } else if (!strncmp(s
, "[]", 2)) {
2687 end
= field
->n_bits
- 1;
2689 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2690 "[<start>..<end>]", *sp
);
2692 s
= strchr(s
, ']') + 1;
2695 return xasprintf("%s: starting bit %d is after ending bit %d",
2697 } else if (start
>= field
->n_bits
) {
2698 return xasprintf("%s: starting bit %d is not valid because field is "
2699 "only %d bits wide", *sp
, start
, field
->n_bits
);
2700 } else if (end
>= field
->n_bits
){
2701 return xasprintf("%s: ending bit %d is not valid because field is "
2702 "only %d bits wide", *sp
, end
, field
->n_bits
);
2707 sf
->n_bits
= end
- start
+ 1;
2713 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2714 * successful, otherwise a malloc()'d string describing the error. The caller
2715 * is responsible for freeing the returned string.
2717 * The syntax parsed from 's' takes the form "header[start..end]" where
2718 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2719 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2720 * may both be omitted (the [] are still required) to indicate an entire
2722 char * WARN_UNUSED_RESULT
2723 mf_parse_subfield(struct mf_subfield
*sf
, const char *s
)
2725 char *error
= mf_parse_subfield__(sf
, &s
);
2726 if (!error
&& s
[0]) {
2727 error
= xstrdup("unexpected input following field syntax");
2733 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2737 for (i
= 0; i
< ARRAY_SIZE(subvalue
->u8
); i
++) {
2738 if (subvalue
->u8
[i
]) {
2739 ds_put_format(s
, "0x%"PRIx8
, subvalue
->u8
[i
]);
2740 for (i
++; i
< ARRAY_SIZE(subvalue
->u8
); i
++) {
2741 ds_put_format(s
, "%02"PRIx8
, subvalue
->u8
[i
]);
2746 ds_put_char(s
, '0');