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"
33 #include "socket-util.h"
34 #include "unaligned.h"
37 VLOG_DEFINE_THIS_MODULE(meta_flow
);
39 #define MF_FIELD_SIZES(MEMBER) \
40 sizeof ((union mf_value *)0)->MEMBER, \
41 8 * sizeof ((union mf_value *)0)->MEMBER
43 static const struct mf_field mf_fields
[MFF_N_IDS
] = {
49 MFF_TUN_ID
, "tun_id", NULL
,
55 NXM_NX_TUN_ID
, "NXM_NX_TUN_ID",
56 OXM_OF_TUNNEL_ID
, "OXM_OF_TUNNEL_ID",
58 MFF_TUN_SRC
, "tun_src", NULL
,
64 NXM_NX_TUN_IPV4_SRC
, "NXM_NX_TUN_IPV4_SRC",
65 NXM_NX_TUN_IPV4_SRC
, "NXM_NX_TUN_IPV4_SRC",
67 MFF_TUN_DST
, "tun_dst", NULL
,
73 NXM_NX_TUN_IPV4_DST
, "NXM_NX_TUN_IPV4_DST",
74 NXM_NX_TUN_IPV4_DST
, "NXM_NX_TUN_IPV4_DST",
76 MFF_TUN_FLAGS
, "tun_flags", NULL
,
85 MFF_TUN_TOS
, "tun_tos", NULL
,
94 MFF_TUN_TTL
, "tun_ttl", NULL
,
103 MFF_METADATA
, "metadata", NULL
,
104 MF_FIELD_SIZES(be64
),
109 OXM_OF_METADATA
, "OXM_OF_METADATA",
110 OXM_OF_METADATA
, "OXM_OF_METADATA",
112 MFF_IN_PORT
, "in_port", NULL
,
113 MF_FIELD_SIZES(be16
),
118 NXM_OF_IN_PORT
, "NXM_OF_IN_PORT",
119 OXM_OF_IN_PORT
, "OXM_OF_IN_PORT",
121 MFF_SKB_PRIORITY
, "skb_priority", NULL
,
122 MF_FIELD_SIZES(be32
),
130 MFF_SKB_MARK
, "skb_mark", NULL
,
131 MF_FIELD_SIZES(be32
),
140 #define REGISTER(IDX) \
142 MFF_REG##IDX, "reg" #IDX, NULL, \
143 MF_FIELD_SIZES(be32), \
148 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
149 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
184 MFF_ETH_SRC
, "eth_src", "dl_src",
190 NXM_OF_ETH_SRC
, "NXM_OF_ETH_SRC",
191 OXM_OF_ETH_SRC
, "OXM_OF_ETH_SRC",
193 MFF_ETH_DST
, "eth_dst", "dl_dst",
199 NXM_OF_ETH_DST
, "NXM_OF_ETH_DST",
200 OXM_OF_ETH_DST
, "OXM_OF_ETH_DST",
202 MFF_ETH_TYPE
, "eth_type", "dl_type",
203 MF_FIELD_SIZES(be16
),
208 NXM_OF_ETH_TYPE
, "NXM_OF_ETH_TYPE",
209 OXM_OF_ETH_TYPE
, "OXM_OF_ETH_TYPE",
213 MFF_VLAN_TCI
, "vlan_tci", NULL
,
214 MF_FIELD_SIZES(be16
),
219 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
220 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
222 MFF_DL_VLAN
, "dl_vlan", NULL
,
223 sizeof(ovs_be16
), 12,
231 MFF_VLAN_VID
, "vlan_vid", NULL
,
232 sizeof(ovs_be16
), 12,
237 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
238 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
240 MFF_DL_VLAN_PCP
, "dl_vlan_pcp", NULL
,
249 MFF_VLAN_PCP
, "vlan_pcp", NULL
,
255 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
256 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
263 MFF_MPLS_LABEL
, "mpls_label", NULL
,
269 OXM_OF_MPLS_LABEL
, "OXM_OF_MPLS_LABEL",
270 OXM_OF_MPLS_LABEL
, "OXM_OF_MPLS_LABEL",
272 MFF_MPLS_TC
, "mpls_tc", NULL
,
278 OXM_OF_MPLS_TC
, "OXM_OF_MPLS_TC",
279 OXM_OF_MPLS_TC
, "OXM_OF_MPLS_TC",
281 MFF_MPLS_BOS
, "mpls_bos", NULL
,
287 OXM_OF_MPLS_BOS
, "OXM_OF_MPLS_BOS",
288 OXM_OF_MPLS_BOS
, "OXM_OF_MPLS_BOS",
296 MFF_IPV4_SRC
, "ip_src", "nw_src",
297 MF_FIELD_SIZES(be32
),
302 NXM_OF_IP_SRC
, "NXM_OF_IP_SRC",
303 OXM_OF_IPV4_SRC
, "OXM_OF_IPV4_SRC",
305 MFF_IPV4_DST
, "ip_dst", "nw_dst",
306 MF_FIELD_SIZES(be32
),
311 NXM_OF_IP_DST
, "NXM_OF_IP_DST",
312 OXM_OF_IPV4_DST
, "OXM_OF_IPV4_DST",
316 MFF_IPV6_SRC
, "ipv6_src", NULL
,
317 MF_FIELD_SIZES(ipv6
),
322 NXM_NX_IPV6_SRC
, "NXM_NX_IPV6_SRC",
323 OXM_OF_IPV6_SRC
, "OXM_OF_IPV6_SRC",
325 MFF_IPV6_DST
, "ipv6_dst", NULL
,
326 MF_FIELD_SIZES(ipv6
),
331 NXM_NX_IPV6_DST
, "NXM_NX_IPV6_DST",
332 OXM_OF_IPV6_DST
, "OXM_OF_IPV6_DST",
335 MFF_IPV6_LABEL
, "ipv6_label", NULL
,
341 NXM_NX_IPV6_LABEL
, "NXM_NX_IPV6_LABEL",
342 OXM_OF_IPV6_FLABEL
, "OXM_OF_IPV6_FLABEL",
346 MFF_IP_PROTO
, "nw_proto", NULL
,
352 NXM_OF_IP_PROTO
, "NXM_OF_IP_PROTO",
353 OXM_OF_IP_PROTO
, "OXM_OF_IP_PROTO",
355 MFF_IP_DSCP
, "nw_tos", NULL
,
361 NXM_OF_IP_TOS
, "NXM_OF_IP_TOS",
362 NXM_OF_IP_TOS
, "NXM_OF_IP_TOS",
364 MFF_IP_DSCP_SHIFTED
, "nw_tos_shifted", NULL
,
370 OXM_OF_IP_DSCP
, "OXM_OF_IP_DSCP",
371 OXM_OF_IP_DSCP
, "OXM_OF_IP_DSCP",
373 MFF_IP_ECN
, "nw_ecn", NULL
,
379 NXM_NX_IP_ECN
, "NXM_NX_IP_ECN",
380 OXM_OF_IP_ECN
, "OXM_OF_IP_ECN",
382 MFF_IP_TTL
, "nw_ttl", NULL
,
388 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
389 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
391 MFF_IP_FRAG
, "ip_frag", NULL
,
397 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
398 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
402 MFF_ARP_OP
, "arp_op", NULL
,
403 MF_FIELD_SIZES(be16
),
408 NXM_OF_ARP_OP
, "NXM_OF_ARP_OP",
409 OXM_OF_ARP_OP
, "OXM_OF_ARP_OP",
411 MFF_ARP_SPA
, "arp_spa", NULL
,
412 MF_FIELD_SIZES(be32
),
417 NXM_OF_ARP_SPA
, "NXM_OF_ARP_SPA",
418 OXM_OF_ARP_SPA
, "OXM_OF_ARP_SPA",
420 MFF_ARP_TPA
, "arp_tpa", NULL
,
421 MF_FIELD_SIZES(be32
),
426 NXM_OF_ARP_TPA
, "NXM_OF_ARP_TPA",
427 OXM_OF_ARP_TPA
, "OXM_OF_ARP_TPA",
429 MFF_ARP_SHA
, "arp_sha", NULL
,
435 NXM_NX_ARP_SHA
, "NXM_NX_ARP_SHA",
436 OXM_OF_ARP_SHA
, "OXM_OF_ARP_SHA",
438 MFF_ARP_THA
, "arp_tha", NULL
,
444 NXM_NX_ARP_THA
, "NXM_NX_ARP_THA",
445 OXM_OF_ARP_THA
, "OXM_OF_ARP_THA",
453 MFF_TCP_SRC
, "tcp_src", "tp_src",
454 MF_FIELD_SIZES(be16
),
459 NXM_OF_TCP_SRC
, "NXM_OF_TCP_SRC",
460 OXM_OF_TCP_SRC
, "OXM_OF_TCP_SRC",
462 MFF_TCP_DST
, "tcp_dst", "tp_dst",
463 MF_FIELD_SIZES(be16
),
468 NXM_OF_TCP_DST
, "NXM_OF_TCP_DST",
469 OXM_OF_TCP_DST
, "OXM_OF_TCP_DST",
473 MFF_UDP_SRC
, "udp_src", NULL
,
474 MF_FIELD_SIZES(be16
),
479 NXM_OF_UDP_SRC
, "NXM_OF_UDP_SRC",
480 OXM_OF_UDP_SRC
, "OXM_OF_UDP_SRC",
482 MFF_UDP_DST
, "udp_dst", NULL
,
483 MF_FIELD_SIZES(be16
),
488 NXM_OF_UDP_DST
, "NXM_OF_UDP_DST",
489 OXM_OF_UDP_DST
, "OXM_OF_UDP_DST",
493 MFF_ICMPV4_TYPE
, "icmp_type", NULL
,
499 NXM_OF_ICMP_TYPE
, "NXM_OF_ICMP_TYPE",
500 OXM_OF_ICMPV4_TYPE
, "OXM_OF_ICMPV4_TYPE",
502 MFF_ICMPV4_CODE
, "icmp_code", NULL
,
508 NXM_OF_ICMP_CODE
, "NXM_OF_ICMP_CODE",
509 OXM_OF_ICMPV4_CODE
, "OXM_OF_ICMPV4_CODE",
513 MFF_ICMPV6_TYPE
, "icmpv6_type", NULL
,
519 NXM_NX_ICMPV6_TYPE
, "NXM_NX_ICMPV6_TYPE",
520 OXM_OF_ICMPV6_TYPE
, "OXM_OF_ICMPV6_TYPE",
522 MFF_ICMPV6_CODE
, "icmpv6_code", NULL
,
528 NXM_NX_ICMPV6_CODE
, "NXM_NX_ICMPV6_CODE",
529 OXM_OF_ICMPV6_CODE
, "OXM_OF_ICMPV6_CODE",
537 MFF_ND_TARGET
, "nd_target", NULL
,
538 MF_FIELD_SIZES(ipv6
),
543 NXM_NX_ND_TARGET
, "NXM_NX_ND_TARGET",
544 OXM_OF_IPV6_ND_TARGET
, "OXM_OF_IPV6_ND_TARGET",
546 MFF_ND_SLL
, "nd_sll", NULL
,
552 NXM_NX_ND_SLL
, "NXM_NX_ND_SLL",
553 OXM_OF_IPV6_ND_SLL
, "OXM_OF_IPV6_ND_SLL",
555 MFF_ND_TLL
, "nd_tll", NULL
,
561 NXM_NX_ND_TLL
, "NXM_NX_ND_TLL",
562 OXM_OF_IPV6_ND_TLL
, "OXM_OF_IPV6_ND_TLL",
566 /* Maps an NXM or OXM header value to an mf_field. */
568 struct hmap_node hmap_node
; /* In 'all_fields' hmap. */
569 uint32_t header
; /* NXM or OXM header value. */
570 const struct mf_field
*mf
;
573 /* Contains 'struct nxm_field's. */
574 static struct hmap all_fields
= HMAP_INITIALIZER(&all_fields
);
576 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
577 * controller and so there's not much point in showing a lot of them. */
578 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
580 const struct mf_field
*mf_from_nxm_header__(uint32_t header
);
582 /* Returns the field with the given 'id'. */
583 const struct mf_field
*
584 mf_from_id(enum mf_field_id id
)
586 ovs_assert((unsigned int) id
< MFF_N_IDS
);
587 return &mf_fields
[id
];
590 /* Returns the field with the given 'name', or a null pointer if no field has
592 const struct mf_field
*
593 mf_from_name(const char *name
)
595 static struct shash mf_by_name
= SHASH_INITIALIZER(&mf_by_name
);
597 if (shash_is_empty(&mf_by_name
)) {
598 const struct mf_field
*mf
;
600 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
601 shash_add_once(&mf_by_name
, mf
->name
, mf
);
602 if (mf
->extra_name
) {
603 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
608 return shash_find_data(&mf_by_name
, name
);
612 add_nxm_field(uint32_t header
, const struct mf_field
*mf
)
616 f
= xmalloc(sizeof *f
);
617 hmap_insert(&all_fields
, &f
->hmap_node
, hash_int(header
, 0));
623 nxm_init_add_field(const struct mf_field
*mf
, uint32_t header
)
626 ovs_assert(!mf_from_nxm_header__(header
));
627 add_nxm_field(header
, mf
);
628 if (mf
->maskable
!= MFM_NONE
) {
629 add_nxm_field(NXM_MAKE_WILD_HEADER(header
), mf
);
637 const struct mf_field
*mf
;
639 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
640 nxm_init_add_field(mf
, mf
->nxm_header
);
641 if (mf
->oxm_header
!= mf
->nxm_header
) {
642 nxm_init_add_field(mf
, mf
->oxm_header
);
647 const struct mf_field
*
648 mf_from_nxm_header(uint32_t header
)
650 if (hmap_is_empty(&all_fields
)) {
653 return mf_from_nxm_header__(header
);
656 const struct mf_field
*
657 mf_from_nxm_header__(uint32_t header
)
659 const struct nxm_field
*f
;
661 HMAP_FOR_EACH_IN_BUCKET (f
, hmap_node
, hash_int(header
, 0), &all_fields
) {
662 if (f
->header
== header
) {
670 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
671 * specifies at least one bit in the field.
673 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
674 * meets 'mf''s prerequisites. */
676 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
680 return !wc
->masks
.tunnel
.ip_src
;
682 return !wc
->masks
.tunnel
.ip_dst
;
687 return !wc
->masks
.tunnel
.tun_id
;
689 return !wc
->masks
.metadata
;
691 return !wc
->masks
.in_port
;
692 case MFF_SKB_PRIORITY
:
693 return !wc
->masks
.skb_priority
;
695 return !wc
->masks
.skb_mark
;
697 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
700 return eth_addr_is_zero(wc
->masks
.dl_src
);
702 return eth_addr_is_zero(wc
->masks
.dl_dst
);
704 return !wc
->masks
.dl_type
;
708 return eth_addr_is_zero(wc
->masks
.arp_sha
);
712 return eth_addr_is_zero(wc
->masks
.arp_tha
);
715 return !wc
->masks
.vlan_tci
;
717 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
));
719 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
720 case MFF_DL_VLAN_PCP
:
722 return !(wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
));
725 return !(wc
->masks
.mpls_lse
& htonl(MPLS_LABEL_MASK
));
727 return !(wc
->masks
.mpls_lse
& htonl(MPLS_TC_MASK
));
729 return !(wc
->masks
.mpls_lse
& htonl(MPLS_BOS_MASK
));
732 return !wc
->masks
.nw_src
;
734 return !wc
->masks
.nw_dst
;
737 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
739 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
742 return !wc
->masks
.ipv6_label
;
745 return !wc
->masks
.nw_proto
;
747 case MFF_IP_DSCP_SHIFTED
:
748 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
750 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
752 return !wc
->masks
.nw_ttl
;
755 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
758 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
761 return !wc
->masks
.nw_proto
;
763 return !wc
->masks
.nw_src
;
765 return !wc
->masks
.nw_dst
;
769 case MFF_ICMPV4_TYPE
:
770 case MFF_ICMPV6_TYPE
:
771 return !wc
->masks
.tp_src
;
774 case MFF_ICMPV4_CODE
:
775 case MFF_ICMPV6_CODE
:
776 return !wc
->masks
.tp_dst
;
784 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
785 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
786 * purposes, or to 0 if it is wildcarded.
788 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
789 * meets 'mf''s prerequisites. */
791 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
792 union mf_value
*mask
)
794 mf_get_value(mf
, &wc
->masks
, mask
);
797 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
798 * if the mask is valid, false otherwise. */
800 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
802 switch (mf
->maskable
) {
804 return (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
) ||
805 is_all_ones((const uint8_t *) mask
, mf
->n_bytes
));
815 is_icmpv4(const struct flow
*flow
)
817 return (flow
->dl_type
== htons(ETH_TYPE_IP
)
818 && flow
->nw_proto
== IPPROTO_ICMP
);
822 is_icmpv6(const struct flow
*flow
)
824 return (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
825 && flow
->nw_proto
== IPPROTO_ICMPV6
);
828 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
830 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
832 switch (mf
->prereqs
) {
837 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
838 flow
->dl_type
== htons(ETH_TYPE_RARP
));
840 return flow
->dl_type
== htons(ETH_TYPE_IP
);
842 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
844 return (flow
->vlan_tci
& htons(VLAN_CFI
)) != 0;
846 return eth_type_mpls(flow
->dl_type
);
848 return is_ip_any(flow
);
851 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
;
853 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
;
855 return is_icmpv4(flow
);
857 return is_icmpv6(flow
);
860 return (is_icmpv6(flow
)
861 && flow
->tp_dst
== htons(0)
862 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
863 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
865 return (is_icmpv6(flow
)
866 && flow
->tp_dst
== htons(0)
867 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
869 return (is_icmpv6(flow
)
870 && flow
->tp_dst
== htons(0)
871 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
877 /* Returns true if 'value' may be a valid value *as part of a masked match*,
880 * A value is not rejected just because it is not valid for the field in
881 * question, but only if it doesn't make sense to test the bits in question at
882 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
883 * without the VLAN_CFI bit being set, but we can't reject those values because
884 * it is still legitimate to test just for those bits (see the documentation
885 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
886 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
888 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
899 case MFF_SKB_PRIORITY
:
920 case MFF_ICMPV4_TYPE
:
921 case MFF_ICMPV4_CODE
:
922 case MFF_ICMPV6_TYPE
:
923 case MFF_ICMPV6_CODE
:
930 return !(value
->u8
& ~IP_DSCP_MASK
);
931 case MFF_IP_DSCP_SHIFTED
:
932 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
934 return !(value
->u8
& ~IP_ECN_MASK
);
936 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
939 return !(value
->be16
& htons(0xff00));
942 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
944 return !(value
->be16
& htons(VLAN_PCP_MASK
));
946 case MFF_DL_VLAN_PCP
:
948 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
951 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
954 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
957 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
960 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
968 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
969 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
971 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
972 union mf_value
*value
)
976 value
->be64
= flow
->tunnel
.tun_id
;
979 value
->be32
= flow
->tunnel
.ip_src
;
982 value
->be32
= flow
->tunnel
.ip_dst
;
985 value
->be16
= htons(flow
->tunnel
.flags
);
988 value
->u8
= flow
->tunnel
.ip_ttl
;
991 value
->u8
= flow
->tunnel
.ip_tos
;
995 value
->be64
= flow
->metadata
;
999 value
->be16
= htons(flow
->in_port
);
1002 case MFF_SKB_PRIORITY
:
1003 value
->be32
= htonl(flow
->skb_priority
);
1007 value
->be32
= htonl(flow
->skb_mark
);
1011 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
1015 memcpy(value
->mac
, flow
->dl_src
, ETH_ADDR_LEN
);
1019 memcpy(value
->mac
, flow
->dl_dst
, ETH_ADDR_LEN
);
1023 value
->be16
= flow
->dl_type
;
1027 value
->be16
= flow
->vlan_tci
;
1031 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
1034 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
1037 case MFF_DL_VLAN_PCP
:
1039 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
1042 case MFF_MPLS_LABEL
:
1043 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
));
1047 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
);
1051 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
);
1055 value
->be32
= flow
->nw_src
;
1059 value
->be32
= flow
->nw_dst
;
1063 value
->ipv6
= flow
->ipv6_src
;
1067 value
->ipv6
= flow
->ipv6_dst
;
1070 case MFF_IPV6_LABEL
:
1071 value
->be32
= flow
->ipv6_label
;
1075 value
->u8
= flow
->nw_proto
;
1079 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
1082 case MFF_IP_DSCP_SHIFTED
:
1083 value
->u8
= flow
->nw_tos
>> 2;
1087 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
1091 value
->u8
= flow
->nw_ttl
;
1095 value
->u8
= flow
->nw_frag
;
1099 value
->be16
= htons(flow
->nw_proto
);
1103 value
->be32
= flow
->nw_src
;
1107 value
->be32
= flow
->nw_dst
;
1112 memcpy(value
->mac
, flow
->arp_sha
, ETH_ADDR_LEN
);
1117 memcpy(value
->mac
, flow
->arp_tha
, ETH_ADDR_LEN
);
1122 value
->be16
= flow
->tp_src
;
1127 value
->be16
= flow
->tp_dst
;
1130 case MFF_ICMPV4_TYPE
:
1131 case MFF_ICMPV6_TYPE
:
1132 value
->u8
= ntohs(flow
->tp_src
);
1135 case MFF_ICMPV4_CODE
:
1136 case MFF_ICMPV6_CODE
:
1137 value
->u8
= ntohs(flow
->tp_dst
);
1141 value
->ipv6
= flow
->nd_target
;
1150 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1151 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1154 mf_set_value(const struct mf_field
*mf
,
1155 const union mf_value
*value
, struct match
*match
)
1159 match_set_tun_id(match
, value
->be64
);
1162 match_set_tun_src(match
, value
->be32
);
1165 match_set_tun_dst(match
, value
->be32
);
1168 match_set_tun_flags(match
, ntohs(value
->be16
));
1171 match_set_tun_tos(match
, value
->u8
);
1174 match_set_tun_ttl(match
, value
->u8
);
1178 match_set_metadata(match
, value
->be64
);
1182 match_set_in_port(match
, ntohs(value
->be16
));
1185 case MFF_SKB_PRIORITY
:
1186 match_set_skb_priority(match
, ntohl(value
->be32
));
1190 match_set_skb_mark(match
, ntohl(value
->be32
));
1194 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1198 match_set_dl_src(match
, value
->mac
);
1202 match_set_dl_dst(match
, value
->mac
);
1206 match_set_dl_type(match
, value
->be16
);
1210 match_set_dl_tci(match
, value
->be16
);
1214 match_set_dl_vlan(match
, value
->be16
);
1217 match_set_vlan_vid(match
, value
->be16
);
1220 case MFF_DL_VLAN_PCP
:
1222 match_set_dl_vlan_pcp(match
, value
->u8
);
1225 case MFF_MPLS_LABEL
:
1226 match_set_mpls_label(match
, value
->be32
);
1230 match_set_mpls_tc(match
, value
->u8
);
1234 match_set_mpls_bos(match
, value
->u8
);
1238 match_set_nw_src(match
, value
->be32
);
1242 match_set_nw_dst(match
, value
->be32
);
1246 match_set_ipv6_src(match
, &value
->ipv6
);
1250 match_set_ipv6_dst(match
, &value
->ipv6
);
1253 case MFF_IPV6_LABEL
:
1254 match_set_ipv6_label(match
, value
->be32
);
1258 match_set_nw_proto(match
, value
->u8
);
1262 match_set_nw_dscp(match
, value
->u8
);
1265 case MFF_IP_DSCP_SHIFTED
:
1266 match_set_nw_dscp(match
, value
->u8
<< 2);
1270 match_set_nw_ecn(match
, value
->u8
);
1274 match_set_nw_ttl(match
, value
->u8
);
1278 match_set_nw_frag(match
, value
->u8
);
1282 match_set_nw_proto(match
, ntohs(value
->be16
));
1286 match_set_nw_src(match
, value
->be32
);
1290 match_set_nw_dst(match
, value
->be32
);
1295 match_set_arp_sha(match
, value
->mac
);
1300 match_set_arp_tha(match
, value
->mac
);
1305 match_set_tp_src(match
, value
->be16
);
1310 match_set_tp_dst(match
, value
->be16
);
1313 case MFF_ICMPV4_TYPE
:
1314 case MFF_ICMPV6_TYPE
:
1315 match_set_icmp_type(match
, value
->u8
);
1318 case MFF_ICMPV4_CODE
:
1319 case MFF_ICMPV6_CODE
:
1320 match_set_icmp_code(match
, value
->u8
);
1324 match_set_nd_target(match
, &value
->ipv6
);
1333 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1334 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1337 mf_set_flow_value(const struct mf_field
*mf
,
1338 const union mf_value
*value
, struct flow
*flow
)
1342 flow
->tunnel
.tun_id
= value
->be64
;
1345 flow
->tunnel
.ip_src
= value
->be32
;
1348 flow
->tunnel
.ip_dst
= value
->be32
;
1351 flow
->tunnel
.flags
= ntohs(value
->be16
);
1354 flow
->tunnel
.ip_tos
= value
->u8
;
1357 flow
->tunnel
.ip_ttl
= value
->u8
;
1361 flow
->metadata
= value
->be64
;
1365 flow
->in_port
= ntohs(value
->be16
);
1368 case MFF_SKB_PRIORITY
:
1369 flow
->skb_priority
= ntohl(value
->be32
);
1373 flow
->skb_mark
= ntohl(value
->be32
);
1377 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1381 memcpy(flow
->dl_src
, value
->mac
, ETH_ADDR_LEN
);
1385 memcpy(flow
->dl_dst
, value
->mac
, ETH_ADDR_LEN
);
1389 flow
->dl_type
= value
->be16
;
1393 flow
->vlan_tci
= value
->be16
;
1397 flow_set_dl_vlan(flow
, value
->be16
);
1400 flow_set_vlan_vid(flow
, value
->be16
);
1403 case MFF_DL_VLAN_PCP
:
1405 flow_set_vlan_pcp(flow
, value
->u8
);
1408 case MFF_MPLS_LABEL
:
1409 flow_set_mpls_label(flow
, value
->be32
);
1413 flow_set_mpls_tc(flow
, value
->u8
);
1417 flow_set_mpls_bos(flow
, value
->u8
);
1421 flow
->nw_src
= value
->be32
;
1425 flow
->nw_dst
= value
->be32
;
1429 flow
->ipv6_src
= value
->ipv6
;
1433 flow
->ipv6_dst
= value
->ipv6
;
1436 case MFF_IPV6_LABEL
:
1437 flow
->ipv6_label
= value
->be32
& ~htonl(IPV6_LABEL_MASK
);
1441 flow
->nw_proto
= value
->u8
;
1445 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1446 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1449 case MFF_IP_DSCP_SHIFTED
:
1450 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1451 flow
->nw_tos
|= value
->u8
<< 2;
1455 flow
->nw_tos
&= ~IP_ECN_MASK
;
1456 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1460 flow
->nw_ttl
= value
->u8
;
1464 flow
->nw_frag
&= value
->u8
;
1468 flow
->nw_proto
= ntohs(value
->be16
);
1472 flow
->nw_src
= value
->be32
;
1476 flow
->nw_dst
= value
->be32
;
1481 memcpy(flow
->arp_sha
, value
->mac
, ETH_ADDR_LEN
);
1486 memcpy(flow
->arp_tha
, value
->mac
, ETH_ADDR_LEN
);
1491 flow
->tp_src
= value
->be16
;
1496 flow
->tp_dst
= value
->be16
;
1499 case MFF_ICMPV4_TYPE
:
1500 case MFF_ICMPV6_TYPE
:
1501 flow
->tp_src
= htons(value
->u8
);
1504 case MFF_ICMPV4_CODE
:
1505 case MFF_ICMPV6_CODE
:
1506 flow
->tp_dst
= htons(value
->u8
);
1510 flow
->nd_target
= value
->ipv6
;
1519 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1521 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1524 mf_is_zero(const struct mf_field
*mf
, const struct flow
*flow
)
1526 union mf_value value
;
1528 mf_get_value(mf
, flow
, &value
);
1529 return is_all_zeros((const uint8_t *) &value
, mf
->n_bytes
);
1532 /* Makes 'match' wildcard field 'mf'.
1534 * The caller is responsible for ensuring that 'match' meets 'mf''s
1537 mf_set_wild(const struct mf_field
*mf
, struct match
*match
)
1541 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1544 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1547 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1550 match_set_tun_flags_masked(match
, 0, 0);
1553 match_set_tun_tos_masked(match
, 0, 0);
1556 match_set_tun_ttl_masked(match
, 0, 0);
1560 match_set_metadata_masked(match
, htonll(0), htonll(0));
1564 match
->flow
.in_port
= 0;
1565 match
->wc
.masks
.in_port
= 0;
1568 case MFF_SKB_PRIORITY
:
1569 match
->flow
.skb_priority
= 0;
1570 match
->wc
.masks
.skb_priority
= 0;
1574 match
->flow
.skb_mark
= 0;
1575 match
->wc
.masks
.skb_mark
= 0;
1579 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1583 memset(match
->flow
.dl_src
, 0, ETH_ADDR_LEN
);
1584 memset(match
->wc
.masks
.dl_src
, 0, ETH_ADDR_LEN
);
1588 memset(match
->flow
.dl_dst
, 0, ETH_ADDR_LEN
);
1589 memset(match
->wc
.masks
.dl_dst
, 0, ETH_ADDR_LEN
);
1593 match
->flow
.dl_type
= htons(0);
1594 match
->wc
.masks
.dl_type
= htons(0);
1598 match_set_dl_tci_masked(match
, htons(0), htons(0));
1603 match_set_any_vid(match
);
1606 case MFF_DL_VLAN_PCP
:
1608 match_set_any_pcp(match
);
1611 case MFF_MPLS_LABEL
:
1612 match_set_any_mpls_label(match
);
1616 match_set_any_mpls_tc(match
);
1620 match_set_any_mpls_bos(match
);
1625 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1630 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1634 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1635 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1639 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1640 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1643 case MFF_IPV6_LABEL
:
1644 match
->wc
.masks
.ipv6_label
= htonl(0);
1645 match
->flow
.ipv6_label
= htonl(0);
1649 match
->wc
.masks
.nw_proto
= 0;
1650 match
->flow
.nw_proto
= 0;
1654 case MFF_IP_DSCP_SHIFTED
:
1655 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1656 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1660 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1661 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1665 match
->wc
.masks
.nw_ttl
= 0;
1666 match
->flow
.nw_ttl
= 0;
1670 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
1671 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1675 match
->wc
.masks
.nw_proto
= 0;
1676 match
->flow
.nw_proto
= 0;
1681 memset(match
->flow
.arp_sha
, 0, ETH_ADDR_LEN
);
1682 memset(match
->wc
.masks
.arp_sha
, 0, ETH_ADDR_LEN
);
1687 memset(match
->flow
.arp_tha
, 0, ETH_ADDR_LEN
);
1688 memset(match
->wc
.masks
.arp_tha
, 0, ETH_ADDR_LEN
);
1693 case MFF_ICMPV4_TYPE
:
1694 case MFF_ICMPV6_TYPE
:
1695 match
->wc
.masks
.tp_src
= htons(0);
1696 match
->flow
.tp_src
= htons(0);
1701 case MFF_ICMPV4_CODE
:
1702 case MFF_ICMPV6_CODE
:
1703 match
->wc
.masks
.tp_dst
= htons(0);
1704 match
->flow
.tp_dst
= htons(0);
1708 memset(&match
->wc
.masks
.nd_target
, 0,
1709 sizeof match
->wc
.masks
.nd_target
);
1710 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1719 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1720 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1721 * with a 1-bit indicating that the corresponding value bit must match and a
1722 * 0-bit indicating a don't-care.
1724 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1725 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1726 * call is equivalent to mf_set_wild(mf, match).
1728 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1729 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1731 mf_set(const struct mf_field
*mf
,
1732 const union mf_value
*value
, const union mf_value
*mask
,
1733 struct match
*match
)
1735 if (!mask
|| is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
1736 mf_set_value(mf
, value
, match
);
1738 } else if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
1739 mf_set_wild(mf
, match
);
1746 case MFF_SKB_PRIORITY
:
1749 case MFF_DL_VLAN_PCP
:
1751 case MFF_MPLS_LABEL
:
1757 case MFF_IP_DSCP_SHIFTED
:
1760 case MFF_ICMPV4_TYPE
:
1761 case MFF_ICMPV4_CODE
:
1762 case MFF_ICMPV6_TYPE
:
1763 case MFF_ICMPV6_CODE
:
1767 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
1770 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
1773 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
1776 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
1779 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
1782 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
1786 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
1790 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
1791 ntohl(value
->be32
), ntohl(mask
->be32
));
1795 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
1799 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
1804 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
1809 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
1813 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
1817 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
1821 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1825 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1829 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1833 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1836 case MFF_IPV6_LABEL
:
1837 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1838 mf_set_value(mf
, value
, match
);
1840 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
1845 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1849 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
1853 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1857 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1862 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
1867 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
1877 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1881 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1882 } else if (!sf
->n_bits
) {
1883 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1884 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1885 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1886 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1887 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1888 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1889 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1890 sf
->field
->n_bits
, type
, sf
->field
->name
);
1891 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1892 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1893 type
, sf
->field
->name
);
1898 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1901 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1902 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1905 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1907 return mf_check__(sf
, flow
, "source");
1910 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1911 * if so, otherwise an OpenFlow error code (e.g. as returned by
1914 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1916 int error
= mf_check__(sf
, flow
, "destination");
1917 if (!error
&& !sf
->field
->writable
) {
1918 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1920 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1925 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1926 * 'value' and 'mask', respectively. */
1928 mf_get(const struct mf_field
*mf
, const struct match
*match
,
1929 union mf_value
*value
, union mf_value
*mask
)
1931 mf_get_value(mf
, &match
->flow
, value
);
1932 mf_get_mask(mf
, &match
->wc
, mask
);
1935 /* Assigns a random value for field 'mf' to 'value'. */
1937 mf_random_value(const struct mf_field
*mf
, union mf_value
*value
)
1939 random_bytes(value
, mf
->n_bytes
);
1951 case MFF_SKB_PRIORITY
:
1971 case MFF_ICMPV4_TYPE
:
1972 case MFF_ICMPV4_CODE
:
1973 case MFF_ICMPV6_TYPE
:
1974 case MFF_ICMPV6_CODE
:
1980 case MFF_IPV6_LABEL
:
1981 value
->be32
&= ~htonl(IPV6_LABEL_MASK
);
1985 value
->u8
&= IP_DSCP_MASK
;
1988 case MFF_IP_DSCP_SHIFTED
:
1989 value
->u8
&= IP_DSCP_MASK
>> 2;
1993 value
->u8
&= IP_ECN_MASK
;
1997 value
->u8
&= FLOW_NW_FRAG_MASK
;
2001 value
->be16
&= htons(0xff);
2005 value
->be16
&= htons(VLAN_VID_MASK
);
2008 value
->be16
&= htons(VLAN_VID_MASK
| VLAN_CFI
);
2011 case MFF_DL_VLAN_PCP
:
2016 case MFF_MPLS_LABEL
:
2017 value
->be32
&= htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
);
2021 value
->u8
&= MPLS_TC_MASK
>> MPLS_TC_SHIFT
;
2025 value
->u8
&= MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
;
2035 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2036 uint8_t *valuep
, uint8_t *maskp
)
2038 unsigned long long int integer
, mask
;
2043 integer
= strtoull(s
, &tail
, 0);
2044 if (errno
|| (*tail
!= '\0' && *tail
!= '/')) {
2049 mask
= strtoull(tail
+ 1, &tail
, 0);
2050 if (errno
|| *tail
!= '\0') {
2057 for (i
= mf
->n_bytes
- 1; i
>= 0; i
--) {
2058 valuep
[i
] = integer
;
2064 return xasprintf("%s: value too large for %u-byte field %s",
2065 s
, mf
->n_bytes
, mf
->name
);
2070 return xasprintf("%s: bad syntax for %s", s
, mf
->name
);
2074 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2075 uint8_t mac
[ETH_ADDR_LEN
],
2076 uint8_t mask
[ETH_ADDR_LEN
])
2078 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2080 switch (sscanf(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
,
2081 ETH_ADDR_SCAN_ARGS(mac
), ETH_ADDR_SCAN_ARGS(mask
))){
2082 case ETH_ADDR_SCAN_COUNT
* 2:
2085 case ETH_ADDR_SCAN_COUNT
:
2086 memset(mask
, 0xff, ETH_ADDR_LEN
);
2090 return xasprintf("%s: invalid Ethernet address", s
);
2095 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2096 ovs_be32
*ip
, ovs_be32
*mask
)
2100 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2102 if (sscanf(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
2103 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
)) == IP_SCAN_COUNT
* 2) {
2105 } else if (sscanf(s
, IP_SCAN_FMT
"/%d",
2106 IP_SCAN_ARGS(ip
), &prefix
) == IP_SCAN_COUNT
+ 1) {
2107 if (prefix
<= 0 || prefix
> 32) {
2108 return xasprintf("%s: network prefix bits not between 1 and "
2110 } else if (prefix
== 32) {
2111 *mask
= htonl(UINT32_MAX
);
2113 *mask
= htonl(((1u << prefix
) - 1) << (32 - prefix
));
2115 } else if (sscanf(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
)) == IP_SCAN_COUNT
) {
2116 *mask
= htonl(UINT32_MAX
);
2118 return xasprintf("%s: invalid IP address", s
);
2124 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2125 struct in6_addr
*value
, struct in6_addr
*mask
)
2127 char *str
= xstrdup(s
);
2128 char *save_ptr
= NULL
;
2129 const char *name
, *netmask
;
2132 ovs_assert(mf
->n_bytes
== sizeof *value
);
2134 name
= strtok_r(str
, "/", &save_ptr
);
2135 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
2139 err
= xasprintf("%s: could not convert to IPv6 address", str
);
2145 netmask
= strtok_r(NULL
, "/", &save_ptr
);
2147 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
2148 int prefix
= atoi(netmask
);
2149 if (prefix
<= 0 || prefix
> 128) {
2151 return xasprintf("%s: prefix bits not between 1 and 128", s
);
2153 *mask
= ipv6_create_mask(prefix
);
2157 *mask
= in6addr_exact
;
2165 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2166 ovs_be16
*valuep
, ovs_be16
*maskp
)
2170 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2172 if (ofputil_port_from_string(s
, &port
)) {
2173 *valuep
= htons(port
);
2174 *maskp
= htons(UINT16_MAX
);
2177 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2180 struct frag_handling
{
2186 static const struct frag_handling all_frags
[] = {
2187 #define A FLOW_NW_FRAG_ANY
2188 #define L FLOW_NW_FRAG_LATER
2189 /* name mask value */
2192 { "first", A
|L
, A
},
2193 { "later", A
|L
, A
|L
},
2198 { "not_later", L
, 0 },
2205 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2207 const struct frag_handling
*h
;
2209 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2210 if (!strcasecmp(s
, h
->name
)) {
2211 /* We force the upper bits of the mask on to make mf_parse_value()
2212 * happy (otherwise it will never think it's an exact match.) */
2213 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2219 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2220 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2224 parse_flow_tun_flags(const char *s_
, const char *(*bit_to_string
)(uint32_t),
2227 uint32_t result
= 0;
2228 char *save_ptr
= NULL
;
2231 char *s
= xstrdup(s_
);
2233 for (name
= strtok_r((char *)s
, " |", &save_ptr
); name
;
2234 name
= strtok_r(NULL
, " |", &save_ptr
)) {
2236 unsigned long long int flags
;
2240 if (sscanf(name
, "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
2244 name_len
= strlen(name
);
2245 for (bit
= 1; bit
; bit
<<= 1) {
2246 const char *fname
= bit_to_string(bit
);
2253 len
= strlen(fname
);
2254 if (len
!= name_len
) {
2257 if (!strncmp(name
, fname
, len
)) {
2269 *res
= htons(result
);
2276 mf_from_tun_flags_string(const char *s
, ovs_be16
*valuep
, ovs_be16
*maskp
)
2278 if (!parse_flow_tun_flags(s
, flow_tun_flag_to_string
, valuep
)) {
2279 *maskp
= htons(UINT16_MAX
);
2283 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2284 "\"csum\", \"key\"", s
);
2287 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2288 * NULL if successful, otherwise a malloc()'d string describing the error. */
2290 mf_parse(const struct mf_field
*mf
, const char *s
,
2291 union mf_value
*value
, union mf_value
*mask
)
2293 if (!strcmp(s
, "*")) {
2294 memset(value
, 0, mf
->n_bytes
);
2295 memset(mask
, 0, mf
->n_bytes
);
2299 switch (mf
->string
) {
2301 case MFS_HEXADECIMAL
:
2302 return mf_from_integer_string(mf
, s
,
2303 (uint8_t *) value
, (uint8_t *) mask
);
2306 return mf_from_ethernet_string(mf
, s
, value
->mac
, mask
->mac
);
2309 return mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2312 return mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2315 return mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
2318 return mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2321 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2322 return mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2327 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2328 * successful, otherwise a malloc()'d string describing the error. */
2330 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
2332 union mf_value mask
;
2335 error
= mf_parse(mf
, s
, value
, &mask
);
2340 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2341 return xasprintf("%s: wildcards not allowed here", s
);
2347 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2348 const uint8_t *maskp
, struct ds
*s
)
2350 unsigned long long int integer
;
2353 ovs_assert(mf
->n_bytes
<= 8);
2356 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2357 integer
= (integer
<< 8) | valuep
[i
];
2359 if (mf
->string
== MFS_HEXADECIMAL
) {
2360 ds_put_format(s
, "%#llx", integer
);
2362 ds_put_format(s
, "%lld", integer
);
2366 unsigned long long int mask
;
2369 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2370 mask
= (mask
<< 8) | maskp
[i
];
2373 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2374 * not sure that that a bit-mask written in decimal is ever easier to
2375 * understand than the same bit-mask written in hexadecimal. */
2376 ds_put_format(s
, "/%#llx", mask
);
2381 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2383 const struct frag_handling
*h
;
2385 mask
&= FLOW_NW_FRAG_MASK
;
2388 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2389 if (value
== h
->value
&& mask
== h
->mask
) {
2390 ds_put_cstr(s
, h
->name
);
2394 ds_put_cstr(s
, "<error>");
2398 mf_format_tnl_flags_string(const ovs_be16
*valuep
, struct ds
*s
)
2400 format_flags(s
, flow_tun_flag_to_string
, ntohs(*valuep
), '|');
2403 /* Appends to 's' a string representation of field 'mf' whose value is in
2404 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2406 mf_format(const struct mf_field
*mf
,
2407 const union mf_value
*value
, const union mf_value
*mask
,
2411 if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
2412 ds_put_cstr(s
, "ANY");
2414 } else if (is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
2419 switch (mf
->string
) {
2422 ofputil_format_port(ntohs(value
->be16
), s
);
2427 case MFS_HEXADECIMAL
:
2428 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2432 eth_format_masked(value
->mac
, mask
->mac
, s
);
2436 ip_format_masked(value
->be32
, mask
? mask
->be32
: htonl(UINT32_MAX
),
2441 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2445 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2449 mf_format_tnl_flags_string(&value
->be16
, s
);
2457 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2458 * least-significant bits in 'x'.
2461 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2462 const union mf_subvalue
*x
, struct flow
*flow
)
2464 const struct mf_field
*field
= sf
->field
;
2465 union mf_value value
;
2467 mf_get_value(field
, flow
, &value
);
2468 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2469 sf
->ofs
, sf
->n_bits
);
2470 mf_set_flow_value(field
, &value
, flow
);
2473 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2474 * least-significant bits in 'x'.
2477 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2478 struct match
*match
)
2480 const struct mf_field
*field
= sf
->field
;
2481 union mf_value value
, mask
;
2483 mf_get(field
, match
, &value
, &mask
);
2484 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2485 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2486 mf_set(field
, &value
, &mask
, match
);
2489 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2490 * reading 'flow', e.g. as checked by mf_check_src(). */
2492 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2493 union mf_subvalue
*x
)
2495 union mf_value value
;
2497 mf_get_value(sf
->field
, flow
, &value
);
2499 memset(x
, 0, sizeof *x
);
2500 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2505 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2506 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2509 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2511 union mf_value value
;
2513 mf_get_value(sf
->field
, flow
, &value
);
2514 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2517 /* Formats 'sf' into 's' in a format normally acceptable to
2518 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2519 * sf->field has no NXM name.) */
2521 mf_format_subfield(const struct mf_subfield
*sf
, struct ds
*s
)
2524 ds_put_cstr(s
, "<unknown>");
2525 } else if (sf
->field
->nxm_name
) {
2526 ds_put_cstr(s
, sf
->field
->nxm_name
);
2527 } else if (sf
->field
->nxm_header
) {
2528 uint32_t header
= sf
->field
->nxm_header
;
2529 ds_put_format(s
, "%d:%d", NXM_VENDOR(header
), NXM_FIELD(header
));
2531 ds_put_cstr(s
, sf
->field
->name
);
2534 if (sf
->field
&& sf
->ofs
== 0 && sf
->n_bits
== sf
->field
->n_bits
) {
2535 ds_put_cstr(s
, "[]");
2536 } else if (sf
->n_bits
== 1) {
2537 ds_put_format(s
, "[%d]", sf
->ofs
);
2539 ds_put_format(s
, "[%d..%d]", sf
->ofs
, sf
->ofs
+ sf
->n_bits
- 1);
2543 static const struct mf_field
*
2544 mf_parse_subfield_name(const char *name
, int name_len
, bool *wild
)
2548 *wild
= name_len
> 2 && !memcmp(&name
[name_len
- 2], "_W", 2);
2553 for (i
= 0; i
< MFF_N_IDS
; i
++) {
2554 const struct mf_field
*mf
= mf_from_id(i
);
2557 && !strncmp(mf
->nxm_name
, name
, name_len
)
2558 && mf
->nxm_name
[name_len
] == '\0') {
2562 && !strncmp(mf
->oxm_name
, name
, name_len
)
2563 && mf
->oxm_name
[name_len
] == '\0') {
2571 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2572 * returns NULL and advances '*sp' to the first byte following the parsed
2573 * string. On failure, returns a malloc()'d error message, does not modify
2574 * '*sp', and does not properly initialize 'sf'.
2576 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2577 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2578 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2579 * may both be omitted (the [] are still required) to indicate an entire
2582 mf_parse_subfield__(struct mf_subfield
*sf
, const char **sp
)
2584 const struct mf_field
*field
;
2593 name_len
= strcspn(s
, "[");
2594 if (s
[name_len
] != '[') {
2595 return xasprintf("%s: missing [ looking for field name", *sp
);
2598 field
= mf_parse_subfield_name(name
, name_len
, &wild
);
2600 return xasprintf("%s: unknown field `%.*s'", *sp
, name_len
, s
);
2604 if (sscanf(s
, "[%d..%d]", &start
, &end
) == 2) {
2605 /* Nothing to do. */
2606 } else if (sscanf(s
, "[%d]", &start
) == 1) {
2608 } else if (!strncmp(s
, "[]", 2)) {
2610 end
= field
->n_bits
- 1;
2612 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2613 "[<start>..<end>]", *sp
);
2615 s
= strchr(s
, ']') + 1;
2618 return xasprintf("%s: starting bit %d is after ending bit %d",
2620 } else if (start
>= field
->n_bits
) {
2621 return xasprintf("%s: starting bit %d is not valid because field is "
2622 "only %d bits wide", *sp
, start
, field
->n_bits
);
2623 } else if (end
>= field
->n_bits
){
2624 return xasprintf("%s: ending bit %d is not valid because field is "
2625 "only %d bits wide", *sp
, end
, field
->n_bits
);
2630 sf
->n_bits
= end
- start
+ 1;
2636 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2637 * byte in 's' following the parsed string.
2639 * Exits with an error message if 's' has incorrect syntax.
2641 * The syntax parsed from 's' takes the form "header[start..end]" where
2642 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2643 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2644 * may both be omitted (the [] are still required) to indicate an entire
2647 mf_parse_subfield(struct mf_subfield
*sf
, const char *s
)
2649 char *msg
= mf_parse_subfield__(sf
, &s
);
2651 ovs_fatal(0, "%s", msg
);
2657 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2661 for (i
= 0; i
< ARRAY_SIZE(subvalue
->u8
); i
++) {
2662 if (subvalue
->u8
[i
]) {
2663 ds_put_format(s
, "0x%"PRIx8
, subvalue
->u8
[i
]);
2664 for (i
++; i
< ARRAY_SIZE(subvalue
->u8
); i
++) {
2665 ds_put_format(s
, "%02"PRIx8
, subvalue
->u8
[i
]);
2670 ds_put_char(s
, '0');