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 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
121 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
122 MF_FIELD_SIZES(be32),
127 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
128 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
130 MFF_SKB_PRIORITY, "skb_priority", NULL,
131 MF_FIELD_SIZES(be32),
139 MFF_SKB_MARK, "skb_mark", NULL,
140 MF_FIELD_SIZES(be32),
149 #define REGISTER(IDX) \
151 MFF_REG##IDX, "reg" #IDX, NULL, \
152 MF_FIELD_SIZES(be32), \
157 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
158 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
193 MFF_ETH_SRC, "eth_src", "dl_src",
199 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
200 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
202 MFF_ETH_DST, "eth_dst", "dl_dst",
208 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
209 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
211 MFF_ETH_TYPE, "eth_type", "dl_type",
212 MF_FIELD_SIZES(be16),
217 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
218 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
222 MFF_VLAN_TCI, "vlan_tci", NULL,
223 MF_FIELD_SIZES(be16),
228 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
229 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
231 MFF_DL_VLAN, "dl_vlan", NULL,
232 sizeof(ovs_be16), 12,
240 MFF_VLAN_VID, "vlan_vid", NULL,
241 sizeof(ovs_be16), 12,
246 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
247 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
249 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
258 MFF_VLAN_PCP, "vlan_pcp", NULL,
264 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
265 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
272 MFF_MPLS_LABEL, "mpls_label", NULL,
278 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
279 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
281 MFF_MPLS_TC, "mpls_tc", NULL,
287 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
288 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
290 MFF_MPLS_BOS, "mpls_bos", NULL,
296 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
297 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
305 MFF_IPV4_SRC, "ip_src", "nw_src",
306 MF_FIELD_SIZES(be32),
311 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
312 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
314 MFF_IPV4_DST, "ip_dst", "nw_dst",
315 MF_FIELD_SIZES(be32),
320 NXM_OF_IP_DST, "NXM_OF_IP_DST",
321 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
325 MFF_IPV6_SRC, "ipv6_src", NULL,
326 MF_FIELD_SIZES(ipv6),
331 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
332 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
334 MFF_IPV6_DST, "ipv6_dst", NULL,
335 MF_FIELD_SIZES(ipv6),
340 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
341 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
344 MFF_IPV6_LABEL, "ipv6_label", NULL,
350 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
351 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
355 MFF_IP_PROTO, "nw_proto", NULL,
361 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
362 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
364 MFF_IP_DSCP, "nw_tos", NULL,
370 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
371 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
373 MFF_IP_DSCP_SHIFTED, "nw_tos_shifted", NULL,
379 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
380 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
382 MFF_IP_ECN, "nw_ecn", NULL,
388 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
389 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
391 MFF_IP_TTL, "nw_ttl", NULL,
397 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
398 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
400 MFF_IP_FRAG, "ip_frag", NULL,
406 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
407 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
411 MFF_ARP_OP, "arp_op", NULL,
412 MF_FIELD_SIZES(be16),
417 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
418 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
420 MFF_ARP_SPA, "arp_spa", NULL,
421 MF_FIELD_SIZES(be32),
426 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
427 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
429 MFF_ARP_TPA, "arp_tpa", NULL,
430 MF_FIELD_SIZES(be32),
435 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
436 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
438 MFF_ARP_SHA, "arp_sha", NULL,
444 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
445 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
447 MFF_ARP_THA, "arp_tha", NULL,
453 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
454 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
462 MFF_TCP_SRC, "tcp_src", "tp_src",
463 MF_FIELD_SIZES(be16),
468 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
469 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
471 MFF_TCP_DST, "tcp_dst", "tp_dst",
472 MF_FIELD_SIZES(be16),
477 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
478 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
482 MFF_UDP_SRC, "udp_src", NULL,
483 MF_FIELD_SIZES(be16),
488 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
489 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
491 MFF_UDP_DST, "udp_dst", NULL,
492 MF_FIELD_SIZES(be16),
497 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
498 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
502 MFF_ICMPV4_TYPE, "icmp_type", NULL,
508 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
509 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
511 MFF_ICMPV4_CODE, "icmp_code", NULL,
517 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
518 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
522 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
528 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
529 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
531 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
537 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
538 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
546 MFF_ND_TARGET, "nd_target", NULL,
547 MF_FIELD_SIZES(ipv6),
552 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
553 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
555 MFF_ND_SLL, "nd_sll", NULL,
561 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
562 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
564 MFF_ND_TLL, "nd_tll", NULL,
570 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
571 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
575 /* Maps an NXM or OXM header value to an mf_field. */
577 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
578 uint32_t header; /* NXM or OXM header value. */
579 const struct mf_field *mf;
582 /* Contains 'struct nxm_field's. */
583 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
585 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
586 * controller and so there's not much point in showing a lot of them. */
587 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
589 const struct mf_field *mf_from_nxm_header__(uint32_t header);
591 /* Returns the field with the given 'id'. */
592 const struct mf_field *
593 mf_from_id(enum mf_field_id id)
595 ovs_assert((unsigned int) id < MFF_N_IDS);
596 return &mf_fields[id];
599 /* Returns the field with the given 'name', or a null pointer if no field has
601 const struct mf_field *
602 mf_from_name(const char *name)
604 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
606 if (shash_is_empty(&mf_by_name)) {
607 const struct mf_field *mf;
609 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
610 shash_add_once(&mf_by_name, mf->name, mf);
611 if (mf->extra_name) {
612 shash_add_once(&mf_by_name, mf->extra_name, mf);
617 return shash_find_data(&mf_by_name, name);
621 add_nxm_field(uint32_t header, const struct mf_field *mf)
625 f = xmalloc(sizeof *f);
626 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
632 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
635 ovs_assert(!mf_from_nxm_header__(header));
636 add_nxm_field(header, mf);
637 if (mf->maskable != MFM_NONE) {
638 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
646 const struct mf_field *mf;
648 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
649 nxm_init_add_field(mf, mf->nxm_header);
650 if (mf->oxm_header != mf->nxm_header) {
651 nxm_init_add_field(mf, mf->oxm_header);
656 const struct mf_field *
657 mf_from_nxm_header(uint32_t header)
659 if (hmap_is_empty(&all_fields)) {
662 return mf_from_nxm_header__(header);
665 const struct mf_field *
666 mf_from_nxm_header__(uint32_t header)
668 const struct nxm_field *f;
670 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
671 if (f->header == header) {
679 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
680 * specifies at least one bit in the field.
682 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
683 * meets 'mf''s prerequisites. */
685 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
689 return !wc->masks.tunnel.ip_src;
691 return !wc->masks.tunnel.ip_dst;
696 return !wc->masks.tunnel.tun_id;
698 return !wc->masks.metadata;
700 case MFF_IN_PORT_OXM:
701 return !wc->masks.in_port;
702 case MFF_SKB_PRIORITY:
703 return !wc->masks.skb_priority;
705 return !wc->masks.skb_mark;
707 return !wc->masks.regs[mf->id - MFF_REG0];
710 return eth_addr_is_zero(wc->masks.dl_src);
712 return eth_addr_is_zero(wc->masks.dl_dst);
714 return !wc->masks.dl_type;
718 return eth_addr_is_zero(wc->masks.arp_sha);
722 return eth_addr_is_zero(wc->masks.arp_tha);
725 return !wc->masks.vlan_tci;
727 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
729 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
730 case MFF_DL_VLAN_PCP:
732 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
735 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
737 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
739 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
742 return !wc->masks.nw_src;
744 return !wc->masks.nw_dst;
747 return ipv6_mask_is_any(&wc->masks.ipv6_src);
749 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
752 return !wc->masks.ipv6_label;
755 return !wc->masks.nw_proto;
757 case MFF_IP_DSCP_SHIFTED:
758 return !(wc->masks.nw_tos & IP_DSCP_MASK);
760 return !(wc->masks.nw_tos & IP_ECN_MASK);
762 return !wc->masks.nw_ttl;
765 return ipv6_mask_is_any(&wc->masks.nd_target);
768 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
771 return !wc->masks.nw_proto;
773 return !wc->masks.nw_src;
775 return !wc->masks.nw_dst;
779 case MFF_ICMPV4_TYPE:
780 case MFF_ICMPV6_TYPE:
781 return !wc->masks.tp_src;
784 case MFF_ICMPV4_CODE:
785 case MFF_ICMPV6_CODE:
786 return !wc->masks.tp_dst;
794 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
795 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
796 * purposes, or to 0 if it is wildcarded.
798 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
799 * meets 'mf''s prerequisites. */
801 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
802 union mf_value *mask)
804 mf_get_value(mf, &wc->masks, mask);
807 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
808 * if the mask is valid, false otherwise. */
810 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
812 switch (mf->maskable) {
814 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
815 is_all_ones((const uint8_t *) mask, mf->n_bytes));
825 is_icmpv4(const struct flow *flow)
827 return (flow->dl_type == htons(ETH_TYPE_IP)
828 && flow->nw_proto == IPPROTO_ICMP);
832 is_icmpv6(const struct flow *flow)
834 return (flow->dl_type == htons(ETH_TYPE_IPV6)
835 && flow->nw_proto == IPPROTO_ICMPV6);
838 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
840 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
842 switch (mf->prereqs) {
847 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
848 flow->dl_type == htons(ETH_TYPE_RARP));
850 return flow->dl_type == htons(ETH_TYPE_IP);
852 return flow->dl_type == htons(ETH_TYPE_IPV6);
854 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
856 return eth_type_mpls(flow->dl_type);
858 return is_ip_any(flow);
861 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
863 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
865 return is_icmpv4(flow);
867 return is_icmpv6(flow);
870 return (is_icmpv6(flow)
871 && flow->tp_dst == htons(0)
872 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
873 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
875 return (is_icmpv6(flow)
876 && flow->tp_dst == htons(0)
877 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
879 return (is_icmpv6(flow)
880 && flow->tp_dst == htons(0)
881 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
887 /* Returns true if 'value' may be a valid value *as part of a masked match*,
890 * A value is not rejected just because it is not valid for the field in
891 * question, but only if it doesn't make sense to test the bits in question at
892 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
893 * without the VLAN_CFI bit being set, but we can't reject those values because
894 * it is still legitimate to test just for those bits (see the documentation
895 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
896 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
898 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
909 case MFF_SKB_PRIORITY:
930 case MFF_ICMPV4_TYPE:
931 case MFF_ICMPV4_CODE:
932 case MFF_ICMPV6_TYPE:
933 case MFF_ICMPV6_CODE:
939 case MFF_IN_PORT_OXM: {
941 return !ofputil_port_from_ofp11(value->be32, &port);
945 return !(value->u8 & ~IP_DSCP_MASK);
946 case MFF_IP_DSCP_SHIFTED:
947 return !(value->u8 & (~IP_DSCP_MASK >> 2));
949 return !(value->u8 & ~IP_ECN_MASK);
951 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
954 return !(value->be16 & htons(0xff00));
957 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
959 return !(value->be16 & htons(VLAN_PCP_MASK));
961 case MFF_DL_VLAN_PCP:
963 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
966 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
969 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
972 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
975 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
983 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
984 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
986 mf_get_value(const struct mf_field *mf, const struct flow *flow,
987 union mf_value *value)
991 value->be64 = flow->tunnel.tun_id;
994 value->be32 = flow->tunnel.ip_src;
997 value->be32 = flow->tunnel.ip_dst;
1000 value->be16 = htons(flow->tunnel.flags);
1003 value->u8 = flow->tunnel.ip_ttl;
1006 value->u8 = flow->tunnel.ip_tos;
1010 value->be64 = flow->metadata;
1014 value->be16 = htons(flow->in_port);
1017 case MFF_IN_PORT_OXM:
1018 value->be32 = ofputil_port_to_ofp11(flow->in_port);
1021 case MFF_SKB_PRIORITY:
1022 value->be32 = htonl(flow->skb_priority);
1026 value->be32 = htonl(flow->skb_mark);
1030 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1034 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1038 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1042 value->be16 = flow->dl_type;
1046 value->be16 = flow->vlan_tci;
1050 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1053 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1056 case MFF_DL_VLAN_PCP:
1058 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1061 case MFF_MPLS_LABEL:
1062 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1066 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1070 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1074 value->be32 = flow->nw_src;
1078 value->be32 = flow->nw_dst;
1082 value->ipv6 = flow->ipv6_src;
1086 value->ipv6 = flow->ipv6_dst;
1089 case MFF_IPV6_LABEL:
1090 value->be32 = flow->ipv6_label;
1094 value->u8 = flow->nw_proto;
1098 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1101 case MFF_IP_DSCP_SHIFTED:
1102 value->u8 = flow->nw_tos >> 2;
1106 value->u8 = flow->nw_tos & IP_ECN_MASK;
1110 value->u8 = flow->nw_ttl;
1114 value->u8 = flow->nw_frag;
1118 value->be16 = htons(flow->nw_proto);
1122 value->be32 = flow->nw_src;
1126 value->be32 = flow->nw_dst;
1131 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1136 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1141 value->be16 = flow->tp_src;
1146 value->be16 = flow->tp_dst;
1149 case MFF_ICMPV4_TYPE:
1150 case MFF_ICMPV6_TYPE:
1151 value->u8 = ntohs(flow->tp_src);
1154 case MFF_ICMPV4_CODE:
1155 case MFF_ICMPV6_CODE:
1156 value->u8 = ntohs(flow->tp_dst);
1160 value->ipv6 = flow->nd_target;
1169 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1170 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1173 mf_set_value(const struct mf_field *mf,
1174 const union mf_value *value, struct match *match)
1178 match_set_tun_id(match, value->be64);
1181 match_set_tun_src(match, value->be32);
1184 match_set_tun_dst(match, value->be32);
1187 match_set_tun_flags(match, ntohs(value->be16));
1190 match_set_tun_tos(match, value->u8);
1193 match_set_tun_ttl(match, value->u8);
1197 match_set_metadata(match, value->be64);
1201 match_set_in_port(match, ntohs(value->be16));
1204 case MFF_IN_PORT_OXM: {
1206 if (ofputil_port_from_ofp11(value->be32, &port)) {
1209 match_set_in_port(match, port);
1213 case MFF_SKB_PRIORITY:
1214 match_set_skb_priority(match, ntohl(value->be32));
1218 match_set_skb_mark(match, ntohl(value->be32));
1222 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1226 match_set_dl_src(match, value->mac);
1230 match_set_dl_dst(match, value->mac);
1234 match_set_dl_type(match, value->be16);
1238 match_set_dl_tci(match, value->be16);
1242 match_set_dl_vlan(match, value->be16);
1245 match_set_vlan_vid(match, value->be16);
1248 case MFF_DL_VLAN_PCP:
1250 match_set_dl_vlan_pcp(match, value->u8);
1253 case MFF_MPLS_LABEL:
1254 match_set_mpls_label(match, value->be32);
1258 match_set_mpls_tc(match, value->u8);
1262 match_set_mpls_bos(match, value->u8);
1266 match_set_nw_src(match, value->be32);
1270 match_set_nw_dst(match, value->be32);
1274 match_set_ipv6_src(match, &value->ipv6);
1278 match_set_ipv6_dst(match, &value->ipv6);
1281 case MFF_IPV6_LABEL:
1282 match_set_ipv6_label(match, value->be32);
1286 match_set_nw_proto(match, value->u8);
1290 match_set_nw_dscp(match, value->u8);
1293 case MFF_IP_DSCP_SHIFTED:
1294 match_set_nw_dscp(match, value->u8 << 2);
1298 match_set_nw_ecn(match, value->u8);
1302 match_set_nw_ttl(match, value->u8);
1306 match_set_nw_frag(match, value->u8);
1310 match_set_nw_proto(match, ntohs(value->be16));
1314 match_set_nw_src(match, value->be32);
1318 match_set_nw_dst(match, value->be32);
1323 match_set_arp_sha(match, value->mac);
1328 match_set_arp_tha(match, value->mac);
1333 match_set_tp_src(match, value->be16);
1338 match_set_tp_dst(match, value->be16);
1341 case MFF_ICMPV4_TYPE:
1342 case MFF_ICMPV6_TYPE:
1343 match_set_icmp_type(match, value->u8);
1346 case MFF_ICMPV4_CODE:
1347 case MFF_ICMPV6_CODE:
1348 match_set_icmp_code(match, value->u8);
1352 match_set_nd_target(match, &value->ipv6);
1361 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1362 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1365 mf_set_flow_value(const struct mf_field *mf,
1366 const union mf_value *value, struct flow *flow)
1370 flow->tunnel.tun_id = value->be64;
1373 flow->tunnel.ip_src = value->be32;
1376 flow->tunnel.ip_dst = value->be32;
1379 flow->tunnel.flags = ntohs(value->be16);
1382 flow->tunnel.ip_tos = value->u8;
1385 flow->tunnel.ip_ttl = value->u8;
1389 flow->metadata = value->be64;
1393 flow->in_port = ntohs(value->be16);
1396 case MFF_IN_PORT_OXM: {
1398 if (ofputil_port_from_ofp11(value->be32, &port)) {
1401 flow->in_port = port;
1405 case MFF_SKB_PRIORITY:
1406 flow->skb_priority = ntohl(value->be32);
1410 flow->skb_mark = ntohl(value->be32);
1414 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1418 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1422 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1426 flow->dl_type = value->be16;
1430 flow->vlan_tci = value->be16;
1434 flow_set_dl_vlan(flow, value->be16);
1437 flow_set_vlan_vid(flow, value->be16);
1440 case MFF_DL_VLAN_PCP:
1442 flow_set_vlan_pcp(flow, value->u8);
1445 case MFF_MPLS_LABEL:
1446 flow_set_mpls_label(flow, value->be32);
1450 flow_set_mpls_tc(flow, value->u8);
1454 flow_set_mpls_bos(flow, value->u8);
1458 flow->nw_src = value->be32;
1462 flow->nw_dst = value->be32;
1466 flow->ipv6_src = value->ipv6;
1470 flow->ipv6_dst = value->ipv6;
1473 case MFF_IPV6_LABEL:
1474 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1478 flow->nw_proto = value->u8;
1482 flow->nw_tos &= ~IP_DSCP_MASK;
1483 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1486 case MFF_IP_DSCP_SHIFTED:
1487 flow->nw_tos &= ~IP_DSCP_MASK;
1488 flow->nw_tos |= value->u8 << 2;
1492 flow->nw_tos &= ~IP_ECN_MASK;
1493 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1497 flow->nw_ttl = value->u8;
1501 flow->nw_frag &= value->u8;
1505 flow->nw_proto = ntohs(value->be16);
1509 flow->nw_src = value->be32;
1513 flow->nw_dst = value->be32;
1518 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1523 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1528 flow->tp_src = value->be16;
1533 flow->tp_dst = value->be16;
1536 case MFF_ICMPV4_TYPE:
1537 case MFF_ICMPV6_TYPE:
1538 flow->tp_src = htons(value->u8);
1541 case MFF_ICMPV4_CODE:
1542 case MFF_ICMPV6_CODE:
1543 flow->tp_dst = htons(value->u8);
1547 flow->nd_target = value->ipv6;
1556 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1558 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1561 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1563 union mf_value value;
1565 mf_get_value(mf, flow, &value);
1566 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1569 /* Makes 'match' wildcard field 'mf'.
1571 * The caller is responsible for ensuring that 'match' meets 'mf''s
1574 mf_set_wild(const struct mf_field *mf, struct match *match)
1578 match_set_tun_id_masked(match, htonll(0), htonll(0));
1581 match_set_tun_src_masked(match, htonl(0), htonl(0));
1584 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1587 match_set_tun_flags_masked(match, 0, 0);
1590 match_set_tun_tos_masked(match, 0, 0);
1593 match_set_tun_ttl_masked(match, 0, 0);
1597 match_set_metadata_masked(match, htonll(0), htonll(0));
1601 case MFF_IN_PORT_OXM:
1602 match->flow.in_port = 0;
1603 match->wc.masks.in_port = 0;
1606 case MFF_SKB_PRIORITY:
1607 match->flow.skb_priority = 0;
1608 match->wc.masks.skb_priority = 0;
1612 match->flow.skb_mark = 0;
1613 match->wc.masks.skb_mark = 0;
1617 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1621 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1622 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1626 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1627 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1631 match->flow.dl_type = htons(0);
1632 match->wc.masks.dl_type = htons(0);
1636 match_set_dl_tci_masked(match, htons(0), htons(0));
1641 match_set_any_vid(match);
1644 case MFF_DL_VLAN_PCP:
1646 match_set_any_pcp(match);
1649 case MFF_MPLS_LABEL:
1650 match_set_any_mpls_label(match);
1654 match_set_any_mpls_tc(match);
1658 match_set_any_mpls_bos(match);
1663 match_set_nw_src_masked(match, htonl(0), htonl(0));
1668 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1672 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1673 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1677 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1678 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1681 case MFF_IPV6_LABEL:
1682 match->wc.masks.ipv6_label = htonl(0);
1683 match->flow.ipv6_label = htonl(0);
1687 match->wc.masks.nw_proto = 0;
1688 match->flow.nw_proto = 0;
1692 case MFF_IP_DSCP_SHIFTED:
1693 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1694 match->flow.nw_tos &= ~IP_DSCP_MASK;
1698 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1699 match->flow.nw_tos &= ~IP_ECN_MASK;
1703 match->wc.masks.nw_ttl = 0;
1704 match->flow.nw_ttl = 0;
1708 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1709 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1713 match->wc.masks.nw_proto = 0;
1714 match->flow.nw_proto = 0;
1719 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1720 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1725 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1726 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1731 case MFF_ICMPV4_TYPE:
1732 case MFF_ICMPV6_TYPE:
1733 match->wc.masks.tp_src = htons(0);
1734 match->flow.tp_src = htons(0);
1739 case MFF_ICMPV4_CODE:
1740 case MFF_ICMPV6_CODE:
1741 match->wc.masks.tp_dst = htons(0);
1742 match->flow.tp_dst = htons(0);
1746 memset(&match->wc.masks.nd_target, 0,
1747 sizeof match->wc.masks.nd_target);
1748 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1757 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1758 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1759 * with a 1-bit indicating that the corresponding value bit must match and a
1760 * 0-bit indicating a don't-care.
1762 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1763 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1764 * call is equivalent to mf_set_wild(mf, match).
1766 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1767 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1769 mf_set(const struct mf_field *mf,
1770 const union mf_value *value, const union mf_value *mask,
1771 struct match *match)
1773 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1774 mf_set_value(mf, value, match);
1776 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1777 mf_set_wild(mf, match);
1783 case MFF_IN_PORT_OXM:
1785 case MFF_SKB_PRIORITY:
1788 case MFF_DL_VLAN_PCP:
1790 case MFF_MPLS_LABEL:
1796 case MFF_IP_DSCP_SHIFTED:
1799 case MFF_ICMPV4_TYPE:
1800 case MFF_ICMPV4_CODE:
1801 case MFF_ICMPV6_TYPE:
1802 case MFF_ICMPV6_CODE:
1806 match_set_tun_id_masked(match, value->be64, mask->be64);
1809 match_set_tun_src_masked(match, value->be32, mask->be32);
1812 match_set_tun_dst_masked(match, value->be32, mask->be32);
1815 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1818 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1821 match_set_tun_tos_masked(match, value->u8, mask->u8);
1825 match_set_metadata_masked(match, value->be64, mask->be64);
1829 match_set_reg_masked(match, mf->id - MFF_REG0,
1830 ntohl(value->be32), ntohl(mask->be32));
1834 match_set_dl_dst_masked(match, value->mac, mask->mac);
1838 match_set_dl_src_masked(match, value->mac, mask->mac);
1843 match_set_arp_sha_masked(match, value->mac, mask->mac);
1848 match_set_arp_tha_masked(match, value->mac, mask->mac);
1852 match_set_dl_tci_masked(match, value->be16, mask->be16);
1856 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1860 match_set_nw_src_masked(match, value->be32, mask->be32);
1864 match_set_nw_dst_masked(match, value->be32, mask->be32);
1868 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1872 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1875 case MFF_IPV6_LABEL:
1876 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1877 mf_set_value(mf, value, match);
1879 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1884 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1888 match_set_nw_frag_masked(match, value->u8, mask->u8);
1892 match_set_nw_src_masked(match, value->be32, mask->be32);
1896 match_set_nw_dst_masked(match, value->be32, mask->be32);
1901 match_set_tp_src_masked(match, value->be16, mask->be16);
1906 match_set_tp_dst_masked(match, value->be16, mask->be16);
1916 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1920 VLOG_WARN_RL(&rl, "unknown %s field", type);
1921 } else if (!sf->n_bits) {
1922 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1923 } else if (sf->ofs >= sf->field->n_bits) {
1924 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1925 sf->ofs, sf->field->n_bits, type, sf->field->name);
1926 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1927 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1928 "of %s field %s", sf->ofs, sf->n_bits,
1929 sf->field->n_bits, type, sf->field->name);
1930 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1931 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1932 type, sf->field->name);
1937 return OFPERR_OFPBAC_BAD_ARGUMENT;
1940 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1941 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1944 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1946 return mf_check__(sf, flow, "source");
1949 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1950 * if so, otherwise an OpenFlow error code (e.g. as returned by
1953 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1955 int error = mf_check__(sf, flow, "destination");
1956 if (!error && !sf->field->writable) {
1957 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1959 return OFPERR_OFPBAC_BAD_ARGUMENT;
1964 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1965 * 'value' and 'mask', respectively. */
1967 mf_get(const struct mf_field *mf, const struct match *match,
1968 union mf_value *value, union mf_value *mask)
1970 mf_get_value(mf, &match->flow, value);
1971 mf_get_mask(mf, &match->wc, mask);
1974 /* Assigns a random value for field 'mf' to 'value'. */
1976 mf_random_value(const struct mf_field *mf, union mf_value *value)
1978 random_bytes(value, mf->n_bytes);
1990 case MFF_SKB_PRIORITY:
2010 case MFF_ICMPV4_TYPE:
2011 case MFF_ICMPV4_CODE:
2012 case MFF_ICMPV6_TYPE:
2013 case MFF_ICMPV6_CODE:
2019 case MFF_IN_PORT_OXM:
2020 value->be32 = ofputil_port_to_ofp11(ntohs(value->be16));
2023 case MFF_IPV6_LABEL:
2024 value->be32 &= ~htonl(IPV6_LABEL_MASK);
2028 value->u8 &= IP_DSCP_MASK;
2031 case MFF_IP_DSCP_SHIFTED:
2032 value->u8 &= IP_DSCP_MASK >> 2;
2036 value->u8 &= IP_ECN_MASK;
2040 value->u8 &= FLOW_NW_FRAG_MASK;
2044 value->be16 &= htons(0xff);
2048 value->be16 &= htons(VLAN_VID_MASK);
2051 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2054 case MFF_DL_VLAN_PCP:
2059 case MFF_MPLS_LABEL:
2060 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2064 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2068 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2078 mf_from_integer_string(const struct mf_field *mf, const char *s,
2079 uint8_t *valuep, uint8_t *maskp)
2081 unsigned long long int integer, mask;
2086 integer = strtoull(s, &tail, 0);
2087 if (errno || (*tail != '\0' && *tail != '/')) {
2092 mask = strtoull(tail + 1, &tail, 0);
2093 if (errno || *tail != '\0') {
2100 for (i = mf->n_bytes - 1; i >= 0; i--) {
2101 valuep[i] = integer;
2107 return xasprintf("%s: value too large for %u-byte field %s",
2108 s, mf->n_bytes, mf->name);
2113 return xasprintf("%s: bad syntax for %s", s, mf->name);
2117 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2118 uint8_t mac[ETH_ADDR_LEN],
2119 uint8_t mask[ETH_ADDR_LEN])
2121 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2123 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
2124 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
2125 case ETH_ADDR_SCAN_COUNT * 2:
2128 case ETH_ADDR_SCAN_COUNT:
2129 memset(mask, 0xff, ETH_ADDR_LEN);
2133 return xasprintf("%s: invalid Ethernet address", s);
2138 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2139 ovs_be32 *ip, ovs_be32 *mask)
2143 ovs_assert(mf->n_bytes == sizeof *ip);
2145 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2146 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2148 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2149 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2150 if (prefix <= 0 || prefix > 32) {
2151 return xasprintf("%s: network prefix bits not between 1 and "
2153 } else if (prefix == 32) {
2154 *mask = htonl(UINT32_MAX);
2156 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2158 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2159 *mask = htonl(UINT32_MAX);
2161 return xasprintf("%s: invalid IP address", s);
2167 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2168 struct in6_addr *value, struct in6_addr *mask)
2170 char *str = xstrdup(s);
2171 char *save_ptr = NULL;
2172 const char *name, *netmask;
2175 ovs_assert(mf->n_bytes == sizeof *value);
2177 name = strtok_r(str, "/", &save_ptr);
2178 retval = name ? lookup_ipv6(name, value) : EINVAL;
2182 err = xasprintf("%s: could not convert to IPv6 address", str);
2188 netmask = strtok_r(NULL, "/", &save_ptr);
2190 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2191 int prefix = atoi(netmask);
2192 if (prefix <= 0 || prefix > 128) {
2194 return xasprintf("%s: prefix bits not between 1 and 128", s);
2196 *mask = ipv6_create_mask(prefix);
2200 *mask = in6addr_exact;
2208 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2209 ovs_be16 *valuep, ovs_be16 *maskp)
2213 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2215 if (ofputil_port_from_string(s, &port)) {
2216 *valuep = htons(port);
2217 *maskp = htons(UINT16_MAX);
2220 return xasprintf("%s: port value out of range for %s", s, mf->name);
2224 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2225 ovs_be32 *valuep, ovs_be32 *maskp)
2229 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2230 if (ofputil_port_from_string(s, &port)) {
2231 *valuep = ofputil_port_to_ofp11(port);
2232 *maskp = htonl(UINT32_MAX);
2235 return xasprintf("%s: port value out of range for %s", s, mf->name);
2238 struct frag_handling {
2244 static const struct frag_handling all_frags[] = {
2245 #define A FLOW_NW_FRAG_ANY
2246 #define L FLOW_NW_FRAG_LATER
2247 /* name mask value */
2250 { "first", A|L, A },
2251 { "later", A|L, A|L },
2256 { "not_later", L, 0 },
2263 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2265 const struct frag_handling *h;
2267 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2268 if (!strcasecmp(s, h->name)) {
2269 /* We force the upper bits of the mask on to make mf_parse_value()
2270 * happy (otherwise it will never think it's an exact match.) */
2271 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2277 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2278 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2282 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2285 uint32_t result = 0;
2286 char *save_ptr = NULL;
2289 char *s = xstrdup(s_);
2291 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2292 name = strtok_r(NULL, " |", &save_ptr)) {
2294 unsigned long long int flags;
2298 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2302 name_len = strlen(name);
2303 for (bit = 1; bit; bit <<= 1) {
2304 const char *fname = bit_to_string(bit);
2311 len = strlen(fname);
2312 if (len != name_len) {
2315 if (!strncmp(name, fname, len)) {
2327 *res = htons(result);
2334 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2336 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2337 *maskp = htons(UINT16_MAX);
2341 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2342 "\"csum\", \"key\"", s);
2345 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2346 * NULL if successful, otherwise a malloc()'d string describing the error. */
2348 mf_parse(const struct mf_field *mf, const char *s,
2349 union mf_value *value, union mf_value *mask)
2351 if (!strcmp(s, "*")) {
2352 memset(value, 0, mf->n_bytes);
2353 memset(mask, 0, mf->n_bytes);
2357 switch (mf->string) {
2359 case MFS_HEXADECIMAL:
2360 return mf_from_integer_string(mf, s,
2361 (uint8_t *) value, (uint8_t *) mask);
2364 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2367 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2370 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2373 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2375 case MFS_OFP_PORT_OXM:
2376 return mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2379 return mf_from_frag_string(s, &value->u8, &mask->u8);
2382 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2383 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2388 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2389 * successful, otherwise a malloc()'d string describing the error. */
2391 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2393 union mf_value mask;
2396 error = mf_parse(mf, s, value, &mask);
2401 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2402 return xasprintf("%s: wildcards not allowed here", s);
2408 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2409 const uint8_t *maskp, struct ds *s)
2411 unsigned long long int integer;
2414 ovs_assert(mf->n_bytes <= 8);
2417 for (i = 0; i < mf->n_bytes; i++) {
2418 integer = (integer << 8) | valuep[i];
2420 if (mf->string == MFS_HEXADECIMAL) {
2421 ds_put_format(s, "%#llx", integer);
2423 ds_put_format(s, "%lld", integer);
2427 unsigned long long int mask;
2430 for (i = 0; i < mf->n_bytes; i++) {
2431 mask = (mask << 8) | maskp[i];
2434 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2435 * not sure that that a bit-mask written in decimal is ever easier to
2436 * understand than the same bit-mask written in hexadecimal. */
2437 ds_put_format(s, "/%#llx", mask);
2442 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2444 const struct frag_handling *h;
2446 mask &= FLOW_NW_FRAG_MASK;
2449 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2450 if (value == h->value && mask == h->mask) {
2451 ds_put_cstr(s, h->name);
2455 ds_put_cstr(s, "<error>");
2459 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2461 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2464 /* Appends to 's' a string representation of field 'mf' whose value is in
2465 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2467 mf_format(const struct mf_field *mf,
2468 const union mf_value *value, const union mf_value *mask,
2472 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2473 ds_put_cstr(s, "ANY");
2475 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2480 switch (mf->string) {
2481 case MFS_OFP_PORT_OXM:
2484 if (ofputil_port_from_ofp11(value->be32, &port)) {
2487 ofputil_format_port(port, s);
2493 ofputil_format_port(ntohs(value->be16), s);
2498 case MFS_HEXADECIMAL:
2499 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2503 eth_format_masked(value->mac, mask->mac, s);
2507 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2512 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2516 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2520 mf_format_tnl_flags_string(&value->be16, s);
2528 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2529 * least-significant bits in 'x'.
2532 mf_write_subfield_flow(const struct mf_subfield *sf,
2533 const union mf_subvalue *x, struct flow *flow)
2535 const struct mf_field *field = sf->field;
2536 union mf_value value;
2538 mf_get_value(field, flow, &value);
2539 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2540 sf->ofs, sf->n_bits);
2541 mf_set_flow_value(field, &value, flow);
2544 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2545 * least-significant bits in 'x'.
2548 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2549 struct match *match)
2551 const struct mf_field *field = sf->field;
2552 union mf_value value, mask;
2554 mf_get(field, match, &value, &mask);
2555 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2556 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2557 mf_set(field, &value, &mask, match);
2560 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2561 * reading 'flow', e.g. as checked by mf_check_src(). */
2563 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2564 union mf_subvalue *x)
2566 union mf_value value;
2568 mf_get_value(sf->field, flow, &value);
2570 memset(x, 0, sizeof *x);
2571 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2576 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2577 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2580 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2582 union mf_value value;
2584 mf_get_value(sf->field, flow, &value);
2585 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2588 /* Formats 'sf' into 's' in a format normally acceptable to
2589 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2590 * sf->field has no NXM name.) */
2592 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2595 ds_put_cstr(s, "<unknown>");
2596 } else if (sf->field->nxm_name) {
2597 ds_put_cstr(s, sf->field->nxm_name);
2598 } else if (sf->field->nxm_header) {
2599 uint32_t header = sf->field->nxm_header;
2600 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2602 ds_put_cstr(s, sf->field->name);
2605 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2606 ds_put_cstr(s, "[]");
2607 } else if (sf->n_bits == 1) {
2608 ds_put_format(s, "[%d]", sf->ofs);
2610 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2614 static const struct mf_field *
2615 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2619 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2624 for (i = 0; i < MFF_N_IDS; i++) {
2625 const struct mf_field *mf = mf_from_id(i);
2628 && !strncmp(mf->nxm_name, name, name_len)
2629 && mf->nxm_name[name_len] == '\0') {
2633 && !strncmp(mf->oxm_name, name, name_len)
2634 && mf->oxm_name[name_len] == '\0') {
2642 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2643 * returns NULL and advances '*sp' to the first byte following the parsed
2644 * string. On failure, returns a malloc()'d error message, does not modify
2645 * '*sp', and does not properly initialize 'sf'.
2647 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2648 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2649 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2650 * may both be omitted (the [] are still required) to indicate an entire
2653 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2655 const struct mf_field *field;
2664 name_len = strcspn(s, "[");
2665 if (s[name_len] != '[') {
2666 return xasprintf("%s: missing [ looking for field name", *sp);
2669 field = mf_parse_subfield_name(name, name_len, &wild);
2671 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2675 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2676 /* Nothing to do. */
2677 } else if (sscanf(s, "[%d]", &start) == 1) {
2679 } else if (!strncmp(s, "[]", 2)) {
2681 end = field->n_bits - 1;
2683 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2684 "[<start>..<end>]", *sp);
2686 s = strchr(s, ']') + 1;
2689 return xasprintf("%s: starting bit %d is after ending bit %d",
2691 } else if (start >= field->n_bits) {
2692 return xasprintf("%s: starting bit %d is not valid because field is "
2693 "only %d bits wide", *sp, start, field->n_bits);
2694 } else if (end >= field->n_bits){
2695 return xasprintf("%s: ending bit %d is not valid because field is "
2696 "only %d bits wide", *sp, end, field->n_bits);
2701 sf->n_bits = end - start + 1;
2707 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2708 * byte in 's' following the parsed string.
2710 * Exits with an error message if 's' has incorrect syntax.
2712 * The syntax parsed from 's' takes the form "header[start..end]" where
2713 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2714 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2715 * may both be omitted (the [] are still required) to indicate an entire
2718 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2720 char *msg = mf_parse_subfield__(sf, &s);
2722 ovs_fatal(0, "%s", msg);
2728 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2732 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2733 if (subvalue->u8[i]) {
2734 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2735 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2736 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2741 ds_put_char(s, '0');