diff options
Diffstat (limited to 'lib/includes')
26 files changed, 1466 insertions, 2126 deletions
diff --git a/lib/includes/CMakeLists.txt b/lib/includes/CMakeLists.txt index 392c2c94e..61af7eca8 100644 --- a/lib/includes/CMakeLists.txt +++ b/lib/includes/CMakeLists.txt @@ -24,29 +24,19 @@ set(LIBHICN_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/hicn/hicn.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/base.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/common.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/compat.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/error.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/face.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/header.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/mapme.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/name.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/packet.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/policy.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/ops.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/strategy.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/validation.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/interest_manifest.h PARENT_SCOPE ) -set(LIBHICN_HEADER_FILES_PROTOCOL - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ah.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/icmp.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/icmprd.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ipv4.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ipv6.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/tcp.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/udp.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/new.h +set(LIBHICN_HEADER_FILES_UTIL ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/array.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/bitmap.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/hash.h @@ -63,3 +53,6 @@ set(LIBHICN_HEADER_FILES_PROTOCOL ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/vector.h PARENT_SCOPE ) + +set_property(GLOBAL PROPERTY LIBHICN_HEADER_FILES_UTIL_PROPERTY "${LIBHICN_HEADER_FILES_UTIL}") + diff --git a/lib/includes/hicn/base.h b/lib/includes/hicn/base.h index b825619b7..0a6ac7321 100644 --- a/lib/includes/hicn/base.h +++ b/lib/includes/hicn/base.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -32,8 +32,16 @@ /* Default header fields */ #define HICN_DEFAULT_TTL 254 +#define SYMBOLIC_NAME_LEN 16 + +/* hICN attribute types */ + +/* Face id */ + typedef u32 hicn_faceid_t; -typedef u8 hicn_pathlabel_t; + +/* Lifetime */ + typedef u32 hicn_lifetime_t; #define HICN_MAX_LIFETIME_SCALED 0xFFFF @@ -56,6 +64,9 @@ typedef u32 hicn_lifetime_t; * currently used by an hypothetical signed MAP-Me update : * [IPPROTO_ICMPRD, IPPROTO_AH, IPPROTO_ICMP, IPPROTO_IPV6] */ + +#define HICN_FORMAT_LEN 4 + typedef union { /** protocol layers representation */ @@ -82,82 +93,166 @@ typedef union }; /** u32 representation */ u32 as_u32; -} hicn_type_t; + u8 as_u8[HICN_FORMAT_LEN]; +} hicn_packet_format_t; /* Common protocol layers */ /* Common protocol layers */ #ifndef _WIN32 -#define HICN_TYPE(x, y, z, t) \ - (hicn_type_t) \ +#define HICN_PACKET_FORMAT(x, y, z, t) \ + (hicn_packet_format_t) \ { \ { \ .l1 = x, .l2 = y, .l3 = z, .l4 = t \ } \ } #else -inline hicn_type_t -HICN_TYPE (int x, int y, int z, int t) +inline const hicn_packet_format_t +HICN_PACKET_FORMAT (int x, int y, int z, int t) { - hicn_type_t type; - type.l1 = x; - type.l2 = y; - type.l3 = z; - type.l4 = t; - return type; + hicn_packet_format_t format; + format.l1 = x; + format.l2 = y; + format.l3 = z; + format.l4 = t; + return format; } #endif -#define HICN_TYPE_IPV4_TCP \ - HICN_TYPE (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV4_ICMP \ - HICN_TYPE (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV6_TCP \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV6_ICMP \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_NEW \ - HICN_TYPE (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV4_UDP \ - HICN_TYPE (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) -#define HICN_TYPE_IPV6_UDP \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) -#define HICN_TYPE_IPV4_TCP_AH \ - HICN_TYPE (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV4_ICMP_AH \ - HICN_TYPE (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV6_TCP_AH \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV6_ICMP_AH \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_NEW_AH \ - HICN_TYPE (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV6_UDP_AH \ - HICN_TYPE (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) -#define HICN_TYPE_IPV4_UDP_AH \ - HICN_TYPE (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) -#define HICN_TYPE_NONE \ - HICN_TYPE (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +extern const char *const _protocol_str[]; + +#define protocol_str(x) protocol_str[x] + +int hicn_packet_format_snprintf (char *s, size_t size, + hicn_packet_format_t format); + +#define MAXSZ_HICN_PACKET_FORMAT 4 * 4 + 3 // ICMP/ICMP/ICMP/ICMP + +#if !defined(__cplusplus) +#define constexpr const +#endif + +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_TCP = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_ICMP = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_TCP = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_ICMP = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, + IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NEW = + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_UDP = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_UDP = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_TCP_AH = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_ICMP_AH = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_TCP_AH = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_ICMP_AH = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NEW_AH = + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV6_UDP_AH = + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_IPV4_UDP_AH = + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH); +static constexpr hicn_packet_format_t HICN_PACKET_FORMAT_NONE = + HICN_PACKET_FORMAT (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE); + +/** + * @brief Return the hICN format with an additional AH header + * @param [in] format - hICN packet format + * @return Updated hICN packet format + */ +static inline hicn_packet_format_t +hicn_get_ah_format (hicn_packet_format_t format) +{ + hicn_packet_format_t ret = format; + for (unsigned i = 0; i < HICN_FORMAT_LEN; i++) + { + switch (ret.as_u8[i]) + { + case IPPROTO_AH: + return ret; + case IPPROTO_NONE: + ret.as_u8[i] = IPPROTO_AH; + return ret; + default: + break; + } + } + return ret; +} + +/* + * MAX(IPV4_HDRLEN (20), IPV6_HDRLEN (40)) + * + MAX (TCP_HDRLEN (20), UDP_HDRLEN (8), ICMP_HDRLEN (8), NEW_HDRLEN (32)) + * + AH_HDRLEN + */ +#define HICN_HDRLEN_MAX 72 /** * @brief Check if type is none. * @return 1 if none, 0 otherwise */ static inline int -hicn_type_is_none (hicn_type_t type) +hicn_type_is_none (hicn_packet_format_t format) { - return (type.l1 == IPPROTO_NONE) && (type.l2 == IPPROTO_NONE) && - (type.l3 == IPPROTO_NONE) && (type.l4 == IPPROTO_NONE); + return (format.l1 == IPPROTO_NONE) && (format.l2 == IPPROTO_NONE) && + (format.l3 == IPPROTO_NONE) && (format.l4 == IPPROTO_NONE); } +#define _is_ipv4(format) (format.l1 == IPPROTO_IP) +#define _is_ipv6(format) (format.l1 == IPPROTO_IPV6) +#define _is_tcp(format) (format.l2 == IPPROTO_TCP) +#define _is_udp(format) (format.l2 == IPPROTO_UDP) +#define _is_icmp(format) \ + ((format.l2 == IPPROTO_ICMP) || (format.l2 == IPPROTO_ICMPV6)) +#define _is_cmpr(format) ((format & HFO_CMPR) >> 5) +#define _is_ah(format) \ + ((format.l1 == IPPROTO_AH) || (format.l2 == IPPROTO_AH) || \ + (format.l3 == IPPROTO_AH)) + +/* + * @brief hICN packet types + * + * probes are like normal interest & data but: + * - interests use BFD port as the destination + * - data use BFD port as the source + expiry time must be 0. + * if any of these conditions is not met, the packet is still matched as an + * interest or data packet. + * + */ + +#define foreach_packet_type \ + _ (UNDEFINED) \ + _ (INTEREST) \ + _ (DATA) \ + _ (WLDR_NOTIFICATION) \ + _ (MAPME) \ + _ (PROBE) \ + _ (COMMAND) \ + _ (N) + /** * @brief hICN Packet type */ typedef enum { - HICN_PACKET_TYPE_INTEREST, - HICN_PACKET_TYPE_DATA, - HICN_PACKET_N_TYPE, +#define _(x) HICN_PACKET_TYPE_##x, + foreach_packet_type +#undef _ } hicn_packet_type_t; +#undef foreach_type + +extern const char *_hicn_packet_type_str[]; + +#define hicn_packet_type_str(x) _hicn_packet_type_str[x] /** * @brief hICN Payload type @@ -172,60 +267,11 @@ typedef enum HPT_UNSPEC = 999 } hicn_payload_type_t; -/*************************************************************** - * Interest Manifest - ***************************************************************/ - -#define MAX_SUFFIXES_IN_MANIFEST 255 -#define WORD_WIDTH (sizeof (uint32_t) * 8) -#define BITMAP_SIZE ((MAX_SUFFIXES_IN_MANIFEST + 1) / WORD_WIDTH) - -typedef struct -{ - /* This can be 16 bits, but we use 32 bits for alignment */ - uint32_t n_suffixes; - - uint32_t request_bitmap[BITMAP_SIZE]; - - /* Followed by the list of prefixes to ask */ - /* ... */ -} interest_manifest_header_t; - -// Bitmap operations - -static inline void -set_bit (uint32_t *bitmap, int i) -{ - size_t offset = i / WORD_WIDTH; - size_t pos = i % WORD_WIDTH; - bitmap[offset] |= ((uint32_t) 1 << pos); -} - -static inline void -unset_bit (uint32_t *bitmap, int i) -{ - size_t offset = i / WORD_WIDTH; - size_t pos = i % WORD_WIDTH; - bitmap[offset] &= ~((uint32_t) 1 << pos); -} +/* Path label */ -static inline bool -is_bit_set (const uint32_t *bitmap, int i) -{ - size_t offset = i / WORD_WIDTH; - size_t pos = i % WORD_WIDTH; - return bitmap[offset] & ((uint32_t) 1 << pos); -} +typedef u8 hicn_path_label_t; -static inline void -bitmap_print (u32 *bitmap, size_t n_words) -{ - for (size_t word = 0; word < n_words; word++) - { - for (int bit = 31; bit >= 0; bit--) - (is_bit_set (&bitmap[word], bit)) ? printf ("1") : printf ("0"); - } -} +#define INVALID_PATH_LABEL 0 /** * @brief Path label computations @@ -237,28 +283,28 @@ bitmap_print (u32 *bitmap, size_t n_words) * NOTE: this computation is not (yet) part of the hICN specification. */ -#define HICN_PATH_LABEL_MASK 0x000000ff -#define HICN_PATH_LABEL_SIZE 8 +#define HICN_PATH_LABEL_MASK 0x000000ff +#define HICN_PATH_LABEL_SIZE_BITS sizeof (hicn_path_label_t) * 8 /** * @brief Path label update - * @param [in] current_label Current pathlabel + * @param [in] current_label Current path_label * @param [in] face_id The face identifier to combine into the path label - * @param [out] new_label Computed pathlabel + * @param [out] new_label Computed path_label * * This function updates the current_label based on the new face_id, and * returns */ static inline void -update_pathlabel (hicn_pathlabel_t current_label, hicn_faceid_t face_id, - hicn_pathlabel_t *new_label) +update_path_label (hicn_path_label_t current_label, hicn_faceid_t face_id, + hicn_path_label_t *new_label) { - hicn_pathlabel_t pl_face_id = - (hicn_pathlabel_t) (face_id & HICN_PATH_LABEL_MASK); + hicn_path_label_t pl_face_id = + (hicn_path_label_t) (face_id & HICN_PATH_LABEL_MASK); - *new_label = - ((current_label << 1) | (current_label >> (HICN_PATH_LABEL_SIZE - 1))) ^ - pl_face_id; + *new_label = ((current_label << 1) | + (current_label >> (HICN_PATH_LABEL_SIZE_BITS - 1))) ^ + pl_face_id; } /*************************************************************** diff --git a/lib/includes/hicn/common.h b/lib/includes/hicn/common.h index 1998099db..a84124617 100644 --- a/lib/includes/hicn/common.h +++ b/lib/includes/hicn/common.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -36,25 +36,11 @@ #include <stdint.h> #include <assert.h> -/* Concise type definitions */ +#include <hicn/util/types.h> -typedef uint64_t u64; -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; - -/* - * Code annotations - * - * NOTE: these are defined by default in VPP. - */ - -#ifndef HICN_VPP_PLUGIN - -#define PREDICT_FALSE(x) (x) -#define PREDICT_TRUE(x) (x) -#define STRUCT_SIZE_OF(type, member) sizeof (((type *) 0)->member) -#define ASSERT +#define HICN_EXPECT_FALSE(x) __builtin_expect ((x), 1) +#define HICN_EXPECT_TRUE(x) __builtin_expect ((x), 0) +#define HICN_UNUSED(x) x __attribute__ ((unused)) #ifndef NDEBUG #define _ASSERT(x) assert (x) @@ -62,36 +48,6 @@ typedef uint8_t u8; #define _ASSERT(x) ((void) (x)) #endif -#define STATIC_ASSERT(x) - -/* Architecture-dependent uword size */ -#if INTPTR_MAX == INT64_MAX -#define log2_uword_bits 6 -#elif INTPTR_MAX == INT32_MAX -#define log2_uword_bits 5 -#else -#error "Impossible to detect architecture" -#endif - -#define uword_bits (1 << log2_uword_bits) - -/* Word types. */ -#if uword_bits == 64 -/* 64 bit word machines. */ -typedef u64 uword; -#else -/* 32 bit word machines. */ -typedef u32 uword; -#endif - -typedef uword ip_csum_t; - -#else - -#include <vppinfra/clib.h> - -#endif /* ! HICN_VPP_PLUGIN */ - /* * Windows compilers do not support named initilizers when .h files are * included inside C++ files. For readability, we either use the following @@ -178,11 +134,11 @@ int get_addr_family (const char *ip_address); */ static inline u16 -ip_csum_fold (ip_csum_t c) +ip_csum_fold (hicn_ip_csum_t c) { /* Reduce to 16 bits. */ #if uword_bits == 64 - c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32); + c = (c & (hicn_ip_csum_t) 0xffffffff) + (c >> (hicn_ip_csum_t) 32); c = (c & 0xffff) + (c >> 16); #endif @@ -192,18 +148,18 @@ ip_csum_fold (ip_csum_t c) return (u16) c; } -static inline ip_csum_t -ip_csum_with_carry (ip_csum_t sum, ip_csum_t x) +static inline hicn_ip_csum_t +ip_csum_with_carry (hicn_ip_csum_t sum, hicn_ip_csum_t x) { - ip_csum_t t = sum + x; + hicn_ip_csum_t t = sum + x; return t + (t < x); } /* Update checksum changing field at even byte offset from x -> 0. */ -static inline ip_csum_t -ip_csum_add_even (ip_csum_t c, ip_csum_t x) +static inline hicn_ip_csum_t +ip_csum_add_even (hicn_ip_csum_t c, hicn_ip_csum_t x) { - ip_csum_t d; + hicn_ip_csum_t d; d = c - x; @@ -214,8 +170,8 @@ ip_csum_add_even (ip_csum_t c, ip_csum_t x) } /* Update checksum changing field at even byte offset from 0 -> x. */ -static inline ip_csum_t -ip_csum_sub_even (ip_csum_t c, ip_csum_t x) +static inline hicn_ip_csum_t +ip_csum_sub_even (hicn_ip_csum_t c, hicn_ip_csum_t x) { return ip_csum_with_carry (c, x); } @@ -265,8 +221,19 @@ csum (const void *addr, size_t size, u16 init) * Query IP version from packet (either 4 or 6) * (version is located as same offsets in both protocol headers) */ -#define HICN_IP_VERSION(packet) \ - ((hicn_header_t *) packet)->protocol.ipv4.version +typedef struct +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + u8 dummy : 4; + u8 version : 4; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + u8 version : 4; + u8 dummy : 4; +#else +#error "Unsupported endianness" +#endif +} ip_version_t; +#define HICN_IP_VERSION(packet) ((ip_version_t *) packet)->version #ifndef ntohll static inline uint64_t @@ -296,9 +263,9 @@ htonll (uint64_t input) } #endif -#define round_pow2(x, pow2) (((x) + (pow2) -1) & ~((pow2) -1)) +#define hicn_round_pow2(x, pow2) (((x) + (pow2) -1) & ~((pow2) -1)) -#define _SIZEOF_ALIGNED(x, size) round_pow2 (sizeof (x), size) +#define _SIZEOF_ALIGNED(x, size) hicn_round_pow2 (sizeof (x), size) #define SIZEOF_ALIGNED(x) _SIZEOF_ALIGNED (x, sizeof (void *)) /* Definitions for builtins unavailable on MSVC */ @@ -336,6 +303,7 @@ uint32_t __inline __builtin_clzl2 (uint64_t value) #endif #define next_pow2(x) (x <= 1 ? 1 : 1ul << (64 - __builtin_clzl (x - 1))) +#define _unused(x) ((void) (x)) #endif /* HICN_COMMON_H */ diff --git a/lib/includes/hicn/compat.h b/lib/includes/hicn/compat.h deleted file mode 100644 index 98c035b57..000000000 --- a/lib/includes/hicn/compat.h +++ /dev/null @@ -1,529 +0,0 @@ -/* - * Copyright (c) 2021 Cisco and/or its affiliates. - * 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. - */ - -/** - * @file compat.h - * @brief Implementation of the compatibility layer. - * - * The structure of the core API has evolved to support operations of a variety - * of packet formats in addition to IPv4/TCP and IPv6/TCP, namely with the use - * of ICMP for signalization and AH headers for integrity. The new API format - * has been designed to scale better with the multiplicity of packet formats, - * and provide a unified interface on top. We maintain an interface for the - * former API in this file, which mainly acts as a wrapper on top of new calls. - */ -#ifndef HICN_COMPAT_H -#define HICN_COMPAT_H - -#include "common.h" -#include "header.h" -#include "name.h" - -/* HICN format options */ -#define HFO_INET 1 << 0 -#define HFO_INET6 1 << 1 -#define HFO_TCP 1 << 2 -#define HFO_UDP 1 << 3 -#define HFO_ICMP 1 << 4 -#define HFO_CMPR 1 << 5 -#define HFO_AH 1 << 6 - -#define _is_ipv4(format) ((format & HFO_INET)) -#define _is_ipv6(format) ((format & HFO_INET6) >> 1) -#define _is_tcp(format) ((format & HFO_TCP) >> 2) -#define _is_udp(format) ((format & HFO_UDP) >> 3) -#define _is_icmp(format) ((format & HFO_ICMP) >> 4) -#define _is_cmpr(format) ((format & HFO_CMPR) >> 5) -#define _is_ah(format) ((format & HFO_AH) >> 6) - -typedef enum -{ - HF_UNSPEC = 0, - HF_INET_TCP = HFO_INET | HFO_TCP, - HF_INET6_TCP = HFO_INET6 | HFO_TCP, - HF_INET_ICMP = HFO_INET | HFO_ICMP, - HF_INET6_ICMP = HFO_INET6 | HFO_ICMP, - HF_NEW = HFO_CMPR, - HF_INET_UDP = HFO_INET | HFO_UDP | HFO_CMPR, - HF_INET6_UDP = HFO_INET6 | HFO_UDP | HFO_CMPR, - HF_INET_TCP_AH = HFO_INET | HFO_TCP | HFO_AH, - HF_INET6_TCP_AH = HFO_INET6 | HFO_TCP | HFO_AH, - HF_INET_ICMP_AH = HFO_INET | HFO_ICMP | HFO_AH, - HF_INET6_ICMP_AH = HFO_INET6 | HFO_ICMP | HFO_AH, - HF_NEW_AH = HFO_CMPR | HFO_AH, - HF_INET_UDP_AH = HFO_INET | HFO_UDP | HFO_CMPR | HFO_AH, - HF_INET6_UDP_AH = HFO_INET6 | HFO_UDP | HFO_CMPR | HFO_AH, -} hicn_format_t; - -/** - * @brief Add AH header to current format. E.g. if format is IP + TCP, this - * will change it to IP = TCP + AH - * - * @param [in] format - The input format - * @return The format with the AH bit set to 1 - */ -static inline hicn_format_t -hicn_get_ah_format (hicn_format_t format) -{ - return (hicn_format_t) (format | HFO_AH); -} - -/** - * Minimum required header length to determine the type and length of a - * supposed hICN packet. This should be equal to the maximum value over all - * possible hICN packet formats, and less than the minimum possible IP packet - * size. - */ -#define HICN_V6_MIN_HDR_LEN 6 /* bytes */ -#define HICN_V4_MIN_HDR_LEN 4 /* bytes */ - -// #define HICN_MIN_HDR_LEN ((HICN_V6_MIN_HDR_LEN > HICN_V4_MIN_HDR_LEN) ? -// HICN_V6_MIN_HDR_LEN : HICN_V4_MIN_HDR_LEN) -#define HICN_MIN_HDR_LEN HICN_V6_MIN_HDR_LEN - -hicn_type_t hicn_header_to_type (const hicn_header_t *h); - -/** - * @brief Parse packet headers and return hICN format - * @param [in] format - hICN Format - * @param [in, out] packet - Buffer containing the hICN header to be - * initialized - * @return hICN error code - */ -int hicn_packet_init_header (hicn_format_t format, hicn_header_t *packet); - -/** - * @brief Parse packet headers and return hICN format - * @param [in] h - hICN header - * @param [out] format - hICN format - * @return hICN error code - */ -int hicn_packet_get_format (const hicn_header_t *packet, - hicn_format_t *format); - -/** - * @brief Update checksums in packet headers - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @return hICN error code - */ -int hicn_packet_compute_checksum (hicn_format_t format, hicn_header_t *packet); - -/** - * @brief compute the checksum of the packet header, adding init_sum to the - * final value - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @param [in] init_sum - value to add to the final checksum - * @return hICN error code - */ -int hicn_packet_compute_header_checksum (hicn_format_t format, - hicn_header_t *packet, u16 init_sum); - -/** - * @brief Verify checksums in packet headers - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @return hICN error code - */ -int hicn_packet_check_integrity_no_payload (hicn_format_t format, - hicn_header_t *packet, - u16 init_sum); - -// this is not accounted here -/** - * @brief Return total length of hicn headers (but signature payload) - * @param [in] format - hICN format - * @param [out] header_length - Total length of headers - * @return hICN error code - */ -int hicn_packet_get_header_length_from_format (hicn_format_t format, - size_t *header_length); - -/** - * @brief Return total length of hicn headers (before payload) - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] header_length - Total length of headers - * @return hICN error code - */ -int hicn_packet_get_header_length (hicn_format_t format, - const hicn_header_t *packet, - size_t *header_length); - -/** - * @brief Return payload length - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] payload_length - payload length - * @return hICN error code - */ -int hicn_packet_get_payload_length (hicn_format_t format, - const hicn_header_t *packet, - size_t *payload_length); - -/** - * @brief Sets payload length - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @param [in] payload_length - payload length - * @return hICN error code - */ -int hicn_packet_set_payload_length (hicn_format_t format, - hicn_header_t *packet, - const size_t payload_length); - -/** - * @brief Compare two hICN packets - * @param [in] packet_1 - First packet - * @param [in] packet_2 - Second packet - * @return 0 if both packets are considered equal, any other value otherwise. - */ -int hicn_packet_compare (const hicn_header_t *packet1, - const hicn_header_t *packet2); - -/** - * @brief Retrieve the name of an interest/data packet - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] name - name holding the result - * @param [in] is_interest - Flag to determine whether it is an interest (1) or - * data packet (0) - * @return hICN error code - */ -int hicn_packet_get_name (hicn_format_t format, const hicn_header_t *packet, - hicn_name_t *name, u8 is_interest); - -/** - * @brief Sets the name of an interest/data packet - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @param [in] name - name to set into packet - * @param [in] is_interest - Flag to determine whether it is an interest (1) or - * data packet (0) - * @return hICN error code - */ -int hicn_packet_set_name (hicn_format_t format, hicn_header_t *packet, - const hicn_name_t *name, u8 is_interest); - -/** - * @brief Sets the payload of a packet - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @param [in] payload - payload to set - * @param [in] payload_length - size of the payload to set - * @return hICN error code - * - * NOTE: - * - The buffer holding payload is assumed sufficiently large - * - This function updates header fields with the new length, but no checksum. - */ -int hicn_packet_set_payload (hicn_format_t format, hicn_header_t *packet, - const u8 *payload, u16 payload_length); - -/** - * @brief Retrieves the payload of a packet - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] payload - pointer to buffer for storing the result - * @param [out] payload_length - size of the retreived payload - * @param [in] hard_copy - Flag : if true (eg. 1), a copy of the payload is - * made into the payload buffer, otherwise (0) the pointer is changed to point - * to the payload offset in the packet. - * @return hICN error code - * - * NOTE: - * - The buffer holding payload is assumed sufficiently large - */ -int hicn_packet_get_payload (hicn_format_t format, const hicn_header_t *packet, - u8 **payload, size_t *payload_size, - bool hard_copy); - -/** - * @brief Retrieve the locator of an interest / data packet - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] ip_address - retrieved locator - * @param [in] is_interest - Flag to determine whether it is an interest (1) or - * data packet (0) - * @return hICN error code - */ -int hicn_packet_get_locator (hicn_format_t format, const hicn_header_t *packet, - ip_address_t *prefix, bool is_interest); - -/** - * @brief Sets the locator of an interest / data packet - * @param [in] format - hICN format - * @param [in,out] packet - packet header - * @param [out] ip_address - retrieved locator - * @param [in] is_interest - Flag to determine whether it is an interest (1) or - * data packet (0) - * @return hICN error code - */ -int hicn_packet_set_locator (hicn_format_t format, hicn_header_t *packet, - const ip_address_t *prefix, bool is_interest); - -/** - * @brief Retrieves the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] bytes - Retrieved signature size - * @return hICN error code - */ -int hicn_packet_get_signature_size (hicn_format_t format, - const hicn_header_t *packet, - size_t *bytes); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [in] bytes - Retrieved signature size - * @return hICN error code - */ -int hicn_packet_set_signature_size (hicn_format_t format, - hicn_header_t *packet, size_t bytes); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [in] signature_timestamp - Signature timestamp to set - * @return hICN error code - */ -int hicn_packet_set_signature_timestamp (hicn_format_t format, - hicn_header_t *h, - uint64_t signature_timestamp); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] signature_timestamp - Retrieved signature timestamp - * @return hICN error code - */ -int hicn_packet_get_signature_timestamp (hicn_format_t format, - const hicn_header_t *h, - uint64_t *signature_timestamp); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [in] validation_algorithm - Validation algorithm to set - * @return hICN error code - */ -int hicn_packet_set_validation_algorithm (hicn_format_t format, - hicn_header_t *h, - uint8_t validation_algorithm); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] validation_algorithm - Retrieved validation algorithm - * @return hICN error code - */ -int hicn_packet_get_validation_algorithm (hicn_format_t format, - const hicn_header_t *h, - uint8_t *validation_algorithm); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [in] key_id - Key id to set - * @return hICN error code - */ -int hicn_packet_set_key_id (hicn_format_t format, hicn_header_t *h, - uint8_t *key_id); - -/** - * @brief Sets the signature size - * @param [in] format - hICN format - * @param [in] packet - packet header - * @param [out] key_id - Retrieved key id - * @return hICN error code - */ -int hicn_packet_get_key_id (hicn_format_t format, hicn_header_t *h, - uint8_t **key_id, uint8_t *key_id_length); - -/** - * @brief Retrieves the packet hop limit - * @param [in] packet - packet header - * @param [out] hops - Retrieved hop limit - * @return hICN error code - */ -int hicn_packet_get_hoplimit (const hicn_header_t *packet, u8 *hops); - -/** - * @brief Sets the packet hop limit - * @param [in] packet - packet header - * @param [in] hops - Hop limit to set - * @return hICN error code - */ -int hicn_packet_set_hoplimit (hicn_header_t *packet, u8 hops); - -/** - * @brief Check if this packet is interest - * - * @param format - hICN format - * @param packet - Packet header - * @return hICN error code - */ -int hicn_packet_is_interest (hicn_format_t format, const hicn_header_t *h, - int *ret); - -/** - * @brief Mark this packet as interest - * - * @param format - hICN format - * @param packet - Packet header - * @return hICN error code - */ -int hicn_packet_set_interest (hicn_format_t format, hicn_header_t *packet); - -/** - * @brief Mark this packet as data - * - * @param format - hICN format - * @param packet - Packet header - * @return hICN error code - */ -int hicn_packet_set_data (hicn_format_t format, hicn_header_t *h); - -int hicn_packet_copy_header (hicn_format_t format, const hicn_header_t *packet, - hicn_header_t *destination, bool copy_ah); - -int hicn_packet_get_lifetime (hicn_format_t format, - const hicn_header_t *packet, u32 *lifetime); -int hicn_packet_set_lifetime (hicn_format_t format, hicn_header_t *packet, - u32 lifetime); -int hicn_packet_get_reserved_bits (const hicn_header_t *packet, - u8 *reserved_bits); -int hicn_packet_set_reserved_bits (hicn_header_t *packet, - const u8 reserved_bits); -int hicn_packet_get_payload_type (hicn_format_t format, - const hicn_header_t *packet, - hicn_payload_type_t *payload_type); -int hicn_packet_set_payload_type (hicn_format_t format, hicn_header_t *packet, - const hicn_payload_type_t payload_type); - -int hicn_packet_set_syn (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_reset_syn (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_test_syn (hicn_format_t format, const hicn_header_t *packet, - bool *flag); -int hicn_packet_set_ack (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_reset_ack (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_test_ack (hicn_format_t format, const hicn_header_t *packet, - bool *flag); -int hicn_packet_set_rst (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_reset_rst (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_test_rst (hicn_format_t format, const hicn_header_t *packet, - bool *flag); -int hicn_packet_set_fin (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_reset_fin (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_test_fin (hicn_format_t format, const hicn_header_t *packet, - bool *flag); -int hicn_packet_set_ece (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_reset_ece (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_test_ece (hicn_format_t format, const hicn_header_t *packet, - bool *flag); - -int hicn_packet_set_src_port (hicn_format_t format, hicn_header_t *packet, - u16 src_port); -int hicn_packet_get_src_port (hicn_format_t format, - const hicn_header_t *packet, u16 *src_port); -int hicn_packet_set_dst_port (hicn_format_t format, hicn_header_t *packet, - u16 dst_port); -int hicn_packet_get_dst_port (hicn_format_t format, - const hicn_header_t *packet, u16 *dst_port); -int hicn_packet_get_signature (hicn_format_t format, hicn_header_t *packet, - uint8_t **sign_buf); - -/* Interest */ -int hicn_interest_get_name (hicn_format_t format, - const hicn_header_t *interest, hicn_name_t *name); -int hicn_interest_set_name (hicn_format_t format, hicn_header_t *interest, - const hicn_name_t *name); -int hicn_interest_get_locator (hicn_format_t format, - const hicn_header_t *interest, - ip_address_t *prefix); -int hicn_interest_set_locator (hicn_format_t format, hicn_header_t *interest, - const ip_address_t *prefix); -int hicn_interest_compare (const hicn_header_t *interest_1, - const hicn_header_t *interest_2); -int hicn_interest_set_lifetime (hicn_header_t *interest, u32 lifetime); -int hicn_interest_get_lifetime (const hicn_header_t *interest, u32 *lifetime); -int hicn_interest_get_header_length (hicn_format_t format, - const hicn_header_t *interest, - size_t *header_length); -int hicn_interest_get_payload_length (hicn_format_t format, - const hicn_header_t *interest, - size_t *payload_length); -int hicn_interest_set_payload (hicn_format_t format, hicn_header_t *interest, - const u8 *payload, size_t payload_length); -int hicn_interest_get_payload (hicn_format_t format, - const hicn_header_t *interest, u8 **payload, - size_t *payload_size, bool hard_copy); -int hicn_interest_reset_for_hash (hicn_format_t format, hicn_header_t *packet); - -/* Data */ - -int hicn_data_get_name (hicn_format_t format, const hicn_header_t *data, - hicn_name_t *name); -int hicn_data_set_name (hicn_format_t format, hicn_header_t *data, - const hicn_name_t *name); -int hicn_data_get_locator (hicn_format_t format, const hicn_header_t *data, - ip_address_t *prefix); -int hicn_data_set_locator (hicn_format_t format, hicn_header_t *data, - const ip_address_t *prefix); -int hicn_data_compare (const hicn_header_t *data_1, - const hicn_header_t *data_2); -int hicn_data_get_expiry_time (const hicn_header_t *data, u32 *expiry_time); -int hicn_data_set_expiry_time (hicn_header_t *data, u32 expiry_time); -int hicn_data_get_header_length (hicn_format_t format, hicn_header_t *data, - size_t *header_length); -int hicn_data_get_payload_length (hicn_format_t format, - const hicn_header_t *data, - size_t *payload_length); -int hicn_data_get_path_label (const hicn_header_t *data, u32 *path_label); -int hicn_data_set_path_label (hicn_header_t *data, u32 path_label); -int hicn_data_get_payload (hicn_format_t format, const hicn_header_t *data, - u8 **payload, size_t *payload_size, bool hard_copy); -int hicn_data_set_payload (hicn_format_t format, hicn_header_t *data, - const u8 *payload, size_t payload_length); -int hicn_data_get_payload_type (const hicn_header_t *data, - hicn_payload_type_t *payload_type); -int hicn_data_set_payload_type (hicn_header_t *data, - hicn_payload_type_t payload_type); -int hicn_data_reset_for_hash (hicn_format_t format, hicn_header_t *packet); -int hicn_data_is_last (hicn_format_t format, hicn_header_t *h, int *is_last); -int hicn_data_set_last (hicn_format_t format, hicn_header_t *h); - -int hicn_packet_get_signature_padding (hicn_format_t format, - const hicn_header_t *h, size_t *bytes); -int hicn_packet_set_signature_padding (hicn_format_t format, hicn_header_t *h, - size_t bytes); - -#endif /* HICN_COMPAT_H */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/lib/includes/hicn/error.h b/lib/includes/hicn/error.h index d769ef693..9926c9cd8 100644 --- a/lib/includes/hicn/error.h +++ b/lib/includes/hicn/error.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -47,6 +47,8 @@ typedef enum extern const char *HICN_LIB_ERROR_STRING[]; +#define HICN_LIB_IS_ERROR(rc) (rc < 0) + #define hicn_strerror(errno) (char *) (HICN_LIB_ERROR_STRING[-errno]) #endif /* HICN_ERROR_H */ diff --git a/lib/includes/hicn/face.h b/lib/includes/hicn/face.h index 31906afab..fbdacec5f 100644 --- a/lib/includes/hicn/face.h +++ b/lib/includes/hicn/face.h @@ -37,6 +37,7 @@ /* Netdevice type */ #include <net/if.h> // IFNAMSIZ +#include "base.h" #define foreach_netdevice_type \ _ (UNDEFINED) \ @@ -58,10 +59,17 @@ typedef enum #undef _ } netdevice_type_t; +typedef uint32_t netdevice_flags_t; +#define NETDEVICE_FLAGS_EMPTY 0 +#define netdevice_flags_clear(F) (F = 0) +#define netdevice_flags_add(F, T) ((F) |= 1 << (T)) +#define netdevice_flags_remove(F, T) ((F) &= ~(1 << (T))) +#define netdevice_flags_has(F, T) ((F) & (1 << (T))) + extern const char *_netdevice_type_str[]; #define netdevice_type_str(x) _netdevice_type_str[x] -#define NETDEVICE_INVALID_INDEX ~0 +#define INVALID_NETDEVICE_ID ~0 /* Netdevice */ @@ -97,6 +105,7 @@ int netdevice_set_name (netdevice_t *netdevice, const char *name); int netdevice_update_index (netdevice_t *netdevice); int netdevice_update_name (netdevice_t *netdevice); int netdevice_cmp (const netdevice_t *nd1, const netdevice_t *nd2); +bool netdevice_is_empty (const netdevice_t *netdevice); #define NETDEVICE_UNDEFINED_INDEX 0 @@ -175,9 +184,12 @@ face_type_t face_type_from_str (const char *str); /* Face */ typedef u32 face_id_t; +#define INVALID_FACE_ID ~0 typedef struct { + face_id_t id; + char name[SYMBOLIC_NAME_LEN]; face_type_t type; face_state_t admin_state; face_state_t state; @@ -191,16 +203,16 @@ typedef struct */ netdevice_t netdevice; int family; /* To access family independently of face type */ - ip_address_t local_addr; - ip_address_t remote_addr; + hicn_ip_address_t local_addr; + hicn_ip_address_t remote_addr; u16 local_port; u16 remote_port; } face_t; int face_initialize (face_t *face); int face_initialize_udp (face_t *face, const char *interface_name, - const ip_address_t *local_addr, u16 local_port, - const ip_address_t *remote_addr, u16 remote_port, + const hicn_ip_address_t *local_addr, u16 local_port, + const hicn_ip_address_t *remote_addr, u16 remote_port, int family); int face_initialize_udp_sa (face_t *face, const char *interface_name, const struct sockaddr *local_addr, @@ -208,8 +220,8 @@ int face_initialize_udp_sa (face_t *face, const char *interface_name, face_t *face_create (); face_t *face_create_udp (const char *interface_name, - const ip_address_t *local_addr, u16 local_port, - const ip_address_t *remote_addr, u16 remote_port, + const hicn_ip_address_t *local_addr, u16 local_port, + const hicn_ip_address_t *remote_addr, u16 remote_port, int family); face_t *face_create_udp_sa (const char *interface_name, const struct sockaddr *local_addr, diff --git a/lib/includes/hicn/header.h b/lib/includes/hicn/header.h deleted file mode 100644 index 208e35d68..000000000 --- a/lib/includes/hicn/header.h +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright (c) 2021 Cisco and/or its affiliates. - * 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. - */ - -/** - * @file header.h - * @brief hICN header data structures. - * - * NOTE: These structures are used as convenient facade for accessing - * the encapsulated headers. They are not written taking compiler padding - * into account, then a sizeof() on these struct could not give the expected - * result. For accessing the size of the hicn headers use the macros at the end - * of this file. - */ - -#ifndef HICN_HEADER_H -#define HICN_HEADER_H - -#include "common.h" -#include "protocol.h" - -typedef struct -{ - _ipv6_header_t ip; - union - { - _tcp_header_t tcp; - struct - { - _udp_header_t udp; - _new_header_t newhdr; - }; - _icmp_header_t icmp; - _icmp_wldr_header_t wldr; - }; -} hicn_v6_hdr_t; - -typedef struct -{ - _ipv6_header_t ip; - union - { - struct - { - _tcp_header_t tcp; - _ah_header_t ah; - }; - struct - { - _udp_header_t udp; - _new_header_t newhdr; - _ah_header_t udp_ah; - }; - struct - { - _icmp_header_t icmp; - _ah_header_t icmp_ah; - }; - }; -} hicn_v6ah_hdr_t; - -// For ipv4 we need to use packed structs as fields may be aligned to 64 bits -// (So for instance tcp header may start at byte #24 instead of byte 20) -typedef PACKED (struct { - _ipv4_header_t ip; - union - { - _tcp_header_t tcp; - struct - { - _udp_header_t udp; - _new_header_t newhdr; - }; - _icmp_header_t icmp; - _icmp_wldr_header_t wldr; - }; -}) hicn_v4_hdr_t; - -typedef PACKED (struct { - _ipv4_header_t ip; - union - { - struct - { - _tcp_header_t tcp; - _ah_header_t ah; - }; - struct - { - _udp_header_t udp; - _new_header_t newhdr; - _ah_header_t udp_ah; - }; - struct - { - _icmp_header_t icmp; - _ah_header_t icmp_ah; - }; - }; -}) hicn_v4ah_hdr_t; - -typedef union -{ - /* To deprecate as redundant with hicn_type_t */ - hicn_v6_hdr_t v6; - hicn_v6ah_hdr_t v6ah; - hicn_v4_hdr_t v4; - hicn_v4ah_hdr_t v4ah; - - hicn_protocol_t protocol; -} hicn_header_t; - -#define HICN_V6_TCP_HDRLEN (IPV6_HDRLEN + TCP_HDRLEN) -#define HICN_V6_ICMP_HDRLEN (IPV6_HDRLEN + ICMP_HDRLEN) -#define HICN_V6_WLDR_HDRLEN (IPV6_HDRLEN + ICMPWLDR_HDRLEN) - -#define HICN_V6_TCP_AH_HDRLEN (HICN_V6_TCP_HDRLEN + AH_HDRLEN) -#define HICN_V6_ICMP_AH_HDRLEN (HICN_V6_ICMP_HDRLEN + AH_HDRLEN) - -#define HICN_V4_TCP_HDRLEN (IPV4_HDRLEN + TCP_HDRLEN) -#define HICN_V4_ICMP_HDRLEN (IPV4_HDRLEN + ICMP_HDRLEN) -#define HICN_V4_WLDR_HDRLEN (IPV4_HDRLEN + ICMPWLDR_HDRLEN) - -#define HICN_V4_TCP_AH_HDRLEN (HICN_V4_TCP_HDRLEN + AH_HDRLEN) -#define HICN_V4_ICMP_AH_HDRLEN (HICN_V4_ICMP_HDRLEN + AH_HDRLEN) - -#define HICN_DEFAULT_PORT 9695 - -#endif /* HICN_HEADER_H */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/lib/includes/hicn/hicn.h b/lib/includes/hicn/hicn.h index 4a5b4dd56..5e67d83a0 100644 --- a/lib/includes/hicn/hicn.h +++ b/lib/includes/hicn/hicn.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -29,6 +29,14 @@ * Recovery (WLDR) [2], Anchorless Mobility Management (hICN-AMM) [3], * including MAP-Me producer mobility mechanisms [4]. * + * Other hICN constructs such as faces, policies and stategies are not included + * in this header, but available separately in : + * - hicn/face.h + * - hicn/policy.h + * - hicn/strategy.h + * + * REFERENCES + * * [1] Hybrid Information-Centric Networking * L. Muscariello, G. Carofiglio, J. Augé, M. Papalini * IETF draft (intarea) @ @@ -52,22 +60,21 @@ #ifndef HICN__H #define HICN__H -#ifdef HICN_VPP_PLUGIN +/* Base data structures */ +#include <hicn/base.h> -#include <hicn/header.h> +/* Names */ #include <hicn/name.h> -#include <hicn/ops.h> -#include <hicn/mapme.h> -#else +/* Packet operations */ +#include <hicn/packet.h> -#include <hicn/error.h> -#include <hicn/header.h> -#include <hicn/name.h> -#include <hicn/ops.h> +/* MAP-Me : mobility management operations */ #include <hicn/mapme.h> -#include <hicn/compat.h> +/* Error management */ +#ifndef HICN_VPP_PLUGIN +#include <hicn/error.h> #endif #endif /* HICN__H */ diff --git a/lib/includes/hicn/interest_manifest.h b/lib/includes/hicn/interest_manifest.h new file mode 100644 index 000000000..03f54d6d5 --- /dev/null +++ b/lib/includes/hicn/interest_manifest.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * 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 HICNLIGHT_INTEREST_MANIFEST_H +#define HICNLIGHT_INTEREST_MANIFEST_H + +#include <string.h> +#include <stdbool.h> + +#include <hicn/util/bitmap.h> +#include <hicn/base.h> + +typedef enum +{ + INT_MANIFEST_SPLIT_STRATEGY_NONE, + INT_MANIFEST_SPLIT_STRATEGY_MAX_N_SUFFIXES, + INT_MANIFEST_SPLIT_N_STRATEGIES, +} int_manifest_split_strategy_t; + +#define MAX_SUFFIXES_IN_MANIFEST 256 + +#define DEFAULT_DISAGGREGATION_STRATEGY \ + INT_MANIFEST_SPLIT_STRATEGY_MAX_N_SUFFIXES +#define DEFAULT_N_SUFFIXES_PER_SPLIT MAX_SUFFIXES_IN_MANIFEST + +#define BITMAP_SIZE (MAX_SUFFIXES_IN_MANIFEST / WORD_WIDTH) + +typedef struct +{ + /* This can be 16 bits, but we use 32 bits for alignment */ + uint32_t n_suffixes; + + /* Align to 64 bits */ + uint32_t padding; + + hicn_uword request_bitmap[BITMAP_SIZE]; + + /* Followed by the list of prefixes to ask */ + /* ... */ +} interest_manifest_header_t; + +static_assert (sizeof (interest_manifest_header_t) == 32 + 4 + 4, + "interest_manifest_header_t size must be 40 bytes"); + +static inline bool +interest_manifest_is_valid (interest_manifest_header_t *int_manifest_header, + size_t payload_length) +{ + if (int_manifest_header->n_suffixes == 0 || + int_manifest_header->n_suffixes > MAX_SUFFIXES_IN_MANIFEST) + { + return false; + } + + hicn_uword empty_bitmap[BITMAP_SIZE] = { 0 }; + if (memcmp (empty_bitmap, int_manifest_header->request_bitmap, + sizeof (empty_bitmap)) == 0) + { + return false; + } + + if (payload_length - sizeof (interest_manifest_header_t) != + int_manifest_header->n_suffixes * sizeof (u32)) + { + return false; + } + + return true; +} + +static inline size_t +interest_manifest_update_bitmap (const hicn_uword *initial_bitmap, + hicn_uword *bitmap_to_update, size_t start, + size_t n, size_t max_suffixes) +{ + size_t i = start, n_ones = 0; + while (i < n) + { + if (bitmap_is_set_no_check (initial_bitmap, i)) + { + bitmap_set_no_check (bitmap_to_update, i); + n_ones++; + } + i++; + + if (n_ones == max_suffixes) + break; + } + + return i; +} + +#endif /* HICNLIGHT_INTEREST_MANIFEST_H */ diff --git a/lib/includes/hicn/mapme.h b/lib/includes/hicn/mapme.h index 63a5cd77e..b452a5dde 100644 --- a/lib/includes/hicn/mapme.h +++ b/lib/includes/hicn/mapme.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -23,9 +23,9 @@ #include <stdint.h> // u32 #include <stdbool.h> +#include <hicn/name.h> + #include "common.h" -#include "protocol.h" -#include "ops.h" /** * @brief MAP-Me configuration options @@ -114,51 +114,11 @@ int hicn_mapme_parse_packet (const u8 *packet, hicn_prefix_t *prefix, /* Default TTL */ #define HICN_MAPME_TTL 255 // typical for redirect (ref?) -/* - * The length of the MAPME4 header struct must be 120 bytes. - */ -#define EXPECTED_MAPME_V4_HDRLEN 120 - -/** @brief MAP-Me packet header for IPv4 */ -typedef struct -{ - _ipv4_header_t ip; - _icmprd4_header_t icmp_rd; - seq_t seq; - u8 len; - u8 _pad[3]; -} hicn_mapme_v4_header_t; +/* Should be moved to mapme.c, but header size still in use in VPP */ -/* - * The length of the MAPME4 header struct must be bytes. - */ +#define EXPECTED_MAPME_V4_HDRLEN 120 #define EXPECTED_MAPME_V6_HDRLEN 88 -/** @brief MAP-Me packet header for IPv6 */ -typedef struct -{ - _ipv6_header_t ip; - _icmprd_header_t icmp_rd; - seq_t seq; - u8 len; - u8 _pad[3]; -} hicn_mapme_v6_header_t; - -/** @brief MAP-Me packet header (IP version agnostic) */ -typedef union -{ - hicn_mapme_v4_header_t v4; - hicn_mapme_v6_header_t v6; -} hicn_mapme_header_t; - -#define HICN_MAPME_V4_HDRLEN sizeof (hicn_mapme_v4_header_t) -#define HICN_MAPME_V6_HDRLEN sizeof (hicn_mapme_v6_header_t) - -static_assert (EXPECTED_MAPME_V4_HDRLEN == HICN_MAPME_V4_HDRLEN, - "Size of MAPME_V4 struct does not match its expected size."); -static_assert (EXPECTED_MAPME_V6_HDRLEN == HICN_MAPME_V6_HDRLEN, - "Size of MAPME_V6 struct does not match its expected size."); - #endif /* HICN_MAPME_H */ /* diff --git a/lib/includes/hicn/name.h b/lib/includes/hicn/name.h index f85b0bc3f..895b86341 100644 --- a/lib/includes/hicn/name.h +++ b/lib/includes/hicn/name.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -24,12 +24,14 @@ #ifndef HICN_NAME_H #define HICN_NAME_H +#include <assert.h> #include <stdbool.h> +#include <stddef.h> #ifndef _WIN32 #include <netinet/in.h> // struct sockadd #endif +#include <hicn/common.h> #include <hicn/util/ip_address.h> -#include "common.h" /****************************************************************************** * hICN names @@ -38,40 +40,92 @@ #define TCP_SEQNO_LEN 4 /* bytes */ #define HICN_V4_PREFIX_LEN IPV4_ADDR_LEN #define HICN_V6_PREFIX_LEN IPV6_ADDR_LEN -#define HICN_SEGMENT_LEN TCP_SEQNO_LEN + +#if 0 +#define HICN_SEGMENT_LEN TCP_SEQNO_LEN + #define HICN_V6_NAME_LEN \ (HICN_V6_PREFIX_LEN + HICN_SEGMENT_LEN) /* 20 bytes \ */ #define HICN_V4_NAME_LEN \ (HICN_V4_PREFIX_LEN + HICN_SEGMENT_LEN) /* 8 bytes \ */ +#endif + +#define HICN_INVALID_SUFFIX ((uint32_t) (~0)) /* Prefix */ -typedef u32 hicn_name_suffix_t; +#define HICN_PREFIX_MAX_LEN IP_ADDRESS_MAX_LEN typedef struct { - ip_address_t name; + hicn_ip_address_t name; u8 len; } hicn_prefix_t; +#define HICN_PREFIX_EMPTY \ + (hicn_prefix_t) { .name = IP_ADDRESS_EMPTY, .len = 0 } + +static inline const hicn_ip_address_t * +hicn_prefix_get_ip_address (const hicn_prefix_t *prefix) +{ + return &prefix->name; +} + +static inline u8 +hicn_prefix_get_len (const hicn_prefix_t *prefix) +{ + return prefix->len; +} + +int hicn_prefix_get_ip_prefix (const hicn_prefix_t *prefix, + hicn_ip_prefix_t *ip_prefix); /* * Name * * A name is a prefix + a segment name (suffix) */ +typedef hicn_ip_address_t hicn_name_prefix_t; +typedef uint32_t hicn_name_suffix_t; + +#define hicn_name_prefix_cmp hicn_ip_address_cmp +#define hicn_name_prefix_equals hicn_ip_address_equals +#define hicn_name_prefix_get_len_bits hicn_ip_address_get_len_bits +#define hicn_name_prefix_get_hash hicn_ip_address_get_hash +#define hicn_name_prefix_snprintf hicn_ip_address_snprintf +#define HICN_NAME_PREFIX_EMPTY IP_ADDRESS_EMPTY + typedef struct { - ip_address_t prefix; + hicn_name_prefix_t prefix; hicn_name_suffix_t suffix; } hicn_name_t; +static_assert (offsetof (hicn_name_t, prefix) == 0, ""); +static_assert (offsetof (hicn_name_t, suffix) == 16, ""); +static_assert (sizeof (hicn_name_t) == 20, ""); + +#define HICN_NAME_EMPTY \ + (hicn_name_t) { .prefix = HICN_NAME_PREFIX_EMPTY, .suffix = 0, } + +static inline const hicn_name_prefix_t * +hicn_name_get_prefix (const hicn_name_t *name) +{ + return &name->prefix; +} + +static inline const hicn_name_suffix_t +hicn_name_get_suffix (const hicn_name_t *name) +{ + return name->suffix; +} + #define _is_unspec(name) \ (((name)->prefix.pad[0] | (name)->prefix.pad[1] | (name)->prefix.pad[2] | \ (name)->prefix.v4.as_u32) == 0) -#define _is_inet4(name) (ip_address_is_v4 (&name->prefix)) +#define _is_inet4(name) (hicn_ip_address_is_v4 (&name->prefix)) #define _is_inet6(name) (!_is_inet4 (name)) /** @@ -86,11 +140,21 @@ int hicn_name_create (const char *ip_address, u32 id, hicn_name_t *name); /** * @brief Create an hICN name from IP address * @param [in] ip_address - IP address - * @param [in] id Segment - identifier + * @param [in] suffix - Name suffix * @param [out] Resulting - hICN name * @return hICN error code */ -int hicn_name_create_from_ip_prefix (const ip_prefix_t *prefix, u32 id, +int hicn_name_create_from_ip_address (const hicn_ip_address_t ip_address, + u32 suffix, hicn_name_t *name); + +/** + * @brief Create an hICN name from IP prefix + * @param [in] prefix - IP prefix + * @param [in] suffix - Name suffix + * @param [out] Resulting - hICN name + * @return hICN error code + */ +int hicn_name_create_from_ip_prefix (const hicn_ip_prefix_t *prefix, u32 id, hicn_name_t *name); /** @@ -113,7 +177,10 @@ int hicn_name_compare (const hicn_name_t *name_1, const hicn_name_t *name_2, * @param [in] consider_suffix - Consider the suffix in the hash computation * @return hICN error code */ -int hicn_name_hash (const hicn_name_t *name, u32 *hash, bool consider_suffix); +uint32_t _hicn_name_get_hash (const hicn_name_t *name, bool consider_suffix); + +#define hicn_name_get_hash(NAME) _hicn_name_get_hash (NAME, true) +#define hicn_name_get_prefix_hash(NAME) _hicn_name_get_hash (NAME, false) /** * @brief Test whether an hICN name is empty @@ -146,7 +213,7 @@ int hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *src); * @param [in] seq_number - Segment identifier * @return hICN error code */ -int hicn_name_set_seq_number (hicn_name_t *name, u32 seq_number); +int hicn_name_set_suffix (hicn_name_t *name, hicn_name_suffix_t suffix); /** * @brief Retrieves the segment part of an hICN name @@ -171,7 +238,8 @@ int hicn_name_to_sockaddr_address (const hicn_name_t *name, * @param [out] ip_address - Resulting IP address * @return hICN error code */ -int hicn_name_to_ip_prefix (const hicn_name_t *name, ip_prefix_t *ip_prefix); +int hicn_name_to_hicn_ip_prefix (const hicn_name_t *name, + hicn_ip_prefix_t *hicn_ip_prefix); /** * @brief Convert an hICN name to presentation format @@ -198,15 +266,50 @@ int hicn_name_pton (const char *src, hicn_name_t *dst); */ int hicn_name_get_family (const hicn_name_t *name, int *family); +bool hicn_name_is_v4 (const hicn_name_t *name); + +int hicn_name_snprintf (char *s, size_t size, const hicn_name_t *name); + +int hicn_name_cmp (const hicn_name_t *n1, const hicn_name_t *n2); +bool hicn_name_equals (const hicn_name_t *n1, const hicn_name_t *n2); + +#define MAXSZ_HICN_NAME MAXSZ_IP_ADDRESS + /** * @brief Creates an hICN prefix from an IP address * @param [in] ip_address - Input IP address * @param [out] prefix - Resulting prefix * @return hICN error code */ -int hicn_prefix_create_from_ip_prefix (const ip_prefix_t *ip_prefix, +int hicn_prefix_create_from_ip_prefix (const hicn_ip_prefix_t *hicn_ip_prefix, hicn_prefix_t *prefix); +int +hicn_prefix_create_from_ip_address_len (const hicn_ip_address_t *ip_address, + uint8_t len, hicn_prefix_t *prefix); + +hicn_prefix_t *hicn_prefix_dup (const hicn_prefix_t *prefix); + +int hicn_prefix_copy (hicn_prefix_t *dst, const hicn_prefix_t *src); + +bool hicn_prefix_is_v4 (const hicn_prefix_t *prefix); + +uint32_t hicn_prefix_lpm (const hicn_prefix_t *p1, const hicn_prefix_t *p2); + +void hicn_prefix_clear (hicn_prefix_t *prefix, uint8_t start_from); + +void hicn_prefix_truncate (hicn_prefix_t *prefix, uint8_t len); + +int hicn_prefix_cmp (const hicn_prefix_t *p1, const hicn_prefix_t *p2); + +bool hicn_prefix_equals (const hicn_prefix_t *p1, const hicn_prefix_t *p2); + +int hicn_prefix_snprintf (char *s, size_t size, const hicn_prefix_t *prefix); + +uint8_t hicn_prefix_get_bit (const hicn_prefix_t *prefix, uint8_t pos); + +#define MAXSZ_HICN_PREFIX MAXSZ_IP_PREFIX + #endif /* HICN_NAME_H */ /* diff --git a/lib/includes/hicn/ops.h b/lib/includes/hicn/ops.h deleted file mode 100644 index 4efef6523..000000000 --- a/lib/includes/hicn/ops.h +++ /dev/null @@ -1,1029 +0,0 @@ -/* - * Copyright (c) 2021 Cisco and/or its affiliates. - * 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. - */ - -/** - * @file base.h - * @brief Protocol-independent packet operations - */ - -#ifndef HICN_OPS_H -#define HICN_OPS_H - -#include <stdlib.h> - -#include "common.h" -#include "error.h" -#include "header.h" -#include "name.h" - -/* - * hICN operations on packets - * - * All prototypes take an hicn_type_t parameter as their first argument, as - * this decides the sequence of protocols that are being used by the different - * operations. - */ - -typedef struct hicn_ops_s -{ - /** - * @brief Initialize the headers of the hicn packet - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the packet - */ - int (*init_packet_header) (hicn_type_t type, hicn_protocol_t *h); - - /** - * @brief Retrieves an Interest locator - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest packet - * @param [out] ip_address - Retrieved locator - * @return hICN error code - */ - int (*get_interest_locator) (hicn_type_t type, const hicn_protocol_t *h, - ip_address_t *ip_address); - - /** - * @brief Sets an Interest locator - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @param [in] ip_address - Locator to set - * @return hICN error code - */ - int (*set_interest_locator) (hicn_type_t type, hicn_protocol_t *h, - const ip_address_t *ip_address); - - /** - * @brief Retrieves an Interest name - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest packet - * @param [out] name - Retrieved name - * @return hICN error code - */ - int (*get_interest_name) (hicn_type_t type, const hicn_protocol_t *h, - hicn_name_t *name); - - /** - * @brief Sets an Interest name - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @param [in] name - Name to set - * @return hICN error code - */ - int (*set_interest_name) (hicn_type_t type, hicn_protocol_t *h, - const hicn_name_t *name); - - /** - * @brief Retrieves an Interest name suffix - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest packet - * @param [out] suffix - Retrieved name suffix - * @return hICN error code - */ - int (*get_interest_name_suffix) (hicn_type_t type, const hicn_protocol_t *h, - hicn_name_suffix_t *suffix); - - /** - * @brief Sets an Interest name suffix - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @param [in] suffix - Name suffix to set - * @return hICN error code - */ - int (*set_interest_name_suffix) (hicn_type_t type, hicn_protocol_t *h, - const hicn_name_suffix_t *suffix); - - /** - * @brief Check if packet is an interest - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest packet - * @param [out] h - 1 if interest, 0 otherwise - * @return hICN error code - */ - int (*is_interest) (hicn_type_t type, const hicn_protocol_t *h, int *ret); - - /** - * @brief Set flag to mark current packet as interest - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @return hICN error code - */ - int (*mark_packet_as_interest) (hicn_type_t type, hicn_protocol_t *h); - - /** - * @brief Set flag to mark current packet as data - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @return hICN error code - */ - int (*mark_packet_as_data) (hicn_type_t type, hicn_protocol_t *h); - - /** - * @brief Clear the necessary Interest fields in order to hash it - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest packet - * @return hICN error code - */ - int (*reset_interest_for_hash) (hicn_type_t type, hicn_protocol_t *h); - - /** - * @brief Retrieves a Data locator - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Data packet - * @param [out] ip_address - Retrieved locator - * @return hICN error code - */ - int (*get_data_locator) (hicn_type_t type, const hicn_protocol_t *h, - ip_address_t *ip_address); - - /** - * @brief Sets a Data locator - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @param [in] ip_address - Locator to set - * @return hICN error code - */ - int (*set_data_locator) (hicn_type_t type, hicn_protocol_t *h, - const ip_address_t *ip_address); - - /** - * @brief Retrieves a Data name - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Data packet - * @param [out] name - Retrieved name - * @return hICN error code - */ - int (*get_data_name) (hicn_type_t type, const hicn_protocol_t *h, - hicn_name_t *name); - - /** - * @brief Sets a Data name - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @param [in] name - Name to set - * @return hICN error code - */ - int (*set_data_name) (hicn_type_t type, hicn_protocol_t *h, - const hicn_name_t *name); - - /** - * @brief Retrieves a Data name suffix - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Data packet - * @param [out] suffix - Retrieved name suffix - * @return hICN error code - */ - int (*get_data_name_suffix) (hicn_type_t type, const hicn_protocol_t *h, - hicn_name_suffix_t *suffix); - - /** - * @brief Sets a Data name suffix - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @param [in] suffix - Name suffix to set - * @return hICN error code - */ - int (*set_data_name_suffix) (hicn_type_t type, hicn_protocol_t *h, - const hicn_name_suffix_t *suffix); - - /** - * @brief Retrieves a Data pathlabel - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Data packet - * @param [out] pathlabel - Retrieved pathlabel - * @return hICN error code - */ - int (*get_data_pathlabel) (hicn_type_t type, const hicn_protocol_t *h, - u32 *pathlabel); - - /** - * @brief Sets a Data pathlabel - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @param [in] pathlabel - Pathlabel to set - * @return hICN error code - */ - int (*set_data_pathlabel) (hicn_type_t type, hicn_protocol_t *h, - const u32 pathlabel); - - /** - * @brief Update a Data pathlabel with a new face identifier - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @param [in] pathlabel - Face identifier used to update pathlabel - * @return hICN error code - */ - int (*update_data_pathlabel) (hicn_type_t type, hicn_protocol_t *h, - const hicn_faceid_t face_id); - - /** - * @brief Clear the necessary Data fields in order to hash it - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Data packet - * @return hICN error code - */ - int (*reset_data_for_hash) (hicn_type_t type, hicn_protocol_t *h); - - /** - * @brief Retrieves an Interest or Data lifetime - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] pathlabel - Retrieved lifetime - * @return hICN error code - */ - int (*get_lifetime) (hicn_type_t type, const hicn_protocol_t *h, - hicn_lifetime_t *lifetime); - - /** - * @brief Sets an Interest or Data lifetime - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] pathlabel - Lifetime to set - * @return hICN error code - */ - int (*set_lifetime) (hicn_type_t type, hicn_protocol_t *h, - const hicn_lifetime_t lifetime); - - /** - * @brief Get the source port of the hicn packet. - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] source_port - Retrieved source port - * @return hICN error code - */ - int (*get_source_port) (hicn_type_t type, const hicn_protocol_t *h, - u16 *source_port); - - /** - * @brief Get the destination port of the hicn packet. - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] source_port - Retrieved destination port - * @return hICN error code - */ - int (*get_dest_port) (hicn_type_t type, const hicn_protocol_t *h, - u16 *dest_port); - - /** - * @brief Set the source port of the hicn packet. - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] source_port - Source port to set - * @return hICN error code - */ - int (*set_source_port) (hicn_type_t type, hicn_protocol_t *h, - u16 source_port); - - /** - * @brief Set the destination port of the hicn packet. - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] source_port - Destination port to set - * @return hICN error code - */ - int (*set_dest_port) (hicn_type_t type, hicn_protocol_t *h, u16 dest_port); - - /** - * @brief Update all checksums in packet headers - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the packet - * @param [in] partial_csum - Partial checksum (set to 0, used internally to - * carry intermediate values from IP pseudo-header) - * @param [in] payload_length - Payload length (can be set to ~0, retrieved - * and used internally to carry payload length across protocol headers) - * @return hICN error code - */ - int (*update_checksums) (hicn_type_t type, hicn_protocol_t *h, - u16 partial_csum, size_t payload_length); - - /** - * @brief Validate all checksums in packet headers - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the packet - * @param [in] partial_csum - Partial checksum, or zero if no partial - * checksum available - * @param [in] payload_length - Payload length (can be set to ~0, retrieved - * and used internally to carry payload length across protocol headers) - * @return hICN error code - */ - int (*verify_checksums) (hicn_type_t type, hicn_protocol_t *h, - u16 partial_csum, size_t payload_length); - - /** - * @brief Rewrite an Interest packet header (locator) - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest packet - * @param [in] addr_new - New locator - * @param [in] addr_old - Old locator (set to NULL, used internally to - * compute incremental checksums) - * @return hICN error code - */ - int (*rewrite_interest) (hicn_type_t type, hicn_protocol_t *h, - const ip_address_t *addr_new, - ip_address_t *addr_old); - - /** - * @brief Rewrite a Data packet header (locator + pathlabel) - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Data packet - * @param [in] addr_new - New locator - * @param [in] addr_old - Old locator (set to NULL, used internally to - * compute incremental checksums) - * @param [in] face_id - Face identifier used to update pathlabel - * @param [in] reset_pl - If not zero, reset the current pathlabel - * before update it - * @return hICN error code - */ - int (*rewrite_data) (hicn_type_t type, hicn_protocol_t *h, - const ip_address_t *addr_new, ip_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl); - - /** - * @brief Return the packet length - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the packet - * @parma [out] length - Returned packet length - * @return hICN error code - */ - int (*get_length) (hicn_type_t type, const hicn_protocol_t *h, - size_t *length); - - /** - * @brief Return the current packet header length - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the packet - * @parma [out] header_length - Returned packet current header length - * @return hICN error code - */ - int (*get_current_header_length) (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length); - - /** - * @brief Return the packet header length - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the packet - * @parma [out] header_length - Returned packet header length - * @return hICN error code - */ - int (*get_header_length) (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length); - - /** - * @brief Return the packet payload length - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the packet - * @parma [out] payload_length - Returned packet payload length - * @return hICN error code - */ - int (*get_payload_length) (hicn_type_t type, const hicn_protocol_t *h, - size_t *payload_length); - - /** - * @brief Sets the packet paylaod length - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the packet - * @parma [out] payload_length - Payload length to set - * @return hICN error code - */ - int (*set_payload_length) (hicn_type_t type, hicn_protocol_t *h, - size_t payload_length); - - /** - * @brief Retrieves an Interest or Data signature size - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] signature_size - Retrieved signature size - * @return hICN error code - */ - int (*get_signature_size) (hicn_type_t type, const hicn_protocol_t *h, - size_t *signature_size); - - /** - * @brief Sets an Interest or Data signature size - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] signature_size - Signature size to set - * @return hICN error code - */ - int (*set_signature_size) (hicn_type_t type, hicn_protocol_t *h, - size_t signature_size); - - /** - * @brief Sets an Interest or Data signature padding between maximum size and - * real size - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] signature_size - Signature size to set - * @return hICN error code - */ - int (*set_signature_padding) (hicn_type_t type, hicn_protocol_t *h, - size_t signature_padding); - - /** - * @brief gets an Interest or Data signature padding between maximum size and - * real size - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] signature_size - retrieve the padding between maximum size and - * real size - * @return hICN error code - */ - int (*get_signature_padding) (hicn_type_t type, const hicn_protocol_t *h, - size_t *signature_padding); - - /** - * @brief Gets the signature timestamp - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [out] signature_timestamp - Retrieved signature timestamp - * @return hICN error code - */ - int (*get_signature_timestamp) (hicn_type_t type, const hicn_protocol_t *h, - uint64_t *signature_timestamp); - - /** - * @brief Sets the signature timestamp - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] signature_timestamp - Signature timestamp to set - * @return hICN error code - */ - int (*set_signature_timestamp) (hicn_type_t type, hicn_protocol_t *h, - uint64_t signature_timestamp); - - /** - * @brief Gets the signature validation algorithm - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [out] validation_algorithm - Retrieved validation_algorithm - * @return hICN error code - */ - int (*get_validation_algorithm) (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *validation_algorithm); - - /** - * @brief Sets the signature validation algorithm - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] validation_algorithm - Validation algorithm enumeration - * @return hICN error code - */ - int (*set_validation_algorithm) (hicn_type_t type, hicn_protocol_t *h, - uint8_t validation_algorithm); - - /** - * @brief Gets the key id - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [out] key_id - Retrieved key id first byte address - * @return hICN error code - */ - int (*get_key_id) (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, - uint8_t *key_id_size); - - /** - * @brief Sets the key id - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] key_id - Key id first byte address - * @return hICN error code - */ - int (*set_key_id) (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id); - - /** - * @brief Get a pointer to the signature field in the packet - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [out] signature - Pointer to the memory region holding the - * signature - * @return hICN error code - */ - int (*get_signature) (hicn_type_t type, hicn_protocol_t *h, - uint8_t **signature); - - /** - * @brief Set payload type of the packet - * @param [in] type - hICN packet type - * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] payload_type - The payload type of this packet - * @return hICN error code - */ - int (*set_payload_type) (hicn_type_t type, hicn_protocol_t *h, - hicn_payload_type_t payload_type); - - /** - * @brief Get payload type from the packet - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] payload_type - The payload type of this packet - * @return hICN error code - */ - int (*get_payload_type) (hicn_type_t type, const hicn_protocol_t *h, - hicn_payload_type_t *payload_type); - - /** - * @brief Check if data packet is last one. - * @param [in] type - hICN packet type - * @param [in] h - Buffer holding the Interest or Data packet - * @param [out] is_last - 1 if last data, 0 otherwise - * @return hICN error code - */ - int (*is_last_data) (hicn_type_t type, const hicn_protocol_t *h, - int *is_last); - - /** - * @brief Mark data packet as last - * @param [in] type - hICN packet type - * @param [in, out] h - Buffer holding the Interest or Data packet - * @return hICN error code - */ - int (*set_last_data) (hicn_type_t type, hicn_protocol_t *h); -} hicn_ops_t; - -#define DECLARE_HICN_OPS(protocol) \ - const hicn_ops_t hicn_ops_##protocol = { \ - ATTR_INIT (init_packet_header, protocol##_init_packet_header), \ - ATTR_INIT (get_interest_locator, protocol##_get_interest_locator), \ - ATTR_INIT (set_interest_locator, protocol##_set_interest_locator), \ - ATTR_INIT (get_interest_name, protocol##_get_interest_name), \ - ATTR_INIT (set_interest_name, protocol##_set_interest_name), \ - ATTR_INIT (get_interest_name_suffix, \ - protocol##_get_interest_name_suffix), \ - ATTR_INIT (set_interest_name_suffix, \ - protocol##_set_interest_name_suffix), \ - ATTR_INIT (is_interest, protocol##_is_interest), \ - ATTR_INIT (mark_packet_as_interest, protocol##_mark_packet_as_interest), \ - ATTR_INIT (mark_packet_as_data, protocol##_mark_packet_as_data), \ - ATTR_INIT (reset_interest_for_hash, protocol##_reset_interest_for_hash), \ - ATTR_INIT (get_data_locator, protocol##_get_data_locator), \ - ATTR_INIT (set_data_locator, protocol##_set_data_locator), \ - ATTR_INIT (get_data_name, protocol##_get_data_name), \ - ATTR_INIT (set_data_name, protocol##_set_data_name), \ - ATTR_INIT (get_data_name_suffix, protocol##_get_data_name_suffix), \ - ATTR_INIT (set_data_name_suffix, protocol##_set_data_name_suffix), \ - ATTR_INIT (get_data_pathlabel, protocol##_get_data_pathlabel), \ - ATTR_INIT (set_data_pathlabel, protocol##_set_data_pathlabel), \ - ATTR_INIT (update_data_pathlabel, protocol##_update_data_pathlabel), \ - ATTR_INIT (reset_data_for_hash, protocol##_reset_data_for_hash), \ - ATTR_INIT (get_lifetime, protocol##_get_lifetime), \ - ATTR_INIT (set_lifetime, protocol##_set_lifetime), \ - ATTR_INIT (get_source_port, protocol##_get_source_port), \ - ATTR_INIT (get_dest_port, protocol##_get_dest_port), \ - ATTR_INIT (set_source_port, protocol##_set_source_port), \ - ATTR_INIT (set_dest_port, protocol##_set_dest_port), \ - ATTR_INIT (update_checksums, protocol##_update_checksums), \ - ATTR_INIT (verify_checksums, protocol##_verify_checksums), \ - ATTR_INIT (rewrite_interest, protocol##_rewrite_interest), \ - ATTR_INIT (rewrite_data, protocol##_rewrite_data), \ - ATTR_INIT (get_length, protocol##_get_length), \ - ATTR_INIT (get_current_header_length, \ - protocol##_get_current_header_length), \ - ATTR_INIT (get_header_length, protocol##_get_header_length), \ - ATTR_INIT (get_payload_length, protocol##_get_payload_length), \ - ATTR_INIT (set_payload_length, protocol##_set_payload_length), \ - ATTR_INIT (get_payload_type, protocol##_get_payload_type), \ - ATTR_INIT (set_payload_type, protocol##_set_payload_type), \ - ATTR_INIT (get_signature_size, protocol##_get_signature_size), \ - ATTR_INIT (get_signature_timestamp, protocol##_get_signature_timestamp), \ - ATTR_INIT (set_signature_timestamp, protocol##_set_signature_timestamp), \ - ATTR_INIT (get_validation_algorithm, \ - protocol##_get_validation_algorithm), \ - ATTR_INIT (set_validation_algorithm, \ - protocol##_set_validation_algorithm), \ - ATTR_INIT (get_key_id, protocol##_get_key_id), \ - ATTR_INIT (set_key_id, protocol##_set_key_id), \ - ATTR_INIT (get_signature, protocol##_get_signature), \ - ATTR_INIT (set_signature_padding, protocol##_set_signature_padding), \ - ATTR_INIT (set_signature_size, protocol##_set_signature_size), \ - ATTR_INIT (get_signature_padding, protocol##_get_signature_padding), \ - ATTR_INIT (is_last_data, protocol##_is_last_data), \ - ATTR_INIT (set_last_data, protocol##_set_last_data), \ - } - -/** - * @brief Protocol-independent packet operations VFT - * NOTE: The following declarations should be kept in order - */ -extern const hicn_ops_t *const hicn_ops_vft[]; - -/* - * Helpers for writing recursive protocol operations on packet headers - * - * NOTE : we cannot use a shift operation as IPPROTO_NONE != 0 (and 0 is - * IPv4...) - */ -static inline hicn_type_t -TYPE_POP (hicn_type_t type) -{ -#ifndef _WIN32 - return HICN_TYPE (type.l2, type.l3, type.l4, IPPROTO_NONE); -#else - hicn_type_t new_type; - new_type.l1 = type.l2; - new_type.l2 = type.l3; - new_type.l3 = type.l4; - new_type.l4 = IPPROTO_NONE; - return new_type; -#endif -} - -static inline hicn_protocol_t * -PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) -{ - size_t header_length; - int rc = - hicn_ops_vft[type.l1]->get_current_header_length (type, h, &header_length); - if (rc < 0) - return NULL; - return (hicn_protocol_t *) ((u8 *) h + header_length); -} - -#define CHILD_OPS(f, type, h, ...) \ - (hicn_ops_vft[type.l2]->f (TYPE_POP (type), PAYLOAD (type, h), \ - ##__VA_ARGS__)) - -/** Shortcuts to entry points in VFT */ -#define HICN_OPS4 hicn_ops_vft[IPPROTO_IP] -#define HICN_OPS6 hicn_ops_vft[IPPROTO_IPV6] - -/* Helpers for simple declarations */ - -#define DECLARE_init_packet_header(protocol, error) \ - int protocol##_init_packet_header (hicn_type_t type, hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_interest_locator(protocol, error) \ - int protocol##_get_interest_locator ( \ - hicn_type_t type, const hicn_protocol_t *h, ip_address_t *ip_address) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_interest_locator(protocol, error) \ - int protocol##_set_interest_locator (hicn_type_t type, hicn_protocol_t *h, \ - const ip_address_t *ip_address) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_interest_name(protocol, error) \ - int protocol##_get_interest_name ( \ - hicn_type_t type, const hicn_protocol_t *h, hicn_name_t *name) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_interest_name(protocol, error) \ - int protocol##_set_interest_name (hicn_type_t type, hicn_protocol_t *h, \ - const hicn_name_t *name) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_interest_name_suffix(protocol, error) \ - int protocol##_get_interest_name_suffix ( \ - hicn_type_t type, const hicn_protocol_t *h, hicn_name_suffix_t *suffix) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_interest_name_suffix(protocol, error) \ - int protocol##_set_interest_name_suffix ( \ - hicn_type_t type, hicn_protocol_t *h, const hicn_name_suffix_t *suffix) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_is_interest(protocol, error) \ - int protocol##_is_interest (hicn_type_t type, const hicn_protocol_t *h, \ - int *ret) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_mark_packet_as_interest(protocol, error) \ - int protocol##_mark_packet_as_interest (hicn_type_t type, \ - hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_mark_packet_as_data(protocol, error) \ - int protocol##_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_reset_interest_for_hash(protocol, error) \ - int protocol##_reset_interest_for_hash (hicn_type_t type, \ - hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_data_locator(protocol, error) \ - int protocol##_get_data_locator ( \ - hicn_type_t type, const hicn_protocol_t *h, ip_address_t *ip_address) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_data_locator(protocol, error) \ - int protocol##_set_data_locator (hicn_type_t type, hicn_protocol_t *h, \ - const ip_address_t *ip_address) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_data_name(protocol, error) \ - int protocol##_get_data_name (hicn_type_t type, const hicn_protocol_t *h, \ - hicn_name_t *name) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_data_name(protocol, error) \ - int protocol##_set_data_name (hicn_type_t type, hicn_protocol_t *h, \ - const hicn_name_t *name) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_data_name_suffix(protocol, error) \ - int protocol##_get_data_name_suffix ( \ - hicn_type_t type, const hicn_protocol_t *h, hicn_name_suffix_t *suffix) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_data_name_suffix(protocol, error) \ - int protocol##_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, \ - const hicn_name_suffix_t *suffix) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_data_pathlabel(protocol, error) \ - int protocol##_get_data_pathlabel ( \ - hicn_type_t type, const hicn_protocol_t *h, u32 *pathlabel) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_data_pathlabel(protocol, error) \ - int protocol##_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, \ - const u32 pathlabel) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_update_data_pathlabel(protocol, error) \ - int protocol##_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, \ - const hicn_faceid_t face_id) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_reset_data_for_hash(protocol, error) \ - int protocol##_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_lifetime(protocol, error) \ - int protocol##_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, \ - hicn_lifetime_t *lifetime) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_lifetime(protocol, error) \ - int protocol##_set_lifetime (hicn_type_t type, hicn_protocol_t *h, \ - const hicn_lifetime_t lifetime) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_source_port(protocol, error) \ - int protocol##_get_source_port (hicn_type_t type, const hicn_protocol_t *h, \ - u16 *source_port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_dest_port(protocol, error) \ - int protocol##_get_dest_port (hicn_type_t type, const hicn_protocol_t *h, \ - u16 *dest_port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_source_port(protocol, error) \ - int protocol##_set_source_port (hicn_type_t type, hicn_protocol_t *h, \ - u16 source_port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_dest_port(protocol, error) \ - int protocol##_set_dest_port (hicn_type_t type, hicn_protocol_t *h, \ - u16 dest_port) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_update_checksums(protocol, error) \ - int protocol##_update_checksums (hicn_type_t type, hicn_protocol_t *h, \ - u16 partial_csum, size_t payload_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_verify_checksums(protocol, error) \ - int protocol##_verify_checksums (hicn_type_t type, hicn_protocol_t *h, \ - u16 partial_csum, size_t payload_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_rewrite_interest(protocol, error) \ - int protocol##_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, \ - const ip_address_t *addr_new, \ - ip_address_t *addr_old) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_rewrite_data(protocol, error) \ - int protocol##_rewrite_data ( \ - hicn_type_t type, hicn_protocol_t *h, const ip_address_t *addr_new, \ - ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_length(protocol, error) \ - int protocol##_get_length (hicn_type_t type, const hicn_protocol_t *h, \ - size_t *length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_current_header_length(protocol, error) \ - int protocol##_get_current_header_length ( \ - hicn_type_t type, const hicn_protocol_t *h, size_t *header_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_header_length(protocol, error) \ - int protocol##_get_header_length ( \ - hicn_type_t type, const hicn_protocol_t *h, size_t *header_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_payload_length(protocol, error) \ - int protocol##_get_payload_length ( \ - hicn_type_t type, const hicn_protocol_t *h, size_t *payload_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_payload_length(protocol, error) \ - int protocol##_set_payload_length (hicn_type_t type, hicn_protocol_t *h, \ - size_t payload_length) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_payload_type(protocol, error) \ - int protocol##_get_payload_type (hicn_type_t type, \ - const hicn_protocol_t *h, \ - hicn_payload_type_t *payload_type) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_payload_type(protocol, error) \ - int protocol##_set_payload_type (hicn_type_t type, hicn_protocol_t *h, \ - hicn_payload_type_t payload_type) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_signature_size(protocol, error) \ - int protocol##_get_signature_size ( \ - hicn_type_t type, const hicn_protocol_t *h, size_t *signature_size) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_signature_size(protocol, error) \ - int protocol##_set_signature_size (hicn_type_t type, hicn_protocol_t *h, \ - size_t signature_size) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_signature_padding(protocol, error) \ - int protocol##_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, \ - size_t padding) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_signature_padding(protocol, error) \ - int protocol##_get_signature_padding ( \ - hicn_type_t type, const hicn_protocol_t *h, size_t *padding) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_signature_timestamp(protocol, error) \ - int protocol##_set_signature_timestamp ( \ - hicn_type_t type, hicn_protocol_t *h, uint64_t signature_timestamp) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_signature_timestamp(protocol, error) \ - int protocol##_get_signature_timestamp (hicn_type_t type, \ - const hicn_protocol_t *h, \ - uint64_t *signature_timestamp) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_validation_algorithm(protocol, error) \ - int protocol##_set_validation_algorithm ( \ - hicn_type_t type, hicn_protocol_t *h, uint8_t validation_algorithm) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_validation_algorithm(protocol, error) \ - int protocol##_get_validation_algorithm (hicn_type_t type, \ - const hicn_protocol_t *h, \ - uint8_t *validation_algorithm) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_key_id(protocol, error) \ - int protocol##_set_key_id (hicn_type_t type, hicn_protocol_t *h, \ - uint8_t *key_id) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_key_id(protocol, error) \ - int protocol##_get_key_id (hicn_type_t type, hicn_protocol_t *h, \ - uint8_t **key_id, uint8_t *key_id_size) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_get_signature(protocol, error) \ - int protocol##_get_signature (hicn_type_t type, hicn_protocol_t *h, \ - uint8_t **signature) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_is_last_data(protocol, error) \ - int protocol##_is_last_data (hicn_type_t type, const hicn_protocol_t *h, \ - int *is_last) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#define DECLARE_set_last_data(protocol, error) \ - int protocol##_set_last_data (hicn_type_t type, hicn_protocol_t *h) \ - { \ - return HICN_LIB_ERROR_##error; \ - } - -#endif /* HICN_OPS_H */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/lib/includes/hicn/packet.h b/lib/includes/hicn/packet.h new file mode 100644 index 000000000..c64014fe6 --- /dev/null +++ b/lib/includes/hicn/packet.h @@ -0,0 +1,697 @@ +/* + * Copyright (c) 2017-2022 Cisco and/or its affiliates. + * 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. + */ + +/** + * @file compat.h + * @brief Implementation of the compatibility layer. + * + * The structure of the core API has evolved to support operations of a variety + * of packet formats in addition to IPv4/TCP and IPv6/TCP, namely with the use + * of ICMP for signalization and AH headers for integrity. The new API format + * has been designed to scale better with the multiplicity of packet formats, + * and provide a unified interface on top. We maintain an interface for the + * former API in this file, which mainly acts as a wrapper on top of new calls. + * + */ +#ifndef HICN_PACKET_H +#define HICN_PACKET_H + +#include <limits.h> + +#include <hicn/base.h> +#include <hicn/common.h> +#include <hicn/name.h> + +/* Packet buffer definition */ + +typedef struct __attribute__ ((packed)) +{ + /* Packet format */ + hicn_packet_format_t format; + + /* Packet type */ + hicn_packet_type_t type; + + /* + * We store an offset to the packet header. + * + * NOTE: This is a signed value. + * + * In most implementations, the buffer located closeby to the current packet + * buffer (eg msgbuf in hicn-light, and vlib buffer in VPP), and an int16_t + * would be sufficient. This is not the case in transport though and we have + * to use a full integer. + */ + int64_t header; + + /* Packet len */ + uint16_t len; + +#ifdef OPAQUE_IP + /* Interest or data packet */ + union + { + uint16_t ipv4; + uint16_t ipv6; + }; +#endif /* OPAQUE_IP */ + union + { + uint16_t tcp; + uint16_t udp; + uint16_t icmp; + }; + uint16_t newhdr; + uint16_t ah; + uint16_t payload; + + uint16_t buffer_size; + // uint16_t len; + + /* Contiguous copy of the name */ + // hicn_name_t *name; + +} hicn_packet_buffer_t; + +static_assert (sizeof (hicn_packet_buffer_t) == 28, ""); + +static inline uint8_t * +_pkbuf_get_ipv4 (const hicn_packet_buffer_t *pkbuf) +{ +#ifdef OPAQUE_IP + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->ipv4; +#else + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header; +#endif + _ASSERT (header); + return header; +} +#define pkbuf_get_ipv4(pkbuf) ((_ipv4_header_t *) (_pkbuf_get_ipv4 (pkbuf))) + +static inline uint8_t * +_pkbuf_get_ipv6 (const hicn_packet_buffer_t *pkbuf) +{ +#ifdef OPAQUE_IP + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->ipv6; +#else + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header; +#endif + assert (header); + return header; +} +#define pkbuf_get_ipv6(pkbuf) ((_ipv6_header_t *) (_pkbuf_get_ipv6 (pkbuf))) + +static inline uint8_t * +_pkbuf_get_tcp (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->tcp; + assert (header); + return header; +} +#define pkbuf_get_tcp(pkbuf) ((_tcp_header_t *) (_pkbuf_get_tcp (pkbuf))) + +static inline uint8_t * +_pkbuf_get_udp (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->udp; + assert (header); + return header; +} +#define pkbuf_get_udp(pkbuf) ((_udp_header_t *) (_pkbuf_get_udp (pkbuf))) + +static inline uint8_t * +_pkbuf_get_icmp (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->icmp; + assert (header); + return header; +} +#define pkbuf_get_icmp(pkbuf) ((_icmp_header_t *) (_pkbuf_get_icmp (pkbuf))) + +static inline uint8_t * +_pkbuf_get_ah (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->ah; + assert (header); + return header; +} +#define pkbuf_get_ah(pkbuf) ((_ah_header_t *) (_pkbuf_get_ah (pkbuf))) + +static inline uint8_t * +_pkbuf_get_new (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header + pkbuf->newhdr; + assert (header); + return header; +} +#define pkbuf_get_new(pkbuf) ((_new_header_t *) (_pkbuf_get_new (pkbuf))) + +static inline uint8_t * +pkbuf_get_header (const hicn_packet_buffer_t *pkbuf) +{ + uint8_t *header = (uint8_t *) pkbuf + pkbuf->header; + assert (header); + return header; +} + +static inline void +pkbuf_set_header (hicn_packet_buffer_t *pkbuf, uint8_t *header) +{ + ssize_t offset = header - (uint8_t *) pkbuf; + assert (offset < INT64_MAX); + assert (offset > INT64_MIN); + pkbuf->header = (int64_t) offset; +} + +/* + * Packet buffer operations + * + * A packet buffer can either be initialized either: + * + * 1) from an empty buffer (packet crafting). + * + * #define MTU 1500 + * size_t size = MTU; + * u8 buffer[MTU]; + * + * hicn_packet_t pkbuf; + * hicn_packet_set_format(&pkbuf, HICN_PACKET_FORMAT_NEW); + * hicn_packet_set_buffer(&pkbuf, &buffer, size); + * hicn_packet_init_header(&pkbuf, 0); + * + * An empty (but correct) packet is not available in the buffer, ready to be + * modified and/or sent. + * + * 2) from an existing buffer (packet reception): + * + * hicn_packet_t pkbuf; + * hicn_packet_set_buffer(&pkbuf, &buffer, size); + * hicn_packet_analyze(&pkbuf); + * + * It is then possible to retrieve properties of the packet such as format and + * type (interest, data, etc.). + * + * hicn_packet_get_format(&pkbuf); + * hicn_packet_get_type(&pkbuf); + * + * */ + +hicn_packet_format_t +hicn_packet_get_format (const hicn_packet_buffer_t *pkbuf); + +void hicn_packet_set_format (hicn_packet_buffer_t *pkbuf, + hicn_packet_format_t format); + +hicn_packet_type_t hicn_packet_get_type (const hicn_packet_buffer_t *pkbuf); +void hicn_packet_set_type (hicn_packet_buffer_t *pkbuf, + hicn_packet_type_t type); + +bool hicn_packet_is_interest (const hicn_packet_buffer_t *pkbuf); + +bool hicn_packet_is_data (const hicn_packet_buffer_t *pkbuf); + +bool hicn_packet_is_undefined (const hicn_packet_buffer_t *pkbuf); + +/** + * @brief Initialize the buffer from packet buffer metadata (builds a valid + * packet). + * @param [in] pkbuf - hICN packet buffer + * @return hICN error code + * + * Packet type, format, and a buffer are required. + */ +int hicn_packet_init_header (hicn_packet_buffer_t *pkbuf, + size_t additional_header_size); + +/** + * @brief Reset information stored in the packet header. + * @param [in] pkbuf - hICN packet buffer + * @return hICN error code + */ +int hicn_packet_reset (hicn_packet_buffer_t *pkbuf); + +/** + * @brief Analyze buffer to populate metadata in packet buffer. + * @param [in] pkbuf - hICN packet buffer + * @return hICN error code + */ +int hicn_packet_analyze (hicn_packet_buffer_t *pkbuf); + +/** + * @brief Initialize hicn packet storage space with a buffer + * @param [in] pkbuf - hICN packet buffer + * @param [in] buffer - Packet storage buffer + * @param [in] analyze - Flag indicating whether to analyze the buffer + * content to populate packet format, header's offsets, etc. + * @return hICN error code + */ +int hicn_packet_set_buffer (hicn_packet_buffer_t *pkbuf, u8 *buffer, + uint16_t buffer_size, uint16_t len); + +/** + * @brief Retrieve the storage buffer. + * @param [in] pkbuf - hICN packet buffer + * @param [out] buffer - Packet buffer + * @param [out] buffer_size - Packet buffer size + * @return Pointer to storage buffer (this only returns the pointer; no copy is + * made) + */ +int hicn_packet_get_buffer (const hicn_packet_buffer_t *pkbuf, u8 **buffer, + uint16_t *buffer_size, uint16_t *len); + +/** + * @brief Retrieve the packet length + * @param [in] pkbuf - hICN packet buffer + * @return Length of the stored packet + */ +size_t hicn_packet_get_len (const hicn_packet_buffer_t *pkbuf); + +/** + * @brief Set the packet length + * @param [in] pkbuf - hICN packet buffer + * @param [in] len - hICN packet length + * @return None + */ +int hicn_packet_set_len (hicn_packet_buffer_t *pkbuf, size_t len); + +/** + * @brief Return total length of hicn headers (before payload) + * @param [in] pkbuf - hICN packet buffer + * @param [out] len - Total headers length + * @return Headers' len + */ +int hicn_packet_get_header_len (const hicn_packet_buffer_t *pkbuf, + size_t *len); + +/** + * @brief Return the length of hICN payload in the packet. + * @param [in] pkbuf - hICN packet buffer + * @param [out] len - hICN payload length + * @return Payload len + */ +int hicn_packet_get_payload_len (const hicn_packet_buffer_t *pkbuf, + size_t *len); + +/** + * @brief Sets the payload of a packet + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @param [in] payload - payload to set + * @param [in] payload_length - size of the payload to set + * @return hICN error code + * + * NOTE: + * - The buffer holding payload is assumed sufficiently large + * - This function updates header fields with the new length, but no checksum. + */ +int hicn_packet_set_payload (const hicn_packet_buffer_t *pkbuf, + const u8 *payload, u16 payload_length); + +/** + * @brief Retrieves the payload of a packet + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] payload - pointer to buffer for storing the result + * @param [out] payload_length - size of the retreived payload + * @param [in] hard_copy - Flag : if true (eg. 1), a copy of the payload is + * made into the payload buffer, otherwise (0) the pointer is changed to point + * to the payload offset in the packet. + * @return hICN error code + * + * NOTE: + * - The buffer holding payload is assumed sufficiently large + */ +int hicn_packet_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy); + +/* Header fields manipulation */ + +/** + * @brief Return total length of hicn headers (but signature payload) + * @param [in] pkbuf - hICN packet buffer + * @param [out] header_length - Total length of headers + * @return hICN error code + */ +int hicn_packet_get_header_length_from_format (hicn_packet_format_t format, + size_t *header_length); + +/** + * @brief Sets payload length + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @param [in] payload_length - payload length + * @return hICN error code + */ +int hicn_packet_set_payload_length (const hicn_packet_buffer_t *pkbuf, + const size_t payload_length); + +/** + * @brief Compare two hICN packets + * @param [in] packet_1 - First packet + * @param [in] packet_2 - Second packet + * @return 0 if both packets are considered equal, any other value otherwise. + */ +int hicn_packet_compare (const hicn_packet_buffer_t *pkbuf11, + const hicn_packet_buffer_t *pkbuf22); + +/** + * @brief Retrieve the name of an interest/data packet + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] name - name holding the result + * @return hICN error code + */ +int hicn_packet_get_name (const hicn_packet_buffer_t *pkbuf, + hicn_name_t *name); + +/** + * @brief Sets the name of an interest/data packet + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @param [in] name - name to set into packet + * @return hICN error code + */ +int hicn_packet_set_name (const hicn_packet_buffer_t *pkbuf, + const hicn_name_t *name); + +/** + * @brief Retrieve the locator of an interest / data packet + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] ip_address - retrieved locator + * @return hICN error code + */ +int hicn_packet_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *prefix); + +/** + * @brief Sets the locator of an interest / data packet + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @param [out] ip_address - retrieved locator + * @return hICN error code + */ +int hicn_packet_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *prefix); + +int hicn_packet_save_header (const hicn_packet_buffer_t *pkbuf, u8 *header, + size_t *header_len, bool copy_ah); + +int hicn_packet_load_header (const hicn_packet_buffer_t *pkbuf, + const u8 *header, size_t header_len); + +int hicn_packet_get_lifetime (const hicn_packet_buffer_t *pkbuf, + u32 *lifetime); +int hicn_packet_set_lifetime (const hicn_packet_buffer_t *pkbuf, u32 lifetime); + +/* Interest */ +int hicn_interest_get_name (const hicn_packet_buffer_t *pkbuf, + hicn_name_t *name); +int hicn_interest_set_name (const hicn_packet_buffer_t *pkbuf, + const hicn_name_t *name); +int hicn_interest_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *prefix); +int hicn_interest_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *prefix); +int hicn_interest_compare (const hicn_packet_buffer_t *pkbuf11, + const hicn_packet_buffer_t *pkbuf2); +int hicn_interest_set_lifetime (const hicn_packet_buffer_t *pkbuf, + u32 lifetime); +int hicn_interest_get_lifetime (const hicn_packet_buffer_t *pkbuf, + u32 *lifetime); +int hicn_interest_get_header_length (const hicn_packet_buffer_t *pkbuf, + size_t *header_length); +int hicn_interest_get_payload_length (const hicn_packet_buffer_t *pkbuf, + size_t *payload_length); +int hicn_interest_set_payload (const hicn_packet_buffer_t *pkbuf, + const u8 *payload, size_t payload_length); +int hicn_interest_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy); +int hicn_interest_reset_for_hash (hicn_packet_buffer_t *pkbuf); + +/* Data */ + +int hicn_data_get_name (const hicn_packet_buffer_t *pkbuf, hicn_name_t *name); +int hicn_data_set_name (const hicn_packet_buffer_t *pkbuf, + const hicn_name_t *name); +int hicn_data_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *prefix); +int hicn_data_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *prefix); +int hicn_data_compare (const hicn_packet_buffer_t *pkbuf11, + const hicn_packet_buffer_t *pkbuf22); +int hicn_data_get_expiry_time (const hicn_packet_buffer_t *pkbuf, + u32 *expiry_time); +int hicn_data_set_expiry_time (const hicn_packet_buffer_t *pkbuf, + u32 expiry_time); +int hicn_data_get_header_length (const hicn_packet_buffer_t *pkbuf, + size_t *header_length); +int hicn_data_get_payload_length (const hicn_packet_buffer_t *pkbuf, + size_t *payload_length); + +/* Path label */ + +/** + * @brief Returns the path label from a data packet + * @param [in] pkbuf - packet buffer + * @param [in] hdr - packet header + * @param [in] path_label - pointer in which to store the path label value + * @return hICN error code + */ +int hicn_data_get_path_label (const hicn_packet_buffer_t *pkbufdr, + hicn_path_label_t *path_label); + +/** + * @brief Returns the path label from a packet + * @param [in] pkbuf - packet buffer + * @param [in] hdr - packet header + * @param [in] path_label - pointer in which to store the path label value + * @return hICN error code + */ +int hicn_get_path_label (const hicn_packet_buffer_t *pkbufdr, + hicn_path_label_t *path_label); + +int hicn_data_set_path_label (const hicn_packet_buffer_t *pkbuf, + hicn_path_label_t path_label); + +/* Data specific flags */ + +int hicn_packet_get_payload_type (const hicn_packet_buffer_t *pkbuf, + + hicn_payload_type_t *payload_type); +int hicn_packet_set_payload_type (const hicn_packet_buffer_t *pkbuf, + + const hicn_payload_type_t payload_type); + +int hicn_data_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy); +int hicn_data_set_payload (const hicn_packet_buffer_t *pkbuf, + const u8 *payload, size_t payload_length); +int hicn_data_get_payload_type (hicn_payload_type_t *payload_type); +int hicn_data_set_payload_type (hicn_payload_type_t payload_type); +int hicn_data_reset_for_hash (hicn_packet_buffer_t *pkbuf); +int hicn_data_is_last (const hicn_packet_buffer_t *pkbuf, int *is_last); +int hicn_data_set_last (const hicn_packet_buffer_t *pkbuf); + +/* Security */ + +/** + * @brief Retrieves the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] bytes - Retrieved signature size + * @return hICN error code + */ +int hicn_packet_get_signature_size (const hicn_packet_buffer_t *pkbuf, + size_t *bytes); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [in] bytes - Retrieved signature size + * @return hICN error code + */ +int hicn_packet_set_signature_size (const hicn_packet_buffer_t *pkbuf, + size_t bytes); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [in] signature_timestamp - Signature timestamp to set + * @return hICN error code + */ +int hicn_packet_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, + uint64_t signature_timestamp); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] signature_timestamp - Retrieved signature timestamp + * @return hICN error code + */ +int hicn_packet_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, + uint64_t *signature_timestamp); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [in] validation_algorithm - Validation algorithm to set + * @return hICN error code + */ +int hicn_packet_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, + uint8_t validation_algorithm); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] validation_algorithm - Retrieved validation algorithm + * @return hICN error code + */ +int hicn_packet_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, + uint8_t *validation_algorithm); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [in] key_id - Key id to set + * @param [in] key_len - Key length + * @return hICN error code + */ +int hicn_packet_set_key_id (const hicn_packet_buffer_t *pkbuf, uint8_t *key_id, + size_t key_len); + +/** + * @brief Sets the signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] packet - packet header + * @param [out] key_id - Retrieved key id + * @return hICN error code + */ +int hicn_packet_get_key_id (const hicn_packet_buffer_t *pkbuf, + uint8_t **key_id, uint8_t *key_id_length); + +int hicn_packet_get_signature (const hicn_packet_buffer_t *pkbuf, + uint8_t **sign_buf); + +int hicn_packet_get_signature_padding (const hicn_packet_buffer_t *pkbuf, + size_t *bytes); +int hicn_packet_set_signature_padding (const hicn_packet_buffer_t *pkbuf, + size_t bytes); + +/* Checksums */ + +/** + * @brief Update checksums in packet headers + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @return hICN error code + */ +int hicn_packet_compute_checksum (const hicn_packet_buffer_t *pkbuf); + +/** + * @brief compute the checksum of the packet header, adding init_sum to the + * final value + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @param [in] init_sum - value to add to the final checksum + * @return hICN error code + */ +int hicn_packet_compute_header_checksum (const hicn_packet_buffer_t *pkbuf, + u16 init_sum); + +/** + * @brief Verify checksums in packet headers + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] packet - packet header + * @return hICN error code + */ +int hicn_packet_check_integrity_no_payload (const hicn_packet_buffer_t *pkbuf, + u16 init_sum); + +/** + * @brief Returns the packet TTL + * @param [in] pkbuf - hICN packet buffer + * @param [out] hops - Pointer to the variable receiving the TTL value + * @return hICN error code + */ +int hicn_packet_get_ttl (const hicn_packet_buffer_t *pkbuf, u8 *hops); + +/** + * @brief Returns the packet source port + * @param [in] pkbuf - hICN packet buffer + * @param [in, out] pos - Current position in the sequence of headers while + * @param [out] hops - The TTL value to set + * @return hICN error code + */ +int hicn_packet_set_ttl (const hicn_packet_buffer_t *pkbuf, u8 hops); + +/** + * @brief Returns the packet source port + * @param [in] pkbuf - hICN packet buffer + * @param [out] port - Pointer to the variable that will receive the port + * number + * @return hICN error code + */ +int hicn_packet_get_src_port (const hicn_packet_buffer_t *pkbuf, u16 *port); + +/** + * @brief Sets the packet source port + * @param [in] pkbuf - hICN packet buffer + * @param [out] port - The port number to set + * @return hICN error code + */ +int hicn_packet_set_src_port (const hicn_packet_buffer_t *pkbuf, u16 port); + +/** + * @brief Returns the packet source port + * @param [in] pkbuf - hICN packet buffer + * @param [out] port - Pointer to the variable that will receive the port + * number + * @return hICN error code + */ +int hicn_packet_get_dst_port (const hicn_packet_buffer_t *pkbuf, u16 *port); + +/** + * @brief Sets the packet source port + * @param [in] pkbuf - hICN packet buffer + * @param [out] port - The port number to set + * @return hICN error code + */ +int hicn_packet_set_dst_port (const hicn_packet_buffer_t *pkbuf, u16 port); + +int hicn_interest_rewrite (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old); + +int hicn_data_rewrite (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, + const hicn_faceid_t face_id, u8 reset_pl); + +#endif /* HICN_PACKET_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/includes/hicn/policy.h b/lib/includes/hicn/policy.h index f39fa9272..50b771537 100644 --- a/lib/includes/hicn/policy.h +++ b/lib/includes/hicn/policy.h @@ -151,7 +151,7 @@ policy_state_t policy_state_from_str (const char *str); /* POLICY TAG STATE */ -typedef struct +typedef struct __attribute__ ((packed, aligned (1))) { policy_state_t state; uint8_t disabled; diff --git a/lib/includes/hicn/protocol.h b/lib/includes/hicn/protocol.h deleted file mode 100644 index fb142e4c3..000000000 --- a/lib/includes/hicn/protocol.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2021 Cisco and/or its affiliates. - * 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. - */ - -/** - * @file protocol.h - * @brief Protocol header definitions - */ -#ifndef HICN_PROTOCOL_H -#define HICN_PROTOCOL_H - -#include "protocol/ah.h" -#include "protocol/ipv4.h" -#include "protocol/ipv6.h" -#include "protocol/icmp.h" -#include "protocol/icmprd.h" -#include "protocol/tcp.h" -#include "protocol/udp.h" -#include "protocol/new.h" - -typedef union -{ - _new_header_t newhdr; - _ipv4_header_t ipv4; - _ipv6_header_t ipv6; - _tcp_header_t tcp; - _udp_header_t udp; - _icmp_header_t icmp; - _icmprd_header_t icmprd; - _ah_header_t ah; - void *bytes; -} hicn_protocol_t; - -#endif /* HICN_PROTOCOL_H */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/lib/includes/hicn/protocol/icmprd.h b/lib/includes/hicn/protocol/icmprd.h index 897e7969e..17088c22a 100644 --- a/lib/includes/hicn/protocol/icmprd.h +++ b/lib/includes/hicn/protocol/icmprd.h @@ -33,7 +33,7 @@ typedef struct u8 type; u8 code; u16 csum; - ip4_address_t ip; + ipv4_address_t ip; _ipv4_header_t iph; u8 data[64]; } _icmprd4_header_t; @@ -53,8 +53,8 @@ typedef struct u8 code; u16 csum; u32 res; - ip6_address_t tgt; - ip6_address_t dst; + ipv6_address_t tgt; + ipv6_address_t dst; } _icmprd_header_t; #define ICMPRD_HDRLEN sizeof (_icmprd_header_t) diff --git a/lib/includes/hicn/protocol/ipv4.h b/lib/includes/hicn/protocol/ipv4.h index 44e95c1e3..89435b516 100644 --- a/lib/includes/hicn/protocol/ipv4.h +++ b/lib/includes/hicn/protocol/ipv4.h @@ -54,8 +54,8 @@ typedef struct u8 ttl; u8 protocol; u16 csum; - ip4_address_t saddr; - ip4_address_t daddr; + ipv4_address_t saddr; + ipv4_address_t daddr; } _ipv4_header_t; #define ipv4_header_bytes(ipv4_header) \ @@ -72,8 +72,8 @@ static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN, typedef struct { - ip4_address_t ip_src; - ip4_address_t ip_dst; + ipv4_address_t ip_src; + ipv4_address_t ip_dst; u8 zero; u8 protocol; u16 size; diff --git a/lib/includes/hicn/protocol/ipv6.h b/lib/includes/hicn/protocol/ipv6.h index 86301e7d1..6e8e30af3 100644 --- a/lib/includes/hicn/protocol/ipv6.h +++ b/lib/includes/hicn/protocol/ipv6.h @@ -42,8 +42,8 @@ typedef struct u8 vfc; /* 4 bits version, top 4 bits class */ }; #endif - ip6_address_t saddr; /* source address */ - ip6_address_t daddr; /* destination address */ + ipv6_address_t saddr; /* source address */ + ipv6_address_t daddr; /* destination address */ } _ipv6_header_t; #define IPV6_HDRLEN sizeof (_ipv6_header_t) @@ -57,8 +57,8 @@ static_assert (EXPECTED_IPV6_HDRLEN == IPV6_HDRLEN, typedef struct { - ip6_address_t ip_src; - ip6_address_t ip_dst; + ipv6_address_t ip_src; + ipv6_address_t ip_dst; u32 size; u16 zeros; u8 zero; diff --git a/lib/includes/hicn/protocol/new.h b/lib/includes/hicn/protocol/new.h index 47f7758d2..e688bef21 100644 --- a/lib/includes/hicn/protocol/new.h +++ b/lib/includes/hicn/protocol/new.h @@ -34,7 +34,7 @@ typedef struct u8 flags; u16 payload_length; u32 lifetime; - ip_address_t prefix; + hicn_ip_address_t prefix; u32 suffix; u32 path_label; } _new_header_t; diff --git a/lib/includes/hicn/util/bitmap.h b/lib/includes/hicn/util/bitmap.h index 11eb7870b..15d47ac61 100644 --- a/lib/includes/hicn/util/bitmap.h +++ b/lib/includes/hicn/util/bitmap.h @@ -24,17 +24,26 @@ #define UTIL_BITMAP_H #include <assert.h> +#include <stdio.h> #include <string.h> #include <sys/param.h> // MIN, MAX -#include <hicn/util/log.h> - #include <hicn/common.h> #include <hicn/util/vector.h> -typedef uint_fast32_t bitmap_t; +typedef hicn_uword bitmap_t; + +#define WORD_WIDTH (sizeof (bitmap_t) * 8) +#define WORD_WIDTH (sizeof (bitmap_t) * 8) #define BITMAP_WIDTH(bitmap) (sizeof ((bitmap)[0]) * 8) +#define BITMAP_INVALID_INDEX ((uint32_t) (~0)) + +static inline int +get_lowest_set_bit_index (hicn_uword w) +{ + return hicn_uword_bits > 32 ? __builtin_ctzll (w) : __builtin_ctz (w); +} /** * @brief Allocate and initialize a bitmap @@ -79,13 +88,27 @@ bitmap_ensure_pos (bitmap_t **bitmap, off_t pos) * @param[in] i The bit position. */ static inline int -bitmap_get (const bitmap_t *bitmap, off_t i) +_bitmap_get (const bitmap_t *bitmap, off_t i) { size_t offset = i / BITMAP_WIDTH (bitmap); assert (offset < bitmap_get_alloc_size (bitmap)); size_t pos = i % BITMAP_WIDTH (bitmap); - size_t shift = BITMAP_WIDTH (bitmap) - pos - 1; - return (bitmap[offset] >> shift) & 1; + return (bitmap[offset] >> pos) & 1; +} + +/** + * @brief Retrieve the state of the i-th bit in the bitmap. + * Does not sanity check the position. + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + */ +static inline int +_bitmap_get_no_check (const bitmap_t *bitmap, off_t i) +{ + size_t offset = i / BITMAP_WIDTH (bitmap); + size_t pos = i % BITMAP_WIDTH (bitmap); + return (bitmap[offset] >> pos) & 1; } /* @@ -96,8 +119,13 @@ bitmap_get (const bitmap_t *bitmap, off_t i) * * @return bool */ -#define bitmap_is_set(bitmap, i) (bitmap_get ((bitmap), (i)) == 1) -#define bitmap_is_unset(bitmap, i) (bitmap_get ((bitmap), (i)) == 0) +#define bitmap_is_set(bitmap, i) (_bitmap_get ((bitmap), (i)) == 1) +#define bitmap_is_unset(bitmap, i) (_bitmap_get ((bitmap), (i)) == 0) + +#define bitmap_is_set_no_check(bitmap, i) \ + (_bitmap_get_no_check ((bitmap), (i)) == 1) +#define bitmap_is_unset_no_check(bitmap, i) \ + (_bitmap_get_no_check ((bitmap), (i)) == 0) /* * @brief Returns whether the i-th bit is unset (equal to 0) in a bitmap. @@ -107,7 +135,9 @@ bitmap_get (const bitmap_t *bitmap, off_t i) * * @return bool */ -#define bitmap_set(bitmap, i) _bitmap_set ((bitmap_t **) &bitmap, i) +#define bitmap_set(bitmap, i) _bitmap_set ((bitmap_t **) &bitmap, i, 1) +#define bitmap_set_no_check(bitmap, i) \ + _bitmap_set ((bitmap_t **) &bitmap, i, 0) /* * @brief Returns whether the i-th bit is unset (equal to 0) in a bitmap @@ -119,29 +149,30 @@ bitmap_get (const bitmap_t *bitmap, off_t i) * @return bool */ static inline int -_bitmap_set (bitmap_t **bitmap_ptr, off_t i) +_bitmap_set (bitmap_t **bitmap_ptr, off_t i, int check) { - if (bitmap_ensure_pos (bitmap_ptr, i) < 0) + if (check && bitmap_ensure_pos (bitmap_ptr, i) < 0) return -1; bitmap_t *bitmap = *bitmap_ptr; size_t offset = i / BITMAP_WIDTH (bitmap); size_t pos = i % BITMAP_WIDTH (bitmap); - size_t shift = BITMAP_WIDTH (bitmap) - pos - 1; - bitmap[offset] |= (bitmap_t) 1 << shift; + bitmap[offset] |= (bitmap_t) 1 << pos; return 0; } +#define bitmap_unset(bitmap, i) _bitmap_unset (bitmap, i, 1) +#define bitmap_unset_no_check(bitmap, i) _bitmap_unset (bitmap, i, 0) + static inline int -bitmap_unset (bitmap_t *bitmap, off_t i) +_bitmap_unset (bitmap_t *bitmap, off_t i, int check) { - if (bitmap_ensure_pos (&bitmap, i) < 0) + if (check && bitmap_ensure_pos (&bitmap, i) < 0) return -1; size_t offset = i / BITMAP_WIDTH (bitmap); size_t pos = i % BITMAP_WIDTH (bitmap); - size_t shift = BITMAP_WIDTH (bitmap) - pos - 1; - bitmap[offset] &= ~(1ul << shift); + bitmap[offset] &= ~(1ul << pos); return 0; } @@ -201,7 +232,6 @@ bitmap_set_range (bitmap_t *bitmap, off_t from, off_t to) return 0; END: - ERROR ("Error setting bitmap range\n"); return -1; } @@ -209,4 +239,88 @@ END: #define bitmap_free(bitmap) vector_free (bitmap) +static inline hicn_uword +bitmap_next_set_no_check (const bitmap_t *bitmap, hicn_uword i, size_t length) +{ + hicn_uword pos = i / WORD_WIDTH; + hicn_uword offset = i % WORD_WIDTH; + hicn_uword tmp; + hicn_uword mask = ~((1ULL << offset) - 1); + + if (pos < length) + { + // This will zeroes all bits < i + tmp = bitmap[pos] & mask; + if (tmp) + return get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + + for (pos += 1; pos < length; pos++) + { + tmp = bitmap[pos]; + if (tmp) + return get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + } + } + + return BITMAP_INVALID_INDEX; +} + +static inline hicn_uword +bitmap_next_set (const bitmap_t *bitmap, hicn_uword i) +{ + return bitmap_next_set_no_check (bitmap, i, vector_get_alloc_size (bitmap)); +} + +static inline hicn_uword +bitmap_next_unset_no_check (const bitmap_t *bitmap, hicn_uword i, + size_t length) +{ + hicn_uword pos = i / WORD_WIDTH; + hicn_uword offset = i % WORD_WIDTH; + hicn_uword tmp; + hicn_uword mask = ~((1ULL << offset) - 1); + + if (pos < length) + { + // This will zeroes all bits < i + tmp = ~bitmap[pos] & mask; + if (tmp) + return get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + + for (pos += 1; pos < length; pos++) + { + tmp = ~bitmap[pos]; + if (tmp) + return get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + } + } + return BITMAP_INVALID_INDEX; +} + +static inline hicn_uword +bitmap_next_unset (const bitmap_t *bitmap, hicn_uword i) +{ + return bitmap_next_unset_no_check (bitmap, i, + vector_get_alloc_size (bitmap)); +} + +#define bitmap_first_set(bitmap) bitmap_next_set (bitmap, 0) +#define bitmap_first_set_no_check(bitmap, size) \ + bitmap_next_set_no_checks (bitmap, 0, size) + +#define bitmap_first_unset(bitmap) bitmap_next_unset (bitmap, 0) +#define bitmap_first_unset_no_check(bitmap, size) \ + bitmap_next_unset_no_checks (bitmap, 0, size) + +static inline void +bitmap_print (const bitmap_t *bitmap, size_t n_words) +{ + for (size_t word = 0; word < n_words; word++) + { + for (int bit = sizeof (hicn_uword) - 1; bit >= 0; bit--) + (bitmap_is_set_no_check (&bitmap[word], bit)) ? printf ("1") : + printf ("0"); + } +} + #endif /* UTIL_BITMAP_H */ diff --git a/lib/includes/hicn/util/hash.h b/lib/includes/hicn/util/hash.h index ded8fc370..8dbe0d680 100644 --- a/lib/includes/hicn/util/hash.h +++ b/lib/includes/hicn/util/hash.h @@ -28,6 +28,7 @@ #define UTIL_HASH_H #include <stdint.h> +#include <stddef.h> // size_t #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ __BYTE_ORDER == __LITTLE_ENDIAN) || \ diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h index 89a4c11e0..55d5f5fc3 100644 --- a/lib/includes/hicn/util/ip_address.h +++ b/lib/includes/hicn/util/ip_address.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -32,10 +32,11 @@ #endif #include <errno.h> +#include <assert.h> #ifndef _WIN32 #include <netinet/in.h> // struct sockadd -#include <arpa/inet.h> -#include <netdb.h> // struct addrinfo +#include <arpa/inet.h> // inet_ntop +#include <netdb.h> // struct addrinfo #endif #include <stdbool.h> #include <stdlib.h> @@ -61,11 +62,10 @@ #endif //#define INET_MAX_ADDRSTRLEN INET6_ADDRSTRLEN -#define IP_MAX_ADDR_LEN IPV6_ADDR_LEN +#define IP_ADDRESS_MAX_LEN IPV6_ADDR_LEN #define DUMMY_PORT 1234 -#ifndef HICN_VPP_PLUGIN typedef union { struct in_addr as_inaddr; @@ -73,9 +73,11 @@ typedef union u8 as_u8[4]; u16 as_u16[2]; u32 as_u32; -} ip4_address_t; +} ipv4_address_t; -typedef union +static_assert (sizeof (ipv4_address_t) == 4, ""); + +typedef union __attribute__ ((__packed__)) { struct in6_addr as_in6addr; u8 buffer[16]; @@ -83,16 +85,24 @@ typedef union u16 as_u16[8]; u32 as_u32[4]; u64 as_u64[2]; -} ip6_address_t; +} ipv6_address_t; + +static_assert (sizeof (ipv6_address_t) == 16, ""); + +#ifdef HICN_VPP_PLUGIN +#include <vnet/ip/ip46_address.h> +static_assert (sizeof (ipv4_address_t) == sizeof (ip4_address_t), ""); +static_assert (sizeof (ipv6_address_t) == sizeof (ip6_address_t), ""); +#endif typedef union { struct { u32 pad[3]; - ip4_address_t v4; + ipv4_address_t v4; }; - ip6_address_t v6; + ipv6_address_t v6; #if 0 /* removed as prone to error due to IPv4 padding */ u8 buffer[IP_MAX_ADDR_LEN]; u8 as_u8[IP_MAX_ADDR_LEN]; @@ -100,28 +110,14 @@ typedef union u32 as_u32[IP_MAX_ADDR_LEN >> 2]; u64 as_u64[IP_MAX_ADDR_LEN >> 3]; #endif -} ip_address_t; - -#else - -#include <vnet/ip/ip4_packet.h> // ip4_address_t -#include <vnet/ip/ip6_packet.h> // ip6_address_t - -#if __GNUC__ >= 9 -#pragma GCC diagnostic ignored "-Waddress-of-packed-member" -#endif - -#include <vnet/ip/ip46_address.h> - -#if __GNUC__ >= 9 -#pragma GCC diagnostic pop -#endif - -typedef ip46_address_t ip_address_t; - +#ifdef HICN_VPP_PLUGIN + ip46_address_t as_ip46; #endif /* HICN_VPP_PLUGIN */ +} hicn_ip_address_t; + +static_assert (sizeof (hicn_ip_address_t) == 16, ""); -#define ip_address_is_v4(ip) \ +#define hicn_ip_address_is_v4(ip) \ (((ip)->pad[0] | (ip)->pad[1] | (ip)->pad[2]) == 0) #define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1 @@ -131,24 +127,26 @@ typedef ip46_address_t ip_address_t; #define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1 #define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1 +#define IP_ADDRESS_V4_OFFSET_LEN 12 + typedef struct { int family; - ip_address_t address; + hicn_ip_address_t address; u8 len; -} ip_prefix_t; +} hicn_ip_prefix_t; #define MAXSZ_IP_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3 #define MAXSZ_IP_PREFIX MAXSZ_IP_PREFIX_ + 1 -extern const ip_address_t IPV4_LOOPBACK; -extern const ip_address_t IPV6_LOOPBACK; -extern const ip_address_t IPV4_ANY; -extern const ip_address_t IPV6_ANY; +extern const hicn_ip_address_t IPV4_LOOPBACK; +extern const hicn_ip_address_t IPV6_LOOPBACK; +extern const hicn_ip_address_t IPV4_ANY; +extern const hicn_ip_address_t IPV6_ANY; -extern const ip4_address_t IP4_ADDRESS_EMPTY; -extern const ip6_address_t IP6_ADDRESS_EMPTY; -extern const ip_address_t IP_ADDRESS_EMPTY; +extern const ipv4_address_t IP4_ADDRESS_EMPTY; +extern const ipv6_address_t IP6_ADDRESS_EMPTY; +extern const hicn_ip_address_t IP_ADDRESS_EMPTY; #define IP_ANY(family) (family == AF_INET) ? IPV4_ANY : IPV6_ANY @@ -161,32 +159,52 @@ extern const ip_address_t IP_ADDRESS_EMPTY; #define IS_VALID_FAMILY(x) ((x == AF_INET) || (x == AF_INET6)) /* IP address */ - -int ip_address_get_family (const char *ip_address); -int ip_address_len (int family); -const u8 *ip_address_get_buffer (const ip_address_t *ip_address, int family); -int ip_address_ntop (const ip_address_t *ip_address, char *dst, - const size_t len, int family); -int ip_address_pton (const char *ip_address_str, ip_address_t *ip_address); -int ip_address_snprintf (char *s, size_t size, const ip_address_t *ip_address, - int family); -int ip_address_to_sockaddr (const ip_address_t *ip_address, - struct sockaddr *sa, int family); -int ip_address_cmp (const ip_address_t *ip1, const ip_address_t *ip2, - int family); -int ip_address_empty (const ip_address_t *ip); +int hicn_ip_address_get_family (const hicn_ip_address_t *address); +int hicn_ip_address_str_get_family (const char *ip_address); +int hicn_ip_address_len (int family); +int hicn_ip_address_get_len (const hicn_ip_address_t *ip_address); + +int hicn_ip_address_get_len_bits (const hicn_ip_address_t *ip_address); +const u8 *hicn_ip_address_get_buffer (const hicn_ip_address_t *ip_address, + int family); +int hicn_ip_address_ntop (const hicn_ip_address_t *ip_address, char *dst, + const size_t len, int family); +int hicn_ip_address_pton (const char *ip_address_str, + hicn_ip_address_t *ip_address); +int hicn_ip_address_snprintf (char *s, size_t size, + const hicn_ip_address_t *ip_address); +int hicn_ip_address_to_sockaddr (const hicn_ip_address_t *ip_address, + struct sockaddr *sa, int family); +int hicn_ip_address_cmp (const hicn_ip_address_t *ip1, + const hicn_ip_address_t *ip2); +bool hicn_ip_address_equals (const hicn_ip_address_t *ip1, + const hicn_ip_address_t *ip2); +int hicn_ip_address_empty (const hicn_ip_address_t *ip); + +uint8_t hicn_ip_address_get_bit (const hicn_ip_address_t *address, + uint8_t pos); + +bool hicn_ip_address_match_family (const hicn_ip_address_t *address, + int family); + +uint32_t hicn_ip_address_get_hash (const hicn_ip_address_t *address); /* Prefix */ -int ip_prefix_pton (const char *ip_address_str, ip_prefix_t *ip_prefix); -int ip_prefix_ntop_short (const ip_prefix_t *ip_prefix, char *dst, - size_t size); -int ip_prefix_ntop (const ip_prefix_t *ip_prefix, char *dst, size_t size); -int ip_prefix_snprintf (char *s, size_t size, const ip_prefix_t *prefix); -int ip_prefix_len (const ip_prefix_t *prefix); -bool ip_prefix_empty (const ip_prefix_t *prefix); -int ip_prefix_to_sockaddr (const ip_prefix_t *prefix, struct sockaddr *sa); -int ip_prefix_cmp (const ip_prefix_t *prefix1, const ip_prefix_t *prefix2); +int hicn_ip_prefix_pton (const char *ip_address_str, + hicn_ip_prefix_t *hicn_ip_prefix); +int hicn_ip_prefix_ntop_short (const hicn_ip_prefix_t *hicn_ip_prefix, + char *dst, size_t size); +int hicn_ip_prefix_ntop (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst, + size_t size); +int hicn_ip_prefix_snprintf (char *s, size_t size, + const hicn_ip_prefix_t *prefix); +int hicn_ip_prefix_len (const hicn_ip_prefix_t *prefix); +bool hicn_ip_prefix_empty (const hicn_ip_prefix_t *prefix); +int hicn_ip_prefix_to_sockaddr (const hicn_ip_prefix_t *prefix, + struct sockaddr *sa); +int hicn_ip_prefix_cmp (const hicn_ip_prefix_t *prefix1, + const hicn_ip_prefix_t *prefix2); /* URL */ @@ -200,7 +218,7 @@ int ip_prefix_cmp (const ip_prefix_t *prefix1, const ip_prefix_t *prefix2); #define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM #define MAXSZ_URL MAXSZ_URL_ + NULLTERM -int url_snprintf (char *s, size_t size, int family, - const ip_address_t *ip_address, u16 port); +int url_snprintf (char *s, size_t size, const hicn_ip_address_t *ip_address, + u16 port); #endif /* UTIL_IP_ADDRESS_H */ diff --git a/lib/includes/hicn/util/pool.h b/lib/includes/hicn/util/pool.h index 7488e08fd..b8acadc44 100644 --- a/lib/includes/hicn/util/pool.h +++ b/lib/includes/hicn/util/pool.h @@ -234,6 +234,26 @@ bool _pool_validate_id (void **pool_ptr, off_t id); } \ while (0) +#define pool_enumerate_typed(pool, i, TYPE, ELTP, BODY) \ + do \ + { \ + pool_hdr_t *_pool_var (ph) = pool_hdr (pool); \ + bitmap_t *_pool_var (fb) = _pool_var (ph)->free_bitmap; \ + TYPE ELTP = NULL; \ + for ((i) = 0; (i) < _pool_var (ph)->alloc_size; (i)++) \ + { \ + if (bitmap_is_set (_pool_var (fb), (i))) \ + continue; \ + ELTP = (pool) + (i); \ + do \ + { \ + BODY; \ + } \ + while (0); \ + } \ + } \ + while (0) + /** * @brief Iterate over elements in a pool. * @@ -256,6 +276,14 @@ bool _pool_validate_id (void **pool_ptr, off_t id); #define pool_get_alloc_size(pool) pool_hdr (pool)->alloc_size +#define pool_foreach_typed(pool, TYPE, ELTP, BODY) \ + do \ + { \ + unsigned _pool_var (i); \ + pool_enumerate_typed ((pool), _pool_var (i), TYPE, ELTP, BODY); \ + } \ + while (0) + #ifdef WITH_TESTS #define pool_get_free_indices(pool) pool_hdr (pool)->free_indices #define pool_get_free_bitmap(pool) pool_hdr (pool)->free_bitmap diff --git a/lib/includes/hicn/util/sstrncpy.h b/lib/includes/hicn/util/sstrncpy.h index b316201be..81427be6b 100644 --- a/lib/includes/hicn/util/sstrncpy.h +++ b/lib/includes/hicn/util/sstrncpy.h @@ -16,7 +16,10 @@ #ifndef UTIL_SSTRNCPY_H #define UTIL_SSTRNCPY_H +#ifndef __STDC_WANT_LIB_EXT1__ #define __STDC_WANT_LIB_EXT1__ 1 +#endif + #include <string.h> #ifdef __STDC_LIB_EXT1__ @@ -30,6 +33,9 @@ typedef int errno_t; #define EOK 0 +#ifndef HICN_VPP_PLUGIN +/* This function is already defined in vppinfra/string.h */ + /** * @brief This function assures a null byte at the end of the buffer. */ @@ -63,6 +69,7 @@ strnlen_s (const char *s, size_t maxlen) return strnlen (s, maxlen); } +#endif /* HICN_VPP_PLUGIN */ #endif /* __STDC_LIB_EXT1__ */ #endif /* UTIL_SSTRNCPY_H */ diff --git a/lib/includes/hicn/util/types.h b/lib/includes/hicn/util/types.h index 50c5362d3..c9cc878cf 100644 --- a/lib/includes/hicn/util/types.h +++ b/lib/includes/hicn/util/types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * 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: @@ -19,11 +19,46 @@ #include <hicn/util/windows/windows_Utils.h> #endif +/* Standard types. */ +#include <stdint.h> + typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef double f64; +typedef float f32; + +/* Architecture-dependent uword size */ +#if INTPTR_MAX == INT64_MAX +#define hicn_log2_uword_bits 6 +#elif INTPTR_MAX == INT32_MAX +#define hicn_log2_uword_bits 5 +#else +#error "Impossible to detect architecture" +#endif + +#define hicn_uword_bits (1 << hicn_log2_uword_bits) + +/* Word types. */ +#if hicn_uword_bits == 64 +/* 64 bit word machines. */ +typedef u64 hicn_uword; +#else +/* 32 bit word machines. */ +typedef u32 hicn_uword; +#endif + +typedef hicn_uword hicn_ip_csum_t; + +#define hicn_uword_bits (1 << hicn_log2_uword_bits) + /* Helper for avoiding warnings about type-punning */ #define UNION_CAST(x, destType) \ (((union { \ @@ -32,12 +67,15 @@ typedef uint64_t u64; }) x) \ .b) -// typedef unsigned int hash_t; - typedef int (*cmp_t) (const void *, const void *); /* Enums */ #define IS_VALID_ENUM_TYPE(NAME, x) ((x > NAME##_UNDEFINED) && (x < NAME##_N)) +/* Float */ + +uint32_t htonf (float f); +float ntohf (uint32_t i); + #endif /* UTIL_TYPES */ diff --git a/lib/includes/hicn/util/vector.h b/lib/includes/hicn/util/vector.h index 46f195c6d..e693df9e3 100644 --- a/lib/includes/hicn/util/vector.h +++ b/lib/includes/hicn/util/vector.h @@ -209,7 +209,7 @@ static inline int _vector_get (void *vector, off_t pos, size_t elt_size, void *elt) { vector_hdr_t *vh = vector_hdr (vector); - if (pos >= vh->alloc_size) + if ((size_t) pos >= vh->alloc_size) return -1; memcpy (elt, (uint8_t *) vector + pos * elt_size, elt_size); @@ -228,7 +228,7 @@ _vector_get (void *vector, off_t pos, size_t elt_size, void *elt) static inline bool _vector_contains (void *vector, size_t elt_size, void *elt) { - for (int i = 0; i < vector_hdr (vector)->cur_size; i++) + for (size_t i = 0; i < vector_hdr (vector)->cur_size; i++) { if (memcmp ((uint8_t *) vector + i * elt_size, elt, elt_size) == 0) return true; @@ -251,7 +251,7 @@ static inline int _vector_remove_at (void **vector_ptr, size_t elt_size, off_t pos) { vector_hdr_t *vh = vector_hdr (*vector_ptr); - if (pos >= vh->cur_size) + if ((size_t) pos >= vh->cur_size) return -1; // Shift backward by one position all the elements after the one specified @@ -342,7 +342,7 @@ _vector_remove_at (void **vector_ptr, size_t elt_size, off_t pos) #define vector_at(vector, pos) \ ({ \ - assert (pos < vector_hdr (vector)->cur_size); \ + assert ((size_t) pos < vector_hdr (vector)->cur_size); \ (vector)[(pos)]; \ }) |