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_FRAG, "ip_frag", NULL,
214 MFF_ARP_OP, "arp_op", NULL,
215 MF_FIELD_SIZES(be16),
216 MFM_NONE, FWW_NW_PROTO,
221 MFF_ARP_SPA, "arp_spa", NULL,
222 MF_FIELD_SIZES(be32),
228 MFF_ARP_TPA, "arp_tpa", NULL,
229 MF_FIELD_SIZES(be32),
235 MFF_ARP_SHA, "arp_sha", NULL,
237 MFM_NONE, FWW_ARP_SHA,
242 MFF_ARP_THA, "arp_tha", NULL,
244 MFM_NONE, FWW_ARP_THA,
255 MFF_TCP_SRC, "tcp_src", "tp_src",
256 MF_FIELD_SIZES(be16),
257 MFM_NONE, FWW_TP_SRC,
262 MFF_TCP_DST, "tcp_dst", "tp_dst",
263 MF_FIELD_SIZES(be16),
264 MFM_NONE, FWW_TP_DST,
271 MFF_UDP_SRC, "udp_src", NULL,
272 MF_FIELD_SIZES(be16),
273 MFM_NONE, FWW_TP_SRC,
278 MFF_UDP_DST, "udp_dst", NULL,
279 MF_FIELD_SIZES(be16),
280 MFM_NONE, FWW_TP_DST,
287 MFF_ICMP_TYPE, "icmp_type", NULL,
289 MFM_NONE, FWW_TP_SRC,
294 MFF_ICMP_CODE, "icmp_code", NULL,
296 MFM_NONE, FWW_TP_DST,
307 MFF_ND_TARGET, "nd_target", NULL,
308 MF_FIELD_SIZES(ipv6),
309 MFM_NONE, FWW_ND_TARGET,
314 MFF_ND_SLL, "nd_sll", NULL,
316 MFM_NONE, FWW_ARP_SHA,
321 MFF_ND_TLL, "nd_tll", NULL,
323 MFM_NONE, FWW_ARP_THA,
330 /* Returns the field with the given 'id'. */
331 const struct mf_field *
332 mf_from_id(enum mf_field_id id)
334 assert((unsigned int) id < MFF_N_IDS);
335 return &mf_fields[id];
338 /* Returns the field with the given 'name', or a null pointer if no field has
340 const struct mf_field *
341 mf_from_name(const char *name)
343 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
345 if (shash_is_empty(&mf_by_name)) {
346 const struct mf_field *mf;
348 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
349 shash_add_once(&mf_by_name, mf->name, mf);
350 if (mf->extra_name) {
351 shash_add_once(&mf_by_name, mf->extra_name, mf);
356 return shash_find_data(&mf_by_name, name);
359 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
360 * specifies at least one bit in the field.
362 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
363 * meets 'mf''s prerequisites. */
365 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
385 assert(mf->fww_bit != 0);
386 return (wc->wildcards & mf->fww_bit) != 0;
389 return !wc->tun_id_mask;
409 return !wc->reg_masks[mf->id - MFF_REG0];
412 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
413 == (FWW_ETH_MCAST | FWW_DL_DST));
416 return !wc->vlan_tci_mask;
418 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
420 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
423 return !wc->nw_src_mask;
425 return !wc->nw_dst_mask;
428 return ipv6_mask_is_any(&wc->ipv6_src_mask);
430 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
433 return !(wc->tos_mask & IP_DSCP_MASK);
435 return !(wc->tos_mask & IP_ECN_MASK);
437 return !(wc->frag_mask & FLOW_FRAG_MASK);
440 return !wc->nw_src_mask;
442 return !wc->nw_dst_mask;
450 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
451 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
452 * purposes, or to 0 if it is wildcarded.
454 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
455 * meets 'mf''s prerequisites. */
457 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
458 union mf_value *mask)
478 assert(mf->fww_bit != 0);
479 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
483 mask->be64 = wc->tun_id_mask;
504 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
508 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
513 mask->be16 = wc->vlan_tci_mask;
516 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
519 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
523 mask->be32 = wc->nw_src_mask;
526 mask->be32 = wc->nw_dst_mask;
530 mask->ipv6 = wc->ipv6_src_mask;
533 mask->ipv6 = wc->ipv6_dst_mask;
537 mask->u8 = wc->tos_mask & IP_DSCP_MASK;
540 mask->u8 = wc->tos_mask & IP_ECN_MASK;
543 mask->u8 = wc->frag_mask & FLOW_FRAG_MASK;
547 mask->be32 = wc->nw_src_mask;
550 mask->be32 = wc->nw_dst_mask;
559 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
560 * if the mask is valid, false otherwise. */
562 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
564 switch (mf->maskable) {
566 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
567 is_all_ones((const uint8_t *) mask, mf->n_bytes));
573 return (mf->n_bytes == 4
574 ? ip_is_cidr(mask->be32)
575 : ipv6_is_cidr(&mask->ipv6));
578 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
585 is_ip_any(const struct flow *flow)
587 return (flow->dl_type == htons(ETH_TYPE_IP) ||
588 flow->dl_type == htons(ETH_TYPE_IPV6));
592 is_icmpv4(const struct flow *flow)
594 return (flow->dl_type == htons(ETH_TYPE_IP)
595 && flow->nw_proto == IPPROTO_ICMP);
599 is_icmpv6(const struct flow *flow)
601 return (flow->dl_type == htons(ETH_TYPE_IPV6)
602 && flow->nw_proto == IPPROTO_ICMPV6);
605 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
607 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
609 switch (mf->prereqs) {
614 return flow->dl_type == htons(ETH_TYPE_ARP);
616 return flow->dl_type == htons(ETH_TYPE_IP);
618 return flow->dl_type == htons(ETH_TYPE_IPV6);
620 return is_ip_any(flow);
623 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
625 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
627 return is_icmpv6(flow);
629 return is_icmpv4(flow) || is_icmpv6(flow);
632 return (is_icmpv6(flow)
633 && flow->tp_dst == htons(0)
634 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
635 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
637 return (is_icmpv6(flow)
638 && flow->tp_dst == htons(0)
639 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
641 return (is_icmpv6(flow)
642 && flow->tp_dst == htons(0)
643 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
649 /* Returns true if 'value' may be a valid value *as part of a masked match*,
652 * A value is not rejected just because it is not valid for the field in
653 * question, but only if it doesn't make sense to test the bits in question at
654 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
655 * without the VLAN_CFI bit being set, but we can't reject those values because
656 * it is still legitimate to test just for those bits (see the documentation
657 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
658 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
660 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
708 return !(value->u8 & ~IP_DSCP_MASK);
710 return !(value->u8 & ~IP_ECN_MASK);
712 return !(value->u8 & ~FLOW_FRAG_MASK);
715 return !(value->be16 & htons(0xff00));
718 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
721 return !(value->u8 & ~7);
724 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
732 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
733 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
735 mf_get_value(const struct mf_field *mf, const struct flow *flow,
736 union mf_value *value)
740 value->be64 = flow->tun_id;
744 value->be16 = htons(flow->in_port);
765 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
769 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
773 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
777 value->be16 = flow->dl_type;
781 value->be16 = flow->vlan_tci;
785 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
789 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
793 value->be32 = flow->nw_src;
797 value->be32 = flow->nw_dst;
801 value->ipv6 = flow->ipv6_src;
805 value->ipv6 = flow->ipv6_dst;
809 value->be32 = flow->ipv6_label;
813 value->u8 = flow->nw_proto;
817 value->u8 = flow->tos & IP_DSCP_MASK;
821 value->u8 = flow->tos & IP_ECN_MASK;
825 value->u8 = flow->frag;
829 value->be16 = htons(flow->nw_proto);
833 value->be32 = flow->nw_src;
837 value->be32 = flow->nw_dst;
842 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
847 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
851 value->be16 = flow->tp_src;
855 value->be16 = flow->tp_dst;
859 value->be16 = flow->tp_src;
863 value->be16 = flow->tp_dst;
867 value->u8 = ntohs(flow->tp_src);
871 value->u8 = ntohs(flow->tp_dst);
875 value->ipv6 = flow->nd_target;
884 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
885 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
888 mf_set_value(const struct mf_field *mf,
889 const union mf_value *value, struct cls_rule *rule)
893 cls_rule_set_tun_id(rule, value->be64);
897 cls_rule_set_in_port(rule, ntohs(value->be16));
919 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
924 cls_rule_set_dl_src(rule, value->mac);
928 cls_rule_set_dl_dst(rule, value->mac);
932 cls_rule_set_dl_type(rule, value->be16);
936 cls_rule_set_dl_tci(rule, value->be16);
940 cls_rule_set_dl_vlan(rule, value->be16);
944 cls_rule_set_dl_vlan_pcp(rule, value->u8);
948 cls_rule_set_nw_src(rule, value->be32);
952 cls_rule_set_nw_dst(rule, value->be32);
956 cls_rule_set_ipv6_src(rule, &value->ipv6);
960 cls_rule_set_ipv6_dst(rule, &value->ipv6);
964 cls_rule_set_ipv6_label(rule, value->be32);
968 cls_rule_set_nw_proto(rule, value->u8);
972 cls_rule_set_nw_dscp(rule, value->u8);
976 cls_rule_set_nw_ecn(rule, value->u8);
980 cls_rule_set_frag(rule, value->u8);
984 cls_rule_set_nw_proto(rule, ntohs(value->be16));
988 cls_rule_set_nw_src(rule, value->be32);
992 cls_rule_set_nw_dst(rule, value->be32);
997 cls_rule_set_arp_sha(rule, value->mac);
1002 cls_rule_set_arp_tha(rule, value->mac);
1006 cls_rule_set_tp_src(rule, value->be16);
1010 cls_rule_set_tp_dst(rule, value->be16);
1014 cls_rule_set_tp_src(rule, value->be16);
1018 cls_rule_set_tp_dst(rule, value->be16);
1022 cls_rule_set_icmp_type(rule, value->u8);
1026 cls_rule_set_icmp_code(rule, value->u8);
1030 cls_rule_set_nd_target(rule, &value->ipv6);
1039 /* Makes 'rule' wildcard field 'mf'.
1041 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1044 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1048 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1052 rule->wc.wildcards |= FWW_IN_PORT;
1053 rule->flow.in_port = 0;
1058 cls_rule_set_reg_masked(rule, 0, 0, 0);
1063 cls_rule_set_reg_masked(rule, 1, 0, 0);
1068 cls_rule_set_reg_masked(rule, 2, 0, 0);
1073 cls_rule_set_reg_masked(rule, 3, 0, 0);
1078 cls_rule_set_reg_masked(rule, 4, 0, 0);
1086 rule->wc.wildcards |= FWW_DL_SRC;
1087 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1091 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1092 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1096 rule->wc.wildcards |= FWW_DL_TYPE;
1097 rule->flow.dl_type = htons(0);
1101 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1105 cls_rule_set_any_vid(rule);
1109 cls_rule_set_any_pcp(rule);
1114 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1119 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1123 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1124 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1128 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1129 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1132 case MFF_IPV6_LABEL:
1133 rule->wc.wildcards |= FWW_IPV6_LABEL;
1134 rule->flow.ipv6_label = 0;
1138 rule->wc.wildcards |= FWW_NW_PROTO;
1139 rule->flow.nw_proto = 0;
1143 rule->wc.tos_mask |= IP_DSCP_MASK;
1144 rule->flow.tos &= ~IP_DSCP_MASK;
1148 rule->wc.tos_mask |= IP_ECN_MASK;
1149 rule->flow.tos &= ~IP_ECN_MASK;
1153 rule->wc.frag_mask |= FLOW_FRAG_MASK;
1154 rule->flow.frag &= ~FLOW_FRAG_MASK;
1158 rule->wc.wildcards |= FWW_NW_PROTO;
1159 rule->flow.nw_proto = 0;
1164 rule->wc.wildcards |= FWW_ARP_SHA;
1165 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1170 rule->wc.wildcards |= FWW_ARP_THA;
1171 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1177 rule->wc.wildcards |= FWW_TP_SRC;
1178 rule->flow.tp_src = htons(0);
1184 rule->wc.wildcards |= FWW_TP_DST;
1185 rule->flow.tp_dst = htons(0);
1189 rule->wc.wildcards |= FWW_ND_TARGET;
1190 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1199 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1200 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1201 * with a 1-bit indicating that the corresponding value bit must match and a
1202 * 0-bit indicating a don't-care.
1204 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1205 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1206 * call is equivalent to mf_set_wild(mf, rule).
1208 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1209 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1211 mf_set(const struct mf_field *mf,
1212 const union mf_value *value, const union mf_value *mask,
1213 struct cls_rule *rule)
1215 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1216 mf_set_value(mf, value, rule);
1218 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1219 mf_set_wild(mf, rule);
1229 case MFF_IPV6_LABEL:
1248 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1269 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1270 ntohl(value->be32), ntohl(mask->be32));
1274 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1275 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1280 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1284 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1288 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1292 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1296 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1300 cls_rule_set_frag_masked(rule, value->u8, mask->u8);
1304 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1308 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1317 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1318 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1321 * Example: suppose that 'mf' is originally the following 2-byte field in
1324 * value == 0xe00a == 2#1110000000001010
1325 * mask == 0xfc3f == 2#1111110000111111
1327 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1328 * effect (note that 0x55 is 2#1010101):
1330 * value == 0xd50a == 2#1101010100001010
1331 * mask == 0xff3f == 2#1111111100111111
1333 * The caller is responsible for ensuring that the result will be a valid
1334 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1335 * 'rule' meets 'mf''s prerequisites. */
1337 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1338 unsigned int n_bits, struct cls_rule *rule)
1340 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1341 union mf_value value;
1344 for (i = mf->n_bytes - 1; i >= 0; i--) {
1345 ((uint8_t *) &value)[i] = x;
1348 mf_set_value(mf, &value, rule);
1350 union mf_value value, mask;
1352 unsigned int byte_ofs;
1354 mf_get(mf, rule, &value, &mask);
1356 byte_ofs = mf->n_bytes - ofs / 8;
1357 vp = &((uint8_t *) &value)[byte_ofs];
1358 mp = &((uint8_t *) &mask)[byte_ofs];
1360 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1361 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1363 *--vp &= ~chunk_mask;
1364 *vp |= chunk_mask & (x << (ofs % 8));
1365 *--mp |= chunk_mask;
1371 while (n_bits >= 8) {
1379 uint8_t chunk_mask = (1 << n_bits) - 1;
1381 *--vp &= ~chunk_mask;
1382 *vp |= chunk_mask & x;
1383 *--mp |= chunk_mask;
1386 mf_set(mf, &value, &mask, rule);
1390 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1391 * 'value' and 'mask', respectively. */
1393 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1394 union mf_value *value, union mf_value *mask)
1396 mf_get_value(mf, &rule->flow, value);
1397 mf_get_mask(mf, &rule->wc, mask);
1400 /* Assigns a random value for field 'mf' to 'value'. */
1402 mf_random_value(const struct mf_field *mf, union mf_value *value)
1404 random_bytes(value, mf->n_bytes);
1451 case MFF_IPV6_LABEL:
1452 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1456 value->u8 &= IP_DSCP_MASK;
1460 value->u8 &= IP_ECN_MASK;
1464 value->u8 &= FLOW_FRAG_MASK;
1468 value->be16 &= htons(0xff);
1472 value->be16 &= htons(VLAN_VID_MASK);
1486 mf_from_integer_string(const struct mf_field *mf, const char *s,
1487 uint8_t *valuep, uint8_t *maskp)
1489 unsigned long long int integer, mask;
1494 integer = strtoull(s, &tail, 0);
1495 if (errno || (*tail != '\0' && *tail != '/')) {
1500 mask = strtoull(tail + 1, &tail, 0);
1501 if (errno || *tail != '\0') {
1508 for (i = mf->n_bytes - 1; i >= 0; i--) {
1509 valuep[i] = integer;
1515 return xasprintf("%s: value too large for %u-byte field %s",
1516 s, mf->n_bytes, mf->name);
1521 return xasprintf("%s: bad syntax for %s", s, mf->name);
1525 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1526 uint8_t mac[ETH_ADDR_LEN],
1527 uint8_t mask[ETH_ADDR_LEN])
1529 assert(mf->n_bytes == ETH_ADDR_LEN);
1531 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1532 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1533 case ETH_ADDR_SCAN_COUNT * 2:
1536 case ETH_ADDR_SCAN_COUNT:
1537 memset(mask, 0xff, ETH_ADDR_LEN);
1541 return xasprintf("%s: invalid Ethernet address", s);
1546 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1547 ovs_be32 *ip, ovs_be32 *mask)
1551 assert(mf->n_bytes == sizeof *ip);
1553 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1554 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1556 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1557 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1558 if (prefix <= 0 || prefix > 32) {
1559 return xasprintf("%s: network prefix bits not between 1 and "
1561 } else if (prefix == 32) {
1562 *mask = htonl(UINT32_MAX);
1564 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1566 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1567 *mask = htonl(UINT32_MAX);
1569 return xasprintf("%s: invalid IP address", s);
1575 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1576 struct in6_addr *value, struct in6_addr *mask)
1578 char *str = xstrdup(s);
1579 char *save_ptr = NULL;
1580 const char *name, *netmask;
1583 assert(mf->n_bytes == sizeof *value);
1585 name = strtok_r(str, "/", &save_ptr);
1586 retval = name ? lookup_ipv6(name, value) : EINVAL;
1590 err = xasprintf("%s: could not convert to IPv6 address", str);
1596 netmask = strtok_r(NULL, "/", &save_ptr);
1598 int prefix = atoi(netmask);
1599 if (prefix <= 0 || prefix > 128) {
1601 return xasprintf("%s: prefix bits not between 1 and 128", s);
1603 *mask = ipv6_create_mask(prefix);
1606 *mask = in6addr_exact;
1614 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1615 ovs_be16 *valuep, ovs_be16 *maskp)
1619 assert(mf->n_bytes == sizeof(ovs_be16));
1620 if (ofputil_port_from_string(s, &port)) {
1621 *valuep = htons(port);
1622 *maskp = htons(UINT16_MAX);
1625 return mf_from_integer_string(mf, s,
1626 (uint8_t *) valuep, (uint8_t *) maskp);
1630 struct frag_handling {
1636 static const struct frag_handling all_frags[] = {
1637 #define A FLOW_FRAG_ANY
1638 #define L FLOW_FRAG_LATER
1639 /* name mask value */
1642 { "first", A|L, A },
1643 { "later", A|L, A|L },
1648 { "not_later", L, 0 },
1655 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1657 const struct frag_handling *h;
1659 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1660 if (!strcasecmp(s, h->name)) {
1661 /* We force the upper bits of the mask on to make mf_parse_value()
1662 * happy (otherwise it will never think it's an exact match.) */
1663 *maskp = h->mask | ~FLOW_FRAG_MASK;
1669 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1670 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1673 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1674 * NULL if successful, otherwise a malloc()'d string describing the error. */
1676 mf_parse(const struct mf_field *mf, const char *s,
1677 union mf_value *value, union mf_value *mask)
1679 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1680 memset(value, 0, mf->n_bytes);
1681 memset(mask, 0, mf->n_bytes);
1685 switch (mf->string) {
1687 case MFS_HEXADECIMAL:
1688 return mf_from_integer_string(mf, s,
1689 (uint8_t *) value, (uint8_t *) mask);
1692 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1695 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1698 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1701 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1704 return mf_from_frag_string(s, &value->u8, &mask->u8);
1709 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1710 * successful, otherwise a malloc()'d string describing the error. */
1712 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1714 union mf_value mask;
1717 error = mf_parse(mf, s, value, &mask);
1722 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1723 return xasprintf("%s: wildcards not allowed here", s);
1729 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1730 const uint8_t *maskp, struct ds *s)
1732 unsigned long long int integer;
1735 assert(mf->n_bytes <= 8);
1738 for (i = 0; i < mf->n_bytes; i++) {
1739 integer = (integer << 8) | valuep[i];
1741 if (mf->string == MFS_HEXADECIMAL) {
1742 ds_put_format(s, "%#llx", integer);
1744 ds_put_format(s, "%lld", integer);
1748 unsigned long long int mask;
1751 for (i = 0; i < mf->n_bytes; i++) {
1752 mask = (mask << 8) | maskp[i];
1755 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1756 * not sure that that a bit-mask written in decimal is ever easier to
1757 * understand than the same bit-mask written in hexadecimal. */
1758 ds_put_format(s, "/%#llx", mask);
1763 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1766 const struct frag_handling *h;
1767 uint8_t value = *valuep;
1768 uint8_t mask = *maskp;
1771 mask &= FLOW_FRAG_MASK;
1773 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1774 if (value == h->value && mask == h->mask) {
1775 ds_put_cstr(s, h->name);
1779 ds_put_cstr(s, "<error>");
1782 /* Appends to 's' a string representation of field 'mf' whose value is in
1783 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1785 mf_format(const struct mf_field *mf,
1786 const union mf_value *value, const union mf_value *mask,
1790 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1791 ds_put_cstr(s, "ANY");
1793 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1798 switch (mf->string) {
1801 ofputil_format_port(ntohs(value->be16), s);
1806 case MFS_HEXADECIMAL:
1807 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1811 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1813 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1818 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1823 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1827 mf_format_frag_string(&value->u8, &mask->u8, s);