2 * Copyright (c) 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "meta-flow.h"
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-errors.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow
);
40 #define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 static const struct mf_field mf_fields
[MFF_N_IDS
] = {
50 MFF_TUN_ID
, "tun_id", NULL
,
56 NXM_NX_TUN_ID
, "NXM_NX_TUN_ID",
57 NXM_NX_TUN_ID
, "NXM_NX_TUN_ID",
59 MFF_METADATA
, "metadata", NULL
,
65 OXM_OF_METADATA
, "OXM_OF_METADATA",
66 OXM_OF_METADATA
, "OXM_OF_METADATA",
68 MFF_IN_PORT
, "in_port", NULL
,
70 MFM_NONE
, FWW_IN_PORT
,
74 NXM_OF_IN_PORT
, "NXM_OF_IN_PORT",
75 OXM_OF_IN_PORT
, "OXM_OF_IN_PORT",
78 #define REGISTER(IDX) \
80 MFF_REG##IDX, "reg" #IDX, NULL, \
81 MF_FIELD_SIZES(be32), \
86 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
87 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
122 MFF_ETH_SRC
, "eth_src", "dl_src",
128 NXM_OF_ETH_SRC
, "NXM_OF_ETH_SRC",
129 OXM_OF_ETH_SRC
, "OXM_OF_ETH_SRC",
131 MFF_ETH_DST
, "eth_dst", "dl_dst",
137 NXM_OF_ETH_DST
, "NXM_OF_ETH_DST",
138 OXM_OF_ETH_DST
, "OXM_OF_ETH_DST",
140 MFF_ETH_TYPE
, "eth_type", "dl_type",
141 MF_FIELD_SIZES(be16
),
142 MFM_NONE
, FWW_DL_TYPE
,
146 NXM_OF_ETH_TYPE
, "NXM_OF_ETH_TYPE",
147 OXM_OF_ETH_TYPE
, "OXM_OF_ETH_TYPE",
151 MFF_VLAN_TCI
, "vlan_tci", NULL
,
152 MF_FIELD_SIZES(be16
),
157 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
158 NXM_OF_VLAN_TCI
, "NXM_OF_VLAN_TCI",
160 MFF_DL_VLAN
, "dl_vlan", NULL
,
161 sizeof(ovs_be16
), 12,
166 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
167 OXM_OF_VLAN_VID
, "OXM_OF_VLAN_VID",
169 MFF_DL_VLAN_PCP
, "dl_vlan_pcp", NULL
,
175 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
176 OXM_OF_VLAN_PCP
, "OXM_OF_VLAN_PCP",
184 MFF_IPV4_SRC
, "ip_src", "nw_src",
185 MF_FIELD_SIZES(be32
),
190 NXM_OF_IP_SRC
, "NXM_OF_IP_SRC",
191 OXM_OF_IPV4_SRC
, "OXM_OF_IPV4_SRC",
193 MFF_IPV4_DST
, "ip_dst", "nw_dst",
194 MF_FIELD_SIZES(be32
),
199 NXM_OF_IP_DST
, "NXM_OF_IP_DST",
200 OXM_OF_IPV4_DST
, "OXM_OF_IPV4_DST",
204 MFF_IPV6_SRC
, "ipv6_src", NULL
,
205 MF_FIELD_SIZES(ipv6
),
210 NXM_NX_IPV6_SRC
, "NXM_NX_IPV6_SRC",
211 OXM_OF_IPV6_SRC
, "OXM_OF_IPV6_SRC",
213 MFF_IPV6_DST
, "ipv6_dst", NULL
,
214 MF_FIELD_SIZES(ipv6
),
219 NXM_NX_IPV6_DST
, "NXM_NX_IPV6_DST",
220 OXM_OF_IPV6_DST
, "OXM_OF_IPV6_DST",
223 MFF_IPV6_LABEL
, "ipv6_label", NULL
,
229 NXM_NX_IPV6_LABEL
, "NXM_NX_IPV6_LABEL",
230 OXM_OF_IPV6_FLABEL
, "OXM_OF_IPV6_FLABEL",
234 MFF_IP_PROTO
, "nw_proto", NULL
,
236 MFM_NONE
, FWW_NW_PROTO
,
240 NXM_OF_IP_PROTO
, "NXM_OF_IP_PROTO",
241 OXM_OF_IP_PROTO
, "OXM_OF_IP_PROTO",
243 MFF_IP_DSCP
, "nw_tos", NULL
,
245 MFM_NONE
, FWW_NW_DSCP
,
249 NXM_OF_IP_TOS
, "NXM_OF_IP_TOS",
250 OXM_OF_IP_DSCP
, "OXM_OF_IP_DSCP",
252 MFF_IP_ECN
, "nw_ecn", NULL
,
254 MFM_NONE
, FWW_NW_ECN
,
258 NXM_NX_IP_ECN
, "NXM_NX_IP_ECN",
259 OXM_OF_IP_ECN
, "OXM_OF_IP_ECN",
261 MFF_IP_TTL
, "nw_ttl", NULL
,
263 MFM_NONE
, FWW_NW_TTL
,
267 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
268 NXM_NX_IP_TTL
, "NXM_NX_IP_TTL",
270 MFF_IP_FRAG
, "ip_frag", NULL
,
276 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
277 NXM_NX_IP_FRAG
, "NXM_NX_IP_FRAG",
281 MFF_ARP_OP
, "arp_op", NULL
,
282 MF_FIELD_SIZES(be16
),
283 MFM_NONE
, FWW_NW_PROTO
,
287 NXM_OF_ARP_OP
, "NXM_OF_ARP_OP",
288 OXM_OF_ARP_OP
, "OXM_OF_ARP_OP",
290 MFF_ARP_SPA
, "arp_spa", NULL
,
291 MF_FIELD_SIZES(be32
),
296 NXM_OF_ARP_SPA
, "NXM_OF_ARP_SPA",
297 OXM_OF_ARP_SPA
, "OXM_OF_ARP_SPA",
299 MFF_ARP_TPA
, "arp_tpa", NULL
,
300 MF_FIELD_SIZES(be32
),
305 NXM_OF_ARP_TPA
, "NXM_OF_ARP_TPA",
306 OXM_OF_ARP_TPA
, "OXM_OF_ARP_TPA",
308 MFF_ARP_SHA
, "arp_sha", NULL
,
314 NXM_NX_ARP_SHA
, "NXM_NX_ARP_SHA",
315 OXM_OF_ARP_SHA
, "OXM_OF_ARP_SHA",
317 MFF_ARP_THA
, "arp_tha", NULL
,
323 NXM_NX_ARP_THA
, "NXM_NX_ARP_THA",
324 OXM_OF_ARP_THA
, "OXM_OF_ARP_THA",
332 MFF_TCP_SRC
, "tcp_src", "tp_src",
333 MF_FIELD_SIZES(be16
),
338 NXM_OF_TCP_SRC
, "NXM_OF_TCP_SRC",
339 OXM_OF_TCP_SRC
, "OXM_OF_TCP_SRC",
341 MFF_TCP_DST
, "tcp_dst", "tp_dst",
342 MF_FIELD_SIZES(be16
),
347 NXM_OF_TCP_DST
, "NXM_OF_TCP_DST",
348 OXM_OF_TCP_DST
, "OXM_OF_TCP_DST",
352 MFF_UDP_SRC
, "udp_src", NULL
,
353 MF_FIELD_SIZES(be16
),
358 NXM_OF_UDP_SRC
, "NXM_OF_UDP_SRC",
359 OXM_OF_UDP_SRC
, "OXM_OF_UDP_SRC",
361 MFF_UDP_DST
, "udp_dst", NULL
,
362 MF_FIELD_SIZES(be16
),
367 NXM_OF_UDP_DST
, "NXM_OF_UDP_DST",
368 OXM_OF_UDP_DST
, "OXM_OF_UDP_DST",
372 MFF_ICMPV4_TYPE
, "icmp_type", NULL
,
378 NXM_OF_ICMP_TYPE
, "NXM_OF_ICMP_TYPE",
379 OXM_OF_ICMPV4_TYPE
, "OXM_OF_ICMPV4_TYPE",
381 MFF_ICMPV4_CODE
, "icmp_code", NULL
,
387 NXM_OF_ICMP_CODE
, "NXM_OF_ICMP_CODE",
388 OXM_OF_ICMPV4_CODE
, "OXM_OF_ICMPV4_CODE",
392 MFF_ICMPV6_TYPE
, "icmpv6_type", NULL
,
398 NXM_NX_ICMPV6_TYPE
, "NXM_NX_ICMPV6_TYPE",
399 OXM_OF_ICMPV6_TYPE
, "OXM_OF_ICMPV6_TYPE",
401 MFF_ICMPV6_CODE
, "icmpv6_code", NULL
,
407 NXM_NX_ICMPV6_CODE
, "NXM_NX_ICMPV6_CODE",
408 OXM_OF_ICMPV6_CODE
, "OXM_OF_ICMPV6_CODE",
416 MFF_ND_TARGET
, "nd_target", NULL
,
417 MF_FIELD_SIZES(ipv6
),
422 NXM_NX_ND_TARGET
, "NXM_NX_ND_TARGET",
423 OXM_OF_IPV6_ND_TARGET
, "OXM_OF_IPV6_ND_TARGET",
425 MFF_ND_SLL
, "nd_sll", NULL
,
431 NXM_NX_ND_SLL
, "NXM_NX_ND_SLL",
432 OXM_OF_IPV6_ND_SLL
, "OXM_OF_IPV6_ND_SLL",
434 MFF_ND_TLL
, "nd_tll", NULL
,
440 NXM_NX_ND_TLL
, "NXM_NX_ND_TLL",
441 OXM_OF_IPV6_ND_TLL
, "OXM_OF_IPV6_ND_TLL",
445 /* Maps an NXM or OXM header value to an mf_field. */
447 struct hmap_node hmap_node
; /* In 'all_fields' hmap. */
448 uint32_t header
; /* NXM or OXM header value. */
449 const struct mf_field
*mf
;
452 /* Contains 'struct nxm_field's. */
453 static struct hmap all_fields
= HMAP_INITIALIZER(&all_fields
);
455 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
456 * controller and so there's not much point in showing a lot of them. */
457 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
459 const struct mf_field
*mf_from_nxm_header__(uint32_t header
);
461 /* Returns the field with the given 'id'. */
462 const struct mf_field
*
463 mf_from_id(enum mf_field_id id
)
465 assert((unsigned int) id
< MFF_N_IDS
);
466 return &mf_fields
[id
];
469 /* Returns the field with the given 'name', or a null pointer if no field has
471 const struct mf_field
*
472 mf_from_name(const char *name
)
474 static struct shash mf_by_name
= SHASH_INITIALIZER(&mf_by_name
);
476 if (shash_is_empty(&mf_by_name
)) {
477 const struct mf_field
*mf
;
479 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
480 shash_add_once(&mf_by_name
, mf
->name
, mf
);
481 if (mf
->extra_name
) {
482 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
487 return shash_find_data(&mf_by_name
, name
);
491 add_nxm_field(uint32_t header
, const struct mf_field
*mf
)
495 f
= xmalloc(sizeof *f
);
496 hmap_insert(&all_fields
, &f
->hmap_node
, hash_int(header
, 0));
502 nxm_init_add_field(const struct mf_field
*mf
, uint32_t header
)
505 assert(!mf_from_nxm_header__(header
));
506 add_nxm_field(header
, mf
);
507 if (mf
->maskable
!= MFM_NONE
) {
508 add_nxm_field(NXM_MAKE_WILD_HEADER(header
), mf
);
516 const struct mf_field
*mf
;
518 for (mf
= mf_fields
; mf
< &mf_fields
[MFF_N_IDS
]; mf
++) {
519 nxm_init_add_field(mf
, mf
->nxm_header
);
520 if (mf
->oxm_header
!= mf
->nxm_header
) {
521 nxm_init_add_field(mf
, mf
->oxm_header
);
526 const struct mf_field
*
527 mf_from_nxm_header(uint32_t header
)
529 if (hmap_is_empty(&all_fields
)) {
532 return mf_from_nxm_header__(header
);
535 const struct mf_field
*
536 mf_from_nxm_header__(uint32_t header
)
538 const struct nxm_field
*f
;
540 HMAP_FOR_EACH_IN_BUCKET (f
, hmap_node
, hash_int(header
, 0), &all_fields
) {
541 if (f
->header
== header
) {
549 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
550 * specifies at least one bit in the field.
552 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
553 * meets 'mf''s prerequisites. */
555 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
565 assert(mf
->fww_bit
!= 0);
566 return (wc
->wildcards
& mf
->fww_bit
) != 0;
569 return !wc
->tun_id_mask
;
571 return !wc
->metadata_mask
;
574 return !wc
->reg_masks
[mf
->id
- MFF_REG0
];
577 return eth_addr_is_zero(wc
->dl_src_mask
);
579 return eth_addr_is_zero(wc
->dl_dst_mask
);
583 return eth_addr_is_zero(wc
->arp_sha_mask
);
587 return eth_addr_is_zero(wc
->arp_tha_mask
);
590 return !wc
->vlan_tci_mask
;
592 return !(wc
->vlan_tci_mask
& htons(VLAN_VID_MASK
));
593 case MFF_DL_VLAN_PCP
:
594 return !(wc
->vlan_tci_mask
& htons(VLAN_PCP_MASK
));
597 return !wc
->nw_src_mask
;
599 return !wc
->nw_dst_mask
;
602 return ipv6_mask_is_any(&wc
->ipv6_src_mask
);
604 return ipv6_mask_is_any(&wc
->ipv6_dst_mask
);
607 return !wc
->ipv6_label_mask
;
610 return ipv6_mask_is_any(&wc
->nd_target_mask
);
613 return !(wc
->nw_frag_mask
& FLOW_NW_FRAG_MASK
);
616 return !wc
->nw_src_mask
;
618 return !wc
->nw_dst_mask
;
622 case MFF_ICMPV4_TYPE
:
623 case MFF_ICMPV6_TYPE
:
624 return !wc
->tp_src_mask
;
627 case MFF_ICMPV4_CODE
:
628 case MFF_ICMPV6_CODE
:
629 return !wc
->tp_dst_mask
;
637 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
638 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
639 * purposes, or to 0 if it is wildcarded.
641 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
642 * meets 'mf''s prerequisites. */
644 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
645 union mf_value
*mask
)
655 assert(mf
->fww_bit
!= 0);
656 memset(mask
, wc
->wildcards
& mf
->fww_bit
? 0x00 : 0xff, mf
->n_bytes
);
660 mask
->be64
= wc
->tun_id_mask
;
663 mask
->be64
= wc
->metadata_mask
;
667 mask
->be32
= htonl(wc
->reg_masks
[mf
->id
- MFF_REG0
]);
671 memcpy(mask
->mac
, wc
->dl_dst_mask
, ETH_ADDR_LEN
);
675 memcpy(mask
->mac
, wc
->dl_src_mask
, ETH_ADDR_LEN
);
679 mask
->be16
= wc
->vlan_tci_mask
;
682 mask
->be16
= wc
->vlan_tci_mask
& htons(VLAN_VID_MASK
);
684 case MFF_DL_VLAN_PCP
:
685 mask
->u8
= vlan_tci_to_pcp(wc
->vlan_tci_mask
);
689 mask
->be32
= wc
->nw_src_mask
;
692 mask
->be32
= wc
->nw_dst_mask
;
696 mask
->ipv6
= wc
->ipv6_src_mask
;
699 mask
->ipv6
= wc
->ipv6_dst_mask
;
702 mask
->be32
= wc
->ipv6_label_mask
;
706 mask
->ipv6
= wc
->nd_target_mask
;
710 mask
->u8
= wc
->nw_frag_mask
& FLOW_NW_FRAG_MASK
;
714 mask
->be32
= wc
->nw_src_mask
;
717 mask
->be32
= wc
->nw_dst_mask
;
721 memcpy(mask
->mac
, wc
->arp_sha_mask
, ETH_ADDR_LEN
);
725 memcpy(mask
->mac
, wc
->arp_tha_mask
, ETH_ADDR_LEN
);
730 mask
->be16
= wc
->tp_src_mask
;
734 mask
->be16
= wc
->tp_dst_mask
;
737 case MFF_ICMPV4_TYPE
:
738 case MFF_ICMPV6_TYPE
:
739 mask
->u8
= ntohs(wc
->tp_src_mask
);
741 case MFF_ICMPV4_CODE
:
742 case MFF_ICMPV6_CODE
:
743 mask
->u8
= ntohs(wc
->tp_dst_mask
);
752 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
753 * if the mask is valid, false otherwise. */
755 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
757 switch (mf
->maskable
) {
759 return (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
) ||
760 is_all_ones((const uint8_t *) mask
, mf
->n_bytes
));
770 is_ip_any(const struct flow
*flow
)
772 return (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
773 flow
->dl_type
== htons(ETH_TYPE_IPV6
));
777 is_icmpv4(const struct flow
*flow
)
779 return (flow
->dl_type
== htons(ETH_TYPE_IP
)
780 && flow
->nw_proto
== IPPROTO_ICMP
);
784 is_icmpv6(const struct flow
*flow
)
786 return (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
787 && flow
->nw_proto
== IPPROTO_ICMPV6
);
790 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
792 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
794 switch (mf
->prereqs
) {
799 return flow
->dl_type
== htons(ETH_TYPE_ARP
);
801 return flow
->dl_type
== htons(ETH_TYPE_IP
);
803 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
805 return is_ip_any(flow
);
808 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
;
810 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
;
812 return is_icmpv4(flow
);
814 return is_icmpv6(flow
);
817 return (is_icmpv6(flow
)
818 && flow
->tp_dst
== htons(0)
819 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
820 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
822 return (is_icmpv6(flow
)
823 && flow
->tp_dst
== htons(0)
824 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
826 return (is_icmpv6(flow
)
827 && flow
->tp_dst
== htons(0)
828 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
834 /* Returns true if 'value' may be a valid value *as part of a masked match*,
837 * A value is not rejected just because it is not valid for the field in
838 * question, but only if it doesn't make sense to test the bits in question at
839 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
840 * without the VLAN_CFI bit being set, but we can't reject those values because
841 * it is still legitimate to test just for those bits (see the documentation
842 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
843 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
845 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
870 case MFF_ICMPV4_TYPE
:
871 case MFF_ICMPV4_CODE
:
872 case MFF_ICMPV6_TYPE
:
873 case MFF_ICMPV6_CODE
:
880 return !(value
->u8
& ~IP_DSCP_MASK
);
882 return !(value
->u8
& ~IP_ECN_MASK
);
884 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
887 return !(value
->be16
& htons(0xff00));
890 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
892 case MFF_DL_VLAN_PCP
:
893 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
896 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
904 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
905 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
907 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
908 union mf_value
*value
)
912 value
->be64
= flow
->tun_id
;
915 value
->be64
= flow
->metadata
;
919 value
->be16
= htons(flow
->in_port
);
923 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
927 memcpy(value
->mac
, flow
->dl_src
, ETH_ADDR_LEN
);
931 memcpy(value
->mac
, flow
->dl_dst
, ETH_ADDR_LEN
);
935 value
->be16
= flow
->dl_type
;
939 value
->be16
= flow
->vlan_tci
;
943 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
946 case MFF_DL_VLAN_PCP
:
947 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
951 value
->be32
= flow
->nw_src
;
955 value
->be32
= flow
->nw_dst
;
959 value
->ipv6
= flow
->ipv6_src
;
963 value
->ipv6
= flow
->ipv6_dst
;
967 value
->be32
= flow
->ipv6_label
;
971 value
->u8
= flow
->nw_proto
;
975 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
979 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
983 value
->u8
= flow
->nw_ttl
;
987 value
->u8
= flow
->nw_frag
;
991 value
->be16
= htons(flow
->nw_proto
);
995 value
->be32
= flow
->nw_src
;
999 value
->be32
= flow
->nw_dst
;
1004 memcpy(value
->mac
, flow
->arp_sha
, ETH_ADDR_LEN
);
1009 memcpy(value
->mac
, flow
->arp_tha
, ETH_ADDR_LEN
);
1014 value
->be16
= flow
->tp_src
;
1019 value
->be16
= flow
->tp_dst
;
1022 case MFF_ICMPV4_TYPE
:
1023 case MFF_ICMPV6_TYPE
:
1024 value
->u8
= ntohs(flow
->tp_src
);
1027 case MFF_ICMPV4_CODE
:
1028 case MFF_ICMPV6_CODE
:
1029 value
->u8
= ntohs(flow
->tp_dst
);
1033 value
->ipv6
= flow
->nd_target
;
1042 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1043 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1046 mf_set_value(const struct mf_field
*mf
,
1047 const union mf_value
*value
, struct cls_rule
*rule
)
1051 cls_rule_set_tun_id(rule
, value
->be64
);
1054 cls_rule_set_metadata(rule
, value
->be64
);
1058 cls_rule_set_in_port(rule
, ntohs(value
->be16
));
1062 cls_rule_set_reg(rule
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1066 cls_rule_set_dl_src(rule
, value
->mac
);
1070 cls_rule_set_dl_dst(rule
, value
->mac
);
1074 cls_rule_set_dl_type(rule
, value
->be16
);
1078 cls_rule_set_dl_tci(rule
, value
->be16
);
1082 cls_rule_set_dl_vlan(rule
, value
->be16
);
1085 case MFF_DL_VLAN_PCP
:
1086 cls_rule_set_dl_vlan_pcp(rule
, value
->u8
);
1090 cls_rule_set_nw_src(rule
, value
->be32
);
1094 cls_rule_set_nw_dst(rule
, value
->be32
);
1098 cls_rule_set_ipv6_src(rule
, &value
->ipv6
);
1102 cls_rule_set_ipv6_dst(rule
, &value
->ipv6
);
1105 case MFF_IPV6_LABEL
:
1106 cls_rule_set_ipv6_label(rule
, value
->be32
);
1110 cls_rule_set_nw_proto(rule
, value
->u8
);
1114 cls_rule_set_nw_dscp(rule
, value
->u8
);
1118 cls_rule_set_nw_ecn(rule
, value
->u8
);
1122 cls_rule_set_nw_ttl(rule
, value
->u8
);
1126 cls_rule_set_nw_frag(rule
, value
->u8
);
1130 cls_rule_set_nw_proto(rule
, ntohs(value
->be16
));
1134 cls_rule_set_nw_src(rule
, value
->be32
);
1138 cls_rule_set_nw_dst(rule
, value
->be32
);
1143 cls_rule_set_arp_sha(rule
, value
->mac
);
1148 cls_rule_set_arp_tha(rule
, value
->mac
);
1153 cls_rule_set_tp_src(rule
, value
->be16
);
1158 cls_rule_set_tp_dst(rule
, value
->be16
);
1161 case MFF_ICMPV4_TYPE
:
1162 case MFF_ICMPV6_TYPE
:
1163 cls_rule_set_icmp_type(rule
, value
->u8
);
1166 case MFF_ICMPV4_CODE
:
1167 case MFF_ICMPV6_CODE
:
1168 cls_rule_set_icmp_code(rule
, value
->u8
);
1172 cls_rule_set_nd_target(rule
, &value
->ipv6
);
1181 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1182 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1185 mf_set_flow_value(const struct mf_field
*mf
,
1186 const union mf_value
*value
, struct flow
*flow
)
1190 flow
->tun_id
= value
->be64
;
1193 flow
->metadata
= value
->be64
;
1197 flow
->in_port
= ntohs(value
->be16
);
1201 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1205 memcpy(flow
->dl_src
, value
->mac
, ETH_ADDR_LEN
);
1209 memcpy(flow
->dl_dst
, value
->mac
, ETH_ADDR_LEN
);
1213 flow
->dl_type
= value
->be16
;
1217 flow
->vlan_tci
= value
->be16
;
1221 flow_set_vlan_vid(flow
, value
->be16
);
1224 case MFF_DL_VLAN_PCP
:
1225 flow_set_vlan_pcp(flow
, value
->u8
);
1229 flow
->nw_src
= value
->be32
;
1233 flow
->nw_dst
= value
->be32
;
1237 flow
->ipv6_src
= value
->ipv6
;
1241 flow
->ipv6_dst
= value
->ipv6
;
1244 case MFF_IPV6_LABEL
:
1245 flow
->ipv6_label
= value
->be32
& ~htonl(IPV6_LABEL_MASK
);
1249 flow
->nw_proto
= value
->u8
;
1253 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1254 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1258 flow
->nw_tos
&= ~IP_ECN_MASK
;
1259 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1263 flow
->nw_ttl
= value
->u8
;
1267 flow
->nw_frag
&= value
->u8
;
1271 flow
->nw_proto
= ntohs(value
->be16
);
1275 flow
->nw_src
= value
->be32
;
1279 flow
->nw_dst
= value
->be32
;
1284 memcpy(flow
->arp_sha
, value
->mac
, ETH_ADDR_LEN
);
1289 memcpy(flow
->arp_tha
, value
->mac
, ETH_ADDR_LEN
);
1294 flow
->tp_src
= value
->be16
;
1299 flow
->tp_dst
= value
->be16
;
1302 case MFF_ICMPV4_TYPE
:
1303 case MFF_ICMPV6_TYPE
:
1304 flow
->tp_src
= htons(value
->u8
);
1307 case MFF_ICMPV4_CODE
:
1308 case MFF_ICMPV6_CODE
:
1309 flow
->tp_dst
= htons(value
->u8
);
1313 flow
->nd_target
= value
->ipv6
;
1322 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1324 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1327 mf_is_zero(const struct mf_field
*mf
, const struct flow
*flow
)
1329 union mf_value value
;
1331 mf_get_value(mf
, flow
, &value
);
1332 return is_all_zeros((const uint8_t *) &value
, mf
->n_bytes
);
1335 /* Makes 'rule' wildcard field 'mf'.
1337 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1340 mf_set_wild(const struct mf_field
*mf
, struct cls_rule
*rule
)
1344 cls_rule_set_tun_id_masked(rule
, htonll(0), htonll(0));
1347 cls_rule_set_metadata_masked(rule
, htonll(0), htonll(0));
1350 rule
->wc
.wildcards
|= FWW_IN_PORT
;
1351 rule
->flow
.in_port
= 0;
1355 cls_rule_set_reg_masked(rule
, mf
->id
- MFF_REG0
, 0, 0);
1359 memset(rule
->flow
.dl_src
, 0, ETH_ADDR_LEN
);
1360 memset(rule
->wc
.dl_src_mask
, 0, ETH_ADDR_LEN
);
1364 memset(rule
->flow
.dl_dst
, 0, ETH_ADDR_LEN
);
1365 memset(rule
->wc
.dl_dst_mask
, 0, ETH_ADDR_LEN
);
1369 rule
->wc
.wildcards
|= FWW_DL_TYPE
;
1370 rule
->flow
.dl_type
= htons(0);
1374 cls_rule_set_dl_tci_masked(rule
, htons(0), htons(0));
1378 cls_rule_set_any_vid(rule
);
1381 case MFF_DL_VLAN_PCP
:
1382 cls_rule_set_any_pcp(rule
);
1387 cls_rule_set_nw_src_masked(rule
, htonl(0), htonl(0));
1392 cls_rule_set_nw_dst_masked(rule
, htonl(0), htonl(0));
1396 memset(&rule
->wc
.ipv6_src_mask
, 0, sizeof rule
->wc
.ipv6_src_mask
);
1397 memset(&rule
->flow
.ipv6_src
, 0, sizeof rule
->flow
.ipv6_src
);
1401 memset(&rule
->wc
.ipv6_dst_mask
, 0, sizeof rule
->wc
.ipv6_dst_mask
);
1402 memset(&rule
->flow
.ipv6_dst
, 0, sizeof rule
->flow
.ipv6_dst
);
1405 case MFF_IPV6_LABEL
:
1406 rule
->wc
.ipv6_label_mask
= 0;
1407 rule
->flow
.ipv6_label
= 0;
1411 rule
->wc
.wildcards
|= FWW_NW_PROTO
;
1412 rule
->flow
.nw_proto
= 0;
1416 rule
->wc
.wildcards
|= FWW_NW_DSCP
;
1417 rule
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1421 rule
->wc
.wildcards
|= FWW_NW_ECN
;
1422 rule
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1426 rule
->wc
.wildcards
|= FWW_NW_TTL
;
1427 rule
->flow
.nw_ttl
= 0;
1431 rule
->wc
.nw_frag_mask
|= FLOW_NW_FRAG_MASK
;
1432 rule
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1436 rule
->wc
.wildcards
|= FWW_NW_PROTO
;
1437 rule
->flow
.nw_proto
= 0;
1442 memset(rule
->flow
.arp_sha
, 0, ETH_ADDR_LEN
);
1443 memset(rule
->wc
.arp_sha_mask
, 0, ETH_ADDR_LEN
);
1448 memset(rule
->flow
.arp_tha
, 0, ETH_ADDR_LEN
);
1449 memset(rule
->wc
.arp_tha_mask
, 0, ETH_ADDR_LEN
);
1454 case MFF_ICMPV4_TYPE
:
1455 case MFF_ICMPV6_TYPE
:
1456 rule
->wc
.tp_src_mask
= htons(0);
1457 rule
->flow
.tp_src
= htons(0);
1462 case MFF_ICMPV4_CODE
:
1463 case MFF_ICMPV6_CODE
:
1464 rule
->wc
.tp_dst_mask
= htons(0);
1465 rule
->flow
.tp_dst
= htons(0);
1469 memset(&rule
->wc
.nd_target_mask
, 0, sizeof rule
->wc
.nd_target_mask
);
1470 memset(&rule
->flow
.nd_target
, 0, sizeof rule
->flow
.nd_target
);
1479 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1480 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1481 * with a 1-bit indicating that the corresponding value bit must match and a
1482 * 0-bit indicating a don't-care.
1484 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1485 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1486 * call is equivalent to mf_set_wild(mf, rule).
1488 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1489 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1491 mf_set(const struct mf_field
*mf
,
1492 const union mf_value
*value
, const union mf_value
*mask
,
1493 struct cls_rule
*rule
)
1495 if (!mask
|| is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
1496 mf_set_value(mf
, value
, rule
);
1498 } else if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
1499 mf_set_wild(mf
, rule
);
1507 case MFF_DL_VLAN_PCP
:
1513 case MFF_ICMPV4_TYPE
:
1514 case MFF_ICMPV4_CODE
:
1515 case MFF_ICMPV6_TYPE
:
1516 case MFF_ICMPV6_CODE
:
1520 cls_rule_set_tun_id_masked(rule
, value
->be64
, mask
->be64
);
1523 cls_rule_set_metadata_masked(rule
, value
->be64
, mask
->be64
);
1527 cls_rule_set_reg_masked(rule
, mf
->id
- MFF_REG0
,
1528 ntohl(value
->be32
), ntohl(mask
->be32
));
1532 cls_rule_set_dl_dst_masked(rule
, value
->mac
, mask
->mac
);
1536 cls_rule_set_dl_src_masked(rule
, value
->mac
, mask
->mac
);
1541 cls_rule_set_arp_sha_masked(rule
, value
->mac
, mask
->mac
);
1546 cls_rule_set_arp_tha_masked(rule
, value
->mac
, mask
->mac
);
1550 cls_rule_set_dl_tci_masked(rule
, value
->be16
, mask
->be16
);
1554 cls_rule_set_nw_src_masked(rule
, value
->be32
, mask
->be32
);
1558 cls_rule_set_nw_dst_masked(rule
, value
->be32
, mask
->be32
);
1562 cls_rule_set_ipv6_src_masked(rule
, &value
->ipv6
, &mask
->ipv6
);
1566 cls_rule_set_ipv6_dst_masked(rule
, &value
->ipv6
, &mask
->ipv6
);
1569 case MFF_IPV6_LABEL
:
1570 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1571 mf_set_value(mf
, value
, rule
);
1573 cls_rule_set_ipv6_label_masked(rule
, value
->be32
, mask
->be32
);
1578 cls_rule_set_nd_target_masked(rule
, &value
->ipv6
, &mask
->ipv6
);
1582 cls_rule_set_nw_frag_masked(rule
, value
->u8
, mask
->u8
);
1586 cls_rule_set_nw_src_masked(rule
, value
->be32
, mask
->be32
);
1590 cls_rule_set_nw_dst_masked(rule
, value
->be32
, mask
->be32
);
1595 cls_rule_set_tp_src_masked(rule
, value
->be16
, mask
->be16
);
1600 cls_rule_set_tp_dst_masked(rule
, value
->be16
, mask
->be16
);
1610 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1614 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1615 } else if (!sf
->n_bits
) {
1616 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1617 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1618 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1619 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1620 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1621 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1622 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1623 sf
->field
->n_bits
, type
, sf
->field
->name
);
1624 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1625 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1626 type
, sf
->field
->name
);
1631 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1634 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1635 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1638 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1640 return mf_check__(sf
, flow
, "source");
1643 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1644 * if so, otherwise an OpenFlow error code (e.g. as returned by
1647 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1649 int error
= mf_check__(sf
, flow
, "destination");
1650 if (!error
&& !sf
->field
->writable
) {
1651 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1653 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1658 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1659 * 'value' and 'mask', respectively. */
1661 mf_get(const struct mf_field
*mf
, const struct cls_rule
*rule
,
1662 union mf_value
*value
, union mf_value
*mask
)
1664 mf_get_value(mf
, &rule
->flow
, value
);
1665 mf_get_mask(mf
, &rule
->wc
, mask
);
1668 /* Assigns a random value for field 'mf' to 'value'. */
1670 mf_random_value(const struct mf_field
*mf
, union mf_value
*value
)
1672 random_bytes(value
, mf
->n_bytes
);
1697 case MFF_ICMPV4_TYPE
:
1698 case MFF_ICMPV4_CODE
:
1699 case MFF_ICMPV6_TYPE
:
1700 case MFF_ICMPV6_CODE
:
1706 case MFF_IPV6_LABEL
:
1707 value
->be32
&= ~htonl(IPV6_LABEL_MASK
);
1711 value
->u8
&= IP_DSCP_MASK
;
1715 value
->u8
&= IP_ECN_MASK
;
1719 value
->u8
&= FLOW_NW_FRAG_MASK
;
1723 value
->be16
&= htons(0xff);
1727 value
->be16
&= htons(VLAN_VID_MASK
);
1730 case MFF_DL_VLAN_PCP
:
1741 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
1742 uint8_t *valuep
, uint8_t *maskp
)
1744 unsigned long long int integer
, mask
;
1749 integer
= strtoull(s
, &tail
, 0);
1750 if (errno
|| (*tail
!= '\0' && *tail
!= '/')) {
1755 mask
= strtoull(tail
+ 1, &tail
, 0);
1756 if (errno
|| *tail
!= '\0') {
1763 for (i
= mf
->n_bytes
- 1; i
>= 0; i
--) {
1764 valuep
[i
] = integer
;
1770 return xasprintf("%s: value too large for %u-byte field %s",
1771 s
, mf
->n_bytes
, mf
->name
);
1776 return xasprintf("%s: bad syntax for %s", s
, mf
->name
);
1780 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
1781 uint8_t mac
[ETH_ADDR_LEN
],
1782 uint8_t mask
[ETH_ADDR_LEN
])
1784 assert(mf
->n_bytes
== ETH_ADDR_LEN
);
1786 switch (sscanf(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
,
1787 ETH_ADDR_SCAN_ARGS(mac
), ETH_ADDR_SCAN_ARGS(mask
))){
1788 case ETH_ADDR_SCAN_COUNT
* 2:
1791 case ETH_ADDR_SCAN_COUNT
:
1792 memset(mask
, 0xff, ETH_ADDR_LEN
);
1796 return xasprintf("%s: invalid Ethernet address", s
);
1801 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
1802 ovs_be32
*ip
, ovs_be32
*mask
)
1806 assert(mf
->n_bytes
== sizeof *ip
);
1808 if (sscanf(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
1809 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
)) == IP_SCAN_COUNT
* 2) {
1811 } else if (sscanf(s
, IP_SCAN_FMT
"/%d",
1812 IP_SCAN_ARGS(ip
), &prefix
) == IP_SCAN_COUNT
+ 1) {
1813 if (prefix
<= 0 || prefix
> 32) {
1814 return xasprintf("%s: network prefix bits not between 1 and "
1816 } else if (prefix
== 32) {
1817 *mask
= htonl(UINT32_MAX
);
1819 *mask
= htonl(((1u << prefix
) - 1) << (32 - prefix
));
1821 } else if (sscanf(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
)) == IP_SCAN_COUNT
) {
1822 *mask
= htonl(UINT32_MAX
);
1824 return xasprintf("%s: invalid IP address", s
);
1830 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
1831 struct in6_addr
*value
, struct in6_addr
*mask
)
1833 char *str
= xstrdup(s
);
1834 char *save_ptr
= NULL
;
1835 const char *name
, *netmask
;
1838 assert(mf
->n_bytes
== sizeof *value
);
1840 name
= strtok_r(str
, "/", &save_ptr
);
1841 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
1845 err
= xasprintf("%s: could not convert to IPv6 address", str
);
1851 netmask
= strtok_r(NULL
, "/", &save_ptr
);
1853 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
1854 int prefix
= atoi(netmask
);
1855 if (prefix
<= 0 || prefix
> 128) {
1857 return xasprintf("%s: prefix bits not between 1 and 128", s
);
1859 *mask
= ipv6_create_mask(prefix
);
1863 *mask
= in6addr_exact
;
1871 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
1872 ovs_be16
*valuep
, ovs_be16
*maskp
)
1876 assert(mf
->n_bytes
== sizeof(ovs_be16
));
1877 if (ofputil_port_from_string(s
, &port
)) {
1878 *valuep
= htons(port
);
1879 *maskp
= htons(UINT16_MAX
);
1882 return mf_from_integer_string(mf
, s
,
1883 (uint8_t *) valuep
, (uint8_t *) maskp
);
1887 struct frag_handling
{
1893 static const struct frag_handling all_frags
[] = {
1894 #define A FLOW_NW_FRAG_ANY
1895 #define L FLOW_NW_FRAG_LATER
1896 /* name mask value */
1899 { "first", A
|L
, A
},
1900 { "later", A
|L
, A
|L
},
1905 { "not_later", L
, 0 },
1912 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
1914 const struct frag_handling
*h
;
1916 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
1917 if (!strcasecmp(s
, h
->name
)) {
1918 /* We force the upper bits of the mask on to make mf_parse_value()
1919 * happy (otherwise it will never think it's an exact match.) */
1920 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
1926 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1927 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
1930 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1931 * NULL if successful, otherwise a malloc()'d string describing the error. */
1933 mf_parse(const struct mf_field
*mf
, const char *s
,
1934 union mf_value
*value
, union mf_value
*mask
)
1936 if (!strcasecmp(s
, "any") || !strcmp(s
, "*")) {
1937 memset(value
, 0, mf
->n_bytes
);
1938 memset(mask
, 0, mf
->n_bytes
);
1942 switch (mf
->string
) {
1944 case MFS_HEXADECIMAL
:
1945 return mf_from_integer_string(mf
, s
,
1946 (uint8_t *) value
, (uint8_t *) mask
);
1949 return mf_from_ethernet_string(mf
, s
, value
->mac
, mask
->mac
);
1952 return mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
1955 return mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
1958 return mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
1961 return mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
1966 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1967 * successful, otherwise a malloc()'d string describing the error. */
1969 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
1971 union mf_value mask
;
1974 error
= mf_parse(mf
, s
, value
, &mask
);
1979 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
1980 return xasprintf("%s: wildcards not allowed here", s
);
1986 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
1987 const uint8_t *maskp
, struct ds
*s
)
1989 unsigned long long int integer
;
1992 assert(mf
->n_bytes
<= 8);
1995 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1996 integer
= (integer
<< 8) | valuep
[i
];
1998 if (mf
->string
== MFS_HEXADECIMAL
) {
1999 ds_put_format(s
, "%#llx", integer
);
2001 ds_put_format(s
, "%lld", integer
);
2005 unsigned long long int mask
;
2008 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2009 mask
= (mask
<< 8) | maskp
[i
];
2012 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2013 * not sure that that a bit-mask written in decimal is ever easier to
2014 * understand than the same bit-mask written in hexadecimal. */
2015 ds_put_format(s
, "/%#llx", mask
);
2020 mf_format_frag_string(const uint8_t *valuep
, const uint8_t *maskp
,
2023 const struct frag_handling
*h
;
2024 uint8_t value
= *valuep
;
2025 uint8_t mask
= *maskp
;
2028 mask
&= FLOW_NW_FRAG_MASK
;
2030 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2031 if (value
== h
->value
&& mask
== h
->mask
) {
2032 ds_put_cstr(s
, h
->name
);
2036 ds_put_cstr(s
, "<error>");
2039 /* Appends to 's' a string representation of field 'mf' whose value is in
2040 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2042 mf_format(const struct mf_field
*mf
,
2043 const union mf_value
*value
, const union mf_value
*mask
,
2047 if (is_all_zeros((const uint8_t *) mask
, mf
->n_bytes
)) {
2048 ds_put_cstr(s
, "ANY");
2050 } else if (is_all_ones((const uint8_t *) mask
, mf
->n_bytes
)) {
2055 switch (mf
->string
) {
2058 ofputil_format_port(ntohs(value
->be16
), s
);
2063 case MFS_HEXADECIMAL
:
2064 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2068 eth_format_masked(value
->mac
, mask
->mac
, s
);
2072 ip_format_masked(value
->be32
, mask
? mask
->be32
: htonl(UINT32_MAX
),
2077 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2081 mf_format_frag_string(&value
->u8
, &mask
->u8
, s
);
2089 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2090 * least-significant bits in 'x'.
2093 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2094 struct cls_rule
*rule
)
2096 const struct mf_field
*field
= sf
->field
;
2097 union mf_value value
, mask
;
2099 mf_get(field
, rule
, &value
, &mask
);
2100 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2101 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2102 mf_set(field
, &value
, &mask
, rule
);
2105 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2106 * reading 'flow', e.g. as checked by mf_check_src(). */
2108 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2109 union mf_subvalue
*x
)
2111 union mf_value value
;
2113 mf_get_value(sf
->field
, flow
, &value
);
2115 memset(x
, 0, sizeof *x
);
2116 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2121 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2122 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2125 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2127 union mf_value value
;
2129 mf_get_value(sf
->field
, flow
, &value
);
2130 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2133 /* Formats 'sf' into 's' in a format normally acceptable to
2134 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2135 * sf->field has no NXM name.) */
2137 mf_format_subfield(const struct mf_subfield
*sf
, struct ds
*s
)
2140 ds_put_cstr(s
, "<unknown>");
2141 } else if (sf
->field
->nxm_name
) {
2142 ds_put_cstr(s
, sf
->field
->nxm_name
);
2143 } else if (sf
->field
->nxm_header
) {
2144 uint32_t header
= sf
->field
->nxm_header
;
2145 ds_put_format(s
, "%d:%d", NXM_VENDOR(header
), NXM_FIELD(header
));
2147 ds_put_cstr(s
, sf
->field
->name
);
2150 if (sf
->field
&& sf
->ofs
== 0 && sf
->n_bits
== sf
->field
->n_bits
) {
2151 ds_put_cstr(s
, "[]");
2152 } else if (sf
->n_bits
== 1) {
2153 ds_put_format(s
, "[%d]", sf
->ofs
);
2155 ds_put_format(s
, "[%d..%d]", sf
->ofs
, sf
->ofs
+ sf
->n_bits
- 1);
2159 static const struct mf_field
*
2160 mf_parse_subfield_name(const char *name
, int name_len
, bool *wild
)
2164 *wild
= name_len
> 2 && !memcmp(&name
[name_len
- 2], "_W", 2);
2169 for (i
= 0; i
< MFF_N_IDS
; i
++) {
2170 const struct mf_field
*mf
= mf_from_id(i
);
2173 && !strncmp(mf
->nxm_name
, name
, name_len
)
2174 && mf
->nxm_name
[name_len
] == '\0') {
2178 && !strncmp(mf
->oxm_name
, name
, name_len
)
2179 && mf
->oxm_name
[name_len
] == '\0') {
2187 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2188 * returns NULL and advances '*sp' to the first byte following the parsed
2189 * string. On failure, returns a malloc()'d error message, does not modify
2190 * '*sp', and does not properly initialize 'sf'.
2192 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2193 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2194 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2195 * may both be omitted (the [] are still required) to indicate an entire
2198 mf_parse_subfield__(struct mf_subfield
*sf
, const char **sp
)
2200 const struct mf_field
*field
;
2209 name_len
= strcspn(s
, "[");
2210 if (s
[name_len
] != '[') {
2211 return xasprintf("%s: missing [ looking for field name", *sp
);
2214 field
= mf_parse_subfield_name(name
, name_len
, &wild
);
2216 return xasprintf("%s: unknown field `%.*s'", *sp
, name_len
, s
);
2220 if (sscanf(s
, "[%d..%d]", &start
, &end
) == 2) {
2221 /* Nothing to do. */
2222 } else if (sscanf(s
, "[%d]", &start
) == 1) {
2224 } else if (!strncmp(s
, "[]", 2)) {
2226 end
= field
->n_bits
- 1;
2228 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2229 "[<start>..<end>]", *sp
);
2231 s
= strchr(s
, ']') + 1;
2234 return xasprintf("%s: starting bit %d is after ending bit %d",
2236 } else if (start
>= field
->n_bits
) {
2237 return xasprintf("%s: starting bit %d is not valid because field is "
2238 "only %d bits wide", *sp
, start
, field
->n_bits
);
2239 } else if (end
>= field
->n_bits
){
2240 return xasprintf("%s: ending bit %d is not valid because field is "
2241 "only %d bits wide", *sp
, end
, field
->n_bits
);
2246 sf
->n_bits
= end
- start
+ 1;
2252 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2253 * byte in 's' following the parsed string.
2255 * Exits with an error message if 's' has incorrect syntax.
2257 * The syntax parsed from 's' takes the form "header[start..end]" where
2258 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2259 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2260 * may both be omitted (the [] are still required) to indicate an entire
2263 mf_parse_subfield(struct mf_subfield
*sf
, const char *s
)
2265 char *msg
= mf_parse_subfield__(sf
, &s
);
2267 ovs_fatal(0, "%s", msg
);