2 * Copyright (c) 2011, 2012 Nicira Networks.
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",
58 MFF_IN_PORT, "in_port", NULL,
60 MFM_NONE, FWW_IN_PORT,
64 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
67 #define REGISTER(IDX) \
69 MFF_REG##IDX, "reg" #IDX, NULL, \
70 MF_FIELD_SIZES(be32), \
111 MFF_ETH_SRC, "eth_src", "dl_src",
113 MFM_NONE, FWW_DL_SRC,
117 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
119 MFF_ETH_DST, "eth_dst", "dl_dst",
125 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
127 MFF_ETH_TYPE, "eth_type", "dl_type",
128 MF_FIELD_SIZES(be16),
129 MFM_NONE, FWW_DL_TYPE,
133 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
137 MFF_VLAN_TCI, "vlan_tci", NULL,
138 MF_FIELD_SIZES(be16),
143 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
145 MFF_VLAN_VID, "dl_vlan", NULL,
146 sizeof(ovs_be16), 12,
153 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
167 MFF_IPV4_SRC, "ip_src", "nw_src",
168 MF_FIELD_SIZES(be32),
173 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
175 MFF_IPV4_DST, "ip_dst", "nw_dst",
176 MF_FIELD_SIZES(be32),
181 NXM_OF_IP_DST, "NXM_OF_IP_DST",
185 MFF_IPV6_SRC, "ipv6_src", NULL,
186 MF_FIELD_SIZES(ipv6),
191 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
193 MFF_IPV6_DST, "ipv6_dst", NULL,
194 MF_FIELD_SIZES(ipv6),
199 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
202 MFF_IPV6_LABEL, "ipv6_label", NULL,
204 MFM_NONE, FWW_IPV6_LABEL,
208 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
212 MFF_IP_PROTO, "nw_proto", NULL,
214 MFM_NONE, FWW_NW_PROTO,
218 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
220 MFF_IP_DSCP, "nw_tos", NULL,
222 MFM_NONE, FWW_NW_DSCP,
226 NXM_OF_IP_TOS, "NXM_OF_IP_TOS"
228 MFF_IP_ECN, "nw_ecn", NULL,
230 MFM_NONE, FWW_NW_ECN,
234 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
236 MFF_IP_TTL, "nw_ttl", NULL,
238 MFM_NONE, FWW_NW_TTL,
242 NXM_NX_IP_TTL, "NXM_NX_IP_TTL"
244 MFF_IP_FRAG, "ip_frag", NULL,
250 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG"
254 MFF_ARP_OP, "arp_op", NULL,
255 MF_FIELD_SIZES(be16),
256 MFM_NONE, FWW_NW_PROTO,
260 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
262 MFF_ARP_SPA, "arp_spa", NULL,
263 MF_FIELD_SIZES(be32),
268 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
270 MFF_ARP_TPA, "arp_tpa", NULL,
271 MF_FIELD_SIZES(be32),
276 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
278 MFF_ARP_SHA, "arp_sha", NULL,
280 MFM_NONE, FWW_ARP_SHA,
284 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
286 MFF_ARP_THA, "arp_tha", NULL,
288 MFM_NONE, FWW_ARP_THA,
292 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
300 MFF_TCP_SRC, "tcp_src", "tp_src",
301 MF_FIELD_SIZES(be16),
306 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
308 MFF_TCP_DST, "tcp_dst", "tp_dst",
309 MF_FIELD_SIZES(be16),
314 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
318 MFF_UDP_SRC, "udp_src", NULL,
319 MF_FIELD_SIZES(be16),
324 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
326 MFF_UDP_DST, "udp_dst", NULL,
327 MF_FIELD_SIZES(be16),
332 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
336 MFF_ICMPV4_TYPE, "icmp_type", NULL,
342 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
344 MFF_ICMPV4_CODE, "icmp_code", NULL,
350 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
354 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
360 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
362 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
368 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
376 MFF_ND_TARGET, "nd_target", NULL,
377 MF_FIELD_SIZES(ipv6),
378 MFM_NONE, FWW_ND_TARGET,
382 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
384 MFF_ND_SLL, "nd_sll", NULL,
386 MFM_NONE, FWW_ARP_SHA,
390 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
392 MFF_ND_TLL, "nd_tll", NULL,
394 MFM_NONE, FWW_ARP_THA,
398 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
403 struct hmap_node hmap_node;
405 const struct mf_field *mf;
408 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
410 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
411 * controller and so there's not much point in showing a lot of them. */
412 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
414 /* Returns the field with the given 'id'. */
415 const struct mf_field *
416 mf_from_id(enum mf_field_id id)
418 assert((unsigned int) id < MFF_N_IDS);
419 return &mf_fields[id];
422 /* Returns the field with the given 'name', or a null pointer if no field has
424 const struct mf_field *
425 mf_from_name(const char *name)
427 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
429 if (shash_is_empty(&mf_by_name)) {
430 const struct mf_field *mf;
432 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
433 shash_add_once(&mf_by_name, mf->name, mf);
434 if (mf->extra_name) {
435 shash_add_once(&mf_by_name, mf->extra_name, mf);
440 return shash_find_data(&mf_by_name, name);
444 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
448 f = xmalloc(sizeof *f);
449 hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
450 f->nxm_header = nxm_header;
457 const struct mf_field *mf;
459 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
460 if (mf->nxm_header) {
461 add_nxm_field(mf->nxm_header, mf);
462 if (mf->maskable != MFM_NONE) {
463 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
469 /* Verify that the header values are unique. */
470 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
471 if (mf->nxm_header) {
472 assert(mf_from_nxm_header(mf->nxm_header) == mf);
473 if (mf->maskable != MFM_NONE) {
474 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
482 const struct mf_field *
483 mf_from_nxm_header(uint32_t header)
485 const struct nxm_field *f;
487 if (hmap_is_empty(&all_nxm_fields)) {
491 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
493 if (f->nxm_header == header) {
501 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
502 * specifies at least one bit in the field.
504 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
505 * meets 'mf''s prerequisites. */
507 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
524 assert(mf->fww_bit != 0);
525 return (wc->wildcards & mf->fww_bit) != 0;
528 return !wc->tun_id_mask;
557 return !wc->reg_masks[mf->id - MFF_REG0];
560 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
561 == (FWW_ETH_MCAST | FWW_DL_DST));
564 return !wc->vlan_tci_mask;
566 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
568 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
571 return !wc->nw_src_mask;
573 return !wc->nw_dst_mask;
576 return ipv6_mask_is_any(&wc->ipv6_src_mask);
578 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
581 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
584 return !wc->nw_src_mask;
586 return !wc->nw_dst_mask;
590 case MFF_ICMPV4_TYPE:
591 case MFF_ICMPV6_TYPE:
592 return !wc->tp_src_mask;
595 case MFF_ICMPV4_CODE:
596 case MFF_ICMPV6_CODE:
597 return !wc->tp_dst_mask;
605 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
606 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
607 * purposes, or to 0 if it is wildcarded.
609 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
610 * meets 'mf''s prerequisites. */
612 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
613 union mf_value *mask)
630 assert(mf->fww_bit != 0);
631 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
635 mask->be64 = wc->tun_id_mask;
665 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
669 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
674 mask->be16 = wc->vlan_tci_mask;
677 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
680 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
684 mask->be32 = wc->nw_src_mask;
687 mask->be32 = wc->nw_dst_mask;
691 mask->ipv6 = wc->ipv6_src_mask;
694 mask->ipv6 = wc->ipv6_dst_mask;
698 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
702 mask->be32 = wc->nw_src_mask;
705 mask->be32 = wc->nw_dst_mask;
710 mask->be16 = wc->tp_src_mask;
714 mask->be16 = wc->tp_dst_mask;
717 case MFF_ICMPV4_TYPE:
718 case MFF_ICMPV6_TYPE:
719 mask->u8 = ntohs(wc->tp_src_mask);
721 case MFF_ICMPV4_CODE:
722 case MFF_ICMPV6_CODE:
723 mask->u8 = ntohs(wc->tp_dst_mask);
732 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
733 * if the mask is valid, false otherwise. */
735 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
737 switch (mf->maskable) {
739 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
740 is_all_ones((const uint8_t *) mask, mf->n_bytes));
746 return (mf->n_bytes == 4
747 ? ip_is_cidr(mask->be32)
748 : ipv6_is_cidr(&mask->ipv6));
751 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
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)
883 case MFF_ICMPV4_TYPE:
884 case MFF_ICMPV4_CODE:
885 case MFF_ICMPV6_TYPE:
886 case MFF_ICMPV6_CODE:
893 return !(value->u8 & ~IP_DSCP_MASK);
895 return !(value->u8 & ~IP_ECN_MASK);
897 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
900 return !(value->be16 & htons(0xff00));
903 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
906 return !(value->u8 & ~7);
909 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
917 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
918 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
920 mf_get_value(const struct mf_field *mf, const struct flow *flow,
921 union mf_value *value)
925 value->be64 = flow->tun_id;
929 value->be16 = htons(flow->in_port);
959 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
963 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
967 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
971 value->be16 = flow->dl_type;
975 value->be16 = flow->vlan_tci;
979 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
983 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
987 value->be32 = flow->nw_src;
991 value->be32 = flow->nw_dst;
995 value->ipv6 = flow->ipv6_src;
999 value->ipv6 = flow->ipv6_dst;
1002 case MFF_IPV6_LABEL:
1003 value->be32 = flow->ipv6_label;
1007 value->u8 = flow->nw_proto;
1011 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1015 value->u8 = flow->nw_tos & IP_ECN_MASK;
1019 value->u8 = flow->nw_ttl;
1023 value->u8 = flow->nw_frag;
1027 value->be16 = htons(flow->nw_proto);
1031 value->be32 = flow->nw_src;
1035 value->be32 = flow->nw_dst;
1040 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1045 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1049 value->be16 = flow->tp_src;
1053 value->be16 = flow->tp_dst;
1057 value->be16 = flow->tp_src;
1061 value->be16 = flow->tp_dst;
1064 case MFF_ICMPV4_TYPE:
1065 case MFF_ICMPV6_TYPE:
1066 value->u8 = ntohs(flow->tp_src);
1069 case MFF_ICMPV4_CODE:
1070 case MFF_ICMPV6_CODE:
1071 value->u8 = ntohs(flow->tp_dst);
1075 value->ipv6 = flow->nd_target;
1084 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1085 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1088 mf_set_value(const struct mf_field *mf,
1089 const union mf_value *value, struct cls_rule *rule)
1093 cls_rule_set_tun_id(rule, value->be64);
1097 cls_rule_set_in_port(rule, ntohs(value->be16));
1128 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1133 cls_rule_set_dl_src(rule, value->mac);
1137 cls_rule_set_dl_dst(rule, value->mac);
1141 cls_rule_set_dl_type(rule, value->be16);
1145 cls_rule_set_dl_tci(rule, value->be16);
1149 cls_rule_set_dl_vlan(rule, value->be16);
1153 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1157 cls_rule_set_nw_src(rule, value->be32);
1161 cls_rule_set_nw_dst(rule, value->be32);
1165 cls_rule_set_ipv6_src(rule, &value->ipv6);
1169 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1172 case MFF_IPV6_LABEL:
1173 cls_rule_set_ipv6_label(rule, value->be32);
1177 cls_rule_set_nw_proto(rule, value->u8);
1181 cls_rule_set_nw_dscp(rule, value->u8);
1185 cls_rule_set_nw_ecn(rule, value->u8);
1189 cls_rule_set_nw_ttl(rule, value->u8);
1193 cls_rule_set_nw_frag(rule, value->u8);
1197 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1201 cls_rule_set_nw_src(rule, value->be32);
1205 cls_rule_set_nw_dst(rule, value->be32);
1210 cls_rule_set_arp_sha(rule, value->mac);
1215 cls_rule_set_arp_tha(rule, value->mac);
1219 cls_rule_set_tp_src(rule, value->be16);
1223 cls_rule_set_tp_dst(rule, value->be16);
1227 cls_rule_set_tp_src(rule, value->be16);
1231 cls_rule_set_tp_dst(rule, value->be16);
1234 case MFF_ICMPV4_TYPE:
1235 case MFF_ICMPV6_TYPE:
1236 cls_rule_set_icmp_type(rule, value->u8);
1239 case MFF_ICMPV4_CODE:
1240 case MFF_ICMPV6_CODE:
1241 cls_rule_set_icmp_code(rule, value->u8);
1245 cls_rule_set_nd_target(rule, &value->ipv6);
1254 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1255 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1258 mf_set_flow_value(const struct mf_field *mf,
1259 const union mf_value *value, struct flow *flow)
1263 flow->tun_id = value->be64;
1267 flow->in_port = ntohs(value->be16);
1298 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1303 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1307 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1311 flow->dl_type = value->be16;
1315 flow->vlan_tci = value->be16;
1319 flow_set_vlan_vid(flow, value->be16);
1323 flow_set_vlan_pcp(flow, value->u8);
1327 flow->nw_src = value->be32;
1331 flow->nw_dst = value->be32;
1335 flow->ipv6_src = value->ipv6;
1339 flow->ipv6_dst = value->ipv6;
1342 case MFF_IPV6_LABEL:
1343 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1347 flow->nw_proto = value->u8;
1351 flow->nw_tos &= ~IP_DSCP_MASK;
1352 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1356 flow->nw_tos &= ~IP_ECN_MASK;
1357 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1361 flow->nw_ttl = value->u8;
1365 flow->nw_frag &= value->u8;
1369 flow->nw_proto = ntohs(value->be16);
1373 flow->nw_src = value->be32;
1377 flow->nw_dst = value->be32;
1382 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1387 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1392 flow->tp_src = value->be16;
1397 flow->tp_dst = value->be16;
1400 case MFF_ICMPV4_TYPE:
1401 case MFF_ICMPV6_TYPE:
1402 flow->tp_src = htons(value->u8);
1405 case MFF_ICMPV4_CODE:
1406 case MFF_ICMPV6_CODE:
1407 flow->tp_dst = htons(value->u8);
1411 flow->nd_target = value->ipv6;
1420 /* Makes 'rule' wildcard field 'mf'.
1422 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1425 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1429 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1433 rule->wc.wildcards |= FWW_IN_PORT;
1434 rule->flow.in_port = 0;
1439 cls_rule_set_reg_masked(rule, 0, 0, 0);
1444 cls_rule_set_reg_masked(rule, 1, 0, 0);
1449 cls_rule_set_reg_masked(rule, 2, 0, 0);
1454 cls_rule_set_reg_masked(rule, 3, 0, 0);
1459 cls_rule_set_reg_masked(rule, 4, 0, 0);
1464 cls_rule_set_reg_masked(rule, 5, 0, 0);
1469 cls_rule_set_reg_masked(rule, 6, 0, 0);
1474 cls_rule_set_reg_masked(rule, 7, 0, 0);
1482 rule->wc.wildcards |= FWW_DL_SRC;
1483 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1487 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1488 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1492 rule->wc.wildcards |= FWW_DL_TYPE;
1493 rule->flow.dl_type = htons(0);
1497 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1501 cls_rule_set_any_vid(rule);
1505 cls_rule_set_any_pcp(rule);
1510 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1515 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1519 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1520 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1524 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1525 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1528 case MFF_IPV6_LABEL:
1529 rule->wc.wildcards |= FWW_IPV6_LABEL;
1530 rule->flow.ipv6_label = 0;
1534 rule->wc.wildcards |= FWW_NW_PROTO;
1535 rule->flow.nw_proto = 0;
1539 rule->wc.wildcards |= FWW_NW_DSCP;
1540 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1544 rule->wc.wildcards |= FWW_NW_ECN;
1545 rule->flow.nw_tos &= ~IP_ECN_MASK;
1549 rule->wc.wildcards |= FWW_NW_TTL;
1550 rule->flow.nw_ttl = 0;
1554 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1555 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1559 rule->wc.wildcards |= FWW_NW_PROTO;
1560 rule->flow.nw_proto = 0;
1565 rule->wc.wildcards |= FWW_ARP_SHA;
1566 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1571 rule->wc.wildcards |= FWW_ARP_THA;
1572 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1577 case MFF_ICMPV4_TYPE:
1578 case MFF_ICMPV6_TYPE:
1579 rule->wc.tp_src_mask = htons(0);
1580 rule->flow.tp_src = htons(0);
1585 case MFF_ICMPV4_CODE:
1586 case MFF_ICMPV6_CODE:
1587 rule->wc.tp_dst_mask = htons(0);
1588 rule->flow.tp_dst = htons(0);
1592 rule->wc.wildcards |= FWW_ND_TARGET;
1593 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1602 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1603 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1604 * with a 1-bit indicating that the corresponding value bit must match and a
1605 * 0-bit indicating a don't-care.
1607 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1608 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1609 * call is equivalent to mf_set_wild(mf, rule).
1611 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1612 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1614 mf_set(const struct mf_field *mf,
1615 const union mf_value *value, const union mf_value *mask,
1616 struct cls_rule *rule)
1618 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1619 mf_set_value(mf, value, rule);
1621 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1622 mf_set_wild(mf, rule);
1632 case MFF_IPV6_LABEL:
1640 case MFF_ICMPV4_TYPE:
1641 case MFF_ICMPV4_CODE:
1642 case MFF_ICMPV6_TYPE:
1643 case MFF_ICMPV6_CODE:
1650 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1680 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1681 ntohl(value->be32), ntohl(mask->be32));
1685 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1686 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1691 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1695 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1699 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1703 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1707 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1711 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1715 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1719 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1724 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1729 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1739 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1743 VLOG_WARN_RL(&rl, "unknown %s field", type);
1744 } else if (!sf->n_bits) {
1745 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1746 } else if (sf->ofs >= sf->field->n_bits) {
1747 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1748 sf->ofs, sf->field->n_bits, type, sf->field->name);
1749 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1750 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1751 "of %s field %s", sf->ofs, sf->n_bits,
1752 sf->field->n_bits, type, sf->field->name);
1753 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1754 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1755 type, sf->field->name);
1760 return OFPERR_OFPBAC_BAD_ARGUMENT;
1763 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1764 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1767 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1769 return mf_check__(sf, flow, "source");
1772 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1773 * if so, otherwise an OpenFlow error code (e.g. as returned by
1776 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1778 int error = mf_check__(sf, flow, "destination");
1779 if (!error && !sf->field->writable) {
1780 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1782 return OFPERR_OFPBAC_BAD_ARGUMENT;
1787 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1788 * 'value' and 'mask', respectively. */
1790 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1791 union mf_value *value, union mf_value *mask)
1793 mf_get_value(mf, &rule->flow, value);
1794 mf_get_mask(mf, &rule->wc, mask);
1797 /* Assigns a random value for field 'mf' to 'value'. */
1799 mf_random_value(const struct mf_field *mf, union mf_value *value)
1801 random_bytes(value, mf->n_bytes);
1851 case MFF_ICMPV4_TYPE:
1852 case MFF_ICMPV4_CODE:
1853 case MFF_ICMPV6_TYPE:
1854 case MFF_ICMPV6_CODE:
1860 case MFF_IPV6_LABEL:
1861 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1865 value->u8 &= IP_DSCP_MASK;
1869 value->u8 &= IP_ECN_MASK;
1873 value->u8 &= FLOW_NW_FRAG_MASK;
1877 value->be16 &= htons(0xff);
1881 value->be16 &= htons(VLAN_VID_MASK);
1895 mf_from_integer_string(const struct mf_field *mf, const char *s,
1896 uint8_t *valuep, uint8_t *maskp)
1898 unsigned long long int integer, mask;
1903 integer = strtoull(s, &tail, 0);
1904 if (errno || (*tail != '\0' && *tail != '/')) {
1909 mask = strtoull(tail + 1, &tail, 0);
1910 if (errno || *tail != '\0') {
1917 for (i = mf->n_bytes - 1; i >= 0; i--) {
1918 valuep[i] = integer;
1924 return xasprintf("%s: value too large for %u-byte field %s",
1925 s, mf->n_bytes, mf->name);
1930 return xasprintf("%s: bad syntax for %s", s, mf->name);
1934 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1935 uint8_t mac[ETH_ADDR_LEN],
1936 uint8_t mask[ETH_ADDR_LEN])
1938 assert(mf->n_bytes == ETH_ADDR_LEN);
1940 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1941 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1942 case ETH_ADDR_SCAN_COUNT * 2:
1945 case ETH_ADDR_SCAN_COUNT:
1946 memset(mask, 0xff, ETH_ADDR_LEN);
1950 return xasprintf("%s: invalid Ethernet address", s);
1955 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1956 ovs_be32 *ip, ovs_be32 *mask)
1960 assert(mf->n_bytes == sizeof *ip);
1962 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1963 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1965 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1966 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1967 if (prefix <= 0 || prefix > 32) {
1968 return xasprintf("%s: network prefix bits not between 1 and "
1970 } else if (prefix == 32) {
1971 *mask = htonl(UINT32_MAX);
1973 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1975 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1976 *mask = htonl(UINT32_MAX);
1978 return xasprintf("%s: invalid IP address", s);
1984 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1985 struct in6_addr *value, struct in6_addr *mask)
1987 char *str = xstrdup(s);
1988 char *save_ptr = NULL;
1989 const char *name, *netmask;
1992 assert(mf->n_bytes == sizeof *value);
1994 name = strtok_r(str, "/", &save_ptr);
1995 retval = name ? lookup_ipv6(name, value) : EINVAL;
1999 err = xasprintf("%s: could not convert to IPv6 address", str);
2005 netmask = strtok_r(NULL, "/", &save_ptr);
2007 int prefix = atoi(netmask);
2008 if (prefix <= 0 || prefix > 128) {
2010 return xasprintf("%s: prefix bits not between 1 and 128", s);
2012 *mask = ipv6_create_mask(prefix);
2015 *mask = in6addr_exact;
2023 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2024 ovs_be16 *valuep, ovs_be16 *maskp)
2028 assert(mf->n_bytes == sizeof(ovs_be16));
2029 if (ofputil_port_from_string(s, &port)) {
2030 *valuep = htons(port);
2031 *maskp = htons(UINT16_MAX);
2034 return mf_from_integer_string(mf, s,
2035 (uint8_t *) valuep, (uint8_t *) maskp);
2039 struct frag_handling {
2045 static const struct frag_handling all_frags[] = {
2046 #define A FLOW_NW_FRAG_ANY
2047 #define L FLOW_NW_FRAG_LATER
2048 /* name mask value */
2051 { "first", A|L, A },
2052 { "later", A|L, A|L },
2057 { "not_later", L, 0 },
2064 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2066 const struct frag_handling *h;
2068 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2069 if (!strcasecmp(s, h->name)) {
2070 /* We force the upper bits of the mask on to make mf_parse_value()
2071 * happy (otherwise it will never think it's an exact match.) */
2072 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2078 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2079 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2082 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2083 * NULL if successful, otherwise a malloc()'d string describing the error. */
2085 mf_parse(const struct mf_field *mf, const char *s,
2086 union mf_value *value, union mf_value *mask)
2088 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2089 memset(value, 0, mf->n_bytes);
2090 memset(mask, 0, mf->n_bytes);
2094 switch (mf->string) {
2096 case MFS_HEXADECIMAL:
2097 return mf_from_integer_string(mf, s,
2098 (uint8_t *) value, (uint8_t *) mask);
2101 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2104 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2107 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2110 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2113 return mf_from_frag_string(s, &value->u8, &mask->u8);
2118 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2119 * successful, otherwise a malloc()'d string describing the error. */
2121 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2123 union mf_value mask;
2126 error = mf_parse(mf, s, value, &mask);
2131 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2132 return xasprintf("%s: wildcards not allowed here", s);
2138 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2139 const uint8_t *maskp, struct ds *s)
2141 unsigned long long int integer;
2144 assert(mf->n_bytes <= 8);
2147 for (i = 0; i < mf->n_bytes; i++) {
2148 integer = (integer << 8) | valuep[i];
2150 if (mf->string == MFS_HEXADECIMAL) {
2151 ds_put_format(s, "%#llx", integer);
2153 ds_put_format(s, "%lld", integer);
2157 unsigned long long int mask;
2160 for (i = 0; i < mf->n_bytes; i++) {
2161 mask = (mask << 8) | maskp[i];
2164 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2165 * not sure that that a bit-mask written in decimal is ever easier to
2166 * understand than the same bit-mask written in hexadecimal. */
2167 ds_put_format(s, "/%#llx", mask);
2172 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2175 const struct frag_handling *h;
2176 uint8_t value = *valuep;
2177 uint8_t mask = *maskp;
2180 mask &= FLOW_NW_FRAG_MASK;
2182 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2183 if (value == h->value && mask == h->mask) {
2184 ds_put_cstr(s, h->name);
2188 ds_put_cstr(s, "<error>");
2191 /* Appends to 's' a string representation of field 'mf' whose value is in
2192 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2194 mf_format(const struct mf_field *mf,
2195 const union mf_value *value, const union mf_value *mask,
2199 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2200 ds_put_cstr(s, "ANY");
2202 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2207 switch (mf->string) {
2210 ofputil_format_port(ntohs(value->be16), s);
2215 case MFS_HEXADECIMAL:
2216 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2220 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2222 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2227 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2232 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2236 mf_format_frag_string(&value->u8, &mask->u8, s);
2244 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
2245 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
2248 * Example: suppose that 'mf' is originally the following 2-byte field in
2251 * value == 0xe00a == 2#1110000000001010
2252 * mask == 0xfc3f == 2#1111110000111111
2254 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
2255 * effect (note that 0x55 is 2#1010101):
2257 * value == 0xd50a == 2#1101010100001010
2258 * mask == 0xff3f == 2#1111111100111111
2260 * The caller is responsible for ensuring that the result will be a valid
2261 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
2262 * 'rule' meets 'mf''s prerequisites. */
2264 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2265 struct cls_rule *rule)
2267 const struct mf_field *field = sf->field;
2268 unsigned int n_bits = sf->n_bits;
2269 unsigned int ofs = sf->ofs;
2271 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2272 union mf_value value;
2275 for (i = field->n_bytes - 1; i >= 0; i--) {
2276 ((uint8_t *) &value)[i] = x;
2279 mf_set_value(field, &value, rule);
2281 union mf_value value, mask;
2282 uint8_t *vp = (uint8_t *) &value;
2283 uint8_t *mp = (uint8_t *) &mask;
2285 mf_get(field, rule, &value, &mask);
2286 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2287 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2288 mf_set(field, &value, &mask, rule);
2292 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2294 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2297 const struct mf_field *field = sf->field;
2298 unsigned int n_bits = sf->n_bits;
2299 unsigned int ofs = sf->ofs;
2300 union mf_value value;
2302 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2305 for (i = field->n_bytes - 1; i >= 0; i--) {
2306 ((uint8_t *) &value)[i] = x;
2309 mf_set_flow_value(field, &value, flow);
2311 mf_get_value(field, flow, &value);
2312 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2313 mf_set_flow_value(field, &value, flow);
2317 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2318 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2321 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2323 union mf_value value;
2325 mf_get_value(sf->field, flow, &value);
2326 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2329 /* Formats 'sf' into 's' in a format normally acceptable to
2330 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2331 * sf->field has no NXM name.) */
2333 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2336 ds_put_cstr(s, "<unknown>");
2337 } else if (sf->field->nxm_name) {
2338 ds_put_cstr(s, sf->field->nxm_name);
2339 } else if (sf->field->nxm_header) {
2340 uint32_t header = sf->field->nxm_header;
2341 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2343 ds_put_cstr(s, sf->field->name);
2346 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2347 ds_put_cstr(s, "[]");
2348 } else if (sf->n_bits == 1) {
2349 ds_put_format(s, "[%d]", sf->ofs);
2351 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2355 static const struct mf_field *
2356 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2360 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2365 for (i = 0; i < MFF_N_IDS; i++) {
2366 const struct mf_field *mf = mf_from_id(i);
2369 && !strncmp(mf->nxm_name, name, name_len)
2370 && mf->nxm_name[name_len] == '\0') {
2378 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2379 * returns NULL and advances '*sp' to the first byte following the parsed
2380 * string. On failure, returns a malloc()'d error message, does not modify
2381 * '*sp', and does not properly initialize 'sf'.
2383 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2384 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2385 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2386 * may both be omitted (the [] are still required) to indicate an entire
2389 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2391 const struct mf_field *field;
2400 name_len = strcspn(s, "[");
2401 if (s[name_len] != '[') {
2402 return xasprintf("%s: missing [ looking for field name", *sp);
2405 field = mf_parse_subfield_name(name, name_len, &wild);
2407 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2411 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2412 /* Nothing to do. */
2413 } else if (sscanf(s, "[%d]", &start) == 1) {
2415 } else if (!strncmp(s, "[]", 2)) {
2417 end = field->n_bits - 1;
2419 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2420 "[<start>..<end>]", *sp);
2422 s = strchr(s, ']') + 1;
2425 return xasprintf("%s: starting bit %d is after ending bit %d",
2427 } else if (start >= field->n_bits) {
2428 return xasprintf("%s: starting bit %d is not valid because field is "
2429 "only %d bits wide", *sp, start, field->n_bits);
2430 } else if (end >= field->n_bits){
2431 return xasprintf("%s: ending bit %d is not valid because field is "
2432 "only %d bits wide", *sp, end, field->n_bits);
2437 sf->n_bits = end - start + 1;
2443 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2444 * byte in 's' following the parsed string.
2446 * Exits with an error message if 's' has incorrect syntax.
2448 * The syntax parsed from 's' takes the form "header[start..end]" where
2449 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2450 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2451 * may both be omitted (the [] are still required) to indicate an entire
2454 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2456 char *msg = mf_parse_subfield__(sf, &s);
2458 ovs_fatal(0, "%s", msg);