2 * Copyright (c) 2011 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"
33 #include "socket-util.h"
34 #include "unaligned.h"
36 #define MF_FIELD_SIZES(MEMBER) \
37 sizeof ((union mf_value *)0)->MEMBER, \
38 8 * sizeof ((union mf_value *)0)->MEMBER
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
46 MFF_TUN_ID, "tun_id", NULL,
53 MFF_IN_PORT, "in_port", NULL,
55 MFM_NONE, FWW_IN_PORT,
61 #define REGISTER(IDX) \
63 MFF_REG##IDX, "reg" #IDX, NULL, \
64 MF_FIELD_SIZES(be32), \
94 MFF_ETH_SRC, "eth_src", "dl_src",
101 MFF_ETH_DST, "eth_dst", "dl_dst",
108 MFF_ETH_TYPE, "eth_type", "dl_type",
109 MF_FIELD_SIZES(be16),
110 MFM_NONE, FWW_DL_TYPE,
117 MFF_VLAN_TCI, "vlan_tci", NULL,
118 MF_FIELD_SIZES(be16),
124 MFF_VLAN_VID, "dl_vlan", NULL,
125 sizeof(ovs_be16), 12,
131 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
144 MFF_IPV4_SRC, "ip_src", "nw_src",
145 MF_FIELD_SIZES(be32),
151 MFF_IPV4_DST, "ip_dst", "nw_dst",
152 MF_FIELD_SIZES(be32),
160 MFF_IPV6_SRC, "ipv6_src", NULL,
161 MF_FIELD_SIZES(ipv6),
167 MFF_IPV6_DST, "ipv6_dst", NULL,
168 MF_FIELD_SIZES(ipv6),
175 MFF_IPV6_LABEL, "ipv6_label", NULL,
177 MFM_NONE, FWW_IPV6_LABEL,
184 MFF_IP_PROTO, "nw_proto", NULL,
186 MFM_NONE, FWW_NW_PROTO,
191 MFF_IP_DSCP, "nw_tos", NULL,
198 MFF_IP_ECN, "nw_ecn", NULL,
205 MFF_IP_TTL, "nw_ttl", NULL,
207 MFM_NONE, FWW_NW_TTL,
212 MFF_IP_FRAG, "ip_frag", NULL,
221 MFF_ARP_OP, "arp_op", NULL,
222 MF_FIELD_SIZES(be16),
223 MFM_NONE, FWW_NW_PROTO,
228 MFF_ARP_SPA, "arp_spa", NULL,
229 MF_FIELD_SIZES(be32),
235 MFF_ARP_TPA, "arp_tpa", NULL,
236 MF_FIELD_SIZES(be32),
242 MFF_ARP_SHA, "arp_sha", NULL,
244 MFM_NONE, FWW_ARP_SHA,
249 MFF_ARP_THA, "arp_tha", NULL,
251 MFM_NONE, FWW_ARP_THA,
262 MFF_TCP_SRC, "tcp_src", "tp_src",
263 MF_FIELD_SIZES(be16),
264 MFM_NONE, FWW_TP_SRC,
269 MFF_TCP_DST, "tcp_dst", "tp_dst",
270 MF_FIELD_SIZES(be16),
271 MFM_NONE, FWW_TP_DST,
278 MFF_UDP_SRC, "udp_src", NULL,
279 MF_FIELD_SIZES(be16),
280 MFM_NONE, FWW_TP_SRC,
285 MFF_UDP_DST, "udp_dst", NULL,
286 MF_FIELD_SIZES(be16),
287 MFM_NONE, FWW_TP_DST,
294 MFF_ICMP_TYPE, "icmp_type", NULL,
296 MFM_NONE, FWW_TP_SRC,
301 MFF_ICMP_CODE, "icmp_code", NULL,
303 MFM_NONE, FWW_TP_DST,
314 MFF_ND_TARGET, "nd_target", NULL,
315 MF_FIELD_SIZES(ipv6),
316 MFM_NONE, FWW_ND_TARGET,
321 MFF_ND_SLL, "nd_sll", NULL,
323 MFM_NONE, FWW_ARP_SHA,
328 MFF_ND_TLL, "nd_tll", NULL,
330 MFM_NONE, FWW_ARP_THA,
337 /* Returns the field with the given 'id'. */
338 const struct mf_field *
339 mf_from_id(enum mf_field_id id)
341 assert((unsigned int) id < MFF_N_IDS);
342 return &mf_fields[id];
345 /* Returns the field with the given 'name', or a null pointer if no field has
347 const struct mf_field *
348 mf_from_name(const char *name)
350 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
352 if (shash_is_empty(&mf_by_name)) {
353 const struct mf_field *mf;
355 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
356 shash_add_once(&mf_by_name, mf->name, mf);
357 if (mf->extra_name) {
358 shash_add_once(&mf_by_name, mf->extra_name, mf);
363 return shash_find_data(&mf_by_name, name);
366 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
367 * specifies at least one bit in the field.
369 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
370 * meets 'mf''s prerequisites. */
372 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
393 assert(mf->fww_bit != 0);
394 return (wc->wildcards & mf->fww_bit) != 0;
397 return !wc->tun_id_mask;
417 return !wc->reg_masks[mf->id - MFF_REG0];
420 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
421 == (FWW_ETH_MCAST | FWW_DL_DST));
424 return !wc->vlan_tci_mask;
426 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
428 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
431 return !wc->nw_src_mask;
433 return !wc->nw_dst_mask;
436 return ipv6_mask_is_any(&wc->ipv6_src_mask);
438 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
441 return !(wc->nw_tos_mask & IP_DSCP_MASK);
443 return !(wc->nw_tos_mask & IP_ECN_MASK);
445 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
448 return !wc->nw_src_mask;
450 return !wc->nw_dst_mask;
458 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
459 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
460 * purposes, or to 0 if it is wildcarded.
462 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
463 * meets 'mf''s prerequisites. */
465 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
466 union mf_value *mask)
487 assert(mf->fww_bit != 0);
488 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
492 mask->be64 = wc->tun_id_mask;
513 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
517 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
522 mask->be16 = wc->vlan_tci_mask;
525 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
528 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
532 mask->be32 = wc->nw_src_mask;
535 mask->be32 = wc->nw_dst_mask;
539 mask->ipv6 = wc->ipv6_src_mask;
542 mask->ipv6 = wc->ipv6_dst_mask;
546 mask->u8 = wc->nw_tos_mask & IP_DSCP_MASK;
549 mask->u8 = wc->nw_tos_mask & IP_ECN_MASK;
552 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
556 mask->be32 = wc->nw_src_mask;
559 mask->be32 = wc->nw_dst_mask;
568 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
569 * if the mask is valid, false otherwise. */
571 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
573 switch (mf->maskable) {
575 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
576 is_all_ones((const uint8_t *) mask, mf->n_bytes));
582 return (mf->n_bytes == 4
583 ? ip_is_cidr(mask->be32)
584 : ipv6_is_cidr(&mask->ipv6));
587 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
594 is_ip_any(const struct flow *flow)
596 return (flow->dl_type == htons(ETH_TYPE_IP) ||
597 flow->dl_type == htons(ETH_TYPE_IPV6));
601 is_icmpv4(const struct flow *flow)
603 return (flow->dl_type == htons(ETH_TYPE_IP)
604 && flow->nw_proto == IPPROTO_ICMP);
608 is_icmpv6(const struct flow *flow)
610 return (flow->dl_type == htons(ETH_TYPE_IPV6)
611 && flow->nw_proto == IPPROTO_ICMPV6);
614 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
616 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
618 switch (mf->prereqs) {
623 return flow->dl_type == htons(ETH_TYPE_ARP);
625 return flow->dl_type == htons(ETH_TYPE_IP);
627 return flow->dl_type == htons(ETH_TYPE_IPV6);
629 return is_ip_any(flow);
632 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
634 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
636 return is_icmpv6(flow);
638 return is_icmpv4(flow) || is_icmpv6(flow);
641 return (is_icmpv6(flow)
642 && flow->tp_dst == htons(0)
643 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
644 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
646 return (is_icmpv6(flow)
647 && flow->tp_dst == htons(0)
648 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
650 return (is_icmpv6(flow)
651 && flow->tp_dst == htons(0)
652 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
658 /* Returns true if 'value' may be a valid value *as part of a masked match*,
661 * A value is not rejected just because it is not valid for the field in
662 * question, but only if it doesn't make sense to test the bits in question at
663 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
664 * without the VLAN_CFI bit being set, but we can't reject those values because
665 * it is still legitimate to test just for those bits (see the documentation
666 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
667 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
669 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
718 return !(value->u8 & ~IP_DSCP_MASK);
720 return !(value->u8 & ~IP_ECN_MASK);
722 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
725 return !(value->be16 & htons(0xff00));
728 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
731 return !(value->u8 & ~7);
734 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
742 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
743 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
745 mf_get_value(const struct mf_field *mf, const struct flow *flow,
746 union mf_value *value)
750 value->be64 = flow->tun_id;
754 value->be16 = htons(flow->in_port);
775 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
779 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
783 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
787 value->be16 = flow->dl_type;
791 value->be16 = flow->vlan_tci;
795 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
799 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
803 value->be32 = flow->nw_src;
807 value->be32 = flow->nw_dst;
811 value->ipv6 = flow->ipv6_src;
815 value->ipv6 = flow->ipv6_dst;
819 value->be32 = flow->ipv6_label;
823 value->u8 = flow->nw_proto;
827 value->u8 = flow->nw_tos & IP_DSCP_MASK;
831 value->u8 = flow->nw_tos & IP_ECN_MASK;
835 value->u8 = flow->nw_ttl;
839 value->u8 = flow->nw_frag;
843 value->be16 = htons(flow->nw_proto);
847 value->be32 = flow->nw_src;
851 value->be32 = flow->nw_dst;
856 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
861 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
865 value->be16 = flow->tp_src;
869 value->be16 = flow->tp_dst;
873 value->be16 = flow->tp_src;
877 value->be16 = flow->tp_dst;
881 value->u8 = ntohs(flow->tp_src);
885 value->u8 = ntohs(flow->tp_dst);
889 value->ipv6 = flow->nd_target;
898 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
899 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
902 mf_set_value(const struct mf_field *mf,
903 const union mf_value *value, struct cls_rule *rule)
907 cls_rule_set_tun_id(rule, value->be64);
911 cls_rule_set_in_port(rule, ntohs(value->be16));
933 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
938 cls_rule_set_dl_src(rule, value->mac);
942 cls_rule_set_dl_dst(rule, value->mac);
946 cls_rule_set_dl_type(rule, value->be16);
950 cls_rule_set_dl_tci(rule, value->be16);
954 cls_rule_set_dl_vlan(rule, value->be16);
958 cls_rule_set_dl_vlan_pcp(rule, value->u8);
962 cls_rule_set_nw_src(rule, value->be32);
966 cls_rule_set_nw_dst(rule, value->be32);
970 cls_rule_set_ipv6_src(rule, &value->ipv6);
974 cls_rule_set_ipv6_dst(rule, &value->ipv6);
978 cls_rule_set_ipv6_label(rule, value->be32);
982 cls_rule_set_nw_proto(rule, value->u8);
986 cls_rule_set_nw_dscp(rule, value->u8);
990 cls_rule_set_nw_ecn(rule, value->u8);
994 cls_rule_set_nw_ttl(rule, value->u8);
998 cls_rule_set_nw_frag(rule, value->u8);
1002 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1006 cls_rule_set_nw_src(rule, value->be32);
1010 cls_rule_set_nw_dst(rule, value->be32);
1015 cls_rule_set_arp_sha(rule, value->mac);
1020 cls_rule_set_arp_tha(rule, value->mac);
1024 cls_rule_set_tp_src(rule, value->be16);
1028 cls_rule_set_tp_dst(rule, value->be16);
1032 cls_rule_set_tp_src(rule, value->be16);
1036 cls_rule_set_tp_dst(rule, value->be16);
1040 cls_rule_set_icmp_type(rule, value->u8);
1044 cls_rule_set_icmp_code(rule, value->u8);
1048 cls_rule_set_nd_target(rule, &value->ipv6);
1057 /* Makes 'rule' wildcard field 'mf'.
1059 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1062 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1066 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1070 rule->wc.wildcards |= FWW_IN_PORT;
1071 rule->flow.in_port = 0;
1076 cls_rule_set_reg_masked(rule, 0, 0, 0);
1081 cls_rule_set_reg_masked(rule, 1, 0, 0);
1086 cls_rule_set_reg_masked(rule, 2, 0, 0);
1091 cls_rule_set_reg_masked(rule, 3, 0, 0);
1096 cls_rule_set_reg_masked(rule, 4, 0, 0);
1104 rule->wc.wildcards |= FWW_DL_SRC;
1105 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1109 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1110 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1114 rule->wc.wildcards |= FWW_DL_TYPE;
1115 rule->flow.dl_type = htons(0);
1119 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1123 cls_rule_set_any_vid(rule);
1127 cls_rule_set_any_pcp(rule);
1132 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1137 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1141 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1142 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1146 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1147 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1150 case MFF_IPV6_LABEL:
1151 rule->wc.wildcards |= FWW_IPV6_LABEL;
1152 rule->flow.ipv6_label = 0;
1156 rule->wc.wildcards |= FWW_NW_PROTO;
1157 rule->flow.nw_proto = 0;
1161 rule->wc.nw_tos_mask |= IP_DSCP_MASK;
1162 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1166 rule->wc.nw_tos_mask |= IP_ECN_MASK;
1167 rule->flow.nw_tos &= ~IP_ECN_MASK;
1171 rule->wc.wildcards |= FWW_NW_TTL;
1172 rule->flow.nw_ttl = 0;
1176 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1177 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1181 rule->wc.wildcards |= FWW_NW_PROTO;
1182 rule->flow.nw_proto = 0;
1187 rule->wc.wildcards |= FWW_ARP_SHA;
1188 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1193 rule->wc.wildcards |= FWW_ARP_THA;
1194 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1200 rule->wc.wildcards |= FWW_TP_SRC;
1201 rule->flow.tp_src = htons(0);
1207 rule->wc.wildcards |= FWW_TP_DST;
1208 rule->flow.tp_dst = htons(0);
1212 rule->wc.wildcards |= FWW_ND_TARGET;
1213 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1222 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1223 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1224 * with a 1-bit indicating that the corresponding value bit must match and a
1225 * 0-bit indicating a don't-care.
1227 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1228 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1229 * call is equivalent to mf_set_wild(mf, rule).
1231 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1232 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1234 mf_set(const struct mf_field *mf,
1235 const union mf_value *value, const union mf_value *mask,
1236 struct cls_rule *rule)
1238 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1239 mf_set_value(mf, value, rule);
1241 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1242 mf_set_wild(mf, rule);
1252 case MFF_IPV6_LABEL:
1272 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1293 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1294 ntohl(value->be32), ntohl(mask->be32));
1298 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1299 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1304 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1308 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1312 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1316 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1320 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1324 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1328 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1332 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1341 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1342 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1345 * Example: suppose that 'mf' is originally the following 2-byte field in
1348 * value == 0xe00a == 2#1110000000001010
1349 * mask == 0xfc3f == 2#1111110000111111
1351 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1352 * effect (note that 0x55 is 2#1010101):
1354 * value == 0xd50a == 2#1101010100001010
1355 * mask == 0xff3f == 2#1111111100111111
1357 * The caller is responsible for ensuring that the result will be a valid
1358 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1359 * 'rule' meets 'mf''s prerequisites. */
1361 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1362 unsigned int n_bits, struct cls_rule *rule)
1364 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1365 union mf_value value;
1368 for (i = mf->n_bytes - 1; i >= 0; i--) {
1369 ((uint8_t *) &value)[i] = x;
1372 mf_set_value(mf, &value, rule);
1374 union mf_value value, mask;
1376 unsigned int byte_ofs;
1378 mf_get(mf, rule, &value, &mask);
1380 byte_ofs = mf->n_bytes - ofs / 8;
1381 vp = &((uint8_t *) &value)[byte_ofs];
1382 mp = &((uint8_t *) &mask)[byte_ofs];
1384 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1385 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1387 *--vp &= ~chunk_mask;
1388 *vp |= chunk_mask & (x << (ofs % 8));
1389 *--mp |= chunk_mask;
1395 while (n_bits >= 8) {
1403 uint8_t chunk_mask = (1 << n_bits) - 1;
1405 *--vp &= ~chunk_mask;
1406 *vp |= chunk_mask & x;
1407 *--mp |= chunk_mask;
1410 mf_set(mf, &value, &mask, rule);
1414 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1415 * 'value' and 'mask', respectively. */
1417 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1418 union mf_value *value, union mf_value *mask)
1420 mf_get_value(mf, &rule->flow, value);
1421 mf_get_mask(mf, &rule->wc, mask);
1424 /* Assigns a random value for field 'mf' to 'value'. */
1426 mf_random_value(const struct mf_field *mf, union mf_value *value)
1428 random_bytes(value, mf->n_bytes);
1476 case MFF_IPV6_LABEL:
1477 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1481 value->u8 &= IP_DSCP_MASK;
1485 value->u8 &= IP_ECN_MASK;
1489 value->u8 &= FLOW_NW_FRAG_MASK;
1493 value->be16 &= htons(0xff);
1497 value->be16 &= htons(VLAN_VID_MASK);
1511 mf_from_integer_string(const struct mf_field *mf, const char *s,
1512 uint8_t *valuep, uint8_t *maskp)
1514 unsigned long long int integer, mask;
1519 integer = strtoull(s, &tail, 0);
1520 if (errno || (*tail != '\0' && *tail != '/')) {
1525 mask = strtoull(tail + 1, &tail, 0);
1526 if (errno || *tail != '\0') {
1533 for (i = mf->n_bytes - 1; i >= 0; i--) {
1534 valuep[i] = integer;
1540 return xasprintf("%s: value too large for %u-byte field %s",
1541 s, mf->n_bytes, mf->name);
1546 return xasprintf("%s: bad syntax for %s", s, mf->name);
1550 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1551 uint8_t mac[ETH_ADDR_LEN],
1552 uint8_t mask[ETH_ADDR_LEN])
1554 assert(mf->n_bytes == ETH_ADDR_LEN);
1556 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1557 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1558 case ETH_ADDR_SCAN_COUNT * 2:
1561 case ETH_ADDR_SCAN_COUNT:
1562 memset(mask, 0xff, ETH_ADDR_LEN);
1566 return xasprintf("%s: invalid Ethernet address", s);
1571 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1572 ovs_be32 *ip, ovs_be32 *mask)
1576 assert(mf->n_bytes == sizeof *ip);
1578 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1579 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1581 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1582 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1583 if (prefix <= 0 || prefix > 32) {
1584 return xasprintf("%s: network prefix bits not between 1 and "
1586 } else if (prefix == 32) {
1587 *mask = htonl(UINT32_MAX);
1589 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1591 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1592 *mask = htonl(UINT32_MAX);
1594 return xasprintf("%s: invalid IP address", s);
1600 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1601 struct in6_addr *value, struct in6_addr *mask)
1603 char *str = xstrdup(s);
1604 char *save_ptr = NULL;
1605 const char *name, *netmask;
1608 assert(mf->n_bytes == sizeof *value);
1610 name = strtok_r(str, "/", &save_ptr);
1611 retval = name ? lookup_ipv6(name, value) : EINVAL;
1615 err = xasprintf("%s: could not convert to IPv6 address", str);
1621 netmask = strtok_r(NULL, "/", &save_ptr);
1623 int prefix = atoi(netmask);
1624 if (prefix <= 0 || prefix > 128) {
1626 return xasprintf("%s: prefix bits not between 1 and 128", s);
1628 *mask = ipv6_create_mask(prefix);
1631 *mask = in6addr_exact;
1639 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1640 ovs_be16 *valuep, ovs_be16 *maskp)
1644 assert(mf->n_bytes == sizeof(ovs_be16));
1645 if (ofputil_port_from_string(s, &port)) {
1646 *valuep = htons(port);
1647 *maskp = htons(UINT16_MAX);
1650 return mf_from_integer_string(mf, s,
1651 (uint8_t *) valuep, (uint8_t *) maskp);
1655 struct frag_handling {
1661 static const struct frag_handling all_frags[] = {
1662 #define A FLOW_NW_FRAG_ANY
1663 #define L FLOW_NW_FRAG_LATER
1664 /* name mask value */
1667 { "first", A|L, A },
1668 { "later", A|L, A|L },
1673 { "not_later", L, 0 },
1680 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1682 const struct frag_handling *h;
1684 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1685 if (!strcasecmp(s, h->name)) {
1686 /* We force the upper bits of the mask on to make mf_parse_value()
1687 * happy (otherwise it will never think it's an exact match.) */
1688 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1694 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1695 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1698 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1699 * NULL if successful, otherwise a malloc()'d string describing the error. */
1701 mf_parse(const struct mf_field *mf, const char *s,
1702 union mf_value *value, union mf_value *mask)
1704 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1705 memset(value, 0, mf->n_bytes);
1706 memset(mask, 0, mf->n_bytes);
1710 switch (mf->string) {
1712 case MFS_HEXADECIMAL:
1713 return mf_from_integer_string(mf, s,
1714 (uint8_t *) value, (uint8_t *) mask);
1717 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1720 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1723 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1726 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1729 return mf_from_frag_string(s, &value->u8, &mask->u8);
1734 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1735 * successful, otherwise a malloc()'d string describing the error. */
1737 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1739 union mf_value mask;
1742 error = mf_parse(mf, s, value, &mask);
1747 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1748 return xasprintf("%s: wildcards not allowed here", s);
1754 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1755 const uint8_t *maskp, struct ds *s)
1757 unsigned long long int integer;
1760 assert(mf->n_bytes <= 8);
1763 for (i = 0; i < mf->n_bytes; i++) {
1764 integer = (integer << 8) | valuep[i];
1766 if (mf->string == MFS_HEXADECIMAL) {
1767 ds_put_format(s, "%#llx", integer);
1769 ds_put_format(s, "%lld", integer);
1773 unsigned long long int mask;
1776 for (i = 0; i < mf->n_bytes; i++) {
1777 mask = (mask << 8) | maskp[i];
1780 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1781 * not sure that that a bit-mask written in decimal is ever easier to
1782 * understand than the same bit-mask written in hexadecimal. */
1783 ds_put_format(s, "/%#llx", mask);
1788 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1791 const struct frag_handling *h;
1792 uint8_t value = *valuep;
1793 uint8_t mask = *maskp;
1796 mask &= FLOW_NW_FRAG_MASK;
1798 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1799 if (value == h->value && mask == h->mask) {
1800 ds_put_cstr(s, h->name);
1804 ds_put_cstr(s, "<error>");
1807 /* Appends to 's' a string representation of field 'mf' whose value is in
1808 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1810 mf_format(const struct mf_field *mf,
1811 const union mf_value *value, const union mf_value *mask,
1815 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1816 ds_put_cstr(s, "ANY");
1818 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1823 switch (mf->string) {
1826 ofputil_format_port(ntohs(value->be16), s);
1831 case MFS_HEXADECIMAL:
1832 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1836 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1838 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1843 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1848 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1852 mf_format_frag_string(&value->u8, &mask->u8, s);