From 229385955109b866a23c4ac2aa03d4d11044c39d Mon Sep 17 00:00:00 2001 From: "Enrico Loparco (eloparco)" Date: Thu, 24 Jun 2021 09:15:41 +0200 Subject: [HICN-708] Rebase with master Signed-off-by: Enrico Loparco (eloparco) Change-Id: I2122e1d61dd3b2e039972624ffbdbcb3c5610159 --- ctrl/libhicnctrl/cmake/Modules/Packaging.cmake | 4 +- ctrl/libhicnctrl/includes/hicn/ctrl/api.h | 5 +- ctrl/libhicnctrl/includes/hicn/ctrl/commands.h | 97 +--- ctrl/libhicnctrl/src/CMakeLists.txt | 8 +- ctrl/libhicnctrl/src/api.c | 325 +++++++------ ctrl/libhicnctrl/src/hicn_plugin_api.c | 641 +++++++++++++------------ ctrl/libhicnctrl/src/hicnctrl.c | 2 +- 7 files changed, 532 insertions(+), 550 deletions(-) (limited to 'ctrl/libhicnctrl') diff --git a/ctrl/libhicnctrl/cmake/Modules/Packaging.cmake b/ctrl/libhicnctrl/cmake/Modules/Packaging.cmake index 92e9d4adb..bf0a4d504 100644 --- a/ctrl/libhicnctrl/cmake/Modules/Packaging.cmake +++ b/ctrl/libhicnctrl/cmake/Modules/Packaging.cmake @@ -32,7 +32,7 @@ if (BUILD_HICNPLUGIN AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") ) set(${LIBHICNCTRL_COMPONENT}-dev_DEB_DEPENDENCIES - "${LIBHICNCTRL_COMPONENT} (>= stable_version), hicn-plugin-dev (>= stable_version-release), hicn-plugin-dev (<< next_version-release)" + "${LIBHICNCTRL_COMPONENT} (>= stable_version), hicn-plugin-dev (>= stable_version)" CACHE STRING "Dependencies for deb/rpm package." ) @@ -42,7 +42,7 @@ if (BUILD_HICNPLUGIN AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") ) set(${LIBHICNCTRL_COMPONENT}-dev_RPM_DEPENDENCIES - "${LIBHICNCTRL_COMPONENT} >= stable_version, hicn-plugin-dev >= stable_version-release, hicn-plugin-dev < next_version-release" + "${LIBHICNCTRL_COMPONENT} >= stable_version, hicn-plugin-dev >= stable_version" CACHE STRING "Dependencies for deb/rpm package." ) diff --git a/ctrl/libhicnctrl/includes/hicn/ctrl/api.h b/ctrl/libhicnctrl/includes/hicn/ctrl/api.h index 3771b3abd..ebb4ee007 100644 --- a/ctrl/libhicnctrl/includes/hicn/ctrl/api.h +++ b/ctrl/libhicnctrl/includes/hicn/ctrl/api.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2017-2020 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: @@ -636,6 +636,7 @@ typedef struct { ip_address_t remote_addr; /* krw */ u8 len; /* krw */ u16 cost; /* .rw */ + // XXX hc_face_t face; } hc_route_t; int hc_route_parse(void *in, hc_route_t *route); @@ -743,7 +744,7 @@ typedef struct { int family; /* Krw */ ip_address_t remote_addr; /* krw */ u8 len; /* krw */ - policy_t policy; /* .rw */ + hicn_policy_t policy; /* .rw */ } hc_policy_t; int hc_policy_parse(void *in, hc_policy_t *policy); diff --git a/ctrl/libhicnctrl/includes/hicn/ctrl/commands.h b/ctrl/libhicnctrl/includes/hicn/ctrl/commands.h index 767ef98a3..472c237b4 100644 --- a/ctrl/libhicnctrl/includes/hicn/ctrl/commands.h +++ b/ctrl/libhicnctrl/includes/hicn/ctrl/commands.h @@ -20,8 +20,8 @@ * Header and payload in binary format. */ -#ifndef commands_h -#define commands_h +#ifndef HICN_CTRL_COMMANDS_H +#define HICN_CTRL_COMMANDS_H #ifndef _WIN32 #include @@ -31,31 +31,14 @@ #include #include -#include #ifdef WITH_POLICY #include #endif /* WITH_POLICY */ +#include +#include -#define SYMBOLIC_NAME_LEN 16 -#define MAX_FWD_STRATEGY_RELATED_PREFIXES 10 - -typedef struct in6_addr ipv6_addr_t; -typedef uint32_t ipv4_addr_t; - -typedef enum { - ADDR_INET = 1, - ADDR_INET6, - ADDR_LINK, - ADDR_IFACE, - ADDR_UNIX /* PF_UNIX */ -} address_type; -typedef enum { - UDP_CONN, - TCP_CONN, - GRE_CONN, // not implemented - HICN_CONN -} connection_type; +#define SYMBOLIC_NAME_LEN 16 typedef struct in6_addr ipv6_addr_t; typedef uint32_t ipv4_addr_t; @@ -118,14 +101,12 @@ typedef enum { #define command_type_from_uchar(x) \ (((x) >= COMMAND_TYPE_N) ? COMMAND_TYPE_N : (command_type_t)(x)) -/* Header */ - /* Should be at least 8 bytes */ typedef struct { - uint8_t messageType; - uint8_t commandID ; + uint8_t message_type; + uint8_t command_id; uint16_t length; /* Number of structures in the payload */ - uint32_t seqNum; + uint32_t seq_num; } cmd_header_t; typedef struct { @@ -134,19 +115,13 @@ typedef struct { /* Listener */ -typedef enum { ETHER_MODE, IP_MODE, HICN_MODE } listener_mode; - typedef struct { char symbolic[SYMBOLIC_NAME_LEN]; - char interfaceName[SYMBOLIC_NAME_LEN]; + char interface_name[SYMBOLIC_NAME_LEN]; ip_address_t address; uint16_t port; - // uint16_t etherType; - uint8_t addressType; - uint8_t listenerMode; - uint8_t connectionType; uint8_t family; - uint8_t listenerType; + uint8_t type; } cmd_listener_add_t; typedef struct { @@ -154,13 +129,6 @@ typedef struct { } cmd_listener_remove_t; typedef struct { - ip_address_t address; - char listenerName[SYMBOLIC_NAME_LEN]; - char interfaceName[SYMBOLIC_NAME_LEN]; - uint32_t connid; - uint16_t port; - uint8_t addressType; - uint8_t encapType; } cmd_listener_list_t; typedef struct { @@ -184,7 +152,6 @@ typedef struct { uint16_t local_port; uint8_t family; uint8_t type; - uint8_t connection_type; uint8_t admin_state; #ifdef WITH_POLICY uint32_t priority; @@ -196,27 +163,7 @@ typedef struct { char symbolicOrConnid[SYMBOLIC_NAME_LEN]; } cmd_connection_remove_t; -typedef enum { - CONN_GRE, - CONN_TCP, - CONN_UDP, - CONN_MULTICAST, - CONN_L2, - CONN_HICN -} list_connections_type; - -typedef enum { - IFACE_UP = 0, - IFACE_DOWN = 1, - IFACE_UNKNOWN = 2 // not used actually -} connection_state; - typedef struct { - cmd_connection_add_t connectionData; - uint32_t connid; - uint8_t state; - char interfaceName[SYMBOLIC_NAME_LEN]; - char connectionName[SYMBOLIC_NAME_LEN]; } cmd_connection_list_t; typedef struct { @@ -236,7 +183,7 @@ typedef struct { uint32_t id; uint8_t state; char interface_name[SYMBOLIC_NAME_LEN]; - char name[SYMBOLIC_NAME_LEN]; + char name[SYMBOLIC_NAME_LEN]; // XXX what is this ? } cmd_connection_list_item_t; typedef struct { @@ -268,7 +215,6 @@ typedef struct { char symbolicOrConnid[SYMBOLIC_NAME_LEN]; ip_address_t address; uint16_t cost; - uint8_t addressType; uint8_t family; uint8_t len; } cmd_route_add_t; @@ -276,17 +222,11 @@ typedef struct { typedef struct { char symbolicOrConnid[SYMBOLIC_NAME_LEN]; ip_address_t address; - uint8_t addressType; uint8_t family; uint8_t len; } cmd_route_remove_t; typedef struct { - ip_address_t address; - uint32_t connid; - uint16_t cost; - uint8_t addressType; - uint8_t len; } cmd_route_list_t; typedef struct { @@ -322,7 +262,6 @@ typedef struct { typedef struct { ip_address_t address; uint8_t strategy_type; - uint8_t address_type; uint8_t family; uint8_t len; uint8_t related_prefixes; @@ -340,7 +279,6 @@ typedef struct { typedef struct { char symbolicOrConnid[SYMBOLIC_NAME_LEN]; ip_address_t address; - uint8_t addressType; uint8_t family; uint8_t len; } cmd_punting_add_t; @@ -367,40 +305,31 @@ typedef struct { uint8_t len; } cmd_mapme_send_update_t; -#ifdef WITH_POLICY /* Policy */ typedef struct { ip_address_t address; - uint8_t addressType; uint8_t family; uint8_t len; - policy_t policy; + hicn_policy_t policy; } cmd_policy_add_t; typedef struct { ip_address_t address; - uint8_t addressType; uint8_t family; uint8_t len; } cmd_policy_remove_t; typedef struct { - ip_address_t address; - uint8_t addressType; - uint8_t len; - policy_t policy; } cmd_policy_list_t; typedef struct { ip_address_t address; uint8_t family; uint8_t len; - policy_t policy; + hicn_policy_t policy; } cmd_policy_list_item_t; -#endif /* WITH_POLICY */ - /* Full messages */ #define _(l, u) \ diff --git a/ctrl/libhicnctrl/src/CMakeLists.txt b/ctrl/libhicnctrl/src/CMakeLists.txt index 1a64296e8..054ae3bd8 100644 --- a/ctrl/libhicnctrl/src/CMakeLists.txt +++ b/ctrl/libhicnctrl/src/CMakeLists.txt @@ -16,6 +16,8 @@ list(APPEND COMPILER_DEFINITIONS ) set(HEADER_FILES + api.h + cli.h commands.h ) @@ -24,8 +26,8 @@ set(UTIL_HEADER_FILES set(SOURCE_FILES hicnctrl.c - cli.c route.c + cli.c ) if(BUILD_HICNPLUGIN) @@ -67,7 +69,7 @@ build_library(${LIBHICNCTRL} DEPENDS ${DEPENDENCIES} COMPONENT ${LIBHICNCTRL_COMPONENT} INCLUDE_DIRS ${INCLUDE_DIRS} - INSTALL_ROOT_DIR hicn + HEADER_ROOT_DIR hicn DEFINITIONS ${COMPILER_DEFINITIONS} ) @@ -75,7 +77,7 @@ if (NOT DISABLE_EXECUTABLES) set(LIBRARIES ${LIBRARIES} ${LIBHICN_SHARED} ${LIBHICNCTRL_SHARED}) list(APPEND DAEMON_SRC - cli.c + hicnctrl.c ) build_executable(${HICNCTRL} diff --git a/ctrl/libhicnctrl/src/api.c b/ctrl/libhicnctrl/src/api.c index ef9f4af65..83c493fe3 100644 --- a/ctrl/libhicnctrl/src/api.c +++ b/ctrl/libhicnctrl/src/api.c @@ -56,7 +56,7 @@ typedef struct { } hc_sock_request_t; /** - * Messages to the forwarder might be multiplexed thanks to the seqNum fields in + * Messages to the forwarder might be multiplexed thanks to the seq_num fields in * the cmd_header_t structure. The forwarder simply answers back the * original sequence number. We maintain a map of such sequence number to * outgoing queries so that replied can be demultiplexed and treated @@ -184,6 +184,7 @@ connection_type_from_str(const char * str) #define IS_VALID_LIST_CONNECTIONS_TYPE(x) ((x >= CONN_GRE) && (x <= CONN_HICN)) +#if 0 static const hc_connection_type_t map_from_list_connections_type[] = { [CONN_GRE] = CONNECTION_TYPE_UNDEFINED, [CONN_TCP] = CONNECTION_TYPE_TCP, @@ -224,6 +225,7 @@ static const listener_mode map_to_listener_mode[] = { }; #define IS_VALID_LIST_CONNECTIONS_STATE(x) ((x >= IFACE_UP) && (x <= IFACE_UNKNOWN)) +#endif /* /!\ Please update constants in header file upon changes */ const char * connection_state_str[] = { @@ -242,11 +244,13 @@ static const connection_state map_to_connection_state[] = { */ +#if 0 static const hc_connection_state_t map_from_list_connections_state[] = { [IFACE_UP] = HC_CONNECTION_STATE_UP, [IFACE_DOWN] = HC_CONNECTION_STATE_DOWN, [IFACE_UNKNOWN] = HC_CONNECTION_STATE_UNDEFINED, }; +#endif #define connection_state_to_face_state(x) ((face_state_t)(x)) @@ -254,6 +258,7 @@ static const hc_connection_state_t map_from_list_connections_state[] = { #define IS_VALID_ADDR_TYPE(x) ((x >= ADDR_INET) && (x <= ADDR_UNIX)) +#if 0 static const int map_from_addr_type[] = { [ADDR_INET] = AF_INET, [ADDR_INET6] = AF_INET6, @@ -266,6 +271,15 @@ static const address_type map_to_addr_type[] = { [AF_INET] = ADDR_INET, [AF_INET6] = ADDR_INET6, }; +#endif + +// XXX Those are always true +// +#define IS_VALID_ADDRESS(x) (1) +#define IS_VALID_CONNECTION_ID(x) (1) +#define IS_VALID_ROUTE_COST(x) (1) +#define IS_VALID_PREFIX_LEN(x) (1) +#define IS_VALID_POLICY(x) (1) /****************************************************************************** * Message helper types and aliases @@ -588,7 +602,7 @@ int hc_sock_send(hc_sock_t * s, hc_msg_t * msg, size_t msglen, int seq) { int rc; - msg->hdr.seqNum = seq; + msg->hdr.seq_num = seq; rc = (int)send(s->fd, msg, msglen, 0); if (rc < 0) { perror("hc_sock_send"); @@ -657,7 +671,7 @@ hc_sock_process(hc_sock_t * s, hc_data_t ** data) } hc_sock_request_t * request = NULL; - if (hc_sock_map_get(s->map, hdr.seqNum, &request) < 0) { + if (hc_sock_map_get(s->map, hdr.seq_num, &request) < 0) { ERROR("[hc_sock_process] Error searching for matching request"); return -99; } @@ -667,7 +681,7 @@ hc_sock_process(hc_sock_t * s, hc_data_t ** data) } s->remaining = hdr.length; - switch(hdr.messageType) { + switch(hdr.message_type) { case ACK_LIGHT: DEBUG("ack received"); assert(s->remaining == 1); @@ -971,19 +985,16 @@ _hc_listener_create(hc_sock_t * s, hc_listener_t * listener, bool async) msg_listener_add_t msg = { .header = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_LISTENER_ADD, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_LISTENER_ADD, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = listener->local_addr, .port = htons(listener->local_port), - .addressType = (u8)map_to_addr_type[listener->family], - .listenerMode = (u8)map_to_listener_mode[listener->type], - .connectionType = (u8)map_to_connection_type[listener->type], .family = listener->family, - .listenerType = listener->type, + .type = listener->type, } }; @@ -991,7 +1002,7 @@ _hc_listener_create(hc_sock_t * s, hc_listener_t * listener, bool async) if (rc >= SYMBOLIC_NAME_LEN) WARN("[_hc_listener_create] Unexpected truncation of symbolic name string"); - rc = snprintf(msg.payload.interfaceName, INTERFACE_LEN, "%s", listener->interface_name); + rc = snprintf(msg.payload.interface_name, INTERFACE_LEN, "%s", listener->interface_name); if (rc >= INTERFACE_LEN) WARN("[_hc_listener_create] Unexpected truncation of interface name string"); @@ -1074,10 +1085,10 @@ _hc_listener_delete(hc_sock_t * s, hc_listener_t * listener, bool async) cmd_listener_remove_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_LISTENER_REMOVE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_LISTENER_REMOVE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, }; @@ -1136,17 +1147,17 @@ _hc_listener_list(hc_sock_t * s, hc_data_t ** pdata, bool async) cmd_header_t hdr; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_LISTENER_LIST, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_LISTENER_LIST, .length = 0, - .seqNum = 0, + .seq_num = 0, }, }; hc_command_params_t params = { .cmd = ACTION_LIST, .cmd_id = COMMAND_TYPE_LISTENER_LIST, - .size_in = sizeof(cmd_listener_list_t), + .size_in = sizeof(cmd_listener_list_item_t), .size_out = sizeof(hc_listener_t), .parse = (HC_PARSE)hc_listener_parse, }; @@ -1217,33 +1228,31 @@ hc_listener_parse(void * in, hc_listener_t * listener) { int rc; - cmd_listener_list_t * cmd = (cmd_listener_list_t *)in; + cmd_listener_list_item_t * item = (cmd_listener_list_item_t *)in; - if (!IS_VALID_LIST_LISTENERS_TYPE(cmd->encapType)) - return -1; - - hc_connection_type_t type = map_from_encap_type[cmd->encapType]; - if (type == CONNECTION_TYPE_UNDEFINED) - return -1; + // XXX TODO validate what we received from hicnlight + // address + // name + // interface_name + // port - if (!IS_VALID_ADDR_TYPE(cmd->addressType)) + if (!(IS_VALID_CONNECTION_TYPE(item->type))) return -1; - int family = map_from_addr_type[cmd->addressType]; - if (!IS_VALID_FAMILY(family)) + if (!IS_VALID_FAMILY(item->family)) return -1; *listener = (hc_listener_t) { - .id = cmd->connid, - .type = type, - .family = family, - .local_addr = UNION_CAST(cmd->address, ip_address_t), - .local_port = ntohs(cmd->port), + .id = item->id, + .type = item->type, + .family = item->family, + .local_addr = UNION_CAST(item->address, ip_address_t), + .local_port = ntohs(item->port), }; - rc = snprintf(listener->name, SYMBOLIC_NAME_LEN, "%s", cmd->listenerName); + rc = snprintf(listener->name, SYMBOLIC_NAME_LEN, "%s", item->name); if (rc >= SYMBOLIC_NAME_LEN) WARN("[hc_listener_parse] Unexpected truncation of symbolic name string"); - rc = snprintf(listener->interface_name, INTERFACE_LEN, "%s", cmd->interfaceName); + rc = snprintf(listener->interface_name, INTERFACE_LEN, "%s", item->interface_name); if (rc >= INTERFACE_LEN) WARN("[hc_listener_parse] Unexpected truncation of interface name string"); return 0; @@ -1293,18 +1302,19 @@ _hc_connection_create(hc_sock_t * s, hc_connection_t * connection, bool async) cmd_connection_add_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_ADD, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_ADD, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { + // symbolic .remote_ip = connection->remote_addr, .local_ip = connection->local_addr, .remote_port = htons(connection->remote_port), .local_port = htons(connection->local_port), - .type = (u8)map_to_addr_type[connection->family], - .connection_type = (u8)map_to_connection_type[connection->type], + .family = connection->family, + .type = connection->type, .admin_state = connection->admin_state, #ifdef WITH_POLICY .priority = connection->priority, @@ -1315,7 +1325,7 @@ _hc_connection_create(hc_sock_t * s, hc_connection_t * connection, bool async) rc = snprintf(msg.payload.symbolic, SYMBOLIC_NAME_LEN, "%s", connection->name); if (rc >= SYMBOLIC_NAME_LEN) WARN("[_hc_connection_create] Unexpected truncation of symbolic name string"); - //snprintf(msg.payload.interfaceName, INTERFACE_NAME_LEN, "%s", connection->interface_name); + //snprintf(msg.payload.interface_name, INTERFACE_NAME_LEN, "%s", connection->interface_name); hc_command_params_t params = { .cmd = ACTION_CREATE, @@ -1395,10 +1405,10 @@ _hc_connection_delete(hc_sock_t * s, hc_connection_t * connection, bool async) cmd_connection_remove_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_REMOVE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_REMOVE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, }; @@ -1456,17 +1466,17 @@ _hc_connection_list(hc_sock_t * s, hc_data_t ** pdata, bool async) cmd_header_t hdr; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_LIST, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_LIST, .length = 0, - .seqNum = 0, + .seq_num = 0, }, }; hc_command_params_t params = { .cmd = ACTION_LIST, .cmd_id = COMMAND_TYPE_CONNECTION_LIST, - .size_in = sizeof(cmd_connection_list_t), + .size_in = sizeof(cmd_connection_list_item_t), .size_out = sizeof(hc_connection_t), .parse = (HC_PARSE)hc_connection_parse, }; @@ -1550,50 +1560,46 @@ int hc_connection_parse(void * in, hc_connection_t * connection) { int rc; - cmd_connection_list_t * cmd = (cmd_connection_list_t *)in; + cmd_connection_list_item_t * item = (cmd_connection_list_item_t *)in; - if (!IS_VALID_LIST_CONNECTIONS_TYPE(cmd->connectionData.connection_type)) - return -1; + // XXX validation + // symbolic + // remote_ip + // local_ip + // remote_port + // local_port - hc_connection_type_t type = map_from_list_connections_type[cmd->connectionData.connection_type]; - if (type == CONNECTION_TYPE_UNDEFINED) + if (!IS_VALID_FAMILY(item->family)) return -1; - if (!IS_VALID_LIST_CONNECTIONS_STATE(cmd->state)) - return -1; - - hc_connection_state_t state = map_from_list_connections_state[cmd->state]; - if (state == HC_CONNECTION_STATE_UNDEFINED) - return -1; - - if (!IS_VALID_ADDR_TYPE(cmd->connectionData.type)) - return -1; - - int family = map_from_addr_type[cmd->connectionData.type]; - if (!IS_VALID_FAMILY(family)) - return -1; + // type + // admin_state + // priority + // tags + // id + // state + // interface_name + // name ??? *connection = (hc_connection_t) { - .id = cmd->connid, - .type = type, - .family = family, - .local_addr = cmd->connectionData.local_ip, - //.local_addr = UNION_CAST(cmd->connectionData.localIp, ip_address_t), - .local_port = ntohs(cmd->connectionData.local_port), - .remote_addr = cmd->connectionData.remote_ip, - //.remote_addr = UNION_CAST(cmd->connectionData.remoteIp, ip_address_t), - .remote_port = ntohs(cmd->connectionData.remote_port), - .admin_state = cmd->connectionData.admin_state, + .id = item->id, + .type = item->type, + .family = item->family, + .local_addr = item->local_ip, + .local_port = ntohs(item->local_port), + .remote_addr = item->remote_ip, + .remote_port = ntohs(item->remote_port), + .admin_state = item->admin_state, #ifdef WITH_POLICY - .priority = cmd->connectionData.priority, - .tags = cmd->connectionData.tags, + .priority = item->priority, + .tags = item->tags, #endif /* WITH_POLICY */ - .state = state, + .state = item->state, }; - rc = snprintf(connection->name, SYMBOLIC_NAME_LEN, "%s", cmd->connectionData.symbolic); + rc = snprintf(connection->name, SYMBOLIC_NAME_LEN, "%s", item->symbolic); if (rc >= SYMBOLIC_NAME_LEN) WARN("[hc_connection_parse] Unexpected truncation of symbolic name string"); - rc = snprintf(connection->interface_name, INTERFACE_LEN, "%s", cmd->interfaceName); + rc = snprintf(connection->interface_name, INTERFACE_LEN, "%s", item->interface_name); if (rc >= INTERFACE_LEN) WARN("[hc_connection_parse] Unexpected truncation of interface name string"); return 0; @@ -1648,10 +1654,10 @@ _hc_connection_set_admin_state(hc_sock_t * s, const char * conn_id_or_name, cmd_connection_set_admin_state_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_SET_ADMIN_STATE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_SET_ADMIN_STATE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .admin_state = state, @@ -1698,10 +1704,10 @@ _hc_connection_set_priority(hc_sock_t * s, const char * conn_id_or_name, cmd_connection_set_priority_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_SET_PRIORITY, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_SET_PRIORITY, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .priority = priority, @@ -1748,10 +1754,10 @@ _hc_connection_set_tags(hc_sock_t * s, const char * conn_id_or_name, cmd_connection_set_tags_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CONNECTION_SET_TAGS, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CONNECTION_SET_TAGS, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .tags = tags, @@ -1812,15 +1818,15 @@ _hc_route_create(hc_sock_t * s, hc_route_t * route, bool async) cmd_route_add_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_ROUTE_ADD, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_ROUTE_ADD, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = route->remote_addr, .cost = route->cost, - .addressType = (u8)map_to_addr_type[route->family], + .family = route->family, .len = route->len, } }; @@ -1875,14 +1881,14 @@ _hc_route_delete(hc_sock_t * s, hc_route_t * route, bool async) cmd_route_remove_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_ROUTE_REMOVE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_ROUTE_REMOVE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = route->remote_addr, - .addressType = (u8)map_to_addr_type[route->family], + .family = route->family, .len = route->len, } }; @@ -1927,17 +1933,17 @@ _hc_route_list(hc_sock_t * s, hc_data_t ** pdata, bool async) cmd_header_t hdr; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_ROUTE_LIST, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_ROUTE_LIST, .length = 0, - .seqNum = 0, + .seq_num = 0, }, }; hc_command_params_t params = { .cmd = ACTION_LIST, .cmd_id = COMMAND_TYPE_ROUTE_LIST, - .size_in = sizeof(cmd_route_list_t), + .size_in = sizeof(cmd_route_list_item_t), .size_out = sizeof(hc_route_t), .parse = (HC_PARSE)hc_route_parse, }; @@ -1962,25 +1968,35 @@ hc_route_list_async(hc_sock_t * s) int hc_route_parse(void * in, hc_route_t * route) { - cmd_route_list_t * cmd = (cmd_route_list_t *) in; + cmd_route_list_item_t * item = (cmd_route_list_item_t *) in; - if (!IS_VALID_ADDR_TYPE(cmd->addressType)) { - ERROR("[hc_route_parse] Invalid address type"); + if (!IS_VALID_ADDRESS(item->address)) { + ERROR("[hc_route_parse] Invalid address"); return -1; } - - int family = map_from_addr_type[cmd->addressType]; - if (!IS_VALID_FAMILY(family)) { + if (!IS_VALID_CONNECTION_ID(item->connection_id)) { + ERROR("[hc_route_parse] Invalid connection id"); + return -1; + } + if (!IS_VALID_ROUTE_COST(item->cost)) { + ERROR("[hc_route_parse] Invalid cost"); + return -1; + } + if (!IS_VALID_FAMILY(item->family)) { ERROR("[hc_route_parse] Invalid address family"); return -1; } + if (!IS_VALID_PREFIX_LEN(item->len)) { + ERROR("[hc_route_parse] Invalid len"); + return -1; + } *route = (hc_route_t) { - .face_id = cmd->connid, - .family = family, - .remote_addr = UNION_CAST(cmd->address, ip_address_t), - .len = cmd->len, - .cost = cmd->cost, + .face_id = item->connection_id, + .family = item->family, + .remote_addr = item->address, + .len = item->len, + .cost = item->cost, }; return 0; } @@ -2521,17 +2537,17 @@ hc_face_list_async(hc_sock_t * s) cmd_header_t hdr; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, + .message_type = REQUEST_LIGHT, COMMAND_TYPE_CONNECTION_LIST, .length = 0, - .seqNum = 0, + .seq_num = 0, }, }; hc_command_params_t params = { .cmd = ACTION_LIST, .cmd_id = COMMAND_TYPE_CONNECTION_LIST, - .size_in = sizeof(cmd_connection_list_t), + .size_in = sizeof(cmd_connection_list_item_t), .size_out = sizeof(hc_face_t), .parse = (HC_PARSE)hc_connection_parse_to_face, }; @@ -2662,14 +2678,14 @@ _hc_punting_create(hc_sock_t * s, hc_punting_t * punting, bool async) cmd_punting_add_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_PUNTING_ADD, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_PUNTING_ADD, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = punting->prefix, - .addressType = (u8)map_to_addr_type[punting->family], + .family = punting->family, .len = punting->prefix_len, } }; @@ -2783,10 +2799,10 @@ _hc_cache_set_store(hc_sock_t * s, int enabled, bool async) cmd_cache_set_store_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CACHE_SET_STORE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CACHE_SET_STORE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .activate = enabled, @@ -2824,10 +2840,10 @@ _hc_cache_set_serve(hc_sock_t * s, int enabled, bool async) cmd_cache_set_serve_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_CACHE_SET_SERVE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_CACHE_SET_SERVE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .activate = enabled, @@ -2961,14 +2977,14 @@ _hc_policy_create(hc_sock_t * s, hc_policy_t * policy, bool async) cmd_policy_add_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, + .message_type = REQUEST_LIGHT, COMMAND_TYPE_POLICY_ADD, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = policy->remote_addr, - .addressType = (u8)map_to_addr_type[policy->family], + .family = policy->family, .len = policy->len, .policy = policy->policy, } @@ -3010,14 +3026,14 @@ _hc_policy_delete(hc_sock_t * s, hc_policy_t * policy, bool async) cmd_policy_remove_t payload; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_POLICY_REMOVE, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_POLICY_REMOVE, .length = 1, - .seqNum = 0, + .seq_num = 0, }, .payload = { .address = policy->remote_addr, - .addressType = (u8)map_to_addr_type[policy->family], + .family = policy->family, .len = policy->len, } }; @@ -3054,17 +3070,17 @@ _hc_policy_list(hc_sock_t * s, hc_data_t ** pdata, bool async) cmd_header_t hdr; } msg = { .hdr = { - .messageType = REQUEST_LIGHT, - .commandID = COMMAND_TYPE_POLICY_LIST, + .message_type = REQUEST_LIGHT, + .command_id = COMMAND_TYPE_POLICY_LIST, .length = 0, - .seqNum = 0, + .seq_num = 0, }, }; hc_command_params_t params = { .cmd = ACTION_LIST, .cmd_id = COMMAND_TYPE_POLICY_LIST, - .size_in = sizeof(cmd_policy_list_t), + .size_in = sizeof(cmd_policy_list_item_t), .size_out = sizeof(hc_policy_t), .parse = (HC_PARSE)hc_policy_parse, }; @@ -3089,20 +3105,30 @@ hc_policy_list_async(hc_sock_t * s, hc_data_t ** pdata) int hc_policy_parse(void * in, hc_policy_t * policy) { - cmd_policy_list_t * cmd = (cmd_policy_list_t *) in; - - if (!IS_VALID_ADDR_TYPE(cmd->addressType)) - return -1; + cmd_policy_list_item_t * item = (cmd_policy_list_item_t *) in; - int family = map_from_addr_type[cmd->addressType]; - if (!IS_VALID_FAMILY(family)) - return -1; + if (!IS_VALID_ADDRESS(item->address)) { + ERROR("[hc_policy_parse] Invalid address"); + return -1; + } + if (!IS_VALID_FAMILY(item->family)) { + ERROR("[hc_policy_parse] Invalid family"); + return -1; + } + if (!IS_VALID_PREFIX_LEN(item->len)) { + ERROR("[hc_policy_parse] Invalid len"); + return -1; + } + if (!IS_VALID_POLICY(item->policy)) { + ERROR("[hc_policy_parse] Invalid policy"); + return -1; + } *policy = (hc_policy_t) { - .family = family, - .remote_addr = UNION_CAST(cmd->address, ip_address_t), - .len = cmd->len, - .policy = cmd->policy, + .family = item->family, + .remote_addr = item->address, + .len = item->len, + .policy = item->policy, }; return 0; } @@ -3159,4 +3185,3 @@ object_from_str(const char * object_str) #undef _ return OBJECT_UNDEFINED; } - diff --git a/ctrl/libhicnctrl/src/hicn_plugin_api.c b/ctrl/libhicnctrl/src/hicn_plugin_api.c index 04d01e2a6..231c491ba 100644 --- a/ctrl/libhicnctrl/src/hicn_plugin_api.c +++ b/ctrl/libhicnctrl/src/hicn_plugin_api.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2017-2020 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,13 +34,24 @@ #include #include #include +#include #include #include #include #include #include + + +#if __GNUC__ >= 9 +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +#endif + #include +#if __GNUC__ >= 9 +#pragma GCC diagnostic pop +#endif + #define APP_NAME "hicn_plugin" #define MAX_OUTSTANDING_REQUESTS 4 #define RESPONSE_QUEUE_SIZE 2 @@ -48,6 +59,7 @@ DEFINE_VAPI_MSG_IDS_HICN_API_JSON DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON DEFINE_VAPI_MSG_IDS_IP_API_JSON +DEFINE_VAPI_MSG_IDS_UDP_API_JSON typedef struct { vapi_ctx_t g_vapi_ctx_instance; @@ -61,7 +73,7 @@ vapi_skc_ctx_t vapi_skc = { /** * Messages to the forwarder might be multiplexed thanks to the seqNum fields in - * the cmd_header_t structure. The forwarder simply answers back the + * the header_control_message structure. The forwarder simply answers back the * original sequence number. We maintain a map of such sequence number to * outgoing queries so that replied can be demultiplexed and treated * appropriately. @@ -94,20 +106,10 @@ struct hc_sock_s { _(hicn_api_node_params_set_reply) \ _(hicn_api_node_params_get_reply) \ _(hicn_api_node_stats_get_reply) \ - _(hicn_api_face_add) \ - _(hicn_api_face_add_reply) \ - _(hicn_api_face_del) \ - _(hicn_api_face_del_reply) \ _(hicn_api_face_get) \ _(hicn_api_faces_details) \ _(hicn_api_face_stats_details) \ _(hicn_api_face_get_reply) \ - _(hicn_api_route_nhops_add) \ - _(hicn_api_route_nhops_add_reply) \ - _(hicn_api_route_del) \ - _(hicn_api_route_del_reply) \ - _(hicn_api_route_nhop_del) \ - _(hicn_api_route_nhop_del_reply) \ _(hicn_api_route_get) \ _(hicn_api_route_get_reply) \ _(hicn_api_routes_details) \ @@ -115,6 +117,7 @@ struct hc_sock_s { _(hicn_api_strategy_get) \ _(hicn_api_strategy_get_reply) + typedef vapi_type_msg_header2_t hc_msg_header_t; typedef union { @@ -622,12 +625,29 @@ int hc_connection_set_admin_state_async(hc_sock_t *s, * Routes *----------------------------------------------------------------------------*/ +vapi_error_e create_udp_tunnel_cb( vapi_ctx_t ctx, + void *callback_ctx, + vapi_error_e rv, + bool is_last, + vapi_payload_hicn_api_udp_tunnel_add_del_reply *reply) { + if (reply == NULL || rv != VAPI_OK) + return rv; + + if (reply->retval != VAPI_OK) + return reply->retval; + + u32 * uei = (u32*) callback_ctx; + *uei = reply->uei; + + return reply->retval; +} + /* ROUTE CREATE */ vapi_error_e parse_route_create( vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last, - vapi_payload_hicn_api_route_nhops_add_reply *reply) { + vapi_payload_ip_route_add_del_reply *reply) { if (reply == NULL || rv != VAPI_OK) return rv; @@ -637,31 +657,133 @@ vapi_error_e parse_route_create( vapi_ctx_t ctx, return reply->retval; } +vapi_error_e hicn_enable_cb( vapi_ctx_t ctx, + void *callback_ctx, + vapi_error_e rv, + bool is_last, + vapi_payload_hicn_api_enable_disable_reply *reply) { + if (reply == NULL || rv != VAPI_OK) + return rv; + + return reply->retval; +} + int _hc_route_create(hc_sock_t *s, hc_route_t *route, bool async) { if (!IS_VALID_FAMILY(route->family)) return -1; + int ret; vapi_lock(); - vapi_msg_hicn_api_route_nhops_add *hicnp_msg; - hicnp_msg = vapi_alloc_hicn_api_route_nhops_add(s->g_vapi_ctx_instance); - if (!hicnp_msg) return VAPI_ENOMEM; + vapi_msg_ip_route_add_del *hicnp_msg = vapi_alloc_ip_route_add_del(s->g_vapi_ctx_instance, 1); + + hicnp_msg->payload.is_add = 1; + if (route->family == AF_INET) { + memcpy(&hicnp_msg->payload.route.prefix.address.un.ip4[0], &route->remote_addr.v4, 4); + hicnp_msg->payload.route.prefix.address.af = ADDRESS_IP4; + } + else { + memcpy(&hicnp_msg->payload.route.prefix.address.un.ip6[0], &route->remote_addr.v6, 16); + hicnp_msg->payload.route.prefix.address.af = ADDRESS_IP6; + } + + hicnp_msg->payload.route.prefix.len = route->len; + + hicnp_msg->payload.route.paths[0].sw_if_index = ~0; + hicnp_msg->payload.route.paths[0].table_id = 0; + + hc_face_t *face = &(route->face); + switch (face->face.type) { + case FACE_TYPE_HICN: + { + if (ip46_address_is_ip4((ip46_address_t *)(&(face->face.remote_addr)))) { + memcpy(&(hicnp_msg->payload.route.paths[0].nh.address.ip4), &face->face.remote_addr.v4, sizeof(ip4_address_t)); + hicnp_msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP4; + } + else{ + memcpy(&(hicnp_msg->payload.route.paths[0].nh.address.ip6), &face->face.remote_addr.v6, sizeof(ip6_address_t)); + hicnp_msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP6; + } + + hicnp_msg->payload.route.paths[0].type = FIB_API_PATH_FLAG_NONE; + hicnp_msg->payload.route.paths[0].flags = FIB_API_PATH_FLAG_NONE; + + break; + } + case FACE_TYPE_UDP: + { + vapi_msg_hicn_api_udp_tunnel_add_del *msg = NULL; + u32 uei = ~0; + + if (ip46_address_is_ip4((ip46_address_t *)(&(face->face.remote_addr))) && + ip46_address_is_ip4((ip46_address_t *)(&(face->face.local_addr)))) { + + msg = vapi_alloc_hicn_api_udp_tunnel_add_del(s->g_vapi_ctx_instance); + memcpy(msg->payload.src_addr.un.ip4, &face->face.local_addr.v4, sizeof(ip4_address_t)); + msg->payload.src_addr.af = ADDRESS_IP4; + + memcpy(msg->payload.dst_addr.un.ip4, &face->face.remote_addr.v4, sizeof(ip4_address_t)); + msg->payload.dst_addr.af = ADDRESS_IP4; + + } else if (!ip46_address_is_ip4((ip46_address_t *)(&(route->face.face.remote_addr))) && + !ip46_address_is_ip4((ip46_address_t *)(&(route->face.face.local_addr)))) { + + msg = vapi_alloc_hicn_api_udp_tunnel_add_del(s->g_vapi_ctx_instance); + memcpy(msg->payload.src_addr.un.ip6, &face->face.local_addr.v6, sizeof(ip6_address_t)); + msg->payload.src_addr.af = ADDRESS_IP4; + + memcpy(msg->payload.dst_addr.un.ip6, &face->face.remote_addr.v6, sizeof(ip6_address_t)); + msg->payload.dst_addr.af = ADDRESS_IP6; + + } else { + //NOT IMPLEMENTED + ret = -1; + goto done; + } + + msg->payload.src_port = face->face.local_port; + msg->payload.dst_port = face->face.remote_port; + msg->payload.is_add = 1; + + int ret = vapi_hicn_api_udp_tunnel_add_del(s->g_vapi_ctx_instance, msg, create_udp_tunnel_cb, &uei); + + if(ret) { + vapi_msg_free(s->g_vapi_ctx_instance, hicnp_msg); + goto done; + } + + hicnp_msg->payload.route.paths[0].type = FIB_API_PATH_TYPE_UDP_ENCAP; + hicnp_msg->payload.route.paths[0].flags = FIB_API_PATH_FLAG_NONE; + hicnp_msg->payload.route.paths[0].nh.obj_id = uei; + break; + } + default: + ret = -1; + goto done; + } + + ret = vapi_ip_route_add_del(s->g_vapi_ctx_instance, hicnp_msg, parse_route_create, NULL); + + if (ret) + goto done; + + vapi_msg_hicn_api_enable_disable *msg = vapi_alloc_hicn_api_enable_disable(s->g_vapi_ctx_instance); if (route->family == AF_INET) { - memcpy(&hicnp_msg->payload.prefix.address.un.ip4[0], &route->remote_addr.v4, 4); + memcpy(&msg->payload.prefix.address.un.ip4[0], &route->remote_addr.v4, 4); + msg->payload.prefix.address.af = ADDRESS_IP4; } else { - memcpy(&hicnp_msg->payload.prefix.address.un.ip6[0], &route->remote_addr.v6, 16); + memcpy(&msg->payload.prefix.address.un.ip6[0], &route->remote_addr.v6, 16); + msg->payload.prefix.address.af = ADDRESS_IP6; } - hicnp_msg->payload.prefix.address.af = - route->family == AF_INET ? ADDRESS_IP4 : ADDRESS_IP6; - hicnp_msg->payload.prefix.len = route->len; - hicnp_msg->payload.face_ids[0] = route->face_id; - hicnp_msg->payload.n_faces = 1; - vapi_error_e ret = vapi_hicn_api_route_nhops_add(s->g_vapi_ctx_instance, hicnp_msg, parse_route_create, NULL); + msg->payload.prefix.len = route->len; + msg->payload.enable_disable = 1; + + ret = vapi_hicn_api_enable_disable(s->g_vapi_ctx_instance, msg, hicn_enable_cb, NULL); +done: vapi_unlock(); return ret; - } int hc_route_create(hc_sock_t *s, hc_route_t *route) { @@ -677,13 +799,10 @@ vapi_error_e parse_route_delete( vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last, - vapi_payload_hicn_api_route_nhop_del_reply *reply) { + vapi_payload_ip_route_add_del_reply *reply) { if (reply == NULL || rv != VAPI_OK) return rv; - if (reply->retval != VAPI_OK) - return reply->retval; - return reply->retval; } @@ -691,20 +810,56 @@ int _hc_route_delete(hc_sock_t *s, hc_route_t *route, bool async) { if (!IS_VALID_FAMILY(route->family)) return -1; vapi_lock(); - vapi_msg_hicn_api_route_nhop_del *hicnp_msg; - hicnp_msg = vapi_alloc_hicn_api_route_nhop_del(s->g_vapi_ctx_instance); + vapi_msg_ip_route_add_del *hicnp_msg = vapi_alloc_ip_route_add_del(s->g_vapi_ctx_instance, 1); - if (!hicnp_msg) return VAPI_ENOMEM; + hicnp_msg->payload.is_add = 0; + if (route->family == AF_INET) { + memcpy(&hicnp_msg->payload.route.prefix.address.un.ip4[0], &route->remote_addr.v4, 4); + hicnp_msg->payload.route.prefix.address.af = ADDRESS_IP4; + } + else { + memcpy(&hicnp_msg->payload.route.prefix.address.un.ip6[0], &route->remote_addr.v6, 16); + hicnp_msg->payload.route.prefix.address.af = ADDRESS_IP6; + } + + hicnp_msg->payload.route.prefix.len = route->len; + + hicnp_msg->payload.route.paths[0].sw_if_index = ~0; + hicnp_msg->payload.route.paths[0].table_id = 0; - memcpy(&hicnp_msg->payload.prefix.address.un.ip6[0], &route->remote_addr, 16); - hicnp_msg->payload.prefix.address.af = - route->family == AF_INET ? ADDRESS_IP4 : ADDRESS_IP6; - hicnp_msg->payload.prefix.len = route->len; - hicnp_msg->payload.faceid = route->face_id; + hc_face_t *face = &(route->face); + switch (face->face.type) { + case FACE_TYPE_HICN: + { + if (ip46_address_is_ip4((ip46_address_t *)(&(face->face.remote_addr)))) { + memcpy(&(hicnp_msg->payload.route.paths[0].nh.address.ip4), &face->face.remote_addr.v4, sizeof(ip4_address_t)); + hicnp_msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP4; + } + else{ + memcpy(&(hicnp_msg->payload.route.paths[0].nh.address.ip6), &face->face.remote_addr.v6, sizeof(ip6_address_t)); + hicnp_msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP6; + } + + hicnp_msg->payload.route.paths[0].type = FIB_API_PATH_FLAG_NONE; + hicnp_msg->payload.route.paths[0].flags = FIB_API_PATH_FLAG_NONE; + + break; + } + case FACE_TYPE_UDP: + { + hicnp_msg->payload.route.paths[0].type = FIB_API_PATH_TYPE_UDP_ENCAP; + hicnp_msg->payload.route.paths[0].flags = FIB_API_PATH_FLAG_NONE; + hicnp_msg->payload.route.paths[0].nh.obj_id = face->face.netdevice.index; + break; + } + default: + return -1; + } + + vapi_error_e ret = vapi_ip_route_add_del(s->g_vapi_ctx_instance, hicnp_msg, parse_route_delete, NULL); - int retval = vapi_hicn_api_route_nhop_del(s->g_vapi_ctx_instance, hicnp_msg, parse_route_delete, NULL); vapi_unlock(); - return retval; + return ret; } int hc_route_delete(hc_sock_t *s, hc_route_t *route) { @@ -715,18 +870,121 @@ int hc_route_delete_async(hc_sock_t *s, hc_route_t *route) { return _hc_route_delete(s, route, true); } +vapi_error_e parse_udp_encap_list( vapi_ctx_t ctx, + void *callback_ctx, + vapi_error_e rv, + bool is_last, + vapi_payload_udp_encap_details *reply) { + if (reply == NULL || rv != VAPI_OK) + return rv; + + hc_face_t * face = (hc_face_t *)callback_ctx; + + if (face->face.netdevice.index == reply->udp_encap.id) + { + switch(reply->udp_encap.src_ip.af) { + case ADDRESS_IP4: + { + memcpy(&face->face.local_addr.v4, &(reply->udp_encap.src_ip.un.ip4), sizeof(ip4_address_t)); + memcpy(&face->face.remote_addr.v4, &(reply->udp_encap.dst_ip.un.ip4), sizeof(ip4_address_t)); + break; + } + case ADDRESS_IP6: + { + memcpy(&face->face.local_addr.v6, &(reply->udp_encap.src_ip.un.ip6), sizeof(ip6_address_t)); + memcpy(&face->face.remote_addr.v6, &(reply->udp_encap.dst_ip.un.ip6), sizeof(ip6_address_t)); + break; + } + default: + break; + } + + face->face.local_port = reply->udp_encap.src_port; + face->face.remote_port = reply->udp_encap.dst_port; + } + return rv; +} + +int fill_face_with_info(hc_face_t * face, vapi_type_fib_path *path, hc_sock_t *s) { + switch(path->type){ + case FIB_API_PATH_FLAG_NONE: + { + face->face.type = FACE_TYPE_HICN; + switch(path->proto){ + case FIB_API_PATH_NH_PROTO_IP4: + memcpy(&face->face.remote_addr.v4, &(path->nh.address.ip4), sizeof(ip4_address_t)); + break; + case FIB_API_PATH_NH_PROTO_IP6: + memcpy(&face->face.remote_addr.v6, &(path->nh.address.ip6), sizeof(ip6_address_t)); + break; + default: + break; + } + face->face.netdevice.index = path->sw_if_index; + } + break; + case FIB_API_PATH_TYPE_UDP_ENCAP: + { + face->face.type = FACE_TYPE_UDP; + face->face.netdevice.index = clib_net_to_host_u32(path->nh.obj_id); + //vapi_msg_udp_encap_dump *msg; + //msg = vapi_alloc_udp_encap_dump(s->g_vapi_ctx_instance); + //vapi_udp_encap_dump(s->g_vapi_ctx_instance, msg, parse_udp_encap_list, face); + } + break; + default: + return -1; + } + return 0; +} + /* ROUTE LIST */ +typedef struct hicn_route_socket_s { + hc_data_t *data; + hc_sock_t *s; +} hicn_route_socket_t; + vapi_error_e parse_route_list( vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last, - vapi_payload_hicn_api_routes_details *reply) { + vapi_payload_ip_route_details *reply) { if (reply == NULL || rv != VAPI_OK) return rv; - if (reply->retval != VAPI_OK) - return reply->retval; + hicn_route_socket_t *rs = (hicn_route_socket_t *)callback_ctx; + hc_data_t *data = rs->data; + + u8 found = false; + for (int j = 0; j < reply->route.n_paths; j++){ + for (int i = 0; i < data->size && !found; i++) { + hc_route_t * route = &((hc_route_t*)(data->buffer))[i]; + + if(ip46_address_is_ip4((ip46_address_t *)&(route->remote_addr)) && + memcmp(route->remote_addr.v4.as_u8, reply->route.prefix.address.un.ip4, sizeof(ip4_address_t)) == 0 && + route->len == reply->route.prefix.len && route->face_id == ~0) { + fill_face_with_info(&(route->face), &reply->route.paths[j], rs->s); + found = true; + } else if (memcmp(route->remote_addr.v6.as_u8, reply->route.prefix.address.un.ip6, sizeof(ip6_address_t)) == 0 && + route->len == reply->route.prefix.len && route->face_id == ~0) { + fill_face_with_info(&(route->face), &reply->route.paths[j], rs->s); + found = true; + } + } + } + + return rv; +} + +vapi_error_e parse_hicn_route_list( vapi_ctx_t ctx, + void *callback_ctx, + vapi_error_e rv, + bool is_last, + vapi_payload_hicn_api_routes_details *reply) { + + if (reply == NULL || rv != VAPI_OK) + return rv; hc_data_t *data = (hc_data_t *)callback_ctx; @@ -742,7 +1000,7 @@ vapi_error_e parse_route_list( vapi_ctx_t ctx, for (int i = 0; i < reply->nfaces; i++) { hc_route_t * route = &((hc_route_t*)(data->buffer))[data->current]; - route->face_id = reply->faceids[i]; + route->face_id = ~0; route->cost = 1; route->len = reply->prefix.len; if (reply->prefix.address.af == ADDRESS_IP6) @@ -757,13 +1015,14 @@ vapi_error_e parse_route_list( vapi_ctx_t ctx, data->current++; } - return reply->retval; + return rv; } int _hc_route_list(hc_sock_t *s, hc_data_t **pdata, bool async) { vapi_lock(); - vapi_msg_hicn_api_routes_dump *hicnp_msg; - hicnp_msg = vapi_alloc_hicn_api_routes_dump(s->g_vapi_ctx_instance); + + vapi_msg_hicn_api_routes_dump *msg; + msg = vapi_alloc_hicn_api_routes_dump(s->g_vapi_ctx_instance); hc_data_t *data = hc_data_create(0, sizeof(hc_route_t),NULL); int ret = VAPI_OK; @@ -781,7 +1040,28 @@ int _hc_route_list(hc_sock_t *s, hc_data_t **pdata, bool async) { goto err_free; } - ret = vapi_hicn_api_routes_dump(s->g_vapi_ctx_instance, hicnp_msg, parse_route_list, data); + ret = vapi_hicn_api_routes_dump(s->g_vapi_ctx_instance, msg, parse_hicn_route_list, data); + + if (ret != VAPI_OK) + goto err_free; + + vapi_msg_ip_route_dump *hicnp_msg; + hicnp_msg = vapi_alloc_ip_route_dump(s->g_vapi_ctx_instance); + hicnp_msg->payload.table.table_id = 0; + hicnp_msg->payload.table.is_ip6 = 1; + + hicn_route_socket_t ctx = { + .data = data, + .s = s, + }; + + ret = vapi_ip_route_dump(s->g_vapi_ctx_instance, hicnp_msg, parse_route_list, &ctx); + + hicnp_msg = vapi_alloc_ip_route_dump(s->g_vapi_ctx_instance); + hicnp_msg->payload.table.table_id = 0; + hicnp_msg->payload.table.is_ip6 = 0; + + ret = vapi_ip_route_dump(s->g_vapi_ctx_instance, hicnp_msg, parse_route_list, &ctx); if (ret != VAPI_OK) goto err_free; @@ -880,278 +1160,23 @@ int hc_connection_to_local_listener(const hc_connection_t *connection, return 0; } -/* FACE CREATE */ -vapi_error_e parse_face_create( vapi_ctx_t ctx, - void *callback_ctx, - vapi_error_e rv, - bool is_last, - vapi_payload_hicn_api_face_add_reply *reply) { - - if (reply == NULL || rv != VAPI_OK) - return rv; - - if (reply->retval != VAPI_OK) - return reply->retval; - - hc_data_t *data = (hc_data_t *)callback_ctx; - - hc_face_t *output = (hc_face_t *)data->buffer; - - output->id = reply->faceid; - return reply->retval; -} - int hc_face_create(hc_sock_t *s, hc_face_t *face) { - - vapi_lock(); - vapi_msg_hicn_api_face_add *hicnp_msg; - hicnp_msg = vapi_alloc_hicn_api_face_add(s->g_vapi_ctx_instance); - - int retval = VAPI_OK; - if (!hicnp_msg) { - retval = VAPI_ENOMEM; - goto END; - } - - switch(face->face.type) { - case FACE_TYPE_HICN: - { - u8 check = ip46_address_is_ip4((ip46_address_t *)&(face->face.local_addr)) == ip46_address_is_ip4((ip46_address_t *)&(face->face.remote_addr)); - if (!check) { - retval = -1; - goto END; - } - - hicnp_msg->payload.type = IP_FACE; - if (ip46_address_is_ip4((ip46_address_t *)&(face->face.local_addr))) - { - memcpy(hicnp_msg->payload.face.ip.local_addr.un.ip4, face->face.local_addr.v4.as_u8, 4); - memcpy(hicnp_msg->payload.face.ip.remote_addr.un.ip4, face->face.remote_addr.v4.as_u8, 4); - hicnp_msg->payload.face.ip.local_addr.af = ADDRESS_IP4; - hicnp_msg->payload.face.ip.remote_addr.af = ADDRESS_IP4; - } - else - { - memcpy(hicnp_msg->payload.face.ip.local_addr.un.ip6, face->face.local_addr.v6.as_u8, 16); - memcpy(hicnp_msg->payload.face.ip.remote_addr.un.ip6, face->face.remote_addr.v6.as_u8, 16); - hicnp_msg->payload.face.ip.local_addr.af = ADDRESS_IP6; - hicnp_msg->payload.face.ip.remote_addr.af = ADDRESS_IP6; - } - hicnp_msg->payload.face.ip.swif = face->face.netdevice.index; - memcpy(hicnp_msg->payload.face.ip.if_name, face->face.netdevice.name, IFNAMSIZ); - break; - } - case FACE_TYPE_UDP: - { - u8 check = ip46_address_is_ip4((ip46_address_t *)&(face->face.local_addr)) == ip46_address_is_ip4((ip46_address_t *)&(face->face.remote_addr)); - if (!check) { - retval = -1; - goto END; - } - - hicnp_msg->payload.type = UDP_FACE; - if (ip46_address_is_ip4((ip46_address_t *)&(face->face.local_addr))) - { - memcpy(hicnp_msg->payload.face.udp.local_addr.un.ip4, face->face.local_addr.v4.as_u8, 4); - memcpy(hicnp_msg->payload.face.udp.remote_addr.un.ip4, face->face.remote_addr.v4.as_u8, 4); - hicnp_msg->payload.face.udp.local_addr.af = ADDRESS_IP4; - hicnp_msg->payload.face.udp.remote_addr.af = ADDRESS_IP4; - } - else - { - memcpy(hicnp_msg->payload.face.udp.local_addr.un.ip6, face->face.local_addr.v6.as_u8, 16); - memcpy(hicnp_msg->payload.face.udp.remote_addr.un.ip6, face->face.remote_addr.v6.as_u8, 16); - hicnp_msg->payload.face.udp.local_addr.af = ADDRESS_IP6; - hicnp_msg->payload.face.udp.remote_addr.af = ADDRESS_IP6; - } - hicnp_msg->payload.face.udp.lport = face->face.local_port; - hicnp_msg->payload.face.udp.rport = face->face.remote_port; - hicnp_msg->payload.face.udp.swif = face->face.netdevice.index; - memcpy(hicnp_msg->payload.face.udp.if_name, face->face.netdevice.name, IFNAMSIZ); - break; - } - default: - { - retval = -1; - goto END; - } - } - - hc_data_t *data = hc_data_create(0, sizeof(hc_face_t),NULL); - - if (!data) { - retval = -1; - goto END; - } - - data->buffer = malloc(sizeof(hc_face_t)); - data->out_element_size = sizeof(hc_face_t); - - if (!data->buffer) { - free (data); - retval = -1; - goto END; - } - - retval = vapi_hicn_api_face_add(s->g_vapi_ctx_instance, hicnp_msg, parse_face_create, data); - - if (retval != VAPI_OK) - goto END; - - face->id = ((hc_face_t *)data->buffer)->id; - - END: - vapi_unlock(); - return retval; -} - -vapi_error_e parse_face_delete( vapi_ctx_t ctx, - void *callback_ctx, - vapi_error_e rv, - bool is_last, - vapi_payload_hicn_api_face_del_reply *reply) { - - if (reply == NULL || rv != VAPI_OK) - return rv; - - return reply->retval; + ERROR("Face creation implemented."); + return -1; } int hc_face_delete(hc_sock_t *s, hc_face_t *face) { - vapi_msg_hicn_api_face_del *hicnp_msg; - vapi_lock(); - hicnp_msg = vapi_alloc_hicn_api_face_del(s->g_vapi_ctx_instance); - - if (!hicnp_msg) return VAPI_ENOMEM; - - hicnp_msg->payload.faceid = face->id; - - int retval = vapi_hicn_api_face_del(s->g_vapi_ctx_instance, hicnp_msg, parse_face_delete, NULL); - vapi_unlock(); - return retval; + ERROR("Face deletion not implemented."); + return -1; } /* FACE LIST */ -vapi_error_e parse_face_list( vapi_ctx_t ctx, - void *callback_ctx, - vapi_error_e rv, - bool is_last, - vapi_payload_hicn_api_faces_details *reply) { - - if (reply == NULL || rv != VAPI_OK) - return rv; - - if (reply->retval != VAPI_OK) - return reply->retval; - - hc_data_t *data = (hc_data_t *)callback_ctx; - - if (data->size == data->current) { - int new_size = data->size *2; - data->buffer = realloc(data->buffer, sizeof(hc_face_t) * (new_size)); - if (!data->buffer) - return VAPI_ENOMEM; - - data->size =new_size; - } - - int retval = VAPI_OK; - - hc_face_t * face = &((hc_face_t *)(data->buffer))[data->current]; - switch(reply->type) - { - case IP_FACE: - { - if (reply->face.ip.local_addr.af == ADDRESS_IP4) - { - memcpy(face->face.local_addr.v4.as_u8, reply->face.ip.local_addr.un.ip4, IPV4_ADDR_LEN); - memcpy(face->face.remote_addr.v4.as_u8, reply->face.ip.remote_addr.un.ip4, IPV4_ADDR_LEN); - } - else - { - memcpy(face->face.local_addr.v6.as_u8, reply->face.ip.local_addr.un.ip6, IPV6_ADDR_LEN); - memcpy(face->face.remote_addr.v6.as_u8, reply->face.ip.remote_addr.un.ip6, IPV6_ADDR_LEN); - } - face->face.type = FACE_TYPE_HICN; - face->id = reply->faceid; - face->face.netdevice.index = reply->face.ip.swif; - memcpy(face->face.netdevice.name, reply->face.ip.if_name, IFNAMSIZ); - break; - } - case UDP_FACE: - { - if (reply->face.ip.local_addr.af == ADDRESS_IP4) - { - memcpy(face->face.local_addr.v4.as_u8, reply->face.udp.local_addr.un.ip4, IPV4_ADDR_LEN); - memcpy(face->face.remote_addr.v4.as_u8, reply->face.udp.remote_addr.un.ip4, IPV4_ADDR_LEN); - } - else - { - memcpy(face->face.local_addr.v6.as_u8, reply->face.udp.local_addr.un.ip6, IPV6_ADDR_LEN); - memcpy(face->face.remote_addr.v6.as_u8, reply->face.udp.remote_addr.un.ip6, IPV6_ADDR_LEN); - } - face->face.local_port = reply->face.udp.lport; - face->face.remote_port = reply->face.udp.rport; - face->face.type = FACE_TYPE_UDP; - face->id = reply->faceid; - face->face.netdevice.index = reply->face.udp.swif; - memcpy(face->face.netdevice.name, reply->face.udp.if_name, IFNAMSIZ); - break; - } - default: - retval = -1; - } - if (!retval) - data->current++; - - return reply->retval; -} - int hc_face_list(hc_sock_t *s, hc_data_t **pdata) { - vapi_lock(); - vapi_msg_hicn_api_faces_dump *hicnp_msg; - hicnp_msg = vapi_alloc_hicn_api_faces_dump(s->g_vapi_ctx_instance); - - int retval = 0; - if (!hicnp_msg) { - retval = VAPI_ENOMEM; - goto END; - } - hc_data_t *data = hc_data_create(0, sizeof(hc_face_t),NULL); - - if (!data) { - retval = -1; - goto END; - } - - data->buffer = malloc(sizeof(hc_face_t)); - data->size = 1; - - if (!data->buffer) { - free (data); - retval = -1; - goto err; - } - - - retval = vapi_hicn_api_faces_dump(s->g_vapi_ctx_instance, hicnp_msg, parse_face_list, data); - *pdata = data; - - if (retval != VAPI_OK) - goto err; - - data->size = data->current; - vapi_unlock(); - return retval; - - err: - free(data); - END: - vapi_unlock(); - return retval; +ERROR("Face list not implemented."); +return -1; } int hc_connection_parse_to_face(void *in, hc_face_t *face) { return 0; } diff --git a/ctrl/libhicnctrl/src/hicnctrl.c b/ctrl/libhicnctrl/src/hicnctrl.c index 4be9f196c..e95b9b0eb 100644 --- a/ctrl/libhicnctrl/src/hicnctrl.c +++ b/ctrl/libhicnctrl/src/hicnctrl.c @@ -354,7 +354,7 @@ parse_options(int argc, char *argv[], hc_command_t * command) switch(command->action) { case ACTION_CREATE: if ((argc - optind != 5) && (argc - optind != 6)) { - usage_face_create(argv[0], true, false); + usage_face_create(argv[0], true, false); goto ERR_PARAM; } /* NAME will be autogenerated (and currently not used) */ -- cgit 1.2.3-korg