]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/meta-flow.c
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
),
176 MFF_IP_PROTO
, "nw_proto", NULL
,
178 MFM_NONE
, FWW_NW_PROTO
,
183 MFF_IP_TOS
, "nw_tos", NULL
,
185 MFM_NONE
, FWW_NW_TOS
,
192 MFF_ARP_OP
, "arp_op", NULL
,
193 MF_FIELD_SIZES(be16
),
194 MFM_NONE
, FWW_NW_PROTO
,
199 MFF_ARP_SPA
, "arp_spa", NULL
,
200 MF_FIELD_SIZES(be32
),
206 MFF_ARP_TPA
, "arp_tpa", NULL
,
207 MF_FIELD_SIZES(be32
),
213 MFF_ARP_SHA
, "arp_sha", NULL
,
215 MFM_NONE
, FWW_ARP_SHA
,
220 MFF_ARP_THA
, "arp_tha", NULL
,
222 MFM_NONE
, FWW_ARP_THA
,
233 MFF_TCP_SRC
, "tcp_src", "tp_src",
234 MF_FIELD_SIZES(be16
),
235 MFM_NONE
, FWW_TP_SRC
,
240 MFF_TCP_DST
, "tcp_dst", "tp_dst",
241 MF_FIELD_SIZES(be16
),
242 MFM_NONE
, FWW_TP_DST
,
249 MFF_UDP_SRC
, "udp_src", NULL
,
250 MF_FIELD_SIZES(be16
),
251 MFM_NONE
, FWW_TP_SRC
,
256 MFF_UDP_DST
, "udp_dst", NULL
,
257 MF_FIELD_SIZES(be16
),
258 MFM_NONE
, FWW_TP_DST
,
265 MFF_ICMP_TYPE
, "icmp_type", NULL
,
267 MFM_NONE
, FWW_TP_SRC
,
272 MFF_ICMP_CODE
, "icmp_code", NULL
,
274 MFM_NONE
, FWW_TP_DST
,
285 MFF_ND_TARGET
, "nd_target", NULL
,
286 MF_FIELD_SIZES(ipv6
),
287 MFM_NONE
, FWW_ND_TARGET
,
292 MFF_ND_SLL
, "nd_sll", NULL
,
294 MFM_NONE
, FWW_ARP_SHA
,
299 MFF_ND_TLL
, "nd_tll", NULL
,
301 MFM_NONE
, FWW_ARP_THA
,
308 /* Returns the field with the given 'id'. */
309 const struct mf_field
*
310 mf_from_id(enum mf_field_id id
)
312 assert((unsigned int) id
< MFF_N_IDS
);
313 return &mf_fields
[id
];
316 /* Returns the field with the given 'name', or a null pointer if no field has
318 const struct mf_field
*
319 mf_from_name(const char *name
)
321 static struct shash mf_by_name
= SHASH_INITIALIZER(&mf_by_name
);
323 if (shash_is_empty(&mf_by_name
)) {
324 const struct mf_field
*mf
;
326 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
327 shash_add_once(&mf_by_name
, mf
->name
, mf
);
328 if (mf
->extra_name
) {
329 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
334 return shash_find_data(&mf_by_name
, name
);
337 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
338 * specifies at least one bit in the field.
340 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
341 * meets 'mf''s prerequisites. */
343 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
363 assert(mf
->fww_bit
!= 0);
364 return (wc
->wildcards
& mf
->fww_bit
) != 0;
367 return !wc
->tun_id_mask
;
387 return !wc
->reg_masks
[mf
->id
- MFF_REG0
];
390 return ((wc
->wildcards
& (FWW_ETH_MCAST
| FWW_DL_DST
))
391 == (FWW_ETH_MCAST
| FWW_DL_DST
));
394 return !wc
->vlan_tci_mask
;
396 return !(wc
->vlan_tci_mask
& htons(VLAN_VID_MASK
));
398 return !(wc
->vlan_tci_mask
& htons(VLAN_PCP_MASK
));
401 return !wc
->nw_src_mask
;
403 return !wc
->nw_dst_mask
;
406 return ipv6_mask_is_any(&wc
->ipv6_src_mask
);
408 return ipv6_mask_is_any(&wc
->ipv6_dst_mask
);
411 return !wc
->nw_src_mask
;
413 return !wc
->nw_dst_mask
;
421 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
422 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
423 * purposes, or to 0 if it is wildcarded.
425 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
426 * meets 'mf''s prerequisites. */
428 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
429 union mf_value
*mask
)
449 assert(mf
->fww_bit
!= 0);
450 memset(mask
, wc
->wildcards
& mf
->fww_bit
? 0x00 : 0xff, mf
->n_bytes
);
454 mask
->be64
= wc
->tun_id_mask
;
475 mask
->be32
= htonl(wc
->reg_masks
[mf
->id
- MFF_REG0
]);
479 memcpy(mask
->mac
, flow_wildcards_to_dl_dst_mask(wc
->wildcards
),
484 mask
->be16
= wc
->vlan_tci_mask
;
487 mask
->be16
= wc
->vlan_tci_mask
& htons(VLAN_VID_MASK
);
490 mask
->u8
= vlan_tci_to_pcp(wc
->vlan_tci_mask
);
494 mask
->be32
= wc
->nw_src_mask
;
497 mask
->be32
= wc
->nw_dst_mask
;
501 mask
->ipv6
= wc
->ipv6_src_mask
;
504 mask
->ipv6
= wc
->ipv6_dst_mask
;
508 mask
->be32
= wc
->nw_src_mask
;
511 mask
->be32
= wc
->nw_dst_mask
;
520 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
521 * if the mask is valid, false otherwise. */
523 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
525 switch (mf
->maskable
) {
527 return (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
) ||
528 is_all_ones((const uint8_t *) mask
, mf
->n_bytes
));
534 return (mf
->n_bytes
== 4
535 ? ip_is_cidr(mask
->be32
)
536 : ipv6_is_cidr(&mask
->ipv6
));
539 return flow_wildcards_is_dl_dst_mask_valid(mask
->mac
);
546 is_ip_any(const struct flow
*flow
)
548 return (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
549 flow
->dl_type
== htons(ETH_TYPE_IPV6
));
553 is_icmpv4(const struct flow
*flow
)
555 return (flow
->dl_type
== htons(ETH_TYPE_IP
)
556 && flow
->nw_proto
== IPPROTO_ICMP
);
560 is_icmpv6(const struct flow
*flow
)
562 return (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
563 && flow
->nw_proto
== IPPROTO_ICMPV6
);
566 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
568 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
570 switch (mf
->prereqs
) {
575 return flow
->dl_type
== htons(ETH_TYPE_ARP
);
577 return flow
->dl_type
== htons(ETH_TYPE_IP
);
579 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
581 return is_ip_any(flow
);
584 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
;
586 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
;
588 return is_icmpv6(flow
);
590 return is_icmpv4(flow
) || is_icmpv6(flow
);
593 return (is_icmpv6(flow
)
594 && flow
->tp_dst
== htons(0)
595 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
596 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
598 return (is_icmpv6(flow
)
599 && flow
->tp_dst
== htons(0)
600 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
602 return (is_icmpv6(flow
)
603 && flow
->tp_dst
== htons(0)
604 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
610 /* Returns true if 'value' may be a valid value *as part of a masked match*,
613 * A value is not rejected just because it is not valid for the field in
614 * question, but only if it doesn't make sense to test the bits in question at
615 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
616 * without the VLAN_CFI bit being set, but we can't reject those values because
617 * it is still legitimate to test just for those bits (see the documentation
618 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
619 * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
621 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
669 return !(value
->u8
& 0x03);
672 return !(value
->be16
& htons(0xff00));
675 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
678 return !(value
->u8
& ~7);
686 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
687 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
689 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
690 union mf_value
*value
)
694 value
->be64
= flow
->tun_id
;
698 value
->be16
= htons(flow
->in_port
);
719 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
723 memcpy(value
->mac
, flow
->dl_src
, ETH_ADDR_LEN
);
727 memcpy(value
->mac
, flow
->dl_dst
, ETH_ADDR_LEN
);
731 value
->be16
= flow
->dl_type
;
735 value
->be16
= flow
->vlan_tci
;
739 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
743 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
747 value
->be32
= flow
->nw_src
;
751 value
->be32
= flow
->nw_dst
;
755 value
->ipv6
= flow
->ipv6_src
;
759 value
->ipv6
= flow
->ipv6_dst
;
763 value
->u8
= flow
->nw_proto
;
767 value
->u8
= flow
->nw_tos
;
771 value
->be16
= htons(flow
->nw_proto
);
775 value
->be32
= flow
->nw_src
;
779 value
->be32
= flow
->nw_dst
;
784 memcpy(value
->mac
, flow
->arp_sha
, ETH_ADDR_LEN
);
789 memcpy(value
->mac
, flow
->arp_tha
, ETH_ADDR_LEN
);
793 value
->be16
= flow
->tp_src
;
797 value
->be16
= flow
->tp_dst
;
801 value
->be16
= flow
->tp_src
;
805 value
->be16
= flow
->tp_dst
;
809 value
->u8
= ntohs(flow
->tp_src
);
813 value
->u8
= ntohs(flow
->tp_dst
);
817 value
->ipv6
= flow
->nd_target
;
826 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
827 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
830 mf_set_value(const struct mf_field
*mf
,
831 const union mf_value
*value
, struct cls_rule
*rule
)
835 cls_rule_set_tun_id(rule
, value
->be64
);
839 cls_rule_set_in_port(rule
, ntohs(value
->be16
));
861 cls_rule_set_reg(rule
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
866 cls_rule_set_dl_src(rule
, value
->mac
);
870 cls_rule_set_dl_dst(rule
, value
->mac
);
874 cls_rule_set_dl_type(rule
, value
->be16
);
878 cls_rule_set_dl_tci(rule
, value
->be16
);
882 cls_rule_set_dl_vlan(rule
, value
->be16
);
886 cls_rule_set_dl_vlan_pcp(rule
, value
->u8
);
890 cls_rule_set_nw_src(rule
, value
->be32
);
894 cls_rule_set_nw_dst(rule
, value
->be32
);
898 cls_rule_set_ipv6_src(rule
, &value
->ipv6
);
902 cls_rule_set_ipv6_dst(rule
, &value
->ipv6
);
906 cls_rule_set_nw_proto(rule
, value
->u8
);
910 cls_rule_set_nw_tos(rule
, value
->u8
);
914 cls_rule_set_nw_proto(rule
, ntohs(value
->be16
));
918 cls_rule_set_nw_src(rule
, value
->be32
);
922 cls_rule_set_nw_dst(rule
, value
->be32
);
927 cls_rule_set_arp_sha(rule
, value
->mac
);
932 cls_rule_set_arp_tha(rule
, value
->mac
);
936 cls_rule_set_tp_src(rule
, value
->be16
);
940 cls_rule_set_tp_dst(rule
, value
->be16
);
944 cls_rule_set_tp_src(rule
, value
->be16
);
948 cls_rule_set_tp_dst(rule
, value
->be16
);
952 cls_rule_set_icmp_type(rule
, value
->u8
);
956 cls_rule_set_icmp_code(rule
, value
->u8
);
960 cls_rule_set_nd_target(rule
, &value
->ipv6
);
969 /* Makes 'rule' wildcard field 'mf'.
971 * The caller is responsible for ensuring that 'rule' meets 'mf''s
974 mf_set_wild(const struct mf_field
*mf
, struct cls_rule
*rule
)
978 cls_rule_set_tun_id_masked(rule
, htonll(0), htonll(0));
982 rule
->wc
.wildcards
|= FWW_IN_PORT
;
983 rule
->flow
.in_port
= 0;
988 cls_rule_set_reg_masked(rule
, 0, 0, 0);
993 cls_rule_set_reg_masked(rule
, 1, 0, 0);
998 cls_rule_set_reg_masked(rule
, 2, 0, 0);
1003 cls_rule_set_reg_masked(rule
, 3, 0, 0);
1008 cls_rule_set_reg_masked(rule
, 4, 0, 0);
1016 rule
->wc
.wildcards
|= FWW_DL_SRC
;
1017 memset(rule
->flow
.dl_src
, 0, sizeof rule
->flow
.dl_src
);
1021 rule
->wc
.wildcards
|= FWW_DL_DST
| FWW_ETH_MCAST
;
1022 memset(rule
->flow
.dl_dst
, 0, sizeof rule
->flow
.dl_dst
);
1026 rule
->wc
.wildcards
|= FWW_DL_TYPE
;
1027 rule
->flow
.dl_type
= htons(0);
1031 cls_rule_set_dl_tci_masked(rule
, htons(0), htons(0));
1035 cls_rule_set_any_vid(rule
);
1039 cls_rule_set_any_pcp(rule
);
1044 cls_rule_set_nw_src_masked(rule
, htonl(0), htonl(0));
1049 cls_rule_set_nw_dst_masked(rule
, htonl(0), htonl(0));
1053 memset(&rule
->wc
.ipv6_src_mask
, 0, sizeof rule
->wc
.ipv6_src_mask
);
1054 memset(&rule
->flow
.ipv6_src
, 0, sizeof rule
->flow
.ipv6_src
);
1058 memset(&rule
->wc
.ipv6_dst_mask
, 0, sizeof rule
->wc
.ipv6_dst_mask
);
1059 memset(&rule
->flow
.ipv6_dst
, 0, sizeof rule
->flow
.ipv6_dst
);
1063 rule
->wc
.wildcards
|= FWW_NW_PROTO
;
1064 rule
->flow
.nw_proto
= 0;
1068 rule
->wc
.wildcards
|= FWW_NW_TOS
;
1069 rule
->flow
.nw_tos
= 0;
1073 rule
->wc
.wildcards
|= FWW_NW_PROTO
;
1074 rule
->flow
.nw_proto
= 0;
1079 rule
->wc
.wildcards
|= FWW_ARP_SHA
;
1080 memset(rule
->flow
.arp_sha
, 0, sizeof rule
->flow
.arp_sha
);
1085 rule
->wc
.wildcards
|= FWW_ARP_THA
;
1086 memset(rule
->flow
.arp_tha
, 0, sizeof rule
->flow
.arp_tha
);
1092 rule
->wc
.wildcards
|= FWW_TP_SRC
;
1093 rule
->flow
.tp_src
= htons(0);
1099 rule
->wc
.wildcards
|= FWW_TP_DST
;
1100 rule
->flow
.tp_dst
= htons(0);
1104 rule
->wc
.wildcards
|= FWW_ND_TARGET
;
1105 memset(&rule
->flow
.nd_target
, 0, sizeof rule
->flow
.nd_target
);
1114 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1115 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1116 * with a 1-bit indicating that the corresponding value bit must match and a
1117 * 0-bit indicating a don't-care.
1119 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1120 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1121 * call is equivalent to mf_set_wild(mf, rule).
1123 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1124 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1126 mf_set(const struct mf_field
*mf
,
1127 const union mf_value
*value
, const union mf_value
*mask
,
1128 struct cls_rule
*rule
)
1130 if (!mask
|| is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
1131 mf_set_value(mf
, value
, rule
);
1133 } else if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
1134 mf_set_wild(mf
, rule
);
1161 cls_rule_set_tun_id_masked(rule
, value
->be64
, mask
->be64
);
1182 cls_rule_set_reg_masked(rule
, mf
->id
- MFF_REG0
,
1183 ntohl(value
->be32
), ntohl(mask
->be32
));
1187 if (flow_wildcards_is_dl_dst_mask_valid(mask
->mac
)) {
1188 cls_rule_set_dl_dst_masked(rule
, value
->mac
, mask
->mac
);
1193 cls_rule_set_dl_tci_masked(rule
, value
->be16
, mask
->be16
);
1197 cls_rule_set_nw_src_masked(rule
, value
->be32
, mask
->be32
);
1201 cls_rule_set_nw_dst_masked(rule
, value
->be32
, mask
->be32
);
1205 cls_rule_set_ipv6_src_masked(rule
, &value
->ipv6
, &mask
->ipv6
);
1209 cls_rule_set_ipv6_dst_masked(rule
, &value
->ipv6
, &mask
->ipv6
);
1213 cls_rule_set_nw_src_masked(rule
, value
->be32
, mask
->be32
);
1217 cls_rule_set_nw_dst_masked(rule
, value
->be32
, mask
->be32
);
1226 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1227 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1230 * Example: suppose that 'mf' is originally the following 2-byte field in
1233 * value == 0xe00a == 2#1110000000001010
1234 * mask == 0xfc3f == 2#1111110000111111
1236 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1237 * effect (note that 0x55 is 2#1010101):
1239 * value == 0xd50a == 2#1101010100001010
1240 * mask == 0xff3f == 2#1111111100111111
1242 * The caller is responsible for ensuring that the result will be a valid
1243 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1244 * 'rule' meets 'mf''s prerequisites. */
1246 mf_set_subfield(const struct mf_field
*mf
, uint64_t x
, unsigned int ofs
,
1247 unsigned int n_bits
, struct cls_rule
*rule
)
1249 if (ofs
== 0 && mf
->n_bytes
* 8 == n_bits
) {
1250 union mf_value value
;
1253 for (i
= mf
->n_bytes
- 1; i
>= 0; i
--) {
1254 ((uint8_t *) &value
)[i
] = x
;
1257 mf_set_value(mf
, &value
, rule
);
1259 union mf_value value
, mask
;
1261 unsigned int byte_ofs
;
1263 mf_get(mf
, rule
, &value
, &mask
);
1265 byte_ofs
= mf
->n_bytes
- ofs
/ 8;
1266 vp
= &((uint8_t *) &value
)[byte_ofs
];
1267 mp
= &((uint8_t *) &mask
)[byte_ofs
];
1269 unsigned int chunk
= MIN(8 - ofs
% 8, n_bits
);
1270 uint8_t chunk_mask
= ((1 << chunk
) - 1) << (ofs
% 8);
1272 *--vp
&= ~chunk_mask
;
1273 *vp
|= chunk_mask
& (x
<< (ofs
% 8));
1274 *--mp
|= chunk_mask
;
1280 while (n_bits
>= 8) {
1288 uint8_t chunk_mask
= (1 << n_bits
) - 1;
1290 *--vp
&= ~chunk_mask
;
1291 *vp
|= chunk_mask
& x
;
1292 *--mp
|= chunk_mask
;
1295 mf_set(mf
, &value
, &mask
, rule
);
1299 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1300 * 'value' and 'mask', respectively. */
1302 mf_get(const struct mf_field
*mf
, const struct cls_rule
*rule
,
1303 union mf_value
*value
, union mf_value
*mask
)
1305 mf_get_value(mf
, &rule
->flow
, value
);
1306 mf_get_mask(mf
, &rule
->wc
, mask
);
1309 /* Assigns a random value for field 'mf' to 'value'. */
1311 mf_random_value(const struct mf_field
*mf
, union mf_value
*value
)
1313 random_bytes(value
, mf
->n_bytes
);
1365 value
->be16
&= htons(0xff);
1369 value
->be16
&= htons(VLAN_VID_MASK
);
1383 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
1384 uint8_t *valuep
, uint8_t *maskp
)
1386 unsigned long long int integer
, mask
;
1391 integer
= strtoull(s
, &tail
, 0);
1392 if (errno
|| (*tail
!= '\0' && *tail
!= '/')) {
1397 mask
= strtoull(tail
+ 1, &tail
, 0);
1398 if (errno
|| *tail
!= '\0') {
1405 for (i
= mf
->n_bytes
- 1; i
>= 0; i
--) {
1406 valuep
[i
] = integer
;
1412 return xasprintf("%s: value too large for %u-byte field %s",
1413 s
, mf
->n_bytes
, mf
->name
);
1418 return xasprintf("%s: bad syntax for %s", s
, mf
->name
);
1422 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
1423 uint8_t mac
[ETH_ADDR_LEN
],
1424 uint8_t mask
[ETH_ADDR_LEN
])
1426 assert(mf
->n_bytes
== ETH_ADDR_LEN
);
1428 switch (sscanf(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
,
1429 ETH_ADDR_SCAN_ARGS(mac
), ETH_ADDR_SCAN_ARGS(mask
))){
1430 case ETH_ADDR_SCAN_COUNT
* 2:
1433 case ETH_ADDR_SCAN_COUNT
:
1434 memset(mask
, 0xff, ETH_ADDR_LEN
);
1438 return xasprintf("%s: invalid Ethernet address", s
);
1443 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
1444 ovs_be32
*ip
, ovs_be32
*mask
)
1448 assert(mf
->n_bytes
== sizeof *ip
);
1450 if (sscanf(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
1451 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
)) == IP_SCAN_COUNT
* 2) {
1453 } else if (sscanf(s
, IP_SCAN_FMT
"/%d",
1454 IP_SCAN_ARGS(ip
), &prefix
) == IP_SCAN_COUNT
+ 1) {
1455 if (prefix
<= 0 || prefix
> 32) {
1456 return xasprintf("%s: network prefix bits not between 1 and "
1458 } else if (prefix
== 32) {
1459 *mask
= htonl(UINT32_MAX
);
1461 *mask
= htonl(((1u << prefix
) - 1) << (32 - prefix
));
1463 } else if (sscanf(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
)) == IP_SCAN_COUNT
) {
1464 *mask
= htonl(UINT32_MAX
);
1466 return xasprintf("%s: invalid IP address", s
);
1472 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
1473 struct in6_addr
*value
, struct in6_addr
*mask
)
1475 char *str
= xstrdup(s
);
1476 char *save_ptr
= NULL
;
1477 const char *name
, *netmask
;
1480 assert(mf
->n_bytes
== sizeof *value
);
1482 name
= strtok_r(str
, "/", &save_ptr
);
1483 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
1487 err
= xasprintf("%s: could not convert to IPv6 address", str
);
1493 netmask
= strtok_r(NULL
, "/", &save_ptr
);
1495 int prefix
= atoi(netmask
);
1496 if (prefix
<= 0 || prefix
> 128) {
1498 return xasprintf("%s: prefix bits not between 1 and 128", s
);
1500 *mask
= ipv6_create_mask(prefix
);
1503 *mask
= in6addr_exact
;
1511 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
1512 ovs_be16
*valuep
, ovs_be16
*maskp
)
1516 assert(mf
->n_bytes
== sizeof(ovs_be16
));
1517 if (ofputil_port_from_string(s
, &port
)) {
1518 *valuep
= htons(port
);
1519 *maskp
= htons(UINT16_MAX
);
1522 return mf_from_integer_string(mf
, s
,
1523 (uint8_t *) valuep
, (uint8_t *) maskp
);
1527 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1528 * NULL if successful, otherwise a malloc()'d string describing the error. */
1530 mf_parse(const struct mf_field
*mf
, const char *s
,
1531 union mf_value
*value
, union mf_value
*mask
)
1533 if (!strcasecmp(s
, "any") || !strcmp(s
, "*")) {
1534 memset(value
, 0, mf
->n_bytes
);
1535 memset(mask
, 0, mf
->n_bytes
);
1539 switch (mf
->string
) {
1541 case MFS_HEXADECIMAL
:
1542 return mf_from_integer_string(mf
, s
,
1543 (uint8_t *) value
, (uint8_t *) mask
);
1546 return mf_from_ethernet_string(mf
, s
, value
->mac
, mask
->mac
);
1549 return mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
1552 return mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
1555 return mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
1560 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1561 * successful, otherwise a malloc()'d string describing the error. */
1563 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
1565 union mf_value mask
;
1568 error
= mf_parse(mf
, s
, value
, &mask
);
1573 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
1574 return xasprintf("%s: wildcards not allowed here", s
);
1580 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
1581 const uint8_t *maskp
, struct ds
*s
)
1583 unsigned long long int integer
;
1586 assert(mf
->n_bytes
<= 8);
1589 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1590 integer
= (integer
<< 8) | valuep
[i
];
1592 if (mf
->string
== MFS_HEXADECIMAL
) {
1593 ds_put_format(s
, "%#llx", integer
);
1595 ds_put_format(s
, "%lld", integer
);
1599 unsigned long long int mask
;
1602 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1603 mask
= (mask
<< 8) | maskp
[i
];
1606 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1607 * not sure that that a bit-mask written in decimal is ever easier to
1608 * understand than the same bit-mask written in hexadecimal. */
1609 ds_put_format(s
, "/%#llx", mask
);
1613 /* Appends to 's' a string representation of field 'mf' whose value is in
1614 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1616 mf_format(const struct mf_field
*mf
,
1617 const union mf_value
*value
, const union mf_value
*mask
,
1621 if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
1622 ds_put_cstr(s
, "ANY");
1624 } else if (is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
1629 switch (mf
->string
) {
1632 ofputil_format_port(ntohs(value
->be16
), s
);
1637 case MFS_HEXADECIMAL
:
1638 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
1642 ds_put_format(s
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(value
->mac
));
1644 ds_put_format(s
, "/"ETH_ADDR_FMT
, ETH_ADDR_ARGS(mask
->mac
));
1649 ip_format_masked(value
->be32
, mask
? mask
->be32
: htonl(UINT32_MAX
),
1654 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);