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,
67 MFF_TUN_DST, "tun_dst", NULL,
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",
264 MFF_IPV4_SRC, "ip_src", "nw_src",
265 MF_FIELD_SIZES(be32),
270 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
271 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
273 MFF_IPV4_DST, "ip_dst", "nw_dst",
274 MF_FIELD_SIZES(be32),
279 NXM_OF_IP_DST, "NXM_OF_IP_DST",
280 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
284 MFF_IPV6_SRC, "ipv6_src", NULL,
285 MF_FIELD_SIZES(ipv6),
290 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
291 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
293 MFF_IPV6_DST, "ipv6_dst", NULL,
294 MF_FIELD_SIZES(ipv6),
299 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
300 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
303 MFF_IPV6_LABEL, "ipv6_label", NULL,
309 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
310 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
314 MFF_IP_PROTO, "nw_proto", NULL,
320 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
321 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
323 MFF_IP_DSCP, "nw_tos", NULL,
329 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
330 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
332 MFF_IP_ECN, "nw_ecn", NULL,
338 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
339 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
341 MFF_IP_TTL, "nw_ttl", NULL,
347 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
348 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
350 MFF_IP_FRAG, "ip_frag", NULL,
356 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
357 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
361 MFF_ARP_OP, "arp_op", NULL,
362 MF_FIELD_SIZES(be16),
367 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
368 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
370 MFF_ARP_SPA, "arp_spa", NULL,
371 MF_FIELD_SIZES(be32),
376 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
377 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
379 MFF_ARP_TPA, "arp_tpa", NULL,
380 MF_FIELD_SIZES(be32),
385 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
386 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
388 MFF_ARP_SHA, "arp_sha", NULL,
394 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
395 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
397 MFF_ARP_THA, "arp_tha", NULL,
403 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
404 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
412 MFF_TCP_SRC, "tcp_src", "tp_src",
413 MF_FIELD_SIZES(be16),
418 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
419 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
421 MFF_TCP_DST, "tcp_dst", "tp_dst",
422 MF_FIELD_SIZES(be16),
427 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
428 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
432 MFF_UDP_SRC, "udp_src", NULL,
433 MF_FIELD_SIZES(be16),
438 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
439 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
441 MFF_UDP_DST, "udp_dst", NULL,
442 MF_FIELD_SIZES(be16),
447 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
448 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
452 MFF_ICMPV4_TYPE, "icmp_type", NULL,
458 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
459 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
461 MFF_ICMPV4_CODE, "icmp_code", NULL,
467 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
468 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
472 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
478 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
479 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
481 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
487 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
488 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
496 MFF_ND_TARGET, "nd_target", NULL,
497 MF_FIELD_SIZES(ipv6),
502 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
503 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
505 MFF_ND_SLL, "nd_sll", NULL,
511 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
512 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
514 MFF_ND_TLL, "nd_tll", NULL,
520 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
521 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
525 /* Maps an NXM or OXM header value to an mf_field. */
527 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
528 uint32_t header; /* NXM or OXM header value. */
529 const struct mf_field *mf;
532 /* Contains 'struct nxm_field's. */
533 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
535 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
536 * controller and so there's not much point in showing a lot of them. */
537 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
539 const struct mf_field *mf_from_nxm_header__(uint32_t header);
541 /* Returns the field with the given 'id'. */
542 const struct mf_field *
543 mf_from_id(enum mf_field_id id)
545 ovs_assert((unsigned int) id < MFF_N_IDS);
546 return &mf_fields[id];
549 /* Returns the field with the given 'name', or a null pointer if no field has
551 const struct mf_field *
552 mf_from_name(const char *name)
554 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
556 if (shash_is_empty(&mf_by_name)) {
557 const struct mf_field *mf;
559 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
560 shash_add_once(&mf_by_name, mf->name, mf);
561 if (mf->extra_name) {
562 shash_add_once(&mf_by_name, mf->extra_name, mf);
567 return shash_find_data(&mf_by_name, name);
571 add_nxm_field(uint32_t header, const struct mf_field *mf)
575 f = xmalloc(sizeof *f);
576 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
582 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
585 ovs_assert(!mf_from_nxm_header__(header));
586 add_nxm_field(header, mf);
587 if (mf->maskable != MFM_NONE) {
588 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
596 const struct mf_field *mf;
598 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
599 nxm_init_add_field(mf, mf->nxm_header);
600 if (mf->oxm_header != mf->nxm_header) {
601 nxm_init_add_field(mf, mf->oxm_header);
606 const struct mf_field *
607 mf_from_nxm_header(uint32_t header)
609 if (hmap_is_empty(&all_fields)) {
612 return mf_from_nxm_header__(header);
615 const struct mf_field *
616 mf_from_nxm_header__(uint32_t header)
618 const struct nxm_field *f;
620 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
621 if (f->header == header) {
629 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
630 * specifies at least one bit in the field.
632 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
633 * meets 'mf''s prerequisites. */
635 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
644 return !wc->masks.tunnel.tun_id;
646 return !wc->masks.metadata;
648 return !wc->masks.in_port;
649 case MFF_SKB_PRIORITY:
650 return !wc->masks.skb_priority;
652 return !wc->masks.skb_mark;
654 return !wc->masks.regs[mf->id - MFF_REG0];
657 return eth_addr_is_zero(wc->masks.dl_src);
659 return eth_addr_is_zero(wc->masks.dl_dst);
661 return !wc->masks.dl_type;
665 return eth_addr_is_zero(wc->masks.arp_sha);
669 return eth_addr_is_zero(wc->masks.arp_tha);
672 return !wc->masks.vlan_tci;
674 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
676 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
677 case MFF_DL_VLAN_PCP:
679 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
682 return !wc->masks.nw_src;
684 return !wc->masks.nw_dst;
687 return ipv6_mask_is_any(&wc->masks.ipv6_src);
689 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
692 return !wc->masks.ipv6_label;
695 return !wc->masks.nw_proto;
697 return !(wc->masks.nw_tos & IP_DSCP_MASK);
699 return !(wc->masks.nw_tos & IP_ECN_MASK);
701 return !wc->masks.nw_ttl;
704 return ipv6_mask_is_any(&wc->masks.nd_target);
707 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
710 return !wc->masks.nw_proto;
712 return !wc->masks.nw_src;
714 return !wc->masks.nw_dst;
718 case MFF_ICMPV4_TYPE:
719 case MFF_ICMPV6_TYPE:
720 return !wc->masks.tp_src;
723 case MFF_ICMPV4_CODE:
724 case MFF_ICMPV6_CODE:
725 return !wc->masks.tp_dst;
733 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
734 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
735 * purposes, or to 0 if it is wildcarded.
737 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
738 * meets 'mf''s prerequisites. */
740 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
741 union mf_value *mask)
743 mf_get_value(mf, &wc->masks, mask);
746 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
747 * if the mask is valid, false otherwise. */
749 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
751 switch (mf->maskable) {
753 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
754 is_all_ones((const uint8_t *) mask, mf->n_bytes));
764 is_icmpv4(const struct flow *flow)
766 return (flow->dl_type == htons(ETH_TYPE_IP)
767 && flow->nw_proto == IPPROTO_ICMP);
771 is_icmpv6(const struct flow *flow)
773 return (flow->dl_type == htons(ETH_TYPE_IPV6)
774 && flow->nw_proto == IPPROTO_ICMPV6);
777 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
779 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
781 switch (mf->prereqs) {
786 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
787 flow->dl_type == htons(ETH_TYPE_RARP));
789 return flow->dl_type == htons(ETH_TYPE_IP);
791 return flow->dl_type == htons(ETH_TYPE_IPV6);
793 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
795 return is_ip_any(flow);
798 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
800 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
802 return is_icmpv4(flow);
804 return is_icmpv6(flow);
807 return (is_icmpv6(flow)
808 && flow->tp_dst == htons(0)
809 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
810 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
812 return (is_icmpv6(flow)
813 && flow->tp_dst == htons(0)
814 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
816 return (is_icmpv6(flow)
817 && flow->tp_dst == htons(0)
818 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
824 /* Returns true if 'value' may be a valid value *as part of a masked match*,
827 * A value is not rejected just because it is not valid for the field in
828 * question, but only if it doesn't make sense to test the bits in question at
829 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
830 * without the VLAN_CFI bit being set, but we can't reject those values because
831 * it is still legitimate to test just for those bits (see the documentation
832 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
833 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
835 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
846 case MFF_SKB_PRIORITY:
867 case MFF_ICMPV4_TYPE:
868 case MFF_ICMPV4_CODE:
869 case MFF_ICMPV6_TYPE:
870 case MFF_ICMPV6_CODE:
877 return !(value->u8 & ~IP_DSCP_MASK);
879 return !(value->u8 & ~IP_ECN_MASK);
881 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
884 return !(value->be16 & htons(0xff00));
887 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
889 return !(value->be16 & htons(VLAN_PCP_MASK));
891 case MFF_DL_VLAN_PCP:
893 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
896 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
904 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
905 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
907 mf_get_value(const struct mf_field *mf, const struct flow *flow,
908 union mf_value *value)
912 value->be64 = flow->tunnel.tun_id;
915 value->be32 = flow->tunnel.ip_src;
918 value->be32 = flow->tunnel.ip_dst;
921 value->be16 = htons(flow->tunnel.flags);
924 value->u8 = flow->tunnel.ip_ttl;
927 value->u8 = flow->tunnel.ip_tos;
931 value->be64 = flow->metadata;
935 value->be16 = htons(flow->in_port);
938 case MFF_SKB_PRIORITY:
939 value->be32 = htonl(flow->skb_priority);
943 value->be32 = htonl(flow->skb_mark);
947 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
951 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
955 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
959 value->be16 = flow->dl_type;
963 value->be16 = flow->vlan_tci;
967 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
970 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
973 case MFF_DL_VLAN_PCP:
975 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
979 value->be32 = flow->nw_src;
983 value->be32 = flow->nw_dst;
987 value->ipv6 = flow->ipv6_src;
991 value->ipv6 = flow->ipv6_dst;
995 value->be32 = flow->ipv6_label;
999 value->u8 = flow->nw_proto;
1003 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1007 value->u8 = flow->nw_tos & IP_ECN_MASK;
1011 value->u8 = flow->nw_ttl;
1015 value->u8 = flow->nw_frag;
1019 value->be16 = htons(flow->nw_proto);
1023 value->be32 = flow->nw_src;
1027 value->be32 = flow->nw_dst;
1032 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1037 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1042 value->be16 = flow->tp_src;
1047 value->be16 = flow->tp_dst;
1050 case MFF_ICMPV4_TYPE:
1051 case MFF_ICMPV6_TYPE:
1052 value->u8 = ntohs(flow->tp_src);
1055 case MFF_ICMPV4_CODE:
1056 case MFF_ICMPV6_CODE:
1057 value->u8 = ntohs(flow->tp_dst);
1061 value->ipv6 = flow->nd_target;
1070 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1071 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1074 mf_set_value(const struct mf_field *mf,
1075 const union mf_value *value, struct match *match)
1079 match_set_tun_id(match, value->be64);
1082 match_set_tun_src(match, value->be32);
1085 match_set_tun_dst(match, value->be32);
1088 match_set_tun_flags(match, ntohs(value->be16));
1091 match_set_tun_tos(match, value->u8);
1094 match_set_tun_ttl(match, value->u8);
1098 match_set_metadata(match, value->be64);
1102 match_set_in_port(match, ntohs(value->be16));
1105 case MFF_SKB_PRIORITY:
1106 match_set_skb_priority(match, ntohl(value->be32));
1110 match_set_skb_mark(match, ntohl(value->be32));
1114 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1118 match_set_dl_src(match, value->mac);
1122 match_set_dl_dst(match, value->mac);
1126 match_set_dl_type(match, value->be16);
1130 match_set_dl_tci(match, value->be16);
1134 match_set_dl_vlan(match, value->be16);
1137 match_set_vlan_vid(match, value->be16);
1140 case MFF_DL_VLAN_PCP:
1142 match_set_dl_vlan_pcp(match, value->u8);
1146 match_set_nw_src(match, value->be32);
1150 match_set_nw_dst(match, value->be32);
1154 match_set_ipv6_src(match, &value->ipv6);
1158 match_set_ipv6_dst(match, &value->ipv6);
1161 case MFF_IPV6_LABEL:
1162 match_set_ipv6_label(match, value->be32);
1166 match_set_nw_proto(match, value->u8);
1170 match_set_nw_dscp(match, value->u8);
1174 match_set_nw_ecn(match, value->u8);
1178 match_set_nw_ttl(match, value->u8);
1182 match_set_nw_frag(match, value->u8);
1186 match_set_nw_proto(match, ntohs(value->be16));
1190 match_set_nw_src(match, value->be32);
1194 match_set_nw_dst(match, value->be32);
1199 match_set_arp_sha(match, value->mac);
1204 match_set_arp_tha(match, value->mac);
1209 match_set_tp_src(match, value->be16);
1214 match_set_tp_dst(match, value->be16);
1217 case MFF_ICMPV4_TYPE:
1218 case MFF_ICMPV6_TYPE:
1219 match_set_icmp_type(match, value->u8);
1222 case MFF_ICMPV4_CODE:
1223 case MFF_ICMPV6_CODE:
1224 match_set_icmp_code(match, value->u8);
1228 match_set_nd_target(match, &value->ipv6);
1237 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1238 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1241 mf_set_flow_value(const struct mf_field *mf,
1242 const union mf_value *value, struct flow *flow)
1246 flow->tunnel.tun_id = value->be64;
1249 flow->tunnel.ip_src = value->be32;
1252 flow->tunnel.ip_dst = value->be32;
1255 flow->tunnel.flags = ntohs(value->be16);
1258 flow->tunnel.ip_tos = value->u8;
1261 flow->tunnel.ip_ttl = value->u8;
1265 flow->metadata = value->be64;
1269 flow->in_port = ntohs(value->be16);
1272 case MFF_SKB_PRIORITY:
1273 flow->skb_priority = ntohl(value->be32);
1277 flow->skb_mark = ntohl(value->be32);
1281 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1285 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1289 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1293 flow->dl_type = value->be16;
1297 flow->vlan_tci = value->be16;
1301 flow_set_dl_vlan(flow, value->be16);
1304 flow_set_vlan_vid(flow, value->be16);
1307 case MFF_DL_VLAN_PCP:
1309 flow_set_vlan_pcp(flow, value->u8);
1313 flow->nw_src = value->be32;
1317 flow->nw_dst = value->be32;
1321 flow->ipv6_src = value->ipv6;
1325 flow->ipv6_dst = value->ipv6;
1328 case MFF_IPV6_LABEL:
1329 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1333 flow->nw_proto = value->u8;
1337 flow->nw_tos &= ~IP_DSCP_MASK;
1338 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1342 flow->nw_tos &= ~IP_ECN_MASK;
1343 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1347 flow->nw_ttl = value->u8;
1351 flow->nw_frag &= value->u8;
1355 flow->nw_proto = ntohs(value->be16);
1359 flow->nw_src = value->be32;
1363 flow->nw_dst = value->be32;
1368 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1373 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1378 flow->tp_src = value->be16;
1383 flow->tp_dst = value->be16;
1386 case MFF_ICMPV4_TYPE:
1387 case MFF_ICMPV6_TYPE:
1388 flow->tp_src = htons(value->u8);
1391 case MFF_ICMPV4_CODE:
1392 case MFF_ICMPV6_CODE:
1393 flow->tp_dst = htons(value->u8);
1397 flow->nd_target = value->ipv6;
1406 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1408 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1411 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1413 union mf_value value;
1415 mf_get_value(mf, flow, &value);
1416 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1419 /* Makes 'match' wildcard field 'mf'.
1421 * The caller is responsible for ensuring that 'match' meets 'mf''s
1424 mf_set_wild(const struct mf_field *mf, struct match *match)
1428 match_set_tun_id_masked(match, htonll(0), htonll(0));
1431 match_set_tun_src_masked(match, htonl(0), htonl(0));
1434 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1437 match_set_tun_flags_masked(match, 0, 0);
1440 match_set_tun_tos_masked(match, 0, 0);
1443 match_set_tun_ttl_masked(match, 0, 0);
1447 match_set_metadata_masked(match, htonll(0), htonll(0));
1450 match->flow.in_port = 0;
1451 match->wc.masks.in_port = 0;
1454 case MFF_SKB_PRIORITY:
1455 match->flow.skb_priority = 0;
1456 match->wc.masks.skb_priority = 0;
1460 match->flow.skb_mark = 0;
1461 match->wc.masks.skb_mark = 0;
1465 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1469 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1470 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1474 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1475 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1479 match->flow.dl_type = htons(0);
1480 match->wc.masks.dl_type = htons(0);
1484 match_set_dl_tci_masked(match, htons(0), htons(0));
1489 match_set_any_vid(match);
1492 case MFF_DL_VLAN_PCP:
1494 match_set_any_pcp(match);
1499 match_set_nw_src_masked(match, htonl(0), htonl(0));
1504 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1508 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1509 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1513 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1514 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1517 case MFF_IPV6_LABEL:
1518 match->wc.masks.ipv6_label = htonl(0);
1519 match->flow.ipv6_label = htonl(0);
1523 match->wc.masks.nw_proto = 0;
1524 match->flow.nw_proto = 0;
1528 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1529 match->flow.nw_tos &= ~IP_DSCP_MASK;
1533 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1534 match->flow.nw_tos &= ~IP_ECN_MASK;
1538 match->wc.masks.nw_ttl = 0;
1539 match->flow.nw_ttl = 0;
1543 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1544 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1548 match->wc.masks.nw_proto = 0;
1549 match->flow.nw_proto = 0;
1554 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1555 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1560 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1561 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1566 case MFF_ICMPV4_TYPE:
1567 case MFF_ICMPV6_TYPE:
1568 match->wc.masks.tp_src = htons(0);
1569 match->flow.tp_src = htons(0);
1574 case MFF_ICMPV4_CODE:
1575 case MFF_ICMPV6_CODE:
1576 match->wc.masks.tp_dst = htons(0);
1577 match->flow.tp_dst = htons(0);
1581 memset(&match->wc.masks.nd_target, 0,
1582 sizeof match->wc.masks.nd_target);
1583 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1592 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1593 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1594 * with a 1-bit indicating that the corresponding value bit must match and a
1595 * 0-bit indicating a don't-care.
1597 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1598 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1599 * call is equivalent to mf_set_wild(mf, match).
1601 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1602 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1604 mf_set(const struct mf_field *mf,
1605 const union mf_value *value, const union mf_value *mask,
1606 struct match *match)
1608 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1609 mf_set_value(mf, value, match);
1611 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1612 mf_set_wild(mf, match);
1619 case MFF_SKB_PRIORITY:
1622 case MFF_DL_VLAN_PCP:
1629 case MFF_ICMPV4_TYPE:
1630 case MFF_ICMPV4_CODE:
1631 case MFF_ICMPV6_TYPE:
1632 case MFF_ICMPV6_CODE:
1636 match_set_tun_id_masked(match, value->be64, mask->be64);
1639 match_set_tun_src_masked(match, value->be32, mask->be32);
1642 match_set_tun_dst_masked(match, value->be32, mask->be32);
1645 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1648 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1651 match_set_tun_tos_masked(match, value->u8, mask->u8);
1655 match_set_metadata_masked(match, value->be64, mask->be64);
1659 match_set_reg_masked(match, mf->id - MFF_REG0,
1660 ntohl(value->be32), ntohl(mask->be32));
1664 match_set_dl_dst_masked(match, value->mac, mask->mac);
1668 match_set_dl_src_masked(match, value->mac, mask->mac);
1673 match_set_arp_sha_masked(match, value->mac, mask->mac);
1678 match_set_arp_tha_masked(match, value->mac, mask->mac);
1682 match_set_dl_tci_masked(match, value->be16, mask->be16);
1686 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1690 match_set_nw_src_masked(match, value->be32, mask->be32);
1694 match_set_nw_dst_masked(match, value->be32, mask->be32);
1698 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1702 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1705 case MFF_IPV6_LABEL:
1706 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1707 mf_set_value(mf, value, match);
1709 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1714 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1718 match_set_nw_frag_masked(match, value->u8, mask->u8);
1722 match_set_nw_src_masked(match, value->be32, mask->be32);
1726 match_set_nw_dst_masked(match, value->be32, mask->be32);
1731 match_set_tp_src_masked(match, value->be16, mask->be16);
1736 match_set_tp_dst_masked(match, value->be16, mask->be16);
1746 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1750 VLOG_WARN_RL(&rl, "unknown %s field", type);
1751 } else if (!sf->n_bits) {
1752 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1753 } else if (sf->ofs >= sf->field->n_bits) {
1754 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1755 sf->ofs, sf->field->n_bits, type, sf->field->name);
1756 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1757 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1758 "of %s field %s", sf->ofs, sf->n_bits,
1759 sf->field->n_bits, type, sf->field->name);
1760 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1761 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1762 type, sf->field->name);
1767 return OFPERR_OFPBAC_BAD_ARGUMENT;
1770 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1771 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1774 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1776 return mf_check__(sf, flow, "source");
1779 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1780 * if so, otherwise an OpenFlow error code (e.g. as returned by
1783 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1785 int error = mf_check__(sf, flow, "destination");
1786 if (!error && !sf->field->writable) {
1787 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1789 return OFPERR_OFPBAC_BAD_ARGUMENT;
1794 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1795 * 'value' and 'mask', respectively. */
1797 mf_get(const struct mf_field *mf, const struct match *match,
1798 union mf_value *value, union mf_value *mask)
1800 mf_get_value(mf, &match->flow, value);
1801 mf_get_mask(mf, &match->wc, mask);
1804 /* Assigns a random value for field 'mf' to 'value'. */
1806 mf_random_value(const struct mf_field *mf, union mf_value *value)
1808 random_bytes(value, mf->n_bytes);
1820 case MFF_SKB_PRIORITY:
1840 case MFF_ICMPV4_TYPE:
1841 case MFF_ICMPV4_CODE:
1842 case MFF_ICMPV6_TYPE:
1843 case MFF_ICMPV6_CODE:
1849 case MFF_IPV6_LABEL:
1850 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1854 value->u8 &= IP_DSCP_MASK;
1858 value->u8 &= IP_ECN_MASK;
1862 value->u8 &= FLOW_NW_FRAG_MASK;
1866 value->be16 &= htons(0xff);
1870 value->be16 &= htons(VLAN_VID_MASK);
1873 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1876 case MFF_DL_VLAN_PCP:
1888 mf_from_integer_string(const struct mf_field *mf, const char *s,
1889 uint8_t *valuep, uint8_t *maskp)
1891 unsigned long long int integer, mask;
1896 integer = strtoull(s, &tail, 0);
1897 if (errno || (*tail != '\0' && *tail != '/')) {
1902 mask = strtoull(tail + 1, &tail, 0);
1903 if (errno || *tail != '\0') {
1910 for (i = mf->n_bytes - 1; i >= 0; i--) {
1911 valuep[i] = integer;
1917 return xasprintf("%s: value too large for %u-byte field %s",
1918 s, mf->n_bytes, mf->name);
1923 return xasprintf("%s: bad syntax for %s", s, mf->name);
1927 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1928 uint8_t mac[ETH_ADDR_LEN],
1929 uint8_t mask[ETH_ADDR_LEN])
1931 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1933 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1934 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1935 case ETH_ADDR_SCAN_COUNT * 2:
1938 case ETH_ADDR_SCAN_COUNT:
1939 memset(mask, 0xff, ETH_ADDR_LEN);
1943 return xasprintf("%s: invalid Ethernet address", s);
1948 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1949 ovs_be32 *ip, ovs_be32 *mask)
1953 ovs_assert(mf->n_bytes == sizeof *ip);
1955 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1956 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1958 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1959 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1960 if (prefix <= 0 || prefix > 32) {
1961 return xasprintf("%s: network prefix bits not between 1 and "
1963 } else if (prefix == 32) {
1964 *mask = htonl(UINT32_MAX);
1966 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1968 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1969 *mask = htonl(UINT32_MAX);
1971 return xasprintf("%s: invalid IP address", s);
1977 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1978 struct in6_addr *value, struct in6_addr *mask)
1980 char *str = xstrdup(s);
1981 char *save_ptr = NULL;
1982 const char *name, *netmask;
1985 ovs_assert(mf->n_bytes == sizeof *value);
1987 name = strtok_r(str, "/", &save_ptr);
1988 retval = name ? lookup_ipv6(name, value) : EINVAL;
1992 err = xasprintf("%s: could not convert to IPv6 address", str);
1998 netmask = strtok_r(NULL, "/", &save_ptr);
2000 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2001 int prefix = atoi(netmask);
2002 if (prefix <= 0 || prefix > 128) {
2004 return xasprintf("%s: prefix bits not between 1 and 128", s);
2006 *mask = ipv6_create_mask(prefix);
2010 *mask = in6addr_exact;
2018 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2019 ovs_be16 *valuep, ovs_be16 *maskp)
2023 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2025 return xasprintf("%s: negative values not supported for %s",
2027 } else if (ofputil_port_from_string(s, &port)) {
2028 *valuep = htons(port);
2029 *maskp = htons(UINT16_MAX);
2032 return mf_from_integer_string(mf, s,
2033 (uint8_t *) valuep, (uint8_t *) maskp);
2037 struct frag_handling {
2043 static const struct frag_handling all_frags[] = {
2044 #define A FLOW_NW_FRAG_ANY
2045 #define L FLOW_NW_FRAG_LATER
2046 /* name mask value */
2049 { "first", A|L, A },
2050 { "later", A|L, A|L },
2055 { "not_later", L, 0 },
2062 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2064 const struct frag_handling *h;
2066 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2067 if (!strcasecmp(s, h->name)) {
2068 /* We force the upper bits of the mask on to make mf_parse_value()
2069 * happy (otherwise it will never think it's an exact match.) */
2070 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2076 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2077 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2081 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2084 uint32_t result = 0;
2085 char *save_ptr = NULL;
2088 char *s = xstrdup(s_);
2090 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2091 name = strtok_r(NULL, " |", &save_ptr)) {
2093 unsigned long long int flags;
2097 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2101 name_len = strlen(name);
2102 for (bit = 1; bit; bit <<= 1) {
2103 const char *fname = bit_to_string(bit);
2110 len = strlen(fname);
2111 if (len != name_len) {
2114 if (!strncmp(name, fname, len)) {
2126 *res = htons(result);
2133 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2135 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2136 *maskp = htons(UINT16_MAX);
2140 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2141 "\"csum\", \"key\"", s);
2144 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2145 * NULL if successful, otherwise a malloc()'d string describing the error. */
2147 mf_parse(const struct mf_field *mf, const char *s,
2148 union mf_value *value, union mf_value *mask)
2150 if (!strcmp(s, "*")) {
2151 memset(value, 0, mf->n_bytes);
2152 memset(mask, 0, mf->n_bytes);
2156 switch (mf->string) {
2158 case MFS_HEXADECIMAL:
2159 return mf_from_integer_string(mf, s,
2160 (uint8_t *) value, (uint8_t *) mask);
2163 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2166 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2169 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2172 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2175 return mf_from_frag_string(s, &value->u8, &mask->u8);
2178 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2179 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2184 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2185 * successful, otherwise a malloc()'d string describing the error. */
2187 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2189 union mf_value mask;
2192 error = mf_parse(mf, s, value, &mask);
2197 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2198 return xasprintf("%s: wildcards not allowed here", s);
2204 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2205 const uint8_t *maskp, struct ds *s)
2207 unsigned long long int integer;
2210 ovs_assert(mf->n_bytes <= 8);
2213 for (i = 0; i < mf->n_bytes; i++) {
2214 integer = (integer << 8) | valuep[i];
2216 if (mf->string == MFS_HEXADECIMAL) {
2217 ds_put_format(s, "%#llx", integer);
2219 ds_put_format(s, "%lld", integer);
2223 unsigned long long int mask;
2226 for (i = 0; i < mf->n_bytes; i++) {
2227 mask = (mask << 8) | maskp[i];
2230 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2231 * not sure that that a bit-mask written in decimal is ever easier to
2232 * understand than the same bit-mask written in hexadecimal. */
2233 ds_put_format(s, "/%#llx", mask);
2238 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2241 const struct frag_handling *h;
2242 uint8_t value = *valuep;
2243 uint8_t mask = *maskp;
2246 mask &= FLOW_NW_FRAG_MASK;
2248 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2249 if (value == h->value && mask == h->mask) {
2250 ds_put_cstr(s, h->name);
2254 ds_put_cstr(s, "<error>");
2258 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2260 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2263 /* Appends to 's' a string representation of field 'mf' whose value is in
2264 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2266 mf_format(const struct mf_field *mf,
2267 const union mf_value *value, const union mf_value *mask,
2271 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2272 ds_put_cstr(s, "ANY");
2274 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2279 switch (mf->string) {
2282 ofputil_format_port(ntohs(value->be16), s);
2287 case MFS_HEXADECIMAL:
2288 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2292 eth_format_masked(value->mac, mask->mac, s);
2296 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2301 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2305 mf_format_frag_string(&value->u8, &mask->u8, s);
2309 mf_format_tnl_flags_string(&value->be16, s);
2317 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2318 * least-significant bits in 'x'.
2321 mf_write_subfield_flow(const struct mf_subfield *sf,
2322 const union mf_subvalue *x, struct flow *flow)
2324 const struct mf_field *field = sf->field;
2325 union mf_value value;
2327 mf_get_value(field, flow, &value);
2328 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2329 sf->ofs, sf->n_bits);
2330 mf_set_flow_value(field, &value, flow);
2333 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2334 * least-significant bits in 'x'.
2337 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2338 struct match *match)
2340 const struct mf_field *field = sf->field;
2341 union mf_value value, mask;
2343 mf_get(field, match, &value, &mask);
2344 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2345 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2346 mf_set(field, &value, &mask, match);
2349 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2350 * reading 'flow', e.g. as checked by mf_check_src(). */
2352 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2353 union mf_subvalue *x)
2355 union mf_value value;
2357 mf_get_value(sf->field, flow, &value);
2359 memset(x, 0, sizeof *x);
2360 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2365 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2366 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2369 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2371 union mf_value value;
2373 mf_get_value(sf->field, flow, &value);
2374 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2377 /* Formats 'sf' into 's' in a format normally acceptable to
2378 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2379 * sf->field has no NXM name.) */
2381 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2384 ds_put_cstr(s, "<unknown>");
2385 } else if (sf->field->nxm_name) {
2386 ds_put_cstr(s, sf->field->nxm_name);
2387 } else if (sf->field->nxm_header) {
2388 uint32_t header = sf->field->nxm_header;
2389 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2391 ds_put_cstr(s, sf->field->name);
2394 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2395 ds_put_cstr(s, "[]");
2396 } else if (sf->n_bits == 1) {
2397 ds_put_format(s, "[%d]", sf->ofs);
2399 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2403 static const struct mf_field *
2404 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2408 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2413 for (i = 0; i < MFF_N_IDS; i++) {
2414 const struct mf_field *mf = mf_from_id(i);
2417 && !strncmp(mf->nxm_name, name, name_len)
2418 && mf->nxm_name[name_len] == '\0') {
2422 && !strncmp(mf->oxm_name, name, name_len)
2423 && mf->oxm_name[name_len] == '\0') {
2431 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2432 * returns NULL and advances '*sp' to the first byte following the parsed
2433 * string. On failure, returns a malloc()'d error message, does not modify
2434 * '*sp', and does not properly initialize 'sf'.
2436 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2437 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2438 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2439 * may both be omitted (the [] are still required) to indicate an entire
2442 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2444 const struct mf_field *field;
2453 name_len = strcspn(s, "[");
2454 if (s[name_len] != '[') {
2455 return xasprintf("%s: missing [ looking for field name", *sp);
2458 field = mf_parse_subfield_name(name, name_len, &wild);
2460 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2464 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2465 /* Nothing to do. */
2466 } else if (sscanf(s, "[%d]", &start) == 1) {
2468 } else if (!strncmp(s, "[]", 2)) {
2470 end = field->n_bits - 1;
2472 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2473 "[<start>..<end>]", *sp);
2475 s = strchr(s, ']') + 1;
2478 return xasprintf("%s: starting bit %d is after ending bit %d",
2480 } else if (start >= field->n_bits) {
2481 return xasprintf("%s: starting bit %d is not valid because field is "
2482 "only %d bits wide", *sp, start, field->n_bits);
2483 } else if (end >= field->n_bits){
2484 return xasprintf("%s: ending bit %d is not valid because field is "
2485 "only %d bits wide", *sp, end, field->n_bits);
2490 sf->n_bits = end - start + 1;
2496 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2497 * byte in 's' following the parsed string.
2499 * Exits with an error message if 's' has incorrect syntax.
2501 * The syntax parsed from 's' takes the form "header[start..end]" where
2502 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2503 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2504 * may both be omitted (the [] are still required) to indicate an entire
2507 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2509 char *msg = mf_parse_subfield__(sf, &s);
2511 ovs_fatal(0, "%s", msg);
2517 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2521 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2522 if (subvalue->u8[i]) {
2523 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2524 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2525 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2530 ds_put_char(s, '0');