2 * Copyright (c) 2011, 2012 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"
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-errors.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
40 #define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
50 MFF_TUN_ID, "tun_id", NULL,
56 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
59 MFF_METADATA, "metadata", NULL,
65 OXM_OF_METADATA, "OXM_OF_METADATA",
66 OXM_OF_METADATA, "OXM_OF_METADATA",
68 MFF_IN_PORT, "in_port", NULL,
74 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
75 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
78 #define REGISTER(IDX) \
80 MFF_REG##IDX, "reg" #IDX, NULL, \
81 MF_FIELD_SIZES(be32), \
86 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
87 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
122 MFF_ETH_SRC, "eth_src", "dl_src",
128 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
129 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
131 MFF_ETH_DST, "eth_dst", "dl_dst",
137 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
138 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
140 MFF_ETH_TYPE, "eth_type", "dl_type",
141 MF_FIELD_SIZES(be16),
146 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
147 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
151 MFF_VLAN_TCI, "vlan_tci", NULL,
152 MF_FIELD_SIZES(be16),
157 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
158 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
160 MFF_DL_VLAN, "dl_vlan", NULL,
161 sizeof(ovs_be16), 12,
169 MFF_VLAN_VID, "vlan_vid", NULL,
170 sizeof(ovs_be16), 12,
175 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
176 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
178 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
187 MFF_VLAN_PCP, "vlan_pcp", NULL,
193 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
194 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
202 MFF_IPV4_SRC, "ip_src", "nw_src",
203 MF_FIELD_SIZES(be32),
208 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
209 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
211 MFF_IPV4_DST, "ip_dst", "nw_dst",
212 MF_FIELD_SIZES(be32),
217 NXM_OF_IP_DST, "NXM_OF_IP_DST",
218 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
222 MFF_IPV6_SRC, "ipv6_src", NULL,
223 MF_FIELD_SIZES(ipv6),
228 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
229 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
231 MFF_IPV6_DST, "ipv6_dst", NULL,
232 MF_FIELD_SIZES(ipv6),
237 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
238 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
241 MFF_IPV6_LABEL, "ipv6_label", NULL,
247 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
248 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
252 MFF_IP_PROTO, "nw_proto", NULL,
258 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
259 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
261 MFF_IP_DSCP, "nw_tos", NULL,
267 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
268 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
270 MFF_IP_ECN, "nw_ecn", NULL,
276 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
277 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
279 MFF_IP_TTL, "nw_ttl", NULL,
285 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
286 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
288 MFF_IP_FRAG, "ip_frag", NULL,
294 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
295 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
299 MFF_ARP_OP, "arp_op", NULL,
300 MF_FIELD_SIZES(be16),
305 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
306 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
308 MFF_ARP_SPA, "arp_spa", NULL,
309 MF_FIELD_SIZES(be32),
314 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
315 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
317 MFF_ARP_TPA, "arp_tpa", NULL,
318 MF_FIELD_SIZES(be32),
323 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
324 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
326 MFF_ARP_SHA, "arp_sha", NULL,
332 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
333 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
335 MFF_ARP_THA, "arp_tha", NULL,
341 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
342 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
350 MFF_TCP_SRC, "tcp_src", "tp_src",
351 MF_FIELD_SIZES(be16),
356 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
357 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
359 MFF_TCP_DST, "tcp_dst", "tp_dst",
360 MF_FIELD_SIZES(be16),
365 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
366 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
370 MFF_UDP_SRC, "udp_src", NULL,
371 MF_FIELD_SIZES(be16),
376 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
377 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
379 MFF_UDP_DST, "udp_dst", NULL,
380 MF_FIELD_SIZES(be16),
385 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
386 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
390 MFF_ICMPV4_TYPE, "icmp_type", NULL,
396 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
397 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
399 MFF_ICMPV4_CODE, "icmp_code", NULL,
405 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
406 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
410 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
416 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
417 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
419 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
425 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
426 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
434 MFF_ND_TARGET, "nd_target", NULL,
435 MF_FIELD_SIZES(ipv6),
440 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
441 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
443 MFF_ND_SLL, "nd_sll", NULL,
449 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
450 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
452 MFF_ND_TLL, "nd_tll", NULL,
458 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
459 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
463 /* Maps an NXM or OXM header value to an mf_field. */
465 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
466 uint32_t header; /* NXM or OXM header value. */
467 const struct mf_field *mf;
470 /* Contains 'struct nxm_field's. */
471 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
473 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
474 * controller and so there's not much point in showing a lot of them. */
475 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
477 const struct mf_field *mf_from_nxm_header__(uint32_t header);
479 /* Returns the field with the given 'id'. */
480 const struct mf_field *
481 mf_from_id(enum mf_field_id id)
483 assert((unsigned int) id < MFF_N_IDS);
484 return &mf_fields[id];
487 /* Returns the field with the given 'name', or a null pointer if no field has
489 const struct mf_field *
490 mf_from_name(const char *name)
492 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
494 if (shash_is_empty(&mf_by_name)) {
495 const struct mf_field *mf;
497 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
498 shash_add_once(&mf_by_name, mf->name, mf);
499 if (mf->extra_name) {
500 shash_add_once(&mf_by_name, mf->extra_name, mf);
505 return shash_find_data(&mf_by_name, name);
509 add_nxm_field(uint32_t header, const struct mf_field *mf)
513 f = xmalloc(sizeof *f);
514 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
520 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
523 assert(!mf_from_nxm_header__(header));
524 add_nxm_field(header, mf);
525 if (mf->maskable != MFM_NONE) {
526 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
534 const struct mf_field *mf;
536 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
537 nxm_init_add_field(mf, mf->nxm_header);
538 if (mf->oxm_header != mf->nxm_header) {
539 nxm_init_add_field(mf, mf->oxm_header);
544 const struct mf_field *
545 mf_from_nxm_header(uint32_t header)
547 if (hmap_is_empty(&all_fields)) {
550 return mf_from_nxm_header__(header);
553 const struct mf_field *
554 mf_from_nxm_header__(uint32_t header)
556 const struct nxm_field *f;
558 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
559 if (f->header == header) {
567 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
568 * specifies at least one bit in the field.
570 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
571 * meets 'mf''s prerequisites. */
573 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
577 return !wc->masks.tunnel.tun_id;
579 return !wc->masks.metadata;
581 return !wc->masks.in_port;
583 return !wc->masks.regs[mf->id - MFF_REG0];
586 return eth_addr_is_zero(wc->masks.dl_src);
588 return eth_addr_is_zero(wc->masks.dl_dst);
590 return !wc->masks.dl_type;
594 return eth_addr_is_zero(wc->masks.arp_sha);
598 return eth_addr_is_zero(wc->masks.arp_tha);
601 return !wc->masks.vlan_tci;
603 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
605 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
606 case MFF_DL_VLAN_PCP:
608 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
611 return !wc->masks.nw_src;
613 return !wc->masks.nw_dst;
616 return ipv6_mask_is_any(&wc->masks.ipv6_src);
618 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
621 return !wc->masks.ipv6_label;
624 return !wc->masks.nw_proto;
626 return !(wc->masks.nw_tos & IP_DSCP_MASK);
628 return !(wc->masks.nw_tos & IP_ECN_MASK);
630 return !wc->masks.nw_ttl;
633 return ipv6_mask_is_any(&wc->masks.nd_target);
636 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
639 return !wc->masks.nw_proto;
641 return !wc->masks.nw_src;
643 return !wc->masks.nw_dst;
647 case MFF_ICMPV4_TYPE:
648 case MFF_ICMPV6_TYPE:
649 return !wc->masks.tp_src;
652 case MFF_ICMPV4_CODE:
653 case MFF_ICMPV6_CODE:
654 return !wc->masks.tp_dst;
662 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
663 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
664 * purposes, or to 0 if it is wildcarded.
666 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
667 * meets 'mf''s prerequisites. */
669 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
670 union mf_value *mask)
674 mask->be64 = wc->masks.tunnel.tun_id;
677 mask->be64 = wc->masks.metadata;
680 mask->be16 = htons(wc->masks.in_port);
683 mask->be32 = htonl(wc->masks.regs[mf->id - MFF_REG0]);
687 memcpy(mask->mac, wc->masks.dl_dst, ETH_ADDR_LEN);
690 memcpy(mask->mac, wc->masks.dl_src, ETH_ADDR_LEN);
693 mask->be16 = wc->masks.dl_type;
697 mask->be16 = wc->masks.vlan_tci;
700 mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
703 mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
705 case MFF_DL_VLAN_PCP:
707 mask->u8 = vlan_tci_to_pcp(wc->masks.vlan_tci);
711 mask->be32 = wc->masks.nw_src;
714 mask->be32 = wc->masks.nw_dst;
718 mask->ipv6 = wc->masks.ipv6_src;
721 mask->ipv6 = wc->masks.ipv6_dst;
724 mask->be32 = wc->masks.ipv6_label;
728 mask->u8 = wc->masks.nw_proto;
731 mask->u8 = wc->masks.nw_tos & IP_DSCP_MASK;
734 mask->u8 = wc->masks.nw_tos & IP_ECN_MASK;
738 mask->ipv6 = wc->masks.nd_target;
742 mask->u8 = wc->masks.nw_ttl;
745 mask->u8 = wc->masks.nw_frag & FLOW_NW_FRAG_MASK;
749 mask->u8 = wc->masks.nw_proto;
752 mask->be32 = wc->masks.nw_src;
755 mask->be32 = wc->masks.nw_dst;
759 memcpy(mask->mac, wc->masks.arp_sha, ETH_ADDR_LEN);
763 memcpy(mask->mac, wc->masks.arp_tha, ETH_ADDR_LEN);
768 mask->be16 = wc->masks.tp_src;
772 mask->be16 = wc->masks.tp_dst;
775 case MFF_ICMPV4_TYPE:
776 case MFF_ICMPV6_TYPE:
777 mask->u8 = ntohs(wc->masks.tp_src);
779 case MFF_ICMPV4_CODE:
780 case MFF_ICMPV6_CODE:
781 mask->u8 = ntohs(wc->masks.tp_dst);
790 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
791 * if the mask is valid, false otherwise. */
793 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
795 switch (mf->maskable) {
797 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
798 is_all_ones((const uint8_t *) mask, mf->n_bytes));
808 is_ip_any(const struct flow *flow)
810 return (flow->dl_type == htons(ETH_TYPE_IP) ||
811 flow->dl_type == htons(ETH_TYPE_IPV6));
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 is_ip_any(flow);
849 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
851 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
853 return is_icmpv4(flow);
855 return is_icmpv6(flow);
858 return (is_icmpv6(flow)
859 && flow->tp_dst == htons(0)
860 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
861 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
863 return (is_icmpv6(flow)
864 && flow->tp_dst == htons(0)
865 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
867 return (is_icmpv6(flow)
868 && flow->tp_dst == htons(0)
869 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
875 /* Returns true if 'value' may be a valid value *as part of a masked match*,
878 * A value is not rejected just because it is not valid for the field in
879 * question, but only if it doesn't make sense to test the bits in question at
880 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
881 * without the VLAN_CFI bit being set, but we can't reject those values because
882 * it is still legitimate to test just for those bits (see the documentation
883 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
884 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
886 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
911 case MFF_ICMPV4_TYPE:
912 case MFF_ICMPV4_CODE:
913 case MFF_ICMPV6_TYPE:
914 case MFF_ICMPV6_CODE:
921 return !(value->u8 & ~IP_DSCP_MASK);
923 return !(value->u8 & ~IP_ECN_MASK);
925 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
928 return !(value->be16 & htons(0xff00));
931 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
933 return !(value->be16 & htons(VLAN_PCP_MASK));
935 case MFF_DL_VLAN_PCP:
937 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
940 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
948 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
949 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
951 mf_get_value(const struct mf_field *mf, const struct flow *flow,
952 union mf_value *value)
956 value->be64 = flow->tunnel.tun_id;
959 value->be64 = flow->metadata;
963 value->be16 = htons(flow->in_port);
967 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
971 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
975 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
979 value->be16 = flow->dl_type;
983 value->be16 = flow->vlan_tci;
987 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
990 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
993 case MFF_DL_VLAN_PCP:
995 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
999 value->be32 = flow->nw_src;
1003 value->be32 = flow->nw_dst;
1007 value->ipv6 = flow->ipv6_src;
1011 value->ipv6 = flow->ipv6_dst;
1014 case MFF_IPV6_LABEL:
1015 value->be32 = flow->ipv6_label;
1019 value->u8 = flow->nw_proto;
1023 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1027 value->u8 = flow->nw_tos & IP_ECN_MASK;
1031 value->u8 = flow->nw_ttl;
1035 value->u8 = flow->nw_frag;
1039 value->be16 = htons(flow->nw_proto);
1043 value->be32 = flow->nw_src;
1047 value->be32 = flow->nw_dst;
1052 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1057 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1062 value->be16 = flow->tp_src;
1067 value->be16 = flow->tp_dst;
1070 case MFF_ICMPV4_TYPE:
1071 case MFF_ICMPV6_TYPE:
1072 value->u8 = ntohs(flow->tp_src);
1075 case MFF_ICMPV4_CODE:
1076 case MFF_ICMPV6_CODE:
1077 value->u8 = ntohs(flow->tp_dst);
1081 value->ipv6 = flow->nd_target;
1090 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1091 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1094 mf_set_value(const struct mf_field *mf,
1095 const union mf_value *value, struct match *match)
1099 match_set_tun_id(match, value->be64);
1102 match_set_metadata(match, value->be64);
1106 match_set_in_port(match, ntohs(value->be16));
1110 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1114 match_set_dl_src(match, value->mac);
1118 match_set_dl_dst(match, value->mac);
1122 match_set_dl_type(match, value->be16);
1126 match_set_dl_tci(match, value->be16);
1130 match_set_dl_vlan(match, value->be16);
1133 match_set_vlan_vid(match, value->be16);
1136 case MFF_DL_VLAN_PCP:
1138 match_set_dl_vlan_pcp(match, value->u8);
1142 match_set_nw_src(match, value->be32);
1146 match_set_nw_dst(match, value->be32);
1150 match_set_ipv6_src(match, &value->ipv6);
1154 match_set_ipv6_dst(match, &value->ipv6);
1157 case MFF_IPV6_LABEL:
1158 match_set_ipv6_label(match, value->be32);
1162 match_set_nw_proto(match, value->u8);
1166 match_set_nw_dscp(match, value->u8);
1170 match_set_nw_ecn(match, value->u8);
1174 match_set_nw_ttl(match, value->u8);
1178 match_set_nw_frag(match, value->u8);
1182 match_set_nw_proto(match, ntohs(value->be16));
1186 match_set_nw_src(match, value->be32);
1190 match_set_nw_dst(match, value->be32);
1195 match_set_arp_sha(match, value->mac);
1200 match_set_arp_tha(match, value->mac);
1205 match_set_tp_src(match, value->be16);
1210 match_set_tp_dst(match, value->be16);
1213 case MFF_ICMPV4_TYPE:
1214 case MFF_ICMPV6_TYPE:
1215 match_set_icmp_type(match, value->u8);
1218 case MFF_ICMPV4_CODE:
1219 case MFF_ICMPV6_CODE:
1220 match_set_icmp_code(match, value->u8);
1224 match_set_nd_target(match, &value->ipv6);
1233 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1234 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1237 mf_set_flow_value(const struct mf_field *mf,
1238 const union mf_value *value, struct flow *flow)
1242 flow->tunnel.tun_id = value->be64;
1245 flow->metadata = value->be64;
1249 flow->in_port = ntohs(value->be16);
1253 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1257 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1261 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1265 flow->dl_type = value->be16;
1269 flow->vlan_tci = value->be16;
1273 flow_set_dl_vlan(flow, value->be16);
1276 flow_set_vlan_vid(flow, value->be16);
1279 case MFF_DL_VLAN_PCP:
1281 flow_set_vlan_pcp(flow, value->u8);
1285 flow->nw_src = value->be32;
1289 flow->nw_dst = value->be32;
1293 flow->ipv6_src = value->ipv6;
1297 flow->ipv6_dst = value->ipv6;
1300 case MFF_IPV6_LABEL:
1301 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1305 flow->nw_proto = value->u8;
1309 flow->nw_tos &= ~IP_DSCP_MASK;
1310 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1314 flow->nw_tos &= ~IP_ECN_MASK;
1315 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1319 flow->nw_ttl = value->u8;
1323 flow->nw_frag &= value->u8;
1327 flow->nw_proto = ntohs(value->be16);
1331 flow->nw_src = value->be32;
1335 flow->nw_dst = value->be32;
1340 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1345 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1350 flow->tp_src = value->be16;
1355 flow->tp_dst = value->be16;
1358 case MFF_ICMPV4_TYPE:
1359 case MFF_ICMPV6_TYPE:
1360 flow->tp_src = htons(value->u8);
1363 case MFF_ICMPV4_CODE:
1364 case MFF_ICMPV6_CODE:
1365 flow->tp_dst = htons(value->u8);
1369 flow->nd_target = value->ipv6;
1378 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1380 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1383 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1385 union mf_value value;
1387 mf_get_value(mf, flow, &value);
1388 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1391 /* Makes 'match' wildcard field 'mf'.
1393 * The caller is responsible for ensuring that 'match' meets 'mf''s
1396 mf_set_wild(const struct mf_field *mf, struct match *match)
1400 match_set_tun_id_masked(match, htonll(0), htonll(0));
1403 match_set_metadata_masked(match, htonll(0), htonll(0));
1406 match->flow.in_port = 0;
1407 match->wc.masks.in_port = 0;
1411 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1415 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1416 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1420 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1421 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1425 match->flow.dl_type = htons(0);
1426 match->wc.masks.dl_type = htons(0);
1430 match_set_dl_tci_masked(match, htons(0), htons(0));
1435 match_set_any_vid(match);
1438 case MFF_DL_VLAN_PCP:
1440 match_set_any_pcp(match);
1445 match_set_nw_src_masked(match, htonl(0), htonl(0));
1450 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1454 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1455 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1459 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1460 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1463 case MFF_IPV6_LABEL:
1464 match->wc.masks.ipv6_label = htonl(0);
1465 match->flow.ipv6_label = htonl(0);
1469 match->wc.masks.nw_proto = 0;
1470 match->flow.nw_proto = 0;
1474 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1475 match->flow.nw_tos &= ~IP_DSCP_MASK;
1479 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1480 match->flow.nw_tos &= ~IP_ECN_MASK;
1484 match->wc.masks.nw_ttl = 0;
1485 match->flow.nw_ttl = 0;
1489 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1490 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1494 match->wc.masks.nw_proto = 0;
1495 match->flow.nw_proto = 0;
1500 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1501 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1506 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1507 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1512 case MFF_ICMPV4_TYPE:
1513 case MFF_ICMPV6_TYPE:
1514 match->wc.masks.tp_src = htons(0);
1515 match->flow.tp_src = htons(0);
1520 case MFF_ICMPV4_CODE:
1521 case MFF_ICMPV6_CODE:
1522 match->wc.masks.tp_dst = htons(0);
1523 match->flow.tp_dst = htons(0);
1527 memset(&match->wc.masks.nd_target, 0,
1528 sizeof match->wc.masks.nd_target);
1529 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1538 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1539 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1540 * with a 1-bit indicating that the corresponding value bit must match and a
1541 * 0-bit indicating a don't-care.
1543 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1544 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1545 * call is equivalent to mf_set_wild(mf, match).
1547 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1548 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1550 mf_set(const struct mf_field *mf,
1551 const union mf_value *value, const union mf_value *mask,
1552 struct match *match)
1554 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1555 mf_set_value(mf, value, match);
1557 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1558 mf_set_wild(mf, match);
1566 case MFF_DL_VLAN_PCP:
1573 case MFF_ICMPV4_TYPE:
1574 case MFF_ICMPV4_CODE:
1575 case MFF_ICMPV6_TYPE:
1576 case MFF_ICMPV6_CODE:
1580 match_set_tun_id_masked(match, value->be64, mask->be64);
1583 match_set_metadata_masked(match, value->be64, mask->be64);
1587 match_set_reg_masked(match, mf->id - MFF_REG0,
1588 ntohl(value->be32), ntohl(mask->be32));
1592 match_set_dl_dst_masked(match, value->mac, mask->mac);
1596 match_set_dl_src_masked(match, value->mac, mask->mac);
1601 match_set_arp_sha_masked(match, value->mac, mask->mac);
1606 match_set_arp_tha_masked(match, value->mac, mask->mac);
1610 match_set_dl_tci_masked(match, value->be16, mask->be16);
1614 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1618 match_set_nw_src_masked(match, value->be32, mask->be32);
1622 match_set_nw_dst_masked(match, value->be32, mask->be32);
1626 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1630 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1633 case MFF_IPV6_LABEL:
1634 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1635 mf_set_value(mf, value, match);
1637 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1642 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1646 match_set_nw_frag_masked(match, value->u8, mask->u8);
1650 match_set_nw_src_masked(match, value->be32, mask->be32);
1654 match_set_nw_dst_masked(match, value->be32, mask->be32);
1659 match_set_tp_src_masked(match, value->be16, mask->be16);
1664 match_set_tp_dst_masked(match, value->be16, mask->be16);
1674 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1678 VLOG_WARN_RL(&rl, "unknown %s field", type);
1679 } else if (!sf->n_bits) {
1680 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1681 } else if (sf->ofs >= sf->field->n_bits) {
1682 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1683 sf->ofs, sf->field->n_bits, type, sf->field->name);
1684 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1685 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1686 "of %s field %s", sf->ofs, sf->n_bits,
1687 sf->field->n_bits, type, sf->field->name);
1688 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1689 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1690 type, sf->field->name);
1695 return OFPERR_OFPBAC_BAD_ARGUMENT;
1698 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1699 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1702 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1704 return mf_check__(sf, flow, "source");
1707 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1708 * if so, otherwise an OpenFlow error code (e.g. as returned by
1711 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1713 int error = mf_check__(sf, flow, "destination");
1714 if (!error && !sf->field->writable) {
1715 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1717 return OFPERR_OFPBAC_BAD_ARGUMENT;
1722 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1723 * 'value' and 'mask', respectively. */
1725 mf_get(const struct mf_field *mf, const struct match *match,
1726 union mf_value *value, union mf_value *mask)
1728 mf_get_value(mf, &match->flow, value);
1729 mf_get_mask(mf, &match->wc, mask);
1732 /* Assigns a random value for field 'mf' to 'value'. */
1734 mf_random_value(const struct mf_field *mf, union mf_value *value)
1736 random_bytes(value, mf->n_bytes);
1761 case MFF_ICMPV4_TYPE:
1762 case MFF_ICMPV4_CODE:
1763 case MFF_ICMPV6_TYPE:
1764 case MFF_ICMPV6_CODE:
1770 case MFF_IPV6_LABEL:
1771 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1775 value->u8 &= IP_DSCP_MASK;
1779 value->u8 &= IP_ECN_MASK;
1783 value->u8 &= FLOW_NW_FRAG_MASK;
1787 value->be16 &= htons(0xff);
1791 value->be16 &= htons(VLAN_VID_MASK);
1794 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1797 case MFF_DL_VLAN_PCP:
1809 mf_from_integer_string(const struct mf_field *mf, const char *s,
1810 uint8_t *valuep, uint8_t *maskp)
1812 unsigned long long int integer, mask;
1817 integer = strtoull(s, &tail, 0);
1818 if (errno || (*tail != '\0' && *tail != '/')) {
1823 mask = strtoull(tail + 1, &tail, 0);
1824 if (errno || *tail != '\0') {
1831 for (i = mf->n_bytes - 1; i >= 0; i--) {
1832 valuep[i] = integer;
1838 return xasprintf("%s: value too large for %u-byte field %s",
1839 s, mf->n_bytes, mf->name);
1844 return xasprintf("%s: bad syntax for %s", s, mf->name);
1848 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1849 uint8_t mac[ETH_ADDR_LEN],
1850 uint8_t mask[ETH_ADDR_LEN])
1852 assert(mf->n_bytes == ETH_ADDR_LEN);
1854 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1855 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1856 case ETH_ADDR_SCAN_COUNT * 2:
1859 case ETH_ADDR_SCAN_COUNT:
1860 memset(mask, 0xff, ETH_ADDR_LEN);
1864 return xasprintf("%s: invalid Ethernet address", s);
1869 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1870 ovs_be32 *ip, ovs_be32 *mask)
1874 assert(mf->n_bytes == sizeof *ip);
1876 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1877 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1879 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1880 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1881 if (prefix <= 0 || prefix > 32) {
1882 return xasprintf("%s: network prefix bits not between 1 and "
1884 } else if (prefix == 32) {
1885 *mask = htonl(UINT32_MAX);
1887 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1889 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1890 *mask = htonl(UINT32_MAX);
1892 return xasprintf("%s: invalid IP address", s);
1898 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1899 struct in6_addr *value, struct in6_addr *mask)
1901 char *str = xstrdup(s);
1902 char *save_ptr = NULL;
1903 const char *name, *netmask;
1906 assert(mf->n_bytes == sizeof *value);
1908 name = strtok_r(str, "/", &save_ptr);
1909 retval = name ? lookup_ipv6(name, value) : EINVAL;
1913 err = xasprintf("%s: could not convert to IPv6 address", str);
1919 netmask = strtok_r(NULL, "/", &save_ptr);
1921 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1922 int prefix = atoi(netmask);
1923 if (prefix <= 0 || prefix > 128) {
1925 return xasprintf("%s: prefix bits not between 1 and 128", s);
1927 *mask = ipv6_create_mask(prefix);
1931 *mask = in6addr_exact;
1939 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1940 ovs_be16 *valuep, ovs_be16 *maskp)
1944 assert(mf->n_bytes == sizeof(ovs_be16));
1945 if (ofputil_port_from_string(s, &port)) {
1946 *valuep = htons(port);
1947 *maskp = htons(UINT16_MAX);
1950 return mf_from_integer_string(mf, s,
1951 (uint8_t *) valuep, (uint8_t *) maskp);
1955 struct frag_handling {
1961 static const struct frag_handling all_frags[] = {
1962 #define A FLOW_NW_FRAG_ANY
1963 #define L FLOW_NW_FRAG_LATER
1964 /* name mask value */
1967 { "first", A|L, A },
1968 { "later", A|L, A|L },
1973 { "not_later", L, 0 },
1980 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1982 const struct frag_handling *h;
1984 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1985 if (!strcasecmp(s, h->name)) {
1986 /* We force the upper bits of the mask on to make mf_parse_value()
1987 * happy (otherwise it will never think it's an exact match.) */
1988 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1994 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1995 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1998 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1999 * NULL if successful, otherwise a malloc()'d string describing the error. */
2001 mf_parse(const struct mf_field *mf, const char *s,
2002 union mf_value *value, union mf_value *mask)
2004 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2005 memset(value, 0, mf->n_bytes);
2006 memset(mask, 0, mf->n_bytes);
2010 switch (mf->string) {
2012 case MFS_HEXADECIMAL:
2013 return mf_from_integer_string(mf, s,
2014 (uint8_t *) value, (uint8_t *) mask);
2017 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2020 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2023 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2026 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2029 return mf_from_frag_string(s, &value->u8, &mask->u8);
2034 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2035 * successful, otherwise a malloc()'d string describing the error. */
2037 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2039 union mf_value mask;
2042 error = mf_parse(mf, s, value, &mask);
2047 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2048 return xasprintf("%s: wildcards not allowed here", s);
2054 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2055 const uint8_t *maskp, struct ds *s)
2057 unsigned long long int integer;
2060 assert(mf->n_bytes <= 8);
2063 for (i = 0; i < mf->n_bytes; i++) {
2064 integer = (integer << 8) | valuep[i];
2066 if (mf->string == MFS_HEXADECIMAL) {
2067 ds_put_format(s, "%#llx", integer);
2069 ds_put_format(s, "%lld", integer);
2073 unsigned long long int mask;
2076 for (i = 0; i < mf->n_bytes; i++) {
2077 mask = (mask << 8) | maskp[i];
2080 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2081 * not sure that that a bit-mask written in decimal is ever easier to
2082 * understand than the same bit-mask written in hexadecimal. */
2083 ds_put_format(s, "/%#llx", mask);
2088 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2091 const struct frag_handling *h;
2092 uint8_t value = *valuep;
2093 uint8_t mask = *maskp;
2096 mask &= FLOW_NW_FRAG_MASK;
2098 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2099 if (value == h->value && mask == h->mask) {
2100 ds_put_cstr(s, h->name);
2104 ds_put_cstr(s, "<error>");
2107 /* Appends to 's' a string representation of field 'mf' whose value is in
2108 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2110 mf_format(const struct mf_field *mf,
2111 const union mf_value *value, const union mf_value *mask,
2115 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2116 ds_put_cstr(s, "ANY");
2118 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2123 switch (mf->string) {
2126 ofputil_format_port(ntohs(value->be16), s);
2131 case MFS_HEXADECIMAL:
2132 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2136 eth_format_masked(value->mac, mask->mac, s);
2140 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2145 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2149 mf_format_frag_string(&value->u8, &mask->u8, s);
2157 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2158 * least-significant bits in 'x'.
2161 mf_write_subfield_flow(const struct mf_subfield *sf,
2162 const union mf_subvalue *x, struct flow *flow)
2164 const struct mf_field *field = sf->field;
2165 union mf_value value;
2167 mf_get_value(field, flow, &value);
2168 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2169 sf->ofs, sf->n_bits);
2170 mf_set_flow_value(field, &value, flow);
2173 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2174 * least-significant bits in 'x'.
2177 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2178 struct match *match)
2180 const struct mf_field *field = sf->field;
2181 union mf_value value, mask;
2183 mf_get(field, match, &value, &mask);
2184 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2185 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2186 mf_set(field, &value, &mask, match);
2189 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2190 * reading 'flow', e.g. as checked by mf_check_src(). */
2192 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2193 union mf_subvalue *x)
2195 union mf_value value;
2197 mf_get_value(sf->field, flow, &value);
2199 memset(x, 0, sizeof *x);
2200 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2205 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2206 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2209 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2211 union mf_value value;
2213 mf_get_value(sf->field, flow, &value);
2214 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2217 /* Formats 'sf' into 's' in a format normally acceptable to
2218 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2219 * sf->field has no NXM name.) */
2221 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2224 ds_put_cstr(s, "<unknown>");
2225 } else if (sf->field->nxm_name) {
2226 ds_put_cstr(s, sf->field->nxm_name);
2227 } else if (sf->field->nxm_header) {
2228 uint32_t header = sf->field->nxm_header;
2229 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2231 ds_put_cstr(s, sf->field->name);
2234 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2235 ds_put_cstr(s, "[]");
2236 } else if (sf->n_bits == 1) {
2237 ds_put_format(s, "[%d]", sf->ofs);
2239 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2243 static const struct mf_field *
2244 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2248 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2253 for (i = 0; i < MFF_N_IDS; i++) {
2254 const struct mf_field *mf = mf_from_id(i);
2257 && !strncmp(mf->nxm_name, name, name_len)
2258 && mf->nxm_name[name_len] == '\0') {
2262 && !strncmp(mf->oxm_name, name, name_len)
2263 && mf->oxm_name[name_len] == '\0') {
2271 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2272 * returns NULL and advances '*sp' to the first byte following the parsed
2273 * string. On failure, returns a malloc()'d error message, does not modify
2274 * '*sp', and does not properly initialize 'sf'.
2276 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2277 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2278 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2279 * may both be omitted (the [] are still required) to indicate an entire
2282 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2284 const struct mf_field *field;
2293 name_len = strcspn(s, "[");
2294 if (s[name_len] != '[') {
2295 return xasprintf("%s: missing [ looking for field name", *sp);
2298 field = mf_parse_subfield_name(name, name_len, &wild);
2300 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2304 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2305 /* Nothing to do. */
2306 } else if (sscanf(s, "[%d]", &start) == 1) {
2308 } else if (!strncmp(s, "[]", 2)) {
2310 end = field->n_bits - 1;
2312 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2313 "[<start>..<end>]", *sp);
2315 s = strchr(s, ']') + 1;
2318 return xasprintf("%s: starting bit %d is after ending bit %d",
2320 } else if (start >= field->n_bits) {
2321 return xasprintf("%s: starting bit %d is not valid because field is "
2322 "only %d bits wide", *sp, start, field->n_bits);
2323 } else if (end >= field->n_bits){
2324 return xasprintf("%s: ending bit %d is not valid because field is "
2325 "only %d bits wide", *sp, end, field->n_bits);
2330 sf->n_bits = end - start + 1;
2336 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2337 * byte in 's' following the parsed string.
2339 * Exits with an error message if 's' has incorrect syntax.
2341 * The syntax parsed from 's' takes the form "header[start..end]" where
2342 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2343 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2344 * may both be omitted (the [] are still required) to indicate an entire
2347 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2349 char *msg = mf_parse_subfield__(sf, &s);
2351 ovs_fatal(0, "%s", msg);
2357 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2361 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2362 if (subvalue->u8[i]) {
2363 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2364 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2365 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2370 ds_put_char(s, '0');