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,
193 MFM_NONE, FWW_NW_DSCP,
198 MFF_IP_ECN, "nw_ecn", NULL,
200 MFM_NONE, FWW_NW_ECN,
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_ICMPV4_TYPE, "icmp_type", NULL,
296 MFM_NONE, FWW_TP_SRC,
301 MFF_ICMPV4_CODE, "icmp_code", NULL,
303 MFM_NONE, FWW_TP_DST,
308 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
310 MFM_NONE, FWW_TP_SRC,
315 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
317 MFM_NONE, FWW_TP_DST,
328 MFF_ND_TARGET, "nd_target", NULL,
329 MF_FIELD_SIZES(ipv6),
330 MFM_NONE, FWW_ND_TARGET,
335 MFF_ND_SLL, "nd_sll", NULL,
337 MFM_NONE, FWW_ARP_SHA,
342 MFF_ND_TLL, "nd_tll", NULL,
344 MFM_NONE, FWW_ARP_THA,
351 /* Returns the field with the given 'id'. */
352 const struct mf_field *
353 mf_from_id(enum mf_field_id id)
355 assert((unsigned int) id < MFF_N_IDS);
356 return &mf_fields[id];
359 /* Returns the field with the given 'name', or a null pointer if no field has
361 const struct mf_field *
362 mf_from_name(const char *name)
364 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
366 if (shash_is_empty(&mf_by_name)) {
367 const struct mf_field *mf;
369 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
370 shash_add_once(&mf_by_name, mf->name, mf);
371 if (mf->extra_name) {
372 shash_add_once(&mf_by_name, mf->extra_name, mf);
377 return shash_find_data(&mf_by_name, name);
380 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
381 * specifies at least one bit in the field.
383 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
384 * meets 'mf''s prerequisites. */
386 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
404 case MFF_ICMPV4_TYPE:
405 case MFF_ICMPV4_CODE:
406 case MFF_ICMPV6_TYPE:
407 case MFF_ICMPV6_CODE:
411 assert(mf->fww_bit != 0);
412 return (wc->wildcards & mf->fww_bit) != 0;
415 return !wc->tun_id_mask;
435 return !wc->reg_masks[mf->id - MFF_REG0];
438 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
439 == (FWW_ETH_MCAST | FWW_DL_DST));
442 return !wc->vlan_tci_mask;
444 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
446 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
449 return !wc->nw_src_mask;
451 return !wc->nw_dst_mask;
454 return ipv6_mask_is_any(&wc->ipv6_src_mask);
456 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
459 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
462 return !wc->nw_src_mask;
464 return !wc->nw_dst_mask;
472 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
473 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
474 * purposes, or to 0 if it is wildcarded.
476 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
477 * meets 'mf''s prerequisites. */
479 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
480 union mf_value *mask)
498 case MFF_ICMPV4_TYPE:
499 case MFF_ICMPV4_CODE:
500 case MFF_ICMPV6_TYPE:
501 case MFF_ICMPV6_CODE:
505 assert(mf->fww_bit != 0);
506 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
510 mask->be64 = wc->tun_id_mask;
531 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
535 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
540 mask->be16 = wc->vlan_tci_mask;
543 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
546 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
550 mask->be32 = wc->nw_src_mask;
553 mask->be32 = wc->nw_dst_mask;
557 mask->ipv6 = wc->ipv6_src_mask;
560 mask->ipv6 = wc->ipv6_dst_mask;
564 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
568 mask->be32 = wc->nw_src_mask;
571 mask->be32 = wc->nw_dst_mask;
580 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
581 * if the mask is valid, false otherwise. */
583 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
585 switch (mf->maskable) {
587 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
588 is_all_ones((const uint8_t *) mask, mf->n_bytes));
594 return (mf->n_bytes == 4
595 ? ip_is_cidr(mask->be32)
596 : ipv6_is_cidr(&mask->ipv6));
599 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
606 is_ip_any(const struct flow *flow)
608 return (flow->dl_type == htons(ETH_TYPE_IP) ||
609 flow->dl_type == htons(ETH_TYPE_IPV6));
613 is_icmpv4(const struct flow *flow)
615 return (flow->dl_type == htons(ETH_TYPE_IP)
616 && flow->nw_proto == IPPROTO_ICMP);
620 is_icmpv6(const struct flow *flow)
622 return (flow->dl_type == htons(ETH_TYPE_IPV6)
623 && flow->nw_proto == IPPROTO_ICMPV6);
626 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
628 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
630 switch (mf->prereqs) {
635 return flow->dl_type == htons(ETH_TYPE_ARP);
637 return flow->dl_type == htons(ETH_TYPE_IP);
639 return flow->dl_type == htons(ETH_TYPE_IPV6);
641 return is_ip_any(flow);
644 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
646 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
648 return is_icmpv4(flow);
650 return is_icmpv6(flow);
653 return (is_icmpv6(flow)
654 && flow->tp_dst == htons(0)
655 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
656 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
658 return (is_icmpv6(flow)
659 && flow->tp_dst == htons(0)
660 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
662 return (is_icmpv6(flow)
663 && flow->tp_dst == htons(0)
664 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
670 /* Returns true if 'value' may be a valid value *as part of a masked match*,
673 * A value is not rejected just because it is not valid for the field in
674 * question, but only if it doesn't make sense to test the bits in question at
675 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
676 * without the VLAN_CFI bit being set, but we can't reject those values because
677 * it is still legitimate to test just for those bits (see the documentation
678 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
679 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
681 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
722 case MFF_ICMPV4_TYPE:
723 case MFF_ICMPV4_CODE:
724 case MFF_ICMPV6_TYPE:
725 case MFF_ICMPV6_CODE:
732 return !(value->u8 & ~IP_DSCP_MASK);
734 return !(value->u8 & ~IP_ECN_MASK);
736 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
739 return !(value->be16 & htons(0xff00));
742 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
745 return !(value->u8 & ~7);
748 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
756 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
757 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
759 mf_get_value(const struct mf_field *mf, const struct flow *flow,
760 union mf_value *value)
764 value->be64 = flow->tun_id;
768 value->be16 = htons(flow->in_port);
789 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
793 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
797 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
801 value->be16 = flow->dl_type;
805 value->be16 = flow->vlan_tci;
809 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
813 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
817 value->be32 = flow->nw_src;
821 value->be32 = flow->nw_dst;
825 value->ipv6 = flow->ipv6_src;
829 value->ipv6 = flow->ipv6_dst;
833 value->be32 = flow->ipv6_label;
837 value->u8 = flow->nw_proto;
841 value->u8 = flow->nw_tos & IP_DSCP_MASK;
845 value->u8 = flow->nw_tos & IP_ECN_MASK;
849 value->u8 = flow->nw_ttl;
853 value->u8 = flow->nw_frag;
857 value->be16 = htons(flow->nw_proto);
861 value->be32 = flow->nw_src;
865 value->be32 = flow->nw_dst;
870 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
875 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
879 value->be16 = flow->tp_src;
883 value->be16 = flow->tp_dst;
887 value->be16 = flow->tp_src;
891 value->be16 = flow->tp_dst;
894 case MFF_ICMPV4_TYPE:
895 case MFF_ICMPV6_TYPE:
896 value->u8 = ntohs(flow->tp_src);
899 case MFF_ICMPV4_CODE:
900 case MFF_ICMPV6_CODE:
901 value->u8 = ntohs(flow->tp_dst);
905 value->ipv6 = flow->nd_target;
914 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
915 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
918 mf_set_value(const struct mf_field *mf,
919 const union mf_value *value, struct cls_rule *rule)
923 cls_rule_set_tun_id(rule, value->be64);
927 cls_rule_set_in_port(rule, ntohs(value->be16));
949 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
954 cls_rule_set_dl_src(rule, value->mac);
958 cls_rule_set_dl_dst(rule, value->mac);
962 cls_rule_set_dl_type(rule, value->be16);
966 cls_rule_set_dl_tci(rule, value->be16);
970 cls_rule_set_dl_vlan(rule, value->be16);
974 cls_rule_set_dl_vlan_pcp(rule, value->u8);
978 cls_rule_set_nw_src(rule, value->be32);
982 cls_rule_set_nw_dst(rule, value->be32);
986 cls_rule_set_ipv6_src(rule, &value->ipv6);
990 cls_rule_set_ipv6_dst(rule, &value->ipv6);
994 cls_rule_set_ipv6_label(rule, value->be32);
998 cls_rule_set_nw_proto(rule, value->u8);
1002 cls_rule_set_nw_dscp(rule, value->u8);
1006 cls_rule_set_nw_ecn(rule, value->u8);
1010 cls_rule_set_nw_ttl(rule, value->u8);
1014 cls_rule_set_nw_frag(rule, value->u8);
1018 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1022 cls_rule_set_nw_src(rule, value->be32);
1026 cls_rule_set_nw_dst(rule, value->be32);
1031 cls_rule_set_arp_sha(rule, value->mac);
1036 cls_rule_set_arp_tha(rule, value->mac);
1040 cls_rule_set_tp_src(rule, value->be16);
1044 cls_rule_set_tp_dst(rule, value->be16);
1048 cls_rule_set_tp_src(rule, value->be16);
1052 cls_rule_set_tp_dst(rule, value->be16);
1055 case MFF_ICMPV4_TYPE:
1056 case MFF_ICMPV6_TYPE:
1057 cls_rule_set_icmp_type(rule, value->u8);
1060 case MFF_ICMPV4_CODE:
1061 case MFF_ICMPV6_CODE:
1062 cls_rule_set_icmp_code(rule, value->u8);
1066 cls_rule_set_nd_target(rule, &value->ipv6);
1075 /* Makes 'rule' wildcard field 'mf'.
1077 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1080 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1084 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1088 rule->wc.wildcards |= FWW_IN_PORT;
1089 rule->flow.in_port = 0;
1094 cls_rule_set_reg_masked(rule, 0, 0, 0);
1099 cls_rule_set_reg_masked(rule, 1, 0, 0);
1104 cls_rule_set_reg_masked(rule, 2, 0, 0);
1109 cls_rule_set_reg_masked(rule, 3, 0, 0);
1114 cls_rule_set_reg_masked(rule, 4, 0, 0);
1122 rule->wc.wildcards |= FWW_DL_SRC;
1123 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1127 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1128 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1132 rule->wc.wildcards |= FWW_DL_TYPE;
1133 rule->flow.dl_type = htons(0);
1137 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1141 cls_rule_set_any_vid(rule);
1145 cls_rule_set_any_pcp(rule);
1150 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1155 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1159 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1160 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1164 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1165 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1168 case MFF_IPV6_LABEL:
1169 rule->wc.wildcards |= FWW_IPV6_LABEL;
1170 rule->flow.ipv6_label = 0;
1174 rule->wc.wildcards |= FWW_NW_PROTO;
1175 rule->flow.nw_proto = 0;
1179 rule->wc.wildcards |= FWW_NW_DSCP;
1180 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1184 rule->wc.wildcards |= FWW_NW_ECN;
1185 rule->flow.nw_tos &= ~IP_ECN_MASK;
1189 rule->wc.wildcards |= FWW_NW_TTL;
1190 rule->flow.nw_ttl = 0;
1194 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1195 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1199 rule->wc.wildcards |= FWW_NW_PROTO;
1200 rule->flow.nw_proto = 0;
1205 rule->wc.wildcards |= FWW_ARP_SHA;
1206 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1211 rule->wc.wildcards |= FWW_ARP_THA;
1212 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1217 case MFF_ICMPV4_TYPE:
1218 case MFF_ICMPV6_TYPE:
1219 rule->wc.wildcards |= FWW_TP_SRC;
1220 rule->flow.tp_src = htons(0);
1225 case MFF_ICMPV4_CODE:
1226 case MFF_ICMPV6_CODE:
1227 rule->wc.wildcards |= FWW_TP_DST;
1228 rule->flow.tp_dst = htons(0);
1232 rule->wc.wildcards |= FWW_ND_TARGET;
1233 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1242 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1243 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1244 * with a 1-bit indicating that the corresponding value bit must match and a
1245 * 0-bit indicating a don't-care.
1247 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1248 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1249 * call is equivalent to mf_set_wild(mf, rule).
1251 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1252 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1254 mf_set(const struct mf_field *mf,
1255 const union mf_value *value, const union mf_value *mask,
1256 struct cls_rule *rule)
1258 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1259 mf_set_value(mf, value, rule);
1261 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1262 mf_set_wild(mf, rule);
1272 case MFF_IPV6_LABEL:
1284 case MFF_ICMPV4_TYPE:
1285 case MFF_ICMPV4_CODE:
1286 case MFF_ICMPV6_TYPE:
1287 case MFF_ICMPV6_CODE:
1294 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1315 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1316 ntohl(value->be32), ntohl(mask->be32));
1320 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1321 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1326 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1330 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1334 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1338 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1342 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1346 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1350 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1354 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1363 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1364 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1367 * Example: suppose that 'mf' is originally the following 2-byte field in
1370 * value == 0xe00a == 2#1110000000001010
1371 * mask == 0xfc3f == 2#1111110000111111
1373 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1374 * effect (note that 0x55 is 2#1010101):
1376 * value == 0xd50a == 2#1101010100001010
1377 * mask == 0xff3f == 2#1111111100111111
1379 * The caller is responsible for ensuring that the result will be a valid
1380 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1381 * 'rule' meets 'mf''s prerequisites. */
1383 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1384 unsigned int n_bits, struct cls_rule *rule)
1386 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1387 union mf_value value;
1390 for (i = mf->n_bytes - 1; i >= 0; i--) {
1391 ((uint8_t *) &value)[i] = x;
1394 mf_set_value(mf, &value, rule);
1396 union mf_value value, mask;
1398 unsigned int byte_ofs;
1400 mf_get(mf, rule, &value, &mask);
1402 byte_ofs = mf->n_bytes - ofs / 8;
1403 vp = &((uint8_t *) &value)[byte_ofs];
1404 mp = &((uint8_t *) &mask)[byte_ofs];
1406 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1407 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1409 *--vp &= ~chunk_mask;
1410 *vp |= chunk_mask & (x << (ofs % 8));
1411 *--mp |= chunk_mask;
1417 while (n_bits >= 8) {
1425 uint8_t chunk_mask = (1 << n_bits) - 1;
1427 *--vp &= ~chunk_mask;
1428 *vp |= chunk_mask & x;
1429 *--mp |= chunk_mask;
1432 mf_set(mf, &value, &mask, rule);
1436 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1437 * 'value' and 'mask', respectively. */
1439 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1440 union mf_value *value, union mf_value *mask)
1442 mf_get_value(mf, &rule->flow, value);
1443 mf_get_mask(mf, &rule->wc, mask);
1446 /* Assigns a random value for field 'mf' to 'value'. */
1448 mf_random_value(const struct mf_field *mf, union mf_value *value)
1450 random_bytes(value, mf->n_bytes);
1491 case MFF_ICMPV4_TYPE:
1492 case MFF_ICMPV4_CODE:
1493 case MFF_ICMPV6_TYPE:
1494 case MFF_ICMPV6_CODE:
1500 case MFF_IPV6_LABEL:
1501 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1505 value->u8 &= IP_DSCP_MASK;
1509 value->u8 &= IP_ECN_MASK;
1513 value->u8 &= FLOW_NW_FRAG_MASK;
1517 value->be16 &= htons(0xff);
1521 value->be16 &= htons(VLAN_VID_MASK);
1535 mf_from_integer_string(const struct mf_field *mf, const char *s,
1536 uint8_t *valuep, uint8_t *maskp)
1538 unsigned long long int integer, mask;
1543 integer = strtoull(s, &tail, 0);
1544 if (errno || (*tail != '\0' && *tail != '/')) {
1549 mask = strtoull(tail + 1, &tail, 0);
1550 if (errno || *tail != '\0') {
1557 for (i = mf->n_bytes - 1; i >= 0; i--) {
1558 valuep[i] = integer;
1564 return xasprintf("%s: value too large for %u-byte field %s",
1565 s, mf->n_bytes, mf->name);
1570 return xasprintf("%s: bad syntax for %s", s, mf->name);
1574 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1575 uint8_t mac[ETH_ADDR_LEN],
1576 uint8_t mask[ETH_ADDR_LEN])
1578 assert(mf->n_bytes == ETH_ADDR_LEN);
1580 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1581 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1582 case ETH_ADDR_SCAN_COUNT * 2:
1585 case ETH_ADDR_SCAN_COUNT:
1586 memset(mask, 0xff, ETH_ADDR_LEN);
1590 return xasprintf("%s: invalid Ethernet address", s);
1595 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1596 ovs_be32 *ip, ovs_be32 *mask)
1600 assert(mf->n_bytes == sizeof *ip);
1602 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1603 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1605 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1606 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1607 if (prefix <= 0 || prefix > 32) {
1608 return xasprintf("%s: network prefix bits not between 1 and "
1610 } else if (prefix == 32) {
1611 *mask = htonl(UINT32_MAX);
1613 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1615 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1616 *mask = htonl(UINT32_MAX);
1618 return xasprintf("%s: invalid IP address", s);
1624 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1625 struct in6_addr *value, struct in6_addr *mask)
1627 char *str = xstrdup(s);
1628 char *save_ptr = NULL;
1629 const char *name, *netmask;
1632 assert(mf->n_bytes == sizeof *value);
1634 name = strtok_r(str, "/", &save_ptr);
1635 retval = name ? lookup_ipv6(name, value) : EINVAL;
1639 err = xasprintf("%s: could not convert to IPv6 address", str);
1645 netmask = strtok_r(NULL, "/", &save_ptr);
1647 int prefix = atoi(netmask);
1648 if (prefix <= 0 || prefix > 128) {
1650 return xasprintf("%s: prefix bits not between 1 and 128", s);
1652 *mask = ipv6_create_mask(prefix);
1655 *mask = in6addr_exact;
1663 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1664 ovs_be16 *valuep, ovs_be16 *maskp)
1668 assert(mf->n_bytes == sizeof(ovs_be16));
1669 if (ofputil_port_from_string(s, &port)) {
1670 *valuep = htons(port);
1671 *maskp = htons(UINT16_MAX);
1674 return mf_from_integer_string(mf, s,
1675 (uint8_t *) valuep, (uint8_t *) maskp);
1679 struct frag_handling {
1685 static const struct frag_handling all_frags[] = {
1686 #define A FLOW_NW_FRAG_ANY
1687 #define L FLOW_NW_FRAG_LATER
1688 /* name mask value */
1691 { "first", A|L, A },
1692 { "later", A|L, A|L },
1697 { "not_later", L, 0 },
1704 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1706 const struct frag_handling *h;
1708 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1709 if (!strcasecmp(s, h->name)) {
1710 /* We force the upper bits of the mask on to make mf_parse_value()
1711 * happy (otherwise it will never think it's an exact match.) */
1712 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1718 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1719 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1722 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1723 * NULL if successful, otherwise a malloc()'d string describing the error. */
1725 mf_parse(const struct mf_field *mf, const char *s,
1726 union mf_value *value, union mf_value *mask)
1728 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1729 memset(value, 0, mf->n_bytes);
1730 memset(mask, 0, mf->n_bytes);
1734 switch (mf->string) {
1736 case MFS_HEXADECIMAL:
1737 return mf_from_integer_string(mf, s,
1738 (uint8_t *) value, (uint8_t *) mask);
1741 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1744 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1747 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1750 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1753 return mf_from_frag_string(s, &value->u8, &mask->u8);
1758 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1759 * successful, otherwise a malloc()'d string describing the error. */
1761 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1763 union mf_value mask;
1766 error = mf_parse(mf, s, value, &mask);
1771 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1772 return xasprintf("%s: wildcards not allowed here", s);
1778 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1779 const uint8_t *maskp, struct ds *s)
1781 unsigned long long int integer;
1784 assert(mf->n_bytes <= 8);
1787 for (i = 0; i < mf->n_bytes; i++) {
1788 integer = (integer << 8) | valuep[i];
1790 if (mf->string == MFS_HEXADECIMAL) {
1791 ds_put_format(s, "%#llx", integer);
1793 ds_put_format(s, "%lld", integer);
1797 unsigned long long int mask;
1800 for (i = 0; i < mf->n_bytes; i++) {
1801 mask = (mask << 8) | maskp[i];
1804 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1805 * not sure that that a bit-mask written in decimal is ever easier to
1806 * understand than the same bit-mask written in hexadecimal. */
1807 ds_put_format(s, "/%#llx", mask);
1812 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1815 const struct frag_handling *h;
1816 uint8_t value = *valuep;
1817 uint8_t mask = *maskp;
1820 mask &= FLOW_NW_FRAG_MASK;
1822 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1823 if (value == h->value && mask == h->mask) {
1824 ds_put_cstr(s, h->name);
1828 ds_put_cstr(s, "<error>");
1831 /* Appends to 's' a string representation of field 'mf' whose value is in
1832 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1834 mf_format(const struct mf_field *mf,
1835 const union mf_value *value, const union mf_value *mask,
1839 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1840 ds_put_cstr(s, "ANY");
1842 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1847 switch (mf->string) {
1850 ofputil_format_port(ntohs(value->be16), s);
1855 case MFS_HEXADECIMAL:
1856 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1860 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1862 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1867 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1872 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1876 mf_format_frag_string(&value->u8, &mask->u8, s);