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,
70 MFM_NONE, FWW_IN_PORT,
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),
142 MFM_NONE, FWW_DL_TYPE,
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_VLAN_VID, "dl_vlan", NULL,
161 sizeof(ovs_be16), 12,
166 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
167 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
169 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
175 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
176 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
184 MFF_IPV4_SRC, "ip_src", "nw_src",
185 MF_FIELD_SIZES(be32),
190 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
191 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
193 MFF_IPV4_DST, "ip_dst", "nw_dst",
194 MF_FIELD_SIZES(be32),
199 NXM_OF_IP_DST, "NXM_OF_IP_DST",
200 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
204 MFF_IPV6_SRC, "ipv6_src", NULL,
205 MF_FIELD_SIZES(ipv6),
210 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
211 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
213 MFF_IPV6_DST, "ipv6_dst", NULL,
214 MF_FIELD_SIZES(ipv6),
219 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
220 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
223 MFF_IPV6_LABEL, "ipv6_label", NULL,
225 MFM_NONE, FWW_IPV6_LABEL,
229 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
230 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
234 MFF_IP_PROTO, "nw_proto", NULL,
236 MFM_NONE, FWW_NW_PROTO,
240 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
241 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
243 MFF_IP_DSCP, "nw_tos", NULL,
245 MFM_NONE, FWW_NW_DSCP,
249 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
250 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
252 MFF_IP_ECN, "nw_ecn", NULL,
254 MFM_NONE, FWW_NW_ECN,
258 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
259 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
261 MFF_IP_TTL, "nw_ttl", NULL,
263 MFM_NONE, FWW_NW_TTL,
267 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
268 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
270 MFF_IP_FRAG, "ip_frag", NULL,
276 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
277 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
281 MFF_ARP_OP, "arp_op", NULL,
282 MF_FIELD_SIZES(be16),
283 MFM_NONE, FWW_NW_PROTO,
287 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
288 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
290 MFF_ARP_SPA, "arp_spa", NULL,
291 MF_FIELD_SIZES(be32),
296 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
297 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
299 MFF_ARP_TPA, "arp_tpa", NULL,
300 MF_FIELD_SIZES(be32),
305 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
306 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
308 MFF_ARP_SHA, "arp_sha", NULL,
310 MFM_NONE, FWW_ARP_SHA,
314 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
315 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
317 MFF_ARP_THA, "arp_tha", NULL,
319 MFM_NONE, FWW_ARP_THA,
323 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
324 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
332 MFF_TCP_SRC, "tcp_src", "tp_src",
333 MF_FIELD_SIZES(be16),
338 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
339 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
341 MFF_TCP_DST, "tcp_dst", "tp_dst",
342 MF_FIELD_SIZES(be16),
347 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
348 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
352 MFF_UDP_SRC, "udp_src", NULL,
353 MF_FIELD_SIZES(be16),
358 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
359 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
361 MFF_UDP_DST, "udp_dst", NULL,
362 MF_FIELD_SIZES(be16),
367 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
368 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
372 MFF_ICMPV4_TYPE, "icmp_type", NULL,
378 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
379 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
381 MFF_ICMPV4_CODE, "icmp_code", NULL,
387 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
388 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
392 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
398 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
399 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
401 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
407 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
408 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
416 MFF_ND_TARGET, "nd_target", NULL,
417 MF_FIELD_SIZES(ipv6),
422 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
423 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
425 MFF_ND_SLL, "nd_sll", NULL,
427 MFM_NONE, FWW_ARP_SHA,
431 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
432 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
434 MFF_ND_TLL, "nd_tll", NULL,
436 MFM_NONE, FWW_ARP_THA,
440 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
441 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
445 /* Maps an NXM or OXM header value to an mf_field. */
447 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
448 uint32_t header; /* NXM or OXM header value. */
449 const struct mf_field *mf;
452 /* Contains 'struct nxm_field's. */
453 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
455 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
456 * controller and so there's not much point in showing a lot of them. */
457 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
459 const struct mf_field *mf_from_nxm_header__(uint32_t header);
461 /* Returns the field with the given 'id'. */
462 const struct mf_field *
463 mf_from_id(enum mf_field_id id)
465 assert((unsigned int) id < MFF_N_IDS);
466 return &mf_fields[id];
469 /* Returns the field with the given 'name', or a null pointer if no field has
471 const struct mf_field *
472 mf_from_name(const char *name)
474 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
476 if (shash_is_empty(&mf_by_name)) {
477 const struct mf_field *mf;
479 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
480 shash_add_once(&mf_by_name, mf->name, mf);
481 if (mf->extra_name) {
482 shash_add_once(&mf_by_name, mf->extra_name, mf);
487 return shash_find_data(&mf_by_name, name);
491 add_nxm_field(uint32_t header, const struct mf_field *mf)
495 f = xmalloc(sizeof *f);
496 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
502 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
505 assert(!mf_from_nxm_header__(header));
506 add_nxm_field(header, mf);
507 if (mf->maskable != MFM_NONE) {
508 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
516 const struct mf_field *mf;
518 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
519 nxm_init_add_field(mf, mf->nxm_header);
520 if (mf->oxm_header != mf->nxm_header) {
521 nxm_init_add_field(mf, mf->oxm_header);
526 const struct mf_field *
527 mf_from_nxm_header(uint32_t header)
529 if (hmap_is_empty(&all_fields)) {
532 return mf_from_nxm_header__(header);
535 const struct mf_field *
536 mf_from_nxm_header__(uint32_t header)
538 const struct nxm_field *f;
540 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
541 if (f->header == header) {
549 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
550 * specifies at least one bit in the field.
552 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
553 * meets 'mf''s prerequisites. */
555 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
570 assert(mf->fww_bit != 0);
571 return (wc->wildcards & mf->fww_bit) != 0;
574 return !wc->tun_id_mask;
576 return !wc->metadata_mask;
579 return !wc->reg_masks[mf->id - MFF_REG0];
582 return eth_addr_is_zero(wc->dl_src_mask);
584 return eth_addr_is_zero(wc->dl_dst_mask);
587 return !wc->vlan_tci_mask;
589 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
591 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
594 return !wc->nw_src_mask;
596 return !wc->nw_dst_mask;
599 return ipv6_mask_is_any(&wc->ipv6_src_mask);
601 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
604 return ipv6_mask_is_any(&wc->nd_target_mask);
607 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
610 return !wc->nw_src_mask;
612 return !wc->nw_dst_mask;
616 case MFF_ICMPV4_TYPE:
617 case MFF_ICMPV6_TYPE:
618 return !wc->tp_src_mask;
621 case MFF_ICMPV4_CODE:
622 case MFF_ICMPV6_CODE:
623 return !wc->tp_dst_mask;
631 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
632 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
633 * purposes, or to 0 if it is wildcarded.
635 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
636 * meets 'mf''s prerequisites. */
638 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
639 union mf_value *mask)
654 assert(mf->fww_bit != 0);
655 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
659 mask->be64 = wc->tun_id_mask;
662 mask->be64 = wc->metadata_mask;
666 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
670 memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
674 memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN);
678 mask->be16 = wc->vlan_tci_mask;
681 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
684 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
688 mask->be32 = wc->nw_src_mask;
691 mask->be32 = wc->nw_dst_mask;
695 mask->ipv6 = wc->ipv6_src_mask;
698 mask->ipv6 = wc->ipv6_dst_mask;
702 mask->ipv6 = wc->nd_target_mask;
706 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
710 mask->be32 = wc->nw_src_mask;
713 mask->be32 = wc->nw_dst_mask;
718 mask->be16 = wc->tp_src_mask;
722 mask->be16 = wc->tp_dst_mask;
725 case MFF_ICMPV4_TYPE:
726 case MFF_ICMPV6_TYPE:
727 mask->u8 = ntohs(wc->tp_src_mask);
729 case MFF_ICMPV4_CODE:
730 case MFF_ICMPV6_CODE:
731 mask->u8 = ntohs(wc->tp_dst_mask);
740 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
741 * if the mask is valid, false otherwise. */
743 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
745 switch (mf->maskable) {
747 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
748 is_all_ones((const uint8_t *) mask, mf->n_bytes));
758 is_ip_any(const struct flow *flow)
760 return (flow->dl_type == htons(ETH_TYPE_IP) ||
761 flow->dl_type == htons(ETH_TYPE_IPV6));
765 is_icmpv4(const struct flow *flow)
767 return (flow->dl_type == htons(ETH_TYPE_IP)
768 && flow->nw_proto == IPPROTO_ICMP);
772 is_icmpv6(const struct flow *flow)
774 return (flow->dl_type == htons(ETH_TYPE_IPV6)
775 && flow->nw_proto == IPPROTO_ICMPV6);
778 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
780 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
782 switch (mf->prereqs) {
787 return flow->dl_type == htons(ETH_TYPE_ARP);
789 return flow->dl_type == htons(ETH_TYPE_IP);
791 return flow->dl_type == htons(ETH_TYPE_IPV6);
793 return is_ip_any(flow);
796 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
798 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
800 return is_icmpv4(flow);
802 return is_icmpv6(flow);
805 return (is_icmpv6(flow)
806 && flow->tp_dst == htons(0)
807 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
808 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
810 return (is_icmpv6(flow)
811 && flow->tp_dst == htons(0)
812 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
814 return (is_icmpv6(flow)
815 && flow->tp_dst == htons(0)
816 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
822 /* Returns true if 'value' may be a valid value *as part of a masked match*,
825 * A value is not rejected just because it is not valid for the field in
826 * question, but only if it doesn't make sense to test the bits in question at
827 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
828 * without the VLAN_CFI bit being set, but we can't reject those values because
829 * it is still legitimate to test just for those bits (see the documentation
830 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
831 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
833 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
858 case MFF_ICMPV4_TYPE:
859 case MFF_ICMPV4_CODE:
860 case MFF_ICMPV6_TYPE:
861 case MFF_ICMPV6_CODE:
868 return !(value->u8 & ~IP_DSCP_MASK);
870 return !(value->u8 & ~IP_ECN_MASK);
872 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
875 return !(value->be16 & htons(0xff00));
878 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
881 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
884 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
892 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
893 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
895 mf_get_value(const struct mf_field *mf, const struct flow *flow,
896 union mf_value *value)
900 value->be64 = flow->tun_id;
903 value->be64 = flow->metadata;
907 value->be16 = htons(flow->in_port);
911 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
915 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
919 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
923 value->be16 = flow->dl_type;
927 value->be16 = flow->vlan_tci;
931 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
935 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
939 value->be32 = flow->nw_src;
943 value->be32 = flow->nw_dst;
947 value->ipv6 = flow->ipv6_src;
951 value->ipv6 = flow->ipv6_dst;
955 value->be32 = flow->ipv6_label;
959 value->u8 = flow->nw_proto;
963 value->u8 = flow->nw_tos & IP_DSCP_MASK;
967 value->u8 = flow->nw_tos & IP_ECN_MASK;
971 value->u8 = flow->nw_ttl;
975 value->u8 = flow->nw_frag;
979 value->be16 = htons(flow->nw_proto);
983 value->be32 = flow->nw_src;
987 value->be32 = flow->nw_dst;
992 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
997 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1002 value->be16 = flow->tp_src;
1007 value->be16 = flow->tp_dst;
1010 case MFF_ICMPV4_TYPE:
1011 case MFF_ICMPV6_TYPE:
1012 value->u8 = ntohs(flow->tp_src);
1015 case MFF_ICMPV4_CODE:
1016 case MFF_ICMPV6_CODE:
1017 value->u8 = ntohs(flow->tp_dst);
1021 value->ipv6 = flow->nd_target;
1030 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1031 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1034 mf_set_value(const struct mf_field *mf,
1035 const union mf_value *value, struct cls_rule *rule)
1039 cls_rule_set_tun_id(rule, value->be64);
1042 cls_rule_set_metadata(rule, value->be64);
1046 cls_rule_set_in_port(rule, ntohs(value->be16));
1050 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1054 cls_rule_set_dl_src(rule, value->mac);
1058 cls_rule_set_dl_dst(rule, value->mac);
1062 cls_rule_set_dl_type(rule, value->be16);
1066 cls_rule_set_dl_tci(rule, value->be16);
1070 cls_rule_set_dl_vlan(rule, value->be16);
1074 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1078 cls_rule_set_nw_src(rule, value->be32);
1082 cls_rule_set_nw_dst(rule, value->be32);
1086 cls_rule_set_ipv6_src(rule, &value->ipv6);
1090 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1093 case MFF_IPV6_LABEL:
1094 cls_rule_set_ipv6_label(rule, value->be32);
1098 cls_rule_set_nw_proto(rule, value->u8);
1102 cls_rule_set_nw_dscp(rule, value->u8);
1106 cls_rule_set_nw_ecn(rule, value->u8);
1110 cls_rule_set_nw_ttl(rule, value->u8);
1114 cls_rule_set_nw_frag(rule, value->u8);
1118 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1122 cls_rule_set_nw_src(rule, value->be32);
1126 cls_rule_set_nw_dst(rule, value->be32);
1131 cls_rule_set_arp_sha(rule, value->mac);
1136 cls_rule_set_arp_tha(rule, value->mac);
1141 cls_rule_set_tp_src(rule, value->be16);
1146 cls_rule_set_tp_dst(rule, value->be16);
1149 case MFF_ICMPV4_TYPE:
1150 case MFF_ICMPV6_TYPE:
1151 cls_rule_set_icmp_type(rule, value->u8);
1154 case MFF_ICMPV4_CODE:
1155 case MFF_ICMPV6_CODE:
1156 cls_rule_set_icmp_code(rule, value->u8);
1160 cls_rule_set_nd_target(rule, &value->ipv6);
1169 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1170 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1173 mf_set_flow_value(const struct mf_field *mf,
1174 const union mf_value *value, struct flow *flow)
1178 flow->tun_id = value->be64;
1181 flow->metadata = value->be64;
1185 flow->in_port = ntohs(value->be16);
1189 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1193 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1197 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1201 flow->dl_type = value->be16;
1205 flow->vlan_tci = value->be16;
1209 flow_set_vlan_vid(flow, value->be16);
1213 flow_set_vlan_pcp(flow, value->u8);
1217 flow->nw_src = value->be32;
1221 flow->nw_dst = value->be32;
1225 flow->ipv6_src = value->ipv6;
1229 flow->ipv6_dst = value->ipv6;
1232 case MFF_IPV6_LABEL:
1233 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1237 flow->nw_proto = value->u8;
1241 flow->nw_tos &= ~IP_DSCP_MASK;
1242 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1246 flow->nw_tos &= ~IP_ECN_MASK;
1247 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1251 flow->nw_ttl = value->u8;
1255 flow->nw_frag &= value->u8;
1259 flow->nw_proto = ntohs(value->be16);
1263 flow->nw_src = value->be32;
1267 flow->nw_dst = value->be32;
1272 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1277 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1282 flow->tp_src = value->be16;
1287 flow->tp_dst = value->be16;
1290 case MFF_ICMPV4_TYPE:
1291 case MFF_ICMPV6_TYPE:
1292 flow->tp_src = htons(value->u8);
1295 case MFF_ICMPV4_CODE:
1296 case MFF_ICMPV6_CODE:
1297 flow->tp_dst = htons(value->u8);
1301 flow->nd_target = value->ipv6;
1310 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1312 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1315 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1317 union mf_value value;
1319 mf_get_value(mf, flow, &value);
1320 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1323 /* Makes 'rule' wildcard field 'mf'.
1325 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1328 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1332 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1335 cls_rule_set_metadata_masked(rule, htonll(0), htonll(0));
1338 rule->wc.wildcards |= FWW_IN_PORT;
1339 rule->flow.in_port = 0;
1343 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0);
1347 memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1348 memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1352 memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1353 memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1357 rule->wc.wildcards |= FWW_DL_TYPE;
1358 rule->flow.dl_type = htons(0);
1362 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1366 cls_rule_set_any_vid(rule);
1370 cls_rule_set_any_pcp(rule);
1375 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1380 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1384 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1385 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1389 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1390 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1393 case MFF_IPV6_LABEL:
1394 rule->wc.wildcards |= FWW_IPV6_LABEL;
1395 rule->flow.ipv6_label = 0;
1399 rule->wc.wildcards |= FWW_NW_PROTO;
1400 rule->flow.nw_proto = 0;
1404 rule->wc.wildcards |= FWW_NW_DSCP;
1405 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1409 rule->wc.wildcards |= FWW_NW_ECN;
1410 rule->flow.nw_tos &= ~IP_ECN_MASK;
1414 rule->wc.wildcards |= FWW_NW_TTL;
1415 rule->flow.nw_ttl = 0;
1419 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1420 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1424 rule->wc.wildcards |= FWW_NW_PROTO;
1425 rule->flow.nw_proto = 0;
1430 rule->wc.wildcards |= FWW_ARP_SHA;
1431 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1436 rule->wc.wildcards |= FWW_ARP_THA;
1437 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1442 case MFF_ICMPV4_TYPE:
1443 case MFF_ICMPV6_TYPE:
1444 rule->wc.tp_src_mask = htons(0);
1445 rule->flow.tp_src = htons(0);
1450 case MFF_ICMPV4_CODE:
1451 case MFF_ICMPV6_CODE:
1452 rule->wc.tp_dst_mask = htons(0);
1453 rule->flow.tp_dst = htons(0);
1457 memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1458 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1467 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1468 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1469 * with a 1-bit indicating that the corresponding value bit must match and a
1470 * 0-bit indicating a don't-care.
1472 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1473 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1474 * call is equivalent to mf_set_wild(mf, rule).
1476 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1477 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1479 mf_set(const struct mf_field *mf,
1480 const union mf_value *value, const union mf_value *mask,
1481 struct cls_rule *rule)
1483 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1484 mf_set_value(mf, value, rule);
1486 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1487 mf_set_wild(mf, rule);
1496 case MFF_IPV6_LABEL:
1504 case MFF_ICMPV4_TYPE:
1505 case MFF_ICMPV4_CODE:
1506 case MFF_ICMPV6_TYPE:
1507 case MFF_ICMPV6_CODE:
1513 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1516 cls_rule_set_metadata_masked(rule, value->be64, mask->be64);
1520 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1521 ntohl(value->be32), ntohl(mask->be32));
1525 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1529 cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1533 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1537 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1541 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1545 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1549 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1553 cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1557 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1561 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1565 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1570 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1575 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1585 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1589 VLOG_WARN_RL(&rl, "unknown %s field", type);
1590 } else if (!sf->n_bits) {
1591 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1592 } else if (sf->ofs >= sf->field->n_bits) {
1593 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1594 sf->ofs, sf->field->n_bits, type, sf->field->name);
1595 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1596 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1597 "of %s field %s", sf->ofs, sf->n_bits,
1598 sf->field->n_bits, type, sf->field->name);
1599 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1600 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1601 type, sf->field->name);
1606 return OFPERR_OFPBAC_BAD_ARGUMENT;
1609 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1610 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1613 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1615 return mf_check__(sf, flow, "source");
1618 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1619 * if so, otherwise an OpenFlow error code (e.g. as returned by
1622 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1624 int error = mf_check__(sf, flow, "destination");
1625 if (!error && !sf->field->writable) {
1626 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1628 return OFPERR_OFPBAC_BAD_ARGUMENT;
1633 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1634 * 'value' and 'mask', respectively. */
1636 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1637 union mf_value *value, union mf_value *mask)
1639 mf_get_value(mf, &rule->flow, value);
1640 mf_get_mask(mf, &rule->wc, mask);
1643 /* Assigns a random value for field 'mf' to 'value'. */
1645 mf_random_value(const struct mf_field *mf, union mf_value *value)
1647 random_bytes(value, mf->n_bytes);
1672 case MFF_ICMPV4_TYPE:
1673 case MFF_ICMPV4_CODE:
1674 case MFF_ICMPV6_TYPE:
1675 case MFF_ICMPV6_CODE:
1681 case MFF_IPV6_LABEL:
1682 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1686 value->u8 &= IP_DSCP_MASK;
1690 value->u8 &= IP_ECN_MASK;
1694 value->u8 &= FLOW_NW_FRAG_MASK;
1698 value->be16 &= htons(0xff);
1702 value->be16 &= htons(VLAN_VID_MASK);
1716 mf_from_integer_string(const struct mf_field *mf, const char *s,
1717 uint8_t *valuep, uint8_t *maskp)
1719 unsigned long long int integer, mask;
1724 integer = strtoull(s, &tail, 0);
1725 if (errno || (*tail != '\0' && *tail != '/')) {
1730 mask = strtoull(tail + 1, &tail, 0);
1731 if (errno || *tail != '\0') {
1738 for (i = mf->n_bytes - 1; i >= 0; i--) {
1739 valuep[i] = integer;
1745 return xasprintf("%s: value too large for %u-byte field %s",
1746 s, mf->n_bytes, mf->name);
1751 return xasprintf("%s: bad syntax for %s", s, mf->name);
1755 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1756 uint8_t mac[ETH_ADDR_LEN],
1757 uint8_t mask[ETH_ADDR_LEN])
1759 assert(mf->n_bytes == ETH_ADDR_LEN);
1761 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1762 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1763 case ETH_ADDR_SCAN_COUNT * 2:
1766 case ETH_ADDR_SCAN_COUNT:
1767 memset(mask, 0xff, ETH_ADDR_LEN);
1771 return xasprintf("%s: invalid Ethernet address", s);
1776 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1777 ovs_be32 *ip, ovs_be32 *mask)
1781 assert(mf->n_bytes == sizeof *ip);
1783 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1784 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1786 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1787 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1788 if (prefix <= 0 || prefix > 32) {
1789 return xasprintf("%s: network prefix bits not between 1 and "
1791 } else if (prefix == 32) {
1792 *mask = htonl(UINT32_MAX);
1794 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1796 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1797 *mask = htonl(UINT32_MAX);
1799 return xasprintf("%s: invalid IP address", s);
1805 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1806 struct in6_addr *value, struct in6_addr *mask)
1808 char *str = xstrdup(s);
1809 char *save_ptr = NULL;
1810 const char *name, *netmask;
1813 assert(mf->n_bytes == sizeof *value);
1815 name = strtok_r(str, "/", &save_ptr);
1816 retval = name ? lookup_ipv6(name, value) : EINVAL;
1820 err = xasprintf("%s: could not convert to IPv6 address", str);
1826 netmask = strtok_r(NULL, "/", &save_ptr);
1828 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1829 int prefix = atoi(netmask);
1830 if (prefix <= 0 || prefix > 128) {
1832 return xasprintf("%s: prefix bits not between 1 and 128", s);
1834 *mask = ipv6_create_mask(prefix);
1838 *mask = in6addr_exact;
1846 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1847 ovs_be16 *valuep, ovs_be16 *maskp)
1851 assert(mf->n_bytes == sizeof(ovs_be16));
1852 if (ofputil_port_from_string(s, &port)) {
1853 *valuep = htons(port);
1854 *maskp = htons(UINT16_MAX);
1857 return mf_from_integer_string(mf, s,
1858 (uint8_t *) valuep, (uint8_t *) maskp);
1862 struct frag_handling {
1868 static const struct frag_handling all_frags[] = {
1869 #define A FLOW_NW_FRAG_ANY
1870 #define L FLOW_NW_FRAG_LATER
1871 /* name mask value */
1874 { "first", A|L, A },
1875 { "later", A|L, A|L },
1880 { "not_later", L, 0 },
1887 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1889 const struct frag_handling *h;
1891 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1892 if (!strcasecmp(s, h->name)) {
1893 /* We force the upper bits of the mask on to make mf_parse_value()
1894 * happy (otherwise it will never think it's an exact match.) */
1895 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1901 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1902 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1905 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1906 * NULL if successful, otherwise a malloc()'d string describing the error. */
1908 mf_parse(const struct mf_field *mf, const char *s,
1909 union mf_value *value, union mf_value *mask)
1911 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1912 memset(value, 0, mf->n_bytes);
1913 memset(mask, 0, mf->n_bytes);
1917 switch (mf->string) {
1919 case MFS_HEXADECIMAL:
1920 return mf_from_integer_string(mf, s,
1921 (uint8_t *) value, (uint8_t *) mask);
1924 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1927 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1930 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1933 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1936 return mf_from_frag_string(s, &value->u8, &mask->u8);
1941 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1942 * successful, otherwise a malloc()'d string describing the error. */
1944 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1946 union mf_value mask;
1949 error = mf_parse(mf, s, value, &mask);
1954 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1955 return xasprintf("%s: wildcards not allowed here", s);
1961 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1962 const uint8_t *maskp, struct ds *s)
1964 unsigned long long int integer;
1967 assert(mf->n_bytes <= 8);
1970 for (i = 0; i < mf->n_bytes; i++) {
1971 integer = (integer << 8) | valuep[i];
1973 if (mf->string == MFS_HEXADECIMAL) {
1974 ds_put_format(s, "%#llx", integer);
1976 ds_put_format(s, "%lld", integer);
1980 unsigned long long int mask;
1983 for (i = 0; i < mf->n_bytes; i++) {
1984 mask = (mask << 8) | maskp[i];
1987 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1988 * not sure that that a bit-mask written in decimal is ever easier to
1989 * understand than the same bit-mask written in hexadecimal. */
1990 ds_put_format(s, "/%#llx", mask);
1995 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1998 const struct frag_handling *h;
1999 uint8_t value = *valuep;
2000 uint8_t mask = *maskp;
2003 mask &= FLOW_NW_FRAG_MASK;
2005 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2006 if (value == h->value && mask == h->mask) {
2007 ds_put_cstr(s, h->name);
2011 ds_put_cstr(s, "<error>");
2014 /* Appends to 's' a string representation of field 'mf' whose value is in
2015 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2017 mf_format(const struct mf_field *mf,
2018 const union mf_value *value, const union mf_value *mask,
2022 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2023 ds_put_cstr(s, "ANY");
2025 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2030 switch (mf->string) {
2033 ofputil_format_port(ntohs(value->be16), s);
2038 case MFS_HEXADECIMAL:
2039 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2043 eth_format_masked(value->mac, mask->mac, s);
2047 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2052 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2056 mf_format_frag_string(&value->u8, &mask->u8, s);
2064 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2065 * least-significant bits in 'x'.
2067 * See mf_set_subfield() for an example.
2069 * The difference between this function and mf_set_subfield() is that the
2070 * latter function can only handle subfields up to 64 bits wide, whereas this
2071 * one handles the general case. On the other hand, mf_set_subfield() is
2072 * arguably easier to use. */
2074 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2075 struct cls_rule *rule)
2077 const struct mf_field *field = sf->field;
2078 union mf_value value, mask;
2080 mf_get(field, rule, &value, &mask);
2081 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2082 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2083 mf_set(field, &value, &mask, rule);
2086 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2087 * least-significant bits of 'x'.
2089 * Example: suppose that 'sf->field' is originally the following 2-byte field
2092 * value == 0xe00a == 2#1110000000001010
2093 * mask == 0xfc3f == 2#1111110000111111
2095 * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2096 * sf->n_bits == 7 would have the following effect (note that 0x55 is
2099 * value == 0xd50a == 2#1101010100001010
2100 * mask == 0xff3f == 2#1111111100111111
2101 * ^^^^^^^ affected bits
2103 * The caller is responsible for ensuring that the result will be a valid
2104 * wildcard pattern for 'sf->field'. The caller is responsible for ensuring
2105 * that 'rule' meets 'sf->field''s prerequisites. */
2107 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2108 struct cls_rule *rule)
2110 const struct mf_field *field = sf->field;
2111 unsigned int n_bits = sf->n_bits;
2112 unsigned int ofs = sf->ofs;
2114 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2115 union mf_value value;
2118 for (i = field->n_bytes - 1; i >= 0; i--) {
2119 ((uint8_t *) &value)[i] = x;
2122 mf_set_value(field, &value, rule);
2124 union mf_value value, mask;
2125 uint8_t *vp = (uint8_t *) &value;
2126 uint8_t *mp = (uint8_t *) &mask;
2128 mf_get(field, rule, &value, &mask);
2129 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2130 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2131 mf_set(field, &value, &mask, rule);
2135 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2137 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2140 const struct mf_field *field = sf->field;
2141 unsigned int n_bits = sf->n_bits;
2142 unsigned int ofs = sf->ofs;
2143 union mf_value value;
2145 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2148 for (i = field->n_bytes - 1; i >= 0; i--) {
2149 ((uint8_t *) &value)[i] = x;
2152 mf_set_flow_value(field, &value, flow);
2154 mf_get_value(field, flow, &value);
2155 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2156 mf_set_flow_value(field, &value, flow);
2160 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2161 * reading 'flow', e.g. as checked by mf_check_src(). */
2163 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2164 union mf_subvalue *x)
2166 union mf_value value;
2168 mf_get_value(sf->field, flow, &value);
2170 memset(x, 0, sizeof *x);
2171 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2176 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2177 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2180 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2182 union mf_value value;
2184 mf_get_value(sf->field, flow, &value);
2185 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2188 /* Formats 'sf' into 's' in a format normally acceptable to
2189 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2190 * sf->field has no NXM name.) */
2192 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2195 ds_put_cstr(s, "<unknown>");
2196 } else if (sf->field->nxm_name) {
2197 ds_put_cstr(s, sf->field->nxm_name);
2198 } else if (sf->field->nxm_header) {
2199 uint32_t header = sf->field->nxm_header;
2200 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2202 ds_put_cstr(s, sf->field->name);
2205 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2206 ds_put_cstr(s, "[]");
2207 } else if (sf->n_bits == 1) {
2208 ds_put_format(s, "[%d]", sf->ofs);
2210 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2214 static const struct mf_field *
2215 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2219 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2224 for (i = 0; i < MFF_N_IDS; i++) {
2225 const struct mf_field *mf = mf_from_id(i);
2228 && !strncmp(mf->nxm_name, name, name_len)
2229 && mf->nxm_name[name_len] == '\0') {
2233 && !strncmp(mf->oxm_name, name, name_len)
2234 && mf->oxm_name[name_len] == '\0') {
2242 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2243 * returns NULL and advances '*sp' to the first byte following the parsed
2244 * string. On failure, returns a malloc()'d error message, does not modify
2245 * '*sp', and does not properly initialize 'sf'.
2247 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2248 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2249 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2250 * may both be omitted (the [] are still required) to indicate an entire
2253 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2255 const struct mf_field *field;
2264 name_len = strcspn(s, "[");
2265 if (s[name_len] != '[') {
2266 return xasprintf("%s: missing [ looking for field name", *sp);
2269 field = mf_parse_subfield_name(name, name_len, &wild);
2271 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2275 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2276 /* Nothing to do. */
2277 } else if (sscanf(s, "[%d]", &start) == 1) {
2279 } else if (!strncmp(s, "[]", 2)) {
2281 end = field->n_bits - 1;
2283 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2284 "[<start>..<end>]", *sp);
2286 s = strchr(s, ']') + 1;
2289 return xasprintf("%s: starting bit %d is after ending bit %d",
2291 } else if (start >= field->n_bits) {
2292 return xasprintf("%s: starting bit %d is not valid because field is "
2293 "only %d bits wide", *sp, start, field->n_bits);
2294 } else if (end >= field->n_bits){
2295 return xasprintf("%s: ending bit %d is not valid because field is "
2296 "only %d bits wide", *sp, end, field->n_bits);
2301 sf->n_bits = end - start + 1;
2307 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2308 * byte in 's' following the parsed string.
2310 * Exits with an error message if 's' has incorrect syntax.
2312 * The syntax parsed from 's' takes the form "header[start..end]" where
2313 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2314 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2315 * may both be omitted (the [] are still required) to indicate an entire
2318 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2320 char *msg = mf_parse_subfield__(sf, &s);
2322 ovs_fatal(0, "%s", msg);