From 9ea0bccc83a84458d934966eae15c78f3ee2bac8 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 5 Oct 2011 10:42:34 -0700 Subject: [PATCH] datapath-protocol: Use Linux kernel types directly. We want datapath-protocol.h to be acceptable as a Linux kernel header, so it must use Linux kernel types and must not have references to Open vSwitch symbols or header files. This commit primarily makes that change to datapath-protocol.h. At the same time, at least for now we also want datapath-protocol.h to be usable on non-Linux platforms, so we need some kind of compatiblity. Thus, this commit also introduces a header file that defines the necessary Linux kernel types on non-Linux platforms. In turn, this requires openvswitch/types.h to use the Linux types directly for ovs_be; otherwise, sparse complains because now __be and ovs_be are incompatible from its perspective, so this commit makes that change too. I don't have a non-Linux kernel platform readily available, so I only tested the non-Linux part of the linux/types.h substitute by forcing that case to be triggered with #if 0. It worked, except for errors in actual Linux kernel headers included explicitly from OVS source files, so I think it's likely to work in practice. Bug #7559. Signed-off-by: Ben Pfaff Acked-by: Jesse Gross --- configure.ac | 2 +- include/automake.mk | 1 + include/linux/automake.mk | 1 + include/linux/types.h | 47 ++++++++++++ include/openvswitch/datapath-protocol.h | 97 ++++++++++++------------- include/openvswitch/types.h | 12 +-- lib/dpif-linux.h | 1 + 7 files changed, 103 insertions(+), 58 deletions(-) create mode 100644 include/linux/automake.mk create mode 100644 include/linux/types.h diff --git a/configure.ac b/configure.ac index 7387d826..e771b50f 100644 --- a/configure.ac +++ b/configure.ac @@ -56,7 +56,7 @@ OVS_CHECK_STRTOK_R AC_CHECK_MEMBERS([struct stat.st_mtim.tv_nsec, struct stat.st_mtimensec], [], [], [[#include ]]) AC_CHECK_FUNCS([mlockall strnlen strsignal getloadavg statvfs setmntent]) -AC_CHECK_HEADERS([mntent.h sys/statvfs.h]) +AC_CHECK_HEADERS([mntent.h sys/statvfs.h linux/types.h]) OVS_CHECK_PKIDIR OVS_CHECK_RUNDIR diff --git a/include/automake.mk b/include/automake.mk index 9ec5b72e..f34e5c9b 100644 --- a/include/automake.mk +++ b/include/automake.mk @@ -1,3 +1,4 @@ +include include/linux/automake.mk include include/openflow/automake.mk include include/openvswitch/automake.mk include include/sparse/automake.mk diff --git a/include/linux/automake.mk b/include/linux/automake.mk new file mode 100644 index 00000000..aa7e7392 --- /dev/null +++ b/include/linux/automake.mk @@ -0,0 +1 @@ +noinst_HEADERS += include/linux/types.h diff --git a/include/linux/types.h b/include/linux/types.h new file mode 100644 index 00000000..845924ec --- /dev/null +++ b/include/linux/types.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2011 Nicira Networks. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LINUX_TYPES_H +#define LINUX_TYPES_H 1 + +/* On Linux, this header file just includes . + * + * On other platforms, this header file implements just enough of + * to allow datapath-protocol.h to work, that is, it defines + * the __u and __be types. */ + +#if __KERNEL__ || HAVE_LINUX_TYPES_H +#include_next +#else /* no */ +#include + +#ifdef __CHECKER__ +#define __bitwise__ __attribute__((bitwise)) +#else +#define __bitwise__ +#endif + +typedef uint8_t __u8; +typedef uint16_t __u16; +typedef uint32_t __u32; +typedef uint64_t __u64; + +typedef uint16_t __bitwise__ __be16; +typedef uint32_t __bitwise__ __be32; +typedef uint64_t __bitwise__ __be64; +#endif /* no */ + +#endif /* */ diff --git a/include/openvswitch/datapath-protocol.h b/include/openvswitch/datapath-protocol.h index b522edc9..07435beb 100644 --- a/include/openvswitch/datapath-protocol.h +++ b/include/openvswitch/datapath-protocol.h @@ -40,15 +40,8 @@ #ifndef OPENVSWITCH_DATAPATH_PROTOCOL_H #define OPENVSWITCH_DATAPATH_PROTOCOL_H 1 -#ifdef __KERNEL__ #include -#define ovs_be16 __be16 -#define ovs_be32 __be32 -#define ovs_be64 __be64 -#else -#include "openvswitch/types.h" -#endif - + /* datapaths. */ #define OVS_DATAPATH_FAMILY "ovs_datapath" @@ -119,26 +112,26 @@ enum ovs_datapath_frag { }; struct ovs_dp_stats { - uint64_t n_frags; /* Number of dropped IP fragments. */ - uint64_t n_hit; /* Number of flow table matches. */ - uint64_t n_missed; /* Number of flow table misses. */ - uint64_t n_lost; /* Number of misses not sent to userspace. */ - uint64_t n_flows; /* Number of flows present */ + __u64 n_frags; /* Number of dropped IP fragments. */ + __u64 n_hit; /* Number of flow table matches. */ + __u64 n_missed; /* Number of flow table misses. */ + __u64 n_lost; /* Number of misses not sent to userspace. */ + __u64 n_flows; /* Number of flows present */ }; struct ovs_vport_stats { - uint64_t rx_packets; /* total packets received */ - uint64_t tx_packets; /* total packets transmitted */ - uint64_t rx_bytes; /* total bytes received */ - uint64_t tx_bytes; /* total bytes transmitted */ - uint64_t rx_errors; /* bad packets received */ - uint64_t tx_errors; /* packet transmit problems */ - uint64_t rx_dropped; /* no space in linux buffers */ - uint64_t tx_dropped; /* no space available in linux */ + __u64 rx_packets; /* total packets received */ + __u64 tx_packets; /* total packets transmitted */ + __u64 rx_bytes; /* total bytes received */ + __u64 tx_bytes; /* total bytes transmitted */ + __u64 rx_errors; /* bad packets received */ + __u64 tx_errors; /* packet transmit problems */ + __u64 rx_dropped; /* no space in linux buffers */ + __u64 tx_dropped; /* no space available in linux */ }; /* Logical ports. */ -#define OVSP_LOCAL ((uint16_t)0) +#define OVSP_LOCAL ((__u16)0) #define OVS_PACKET_FAMILY "ovs_packet" @@ -273,8 +266,8 @@ enum ovs_flow_cmd { }; struct ovs_flow_stats { - uint64_t n_packets; /* Number of matched packets. */ - uint64_t n_bytes; /* Number of matched bytes. */ + __u64 n_packets; /* Number of matched packets. */ + __u64 n_bytes; /* Number of matched bytes. */ }; enum ovs_key_attr { @@ -298,61 +291,61 @@ enum ovs_key_attr { #define OVS_KEY_ATTR_MAX (__OVS_KEY_ATTR_MAX - 1) struct ovs_key_ethernet { - uint8_t eth_src[6]; - uint8_t eth_dst[6]; + __u8 eth_src[6]; + __u8 eth_dst[6]; }; struct ovs_key_8021q { - ovs_be16 q_tpid; - ovs_be16 q_tci; + __be16 q_tpid; + __be16 q_tci; }; struct ovs_key_ipv4 { - ovs_be32 ipv4_src; - ovs_be32 ipv4_dst; - uint8_t ipv4_proto; - uint8_t ipv4_tos; + __be32 ipv4_src; + __be32 ipv4_dst; + __u8 ipv4_proto; + __u8 ipv4_tos; }; struct ovs_key_ipv6 { - ovs_be32 ipv6_src[4]; - ovs_be32 ipv6_dst[4]; - uint8_t ipv6_proto; - uint8_t ipv6_tos; + __be32 ipv6_src[4]; + __be32 ipv6_dst[4]; + __u8 ipv6_proto; + __u8 ipv6_tos; }; struct ovs_key_tcp { - ovs_be16 tcp_src; - ovs_be16 tcp_dst; + __be16 tcp_src; + __be16 tcp_dst; }; struct ovs_key_udp { - ovs_be16 udp_src; - ovs_be16 udp_dst; + __be16 udp_src; + __be16 udp_dst; }; struct ovs_key_icmp { - uint8_t icmp_type; - uint8_t icmp_code; + __u8 icmp_type; + __u8 icmp_code; }; struct ovs_key_icmpv6 { - uint8_t icmpv6_type; - uint8_t icmpv6_code; + __u8 icmpv6_type; + __u8 icmpv6_code; }; struct ovs_key_arp { - ovs_be32 arp_sip; - ovs_be32 arp_tip; - ovs_be16 arp_op; - uint8_t arp_sha[6]; - uint8_t arp_tha[6]; + __be32 arp_sip; + __be32 arp_tip; + __be16 arp_op; + __u8 arp_sha[6]; + __u8 arp_tha[6]; }; struct ovs_key_nd { - uint32_t nd_target[4]; - uint8_t nd_sll[6]; - uint8_t nd_tll[6]; + __u32 nd_target[4]; + __u8 nd_sll[6]; + __u8 nd_tll[6]; }; /** diff --git a/include/openvswitch/types.h b/include/openvswitch/types.h index 4d2a1e9a..90a32f55 100644 --- a/include/openvswitch/types.h +++ b/include/openvswitch/types.h @@ -17,6 +17,7 @@ #ifndef OPENVSWITCH_TYPES_H #define OPENVSWITCH_TYPES_H 1 +#include #include #include @@ -31,11 +32,12 @@ /* The ovs_be types indicate that an object is in big-endian, not * native-endian, byte order. They are otherwise equivalent to uint_t. * - * The OVS_BITWISE annotation allows the sparse checker to issue warnings - * for incorrect use of values in network byte order. */ -typedef uint16_t OVS_BITWISE ovs_be16; -typedef uint32_t OVS_BITWISE ovs_be32; -typedef uint64_t OVS_BITWISE ovs_be64; + * We bootstrap these from the Linux __be types. If we instead define our + * own independently then __be and ovs_be become mutually + * incompatible. */ +typedef __be16 ovs_be16; +typedef __be32 ovs_be32; +typedef __be64 ovs_be64; /* Netlink and OpenFlow both contain 64-bit values that are only guaranteed to * be aligned on 32-bit boundaries. These types help. diff --git a/lib/dpif-linux.h b/lib/dpif-linux.h index a9f8bfea..ced869fb 100644 --- a/lib/dpif-linux.h +++ b/lib/dpif-linux.h @@ -18,6 +18,7 @@ #define DPIF_LINUX_H 1 #include +#include #include #include "openvswitch/datapath-protocol.h" -- 2.30.2