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",
59 MFF_IN_PORT, "in_port", NULL,
61 MFM_NONE, FWW_IN_PORT,
65 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
66 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
69 #define REGISTER(IDX) \
71 MFF_REG##IDX, "reg" #IDX, NULL, \
72 MF_FIELD_SIZES(be32), \
114 MFF_ETH_SRC, "eth_src", "dl_src",
120 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
121 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
123 MFF_ETH_DST, "eth_dst", "dl_dst",
129 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
130 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
132 MFF_ETH_TYPE, "eth_type", "dl_type",
133 MF_FIELD_SIZES(be16),
134 MFM_NONE, FWW_DL_TYPE,
138 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
139 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
143 MFF_VLAN_TCI, "vlan_tci", NULL,
144 MF_FIELD_SIZES(be16),
149 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
152 MFF_VLAN_VID, "dl_vlan", NULL,
153 sizeof(ovs_be16), 12,
159 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
161 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
168 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
176 MFF_IPV4_SRC, "ip_src", "nw_src",
177 MF_FIELD_SIZES(be32),
182 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
183 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
185 MFF_IPV4_DST, "ip_dst", "nw_dst",
186 MF_FIELD_SIZES(be32),
191 NXM_OF_IP_DST, "NXM_OF_IP_DST",
192 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
196 MFF_IPV6_SRC, "ipv6_src", NULL,
197 MF_FIELD_SIZES(ipv6),
202 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
203 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
205 MFF_IPV6_DST, "ipv6_dst", NULL,
206 MF_FIELD_SIZES(ipv6),
211 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
212 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
215 MFF_IPV6_LABEL, "ipv6_label", NULL,
217 MFM_NONE, FWW_IPV6_LABEL,
221 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
222 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
226 MFF_IP_PROTO, "nw_proto", NULL,
228 MFM_NONE, FWW_NW_PROTO,
232 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
233 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
235 MFF_IP_DSCP, "nw_tos", NULL,
237 MFM_NONE, FWW_NW_DSCP,
241 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
242 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
244 MFF_IP_ECN, "nw_ecn", NULL,
246 MFM_NONE, FWW_NW_ECN,
250 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
251 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
253 MFF_IP_TTL, "nw_ttl", NULL,
255 MFM_NONE, FWW_NW_TTL,
259 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
262 MFF_IP_FRAG, "ip_frag", NULL,
268 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
273 MFF_ARP_OP, "arp_op", NULL,
274 MF_FIELD_SIZES(be16),
275 MFM_NONE, FWW_NW_PROTO,
279 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
280 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
282 MFF_ARP_SPA, "arp_spa", NULL,
283 MF_FIELD_SIZES(be32),
288 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
289 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
291 MFF_ARP_TPA, "arp_tpa", NULL,
292 MF_FIELD_SIZES(be32),
297 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
298 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
300 MFF_ARP_SHA, "arp_sha", NULL,
302 MFM_NONE, FWW_ARP_SHA,
306 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
307 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
309 MFF_ARP_THA, "arp_tha", NULL,
311 MFM_NONE, FWW_ARP_THA,
315 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
316 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
324 MFF_TCP_SRC, "tcp_src", "tp_src",
325 MF_FIELD_SIZES(be16),
330 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
331 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
333 MFF_TCP_DST, "tcp_dst", "tp_dst",
334 MF_FIELD_SIZES(be16),
339 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
340 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
344 MFF_UDP_SRC, "udp_src", NULL,
345 MF_FIELD_SIZES(be16),
350 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
351 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
353 MFF_UDP_DST, "udp_dst", NULL,
354 MF_FIELD_SIZES(be16),
359 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
360 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
364 MFF_ICMPV4_TYPE, "icmp_type", NULL,
370 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
371 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
373 MFF_ICMPV4_CODE, "icmp_code", NULL,
379 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
380 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
384 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
390 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
391 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
393 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
399 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
400 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
408 MFF_ND_TARGET, "nd_target", NULL,
409 MF_FIELD_SIZES(ipv6),
414 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
415 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
417 MFF_ND_SLL, "nd_sll", NULL,
419 MFM_NONE, FWW_ARP_SHA,
423 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
424 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
426 MFF_ND_TLL, "nd_tll", NULL,
428 MFM_NONE, FWW_ARP_THA,
432 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
433 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
438 struct hmap_node hmap_node;
440 const struct mf_field *mf;
443 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
444 static struct hmap all_oxm_fields = HMAP_INITIALIZER(&all_oxm_fields);
446 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
447 * controller and so there's not much point in showing a lot of them. */
448 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
450 /* Returns the field with the given 'id'. */
451 const struct mf_field *
452 mf_from_id(enum mf_field_id id)
454 assert((unsigned int) id < MFF_N_IDS);
455 return &mf_fields[id];
458 /* Returns the field with the given 'name', or a null pointer if no field has
460 const struct mf_field *
461 mf_from_name(const char *name)
463 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
465 if (shash_is_empty(&mf_by_name)) {
466 const struct mf_field *mf;
468 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
469 shash_add_once(&mf_by_name, mf->name, mf);
470 if (mf->extra_name) {
471 shash_add_once(&mf_by_name, mf->extra_name, mf);
476 return shash_find_data(&mf_by_name, name);
480 add_nxm_field(struct hmap *all_fields, uint32_t nxm_header,
481 const struct mf_field *mf)
485 f = xmalloc(sizeof *f);
486 hmap_insert(all_fields, &f->hmap_node, hash_int(nxm_header, 0));
487 f->nxm_header = nxm_header;
492 get_all_fields(uint32_t header)
494 return IS_OXM_HEADER(header) ? &all_oxm_fields : &all_nxm_fields;
498 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
500 struct hmap *all_fields = get_all_fields(header);
505 add_nxm_field(all_fields, header, mf);
506 if (mf->maskable == MFM_NONE) {
509 add_nxm_field(all_fields, NXM_MAKE_WILD_HEADER(header), mf);
514 nxm_init_verify_field(const struct mf_field *mf, uint32_t header)
519 assert(mf_from_nxm_header(header) == mf);
520 /* Some OXM fields are not maskable while their NXM
521 * counterparts are, just skip this check for now */
522 if (mf->maskable == MFM_NONE || IS_OXM_HEADER(header)) {
525 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header)) == mf);
532 const struct mf_field *mf;
534 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
535 nxm_init_add_field(mf, mf->nxm_header);
536 nxm_init_add_field(mf, mf->oxm_header);
540 /* Verify that the header values are unique. */
541 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
542 nxm_init_verify_field(mf, mf->nxm_header);
543 nxm_init_verify_field(mf, mf->oxm_header);
548 const struct mf_field *
549 mf_from_nxm_header(uint32_t header)
551 const struct nxm_field *f;
552 struct hmap *all_fields = get_all_fields(header);
554 if (hmap_is_empty(all_fields)) {
558 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), all_fields) {
559 if (f->nxm_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)
588 assert(mf->fww_bit != 0);
589 return (wc->wildcards & mf->fww_bit) != 0;
592 return !wc->tun_id_mask;
621 return !wc->reg_masks[mf->id - MFF_REG0];
624 return eth_addr_is_zero(wc->dl_src_mask);
626 return eth_addr_is_zero(wc->dl_dst_mask);
629 return !wc->vlan_tci_mask;
631 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
633 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
636 return !wc->nw_src_mask;
638 return !wc->nw_dst_mask;
641 return ipv6_mask_is_any(&wc->ipv6_src_mask);
643 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
646 return ipv6_mask_is_any(&wc->nd_target_mask);
649 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
652 return !wc->nw_src_mask;
654 return !wc->nw_dst_mask;
658 case MFF_ICMPV4_TYPE:
659 case MFF_ICMPV6_TYPE:
660 return !wc->tp_src_mask;
663 case MFF_ICMPV4_CODE:
664 case MFF_ICMPV6_CODE:
665 return !wc->tp_dst_mask;
673 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
674 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
675 * purposes, or to 0 if it is wildcarded.
677 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
678 * meets 'mf''s prerequisites. */
680 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
681 union mf_value *mask)
696 assert(mf->fww_bit != 0);
697 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
701 mask->be64 = wc->tun_id_mask;
731 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
735 memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
739 memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN);
743 mask->be16 = wc->vlan_tci_mask;
746 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
749 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
753 mask->be32 = wc->nw_src_mask;
756 mask->be32 = wc->nw_dst_mask;
760 mask->ipv6 = wc->ipv6_src_mask;
763 mask->ipv6 = wc->ipv6_dst_mask;
767 mask->ipv6 = wc->nd_target_mask;
771 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
775 mask->be32 = wc->nw_src_mask;
778 mask->be32 = wc->nw_dst_mask;
783 mask->be16 = wc->tp_src_mask;
787 mask->be16 = wc->tp_dst_mask;
790 case MFF_ICMPV4_TYPE:
791 case MFF_ICMPV6_TYPE:
792 mask->u8 = ntohs(wc->tp_src_mask);
794 case MFF_ICMPV4_CODE:
795 case MFF_ICMPV6_CODE:
796 mask->u8 = ntohs(wc->tp_dst_mask);
805 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
806 * if the mask is valid, false otherwise. */
808 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
810 switch (mf->maskable) {
812 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
813 is_all_ones((const uint8_t *) mask, mf->n_bytes));
823 is_ip_any(const struct flow *flow)
825 return (flow->dl_type == htons(ETH_TYPE_IP) ||
826 flow->dl_type == htons(ETH_TYPE_IPV6));
830 is_icmpv4(const struct flow *flow)
832 return (flow->dl_type == htons(ETH_TYPE_IP)
833 && flow->nw_proto == IPPROTO_ICMP);
837 is_icmpv6(const struct flow *flow)
839 return (flow->dl_type == htons(ETH_TYPE_IPV6)
840 && flow->nw_proto == IPPROTO_ICMPV6);
843 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
845 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
847 switch (mf->prereqs) {
852 return flow->dl_type == htons(ETH_TYPE_ARP);
854 return flow->dl_type == htons(ETH_TYPE_IP);
856 return flow->dl_type == htons(ETH_TYPE_IPV6);
858 return is_ip_any(flow);
861 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
863 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
865 return is_icmpv4(flow);
867 return is_icmpv6(flow);
870 return (is_icmpv6(flow)
871 && flow->tp_dst == htons(0)
872 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
873 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
875 return (is_icmpv6(flow)
876 && flow->tp_dst == htons(0)
877 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
879 return (is_icmpv6(flow)
880 && flow->tp_dst == htons(0)
881 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
887 /* Returns true if 'value' may be a valid value *as part of a masked match*,
890 * A value is not rejected just because it is not valid for the field in
891 * question, but only if it doesn't make sense to test the bits in question at
892 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
893 * without the VLAN_CFI bit being set, but we can't reject those values because
894 * it is still legitimate to test just for those bits (see the documentation
895 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
896 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
898 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
948 case MFF_ICMPV4_TYPE:
949 case MFF_ICMPV4_CODE:
950 case MFF_ICMPV6_TYPE:
951 case MFF_ICMPV6_CODE:
958 return !(value->u8 & ~IP_DSCP_MASK);
960 return !(value->u8 & ~IP_ECN_MASK);
962 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
965 return !(value->be16 & htons(0xff00));
968 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
971 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
974 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
982 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
983 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
985 mf_get_value(const struct mf_field *mf, const struct flow *flow,
986 union mf_value *value)
990 value->be64 = flow->tun_id;
994 value->be16 = htons(flow->in_port);
1024 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1028 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1032 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1036 value->be16 = flow->dl_type;
1040 value->be16 = flow->vlan_tci;
1044 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1048 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1052 value->be32 = flow->nw_src;
1056 value->be32 = flow->nw_dst;
1060 value->ipv6 = flow->ipv6_src;
1064 value->ipv6 = flow->ipv6_dst;
1067 case MFF_IPV6_LABEL:
1068 value->be32 = flow->ipv6_label;
1072 value->u8 = flow->nw_proto;
1076 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1080 value->u8 = flow->nw_tos & IP_ECN_MASK;
1084 value->u8 = flow->nw_ttl;
1088 value->u8 = flow->nw_frag;
1092 value->be16 = htons(flow->nw_proto);
1096 value->be32 = flow->nw_src;
1100 value->be32 = flow->nw_dst;
1105 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1110 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1114 value->be16 = flow->tp_src;
1118 value->be16 = flow->tp_dst;
1122 value->be16 = flow->tp_src;
1126 value->be16 = flow->tp_dst;
1129 case MFF_ICMPV4_TYPE:
1130 case MFF_ICMPV6_TYPE:
1131 value->u8 = ntohs(flow->tp_src);
1134 case MFF_ICMPV4_CODE:
1135 case MFF_ICMPV6_CODE:
1136 value->u8 = ntohs(flow->tp_dst);
1140 value->ipv6 = flow->nd_target;
1149 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1150 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1153 mf_set_value(const struct mf_field *mf,
1154 const union mf_value *value, struct cls_rule *rule)
1158 cls_rule_set_tun_id(rule, value->be64);
1162 cls_rule_set_in_port(rule, ntohs(value->be16));
1193 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1198 cls_rule_set_dl_src(rule, value->mac);
1202 cls_rule_set_dl_dst(rule, value->mac);
1206 cls_rule_set_dl_type(rule, value->be16);
1210 cls_rule_set_dl_tci(rule, value->be16);
1214 cls_rule_set_dl_vlan(rule, value->be16);
1218 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1222 cls_rule_set_nw_src(rule, value->be32);
1226 cls_rule_set_nw_dst(rule, value->be32);
1230 cls_rule_set_ipv6_src(rule, &value->ipv6);
1234 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1237 case MFF_IPV6_LABEL:
1238 cls_rule_set_ipv6_label(rule, value->be32);
1242 cls_rule_set_nw_proto(rule, value->u8);
1246 cls_rule_set_nw_dscp(rule, value->u8);
1250 cls_rule_set_nw_ecn(rule, value->u8);
1254 cls_rule_set_nw_ttl(rule, value->u8);
1258 cls_rule_set_nw_frag(rule, value->u8);
1262 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1266 cls_rule_set_nw_src(rule, value->be32);
1270 cls_rule_set_nw_dst(rule, value->be32);
1275 cls_rule_set_arp_sha(rule, value->mac);
1280 cls_rule_set_arp_tha(rule, value->mac);
1284 cls_rule_set_tp_src(rule, value->be16);
1288 cls_rule_set_tp_dst(rule, value->be16);
1292 cls_rule_set_tp_src(rule, value->be16);
1296 cls_rule_set_tp_dst(rule, value->be16);
1299 case MFF_ICMPV4_TYPE:
1300 case MFF_ICMPV6_TYPE:
1301 cls_rule_set_icmp_type(rule, value->u8);
1304 case MFF_ICMPV4_CODE:
1305 case MFF_ICMPV6_CODE:
1306 cls_rule_set_icmp_code(rule, value->u8);
1310 cls_rule_set_nd_target(rule, &value->ipv6);
1319 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1320 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1323 mf_set_flow_value(const struct mf_field *mf,
1324 const union mf_value *value, struct flow *flow)
1328 flow->tun_id = value->be64;
1332 flow->in_port = ntohs(value->be16);
1363 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1368 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1372 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1376 flow->dl_type = value->be16;
1380 flow->vlan_tci = value->be16;
1384 flow_set_vlan_vid(flow, value->be16);
1388 flow_set_vlan_pcp(flow, value->u8);
1392 flow->nw_src = value->be32;
1396 flow->nw_dst = value->be32;
1400 flow->ipv6_src = value->ipv6;
1404 flow->ipv6_dst = value->ipv6;
1407 case MFF_IPV6_LABEL:
1408 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1412 flow->nw_proto = value->u8;
1416 flow->nw_tos &= ~IP_DSCP_MASK;
1417 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1421 flow->nw_tos &= ~IP_ECN_MASK;
1422 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1426 flow->nw_ttl = value->u8;
1430 flow->nw_frag &= value->u8;
1434 flow->nw_proto = ntohs(value->be16);
1438 flow->nw_src = value->be32;
1442 flow->nw_dst = value->be32;
1447 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1452 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1457 flow->tp_src = value->be16;
1462 flow->tp_dst = value->be16;
1465 case MFF_ICMPV4_TYPE:
1466 case MFF_ICMPV6_TYPE:
1467 flow->tp_src = htons(value->u8);
1470 case MFF_ICMPV4_CODE:
1471 case MFF_ICMPV6_CODE:
1472 flow->tp_dst = htons(value->u8);
1476 flow->nd_target = value->ipv6;
1485 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1487 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1490 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1492 union mf_value value;
1494 mf_get_value(mf, flow, &value);
1495 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1498 /* Makes 'rule' wildcard field 'mf'.
1500 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1503 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1507 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1511 rule->wc.wildcards |= FWW_IN_PORT;
1512 rule->flow.in_port = 0;
1517 cls_rule_set_reg_masked(rule, 0, 0, 0);
1522 cls_rule_set_reg_masked(rule, 1, 0, 0);
1527 cls_rule_set_reg_masked(rule, 2, 0, 0);
1532 cls_rule_set_reg_masked(rule, 3, 0, 0);
1537 cls_rule_set_reg_masked(rule, 4, 0, 0);
1542 cls_rule_set_reg_masked(rule, 5, 0, 0);
1547 cls_rule_set_reg_masked(rule, 6, 0, 0);
1552 cls_rule_set_reg_masked(rule, 7, 0, 0);
1560 memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1561 memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1565 memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1566 memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1570 rule->wc.wildcards |= FWW_DL_TYPE;
1571 rule->flow.dl_type = htons(0);
1575 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1579 cls_rule_set_any_vid(rule);
1583 cls_rule_set_any_pcp(rule);
1588 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1593 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1597 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1598 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1602 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1603 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1606 case MFF_IPV6_LABEL:
1607 rule->wc.wildcards |= FWW_IPV6_LABEL;
1608 rule->flow.ipv6_label = 0;
1612 rule->wc.wildcards |= FWW_NW_PROTO;
1613 rule->flow.nw_proto = 0;
1617 rule->wc.wildcards |= FWW_NW_DSCP;
1618 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1622 rule->wc.wildcards |= FWW_NW_ECN;
1623 rule->flow.nw_tos &= ~IP_ECN_MASK;
1627 rule->wc.wildcards |= FWW_NW_TTL;
1628 rule->flow.nw_ttl = 0;
1632 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1633 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1637 rule->wc.wildcards |= FWW_NW_PROTO;
1638 rule->flow.nw_proto = 0;
1643 rule->wc.wildcards |= FWW_ARP_SHA;
1644 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1649 rule->wc.wildcards |= FWW_ARP_THA;
1650 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1655 case MFF_ICMPV4_TYPE:
1656 case MFF_ICMPV6_TYPE:
1657 rule->wc.tp_src_mask = htons(0);
1658 rule->flow.tp_src = htons(0);
1663 case MFF_ICMPV4_CODE:
1664 case MFF_ICMPV6_CODE:
1665 rule->wc.tp_dst_mask = htons(0);
1666 rule->flow.tp_dst = htons(0);
1670 memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1671 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1680 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1681 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1682 * with a 1-bit indicating that the corresponding value bit must match and a
1683 * 0-bit indicating a don't-care.
1685 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1686 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1687 * call is equivalent to mf_set_wild(mf, rule).
1689 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1690 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1692 mf_set(const struct mf_field *mf,
1693 const union mf_value *value, const union mf_value *mask,
1694 struct cls_rule *rule)
1696 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1697 mf_set_value(mf, value, rule);
1699 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1700 mf_set_wild(mf, rule);
1709 case MFF_IPV6_LABEL:
1717 case MFF_ICMPV4_TYPE:
1718 case MFF_ICMPV4_CODE:
1719 case MFF_ICMPV6_TYPE:
1720 case MFF_ICMPV6_CODE:
1726 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1756 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1757 ntohl(value->be32), ntohl(mask->be32));
1761 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1765 cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1769 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1773 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1777 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1781 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1785 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1789 cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1793 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1797 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1801 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1806 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1811 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1821 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1825 VLOG_WARN_RL(&rl, "unknown %s field", type);
1826 } else if (!sf->n_bits) {
1827 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1828 } else if (sf->ofs >= sf->field->n_bits) {
1829 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1830 sf->ofs, sf->field->n_bits, type, sf->field->name);
1831 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1832 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1833 "of %s field %s", sf->ofs, sf->n_bits,
1834 sf->field->n_bits, type, sf->field->name);
1835 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1836 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1837 type, sf->field->name);
1842 return OFPERR_OFPBAC_BAD_ARGUMENT;
1845 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1846 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1849 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1851 return mf_check__(sf, flow, "source");
1854 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1855 * if so, otherwise an OpenFlow error code (e.g. as returned by
1858 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1860 int error = mf_check__(sf, flow, "destination");
1861 if (!error && !sf->field->writable) {
1862 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1864 return OFPERR_OFPBAC_BAD_ARGUMENT;
1869 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1870 * 'value' and 'mask', respectively. */
1872 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1873 union mf_value *value, union mf_value *mask)
1875 mf_get_value(mf, &rule->flow, value);
1876 mf_get_mask(mf, &rule->wc, mask);
1879 /* Assigns a random value for field 'mf' to 'value'. */
1881 mf_random_value(const struct mf_field *mf, union mf_value *value)
1883 random_bytes(value, mf->n_bytes);
1933 case MFF_ICMPV4_TYPE:
1934 case MFF_ICMPV4_CODE:
1935 case MFF_ICMPV6_TYPE:
1936 case MFF_ICMPV6_CODE:
1942 case MFF_IPV6_LABEL:
1943 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1947 value->u8 &= IP_DSCP_MASK;
1951 value->u8 &= IP_ECN_MASK;
1955 value->u8 &= FLOW_NW_FRAG_MASK;
1959 value->be16 &= htons(0xff);
1963 value->be16 &= htons(VLAN_VID_MASK);
1977 mf_from_integer_string(const struct mf_field *mf, const char *s,
1978 uint8_t *valuep, uint8_t *maskp)
1980 unsigned long long int integer, mask;
1985 integer = strtoull(s, &tail, 0);
1986 if (errno || (*tail != '\0' && *tail != '/')) {
1991 mask = strtoull(tail + 1, &tail, 0);
1992 if (errno || *tail != '\0') {
1999 for (i = mf->n_bytes - 1; i >= 0; i--) {
2000 valuep[i] = integer;
2006 return xasprintf("%s: value too large for %u-byte field %s",
2007 s, mf->n_bytes, mf->name);
2012 return xasprintf("%s: bad syntax for %s", s, mf->name);
2016 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2017 uint8_t mac[ETH_ADDR_LEN],
2018 uint8_t mask[ETH_ADDR_LEN])
2020 assert(mf->n_bytes == ETH_ADDR_LEN);
2022 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
2023 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
2024 case ETH_ADDR_SCAN_COUNT * 2:
2027 case ETH_ADDR_SCAN_COUNT:
2028 memset(mask, 0xff, ETH_ADDR_LEN);
2032 return xasprintf("%s: invalid Ethernet address", s);
2037 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2038 ovs_be32 *ip, ovs_be32 *mask)
2042 assert(mf->n_bytes == sizeof *ip);
2044 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2045 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2047 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2048 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2049 if (prefix <= 0 || prefix > 32) {
2050 return xasprintf("%s: network prefix bits not between 1 and "
2052 } else if (prefix == 32) {
2053 *mask = htonl(UINT32_MAX);
2055 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2057 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2058 *mask = htonl(UINT32_MAX);
2060 return xasprintf("%s: invalid IP address", s);
2066 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2067 struct in6_addr *value, struct in6_addr *mask)
2069 char *str = xstrdup(s);
2070 char *save_ptr = NULL;
2071 const char *name, *netmask;
2074 assert(mf->n_bytes == sizeof *value);
2076 name = strtok_r(str, "/", &save_ptr);
2077 retval = name ? lookup_ipv6(name, value) : EINVAL;
2081 err = xasprintf("%s: could not convert to IPv6 address", str);
2087 netmask = strtok_r(NULL, "/", &save_ptr);
2089 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2090 int prefix = atoi(netmask);
2091 if (prefix <= 0 || prefix > 128) {
2093 return xasprintf("%s: prefix bits not between 1 and 128", s);
2095 *mask = ipv6_create_mask(prefix);
2099 *mask = in6addr_exact;
2107 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2108 ovs_be16 *valuep, ovs_be16 *maskp)
2112 assert(mf->n_bytes == sizeof(ovs_be16));
2113 if (ofputil_port_from_string(s, &port)) {
2114 *valuep = htons(port);
2115 *maskp = htons(UINT16_MAX);
2118 return mf_from_integer_string(mf, s,
2119 (uint8_t *) valuep, (uint8_t *) maskp);
2123 struct frag_handling {
2129 static const struct frag_handling all_frags[] = {
2130 #define A FLOW_NW_FRAG_ANY
2131 #define L FLOW_NW_FRAG_LATER
2132 /* name mask value */
2135 { "first", A|L, A },
2136 { "later", A|L, A|L },
2141 { "not_later", L, 0 },
2148 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2150 const struct frag_handling *h;
2152 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2153 if (!strcasecmp(s, h->name)) {
2154 /* We force the upper bits of the mask on to make mf_parse_value()
2155 * happy (otherwise it will never think it's an exact match.) */
2156 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2162 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2163 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2166 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2167 * NULL if successful, otherwise a malloc()'d string describing the error. */
2169 mf_parse(const struct mf_field *mf, const char *s,
2170 union mf_value *value, union mf_value *mask)
2172 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2173 memset(value, 0, mf->n_bytes);
2174 memset(mask, 0, mf->n_bytes);
2178 switch (mf->string) {
2180 case MFS_HEXADECIMAL:
2181 return mf_from_integer_string(mf, s,
2182 (uint8_t *) value, (uint8_t *) mask);
2185 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2188 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2191 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2194 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2197 return mf_from_frag_string(s, &value->u8, &mask->u8);
2202 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2203 * successful, otherwise a malloc()'d string describing the error. */
2205 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2207 union mf_value mask;
2210 error = mf_parse(mf, s, value, &mask);
2215 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2216 return xasprintf("%s: wildcards not allowed here", s);
2222 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2223 const uint8_t *maskp, struct ds *s)
2225 unsigned long long int integer;
2228 assert(mf->n_bytes <= 8);
2231 for (i = 0; i < mf->n_bytes; i++) {
2232 integer = (integer << 8) | valuep[i];
2234 if (mf->string == MFS_HEXADECIMAL) {
2235 ds_put_format(s, "%#llx", integer);
2237 ds_put_format(s, "%lld", integer);
2241 unsigned long long int mask;
2244 for (i = 0; i < mf->n_bytes; i++) {
2245 mask = (mask << 8) | maskp[i];
2248 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2249 * not sure that that a bit-mask written in decimal is ever easier to
2250 * understand than the same bit-mask written in hexadecimal. */
2251 ds_put_format(s, "/%#llx", mask);
2256 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2259 const struct frag_handling *h;
2260 uint8_t value = *valuep;
2261 uint8_t mask = *maskp;
2264 mask &= FLOW_NW_FRAG_MASK;
2266 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2267 if (value == h->value && mask == h->mask) {
2268 ds_put_cstr(s, h->name);
2272 ds_put_cstr(s, "<error>");
2275 /* Appends to 's' a string representation of field 'mf' whose value is in
2276 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2278 mf_format(const struct mf_field *mf,
2279 const union mf_value *value, const union mf_value *mask,
2283 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2284 ds_put_cstr(s, "ANY");
2286 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2291 switch (mf->string) {
2294 ofputil_format_port(ntohs(value->be16), s);
2299 case MFS_HEXADECIMAL:
2300 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2304 eth_format_masked(value->mac, mask->mac, s);
2308 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2313 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2317 mf_format_frag_string(&value->u8, &mask->u8, s);
2325 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2326 * least-significant bits in 'x'.
2328 * See mf_set_subfield() for an example.
2330 * The difference between this function and mf_set_subfield() is that the
2331 * latter function can only handle subfields up to 64 bits wide, whereas this
2332 * one handles the general case. On the other hand, mf_set_subfield() is
2333 * arguably easier to use. */
2335 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2336 struct cls_rule *rule)
2338 const struct mf_field *field = sf->field;
2339 union mf_value value, mask;
2341 mf_get(field, rule, &value, &mask);
2342 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2343 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2344 mf_set(field, &value, &mask, rule);
2347 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2348 * least-significant bits of 'x'.
2350 * Example: suppose that 'sf->field' is originally the following 2-byte field
2353 * value == 0xe00a == 2#1110000000001010
2354 * mask == 0xfc3f == 2#1111110000111111
2356 * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2357 * sf->n_bits == 7 would have the following effect (note that 0x55 is
2360 * value == 0xd50a == 2#1101010100001010
2361 * mask == 0xff3f == 2#1111111100111111
2362 * ^^^^^^^ affected bits
2364 * The caller is responsible for ensuring that the result will be a valid
2365 * wildcard pattern for 'sf->field'. The caller is responsible for ensuring
2366 * that 'rule' meets 'sf->field''s prerequisites. */
2368 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2369 struct cls_rule *rule)
2371 const struct mf_field *field = sf->field;
2372 unsigned int n_bits = sf->n_bits;
2373 unsigned int ofs = sf->ofs;
2375 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2376 union mf_value value;
2379 for (i = field->n_bytes - 1; i >= 0; i--) {
2380 ((uint8_t *) &value)[i] = x;
2383 mf_set_value(field, &value, rule);
2385 union mf_value value, mask;
2386 uint8_t *vp = (uint8_t *) &value;
2387 uint8_t *mp = (uint8_t *) &mask;
2389 mf_get(field, rule, &value, &mask);
2390 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2391 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2392 mf_set(field, &value, &mask, rule);
2396 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2398 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2401 const struct mf_field *field = sf->field;
2402 unsigned int n_bits = sf->n_bits;
2403 unsigned int ofs = sf->ofs;
2404 union mf_value value;
2406 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2409 for (i = field->n_bytes - 1; i >= 0; i--) {
2410 ((uint8_t *) &value)[i] = x;
2413 mf_set_flow_value(field, &value, flow);
2415 mf_get_value(field, flow, &value);
2416 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2417 mf_set_flow_value(field, &value, flow);
2421 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2422 * reading 'flow', e.g. as checked by mf_check_src(). */
2424 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2425 union mf_subvalue *x)
2427 union mf_value value;
2429 mf_get_value(sf->field, flow, &value);
2431 memset(x, 0, sizeof *x);
2432 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2437 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2438 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2441 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2443 union mf_value value;
2445 mf_get_value(sf->field, flow, &value);
2446 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2449 /* Formats 'sf' into 's' in a format normally acceptable to
2450 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2451 * sf->field has no NXM name.) */
2453 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2456 ds_put_cstr(s, "<unknown>");
2457 } else if (sf->field->nxm_name) {
2458 ds_put_cstr(s, sf->field->nxm_name);
2459 } else if (sf->field->nxm_header) {
2460 uint32_t header = sf->field->nxm_header;
2461 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2463 ds_put_cstr(s, sf->field->name);
2466 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2467 ds_put_cstr(s, "[]");
2468 } else if (sf->n_bits == 1) {
2469 ds_put_format(s, "[%d]", sf->ofs);
2471 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2475 static const struct mf_field *
2476 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2480 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2485 for (i = 0; i < MFF_N_IDS; i++) {
2486 const struct mf_field *mf = mf_from_id(i);
2489 && !strncmp(mf->nxm_name, name, name_len)
2490 && mf->nxm_name[name_len] == '\0') {
2498 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2499 * returns NULL and advances '*sp' to the first byte following the parsed
2500 * string. On failure, returns a malloc()'d error message, does not modify
2501 * '*sp', and does not properly initialize 'sf'.
2503 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2504 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2505 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2506 * may both be omitted (the [] are still required) to indicate an entire
2509 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2511 const struct mf_field *field;
2520 name_len = strcspn(s, "[");
2521 if (s[name_len] != '[') {
2522 return xasprintf("%s: missing [ looking for field name", *sp);
2525 field = mf_parse_subfield_name(name, name_len, &wild);
2527 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2531 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2532 /* Nothing to do. */
2533 } else if (sscanf(s, "[%d]", &start) == 1) {
2535 } else if (!strncmp(s, "[]", 2)) {
2537 end = field->n_bits - 1;
2539 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2540 "[<start>..<end>]", *sp);
2542 s = strchr(s, ']') + 1;
2545 return xasprintf("%s: starting bit %d is after ending bit %d",
2547 } else if (start >= field->n_bits) {
2548 return xasprintf("%s: starting bit %d is not valid because field is "
2549 "only %d bits wide", *sp, start, field->n_bits);
2550 } else if (end >= field->n_bits){
2551 return xasprintf("%s: ending bit %d is not valid because field is "
2552 "only %d bits wide", *sp, end, field->n_bits);
2557 sf->n_bits = end - start + 1;
2563 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2564 * byte in 's' following the parsed string.
2566 * Exits with an error message if 's' has incorrect syntax.
2568 * The syntax parsed from 's' takes the form "header[start..end]" where
2569 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2570 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2571 * may both be omitted (the [] are still required) to indicate an entire
2574 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2576 char *msg = mf_parse_subfield__(sf, &s);
2578 ovs_fatal(0, "%s", msg);