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",
116 MFM_NONE, FWW_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);
445 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
446 * controller and so there's not much point in showing a lot of them. */
447 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
449 /* Returns the field with the given 'id'. */
450 const struct mf_field *
451 mf_from_id(enum mf_field_id id)
453 assert((unsigned int) id < MFF_N_IDS);
454 return &mf_fields[id];
457 /* Returns the field with the given 'name', or a null pointer if no field has
459 const struct mf_field *
460 mf_from_name(const char *name)
462 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
464 if (shash_is_empty(&mf_by_name)) {
465 const struct mf_field *mf;
467 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
468 shash_add_once(&mf_by_name, mf->name, mf);
469 if (mf->extra_name) {
470 shash_add_once(&mf_by_name, mf->extra_name, mf);
475 return shash_find_data(&mf_by_name, name);
479 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
483 f = xmalloc(sizeof *f);
484 hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
485 f->nxm_header = nxm_header;
492 const struct mf_field *mf;
494 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
495 if (mf->nxm_header) {
496 add_nxm_field(mf->nxm_header, mf);
497 if (mf->maskable != MFM_NONE) {
498 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
504 /* Verify that the header values are unique. */
505 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
506 if (mf->nxm_header) {
507 assert(mf_from_nxm_header(mf->nxm_header) == mf);
508 if (mf->maskable != MFM_NONE) {
509 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
517 const struct mf_field *
518 mf_from_nxm_header(uint32_t header)
520 const struct nxm_field *f;
522 if (hmap_is_empty(&all_nxm_fields)) {
526 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
528 if (f->nxm_header == header) {
536 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
537 * specifies at least one bit in the field.
539 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
540 * meets 'mf''s prerequisites. */
542 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
558 assert(mf->fww_bit != 0);
559 return (wc->wildcards & mf->fww_bit) != 0;
562 return !wc->tun_id_mask;
591 return !wc->reg_masks[mf->id - MFF_REG0];
594 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
595 == (FWW_ETH_MCAST | FWW_DL_DST));
598 return !wc->vlan_tci_mask;
600 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
602 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
605 return !wc->nw_src_mask;
607 return !wc->nw_dst_mask;
610 return ipv6_mask_is_any(&wc->ipv6_src_mask);
612 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
615 return ipv6_mask_is_any(&wc->nd_target_mask);
618 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
621 return !wc->nw_src_mask;
623 return !wc->nw_dst_mask;
627 case MFF_ICMPV4_TYPE:
628 case MFF_ICMPV6_TYPE:
629 return !wc->tp_src_mask;
632 case MFF_ICMPV4_CODE:
633 case MFF_ICMPV6_CODE:
634 return !wc->tp_dst_mask;
642 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
643 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
644 * purposes, or to 0 if it is wildcarded.
646 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
647 * meets 'mf''s prerequisites. */
649 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
650 union mf_value *mask)
666 assert(mf->fww_bit != 0);
667 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
671 mask->be64 = wc->tun_id_mask;
701 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
705 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
710 mask->be16 = wc->vlan_tci_mask;
713 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
716 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
720 mask->be32 = wc->nw_src_mask;
723 mask->be32 = wc->nw_dst_mask;
727 mask->ipv6 = wc->ipv6_src_mask;
730 mask->ipv6 = wc->ipv6_dst_mask;
734 mask->ipv6 = wc->nd_target_mask;
738 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
742 mask->be32 = wc->nw_src_mask;
745 mask->be32 = wc->nw_dst_mask;
750 mask->be16 = wc->tp_src_mask;
754 mask->be16 = wc->tp_dst_mask;
757 case MFF_ICMPV4_TYPE:
758 case MFF_ICMPV6_TYPE:
759 mask->u8 = ntohs(wc->tp_src_mask);
761 case MFF_ICMPV4_CODE:
762 case MFF_ICMPV6_CODE:
763 mask->u8 = ntohs(wc->tp_dst_mask);
772 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
773 * if the mask is valid, false otherwise. */
775 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
777 switch (mf->maskable) {
779 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
780 is_all_ones((const uint8_t *) mask, mf->n_bytes));
786 return (mf->n_bytes == 4
787 ? ip_is_cidr(mask->be32)
788 : ipv6_is_cidr(&mask->ipv6));
791 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
798 is_ip_any(const struct flow *flow)
800 return (flow->dl_type == htons(ETH_TYPE_IP) ||
801 flow->dl_type == htons(ETH_TYPE_IPV6));
805 is_icmpv4(const struct flow *flow)
807 return (flow->dl_type == htons(ETH_TYPE_IP)
808 && flow->nw_proto == IPPROTO_ICMP);
812 is_icmpv6(const struct flow *flow)
814 return (flow->dl_type == htons(ETH_TYPE_IPV6)
815 && flow->nw_proto == IPPROTO_ICMPV6);
818 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
820 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
822 switch (mf->prereqs) {
827 return flow->dl_type == htons(ETH_TYPE_ARP);
829 return flow->dl_type == htons(ETH_TYPE_IP);
831 return flow->dl_type == htons(ETH_TYPE_IPV6);
833 return is_ip_any(flow);
836 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
838 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
840 return is_icmpv4(flow);
842 return is_icmpv6(flow);
845 return (is_icmpv6(flow)
846 && flow->tp_dst == htons(0)
847 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
848 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
850 return (is_icmpv6(flow)
851 && flow->tp_dst == htons(0)
852 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
854 return (is_icmpv6(flow)
855 && flow->tp_dst == htons(0)
856 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
862 /* Returns true if 'value' may be a valid value *as part of a masked match*,
865 * A value is not rejected just because it is not valid for the field in
866 * question, but only if it doesn't make sense to test the bits in question at
867 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
868 * without the VLAN_CFI bit being set, but we can't reject those values because
869 * it is still legitimate to test just for those bits (see the documentation
870 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
871 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
873 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
923 case MFF_ICMPV4_TYPE:
924 case MFF_ICMPV4_CODE:
925 case MFF_ICMPV6_TYPE:
926 case MFF_ICMPV6_CODE:
933 return !(value->u8 & ~IP_DSCP_MASK);
935 return !(value->u8 & ~IP_ECN_MASK);
937 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
940 return !(value->be16 & htons(0xff00));
943 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
946 return !(value->u8 & ~7);
949 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
957 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
958 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
960 mf_get_value(const struct mf_field *mf, const struct flow *flow,
961 union mf_value *value)
965 value->be64 = flow->tun_id;
969 value->be16 = htons(flow->in_port);
999 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1003 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1007 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1011 value->be16 = flow->dl_type;
1015 value->be16 = flow->vlan_tci;
1019 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1023 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1027 value->be32 = flow->nw_src;
1031 value->be32 = flow->nw_dst;
1035 value->ipv6 = flow->ipv6_src;
1039 value->ipv6 = flow->ipv6_dst;
1042 case MFF_IPV6_LABEL:
1043 value->be32 = flow->ipv6_label;
1047 value->u8 = flow->nw_proto;
1051 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1055 value->u8 = flow->nw_tos & IP_ECN_MASK;
1059 value->u8 = flow->nw_ttl;
1063 value->u8 = flow->nw_frag;
1067 value->be16 = htons(flow->nw_proto);
1071 value->be32 = flow->nw_src;
1075 value->be32 = flow->nw_dst;
1080 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1085 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1089 value->be16 = flow->tp_src;
1093 value->be16 = flow->tp_dst;
1097 value->be16 = flow->tp_src;
1101 value->be16 = flow->tp_dst;
1104 case MFF_ICMPV4_TYPE:
1105 case MFF_ICMPV6_TYPE:
1106 value->u8 = ntohs(flow->tp_src);
1109 case MFF_ICMPV4_CODE:
1110 case MFF_ICMPV6_CODE:
1111 value->u8 = ntohs(flow->tp_dst);
1115 value->ipv6 = flow->nd_target;
1124 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1125 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1128 mf_set_value(const struct mf_field *mf,
1129 const union mf_value *value, struct cls_rule *rule)
1133 cls_rule_set_tun_id(rule, value->be64);
1137 cls_rule_set_in_port(rule, ntohs(value->be16));
1168 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1173 cls_rule_set_dl_src(rule, value->mac);
1177 cls_rule_set_dl_dst(rule, value->mac);
1181 cls_rule_set_dl_type(rule, value->be16);
1185 cls_rule_set_dl_tci(rule, value->be16);
1189 cls_rule_set_dl_vlan(rule, value->be16);
1193 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1197 cls_rule_set_nw_src(rule, value->be32);
1201 cls_rule_set_nw_dst(rule, value->be32);
1205 cls_rule_set_ipv6_src(rule, &value->ipv6);
1209 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1212 case MFF_IPV6_LABEL:
1213 cls_rule_set_ipv6_label(rule, value->be32);
1217 cls_rule_set_nw_proto(rule, value->u8);
1221 cls_rule_set_nw_dscp(rule, value->u8);
1225 cls_rule_set_nw_ecn(rule, value->u8);
1229 cls_rule_set_nw_ttl(rule, value->u8);
1233 cls_rule_set_nw_frag(rule, value->u8);
1237 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1241 cls_rule_set_nw_src(rule, value->be32);
1245 cls_rule_set_nw_dst(rule, value->be32);
1250 cls_rule_set_arp_sha(rule, value->mac);
1255 cls_rule_set_arp_tha(rule, value->mac);
1259 cls_rule_set_tp_src(rule, value->be16);
1263 cls_rule_set_tp_dst(rule, value->be16);
1267 cls_rule_set_tp_src(rule, value->be16);
1271 cls_rule_set_tp_dst(rule, value->be16);
1274 case MFF_ICMPV4_TYPE:
1275 case MFF_ICMPV6_TYPE:
1276 cls_rule_set_icmp_type(rule, value->u8);
1279 case MFF_ICMPV4_CODE:
1280 case MFF_ICMPV6_CODE:
1281 cls_rule_set_icmp_code(rule, value->u8);
1285 cls_rule_set_nd_target(rule, &value->ipv6);
1294 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1295 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1298 mf_set_flow_value(const struct mf_field *mf,
1299 const union mf_value *value, struct flow *flow)
1303 flow->tun_id = value->be64;
1307 flow->in_port = ntohs(value->be16);
1338 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1343 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1347 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1351 flow->dl_type = value->be16;
1355 flow->vlan_tci = value->be16;
1359 flow_set_vlan_vid(flow, value->be16);
1363 flow_set_vlan_pcp(flow, value->u8);
1367 flow->nw_src = value->be32;
1371 flow->nw_dst = value->be32;
1375 flow->ipv6_src = value->ipv6;
1379 flow->ipv6_dst = value->ipv6;
1382 case MFF_IPV6_LABEL:
1383 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1387 flow->nw_proto = value->u8;
1391 flow->nw_tos &= ~IP_DSCP_MASK;
1392 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1396 flow->nw_tos &= ~IP_ECN_MASK;
1397 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1401 flow->nw_ttl = value->u8;
1405 flow->nw_frag &= value->u8;
1409 flow->nw_proto = ntohs(value->be16);
1413 flow->nw_src = value->be32;
1417 flow->nw_dst = value->be32;
1422 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1427 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1432 flow->tp_src = value->be16;
1437 flow->tp_dst = value->be16;
1440 case MFF_ICMPV4_TYPE:
1441 case MFF_ICMPV6_TYPE:
1442 flow->tp_src = htons(value->u8);
1445 case MFF_ICMPV4_CODE:
1446 case MFF_ICMPV6_CODE:
1447 flow->tp_dst = htons(value->u8);
1451 flow->nd_target = value->ipv6;
1460 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1462 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1465 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1467 union mf_value value;
1469 mf_get_value(mf, flow, &value);
1470 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1473 /* Makes 'rule' wildcard field 'mf'.
1475 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1478 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1482 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1486 rule->wc.wildcards |= FWW_IN_PORT;
1487 rule->flow.in_port = 0;
1492 cls_rule_set_reg_masked(rule, 0, 0, 0);
1497 cls_rule_set_reg_masked(rule, 1, 0, 0);
1502 cls_rule_set_reg_masked(rule, 2, 0, 0);
1507 cls_rule_set_reg_masked(rule, 3, 0, 0);
1512 cls_rule_set_reg_masked(rule, 4, 0, 0);
1517 cls_rule_set_reg_masked(rule, 5, 0, 0);
1522 cls_rule_set_reg_masked(rule, 6, 0, 0);
1527 cls_rule_set_reg_masked(rule, 7, 0, 0);
1535 rule->wc.wildcards |= FWW_DL_SRC;
1536 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1540 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1541 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1545 rule->wc.wildcards |= FWW_DL_TYPE;
1546 rule->flow.dl_type = htons(0);
1550 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1554 cls_rule_set_any_vid(rule);
1558 cls_rule_set_any_pcp(rule);
1563 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1568 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1572 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1573 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1577 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1578 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1581 case MFF_IPV6_LABEL:
1582 rule->wc.wildcards |= FWW_IPV6_LABEL;
1583 rule->flow.ipv6_label = 0;
1587 rule->wc.wildcards |= FWW_NW_PROTO;
1588 rule->flow.nw_proto = 0;
1592 rule->wc.wildcards |= FWW_NW_DSCP;
1593 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1597 rule->wc.wildcards |= FWW_NW_ECN;
1598 rule->flow.nw_tos &= ~IP_ECN_MASK;
1602 rule->wc.wildcards |= FWW_NW_TTL;
1603 rule->flow.nw_ttl = 0;
1607 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1608 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1612 rule->wc.wildcards |= FWW_NW_PROTO;
1613 rule->flow.nw_proto = 0;
1618 rule->wc.wildcards |= FWW_ARP_SHA;
1619 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1624 rule->wc.wildcards |= FWW_ARP_THA;
1625 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1630 case MFF_ICMPV4_TYPE:
1631 case MFF_ICMPV6_TYPE:
1632 rule->wc.tp_src_mask = htons(0);
1633 rule->flow.tp_src = htons(0);
1638 case MFF_ICMPV4_CODE:
1639 case MFF_ICMPV6_CODE:
1640 rule->wc.tp_dst_mask = htons(0);
1641 rule->flow.tp_dst = htons(0);
1645 memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1646 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1655 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1656 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1657 * with a 1-bit indicating that the corresponding value bit must match and a
1658 * 0-bit indicating a don't-care.
1660 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1661 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1662 * call is equivalent to mf_set_wild(mf, rule).
1664 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1665 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1667 mf_set(const struct mf_field *mf,
1668 const union mf_value *value, const union mf_value *mask,
1669 struct cls_rule *rule)
1671 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1672 mf_set_value(mf, value, rule);
1674 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1675 mf_set_wild(mf, rule);
1685 case MFF_IPV6_LABEL:
1693 case MFF_ICMPV4_TYPE:
1694 case MFF_ICMPV4_CODE:
1695 case MFF_ICMPV6_TYPE:
1696 case MFF_ICMPV6_CODE:
1702 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1732 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1733 ntohl(value->be32), ntohl(mask->be32));
1737 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1738 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1743 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1747 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1751 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1755 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1759 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1763 cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1767 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1771 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1775 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1780 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1785 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1795 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1799 VLOG_WARN_RL(&rl, "unknown %s field", type);
1800 } else if (!sf->n_bits) {
1801 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1802 } else if (sf->ofs >= sf->field->n_bits) {
1803 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1804 sf->ofs, sf->field->n_bits, type, sf->field->name);
1805 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1806 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1807 "of %s field %s", sf->ofs, sf->n_bits,
1808 sf->field->n_bits, type, sf->field->name);
1809 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1810 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1811 type, sf->field->name);
1816 return OFPERR_OFPBAC_BAD_ARGUMENT;
1819 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1820 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1823 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1825 return mf_check__(sf, flow, "source");
1828 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1829 * if so, otherwise an OpenFlow error code (e.g. as returned by
1832 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1834 int error = mf_check__(sf, flow, "destination");
1835 if (!error && !sf->field->writable) {
1836 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1838 return OFPERR_OFPBAC_BAD_ARGUMENT;
1843 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1844 * 'value' and 'mask', respectively. */
1846 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1847 union mf_value *value, union mf_value *mask)
1849 mf_get_value(mf, &rule->flow, value);
1850 mf_get_mask(mf, &rule->wc, mask);
1853 /* Assigns a random value for field 'mf' to 'value'. */
1855 mf_random_value(const struct mf_field *mf, union mf_value *value)
1857 random_bytes(value, mf->n_bytes);
1907 case MFF_ICMPV4_TYPE:
1908 case MFF_ICMPV4_CODE:
1909 case MFF_ICMPV6_TYPE:
1910 case MFF_ICMPV6_CODE:
1916 case MFF_IPV6_LABEL:
1917 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1921 value->u8 &= IP_DSCP_MASK;
1925 value->u8 &= IP_ECN_MASK;
1929 value->u8 &= FLOW_NW_FRAG_MASK;
1933 value->be16 &= htons(0xff);
1937 value->be16 &= htons(VLAN_VID_MASK);
1951 mf_from_integer_string(const struct mf_field *mf, const char *s,
1952 uint8_t *valuep, uint8_t *maskp)
1954 unsigned long long int integer, mask;
1959 integer = strtoull(s, &tail, 0);
1960 if (errno || (*tail != '\0' && *tail != '/')) {
1965 mask = strtoull(tail + 1, &tail, 0);
1966 if (errno || *tail != '\0') {
1973 for (i = mf->n_bytes - 1; i >= 0; i--) {
1974 valuep[i] = integer;
1980 return xasprintf("%s: value too large for %u-byte field %s",
1981 s, mf->n_bytes, mf->name);
1986 return xasprintf("%s: bad syntax for %s", s, mf->name);
1990 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1991 uint8_t mac[ETH_ADDR_LEN],
1992 uint8_t mask[ETH_ADDR_LEN])
1994 assert(mf->n_bytes == ETH_ADDR_LEN);
1996 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1997 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1998 case ETH_ADDR_SCAN_COUNT * 2:
2001 case ETH_ADDR_SCAN_COUNT:
2002 memset(mask, 0xff, ETH_ADDR_LEN);
2006 return xasprintf("%s: invalid Ethernet address", s);
2011 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2012 ovs_be32 *ip, ovs_be32 *mask)
2016 assert(mf->n_bytes == sizeof *ip);
2018 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2019 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2021 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2022 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2023 if (prefix <= 0 || prefix > 32) {
2024 return xasprintf("%s: network prefix bits not between 1 and "
2026 } else if (prefix == 32) {
2027 *mask = htonl(UINT32_MAX);
2029 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2031 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2032 *mask = htonl(UINT32_MAX);
2034 return xasprintf("%s: invalid IP address", s);
2040 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2041 struct in6_addr *value, struct in6_addr *mask)
2043 char *str = xstrdup(s);
2044 char *save_ptr = NULL;
2045 const char *name, *netmask;
2048 assert(mf->n_bytes == sizeof *value);
2050 name = strtok_r(str, "/", &save_ptr);
2051 retval = name ? lookup_ipv6(name, value) : EINVAL;
2055 err = xasprintf("%s: could not convert to IPv6 address", str);
2061 netmask = strtok_r(NULL, "/", &save_ptr);
2063 int prefix = atoi(netmask);
2064 if (prefix <= 0 || prefix > 128) {
2066 return xasprintf("%s: prefix bits not between 1 and 128", s);
2068 *mask = ipv6_create_mask(prefix);
2071 *mask = in6addr_exact;
2079 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2080 ovs_be16 *valuep, ovs_be16 *maskp)
2084 assert(mf->n_bytes == sizeof(ovs_be16));
2085 if (ofputil_port_from_string(s, &port)) {
2086 *valuep = htons(port);
2087 *maskp = htons(UINT16_MAX);
2090 return mf_from_integer_string(mf, s,
2091 (uint8_t *) valuep, (uint8_t *) maskp);
2095 struct frag_handling {
2101 static const struct frag_handling all_frags[] = {
2102 #define A FLOW_NW_FRAG_ANY
2103 #define L FLOW_NW_FRAG_LATER
2104 /* name mask value */
2107 { "first", A|L, A },
2108 { "later", A|L, A|L },
2113 { "not_later", L, 0 },
2120 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2122 const struct frag_handling *h;
2124 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2125 if (!strcasecmp(s, h->name)) {
2126 /* We force the upper bits of the mask on to make mf_parse_value()
2127 * happy (otherwise it will never think it's an exact match.) */
2128 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2134 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2135 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2138 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2139 * NULL if successful, otherwise a malloc()'d string describing the error. */
2141 mf_parse(const struct mf_field *mf, const char *s,
2142 union mf_value *value, union mf_value *mask)
2144 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2145 memset(value, 0, mf->n_bytes);
2146 memset(mask, 0, mf->n_bytes);
2150 switch (mf->string) {
2152 case MFS_HEXADECIMAL:
2153 return mf_from_integer_string(mf, s,
2154 (uint8_t *) value, (uint8_t *) mask);
2157 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2160 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2163 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2166 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2169 return mf_from_frag_string(s, &value->u8, &mask->u8);
2174 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2175 * successful, otherwise a malloc()'d string describing the error. */
2177 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2179 union mf_value mask;
2182 error = mf_parse(mf, s, value, &mask);
2187 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2188 return xasprintf("%s: wildcards not allowed here", s);
2194 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2195 const uint8_t *maskp, struct ds *s)
2197 unsigned long long int integer;
2200 assert(mf->n_bytes <= 8);
2203 for (i = 0; i < mf->n_bytes; i++) {
2204 integer = (integer << 8) | valuep[i];
2206 if (mf->string == MFS_HEXADECIMAL) {
2207 ds_put_format(s, "%#llx", integer);
2209 ds_put_format(s, "%lld", integer);
2213 unsigned long long int mask;
2216 for (i = 0; i < mf->n_bytes; i++) {
2217 mask = (mask << 8) | maskp[i];
2220 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2221 * not sure that that a bit-mask written in decimal is ever easier to
2222 * understand than the same bit-mask written in hexadecimal. */
2223 ds_put_format(s, "/%#llx", mask);
2228 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2231 const struct frag_handling *h;
2232 uint8_t value = *valuep;
2233 uint8_t mask = *maskp;
2236 mask &= FLOW_NW_FRAG_MASK;
2238 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2239 if (value == h->value && mask == h->mask) {
2240 ds_put_cstr(s, h->name);
2244 ds_put_cstr(s, "<error>");
2247 /* Appends to 's' a string representation of field 'mf' whose value is in
2248 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2250 mf_format(const struct mf_field *mf,
2251 const union mf_value *value, const union mf_value *mask,
2255 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2256 ds_put_cstr(s, "ANY");
2258 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2263 switch (mf->string) {
2266 ofputil_format_port(ntohs(value->be16), s);
2271 case MFS_HEXADECIMAL:
2272 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2276 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2278 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2283 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2288 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2292 mf_format_frag_string(&value->u8, &mask->u8, s);
2300 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2301 * least-significant bits in 'x'.
2303 * See mf_set_subfield() for an example.
2305 * The difference between this function and mf_set_subfield() is that the
2306 * latter function can only handle subfields up to 64 bits wide, whereas this
2307 * one handles the general case. On the other hand, mf_set_subfield() is
2308 * arguably easier to use. */
2310 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2311 struct cls_rule *rule)
2313 const struct mf_field *field = sf->field;
2314 union mf_value value, mask;
2316 mf_get(field, rule, &value, &mask);
2317 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2318 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2319 mf_set(field, &value, &mask, rule);
2322 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2323 * least-significant bits of 'x'.
2325 * Example: suppose that 'sf->field' is originally the following 2-byte field
2328 * value == 0xe00a == 2#1110000000001010
2329 * mask == 0xfc3f == 2#1111110000111111
2331 * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2332 * sf->n_bits == 7 would have the following effect (note that 0x55 is
2335 * value == 0xd50a == 2#1101010100001010
2336 * mask == 0xff3f == 2#1111111100111111
2337 * ^^^^^^^ affected bits
2339 * The caller is responsible for ensuring that the result will be a valid
2340 * wildcard pattern for 'sf->field'. The caller is responsible for ensuring
2341 * that 'rule' meets 'sf->field''s prerequisites. */
2343 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2344 struct cls_rule *rule)
2346 const struct mf_field *field = sf->field;
2347 unsigned int n_bits = sf->n_bits;
2348 unsigned int ofs = sf->ofs;
2350 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2351 union mf_value value;
2354 for (i = field->n_bytes - 1; i >= 0; i--) {
2355 ((uint8_t *) &value)[i] = x;
2358 mf_set_value(field, &value, rule);
2360 union mf_value value, mask;
2361 uint8_t *vp = (uint8_t *) &value;
2362 uint8_t *mp = (uint8_t *) &mask;
2364 mf_get(field, rule, &value, &mask);
2365 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2366 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2367 mf_set(field, &value, &mask, rule);
2371 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2373 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2376 const struct mf_field *field = sf->field;
2377 unsigned int n_bits = sf->n_bits;
2378 unsigned int ofs = sf->ofs;
2379 union mf_value value;
2381 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2384 for (i = field->n_bytes - 1; i >= 0; i--) {
2385 ((uint8_t *) &value)[i] = x;
2388 mf_set_flow_value(field, &value, flow);
2390 mf_get_value(field, flow, &value);
2391 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2392 mf_set_flow_value(field, &value, flow);
2396 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2397 * reading 'flow', e.g. as checked by mf_check_src(). */
2399 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2400 union mf_subvalue *x)
2402 union mf_value value;
2404 mf_get_value(sf->field, flow, &value);
2406 memset(x, 0, sizeof *x);
2407 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2412 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2413 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2416 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2418 union mf_value value;
2420 mf_get_value(sf->field, flow, &value);
2421 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2424 /* Formats 'sf' into 's' in a format normally acceptable to
2425 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2426 * sf->field has no NXM name.) */
2428 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2431 ds_put_cstr(s, "<unknown>");
2432 } else if (sf->field->nxm_name) {
2433 ds_put_cstr(s, sf->field->nxm_name);
2434 } else if (sf->field->nxm_header) {
2435 uint32_t header = sf->field->nxm_header;
2436 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2438 ds_put_cstr(s, sf->field->name);
2441 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2442 ds_put_cstr(s, "[]");
2443 } else if (sf->n_bits == 1) {
2444 ds_put_format(s, "[%d]", sf->ofs);
2446 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2450 static const struct mf_field *
2451 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2455 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2460 for (i = 0; i < MFF_N_IDS; i++) {
2461 const struct mf_field *mf = mf_from_id(i);
2464 && !strncmp(mf->nxm_name, name, name_len)
2465 && mf->nxm_name[name_len] == '\0') {
2473 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2474 * returns NULL and advances '*sp' to the first byte following the parsed
2475 * string. On failure, returns a malloc()'d error message, does not modify
2476 * '*sp', and does not properly initialize 'sf'.
2478 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2479 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2480 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2481 * may both be omitted (the [] are still required) to indicate an entire
2484 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2486 const struct mf_field *field;
2495 name_len = strcspn(s, "[");
2496 if (s[name_len] != '[') {
2497 return xasprintf("%s: missing [ looking for field name", *sp);
2500 field = mf_parse_subfield_name(name, name_len, &wild);
2502 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2506 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2507 /* Nothing to do. */
2508 } else if (sscanf(s, "[%d]", &start) == 1) {
2510 } else if (!strncmp(s, "[]", 2)) {
2512 end = field->n_bits - 1;
2514 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2515 "[<start>..<end>]", *sp);
2517 s = strchr(s, ']') + 1;
2520 return xasprintf("%s: starting bit %d is after ending bit %d",
2522 } else if (start >= field->n_bits) {
2523 return xasprintf("%s: starting bit %d is not valid because field is "
2524 "only %d bits wide", *sp, start, field->n_bits);
2525 } else if (end >= field->n_bits){
2526 return xasprintf("%s: ending bit %d is not valid because field is "
2527 "only %d bits wide", *sp, end, field->n_bits);
2532 sf->n_bits = end - start + 1;
2538 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2539 * byte in 's' following the parsed string.
2541 * Exits with an error message if 's' has incorrect syntax.
2543 * The syntax parsed from 's' takes the form "header[start..end]" where
2544 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2545 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2546 * may both be omitted (the [] are still required) to indicate an entire
2549 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2551 char *msg = mf_parse_subfield__(sf, &s);
2553 ovs_fatal(0, "%s", msg);