diff options
Diffstat (limited to 'lib/includes')
43 files changed, 6009 insertions, 2861 deletions
diff --git a/lib/includes/CMakeLists.txt b/lib/includes/CMakeLists.txt index 12529bd57..3c79fb891 100644 --- a/lib/includes/CMakeLists.txt +++ b/lib/includes/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 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: @@ -11,8 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -set(HICN_INCLUDE_DIRS - ${CMAKE_CURRENT_SOURCE_DIR} "" +############################################################## +# Public headers directory +############################################################## +set(Libhicn_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "" FORCE ) @@ -21,36 +24,35 @@ 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/header.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/face.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 - 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/strategy.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/validation.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/interest_manifest.h PARENT_SCOPE ) 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 ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/ip_address.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/khash.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/log.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/map.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/pool.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/ring.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/set.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/slab.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/sstrncpy.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/token.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/types.h + ${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 d8a79a9c2..dcbe47877 100644 --- a/lib/includes/hicn/base.h +++ b/lib/includes/hicn/base.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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: @@ -21,25 +21,40 @@ #ifndef HICN_BASE_H #define HICN_BASE_H +#include <stdio.h> +#include <stdbool.h> #include "common.h" - +#ifdef _WIN32 +#include <Winsock2.h> +#else +#include <netinet/in.h> +#endif /* 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 -#define HICN_MAX_LIFETIME_MULTIPLIER 0x0F /* 4 bits */ -#define HICN_MAX_LIFETIME HICN_MAX_LIFETIME_SCALED << HICN_MAX_LIFETIME_MULTIPLIER +#define HICN_MAX_LIFETIME_SCALED 0xFFFF +#define HICN_MAX_LIFETIME_MULTIPLIER 0x0F /* 4 bits */ +#define HICN_MAX_LIFETIME \ + HICN_MAX_LIFETIME_SCALED << HICN_MAX_LIFETIME_MULTIPLIER /** * @brief hICN packet format type * - * The hICN type represents the sequence of protocols that we can find in packet - * headers. They are represented as a quartet of u8 values, correponding to - * IANA protocol assignment, and read from right to left. This is done to + * The hICN type represents the sequence of protocols that we can find in + * packet headers. They are represented as a quartet of u8 values, correponding + * to IANA protocol assignment, and read from right to left. This is done to * faciliate decapsulation of packet header by simple shift/mask operations. * * For instance, an IPv6/TCP packet will be identified as : @@ -49,60 +64,167 @@ typedef u32 hicn_lifetime_t; * currently used by an hypothetical signed MAP-Me update : * [IPPROTO_ICMPRD, IPPROTO_AH, IPPROTO_ICMP, IPPROTO_IPV6] */ -typedef union -{ - /** protocol layers representation */ - struct - { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - u8 l1; /**< First layer */ - u8 l2; /**< Second layer */ - u8 l3; /**< Third layer */ - u8 l4; /**< Fourth layer */ -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - u8 l4; /**< Fourth layer */ - u8 l3; /**< Third layer */ - u8 l2; /**< Second layer */ - u8 l1; /**< First layer */ -#elif _WIN32 /* Windows is assumed little-endian */ - u8 l1; - u8 l2; - u8 l3; - u8 l4; -#else -#error "Unsupported endianness" -#endif - }; - /** u32 representation */ - u32 as_u32; -} hicn_type_t; + +typedef uint32_t hicn_packet_format_t; /* Common protocol layers */ -/* Common protocol layers */ -#ifndef _WIN32 -#define HICN_TYPE(x,y,z,t) (hicn_type_t) {{ .l1 = x, .l2 = y, .l3 = z, .l4 = t }} -#else -inline hicn_type_t -HICN_TYPE(int x, int y, int z, int t) +#define HICN_PACKET_FORMAT(x, y, z, t) \ + (uint32_t) (((x) << 24) + ((y) << 16) + ((z) << 8) + (t)) + +#define HICN_PACKET_FORMAT_SIZE 4 + +// i = 0..3 +#define HICN_PACKET_FORMAT_GET(format, i) \ + (i < 0 || i > 3) ? IPPROTO_NONE : ((format >> ((3 - (i)) << 3)) & 0xFF) + +#define HICN_PACKET_FORMAT_SET(format, i, val) \ + format = ((val << ((3 - i) << 3)) | \ + (format & (0xFFFFFFFF ^ (0xFF << ((3 - i) << 3))))) + +#define HICN_PACKET_FORMAT_ENUMERATE(FORMAT, POS, PROTOCOL, BODY) \ + for (unsigned POS = 0; POS <= HICN_PACKET_FORMAT_SIZE - 1; POS++) \ + { \ + uint8_t PROTOCOL = HICN_PACKET_FORMAT_GET (FORMAT, POS); \ + BODY; \ + } + +#define HICN_PACKET_FORMAT_L1(format) HICN_PACKET_FORMAT_L ((format), 0) +#define HICN_PACKET_FORMAT_L2(format) HICN_PACKET_FORMAT_L ((format), 1) +#define HICN_PACKET_FORMAT_L3(format) HICN_PACKET_FORMAT_L ((format), 2) +#define HICN_PACKET_FORMAT_L4(format) HICN_PACKET_FORMAT_L ((format), 3) + +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 + +#define HICN_PACKET_FORMAT_IPV4_TCP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_ICMP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_TCP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_ICMP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_NEW \ + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_UDP \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_UDP \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_TCP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV4_ICMP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_TCP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_ICMP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_NEW_AH \ + HICN_PACKET_FORMAT (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_PACKET_FORMAT_IPV6_UDP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_PACKET_FORMAT_IPV4_UDP_AH \ + HICN_PACKET_FORMAT (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_PACKET_FORMAT_NONE \ + HICN_PACKET_FORMAT (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) + +/* Default packet format */ + +#define HICN_PACKET_FORMAT_DEFAULT HICN_PACKET_FORMAT_IPV6_TCP + +/** + * @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_type_t type; - type.l1 = x; - type.l2 = y; - type.l3 = z; - type.l4 = t; - return type; + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + switch (protocol) + { + { + case IPPROTO_AH: + return format; + case IPPROTO_NONE: + HICN_PACKET_FORMAT_SET (format, i, IPPROTO_AH); + return format; + default: + break; + } + } + }); + 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_IPV4_TCP_AH HICN_TYPE(IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV4_ICMP_AH HICN_TYPE(IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, 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_NONE HICN_TYPE(IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +/* + * 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 + +#define HICN_PACKET_FORMAT_IS_NONE(format) \ + ((HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 1) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 2) == IPPROTO_NONE) && \ + (HICN_PACKET_FORMAT_GET (format, 3) == IPPROTO_NONE)) + +#define HICN_PACKET_FORMAT_IS_AH(format) \ + ((HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 1) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 2) == IPPROTO_AH) || \ + (HICN_PACKET_FORMAT_GET (format, 3) == IPPROTO_AH)) + +#define HICN_PACKET_FORMAT_IS_IPV4(format) \ + (HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_IP) +#define HICN_PACKET_FORMAT_IS_IPV6(format) \ + (HICN_PACKET_FORMAT_GET (format, 0) == IPPROTO_IPV6) + +/* + * @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 +{ +#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 @@ -117,39 +239,120 @@ typedef enum HPT_UNSPEC = 999 } hicn_payload_type_t; +/* Path label */ + +typedef u8 hicn_path_label_t; + +#define INVALID_PATH_LABEL 0 + /** * @brief Path label computations * - * Path label is computed by accumulating the identifiers of successive output - * faces as a Data packet is traveling from its producer back to the consumer - * originating the Interest. + * Path label is computed by accumulating the identifiers of successive + * output faces as a Data packet is traveling from its producer back to the + * consumer originating the Interest. * * NOTE: this computation is not (yet) part of the hICN specification. */ -#define HICN_PATH_LABEL_MASK 0xF000 /* 1000 0000 0000 0000 */ -#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 + * This function updates the current_label based on the new face_id, and + * returns */ -always_inline void -update_pathlabel (hicn_pathlabel_t current_label, hicn_faceid_t face_id, - hicn_pathlabel_t * new_label) +static inline void +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) >> - (16 - HICN_PATH_LABEL_SIZE)); - *new_label = - ((current_label << 1) | (current_label >> (HICN_PATH_LABEL_SIZE - 1))) ^ - pl_face_id; + 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_BITS - 1))) ^ + pl_face_id; } +/*************************************************************** + * Statistics + ***************************************************************/ + +typedef struct +{ + // Packets processed + uint32_t countReceived; // Interest and data only + uint32_t countInterestsReceived; + uint32_t countObjectsReceived; + + // Packets Dropped + uint32_t countDropped; + uint32_t countInterestsDropped; + uint32_t countObjectsDropped; + uint32_t countOtherDropped; + + // Forwarding + uint32_t countInterestForwarded; + uint32_t countObjectsForwarded; + + // Errors while forwarding + uint32_t countDroppedConnectionNotFound; + uint32_t countSendFailures; + uint32_t countDroppedNoRoute; + + // Interest processing + uint32_t countInterestsAggregated; + uint32_t countInterestsRetransmitted; + uint32_t countInterestsSatisfiedFromStore; + uint32_t countInterestsExpired; + + // Data processing + uint32_t countDroppedNoReversePath; + uint32_t countDataExpired; + + // TODO(eloparco): Currently not used + // uint32_t countDroppedNoHopLimit; + // uint32_t countDroppedZeroHopLimitFromRemote; + // uint32_t countDroppedZeroHopLimitToRemote; +} forwarder_stats_t; + +typedef struct +{ + uint32_t n_pit_entries; + uint32_t n_cs_entries; + uint32_t n_lru_evictions; +} pkt_cache_stats_t; + +typedef struct +{ + forwarder_stats_t forwarder; + pkt_cache_stats_t pkt_cache; +} hicn_light_stats_t; + +typedef struct +{ + uint32_t conn_id; + struct + { + uint32_t rx_pkts; + uint32_t rx_bytes; + uint32_t tx_pkts; + uint32_t tx_bytes; + } interests; + struct + { + uint32_t rx_pkts; + uint32_t rx_bytes; + uint32_t tx_pkts; + uint32_t tx_bytes; + } data; +} connection_stats_t; + #endif /* HICN_BASE_H */ /* diff --git a/lib/includes/hicn/common.h b/lib/includes/hicn/common.h index 30f370241..64aca8f1f 100644 --- a/lib/includes/hicn/common.h +++ b/lib/includes/hicn/common.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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,59 +36,23 @@ #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; +#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)) -/* - * 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 always_inline static inline -#define static_always_inline static inline -#define STRUCT_SIZE_OF(type, member) sizeof(((type *)0)->member) -#define ASSERT - -#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; +#ifndef NDEBUG +#define _ASSERT(x) assert (x) #else -/* 32 bit word machines. */ -typedef u32 uword; +#define _ASSERT(x) ((void) (x)) #endif -typedef uword ip_csum_t; - -#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 macro, or - * duplicate some code, with the intent of preserving those safeguards for - * non-Windows platforms. + * Windows compilers do not support named initilizers when .h files are + * included inside C++ files. For readability, we either use the following + * macro, or duplicate some code, with the intent of preserving those + * safeguards for non-Windows platforms. */ #ifndef _WIN32 #define ATTR_INIT(key, value) .key = value @@ -97,13 +61,16 @@ typedef uword ip_csum_t; #endif #ifdef _WIN32 - /* Endianness detection for Windows platforms */ +/* Endianness detection for Windows platforms */ #define __ORDER_LITTLE_ENDIAN__ 0x41424344UL -#define __ORDER_BIG_ENDIAN__ 0x44434241UL -#define __BYTE_ORDER__ ('ABCD') +#define __ORDER_BIG_ENDIAN__ 0x44434241UL +#define __BYTE_ORDER__ ('ABCD') - /* Windows compatibility headers */ +/* Windows compatibility headers */ #define WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX +#define NOMINMAX +#endif #include <windows.h> #include <winsock2.h> #include <ws2ipdef.h> @@ -113,10 +80,10 @@ typedef uword ip_csum_t; #define strdup _strdup #define __attribute__(A) -#ifndef IOVEC +#ifndef IOVEC #define IOVEC #define UIO_MAXIOV 16 -#define IOV_MAX UIO_MAXIOV +#define IOV_MAX UIO_MAXIOV struct iovec { void *iov_base; @@ -129,57 +96,20 @@ struct iovec * Portable attribute packed. */ #ifndef _WIN32 -#define PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__)) +#define PACKED(__Declaration__) __Declaration__ __attribute__ ((__packed__)) #else -#define PACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) ) +#define PACKED(__Declaration__) \ + __pragma (pack (push, 1)) __Declaration__ __pragma (pack (pop)) #endif - /* * IP address types */ -#ifdef HICN_VPP_PLUGIN - -#include <vnet/ip/ip4_packet.h> // ip4_address_t -#include <vnet/ip/ip6_packet.h> // ip6_address_t -#include <vnet/ip/ip46_address.h> - -#else - - #ifndef _WIN32 #include <netinet/in.h> #endif -typedef union -{ - u32 as_u32; - struct in_addr as_inaddr; -} ip4_address_t; - -typedef union -{ - u64 as_u64[2]; - u32 as_u32[4]; - u8 as_u8[16]; - struct in6_addr as_in6addr; -} ip6_address_t; - -typedef union -{ - struct - { - u32 pad[3]; - ip4_address_t ip4; - }; - ip6_address_t ip6; -} ip46_address_t; - -#define ip46_address_is_ip4(ip46) (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0) - -#endif /* ! HICN_VPP_PLUGIN */ - /** * @brief Returns the family of an IP address * @param [in] ip_address - IP address in presentation format @@ -203,33 +133,33 @@ int get_addr_family (const char *ip_address); * borrow this code here. */ -static_always_inline u16 -ip_csum_fold (ip_csum_t c) +static inline u16 +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 c = (c & 0xffff) + (c >> 16); c = (c & 0xffff) + (c >> 16); - return (u16)c; + return (u16) c; } -static_always_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_always_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; @@ -240,19 +170,17 @@ ip_csum_add_even (ip_csum_t c, ip_csum_t x) } /* Update checksum changing field at even byte offset from 0 -> x. */ -static_always_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); } +#endif /* ! HICN_VPP_PLUGIN */ + u32 cumulative_hash32 (const void *data, size_t len, u32 lastValue); u32 hash32 (const void *data, size_t len); -u64 cumulative_hash64 (const void *data, size_t len, u64 lastValue); -u64 hash64 (const void *data, size_t len); -void hicn_packet_dump (uint8_t * buffer, size_t len); - -#endif /* ! HICN_VPP_PLUGIN */ +void hicn_packet_dump (const uint8_t *buffer, size_t len); /** * @brief Computes buffer checksum @@ -261,7 +189,7 @@ void hicn_packet_dump (uint8_t * buffer, size_t len); * @param [in] init - Checksum initial value * @return Checksum of specified buffer */ -always_inline u16 +static inline u16 csum (const void *addr, size_t size, u16 init) { u32 sum = init; @@ -279,7 +207,7 @@ csum (const void *addr, size_t size, u16 init) sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); - return (u16) ~ sum; + return (u16) ~sum; } /* @@ -293,19 +221,110 @@ 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)->v4.ip.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 /* - * ntohll / htonll allows byte swapping for 64 bits integers + * Endianess utils */ -#ifndef htonll -#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) + +#if (__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__) +#define HICN_LITTLE_ENDIAN_ARCH +#else +#define HICN_BIG_ENDIAN_ARCH +#endif + +static inline u16 +hicn_conditional_swap_u16 (u16 value) +{ +#ifdef HICN_LITTLE_ENDIAN_ARCH + value = __builtin_bswap16 (value); +#endif + + return value; +} + +static inline u32 +hicn_conditional_swap_u32 (u32 value) +{ +#ifdef HICN_LITTLE_ENDIAN_ARCH + value = __builtin_bswap32 (value); #endif -#ifndef ntohll -#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) + return value; +} + +static inline u64 +hicn_conditional_swap_u64 (u64 value) +{ +#ifdef HICN_LITTLE_ENDIAN_ARCH + value = __builtin_bswap64 (value); #endif + return value; +} + +#define hicn_net_to_host_16(x) hicn_conditional_swap_u16 ((u16) (x)) +#define hicn_net_to_host_32(x) hicn_conditional_swap_u32 ((u32) (x)) +#define hicn_net_to_host_64(x) hicn_conditional_swap_u64 ((u64) (x)) + +#define hicn_host_to_net_16(x) hicn_conditional_swap_u16 ((u16) (x)) +#define hicn_host_to_net_32(x) hicn_conditional_swap_u32 ((u32) (x)) +#define hicn_host_to_net_64(x) hicn_conditional_swap_u64 ((u64) (x)) + +#define hicn_round_pow2(x, pow2) (((x) + (pow2) -1) & ~((pow2) -1)) + +#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 */ +#if defined(_MSC_VER) && !defined(__clang__) +#include <intrin.h> + +uint32_t __inline __builtin_ctz (uint32_t value) +{ + uint32_t trailing_zero = 0; + if (_BitScanForward (&trailing_zero, value)) + return trailing_zero; + else + return 32; +} + +uint32_t __inline __builtin_clz (uint32_t value) +{ + uint32_t leading_zero = 0; + if (_BitScanReverse (&leading_zero, value)) + return 31 - leading_zero; + else + return 32; +} + +uint32_t __inline __builtin_clzl2 (uint64_t value) +{ + uint32_t leading_zero = 0; + if (_BitScanReverse64 (&leading_zero, value)) + return 63 - leading_zero; + else + return 64; +} + +#define __builtin_clzl __builtin_clzll +#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 2796983c6..000000000 --- a/lib/includes/hicn/compat.h +++ /dev/null @@ -1,461 +0,0 @@ -/* - * Copyright (c) 2017-2019 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_ICMP 1 << 3 -#define HFO_AH 1 << 4 - -#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_icmp(format) ((format & HFO_ICMP) >> 3) -#define _is_ah(format) ((format & HFO_AH) >> 4) - -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_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 -} hicn_format_t; - -/** - * 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 - -/** - * @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 (hicn_format_t format, - hicn_header_t * packet); - -// 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); - -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 (const hicn_header_t * packet, u32 * lifetime); -int hicn_packet_set_lifetime (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 (const hicn_header_t * packet, - hicn_payload_type_t * payload_type); -int hicn_packet_set_payload_type (hicn_header_t * packet, - const hicn_payload_type_t payload_type); - -int hicn_packet_set_syn (hicn_header_t * packet); -int hicn_packet_reset_syn (hicn_header_t * packet); -int hicn_packet_test_syn (const hicn_header_t * packet, bool * flag); -int hicn_packet_set_ack (hicn_header_t * packet); -int hicn_packet_reset_ack (hicn_header_t * packet); -int hicn_packet_test_ack (const hicn_header_t * packet, bool * flag); -int hicn_packet_set_rst (hicn_header_t * packet); -int hicn_packet_reset_rst (hicn_header_t * packet); -int hicn_packet_test_rst (const hicn_header_t * packet, bool * flag); -int hicn_packet_set_fin (hicn_header_t * packet); -int hicn_packet_reset_fin (hicn_header_t * packet); -int hicn_packet_test_fin (const hicn_header_t * packet, bool * flag); -int hicn_packet_set_ece (hicn_header_t * packet); -int hicn_packet_reset_ece (hicn_header_t * packet); -int hicn_packet_test_ece (const hicn_header_t * packet, bool * flag); - -int hicn_packet_set_src_port (hicn_header_t * packet, u16 src_port); -int hicn_packet_get_src_port (const hicn_header_t * packet, u16 * src_port); -int hicn_packet_set_dst_port (hicn_header_t * packet, u16 dst_port); -int hicn_packet_get_dst_port (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); - -#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 3e027c4e5..9926c9cd8 100644 --- a/lib/includes/hicn/error.h +++ b/lib/includes/hicn/error.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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,20 +24,22 @@ * Error definitions ******************************************************************************/ -#define foreach_libhicn_error \ -_(NONE, 0, "OK") \ -_(UNSPECIFIED, 128, "Unspecified Error") \ -_(NOT_IMPLEMENTED, 180, "Function not yet implemented") \ -_(NOT_HICN, 202, "Non hICN packet") \ -_(UNKNOWN_ADDRESS, 210, "Unknown address") \ -_(INVALID_PARAMETER, 220, "Invalid parameter") \ -_(INVALID_IP_ADDRESS, 221, "Invalid IP address") \ -_(CORRUPTED_PACKET, 222, "Corrupted packet ") \ -_(UNEXPECTED, 298, "Unexpected error") +#define foreach_libhicn_error \ + _ (NONE, 0, "OK") \ + _ (UNSPECIFIED, 128, "Unspecified Error") \ + _ (NOT_IMPLEMENTED, 180, "Function not yet implemented") \ + _ (NOT_HICN, 202, "Non hICN packet") \ + _ (UNKNOWN_ADDRESS, 210, "Unknown address") \ + _ (INVALID_PARAMETER, 220, "Invalid parameter") \ + _ (INVALID_IP_ADDRESS, 221, "Invalid IP address") \ + _ (CORRUPTED_PACKET, 222, "Corrupted packet ") \ + _ (REWRITE_CKSUM_REQUIRED, 223, \ + "Incremental csum calculation error: cksum required.") \ + _ (UNEXPECTED, 298, "Unexpected error") typedef enum { -#define _(a,b,c) HICN_LIB_ERROR_##a = (-b), +#define _(a, b, c) HICN_LIB_ERROR_##a = (-b), foreach_libhicn_error #undef _ HICN_LIB_N_ERROR, @@ -45,7 +47,9 @@ typedef enum extern const char *HICN_LIB_ERROR_STRING[]; -#define hicn_strerror(errno) (char *)(HICN_LIB_ERROR_STRING[-errno]) +#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 new file mode 100644 index 000000000..fbdacec5f --- /dev/null +++ b/lib/includes/hicn/face.h @@ -0,0 +1,245 @@ +/* + * 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 face.h + * \brief Face abstraction + */ +#ifndef HICN_FACE_H +#define HICN_FACE_H + +#ifndef SPACES +#define SPACES(x) x +#endif +#ifndef SPACE +#define SPACE 1 +#endif +#ifndef NULLTERM +#define NULLTERM 1 +#endif + +#include <hicn/policy.h> + +#include <hicn/util/ip_address.h> + +/* Netdevice type */ + +#include <net/if.h> // IFNAMSIZ +#include "base.h" + +#define foreach_netdevice_type \ + _ (UNDEFINED) \ + _ (LOOPBACK) \ + _ (WIRED) \ + _ (WIFI) \ + _ (CELLULAR) \ + _ (VPN) \ + _ (MEMIF) \ + _ (N) + +#define MAXSZ_NETDEVICE_TYPE_ 9 +#define MAXSZ_NETDEVICE_TYPE MAXSZ_NETDEVICE_TYPE_ + NULLTERM + +typedef enum +{ +#define _(x) NETDEVICE_TYPE_##x, + foreach_netdevice_type +#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 INVALID_NETDEVICE_ID ~0 + +/* Netdevice */ + +/** + * \brief Netdevice type + * + * NOTE + * - This struct cannot be made opaque as it is currently part of face_t + * - We recommand using the API as to keep redundant attributes consistent + */ + +typedef struct +{ + u32 index; + char name[IFNAMSIZ]; +} netdevice_t; + +#define NETDEVICE_EMPTY \ + (netdevice_t) \ + { \ + .index = 0, .name = { 0 } \ + } + +netdevice_t *netdevice_create_from_index (u32 index); +netdevice_t *netdevice_create_from_name (const char *name); +#define netdevice_initialize_from_index netdevice_set_index +#define netdevice_initialize_from_name netdevice_set_name +void netdevice_free (netdevice_t *netdevice); +int netdevice_get_index (const netdevice_t *netdevice, u32 *index); +int netdevice_set_index (netdevice_t *netdevice, u32 index); +int netdevice_get_name (const netdevice_t *netdevice, const char **name); +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 + +/* Face state */ + +#define foreach_face_state \ + _ (UNDEFINED) \ + _ (DOWN) \ + _ (UP) \ + _ (N) + +#define MAXSZ_FACE_STATE_ 9 +#define MAXSZ_FACE_STATE MAXSZ_FACE_STATE_ + 1 + +typedef enum +{ +#define _(x) FACE_STATE_##x, + foreach_face_state +#undef _ +} face_state_t; + +extern const char *_face_state_str[]; + +#define face_state_str(x) _face_state_str[x] + +/* Face type */ + +#define foreach_face_type \ + _ (UNDEFINED) \ + _ (HICN) \ + _ (HICN_LISTENER) \ + _ (TCP) \ + _ (TCP_LISTENER) \ + _ (UDP) \ + _ (UDP_LISTENER) \ + _ (N) + +#define MAXSZ_FACE_TYPE_ 13 +#define MAXSZ_FACE_TYPE MAXSZ_FACE_TYPE_ + 1 + +typedef enum +{ +#define _(x) FACE_TYPE_##x, + foreach_face_type +#undef _ +} face_type_t; + +typedef enum +{ + FACE_PROTOCOL_HICN, + FACE_PROTOCOL_UDP, + FACE_PROTOCOL_TCP, + FACE_PROTOCOL_UNKNOWN, +} face_protocol_t; + +#define face_type_is_valid(face_type) \ + (((face_type) >= FACE_TYPE_UNDEFINED) && (face_type < FACE_TYPE_N)) +#define face_type_is_defined(face_type) \ + (((face_type) > FACE_TYPE_UNDEFINED) && (face_type < FACE_TYPE_N)) + +extern const char *_face_type_str[]; +#define face_type_str(x) _face_type_str[x] + +face_type_t face_type_from_str (const char *str); + +#ifdef WITH_POLICY +#define MAXSZ_FACE_ \ + MAXSZ_FACE_TYPE_ + 2 * MAXSZ_URL_ + 2 * MAXSZ_FACE_STATE_ + \ + MAXSZ_POLICY_TAGS_ + 7 +#else +#define MAXSZ_FACE_ \ + MAXSZ_FACE_TYPE_ + 2 * MAXSZ_URL_ + 2 * MAXSZ_FACE_STATE_ + 4 +#endif /* WITH_POLICY */ +#define MAXSZ_FACE MAXSZ_FACE_ + 1 + +/* 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; +#ifdef WITH_POLICY + uint32_t priority; + policy_tags_t tags; /**< \see policy_tag_t */ +#endif /* WITH_POLICY */ + + /* + * Depending on the face type, some of the following fields will be unused + */ + netdevice_t netdevice; + int family; /* To access family independently of face type */ + 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 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, + const struct sockaddr *remote_addr); + +face_t *face_create (); +face_t *face_create_udp (const char *interface_name, + 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, + const struct sockaddr *remote_addr); + +int face_finalize (face_t *face); + +void face_free (face_t *face); + +typedef int (*face_cmp_t) (const face_t *f1, const face_t *f2); + +int face_cmp (const face_t *f1, const face_t *f2); + +size_t face_snprintf (char *s, size_t size, const face_t *face); + +policy_tags_t face_get_tags (const face_t *face); +int face_set_tags (face_t *face, policy_tags_t tags); + +face_protocol_t get_protocol (face_type_t face_type); + +#endif /* HICN_FACE_H */ diff --git a/lib/includes/hicn/header.h b/lib/includes/hicn/header.h deleted file mode 100644 index b21fe5c84..000000000 --- a/lib/includes/hicn/header.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2017-2019 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; - _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 - { - _icmp_header_t icmp; - _ah_header_t icmp_ah; - }; - }; -} hicn_v6ah_hdr_t; - -typedef struct -{ - _ipv4_header_t ip; - union - { - _tcp_header_t tcp; - _icmp_header_t icmp; - _icmp_wldr_header_t wldr; - }; -} hicn_v4_hdr_t; - -typedef struct -{ - _ipv4_header_t ip; - union - { - struct - { - _tcp_header_t tcp; - _ah_header_t 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) - - - - -#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 749fd4247..5e67d83a0 100644 --- a/lib/includes/hicn/hicn.h +++ b/lib/includes/hicn/hicn.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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,17 +29,27 @@ * 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) @ https://tools.ietf.org/html/draft-muscariello-intarea-hicn + * IETF draft (intarea) @ + * https://tools.ietf.org/html/draft-muscariello-intarea-hicn * - * [2] Leveraging ICN in-network control for loss detection and recovery in wireless mobile networks - * G. Carofiglio, L. Muscariello, M. Papalini, N. Rozhnova, X. Zeng - * In proc. ICN'2016, Kyoto, JP + * [2] Leveraging ICN in-network control for loss detection and recovery in + * wireless mobile networks G. Carofiglio, L. Muscariello, M. Papalini, N. + * Rozhnova, X. Zeng In proc. ICN'2016, Kyoto, JP * * [3] Anchorless mobility through hICN * J. Augé, G. Carofiglio, L. Muscariello, M. Papalini - * IETF draft (DMM) @ https://tools.ietf.org/html/draft-auge-dmm-hicn-mobility + * IETF draft (DMM) @ + * https://tools.ietf.org/html/draft-auge-dmm-hicn-mobility * * * [4] MAP-Me : Managing Anchorless Mobility in Content Centric Networking @@ -50,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..b6122ce1c --- /dev/null +++ b/lib/includes/hicn/interest_manifest.h @@ -0,0 +1,220 @@ +/* + * 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> +#include <hicn/name.h> +#include <hicn/common.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; + + /* First suffix */ + 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 exactly 40 bytes"); + +#define _interest_manifest_serialize_deserialize(manifest, first, second, \ + size, serialize) \ + do \ + { \ + u32 n_suffixes = 0; \ + if (serialize) \ + n_suffixes = int_manifest_header->n_suffixes; \ + \ + int_manifest_header->n_suffixes = \ + hicn_##first##_to_##second##_32 (int_manifest_header->n_suffixes); \ + int_manifest_header->padding = \ + hicn_##first##_to_##second##_32 (int_manifest_header->padding); \ + \ + for (unsigned i = 0; i < BITMAP_SIZE; i++) \ + { \ + int_manifest_header->request_bitmap[i] = \ + hicn_##first##_to_##second##_##size ( \ + int_manifest_header->request_bitmap[i]); \ + } \ + \ + hicn_name_suffix_t *suffix = \ + (hicn_name_suffix_t *) (int_manifest_header + 1); \ + if (!serialize) \ + n_suffixes = int_manifest_header->n_suffixes; \ + for (unsigned i = 0; i < n_suffixes; i++) \ + { \ + *(suffix + i) = hicn_##first##_to_##second##_32 (*(suffix + i)); \ + } \ + } \ + while (0) + +#define _interest_manifest_serialize(manifest, size) \ + _interest_manifest_serialize_deserialize (manifest, host, net, size, 1) + +#define _interest_manifest_deserialize(manifest, size) \ + _interest_manifest_serialize_deserialize (manifest, net, host, size, 0) + +static inline void +interest_manifest_serialize (interest_manifest_header_t *int_manifest_header) +{ + _interest_manifest_serialize (int_manifest_header, hicn_uword_bits); +} + +static inline void +interest_manifest_deserialize (interest_manifest_header_t *int_manifest_header) +{ + u32 n_suffixes = 0; + + int_manifest_header->n_suffixes = + hicn_net_to_host_32 (int_manifest_header->n_suffixes); + int_manifest_header->padding = + hicn_net_to_host_32 (int_manifest_header->padding); + + for (unsigned i = 0; i < BITMAP_SIZE; i++) + { + int_manifest_header->request_bitmap[i] = + hicn_net_to_host_64 (int_manifest_header->request_bitmap[i]); + } + + hicn_name_suffix_t *suffix = + (hicn_name_suffix_t *) (int_manifest_header + 1); + + n_suffixes = int_manifest_header->n_suffixes; + for (unsigned i = 0; i < n_suffixes; i++) + { + *(suffix + i) = hicn_net_to_host_32 (*(suffix + i)); + } +} + +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 void +interest_manifest_add_suffix (interest_manifest_header_t *int_manifest_header, + hicn_name_suffix_t suffix) +{ + hicn_name_suffix_t *start = (hicn_name_suffix_t *) (int_manifest_header + 1); + *(start + int_manifest_header->n_suffixes) = suffix; + hicn_uword *request_bitmap = int_manifest_header->request_bitmap; + bitmap_set_no_check (request_bitmap, int_manifest_header->n_suffixes); + int_manifest_header->n_suffixes++; +} + +static inline void +interest_manifest_init (interest_manifest_header_t *int_manifest_header, + u32 fist_suffix) +{ + int_manifest_header->n_suffixes = 0; + int_manifest_header->padding = 0; + memset (int_manifest_header->request_bitmap, 0, + sizeof (int_manifest_header->request_bitmap)); + + interest_manifest_add_suffix (int_manifest_header, fist_suffix); +} + +static inline void +interest_manifest_del_suffix (interest_manifest_header_t *int_manifest_header, + hicn_uword pos) +{ + bitmap_unset_no_check (int_manifest_header->request_bitmap, pos); +} + +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; +} + +#define _FIRST(h) (hicn_name_suffix_t *) (h + 1) + +#define interest_manifest_foreach_suffix(header, suffix, pos) \ + for (suffix = _FIRST (header) + bitmap_first_set_no_check ( \ + header->request_bitmap, BITMAP_SIZE), \ + pos = 0; \ + suffix - _FIRST (header) < header->n_suffixes; \ + pos = suffix - _FIRST (header) + 1, \ + suffix = _FIRST (header) + \ + bitmap_next_set_no_check (header->request_bitmap, \ + suffix - _FIRST (header) + 1, \ + BITMAP_SIZE), \ + pos = suffix - _FIRST (header)) + +#endif /* HICNLIGHT_INTEREST_MANIFEST_H */ diff --git a/lib/includes/hicn/mapme.h b/lib/includes/hicn/mapme.h index 8fae44530..b452a5dde 100644 --- a/lib/includes/hicn/mapme.h +++ b/lib/includes/hicn/mapme.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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: @@ -20,28 +20,28 @@ #ifndef HICN_MAPME_H #define HICN_MAPME_H -#include <stdint.h> // u32 +#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 */ typedef struct { - /** MAP-Me enabled flag (default: false) */ + /** MAP-Me enabled flag (default: false) */ bool enabled; - /** timescale (T_U parameter) in ms (default: 0 for no notifications) */ + /** timescale (T_U parameter) in ms (default: 0 for no notifications) */ u32 timescale; - /** retransmission timer in ms (default: 50) */ + /** retransmission timer in ms (default: 50) */ u32 retx; - /** - * Discovery enabled flag (default: true, should be true if mandatory is - * notifications are enabled) - */ + /** + * Discovery enabled flag (default: true, should be true if mandatory is + * notifications are enabled) + */ bool discovery; } hicn_mapme_conf_t; @@ -74,83 +74,51 @@ typedef struct seq_t seq; } mapme_params_t; - /* MAP-Me API */ -size_t hicn_mapme_create_packet (u8 * buf, const hicn_prefix_t * prefix, - const mapme_params_t * params); -size_t hicn_mapme_create_ack (u8 * buf, const mapme_params_t * params); -int hicn_mapme_parse_packet (const u8 * packet, hicn_prefix_t * prefix, - mapme_params_t * params); +size_t hicn_mapme_create_packet (u8 *buf, const hicn_prefix_t *prefix, + const mapme_params_t *params); +size_t hicn_mapme_create_ack (u8 *buf, const mapme_params_t *params); +int hicn_mapme_parse_packet (const u8 *packet, hicn_prefix_t *prefix, + mapme_params_t *params); /* Implementation & parsing : ICMP Redirect */ #define HICN_MAPME_ACK_FLAG (0x20 | 0x60) -#define HICN_MAPME_ICMP_TYPE_IPV4 5 -#define HICN_MAPME_ICMP_TYPE_IPV6 137 -#define HICN_MAPME_ICMP_TYPE_ACK_IPV4 (HICN_MAPME_ICMP_TYPE_IPV4 | HICN_MAPME_ACK_FLAG) -#define HICN_MAPME_ICMP_TYPE_ACK_IPV6 (HICN_MAPME_ICMP_TYPE_IPV6 | HICN_MAPME_ACK_FLAG) -#define HICN_MAPME_ICMP_CODE 0 /* Redirect Datagrams for the Network (or subnet) */ - -#define HICN_MAPME_TYPE_IS_IU(type) ((type == HICN_MAPME_ICMP_TYPE_IPV4) || (type == HICN_MAPME_ICMP_TYPE_IPV6)) -#define HICN_MAPME_TYPE_IS_IU_ACK(type) ((type == HICN_MAPME_ICMP_TYPE_ACK_IPV4) || (type == HICN_MAPME_ICMP_TYPE_ACK_IPV6)) - -#define HICN_MAPME_IS_IU(type, code) (HICN_MAPME_TYPE_IS_IU(type) && (code == HICN_MAPME_ICMP_CODE)) -#define HICN_MAPME_IS_ACK(type, code) (HICN_MAPME_TYPE_IS_IU_ACK(type) && (code == HICN_MAPME_ICMP_CODE)) - -#define HICN_IS_MAPME(type, code) (HICN_MAPME_IS_IU(type, code) || HICN_MAPME_IS_ACK(type, code)) +#define HICN_MAPME_ICMP_TYPE_IPV4 5 +#define HICN_MAPME_ICMP_TYPE_IPV6 137 +#define HICN_MAPME_ICMP_TYPE_ACK_IPV4 \ + (HICN_MAPME_ICMP_TYPE_IPV4 | HICN_MAPME_ACK_FLAG) +#define HICN_MAPME_ICMP_TYPE_ACK_IPV6 \ + (HICN_MAPME_ICMP_TYPE_IPV6 | HICN_MAPME_ACK_FLAG) +#define HICN_MAPME_ICMP_CODE \ + 0 /* Redirect Datagrams for the Network (or subnet) */ + +#define HICN_MAPME_TYPE_IS_IU(type) \ + ((type == HICN_MAPME_ICMP_TYPE_IPV4) || (type == HICN_MAPME_ICMP_TYPE_IPV6)) +#define HICN_MAPME_TYPE_IS_IU_ACK(type) \ + ((type == HICN_MAPME_ICMP_TYPE_ACK_IPV4) || \ + (type == HICN_MAPME_ICMP_TYPE_ACK_IPV6)) + +#define HICN_MAPME_IS_IU(type, code) \ + (HICN_MAPME_TYPE_IS_IU (type) && (code == HICN_MAPME_ICMP_CODE)) +#define HICN_MAPME_IS_ACK(type, code) \ + (HICN_MAPME_TYPE_IS_IU_ACK (type) && (code == HICN_MAPME_ICMP_CODE)) + +#define HICN_IS_MAPME(type, code) \ + (HICN_MAPME_IS_IU (type, code) || HICN_MAPME_IS_ACK (type, code)) /* Fast check for ACK flag */ #define HICN_MAPME_IS_ACK_FAST(icmp_type) (icmp_type & HICN_MAPME_ACK_FLAG) /* 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 +#define HICN_MAPME_TTL 255 // typical for redirect (ref?) -/** @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 d5202068b..a4e8e02ad 100644 --- a/lib/includes/hicn/name.h +++ b/lib/includes/hicn/name.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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,119 +24,109 @@ #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 +#include <netinet/in.h> // struct sockadd #endif +#include <hicn/common.h> #include <hicn/util/ip_address.h> -#include "common.h" /****************************************************************************** * hICN names ******************************************************************************/ -#define TCP_SEQNO_LEN 4 /* bytes */ +#define TCP_SEQNO_LEN 4 /* bytes */ #define HICN_V4_PREFIX_LEN IPV4_ADDR_LEN #define HICN_V6_PREFIX_LEN IPV6_ADDR_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 */ + +#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 { - ip46_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 union -{ - struct - { - union - { - u32 prefix; - u8 prefix_as_u8[4]; - ip4_address_t prefix_as_ip4; - }; - hicn_name_suffix_t suffix; - }; - u8 buffer[HICN_V4_NAME_LEN]; -} hicn_v4_name_t; - -typedef union -{ - struct - { - union - { - u64 prefix[2]; - u8 prefix_as_u8[16]; - ip6_address_t prefix_as_ip6; - }; - hicn_name_suffix_t suffix; - }; - u8 buffer[HICN_V6_NAME_LEN]; -} hicn_v6_name_t; - -#ifndef HICN_VPP_PLUGIN -#define HICN_NAME_COMPONENT_SIZE 2 +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 { - struct iovec buffers[HICN_NAME_COMPONENT_SIZE]; -} hicn_iov_name_t; + hicn_name_prefix_t prefix; + hicn_name_suffix_t suffix; +} hicn_name_t; -#define UNSPEC 1 << 0 -#define HNT_CONTIGUOUS 1 << 1 -#define HNT_IOV 1 << 2 -#define HNT_INET 1 << 3 -#define HNT_INET6 1 << 4 +static_assert (offsetof (hicn_name_t, prefix) == 0, ""); +static_assert (offsetof (hicn_name_t, suffix) == 16, ""); +static_assert (sizeof (hicn_name_t) == 20, ""); -typedef enum +#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) { - HNT_UNSPEC = UNSPEC, - HNT_CONTIGUOUS_V4 = HNT_CONTIGUOUS | HNT_INET, - HNT_CONTIGUOUS_V6 = HNT_CONTIGUOUS | HNT_INET6, - HNT_IOV_V4 = HNT_IOV | HNT_INET, - HNT_IOV_V6 = HNT_IOV | HNT_INET6, -} hicn_name_type_t; -#endif /* HICN_VPP_PLUGIN */ + return &name->prefix; +} -typedef struct +static inline const hicn_name_suffix_t +hicn_name_get_suffix (const hicn_name_t *name) { -#ifndef HICN_VPP_PLUGIN - hicn_name_type_t type; - u8 len; -#endif /* HICN_VPP_PLUGIN */ - union - { - hicn_v4_name_t ip4; - hicn_v6_name_t ip6; - ip46_address_t ip46; -#ifndef HICN_VPP_PLUGIN - hicn_iov_name_t iov; - u8 buffer[HICN_V6_NAME_LEN]; -#endif /* HICN_VPP_PLUGIN */ - }; -} hicn_name_t; + return name->suffix; +} -#ifndef HICN_VPP_PLUGIN -#define _is_unspec(name) ((name->type & UNSPEC)) -#define _is_contiguous(name) ((name->type & HNT_CONTIGUOUS) >> 1) -#define _is_iov(name) ((name->type & HNT_IOV) >> 2) -#define _is_inet4(name) ((name->type & HNT_INET) >> 3) -#define _is_inet6(name) ((name->type & HNT_INET6) >> 4) -#endif /* HICN_VPP_PLUGIN */ +#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) (hicn_ip_address_is_v4 (&name->prefix)) +#define _is_inet6(name) (!_is_inet4 (name)) /** * @brief Create an hICN name from IP address in presentation format @@ -145,37 +135,40 @@ typedef struct * @param [out] Resulting hICN name * @return hICN error code */ -int hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name); +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, - hicn_name_t * name); +int hicn_name_create_from_ip_address (const hicn_ip_address_t ip_address, + u32 suffix, hicn_name_t *name); /** - * @brief Returns the length of an hICN name - * @param [in] name - hICN name - * @return Name length + * @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 */ -u8 hicn_name_get_length (const hicn_name_t * name); +int hicn_name_create_from_ip_prefix (const hicn_ip_prefix_t *prefix, u32 id, + hicn_name_t *name); /** * @brief Compare two hICN names * @param [in] name_1 - First name to compare * @param [in] name_2 - Second name to compare - * @param [in] consider_segment - Flag indicating whether the segment part has to be - * considered + * @param [in] consider_segment - Flag indicating whether the segment part has + * to be considered * @return An integer less than, equal to, or greater than zero if name_1 is * found, respectively, to be lest than, to match, or be greater than name_2 * based on numeric order. */ -int hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2, - bool consider_segment); +int hicn_name_compare (const hicn_name_t *name_1, const hicn_name_t *name_2, + bool consider_segment); /** * @brief Provides a 32-bit hash of an hICN name @@ -184,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 @@ -192,7 +188,7 @@ int hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix); * @return 0 if the name is empty, any other value otherwise (implementation * returns 1) */ -int hicn_name_empty (hicn_name_t * name); +int hicn_name_empty (hicn_name_t *name); /** * @brief Copy an hICN name @@ -200,7 +196,7 @@ int hicn_name_empty (hicn_name_t * name); * @param [in] src - Source name to copy * @return hICN error code */ -int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src); +int hicn_name_copy (hicn_name_t *dst, const hicn_name_t *src); /** * @brief Copy an hICN name to a buffer @@ -209,8 +205,7 @@ int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src); * @param [in] copy_suffix - Flag indicating whether the suffix has to be * considered */ -int hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src, - bool copy_suffix); +int hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *src); /** * @brief Sets the segment part of an hICN name @@ -218,7 +213,7 @@ int hicn_name_copy_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 @@ -226,7 +221,7 @@ int hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number); * @param [in] seq_number - Segment identifier * @return hICN error code */ -int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number); +int hicn_name_get_seq_number (const hicn_name_t *name, u32 *seq_number); /** * @brief Convert an hICN name to a socket address @@ -234,8 +229,8 @@ int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number); * @param [out] ip_address - Resulting socket address * @return hICN error code */ -int hicn_name_to_sockaddr_address (const hicn_name_t * name, - struct sockaddr *ip_address); +int hicn_name_to_sockaddr_address (const hicn_name_t *name, + struct sockaddr *ip_address); /** * @brief Convert an hICN name to an IP address @@ -243,8 +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 @@ -253,7 +248,7 @@ int hicn_name_to_ip_prefix (const hicn_name_t * name, * @param [in] len - Number of bytes available in the buffer * @return hICN error code */ -int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len); +int hicn_name_ntop (const hicn_name_t *src, char *dst, size_t len); /** * @brief Convert an hICN name from presentation format @@ -261,7 +256,7 @@ int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len); * @param [out] dst - Resulting name * @return hICN error code */ -int hicn_name_pton (const char *src, hicn_name_t * dst); +int hicn_name_pton (const char *src, hicn_name_t *dst); /** * @brief Returns the IP address family of an hICN name @@ -269,7 +264,19 @@ int hicn_name_pton (const char *src, hicn_name_t * dst); * @param [out] family - Resulting IP address family (AF_INET or AF_INET6) * @return hICN error code */ -int hicn_name_get_family (const hicn_name_t * name, int *family); +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_no_suffix_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 @@ -277,8 +284,35 @@ int hicn_name_get_family (const hicn_name_t * name, int *family); * @param [out] prefix - Resulting prefix * @return hICN error code */ -int hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, - hicn_prefix_t * 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); +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 e8feff92d..000000000 --- a/lib/includes/hicn/ops.h +++ /dev/null @@ -1,665 +0,0 @@ -/* - * Copyright (c) 2017-2019 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 "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, - ip46_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 ip46_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 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, - ip46_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 ip46_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 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 (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 (*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 ip46_address_t * addr_new, - ip46_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 - * @return hICN error code - */ - int (*rewrite_data) (hicn_type_t type, hicn_protocol_t * h, - const ip46_address_t * addr_new, - ip46_address_t * addr_old, - const hicn_faceid_t face_id); - - /** - * @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 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); -} 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(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(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_signature_size, protocol ## _get_signature_size), \ - ATTR_INIT(set_signature_size, protocol ## _set_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), \ - } - -/** - * @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...) - */ -always_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 -} - -always_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, ip46_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 ip46_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_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, ip46_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 ip46_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_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 ip46_address_t * addr_new, ip46_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 ip46_address_t * addr_new, ip46_address_t * addr_old, const hicn_faceid_t face_id) { 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_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_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 ; } - -#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..c4a0dd80e --- /dev/null +++ b/lib/includes/hicn/packet.h @@ -0,0 +1,668 @@ +/* + * 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)) +{ + /* + * 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 format [4] + */ + hicn_packet_format_t format; + + /* + * Packet type [2] + */ + uint16_t type; + + /* + * Buffer size [2] + */ + uint16_t buffer_size; + + /* + * Packet len [2] + */ + uint16_t len; + +#ifdef OPAQUE_IP + /* Interest or data packet */ + union + { + uint16_t ipv4; + uint16_t ipv6; + }; +#endif /* OPAQUE_IP */ + /* + * L4 offset [1] + */ + union + { + uint8_t tcp; + uint8_t udp; + uint8_t icmp; + }; + /* + * New header offset [1] + */ + uint8_t newhdr; + + /* + * AH offset [2] + */ + uint16_t ah; + + /* + * Payload offset [2] + */ + uint16_t payload; +} hicn_packet_buffer_t; + +static_assert (sizeof (hicn_packet_buffer_t) == 24, ""); + +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_initialize_type (hicn_packet_buffer_t *pkbuf, + hicn_packet_type_t type); + +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); + +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 51bab4241..50b771537 100644 --- a/lib/includes/hicn/policy.h +++ b/lib/includes/hicn/policy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -30,37 +30,44 @@ /* POLICY TAG */ -#define foreach_policy_tag \ - /* Interface type */ \ - _(WIRED, 'E') \ - _(WIFI, 'W') \ - _(CELLULAR, 'C') \ - /* QoS */ \ - _(BEST_EFFORT, 'b') \ - _(REALTIME, 'r') \ - _(MULTIPATH, 'M') \ - /* Security */ \ - _(TRUSTED, 'T') - -typedef enum { -#define _(x, y) POLICY_TAG_ ## x, -foreach_policy_tag +#define foreach_policy_tag \ + /* Interface type */ \ + _ (WIRED, 'E') \ + _ (WIFI, 'W') \ + _ (CELLULAR, 'C') \ + /* QoS */ \ + _ (BEST_EFFORT, 'b') \ + _ (REALTIME, 'r') \ + _ (MULTIPATH, 'M') \ + /* Security */ \ + _ (TRUSTED, 'T') + +typedef enum +{ +#define _(x, y) POLICY_TAG_##x, + foreach_policy_tag #undef _ POLICY_TAG_N } policy_tag_t; +#define IS_VALID_POLICY_TAG(x) (x != POLICY_TAG_N) + #define MAXSZ_POLICY_TAG_ 11 -#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 +#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 -extern const char * policy_tag_str[]; +extern const char *policy_tag_str[]; extern const char policy_tag_short_str[]; -static inline -policy_tag_t -policy_tag_from_str(const char * str) +static inline policy_tag_t +policy_tag_from_str (const char *str) { -#define _(x, y) if (strcasecmp(str, policy_tag_str[POLICY_TAG_ ## x] ) == 0) { return POLICY_TAG_ ## x; } else -foreach_policy_tag +#define _(x, y) \ + if (strcasecmp (str, policy_tag_str[POLICY_TAG_##x]) == 0) \ + { \ + return POLICY_TAG_##x; \ + } \ + else + foreach_policy_tag #undef _ return POLICY_TAG_N; } @@ -69,178 +76,188 @@ foreach_policy_tag typedef int policy_tags_t; -static inline -void policy_tags_add(policy_tags_t * tags, policy_tag_t tag) +static inline void +policy_tags_add (policy_tags_t *tags, policy_tag_t tag) { - *tags |= (1 << tag); + *tags |= (1 << tag); } -static inline -void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag) +static inline void +policy_tags_remove (policy_tags_t *tags, policy_tag_t tag) { - *tags &= ~(1 << tag); + *tags &= ~(1 << tag); } -static inline -int policy_tags_has(policy_tags_t tags, policy_tag_t tag) +static inline int +policy_tags_has (policy_tags_t tags, policy_tag_t tag) { - return tags & (1 << tag); + return tags & (1 << tag); } -static inline -void policy_tags_union(policy_tags_t * tags, policy_tags_t * tags_to_union) +static inline void +policy_tags_union (policy_tags_t *tags, policy_tags_t *tags_to_union) { -#define _(x, y) *tags |= policy_tags_has(*tags_to_union, POLICY_TAG_ ## x) ? (1 << POLICY_TAG_ ## x) : 0; -foreach_policy_tag +#define _(x, y) \ + *tags |= policy_tags_has (*tags_to_union, POLICY_TAG_##x) ? \ + (1 << POLICY_TAG_##x) : \ + 0; + foreach_policy_tag #undef _ } #define POLICY_TAGS_EMPTY 0 -static inline -int -policy_tags_snprintf(char * s, size_t size, policy_tags_t tags) +static inline int +policy_tags_snprintf (char *s, size_t size, policy_tags_t tags) { -#define _(x, y) s[POLICY_TAG_ ## x] = policy_tags_has(tags, POLICY_TAG_ ## x) ? y : '.'; -foreach_policy_tag +#define _(x, y) \ + s[POLICY_TAG_##x] = policy_tags_has (tags, POLICY_TAG_##x) ? y : '.'; + foreach_policy_tag #undef _ s[POLICY_TAG_N] = '\0'; - return POLICY_TAG_N + 1; + return POLICY_TAG_N + 1; } #define MAXSZ_POLICY_TAGS_ POLICY_TAG_N + 1 -#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1 +#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1 /* POLICY STATE */ /* TODO vs. weight */ -#define foreach_policy_state \ - _(NEUTRAL) \ - _(REQUIRE) \ - _(PREFER) \ - _(AVOID) \ - _(PROHIBIT) \ - _(N) - -typedef enum { -#define _(x) POLICY_STATE_ ## x, -foreach_policy_state +#define foreach_policy_state \ + _ (NEUTRAL) \ + _ (REQUIRE) \ + _ (PREFER) \ + _ (AVOID) \ + _ (PROHIBIT) \ + _ (N) + +typedef enum +{ +#define _(x) POLICY_STATE_##x, + foreach_policy_state #undef _ } policy_state_t; #define MAXSZ_POLICY_STATE_ 8 -#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 +#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 + +extern const char *policy_state_str[]; -extern const char * policy_state_str[]; +#define policy_state_str(x) policy_state_str[x] +policy_state_t policy_state_from_str (const char *str); /* POLICY TAG STATE */ -typedef struct { - policy_state_t state; - uint8_t disabled; +typedef struct __attribute__ ((packed, aligned (1))) +{ + policy_state_t state; + uint8_t disabled; } policy_tag_state_t; #define MAXSZ_POLICY_TAG_STATE_ 8 -#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1 - -int policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state); +#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1 +int policy_tag_state_snprintf (char *s, size_t size, + const policy_tag_state_t *tag_state); /* INTERFACE STATS */ -typedef struct { - float throughput; - float latency; - float loss_rate; +typedef struct +{ + float throughput; + float latency; + float loss_rate; } interface_stats_t; -#define INTERFACE_STATS_NONE { \ - .throughput = 0, \ - .latency = 0, \ - .loss_rate = 0, \ -} - +#define INTERFACE_STATS_EMPTY \ + { \ + .throughput = 0, .latency = 0, .loss_rate = 0, \ + } /* POLICY STATS */ -typedef struct { - interface_stats_t wired; - interface_stats_t wifi; - interface_stats_t cellular; - interface_stats_t all; +typedef struct +{ + interface_stats_t wired; + interface_stats_t wifi; + interface_stats_t cellular; + interface_stats_t all; } policy_stats_t; -#define POLICY_STATS_NONE { \ - .wired = INTERFACE_STATS_NONE, \ - .wifi = INTERFACE_STATS_NONE, \ - .cellular = INTERFACE_STATS_NONE, \ - .all = INTERFACE_STATS_NONE, \ -} +#define POLICY_STATS_EMPTY \ + (policy_stats_t) \ + { \ + .wired = INTERFACE_STATS_EMPTY, .wifi = INTERFACE_STATS_EMPTY, \ + .cellular = INTERFACE_STATS_EMPTY, .all = INTERFACE_STATS_EMPTY, \ + } -typedef struct { - uint32_t num_packets; - uint32_t num_bytes; - uint32_t num_losses; - uint32_t latency_idle; +typedef struct +{ + uint32_t num_packets; + uint32_t num_bytes; + uint32_t num_losses; + uint32_t latency_idle; } interface_counters_t; -#define INTERFACE_COUNTERS_NONE { \ - .num_packets = 0, \ - .num_bytes = 0, \ - .num_losses = 0, \ - .latency_idle = 0, \ -} +#define INTERFACE_COUNTERS_EMPTY \ + { \ + .num_packets = 0, .num_bytes = 0, .num_losses = 0, .latency_idle = 0, \ + } -typedef struct { - interface_counters_t wired; - interface_counters_t wifi; - interface_counters_t cellular; - interface_counters_t all; - uint64_t last_update; +typedef struct +{ + interface_counters_t wired; + interface_counters_t wifi; + interface_counters_t cellular; + interface_counters_t all; + uint64_t last_update; } policy_counters_t; -#define POLICY_COUNTERS_NONE (policy_counters_t) { \ - .wired = INTERFACE_COUNTERS_NONE, \ - .wifi = INTERFACE_COUNTERS_NONE, \ - .cellular = INTERFACE_COUNTERS_NONE, \ - .all = INTERFACE_COUNTERS_NONE, \ - .last_update = 0, \ -} +#define POLICY_COUNTERS_EMPTY \ + (policy_counters_t) \ + { \ + .wired = INTERFACE_COUNTERS_EMPTY, .wifi = INTERFACE_COUNTERS_EMPTY, \ + .cellular = INTERFACE_COUNTERS_EMPTY, .all = INTERFACE_COUNTERS_EMPTY, \ + .last_update = 0, \ + } /* POLICY */ #define APP_NAME_LEN 128 -typedef struct { - char app_name[APP_NAME_LEN]; - policy_tag_state_t tags[POLICY_TAG_N]; - policy_stats_t stats; -} policy_t; +typedef struct +{ + char app_name[APP_NAME_LEN]; + policy_tag_state_t tags[POLICY_TAG_N]; + policy_stats_t stats; +} hicn_policy_t; -static const policy_t POLICY_NONE = { +static const hicn_policy_t POLICY_EMPTY = { .app_name = { 0 }, .tags = { -#define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 }, +#define _(x, y) { POLICY_STATE_NEUTRAL, 0 }, foreach_policy_tag #undef _ }, - .stats = POLICY_STATS_NONE, + .stats = POLICY_STATS_EMPTY, }; - /* POLICY DESCRIPTION */ #define PFX_STRLEN 4 /* eg. /128 */ -typedef struct { - int family; - union { - char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN]; - char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN]; - }; - policy_t policy; +typedef struct +{ + int family; + union + { + char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN]; + char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN]; + }; + hicn_policy_t policy; } policy_description_t; #endif /* HICN_POLICY_H */ diff --git a/lib/includes/hicn/protocol.h b/lib/includes/hicn/protocol.h deleted file mode 100644 index a97cc99cf..000000000 --- a/lib/includes/hicn/protocol.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2017-2019 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" - -typedef union -{ - _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/ah.h b/lib/includes/hicn/protocol/ah.h index a59a5051a..36fd7d509 100644 --- a/lib/includes/hicn/protocol/ah.h +++ b/lib/includes/hicn/protocol/ah.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -35,16 +35,19 @@ typedef struct { - u8 nh; // (to match with reserved in IPSEC AH) - u8 payloadlen; // Len of signature/HMAC in 4-bytes words + u8 nh; // To match with reserved in IPSEC AH + // Length of the signature field. Note that the signature might be smaller + // than the field: the actual size is computed from the field size and + // signaturePadding. + u8 payloadlen; union { u16 reserved; struct { - u8 validationAlgorithm; // As defined in parc_SignerAlgorithm.h - u8 unused; // Unused (to match with reserved in IPSEC AH) + u8 validationAlgorithm; + u8 signaturePadding; }; }; union @@ -60,12 +63,12 @@ typedef struct u32 timestamp_as_u32[2]; }; // ICV would follow - u8 keyId[32]; // Hash of the pub key + u8 keyId[32]; // Hash of pub key /* 44 B + validationPayload */ - u8 validationPayload[0]; // Holds the signature + u8 validationPayload[0]; // Holds the signature } _ah_header_t; -#define AH_HDRLEN sizeof(_ah_header_t) +#define AH_HDRLEN sizeof (_ah_header_t) static_assert (EXPECTED_AH_HDRLEN == AH_HDRLEN, "Size of AH Struct does not match its expected size."); diff --git a/lib/includes/hicn/protocol/icmp.h b/lib/includes/hicn/protocol/icmp.h index 36954bb6d..9315e1e56 100644 --- a/lib/includes/hicn/protocol/icmp.h +++ b/lib/includes/hicn/protocol/icmp.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -34,7 +34,7 @@ typedef struct u16 csum; } _icmp_header_t; -#define ICMP_HDRLEN sizeof(_icmp_header_t) +#define ICMP_HDRLEN sizeof (_icmp_header_t) static_assert (EXPECTED_ICMP_HDRLEN == ICMP_HDRLEN, "Size of ICMP struct does not match its expected size."); @@ -54,13 +54,13 @@ typedef struct { u16 id; u16 sequence; - } echo; /* echo datagram */ - u32 gateway; /* gateway address */ + } echo; /* echo datagram */ + u32 gateway; /* gateway address */ struct { u16 _unused; u16 mtu; - } frag; /* path mtu discovery */ + } frag; /* path mtu discovery */ struct { u16 expected_lbl; @@ -69,7 +69,7 @@ typedef struct }; } _icmp_wldr_header_t; -#define ICMPWLDR_HDRLEN sizeof(_icmp_wldr_header_t) +#define ICMPWLDR_HDRLEN sizeof (_icmp_wldr_header_t) static_assert (EXPECTED_ICMPWLDR_HDRLEN == ICMPWLDR_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); diff --git a/lib/includes/hicn/protocol/icmprd.h b/lib/includes/hicn/protocol/icmprd.h index aa1fa01ae..17088c22a 100644 --- a/lib/includes/hicn/protocol/icmprd.h +++ b/lib/includes/hicn/protocol/icmprd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -33,12 +33,12 @@ 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; -#define ICMPRD4_HDRLEN sizeof(_icmprd4_header_t) +#define ICMPRD4_HDRLEN sizeof (_icmprd4_header_t) static_assert (EXPECTED_ICMPRD4_HDRLEN == ICMPRD4_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); @@ -53,11 +53,11 @@ 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) +#define ICMPRD_HDRLEN sizeof (_icmprd_header_t) static_assert (EXPECTED_ICMPRD_HDRLEN == ICMPRD_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); diff --git a/lib/includes/hicn/protocol/ipv4.h b/lib/includes/hicn/protocol/ipv4.h index 8a5b6683b..89435b516 100644 --- a/lib/includes/hicn/protocol/ipv4.h +++ b/lib/includes/hicn/protocol/ipv4.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -16,9 +16,10 @@ #ifndef HICN_PROTOCOL_IPV4 #define HICN_PROTOCOL_IPV4 +#include <hicn/util/ip_address.h> + #include "../base.h" #include "../common.h" -#include "../protocol.h" /* Headers were adapted from linux' definitions in netinet/ip.h */ @@ -34,11 +35,11 @@ typedef struct struct { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - u8 ihl:4; - u8 version:4; + u8 ihl : 4; + u8 version : 4; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - u8 version:4; - u8 ihl:4; + u8 version : 4; + u8 ihl : 4; #else #error "Unsupported endianness" #endif @@ -53,13 +54,14 @@ 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) (sizeof(u32) * (ipv4_header->version_ihl & 0xf)) +#define ipv4_header_bytes(ipv4_header) \ + (sizeof (u32) * (ipv4_header->version_ihl & 0xf)) -#define IPV4_HDRLEN sizeof(_ipv4_header_t) +#define IPV4_HDRLEN sizeof (_ipv4_header_t) static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN, "Size of IPV4 struct does not match its expected size."); @@ -70,29 +72,28 @@ 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; } ipv4_pseudo_header_t; -#define IPV4_PSHDRLEN sizeof(ipv4_pseudo_header_t) +#define IPV4_PSHDRLEN sizeof (ipv4_pseudo_header_t) static_assert (EXPECTED_IPV4_PSHDRLEN == IPV4_PSHDRLEN, "Size of IPV4_PSHDR struct does not match its expected size."); /* Default field values */ -#define IPV4_DEFAULT_VERSION 4 -#define IPV4_DEFAULT_IHL 5 -#define IPV4_DEFAULT_TOS 0 -#define IPV4_DEFAULT_PAYLOAD_LENGTH 0 -#define IPV4_DEFAULT_ID 300 -#define IPV4_DEFAULT_FRAG_OFF 0x000 -#define IPV4_DEFAULT_TTL 64 -#define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP -#define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0 -#define IPV4_DEFAULT_DST_IP 0, 0, 0, 0 - +#define IPV4_DEFAULT_VERSION 4 +#define IPV4_DEFAULT_IHL 5 +#define IPV4_DEFAULT_TOS 0 +#define IPV4_DEFAULT_PAYLOAD_LENGTH 0 +#define IPV4_DEFAULT_ID 300 +#define IPV4_DEFAULT_FRAG_OFF 0x000 +#define IPV4_DEFAULT_TTL 64 +#define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP +#define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0 +#define IPV4_DEFAULT_DST_IP 0, 0, 0, 0 #endif /* HICN_PROTOCOL_IPV4 */ diff --git a/lib/includes/hicn/protocol/ipv6.h b/lib/includes/hicn/protocol/ipv6.h index 5a83abcae..6e8e30af3 100644 --- a/lib/includes/hicn/protocol/ipv6.h +++ b/lib/includes/hicn/protocol/ipv6.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -16,6 +16,8 @@ #ifndef HICN_PROTOCOL_IPV6_H #define HICN_PROTOCOL_IPV6_H +#include <hicn/util/ip_address.h> + #include "../common.h" /* @@ -25,22 +27,26 @@ typedef struct { +#if 0 // TEMPORARY FIX union { struct { - u32 version_class_flow; /* version, traffic class and 20 bits of flow-ID */ - u16 len; /* payload length */ - u8 nxt; /* next header */ - u8 hlim; /* hop limit */ +#endif + u32 version_class_flow; /* version, traffic class and 20 bits of flow-ID */ + u16 len; /* payload length */ + u8 nxt; /* next header */ + u8 hlim; /* hop limit */ +#if 0 }; u8 vfc; /* 4 bits version, top 4 bits class */ }; - ip6_address_t saddr; /* source address */ - ip6_address_t daddr; /* destination address */ +#endif + ipv6_address_t saddr; /* source address */ + ipv6_address_t daddr; /* destination address */ } _ipv6_header_t; -#define IPV6_HDRLEN sizeof(_ipv6_header_t) +#define IPV6_HDRLEN sizeof (_ipv6_header_t) static_assert (EXPECTED_IPV6_HDRLEN == IPV6_HDRLEN, "Size of IPV6 struct does not match its expected size."); @@ -51,23 +57,23 @@ 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; u8 protocol; } ipv6_pseudo_header_t; -#define IPV6_PSHDRLEN sizeof(ipv6_pseudo_header_t) +#define IPV6_PSHDRLEN sizeof (ipv6_pseudo_header_t) static_assert (EXPECTED_IPV6_PSHDRLEN == IPV6_PSHDRLEN, "Size of IPV6_PSHDR struct does not match its expected size."); /* Default field values */ -#define IPV6_DEFAULT_VERSION 6 -#define IPV6_DEFAULT_TRAFFIC_CLASS 0 -#define IPV6_DEFAULT_FLOW_LABEL 0 -#define IPV6_DEFAULT_PAYLOAD_LENGTH 0 +#define IPV6_DEFAULT_VERSION 6 +#define IPV6_DEFAULT_TRAFFIC_CLASS 0 +#define IPV6_DEFAULT_FLOW_LABEL 0 +#define IPV6_DEFAULT_PAYLOAD_LENGTH 0 #endif diff --git a/lib/includes/hicn/protocol/new.h b/lib/includes/hicn/protocol/new.h new file mode 100644 index 000000000..e688bef21 --- /dev/null +++ b/lib/includes/hicn/protocol/new.h @@ -0,0 +1,88 @@ +/* + * 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/ah.h + * @brief AH packet header + */ +#ifndef HICN_PROTOCOL_NEW_H +#define HICN_PROTOCOL_NEW_H + +#include "../common.h" +#include "../name.h" + +/* + * The length of the new header struct must be 28 bytes. + */ +#define EXPECTED_NEW_HDRLEN 32 + +typedef struct +{ + u8 version_reserved; + u8 flags; + u16 payload_length; + u32 lifetime; + hicn_ip_address_t prefix; + u32 suffix; + u32 path_label; +} _new_header_t; + +#define NEW_HDRLEN sizeof (_new_header_t) +static_assert (EXPECTED_NEW_HDRLEN == NEW_HDRLEN, + "Size of new_header Struct does not match its expected size."); + +/* TCP flags bit 0 first. */ +#define foreach_hicn_new_flag \ + _ (SIG) /**< Signature header after. */ \ + _ (MAN) /**< Payload type is manifest. */ \ + _ (INT) /**< Packet is interest. */ \ + _ (LST) /**< Last data. */ + +enum +{ +#define _(f) HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ + HICN_NEW_N_FLAG_BITS, +}; + +enum +{ +#define _(f) HICN_NEW_FLAG_##f = 1 << HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ +}; + +static inline int +_get_new_header_version (const _new_header_t *new_hdr) +{ + return ((new_hdr->version_reserved >> 4) & 0x0F); +} + +static inline void +_set_new_header_version (_new_header_t *new_hdr) +{ + new_hdr->version_reserved = (0x9 << 4) & 0xF0; +} + +#endif /* HICN_PROTOCOL_NEW_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/includes/hicn/protocol/tcp.h b/lib/includes/hicn/protocol/tcp.h index 347682299..1731f0c24 100644 --- a/lib/includes/hicn/protocol/tcp.h +++ b/lib/includes/hicn/protocol/tcp.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -59,55 +59,55 @@ typedef struct #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ struct { - u16 reserved:4; - u16 doff:4; - u16 fin:1; - u16 syn:1; - u16 rst:1; - u16 psh:1; - u16 ack:1; - u16 urg:1; - u16 ece:1; - u16 cwr:1; + u16 reserved : 4; + u16 doff : 4; + u16 fin : 1; + u16 syn : 1; + u16 rst : 1; + u16 psh : 1; + u16 ack : 1; + u16 urg : 1; + u16 ece : 1; + u16 cwr : 1; }; struct - { /* __ denotes unchanged bitfields */ - u16 timescale:4; - u16 __doff:4; - u16 __fin:1; - u16 __syn:1; - u16 __rst:1; - u16 sig:1; - u16 __ack:1; - u16 man:1; - u16 id:1; - u16 __cwr:1; + { /* __ denotes unchanged bitfields */ + u16 timescale : 4; + u16 __doff : 4; + u16 __fin : 1; + u16 __syn : 1; + u16 __rst : 1; + u16 sig : 1; + u16 __ack : 1; + u16 man : 1; + u16 id : 1; + u16 __cwr : 1; }; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ struct { - u16 doff:4; - u16 reserved:4; - u16 cwr:1; - u16 ece:1; - u16 urg:1; - u16 ack:1; - u16 psh:1; - u16 rst:1; - u16 syn:1; - u16 fin:1; + u16 doff : 4; + u16 reserved : 4; + u16 cwr : 1; + u16 ece : 1; + u16 urg : 1; + u16 ack : 1; + u16 psh : 1; + u16 rst : 1; + u16 syn : 1; + u16 fin : 1; }; struct { - u16 __doff:4; - u16 timescale:4; - u16 __cwr:1; - u16 id:1 u16 man:1; - u16 __ack:1; - u16 sig:1; - u16 __rst:1; - u16 __syn:1; - u16 __fin:1; + u16 __doff : 4; + u16 timescale : 4; + u16 __cwr : 1; + u16 id : 1 u16 man : 1; + u16 __ack : 1; + u16 sig : 1; + u16 __rst : 1; + u16 __syn : 1; + u16 __fin : 1; }; #endif }; @@ -124,20 +124,20 @@ typedef struct }; } _tcp_header_t; -#define TCP_HDRLEN sizeof(_tcp_header_t) +#define TCP_HDRLEN sizeof (_tcp_header_t) static_assert (EXPECTED_TCP_HDRLEN == TCP_HDRLEN, "Size of TCP struct does not match its expected size."); /* TCP flags bit 0 first. */ -#define foreach_tcp_flag \ - _ (FIN) /**< No more data from sender. */ \ - _ (SYN) /**< Synchronize sequence numbers. */ \ - _ (RST) /**< Reset the connection. */ \ - _ (PSH) /**< Push function. */ \ - _ (ACK) /**< Ack field significant. */ \ - _ (URG) /**< Urgent pointer field significant. */ \ - _ (ECE) /**< ECN-echo. Receiver got CE packet */ \ - _ (CWR) /**< Sender reduced congestion window */ +#define foreach_tcp_flag \ + _ (FIN) /**< No more data from sender. */ \ + _ (SYN) /**< Synchronize sequence numbers. */ \ + _ (RST) /**< Reset the connection. */ \ + _ (PSH) /**< Push function. */ \ + _ (ACK) /**< Ack field significant. */ \ + _ (URG) /**< Urgent pointer field significant. */ \ + _ (ECE) /**< ECN-echo. Receiver got CE packet */ \ + _ (CWR) /**< Sender reduced congestion window */ enum { diff --git a/lib/includes/hicn/protocol/udp.h b/lib/includes/hicn/protocol/udp.h index 75d1ea98c..0e0dddd2c 100644 --- a/lib/includes/hicn/protocol/udp.h +++ b/lib/includes/hicn/protocol/udp.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -29,7 +29,7 @@ typedef struct u16 checksum; } _udp_header_t; -#define UDP_HDRLEN sizeof(_udp_header_t) +#define UDP_HDRLEN sizeof (_udp_header_t) static_assert (EXPECTED_UDP_HDRLEN == UDP_HDRLEN, "Size of UDP struct does not match its expected size."); diff --git a/lib/includes/hicn/strategy.h b/lib/includes/hicn/strategy.h new file mode 100644 index 000000000..196d59efa --- /dev/null +++ b/lib/includes/hicn/strategy.h @@ -0,0 +1,64 @@ +/* + * 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 strategy.h + * \brief hICN forwarding strategy + */ +#ifndef HICN_STRATEGY_H +#define HICN_STRATEGY_H + +#include <string.h> + +#define foreach_strategy_type \ + _ (UNDEFINED) \ + _ (LOADBALANCER) \ + _ (LOW_LATENCY) \ + _ (RANDOM) \ + _ (REPLICATION) \ + _ (BESTPATH) \ + _ (LOCAL_REMOTE) \ + _ (N) + +typedef enum +{ +#define _(x) STRATEGY_TYPE_##x, + foreach_strategy_type +#undef _ +} strategy_type_t; + +extern const char *strategy_str[]; +#define strategy_str(x) strategy_str[x] + +#define IS_VALID_STRATEGY_TYPE(x) IS_VALID_ENUM_TYPE (STRATEGY_TYPE, x) + +static inline strategy_type_t +strategy_type_from_str (const char *strategy_str) +{ +#define _(x) \ + if (strcasecmp (strategy_str, #x) == 0) \ + return STRATEGY_TYPE_##x; \ + else + foreach_strategy_type +#undef _ + return STRATEGY_TYPE_UNDEFINED; +} + +#define STRATEGY_TYPE_VALID(type) \ + ((type != STRATEGY_TYPE_UNDEFINED) && (type != STRATEGY_TYPE_N)) + +#define MAX_FWD_STRATEGY_RELATED_PREFIXES 10 + +#endif /* HICN_STRATEGY_H */ diff --git a/lib/includes/hicn/util/array.h b/lib/includes/hicn/util/array.h index 56cfcad8b..f56c13140 100644 --- a/lib/includes/hicn/util/array.h +++ b/lib/includes/hicn/util/array.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -23,178 +23,167 @@ #include <assert.h> #include <hicn/util/log.h> -#include <math.h> // log2 +#include <math.h> // log2 #include <string.h> // memmove #define BUFSIZE 1024 -typedef int(*cmp_t)(const void * x, const void * y); - -#define TYPEDEF_ARRAY_H(NAME, T) \ - \ -typedef struct { \ - size_t size; \ - size_t max_size_log; \ - T * elements; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * array); \ - \ -int NAME ## _finalize(NAME ## _t * array); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * array); \ - \ -int NAME ## _add(NAME ## _t * array, T element); \ - \ -int NAME ## _remove_index(NAME ## _t * array, int index, T * element); \ - \ -int NAME ## _remove(NAME ## _t * array, const T search, T * element); \ - \ -int NAME ## _get(const NAME ## _t * array, const T search, T * element); \ - \ -int NAME ## _get_index(const NAME ## _t * array, int index, T * element); \ - \ -int NAME ## _get_elements(const NAME ## _t * array, T ** elements); \ - \ -size_t NAME ## _len(const NAME ## _t * array); - +#define TYPEDEF_ARRAY_H(NAME, T) \ + \ + typedef struct \ + { \ + size_t size; \ + size_t max_size_log; \ + T *elements; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *array); \ + \ + int NAME##_finalize (NAME##_t *array); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *array); \ + \ + int NAME##_add (NAME##_t *array, T element); \ + \ + int NAME##_remove_index (NAME##_t *array, int index, T *element); \ + \ + int NAME##_remove (NAME##_t *array, const T search, T *element); \ + \ + int NAME##_get (const NAME##_t *array, const T search, T *element); \ + \ + int NAME##_get_index (const NAME##_t *array, int index, T *element); \ + \ + int NAME##_get_elements (const NAME##_t *array, T **elements); \ + \ + size_t NAME##_len (const NAME##_t *array); #define ARRAY_MAX_SIZE_LOG_INIT 0 -#define TYPEDEF_ARRAY(NAME, T, CMP, SNPRINTF) \ -int \ -NAME ## _initialize(NAME ## _t * array) \ -{ \ - array->max_size_log = ARRAY_MAX_SIZE_LOG_INIT; \ - array->size = 0; \ - if (array->max_size_log == 0) { \ - array->elements = NULL; \ - return 0; \ - } \ - array->elements = malloc((1 << array->max_size_log) * sizeof(T)); \ - if (!array->elements) \ - return -1; \ - return 0; \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * array) \ -{ \ - for (unsigned i = 0; i < array->size; i++) { \ - NAME ## _remove_index(array, i, NULL); \ - } \ - return 0; \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * array = malloc(sizeof(NAME ## _t)); \ - if (!array) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(array) < 0) \ - goto ERR_INITIALIZE; \ - \ - return array; \ - \ -ERR_INITIALIZE: \ - free(array); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * array) \ -{ \ - NAME ## _finalize(array); \ - free(array->elements); \ - free(array); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * array, T element) \ -{ \ - /* Ensure sufficient space for next addition */ \ - size_t new_size_log = (array->size > 0) ? log2(array->size)+1 : 1; \ - if (new_size_log > array->max_size_log) { \ - array->max_size_log = new_size_log; \ - array->elements = realloc(array->elements, \ - (1 << new_size_log) * sizeof(T)); \ - } \ - \ - if (!array->elements) \ - goto ERR_REALLOC; \ - \ - array->elements[array->size++] = element; \ - return 0; \ - \ -ERR_REALLOC: \ - return -1; \ -} \ - \ -int \ -NAME ## _remove_index(NAME ## _t * array, int index, T * element) \ -{ \ - if (index > NAME ## _len(array)) \ - return -1; \ - if (element) \ - *element = array->elements[index]; \ - if (index < array->size) \ - memmove(array->elements + index, array->elements + index + 1, \ - array->size - index); \ - array->size--; \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * array, const T search, T * element) \ -{ \ - for (unsigned i = 0; i < array->size; i++) { \ - if (CMP(search, array->elements[i]) == 0) \ - return facelet_array_remove_index(array, i, element); \ - } \ - /* Not found */ \ - if (element) \ - *element = NULL; \ - return 0; \ -} \ - \ -int \ -NAME ## _get(const NAME ## _t * array, const T search, T * element) \ -{ \ - assert(element); \ - for (unsigned i = 0; i < array->size; i++) \ - if (CMP(search, array->elements[i]) == 0) { \ - *element = array->elements[i]; \ - return 0; \ - } \ - /* Not found */ \ - *element = NULL; \ - return 0; \ -} \ - \ -int \ -NAME ## _get_index(const NAME ## _t * array, int index, T * element) \ -{ \ - assert(element); \ - *element = array->elements[index]; \ - return 0; \ -} \ - \ -int \ -NAME ## _get_elements(const NAME ## _t * array, T ** elements) \ -{ \ - *elements = array->elements; \ - return 0; \ -} \ - \ -size_t \ -NAME ## _len(const NAME ## _t * array) \ -{ \ - return array->size; \ -} +#define TYPEDEF_ARRAY(NAME, T, CMP, SNPRINTF) \ + int NAME##_initialize (NAME##_t *array) \ + { \ + array->max_size_log = ARRAY_MAX_SIZE_LOG_INIT; \ + array->size = 0; \ + if (array->max_size_log == 0) \ + { \ + array->elements = NULL; \ + return 0; \ + } \ + array->elements = malloc ((1 << array->max_size_log) * sizeof (T)); \ + if (!array->elements) \ + return -1; \ + return 0; \ + } \ + \ + int NAME##_finalize (NAME##_t *array) \ + { \ + for (unsigned i = 0; i < array->size; i++) \ + { \ + NAME##_remove_index (array, i, NULL); \ + } \ + return 0; \ + } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *array = malloc (sizeof (NAME##_t)); \ + if (!array) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (array) < 0) \ + goto ERR_INITIALIZE; \ + \ + return array; \ + \ + ERR_INITIALIZE: \ + free (array); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *array) \ + { \ + NAME##_finalize (array); \ + free (array->elements); \ + free (array); \ + } \ + \ + int NAME##_add (NAME##_t *array, T element) \ + { \ + /* Ensure sufficient space for next addition */ \ + size_t new_size_log = (array->size > 0) ? log2 (array->size) + 1 : 1; \ + if (new_size_log > array->max_size_log) \ + { \ + array->max_size_log = new_size_log; \ + array->elements = \ + realloc (array->elements, (1 << new_size_log) * sizeof (T)); \ + } \ + \ + if (!array->elements) \ + goto ERR_REALLOC; \ + \ + array->elements[array->size++] = element; \ + return 0; \ + \ + ERR_REALLOC: \ + return -1; \ + } \ + \ + int NAME##_remove_index (NAME##_t *array, int index, T *element) \ + { \ + if (index > NAME##_len (array)) \ + return -1; \ + if (element) \ + *element = array->elements[index]; \ + if (index < array->size) \ + memmove (array->elements + index, array->elements + index + 1, \ + array->size - index); \ + array->size--; \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *array, const T search, T *element) \ + { \ + for (unsigned i = 0; i < array->size; i++) \ + { \ + if (CMP (search, array->elements[i]) == 0) \ + return NAME##_remove_index (array, i, element); \ + } \ + /* Not found */ \ + if (element) \ + *element = NULL; \ + return 0; \ + } \ + \ + int NAME##_get (const NAME##_t *array, const T search, T *element) \ + { \ + assert (element); \ + for (unsigned i = 0; i < array->size; i++) \ + if (CMP (search, array->elements[i]) == 0) \ + { \ + *element = array->elements[i]; \ + return 0; \ + } \ + /* Not found */ \ + *element = NULL; \ + return 0; \ + } \ + \ + int NAME##_get_index (const NAME##_t *array, int index, T *element) \ + { \ + assert (element); \ + *element = array->elements[index]; \ + return 0; \ + } \ + \ + int NAME##_get_elements (const NAME##_t *array, T **elements) \ + { \ + *elements = array->elements; \ + return 0; \ + } \ + \ + size_t NAME##_len (const NAME##_t *array) { return array->size; } #endif /* UTIL_ARRAY_H */ diff --git a/lib/includes/hicn/util/bitmap.h b/lib/includes/hicn/util/bitmap.h new file mode 100644 index 000000000..d83c838b7 --- /dev/null +++ b/lib/includes/hicn/util/bitmap.h @@ -0,0 +1,332 @@ +/* + * 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 bitmap.h + * \brief Bitmap + * + * A bitmap is implemented as a wrapper over a vector made of bit elements + */ + +#ifndef UTIL_BITMAP_H +#define UTIL_BITMAP_H + +#include <assert.h> +#include <stdio.h> +#include <string.h> +#include <sys/param.h> // MIN, MAX + +#include <hicn/common.h> +#include <hicn/util/vector.h> + +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 +hicn_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 + * + * @param[in,out] bitmap Bitmap to allocate and initialize + * @param[in] max_size Bitmap max_size + */ +#define bitmap_init(bitmap, init_size, max_size) \ + vector_init ( \ + bitmap, next_pow2 ((init_size) / BITMAP_WIDTH (bitmap)), \ + max_size == 0 ? 0 : next_pow2 ((max_size) / BITMAP_WIDTH (bitmap))) + +/* + * @brief Ensures a bitmap is sufficiently large to hold an element at the + * given position. + * + * @param[in] bitmap The bitmap for which to validate the position. + * @param[in] pos The position to validate. + * + * NOTE: + * - This function should always be called before writing to a bitmap element + * to eventually make room for it (the bitmap will eventually be resized). + */ +static inline int +bitmap_ensure_pos (bitmap_t **bitmap, off_t pos) +{ + size_t offset = pos / BITMAP_WIDTH (*bitmap); + return vector_ensure_pos (*bitmap, offset); +} + +/** + * @brief Returns the allocated size of a bitmap. + * + * @see listener_table_get_by_id + */ +#define bitmap_get_alloc_size(bitmap) vector_get_alloc_size (bitmap) + +/** + * @brief Retrieve the state of the i-th bit in the bitmap. + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + */ +static inline int +_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); + 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; +} + +/* + * @brief Returns whether the i-th bit is set (equal to 1) in a bitmap. + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + * + * @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_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) + +static inline int +_bitmap_set_no_check (bitmap_t *bitmap, off_t i) +{ + size_t offset = i / BITMAP_WIDTH (bitmap); + size_t pos = i % BITMAP_WIDTH (bitmap); + bitmap[offset] |= (hicn_uword) (1) << pos; + return 0; +} + +static inline int +_bitmap_set (bitmap_t **bitmap_ptr, off_t i) +{ + if (bitmap_ensure_pos (bitmap_ptr, i) < 0) + return -1; + + bitmap_t *bitmap = *bitmap_ptr; + return _bitmap_set_no_check (bitmap, i); +} + +/* + * @brief Set i-th bit to 1 in a bitmap. Reallocate the vector if the bit + * position is greater than the vector length. + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + * + * @return bool + */ +#define bitmap_set(bitmap, i) _bitmap_set ((bitmap_t **) &bitmap, i) + +/* + * @brief Set i-th bit to 1 in a bitmap. Unsafe version, does not check + * boundaries. + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + * + * @return bool + */ +#define bitmap_set_no_check(bitmap, i) _bitmap_set_no_check (bitmap, i) + +#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, int check) +{ + if (check && bitmap_ensure_pos (&bitmap, i) < 0) + return -1; + size_t offset = i / BITMAP_WIDTH (bitmap); + size_t pos = i % BITMAP_WIDTH (bitmap); + bitmap[offset] &= ~((hicn_uword) (1) << pos); + return 0; +} + +static inline int +bitmap_set_range (bitmap_t *bitmap, off_t from, off_t to) +{ + assert (from <= to); + ssize_t offset_from = from / BITMAP_WIDTH (bitmap); + ssize_t offset_to = to / BITMAP_WIDTH (bitmap); + size_t pos_from = from % BITMAP_WIDTH (bitmap); + size_t pos_to = to % BITMAP_WIDTH (bitmap); + + /* + * First block initialization is needed if <from> is not aligned with the + * bitmap element size or if to is within the same one. + */ + if ((pos_from != 0) || + ((offset_to == offset_from) && (pos_to != BITMAP_WIDTH (bitmap) - 1))) + { + size_t from_end = MIN (to, (offset_from + 1) * BITMAP_WIDTH (bitmap)); + for (size_t k = from; k < from_end; k++) + { + if (bitmap_set (bitmap, k) < 0) + goto END; + } + } + + /* + * Second block is needed if <to> is not aligned with the bitmap element + * size + */ + if ((pos_to != BITMAP_WIDTH (bitmap) - 1) && (offset_to != offset_from)) + { + size_t to_start = MAX (from, offset_to * BITMAP_WIDTH (bitmap)); + for (size_t k = to_start; k < (size_t) to; k++) + { + if (bitmap_set (bitmap, k) < 0) + goto END; + } + } + + if (pos_from != 0) + offset_from += 1; + if (pos_to != BITMAP_WIDTH (bitmap) - 1) + offset_to -= 1; + + /* + * We need to cover both elements at position offset_from and offset_to + * provided that offset_from is not bigger + */ + if (offset_to >= offset_from) + { + memset (&bitmap[offset_from], 0xFF, + (offset_to - offset_from + 1) * sizeof (bitmap[0])); + } + + return 0; + +END: + return -1; +} + +#define bitmap_set_to(bitmap, to) bitmap_set_range ((bitmap), 0, (to)) + +#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 hicn_get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + + for (pos += 1; pos < length; pos++) + { + tmp = bitmap[pos]; + if (tmp) + return hicn_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 hicn_get_lowest_set_bit_index (tmp) + pos * WORD_WIDTH; + + for (pos += 1; pos < length; pos++) + { + tmp = ~bitmap[pos]; + if (tmp) + return hicn_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_check (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_check (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 new file mode 100644 index 000000000..8dbe0d680 --- /dev/null +++ b/lib/includes/hicn/util/hash.h @@ -0,0 +1,368 @@ +/* + * 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 hash.h + * \brief Simple non-cryptographic hash implementation. + * + * Two helpers are provided : + * hash(buf, len) : hash a buffer <buf> of length <len> + * hash_struct(buf) : hash a buffer corresponding to an allocated struct + * + * This file consists in excerpts from Jenkins hash (public domain). + * http://www.burtleburtle.net/bob/c/lookup3.c + */ +#ifndef UTIL_HASH_H +#define UTIL_HASH_H + +#include <stdint.h> +#include <stddef.h> // size_t + +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) +#define HASH_LITTLE_ENDIAN 1 +#define HASH_BIG_ENDIAN 0 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ + __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) +#define HASH_LITTLE_ENDIAN 0 +#define HASH_BIG_ENDIAN 1 +#else +#define HASH_LITTLE_ENDIAN 0 +#define HASH_BIG_ENDIAN 0 +#endif + +#define hashsize(n) ((uint32_t) 1 << (n)) +#define hashmask(n) (hashsize (n) - 1) +#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) + +#define mix(a, b, c) \ + { \ + a -= c; \ + a ^= rot (c, 4); \ + c += b; \ + b -= a; \ + b ^= rot (a, 6); \ + a += c; \ + c -= b; \ + c ^= rot (b, 8); \ + b += a; \ + a -= c; \ + a ^= rot (c, 16); \ + c += b; \ + b -= a; \ + b ^= rot (a, 19); \ + a += c; \ + c -= b; \ + c ^= rot (b, 4); \ + b += a; \ + } + +#define final(a, b, c) \ + { \ + c ^= b; \ + c -= rot (b, 14); \ + a ^= c; \ + a -= rot (c, 11); \ + b ^= a; \ + b -= rot (a, 25); \ + c ^= b; \ + c -= rot (b, 16); \ + a ^= c; \ + a -= rot (c, 4); \ + b ^= a; \ + b -= rot (a, 14); \ + c ^= b; \ + c -= rot (b, 24); \ + } + +static inline uint32_t +hashlittle (const void *key, size_t length, uint32_t initval) +{ + uint32_t a, b, c; /* internal state */ + union + { + const void *ptr; + size_t i; + } u; /* needed for Mac Powerbook G4 */ + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t) length) + initval; + + u.ptr = key; + if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) + { + const uint32_t *k = (const uint32_t *) key; /* read 32-bit chunks */ + + /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) + */ + while (length > 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix (a, b, c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) + * block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticably faster for short strings (like English words). + */ +#ifndef VALGRIND + + switch (length) + { + case 12: + c += k[2]; + b += k[1]; + a += k[0]; + break; + case 11: + c += k[2] & 0xffffff; + b += k[1]; + a += k[0]; + break; + case 10: + c += k[2] & 0xffff; + b += k[1]; + a += k[0]; + break; + case 9: + c += k[2] & 0xff; + b += k[1]; + a += k[0]; + break; + case 8: + b += k[1]; + a += k[0]; + break; + case 7: + b += k[1] & 0xffffff; + a += k[0]; + break; + case 6: + b += k[1] & 0xffff; + a += k[0]; + break; + case 5: + b += k[1] & 0xff; + a += k[0]; + break; + case 4: + a += k[0]; + break; + case 3: + a += k[0] & 0xffffff; + break; + case 2: + a += k[0] & 0xffff; + break; + case 1: + a += k[0] & 0xff; + break; + case 0: + return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *) k; + switch (length) + { + case 12: + c += k[2]; + b += k[1]; + a += k[0]; + break; + case 11: + c += ((uint32_t) k8[10]) << 16; /* fall through */ + case 10: + c += ((uint32_t) k8[9]) << 8; /* fall through */ + case 9: + c += k8[8]; /* fall through */ + case 8: + b += k[1]; + a += k[0]; + break; + case 7: + b += ((uint32_t) k8[6]) << 16; /* fall through */ + case 6: + b += ((uint32_t) k8[5]) << 8; /* fall through */ + case 5: + b += k8[4]; /* fall through */ + case 4: + a += k[0]; + break; + case 3: + a += ((uint32_t) k8[2]) << 16; /* fall through */ + case 2: + a += ((uint32_t) k8[1]) << 8; /* fall through */ + case 1: + a += k8[0]; + break; + case 0: + return c; + } + +#endif /* !valgrind */ + } + else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) + { + const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing + */ + while (length > 12) + { + a += k[0] + (((uint32_t) k[1]) << 16); + b += k[2] + (((uint32_t) k[3]) << 16); + c += k[4] + (((uint32_t) k[5]) << 16); + mix (a, b, c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block + */ + k8 = (const uint8_t *) k; + switch (length) + { + case 12: + c += k[4] + (((uint32_t) k[5]) << 16); + b += k[2] + (((uint32_t) k[3]) << 16); + a += k[0] + (((uint32_t) k[1]) << 16); + break; + case 11: + c += ((uint32_t) k8[10]) << 16; /* fall through */ + case 10: + c += k[4]; + b += k[2] + (((uint32_t) k[3]) << 16); + a += k[0] + (((uint32_t) k[1]) << 16); + break; + case 9: + c += k8[8]; /* fall through */ + case 8: + b += k[2] + (((uint32_t) k[3]) << 16); + a += k[0] + (((uint32_t) k[1]) << 16); + break; + case 7: + b += ((uint32_t) k8[6]) << 16; /* fall through */ + case 6: + b += k[2]; + a += k[0] + (((uint32_t) k[1]) << 16); + break; + case 5: + b += k8[4]; /* fall through */ + case 4: + a += k[0] + (((uint32_t) k[1]) << 16); + break; + case 3: + a += ((uint32_t) k8[2]) << 16; /* fall through */ + case 2: + a += k[0]; + break; + case 1: + a += k8[0]; + break; + case 0: + return c; /* zero length requires no mixing */ + } + } + else + { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *) key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) + */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t) k[1]) << 8; + a += ((uint32_t) k[2]) << 16; + a += ((uint32_t) k[3]) << 24; + b += k[4]; + b += ((uint32_t) k[5]) << 8; + b += ((uint32_t) k[6]) << 16; + b += ((uint32_t) k[7]) << 24; + c += k[8]; + c += ((uint32_t) k[9]) << 8; + c += ((uint32_t) k[10]) << 16; + c += ((uint32_t) k[11]) << 24; + mix (a, b, c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) + */ + switch (length) /* all the case statements fall through */ + { + case 12: + c += ((uint32_t) k[11]) << 24; + case 11: + c += ((uint32_t) k[10]) << 16; + case 10: + c += ((uint32_t) k[9]) << 8; + case 9: + c += k[8]; + case 8: + b += ((uint32_t) k[7]) << 24; + case 7: + b += ((uint32_t) k[6]) << 16; + case 6: + b += ((uint32_t) k[5]) << 8; + case 5: + b += k[4]; + case 4: + a += ((uint32_t) k[3]) << 24; + case 3: + a += ((uint32_t) k[2]) << 16; + case 2: + a += ((uint32_t) k[1]) << 8; + case 1: + a += k[0]; + break; + case 0: + return c; + } + } + + final (a, b, c); + return c; +} + +/* Helpers */ + +#define HASH_INITVAL 1 +//#define hash(buf, len) (hash_t)hashlittle(buf, len, HASH_INITVAL) +#define hash(buf, len) hashlittle (buf, len, HASH_INITVAL) +#define hash_struct(buf) hash (buf, sizeof (*buf)) + +#define str_hash(str) (hash (str, strlen (str))) +#define str_hash_eq(a, b) (str_hash (b) - str_hash (a)) + +#endif /* UTIL_JENKINS_HASH_H */ diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h index 4facd9ad0..979efaa40 100644 --- a/lib/includes/hicn/util/ip_address.h +++ b/lib/includes/hicn/util/ip_address.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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: @@ -20,10 +20,9 @@ #ifndef UTIL_IP_ADDRESS_H #define UTIL_IP_ADDRESS_H - #ifdef __APPLE__ #include <libkern/OSByteOrder.h> -#define __bswap_constant_32(x) OSSwapInt32(x) +#define __bswap_constant_32(x) OSSwapInt32 (x) #include <machine/endian.h> #else #ifdef __ANDROID__ @@ -33,10 +32,11 @@ #endif #include <errno.h> +#include <assert.h> #ifndef _WIN32 -#include <netinet/in.h> // struct sockadd -#include <arpa/inet.h> // inet_ntop -#include <netdb.h> // struct addrinfo +#include <netinet/in.h> // struct sockadd +#include <arpa/inet.h> // inet_ntop +#include <netdb.h> // struct addrinfo #endif #include <stdbool.h> #include <stdlib.h> @@ -45,11 +45,12 @@ #include "types.h" -#define bytes_to_bits(x) (x * 8) -#define IPV6_ADDR_LEN 16 /* bytes */ -#define IPV4_ADDR_LEN 4 /* bytes */ -#define IPV6_ADDR_LEN_BITS bytes_to_bits(IPV6_ADDR_LEN) -#define IPV4_ADDR_LEN_BITS bytes_to_bits(IPV4_ADDR_LEN) +#define bytes_to_bits(x) (x * 8) +#define IPV6_ADDR_LEN 16 /* bytes */ +#define IPV4_ADDR_LEN 4 /* bytes */ +#define IPV6_ADDR_LEN_BITS bytes_to_bits (IPV6_ADDR_LEN) +#define IPV4_ADDR_LEN_BITS bytes_to_bits (IPV4_ADDR_LEN) +#define MAX_IPV6_PREFIX_LEN 128 /* Presentation format */ #ifndef INET_ADDRSTRLEN @@ -61,29 +62,47 @@ #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 -typedef union { - struct { - u32 pad[3]; - union { - struct in_addr as_inaddr; - u8 buffer[4]; - u8 as_u8[4]; - u16 as_u16[2]; - u32 as_u32; - } v4; - }; - union { - struct in6_addr as_in6addr; - u8 buffer[16]; - u8 as_u8[16]; - u16 as_u16[8]; - u32 as_u32[4]; - u64 as_u64[2]; - } v6; +typedef union +{ + struct in_addr as_inaddr; + u8 buffer[4]; + u8 as_u8[4]; + u16 as_u16[2]; + u32 as_u32; +} ipv4_address_t; + +static_assert (sizeof (ipv4_address_t) == 4, ""); + +typedef union __attribute__ ((__packed__)) +{ + struct in6_addr as_in6addr; + u8 buffer[16]; + u8 as_u8[16]; + u16 as_u16[8]; + u32 as_u32[4]; + u64 as_u64[2]; +} 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]; + ipv4_address_t v4; + }; + 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]; @@ -91,79 +110,117 @@ typedef union { u32 as_u32[IP_MAX_ADDR_LEN >> 2]; u64 as_u64[IP_MAX_ADDR_LEN >> 3]; #endif -} 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 hicn_ip_address_is_v4(ip) \ + (((ip)->pad[0] | (ip)->pad[1] | (ip)->pad[2]) == 0) #define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1 #define MAXSZ_IP6_ADDRESS_ INET6_ADDRSTRLEN - 1 -#define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_ -#define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1 -#define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1 -#define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1 +#define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_ +#define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1 +#define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1 +#define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1 -typedef struct { +#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_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3 -#define MAXSZ_PREFIX MAXSZ_PREFIX_ + 1 +#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 ip_address_t IP_ADDRESS_EMPTY; +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; -#define IP_ANY(family) (family == AF_INET) ? IPV4_ANY : IPV6_ANY +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 -#define MAX_PORT 1 << (8 * sizeof(u16)) -#define IS_VALID_PORT(x) ((x > 0) && ((int)x < MAX_PORT)) +#define MAX_PORT 1 << (8 * sizeof (u16)) +#define IS_VALID_PORT(x) ((x > 0) && ((int) x < MAX_PORT)) #define MAXSZ_PORT_ 5 -#define MAXSZ_PORT MAXSZ_PORT_ + 1 +#define MAXSZ_PORT MAXSZ_PORT_ + 1 #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); + +void hicn_ip_address_clear (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_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 */ #define MAXSZ_PROTO_ 8 /* inetX:// */ -#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM +#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM #define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_ #define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_ -#define MAXSZ_URL_ MAXSZ_URL6_ -#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM -#define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM -#define MAXSZ_URL MAXSZ_URL_ + NULLTERM +#define MAXSZ_URL_ MAXSZ_URL6_ +#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM +#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/khash.h b/lib/includes/hicn/util/khash.h new file mode 100644 index 000000000..17401091f --- /dev/null +++ b/lib/includes/hicn/util/khash.h @@ -0,0 +1,826 @@ +/* The MIT License + + Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk> + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +/* + An example: + +#include "khash.h" +KHASH_MAP_INIT_INT(32, char) +int main() { + int ret, is_missing; + khiter_t k; + khash_t(32) *h = kh_init(32); + k = kh_put(32, h, 5, &ret); + kh_value(h, k) = 10; + k = kh_get(32, h, 10); + is_missing = (k == kh_end(h)); + k = kh_get(32, h, 5); + kh_del(32, h, k); + for (k = kh_begin(h); k != kh_end(h); ++k) + if (kh_exist(h, k)) kh_value(h, k) = 1; + kh_destroy(32, h); + return 0; +} +*/ + +/* + 2013-05-02 (0.2.8): + + * Use quadratic probing. When the capacity is power of 2, stepping + function i*(i+1)/2 guarantees to traverse each bucket. It is better than + double hashing on cache performance and is more robust than linear probing. + + In theory, double hashing should be more robust than quadratic + probing. However, my implementation is probably not for large hash tables, + because the second hash function is closely tied to the first hash function, + which reduce the effectiveness of double hashing. + + Reference: http://research.cs.vt.edu/AVresearch/hashing/quadratic.php + + 2011-12-29 (0.2.7): + + * Minor code clean up; no actual effect. + + 2011-09-16 (0.2.6): + + * The capacity is a power of 2. This seems to dramatically improve the + speed for simple keys. Thank Zilong Tan for the suggestion. + Reference: + + - http://code.google.com/p/ulib/ + - http://nothings.org/computer/judy/ + + * Allow to optionally use linear probing which usually has better + performance for random input. Double hashing is still the default as + it is more robust to certain non-random input. + + * Added Wang's integer hash function (not used by default). This hash + function is more robust to certain non-random input. + + 2011-02-14 (0.2.5): + + * Allow to declare global functions. + + 2009-09-26 (0.2.4): + + * Improve portability + + 2008-09-19 (0.2.3): + + * Corrected the example + * Improved interfaces + + 2008-09-11 (0.2.2): + + * Improved speed a little in kh_put() + + 2008-09-10 (0.2.1): + + * Added kh_clear() + * Fixed a compiling error + + 2008-09-02 (0.2.0): + + * Changed to token concatenation which increases flexibility. + + 2008-08-31 (0.1.2): + + * Fixed a bug in kh_get(), which has not been tested previously. + + 2008-08-31 (0.1.1): + + * Added destructor +*/ + +#ifndef __AC_KHASH_H +#define __AC_KHASH_H + +/*! + @header + + Generic hash table library. + */ + +#define AC_VERSION_KHASH_H "0.2.8" + +#include <stdlib.h> +#include <string.h> +#include <limits.h> + +/* compiler specific configuration */ + +#if UINT_MAX == 0xffffffffu +typedef unsigned int khint32_t; +#elif ULONG_MAX == 0xffffffffu +typedef unsigned long khint32_t; +#endif + +#if ULONG_MAX == ULLONG_MAX +typedef unsigned long khint64_t; +#else +typedef unsigned long long khint64_t; +#endif + +#ifndef kh_inline +#ifdef _MSC_VER +#define kh_inline __inline +#else +#define kh_inline inline +#endif +#endif /* kh_inline */ + +#ifndef klib_unused +#if (defined __clang__ && __clang_major__ >= 3) || \ + (defined __GNUC__ && __GNUC__ >= 3) +#define klib_unused __attribute__ ((__unused__)) +#else +#define klib_unused +#endif +#endif /* klib_unused */ + +typedef khint32_t khint_t; +typedef khint_t khiter_t; + +#define __ac_isempty(flag, i) ((flag[i >> 4] >> ((i & 0xfU) << 1)) & 2) +#define __ac_isdel(flag, i) ((flag[i >> 4] >> ((i & 0xfU) << 1)) & 1) +#define __ac_iseither(flag, i) ((flag[i >> 4] >> ((i & 0xfU) << 1)) & 3) +#define __ac_set_isdel_false(flag, i) \ + (flag[i >> 4] &= ~(1ul << ((i & 0xfU) << 1))) +#define __ac_set_isempty_false(flag, i) \ + (flag[i >> 4] &= ~(2ul << ((i & 0xfU) << 1))) +#define __ac_set_isboth_false(flag, i) \ + (flag[i >> 4] &= ~(3ul << ((i & 0xfU) << 1))) +#define __ac_set_isdel_true(flag, i) (flag[i >> 4] |= 1ul << ((i & 0xfU) << 1)) + +#define __ac_fsize(m) ((m) < 16 ? 1 : (m) >> 4) + +#ifndef kroundup32 +#define kroundup32(x) \ + (--(x), (x) |= (x) >> 1, (x) |= (x) >> 2, (x) |= (x) >> 4, (x) |= (x) >> 8, \ + (x) |= (x) >> 16, ++(x)) +#endif + +#ifndef kcalloc +#define kcalloc(N, Z) calloc (N, Z) +#endif +#ifndef kmalloc +#define kmalloc(Z) malloc (Z) +#endif +#ifndef krealloc +#define krealloc(P, Z) realloc (P, Z) +#endif +#ifndef kfree +#define kfree(P) free (P) +#endif + +static const double __ac_HASH_UPPER = 0.77; + +#define __KHASH_TYPE(name, khkey_t, khval_t) \ + typedef struct kh_##name##_s \ + { \ + khint_t n_buckets, size, n_occupied, upper_bound; \ + khint32_t *flags; \ + khkey_t *keys; \ + khval_t *vals; \ + } kh_##name##_t; + +#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ + extern kh_##name##_t *kh_init_##name (void); \ + extern void kh_destroy_##name (kh_##name##_t *h); \ + extern void kh_clear_##name (kh_##name##_t *h); \ + extern khint_t kh_get_##name (const kh_##name##_t *h, khkey_t key); \ + extern int kh_resize_##name (kh_##name##_t *h, khint_t new_n_buckets); \ + extern khint_t kh_put_##name (kh_##name##_t *h, khkey_t key, int *ret); \ + extern void kh_del_##name (kh_##name##_t *h, khint_t x); + +#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, \ + __hash_equal) \ + SCOPE kh_##name##_t *kh_init_##name (void) \ + { \ + return (kh_##name##_t *) kcalloc (1, sizeof (kh_##name##_t)); \ + } \ + SCOPE void kh_destroy_##name (kh_##name##_t *h) \ + { \ + if (h) \ + { \ + kfree ((void *) h->keys); \ + kfree (h->flags); \ + kfree ((void *) h->vals); \ + kfree (h); \ + } \ + } \ + SCOPE void kh_clear_##name (kh_##name##_t *h) \ + { \ + if (h && h->flags) \ + { \ + memset (h->flags, 0xaa, \ + __ac_fsize (h->n_buckets) * sizeof (khint32_t)); \ + h->size = h->n_occupied = 0; \ + } \ + } \ + SCOPE khint_t kh_get_##name (const kh_##name##_t *h, khkey_t key) \ + { \ + if (h->n_buckets) \ + { \ + khint_t k, i, last, mask, step = 0; \ + mask = h->n_buckets - 1; \ + k = __hash_func (key); \ + i = k & mask; \ + last = i; \ + while (!__ac_isempty (h->flags, i) && \ + (__ac_isdel (h->flags, i) || !__hash_equal (h->keys[i], key))) \ + { \ + i = (i + (++step)) & mask; \ + if (i == last) \ + return h->n_buckets; \ + } \ + return __ac_iseither (h->flags, i) ? h->n_buckets : i; \ + } \ + else \ + return 0; \ + } \ + SCOPE int kh_resize_##name (kh_##name##_t *h, khint_t new_n_buckets) \ + { /* This function uses 0.25*n_buckets bytes of \ + working space instead of \ + [sizeof(key_t+val_t)+.25]*n_buckets. */ \ + khint32_t *new_flags = 0; \ + khint_t j = 1; \ + { \ + kroundup32 (new_n_buckets); \ + if (new_n_buckets < 4) \ + new_n_buckets = 4; \ + if (h->size >= (khint_t) (new_n_buckets * __ac_HASH_UPPER + 0.5)) \ + j = 0; /* requested size is too small */ \ + else \ + { /* hash table size to be changed (shrink or expand); rehash */ \ + new_flags = (khint32_t *) kmalloc (__ac_fsize (new_n_buckets) * \ + sizeof (khint32_t)); \ + if (!new_flags) \ + return -1; \ + memset (new_flags, 0xaa, \ + __ac_fsize (new_n_buckets) * sizeof (khint32_t)); \ + if (h->n_buckets < new_n_buckets) \ + { /* expand */ \ + khkey_t *new_keys = (khkey_t *) krealloc ( \ + (void *) h->keys, new_n_buckets * sizeof (khkey_t)); \ + if (!new_keys) \ + { \ + kfree (new_flags); \ + return -1; \ + } \ + h->keys = new_keys; \ + if (kh_is_map) \ + { \ + khval_t *new_vals = (khval_t *) krealloc ( \ + (void *) h->vals, new_n_buckets * sizeof (khval_t)); \ + if (!new_vals) \ + { \ + kfree (new_flags); \ + return -1; \ + } \ + h->vals = new_vals; \ + } \ + } /* otherwise shrink */ \ + } \ + } \ + if (j) \ + { /* rehashing is needed */ \ + for (j = 0; j != h->n_buckets; ++j) \ + { \ + if (__ac_iseither (h->flags, j) == 0) \ + { \ + khkey_t key = h->keys[j]; \ + khval_t val; \ + khint_t new_mask; \ + new_mask = new_n_buckets - 1; \ + if (kh_is_map) \ + val = h->vals[j]; \ + __ac_set_isdel_true (h->flags, j); \ + while (1) \ + { /* kick-out process; sort of like in Cuckoo hashing */ \ + khint_t k, i, step = 0; \ + k = __hash_func (key); \ + i = k & new_mask; \ + while (!__ac_isempty (new_flags, i)) \ + i = (i + (++step)) & new_mask; \ + __ac_set_isempty_false (new_flags, i); \ + if (i < h->n_buckets && __ac_iseither (h->flags, i) == 0) \ + { /* kick out the existing element */ \ + { \ + khkey_t tmp = h->keys[i]; \ + h->keys[i] = key; \ + key = tmp; \ + } \ + if (kh_is_map) \ + { \ + khval_t tmp = h->vals[i]; \ + h->vals[i] = val; \ + val = tmp; \ + } \ + __ac_set_isdel_true ( \ + h->flags, \ + i); /* mark it as deleted in the old hash table */ \ + } \ + else \ + { /* write the element and jump out of the loop */ \ + h->keys[i] = key; \ + if (kh_is_map) \ + h->vals[i] = val; \ + break; \ + } \ + } \ + } \ + } \ + if (h->n_buckets > new_n_buckets) \ + { /* shrink the hash table */ \ + h->keys = (khkey_t *) krealloc ( \ + (void *) h->keys, new_n_buckets * sizeof (khkey_t)); \ + if (kh_is_map) \ + h->vals = (khval_t *) krealloc ( \ + (void *) h->vals, new_n_buckets * sizeof (khval_t)); \ + } \ + kfree (h->flags); /* free the working space */ \ + h->flags = new_flags; \ + h->n_buckets = new_n_buckets; \ + h->n_occupied = h->size; \ + h->upper_bound = (khint_t) (h->n_buckets * __ac_HASH_UPPER + 0.5); \ + } \ + return 0; \ + } \ + SCOPE khint_t kh_put_##name (kh_##name##_t *h, khkey_t key, int *ret) \ + { \ + khint_t x; \ + if (h->n_occupied >= h->upper_bound) \ + { /* update the hash table */ \ + if (h->n_buckets > (h->size << 1)) \ + { \ + if (kh_resize_##name (h, h->n_buckets - 1) < 0) \ + { /* clear "deleted" elements */ \ + *ret = -1; \ + return h->n_buckets; \ + } \ + } \ + else if (kh_resize_##name (h, h->n_buckets + 1) < 0) \ + { /* expand the hash table */ \ + *ret = -1; \ + return h->n_buckets; \ + } \ + } /* TODO: to implement automatically shrinking; resize() already \ + support shrinking */ \ + { \ + khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ + x = site = h->n_buckets; \ + k = __hash_func (key); \ + i = k & mask; \ + if (__ac_isempty (h->flags, i)) \ + x = i; /* for speed up */ \ + else \ + { \ + last = i; \ + while ( \ + !__ac_isempty (h->flags, i) && \ + (__ac_isdel (h->flags, i) || !__hash_equal (h->keys[i], key))) \ + { \ + if (__ac_isdel (h->flags, i)) \ + site = i; \ + i = (i + (++step)) & mask; \ + if (i == last) \ + { \ + x = site; \ + break; \ + } \ + } \ + if (x == h->n_buckets) \ + { \ + if (__ac_isempty (h->flags, i) && site != h->n_buckets) \ + x = site; \ + else \ + x = i; \ + } \ + } \ + } \ + if (__ac_isempty (h->flags, x)) \ + { /* not present at all */ \ + h->keys[x] = key; \ + __ac_set_isboth_false (h->flags, x); \ + ++h->size; \ + ++h->n_occupied; \ + *ret = 1; \ + } \ + else if (__ac_isdel (h->flags, x)) \ + { /* deleted */ \ + h->keys[x] = key; \ + __ac_set_isboth_false (h->flags, x); \ + ++h->size; \ + *ret = 2; \ + } \ + else \ + *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ + return x; \ + } \ + SCOPE void kh_del_##name (kh_##name##_t *h, khint_t x) \ + { \ + if (x != h->n_buckets && !__ac_iseither (h->flags, x)) \ + { \ + __ac_set_isdel_true (h->flags, x); \ + --h->size; \ + } \ + } + +#define KHASH_DECLARE(name, khkey_t, khval_t) \ + __KHASH_TYPE (name, khkey_t, khval_t) \ + __KHASH_PROTOTYPES (name, khkey_t, khval_t) + +#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, \ + __hash_equal) \ + __KHASH_TYPE (name, khkey_t, khval_t) \ + __KHASH_IMPL (name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, \ + __hash_equal) + +#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, \ + __hash_equal) \ + KHASH_INIT2 (name, static kh_inline klib_unused, khkey_t, khval_t, \ + kh_is_map, __hash_func, __hash_equal) + +/* --- BEGIN OF HASH FUNCTIONS --- */ + +/*! @function + @abstract Integer hash function + @param key The integer [khint32_t] + @return The hash value [khint_t] + */ +#define kh_int_hash_func(key) (khint32_t) (key) +/*! @function + @abstract Integer comparison function + */ +#define kh_int_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract 64-bit integer hash function + @param key The integer [khint64_t] + @return The hash value [khint_t] + */ +#define kh_int64_hash_func(key) (khint32_t) ((key) >> 33 ^ (key) ^ (key) << 11) +/*! @function + @abstract 64-bit integer comparison function + */ +#define kh_int64_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract const char* hash function + @param s Pointer to a null terminated string + @return The hash value + */ +static kh_inline khint_t +__ac_X31_hash_string (const char *s) +{ + khint_t h = (khint_t) *s; + if (h) + for (++s; *s; ++s) + h = (h << 5) - h + (khint_t) *s; + return h; +} +/*! @function + @abstract Another interface to const char* hash function + @param key Pointer to a null terminated string [const char*] + @return The hash value [khint_t] + */ +#define kh_str_hash_func(key) __ac_X31_hash_string (key) +/*! @function + @abstract Const char* comparison function + */ +#define kh_str_hash_equal(a, b) (strcmp (a, b) == 0) + +static kh_inline khint_t +__ac_Wang_hash (khint_t key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} +#define kh_int_hash_func2(key) __ac_Wang_hash ((khint_t) key) + +/* --- END OF HASH FUNCTIONS --- */ + +/* Other convenient macros... */ + +/*! + @abstract Type of the hash table. + @param name Name of the hash table [symbol] + */ +#define khash_t(name) kh_##name##_t + +/*! @function + @abstract Initiate a hash table. + @param name Name of the hash table [symbol] + @return Pointer to the hash table [khash_t(name)*] + */ +#define kh_init(name) kh_init_##name () + +/*! @function + @abstract Destroy a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_destroy(name, h) kh_destroy_##name (h) + +/*! @function + @abstract Reset a hash table without deallocating memory. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_clear(name, h) kh_clear_##name (h) + +/*! @function + @abstract Resize a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param s New size [khint_t] + */ +#define kh_resize(name, h, s) kh_resize_##name (h, s) + +/*! @function + @abstract Insert a key to the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @param r Extra return code: -1 if the operation failed; + 0 if the key is present in the hash table; + 1 if the bucket is empty (never used); 2 if the element in + the bucket has been deleted [int*] + @return Iterator to the inserted element [khint_t] + */ +#define kh_put(name, h, k, r) kh_put_##name (h, k, r) + +/*! @function + @abstract Retrieve a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @return Iterator to the found element, or kh_end(h) if the element is + absent [khint_t] + */ +#define kh_get(name, h, k) kh_get_##name (h, k) + +/*! @function + @abstract Remove a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Iterator to the element to be deleted [khint_t] + */ +#define kh_del(name, h, k) kh_del_##name (h, k) + +/*! @function + @abstract Test whether a bucket contains data. + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return 1 if containing data; 0 otherwise [int] + */ +#define kh_exist(h, x) (!__ac_iseither ((h)->flags, (x))) + +/*! @function + @abstract Get key given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Key [type of keys] + */ +#define kh_key(h, x) ((h)->keys[x]) + +/*! @function + @abstract Get value given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Value [type of values] + @discussion For hash sets, calling this results in segfault. + */ +#define kh_val(h, x) ((h)->vals[x]) + +/*! @function + @abstract Alias of kh_val() + */ +#define kh_value(h, x) ((h)->vals[x]) + +/*! @function + @abstract Get the start iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The start iterator [khint_t] + */ +#define kh_begin(h) (khint_t) (0) + +/*! @function + @abstract Get the end iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The end iterator [khint_t] + */ +#define kh_end(h) ((h)->n_buckets) + +/*! @function + @abstract Get the number of elements in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of elements in the hash table [khint_t] + */ +#define kh_size(h) ((h)->size) + +/*! @function + @abstract Get the number of buckets in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of buckets in the hash table [khint_t] + */ +#define kh_n_buckets(h) ((h)->n_buckets) + +/*! @function + @abstract Iterate over the entries in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param kvar Variable to which key will be assigned + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach(h, kvar, vvar, code) \ + { \ + khint_t __i; \ + for (__i = kh_begin (h); __i != kh_end (h); ++__i) \ + { \ + if (!kh_exist (h, __i)) \ + continue; \ + (kvar) = kh_key (h, __i); \ + (vvar) = kh_val (h, __i); \ + code; \ + } \ + } + +/*! @function + @abstract Iterate over the values in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach_value(h, vvar, code) \ + { \ + khint_t __i; \ + for (__i = kh_begin (h); __i != kh_end (h); ++__i) \ + { \ + if (!kh_exist (h, __i)) \ + continue; \ + (vvar) = kh_val (h, __i); \ + code; \ + } \ + } + +/* More convenient interfaces */ + +/*! @function + @abstract Instantiate a hash set containing integer keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_INT(name) \ + KHASH_INIT (name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_INT(name, khval_t) \ + KHASH_INIT (name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash set containing 64-bit integer keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_INT64(name) \ + KHASH_INIT (name, khint64_t, char, 0, kh_int64_hash_func, \ + kh_int64_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing 64-bit integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_INT64(name, khval_t) \ + KHASH_INIT (name, khint64_t, khval_t, 1, kh_int64_hash_func, \ + kh_int64_hash_equal) + +typedef const char *kh_cstr_t; +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] + */ +#define KHASH_SET_INIT_STR(name) \ + KHASH_INIT (name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] + */ +#define KHASH_MAP_INIT_STR(name, khval_t) \ + KHASH_INIT (name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal) + +/****************************************************************************** + * Custom return codes + ******************************************************************************/ + +// RESET: same as added, but the key was already added in the past +#define foreach_kh_rc \ + _ (REPLACED) \ + _ (ADDED) \ + _ (RESET) \ + _ (NOT_FOUND) \ + _ (FOUND) \ + _ (FAIL) + +typedef enum +{ +#define _(x) KH_##x, + foreach_kh_rc +#undef _ +} kh_rc; + +/****************************************************************************** + * Custom + *high-level interface + ******************************************************************************/ + +#define _kh_var(x) _kh_var_##x + +/** + * @brief Return the value corresponding to a key in the hashtable. + * @return The value associated with the key or null if not found + */ +#define kh_get_val(kname, hashtable, key, default_val) \ + ({ \ + khiter_t _kh_var (k) = kh_get (kname, hashtable, key); \ + (_kh_var (k) != kh_end (hashtable) ? kh_val (hashtable, _kh_var (k)) : \ + default_val); \ + }) + +/** + * @brief Add key/value pair in the hashtable. + * @return 0 if an existing value (corresponding to the provided key) + * has been replaced; 1 if a new key/value pair has been added + * (the key was not already present in the hash table); + * 2 if a new key/value pair has been added in correspondence + * of a key previously deleted key + */ +#define kh_put_val(kname, hashtable, key, val) \ + ({ \ + int _kh_var (ret); \ + khiter_t _kh_var (k) = kh_put (kname, hashtable, key, &_kh_var (ret)); \ + kh_value (hashtable, _kh_var (k)) = val; \ + _kh_var (ret); \ + }) + +/** + * @brief Remove a key/value pair from the hashtable. + * @return void + */ +#define kh_remove_val(kname, hashtable, key) \ + ({ \ + khiter_t _kh_var (k) = kh_get (kname, hashtable, key); \ + if (_kh_var (k) != kh_end (hashtable)) \ + { \ + free ((void *) kh_key (hashtable, _kh_var (k))); \ + kh_del (kname, hashtable, _kh_var (k)); \ + } \ + }) + +/** + * @brief Free the hashtable. + * @return void + */ +#define kh_free(kname, hashtable) \ + ({ \ + const void *_kh_var (key); \ + unsigned _kh_var (val); \ + (void) _kh_var (val); \ + \ + kh_foreach (hashtable, _kh_var (key), _kh_var (val), { \ + free ((void *) _kh_var (key)); \ + }) kh_destroy (kname, hashtable); \ + }) + +#endif /* __AC_KHASH_H */ diff --git a/lib/includes/hicn/util/log.h b/lib/includes/hicn/util/log.h index 6763d464f..b9b7725d6 100644 --- a/lib/includes/hicn/util/log.h +++ b/lib/includes/hicn/util/log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -17,53 +17,72 @@ #define UTIL_LOG_H #include <stdarg.h> // va_* -#include <stdio.h> // FILE -#include <time.h> // time, localtime +#include <stdio.h> // FILE +#include <time.h> // time, localtime -#define LOG_FATAL 0 -#define LOG_ERROR 1 -#define LOG_WARN 2 -#define LOG_INFO 3 -#define LOG_DEBUG 4 -#define LOG_TRACE 5 +typedef enum +{ + LOG_FATAL, + LOG_ERROR, + LOG_WARN, + LOG_INFO, + LOG_DEBUG, + LOG_TRACE +} log_level_t; -typedef struct { +typedef struct +{ int log_level; int debug; - FILE * log_file; + FILE *log_file; } log_conf_t; -#define DEFAULT_LOG_CONF { \ - .log_level = LOG_INFO, \ - .debug = 0, \ - .log_file = NULL, \ -}; +#define DEFAULT_LOG_CONF \ + { \ + .log_level = LOG_INFO, \ + .debug = 0, \ + .log_file = NULL, \ + }; extern log_conf_t log_conf; -#define WITH_DEBUG(BLOCK) \ - if (log_conf.log_level >= LOG_DEBUG) \ - BLOCK +#define WITH_ERROR(BLOCK) \ + if (log_conf.log_level >= LOG_ERROR) \ + BLOCK +#define WITH_WARN(BLOCK) \ + if (log_conf.log_level >= LOG_WARN) \ + BLOCK +#define WITH_INFO(BLOCK) \ + if (log_conf.log_level >= LOG_INFO) \ + BLOCK +#define WITH_DEBUG(BLOCK) \ + if (log_conf.log_level >= LOG_DEBUG) \ + BLOCK +#define WITH_TRACE(BLOCK) \ + if (log_conf.log_level >= LOG_TRACE) \ + BLOCK -#define FATAL(fmt, ...) (_log(LOG_FATAL, fmt, ##__VA_ARGS__ )) +#define FATAL(fmt, ...) (_log (LOG_FATAL, fmt, ##__VA_ARGS__)) #ifdef ERROR #undef ERROR #endif -#define ERROR(fmt, ...) (_log(LOG_ERROR, fmt, ##__VA_ARGS__ )) -#define WARN(fmt, ...) (_log(LOG_WARN, fmt, ##__VA_ARGS__ )) -#define INFO(fmt, ...) (_log(LOG_INFO, fmt, ##__VA_ARGS__ )) -#define DEBUG(fmt, ...) (_log(LOG_DEBUG, fmt, ##__VA_ARGS__ )) -#define TRACE(fmt, ...) (_log(LOG_TRACE, fmt, ##__VA_ARGS__ )) +#define ERROR(fmt, ...) (_log (LOG_ERROR, fmt, ##__VA_ARGS__)) +#define WARN(fmt, ...) (_log (LOG_WARN, fmt, ##__VA_ARGS__)) +#define INFO(fmt, ...) (_log (LOG_INFO, fmt, ##__VA_ARGS__)) +#define DEBUG(fmt, ...) (_log (LOG_DEBUG, fmt, ##__VA_ARGS__)) +#define TRACE(fmt, ...) (_log (LOG_TRACE, fmt, ##__VA_ARGS__)) -void _log_va(int level, const char *fmt, va_list ap); +void _log_va (int level, const char *fmt, va_list ap); -void _log(int level, const char *fmt, ...); +void _log (int level, const char *fmt, ...); -void fatal(char *fmt, ...); +void fatal (char *fmt, ...); + +int loglevel_from_str (const char *loglevel); #ifdef HAVE_BACKTRACE #include <execinfo.h> -void print_trace(void); +void print_trace (void); #endif #endif // UTIL_LOG_H diff --git a/lib/includes/hicn/util/map.h b/lib/includes/hicn/util/map.h index 01195865e..6e23f222f 100644 --- a/lib/includes/hicn/util/map.h +++ b/lib/includes/hicn/util/map.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -20,231 +20,227 @@ #include "set.h" -#define ERR_MAP_EXISTS -2 +#define ERR_MAP_EXISTS -2 #define ERR_MAP_NOT_FOUND -3 -#define TYPEDEF_MAP_H(NAME, KEY_T, VAL_T) \ - \ -typedef struct { \ - KEY_T key; \ - VAL_T value; \ -} NAME ## _pair_t; \ - \ -NAME ## _pair_t * NAME ## _pair_create(KEY_T key, VAL_T value); \ - \ -void NAME ## _pair_free(NAME ## _pair_t * pair); \ - \ -int NAME ## _pair_cmp(const NAME ## _pair_t * p1, const NAME ## _pair_t * p2); \ - \ -TYPEDEF_SET_H(NAME ## _pair_set, NAME ## _pair_t *) \ - \ -typedef struct NAME ## _s { \ - NAME ## _pair_set_t pair_set; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * map); \ - \ -int NAME ## _finalize(NAME ## _t * map); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * map); \ - \ -int NAME ## _add(NAME ## _t * map, KEY_T key, VAL_T value); \ - \ -int NAME ## _remove(NAME ## _t * map, KEY_T key, VAL_T * value); \ - \ -int NAME ## _get(NAME ## _t * map, KEY_T key, VAL_T * value); \ - \ -void NAME ## _dump(NAME ## _t * map); +#define TYPEDEF_MAP_H(NAME, KEY_T, VAL_T) \ + \ + typedef struct \ + { \ + KEY_T key; \ + VAL_T value; \ + } NAME##_pair_t; \ + \ + NAME##_pair_t *NAME##_pair_create (KEY_T key, VAL_T value); \ + \ + void NAME##_pair_free (NAME##_pair_t *pair); \ + \ + int NAME##_pair_cmp (const NAME##_pair_t *p1, const NAME##_pair_t *p2); \ + \ + TYPEDEF_SET_H (NAME##_pair_set, NAME##_pair_t *) \ + \ + typedef struct NAME##_s \ + { \ + NAME##_pair_set_t pair_set; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *map); \ + \ + int NAME##_finalize (NAME##_t *map); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *map); \ + \ + int NAME##_add (NAME##_t *map, KEY_T key, VAL_T value); \ + \ + int NAME##_remove (NAME##_t *map, KEY_T key, VAL_T *value); \ + \ + int NAME##_get (NAME##_t *map, KEY_T key, VAL_T *value); \ + \ + void NAME##_dump (NAME##_t *map); \ + \ + int NAME##_get_key_array (NAME##_t *map, KEY_T **array); \ + \ + int NAME##_get_value_array (NAME##_t *map, VAL_T **array); - - - -#define TYPEDEF_MAP(NAME, KEY_T, VAL_T, CMP, KEY_SNPRINTF, VALUE_SNPRINTF) \ - \ -NAME ## _pair_t * NAME ## _pair_create(KEY_T key, VAL_T value) \ -{ \ - /* Create pair */ \ - NAME ## _pair_t * pair = malloc(sizeof(NAME ## _pair_t)); \ - if (!pair) \ - return NULL; \ - \ - pair->key = key; \ - pair->value = value; \ - \ - return pair; \ -} \ - \ -void NAME ## _pair_free(NAME ## _pair_t * pair) \ -{ \ - free(pair); \ -} \ - \ -int \ -NAME ## _pair_cmp(const NAME ## _pair_t * p1, const NAME ## _pair_t * p2) \ -{ \ - return (CMP(p1->key, p2->key)); \ -} \ - \ -int \ -NAME ## _pair_snprintf(char * buf, size_t size, const NAME ## _pair_t * pair) { \ - int rc; \ - rc = KEY_SNPRINTF(buf, BUFSIZE/2, (KEY_T)pair->key); \ - if (rc < 0) \ - return rc; \ - rc = VALUE_SNPRINTF(buf+rc, BUFSIZE/2, (VAL_T)pair->value); \ - return (int)rc; \ -} \ - \ -TYPEDEF_SET(NAME ## _pair_set, NAME ## _pair_t *, NAME ## _pair_cmp, NAME ## _pair_snprintf); \ - \ -int \ -NAME ## _initialize(NAME ## _t * map) \ -{ \ - return NAME ## _pair_set_initialize(&map->pair_set); \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * map) \ -{ \ - NAME ## _pair_t ** array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &array); \ - if (n < 0) \ - return -1; \ - for (unsigned i = 0; i < n; i++) { \ - NAME ## _pair_t * pair = array[i]; \ - NAME ## _pair_set_remove(&map->pair_set, pair, NULL); \ - NAME ## _pair_free(pair); \ - } \ - free(array); \ - return NAME ## _pair_set_finalize(&map->pair_set); \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * map = malloc(sizeof(NAME ## _t)); \ - if (!map) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(map) < 0) \ - goto ERR_INITIALIZE; \ - \ - return map; \ - \ -ERR_INITIALIZE: \ - free(map); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * map) \ -{ \ - NAME ## _finalize(map); \ - free(map); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * map, KEY_T key, VAL_T value) \ -{ \ - int rc; \ - NAME ## _pair_t * found = NULL; \ - \ - NAME ## _pair_t * pair = NAME ## _pair_create(key, value); \ - if (!pair) \ - return -1; \ - \ - rc = NAME ## _pair_set_get(&map->pair_set, pair, &found); \ - if (rc < 0) \ - return -1; \ - if (found) { \ - NAME ## _pair_free(pair); \ - return ERR_MAP_EXISTS; \ - } \ - \ - rc = NAME ## _pair_set_add(&map->pair_set, pair); \ - if (rc < 0) { \ - NAME ## _pair_free(pair); \ - return -1; \ - } \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * map, KEY_T key, VAL_T * value) \ -{ \ - NAME ## _pair_t * found = NULL; \ - NAME ## _pair_t search = { .key = key }; \ - int rc = NAME ## _pair_set_remove(&map->pair_set, &search, &found); \ - if (rc < 0) \ - return ERR_MAP_NOT_FOUND; \ - if (value) \ - *value = found->value; \ - NAME ## _pair_free(found); \ - return 0; \ -} \ - \ -int \ -NAME ## _get(NAME ## _t * map, KEY_T key, VAL_T * value) \ -{ \ - NAME ## _pair_t * found = NULL, search = { .key = key }; \ - int rc = NAME ## _pair_set_get(&map->pair_set, &search, &found); \ - if (rc < 0) \ - return -1; \ - if (found) \ - *value = found->value; \ - return 0; \ -} \ - \ -void \ -NAME ## _dump(NAME ## _t * map) { \ - NAME ## _pair_set_dump(&map->pair_set); \ -} \ - \ -int \ -NAME ## _get_key_array(NAME ## _t * map, KEY_T **array) { \ - NAME ## _pair_t ** pair_array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &pair_array); \ - if (n < 0) \ - return -1; \ - if (!array) \ - goto END; \ - /* Allocate result array */ \ - *array = malloc(n * sizeof(KEY_T)); \ - if (!array) { \ - free(pair_array); \ - return -1; \ - } \ - /* Copy keys */ \ - for (int i = 0; i < n; i++) \ - (*array)[i] = pair_array[i]->key; \ - free(pair_array); \ -END: \ - return n; \ -} \ - \ -int \ -NAME ## _get_value_array(NAME ## _t * map, VAL_T **array) { \ - NAME ## _pair_t ** pair_array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &pair_array); \ - if (n < 0) \ - return -1; \ - if (!array) \ - goto END; \ - /* Allocate result array */ \ - *array = malloc(n * sizeof(VAL_T)); \ - if (!array) { \ - free(pair_array); \ - return -1; \ - } \ - /* Copy values */ \ - for (int i = 0; i < n; i++) \ - (*array)[i] = pair_array[i]->value; \ - free(pair_array); \ -END: \ - return n; \ -} +#define TYPEDEF_MAP(NAME, KEY_T, VAL_T, CMP, KEY_SNPRINTF, VALUE_SNPRINTF) \ + \ + NAME##_pair_t *NAME##_pair_create (KEY_T key, VAL_T value) \ + { \ + /* Create pair */ \ + NAME##_pair_t *pair = malloc (sizeof (NAME##_pair_t)); \ + if (!pair) \ + return NULL; \ + \ + pair->key = key; \ + pair->value = value; \ + \ + return pair; \ + } \ + \ + void NAME##_pair_free (NAME##_pair_t *pair) { free (pair); } \ + \ + int NAME##_pair_cmp (const NAME##_pair_t *p1, const NAME##_pair_t *p2) \ + { \ + return (CMP (p1->key, p2->key)); \ + } \ + \ + int NAME##_pair_snprintf (char *buf, size_t size, \ + const NAME##_pair_t *pair) \ + { \ + int rc; \ + rc = KEY_SNPRINTF (buf, BUFSIZE / 2, (KEY_T) pair->key); \ + if (rc < 0) \ + return rc; \ + rc = VALUE_SNPRINTF (buf + rc, BUFSIZE / 2, (VAL_T) pair->value); \ + return (int) rc; \ + } \ + \ + TYPEDEF_SET (NAME##_pair_set, NAME##_pair_t *, NAME##_pair_cmp, \ + NAME##_pair_snprintf); \ + \ + int NAME##_initialize (NAME##_t *map) \ + { \ + return NAME##_pair_set_initialize (&map->pair_set); \ + } \ + \ + int NAME##_finalize (NAME##_t *map) \ + { \ + NAME##_pair_t **array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &array); \ + if (n < 0) \ + return -1; \ + for (unsigned i = 0; i < n; i++) \ + { \ + NAME##_pair_t *pair = array[i]; \ + NAME##_pair_set_remove (&map->pair_set, pair, NULL); \ + NAME##_pair_free (pair); \ + } \ + free (array); \ + return NAME##_pair_set_finalize (&map->pair_set); \ + } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *map = malloc (sizeof (NAME##_t)); \ + if (!map) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (map) < 0) \ + goto ERR_INITIALIZE; \ + \ + return map; \ + \ + ERR_INITIALIZE: \ + free (map); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *map) \ + { \ + NAME##_finalize (map); \ + free (map); \ + } \ + \ + int NAME##_add (NAME##_t *map, KEY_T key, VAL_T value) \ + { \ + int rc; \ + NAME##_pair_t *found = NULL; \ + \ + NAME##_pair_t *pair = NAME##_pair_create (key, value); \ + if (!pair) \ + return -1; \ + \ + rc = NAME##_pair_set_get (&map->pair_set, pair, &found); \ + if (rc < 0) \ + return -1; \ + if (found) \ + { \ + NAME##_pair_free (pair); \ + return ERR_MAP_EXISTS; \ + } \ + \ + rc = NAME##_pair_set_add (&map->pair_set, pair); \ + if (rc < 0) \ + { \ + NAME##_pair_free (pair); \ + return -1; \ + } \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *map, KEY_T key, VAL_T *value) \ + { \ + NAME##_pair_t *found = NULL; \ + NAME##_pair_t search = { .key = key }; \ + int rc = NAME##_pair_set_remove (&map->pair_set, &search, &found); \ + if (rc < 0) \ + return ERR_MAP_NOT_FOUND; \ + if (value) \ + *value = found->value; \ + NAME##_pair_free (found); \ + return 0; \ + } \ + \ + int NAME##_get (NAME##_t *map, KEY_T key, VAL_T *value) \ + { \ + NAME##_pair_t *found = NULL, search = { .key = key }; \ + int rc = NAME##_pair_set_get (&map->pair_set, &search, &found); \ + if (rc < 0) \ + return -1; \ + if (found) \ + *value = found->value; \ + return 0; \ + } \ + \ + void NAME##_dump (NAME##_t *map) { NAME##_pair_set_dump (&map->pair_set); } \ + \ + int NAME##_get_key_array (NAME##_t *map, KEY_T **array) \ + { \ + NAME##_pair_t **pair_array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &pair_array); \ + if (n < 0) \ + return -1; \ + if (!array) \ + goto END; \ + /* Allocate result array */ \ + *array = malloc (n * sizeof (KEY_T)); \ + if (!array) \ + { \ + free (pair_array); \ + return -1; \ + } \ + /* Copy keys */ \ + for (int i = 0; i < n; i++) \ + (*array)[i] = pair_array[i]->key; \ + free (pair_array); \ + END: \ + return n; \ + } \ + \ + int NAME##_get_value_array (NAME##_t *map, VAL_T **array) \ + { \ + NAME##_pair_t **pair_array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &pair_array); \ + if (n < 0) \ + return -1; \ + if (!array) \ + goto END; \ + /* Allocate result array */ \ + *array = malloc (n * sizeof (VAL_T)); \ + if (!array) \ + { \ + free (pair_array); \ + return -1; \ + } \ + /* Copy values */ \ + for (int i = 0; i < n; i++) \ + (*array)[i] = pair_array[i]->value; \ + free (pair_array); \ + END: \ + return n; \ + } #endif /* UTIL_MAP_H */ diff --git a/lib/includes/hicn/util/pool.h b/lib/includes/hicn/util/pool.h new file mode 100644 index 000000000..b8acadc44 --- /dev/null +++ b/lib/includes/hicn/util/pool.h @@ -0,0 +1,292 @@ +/* + * 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 pool.h + * \brief Fixed-size pool allocator. + * + * This memory pool allocates a single block of memory that is used to + * efficiently allocate/deallocate fixed-size blocks for high churn data + * structures. + * + * Internally this data structure leverages a vector for managing elements (and + * it thus resizeable if needed), as well as a list of free indices (in the + * form of another vector) and a bitmap marking free indices also (for fast + * iteration). + * + * The internal API manipulates a pointer to the vector that that is can be + * seamlessly resized, and a more convenient user interface is provided through + * macros. + * + * The vector of free indices is managed as a stack where elements indices are + * retrieved from and put back to the end of the vector. In the bitmap, + * available elements are set to 1, and unset to 0 when in use. + * + * The pool is not currently resized down when releasing elements. + * + * It is freely inspired (and simplified) from the VPP infra infrastructure + * library. + */ + +#ifndef UTIL_POOL_H +#define UTIL_POOL_H + +#include <stdint.h> +#include <stdbool.h> + +#include "bitmap.h" +#include <hicn/util/vector.h> +#include "../common.h" + +/* Pool header */ + +typedef struct +{ + size_t elt_size; + size_t alloc_size; + size_t max_size; + bitmap_t *free_bitmap; /* bitmap of free indices */ + off_t *free_indices; /* vector of free indices */ +} pool_hdr_t; + +#define POOL_HDRLEN SIZEOF_ALIGNED (pool_hdr_t) + +/* This header actually prepends the actual content of the pool. */ +#define pool_hdr(pool) ((pool_hdr_t *) ((uint8_t *) (pool) -POOL_HDRLEN)) + +/******************************************************************************/ +/* Helpers */ + +/** Local variable naming macro. */ +#define _pool_var(v) _pool_##v + +/** + * @brief Allocate and initialize a pool data structure (helper). + * + * @param[in,out] pool_ptr Pointer to the pool data structure. + * @param[in] elt_size Size of elements in vector. + * @param[in] max_size Maximum size. + * + * NOTE: that an empty pool might be equal to NULL. + */ +void _pool_init (void **pool_ptr, size_t elt_size, size_t init_size, + size_t max_size); + +/** + * @brief Free a pool data structure (helper). + * + * @param[in] pool_ptr Pointer to the pool data structure. + */ +void _pool_free (void **pool_ptr); + +/** + * @brief Resize a pool data structure (helper). + * + * @param pool_ptr Pointer to the pool data structure. + * + * This function should only be called internally, as the resize is implicitly + * done (if allowed by the maximum size) when the user tries to get a new slot. + */ +void _pool_resize (void **pool_ptr, size_t elt_size); + +/** + * @brief Get a free element from the pool data structure (helper). + * + * @param[in] pool Pointer to the pool data structure to use. + * @param[in,out] elt Pointer to an empty element that will be used to return + * the allocated one from the pool. + * + * NOTES: + * - The memory chunk is cleared upon attribution + */ +off_t _pool_get (void **pool, void **elt, size_t elt_size); + +/** + * @brief Put an element back into the pool data structure (helper). + * + * @param[in] pool_ptr Pointer to the pool data structure to use. + * @param[in] elt Pointer to the pool element to put back. + */ +void _pool_put (void **pool, void **elt, size_t elt_size); + +/** + * @brief Validate a pool element by index (helper). + * + * @param[in] pool The pool data structure to use. + * @param[in] id The index of the element to validate. + * + * @return bool A flag indicating whether the index is valid or not. + */ +bool _pool_validate_id (void **pool_ptr, off_t id); +/******************************************************************************/ +/* Public API */ + +/** + * @brief Allocate and initialize a pool data structure. + * + * @param[in,out] pool Pointer to the pool data structure. + * @param[in] elt_size Size of elements in pool. + * @param[in] max_size Maximum size. + * + * NOTE: that an empty pool might be equal to NULL. + */ +#define pool_init(pool, init_size, max_size) \ + _pool_init ((void **) &pool, sizeof (pool[0]), init_size, max_size); + +/** + * @brief Free a pool data structure. + * + * @param[in] pool The pool data structure to free. + */ +#define pool_free(pool) _pool_free ((void **) &pool); + +/** + * @brief Get a free element from the pool data structure. + * + * @param[in] pool The pool data structure to use. + * @param[in,out] elt An empty element that will be used to return the + * allocated one from the pool. + * + * NOTES: + * - The memory chunk is cleared upon attribution + */ +#define pool_get(pool, elt) \ + _pool_get ((void **) &pool, (void **) &elt, sizeof (*elt)) + +/** + * @brief Put an element back into the pool data structure. + * + * @param[in] pool The pool data structure to use. + * @param[in] elt The pool element to put back. + */ +#define pool_put(pool, elt) \ + _pool_put ((void **) &pool, (void **) &elt, sizeof (*elt)) + +/** + * @brief Validate a pool element by index. + * + * @param[in] pool The pool data structure to use. + * @param[in] id The index of the element to validate. + * + * @return bool A flag indicating whether the index is valid or not. + */ +#define pool_validate_id(pool, id) _pool_validate_id ((void **) &pool, (id)) + +#define pool_get_free_indices_size(pool) \ + vector_len (pool_hdr (pool)->free_indices) + +/** + * @brief Returns the current length of the pool. + * + * @param[in] pool The pool data structure for which to return the length. + * + * @return size_t The current length of the pool. + * + * NOTE: + * - The pool length corresponds to the number of allocated elements, not the + * size of the pool. + */ +#define pool_len(pool) \ + (pool_hdr (pool)->alloc_size - pool_get_free_indices_size (pool)) + +/** + * @brief Enumerate elements from a pool. + * + * @param[in] pool The pool data structure to enumerate. + * @param[in, out] i An integer that will be used for enumeration. + * @param[in, out] eltp A pointer to the element type that will be used for + * enumeration. + * @param[in] BODY Block to execute during enumeration. + * + * Enumeration will iteratively execute BODY with (i, eltp) corresponding + * respectively to the index and element found in the pool. + * + * NOTE: i stars at 0. + */ +#define pool_enumerate(pool, i, eltp, BODY) \ + do \ + { \ + pool_hdr_t *_pool_var (ph) = pool_hdr (pool); \ + bitmap_t *_pool_var (fb) = _pool_var (ph)->free_bitmap; \ + 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) + +#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. + * + * @param[in] pool The pool data structure to iterate over. + * @param[in,out] eltp A pointer to the element type that will be used for + * iteration. + * @param[in] BODY Block to execute during iteration. + * + * Iteration will execute BODY with eltp corresponding successively to all + * elements found in the pool. It is implemented using the more generic + * enumeration function. + */ +#define pool_foreach(pool, eltp, BODY) \ + do \ + { \ + unsigned _pool_var (i); \ + pool_enumerate ((pool), _pool_var (i), (eltp), BODY); \ + } \ + while (0) + +#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 +#endif /* WITH_TESTS */ + +#endif /* UTIL_POOL_H */ diff --git a/lib/includes/hicn/util/ring.h b/lib/includes/hicn/util/ring.h new file mode 100644 index 000000000..9510672b3 --- /dev/null +++ b/lib/includes/hicn/util/ring.h @@ -0,0 +1,227 @@ +/* + * 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 ring.h + * \brief Fixed-size pool allocator. + */ + +#ifndef UTIL_RING_H +#define UTIL_RING_H + +#include <assert.h> +#include <stdint.h> +#include <string.h> +#include <sys/param.h> // MIN +#include <sys/types.h> + +#include <stdio.h> // XXX debug + +#include "../common.h" + +/******************************************************************************/ +/* Ring header */ + +typedef struct +{ + size_t roff; + size_t woff; + size_t size; + size_t max_size; +} ring_hdr_t; + +/* Make sure elements following the header are aligned */ +#define RING_HDRLEN SIZEOF_ALIGNED (ring_hdr_t) + +/* This header actually prepends the actual content of the vector */ +#define ring_hdr(ring) ((ring_hdr_t *) ((uint8_t *) ring - RING_HDRLEN)) + +/******************************************************************************/ +/* Helpers */ + +/** Local variable naming macro. */ +#define _ring_var(v) _ring_##v + +/** + * @brief Allocate and initialize a ring data structure (helper function). + * + * @param[in,out] ring_ptr Ring buffer to allocate and initialize. + * @param[in] elt_size Size of a ring element. + * @param[in] max_size Maximum vector size (O = unlimited). + */ +void _ring_init (void **ring_ptr, size_t elt_size, size_t max_size); + +/** + * @brief Free a ring data structure. + * + * @param ring_ptr[in] Pointer to the ring data structure to free. + */ +void _ring_free (void **ring_ptr); + +static inline int +_ring_add (void **ring_ptr, size_t elt_size, void *eltp) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + + /* We always write ! */ + memcpy ((uint8_t *) *ring_ptr + rh->woff * elt_size, eltp, elt_size); + rh->woff++; + if (rh->woff == rh->max_size) + rh->woff = 0; + if (rh->size < rh->max_size) + { + rh->size++; + } + else + { + /* One packet was dropped */ + rh->roff++; + if (rh->roff == rh->max_size) + rh->roff = 0; + } + return 0; +} + +static inline unsigned +_ring_get_fullness (void **ring_ptr) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + return (unsigned int) (rh->size * 100 / rh->max_size); +} + +static inline unsigned +_ring_is_full (void **ring_ptr) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + return rh->size == rh->max_size; +} + +static inline size_t +_ring_get_size (void **ring_ptr) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + return rh->size; +} + +static inline int +_ring_advance (void **ring_ptr, unsigned n) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + assert (n <= rh->size); + + rh->roff += n; + rh->size -= n; + while (rh->roff >= rh->max_size) + rh->roff -= rh->max_size; + return 0; +} + +static inline int +_ring_get (void **ring_ptr, size_t elt_size, unsigned i, void *eltp) +{ + assert (*ring_ptr); + ring_hdr_t *rh = ring_hdr (*ring_ptr); + assert (i <= rh->size); + size_t pos = rh->roff + i; + if (pos >= rh->max_size) + pos -= rh->max_size; + memcpy (eltp, (uint8_t *) *ring_ptr + pos * elt_size, elt_size); + return 0; +} + +/******************************************************************************/ +/* Public API */ + +/** + * @brief Allocate and initialize a ring data structure. + * + * @param[in,out] ring Ring to allocate and initialize. + * @param[in] max_size Maximum ring size (nonzero). + * + * NOTE: + * - Allocated memory is set to 0 (used by bitmap) + */ + +#define ring_init(RING, MAX_SIZE) \ + _ring_init ((void **) &(RING), sizeof ((RING)[0]), (MAX_SIZE)) + +#define ring_free(RING) _ring_free ((void **) &(RING)) + +#define ring_get_fullness(RING) _ring_get_fullness ((void **) &(RING)) + +#define ring_is_full(RING) _ring_is_full ((void **) &(RING)) + +#define ring_get_size(RING) _ring_get_size ((void **) &(RING)) + +#define ring_add(RING, ELT) \ + _ring_add ((void **) &(RING), sizeof (RING[0]), ELT) + +#define ring_add_value(RING, VALUE) \ + do \ + { \ + typeof (VALUE) _ring_var (v) = VALUE; \ + _ring_add ((void **) &(RING), sizeof (RING[0]), &_ring_var (v)); \ + } \ + while (0) + +#define ring_advance(RING, N) _ring_advance ((void **) &(RING), (N)) + +#define ring_get(RING, I, ELTP) \ + _ring_get ((void **) &RING, sizeof (RING[0]), (I), (ELTP)) + +/** + * @brief Helper function used by ring_foreach(). + */ +#define ring_enumerate_n(RING, I, ELTP, COUNT, BODY) \ + ({ \ + for ((I) = 0; (I) < MIN (ring_get_size (RING), (COUNT)); (I)++) \ + { \ + ring_get ((RING), (I), (ELTP)); \ + { \ + BODY; \ + } \ + } \ + }) + +#define ring_enumerate(ring, i, eltp, BODY) \ + ring_enumerate_n ((ring), (i), (eltp), 1, (BODY)) + +/** + * @brief Iterate over elements in a ring. + * + * @param[in] pool The ring data structure to iterate over + * @param[in, out] eltp A pointer to the element that will be used for + * iteration + * @param[in] BODY Block to execute during iteration + * + * @note Iteration will execute BODY with eltp corresponding successively to + * all elements found in the ring. It is implemented using the more generic + * enumeration function. + */ +#define ring_foreach_n(ring, eltp, count, BODY) \ + ({ \ + unsigned _ring_var (i); \ + ring_enumerate_n ((ring), _ring_var (i), (eltp), (count), BODY); \ + }) + +#define ring_foreach(ring, eltp, BODY) \ + ring_foreach_n ((ring), (eltp), 1, (BODY)) + +#endif /* UTIL_RING_H */ diff --git a/lib/includes/hicn/util/set.h b/lib/includes/hicn/util/set.h index bc2e3caac..0a5ff6777 100644 --- a/lib/includes/hicn/util/set.h +++ b/lib/includes/hicn/util/set.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -25,214 +25,196 @@ #define thread_local _Thread_local //#endif /* ! __ANDROID__ */ -#define ERR_SET_EXISTS -2 +#define ERR_SET_EXISTS -2 #define ERR_SET_NOT_FOUND -3 /* FIXME: buffer overflow when this is too small... investigate */ #define BUFSIZE 1024 -static inline -int -int_cmp(const int x, const int y) +static inline int +int_cmp (const int x, const int y) { - return x - y; + return x - y; } -static inline -int -int_snprintf(char * buf, size_t size, int value) { - return snprintf(buf, size, "%d", value); +static inline int +int_snprintf (char *buf, size_t size, int value) +{ + return snprintf (buf, size, "%d", value); } -static inline -int -string_snprintf(char * buf, size_t size, const char * s) { - return snprintf(buf, size, "%s", s); +static inline int +string_snprintf (char *buf, size_t size, const char *s) +{ + return snprintf (buf, size, "%s", s); } -static inline -int -generic_snprintf(char * buf, size_t size, const void * value) { - return snprintf(buf, BUFSIZE, "%p", value); +static inline int +generic_snprintf (char *buf, size_t size, const void *value) +{ + return snprintf (buf, BUFSIZE, "%p", value); } -typedef int(*cmp_t)(const void * x, const void * y); - -#define TYPEDEF_SET_H(NAME, T) \ - \ -typedef struct { \ - size_t size; \ - void * root; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * set); \ - \ -int NAME ## _finalize(NAME ## _t * set); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * set); \ - \ -int NAME ## _add(NAME ## _t * set, const T element); \ - \ -int NAME ## _remove(NAME ## _t * set, const T search, T * element); \ - \ -int NAME ## _clear(NAME ## _t * set); \ - \ -int NAME ## _get(const NAME ## _t * set, const T search, T * element); \ - \ -int NAME ## _get_array(const NAME ## _t * set, T ** element); \ - \ -void NAME ## _dump(NAME ## _t * set); +typedef int (*cmp_t) (const void *x, const void *y); +#define TYPEDEF_SET_H(NAME, T) \ + \ + typedef struct \ + { \ + size_t size; \ + void *root; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *set); \ + \ + int NAME##_finalize (NAME##_t *set); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *set); \ + \ + int NAME##_add (NAME##_t *set, const T element); \ + \ + int NAME##_remove (NAME##_t *set, const T search, T *element); \ + \ + int NAME##_clear (NAME##_t *set); \ + \ + int NAME##_get (const NAME##_t *set, const T search, T *element); \ + \ + int NAME##_get_array (const NAME##_t *set, T **element); \ + \ + void NAME##_dump (NAME##_t *set); - - -#define TYPEDEF_SET(NAME, T, CMP, SNPRINTF) \ -int \ -NAME ## _initialize(NAME ## _t * set) \ -{ \ - set->root = NULL; \ - set->size = 0; \ - return 0; \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * set) \ -{ \ - return NAME ## _clear(set); \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * set = malloc(sizeof(NAME ## _t)); \ - if (!set) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(set) < 0) \ - goto ERR_INITIALIZE; \ - \ - return set; \ - \ -ERR_INITIALIZE: \ - free(set); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * set) \ -{ \ - NAME ## _finalize(set); \ - free(set); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * set, const T element) \ -{ \ - T * found = tfind(element, &set->root, (cmp_t)CMP); \ - void * ptr = tsearch(element, &set->root, (cmp_t)CMP); \ - if (!ptr) \ - return -1; \ - if (!found) \ - set->size++; \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * set, const T search, T * element) \ -{ \ - T * found = tfind(search, &set->root, (cmp_t)CMP); \ - if (!found) \ - return ERR_SET_NOT_FOUND; \ - if (element) \ - *element = *found; \ - tdelete(search, &set->root, (cmp_t)CMP); \ - set->size--; \ - return 0; \ -} \ - \ -int \ -NAME ## _clear(NAME ## _t * set) \ -{ \ - T * array; \ - int n = NAME ## _get_array(set, &array); \ - if (n < 0) \ - return -1; \ - for (unsigned i = 0; i < n; i++) { \ - T element = array[i]; \ - NAME ## _remove(set, element, NULL); \ - } \ - free(array); \ - return 0; \ -} \ - \ -int \ -NAME ## _get(const NAME ## _t * set, const T search, T * element) \ -{ \ - T * found = tfind(search, &set->root, (cmp_t)CMP); \ - if (element) \ - *element = found ? *found : NULL; \ - return 0; \ -} \ - \ -static void \ -NAME ## _dump_node(const void *nodep, const VISIT which, \ - const int depth) \ -{ \ - char buf[BUFSIZE]; \ - switch (which) { \ - case preorder: \ - case endorder: \ - break; \ - case postorder: \ - case leaf: \ - SNPRINTF(buf, BUFSIZE, *(T*)nodep); \ - INFO("%s", buf); \ - break; \ - } \ -} \ - \ -void \ -NAME ## _dump(NAME ## _t * set) { \ - twalk(set->root, NAME ## _dump_node); \ -} \ - \ -thread_local \ -T * NAME ## _array_pos = NULL; \ - \ -static void \ -NAME ## _add_node_to_array(const void *nodep, const VISIT which, \ - const int depth) \ -{ \ - if (!NAME ## _array_pos) \ - return; \ - switch (which) { \ - case preorder: \ - case endorder: \ - break; \ - case postorder: \ - case leaf: \ - *NAME ## _array_pos = *(T*)nodep; \ - NAME ## _array_pos++; \ - break; \ - } \ -} \ - \ -int \ -NAME ## _get_array(const NAME ## _t * set, T ** element) \ -{ \ - if (!element) \ - goto END; \ - *element = malloc(set->size * sizeof(T)); \ - if (!*element) \ - return -1; \ - NAME ## _array_pos = *element; \ - twalk(set->root, NAME ## _add_node_to_array); \ - NAME ## _array_pos = NULL; \ -END: \ - return (int)(set->size); \ -} +#define TYPEDEF_SET(NAME, T, CMP, SNPRINTF) \ + int NAME##_initialize (NAME##_t *set) \ + { \ + set->root = NULL; \ + set->size = 0; \ + return 0; \ + } \ + \ + int NAME##_finalize (NAME##_t *set) { return NAME##_clear (set); } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *set = malloc (sizeof (NAME##_t)); \ + if (!set) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (set) < 0) \ + goto ERR_INITIALIZE; \ + \ + return set; \ + \ + ERR_INITIALIZE: \ + free (set); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *set) \ + { \ + NAME##_finalize (set); \ + free (set); \ + } \ + \ + int NAME##_add (NAME##_t *set, const T element) \ + { \ + T *found = tfind (element, &set->root, (cmp_t) CMP); \ + void *ptr = tsearch (element, &set->root, (cmp_t) CMP); \ + if (!ptr) \ + return -1; \ + if (!found) \ + set->size++; \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *set, const T search, T *element) \ + { \ + T *found = tfind (search, &set->root, (cmp_t) CMP); \ + if (!found) \ + return ERR_SET_NOT_FOUND; \ + if (element) \ + *element = *found; \ + tdelete (search, &set->root, (cmp_t) CMP); \ + set->size--; \ + return 0; \ + } \ + \ + int NAME##_clear (NAME##_t *set) \ + { \ + T *array; \ + int n = NAME##_get_array (set, &array); \ + if (n < 0) \ + return -1; \ + for (unsigned i = 0; i < n; i++) \ + { \ + T element = array[i]; \ + NAME##_remove (set, element, NULL); \ + } \ + free (array); \ + return 0; \ + } \ + \ + int NAME##_get (const NAME##_t *set, const T search, T *element) \ + { \ + T *found = tfind (search, &set->root, (cmp_t) CMP); \ + if (element) \ + *element = found ? *found : NULL; \ + return 0; \ + } \ + \ + static void NAME##_dump_node (const void *nodep, const VISIT which, \ + const int depth) \ + { \ + char buf[BUFSIZE]; \ + switch (which) \ + { \ + case preorder: \ + case endorder: \ + break; \ + case postorder: \ + case leaf: \ + SNPRINTF (buf, BUFSIZE, *(T *) nodep); \ + INFO ("%s", buf); \ + break; \ + } \ + } \ + \ + void NAME##_dump (NAME##_t *set) { twalk (set->root, NAME##_dump_node); } \ + \ + thread_local T *NAME##_array_pos = NULL; \ + \ + static void NAME##_add_node_to_array (const void *nodep, const VISIT which, \ + const int depth) \ + { \ + if (!NAME##_array_pos) \ + return; \ + switch (which) \ + { \ + case preorder: \ + case endorder: \ + break; \ + case postorder: \ + case leaf: \ + *NAME##_array_pos = *(T *) nodep; \ + NAME##_array_pos++; \ + break; \ + } \ + } \ + \ + int NAME##_get_array (const NAME##_t *set, T **element) \ + { \ + if (!element) \ + goto END; \ + *element = calloc (set->size, sizeof (T)); \ + if (!*element) \ + return -1; \ + NAME##_array_pos = *element; \ + twalk (set->root, NAME##_add_node_to_array); \ + NAME##_array_pos = NULL; \ + END: \ + return (int) (set->size); \ + } #endif /* UTIL_SET_H */ diff --git a/lib/includes/hicn/util/slab.h b/lib/includes/hicn/util/slab.h new file mode 100644 index 000000000..2c6546add --- /dev/null +++ b/lib/includes/hicn/util/slab.h @@ -0,0 +1,126 @@ +/* + * 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. + */ + +/** + * @brief The slab is used to store elements of the same size. + * + * The slab contains blocks of contiguous memory. Each block contains multiple + * chunks. An element is stored inside a chunk and the chunk has a header with + * a pointer to the block it belongs to. + * + * Blocks are stored in two doubly-linked lists: 'full' for blocks that + * are already full, 'partial_or_empty' for blocks with available chunks. When + * a block becomes full it is moved into the 'full' list and vice versa. + * + * When allocationg an element, a block is taken from the 'partial_or_empty' + * list if such list is not empty. If empty, a new block of contiguous memory + * is created and put in the 'partial_or_empty' list. Then, a chunk is taken + * from the block. When releasing an element, the block it belongs to is + * retrieved from the chunk header and used to release the chunk. + * + * Blocks are created with increasing capacity (i.e. number of chunks they + * contain) such that every new block allocaion doubles the total number of + * chunks stored in the slab. + */ + +#ifndef UTIL_SLAB_H +#define UTIL_SLAB_H + +#include <stddef.h> + +#define SLAB_INIT_SIZE 32 + +/* CHUNK */ + +typedef struct block_s block_t; +typedef struct +{ + block_t *block; // Pointer to the block that contains the chunk +} chunk_hdr_t; + +#define CHUNK_HDRLEN SIZEOF_ALIGNED (chunk_hdr_t) +#define chunk_hdr(chunk) ((chunk_hdr_t *) ((uint8_t *) (chunk) -CHUNK_HDRLEN)) + +/* BLOCK */ + +struct block_s +{ + void *pool; + block_t *prev; + block_t *next; +}; + +/* SLAB */ + +typedef struct +{ + size_t num_chunks; // Total number of chunks (from all blocks) currently + // stored in the slab + size_t chunk_size; + block_t *full; + block_t *partial_or_empty; +} slab_t; + +/* Internal API */ + +slab_t *_slab_create (size_t elt_size, size_t num_elts); +void *_slab_get (slab_t *slab); +void _slab_put (slab_t *slab, void *elt); + +/* Public API */ + +/** + * @brief Create a slab able to store elements of type 'TYPE'. + * + * @param[in] TYPE Type of the elements to store in the slab. + * @param[in] SIZE Initial size of the slab, i.e. size of the initial block. + * @return slab_t* The slab created, NULL if error. + */ +#define slab_create(TYPE, SIZE) _slab_create (sizeof (TYPE), SIZE) + +/** + * @brief Free a slab. + * + * @param[in] slab Slab to free. + */ +void slab_free (slab_t *slab); + +/** + * @brief Get an element from the slab. + * + * @param[in] TYPE Type of the elements stored in the slab. + * @param[in] SLAB Slab to take the element from. + * @return TYPE* Element retrieved from the slab + */ +#define slab_get(TYPE, SLAB) (TYPE *) _slab_get (SLAB) + +/** + * @brief Same as 'slab_get' but with a different signature, to avoid passing + * the type that is instead inferred from the element. + * + * @param[in] SLAB Slab to take the element from. + * @param[in, out] ELT Element retrieved from the slab. + */ +#define slab_get2(SLAB, ELT) ELT = (typeof (*(ELT)) *) _slab_get (SLAB) + +/** + * @brief Put an element back into the slab. + * + * @param[in] SLAB Slab to return the element to. + * @param[in] ELT Element to put in the slab. + */ +#define slab_put(SLAB, ELT) _slab_put (SLAB, (void *) ELT) + +#endif /* UTIL_SLAB_H */ diff --git a/lib/includes/hicn/util/sstrncpy.h b/lib/includes/hicn/util/sstrncpy.h new file mode 100644 index 000000000..0b397c26b --- /dev/null +++ b/lib/includes/hicn/util/sstrncpy.h @@ -0,0 +1,76 @@ +/* + * 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. + */ + +#ifndef UTIL_SSTRNCPY_H +#define UTIL_SSTRNCPY_H + +#ifndef __STDC_WANT_LIB_EXT1__ +#define __STDC_WANT_LIB_EXT1__ 1 +#endif + +#include <errno.h> +#include <string.h> + +#ifdef __STDC_LIB_EXT1__ +// If safe string functions already available in the system, use them +#elif ENABLE_SAFEC +// If safe string functions not available and SafeC is enabled, +// use SafeC +#include <safe_string.h> +#else +// Use custom safe string functions +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. + */ +static inline errno_t +strcpy_s (char *dst, size_t n, const char *src) +{ + if (!dst || !src || !n) + { + fprintf (stderr, "[strncpy] invalid input received"); + return EINVAL; + } + + dst[n - 1] = 0; + strncpy (dst, src, n); + + if (dst[n - 1] != 0) + { + fprintf (stderr, "[strncpy] '%s' has been trucated\n", src); + dst[n - 1] = 0; + return EINVAL; + } + + return EOK; +} + +static inline size_t +strnlen_s (const char *s, size_t maxlen) +{ + if (s == NULL) + return 0; + + return strnlen (s, maxlen); +} +#endif /* HICN_VPP_PLUGIN */ + +#endif /* __STDC_LIB_EXT1__ */ +#endif /* UTIL_SSTRNCPY_H */ diff --git a/lib/includes/hicn/util/token.h b/lib/includes/hicn/util/token.h index 43e0a77b2..c62c294bc 100644 --- a/lib/includes/hicn/util/token.h +++ b/lib/includes/hicn/util/token.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * 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: @@ -19,12 +19,12 @@ * Concatenate preprocessor tokens A and B without expanding macro definitions * (however, if invoked from a macro, macro arguments are expanded). */ -#define PPCAT_NX(A, B) A ## B +#define PPCAT_NX(A, B) A##B /* * Concatenate preprocessor tokens A and B after macro-expanding them. */ -#define PPCAT(A, B) PPCAT_NX(A, B) +#define PPCAT(A, B) PPCAT_NX (A, B) /* Token stringification */ @@ -37,4 +37,4 @@ /* * Turn A into a string literal after macro-expanding it. */ -#define STRINGIZE(A) STRINGIZE_NX(A) +#define STRINGIZE(A) STRINGIZE_NX (A) diff --git a/lib/includes/hicn/util/types.h b/lib/includes/hicn/util/types.h index 017e85b72..a883b8220 100644 --- a/lib/includes/hicn/util/types.h +++ b/lib/includes/hicn/util/types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 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,21 +19,63 @@ #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; -/* Helper for avoiding warnings about type-punning */ -#define UNION_CAST(x, destType) \ - (((union {__typeof__(x) a; destType b;})x).b) +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) -//typedef unsigned int hash_t; +/* Word types. */ +#if _hicn_uword_bits == 64 +/* 64 bit word machines. */ +typedef u64 hicn_uword; +#define hicn_uword_bits 64 +#else +/* 32 bit word machines. */ +typedef u32 hicn_uword; +#define hicn_uword_bits 32 +#endif + +typedef hicn_uword hicn_ip_csum_t; + +/* Helper for avoiding warnings about type-punning */ +#define UNION_CAST(x, destType) \ + (((union { \ + __typeof__ (x) a; \ + destType b; \ + }) x) \ + .b) -typedef int (*cmp_t)(const void *, const void *); +typedef int (*cmp_t) (const void *, const void *); /* Enums */ -#define IS_VALID_ENUM_TYPE(NAME, x) ((x > NAME ## _UNDEFINED) && (x < NAME ## _N)) +#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 new file mode 100644 index 000000000..e693df9e3 --- /dev/null +++ b/lib/includes/hicn/util/vector.h @@ -0,0 +1,470 @@ +/* + * 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 vector.h + * \brief Resizeable static array + * + * A vector is a resizeable area of contiguous memory that contains elements of + * fixed size. It is mostly useful to serve as the basis for more advanced data + * structures such as memory pools. + * + * The internal API manipulates a pointer to the vector so that it can be + * seamlessly resized, and a more convenient user interface is provided through + * macros. + * + * A vector starts at index 0, and is typed according to the elements it + * contains. For that matter, the data structure header precedes the returned + * pointer which corresponds to the storage area. + * + * A vector is by default used as a stack where an end marker is maintained and + * new elements are pushed right after this end marker (an indication of + * the size of the vector) after ensuring the vector is sufficiently large. + * + * A user should not store any pointer to vector elements as this might change + * during reallocations, but should use indices instead. + * + * NOTE: a maximum size is currently not implemented. + * + * It is freely inspired (and simplified) from the VPP infra infrastructure + * library. + */ + +#ifndef UTIL_VECTOR_H +#define UTIL_VECTOR_H + +#include <stdint.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <sys/types.h> +#include <stdbool.h> + +#include "../common.h" + +/******************************************************************************/ +/* Vector header */ + +typedef struct +{ + size_t cur_size; /** Vector current size (corresponding to the highest used + element). */ + size_t alloc_size; /** The currently allocated size. */ + size_t max_size; /** The maximum allowed size (0 = no limit) */ +} vector_hdr_t; + +/* Make sure elements following the header are aligned */ +#define VECTOR_HDRLEN SIZEOF_ALIGNED (vector_hdr_t) + +/* This header actually prepends the actual content of the vector */ +#define vector_hdr(vector) \ + ((vector_hdr_t *) ((uint8_t *) vector - VECTOR_HDRLEN)) + +/******************************************************************************/ +/* Helpers */ + +/** Local variable naming macro. */ +#define _vector_var(v) _vector_##v + +/** + * @brief Allocate and initialize a vector data structure (helper function). + * + * @param[in,out] vector_ptr Vector to allocate and initialize. + * @param[in] elt_size Size of a vector element. + * @param[in] init_size Initial vector size. + * @param[in] max_size Maximum vector size (O = unlimited). + * @return int 0 if successful, -1 otherwise + */ +int _vector_init (void **vector_ptr, size_t elt_size, size_t init_size, + size_t max_size); + +/** + * @brief Free a vector data structure. + * + * @param vector_ptr[in] Pointer to the vector data structure to free. + */ +void _vector_free (void **vector_ptr); + +/** + * @brief Resize a vector data structure. + * + * @param[in] vector_ptr A pointer to the vector data structure to resize. + * @param[in] elt_size The size of a vector element. + * @param[in] pos The position at which the vector should be able to hold an + * element. + * + * @return int Flag indicating whether the vector has been correctly resized. + * + * NOTE: + * - The resize operation does not specify the final size of the vector but + * instead ensure that it is large enough to hold an element at the specified + * position. This allows the caller not to care about doing successive calls to + * this API while the vector is growing in size. + */ +int _vector_resize (void **vector_ptr, size_t elt_size, off_t pos); + +/** + * @brief Ensures a vector is sufficiently large to hold an element at the + * given position. + * + * @param[in] vector_ptr A pointer to the vector data structure to resize. + * @param[in] elt_size The size of a vector element. + * @param[in] pos The position to validate. + * + * @return int Flag indicating whether the vector is available. + * + * NOTE: + * - This function should always be called before writing to a vector element + * to eventually make room for it (the vector will eventually be resized). + * - This function can fail if the vector is full and for any reason it cannot + * be resized. + */ +static inline int +_vector_ensure_pos (void **vector_ptr, size_t elt_size, off_t pos) +{ + vector_hdr_t *vh = vector_hdr (*vector_ptr); + if (pos >= (off_t) vh->alloc_size) + return _vector_resize (vector_ptr, elt_size, pos + 1); + return 0; +} + +/** + * @brief Push an element at the end of a vector. + * + * @param[in] vector_ptr A pointer to the vector data structure to resize. + * @param[in] elt_size The size of a vector element. + * @param[in] elt The element to insert. + * + * NOTE: + * - This function ensures there is sufficient room for inserting the element, + * and evenutually resizes the vector to make room for it (if allowed by + * maximum size). + */ +static inline int +_vector_push (void **vector_ptr, size_t elt_size, void *elt) +{ + vector_hdr_t *vh = vector_hdr (*vector_ptr); + if (_vector_ensure_pos (vector_ptr, elt_size, vh->cur_size) < 0) + return -1; + + /* Always get header after a potential resize */ + vh = vector_hdr (*vector_ptr); + memcpy ((uint8_t *) *vector_ptr + vh->cur_size * elt_size, elt, elt_size); + vh = vector_hdr (*vector_ptr); + vh->cur_size++; + return 0; +} + +/** + * @brief Remove all the occurrencies of an element from the vector. + * The order of the elements is NOT maintained. + * + * @param[in, out] vector The vector data structure to resize + * @param[in] elt_size The size of a vector element + * @param[in] elt The element to remove + * @return int Number of elemets (equal to 'elt') removed from the vector + */ +static inline int +_vector_remove_unordered (void *vector, size_t elt_size, void *elt) +{ + size_t num_removed = 0; + vector_hdr_t *vh = vector_hdr (vector); + for (size_t i = 0; i < vector_hdr (vector)->cur_size; i++) + { + if (memcmp ((uint8_t *) vector + i * elt_size, elt, elt_size) == 0) + { + vh->cur_size--; + // Copy last element to current position (hence order is not + // maintained) + memcpy ((uint8_t *) vector + i * elt_size, + (uint8_t *) vector + vh->cur_size * elt_size, elt_size); + num_removed++; + } + } + return (int) num_removed; +} + +/** + * @brief Get the element at the specified position and store in 'elt'. + * + * @param[in] vector Pointer to the vector data structure to use + * @param[in] pos Position of the element to retrieve + * @param[in] elt_size The size of a vector element + * @param[in] elt The element where the result is stored + * @return int 0 if successful, -1 otherwise + */ +static inline int +_vector_get (void *vector, off_t pos, size_t elt_size, void *elt) +{ + vector_hdr_t *vh = vector_hdr (vector); + if ((size_t) pos >= vh->alloc_size) + return -1; + + memcpy (elt, (uint8_t *) vector + pos * elt_size, elt_size); + return 0; +} + +/** + * @brief Check if specified element is present in vector. + * + * @param[in] vector Pointer to the vector data structure to use + * @param[in] elt_size The size of a vector element + * @param[in] elt The element to search for + * @return true If specified element is contained in the vector + * @return false + */ +static inline bool +_vector_contains (void *vector, size_t elt_size, void *elt) +{ + 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; + } + + return false; +} + +/** + * @brief Remove the element at the specified position from the vector. + * Relative element order is preserved by shifting left the elements after the + * target. + * + * @param[in, out] vector Pointer to the vector data structure to use + * @param[in] elt_size The size of a vector element + * @param[in] pos Position of the element to remove + * @return int 0 if successful, -1 otherwise + */ +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 ((size_t) pos >= vh->cur_size) + return -1; + + // Shift backward by one position all the elements after the one specified + memmove ((uint8_t *) (*vector_ptr) + pos * elt_size, + (uint8_t *) (*vector_ptr) + (pos + 1) * elt_size, + (vh->cur_size - 1 - pos) * elt_size); + vh->cur_size--; + + return 0; +} + +/******************************************************************************/ +/* Public API */ + +/** + * @brief Allocate and initialize a vector data structure. + * + * @param[in,out] vector Vector to allocate and initialize. + * @param[in] init_size Initial vector size. + * @param[in] max_size Maximum vector size (nonzero). + * + * NOTE: + * - Allocated memory is set to 0 (used by bitmap) + */ + +#define vector_init(vector, init_size, max_size) \ + _vector_init ((void **) &vector, sizeof (vector[0]), init_size, max_size) + +/** + * @brief Free a vector data structure. + * + * @param[in] vector The vector data structure to free. + */ +#define vector_free(vector) _vector_free ((void **) &vector) + +/** + * @brief Resize a vector data structure. + * + * @param[in] vector The vector data structure to resize. + * @param[in] pos The position at which the vector should be able to hold an + * element. + * + * @return int Flag indicating whether the vector has been correctly resized. + * + * NOTE: + * - The resize operation does not specify the final size of the vector but + * instead ensure that it is large enough to hold an element at the specified + * position. This allows the caller not to care about doing successive calls to + * this API while the vector is growing in size. + * - If the new size is smaller than the current size, the content of the + * vector will be truncated. + * - Newly allocated memory is set to 0 (used by bitmap) + */ +#define vector_resize(vector) \ + _vector_resize ((void **) &(vector), sizeof ((vector)[0]), 0) + +/** + * @brief Ensures a vector is sufficiently large to hold an element at the + * given position. + * + * @param[in] vector The vector for which to validate the position. + * @param[in] pos The position to validate. + * + * NOTE: + * - This function should always be called before writing to a vector element + * to eventually make room for it (the vector will eventually be resized). + */ +#define vector_ensure_pos(vector, pos) \ + _vector_ensure_pos ((void **) &(vector), sizeof ((vector)[0]), pos); + +/** + * @brief Push an element at the end of a vector. + * + * @param[in] vector The vector in which to insert the element. + * @param[in] elt The element to insert. + * + * NOTE: + * - This function ensures there is sufficient room for inserting the element, + * and evenutually resizes the vector to make room for it (if allowed by + * maximum size). + */ +#define vector_push(vector, elt) \ + ({ \ + typeof (elt) _vector_var (x) = elt; \ + _vector_push ((void **) &(vector), sizeof ((vector)[0]), \ + (void *) (&_vector_var (x))); \ + }) + +#define vector_at(vector, pos) \ + ({ \ + assert ((size_t) pos < vector_hdr (vector)->cur_size); \ + (vector)[(pos)]; \ + }) + +#define vector_set(vector, pos, elt) \ + ({ \ + assert (pos < vector_hdr (vector)->cur_size); \ + (vector)[(pos)] = elt; \ + }) + +/** + * @brief Clear the vector content, i.e. new pushes will insert starting from + * position 0. + * + * @param[in, out] vector The vector to reset + */ +#define vector_reset(vector) (vector_len (vector) = 0) + +/** + * @brief Get the element at the specified position and store in 'elt'. + * + * @param[in] vector The vector data structure to use + * @param[in] pos Position of the element to retrieve + * @param[in] elt The element where the result is stored + * @return int 0 if successful, -1 otherwise + */ +#define vector_get(vector, pos, elt) \ + _vector_get ((void *) (vector), (pos), sizeof ((vector)[0]), \ + (void *) (&elt)); + +/** + * @brief Check if specified element is present in vector. + * + * @param[in] vector The vector data structure to use + * @param[in] elt The element to search for + * @return true If specified element is contained in the vector + * @return false + */ +#define vector_contains(vector, elt) \ + ({ \ + typeof (elt) _vector_var (x) = elt; \ + _vector_contains ((void *) (vector), sizeof ((vector)[0]), \ + (void *) (&_vector_var (x))); \ + }) + +/** + * @brief Remove the element at the specified position from the vector. + * Relative element order is preserved by shifting left the elements after the + * target. + * + * @param[in, out] vector The vector data structure to use + * @param[in] pos Position of the element to remove + * @return int 0 if successful, -1 otherwise + */ +#define vector_remove_at(vector, pos) \ + _vector_remove_at ((void **) &(vector), sizeof ((vector)[0]), (pos)) + +/** + * @brief Remove all the occurrencies of an element from the vector. + * The order of the elements is NOT maintained. + * + * @param[in, out] vector The vector data structure to resize + * @param[in] elt The element to remove + * @return int Number of elemets (equal to 'elt') removed from the vector + */ +#define vector_remove_unordered(vector, elt) \ + ({ \ + typeof (elt) x = elt; \ + _vector_remove_unordered ((void *) (vector), sizeof ((vector)[0]), \ + (void *) (&x)); \ + }) + +/** + * @brief Returns the length of a vector. + * + * @param[in] vector The vector from which to get the size. + * + * @see vector_ensure_pos + * + * NOTE: + * - The size of the vector corresponds to the highest accessed index (for + * example as specified in the resize operation) and not the currently + * allocated size which will typically be bigger to amortize allocations. + * - A user should always call vector_ensure_pos to ensure the vector is + * sufficiently large to hold an element at the specified position. + */ +#define vector_len(vector) vector_hdr (vector)->cur_size + +/** + * @brief Returns the allocated size of a vector. + */ +#define vector_get_alloc_size(vector) vector_hdr (vector)->alloc_size + +/** + * @brief Iterate over elements in a vector. + * + * @param[in] pool The vector data structure to iterate over + * @param[in, out] eltp A pointer to the element that will be used for + * iteration + * @param[in] BODY Block to execute during iteration + * + * @note Iteration will execute BODY with eltp corresponding successively to + * all elements found in the vector. It is implemented using the more generic + * enumeration function. + */ +#define vector_foreach(vector, eltp, BODY) \ + ({ \ + unsigned _vector_var (i); \ + vector_enumerate ((vector), _vector_var (i), (eltp), BODY); \ + }) + +/** + * @brief Helper function used by vector_foreach(). + */ +#define vector_enumerate(vector, i, eltp, BODY) \ + ({ \ + for ((i) = 0; (i) < vector_len (vector); (i)++) \ + { \ + eltp = (vector) + (i); \ + { \ + BODY; \ + } \ + } \ + }) + +#endif /* UTIL_VECTOR_H */ diff --git a/lib/includes/hicn/util/win_portability.h b/lib/includes/hicn/util/win_portability.h index 5f30cfbb2..609203afc 100644 --- a/lib/includes/hicn/util/win_portability.h +++ b/lib/includes/hicn/util/win_portability.h @@ -1,45 +1,45 @@ -/*
- * Copyright (c) 2019 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.
- */
-
-#pragma once
-#include <hicn/util/windows/windows_Utils.h>
-#include <afunix.h>
-#include <io.h>
-#include <iphlpapi.h>
-#include <process.h>
-#include <stdio.h>
-#pragma comment(lib, "IPHLPAPI.lib")
-
-#ifndef in_port_t
-#define in_port_t uint16_t
-#endif
-
-#ifndef in_addr_t
-#define in_addr_t uint32_t
-#endif
-
-#ifndef strncasecmp
-#define strncasecmp _strnicmp
-#endif
-
-#ifndef strcasecmp
-#define strcasecmp _stricmp
-#endif
-
-#define HAVE_STRUCT_TIMESPEC
-
-#ifndef getline
-int getline(char **lineptr, size_t *n, FILE *stream);
+/* + * 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. + */ + +#pragma once +#include <hicn/util/windows/windows_Utils.h> +#include <afunix.h> +#include <io.h> +#include <iphlpapi.h> +#include <process.h> +#include <stdio.h> +#pragma comment(lib, "IPHLPAPI.lib") + +#ifndef in_port_t +#define in_port_t uint16_t +#endif + +#ifndef in_addr_t +#define in_addr_t uint32_t +#endif + +#ifndef strncasecmp +#define strncasecmp _strnicmp +#endif + +#ifndef strcasecmp +#define strcasecmp _stricmp +#endif + +#define HAVE_STRUCT_TIMESPEC + +#ifndef getline +int getline (char **lineptr, size_t *n, FILE *stream); #endif
\ No newline at end of file diff --git a/lib/includes/hicn/util/windows/dlfcn.h b/lib/includes/hicn/util/windows/dlfcn.h new file mode 100644 index 000000000..f1457964e --- /dev/null +++ b/lib/includes/hicn/util/windows/dlfcn.h @@ -0,0 +1,34 @@ +/* dlfcn.h */ + +#ifndef DLFCN_H +#define DLFCN_H +#define RTLD_GLOBAL 0x100 /* do not hide entries in this module */ +#define RTLD_LOCAL 0x000 /* hide entries in this module */ + +#define RTLD_LAZY 0x000 /* accept unresolved externs */ +#define RTLD_NOW 0x001 /* abort if module has unresolved externs */ + +/* + How to call in Windows: + + void *h = dlopen ("path\\library.dll", flags) + void (*fun)() = dlsym (h, "entry") +*/ + +#ifdef __cplusplus +extern "C" +{ +#endif + + void *dlopen (const char *filename, int flag); + int dlclose (void *handle); + + void *dlsym (void *handle, const char *name); + + const char *dlerror (void); + +#ifdef __cplusplus +} +#endif + +#endif
\ No newline at end of file diff --git a/lib/includes/hicn/util/windows/windows_utils.h b/lib/includes/hicn/util/windows/windows_utils.h index c4662af5e..e15c0d752 100755..100644 --- a/lib/includes/hicn/util/windows/windows_utils.h +++ b/lib/includes/hicn/util/windows/windows_utils.h @@ -1,154 +1,166 @@ -/*
- * Copyright (c) 2019 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 WINDOWS_UTILS_H
-#define WINDOWS_UTILS_H
-#define WIN32_LEAN_AND_MEAN
-#define HAVE_STRUCT_TIMESPEC
-#include <Windows.h>
-#include <stdint.h>
-#include <io.h>
-#include <stdlib.h>
-#include <winsock2.h>
-#include <WS2tcpip.h>
-
-#ifndef IOVEC
-#define IOVEC
-struct iovec {
- void* iov_base;
- size_t iov_len;
-};
-#endif
-
-typedef uint16_t in_port_t;
-
-#ifndef SLEEP
-#define SLEEP
-#define sleep Sleep
-#endif
-
-#ifndef USLEEP
-#define USLEEP
-void usleep(__int64 usec);
-#endif
-
-#ifndef S_ISDIR
-#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
-#endif
-
-#define PARCLibrary_DISABLE_ATOMICS
-#include <BaseTsd.h>
-typedef SSIZE_T ssize_t;
-
-#ifndef __ATTRIBUTE__
-#define __ATTRIBUTE__
-#define __attribute__(A)
-#endif
-
-#ifndef RESTRICT
-#define RESTRICT
-#define restrict __restrict
-#endif
-
-#ifndef GETTIMEOFDAY
-#define GETTIMEOFDAY
-int gettimeofday(struct timeval * tp, struct timezone * tzp);
-#endif
-
-#ifndef timersub
-#define timersub(a, b, result) \
- do { \
- (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
- (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
- if ((result)->tv_usec < 0) { \
- --(result)->tv_sec; \
- (result)->tv_usec += 1000000; \
- } \
- } while (0)
-#endif // timersub
-
-#ifndef dup
-#define dup _dup
-#endif
-
-#ifndef access
-#define access _access
-#endif
-
-#ifndef __cplusplus
-
-#ifndef read
-#define read _read
-#endif
-
-#ifndef close
-#define close _close
-#endif
-
-#ifndef write
-#define write _write
-#endif
-
-#ifndef open
-#define open _open
-#endif
-
-#endif
-
-#ifndef unlink
-#define unlink _unlink
-#endif
-
-#ifndef strcasecmp
-#define strncasecmp _strnicmp
-#endif
-
-#ifndef strcasecmp
-
-#define strcasecmp _stricmp
-#endif
-
-#ifndef S_ISREG
-#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
-#endif
-#ifndef R_OK
-#define R_OK 4 /* Test for read permission. */
-#endif
-#ifndef W_OK
-#define W_OK 2 /* Test for write permission. */
-#endif
-#ifndef F_OK
-#define F_OK 0
-#endif
-
-#ifndef STDIN_FILENO
-#define STDIN_FILENO _fileno(stdin)
-#endif
-
-#ifndef STDOUT_FILENO
-#define STDOUT_FILENO _fileno(stdout)
-#endif
-
-#ifndef STDERR_FILENO
-#define STDERR_FILENO _fileno(stderr)
-#endif
-
-#endif
-
-#ifndef __bswap_constant_32
-#define __bswap_constant_32(x) \
- ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) \
- | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
-#endif
+/* + * 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. + */ + +#ifndef WINDOWS_UTILS_H +#define WINDOWS_UTILS_H +#define WIN32_LEAN_AND_MEAN +#define HAVE_STRUCT_TIMESPEC +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include <Windows.h> +#include <stdint.h> +#include <io.h> +#include <stdlib.h> +#include <winsock2.h> +#include <WS2tcpip.h> +#include "dlfcn.h" + +#ifndef IOVEC +#define IOVEC +struct iovec +{ + void *iov_base; + size_t iov_len; +}; +#endif + +typedef uint16_t in_port_t; + +#ifndef SLEEP +#define SLEEP +#define sleep Sleep +#endif + +#ifndef USLEEP +#define USLEEP +void usleep (__int64 usec); +#endif + +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) &S_IFMT) == S_IFDIR) +#endif + +#define PARCLibrary_DISABLE_ATOMICS +#include <BaseTsd.h> +typedef SSIZE_T ssize_t; + +#ifndef __ATTRIBUTE__ +#define __ATTRIBUTE__ +#define __attribute__(A) +#endif + +#ifndef RESTRICT +#define RESTRICT +#define restrict __restrict +#endif + +#ifndef GETTIMEOFDAY +#define GETTIMEOFDAY +int gettimeofday (struct timeval *tp, struct timezone *tzp); +#endif + +#ifndef timersub +#define timersub(a, b, result) \ + do \ + { \ + (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ + if ((result)->tv_usec < 0) \ + { \ + --(result)->tv_sec; \ + (result)->tv_usec += 1000000; \ + } \ + } \ + while (0) +#endif // timersub + +#ifndef dup +#define dup _dup +#endif + +#ifndef access +#define access _access +#endif + +#ifndef __cplusplus + +#ifndef read +#define read _read +#endif + +#ifndef close +#define close _close +#endif + +#ifndef write +#define write _write +#endif + +#ifndef open +#define open _open +#endif + +#endif + +#ifndef unlink +#define unlink _unlink +#endif + +#ifndef strcasecmp +#define strncasecmp _strnicmp +#endif + +#ifndef strcasecmp + +#define strcasecmp _stricmp +#endif + +#ifndef S_ISREG +#define S_ISREG(mode) (((mode) &S_IFMT) == S_IFREG) +#endif +#ifndef R_OK +#define R_OK 4 /* Test for read permission. */ +#endif +#ifndef W_OK +#define W_OK 2 /* Test for write permission. */ +#endif +#ifndef F_OK +#define F_OK 0 +#endif + +#ifndef STDIN_FILENO +#define STDIN_FILENO _fileno (stdin) +#endif + +#ifndef STDOUT_FILENO +#define STDOUT_FILENO _fileno (stdout) +#endif + +#ifndef STDERR_FILENO +#define STDERR_FILENO _fileno (stderr) +#endif + +#endif + +#ifndef __bswap_constant_32 +#define __bswap_constant_32(x) \ + ((((x) &0xff000000u) >> 24) | (((x) &0x00ff0000u) >> 8) | \ + (((x) &0x0000ff00u) << 8) | (((x) &0x000000ffu) << 24)) +#endif + +#ifndef bzero +#define bzero(b, len) (memset ((b), '\0', (len)), (void) 0) +#endif
\ No newline at end of file diff --git a/lib/includes/hicn/validation.h b/lib/includes/hicn/validation.h new file mode 100644 index 000000000..05be6d306 --- /dev/null +++ b/lib/includes/hicn/validation.h @@ -0,0 +1,55 @@ +/* + * 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. + */ + +/** + * \file validation.h + * \brief Functions for input validation + */ +#include <ctype.h> +#include <hicn/util/sstrncpy.h> + +static inline bool +is_number (const char *string, size_t maxlen) +{ + size_t len = strnlen_s (string, maxlen); + for (size_t i = 0; i < len; i++) + { + if (!isdigit (string[i])) + return false; + } + return len != 0; +} + +/** + * A symbolic name must be at least 1 character and must begin with an alpha. + * The remainder must be an alphanum. + */ +static inline bool +is_symbolic_name (const char *name, size_t maxlen) +{ + size_t len = strnlen_s (name, maxlen); + if (len <= 0) + return false; + if (!isalpha (name[0])) + return false; + + for (size_t i = 1; i < len; i++) + { + if (!isalnum (name[i]) && name[i] != '_' && name[i] != '-') + return false; + } + + return true; +} |